Java Code Examples for org.apache.commons.math3.optim.PointVectorValuePair#getPoint()

The following examples show how to use org.apache.commons.math3.optim.PointVectorValuePair#getPoint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example 2
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example 3
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example 4
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example 5
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example 6
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();
    final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100),
                                                            circle.getModelFunction(),
                                                            circle.getModelFunctionJacobian(),
                                                            new Target(circle.target()),
                                                            new Weight(circle.weight()),
                                                            new InitialGuess(init));

    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 7
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();
    final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100),
                                                            circle.getModelFunction(),
                                                            circle.getModelFunctionJacobian(),
                                                            new Target(circle.target()),
                                                            new Weight(circle.weight()),
                                                            new InitialGuess(init));

    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 8
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(dataPoints[1]),
                             new Weight(weights),
                             new InitialGuess(new double[] { 10, 900, 80, 27, 225 }));

    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 9
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();
    final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100),
                                                            circle.getModelFunction(),
                                                            circle.getModelFunctionJacobian(),
                                                            new Target(circle.target()),
                                                            new Weight(circle.weight()),
                                                            new InitialGuess(init));

    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 10
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(dataPoints[1]),
                             new Weight(weights),
                             new InitialGuess(new double[] { 10, 900, 80, 27, 225 }));

    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 11
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer = LevenbergMarquardtOptimizer.create()
        .withMaxEvaluations(100)
        .withMaxIterations(50)
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(circle.target())
        .withWeight(new DiagonalMatrix(circle.weight()))
        .withStartPoint(init);

    final PointVectorValuePair optimum = optimizer.optimize();
    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 12
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer = LevenbergMarquardtOptimizer.create()
        .withMaxEvaluations(100)
        .withMaxIterations(20)
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(dataPoints[1])
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 10, 900, 80, 27, 225 });

    final PointVectorValuePair optimum = optimizer.optimize();
    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 13
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(dataPoints[1]),
                             new Weight(weights),
                             new InitialGuess(new double[] { 10, 900, 80, 27, 225 }));

    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 14
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(dataPoints[1]),
                             new Weight(weights),
                             new InitialGuess(new double[] { 10, 900, 80, 27, 225 }));

    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 15
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();
    final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100),
                                                            circle.getModelFunction(),
                                                            circle.getModelFunctionJacobian(),
                                                            new Target(circle.target()),
                                                            new Weight(circle.weight()),
                                                            new InitialGuess(init));

    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 16
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(dataPoints[1]),
                             new Weight(weights),
                             new InitialGuess(new double[] { 10, 900, 80, 27, 225 }));

    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 17
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer = LevenbergMarquardtOptimizer.create()
        .withMaxEvaluations(100)
        .withMaxIterations(50)
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(circle.target())
        .withWeight(new DiagonalMatrix(circle.weight()))
        .withStartPoint(init);

    final PointVectorValuePair optimum = optimizer.optimize();
    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}
 
Example 18
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Non-linear test case: fitting of decay curve (from Chapter 8 of
 * Bevington's textbook, "Data reduction and analysis for the physical sciences").
 * XXX The expected ("reference") values may not be accurate and the tolerance too
 * relaxed for this test to be currently really useful (the issue is under
 * investigation).
 */
@Test
public void testBevington() {
    final double[][] dataPoints = {
        // column 1 = times
        { 15, 30, 45, 60, 75, 90, 105, 120, 135, 150,
          165, 180, 195, 210, 225, 240, 255, 270, 285, 300,
          315, 330, 345, 360, 375, 390, 405, 420, 435, 450,
          465, 480, 495, 510, 525, 540, 555, 570, 585, 600,
          615, 630, 645, 660, 675, 690, 705, 720, 735, 750,
          765, 780, 795, 810, 825, 840, 855, 870, 885, },
        // column 2 = measured counts
        { 775, 479, 380, 302, 185, 157, 137, 119, 110, 89,
          74, 61, 66, 68, 48, 54, 51, 46, 55, 29,
          28, 37, 49, 26, 35, 29, 31, 24, 25, 35,
          24, 30, 26, 28, 21, 18, 20, 27, 17, 17,
          14, 17, 24, 11, 22, 17, 12, 10, 13, 16,
          9, 9, 14, 21, 17, 13, 12, 18, 10, },
    };

    final BevingtonProblem problem = new BevingtonProblem();

    final int len = dataPoints[0].length;
    final double[] weights = new double[len];
    for (int i = 0; i < len; i++) {
        problem.addPoint(dataPoints[0][i],
                         dataPoints[1][i]);

        weights[i] = 1 / dataPoints[1][i];
    }

    final LevenbergMarquardtOptimizer optimizer = LevenbergMarquardtOptimizer.create()
        .withMaxEvaluations(100)
        .withMaxIterations(20)
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(dataPoints[1])
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 10, 900, 80, 27, 225 });

    final PointVectorValuePair optimum = optimizer.optimize();
    final double[] solution = optimum.getPoint();
    final double[] expectedSolution = { 10.4, 958.3, 131.4, 33.9, 205.0 };

    final double[][] covarMatrix = optimizer.computeCovariances(solution, 1e-14);
    final double[][] expectedCovarMatrix = {
        { 3.38, -3.69, 27.98, -2.34, -49.24 },
        { -3.69, 2492.26, 81.89, -69.21, -8.9 },
        { 27.98, 81.89, 468.99, -44.22, -615.44 },
        { -2.34, -69.21, -44.22, 6.39, 53.80 },
        { -49.24, -8.9, -615.44, 53.8, 929.45 }
    };

    final int numParams = expectedSolution.length;

    // Check that the computed solution is within the reference error range.
    for (int i = 0; i < numParams; i++) {
        final double error = FastMath.sqrt(expectedCovarMatrix[i][i]);
        Assert.assertEquals("Parameter " + i, expectedSolution[i], solution[i], error);
    }

    // Check that each entry of the computed covariance matrix is within 10%
    // of the reference matrix entry.
    for (int i = 0; i < numParams; i++) {
        for (int j = 0; j < numParams; j++) {
            Assert.assertEquals("Covariance matrix [" + i + "][" + j + "]",
                                expectedCovarMatrix[i][j],
                                covarMatrix[i][j],
                                FastMath.abs(0.1 * expectedCovarMatrix[i][j]));
        }
    }
}
 
Example 19
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting2() {
    final double xCenter = 123.456;
    final double yCenter = 654.321;
    final double xSigma = 10;
    final double ySigma = 15;
    final double radius = 111.111;
    // The test is extremely sensitive to the seed.
    final long seed = 59421061L;
    final RandomCirclePointGenerator factory
        = new RandomCirclePointGenerator(xCenter, yCenter, radius,
                                         xSigma, ySigma,
                                         seed);
    final CircleProblem circle = new CircleProblem(xSigma, ySigma);

    final int numPoints = 10;
    for (Vector2D p : factory.generate(numPoints)) {
        circle.addPoint(p.getX(), p.getY());
    }

    // First guess for the center's coordinates and radius.
    final double[] init = { 90, 659, 115 };

    final LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer();
    final PointVectorValuePair optimum = optimizer.optimize(new MaxEval(100),
                                                            circle.getModelFunction(),
                                                            circle.getModelFunctionJacobian(),
                                                            new Target(circle.target()),
                                                            new Weight(circle.weight()),
                                                            new InitialGuess(init));

    final double[] paramFound = optimum.getPoint();

    // Retrieve errors estimation.
    final double[] asymptoticStandardErrorFound = optimizer.computeSigma(paramFound, 1e-14);

    // Check that the parameters are found within the assumed error bars.
    Assert.assertEquals(xCenter, paramFound[0], asymptoticStandardErrorFound[0]);
    Assert.assertEquals(yCenter, paramFound[1], asymptoticStandardErrorFound[1]);
    Assert.assertEquals(radius, paramFound[2], asymptoticStandardErrorFound[2]);
}