Class GaussianFitter

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

public final class GaussianFitter extends ALeastSquaresFitter
Nonlinear least-squares fitter for a 4-parameter Gaussian with baseline:
 y(x) = A * exp(-(x - mu) ^ 2 / (2 * sigma ^ 2)) + B
 

Parameter ordering

  • params[0] = A
  • params[1] = mu
  • params[2] = sigma
  • params[3] = B

Bounds / validation

This fitter enforces sigma >= DEFAULT_MIN_SIGMA by default using a parameter clamping validator. Optional bounds can also be supplied via GaussianFitter.ParameterBounds.
  • Field Details

    • IDX_A

      public static final int IDX_A
      Parameter indices.
      See Also:
    • IDX_MU

      public static final int IDX_MU
      See Also:
    • IDX_SIGMA

      public static final int IDX_SIGMA
      See Also:
    • IDX_B

      public static final int IDX_B
      See Also:
    • paramNames

      public static final String[] paramNames
      Parameter names.
    • DEFAULT_MIN_SIGMA

      public static final double DEFAULT_MIN_SIGMA
      Default minimum allowed sigma to avoid division by zero and ill-conditioned Jacobians.
      See Also:
  • Constructor Details

    • GaussianFitter

      public GaussianFitter()
      Create with a Levenberg-Marquardt optimizer and a robust default guesser.
    • GaussianFitter

      public GaussianFitter(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer optimizer)
      Create with a custom optimizer and robust default guesser.
  • Method Details

    • 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()
    • defaultValidator

      protected org.apache.commons.math3.fitting.leastsquares.ParameterValidator defaultValidator()
      Description copied from class: ALeastSquaresFitter
      Default validator/bounds policy (optional).

      Return null for "no validation". For simple bound constraints, consider using ALeastSquaresFitter.clampingValidator(double[], double[]).

      Overrides:
      defaultValidator in class ALeastSquaresFitter
      Returns:
      a parameter validator or null
    • fit

      public FitResult fit(double[] x, double[] y, double[] weights, GaussianFitter.ParameterBounds bounds, double[] initialGuess)
      Expert overload: fit with optional weights, optional bounds, and optional initial guess.

      If bounds is null, only sigma >= DEFAULT_MIN_SIGMA is enforced.

      Parameters:
      x - x data
      y - y data
      weights - optional weights (typically 1/sigmaY^2), may be null
      bounds - optional bounds, may be null
      initialGuess - optional initial guess, may be null
      Returns:
      fit result
    • asEvaluator

      public Evaluator asEvaluator(FitResult fit)
      Build a value getter using the fitted parameters.
      Specified by:
      asEvaluator in class ALeastSquaresFitter
      Parameters:
      fit - fit result
      Returns:
      an evaluator for the fitted Gaussian
    • modelName

      public String modelName()
      Returns:
      model name suitable for a legend or report
    • functionForm

      public String functionForm()
      Returns:
      mathematical function form suitable for display
    • parameterName

      public String parameterName(int index)
      Get the parameter name for the given index.
      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