Diagnosing Differences Between Linear Regression Implementations
Summary
The document compares three reported outputs for a linear regression calculation: a trading platform’s method, a custom C# routine, and a spreadsheet function. The accompanying reply attributes small discrepancies to floating-point precision and notes that software packages may use different numeric precision. It provides no controlled comparison or evidence that precision alone explains the reported differences.
The displayed routine also makes its own choices about which observations to include and which x-coordinate to evaluate, so matching the input range, indexing, and requested output across implementations is necessary before diagnosing a numerical precision issue. The post does not establish whether all three calculations fit the same sample or evaluate the same point. Its practical lesson is to check conventions and implementation details as well as arithmetic precision; the proposed explanation remains tentative, and the example does not isolate the cause.
Key ideas
- Different software tools can produce different regression outputs when their numerical precision differs.
- The comparison should confirm that every implementation uses the same observations and index range.
- The custom routine evaluates the fitted line at a chosen x-coordinate, which affects the returned value.
- The reply offers floating-point arithmetic as a possible cause but does not demonstrate it.
Tags
Full text
# Linear Regession 3 methods different results
# 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:
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 ; )
## Answer by stochazesthai (score 3)
https://quant.stackexchange.com/a/24640
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.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.