| numDerivs | R Documentation |
These two functions create Gradient and Hessian matrices by Richardson's central finite difference method of the partial derivatives for any expression.
numGrad(expr, envir = parent.frame())
numHess(expr, envir = parent.frame())
expr |
an expression, such as |
envir |
the |
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).
The numeric Gradient/Hessian matrices.
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.
Andrej-Nikolai Spiess
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.