Learn R Programming

maxLik (version 0.5-6)

compareDerivatives: function to compare analytic and numeric derivatives

Description

This function compares analytic and numerical derivative and prints a few diagnostics. It is intended for testing pre-programmed derivative routines for maximisation algorithms.

Usage

compareDerivatives(f, grad, hess=NULL, t0, eps=1e-6, print=TRUE, ...)

Arguments

f
function to be differentiated. The parameter (vector) of interest must be the first argument. The function may return a vector.
grad
function returning the analytic gradient. Must use the same set of parameters as f. If f is a vector-valued function, grad must return a matrix where the number of rows equals the number of components of f
hess
function returning the analytic hessian. If present, hessian matrices are compared too. Only appropriate for scalar-valued functions.
t0
parameter vector indicating the point at which the derivatives are compared. The derivative is taken with respect to this vector.
eps
numeric. Step size for numeric differentiation. Central derivative is used.
print
logical: TRUE to print a summary, FALSE to return the comparison only (invisibly).
...
further arguments to f, grad and hess.

Value

  • A list with the following components:

    • t0
    {the input argument t0}

  • f.t0f(t0)
  • compareGrada list with components analytic = grad(t0), nmeric = numericGradient(f, t0), and their rel.diff.
  • maxRelDiffGradmax(abs(rel.diff))
  • If hess is also provided, the following optional components are also present:
  • compareHessiana list with components analytic = hess(t0), numeric = numericGradient(grad, t0), and their rel.diff.
  • maxRelDiffHessmax(abs(rel.diff)) for the Hessian

Details

For every component of f, the parameter value, analytic and numeric derivative and their relative difference

rel.diff = (analytic - numeric)/(0.5*(analytic+numeric)) are printed; if analytic = 0 = numeric, we define rel.diff = 0. If analytic derivatives are correct and the function is sufficiently smooth, expect the relative differences to be less than 1e-7.

See Also

numericGradient deriv

Examples

Run this code
## A simple example with sin(x)' = cos(x)
f <- function(x)c(sin=sin(x))
Dsin <- compareDerivatives(f, cos, t0=c(angle=1))
D2sin <- compareDerivatives(f, cos, function(x)-sin(x), t0=1)

##
## Example of log-likelihood of normal density.  Two-parameter
## function.
##
x <- rnorm(100, 1, 2) # generate rnorm x
l <- function(b) sum(log(dnorm((x-b[1])/b[2])/b[2]))
              # b[1] = mu, b[2] = sigma
gradl <- function(b) {
    c(mu=sum(x - b[1])/b[2]^2,
    sigma=sum((x - b[1])^2/b[2]^3 - 1/b[2]))
}
gradl. <- compareDerivatives(l, gradl, t0=c(mu=1,sigma=2))

##
## An example with f returning a vector, t0 = a scalar
##
trig <- function(x)c(sin=sin(x), cos=cos(x))
Dtrig <- function(x)c(sin=cos(x), cos=-sin(x))
Dtrig. <- compareDerivatives(trig, Dtrig, t0=1)

D2trig <- function(x)-trig(x) 
D2trig. <- compareDerivatives(trig, Dtrig, D2trig, t0=1)

Run the code above in your browser using DataLab