E04XAF/E04XAA (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document

E04XAF/E04XAA

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

E04XAF/E04XAA computes an approximation to the gradient vector and/or the Hessian matrix for use in conjunction with, or following the use of an optimization routine (such as E04UFF/E04UFA).
E04XAA is a version of E04XAF that has additional parameters in order to make it safe for use in multithreaded applications (see Section 5). The initialization routine E04WBF must have been called before calling E04XAA.

2  Specification

2.1  Specification for E04XAF

SUBROUTINE E04XAF ( MSGLVL, N, EPSRF, X, MODE, OBJFUN, LDH, HFORW, OBJF, OBJGRD, HCNTRL, H, IWARN, WORK, IUSER, RUSER, INFO, IFAIL)
INTEGER  MSGLVL, N, MODE, LDH, IWARN, IUSER(*), INFO(N), IFAIL
REAL (KIND=nag_wp)  EPSRF, X(N), HFORW(N), OBJF, OBJGRD(N), HCNTRL(N), H(LDH,*), WORK(*), RUSER(*)
EXTERNAL  OBJFUN

2.2  Specification for E04XAA

SUBROUTINE E04XAA ( MSGLVL, N, EPSRF, X, MODE, OBJFUN, LDH, HFORW, OBJF, OBJGRD, HCNTRL, H, IWARN, WORK, IUSER, RUSER, INFO, LWSAV, IWSAV, RWSAV, IFAIL)
INTEGER  MSGLVL, N, MODE, LDH, IWARN, IUSER(*), INFO(N), IWSAV(1), IFAIL
REAL (KIND=nag_wp)  EPSRF, X(N), HFORW(N), OBJF, OBJGRD(N), HCNTRL(N), H(LDH,*), WORK(*), RUSER(*), RWSAV(1)
LOGICAL  LWSAV(1)
EXTERNAL  OBJFUN

3  Description

E04XAF/E04XAA is similar to routine FDCALC described in Gill et al. (1983a). It should be noted that this routine aims to compute sufficiently accurate estimates of the derivatives for use with an optimization algorithm. If you require more accurate estimates you should refer to Chapter D04.
E04XAF/E04XAA computes finite difference approximations to the gradient vector and the Hessian matrix for a given function. The simplest approximation involves the forward-difference formula, in which the derivative fx of a univariate function fx is approximated by the quantity
ρFf,h=fx+h-fxh
for some interval h>0, where the subscript 'F' denotes ‘forward-difference’ (see Gill et al. (1983b)).
To summarise the procedure used by E04XAF/E04XAA (for the case when the objective function is available and you require estimates of gradient values and Hessian matrix diagonal values, i.e., MODE=0) consider a univariate function f at the point x. (In order to obtain the gradient of a multivariate function Fx, where x is an n-vector, the procedure is applied to each component of x, keeping the other components fixed.) Roughly speaking, the method is based on the fact that the bound on the relative truncation error in the forward-difference approximation tends to be an increasing function of h, while the relative condition error bound is generally a decreasing function of h, hence changes in h will tend to have opposite effects on these errors (see Gill et al. (1983b)).
The ‘best’ interval h is given by
hF=21+fxeRΦ (1)
where Φ is an estimate of fx, and eR is an estimate of the relative error associated with computing the function (see Chapter 8 of Gill et al. (1981)). Given an interval h, Φ is defined by the second-order approximation
Φ=fx+h-2fx+fx-hh2.
The decision as to whether a given value of Φ is acceptable involves c^Φ, the following bound on the relative condition error in Φ:
c^Φ=4eR1+f h2Φ
(When Φ is zero, c^Φ is taken as an arbitrary large number.)
The procedure selects the interval hϕ (to be used in computing Φ) from a sequence of trial intervals hk. The initial trial interval is taken as 10h-, where
h-=21+xeR
unless you specify the initial value to be used.
The value of c^Φ for a trial value hk is defined as ‘acceptable’ if it lies in the interval 0.001,0.1. In this case hϕ is taken as hk, and the current value of Φ is used to compute hF from (1). If c^Φ is unacceptable, the next trial interval is chosen so that the relative condition error bound will either decrease or increase, as required. If the bound on the relative condition error is too large, a larger interval is used as the next trial value in an attempt to reduce the condition error bound. On the other hand, if the relative condition error bound is too small, hk is reduced.
The procedure will fail to produce an acceptable value of c^Φ in two situations. Firstly, if fx is extremely small, then c^Φ may never become small, even for a very large value of the interval. Alternatively, c^Φ may never exceed 0.001, even for a very small value of the interval. This usually implies that fx is extremely large, and occurs most often near a singularity.
As a check on the validity of the estimated first derivative, the procedure provides a comparison of the forward-difference approximation computed with hF (as above) and the central-difference approximation computed with hϕ. Using the central-difference formula the first derivative can be approximated by
ρcf,h=fx+h-fx-h 2h
where h>0. If the values hF and hϕ do not display some agreement, neither can be considered reliable.
When both function and gradients are available and you require the Hessian matrix (i.e., MODE=1) E04XAF/E04XAA follows a similar procedure to the case above with the exception that the gradient function gx is substituted for the objective function and so the forward-difference interval for the first derivative of gx with respect to variable xj is computed. The jth column of the approximate Hessian matrix is then defined as in Chapter 2 of Gill et al. (1981), by
gx+hjej-gxhj
where hj is the best forward-difference interval associated with the jth component of g and ej is the vector with unity in the jth position and zeros elsewhere.
When only the objective function is available and you require the gradients and Hessian matrix (i.e., MODE=2) E04XAF/E04XAA again follows the same procedure as the case for MODE=0 except that this time the value of c^Φ for a trial value hk is defined as acceptable if it lies in the interval 0.0001,0.01 and the initial trial interval is taken as
h-=21+xeR4.
The approximate Hessian matrix G is then defined as in Chapter 2 of Gill et al. (1981), by
Gijx=1hihj fx+hiei+hjej-fx+hiei-fx+hjej+fx.

4  References

Gill P E, Murray W, Saunders M A and Wright M H (1983a) Documentation for FDCALC and FDCORE Technical Report SOL 83–6 Stanford University
Gill P E, Murray W, Saunders M A and Wright M H (1983b) Computing forward-difference intervals for numerical optimization SIAM J. Sci. Statist. Comput. 4 310–321
Gill P E, Murray W and Wright M H (1981) Practical Optimization Academic Press

5  Parameters

1:     MSGLVL – INTEGERInput
On entry: must indicate the amount of intermediate output desired (see Section 8.1 for a description of the printed output). All output is written on the current advisory message unit (see X04ABF).
Value Definition
0 No printout
1 A summary is printed out for each variable plus any warning messages.
Other Values other than 0 and 1 should normally be used only at the direction of NAG.
2:     N – INTEGERInput
On entry: the number n of independent variables.
Constraint: N1.
3:     EPSRF – REAL (KIND=nag_wp)Input
On entry: must define eR, which is intended to be a measure of the accuracy with which the problem function F can be computed. The value of eR should reflect the relative precision of 1+Fx, i.e., acts as a relative precision when F is large, and as an absolute precision when F is small. For example, if Fx is typically of order 1000 and the first six significant digits are known to be correct, an appropriate value for eR would be 1.0E−6.
A discussion of EPSRF is given in Chapter 8 of Gill et al. (1981). If EPSRF is either too small or too large on entry a warning will be printed if MSGLVL=1, the parameter IWARN set to the appropriate value on exit and E04XAF/E04XAA will use a default value of eM0.9, where eM is the machine precision.
If EPSRF0.0 on entry then E04XAF/E04XAA will use the default value internally. The default value will be appropriate for most simple functions that are computed with full accuracy.
4:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the derivatives are to be computed.
5:     MODE – INTEGERInput/Output
On entry: indicates which derivatives are required.
MODE=0
The gradient and Hessian diagonal values having supplied the objective function via OBJFUN.
MODE=1
The Hessian matrix having supplied both the objective function and gradients via OBJFUN.
MODE=2
The gradient values and Hessian matrix having supplied the objective function via OBJFUN.
On exit: is changed only if you set MODE negative in OBJFUN, i.e., you have requested termination of E04XAF/E04XAA.
6:     OBJFUN – SUBROUTINE, supplied by the user.External Procedure
If MODE=0 or 2, OBJFUN must calculate the objective function; otherwise if MODE=1, OBJFUN must calculate the objective function and the gradients.
The specification of OBJFUN is:
SUBROUTINE OBJFUN ( MODE, N, X, OBJF, OBJGRD, NSTATE, IUSER, RUSER)
INTEGER  MODE, N, NSTATE, IUSER(*)
REAL (KIND=nag_wp)  X(N), OBJF, OBJGRD(N), RUSER(*)
1:     MODE – INTEGERInput/Output
MODE indicates which parameter values within OBJFUN need to be set.
On entry: to OBJFUN, MODE is always set to the value that you set it to before the call to E04XAF/E04XAA.
On exit: its value must not be altered unless you wish to indicate a failure within OBJFUN, in which case it should be set to a negative value. If MODE is negative on exit from OBJFUN, the execution of E04XAF/E04XAA is terminated with IFAIL set to MODE.
2:     N – INTEGERInput
On entry: the number n of variables as input to E04XAF/E04XAA.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which the objective function (and gradients if MODE=1) is to be evaluated.
4:     OBJF – REAL (KIND=nag_wp)Output
On exit: must be set to the value of the objective function.
5:     OBJGRD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: if MODE=1, OBJGRDj must contain the value of the first derivative with respect to x.
If MODE1, OBJGRD need not be set.
6:     NSTATE – INTEGERInput
On entry: will be set to 1 on the first call of OBJFUN by E04XAF/E04XAA, and is 0 for all subsequent calls. Thus, if you wish, NSTATE may be tested within OBJFUN in order to perform certain calculations once only. For example you may read data.
7:     IUSER(*) – INTEGER arrayUser Workspace
8:     RUSER(*) – REAL (KIND=nag_wp) arrayUser Workspace
OBJFUN is called with the parameters IUSER and RUSER as supplied to E04XAF/E04XAA. You are free to use the arrays IUSER and RUSER to supply information to OBJFUN as an alternative to using COMMON global variables.
OBJFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04XAF/E04XAA is called. Parameters denoted as Input must not be changed by this procedure.
7:     LDH – INTEGERInput
On entry: the first dimension of the array H as declared in the (sub)program from which E04XAF/E04XAA is called.
Constraint: LDHN.
8:     HFORW(N) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the initial trial interval for computing the appropriate partial derivative to the jth variable.
If HFORWj0.0, then the initial trial interval is computed by E04XAF/E04XAA (see Section 3).
On exit: HFORWj is the best interval found for computing a forward-difference approximation to the appropriate partial derivative for the jth variable.
9:     OBJF – REAL (KIND=nag_wp)Output
On exit: the value of the objective function evaluated at the input vector in X.
10:   OBJGRD(N) – REAL (KIND=nag_wp) arrayOutput
On exit: if MODE=0 or 2, OBJGRDj contains the best estimate of the first partial derivative for the jth variable.
If MODE=1, OBJGRDj contains the first partial derivative for the jth variable evaluated at the input vector in X.
11:   HCNTRL(N) – REAL (KIND=nag_wp) arrayOutput
On exit: HCNTRLj is the best interval found for computing a central-difference approximation to the appropriate partial derivative for the jth variable.
12:   H(LDH,*) – REAL (KIND=nag_wp) arrayOutput
Note: the second dimension of the array H must be at least 1 if MODE=0 and at least N if MODE=1 or 2.
On exit: if MODE=0, the estimated Hessian diagonal elements are contained in the first column of this array.
If MODE=1 or 2, the estimated Hessian matrix is contained in the leading n by n part of this array.
13:   IWARN – INTEGEROutput
On exit: IWARN=0 on successful exit.
If the value of EPSRF on entry is too small or too large then IWARN is set to 1 or 2 respectively on exit and the default value for EPSRF is used within E04XAF/E04XAA.
If MSGLVL>0 then warnings will be printed if EPSRF is too small or too large.
14:   WORK(*) – REAL (KIND=nag_wp) arrayWorkspace
Note: the dimension of the array WORK must be at least N if MODE=0 and at least N×N+1 if MODE=1 or 2.
15:   IUSER(*) – INTEGER arrayUser Workspace
16:   RUSER(*) – REAL (KIND=nag_wp) arrayUser Workspace
IUSER and RUSER are not used by E04XAF/E04XAA, but are passed directly to OBJFUN and may be used to pass information to this routine as an alternative to using COMMON global variables.
17:   INFO(N) – INTEGER arrayOutput
On exit: INFOj represents diagnostic information on variable j. (See Section 6 for more details.)
18:   IFAIL – INTEGERInput/Output
Note: for E04XAA, IFAIL does not occur in this position in the parameter list. See the additional parameters described below.
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, if you are not familiar with this parameter, the recommended value is 0. 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).
Note: the following are additional parameters for specific use with E04XAA. Users of E04XAF therefore need not read the remainder of this description.
18:   LWSAV(1) – LOGICAL arrayCommunication Array
19:   IWSAV(1) – INTEGER arrayCommunication Array
20:   RWSAV(1) – REAL (KIND=nag_wp) arrayCommunication Array
These parameters are no longer required by E04XAF/E04XAA.
21:   IFAIL – INTEGERInput/Output
Note: see the parameter description for IFAIL above.

6  Error Indicators and Warnings

On exit from E04XAF/E04XAA both diagnostic parameters INFO and IFAIL should be tested. IFAIL represents an overall diagnostic indicator, whereas the integer array INFO represents diagnostic information on each variable.
If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
IFAIL<0
A negative value of IFAIL indicates an exit from E04XAF/E04XAA because you set MODE negative in OBJFUN. The value of IFAIL will be the same as your setting of MODE.
IFAIL=1
On entry, one or more of the following conditions are satisfied: N<1, LDH<N​ or ​MODE is invalid.
IFAIL=2
One or more variables have a nonzero INFO value. This may not necessarily represent an unsuccessful exit – see diagnostic information on INFO.
Diagnostic information returned via INFO is as follows:
INFO=1
The appropriate function appears to be constant. HFORWi is set to the initial trial interval value (see Section 3) corresponding to a well-scaled problem and Error est. in the printed output is set to zero. This value occurs when the estimated relative condition error in the first derivative approximation is unacceptably large for every value of the finite difference interval. If this happens when the function is not constant the initial interval may be too small; in this case, it may be worthwhile to rerun E04XAF/E04XAA with larger initial trial interval values supplied in HFORW (see Section 3). This error may also occur if the function evaluation includes an inordinately large constant term or if EPSRF is too large.
INFO=2
The appropriate function appears to be linear or odd. HFORWi is set to the smallest interval with acceptable bounds on the relative condition error in the forward- and backward-difference estimates. In this case, the estimated relative condition error in the second derivative approximation remained large for every trial interval, but the estimated error in the first derivative approximation was acceptable for at least one interval. If the function is not linear or odd the relative condition error in the second derivative may be decreasing very slowly, it may be worthwhile to rerun E04XAF/E04XAA with larger initial trial interval values supplied in HFORW (see Section 3).
INFO=3
The second derivative of the appropriate function appears to be so large that it cannot be reliably estimated (i.e., near a singularity). HFORWi is set to the smallest trial interval.
This value occurs when the relative condition error estimate in the second derivative remained very small for every trial interval.
If the second derivative is not large the relative condition error in the second derivative may be increasing very slowly. It may be worthwhile to rerun E04XAF/E04XAA with smaller initial trial interval values supplied in HFORW (see Section 3). This error may also occur when the given value of EPSRF is not a good estimate of a bound on the absolute error in the appropriate function (i.e., EPSRF is too small).
INFO=4
The algorithm terminated with an apparently acceptable estimate of the second derivative. However the forward-difference estimates of the appropriate first derivatives (computed with the final estimate of the ‘optimal’ forward-difference interval) and the central difference estimates (computed with the interval used to compute the final estimate of the second derivative) do not agree to half a decimal place. The usual reason that the forward- and central-difference estimates fail to agree is that the first derivative is small.
If the first derivative is not small, it may be helpful to execute the procedure at a different point.

7  Accuracy

If IFAIL=0 on exit the algorithm terminated successfully, i.e., the forward-difference estimates of the appropriate first derivatives (computed with the final estimate of the ‘optimal’ forward-difference interval hF) and the central-difference estimates (computed with the interval hϕ used to compute the final estimate of the second derivative) agree to at least half a decimal place.
In short word length implementations when computing the full Hessian matrix given function values only (i.e., MODE=2) the elements of the computed Hessian will have at best 1 to 2 figures of accuracy.

8  Further Comments

To evaluate an acceptable set of finite difference intervals for a well-scaled problem, the routine will require around two function evaluations per variable; in a badly scaled problem however, as many as six function evaluations per variable may be needed.
If you request the full Hessian matrix supplying both function and gradients (i.e., MODE=1) or function only (i.e., MODE=2) then a further N or 3×N×N+1/2 function evaluations respectively are required.

8.1  Description of the Printed Output

The following is a description of the printed output from E04XAF/E04XAA as controlled by the parameter MSGLVL.
Output when MSGLVL=1 is as follows:
J number of variable for which the difference interval has been computed.
Xj jth variable of x as set by you.
F. dif. int. the best interval found for computing a forward-difference approximation to the appropriate partial derivative with respect to the jth variable.
C. dif. int. the best interval found for computing a central-difference approximation to the appropriate partial derivative with respect to the jth variable.
Error est. a bound on the estimated error in the final forward-difference approximation. When INFOj=1, Error est. is set to zero.
Grad. est. best estimate of the first partial derivative with respect to the jth variable.
Hess diag est. best estimate of the second partial derivative with respect to the jth variable.
fun evals. the number of function evaluations used to compute the final difference intervals for the jth variable.
infoj the value of INFO for the jth variable.

9  Example

This example computes the gradient vector and the Hessian matrix of the following function:
Fx= x1+10x2 2+5 x3-x4 2+ x2-2x3 4+10 x1-x4 4
at the point 3,-1,0,1.

9.1  Program Text

Note: the following programs illustrate the use of E04XAF and E04XAA.

Program Text (e04xafe.f90)

Program Text (e04xaae.f90)

9.2  Program Data

None.

9.3  Program Results

Program Results (e04xafe.r)

Program Results (e04xaae.r)


E04XAF/E04XAA (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

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