org.apache.commons.math.optimization.fitting

## Class CurveFitter

- java.lang.Object
- - org.apache.commons.math.optimization.fitting.CurveFitter

    public class CurveFitter
    extends java.lang.Object

Fitter for parametric univariate real functions y = f(x).

When a univariate real function y = f(x) does depend on some unknown parameters p0, p1 ... pn-1, this class can be used to find these parameters. It does this by fitting the curve so it remains very close to a set of observed points (x0, y0), (x1, y1) ... (xk-1, yk-1). This fitting is done by finding the parameters values that minimizes the objective function ∑(yi-f(xi))2. This is really a least squares problem.

Since:
  2.0
Version:
  $Revision: 1073158 $ $Date: 2011-02-21 22:46:52 +0100 (lun. 21 févr. 2011) $

### Nested Class Summary

Nested Classes
`private class` `CurveFitter.TheoreticalValuesFunction` Vectorial function computing function theoretical values.

### Field Summary

Fields
`private java.util.List<WeightedObservedPoint>` `observations` Observed points.
`private DifferentiableMultivariateVectorialOptimizer` `optimizer` Optimizer to use for the fitting.

### Constructor Summary

Constructors
Constructor and Description
`CurveFitter(DifferentiableMultivariateVectorialOptimizer optimizer)` Simple constructor.

### Method Summary

Methods
`void` `addObservedPoint(double x, double y)` Add an observed (x,y) point to the sample with unit weight.
`void` `addObservedPoint(double weight, double x, double y)` Add an observed weighted (x,y) point to the sample.
`void` `addObservedPoint(WeightedObservedPoint observed)` Add an observed weighted (x,y) point to the sample.
`void` `clearObservations()` Remove all observations.
`double[]` `fit(ParametricRealFunction f, double[] initialGuess)` Fit a curve.
`WeightedObservedPoint[]` `getObservations()` Get the observed points.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### optimizer

    private final DifferentiableMultivariateVectorialOptimizer optimizer

Optimizer to use for the fitting.

#### observations

    private final java.util.List<WeightedObservedPoint> observations

Observed points.

### Constructor Detail

#### CurveFitter

    public CurveFitter(DifferentiableMultivariateVectorialOptimizer optimizer)

Simple constructor.

Parameters:
  `optimizer` - optimizer to use for the fitting

### Method Detail

#### addObservedPoint

    public void addObservedPoint(double x,
                        double y)

Add an observed (x,y) point to the sample with unit weight.

Calling this method is equivalent to call `addObservedPoint(1.0, x, y)`.

Parameters:
  `x` - abscissa of the point
  `y` - observed value of the point at x, after fitting we should have f(x) as close as possible to this value
See Also:
  `addObservedPoint(double, double, double)`, `addObservedPoint(WeightedObservedPoint)`, `getObservations()`

#### addObservedPoint

    public void addObservedPoint(double weight,
                        double x,
                        double y)

Add an observed weighted (x,y) point to the sample.

Parameters:
  `weight` - weight of the observed point in the fit
  `x` - abscissa of the point
  `y` - observed value of the point at x, after fitting we should have f(x) as close as possible to this value
See Also:
  `addObservedPoint(double, double)`, `addObservedPoint(WeightedObservedPoint)`, `getObservations()`

#### addObservedPoint

    public void addObservedPoint(WeightedObservedPoint observed)

Add an observed weighted (x,y) point to the sample.

Parameters:
  `observed` - observed point to add
See Also:
  `addObservedPoint(double, double)`, `addObservedPoint(double, double, double)`, `getObservations()`

#### getObservations

    public WeightedObservedPoint[] getObservations()

Get the observed points.

Returns:
  observed points
See Also:
  `addObservedPoint(double, double)`, `addObservedPoint(double, double, double)`, `addObservedPoint(WeightedObservedPoint)`

#### clearObservations

    public void clearObservations()

Remove all observations.

#### fit

    public double[] fit(ParametricRealFunction f,
               double[] initialGuess)
                 throws FunctionEvaluationException,
                        OptimizationException,
                        java.lang.IllegalArgumentException

Fit a curve.

This method compute the coefficients of the curve that best fit the sample of observed points previously given through calls to the `addObservedPoint` method.

Parameters:
  `f` - parametric function to fit
  `initialGuess` - first guess of the function parameters
Returns:
  fitted parameters
Throws:
  `FunctionEvaluationException` - if the objective function throws one during the search
  `OptimizationException` - if the algorithm failed to converge
  `java.lang.IllegalArgumentException` - if the start point dimension is wrong