NonLinearLeastSquare { public LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func, final DoubleMatrix1D startPos) { Validate.notNull(x, "x"); Validate.notNull(y, "y"); final int n = x.getNumberOfElements(); Validate.isTrue(y.getNumberOfElements() == n, "y wrong length"); final double[] sigmas = new double[n]; Arrays.fill(sigmas, 1); return solve(x, y, new DoubleMatrix1D(sigmas), func, startPos); } NonLinearLeastSquare();  NonLinearLeastSquare(final Decomposition<?> decomposition, final MatrixAlgebra algebra, final double eps); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final double sigma, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final DoubleMatrix1D sigma, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func,
      final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func,
      final ParameterizedFunction<Double, DoubleMatrix1D, DoubleMatrix1D> grad, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final double sigma, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func,
      final ParameterizedFunction<Double, DoubleMatrix1D, DoubleMatrix1D> grad, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D x, final DoubleMatrix1D y, final DoubleMatrix1D sigma, final ParameterizedFunction<Double, DoubleMatrix1D, Double> func,
      final ParameterizedFunction<Double, DoubleMatrix1D, DoubleMatrix1D> grad, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D observedValues, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D observedValues, final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D observedValues, final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func, final DoubleMatrix1D startPos,
      final DoubleMatrix1D maxJumps); LeastSquareResults solve(final DoubleMatrix1D observedValues, final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func,
      final Function1D<DoubleMatrix1D, DoubleMatrix2D> jac, final DoubleMatrix1D startPos); LeastSquareResults solve(final DoubleMatrix1D observedValues, final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func,
      final Function1D<DoubleMatrix1D, DoubleMatrix2D> jac, final DoubleMatrix1D startPos, final DoubleMatrix1D maxJumps); LeastSquareResults solve(final DoubleMatrix1D observedValues, final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func,
      final Function1D<DoubleMatrix1D, DoubleMatrix2D> jac, final DoubleMatrix1D startPos, final Function1D<DoubleMatrix1D, Boolean> constraints, final DoubleMatrix1D maxJumps); DoubleMatrix2D calInverseJacobian(final DoubleMatrix1D sigma, final Function1D<DoubleMatrix1D, DoubleMatrix1D> func, final Function1D<DoubleMatrix1D, DoubleMatrix2D> jac,
      final DoubleMatrix1D originalSolution);  }
@Test public void solveExactTest() { final DoubleMatrix1D start = new DoubleMatrix1D(new double[] {1.2, 0.8, -0.2, -0.3 }); LeastSquareResults result = LS.solve(X, Y, SIGMA, PARAM_FUNCTION, PARAM_GRAD, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(0), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(1), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(2), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8); result = LS.solve(X, Y, SIGMA.getEntry(0), PARAM_FUNCTION, PARAM_GRAD, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(0), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(1), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(2), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8); result = LS.solve(X, Y, PARAM_FUNCTION, PARAM_GRAD, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(0), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(1), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(2), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8); }
@Test public void solveExactTest2() { final DoubleMatrix1D start = new DoubleMatrix1D(new double[] {0.2, 1.8, 0.2, 0.3 }); final LeastSquareResults result = LS.solve(Y, SIGMA, FUNCTION, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(0), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(1), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(2), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8); }
@Test public void solveExactWithoutGradientTest() { final DoubleMatrix1D start = new DoubleMatrix1D(new double[] {1.2, 0.8, -0.2, -0.3 }); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final LeastSquareResults result = ls.solve(X, Y, SIGMA, PARAM_FUNCTION, start); assertEquals(0.0, result.getChiSq(), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(0), 1e-8); assertEquals(1.0, result.getFitParameters().getEntry(1), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(2), 1e-8); assertEquals(0.0, result.getFitParameters().getEntry(3), 1e-8); }
@Test public void solveRandomNoiseTest() { final MatrixAlgebra ma = new OGMatrixAlgebra(); final double[] y = new double[20]; for (int i = 0; i < 20; i++) { y[i] = Y.getEntry(i) + SIGMA.getEntry(i) * NORMAL.nextRandom(); } final DoubleMatrix1D start = new DoubleMatrix1D(new double[] {0.7, 1.4, 0.2, -0.3 }); final NonLinearLeastSquare ls = new NonLinearLeastSquare(); final LeastSquareResults res = ls.solve(X, new DoubleMatrix1D(y), SIGMA, PARAM_FUNCTION, PARAM_GRAD, start); final double chiSqDoF = res.getChiSq() / 16; assertTrue(chiSqDoF > 0.25); assertTrue(chiSqDoF < 3.0); final DoubleMatrix1D trueValues = new DoubleMatrix1D(new double[] {1, 1, 0, 0 }); final DoubleMatrix1D delta = (DoubleMatrix1D) ma.subtract(res.getFitParameters(), trueValues); final LUDecompositionCommons decmp = new LUDecompositionCommons(); final LUDecompositionResult decmpRes = decmp.evaluate(res.getCovariance()); final DoubleMatrix2D invCovariance = decmpRes.solve(DoubleMatrixUtils.getIdentityMatrix2D(4)); double z = ma.getInnerProduct(delta, ma.multiply(invCovariance, delta)); z = Math.sqrt(z); assertTrue(z < 3.0); }