Skip to contents

Create a monty_model from a function that computes density. This allows use of any R function as a simple monty model. If you need advanced model features, then this interface may not suit you and you may prefer to use monty_model directly.

Usage

monty_model_function(
  density,
  packer = NULL,
  fixed = NULL,
  allow_multiple_parameters = FALSE
)

Arguments

density

A function to compute log density. It can take any number of parameters

packer

Optionally, a monty_packer object to control how your function parameters are packed into a numeric vector. You can typically omit this if all the arguments to your functions are present in your numeric vector and if they are all scalars.

fixed

Optionally, a named list of fixed values to substitute into the call to density. This cannot be used in conjunction with packer (you should use the fixed argument to monty_packer instead).

allow_multiple_parameters

Logical, indicating if passing in vectors for all parameters will return a vector of densities. This is FALSE by default because we cannot determine this automatically.

Value

A monty_model object that computes log density with the provided density function, given a numeric vector argument representing all parameters.

Details

This interface will expand in future versions of monty to support gradients, stochastic models, parameter groups and simultaneous calculation of density.

Examples

banana <- function(a, b, sd) {
  dnorm(b, log = TRUE) + dnorm((a - b^2) / sd, log = TRUE)
}
m <- monty_model_function(banana, fixed = list(sd = 0.25))
m
#> 
#> ── <monty_model> ───────────────────────────────────────────────────────────────
#>  Model has 2 parameters: 'a' and 'b'
#>  See `?monty_model()` for more information

# Density from our new model. Note that this computes density
# using an unstructured parameter vector, which is mapped to 'a'
# and 'b':
monty_model_density(m, c(0, 0))
#> [1] -1.837877

# Same as the built-in banana example:
monty_model_density(monty_example("banana"), c(0, 0))
#> [1] -1.837877