View source: R/secondDerivative.R
secondDerivative | R Documentation |
Computes numeric second derivatives (hessian) of an arbitrary multidimensional function at a particular location.
secondDerivative(loc, FUN, ..., eps = 1e-07)
loc |
The location (a vector) where the second derivatives
of |
FUN |
An R function to compute second derivatives for.
This must be a function of the form FUN <- function(x, ...)...
where x is the parameters of the function (e.g., location |
... |
Additional agruments to |
eps |
Radius argument, see details. Default is |
This function uses the "5-point" numeric second derivative
method advocated in numerous numerical recipe texts. During computation
of the second derivative, FUN will be evaluated at locations within a hyper-elipsoid
with cardinal radius 2*loc*(eps)^0.25
.
A handy way to use this function is to call an optimization routine
like nlminb
with FUN, then call this function with the
optimized values (solution) and FUN. This will yeild the hessian
at the solution rather than the hessian at the previous step of the
optimization.
Matrix
func <- function(x){-x*x} # second derivative should be -2
secondDerivative(0,func)
secondDerivative(3,func)
func <- function(x){3 + 5*x^2 + 2*x^3} # second derivative should be 10+12x
secondDerivative(0,func)
secondDerivative(2,func)
func <- function(x){x[1]^2 + 5*x[2]^2} # should be rbind(c(2,0),c(0,10))
secondDerivative(c(1,1),func)
secondDerivative(c(4,9),func)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.