function to compare analytic and numeric derivatives
This function compares analytic and numerical derivative and prints related diagnostics information. It is intended for testing and debugging code for analytic derivatives for maximization algorithms.
compareDerivatives(f, grad, hess=NULL, t0, eps=1e-6, printLevel=1, print=printLevel > 0, max.rows=getOption("max.rows", 20), max.cols=getOption("max.cols", 7), ...)
f |
function to be differentiated. The parameter (vector) of interest must be the first argument. The function may return a vector, in that case the derivative will be a matrix. |
grad |
analytic gradient. This may be either a function,
returning the analytic gradient, or a numeric vector, the pre-computed
gradient. The function must use the same set of
parameters as |
hess |
function returning the analytic hessian. If present, hessian matrices are compared too. Only appropriate for scalar-valued functions. |
t0 |
numeric vector, parameter at which the derivatives are
compared. The derivative is taken with respect to this vector. both
|
eps |
numeric. Step size for numeric differentiation. Central derivative is used. |
printLevel |
numeric: a positive number prints summary of the comparison. 0 does not do any printing, only returns the comparison results (invisibly). |
print |
deprecated (for backward compatibility only). |
max.rows |
maximum number of matrix rows to be printed. |
max.cols |
maximum number of columns to be printed. |
... |
further arguments to |
Analytic derivatives (and Hessian) substantially improve the
estimation speed and reliability. However, these are
typically hard to program. This utility compares the programmed result
and the (internally calculated) numeric derivative.
For every component of f
, it prints the parameter value, analytic and
numeric derivative, and their relative difference
rel.diff = (analytic - numeric)/(0.5*(abs(analytic) + abs(numeric))).
If analytic == 0 and numeric == 0, then rel.diff is also set to 0. If analytic derivatives are correct and the function is sufficiently smooth, expect the relative differences to be less than 1e-7.
A list with following components:
t0 |
the input argument |
f.t0 |
f(t0) |
compareGrad |
a list with components |
maxRelDiffGrad |
max(abs(rel.diff)) |
If hess
is also provided, the following optional components
are also present:
compareHessian |
a list with components |
maxRelDiffHess |
max(abs(rel.diff)) for the Hessian |
Ott Toomet otoomet@ut.ee and Spencer Graves
## A simple example with sin(x)' = cos(x) f <- function(x) c(sin=sin(x)) Dsin <- compareDerivatives(f, cos, t0=c(angle=1)) ## ## Example of normal log-likelihood. Two-parameter ## function. ## x <- rnorm(100, 1, 2) # generate rnorm x l <- function(b) sum(dnorm(x, mean=b[1], sd=b[2], log=TRUE)) 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)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.