public void test1dLinearRegression() { //BOOST_MESSAGE("Testing 1d simple linear least-squares regression..."); /* Example taken from the QuantLib-User list, see posting * Multiple linear regression/weighted regression, Boris Skorodumov */ //SavedSettings backup; List <double> x = new InitializedList <double>(9), y = new InitializedList <double>(9); x[0] = 2.4; x[1] = 1.8; x[2] = 2.5; x[3] = 3.0; x[4] = 2.1; x[5] = 1.2; x[6] = 2.0; x[7] = 2.7; x[8] = 3.6; y[0] = 7.8; y[1] = 5.5; y[2] = 8.0; y[3] = 9.0; y[4] = 6.5; y[5] = 4.0; y[6] = 6.3; y[7] = 8.4; y[8] = 10.2; List <Func <double, double> > v = new List <Func <double, double> >(); v.Add(a => 1.0); v.Add(a => a); LinearRegression m = new LinearRegression(x, y); const double tol = 0.0002; double[] coeffExpected = new double[] { 0.9448, 2.6853 }; double[] errorsExpected = new double[] { 0.3654, 0.1487 }; for (int i = 0; i < 2; ++i) { if (Math.Abs(m.standardErrors()[i] - errorsExpected[i]) > tol) { Assert.Fail("Failed to reproduce linear regression standard errors" + "\n calculated: " + m.standardErrors()[i] + "\n expected: " + errorsExpected[i] + "\n tolerance: " + tol); } if (Math.Abs(m.coefficients()[i] - coeffExpected[i]) > tol) { Assert.Fail("Failed to reproduce linear regression coef." + "\n calculated: " + m.coefficients()[i] + "\n expected: " + coeffExpected[i] + "\n tolerance: " + tol); } } }
public void test1dLinearRegression() { //BOOST_MESSAGE("Testing 1d simple linear least-squares regression..."); /* Example taken from the QuantLib-User list, see posting * Multiple linear regression/weighted regression, Boris Skorodumov */ //SavedSettings backup; List<double> x = new InitializedList<double>(9), y = new InitializedList<double>(9); x[0] = 2.4; x[1] = 1.8; x[2] = 2.5; x[3] = 3.0; x[4] = 2.1; x[5] = 1.2; x[6] = 2.0; x[7] = 2.7; x[8] = 3.6; y[0] = 7.8; y[1] = 5.5; y[2] = 8.0; y[3] = 9.0; y[4] = 6.5; y[5] = 4.0; y[6] = 6.3; y[7] = 8.4; y[8] = 10.2; List<Func<double, double>> v = new List<Func<double, double>>(); v.Add(a => 1.0); v.Add(a => a); LinearRegression m = new LinearRegression(x, y); const double tol = 0.0002; double[] coeffExpected = new double[] { 0.9448, 2.6853 }; double[] errorsExpected = new double[] { 0.3654, 0.1487 }; for (int i = 0; i < 2; ++i) { if (Math.Abs(m.standardErrors()[i] - errorsExpected[i]) > tol) { Assert.Fail("Failed to reproduce linear regression standard errors" + "\n calculated: " + m.standardErrors()[i] + "\n expected: " + errorsExpected[i] + "\n tolerance: " + tol); } if (Math.Abs(m.coefficients()[i] - coeffExpected[i]) > tol) { Assert.Fail("Failed to reproduce linear regression coef." + "\n calculated: " + m.coefficients()[i] + "\n expected: " + coeffExpected[i] + "\n tolerance: " + tol); } } }