NAG CL Interface
e04fcc (lsq_uncon_mod_func_comp)
1
Purpose
e04fcc is a comprehensive algorithm for finding an unconstrained minimum of a sum of squares of nonlinear functions in variables . No derivatives are required.
e04fcc is intended for objective functions which have continuous first and second derivatives (although it will usually work even if the derivatives have occasional discontinuities).
2
Specification
void |
e04fcc (Integer m,
Integer n,
void |
(*lsqfun)(Integer m,
Integer n,
const double x[],
double fvec[],
Nag_Comm *comm),
|
|
double x[],
double *fsumsq,
double fvec[],
double fjac[],
Integer tdfjac,
Nag_E04_Opt *options,
Nag_Comm *comm,
NagError *fail) |
|
The function may be called by the names: e04fcc, nag_opt_lsq_uncon_mod_func_comp or nag_opt_lsq_no_deriv.
3
Description
e04fcc is applicable to problems of the form:
where
and
. (The functions
are often referred to as ‘residuals’.) You must supply a C function,
lsqfun, to calculate the values of the
at any point
.
From a starting point
e04fcc 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 an approximation to the Gauss–Newton direction; otherwise additional function evaluations are made so as to enable to be a more accurate approximation to the Newton direction.
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:
– Integer
Input
-
On entry: , the number of residuals, .
-
2:
– Integer
Input
-
On entry: , the number of variables, .
Constraint:
.
-
3:
– function, supplied by the user
External Function
-
lsqfun must calculate the vector of values
at any point
. (However, if you do not wish to calculate the residuals at a particular
, there is the option of setting an argument to cause
e04fcc to terminate immediately.)
The specification of
lsqfun is:
void |
lsqfun (Integer m,
Integer n,
const double x[],
double fvec[],
Nag_Comm *comm)
|
|
-
1:
– Integer
Input
-
2:
– Integer
Input
-
On entry: the numbers and of residuals and variables, respectively.
-
3:
– const double
Input
-
On entry: the point at which the values of the are required.
-
4:
– double
Output
-
On exit: unless is reset to a negative number, on exit must contain the value of at the point , for .
-
5:
– Nag_Comm *
-
Pointer to structure of type Nag_Comm; the following members are relevant to
lsqfun.
- flag – IntegerInput/Output
-
On entry: contains a non-negative number.
On exit: if
lsqfun resets
to some negative number then
e04fcc will terminate immediately with the error indicator
NE_USER_STOP. If
fail is supplied to
e04fcc,
will be set to the user's setting of
.
- first – Nag_BooleanInput
-
On entry: the value Nag_TRUE on the first call to
lsqfun and Nag_FALSE for all subsequent calls.
- nf – IntegerInput
-
On entry: the number of calls made to
lsqfun including the current one.
- user – double *
- iuser – Integer *
- p – Pointer
-
The type Pointer will be
void * with a C compiler that defines
void * and
char * otherwise. Before calling
e04fcc these pointers may be allocated memory and initialized with various quantities for use by
lsqfun when called from
e04fcc.
Note: lsqfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
e04fcc. If your code inadvertently
does return any NaNs or infinities,
e04fcc is likely to produce unexpected results.
Note:
lsqfun should be tested separately before being used in conjunction with
e04fcc. The array
x must
not be changed within
lsqfun.
-
4:
– double
Input/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 . On successful exit, is the th component of the estimated position of the minimum.
-
5:
– double *
Output
-
On exit: the value of
, the sum of squares of the residuals
, at the final point given in
x.
-
6:
– double
Output
-
On exit:
is the value of the residual
at the final point given in
x, for
.
-
7:
– double
Output
-
On exit:
contains the estimate of the first derivative
at the final point given in
x, for
and
.
-
8:
– Integer
Input
-
On entry: the stride separating matrix column elements in the array
fjac.
Constraint:
.
-
9:
– Nag_E04_Opt *
Input/Output
-
On entry/exit: a pointer to a structure of type Nag_E04_Opt whose members are optional parameters for
e04fcc. These structure members offer the means of adjusting some of the argument values of the algorithm and on output will supply further details of the results. A description of the members of
options is given in
Section 11.2.
If any of these optional parameters are required then the structure
options should be declared and initialized by a call to
e04xxc and supplied as an argument to
e04fcc. However, if the optional parameters are not required the NAG defined null pointer,
E04_DEFAULT, can be used in the function call.
-
10:
– Nag_Comm *
Input/Output
-
Note: comm is a NAG defined type (see
Section 3.1.1 in the Introduction to the NAG Library CL Interface).
On entry/exit: structure containing pointers for communication to user-supplied functions; see the above description of
lsqfun for details. If you do not need to make use of this communication feature the null pointer
NAGCOMM_NULL may be used in the call to
e04fcc;
comm will then be declared internally for use in calls to user-supplied functions.
-
11:
– NagError *
Input/Output
-
The NAG error argument (see
Section 7 in the Introduction to the NAG Library CL Interface).
6
Error Indicators and Warnings
- If one of NE_USER_STOP, NE_2_INT_ARG_LT, NE_OPT_NOT_INIT, NE_BAD_PARAM, NE_2_REAL_ARG_LT, NE_INVALID_INT_RANGE_1, NE_INVALID_REAL_RANGE_EF, NE_INVALID_REAL_RANGE_FF and NE_ALLOC_FAIL occurs, no values will have been assigned to fsumsq, or to the elements of fvec, fjac, or .
- The exits NW_TOO_MANY_ITER, NW_COND_MIN, and NE_SVD_FAIL may also be caused by mistakes in lsqfun, 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.
- NE_2_INT_ARG_LT
-
On entry, while . These arguments must satisfy .
On entry, while . These arguments must satisfy .
On entry, while . These arguments must satisfy .
- NE_2_REAL_ARG_LT
-
On entry, while . These arguments must satisfy .
- NE_ALLOC_FAIL
-
Dynamic memory allocation failed.
- NE_BAD_PARAM
-
On entry, argument had an illegal value.
- NE_INT_ARG_LT
-
On entry, .
Constraint: .
- NE_INVALID_INT_RANGE_1
-
Value given to not valid. Correct range is .
- NE_INVALID_REAL_RANGE_EF
-
Value given to not valid. Correct range is .
- NE_INVALID_REAL_RANGE_FF
-
Value given to not valid. Correct range is .
- NE_NOT_APPEND_FILE
-
Cannot open file for appending.
- NE_NOT_CLOSE_FILE
-
Cannot close file .
- NE_OPT_NOT_INIT
-
Options structure not initialized.
- NE_SVD_FAIL
-
The computation of the singular value decomposition of the Jacobian matrix has failed to converge in a reasonable number of sub-iterations.
It may be worth applying e04fcc again starting with an initial approximation which is not too close to the point at which the failure occurred.
- NE_USER_STOP
-
User requested termination, user flag value .
This exit occurs if you set
to a negative value in
lsqfun. If
fail is supplied the value of
will be the same as your setting of
.
- NE_WRITE_ERROR
-
Error occurred when writing to file .
- NW_COND_MIN
-
The conditions for a minimum have not all been satisfied, but a lower point could not be found.
This could be because has been set so small that rounding errors in the evaluation of the residuals make attainment of the convergence conditions impossible.
- NW_TOO_MANY_ITER
-
The maximum number of iterations, , have been performed.
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
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.
7
Accuracy
If the problem is reasonably well scaled and a successful exit is made, then, for a computer with a mantissa of decimals, one would expect to get about decimals accuracy in the components of and between (if is of order 1 at the minimum) and (if is close to zero at the minimum) decimals accuracy in .
A successful exit (NE_NOERROR) is made from
e04fcc when (B1, B2 and B3) or B4 or B5 hold, where
and where
,
and the optional parameter
are as defined in
Section 11.2, while
and
are the values of
and its vector of estimated 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
.
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
.
Further suggestions about confirmation of a computed solution are given in the
E04 Chapter Introduction.
8
Parallelism and Performance
e04fcc is not threaded in any implementation.
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
e04fcc varies, but for
is approximately
. In addition, each iteration makes at least
calls of
lsqfun. So, unless the residuals can be evaluated very quickly, the run time will be dominated by the time spent in
lsqfun.
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 e04fcc 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
e04ycc, using information returned in the arrays
and
. See
e04ycc for further details.
10
Example
This example shows option values being assigned directly within the program text and by reading values from a data file. The
options structure is declared and initialized by
e04xxc. Values are then assigned directly to options
and
and two further options are read from the data file by use of
e04xyc. The memory freeing function
e04xzc is used to free the memory assigned to the pointers in the option structure. You must
not use the standard C function
free() for this purpose.
10.1
Program Text
10.2
Program Data
10.3
Program Results
11
Optional Parameters
A number of optional input and output arguments to
e04fcc are available through the structure argument
options, type Nag_E04_Opt. An argument may be selected by assigning an appropriate value to the relevant structure member; those arguments not selected will be assigned default values. If no use is to be made of any of the optional parameters you should use the NAG defined null pointer,
E04_DEFAULT, in place of
options when calling
e04fcc; the default settings will then be used for all arguments.
Before assigning values to
options directly the structure
must be initialized by a call to the function
e04xxc. Values may then be assigned to the structure members in the normal C manner.
After return from
e04fcc, the
options structure may only be re-used for future calls of
e04fcc if the dimensions of the new problem are the same. Otherwise, the structure must be cleared by a call of
e04xzc) and re-initialized by a call of
e04xxc before future calls. Failure to do this will result in unpredictable behaviour.
Option settings may also be read from a text file using the function
e04xyc in which case initialization of the
options structure will be performed automatically if not already done. Any subsequent direct assignment to the
options structure must
not be preceded by initialization.
If assignment of functions and memory to pointers in the
options structure is required, this must be done directly in the calling program, they cannot be assigned using
e04xyc.
11.1
Optional Parameter Checklist and Default Values
For easy reference, the following list shows the members of
options which are valid for
e04fcc together with their default values where relevant. The number
is a generic notation for
machine precision (see
X02AJC).
Boolean list |
Nag_TRUE |
Nag_PrintType print_level |
|
char outfile[512] |
stdout |
void (*print_fun)() |
NULL |
Integer max_iter |
|
double optim_tol |
|
double linesearch_tol |
( if ) |
double step_max |
100000.0 |
double *s |
size n |
double *v |
size |
Integer tdv |
n |
Integer grade |
Integer iter |
Integer nf |
11.2
Description of the Optional Parameters
list – Nag_Boolean | | Default |
On entry: if the argument settings in the call to e04fcc will be printed.
print_level – Nag_PrintType | | Default |
On entry: the level of results printout produced by
e04fcc. The following values are available:
|
No output. |
|
The final solution. |
|
One line of output for each iteration. |
|
The final solution and one line of output for each iteration. |
|
The final solution and detailed printout at each iteration. |
Details of each level of results printout are described in
Section 9.
Constraint:
, , , or .
outfile – const char[512] | | Default |
On entry: the name of the file to which results should be printed. If then the stdout stream is used.
print_fun – pointer to function | | Default NULL |
On entry: printing function defined by you; the prototype of
is
void (*print_fun)(const Nag_Search_State *st, Nag_Comm *comm);
See
Section 9 for further details.
max_iter – Integer | | Default |
On entry: the limit on the number of iterations allowed before termination.
Constraint:
.
optim_tol – double | | Default |
On entry: the accuracy in
to which the solution is required. If
is the true value of
at the minimum, then
, the estimated position prior to 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 5 decimal places. (For further details see
Section 9.) 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.
Constraint:
.
linesearch_tol – double | | Default . (If , default ) |
On entry: every iteration of
e04fcc involves a linear minimization, i.e., minimization of
with respect to
.
specifies how accurately the linear minimizations are to be performed. The minimum with respect to
will be located more accurately for small values of
(say 0.01) than for large values (say 0.9). Although accurate linear minimizations will generally reduce the number of iterations performed by
e04fcc, 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.
Constraint:
.
step_max – double | | Default |
On entry: an estimate of the Euclidean distance between the solution and the starting point supplied. (For maximum efficiency, a slight overestimate is preferable.)
e04fcc will ensure that, for each iteration,
where
is the iteration number. Thus, if the problem has more than one solution,
e04fcc is most likely to find the one nearest to the starting point. On difficult problems, a realistic choice can prevent the sequence
entering a region where the problem is ill-behaved and can help avoid overflow in the evaluation of
. However, an underestimate of
can lead to inefficiency.
Constraint:
.
s – double * | | Default memory |
On entry:
n values of memory will be automatically allocated by
e04fcc and this is the recommended method of use of
. However you may supply memory from the calling program.
On exit: the singular values of the estimated Jacobian matrix at the final point. Thus may be useful as information about the structure of your problem.
v – double * | | Default memory |
On entry: values of memory will be automatically allocated by e04fcc and this is the recommended method of use of . However you may supply memory from the calling program.
On exit: the matrix
associated with the singular value decomposition
of the estimated Jacobian matrix at the final point, stored by rows. This matrix may be useful for statistical purposes, since it is the matrix of orthonormalized eigenvectors of
.
On entry: if memory is supplied then must contain the last dimension of the array assigned to as declared in the function from which e04fcc is called.
On exit: the trailing dimension used by
. If the Nag default memory allocation has been used this value will be
n.
Constraint:
.
On exit: the grade of the Jacobian at the final point.
e04fcc 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.
On exit: the number of iterations which have been performed in e04fcc.
On exit: the number of times the residuals have been evaluated (i.e., number of calls of
lsqfun).
11.3
Description of Printed Output
The level of printed output can be controlled with the structure members
and
(see
Section 11.2). If
then the argument values to
e04fcc are listed, whereas the printout of results is governed by the value of
. The default of
provides a single line of output at each iteration and the final result. This section describes all of the possible levels of results printout available from
e04fcc.
When
or
a single line of output is produced on completion of each iteration, this gives the following values:
Itn |
the current iteration number . |
Nfun |
the cumulative number of calls to lsqfun. |
Objective |
the value of the objective function, . |
Norm g |
the Euclidean norm of the gradient of . |
Norm x |
the Euclidean norm of . |
Norm(x(k-1)-x(k)) |
the Euclidean norm of . |
Step |
the step taken along the computed search direction . |
When
more detailed results are given at each iteration. Additional values output are:
Grade |
the grade of the Jacobian matrix. (See description of , Section 11.2 |
x |
the current point . |
g |
the current estimate of the gradient of . |
Singular values |
the singular values of the current approximation to the Jacobian matrix. |
If
,
or
the final result is printed out. This consists of:
x |
the final point . |
g |
the estimate of the gradient of at the final point. |
Residuals |
the values of the residuals at the final point. |
Sum of squares |
the value of , the sum of squares of the residuals at the final point. |
If then printout will be suppressed; you can print the final solution when e04fcc returns to the calling program.
11.3.1
Output of results via a user-defined printing function
You may also specify your own print function for output of iteration results and the final solution by use of the
function pointer, which has prototype
void (*print_fun)(const Nag_Search_State *st, Nag_Comm *comm);
The rest of this section can be skipped if the default printing facilities provide the required functionality.
When a user-defined function is assigned to
this will be called in preference to the internal print function of
e04fcc. Calls to the user-defined function are again controlled by means of the
member. Information is provided through
st and
comm, the two structure arguments to
. If
then the results from the last iteration of
e04fcc are in the following members of
st:
- m – Integer
-
The number of residuals.
- n – Integer
-
The number of variables.
- x – double *
-
Points to the memory locations holding the current point .
- fvec – double *
-
Points to the memory locations holding the values of the residuals at the current point .
- fjac – double *
-
Points to memory locations. contains the value of , for and , at the current point .
- tdj – Integer
-
The trailing dimension for .
- step – double
-
The step taken along the search direction .
- xk_norm – double
-
The Euclidean norm of .
- g – double *
-
Points to the memory locations holding the estimated gradient of at the current point .
- grade – Integer
-
The grade of the Jacobian matrix.
- s – double *
-
Points to the memory locations holding the singular values of the current approximation to the Jacobian.
- iter – Integer
-
The number of iterations, , performed by e04fcc.
- nf – Integer
-
The cumulative number of calls made to
lsqfun.
The relevant members of the structure
comm are:
- it_prt – Nag_Boolean
-
Will be Nag_TRUE when the print function is called with the result of the current iteration.
- sol_prt – Nag_Boolean
-
Will be Nag_TRUE when the print function is called with the final result.
- user – double *
- iuser – Integer *
- p – Pointer
-
Pointers for communication of user information. If used they must be allocated memory either before entry to
e04fcc or during a call to
lsqfun or
. The type Pointer will be
void * with a C compiler that defines
void * and
char * otherwise.