Package edu.cnu.mdi.splot.fit
Class PolynomialFitter
java.lang.Object
edu.cnu.mdi.splot.fit.ALeastSquaresFitter
edu.cnu.mdi.splot.fit.PolynomialFitter
- All Implemented Interfaces:
IFitStringGetter,IFitter
Polynomial least-squares fitter using Apache Commons Math 3.x least-squares
API.
Model:
y(x) = c0 + c1 x + c2 x^2 + ... + cN x^N
Returned parameter ordering:
params[k] = c_k
Initial guess
If the caller does not supply an initial guess, the fitter computes a weighted linear least-squares solution (viaLinearLeastSquaresGuesser) which is an excellent starting point
(often already the solution) for polynomials.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classCoefficient bounds for c0..cN. -
Field Summary
Fields inherited from class edu.cnu.mdi.splot.fit.ALeastSquaresFitter
CAPSIG, checker, covarianceThreshold, initialGuesser, maxEvaluations, maxIterations, MU, OMEGA, optimizer, PHI, SMALLSIG, SUB0, SUB1, SUB2, subArray, SUBN, SUP0, SUP1, SUP2, SUPN -
Constructor Summary
ConstructorsConstructorDescriptionPolynomialFitter(int degree) Create a polynomial fitter for a given degree using Levenberg-Marquardt and a robust default guess.PolynomialFitter(int degree, org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer optimizer) Create a polynomial fitter for a given degree with a custom optimizer. -
Method Summary
Modifier and TypeMethodDescriptionasEvaluator(FitResult fit) Create anEvaluatorthat evaluates this polynomial using Horner's method.protected double[]defaultInitialGuess(double[] x, double[] y, double[] weights) Default initial guess used if the caller does not provide an explicit override.fit(double[] x, double[] y, double[] weights, PolynomialFitter.ParameterBounds bounds, double[] initialGuess) Fit with optional weights, bounds, and explicit initial guess.static FitResultfit(int degree, double[] x, double[] y) Convenience: fit a polynomial of a given degree with unit weights.static FitResultfit(int degree, double[] x, double[] y, double[] weights) Convenience: fit a polynomial of a given degree with supplied weights.intprotected intGet the descriptive string getter for this fitter.protected org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunctionmodel(double[] x) Build the model + analytic (or numeric) Jacobian for this fitter, given x values.parameterName(int index) Get the parameter name for the given index.Methods inherited from class edu.cnu.mdi.splot.fit.ALeastSquaresFitter
buildFitResult, clampingValidator, defaultValidator, fit, fit, fit, getChecker, getCovarianceThreshold, getMaxEvaluations, getMaxIterations, safeCovariances, validateWeights, validateXY, weightsFromSigmaY
-
Constructor Details
-
PolynomialFitter
public PolynomialFitter(int degree) Create a polynomial fitter for a given degree using Levenberg-Marquardt and a robust default guess.- Parameters:
degree- polynomial degree (>= 0)
-
PolynomialFitter
public PolynomialFitter(int degree, org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer optimizer) Create a polynomial fitter for a given degree with a custom optimizer.- Parameters:
degree- polynomial degree (>= 0)optimizer- least-squares optimizer
-
-
Method Details
-
fit
Convenience: fit a polynomial of a given degree with unit weights. -
fit
Convenience: fit a polynomial of a given degree with supplied weights. -
getDegree
public int getDegree()- Returns:
- polynomial degree.
-
getParameterCount
protected int getParameterCount()- Specified by:
getParameterCountin classALeastSquaresFitter- Returns:
- number of parameters for this fitter.
-
model
protected org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction model(double[] x) Description copied from class:ALeastSquaresFitterBuild the model + analytic (or numeric) Jacobian for this fitter, given x values.- Specified by:
modelin classALeastSquaresFitter- Parameters:
x- x data (length n)- Returns:
- a
MultivariateJacobianFunctioncompatible with Commons Math least-squares
-
defaultInitialGuess
protected double[] defaultInitialGuess(double[] x, double[] y, double[] weights) Description copied from class:ALeastSquaresFitterDefault initial guess used if the caller does not provide an explicit override. The default implementation delegates to theALeastSquaresFitter.initialGuesser.Subclasses may override if they have a better model-specific guess.
- Overrides:
defaultInitialGuessin classALeastSquaresFitter- Parameters:
x- x datay- y dataweights- optional weights (may be null)- Returns:
- initial parameter vector of length
ALeastSquaresFitter.getParameterCount()
-
fit
public FitResult fit(double[] x, double[] y, double[] weights, PolynomialFitter.ParameterBounds bounds, double[] initialGuess) Fit with optional weights, bounds, and explicit initial guess.This is an "expert" overload that adapts polynomial coefficient bounds to the base class
ParameterValidatormechanism.- Parameters:
x- x datay- y dataweights- optional weights (length n). Typically weights = 1/sigmaY^2.bounds- optional coefficient bounds; if null, unboundedinitialGuess- optional coefficients length degree+1; if null, uses default guess- Returns:
- FitResult
-
asEvaluator
Create anEvaluatorthat evaluates this polynomial using Horner's method.- Specified by:
asEvaluatorin classALeastSquaresFitter- Parameters:
fit- fit result produced by this fitter- Returns:
- value getter
-
modelName
- Specified by:
modelNamein interfaceIFitStringGetter- Returns:
- model name suitable for a legend or report
-
functionForm
- Specified by:
functionFormin interfaceIFitStringGetter- Returns:
- mathematical function form suitable for display
-
parameterName
Get the parameter name for the given index.- Specified by:
parameterNamein interfaceIFitStringGetter- Parameters:
index- the parameter index- Returns:
- the parameter name
-
getStringGetter
Description copied from class:ALeastSquaresFitterGet the descriptive string getter for this fitter.- Specified by:
getStringGetterin classALeastSquaresFitter- Returns:
- the string getter
-