Learn R Programming

nimble (version 1.4.0)

nimDerivs: Nimble Derivatives

Description

Computes the value, 1st order (Jacobian), and 2nd order (Hessian) derivatives of a given nimbleFunction method and/or model log probabilities

Usage

nimDerivs(
  call = NA,
  wrt = NULL,
  order = nimC(0, 1, 2),
  model = NA,
  reset = FALSE,
  ...
)

Value

an ADNimbleList with elements value, jacobian, and hessian.

Arguments

call

a call to a nimbleFunction method with arguments included. Can also be a call to model$calculate(nodes), or to calculate(model, nodes).

wrt

a character vector of either: names of function arguments (if taking derivatives of a nimbleFunction method), or node names (if taking derivatives of model$calculate(nodes)) to take derivatives with respect to. If left empty, derivatives will be taken with respect to all arguments to nimFxn.

order

an integer vector with values within the set \({0, 1, 2}\), corresponding to whether the function value, Jacobian, and Hessian should be returned respectively. Defaults to c(0, 1, 2).

model

(optional) the uncompiled model that is used, if taking derivatives of a nimbleFunction that involves model calculations. This is needed in order to be able to correctly restore values into the model when order does not include 0 (or in all cases when double-taping). IMPORTANT: if model is included, one should also include the arguments updateNodes and constantNodes using the output obtained from running makeModelDerivsInfo.

reset

a logical specifying whether to reset the AD tape. See Section 17.4.5 of user manual for details. Not used/relevant for uncompiled execution. Defaults to FALSE.

...

additional arguments intended for internal use only.

Details

Derivatives for uncompiled nimbleFunctions are calculated using the numDeriv package. If this package is not installed, an error will be issued. Derivatives for matrix valued arguments will be returned in column-major order.

As discussed above with the model argument, if taking derivatives of a nimbleFunction that involves model calculations (rather than directly taking derivatives of `calculate`), care needs to be taken to provide model, updateNodes, and calcNodes arguments. See Section 17.6.2 of the User Manual for more details.

Examples

Run this code

if (FALSE) {
model <- nimbleModel(code = ...)
calcDerivs <- nimDerivs(model$calculate(model$getDependencies('x')),
 wrt = 'x')
}

Run the code above in your browser using DataLab