/** * * @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); }
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); }
DoubleMatrix ma = getAMatrix(f, invSigmaSqr); DoubleMatrix d = getDiffMatrix(sizes, differenceOrder[i], i); ma = (DoubleMatrix) _algebra.add(ma, _algebra.scale(d, lambda[i]));
/** * Generalised least square with penalty on (higher-order) finite differences of weights. * @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 * @param lambda strength of penalty function * @param differenceOrder difference order between weights used in penalty function * @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, double lambda, int differenceOrder) { ArgChecker.notEmpty(x, "empty measurement points"); ArgChecker.notEmpty(y, "empty measurement values"); ArgChecker.notEmpty(sigma, "empty measurement errors"); ArgChecker.notEmpty(basisFunctions, "empty basisFunctions"); int n = x.size(); ArgChecker.isTrue(n > 0, "no data"); ArgChecker.isTrue(y.size() == n, "y wrong length"); ArgChecker.isTrue(sigma.size() == n, "sigma wrong length"); ArgChecker.isTrue(lambda >= 0.0, "negative lambda"); ArgChecker.isTrue(differenceOrder >= 0, "difference order"); return solveImp(x, y, sigma, basisFunctions, lambda, differenceOrder); }
private DoubleMatrix getDiffMatrix(int[] size, int k, int indices) { int dim = size.length; DoubleMatrix d = getDiffMatrix(size[indices], k); int preProduct = 1; int postProduct = 1; for (int j = indices + 1; j < dim; j++) { preProduct *= size[j]; } for (int j = 0; j < indices; j++) { postProduct *= size[j]; } DoubleMatrix temp = d; if (preProduct != 1) { temp = (DoubleMatrix) _algebra.kroneckerProduct(DoubleMatrix.identity(preProduct), temp); } if (postProduct != 1) { temp = (DoubleMatrix) _algebra.kroneckerProduct(temp, DoubleMatrix.identity(postProduct)); } return temp; }
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); } }
DoubleMatrix ma = getAMatrix(f, invSigmaSqr); DoubleMatrix d = getDiffMatrix(m, differenceOrder); ma = (DoubleMatrix) _algebra.add(ma, _algebra.scale(d, lambda));
/** * Generalised least square with penalty on (higher-order) finite differences of weights. * @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 * @param lambda strength of penalty function * @param differenceOrder difference order between weights used in penalty function * @return the results of the least square */ public <T> GeneralizedLeastSquareResults<T> solve( T[] x, double[] y, double[] sigma, List<Function<T, Double>> basisFunctions, double lambda, int differenceOrder) { ArgChecker.notNull(x, "x null"); ArgChecker.notNull(y, "y null"); ArgChecker.notNull(sigma, "sigma null"); ArgChecker.notEmpty(basisFunctions, "empty basisFunctions"); int n = x.length; ArgChecker.isTrue(n > 0, "no data"); ArgChecker.isTrue(y.length == n, "y wrong length"); ArgChecker.isTrue(sigma.length == n, "sigma wrong length"); ArgChecker.isTrue(lambda >= 0.0, "negative lambda"); ArgChecker.isTrue(differenceOrder >= 0, "difference order"); List<T> lx = Lists.newArrayList(x); List<Double> ly = Lists.newArrayList(Doubles.asList(y)); List<Double> lsigma = Lists.newArrayList(Doubles.asList(sigma)); return solveImp(lx, ly, lsigma, basisFunctions, 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]); } } }
/** * * @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); }
ArgChecker.isTrue(differenceOrder[i] >= 0, "difference order"); return solveImp(x, y, sigma, basisFunctions, sizes, lambda, differenceOrder);
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); } }
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); }
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]); } } }
/** * 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); }
List<Function<Double, Double>> basisFuncsLog = generator.generateSet(BasisFunctionKnots.fromUniform(-5, 3, 100, 3)); final GeneralizedLeastSquare gls = new GeneralizedLeastSquare(); 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();
sizes[i] = nKnots[i] + degree[i] - 1; return _gls.solve(x, y, sigma, bSplines, sizes, lambda, differenceOrder);
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());