java.lang.Object
org.bzdev.math.RealValuedFunctionVA
org.bzdev.math.RealValuedFunction
org.bzdev.math.LeastSquaresFit
org.bzdev.math.LeastSquaresFit.NonLinear
- All Implemented Interfaces:
DoubleUnaryOperator
,RealValuedDomainOps
,RealValuedFunctOps
,RealValuedFunctVAOps
,VADomainOps
- Enclosing class:
- LeastSquaresFit
Class to provide non-linear least-squares fits.
The implementation uses the Levenberg-Marquardt algorithm to
find the best fit.
Half of the constructors for this class take a final argument providing an initial guess of the parameters for the least squares fit because of the possibility of local minima, etc., that could lead to erroneous results or an exception being thrown by the constructor.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Class to provide data that controls the algorithm used to create a least-squares fit.Nested classes/interfaces inherited from class org.bzdev.math.LeastSquaresFit
LeastSquaresFit.BSpline, LeastSquaresFit.FunctionBasis, LeastSquaresFit.Linear, LeastSquaresFit.NonLinear, LeastSquaresFit.Polynomial
-
Field Summary
Fields inherited from class org.bzdev.math.RealValuedFunction
xFunction
-
Constructor Summary
ConstructorsConstructorDescriptionNonLinear
(double[] x, double[] y, double[] sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor given the standard deviation for the Y values.NonLinear
(double[] x, double[] y, double[] sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor given the standard deviation for the Y values and an initial guess for the fit's parameters.NonLinear
(double[] x, double[] y, double sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor given the standard deviation for the Y values.NonLinear
(double[] x, double[] y, double sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor given the standard deviation for the Y values and an initial guess for the fit's parameters.NonLinear
(double[] x, double[] y, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor.NonLinear
(double[] x, double[] y, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor with an initial guess for the fit's parameters. -
Method Summary
Modifier and TypeMethodDescriptiondouble
covariance
(double x1, double x2) Compute the covariance for the y values associated with two data points.protected void
Create the covariance array.double
derivAt
(double arg) Evaluate the function's first derivative.protected LeastSquaresFit
getFit()
Get a copy of this least squares fit but without its parameters, degrees of freedom, chi square value, reduced chi square value, or covariance set.double
secondDerivAt
(double arg) Evaluate the function's second derivative.protected void
setParameters
(double[] parameters) Set the parameters array.protected void
setVariance
(double value) Set the variance.double
valueAt
(double arg) Call the function.Methods inherited from class org.bzdev.math.LeastSquaresFit
chiSquare, chiSquare, getChiSquare, getCovarianceArray, getCovarianceMatrix, getCovarianceMatrix, getDegreesOfFreedom, getFunction, getNumberOfParameters, getParameters, getParameters, getParametersArray, getReducedChiSquare, getStat, setChiSquare, setCovariance, setDegreesOfFreedom, setReducedChiSquare, sumOfSquares
Methods inherited from class org.bzdev.math.RealValuedFunction
deriv, deriv, derivAt, domainMaxClosed, domainMaxClosed, domainMinClosed, domainMinClosed, getDomainMax, getDomainMax, getDomainMin, getDomainMin, isInDomain, isInDomain, secondDeriv, secondDerivAt, valueAt
Methods inherited from class org.bzdev.math.RealValuedFunctionVA
jacobian, jacobian, maxArgLength, minArgLength
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.bzdev.math.RealValuedFunctOps
andThen, andThen, applyAsDouble, compose, compose, maxArgLength, minArgLength
-
Constructor Details
-
NonLinear
public NonLinear(double[] x, double[] y, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. The real-valued function must implement the derivatives with respect to all its arguments except the first. Derivatives with respect to the first argument, however, must be implemented if the least-square fit's derivatives are used.- Parameters:
x
- the values for xiy
- the values for yirf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parameters- See Also:
-
NonLinear
public NonLinear(double[] x, double[] y, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor with an initial guess for the fit's parameters. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. The real-valued function must implement the derivatives with respect to all its arguments except the first. Derivatives with respect to the first argument, however, must be implemented if the least-square fit's derivatives are used. The order of the parameters provide in the initial guess is the same as those for the parameter arguments of the function fitting the data points.- Parameters:
x
- the values for xiy
- the values for yirf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parametersguess
- an initial guess for the parameters- Throws:
IllegalArgumentException
- the array guess is too short- See Also:
-
NonLinear
public NonLinear(double[] x, double[] y, double sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor given the standard deviation for the Y values. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. The real-valued function must implement the derivatives with respect to all its arguments except the first. Derivatives with respect to the first argument, however, must be implemented if the least-square fit's derivatives are used.- Parameters:
x
- the values for xiy
- the values for yisigma
- the standard deviation for every y valuerf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parameters- See Also:
-
NonLinear
public NonLinear(double[] x, double[] y, double sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor given the standard deviation for the Y values and an initial guess for the fit's parameters. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. The real-valued function must implement the derivatives with respect to all its arguments except the first. Derivatives with respect to the first argument, however, must be implemented if the least-square fit's derivatives are used. The order of the parameters provide in the initial guess is the same as those for the parameter arguments of the function fitting the data points.- Parameters:
x
- the values for xiy
- the values for yisigma
- the standard deviation for every y valuerf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parametersguess
- an initial guess for the parameters- Throws:
IllegalArgumentException
- the array guess is too short- See Also:
-
NonLinear
public NonLinear(double[] x, double[] y, double[] sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config) Constructor given the standard deviation for the Y values. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. parameters. The real-valued function must implement the derivatives with respect to all its arguments except the first. Derivatives with respect to the first argument, however, must be implemented if the least-square fit's derivatives are used.- Parameters:
y
- the values for yisigma
- the standard deviation for every y valuex
- the values for xirf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parameters- See Also:
-
NonLinear
public NonLinear(double[] x, double[] y, double[] sigma, RealValuedFunctionVA rf, LeastSquaresFit.NonLinear.Config config, double[] guess) Constructor given the standard deviation for the Y values and an initial guess for the fit's parameters. The first argument of the function is a value of x and the remaining arguments are parameters set by the least-squares fit. parameters. The real-valued function must implement the derivatives with respect to all its arguments except the first. The order of the parameters provide in the initial guess is the same as those for the parameter arguments of the function fitting the data points.- Parameters:
y
- the values for yisigma
- the standard deviation for every y valuex
- the values for xirf
- the function used to fit the data points given by x and yconfig
- parameters used to control the least-squares fit; null for a default set of parametersguess
- an initial guess for the parameters- Throws:
IllegalArgumentException
- the array guess is too short- See Also:
-
-
Method Details
-
getFit
Description copied from class:LeastSquaresFit
Get a copy of this least squares fit but without its parameters, degrees of freedom, chi square value, reduced chi square value, or covariance set.This method is used by
LeastSquaresFit.getFunction(double[])
, which only provides a real-valued function. The only methods that will be called on the value returned areLeastSquaresFit.setChiSquare(double)
,LeastSquaresFit.setDegreesOfFreedom(int)
,LeastSquaresFit.setReducedChiSquare(double)
,LeastSquaresFit.setParameters(double[])
,RealValuedFunction.valueAt(double)
,RealValuedFunction.derivAt(double)
, andRealValuedFunction.secondDerivAt(double)
.- Specified by:
getFit
in classLeastSquaresFit
- Returns:
- a least squares fit
-
setVariance
protected void setVariance(double value) Set the variance. The variance should be set to zero in cases where the covariance matrix should be set to zero. Otherwise, it should be set to a variance by which the computed covariances will be multiplied. This is intended for cases where the covariance is computed as if sigma had the value of 1.0 (some subclasses do this when all the values of sigma are the same).- Parameters:
value
- the value of the variance, which must not be negative.
-
createCovariance
protected void createCovariance()Description copied from class:LeastSquaresFit
Create the covariance array. Subclasses must implement this method, which is expected to set up the covariance array and then callLeastSquaresFit.setCovariance(double[][])
to make it available.- Specified by:
createCovariance
in classLeastSquaresFit
-
covariance
public double covariance(double x1, double x2) Description copied from class:LeastSquaresFit
Compute the covariance for the y values associated with two data points. The covariance for two random variables X and Y is E[(X-E[X](Y-E[Y])] where E[Z] is the expected value of a random variable Z. The covariance of X and X is simply the variance of X. The value returned allows one to estimate the error in the fit at any point, not just the data points, and whether errors at different values of X are correlated.- Specified by:
covariance
in classLeastSquaresFit
- Parameters:
x1
- the x value for the first y valuex2
- the x value for the second y value- Returns:
- the covariance
-
setParameters
protected void setParameters(double[] parameters) Description copied from class:LeastSquaresFit
Set the parameters array. Subclasses must call this method, generally in a constructor.- Overrides:
setParameters
in classLeastSquaresFit
- Parameters:
parameters
- the array to use to store parameters
-
valueAt
public double valueAt(double arg) Description copied from class:RealValuedFunction
Call the function.- Specified by:
valueAt
in interfaceRealValuedFunctOps
- Overrides:
valueAt
in classRealValuedFunction
- Parameters:
arg
- the function's argument- Returns:
- the value of the function for the given argument
-
derivAt
public double derivAt(double arg) Description copied from class:RealValuedFunction
Evaluate the function's first derivative.- Overrides:
derivAt
in classRealValuedFunction
- Parameters:
arg
- the function's argument- Returns:
- the value of the function for the given argument
-
secondDerivAt
public double secondDerivAt(double arg) Description copied from class:RealValuedFunction
Evaluate the function's second derivative.- Overrides:
secondDerivAt
in classRealValuedFunction
- Parameters:
arg
- the function's argument- Returns:
- the value of the function for the given argument
-