Skip to contents

Compute the gradient of log density (which is returned by monty_model_density) with respect to parameters. Not all models support this, and an error will be thrown if it is not possible.

Usage

monty_model_gradient(model, parameters, named = FALSE)

Arguments

model

A monty_model object

parameters

A vector or matrix of parameters

named

Logical, indicating if the output should be named using the parameter names.

Value

A vector or matrix of gradients

See also

monty_model_density for log density, and monty_model_direct_sample to sample from a model

Examples

m <- monty_example("banana")
# Global maximum at (0, 0), and gradient is zero there:
monty_model_density(m, c(0, 0))
#> [1] -1.837877
monty_model_gradient(m, c(0, 0))
#> [1] 0 0

# Nonzero gradient away from the origin:
monty_model_gradient(m, c(0.4, 0.2))
#> [1] -1.440  0.376