org.apache.commons.math3.analysis.MultivariateVectorFunction Java Examples

The following examples show how to use org.apache.commons.math3.analysis.MultivariateVectorFunction. 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: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #2
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #3
Source File: MultiStartMultivariateVectorOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test demonstrating that the user exception is finally thrown if none
 * of the runs succeed.
 */
@Test(expected=TestException.class)
public void testNoOptimum() {
    JacobianMultivariateVectorOptimizer underlyingOptimizer
        = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(12373523445l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartMultivariateVectorOptimizer optimizer
        = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);
    optimizer.optimize(new MaxEval(100),
                       new Target(new double[] { 0 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0 }),
                       new ModelFunction(new MultivariateVectorFunction() {
                               public double[] value(double[] point) {
                                   throw new TestException();
                               }
                           }));
}
 
Example #4
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #5
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares3() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #6
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #7
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public ObjectiveFunctionGradient getObjectiveFunctionGradient() {
    return new ObjectiveFunctionGradient(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                // gradient of the sum of squared residuals
                double dJdX = 0;
                double dJdY = 0;
                for (Vector2D pk : points) {
                    double dk = pk.distance(center);
                    dJdX += (center.getX() - pk.getX()) * (dk - radius) / dk;
                    dJdY += (center.getY() - pk.getY()) * (dk - radius) / dk;
                }
                dJdX *= 2;
                dJdY *= 2;

                return new double[] { dJdX, dJdY };
            }
        });
}
 
Example #8
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public ObjectiveFunctionGradient getObjectiveFunctionGradient() {
    return new ObjectiveFunctionGradient(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                // gradient of the sum of squared residuals
                double dJdX = 0;
                double dJdY = 0;
                for (Vector2D pk : points) {
                    double dk = pk.distance(center);
                    dJdX += (center.getX() - pk.getX()) * (dk - radius) / dk;
                    dJdY += (center.getY() - pk.getY()) * (dk - radius) / dk;
                }
                dJdX *= 2;
                dJdY *= 2;

                return new double[] { dJdX, dJdY };
            }
        });
}
 
Example #9
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #10
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public ObjectiveFunctionGradient getObjectiveFunctionGradient() {
    return new ObjectiveFunctionGradient(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                // gradient of the sum of squared residuals
                double dJdX = 0;
                double dJdY = 0;
                for (Vector2D pk : points) {
                    double dk = pk.distance(center);
                    dJdX += (center.getX() - pk.getX()) * (dk - radius) / dk;
                    dJdY += (center.getY() - pk.getY()) * (dk - radius) / dk;
                }
                dJdX *= 2;
                dJdY *= 2;

                return new double[] { dJdX, dJdY };
            }
        });
}
 
Example #11
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #12
Source File: CircleScalar.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public ObjectiveFunctionGradient getObjectiveFunctionGradient() {
    return new ObjectiveFunctionGradient(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                // gradient of the sum of squared residuals
                double dJdX = 0;
                double dJdY = 0;
                for (Vector2D pk : points) {
                    double dk = pk.distance(center);
                    dJdX += (center.getX() - pk.getX()) * (dk - radius) / dk;
                    dJdY += (center.getY() - pk.getY()) * (dk - radius) / dk;
                }
                dJdX *= 2;
                dJdY *= 2;

                return new double[] { dJdX, dJdY };
            }
        });
}
 
Example #13
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #14
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {

    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2.0, -3.0 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    optimizer.setSimplex(new NelderMeadSimplex(2));
    PointValuePair optimum =
        optimizer.optimize(200, ls, GoalType.MINIMIZE, new double[] { 10, 10 });
    Assert.assertEquals( 2, optimum.getPointRef()[0], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #15
Source File: PSFModel.java    From thunderstorm with GNU General Public License v3.0 6 votes vote down vote up
public MultivariateFunction getLikelihoodFunction(final double[] xgrid, final double[] ygrid, final double[] imageValues) {
        final MultivariateVectorFunction valueFunction = this.getValueFunction(xgrid, ygrid);
        return new MultivariateFunction() {
            @Override
            public double value(double[] point) {

                double[] expectedValues = valueFunction.value(point);
                double logLikelihood = 0;
                for(int i = 0; i < expectedValues.length; i++) {
                    double expectedValue = expectedValues[i];
                    double log = log(expectedValue);
                    if(log < -1e6) {
                        log = -1e6;
                    }
                    logLikelihood += imageValues[i] * log - expectedValue;
                }
//        IJ.log("likelihood:" + logLikelihood);
//        IJ.log("point: " + Arrays.toString(point));
                return logLikelihood;
            }
        };
    }
 
Example #16
Source File: PSFModel.java    From thunderstorm with GNU General Public License v3.0 6 votes vote down vote up
public MultivariateMatrixFunction getNumericJacobianFunction(final double[] xgrid, final double[] ygrid) {
    final MultivariateVectorFunction valueFunction = getValueFunction(xgrid, ygrid);
    return new MultivariateMatrixFunction() {
        static final double step = 0.01;

        @Override
        public double[][] value(double[] point) throws IllegalArgumentException {
            double[][] retVal = new double[xgrid.length][point.length];

            for(int i = 0; i < point.length; i++) {
                double[] newPoint = point.clone();
                newPoint[i] = newPoint[i] + step;
                double[] f1 = valueFunction.value(newPoint);
                double[] f2 = valueFunction.value(point);
                for(int j = 0; j < f1.length; j++) {
                    retVal[j][i] = (f1[j] - f2[j]) / step;
                }
            }
            return retVal;
        }
    };
}
 
Example #17
Source File: CircleVectorial.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModelFunction getModelFunction() {
    return new ModelFunction(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                Vector2D center = new Vector2D(params[0], params[1]);
                double radius = getRadius(center);
                double[] residuals = new double[points.size()];
                for (int i = 0; i < residuals.length; i++) {
                    residuals[i] = points.get(i).distance(center) - radius;
                }

                return residuals;
            }
    });
}
 
Example #18
Source File: StatisticalReferenceDataset.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(final double[] a) {
            final int n = getNumObservations();
            final double[] yhat = new double[n];
            for (int i = 0; i < n; i++) {
                yhat[i] = getModelValue(getX(i), a);
            }
            return yhat;
        }
    };
}
 
Example #19
Source File: LeastSquaresConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a simple converter for uncorrelated residuals with the same weight.
 * @param function vectorial residuals function to wrap
 * @param observations observations to be compared to objective function to compute residuals
 */
public LeastSquaresConverter(final MultivariateVectorFunction function,
                             final double[] observations) {
    this.function     = function;
    this.observations = observations.clone();
    this.weights      = null;
    this.scale        = null;
}
 
Example #20
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModelFunction getModelFunction() {
    return new ModelFunction(new MultivariateVectorFunction() {
            public double[] value(double[] point) {
                return computeValue(point);
            }
        });
}
 
Example #21
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] point) {
            return computeValue(point);
        }
    };
}
 
Example #22
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testLeastSquares3() {
    final RealMatrix factors =
        new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new Array2DRowRealMatrix(new double [][] {
                { 1, 1.2 }, { 1.2, 2 }
            }));
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(ls),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 10, 10 }),
                             new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 2e-3);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #23
Source File: LeastSquaresConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Builds a simple converter for uncorrelated residuals with identical
 * weights.
 *
 * @param function vectorial residuals function to wrap
 * @param observations observations to be compared to objective function to compute residuals
 */
public LeastSquaresConverter(final MultivariateVectorFunction function,
                             final double[] observations) {
    this.function     = function;
    this.observations = observations.clone();
    this.weights      = null;
    this.scale        = null;
}
 
Example #24
Source File: LeastSquaresConverter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Build a simple converter for uncorrelated residuals with the same weight.
 * @param function vectorial residuals function to wrap
 * @param observations observations to be compared to objective function to compute residuals
 */
public LeastSquaresConverter(final MultivariateVectorFunction function,
                             final double[] observations) {
    this.function     = function;
    this.observations = observations.clone();
    this.weights      = null;
    this.scale        = null;
}
 
Example #25
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] params) {
            double[] values = new double[time.size()];
            for (int i = 0; i < values.length; ++i) {
                final double t = time.get(i);
                values[i] = params[0] +
                    params[1] * Math.exp(-t / params[3]) +
                    params[2] * Math.exp(-t / params[4]);
            }
            return values;
        }
    };
}
 
Example #26
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] params) {
            return factors.operate(params);
        }
    };
}
 
Example #27
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] point) {
            return computeValue(point);
        }
    };
}
 
Example #28
Source File: DifferentiableMultivariateMultiStartOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction gradient() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] point) {
            return gradient(point);
        }
    };
}
 
Example #29
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public ModelFunction getModelFunction() {
    return new ModelFunction(new MultivariateVectorFunction() {
            public double[] value(double[] params) {
                return factors.operate(params);
            }
        });
}
 
Example #30
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MultivariateVectorFunction getModelFunction() {
    return new MultivariateVectorFunction() {
        public double[] value(double[] params) {
            return factors.operate(params);
        }
    };
}