| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes functions for multidimensional nonlinear least-squares fitting. The library provides low level components for a variety of iterative solvers and convergence tests. These can be combined by the user to achieve the desired solution, with full access to the intermediate steps of the iteration. Each class of methods uses the same framework, so that you can switch between solvers at runtime without needing to recompile your program. Each instance of a solver keeps track of its own state, allowing the solvers to be used in multi-threaded programs.
The header file ‘gsl_multifit_nlin.h’ contains prototypes for the multidimensional nonlinear fitting functions and related declarations.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The problem of multidimensional nonlinear least-squares fitting requires
the minimization of the squared residuals of
functions,
, in
parameters,
,
All algorithms proceed from an initial guess using the linearization,
where
is the initial point,
is the proposed step
and
is the
Jacobian matrix
.
Additional strategies are used to enlarge the region of convergence.
These include requiring a decrease in the norm
on each
step or using a trust region to avoid steps which fall outside the linear
regime.
To perform a weighted least-squares fit of a nonlinear model
to data (
,
) with independent gaussian
errors
, use function components of the following form,
Note that the model parameters are denoted by
in this chapter
since the non-linear least-squares algorithms are described
geometrically (i.e. finding the minimum of a surface). The
independent variable of any data to be fitted is denoted by
.
With the definition above the Jacobian is
, where
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns a pointer to a newly allocated instance of a solver of type T for n observations and p parameters. The number of observations n must be greater than or equal to parameters p.
If there is insufficient memory to create the solver then the function
returns a null pointer and the error handler is invoked with an error
code of GSL_ENOMEM.
This function returns a pointer to a newly allocated instance of a derivative solver of type T for n observations and p parameters. For example, the following code creates an instance of a Levenberg-Marquardt solver for 100 data points and 3 parameters,
const gsl_multifit_fdfsolver_type * T
= gsl_multifit_fdfsolver_lmder;
gsl_multifit_fdfsolver * s
= gsl_multifit_fdfsolver_alloc (T, 100, 3);
|
The number of observations n must be greater than or equal to parameters p.
If there is insufficient memory to create the solver then the function
returns a null pointer and the error handler is invoked with an error
code of GSL_ENOMEM.
This function initializes, or reinitializes, an existing solver s to use the function f and the initial guess x.
This function initializes, or reinitializes, an existing solver s to use the function and derivative fdf and the initial guess x.
These functions free all the memory associated with the solver s.
These functions return a pointer to the name of the solver. For example,
printf ("s is a '%s' solver\n",
gsl_multifit_fdfsolver_name (s));
|
would print something like s is a 'lmder' solver.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
You must provide
functions of
variables for the
minimization algorithms to operate on. In order to allow for
arbitrary parameters the functions are defined by the following data
types:
This data type defines a general system of functions with arbitrary parameters.
int (* f) (const gsl_vector * x, void * params, gsl_vector * f)this function should store the vector result
in f for argument x and arbitrary parameters params,
returning an appropriate error code if the function cannot be computed.
size_t nthe number of functions, i.e. the number of components of the vector f.
size_t pthe number of independent variables, i.e. the number of components of the vector x.
void * paramsa pointer to the arbitrary parameters of the function.
This data type defines a general system of functions with arbitrary parameters and the corresponding Jacobian matrix of derivatives,
int (* f) (const gsl_vector * x, void * params, gsl_vector * f)this function should store the vector result
in f for argument x and arbitrary parameters params,
returning an appropriate error code if the function cannot be computed.
int (* df) (const gsl_vector * x, void * params, gsl_matrix * J)this function should store the n-by-p matrix result
in J for argument x
and arbitrary parameters params, returning an appropriate error code if the
function cannot be computed.
int (* fdf) (const gsl_vector * x, void * params, gsl_vector * f, gsl_matrix * J)This function should set the values of the f and J as above,
for arguments x and arbitrary parameters params. This function
provides an optimization of the separate functions for
and
—it is always faster to compute the function and its
derivative at the same time.
size_t nthe number of functions, i.e. the number of components of the vector f.
size_t pthe number of independent variables, i.e. the number of components of the vector x.
void * paramsa pointer to the arbitrary parameters of the function.
Note that when fitting a non-linear model against experimental data, the data is passed to the functions above using the params argument and the trial best-fit parameters through the x argument.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following functions drive the iteration of each algorithm. Each function performs one iteration to update the state of any solver of the corresponding type. The same functions work for all solvers so that different methods can be substituted at runtime without modifications to the code.
These functions perform a single iteration of the solver s. If the iteration encounters an unexpected problem then an error code will be returned. The solver maintains a current estimate of the best-fit parameters at all times.
The solver struct s contains the following entries, which can be used to track the progress of the solution:
gsl_vector * xThe current position.
gsl_vector * fThe function value at the current position.
gsl_vector * dxThe difference between the current position and the previous position, i.e. the last step, taken as a vector.
gsl_matrix * JThe Jacobian matrix at the current position (for the
gsl_multifit_fdfsolver struct only)
The best-fit information also can be accessed with the following auxiliary functions,
These functions return the current position (i.e. best-fit parameters)
s->x of the solver s.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A minimization procedure should stop when one of the following conditions is true:
The handling of these conditions is under user control. The functions below allow the user to test the current estimate of the best-fit parameters in several standard ways.
This function tests for the convergence of the sequence by comparing the
last step dx with the absolute error epsabs and relative
error epsrel to the current position x. The test returns
GSL_SUCCESS if the following condition is achieved,
for each component of x and returns GSL_CONTINUE otherwise.
This function tests the residual gradient g against the absolute
error bound epsabs. Mathematically, the gradient should be
exactly zero at the minimum. The test returns GSL_SUCCESS if the
following condition is achieved,
and returns GSL_CONTINUE otherwise. This criterion is suitable
for situations where the precise location of the minimum,
,
is unimportant provided a value can be found where the gradient is small
enough.
This function computes the gradient g of
from the Jacobian matrix
and the function values
f, using the formula
.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The minimization algorithms described in this section make use of both the function and its derivative. They require an initial guess for the location of the minimum. There is no absolute guarantee of convergence—the function must be suitable for this technique and the initial guess must be sufficiently close to the minimum for it to work.
This is a robust and efficient version of the Levenberg-Marquardt algorithm as implemented in the scaled LMDER routine in MINPACK. Minpack was written by Jorge J. Moré, Burton S. Garbow and Kenneth E. Hillstrom.
The algorithm uses a generalized trust region to keep each step under
control. In order to be accepted a proposed new position
must
satisfy the condition
, where
is a
diagonal scaling matrix and
is the size of the trust
region. The components of
are computed internally, using the
column norms of the Jacobian to estimate the sensitivity of the residual
to each component of
. This improves the behavior of the
algorithm for badly scaled functions.
On each iteration the algorithm attempts to minimize the linear system
subject to the constraint
. The
solution to this constrained linear system is found using the
Levenberg-Marquardt method.
The proposed step is now tested by evaluating the function at the
resulting point,
. If the step reduces the norm of the
function sufficiently, and follows the predicted behavior of the
function within the trust region, then it is accepted and the size of the
trust region is increased. If the proposed step fails to improve the
solution, or differs significantly from the expected behavior within
the trust region, then the size of the trust region is decreased and
another trial step is computed.
The algorithm also monitors the progress of the solution and returns an error if the changes in the solution are smaller than the machine precision. The possible error codes are,
GSL_ETOLFthe decrease in the function falls below machine precision
GSL_ETOLXthe change in the position vector falls below machine precision
GSL_ETOLGthe norm of the gradient, relative to the norm of the function, falls below machine precision
These error codes indicate that further iterations will be unlikely to change the solution from its current value.
This is an unscaled version of the LMDER algorithm. The elements of the
diagonal scaling matrix
are set to 1. This algorithm may be
useful in circumstances where the scaled version of LMDER converges too
slowly, or the function is already scaled appropriately.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
There are no algorithms implemented in this section at the moment.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function uses the Jacobian matrix J to compute the covariance matrix of the best-fit parameters, covar. The parameter epsrel is used to remove linear-dependent columns when J is rank deficient.
The covariance matrix is given by,
and is computed by QR decomposition of J with column-pivoting. Any
columns of
which satisfy
are considered linearly-dependent and are excluded from the covariance
matrix (the corresponding rows and columns of the covariance matrix are
set to zero).
If the minimisation uses the weighted least-squares function
then the covariance
matrix above gives the statistical error on the best-fit parameters
resulting from the gaussian errors
on
the underlying data
. This can be verified from the relation
and the fact that the fluctuations in
from the data
are normalised by
and
so satisfy
.
For an unweighted least-squares function
the covariance matrix above should be multiplied by the variance
of the residuals about the best-fit
to give the variance-covariance
matrix
. This estimates the statistical error on the
best-fit parameters from the scatter of the underlying data.
For more information about covariance matrices see Overview.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following example program fits a weighted exponential model with
background to experimental data,
. The
first part of the program sets up the functions expb_f and
expb_df to calculate the model and its Jacobian. The appropriate
fitting function is given by,
where we have chosen
. The Jacobian matrix
is
the derivative of these functions with respect to the three parameters
(
,
,
). It is given by,
where
,
and
.
|
The main part of the program sets up a Levenberg-Marquardt solver and some simulated random data. The data uses the known parameters (1.0,5.0,0.1) combined with gaussian noise (standard deviation = 0.1) over a range of 40 timesteps. The initial guess for the parameters is chosen as (0.0, 1.0, 0.0).
|
The iteration terminates when the change in x is smaller than 0.0001, as both an absolute and relative change. Here are the results of running the program:
iter: 0 x=1.00000000 0.00000000 0.00000000 |f(x)|=117.349 status=success iter: 1 x=1.64659312 0.01814772 0.64659312 |f(x)|=76.4578 status=success iter: 2 x=2.85876037 0.08092095 1.44796363 |f(x)|=37.6838 status=success iter: 3 x=4.94899512 0.11942928 1.09457665 |f(x)|=9.58079 status=success iter: 4 x=5.02175572 0.10287787 1.03388354 |f(x)|=5.63049 status=success iter: 5 x=5.04520433 0.10405523 1.01941607 |f(x)|=5.44398 status=success iter: 6 x=5.04535782 0.10404906 1.01924871 |f(x)|=5.44397 chisq/dof = 0.800996 A = 5.04536 +/- 0.06028 lambda = 0.10405 +/- 0.00316 b = 1.01925 +/- 0.03782 status = success |
The approximate values of the parameters are found correctly, and the chi-squared value indicates a good fit (the chi-squared per degree of freedom is approximately 1). In this case the errors on the parameters can be estimated from the square roots of the diagonal elements of the covariance matrix.
If the chi-squared value shows a poor fit (i.e.
) then the error estimates obtained from the
covariance matrix will be too small. In the example program the error estimates
are multiplied by
in this case, a common way of increasing the
errors for a poor fit. Note that a poor fit will result from the use
an inappropriate model, and the scaled error estimates may then
be outside the range of validity for gaussian errors.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The MINPACK algorithm is described in the following article,
The following paper is also relevant to the algorithms described in this section,
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Autobuild on September, 26 2007 using texi2html 1.78.