[chronojump] New method for LeastSquares linear regression



commit 17cffacc50dd8210ac4ed638344ddeef955cb793
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Jan 10 18:39:29 2020 +0100

    New method for LeastSquares linear regression

 src/gui/app1/chronojump.cs | 16 +++++++++++----
 src/utilMath.cs            | 49 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 6 deletions(-)
---
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index 0216483c..e10a15c8 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -732,12 +732,20 @@ public partial class ChronoJumpWindow
                //Json js = new Json();
                //js.UploadEncoderData();
 
-               //LeastSquaresParabole test
                /*
-               LeastSquaresParabole ls = new LeastSquaresParabole();
-               ls.Test();
-               LogB.Information(string.Format("coef = {0} {1} {2}", ls.Coef[0], ls.Coef[1], ls.Coef[2]));
+               //LeastSquaresParabole tests
+
+               //a) straight line:
+               LeastSquaresLine lsl = new LeastSquaresLine();
+               lsl.Test();
+               LogB.Information(string.Format("slope = {0}; intercept = {1}", lsl.Slope, lsl.Intercept));
+
+               //b) LeastSquaresParabole test
+               LeastSquaresParabole lsp = new LeastSquaresParabole();
+               lsp.Test();
+               LogB.Information(string.Format("coef = {0} {1} {2}", lsp.Coef[0], lsp.Coef[1], lsp.Coef[2]));
                */
+
                //new VersionCompareTests();
                if(configChronojump.PlaySoundsFromFile)
                {
diff --git a/src/utilMath.cs b/src/utilMath.cs
index ed1e187f..5b2b06a1 100644
--- a/src/utilMath.cs
+++ b/src/utilMath.cs
@@ -24,8 +24,8 @@ using System.Collections.Generic; //List<T>
 
 public class Point
 {
-       double x;
-       double y;
+       private double x;
+       private double y;
 
        public Point(double x, double y) 
        {
@@ -48,6 +48,51 @@ public class Point
 
 }
 
+public class LeastSquaresLine
+{
+       //public double [] Coef;        //indep, x
+       public double Slope;
+       public double Intercept;
+
+       public LeastSquaresLine() {
+               Slope = 0;
+               Intercept = 0;
+       }
+
+       public void Test()
+       {
+               List<Point> measures = new List<Point> {
+                       new Point(1, 10.3214), new Point(2, 13.3214), new Point(3, 18.3214) };
+               Calculate(measures);
+       }
+
+       public void Calculate(List<Point> measures)
+       {
+               int n = measures.Count;
+               double sumX = 0; //sumatory of the X values
+               double sumY = 0; //sumatory of the Y values
+               double sumX2 = 0; //sumatory of the squared X values
+               double sumXY = 0; //sumatory of the squared X values
+
+               //for(int i = 0; i < numMeasures; i++)
+               foreach(Point p in measures)
+               {
+                       sumX = sumX + p.X;
+                       sumY = sumY + p.Y;
+                       sumX2 = sumX2 + p.X * p.X;
+                       sumXY = sumXY + p.X * p.Y;
+               }
+
+               Slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
+               Intercept = (sumX2 * sumY - sumX * sumXY) / (sumX2 * n - sumX * sumX);
+
+               /*
+               double [] yFit = new double[numMeasures];
+               for (int i = 0; i < n; i++)
+                       yFit[i] = slope * x[i] + intercept;
+                       */
+       }
+}
 
 public class LeastSquaresParabole 
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]