Class LevenbergMarquardtSingleDimensionFitter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Indicates whether covariance must be adjusted or not.private com.irurueta.algebra.Matrix
Curvature matrix.static final boolean
Indicates whether covariance must be adjusted or not after fitting is finished.static final int
Default maximum number of iterations.static final int
Default convergence parameter.static final double
Default tolerance to reach convergence.Evaluator of functions.private boolean[]
Determines which parameters can be modified during estimation (if true) and which ones are locked (if false).private int
Maximum number of iterations.private int
Number of function parameters to be estimated.private int
Number of parameters to be fitted.private double
Mean square error.private int
Convergence parameter.private double
Tolerance to reach convergence.Fields inherited from class com.irurueta.numerical.fitting.SingleDimensionFitter
a, chisq, covar, ndat, sig, x, y
Fields inherited from class com.irurueta.numerical.fitting.Fitter
resultAvailable
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor.LevenbergMarquardtSingleDimensionFitter
(double[] x, double[] y, double sig) Constructor.LevenbergMarquardtSingleDimensionFitter
(double[] x, double[] y, double[] sig) Constructor.LevenbergMarquardtSingleDimensionFitter
(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator) Constructor.LevenbergMarquardtSingleDimensionFitter
(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator, double[] x, double[] y, double sig) Constructor.LevenbergMarquardtSingleDimensionFitter
(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator, double[] x, double[] y, double[] sig) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Adjusts covariance.private void
covsrt
(com.irurueta.algebra.Matrix covar) Expand in storage the covariance matrix covar, to take into account parameters that are being held fixed.void
fit()
Fits a function to provided data so that parameters associated to that function can be estimated along with their covariance matrix and chi square value.void
free
(int i) Releases parameter at position i of linear combination of basis functions, so it can be modified again if needed.com.irurueta.algebra.Matrix
getAlpha()
Returns curvature matrix.int
Returns degrees of freedom of computed chi square value.Returns function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parameterint
getItmax()
Returns maximum number of iterations.double
getMse()
Gets mean square error produced by estimated parameters respect to provided sample data.int
getNdone()
Returns convergence parameter.double
getP()
Gets the probability of finding a smaller chi square value.double
getQ()
Gets a measure of quality of estimated fit as a value between 0.0 and 1.0.double
getTol()
Returns tolerance to reach convergence.void
hold
(int i, double val) Prevents parameter at position i of linear combination of basis functions to be modified during function fitting.private void
Internal method to set function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parameterboolean
Indicates whether covariance must be adjusted or not.boolean
isReady()
Indicates whether provided instance has enough data to start the function fitting.private void
mrqcof
(double[] a, com.irurueta.algebra.Matrix alpha, double[] beta) Used byfit()
to evaluate the linearized fitting matrix alpha, and vector beta to calculate chi square.void
setCovarianceAdjusted
(boolean adjustCovariance) Specifies whether covariance must be adjusted or not.final void
Sets function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parametervoid
setItmax
(int itmax) Sets maximum number of iterations.void
setNdone
(int ndone) Sets convergence parameter.void
setTol
(double tol) Sets tolerance to reach convergence.private static void
swap
(double[] array1, double[] array2, int pos1, int pos2) Swaps values of arrays at provided positions.Methods inherited from class com.irurueta.numerical.fitting.SingleDimensionFitter
getA, getChisq, getCovar, getSig, getX, getY, setInputData, setInputData
Methods inherited from class com.irurueta.numerical.fitting.Fitter
isResultAvailable
-
Field Details
-
DEFAULT_NDONE
public static final int DEFAULT_NDONEDefault convergence parameter. Number of times that tolerance is assumed to be reached to consider that algorithm has finished iterating.- See Also:
-
DEFAULT_ITMAX
public static final int DEFAULT_ITMAXDefault maximum number of iterations.- See Also:
-
DEFAULT_TOL
public static final double DEFAULT_TOLDefault tolerance to reach convergence.- See Also:
-
DEFAULT_ADJUST_COVARIANCE
public static final boolean DEFAULT_ADJUST_COVARIANCEIndicates whether covariance must be adjusted or not after fitting is finished.- See Also:
-
ndone
private int ndoneConvergence parameter. -
itmax
private int itmaxMaximum number of iterations. -
tol
private double tolTolerance to reach convergence. -
evaluator
Evaluator of functions. -
ma
private int maNumber of function parameters to be estimated. -
ia
private boolean[] iaDetermines which parameters can be modified during estimation (if true) and which ones are locked (if false). -
alpha
private com.irurueta.algebra.Matrix alphaCurvature matrix. -
mfit
private int mfitNumber of parameters to be fitted. -
mse
private double mseMean square error. -
adjustCovariance
private boolean adjustCovarianceIndicates whether covariance must be adjusted or not. When covariance adjustment is enabled, then covariance is recomputed taking into account input samples, input standard deviations of the samples and jacobians of the model function overestimated parameters using the following expression: Cov = (J'*W*J)^-1 where: Cov is the covariance of estimated parameters J is a matrix containing the Jacobians of the function overestimated parameters for each input parameter x. Each row of J matrix contains an evaluation of the model function Jacobian for i-th input parameter x. Each column of J matrix contains the partial derivative of model function over j-th estimated parameter. W is the inverse of input variances. It's a diagonal matrix containing the reciprocal of the input variances (squared input standard deviations). That is: W = diag(w) where k element of w is wk = 1 / sigmak^2, which corresponds to the k-th standard deviation of input sample k. By default, covariance is adjusted after fitting finishes.
-
-
Constructor Details
-
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter()Constructor. -
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter(double[] x, double[] y, double[] sig) Constructor.- Parameters:
x
- input points x where function f(x) is evaluated.y
- result of evaluation of linear single dimensional function f(x) at provided x points.sig
- standard deviations of each pair of points (x, y).- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter(double[] x, double[] y, double sig) Constructor.- Parameters:
x
- input points x where function f(x) is evaluated.y
- result of evaluation of linear single dimensional function f(x) at provided x points.sig
- standard deviation of all pair of points assuming that standard deviations are constant.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.
-
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator) throws FittingException Constructor.- Parameters:
evaluator
- evaluator to evaluate function at provided point and obtain the evaluation of function basis at such point.- Throws:
FittingException
- if evaluation fails.
-
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator, double[] x, double[] y, double[] sig) throws FittingException Constructor.- Parameters:
evaluator
- evaluator to evaluate function at provided point and obtain the evaluation of function basis at such point.x
- input points x where function f(x) is evaluated.y
- result of evaluation of linear single dimensional function f(x) at provided x points.sig
- standard deviations of each pair of points (x, y).- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.FittingException
- if evaluation fails.
-
LevenbergMarquardtSingleDimensionFitter
public LevenbergMarquardtSingleDimensionFitter(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator, double[] x, double[] y, double sig) throws FittingException Constructor.- Parameters:
evaluator
- evaluator to evaluate function at provided point and obtain the evaluation of function basis at such point.x
- input points x where function f(x) is evaluated.y
- result of evaluation of linear single dimensional function f(x) at provided x points.sig
- standard deviation of all pair of points assuming that standard deviations are constant.- Throws:
IllegalArgumentException
- if provided arrays don't have the same length.FittingException
- if evaluation fails.
-
-
Method Details
-
getNdone
public int getNdone()Returns convergence parameter.- Returns:
- convergence parameter.
-
setNdone
public void setNdone(int ndone) Sets convergence parameter.- Parameters:
ndone
- convergence parameter.- Throws:
IllegalArgumentException
- if provided value is less than 1.
-
getItmax
public int getItmax()Returns maximum number of iterations.- Returns:
- maximum number of iterations.
-
setItmax
public void setItmax(int itmax) Sets maximum number of iterations.- Parameters:
itmax
- maximum number of iterations.- Throws:
IllegalArgumentException
- if provided value is zero or negative.
-
getTol
public double getTol()Returns tolerance to reach convergence.- Returns:
- tolerance to reach convergence.
-
setTol
public void setTol(double tol) Sets tolerance to reach convergence.- Parameters:
tol
- tolerance to reach convergence.- Throws:
IllegalArgumentException
- if provided value is zero or negative.
-
getFunctionEvaluator
Returns function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parameter- Returns:
- function evaluator
-
setFunctionEvaluator
public final void setFunctionEvaluator(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator) throws FittingException Sets function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parameter- Parameters:
evaluator
- function evaluator- Throws:
FittingException
- if evaluation fails
-
isReady
public boolean isReady()Indicates whether provided instance has enough data to start the function fitting. -
getAlpha
public com.irurueta.algebra.Matrix getAlpha()Returns curvature matrix. Curvature matrix is an indicatiion o fht curvature of the error of the function being fitted on parameters dimensions. The larger the curvatures are, the more likely that parameters can be correctly fitted because a deep enough valley has been found to converge to an optimal solution. Typically, curvature is proportional to the inverse of the covariance matrix.- Returns:
- curvature matrix.
-
getChisqDegreesOfFreedom
public int getChisqDegreesOfFreedom()Returns degrees of freedom of computed chi square value. Degrees of fredom is equal to the number of sampled data minus the number of estimated parameters.- Returns:
- degrees of freedom of computed chi square value.
-
getMse
public double getMse()Gets mean square error produced by estimated parameters respect to provided sample data.- Returns:
- mean square error.
-
getP
public double getP() throws com.irurueta.statistics.MaxIterationsExceededExceptionGets the probability of finding a smaller chi square value. The smaller the found chi square value is, the better the fit of the estimated parameters to the actual parameter. Thus, the smaller the chance of finding a smaller chi square value, then the better the estimated fit is.- Returns:
- probability of finding a smaller chi square value (better fit), expressed as a value between 0.0 and 1.0.
- Throws:
com.irurueta.statistics.MaxIterationsExceededException
- if convergence of incomplete gamma function cannot be reached. This is rarely thrown and happens usually for numerically unstable input values.
-
getQ
public double getQ() throws com.irurueta.statistics.MaxIterationsExceededExceptionGets a measure of quality of estimated fit as a value between 0.0 and 1.0. The larger the quality value is, the better the fit that has been estimated.- Returns:
- measure of quality of estimated fit.
- Throws:
com.irurueta.statistics.MaxIterationsExceededException
- if convergence of incomplete gamma function cannot be reached. This is rarely thrown and happens usually for numerically unstable input values.
-
isCovarianceAdjusted
public boolean isCovarianceAdjusted()Indicates whether covariance must be adjusted or not. When covariance adjustment is enabled, then covariance is recomputed taking into account input samples, input standard deviations of the samples and jacobians of the model function overestimated parameters using the following expression: Cov = (J'*W*J)^-1 where: Cov is the covariance of estimated parameters J is a matrix containing the Jacobians of the function overestimated parameters for each input parameter x. Each row of J matrix contains an evaluation of the model function Jacobian for i-th input parameter x. Each column of J matrix contains the partial derivative of model function over j-th estimated parameter. W is the inverse of input variances. It's a diagonal matrix containing the reciprocal of the input variances (squared input standard deviations). That is: W = diag(w) where k element of w is wk = 1 / sigmak^2, which corresponds to the k-th standard deviation of input sample k. By default, covariance is adjusted after fitting finishes. More info about confidence os estimated parameters can be found here: http://people.duke.edu/~hpgavin/ce281/lm.pdf https://www8.cs.umu.se/kurser/5DA001/HT07/lectures/lsq-handouts.pdf Numerical Recipes 3rd Ed, page 812- Returns:
- true if covariance must be adjusted, false otherwise.
-
setCovarianceAdjusted
public void setCovarianceAdjusted(boolean adjustCovariance) Specifies whether covariance must be adjusted or not. When covariance adjustment is enabled, then covariance is recomputed taking into account input samples, input standard deviations of the samples and jacobians of the model function overestimated parameters using the following expression: Cov = (J'*W*J)^-1 where: Cov is the covariance of estimated parameters J is a matrix containing the Jacobians of the function overestimated parameters for each input parameter x. Each row of J matrix contains an evaluation of the model function Jacobian for i-th input parameter x. Each column of J matrix contains the partial derivative of model function over j-th estimated parameter. W is the inverse of input variances. It's a diagonal matrix containing the reciprocal of the input variances (squared input standard deviations). That is: W = diag(w) where k element of w is wk = 1 / sigmak^2, which corresponds to the k-th standard deviation of input sample k. By default, covariance is adjusted after fitting finishes.- Parameters:
adjustCovariance
- true if covariance must be adjusted, false otherwise.
-
fit
Fits a function to provided data so that parameters associated to that function can be estimated along with their covariance matrix and chi square value. If chi square value is close to 1, the fit is usually good. If it is much larger, then error cannot be properly fitted. If it is close to zero, then the model overfits the error. MethodsgetP()
andgetQ()
can also be used to determine the quality of the fit.- Specified by:
fit
in classFitter
- Throws:
FittingException
- if fitting fails.NotReadyException
- if enough input data has not yet been provided.
-
hold
public void hold(int i, double val) Prevents parameter at position i of linear combination of basis functions to be modified during function fitting.- Parameters:
i
- position of parameter to be retained.val
- value to be set for parameter at position i.
-
free
public void free(int i) Releases parameter at position i of linear combination of basis functions, so it can be modified again if needed.- Parameters:
i
- position of parameter to be released.
-
adjustCovariance
Adjusts covariance. Covariance must be adjusted to produce more real results close to the scale of problem, otherwise estimated covariance will just be a measure of goodness similar to chi square value because it will be the inverse of the curvature matrix, which is just a solution of the covariance up to scale.Covariance is adjusted taking into account input samples, input standard deviations of the samples and jacobians of the model function overestimated parameters using the following expression: Cov = (J'*W*J)^-1 where: Cov is the covariance of estimated parameters J is a matrix containing the Jacobians of the function overestimated parameters for each input parameter x. Each row of J matrix contains an evaluation of the model function Jacobian for i-th input parameter x. Each column of J matrix contains the partial derivative of model function over j-th estimated parameter. W is the inverse of input variances. It's a diagonal matrix containing the reciprocal of the input variances (squared input standard deviations). That is: W = diag(w) where k element of w is wk = 1 / sigmak^2, which corresponds to the k-th standard deviation of input sample k.
- Throws:
com.irurueta.algebra.AlgebraException
- if there are numerical instabilities.EvaluationException
- if function evaluation fails.
-
internalSetFunctionEvaluator
private void internalSetFunctionEvaluator(LevenbergMarquardtSingleDimensionFunctionEvaluator evaluator) throws FittingException Internal method to set function evaluator to evaluate function at a given point and obtain function derivatives respect to each provided parameter- Parameters:
evaluator
- function evaluator- Throws:
FittingException
- if evaluation fails
-
mrqcof
private void mrqcof(double[] a, com.irurueta.algebra.Matrix alpha, double[] beta) throws EvaluationException Used byfit()
to evaluate the linearized fitting matrix alpha, and vector beta to calculate chi square.- Parameters:
a
- estimated parameters so far.alpha
- curvature (i.e. fitting) matrix.beta
- array where derivative increments for each parameter are stored.- Throws:
EvaluationException
- if function evaluation fails.
-
covsrt
private void covsrt(com.irurueta.algebra.Matrix covar) Expand in storage the covariance matrix covar, to take into account parameters that are being held fixed. (For the latter, return zero covariances).- Parameters:
covar
- covariance matrix.
-
swap
private static void swap(double[] array1, double[] array2, int pos1, int pos2) Swaps values of arrays at provided positions.- Parameters:
array1
- 1st array.array2
- 2nd array.pos1
- 1st position.pos2
- 2nd position.
-