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

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

Introduction

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

Prototype

public double getChiSquare() 

Source Link

Document

Get a Chi-Square-like value assuming the N residuals follow N distinct normal distributions centered on 0 and whose variances are the reciprocal of the weights.

Usage

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.//  w  ww.  j  a va 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");
    }
}