Example usage for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer computeCovariances

List of usage examples for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer computeCovariances

Introduction

In this page you can find the example usage for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer computeCovariances.

Prototype

public double[][] computeCovariances(double[] params, double threshold) 

Source Link

Document

Get the covariance matrix of the optimized parameters.

Usage

From source file:gdsc.smlm.fitting.nonlinear.ApacheLVMFitter.java

public FitStatus fit(int n, double[] y, double[] y_fit, double[] a, double[] a_dev, double[] error,
        double noise) {
    numberOfFittedPoints = n;/*from  ww w .  ja  v a  2 s. c  o m*/

    try {
        // Different convergence thresholds seem to have no effect on the resulting fit, only the number of
        // iterations for convergence
        final double initialStepBoundFactor = 100;
        final double costRelativeTolerance = 1e-10;
        final double parRelativeTolerance = 1e-10;
        final double orthoTolerance = 1e-10;
        final double threshold = Precision.SAFE_MIN;

        // Extract the parameters to be fitted
        final double[] initialSolution = getInitialSolution(a);

        // TODO - Pass in more advanced stopping criteria.

        // Create the target and weight arrays
        final double[] yd = new double[n];
        final double[] w = new double[n];
        for (int i = 0; i < n; i++) {
            yd[i] = y[i];
            w[i] = 1;
        }

        LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(initialStepBoundFactor,
                costRelativeTolerance, parRelativeTolerance, orthoTolerance, threshold);
        PointVectorValuePair optimum = optimizer.optimize(new MaxIter(getMaxEvaluations()),
                new MaxEval(Integer.MAX_VALUE),
                new ModelFunctionJacobian(new MultivariateMatrixFunctionWrapper(f, a, n)),
                new ModelFunction(new MultivariateVectorFunctionWrapper(f, a, n)), new Target(yd),
                new Weight(w), new InitialGuess(initialSolution));

        final double[] parameters = optimum.getPoint();
        setSolution(a, parameters);
        iterations = optimizer.getIterations();
        evaluations = optimizer.getEvaluations();
        if (a_dev != null) {
            double[][] covar = optimizer.computeCovariances(parameters, threshold);
            setDeviations(a_dev, covar);
        }
        // Compute fitted function if desired 
        if (y_fit != null) {
            f.initialise(a);
            for (int i = 0; i < n; i++)
                y_fit[i] = f.eval(i);
        }

        residualSumOfSquares = error[0] = optimizer.getChiSquare();
        totalSumOfSquares = getSumOfSquares(n, y);
    } catch (TooManyEvaluationsException e) {
        return FitStatus.FAILED_TO_CONVERGE;
    } catch (ConvergenceException e) {
        // Occurs when QR decomposition fails - mark as a singular non-linear model (no solution)
        return FitStatus.SINGULAR_NON_LINEAR_MODEL;
    } catch (Exception e) {
        // TODO - Find out the other exceptions from the fitter and add return values to match. 
        return FitStatus.UNKNOWN;
    }

    return FitStatus.OK;
}