ChiSquaredFitter
joptima

## Class ChiSquaredFitter

• All Implemented Interfaces:
Function, Minimizable

`public class ChiSquaredFitterextends Fitter`

χ² fitter. This class can be passed to a `Minimizer` to minimize χ² of a sum of one or more functions fit to the given data set. Error bars may be provided for the data points if the data has compounded errors that are Gaussian. sqrt(y) errors are assumed otherwise. For different likelihood functions you can extend the `calculate()` method on this class.

A typical usage of this class would go as follows:

`   // The data points.   double[] x = new double[]{  1,   2,   3,    4,  5};   double[] y = new double[]{1.1, 4.0, 8.7, 15.4, 26};   // The `Function` to fit to the data points.   Function f = new Quadratic();   ChiSquaredFitter fitter = new ChiSquaredFitter();   fitter.setData(x, y);   fitter.addFunction(f);   Minimizer.minimize(fitter);   // At this point, if the fit converged, the `Function` f will have   // its `Parameter`s set to the optimal values. `

• ### Constructor Summary

Constructors
Constructor and Description
`ChiSquaredFitter()`
Constructor.
`ChiSquaredFitter(double[] x, double[] y)`
Constructor.
`ChiSquaredFitter(double[] x, double[] y, double[] errors)`
Constructor.
`ChiSquaredFitter(double[] x, double[] y, double[] errors, Function f)`
Constructor.
`ChiSquaredFitter(double[] x, double[] y, Function f)`
Constructor.
• ### Method Summary

Methods
Modifier and TypeMethod and Description
`double``calculate()`
Calculates the χ² given parameter values set by a `Minimizer` using reflection.
`void``setData(double[] x, double[] y)`
Fit the sum of functions to this set of points.
`void``setData(double[] x, double[] y, double[] errors)`
Fit the sum of functions to this set of points.
• ### Methods inherited from class joptima.Fitter

`addFunction, calculate, getName, getParameters, removeAllFunctions, setName`
• ### Methods inherited from class java.lang.Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Constructor Detail

• #### ChiSquaredFitter

`public ChiSquaredFitter()`
Constructor.
• #### ChiSquaredFitter

`public ChiSquaredFitter(double[] x,                double[] y)`
Constructor. Assumes sqrt(y) errors.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
• #### ChiSquaredFitter

`public ChiSquaredFitter(double[] x,                double[] y,                double[] errors)`
Constructor.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
`errors` - the error bars on y coordinates of points
• #### ChiSquaredFitter

`public ChiSquaredFitter(double[] x,                double[] y,                Function f)`
Constructor. Assumes sqrt(y) errors.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
`f` - the `Function` to fit to the given set of points
• #### ChiSquaredFitter

`public ChiSquaredFitter(double[] x,                double[] y,                double[] errors,                Function f)`
Constructor.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
`errors` - the error bars on y coordinates of points
`f` - the `Function` to fit to the given set of points
• ### Method Detail

• #### setData

`public void setData(double[] x,           double[] y)`
Fit the sum of functions to this set of points. Assumes sqrt(y) error bars.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
• #### setData

`public void setData(double[] x,           double[] y,           double[] errors)`
Fit the sum of functions to this set of points.
Parameters:
`x` - the x coordinates of points
`y` - the y coordinates of points
`errors` - the error bars on y coordinates of points
• #### calculate

`public double calculate()`
Calculates the χ² given parameter values set by a `Minimizer` using reflection. The formula is:
`   for each point in the data set {     f = 0     for each `Function` {       f += `Function.calculate(double)`(x[point])     }     χ² += (y[point] - f)² / error[point]²   } `