
Last chance! 50% off unlimited learning
Sale ends in
Computes numeric second derivatives (hessian) of an arbitrary multidimensional function at a particular location.
secondDerivative(loc, FUN, ..., eps = 1e-07)
The location (a vector) where the second derivatives
of FUN
are desired.
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 loc
).
FUN
must return a single value (scalar), the height of the
surface above x
, i.e., FUN
evaluated at x
.
Additional agruments to FUN
.
Radius argument, see details. Default is 10e-7
.
Matrix
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.
# NOT RUN {
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)
# }
Run the code above in your browser using DataLab