hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_opt_check_deriv2 (e04hd)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

nag_opt_check_deriv2 (e04hd) checks that a function for calculating second derivatives of an objective function is consistent with a function for calculating the corresponding first derivatives.

Syntax

[g, hesl, hesd, iw, w, ifail] = e04hd(funct, h, x, lh, iw, w, 'n', n)
[g, hesl, hesd, iw, w, ifail] = nag_opt_check_deriv2(funct, h, x, lh, iw, w, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: liw and lw were removed from the interface

Description

Routines for minimizing a function Fx1,x2,,xn of the variables x1,x2,,xn may require you to provide a function to evaluate the second derivatives of F. nag_opt_check_deriv2 (e04hd) is designed to check the second derivatives calculated by such user-supplied functions. As well as the function to be checked (h), you must supply a function (funct) to evaluate the first derivatives, and a point x=x1,x2,,xnT at which the checks will be made. Note that nag_opt_check_deriv2 (e04hd) checks functions of the form required for nag_opt_bounds_mod_deriv2_comp (e04lb).
nag_opt_check_deriv2 (e04hd) first calls user-supplied functions funct and h to evaluate the first and second derivatives of F at x. The user-supplied Hessian matrix (H, say) is projected onto two orthogonal vectors y and z to give the scalars yTHy and zTHz 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 vector of first derivatives at the point in brackets and h is a small positive scalar. If the relative difference between p and yTHy or between q and zTHz is judged too large, an error indicator is set.

References

None.

Parameters

Compulsory Input Parameters

1:     funct – function handle or string containing name of m-file
funct must evaluate the function and its first derivatives at a given point. (nag_opt_bounds_mod_deriv2_comp (e04lb) gives you the option of resetting arguments of funct to cause the minimization process to terminate immediately. nag_opt_check_deriv2 (e04hd) will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
[iflag, fc, gc, iw, w] = funct(iflag, n, xc, iw, w)

Input Parameters

1:     iflag int64int32nag_int scalar
To funct, iflag will be set to 2.
2:     n int64int32nag_int scalar
The number n of variables.
3:     xcn – double array
The point x at which the function and first derivatives are required.
4:     iwliw int64int32nag_int array
5:     wlw – double array
These arguments are present so that funct will be of the form required by nag_opt_bounds_mod_deriv2_comp (e04lb). funct is called with nag_opt_check_deriv2 (e04hd)'s arguments iw, liw, w, lw as these arguments. If the advice given in nag_opt_bounds_mod_deriv2_comp (e04lb) is being followed, you will have no reason to examine or change any elements of iw or w. In any case, funct must not change the first 5×n elements of w.

Output Parameters

1:     iflag int64int32nag_int scalar
If you set iflag to some negative number in funct and return control to nag_opt_check_deriv2 (e04hd), nag_opt_check_deriv2 (e04hd) will terminate immediately with ifail set to your setting of iflag.
2:     fc – double scalar
Unless funct resets iflag, fc must be set to the value of the objective function F at the current point x.
3:     gcn – double array
Unless funct resets iflag, gcj must be set to the value of the first derivative F xj at the point x, for j=1,2,,n.
4:     iwliw int64int32nag_int array
5:     wlw – double array
nag_opt_check_deriv (e04hc) should be used to check the first derivatives calculated by funct before nag_opt_check_deriv2 (e04hd) is used to check the second derivatives, since nag_opt_check_deriv2 (e04hd) assumes that the first derivatives are correct.
2:     h – function handle or string containing name of m-file
h must evaluate the second derivatives of the function at a given point. (As with funct, a argument can be set to cause immediate termination.)
[iflag, fhesl, fhesd, iw, w] = h(iflag, n, xc, lh, fhesd, iw, w)

Input Parameters

1:     iflag int64int32nag_int scalar
Is set to a non-negative number.
2:     n int64int32nag_int scalar
The number n of variables.
3:     xcn – double array
The point x at which the second derivatives of Fx are required.
4:     lh int64int32nag_int scalar
The length of the array fhesl.
5:     fhesdn – double array
Contains the value of F xj  at the point x, for j=1,2,,n. Functions written to take advantage of a similar feature of nag_opt_bounds_mod_deriv2_comp (e04lb) can be tested as they stand by nag_opt_check_deriv2 (e04hd).
6:     iwliw int64int32nag_int array
7:     wlw – double array
As in funct, these arguments correspond to the arguments iw, liw, w and lw of nag_opt_check_deriv2 (e04hd). h must not change the first 5×n elements of w.

Output Parameters

1:     iflag int64int32nag_int scalar
If h resets iflag to a negative number, nag_opt_check_deriv2 (e04hd) will terminate immediately with ifail set to your setting of iflag.
2:     fhesllh – double array
Unless iflag is reset, h must place the strict lower triangle of the second derivative matrix of F (evaluated at the point x) in fhesl, stored by rows, i.e., fhesli-1i-2/2+j must be set to the value of 2F xixj at the point x, for i=2,3,,n and j=1,2,,i-1. (The upper triangle is not required because the matrix is symmetric.)
3:     fhesdn – double array
Unless iflag is reset, h must place the diagonal elements of the second derivative matrix of F (evaluated at the point x) in fhesd, i.e., fhesdj must be set to the value of 2F xj2 at the point x, for j=1,2,,n.
4:     iwliw int64int32nag_int array
5:     wlw – double array
3:     xn – double array
xj, for j=1,2,,n must contain the coordinates of a suitable point at which to check the derivatives calculated by funct. ‘Obvious’ settings, such as 0.0 ​ or ​ 1.0, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. Similarly, it is advisable that no two elements of x should be the same.
4:     lh int64int32nag_int scalar
The dimension of the array hesl.
Constraint: lhmax1,n×n-1/2.
5:     iwliw int64int32nag_int array
liw, the dimension of the array, must satisfy the constraint liw1.
This array is in the argument list so that it can be used by other library functions for passing integer quantities to funct or h. It is not examined or changed by nag_opt_check_deriv2 (e04hd). In general you must provide an array iw, but are advised not to use it.
6:     wlw – double array
lw, the dimension of the array, must satisfy the constraint lw5×n.
Constraint: lw5×n.

Optional Input Parameters

1:     n int64int32nag_int scalar
Default: the dimension of the array x.
The number n of independent variables in the objective function.
Constraint: n1.

Output Parameters

1:     gn – double array
Unless you set iflag negative in the first call of funct, gj contains the value of the first derivative F xj at the point given in x, as calculated by funct, for j=1,2,,n.
2:     hesllh – double array
Unless you set iflag negative in h, hesl contains the strict lower triangle of the second derivative matrix of F, as evaluated by h at the point given in x, stored by rows.
3:     hesdn – double array
Unless you set iflag negative in h, hesd contains the diagonal elements of the second derivative matrix of F, as evaluated by h at the point given in x.
4:     iwliw int64int32nag_int array
5:     wlw – double array
lw=5×n.
Communication array, used to store information between calls to nag_opt_check_deriv2 (e04hd).
6:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Note: nag_opt_check_deriv2 (e04hd) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W  ifail<0
A negative value of ifail indicates an exit from nag_opt_check_deriv2 (e04hd) because you have set iflag negative in funct or h. The value of ifail will be the same as the value you set for iflag. The check on h will not have been completed.
   ifail=1
On entry,n<1,
or lh < max1,n×n-1/2 ,
orliw<1,
orlw<5×n.
W  ifail=2
You should check carefully the derivation and programming of expressions for the second derivatives of Fx, because it is very unlikely that h is calculating them correctly.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

ifail is set to 2 if
yTHy-ph×yTHy+1.0  or ​ zTHz-qh×zTHz+1.0  
where h is set equal to ε (ε being the machine precision as given by nag_machine_precision (x02aj)) and other quantities are as defined in Description.

Further Comments

nag_opt_check_deriv2 (e04hd) calls h once and funct three times.

Example

Suppose that it is intended to use nag_opt_bounds_mod_deriv2_comp (e04lb) to minimize
F= x1+10x2 2+5 x3-x4 2+ x2-2x3 4+10 x1-x4 4  
The following program could be used to check the second derivatives calculated by h required. (The call of nag_opt_check_deriv2 (e04hd) is preceded by a call of nag_opt_check_deriv (e04hc) to check funct which calculates the first derivatives.)
function e04hd_example


fprintf('e04hd example results\n\n');

x = [1.46  -0.82    0.57   1.21];
n = int64(4);
lh = int64(6);
iw = [int64(0)];
w =  zeros(20,1);
[g, hesl, hesd, iw, w, ifail] = ...
e04hd( ...
       @funct, @hess, x, lh, iw, w);

[iflag, f, gc] = funct(n, n, x);

fprintf('The test point is: %8.2f%8.2f%8.2f%8.2f\n',x);

fprintf('\n2nd derivatives are consistent with 1st derivatives\n');
fprintf('\nAt test point, f = %7.3f\n',f)
fprintf(' 1st derivatives = %7.3f %7.3f %7.3f %7.3f\n',g)

fprintf('\nH gives the lower triangle of the Hessian matrix\n');
fprintf('%12.3e\n', hesd(1));
k = 1;
for i = 2:n
  fprintf('%12.3e', hesl(k:(k+i-2)), hesd(i)); 
  fprintf('\n');
  k = k + i - 1;
end



function [iflag, fc, gc] = funct(iflag, n, xc)
  gc = zeros(n, 1);
  fc = (xc(1)+10*xc(2))^2 +  5*(xc(3)-xc(4))^2 + ...
       (xc(2)- 2*xc(3))^4 + 10*(xc(1)-xc(4))^4;
  gc(1) =  2*(xc(1)+10*xc(2)) + 40*(xc(1)-xc(4))^3;
  gc(2) = 20*(xc(1)+10*xc(2)) +  4*(xc(2)-2*xc(3))^3;
  gc(3) = 10*(xc(3)-xc(4))    -  8*(xc(2)-2*xc(3))^3;
  gc(4) = 10*(xc(4)-xc(3))    - 40*(xc(1)-xc(4))^3;


function [iflag, fhesl, fhesd] = hess(iflag, n, xc, lh, fhesd)
  fhesl = zeros(lh, 1);
  fhesd = zeros(n, 1);

  fhesd(1) =   2 + 120*(xc(1)-  xc(4))^2;
  fhesd(2) = 200 +  12*(xc(2)-2*xc(3))^2;
  fhesd(3) =  10 +  48*(xc(2)-2*xc(3))^2;
  fhesd(4) =  10 + 120*(xc(1)-  xc(4))^2;

  fhesl(1) = 20;
  fhesl(2) =  0;
  fhesl(3) = -24*(xc(2)-2*xc(3))^2;
  fhesl(4) = -120*(xc(1)-xc(4))^2;
  fhesl(5) =  0;
  fhesl(6) = -10;
e04hd example results

The test point is:     1.46   -0.82    0.57    1.21

2nd derivatives are consistent with 1st derivatives

At test point, f =  62.273
 1st derivatives = -12.855 -164.918  53.836   5.775

H gives the lower triangle of the Hessian matrix
   9.500e+00
   2.000e+01   2.461e+02
   0.000e+00  -9.220e+01   1.944e+02
  -7.500e+00   0.000e+00  -1.000e+01   1.750e+01

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

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