Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. private FunctionSeries calculateLinearRegression(List<DataPoint> points)
  2. {
  3. //Y = bx + a
  4.  
  5. double avgX = 0;
  6. double avgY = 0;
  7. double xySum = 0;
  8. double xSumSquared = 0;
  9.  
  10. int n = points.Count;
  11.  
  12. foreach (var item in points)
  13. {
  14. avgX += item.X;
  15. avgY += item.Y;
  16. xySum += item.X * item.Y;
  17. xSumSquared += Math.Pow(item.X, 2);
  18. }
  19.  
  20. avgX = avgX / n;
  21. avgY = avgY / n;
  22.  
  23. double b = (xySum - n * avgX * avgY) / (xSumSquared - n * Math.Pow(avgX, 2));
  24. double a = avgY - b * avgX;
  25.  
  26. Func<double, double> func = (x) => b * x + a;
  27.  
  28. return new FunctionSeries(func, -200, 200, 0.5);
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement