PolynomialsLeastSquaresFitter { public LeastSquaresRegressionResult regress(final double[] xData, final double[] yData, final int degree) { return regress(xData, yData, degree, false); }  LeastSquaresRegressionResult regress(final double[] xData, final double[] yData, final int degree); PolynomialsLeastSquaresFitterResult regressVerbose(final double[] xData, final double[] yData, final int degree, final boolean normalize);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void DataShortTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 6; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1 }; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void MinusDegreeTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = -4; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1 }; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void WrongDataLengthTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1, 2, 3, 5, 6 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 1, 2 }; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void RepeatDataTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1, 2, 3, 1, 1 }; final double[] yValues = new double[] {1, 2, 3, 4, 2, 2 }; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void ExtremeValueTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1e-307, 2e-307, 3e18, 4 }; final double[] yValues = new double[] {1, 2, 3, 4, 5 }; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; xValues = null; yValues = null; regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfinityTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; final double zero = 0.; for (int i = 0; i < nPts; ++i) { xValues[i] = 1. / zero; yValues[i] = 1. / zero; } regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final int nPts = 5; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; for (int i = 0; i < nPts; ++i) { xValues[i] = Double.NaN; yValues[i] = Double.NaN; } regObj.regress(xValues, yValues, degree); }
@Test(expectedExceptions = IllegalArgumentException.class) public void LargeNumberTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; double[] xValues = new double[] {1, 2, 3, 4e2, 5, 6, 7 }; double[] yValues = new double[] {1, 2, 3, 4, 5, 6, 7 }; regObj.regress(xValues, yValues, degree); }
@Test(enabled = false) public void RandomDataFitPrintTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 6; final int nPts = 7; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; for (int i = 0; i < nPts; ++i) { xValues[i] = 10. * (randObj.nextDouble() - .5); yValues[i] = 10. * (randObj.nextDouble() - .5); System.out.println(xValues[i] + "\t" + yValues[i]); } System.out.println("\n"); LeastSquaresRegressionResult result = regObj.regress(xValues, yValues, degree); final double[] coeffResult = result.getBetas(); final DoubleFunction1D func = new RealPolynomialFunction1D(coeffResult); for (int i = 0; i < 100; ++i) { final double k = -5. + 10. * i / 100.; System.out.println(k + "\t" + func.evaluate(k)); } System.out.println("\n"); final double[] resResult = result.getResiduals(); double resSumSqHalf = 0.; for (int i = 0; i < nPts; ++i) { resSumSqHalf += 0.5 * resResult[i] * resResult[i]; System.out.println(resResult[i]); } System.out.println("\n"); System.out.println("chisq: " + "\t" + resSumSqHalf); }