NAG Library Routine Document
e04hef
(lsq_uncon_mod_deriv2_comp)
1
Purpose
e04hef is a comprehensive modified Gauss–Newton algorithm for finding an unconstrained minimum of a sum of squares of nonlinear functions in variables . First and second derivatives are required.
The routine is intended for functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).
2
Specification
Fortran Interface
Subroutine e04hef ( |
m,
n,
lsqfun,
lsqhes,
lsqmon,
iprint,
maxcal,
eta,
xtol,
stepmx,
x,
fsumsq,
fvec,
fjac,
ldfjac,
s,
v,
ldv,
niter,
nf,
iw,
liw,
w,
lw,
ifail) |
Integer, Intent (In) | :: |
m,
n,
iprint,
maxcal,
ldfjac,
ldv,
liw,
lw | Integer, Intent (Inout) | :: |
iw(liw),
ifail | Integer, Intent (Out) | :: |
niter,
nf | Real (Kind=nag_wp), Intent (In) | :: |
eta,
xtol,
stepmx | Real (Kind=nag_wp), Intent (Inout) | :: |
x(n),
fjac(ldfjac,n),
v(ldv,n),
w(lw) | Real (Kind=nag_wp), Intent (Out) | :: |
fsumsq,
fvec(m),
s(n) | External | :: |
lsqfun,
lsqhes,
lsqmon |
|
C Header Interface
#include nagmk26.h
void |
e04hef_ (
const Integer *m,
const Integer *n,
void (NAG_CALL *lsqfun)(
Integer *iflag,
const Integer *m,
const Integer *n,
const double xc[],
double fvec[],
double fjac[],
const Integer *ldfjac,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw),
void (NAG_CALL *lsqhes)(
Integer *iflag,
const Integer *m,
const Integer *n,
const double fvec[],
const double xc[],
double b[],
const Integer *lb,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw),
void (NAG_CALL *lsqmon)(
const Integer *m,
const Integer *n,
const double xc[],
const double fvec[],
const double fjac[],
const Integer *ldfjac,
const double s[],
const Integer *igrade,
const Integer *niter,
const Integer *nf,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw),
const Integer *iprint,
const Integer *maxcal,
const double *eta,
const double *xtol,
const double *stepmx,
double x[],
double *fsumsq,
double fvec[],
double fjac[],
const Integer *ldfjac,
double s[],
double v[],
const Integer *ldv,
Integer *niter,
Integer *nf,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw,
Integer *ifail) |
|
3
Description
e04hef is essentially identical to the subroutine LSQSDN in the NPL Algorithms Library. It is applicable to problems of the form:
where
and
. (The functions
are often referred to as ‘residuals’.)
You must supply subroutines to calculate the values of the and their first derivatives and second derivatives at any point .
From a starting point
supplied by you, the routine generates a sequence of points
, which is intended to converge to a local minimum of
. The sequence of points is given by
where the vector
is a direction of search, and
is chosen such that
is approximately a minimum with respect to
.
The vector used depends upon the reduction in the sum of squares obtained during the last iteration. If the sum of squares was sufficiently reduced, then is the Gauss–Newton direction; otherwise the second derivatives of the are taken into account.
The method is designed to ensure that steady progress is made whatever the starting point, and to have the rapid ultimate convergence of Newton's method.
4
References
Gill P E and Murray W (1978) Algorithms for the solution of the nonlinear least squares problem SIAM J. Numer. Anal. 15 977–992
5
Arguments
- 1: – IntegerInput
- 2: – IntegerInput
-
On entry: the number of residuals, , and the number of variables, .
Constraint:
.
- 3: – Subroutine, supplied by the user.External Procedure
-
lsqfun must calculate the vector of values
and Jacobian matrix of first derivatives
at any point
. (However, if you do not wish to calculate the residuals or first derivatives at a particular
, there is the option of setting an argument to cause
e04hef to terminate immediately.)
The specification of
lsqfun is:
Fortran Interface
Subroutine lsqfun ( |
iflag,
m,
n,
xc,
fvec,
fjac,
ldfjac,
iw,
liw,
w,
lw) |
Integer, Intent (In) | :: |
m,
n,
ldfjac,
liw,
lw | Integer, Intent (Inout) | :: |
iflag,
iw(liw) | Real (Kind=nag_wp), Intent (In) | :: |
xc(n) | Real (Kind=nag_wp), Intent (Inout) | :: |
fjac(ldfjac,n),
w(lw) | Real (Kind=nag_wp), Intent (Out) | :: |
fvec(m) |
|
C Header Interface
#include nagmk26.h
void |
lsqfun (
Integer *iflag,
const Integer *m,
const Integer *n,
const double xc[],
double fvec[],
double fjac[],
const Integer *ldfjac,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw) |
|
Important: the dimension declaration
fjac must contain the variable
ldfjac, not an integer constant.
- 1: – IntegerInput/Output
-
On entry: to
lsqfun,
iflag will be set to
.
On exit: if it is not possible to evaluate the
or their first derivatives at the point given in
xc (or if it wished to stop the calculations for any other reason), you should reset
iflag to some negative number and return control to
e04hef.
e04hef will then terminate immediately, with
ifail set to your setting of
iflag.
- 2: – IntegerInput
-
On entry: , the numbers of residuals.
- 3: – IntegerInput
-
On entry: , the numbers of variables.
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry: the point at which the values of the and the are required.
- 5: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
iflag is reset to a negative number,
must contain the value of
at the point
, for
.
- 6: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
iflag is reset to a negative number,
must contain the value of
at the point
, for
and
.
- 7: – IntegerInput
-
On entry: the first dimension of the array
fjac as declared in the (sub)program from which
e04hef is called.
- 8: – Integer arrayWorkspace
- 9: – IntegerInput
- 10: – Real (Kind=nag_wp) arrayWorkspace
- 11: – IntegerInput
-
lsqfun is called with
e04hef's arguments
iw,
liw,
w,
lw as these arguments. They are present so that, when other library routines require the solution of a minimization subproblem, constants needed for the evaluation of residuals can be passed through
iw and
w. Similarly, you could pass quantities of
lsqfun from the segment which calls
e04hef by using partitions of
iw and
w beyond those used as workspace by
e04hef. However, because of the danger of mistakes in partitioning, it is recommended that you should pass information to
lsqfun via COMMON global variables and
not use iw or w at all. In any case you
must not change the elements of
iw and
w used as workspace by
e04hef.
lsqfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
e04hef is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: lsqfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
e04hef. If your code inadvertently
does return any NaNs or infinities,
e04hef is likely to produce unexpected results.
lsqfun should be tested separately before being used in conjunction with
e04hef.
- 4: – Subroutine, supplied by the user.External Procedure
-
lsqhes must calculate the elements of the symmetric matrix
at any point
, where
is the Hessian matrix of
. (As with
lsqfun, there is the option of causing
e04hef to terminate immediately.)
The specification of
lsqhes is:
Fortran Interface
Subroutine lsqhes ( |
iflag,
m,
n,
fvec,
xc,
b,
lb,
iw,
liw,
w,
lw) |
Integer, Intent (In) | :: |
m,
n,
lb,
liw,
lw | Integer, Intent (Inout) | :: |
iflag,
iw(liw) | Real (Kind=nag_wp), Intent (In) | :: |
fvec(m),
xc(n) | Real (Kind=nag_wp), Intent (Inout) | :: |
w(lw) | Real (Kind=nag_wp), Intent (Out) | :: |
b(lb) |
|
C Header Interface
#include nagmk26.h
void |
lsqhes (
Integer *iflag,
const Integer *m,
const Integer *n,
const double fvec[],
const double xc[],
double b[],
const Integer *lb,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw) |
|
- 1: – IntegerInput/Output
-
On entry: is set to a non-negative number.
On exit: if
lsqhes resets
iflag to some negative number,
e04hef will terminate immediately, with
ifail set to your setting of
iflag.
- 2: – IntegerInput
-
On entry: , the numbers of residuals.
- 3: – IntegerInput
-
On entry: , the numbers of variables.
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry: the value of the residual
at the point
, for
, so that the values of the
can be used in the calculation of the elements of
b.
- 5: – Real (Kind=nag_wp) arrayInput
-
On entry: the point
at which the elements of
b are to be evaluated.
- 6: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
iflag is reset to a negative number,
b must contain the lower triangle of the matrix
, evaluated at the point
, stored by rows. (The upper triangle is not required because the matrix is symmetric.) More precisely,
must contain
evaluated at the point
, for
and
.
- 7: – IntegerInput
-
On entry: the length of the array
b.
- 8: – Integer arrayWorkspace
- 9: – IntegerInput
- 10: – Real (Kind=nag_wp) arrayWorkspace
- 11: – IntegerInput
-
As in
lsqfun, these arguments correspond to the arguments
iw,
liw,
w,
lw of
e04hef.
lsqhes must not change the sections of
iw and
w required as workspace by
e04hef. Again, it is recommended that you should pass quantities to
lsqhes via COMMON global variables and not use
iw or
w at all.
lsqhes must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
e04hef is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: lsqhes should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
e04hef. If your code inadvertently
does return any NaNs or infinities,
e04hef is likely to produce unexpected results.
lsqhes should be tested separately before being used in conjunction with
e04hef.
- 5: – Subroutine, supplied by the NAG Library or the user.External Procedure
-
If
, you must supply
lsqmon which is suitable for monitoring the minimization process.
lsqmon must not change the values of any of its arguments.
If
, the dummy routine e04fdz can be used as
lsqmon.
The specification of
lsqmon is:
Fortran Interface
Subroutine lsqmon ( |
m,
n,
xc,
fvec,
fjac,
ldfjac,
s,
igrade,
niter,
nf,
iw,
liw,
w,
lw) |
Integer, Intent (In) | :: |
m,
n,
ldfjac,
igrade,
niter,
nf,
liw,
lw | Integer, Intent (Inout) | :: |
iw(liw) | Real (Kind=nag_wp), Intent (In) | :: |
xc(n),
fvec(m),
fjac(ldfjac,n),
s(n) | Real (Kind=nag_wp), Intent (Inout) | :: |
w(lw) |
|
C Header Interface
#include nagmk26.h
void |
lsqmon (
const Integer *m,
const Integer *n,
const double xc[],
const double fvec[],
const double fjac[],
const Integer *ldfjac,
const double s[],
const Integer *igrade,
const Integer *niter,
const Integer *nf,
Integer iw[],
const Integer *liw,
double w[],
const Integer *lw) |
|
Important: the dimension declaration for
fjac must contain the variable
ldfjac, not an integer constant.
- 1: – IntegerInput
-
On entry: , the numbers of residuals.
- 2: – IntegerInput
-
On entry: , the numbers of variables.
- 3: – Real (Kind=nag_wp) arrayInput
-
On entry: the coordinates of the current point .
- 4: – Real (Kind=nag_wp) arrayInput
-
On entry: the values of the residuals at the current point .
- 5: – Real (Kind=nag_wp) arrayInput
-
On entry: contains the value of at the current point , for and .
- 6: – IntegerInput
-
On entry: the first dimension of the array
fjac as declared in the (sub)program from which
e04hef is called.
- 7: – Real (Kind=nag_wp) arrayInput
-
On entry: the singular values of the current Jacobian matrix. Thus
s may be useful as information about the structure of your problem. (If
,
lsqmon is called at the initial point before the singular values have been calculated, so the elements of
s are set to zero for the first call of
lsqmon.)
- 8: – IntegerInput
-
On entry:
e04hef estimates the dimension of the subspace for which the Jacobian matrix can be used as a valid approximation to the curvature (see
Gill and Murray (1978)). This estimate is called the grade of the Jacobian matrix, and
igrade gives its current value.
- 9: – IntegerInput
-
On entry: the number of iterations which have been performed in e04hef.
- 10: – IntegerInput
-
On entry: the number of times that
lsqfun has been called so far. Thus
nf gives the number of evaluations of the residuals and the Jacobian matrix.
- 11: – Integer arrayWorkspace
- 12: – IntegerInput
- 13: – Real (Kind=nag_wp) arrayWorkspace
- 14: – IntegerInput
-
As in
lsqfun and
lsqhes, these arguments correspond to the arguments
iw,
liw,
w,
lw of
e04hef. They are included in
lsqmon's argument list primarily for when
e04hef is called by other library routines.
lsqmon must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
e04hef is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: you should normally print the sum of squares of residuals, so as to be able to examine the sequence of values of
mentioned in
Section 7. It is usually helpful to also print
xc, the gradient of the sum of squares,
niter and
nf.
- 6: – IntegerInput
-
On entry: specifies the frequency with which
lsqmon is to be called.
- lsqmon is called once every iprint iterations and just before exit from e04hef.
- lsqmon is just called at the final point.
- lsqmon is not called at all.
iprint should normally be set to a small positive number.
Suggested value:
.
- 7: – IntegerInput
-
On entry: this argument is present so as to enable you to limit the number of times that
lsqfun is called by
e04hef. There will be an error exit (see
Section 6) after
maxcal calls of
lsqfun.
Suggested value:
.
Constraint:
.
- 8: – Real (Kind=nag_wp)Input
-
On entry: every iteration of
e04hef involves a linear minimization (i.e., minimization of
with respect to
).
eta must lie in the range
, and specifies how accurately these linear minimizations are to be performed. The minimum with respect to
will be located more accurately for small values of
eta (say,
) than for large values (say,
).
Although accurate linear minimizations will generally reduce the number of iterations performed by
e04hef, they will increase the number of calls of
lsqfun made each iteration. On balance it is usually more efficient to perform a low accuracy minimization.
Suggested value:
( if ).
Constraint:
.
- 9: – Real (Kind=nag_wp)Input
-
On entry: the accuracy in
to which the solution is required.
If
is the true value of
at the minimum, then
, the estimated position before a normal exit, is such that
where
. For example, if the elements of
are not much larger than
in modulus and if
, then
is usually accurate to about five decimal places. (For further details see
Section 7.)
If
and the variables are scaled roughly as described in
Section 9 and
is the
machine precision, then a setting of order
will usually be appropriate. If
xtol is set to
or some positive value less than
,
e04hef will use
instead of
xtol, since
is probably the smallest reasonable setting.
Constraint:
.
- 10: – Real (Kind=nag_wp)Input
-
On entry: an estimate of the Euclidean distance between the solution and the starting point supplied by you. (For maximum efficiency, a slight overestimate is preferable.)
e04hef will ensure that, for each iteration
where
is the iteration number. Thus, if the problem has more than one solution,
e04hef is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence of
entering a region where the problem is ill-behaved and can help avoid overflow in the evaluation of
. However, an underestimate of
stepmx can lead to inefficiency.
Suggested value:
.
Constraint:
.
- 11: – Real (Kind=nag_wp) arrayInput/Output
-
On entry: must be set to a guess at the th component of the position of the minimum, for .
On exit: the final point . Thus, if on exit, is the th component of the estimated position of the minimum.
- 12: – Real (Kind=nag_wp)Output
-
On exit: the value of
, the sum of squares of the residuals
, at the final point given in
x.
- 13: – Real (Kind=nag_wp) arrayOutput
-
On exit: the value of the residual
at the final point given in
x, for
.
- 14: – Real (Kind=nag_wp) arrayOutput
-
On exit: the value of the first derivative
evaluated at the final point given in
x, for
and
.
- 15: – IntegerInput
-
On entry: the first dimension of the array
fjac as declared in the (sub)program from which
e04hef is called.
Constraint:
.
- 16: – Real (Kind=nag_wp) arrayOutput
-
On exit: the singular values of the Jacobian matrix at the final point. Thus
s may be useful as information about the structure of your problem.
- 17: – Real (Kind=nag_wp) arrayOutput
-
On exit: the matrix
associated with the singular value decomposition
of the Jacobian matrix at the final point, stored by columns. This matrix may be useful for statistical purposes, since it is the matrix of orthonormalized eigenvectors of
.
- 18: – IntegerInput
-
On entry: the first dimension of the array
v as declared in the (sub)program from which
e04hef is called.
Constraint:
.
- 19: – IntegerOutput
-
On exit: the number of iterations which have been performed in e04hef.
- 20: – IntegerOutput
-
On exit: the number of times that the residuals and Jacobian matrix have been evaluated (i.e., number of calls of
lsqfun).
- 21: – Integer arrayCommunication Array
- 22: – IntegerInput
-
On entry: the dimension of the array
iw as declared in the (sub)program from which
e04hef is called.
Constraint:
.
- 23: – Real (Kind=nag_wp) arrayCommunication Array
- 24: – IntegerInput
-
On entry: the dimension of the array
w as declared in the (sub)program from which
e04hef is called.
Constraints:
- if , ;
- if , .
- 25: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if
on exit, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Note: e04hef may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
-
A negative value of
ifail indicates an exit from
e04hef because you have set
iflag negative in
lsqfun or
lsqhes. The value of
ifail will be the same as your setting of
iflag.
-
On entry, | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | , |
or | when , |
or | when . |
When this exit occurs, no values will have been assigned to
fsumsq, or to the elements of
fvec,
fjac,
s or
v.
-
There have been
maxcal calls of
lsqfun. If steady reductions in the sum of squares,
, were monitored up to the point where this exit occurred, then the exit probably occurred simply because
maxcal was set too small, so the calculations should be restarted from the final point held in
x. This exit may also indicate that
has no minimum.
-
The conditions for a minimum have not all been satisfied, but a lower point could not be found. This could be because
xtol has been set so small that rounding errors in the evaluation of the residuals and derivatives make attainment of the convergence conditions impossible.
-
The method for computing the singular value decomposition of the Jacobian matrix has failed to converge in a reasonable number of sub-iterations. It may be worth applying e04hef again starting with an initial approximation which is not too close to the point at which the failure occurred.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
The values
,
and
may also be caused by mistakes in
lsqfun or
lsqhes, by the formulation of the problem or by an awkward function. If there are no such mistakes it is worth restarting the calculations from a different starting point (not the point at which the failure occurred) in order to avoid the region which caused the failure.
7
Accuracy
A successful exit (
) is made from
e04hef when the matrix of second derivatives of
is positive definite, and when (B1, B2 and B3) or B4 or B5 hold, where
and where
and
are as defined in
Section 5, and
and
are the values of
and its vector of first derivatives at
.
If
, then the vector in
x on exit,
, is almost certainly an estimate of
, the position of the minimum to the accuracy specified by
xtol.
If
, then
may still be a good estimate of
, but to verify this you should make the following checks. If
(a) |
the sequence converges to at a superlinear or a fast linear rate, and |
(b) |
, where denotes transpose, then it is almost certain that is a close approximation to the minimum. |
When
(b) is true, then usually
is a close approximation to
. The values of
can be calculated in
lsqmon, and the vector
can be calculated from the contents of
fvec and
fjac on exit from
e04hef.
Further suggestions about confirmation of a computed solution are given in the
E04 Chapter Introduction.
8
Parallelism and Performance
e04hef is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
e04hef makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
The number of iterations required depends on the number of variables, the number of residuals, the behaviour of
, the accuracy demanded and the distance of the starting point from the solution. The number of multiplications performed per iteration of
e04hef varies, but for
is approximately
. In addition, each iteration makes at least one call of
lsqfun and some iterations may call
lsqhes. So, unless the residuals and their derivatives can be evaluated very quickly, the run time will be dominated by the time spent in
lsqfun (and, to a lesser extent, in
lsqhes).
Ideally, the problem should be scaled so that, at the solution, and the corresponding values of the are each in the range , and so that at points one unit away from the solution, differs from its value at the solution by approximately one unit. This will usually imply that the Hessian matrix of at the solution is well-conditioned. It is unlikely that you will be able to follow these recommendations very closely, but it is worth trying (by guesswork), as sensible scaling will reduce the difficulty of the minimization problem, so that e04hef will take less computer time.
When the sum of squares represents the goodness-of-fit of a nonlinear model to observed data, elements of the variance-covariance matrix of the estimated regression coefficients can be computed by a subsequent call to
e04ycf, using information returned in the arrays
s and
v. See
e04ycf for further details.
10
Example
This example finds least squares estimates of
and
in the model
using the
sets of data given in the following table.
Before calling
e04hef, the program calls
e04yaf and
e04ybf to check
lsqfun and
lsqhes. It uses
as the initial guess at the position of the minimum.
10.1
Program Text
Program Text (e04hefe.f90)
10.2
Program Data
Program Data (e04hefe.d)
10.3
Program Results
Program Results (e04hefe.r)