Statistics[NonlinearFit]  fit a nonlinear model function to data

Calling Sequence


NonlinearFit(falg, X, Y, v, options)
NonlinearFit(falg, XY, v, options)
NonlinearFit(fop, X, Y, options)
NonlinearFit(fop, XY, options)


Parameters


falg



algebraic; model function in algebraic form

X



Vector or Matrix; values of independent variable(s)

Y



Vector; values of dependent variable

XY



Matrix; values of independent and dependent variables

v



name or list(names); name(s) of independent variables in the model function

fop



procedure; model function in operator form

options



(optional) equation(s) of the form option=value where option is one of initialvalues, output, parameternames, parameterranges and weights; specify options for the NonlinearFit command





Description


•

The NonlinearFit command fits a model function that is nonlinear in the model parameters to data by minimizing the leastsquares error. If you are not sure if the model function is linear or not, it is recommended to use the Statistics[Fit] command, which will call this command or Statistics[LinearFit] depending on linearity of the model.

•

The NonlinearFit command minimizes the error in a local sense; see the Notes section below, in particular the advice regarding the initialvalues option.

•

This help page describes how to use the NonlinearFit command with algebraicform and operatorform input. An advanced form of the command is described in the Statistics/NonlinearFitMatrixForm help page. For more information about the input forms, see the Input Forms help page.

•

The parameter X is a Matrix containing the values of the independent variables. Row i in the Matrix contains the n values for the ith data point while column j contains all values of the single variable . If there is only one independent variable, X can be either a Vector or a kby1 Matrix. The parameter Y is a Vector containing the k values of the dependent variable y. The parameter XY is a Matrix consisting of the n columns of X and, as last column, Y. For X, Y, and XY, one can also use lists or Arrays; for details, see the Input Forms help page.

•

The parameter v is a list of the independent variable names used in falg. If there is only one independent variable, then v can be a single name. The order of the names in the list must match exactly the order in which the independent variable values are placed in the columns of X.

•

By default, either the model function with the final parameter values or a Vector containing the parameter values is returned, depending on the input form. Additional results or a solution module that allows you to query for various settings and results can be obtained with the output option. For more information, see the Statistics/Regression/Solution help page.

•

Weights for the data points can be supplied through the weights option.



Options



The options argument can contain one or more of the options shown below. These options are described in more detail on the Statistics/Regression/Options help page.

•

initialvalues = set(equation), list(equation), list(realcons) or Vector(realcons)  Provide initial values for the parameters.

•

output = name or string  Specify the form of the solution. The output option can take as a value the name solutionmodule, or one of the following names (or a list of these names): degreesoffreedom, leastsquaresfunction, parametervalues, parametervector, residuals, residualmeansquare, residualstandarddeviation, residualsumofsquares. For more information, see the Statistics/Regression/Solution help page.

•

parameternames = list(name)  Specify the order of parameter names in the model function. This determines the order in which values are placed in Vectorvalued results.

•

parameterranges = list(name=range), list(range)  Specify the allowable range for each parameter.

•

weights = Vector  Provide weights for the data points.



Notes


•

The underlying computation is done in floatingpoint; therefore, all data points must have type realcons and all returned solutions are floatingpoint, even if the problem is specified with exact values. For more information about numeric computation in the Statistics package, see the Statistics/Computation help page.

•

The NonlinearFit command relies on the Matrixform version of the Optimization[LSSolve] command, which in turns uses various methods implemented in a builtin library provided by the Numerical Algorithms Group (NAG). More information is available on the Optimization[LSSolveMatrixForm] help page. Additional options listed on that page may be provided to the NonlinearFit command and are passed directly to the LSSolve command.

•

The Optimization[LSSolve] command computes only local solutions to nonlinear leastsquares problems. The parameter values returned by NonlinearFit minimize the sum of the residuals squared in a local sense. When the results returned are unexpected, it is highly recommended that you provide initial values for the parameters using the initialvalues option.

•

To obtain more details as the leastsquares problem is being solved, set infolevel[Statistics] to 1 or higher. To obtain details about the progress of the Optimization solver, set to 1 or higher.



Compatibility


•

The XY parameter was introduced in Maple 15.



Examples


>


>


>


>


 (1) 
Consider now an experiment where quantities , , and are quantities influencing a quantity according to an approximate relationship
with unknown parameters , , and . Six data points are given by the following matrix, with respective columns for , , , and .
>


 (2) 
We take an initial guess that the first term will be approximately quadratic in , that will be approximately , and for we don't even know whether it's going to be positive or negative, so we guess . We compute both the model function and the residuals.
>


 (3) 
We see that the exponent on is only about , and the other guesses were not very good either. However, this problem is conditioned well enough that Maple finds a good fit anyway.
Finally, consider a situation where an ordinary differential equation leads to results that need to be fitted. The system is given by
where and are parameters that we want to find, is a variable that we can vary between experiments, and is a quantity that we can measure at . We perform 10 experiments at , and the results are as follows.
>


 (4) 
>


 (5) 
We now need to set up the procedure (called fop in the calling sequences above) that NonlinearFit can call to obtain the value for a given input value and a given pair of parameters and . We do this using dsolve/numeric.
>


 (6) 
>


 (7) 
We now have a procedure ODE_Solution that can compute the correct value, but we cannot call that with the values for z, a, and b, and expect to get the correct answer. We first need to call it once to set the parameters, then another time to obtain the value of at , and then return this value (for more information about how this works, see dsolve/numeric). By hand, we can do this as follows:
>


 (8) 
>


 (9) 
>


 (10) 
>


Note that for some settings of the parameters, we cannot obtain a solution. We need to take care of this in the fop procedure we create (which we call f), by returning a value that is very far from all output points, leading to a very bad fit for these erroneous parameter values.
>

f := proc(zValue, aValue, bValue) global ODE_Solution, a, b, z, x, t; ODE_Solution('parameters' = [a = aValue, b = bValue, z = zValue]); try return eval(x(t), ODE_Solution(1)); catch: return 100; end try; end proc;

 (11) 
>


 (12) 
With this setup, we can perform an initial attempt at fitting the data.
>


 (13) 
That is an extremely bad fit, and indeed this is a case where the solution to the ODE is undefined. Since NonlinearFit does local optimization, it never finds a point where the result is anything other than the error return value, . The solution is to provide an initial estimate. We will go with the values that provided a solution above: .
>


 (14) 
This is a much better fit.


See Also


CurveFitting, Optimization[LSSolveMatrixForm], Statistics, Statistics/Computation, Statistics/MaximumLikelihoodEstimate, Statistics/NonlinearFitMatrixForm, Statistics/Regression, Statistics/Regression/InputForms, Statistics/Regression/Options, Statistics/Regression/Solution, Statistics[Fit], Statistics[LinearFit]

