Syntax Supported by the IML Procedure and the iml Action

FDDSOLVE Call

CALL FDDSOLVE (f, g, h, "fun", x0, <, opt> <, "grd"> ) ;

CALL FDDSOLVE (f, g, h, "fun", x0) <OPT=opt> <GRD="grd"> ;

This subroutine is supported by the IML procedure and the iml action.

The FDDSOLVE subroutine uses finite differences to approximate derivatives at a specified point in the domain of a differentiable function. Derivatives are important for many tasks in computational statistics, including optimization, solving nonlinear equations, and fitting statistical models to data.

The FDDSOLVE subroutine approximates derivatives for a function f colon upper R Superscript n Baseline right-arrow upper R Superscript m for m greater-than-or-equal-to 1. When m equals 1, the function is a scalar-valued function. When m greater-than 1, the function is a vector-valued function. The derivatives are evaluated at an n-dimensional point, bold x bold 0, in the domain of f.

The FDDSOLVE subroutine calls the user-defined module "fun" to evaluate the function f and its derivatives at the point x0.

  • If the module "fun" returns a scalar value, the FDDSOLVE subroutine computes the following quantities:

    • the 1 times 1 function value f

    • the 1 times n gradient vector g

    • the n times n Hessian matrix H

  • If the module "fun" returns a column vector of m function values, the subroutine computes the following quantities:

    • the m times 1 function value f

    • the m times n Jacobian matrix bold upper J

    • the n times n crossproduct matrix bold upper J prime bold upper J

The subroutine returns a missing value for any result that cannot be computed.

The FDDSOLVE subroutine requires the following input arguments:

  • The "fun" argument specifies a module that returns the value of the function, f. For vector-valued functions, the module should return a column vector.

  • The x0 argument is a row vector of length n that defines the point at which the functions and derivatives are computed.

In addition, you can specify the following input arguments:

  • The opt argument is a three-element vector. It is optional for scalar-valued functions, but it is required for vector-valued functions. You can use the OPT keyword to specify the vector opt.

    • opt[1] indicates how to approximate the first derivatives in the gradient vector or Jacobian matrix. If opt[1] is missing or equal to 0, the subroutine uses forward-difference derivatives. Otherwise, it uses central-difference derivatives. Forward differences are faster but less accurate than central differences. The default is to use forward-difference derivatives.

    • opt[2] indicates how to approximate the Hessian matrix for a scalar-valued function. If opt[2] is missing or equal to 0, the subroutine uses forward-difference derivatives. Otherwise, it uses central-difference derivatives. The default is to use forward-difference derivatives. This option is ignored for vector-valued functions.

    • opt[3] is relevant only to vector-valued functions. It specifies the number of elements that are returned by the module "fun". If opt[3] is missing or less than 1, it is set to 1.

  • The optional "grd" argument specifies a user-defined function module that returns the analytical gradient of the function at the point x0. You can use the GRD keyword to specify the module "grd". If specified, this module is called to compute the gradient, and the Hessian is approximated by using first-order derivatives of the gradient function. For vector-valued functions, the "grd" argument is ignored.

The FDDSOLVE subroutine is useful for checking first-order and second-order analytical derivatives. It is also useful for approximating derivatives when a formula is not available.

Derivatives for a Scalar-Valued Function

The following example demonstrates how to call the FDDSOLVE subroutine. In the unconstrained Rosenbrock problem, the objective function is

f left-parenthesis x right-parenthesis equals 50 left-parenthesis x 2 minus x 1 squared right-parenthesis squared plus one-half left-parenthesis 1 minus x 1 right-parenthesis squared

The gradient and the Hessian are

StartLayout 1st Row 1st Column g left-parenthesis x comma y right-parenthesis 2nd Column equals 3rd Column Start 1 By 2 Matrix 1st Row 1st Column StartFraction partial-differential f Over partial-differential x 1 EndFraction 2nd Column StartFraction partial-differential f Over partial-differential x 2 EndFraction EndMatrix equals Start 1 By 2 Matrix 1st Row 1st Column 200 x 1 cubed minus 200 x 1 x 2 plus x 1 minus 1 2nd Column minus 100 x 1 squared plus 100 x 2 EndMatrix 2nd Row 1st Column upper H left-parenthesis x comma y right-parenthesis 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column StartFraction partial-differential squared f Over partial-differential x 1 squared EndFraction 2nd Column StartFraction partial-differential squared f Over partial-differential x 1 partial-differential x 2 EndFraction 2nd Row 1st Column StartFraction partial-differential squared f Over partial-differential x 2 partial-differential x 1 EndFraction 2nd Column StartFraction partial-differential squared f Over partial-differential x 2 squared EndFraction EndMatrix equals Start 2 By 2 Matrix 1st Row 1st Column 600 x 1 squared minus 200 x 2 plus 1 2nd Column minus 200 x 1 2nd Row 1st Column minus 200 x 1 2nd Column 100 EndMatrix EndLayout

At the point x equals left-parenthesis 2 comma 7 right-parenthesis, these expressions evaluate to

StartLayout 1st Row 1st Column g left-parenthesis 2 comma 7 right-parenthesis 2nd Column equals 3rd Column Start 1 By 2 Matrix 1st Row 1st Column negative 1199 2nd Column 300 EndMatrix 2nd Row 1st Column upper H left-parenthesis 2 comma 7 right-parenthesis 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column 1001 2nd Column negative 400 2nd Row 1st Column negative 400 2nd Column 100 EndMatrix EndLayout

The following statements define the Rosenbrock function and use the FDDSOLVE subroutine to compute the gradient and the Hessian. The results are shown in Figure 117.

start F_ROSEN(x);
   y1 = 10 * (x[2] - x[1] * x[1]);
   y2 =  1 - x[1];
   f  = 0.5 * (y1 * y1 + y2 * y2);
   return(f);
finish F_ROSEN;
x = {2 7};
call fddsolve(f, grad, Hess, "F_ROSEN", x);
print f, grad, Hess;

Figure 117: Gradient and Hessian at a Point

f
450.5

grad
-1199300.00001

Hess
1001.0438-400.0018
-400.001899.99992


Figure 117 shows forward-difference approximations to the derivatives. You can compute central-difference derivatives by using the OPT keyword: OPT={1 1}.

If you define a function that evaluates the gradient of the function, you can use the GRD keyword to use the analytic gradient. For example, the following call to the FDDSOLVE subroutine uses the G_ROSEN function to evaluate the analytical gradient. The Hessian is computed by using first-order differences of the gradient function.

start G_ROSEN(x);
   g = j(1,2,0.);
   g[1] = -200*x[1]*(x[2]-x[1]*x[1]) - (1-x[1]);
   g[2] =  100*(x[2]-x[1]*x[1]);
   return(g);
finish;

call fddsolve(f, grad, Hess, "F_ROSEN", x) grd="G_ROSEN";

Derivatives for a Vector-Valued Function

If the Rosenbrock problem is considered from a least squares perspective, the two component functions are

StartLayout 1st Row 1st Column f 1 left-parenthesis x right-parenthesis 2nd Column equals 3rd Column 10 left-parenthesis x 2 minus x 1 squared right-parenthesis 2nd Row 1st Column f 2 left-parenthesis x right-parenthesis 2nd Column equals 3rd Column 1 minus x 1 EndLayout

The Jacobian and the crossproduct of the Jacobian are

StartLayout 1st Row 1st Column bold upper J 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column StartFraction partial-differential f 1 Over partial-differential x 1 EndFraction 2nd Column StartFraction partial-differential f 1 Over partial-differential x 2 EndFraction 2nd Row 1st Column StartFraction partial-differential f 2 Over partial-differential x 1 EndFraction 2nd Column StartFraction partial-differential f 2 Over partial-differential x 2 EndFraction EndMatrix equals Start 2 By 2 Matrix 1st Row 1st Column minus 20 x 1 2nd Column 10 2nd Row 1st Column negative 1 2nd Column 0 EndMatrix 2nd Row 1st Column bold upper J Superscript upper T Baseline bold upper J 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column 400 x 1 squared plus 1 2nd Column minus 200 x 1 2nd Row 1st Column minus 200 x 1 2nd Column 100 EndMatrix EndLayout

At the point x equals left-parenthesis 2 comma 7 right-parenthesis, these matrices evaluate to

StartLayout 1st Row 1st Column bold upper J left-parenthesis 2 comma 7 right-parenthesis 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column negative 40 2nd Column 10 2nd Row 1st Column negative 1 2nd Column 0 EndMatrix 2nd Row 1st Column bold upper J Superscript upper T Baseline bold upper J vertical-bar Subscript left-parenthesis 2 comma 7 right-parenthesis Baseline 2nd Column equals 3rd Column Start 2 By 2 Matrix 1st Row 1st Column 1601 2nd Column negative 400 2nd Row 1st Column negative 400 2nd Column 100 EndMatrix EndLayout

The following statements define the Rosenbrock problem in a least squares framework and use the FDDSOLVE subroutine to compute the Jacobian and the crossproduct matrix. The OPT keyword is used to specify the vector opt. Because the last element of opt is 2, the FDDSOLVE subroutine allocates memory for a least squares problem with two functions, f 1 left-parenthesis x right-parenthesis and f 2 left-parenthesis x right-parenthesis.

start F_ROSEN(x);
   y = j(2, 1, 0);
   y[1] = 10 * (x[2] - x[1] * x[1]);
   y[2] =  1 - x[1];
   return(y);
finish F_ROSEN;
x     = {2 7};
parms = {. . 2};
call fddsolve(f, jac, crpj, "F_ROSEN", x) opt=parms;
print f, jac, crpj;

Figure 118: Jacobian and Crossproduct Matrix at a Point

f
30
-1

jac
-4010
-10

crpj
1601-400
-400100


For this example, the domain and the range of the objective function are both two-dimensional. However, in general, you can analyze the derivatives of a function f colon upper R Superscript n Baseline right-arrow upper R Superscript m for any values of n and m. The dimension of the domain is determined by the number of elements in the x0 vector. The dimension of the range is determined by the last element of the vector opt.

Formulas for Finite-Difference Gradients

In the following formulas for gradients, Jacobians, and Hessians, the vector e Subscript i is the ith basis row vector e Subscript i Baseline equals left-parenthesis 0 comma ellipsis comma 0 comma 1 comma 0 comma ellipsis comma 0 right-parenthesis, where the 1 is in the ith coordinate position. The symbol eta denotes machine precision, which is approximately 2.22E–16.

The FDDSOLVE subroutine approximates derivatives by using finite-difference formulas. The formulas depend on whether you request a forward-difference or central-difference derivative.

If you do not provide an analytic gradient function, then the following formulas are used:

Forward-difference gradient:

The ith component of the gradient vector at the point x is computed by using the formula

g Subscript i Baseline left-parenthesis x right-parenthesis equals StartFraction f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline right-parenthesis minus f left-parenthesis x right-parenthesis Over h Subscript i Baseline EndFraction

The step size is defined by h Subscript i Baseline equals StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the function f at n plus 1 locations.

Central-difference gradient:

The ith component of the gradient vector at the point x is computed by using the formula

g Subscript i Baseline left-parenthesis x right-parenthesis equals StartFraction f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline right-parenthesis minus f left-parenthesis x minus h Subscript i Baseline e Subscript i Baseline right-parenthesis Over 2 h Subscript i Baseline EndFraction

The step size is defined by h Subscript i Baseline equals RootIndex 3 StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the function f at 2 n locations.

Formulas for Finite-Difference Jacobians

The finite-difference formulas for a Jacobian are similar to the formulas for the gradient. The left-parenthesis i comma j right-parenthesisth position of the Jacobian matrix is the partial derivative of the ith component function with respect to the jth variable: upper J Subscript i j Baseline equals StartFraction partial-differential f Subscript i Baseline Over partial-differential x Subscript j Baseline EndFraction.

Forward-difference Jacobian:

The left-parenthesis i comma j right-parenthesisth element of the Jacobian matrix at the point x is approximated by using the formula

upper J Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction f Subscript i Baseline left-parenthesis x plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus f Subscript i Baseline left-parenthesis x right-parenthesis Over h Subscript j Baseline EndFraction

The step size is defined by h Subscript j Baseline equals StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript j Baseline EndAbsoluteValue right-parenthesis. The formula evaluates each component function f Subscript i at n plus 1 locations.

Central-difference Jacobian:
upper J Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction f Subscript i Baseline left-parenthesis x plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus f Subscript i Baseline left-parenthesis x minus h Subscript j Baseline e Subscript j Baseline right-parenthesis Over 2 h Subscript j Baseline EndFraction

The step size is defined by h Subscript j Baseline equals RootIndex 3 StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript j Baseline EndAbsoluteValue right-parenthesis. The formula evaluates each component function f Subscript i at 2 n locations.

Formulas for Finite-Difference Hessians

If you do not provide an analytical gradient, then the Hessian matrix is approximated by using a second-order finite-difference formula.

Forward-difference Hessian:

The left-parenthesis i comma j right-parenthesisth component of the Hessian matrix at the point x is computed by using the formula

upper H Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline right-parenthesis minus f left-parenthesis x plus h Subscript j Baseline e Subscript j Baseline right-parenthesis plus f left-parenthesis x right-parenthesis Over h Subscript i Baseline h Subscript j Baseline EndFraction

The step size is defined by h Subscript i Baseline equals RootIndex 3 StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the function f at one-half n squared plus three-halves n plus 1 locations.

Central-difference Hessian:

The left-parenthesis i comma j right-parenthesisth component of the Hessian matrix at the point x is computed by using the formula

upper H Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus f left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline minus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus f left-parenthesis x minus h Subscript i Baseline e Subscript i Baseline plus h Subscript j Baseline e Subscript j Baseline right-parenthesis plus f left-parenthesis x minus h Subscript i Baseline e Subscript i Baseline minus h Subscript j Baseline e Subscript j Baseline right-parenthesis Over 4 h Subscript i Baseline h Subscript j Baseline EndFraction

The step size is defined by h Subscript i Baseline equals RootIndex 4 StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the function f at 2 n squared plus 1 locations.

If you provide an analytic gradient function, the Hessian is the result of first-order calls to the gradient function, as follows:

Forward-difference Hessian:

The left-parenthesis i comma j right-parenthesisth component of the Hessian matrix at the point x is computed by using the formula

upper H Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction g Subscript i Baseline left-parenthesis x plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus g Subscript i Baseline left-parenthesis x right-parenthesis Over 2 h Subscript j Baseline EndFraction plus StartFraction g Subscript j Baseline left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline right-parenthesis minus g Subscript j Baseline left-parenthesis x right-parenthesis Over 2 h Subscript i Baseline EndFraction

The step size is defined by h Subscript i Baseline equals StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the gradient at n plus 1 locations.

Central-difference Hessian:

The left-parenthesis i comma j right-parenthesisth component of the Hessian matrix at the point x is computed by using the formula

upper H Subscript i j Baseline left-parenthesis x right-parenthesis equals StartFraction g Subscript i Baseline left-parenthesis x plus h Subscript j Baseline e Subscript j Baseline right-parenthesis minus g Subscript i Baseline left-parenthesis x minus h Subscript j Baseline e Subscript j Baseline right-parenthesis Over 4 h Subscript j Baseline EndFraction plus StartFraction g Subscript j Baseline left-parenthesis x plus h Subscript i Baseline e Subscript i Baseline right-parenthesis minus g Subscript j Baseline left-parenthesis x minus h Subscript i Baseline e Subscript i Baseline right-parenthesis Over 4 h Subscript i Baseline EndFraction

The step size is defined by h Subscript i Baseline equals RootIndex 3 StartRoot eta EndRoot left-parenthesis 1 plus StartAbsoluteValue x Subscript i Baseline EndAbsoluteValue right-parenthesis. The formula evaluates the gradient at 2 n locations.

Last updated: March 08, 2024