2
$\begingroup$

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:

Ninja Trader method : 165.034571428572
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 ; )

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Gordon Mar 1 '16 at 11:25
  • $\begingroup$ Still, there is more one to implement this algorithm and I suspect a difference in method results in a rounding error. $\endgroup$ – Bob Jansen Mar 1 '16 at 13:41
3
$\begingroup$

It is a common problem due to floating point arithmetics. For example you cast variables to double type, whereas Excel uses a limited precision.

Here you can find more info about the numeric precision in Excel: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel

I don't know anything about NinjaTrader, but I think the results are slightly different from your implementation because of the precision used.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.