hessian: Estimates the hessian matrix

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Given a vector of variables (x), and a function (f) that estimates one function value, estimates the hessian matrix by numerical differencing. The hessian matrix is a square matrix of second-order partial derivatives of the function f with respect to x. It contains, on rows i and columns j

d^2(f(x))/d(x_i)/d(x_j)

Usage

1
hessian(f, x, centered = FALSE, pert = 1e-8, ...)

Arguments

f

function returning one function value, or a vector of function values.

x

either one value or a vector containing the x-value(s) at which the hessian matrix should be estimated.

centered

if TRUE, uses a centered difference approximation, else a forward difference approximation.

pert

numerical perturbation factor; increase depending on precision of model solution.

...

other arguments passed to function f.

Details

Function hessian(f,x) returns a forward or centered difference approximation of the gradient, which itself is also estimated by differencing. Because of that, it is not very precise.

Value

The gradient matrix where the number of rows equals the length of f and the number of columns equals the length of x.

the elements on i-th row and j-th column contain: d((f(x))_i)/d(x_j)

Author(s)

Karline Soetaert <karline.soetaert@nioz.nl>

See Also

gradient, for a full (not necessarily square) gradient matrix

Examples

1
2
3
4
5
6
7
8
## =======================================================================
## the banana function
## =======================================================================
   fun <- function(x)  100*(x[2] - x[1]^2)^2 + (1 - x[1])^2
   mm  <- nlm(fun, p = c(0, 0))$estimate
   (Hes <- hessian(fun, mm))
   # can also be estimated by nlm(fun, p=c(0,0), hessian=TRUE)
   solve(Hes)   # estimate of parameter uncertainty

Example output

          [,1]      [,2]
[1,]  801.9968 -399.9992
[2,] -399.9992  200.0000
          [,1]      [,2]
[1,] 0.4999936 0.9999853
[2,] 0.9999853 2.0049665

rootSolve documentation built on Sept. 23, 2021, 3 a.m.