finite_diff: Finite difference method

Description Usage Arguments Value Examples

View source: R/finite_diff.R

Description

Finite difference method

Usage

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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")

ADtools documentation built on Nov. 9, 2020, 5:09 p.m.