///<summary> ///This subroutine determines the best fit line by minimizing the sum of the squares ///of the perpendicular distances of the points to the line. ///This was initially reported by Kermack and Haldane (1950) Biometrika, 37, 30. ///However I found it in York, D. (1966) Canadian Journal of Physics, vol 44, p 1079. ///</summary> public static LinearRegression LinearRegressionWithErrorsInBothCoordinates(Statistics a, Statistics b) { double meanA = a.Mean(); double meanB = b.Mean(); double sA2 = 0; double sB2 = 0; double sAb = 0; for (int i = 0; i < a.Length; i++) { double dA = a._list[i] - meanA; double dB = b._list[i] - meanB; sA2 += dA * dA; sB2 += dB * dB; sAb += dA * dB; } LinearRegression result = new LinearRegression(); if (sA2 > 0 && sB2 > 0 && sAb > 0) { result.Correlation = sAb / Math.Sqrt(sA2 * sB2); result.Slope = (sB2 - sA2 + Math.Sqrt((sB2 - sA2) * (sB2 - sA2) + 4 * (sAb * sAb))) / 2 / sAb; result.Intercept = meanB - result.Slope * meanA; if (result.Correlation < 1) { result.SlopeError = (result.Slope / result.Correlation) * Math.Sqrt((1 - (result.Correlation * result.Correlation)) / a.Length); } else { result.SlopeError = 0; } } else { result.Correlation = 0; result.Slope = 0; result.SlopeError = 0; result.Intercept = 0; } return(result); }
public LinearRegressionEntry(double startTime, double endTime, LinearRegression linearRegression) { StartTime = startTime; EndTime = endTime; LinearRegression = linearRegression; }
///<summary> ///This subroutine determines the best fit line by minimizing the sum of the squares ///of the perpendicular distances of the points to the line. ///This was initially reported by Kermack and Haldane (1950) Biometrika, 37, 30. ///However I found it in York, D. (1966) Canadian Journal of Physics, vol 44, p 1079. ///</summary> public static LinearRegression LinearRegressionWithErrorsInBothCoordinates(Statistics a, Statistics b) { double meanA = a.Mean(); double meanB = b.Mean(); double sA2 = 0; double sB2 = 0; double sAb = 0; for (int i = 0; i < a.Length; i++) { double dA = a._list[i] - meanA; double dB = b._list[i] - meanB; sA2 += dA * dA; sB2 += dB * dB; sAb += dA * dB; } LinearRegression result = new LinearRegression(); if (sA2 > 0 && sB2 > 0 && sAb > 0) { result.Correlation = sAb / Math.Sqrt(sA2 * sB2); result.Slope = (sB2 - sA2 + Math.Sqrt((sB2 - sA2) * (sB2 - sA2) + 4 * (sAb * sAb))) / 2 / sAb; result.Intercept = meanB - result.Slope * meanA; if (result.Correlation < 1) { result.SlopeError = (result.Slope / result.Correlation) * Math.Sqrt((1 - (result.Correlation * result.Correlation)) / a.Length); } else { result.SlopeError = 0; } } else { result.Correlation = 0; result.Slope = 0; result.SlopeError = 0; result.Intercept = 0; } return result; }