Class PolynomialFitter

java.lang.Object
edu.cnu.mdi.splot.fit.ALeastSquaresFitter
edu.cnu.mdi.splot.fit.PolynomialFitter
All Implemented Interfaces:
IFitStringGetter, IFitter

public final class PolynomialFitter extends ALeastSquaresFitter implements 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 (via LinearLeastSquaresGuesser) which is an excellent starting point (often already the solution) for polynomials.
  • 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

      public static FitResult fit(int degree, double[] x, double[] y)
      Convenience: fit a polynomial of a given degree with unit weights.
    • fit

      public static FitResult fit(int degree, double[] x, double[] y, double[] weights)
      Convenience: fit a polynomial of a given degree with supplied weights.
    • getDegree

      public int getDegree()
      Returns:
      polynomial degree.
    • getParameterCount

      protected int getParameterCount()
      Specified by:
      getParameterCount in class ALeastSquaresFitter
      Returns:
      number of parameters for this fitter.
    • model

      protected org.apache.commons.math3.fitting.leastsquares.MultivariateJacobianFunction model(double[] x)
      Description copied from class: ALeastSquaresFitter
      Build the model + analytic (or numeric) Jacobian for this fitter, given x values.
      Specified by:
      model in class ALeastSquaresFitter
      Parameters:
      x - x data (length n)
      Returns:
      a MultivariateJacobianFunction compatible with Commons Math least-squares
    • defaultInitialGuess

      protected double[] defaultInitialGuess(double[] x, double[] y, double[] weights)
      Description copied from class: ALeastSquaresFitter
      Default initial guess used if the caller does not provide an explicit override. The default implementation delegates to the ALeastSquaresFitter.initialGuesser.

      Subclasses may override if they have a better model-specific guess.

      Overrides:
      defaultInitialGuess in class ALeastSquaresFitter
      Parameters:
      x - x data
      y - y data
      weights - 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 ParameterValidator mechanism.

      Parameters:
      x - x data
      y - y data
      weights - optional weights (length n). Typically weights = 1/sigmaY^2.
      bounds - optional coefficient bounds; if null, unbounded
      initialGuess - optional coefficients length degree+1; if null, uses default guess
      Returns:
      FitResult
    • asEvaluator

      public Evaluator asEvaluator(FitResult fit)
      Create an Evaluator that evaluates this polynomial using Horner's method.
      Specified by:
      asEvaluator in class ALeastSquaresFitter
      Parameters:
      fit - fit result produced by this fitter
      Returns:
      value getter
    • modelName

      public String modelName()
      Specified by:
      modelName in interface IFitStringGetter
      Returns:
      model name suitable for a legend or report
    • functionForm

      public String functionForm()
      Specified by:
      functionForm in interface IFitStringGetter
      Returns:
      mathematical function form suitable for display
    • parameterName

      public String parameterName(int index)
      Get the parameter name for the given index.
      Specified by:
      parameterName in interface IFitStringGetter
      Parameters:
      index - the parameter index
      Returns:
      the parameter name
    • getStringGetter

      public IFitStringGetter getStringGetter()
      Description copied from class: ALeastSquaresFitter
      Get the descriptive string getter for this fitter.
      Specified by:
      getStringGetter in class ALeastSquaresFitter
      Returns:
      the string getter