PolynomialsLeastSquaresFitter { public PolynomialsLeastSquaresFitterResult regressVerbose(final double[] xData, final double[] yData, final int degree, final boolean normalize) { final LeastSquaresRegressionResult result = regress(xData, yData, degree, normalize); final int nData = xData.length; final DoubleMatrix2D rMatriX = _qrResult.getR(); final DoubleMatrix1D resResult = new DoubleMatrix1D(result.getResiduals()); final double resNorm = OG_ALGEBRA.getNorm2(resResult); if (normalize == true) { return new PolynomialsLeastSquaresFitterResult(result.getBetas(), rMatriX, nData - degree - 1, resNorm, _renorm); } return new PolynomialsLeastSquaresFitterResult(result.getBetas(), rMatriX, nData - degree - 1, resNorm); }  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 public void RmatrixTest() { final PolynomialsLeastSquaresFitter regObj1 = new PolynomialsLeastSquaresFitter(); final double[] xValues = new double[] {-1., 0, 1. }; final double[] yValues = new double[] {1., 0, 1. }; final double[][] rMatrix = new double[][] { {-Math.sqrt(3.), 0., -2. / Math.sqrt(3.) }, {0., -Math.sqrt(2.), 0. }, {0., 0., -Math.sqrt(2. / 3.) } }; final int degree = 2; PolynomialsLeastSquaresFitterResult resultVer = regObj1.regressVerbose(xValues, yValues, degree, false); double[][] rMatResult = resultVer.getRMat().getData(); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { assertEquals(rMatrix[i][j], rMatResult[i][j], EPS); } } final PolynomialsLeastSquaresFitter regObj2 = new PolynomialsLeastSquaresFitter(); PolynomialsLeastSquaresFitterResult resultNorm = regObj2.regressVerbose(xValues, yValues, degree, true); rMatResult = resultNorm.getRMat().getData(); for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { assertEquals(rMatrix[i][j], rMatResult[i][j], EPS); } } }
@Test(expectedExceptions = IllegalArgumentException.class) public void NormalisationErrorTest() { 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 }; PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValues, yValues, degree, false); result.getMeanAndStd(); }
@Test(expectedExceptions = IllegalArgumentException.class) public void DataShortVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void DataShortVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void MinusDegreeVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void MinusDegreeVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void WrongDataLengthVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void WrongDataLengthVerboseTureTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void RepeatDataVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void RepeatDataVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void ExtremeValueVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void ExtremeValueVerboseTrueAlphaTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 4; final double[] xValues = new double[] {0, 1e-307, 2e-307, 3e-307, 4 }; final double[] yValues = new double[] {1, 2, 3, 4, 5 }; regObj.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NullVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfinityVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void InfinityVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void NaNVerboseTrueTest() { 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.regressVerbose(xValues, yValues, degree, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void LargeNumberVerboseFalseTest() { 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.regressVerbose(xValues, yValues, degree, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void LargeNumberVerboseTrueTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 6; double[] xValues = new double[] {1, 2, 3, 4e17, 5, 6, 7 }; double[] yValues = new double[] {1, 2, 3, 4, 5, 6, 7 }; regObj.regressVerbose(xValues, yValues, degree, true); }
@Test(enabled = false) public void PolynomialFunctionFitPrintTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final double[] coeff = new double[] {-(randObj.nextDouble() + 1.), (randObj.nextDouble() + 1.), -(randObj.nextDouble() + 1.), (randObj.nextDouble() + 1.) }; final DoubleFunction1D func = new RealPolynomialFunction1D(coeff); final int degree = coeff.length - 1; final int nPts = 7; double[] xValues = new double[nPts]; double[] yValues = new double[nPts]; for (int i = 0; i < nPts; ++i) { xValues[i] = -5. + 10 * i / (nPts - 1); yValues[i] = func.evaluate(xValues[i]); } PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValues, yValues, degree, false); final double[] coeffResult = result.getCoeff(); System.out.println("xValues"); for (int i = 0; i < nPts; ++i) { System.out.print(xValues[i] + ","); } System.out.println("\n"); System.out.println("yValues"); for (int i = 0; i < nPts; ++i) { System.out.print(yValues[i] + ","); } System.out.println("\n"); System.out.println("true coeffs"); for (int i = 0; i < degree + 1; ++i) { System.out.print(coeff[degree - i] + ","); } System.out.println("\n"); System.out.println("fit coeffs"); for (int i = 0; i < degree + 1; ++i) { System.out.print(coeffResult[degree - i] + ","); } System.out.println("\n"); System.out.println("fit norm"); System.out.println(result.getDiffNorm()); System.out.println("\n"); System.out.println("fit dof"); System.out.println(result.getDof()); final DoubleMatrix2D rMatrix = result.getRMat(); final double[][] rMatrixDoub = rMatrix.getData(); for (int i = 0; i < degree + 1; ++i) { for (int j = 0; j < degree + 1; ++j) { System.out.print(rMatrixDoub[i][j] + "\t"); } System.out.print("\n"); } }
@Test(enabled = false) public void PolynomialFunctionFitPrintTest2() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final double[] coeff = new double[] {-1.9564385860928322, 1.968428061753627, -1.8042487762604558, 1.1347030699838965, 2.1347030699838965 }; final DoubleFunction1D func = new RealPolynomialFunction1D(coeff); final int degree = coeff.length - 1; final int nPts = 7; double[] xValues = new double[] {-2.0, -1.0, -.5, 0.0, 1.0, 2.0, 3.0 }; final double mean = 0.357142857142857; final double std = 1.749149453169685; double[] xValuesNom = new double[nPts]; double[] yValues = new double[nPts]; for (int i = 0; i < nPts; ++i) { xValuesNom[i] = xValues[i] / std - mean / std; yValues[i] = func.evaluate(xValuesNom[i]); } PolynomialsLeastSquaresFitterResult resultNom = regObj.regressVerbose(xValues, yValues, degree, true); PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValuesNom, yValues, degree, false); final double[] coeffResultNom = resultNom.getCoeff(); final double[] coeffResult = result.getCoeff(); System.out.println("true coeffs"); for (int i = 0; i < degree + 1; ++i) { assertEquals(coeffResultNom[i], coeffResult[i], EPS * Math.abs(coeff[i])); } System.out.println("xValues"); for (int i = 0; i < nPts; ++i) { System.out.print(xValues[i] + ","); } System.out.println("\n"); System.out.println("yValues"); for (int i = 0; i < nPts; ++i) { System.out.print(yValues[i] + ","); } System.out.println("\n"); System.out.println("true coeffs"); for (int i = 0; i < degree + 1; ++i) { System.out.print(coeff[degree - i] + ","); } System.out.println("\n"); System.out.println("fit coeffs"); for (int i = 0; i < degree + 1; ++i) { System.out.print(coeffResult[degree - i] + ","); } System.out.println("\n"); System.out.println("fit norm"); System.out.println(result.getDiffNorm()); System.out.println("\n"); System.out.println("fit dof"); System.out.println(result.getDof()); final DoubleMatrix2D rMatrix = result.getRMat(); final double[][] rMatrixDoub = rMatrix.getData(); for (int i = 0; i < degree + 1; ++i) { for (int j = 0; j < degree + 1; ++j) { System.out.print(rMatrixDoub[i][j] + "\t"); } System.out.print("\n"); } System.out.print("\n"); System.out.println("Norm of rMatrix"); System.out.println(OG_ALGEBRA.getNorm2(rMatrix)); System.out.println("\n"); }
@Test (enabled = false) public void GeneralTest() { final PolynomialsLeastSquaresFitter regObj = new PolynomialsLeastSquaresFitter(); final int degree = 6; double[] xValues = new double[] {1, 2, 3, 4e16, 5, 6, 7 }; double[] yValues = new double[] {1, 2, 3, 4, 5, 6, 7 }; PolynomialsLeastSquaresFitterResult result = regObj.regressVerbose(xValues, yValues, degree, true); final double[] coeffResult = result.getCoeff(); final double res = result.getDiffNorm(); for (int i = 0; i < degree + 1; ++i) { System.out.println(coeffResult[i]); } System.out.println(res); }