Learn R Programming

SamplerCompare (version 1.3.4)

check.dist.gradient: Test a gradient function

Description

Evaluates the gradient function of a distribution object and compares it to a numeric gradient computed from the log density function.

Usage

check.dist.gradient(ds, x, h=1e-7)

Arguments

ds

A distribution object with defined log density and gradient functions.

x

A point inside the support of ds.

h

An offset from x at which to evaluate the log density when computing numeric derivatives.

Details

check.dist.gradient computes the numeric derivative of ds$log.density at x in each of its coordinates and compares this to the value returned by ds$grad.log.density. If the relative error is greater than \(0.001\), an error is reported.

This function can be used when defining a distribution to ensure that the gradient function is implemented correctly.

See Also

make.dist

Examples

Run this code
  check.dist.gradient(N2weakcor.dist, runif(2))

Run the code above in your browser using DataLab