/** * * @param <T> The type of the independent variables (e.g. Double, double[], DoubleArray etc) * @param x independent variables * @param y dependent (scalar) variables * @param sigma (Gaussian) measurement error on dependent variables * @param basisFunctions set of basis functions - the fitting function is formed by these basis functions times a set of weights * @return the results of the least square */ public <T> GeneralizedLeastSquareResults<T> solve( T[] x, double[] y, double[] sigma, List<Function<T, Double>> basisFunctions) { return solve(x, y, sigma, basisFunctions, 0.0, 0); }
/** * * @param <T> The type of the independent variables (e.g. Double, double[], DoubleArray etc) * @param x independent variables * @param y dependent (scalar) variables * @param sigma (Gaussian) measurement error on dependent variables * @param basisFunctions set of basis functions - the fitting function is formed by these basis functions times a set of weights * @return the results of the least square */ public <T> GeneralizedLeastSquareResults<T> solve( List<T> x, List<Double> y, List<Double> sigma, List<Function<T, Double>> basisFunctions) { return solve(x, y, sigma, basisFunctions, 0.0, 0); }
GeneralizedLeastSquareResults<Double> solve( double[] x, double[] y, double[] sigma, List<Function<Double, Double>> basisFunctions, double lambda, int differenceOrder) { return solve(DoubleArrayMath.toObject(x), y, sigma, basisFunctions, lambda, differenceOrder); }
/** * Fits a curve to x-y data. * @param x The independent variables * @param y The dependent variables * @param sigma The error (or tolerance) on the y variables * @param xa The lowest value of x * @param xb The highest value of x * @param nKnots Number of knots (note, the actual number of basis splines and thus fitted weights, equals nKnots + degree-1) * @param degree The degree of the basis function - 0 is piecewise constant, 1 is a sawtooth function (i.e. two straight lines joined in the middle), 2 gives three * quadratic sections joined together, etc. For a large value of degree, the basis function tends to a gaussian * @param lambda The weight given to the penalty function * @param differenceOrder applies the penalty the nth order difference in the weights, so a differenceOrder of 2 will penalise large 2nd derivatives etc * @return The results of the fit */ public GeneralizedLeastSquareResults<Double> solve(List<Double> x, List<Double> y, List<Double> sigma, double xa, double xb, int nKnots, int degree, double lambda, int differenceOrder) { List<Function<Double, Double>> bSplines = _generator.generateSet(BasisFunctionKnots.fromUniform(xa, xb, nKnots, degree)); return _gls.solve(x, y, sigma, bSplines, lambda, differenceOrder); }
sizes[i] = nKnots[i] + degree[i] - 1; return _gls.solve(x, y, sigma, bSplines, sizes, lambda, differenceOrder);
public void testPSplineFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final GeneralizedLeastSquareResults<Double> results = gls.solve(X, Y, SIGMA, BASIS_FUNCTIONS, 1000.0, 2); final Function<Double, Double> spline = results.getFunction(); assertEquals(2225.7, results.getChiSq(), 1e-1); assertEquals(-0.758963811327287, spline.apply(1.1), 1e-8); /* * Print out function for debugging */ 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.apply(x)); } for (int i = 0; i < X.length; i++) { System.out.println(X[i] + "\t" + Y[i]); } } }
final GeneralizedLeastSquareResults<Double> results = gls.solve(xData, yData, sigma, basisFuncs, 1000.0, 2); final Function<Double, Double> spline = results.getFunction(); final GeneralizedLeastSquareResults<Double> resultsLog = gls.solve(lnX, yData, sigma, basisFuncsLog, 1000.0, 2); final Function<Double, Double> splineLog = resultsLog.getFunction(); final GeneralizedLeastSquareResults<Double> resultsVar = gls.solve(xData, yData2, sigma, basisFuncs, 1000.0, 2); final Function<Double, Double> splineVar = resultsVar.getFunction(); final GeneralizedLeastSquareResults<Double> resultsVarLog = gls.solve(lnX, yData2, sigma, basisFuncsLog, 1000.0, 2); final Function<Double, Double> splineVarLog = resultsVarLog.getFunction();
public void testBSplineFit() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X, Y, SIGMA, BASIS_FUNCTIONS); final Function<Double, Double> spline = new BasisFunctionAggregation<>(BASIS_FUNCTIONS, results.getFitParameters().toArray()); assertEquals(0.0, results.getChiSq(), 1e-12); assertEquals(-0.023605293, spline.apply(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.apply(x)); } for (int i = 0; i < X.length; i++) { System.out.println(X[i] + "\t" + Y[i]); } } }
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); }
public void testBSplineFit2D() { final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); final LeastSquareResults results = gls.solve(X_SIN_EXP, Y_SIN_EXP, SIGMA_COS_EXP, BASIS_FUNCTIONS_2D); final Function<double[], Double> spline = new BasisFunctionAggregation<>(BASIS_FUNCTIONS_2D, results.getFitParameters().toArray());
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 DoubleArray w = results.getFitParameters(); for (int i = 0; i < WEIGHTS.length; i++) { assertEquals(WEIGHTS[i], w.get(i), 1e-8); } }
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 DoubleArray w = results.getFitParameters(); for (int i = 0; i < WEIGHTS.length; i++) { assertEquals(WEIGHTS[i], w.get(i), 1e-8); } }