Class LeastSquaresFit.NonLinear

All Implemented Interfaces:
DoubleUnaryOperator, RealValuedDomainOps, RealValuedFunctOps, RealValuedFunctVAOps, VADomainOps
Enclosing class:
LeastSquaresFit

public static class LeastSquaresFit.NonLinear extends 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.

  • 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 xi
      y - the values for yi
      rf - the function used to fit the data points given by x and y
      config - 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 xi
      y - the values for yi
      rf - the function used to fit the data points given by x and y
      config - parameters used to control the least-squares fit; null for a default set of parameters
      guess - 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 xi
      y - the values for yi
      sigma - the standard deviation for every y value
      rf - the function used to fit the data points given by x and y
      config - 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 xi
      y - the values for yi
      sigma - the standard deviation for every y value
      rf - the function used to fit the data points given by x and y
      config - parameters used to control the least-squares fit; null for a default set of parameters
      guess - 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 yi
      sigma - the standard deviation for every y value
      x - the values for xi
      rf - the function used to fit the data points given by x and y
      config - 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 yi
      sigma - the standard deviation for every y value
      x - the values for xi
      rf - the function used to fit the data points given by x and y
      config - parameters used to control the least-squares fit; null for a default set of parameters
      guess - an initial guess for the parameters
      Throws:
      IllegalArgumentException - the array guess is too short
      See Also:
  • Method Details

    • getFit

      protected LeastSquaresFit 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 are LeastSquaresFit.setChiSquare(double), LeastSquaresFit.setDegreesOfFreedom(int), LeastSquaresFit.setReducedChiSquare(double), LeastSquaresFit.setParameters(double[]), RealValuedFunction.valueAt(double), RealValuedFunction.derivAt(double), and RealValuedFunction.secondDerivAt(double).

      Specified by:
      getFit in class LeastSquaresFit
      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 call LeastSquaresFit.setCovariance(double[][]) to make it available.
      Specified by:
      createCovariance in class LeastSquaresFit
    • 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 class LeastSquaresFit
      Parameters:
      x1 - the x value for the first y value
      x2 - 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 class LeastSquaresFit
      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 interface RealValuedFunctOps
      Overrides:
      valueAt in class RealValuedFunction
      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 class RealValuedFunction
      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 class RealValuedFunction
      Parameters:
      arg - the function's argument
      Returns:
      the value of the function for the given argument