Learn R Programming

ADtools (version 0.5.4)

finite_diff: Finite difference method

Description

Finite difference method

Usage

finite_diff(f, wrt = NULL, at, h = 1e-08, seed, method = "forward")

Arguments

f

A function of which the derivative is sought.

wrt

Character vector; the name of the variables to differentiate with respect to.

at

A named list of variables; the point at which the derivative is evaluated.

h

The finite differencing parameter; the size of perturbation.

seed

Seed; for pathwise derivative only.

method

"forward" for forward differencing or "central" for central differencing.

Value

A numeric matrix; the Jacobian matrix.

Examples

Run this code
# NOT RUN {
f <- function(y, X, beta) { y - X %*% beta }
finite_diff(
  f, wrt = "beta",
  at = list(X = matrix(1:4, 2, 2), y = c(2, 3), beta = c(5, 6))
)

g <- function(X, Y) { X %*% Y }
finite_diff(g, at = list(X = randn(2, 2), Y = randn(2, 2)))

h <- function(x) { exp(-x^2) }
finite_diff(h, at = list(x = 0.01), wrt = "x")
finite_diff(h, at = list(x = 0.01), wrt = "x", method = "central")

# }

Run the code above in your browser using DataLab