org.apache.commons.math3.linear.DiagonalMatrix Java Examples

The following examples show how to use org.apache.commons.math3.linear.DiagonalMatrix. 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
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
 
Example #2
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    final double[] start = {-12, -12};
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }

    Optimum optimum = optimizer.optimize(builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    Vector2D center = new Vector2D(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
    Assert.assertTrue(optimum.getEvaluations() < 25);
    Assert.assertEquals(0.043, optimum.getRMS(), 1e-3);
    Assert.assertEquals(0.292235, circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738, center.getX(), 1e-6);
    Assert.assertEquals(0.2075001, center.getY(), 1e-6);
}
 
Example #3
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

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

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
 
Example #4
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
Example #5
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } },
                            new double[] { 3 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1 }))
        .withStartPoint(new double[] { 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
}
 
Example #6
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer = createOptimizer()
        .withConvergenceChecker(new SimpleVectorValueChecker(1e-30, 1e-30))
        .withMaxIterations(Integer.MAX_VALUE)
        .withMaxEvaluations(100)
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(new double[] { 0, 0, 0, 0, 0 })
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 98.680, 47.345 });

    optimizer.optimize();
}
 
Example #7
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
 
Example #8
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testComputeRMS() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = FastMath.sqrt(dataset.getResidualSumOfSquares() /
                                          dataset.getNumObservations());
    final double actual = optim.computeRMS(optim.getStart());
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
 
Example #9
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
 
Example #10
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
Example #11
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentSizes1() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0},
                {0, 1}},
                new double[]{-1, 1});

        //TODO why is this part here? hasn't it been tested already?
        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder().weight(new DiagonalMatrix(new double[]{1})).build());

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
Example #12
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentSizes2() {
    try {
        LinearProblem problem
                = new LinearProblem(new double[][]{{1, 0}, {0, 1}},
                new double[]{-1, 1});

        Optimum optimum = optimizer.optimize(problem.getBuilder().build());

        Assert.assertEquals(0, optimum.getRMS(), TOl);
        assertEquals(TOl, optimum.getPoint(), -1, 1);

        //TODO move to builder test
        optimizer.optimize(
                problem.getBuilder()
                        .target(new double[]{1})
                        .weight(new DiagonalMatrix(new double[]{1}))
                        .build()
        );

        fail(optimizer);
    } catch (DimensionMismatchException e) {
        //expected
    }
}
 
Example #13
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 7, 6, 5, 4 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
}
 
Example #14
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

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

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
 
Example #15
Source File: DecomposeSingularValuesIntegrationTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void assertSVDValues(final File outputFileV, final File outputFileS, final File outputFileU) {
    try {
        final ReadCountCollection rcc = ReadCountCollectionUtils.parse(CONTROL_PCOV_FULL_FILE);
        final SVD svd = SVDFactory.createSVD(rcc.counts());
        final RealMatrix sDiag = new DiagonalMatrix(svd.getSingularValues());
        assertOutputFileValues(outputFileU, svd.getU());
        assertOutputFileValues(outputFileS, sDiag);
        assertOutputFileValues(outputFileV, svd.getV());
        assertUnitaryMatrix(svd.getV());
        assertUnitaryMatrix(svd.getU());
        Assert.assertTrue(MatrixUtils.isSymmetric(sDiag, 1e-32));

    } catch (final IOException ioe) {
        Assert.fail("Could not open test file: " + CONTROL_PCOV_FULL_FILE, ioe);
    }
}
 
Example #16
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQRColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

    PointVectorValuePair optimum = optimizer.optimize();

    Assert.assertEquals(0, optimizer.computeRMS(optimum.getPoint()), 1e-10);
    Assert.assertEquals(7, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(4, optimum.getValue()[0], 1e-10);
    Assert.assertEquals(6, optimum.getValue()[1], 1e-10);
    Assert.assertEquals(1, optimum.getValue()[2], 1e-10);
}
 
Example #17
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0, 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum[i], 1e-10);
    }
}
 
Example #18
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testComputeCost() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1d);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final LevenbergMarquardtOptimizer optim = LevenbergMarquardtOptimizer.create()
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(y)
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(a);

    final double expected = dataset.getResidualSumOfSquares();
    final double cost = optim.computeCost(optim.computeResiduals(optim.getModel().value(optim.getStart())));
    final double actual = cost * cost;
    Assert.assertEquals(dataset.getName(), expected, actual, 1e-11 * expected);
}
 
Example #19
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(circle.getModelFunction(),
                              circle.getModelFunctionJacobian())
        .withTarget(target)
        .withWeight(new DiagonalMatrix(weights))
        .withStartPoint(new double[] { 0, 0 });

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

    Assert.assertEquals(-0.1517383071957963, optimum[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.computeRMS(optimum), 1e-8);
}
 
Example #20
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(1, optimum[0], 1e-10);
    Assert.assertEquals(2, optimum[1], 1e-10);
    Assert.assertEquals(3, optimum[2], 1e-10);
}
 
Example #21
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 },
                                             { 0, 1 } },
                            new double[] { -1, 1 });
    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1 }))
        .withStartPoint(new double[] { 0, 0 });

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

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(-1, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);

    optimizer.withWeight(new DiagonalMatrix(new double[] { 1 })).optimize();
}
 
Example #22
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    optimizer.optimize();
}
 
Example #23
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 7, 6, 5, 4 });

    double[] optimum = optimizer.optimize().getPoint();
    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
}
 
Example #24
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 5 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

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

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(2, optimum[0], 1e-10);
    Assert.assertEquals(1, optimum[1], 1e-10);
}
 
Example #25
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 4 });

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 1, 1 });

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

    Assert.assertTrue(optimizer.computeRMS(optimum) > 0.1);
}
 
Example #26
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    final double[] start = {0, 0};

    Optimum optimum = optimizer.optimize(
            builder(circle).weight(new DiagonalMatrix(weights)).start(start).build());

    assertEquals(1e-6, optimum.getPoint(), -0.1517383071957963, 0.2074999736353867);
    Assert.assertEquals(0.04268731682389561, optimum.getRMS(), 1e-8);
}
 
Example #27
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    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 T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(data[1])
        .withWeight(new DiagonalMatrix(w))
        .withStartPoint(initial);

    final double[] actual = optimizer.optimize().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 #28
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testTwoSets() {
    double epsilon = 1e-7;
    LinearProblem problem = new LinearProblem(new double[][] {
            {  2,  1,   0,  4,       0, 0 },
            { -4, -2,   3, -7,       0, 0 },
            {  4,  1,  -2,  8,       0, 0 },
            {  0, -3, -12, -1,       0, 0 },
            {  0,  0,   0,  0, epsilon, 1 },
            {  0,  0,   0,  0,       1, 1 }
    }, new double[] { 2, -9, 2, 2, 1 + epsilon * epsilon, 2});

    T optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(getMaxIterations())
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1, 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0, 0, 0, 0 });

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

    Assert.assertEquals(0, optimizer.computeRMS(optimum), 1e-10);
    Assert.assertEquals(3, optimum[0], 1e-10);
    Assert.assertEquals(4, optimum[1], 1e-10);
    Assert.assertEquals(-1, optimum[2], 1e-10);
    Assert.assertEquals(-2, optimum[3], 1e-10);
    Assert.assertEquals(1 + epsilon, optimum[4], 1e-10);
    Assert.assertEquals(1 - epsilon, optimum[5], 1e-10);
}
 
Example #29
Source File: LeastSquaresFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the square-root of the weight matrix.
 *
 * @param m Symmetric, positive-definite (weight) matrix.
 * @return the square-root of the weight matrix.
 */
private static RealMatrix squareRoot(final RealMatrix m) {
    if (m instanceof DiagonalMatrix) {
        final int dim = m.getRowDimension();
        final RealMatrix sqrtM = new DiagonalMatrix(dim);
        for (int i = 0; i < dim; i++) {
            sqrtM.setEntry(i, i, FastMath.sqrt(m.getEntry(i, i)));
        }
        return sqrtM;
    } else {
        final EigenDecomposition dec = new EigenDecomposition(m);
        return dec.getSquareRoot();
    }
}
 
Example #30
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    final LevenbergMarquardtOptimizer optimizer = createOptimizer()
        .withMaxEvaluations(100)
        .withMaxIterations(20)
        .withModelAndJacobian(problem.getModelFunction(),
                              problem.getModelFunctionJacobian())
        .withTarget(problem.getTarget())
        .withWeight(new DiagonalMatrix(new double[] { 1, 1, 1 }))
        .withStartPoint(new double[] { 0, 0, 0 });

    final double[] optimum = optimizer.optimize().getPoint();
    Assert.assertTrue(FastMath.sqrt(optimizer.getTarget().length) * optimizer.computeRMS(optimum) > 0.6);

    optimizer.computeCovariances(optimum, 1.5e-14);
}