Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

check.derivatives

Check analytic gradients of a function using finite difference approximations


Description

This function compares the analytic gradients of a function with a finite difference approximation and prints the results of these checks.

Usage

check.derivatives(.x, func, func_grad, check_derivatives_tol = 1e-04,
  check_derivatives_print = "all", func_grad_name = "grad_f", ...)

Arguments

.x

point at which the comparison is done.

func

function to be evaluated.

func_grad

function calculating the analytic gradients.

check_derivatives_tol

option determining when differences between the analytic gradient and its finite difference approximation are flagged as an error.

check_derivatives_print

option related to the amount of output. 'all' means that all comparisons are shown, 'errors' only shows comparisons that are flagged as an error, and 'none' shows the number of errors only.

func_grad_name

option to change the name of the gradient function that shows up in the output.

...

further arguments passed to the functions func and func_grad.

Value

The return value contains a list with the analytic gradient, its finite difference approximation, the relative errors, and vector comparing the relative errors to the tolerance.

Author(s)

Jelmer Ypma

See Also

Examples

library('nloptr')

# example with correct gradient
f <- function( x, a ) {
	return( sum( ( x - a )^2 ) )
}

f_grad <- function( x, a ) {
	return( 2*( x - a ) )
}

check.derivatives( .x=1:10, func=f, func_grad=f_grad,
    check_derivatives_print='none', a=runif(10) )

# example with incorrect gradient
f_grad <- function( x, a ) {
	return( 2*( x - a ) + c(0,.1,rep(0,8)) )
}

check.derivatives( .x=1:10, func=f, func_grad=f_grad,
    check_derivatives_print='errors', a=runif(10) )

# example with incorrect gradient of vector-valued function
g <- function( x, a ) {
	return( c( sum(x-a), sum( (x-a)^2 ) ) )
}

g_grad <- function( x, a ) {
	return( rbind( rep(1,length(x)) + c(0,.01,rep(0,8)), 2*(x-a) + c(0,.1,rep(0,8)) ) )
}

check.derivatives( .x=1:10, func=g, func_grad=g_grad,
    check_derivatives_print='all', a=runif(10) )

nloptr

R Interface to NLopt

v1.2.2.2
LGPL-3
Authors
Jelmer Ypma [aut, cre], Steven G. Johnson [aut] (author of the NLopt C library), Hans W. Borchers [ctb], Dirk Eddelbuettel [ctb], Brian Ripley [ctb] (build process on multiple OS), Kurt Hornik [ctb] (build process on multiple OS), Julien Chiquet [ctb], Avraham Adler [ctb] (removal deprecated calls from tests, <https://orcid.org/0000-0002-3039-0703>)
Initial release
2020-07-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.