numerical_deriv: Compute numerical derivatives

Description Usage Arguments Author(s) Examples

View source: R/utils.R

Description

Compute numerical derivatives using forward/backword difference, central difference, or Richardson extropolation.

Usage

1
2
numerical_deriv(par, f, ..., delta = 1e-05, gradient = TRUE,
  type = "forward")

Arguments

par

a vector of parameters

f

the objective function being evaluated

...

additional arguments to be passed to f and the numDeriv package when the Richardson type is used

delta

the term used to perturb the f function. Default is 1e-5

gradient

logical; compute the gradient terms? If FALSE then the Hessian is computed instead

type

type of difference to compute. Can be either 'forward' for the forward difference, 'central' for the central difference, or 'Richardson' for the Richardson extropolation. Backword difference is acheived by supplying a negative delta value

Author(s)

Phil Chalmers rphilip.chalmers@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
## Not run: 
f <- function(x) 3*x[1]^3 - 4*x[2]^2
par <- c(3,8)

# grad = 9 * x^2 , -8 * y
(actual <- c(9 * par[1]^2, -8 * par[2]))
numerical_deriv(par, f, type = 'forward')
numerical_deriv(par, f, type = 'central')
numerical_deriv(par, f, type = 'Richardson')

# hessian = h11 -> 18 * x, h22 -> -8, h12 -> h21 -> 0
(actual <- matrix(c(18 * par[1], 0, 0, -8), 2, 2))
numerical_deriv(par, f, type = 'forward', gradient = FALSE)
numerical_deriv(par, f, type = 'central', gradient = FALSE)
numerical_deriv(par, f, type = 'Richardson', gradient = FALSE)


## End(Not run)

xzhaopsy/MIRT documentation built on May 29, 2019, 12:42 p.m.