Example usage for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer LevenbergMarquardtOptimizer

List of usage examples for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer LevenbergMarquardtOptimizer

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer LevenbergMarquardtOptimizer.

Prototype

public LevenbergMarquardtOptimizer() 

Source Link

Document

Build an optimizer for least squares problems.

Usage

From source file:net.bioclipse.brunn.ui.editors.plateEditor.model.HillCurveIC50Calculator.java

public double calculateIC50(double[] conc, double[] si) {

    LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();

    CurveFitter fitter = new CurveFitter(optimizer);

    for (int i = 0; i < si.length; i++) {
        fitter.addObservedPoint(conc[i], si[i] / 100);
        System.out.println("Added point: " + conc[i] + "; " + si[i] / 100);
    }/*from w  ww .  j a v  a  2  s .com*/

    ParametricRealFunction function = new ParametricRealFunction() {

        @Override
        public double value(double c, double[] paramaters) throws FunctionEvaluationException {

            double d = paramaters[0];
            double n = paramaters[1];
            double c_pow_n = Math.pow(c, n);

            return c_pow_n / (c_pow_n + Math.pow(d, n));
        }

        @Override
        public double[] gradient(double c, double[] paramaters) throws FunctionEvaluationException {

            double d = paramaters[0];
            double n = paramaters[1];
            double c_pow_n = Math.pow(c, n);
            double d_pow_n = Math.pow(d, n);

            double ddd = -n * c_pow_n * Math.pow(d, n - 1) / Math.pow(c_pow_n + d_pow_n, 2);

            double ddn = (c_pow_n * d_pow_n * (Math.log(c) - Math.log(d))) / Math.pow(c_pow_n + d_pow_n, 2);

            return new double[] { ddd, ddn };
        }

    };

    double[] params = null;
    try {
        params = fitter.fit(function, new double[] { 1, 1 });
    } catch (Exception e) {
        Logger.getLogger(HillCurveIC50Calculator.class)
                .debug("Caught Exception while fitting dose response curve", e);
        return Double.NaN;
    }

    double d = params[0];
    double n = params[1];

    System.out.println("d=" + d);
    System.out.println("n=" + n);

    return Math.pow(-(0.5 - 1) * Math.pow(d, -n) / 0.5, -1 / n);
}

From source file:uk.ac.diamond.scisoft.analysis.fitting.AngleDerivativeFunction.java

/**
 * Fit points given by x, y datasets to an ellipse. If no initial parameters are given, then
 * an algebraic fit is performed then a non-linear least squares fitting routine is used to
 * provide the best geometric fit.//from   www. j  a v a  2  s  .c  o  m
 * @param x
 * @param y
 * @param init parameters (can be null)
 */
public void geometricFit(AbstractDataset x, AbstractDataset y, double[] init) {

    if (x.getSize() < PARAMETERS || y.getSize() < PARAMETERS) {
        throw new IllegalArgumentException("Need " + PARAMETERS + " or more points");
    }

    if (init == null)
        init = quickfit(x, y);
    else if (init.length < PARAMETERS)
        throw new IllegalArgumentException("Need " + PARAMETERS + " parameters");

    EllipseCoordinatesFunction f = new EllipseCoordinatesFunction(x, y);
    LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer();

    try {
        VectorialPointValuePair result = opt.optimize(f, f.getTarget(), f.getWeight(),
                f.calcAllInitValues(init));

        double[] point = result.getPointRef();
        parameters[0] = point[0] * point[0];
        parameters[1] = point[1] * point[1];
        for (int i = 2; i < PARAMETERS; i++)
            parameters[i] = point[i];

        logger.info("Ellipse fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare());
    } catch (FunctionEvaluationException e) {
        // cannot happen
    } catch (IllegalArgumentException e) {
        // should not happen!
    } catch (ConvergenceException e) {
        throw new IllegalArgumentException("Problem with optimizer converging");
    }
}

From source file:uk.ac.diamond.scisoft.analysis.fitting.Gaussian.java

public void optimise() {
    LevenbergMarquardtOptimizer lmopt = new LevenbergMarquardtOptimizer();
    ObjectiveFunction f = new ObjectiveFunction(2);

    try {//from  w  w w  . j  av  a 2 s .  c  om
        lmopt.optimize(f, dataset.getData(), weights, startPoint);
    } catch (OptimizationException e) {
        e.printStackTrace();
    } catch (FunctionEvaluationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}