E04YBF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

E04YBF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

 Contents

    1  Purpose
    7  Accuracy

1  Purpose

E04YBF checks that a user-supplied subroutine for evaluating the second derivative term of the Hessian matrix of a sum of squares is consistent with a user-supplied subroutine for calculating the corresponding first derivatives.

2  Specification

SUBROUTINE E04YBF ( M, N, LSQFUN, LSQHES, X, FVEC, FJAC, LDFJAC, B, LB, IW, LIW, W, LW, IFAIL)
INTEGER  M, N, LDFJAC, LB, IW(LIW), LIW, LW, IFAIL
REAL (KIND=nag_wp)  X(N), FVEC(M), FJAC(LDFJAC,N), B(LB), W(LW)
EXTERNAL  LSQFUN, LSQHES

3  Description

Routines for minimizing a sum of squares of m nonlinear functions (or ‘residuals’), fix1,x2,,xn, for i=1,2,,m and mn, may require you to supply a subroutine to evaluate the quantities
bjk=i=1mfi 2fi xjxk  
for j=1,2,,n and k=1,2,,j. E04YBF is designed to check the bjk calculated by such user-supplied subroutines. As well as the routine to be checked (LSQHES), you must supply a subroutine (LSQFUN) to evaluate the fi and their first derivatives, and a point x=x1,x2,,xnT at which the checks will be made. Note that E04YBF checks routines of the form required by E04HEF. E04YBF is essentially identical to CHKLSH in the NPL Algorithms Library.
E04YBF first calls user-supplied subroutines LSQFUN and LSQHES to evaluate the first derivatives and the bjk at x. Let J denote the m by n matrix of first derivatives of the residuals. The Hessian matrix of the sum of squares,
G=JTJ+B,  
is calculated and projected onto two orthogonal vectors y and z to give the scalars yTGy and zTGz respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
p=yTgx+hy-yTgx/h  and q=zTgx+hz-zTgx/h  
respectively, where g denotes the gradient vector of the sum of squares at the point in brackets and h is a small positive scalar. If the relative difference between p and yTGy or between q and zTGz is judged too large, an error indicator is set.

4  References

None.

5  Parameters

1:     M – INTEGERInput
2:     N – INTEGERInput
On entry: the number m of residuals, fix, and the number n of variables, xj.
Constraint: 1NM.
3:     LSQFUN – SUBROUTINE, supplied by the user.External Procedure
LSQFUN must calculate the vector of values fix and their first derivatives fi xj  at any point x. (E04HEF gives you the option of resetting parameters of LSQFUN to cause the minimization process to terminate immediately. E04YBF will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
The specification of LSQFUN is:
SUBROUTINE LSQFUN ( IFLAG, M, N, XC, FVEC, FJAC, LDFJAC, IW, LIW, W, LW)
INTEGER  IFLAG, M, N, LDFJAC, IW(LIW), LIW, LW
REAL (KIND=nag_wp)  XC(N), FVEC(M), FJAC(LDFJAC,N), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: to LSQFUN, IFLAG will be set to 2.
On exit: if you reset IFLAG to some negative number in LSQFUN and return control to E04YBF, the routine will terminate immediately with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: the numbers m of residuals.
3:     N – INTEGERInput
On entry: the numbers n of variables.
4:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the values of the fi and the fi xj  are required.
5:     FVECM – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number, FVECi must contain the value of fi at the point x, for i=1,2,,m.
6:     FJACLDFJACN – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number, FJACij must contain the value of fi xj  at the point x, for i=1,2,,m and j=1,2,,n.
7:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04YBF is called.
8:     IWLIW – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   WLW – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
These parameters are present so that LSQFUN will be of the form required by E04HEF. LSQFUN is called with E04YBF's parameters IW, LIW, W, LW as these parameters. If the recommendation in E04HEF is followed, you will have no reason to examine or change the elements of IW or W. In any case, LSQFUN must not change the first 5×N+M+M×N+N×N-1/2 (or 6+2×M if N=1) elements of W.
LSQFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04YBF is called. Parameters denoted as Input must not be changed by this procedure.
Note:  E04YAF should be used to check the first derivatives calculated by LSQFUN before E04YBF is used to check the bjk since E04YBF assumes that the first derivatives are correct.
4:     LSQHES – SUBROUTINE, supplied by the user.External Procedure
LSQHES must calculate the elements of the symmetric matrix
Bx=i=1mfixGix,  
at any point x, where Gix is the Hessian matrix of fix. (As with LSQFUN, a parameter can be set to cause immediate termination.)
The specification of LSQHES is:
SUBROUTINE LSQHES ( IFLAG, M, N, FVEC, XC, B, LB, IW, LIW, W, LW)
INTEGER  IFLAG, M, N, LB, IW(LIW), LIW, LW
REAL (KIND=nag_wp)  FVEC(M), XC(N), B(LB), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: is set to a non-negative number.
On exit: if LSQHES resets IFLAG to some negative number, E04YBF will terminate immediately, with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: the numbers m of residuals.
3:     N – INTEGERInput
On entry: the numbers n of variables.
4:     FVECM – REAL (KIND=nag_wp) arrayInput
On entry: the value of the residual fi at the point x, for i=1,2,,m, so that the values of the fi can be used in the calculation of the elements of B.
5:     XCN – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the elements of B are to be evaluated.
6:     BLB – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number B must contain the lower triangle of the matrix Bx, evaluated at the point in XC, stored by rows. (The upper triangle is not needed because the matrix is symmetric.) More precisely, Bjj-1/2+k must contain i=1mfi 2fi xjxk  evaluated at the point x, for j=1,2,,n and k=1,2,,j.
7:     LB – INTEGERInput
On entry: gives the length of the array B.
8:     IWLIW – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   WLW – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
As in LSQFUN, these parameters correspond to the parameters IW, LIW, W, LW of E04YBF. LSQHES must not change the first 5×N+M×N+N×N-1/2 (or 6+2×M if N=1) elements of W.
LSQHES must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04YBF is called. Parameters denoted as Input must not be changed by this procedure.
5:     XN – REAL (KIND=nag_wp) arrayInput
On entry: Xj, for j=1,2,,n, must be set to the coordinates of a suitable point at which to check the bjk calculated by LSQHES. ‘Obvious’ settings, such as 0 or 1, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. For a similar reason, it is preferable that no two elements of X should have the same value.
6:     FVECM – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in the first call of LSQFUN, FVECi contains the value of fi at the point supplied by you in X, for i=1,2,,m.
7:     FJACLDFJACN – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in the first call of LSQFUN, FJACij contains the value of the first derivative fi xj  at the point given in X, as calculated by LSQFUN, for i=1,2,,m and j=1,2,,n.
8:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04YBF is called.
Constraint: LDFJACM.
9:     BLB – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in LSQHES, Bj×j-1/2+k contains the value of bjk at the point given in X as calculated by LSQHES, for j=1,2,,n and k=1,2,,j.
10:   LB – INTEGERInput
On entry: the dimension of the array B as declared in the (sub)program from which E04YBF is called.
Constraint: LBN+1×N/2.
11:   IWLIW – INTEGER arrayWorkspace
This array appears in the parameter list purely so that, if E04YBF is called by another library routine, the library routine can pass quantities to user-supplied subroutines LSQFUN and LSQHES via IW. IW is not examined or changed by E04YBF. In general you must provide an array IW, but are advised not to use it.
12:   LIW – INTEGERInput
On entry: the actual length of IW as declared in the subroutine from which E04YBF is called.
Constraint: LIW1.
13:   WLW – REAL (KIND=nag_wp) arrayWorkspace
14:   LW – INTEGERInput
On entry: the actual length of W as declared in the subroutine from which E04YBF is called.
Constraints:
  • if N>1, LW5×N+M+M×N+N×N-1/2;
  • if N=1, LW6+2×M.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: E04YBF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
IFAIL<0
A negative value of IFAIL indicates an exit from E04YBF because you have set IFLAG negative in user-supplied subroutines LSQFUN or LSQHES. The setting of IFAIL will be the same as your setting of IFLAG. The check on LSQHES will not have been completed.
IFAIL=1
On entry,M<N,
orN<1,
orLDFJAC<M,
orLB<N+1×N/2,
orLIW<1,
orLW<5×N+M+M×N+N×N-1/2, if N>1,
orLW<6+2×M, if N=1.
IFAIL=2
You should check carefully the derivation and programming of expressions for the bjk, because it is very unlikely that LSQHES is calculating them correctly.
IFAIL=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.8 in the Essential Introduction for further information.
IFAIL=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
IFAIL=-999
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

7  Accuracy

IFAIL is set to 2 if
yTGy-phyTGy+1.0 or zTGz-qhzTGz+1.0  
where h is set equal to ε (ε being the machine precision as given by X02AJF) and other quantities are defined as in Section 3.

8  Parallelism and Performance

Not applicable.

9  Further Comments

E04YBF calls LSQHES once and LSQFUN three times.

10  Example

Suppose that it is intended to use E04HEF to find least squares estimates of x1,x2 and x3 in the model
y = x1 + t1 x2 t2+ x3 t3  
using the 15 sets of data given in the following table.
y t1 t2 t3 0.14 1.0 15.0 1.0 0.18 2.0 14.0 2.0 0.22 3.0 13.0 3.0 0.25 4.0 12.0 4.0 0.29 5.0 11.0 5.0 0.32 6.0 10.0 6.0 0.35 7.0 9.0 7.0 0.39 8.0 8.0 8.0 0.37 9.0 7.0 7.0 0.58 10.0 6.0 6.0 0.73 11.0 5.0 5.0 0.96 12.0 4.0 4.0 1.34 13.0 3.0 3.0 2.10 14.0 2.0 2.0 4.39 15.0 1.0 1.0  
This example program could be used to check the bjk calculated by LSQHES required. (The call of E04YBF is preceded by a call of E04YAF to check LSQFUN which calculates the first derivatives.)

10.1  Program Text

Program Text (e04ybfe.f90)

10.2  Program Data

Program Data (e04ybfe.d)

10.3  Program Results

Program Results (e04ybfe.r)


E04YBF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2015