e04yaf checks that a user-supplied subroutine for evaluating a vector of functions and the matrix of their first derivatives produces derivative values which are consistent with the function values calculated.
The routine may be called by the names e04yaf or nagf_opt_lsq_check_deriv.
3Description
Routines for minimizing a sum of squares of nonlinear functions (or ‘residuals’), , for
and , may require you to supply a subroutine to evaluate the and their first derivatives. e04yaf checks the derivatives calculated by such user-supplied subroutines, e.g., routines of the form required for e04gbf, e04gdfande04hef. As well as the routine to be checked (lsqfun), you must supply a point at which the check will be made. e04yaf is essentially identical to CHKLSJ in the NPL Algorithms Library.
e04yaf first calls lsqfun to evaluate the and their first derivatives, and uses these to calculate the sum of squares ,
and its first derivatives , for . The components of along two orthogonal directions (defined by unit vectors and , say) are then calculated; these will be and respectively. The same components are also estimated by finite differences, giving quantities
where is a small positive scalar. If the relative difference between and or between and is judged too large, an error indicator is set.
4References
None.
5Arguments
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 their first derivatives at any point . (The minimization routines mentioned in Section 3 give you the option of resetting an argument to terminate immediately. e04yaf will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
On exit: if you reset iflag to some negative number in lsqfun and return control to e04yaf, the routine 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 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 e04yaf is called.
8: – Integer arrayWorkspace
9: – IntegerInput
10: – Real (Kind=nag_wp) arrayWorkspace
11: – IntegerInput
These arguments are present so that lsqfun will be of the form required by the minimization routines mentioned in Section 3. lsqfun is called with the same arguments iw, liw, w, lw as in the call to e04yaf. If the recommendation in the minimization routine document is followed, you will have no reason to examine or change the elements of iw or w. In any case, lsqfunmust not change the first elements of w.
lsqfun must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04yaf 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 e04yaf. If your code inadvertently does return any NaNs or infinities, e04yaf is likely to produce unexpected results.
4: – Real (Kind=nag_wp) arrayInput
On entry: , for , must be set to the coordinates of a suitable point at which to check the derivatives calculated by lsqfun. ‘Obvious’ settings, such as or , should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors can go undetected. For a similar reason, it is preferable that no two elements of x should have the same value.
5: – Real (Kind=nag_wp) arrayOutput
On exit: unless you set iflag negative in the first call of lsqfun,
contains the value of at the point supplied by you in x, for .
6: – Real (Kind=nag_wp) arrayOutput
On exit: unless you set iflag negative in the first call of lsqfun,
contains the value of the first derivative at the point given in x, as calculated by lsqfun, for and .
7: – IntegerInput
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04yaf is called.
Constraint:
.
8: – Integer arrayWorkspace
9: – IntegerInput
This array appears in the argument list purely so that, if e04yaf is called by another library routine, the library routine can pass quantities to lsqfun via iw. iw is not examined or changed by e04yaf. In general you must provide an array iw, but are advised not to use it.
On entry: the dimension of the array iw as declared in the (sub)program from which e04yaf is called.
Constraint:
.
10: – Real (Kind=nag_wp) arrayWorkspace
11: – IntegerInput
On entry: the dimension of the array w as declared in the (sub)program from which e04yaf is called.
Constraint:
.
12: – IntegerInput/Output
On entry: ifail must be set to , or to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of means that an error message is printed while a value of means that it is not.
If halting is not appropriate, the value or is recommended. If message printing is undesirable, then the value is recommended. Otherwise, the value is recommended since useful values can be provided in some output arguments even when on exit. When the value or 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).
6Error Indicators and Warnings
If on entry or , explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: in some cases e04yaf may return useful information.
On entry, and .
Constraint: .
On entry, .
Constraint: .
On entry, .
Constraint: ; that is, .
On entry, and .
Constraint: .
On entry, .
Constraint: .
It is very likely that you have made an error in forming the derivatives in lsqfun.
User requested termination by setting iflag negative in lsqfun.
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
for or . (See Section 3 for definitions of the quantities involved.) The scalar is set equal to , where is the machine precision as given by x02ajf.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
e04yaf 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.
Before using e04yaf to check the calculation of the first derivatives, you should be confident that lsqfun is calculating the residuals correctly.
e04yaf only checks the derivatives calculated by a user-supplied routine when . So, if lsqfun is intended for use in conjunction with a minimization routine which may set iflag to , you must check that, for given settings of the , lsqfun produces the same values for the when iflag is set to as when iflag is set to .
10Example
Suppose that it is intended to use e04gbf or e04gdf to find least squares estimates of and in the model
using the sets of data given in the following table.
The following program could be used to check the first derivatives calculated by lsqfun. (The tests of whether or in lsqfun are present ready for when lsqfun is called by e04gbf or e04gdf. e04yaf will always call lsqfun with iflag set to 2.)