GeneralizedLeastSquare { public <T> GeneralizedLeastSquareResults<T> solve(final T[] x, final double[] y, final double[] sigma, final List<Function1D<T, Double>> basisFunctions) { return solve(x, y, sigma, basisFunctions, 0.0, 0); } GeneralizedLeastSquare(); GeneralizedLeastSquareResults<T> solve(final T[] x, final double[] y, final double[] sigma, final List<Function1D<T, Double>> basisFunctions); GeneralizedLeastSquareResults<T> solve(final T[] x, final double[] y, final double[] sigma, final List<Function1D<T, Double>> basisFunctions, final double lambda,
      final int differenceOrder); GeneralizedLeastSquareResults<T> solve(final List<T> x, final List<Double> y, final List<Double> sigma, final List<Function1D<T, Double>> basisFunctions); GeneralizedLeastSquareResults<T> solve(final List<T> x, final List<Double> y, final List<Double> sigma, final List<Function1D<T, Double>> basisFunctions, final double lambda,
      final int differenceOrder); GeneralizedLeastSquareResults<T> solve(final List<T> x, final List<Double> y, final List<Double> sigma, final List<Function1D<T, Double>> basisFunctions, final int[] sizes,
      final double[] lambda, final int[] differenceOrder);  }
@Test public void testPerfectFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X, Y, SIGMA, SIN_FUNCTIONS); assertEquals(0.0, results.getChiSq(), 1e-8); final DoubleMatrix1D w = results.getFitParameters(); for (int i = 0; i < WEIGHTS.length; i++) { assertEquals(WEIGHTS[i], w.getEntry(i), 1e-8); } }
@Test public void testPerfectFitVector() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X_TRIG, Y_TRIG, SIGMA_TRIG, VECTOR_TRIG_FUNCTIONS); assertEquals(0.0, results.getChiSq(), 1e-8); final DoubleMatrix1D w = results.getFitParameters(); for (int i = 0; i < WEIGHTS.length; i++) { assertEquals(WEIGHTS[i], w.getEntry(i), 1e-8); } }
@Test public void testFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final double[] y = new double[Y.length]; for (int i = 0; i < Y.length; i++) { y[i] = Y[i] + SIGMA[i] * NORMAL.nextRandom(); } final LeastSquareResults results = gls.solve(X, y, SIGMA, SIN_FUNCTIONS); assertTrue(results.getChiSq() < 3 * Y.length); }
@Test public void testBSplineFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X, Y, SIGMA, BASIS_FUNCTIONS); final Function1D<Double, Double> spline = new BasisFunctionAggregation<>(BASIS_FUNCTIONS, results.getFitParameters().getData()); assertEquals(0.0, results.getChiSq(), 1e-12); assertEquals(-0.023605293, spline.evaluate(0.5), 1e-8); if (PRINT) { System.out.println("Chi^2:\t" + results.getChiSq()); System.out.println("weights:\t" + results.getFitParameters()); for (int i = 0; i < 101; i++) { final double x = 0 + i * 2.0 / 100.0; System.out.println(x + "\t" + spline.evaluate(x)); } for (int i = 0; i < X.length; i++) { System.out.println(X[i] + "\t" + Y[i]); } } }
@Test public void testBSplineFit2D() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X_COS_EXP, Y_COS_EXP, SIGMA_COS_EXP, BASIS_FUNCTIONS_2D); final Function1D<double[], Double> spline = new BasisFunctionAggregation<>(BASIS_FUNCTIONS_2D, results.getFitParameters().getData()); assertEquals(0.0, results.getChiSq(), 1e-16); assertEquals(0.05161579, spline.evaluate(new double[] {4, 3 }), 1e-8); if (PRINT) { System.out.println("Chi^2:\t" + results.getChiSq()); System.out.println("weights:\t" + results.getFitParameters()); final double[] x = new double[2]; for (int i = 0; i < 101; i++) { x[0] = 0 + i * 10.0 / 100.0; System.out.print("\t" + x[0]); } System.out.print("\n"); for (int i = 0; i < 101; i++) { x[0] = -0. + i * 10 / 100.0; System.out.print(x[0]); for (int j = 0; j < 101; j++) { x[1] = -0.0 + j * 10.0 / 100.0; final double y = spline.evaluate(x); System.out.print("\t" + y); } System.out.print("\n"); } } }
@Test public void testPSplineFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final GeneralizedLeastSquareResults<Double> results = gls.solve(X, Y, SIGMA, BASIS_FUNCTIONS, 1000.0, 2); final Function1D<Double, Double> spline = results.getFunction(); assertEquals(2225.7, results.getChiSq(), 1e-1); assertEquals(-0.758963811327287, spline.evaluate(1.1), 1e-8); if (PRINT) { System.out.println("Chi^2:\t" + results.getChiSq()); System.out.println("weights:\t" + results.getFitParameters()); for (int i = 0; i < 101; i++) { final double x = 0 + i * 2.0 / 100.0; System.out.println(x + "\t" + spline.evaluate(x)); } for (int i = 0; i < X.length; i++) { System.out.println(X[i] + "\t" + Y[i]); } } }
@Test public void testPSplineFit2() { final BasisFunctionGenerator generator = new BasisFunctionGenerator(); List<Function1D<Double, Double>> basisFuncs = generator.generateSet(0, 12, 100, 3); List<Function1D<Double, Double>> basisFuncsLog = generator.generateSet(-5, 3, 100, 3); final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final Interpolator1D interpolator = new CombinedInterpolatorExtrapolator(new DoubleQuadraticInterpolator1D(), new FlatExtrapolator1D()); final double[] xData = new double[] {7. / 365, 14 / 365., 21 / 365., 1 / 12., 3 / 12., 0.5, 0.75, 1, 5, 10 }; final double[] yData = new double[] {0.972452371, 0.749039802, 0.759792085, 0.714206462, 0.604446956, 0.517955313, 0.474807307, 0.443532132, 0.2404755, 0.197128583, }; final int n = xData.length; final double[] lnX = new double[n]; final double[] yData2 = new double[n]; for (int i = 0; i < n; i++) { lnX[i] = Math.log(xData[i]); yData2[i] = yData[i] * yData[i] * xData[i]; } Interpolator1DDataBundle db = interpolator.getDataBundle(xData, yData); Interpolator1DDataBundle dbLog = interpolator.getDataBundle(lnX, yData); Interpolator1DDataBundle dbVar = interpolator.getDataBundle(xData, yData2); Interpolator1DDataBundle dbVarLog = interpolator.getDataBundle(lnX, yData2); final double[] sigma = new double[n]; Arrays.fill(sigma, 0.01); final GeneralizedLeastSquareResults<Double> results = gls.solve(xData, yData, sigma, basisFuncs, 1000.0, 2); final Function1D<Double, Double> spline = results.getFunction(); final GeneralizedLeastSquareResults<Double> resultsLog = gls.solve(lnX, yData, sigma, basisFuncsLog, 1000.0, 2); final Function1D<Double, Double> splineLog = resultsLog.getFunction(); final GeneralizedLeastSquareResults<Double> resultsVar = gls.solve(xData, yData2, sigma, basisFuncs, 1000.0, 2); final Function1D<Double, Double> splineVar = resultsVar.getFunction(); final GeneralizedLeastSquareResults<Double> resultsVarLog = gls.solve(lnX, yData2, sigma, basisFuncsLog, 1000.0, 2); final Function1D<Double, Double> splineVarLog = resultsVarLog.getFunction(); if (PRINT) { System.out.println("Chi^2:\t" + results.getChiSq()); System.out.println("weights:\t" + results.getFitParameters()); for (int i = 0; i < 101; i++) { final double logX = -5 + 8 * i / 100.; final double x = Math.exp(logX); System.out.println(x + "\t" + +logX + "\t" + spline.evaluate(x) + "\t" + interpolator.interpolate(db, x) + "\t" + splineLog.evaluate(logX) + "\t" + interpolator.interpolate(dbLog, logX) + "\t" + splineVar.evaluate(x) + "\t" + interpolator.interpolate(dbVar, x) + "\t" + splineVarLog.evaluate(logX) + "\t" + interpolator.interpolate(dbVarLog, logX)); } for (int i = 0; i < n; i++) { System.out.println(lnX[i] + "\t" + yData[i]); } } }
@Test public void testPSplineFit2D() { final PSplineFitter psf = new PSplineFitter(); final GeneralizedLeastSquareResults<double[]> results = psf.solve(X_COS_EXP, Y_COS_EXP, SIGMA_COS_EXP, new double[] {0.0, 0.0 }, new double[] {10.0, 10.0 }, new int[] {10, 10 }, new int[] {3, 3 }, new double[] {0.001, 0.001 }, new int[] {3, 3 }); assertEquals(0.0, results.getChiSq(), 1e-9); final Function1D<double[], Double> spline = results.getFunction(); assertEquals(0.4635978895963084, spline.evaluate(new double[] {4, 3 }), 1e-8); if (PRINT) { System.out.println("Chi^2:\t" + results.getChiSq()); System.out.println("weights:\t" + results.getFitParameters()); final double[] x = new double[2]; for (int i = 0; i < 101; i++) { x[0] = 0 + i * 10.0 / 100.0; System.out.print("\t" + x[0]); } System.out.print("\n"); for (int i = 0; i < 101; i++) { x[0] = -0. + i * 10 / 100.0; System.out.print(x[0]); for (int j = 0; j < 101; j++) { x[1] = -0.0 + j * 10.0 / 100.0; final double y = spline.evaluate(x); System.out.print("\t" + y); } System.out.print("\n"); } } }