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

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

Introduction

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

Prototype

public LevenbergMarquardtOptimizer() 

Source Link

Document

Build an optimizer for least squares problems with default values for all the tuning parameters (see the #LevenbergMarquardtOptimizer(double,double,double,double,double) other contructor .

Usage

From source file:uk.co.kevinjjones.model.SmoothStream.java

private void curveFit() {
    if (_curve == null) {
        Double[] data = toArray();
        int points = data.length;
        int blockSize = 8;
        _curve = new double[points];

        int idx = 0;
        while (idx < points) {

            // Skip zeros, they curve fit badly 
            if (data[idx] == 0) {
                _curve[idx] = 0;//from w ww  .ja v a2  s .  c o  m
                idx++;
            } else {
                // Consume a block
                int bcount = 0;
                while (idx + bcount < points && bcount < blockSize) {
                    if (data[idx + bcount] == 0) {
                        break;
                    }
                    bcount++;
                }

                // Skip short blocks
                if (bcount < 5) {
                    for (int i = 0; i < bcount; i++) {
                        _curve[idx + i] = data[idx + i];
                    }
                    idx += bcount;
                } else {
                    // Try to fit this
                    CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer());
                    double[] init = new double[4];
                    for (int i = 0; i < bcount; i++) {
                        fitter.addObservedPoint(i, data[idx + i]);
                    }
                    final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init);
                    final PolynomialFunction fitted = new PolynomialFunction(best);

                    for (int i = 0; i < bcount; i++) {
                        _curve[idx + i] = fitted.value(i);

                        // Negative fits upset the graphing
                        if (_curve[idx + i] < 0) {
                            _curve[idx + i] = 0;
                        }
                    }
                    idx += bcount;
                }
            }
        }
    }
}