You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jan Bonne Aans <j....@gmail.com> on 2014/08/28 15:17:39 UTC

[math] LevenbergMarquardtOptimizer with multiple variables

Hello,

First of all, developers: thank you for providing this excellent math package!

Unfortunately I am having problems getting the fitting.leastsquares to
work with math3.3:
I've been trying to get the LevenbergMarquardtOptimizer fit a model
with multiple variables to a dataset (for testing purposes simple
gaussian, my ultimate goal is to use it for spectra in principal
component analysis).
However my current code will only optimise the first variable, leaving
the second variable at the initial guess.
Please see my main code below. Output is currently as follows for data
with mu=0 and rho=1.1:

Trying coefficients: 0.001 and 1.0
Trying coefficients: -86.87232151312965 and 1.0
Trying coefficients: -8.686332151312968 and 1.0
Trying coefficients: -0.9536518847596671 and 1.0
Trying coefficients: -0.08587332151312965 and 1.0
Trying coefficients: -0.007695157793326956 and 1.0
Trying coefficients: 1.3040588669653303E-4 and 1.0
Trying coefficients: -0.0016087741636237759 and 1.0
Trying coefficients: -0.00508727538109811 and 1.0
Trying coefficients: -0.001956636820460541 and 1.0
Trying coefficients: -0.0016469977085221531 and 1.0
Trying coefficients: -0.0016142139967060354 and 1.0
Trying coefficients: -0.0016095733167257485 and 1.0
Trying coefficients: -0.0016088921039381933 and 1.0
optimum = {-0.0016087742; 1}

I would greatly appreciate any help on getting the code to optimise
the second variable as well.

Kind regards,
Jan Bonne Aans

Main function:

        /*
         * Test the Levenberg Marquardt optimizer by fitting a gaussian to
         * a dataset derived from a gaussian.
         */

        // create a gaussian model
        double mu = 0;
        double rho = 1.1;
        ModelGaussian model = new ModelGaussian(128);   // gaussian of
128 points

        // create the datapoints
        double[] x = model.getX();
        double[] data = model.evaluate(mu, rho);
        // add noise to data
        for(int i = 0; i < data.length; i++)
            data[i] = data[i] + Math.random() / 50;

        // create weights matrix
        RealMatrix weights = MatrixUtils.createRealIdentityMatrix(data.length);

        // create an initial set of guess coefficients
        double[] coefficients = {0.001,1}; // {mu,rho}

        // create a least squares optimization problem
        LeastSquaresBuilder lsBuilder = new LeastSquaresBuilder();

        MultivariateVectorFunction function = new
MultivariateVectorFunctionGaussian(model);
        MultivariateMatrixFunction jacobian = new
MultivariateMatrixFunctionGaussianJacobian(model);

        // Configure the model function.
        lsBuilder.model(function,jacobian);
        lsBuilder.maxEvaluations(100);
        lsBuilder.maxIterations(100);
        lsBuilder.start(coefficients);
        lsBuilder.target(data);
        lsBuilder.weight(weights);

        // Construct a LeastSquaresProblem from the data in this builder.
        LeastSquaresProblem lsProblem = lsBuilder.build();

        // construct LM optimizer
        LevenbergMarquardtOptimizer lmOptimizer = new
LevenbergMarquardtOptimizer();

        // find optimum
        Optimum optimum = lmOptimizer.optimize(lsProblem);

        System.out.println("optimum = " + optimum.getPoint().toString());
        double muOptimum = optimum.getPoint().getEntry(0);
        double rhoOptimum = optimum.getPoint().getEntry(1);

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [math] LevenbergMarquardtOptimizer with multiple variables

Posted by Gilles <gi...@harfang.homelinux.org>.
Hi.

On Thu, 28 Aug 2014 15:17:39 +0200, Jan Bonne Aans wrote:
> Hello,
>
> First of all, developers: thank you for providing this excellent math
> package!
>
> Unfortunately I am having problems getting the fitting.leastsquares 
> to
> work with math3.3:
> I've been trying to get the LevenbergMarquardtOptimizer fit a model
> with multiple variables to a dataset (for testing purposes simple
> gaussian, my ultimate goal is to use it for spectra in principal
> component analysis).
> However my current code will only optimise the first variable, 
> leaving
> the second variable at the initial guess.
> Please see my main code below. Output is currently as follows for 
> data
> with mu=0 and rho=1.1:
>
> Trying coefficients: 0.001 and 1.0
> Trying coefficients: -86.87232151312965 and 1.0
> Trying coefficients: -8.686332151312968 and 1.0
> Trying coefficients: -0.9536518847596671 and 1.0
> Trying coefficients: -0.08587332151312965 and 1.0
> Trying coefficients: -0.007695157793326956 and 1.0
> Trying coefficients: 1.3040588669653303E-4 and 1.0
> Trying coefficients: -0.0016087741636237759 and 1.0
> Trying coefficients: -0.00508727538109811 and 1.0
> Trying coefficients: -0.001956636820460541 and 1.0
> Trying coefficients: -0.0016469977085221531 and 1.0
> Trying coefficients: -0.0016142139967060354 and 1.0
> Trying coefficients: -0.0016095733167257485 and 1.0
> Trying coefficients: -0.0016088921039381933 and 1.0
> optimum = {-0.0016087742; 1}
>
> I would greatly appreciate any help on getting the code to optimise
> the second variable as well.
>
> Kind regards,
> Jan Bonne Aans
>
> Main function:
>
>         /*
>          * Test the Levenberg Marquardt optimizer by fitting a 
> gaussian to
>          * a dataset derived from a gaussian.
>          */
>
>         // create a gaussian model
>         double mu = 0;
>         double rho = 1.1;
>         ModelGaussian model = new ModelGaussian(128);   // gaussian 
> of
> 128 points
>
>         // create the datapoints
>         double[] x = model.getX();
>         double[] data = model.evaluate(mu, rho);
>         // add noise to data
>         for(int i = 0; i < data.length; i++)
>             data[i] = data[i] + Math.random() / 50;
>
>         // create weights matrix
>         RealMatrix weights =
> MatrixUtils.createRealIdentityMatrix(data.length);
>
>         // create an initial set of guess coefficients
>         double[] coefficients = {0.001,1}; // {mu,rho}


Perhaps the solution is within one of the default tolerance
settings.
Did you try initial values much more different from the optimal
ones?


Best regards,
Gilles

>
>         // create a least squares optimization problem
>         LeastSquaresBuilder lsBuilder = new LeastSquaresBuilder();
>
>         MultivariateVectorFunction function = new
> MultivariateVectorFunctionGaussian(model);
>         MultivariateMatrixFunction jacobian = new
> MultivariateMatrixFunctionGaussianJacobian(model);
>
>         // Configure the model function.
>         lsBuilder.model(function,jacobian);
>         lsBuilder.maxEvaluations(100);
>         lsBuilder.maxIterations(100);
>         lsBuilder.start(coefficients);
>         lsBuilder.target(data);
>         lsBuilder.weight(weights);
>
>         // Construct a LeastSquaresProblem from the data in this 
> builder.
>         LeastSquaresProblem lsProblem = lsBuilder.build();
>
>         // construct LM optimizer
>         LevenbergMarquardtOptimizer lmOptimizer = new
> LevenbergMarquardtOptimizer();
>
>         // find optimum
>         Optimum optimum = lmOptimizer.optimize(lsProblem);
>
>         System.out.println("optimum = " + 
> optimum.getPoint().toString());
>         double muOptimum = optimum.getPoint().getEntry(0);
>         double rhoOptimum = optimum.getPoint().getEntry(1);
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org