numDerivs: Functions for creating Gradient and Hessian matrices by...

numDerivsR Documentation

Functions for creating Gradient and Hessian matrices by numerical differentiation (Richardson's method) of the partial derivatives

Description

These two functions create Gradient and Hessian matrices by Richardson's central finite difference method of the partial derivatives for any expression.

Usage

numGrad(expr, envir = parent.frame())
numHess(expr, envir = parent.frame())

Arguments

expr

an expression, such as expression(x/y).

envir

the environment in which the variables of expr are found (default: the calling environment, so that it also works inside functions).

Details

Calculates first- and second-order numerical approximations of the partial derivatives at the current values x = (x_1, \ldots, x_N) of the variables, using the central difference formulas

f'_i(x) \approx \frac{f(x_1, \ldots, x_i + d_i, \ldots, x_N) - f(x_1, \ldots, x_i - d_i, \ldots, x_N)}{2d_i}

f''_{ii}(x) \approx \frac{f(x_1, \ldots, x_i + d_i, \ldots, x_N) - 2f(x_1, \ldots, x_N) + f(x_1, \ldots, x_i - d_i, \ldots, x_N)}{d_i^2}

and, for the mixed partial derivatives (i \neq j), with x^{\pm} = x with x_i \pm d_i and x_j \pm d_j,

f''_{ij}(x) \approx \frac{f(x^{+}) - 2f(x) + f(x^{-}) - f''_{ii}(x)\,d_i^2 - f''_{jj}(x)\,d_j^2}{2 d_i d_j}.

These approximations are refined by Richardson extrapolation, as in genD of the 'numDeriv' package: each is computed for r = 4 step sizes, starting with d_i = 0.1|x_i| (plus 10^{-4} if x_i is close to 0) and halving the step each time, and the r values D_1, \ldots, D_r are combined successively for m = 1, \ldots, r - 1 by D_k \leftarrow (4^m D_{k+1} - D_k)/(4^m - 1).

Value

The numeric Gradient/Hessian matrices.

Note

The two functions are modified versions of the genD function in the 'numDeriv' package, but easier to handle because they use expressions: the variables are named as in the expression and need not be written as x[1], x[2], ..., x[n] in the body of a function.

Author(s)

Andrej-Nikolai Spiess

Examples

## Check for equality of symbolic  
## and numerical derivatives.
EXPR <- expression(2^x + sin(2 * y) - cos(z))
x <- 5
y <- 10
z <- 20

symGRAD <- evalDerivs(makeGrad(EXPR))
numGRAD <- numGrad(EXPR)
all.equal(symGRAD, numGRAD)

symHESS <- evalDerivs(makeHess(EXPR))
numHESS <- numHess(EXPR)
all.equal(symHESS, numHESS)

propagate documentation built on Nov. 10, 2026, 9:07 a.m.