joptima
Class ChiSquaredFitter2D
- java.lang.Object
-
- joptima.Fitter
-
- joptima.ChiSquaredFitter2D
-
- All Implemented Interfaces:
- Function, Minimizable
public class ChiSquaredFitter2D extends Fitter
χ² fitter. This class can be passed to a
Minimizerto minimize χ² of a sum of one or more functions fit to the given two-dimensional data set. Error bars may be provided for the data points if the data has compounded errors that are Gaussian. sqrt(z) errors are assumed otherwise.Note that the usual two-dimensional matrix of values is passed to this function unfolded into a single array, i.e. the (x[i], y[i], z[i]) tuple for an m by n histogram where z = f(x, y) will have indices running from 0 through m * n - 1.
A typical usage of this class would go as follows:
// The data points. double[] x = new double[]{ 1, 2, 3, 1, 2, 3, 1, 2, 3}; double[] y = new double[]{ 1, 1, 1, 2, 2, 2, 3, 3, 3}; double[] z = new double[]{1.1, 4.0, 8.7, 3.9, 16, 24, 9.2, 22, 38}; // TheFunctionto fit to the data points. Function2D f = new Sum2D(new Quadratic(), new Quadratic); ChiSquaredFitter2D fitter = new ChiSquaredFitter2D(); fitter.setData(x, y, z); fitter.addFunction(f); Minimizer.minimize(fitter); // At this point, if the fit converged, theFunctionf will have // itsParameters set to the optimal values.
-
-
Constructor Summary
Constructors Constructor and Description ChiSquaredFitter2D()Constructor.ChiSquaredFitter2D(double[] x, double[] y, double[] z)Constructor.ChiSquaredFitter2D(double[] x, double[] y, double[] z, double[] errors)Constructor.ChiSquaredFitter2D(double[] x, double[] y, double[] z, double[] errors, Function f)Constructor.ChiSquaredFitter2D(double[] x, double[] y, double[] z, Function f)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddFunction(Function f)Invalidates this signature of this method on the superclass.voidaddFunction(Function2D f)Adds aFunction2Dto thisFitter.voidaddFunction1Dx(Function f)voidaddFunction1Dy(Function f)doublecalculate()Calculates the χ² given parameter values set by aMinimizerusing reflection.voidsetData(double[] x, double[] y, double[] z)Fit the sum of functions to this set of points.voidsetData(double[] x, double[] y, double[] z, double[] errors)Fit the sum of functions to this set of points.-
Methods inherited from class joptima.Fitter
calculate, getName, getParameters, removeAllFunctions, setName
-
-
-
-
Constructor Detail
-
ChiSquaredFitter2D
public ChiSquaredFitter2D()
Constructor.
-
ChiSquaredFitter2D
public ChiSquaredFitter2D(double[] x, double[] y, double[] z)Constructor. Assumes sqrt(z) errors.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of points
-
ChiSquaredFitter2D
public ChiSquaredFitter2D(double[] x, double[] y, double[] z, double[] errors)Constructor.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of pointserrors- the error bars on z coordinates of points
-
ChiSquaredFitter2D
public ChiSquaredFitter2D(double[] x, double[] y, double[] z, Function f)Constructor. Assumes sqrt(y) errors.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of pointsf- theFunctionto fit to the given set of points
-
ChiSquaredFitter2D
public ChiSquaredFitter2D(double[] x, double[] y, double[] z, double[] errors, Function f)Constructor.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of pointserrors- the error bars on z coordinates of pointsf- theFunctionto fit to the given set of points
-
-
Method Detail
-
addFunction
public void addFunction(Function f)
Invalidates this signature of this method on the superclass. This form throws an UnsupportedOperationException() because its meaning is ambiguous for a 2D fitter. UseaddFunction(joptima.Function)instead.- Overrides:
addFunctionin classFitter
-
addFunction
public void addFunction(Function2D f)
Adds aFunction2Dto thisFitter.
-
addFunction1Dx
public void addFunction1Dx(Function f)
Adds a one-dimensionalFunctionto thisFitter. This form makes 2D extension of the given function along the y-axis, i.e. makes a Sum2D(function, null).- Parameters:
f- the one-dimensional function to extend along the y-axis
-
addFunction1Dy
public void addFunction1Dy(Function f)
Adds a one-dimensionalFunctionto thisFitter. This form makes 2D extension of the given function along the x-axis, i.e. makes a Sum2D(null, function).- Parameters:
f- the one-dimensional function to extend along the x-axis
-
setData
public void setData(double[] x, double[] y, double[] z)Fit the sum of functions to this set of points. Assumes sqrt(y) error bars.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of points
-
setData
public void setData(double[] x, double[] y, double[] z, double[] errors)Fit the sum of functions to this set of points.- Parameters:
x- the x coordinates of pointsy- the y coordinates of pointsz- the z coordinates of pointserrors- the error bars on y coordinates of points
-
calculate
public double calculate()
Calculates the χ² given parameter values set by aMinimizerusing reflection. The formula is:for each point in the data set { f = 0 for eachFunction{ f +=Function.calculate(double)(x[point], y[point]) } χ² += (y[point] - f)² / error[point]² }
-
-
DMelt 3.0 © DataMelt by jWork.ORG