List of usage examples for org.apache.commons.math3.optim.nonlinear.vector.jacobian LevenbergMarquardtOptimizer getChiSquare
public double getChiSquare()
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;/*w w 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; }
From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.CircleCoordinatesFunction.java
@Override public void geometricFit(IDataset x, IDataset y, double[] init) { residual = Double.NaN;/*from w ww . j av a2 s .c om*/ if (x.getSize() < PARAMETERS || y.getSize() < PARAMETERS) { throw new IllegalArgumentException("Need " + PARAMETERS + " or more points"); } if (x.getSize() == PARAMETERS) { init = quickfit(x, y); for (int i = 0; i < PARAMETERS; i++) parameters[i] = init[i]; residual = 0; return; } if (init == null) init = quickfit(x, y); else if (init.length < PARAMETERS) throw new IllegalArgumentException("Need " + PARAMETERS + " parameters"); CircleCoordinatesFunction f = (CircleCoordinatesFunction) getFitFunction(x, y); LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer(); try { PointVectorValuePair result = opt.optimize(new ModelFunction(f), new ModelFunctionJacobian(f.jacobian()), f.getTarget(), f.getWeight(), f.calcAllInitValues(init), new MaxEval(MAX_EVALUATIONS)); double[] point = result.getPointRef(); for (int i = 0; i < PARAMETERS; i++) parameters[i] = point[i]; residual = opt.getRMS(); logger.trace("Circle fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare()); } catch (DimensionMismatchException e) { // cannot happen } catch (IllegalArgumentException e) { // should not happen! } catch (TooManyEvaluationsException e) { throw new IllegalArgumentException("Problem with optimizer converging"); } }
From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.EllipseCoordinatesFunction.java
@Override public void geometricFit(IDataset x, IDataset y, double[] init) { residual = Double.NaN;/*from w w w .j av a 2 s .c o m*/ 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 = (EllipseCoordinatesFunction) getFitFunction(x, y); LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer(); try { PointVectorValuePair result = opt.optimize(new ModelFunction(f), new ModelFunctionJacobian(f.jacobian()), f.getTarget(), f.getWeight(), f.calcAllInitValues(init), new MaxEval(MAX_EVALUATIONS)); double[] point = result.getPointRef(); for (int i = 0; i < PARAMETERS; i++) parameters[i] = point[i]; residual = opt.getRMS(); logger.trace("Ellipse fit: rms = {}, x^2 = {}", residual, opt.getChiSquare()); } catch (DimensionMismatchException e) { // cannot happen } catch (IllegalArgumentException e) { // should not happen! } catch (TooManyEvaluationsException e) { throw new IllegalArgumentException("Problem with optimizer converging"); } }