# Linear Regession 3 methods different results

Morning,

So I use a package called Ninja Trader that has a linear regression method, I have also written my own method and compared the results to excels linear regression method. All three are giving different results and I an trying to understand why.

If we look at one example maybe someone will be able to shed some light one why this is.

EXAMPLE

Y vals 165.05 165.02 165.03 165.07 165.02 165.07 165.04 165.03 165.02 165.01 165 165.02 165.03 165.02 165.02 165.01 165.03 165.04 165.04 165.07

x vals 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

The results I have from the three different methods are:

My own method:        165.036842105263
Excels method:        165.025578947368000


Here is My linea regression method, in C#:

public static double LinearRegression(double[] xVals, double[] yVals, int inclusiveStart, int inclusiveEnd)
{

double sumOfX = 0;
double sumOfY = 0;
double sumOfXSq = 0;
double sumOfYSq = 0;
double ssX = 0;
double ssY = 0;
double sumCodeviates = 0;
double sCo = 0;
double count = xVals.Length;
double x;
double y;

for (int ctr = inclusiveStart; ctr < count; ctr++)
{
x = xVals[ctr];
y = yVals[ctr];
sumCodeviates += x * y;
sumOfX += x;
sumOfY += y;
sumOfXSq += x * x;
sumOfYSq += y * y;
}

double slope = ((count * sumCodeviates) - (sumOfX * sumOfY)) / ( (count * sumOfXSq) - (sumOfX * sumOfX));
double yIntercept = ((sumOfXSq * sumOfY) - (sumOfX * sumCodeviates)) / ((count * sumOfXSq) - (sumOfX * sumOfX));

return yIntercept + slope * (xVals.Length + 1);
}


I hope that someone can help me out getting to the bottom of this. Thx ; )

• Yes I can see that being an issue with Excel but the ninjatarder Method is written in c# and also uses Doubles. I have written to ninja trader so lets see what they come back with. Thanks for the help. – Gordon Mar 1 '16 at 11:25
• Still, there is more one to implement this algorithm and I suspect a difference in method results in a rounding error. – Bob Jansen Mar 1 '16 at 13:41