Skip to contents

monty includes a simple probabilistic domain-specific language (DSL) that is inspired by stan and Statistical Rethinking. It is designed to make some tasks a bit easier, particularly when defining priors for your model. We expect that this DSL is not sufficiently advanced to represent most interesting models but it may get more clever and flexible in the future. In particular we do not expect the DSL to be useful in writing likelihood functions for comparison to data; we expect that if your model is simple enough for this you would be better off using stan or some similarly flexible system.

Some simple examples

In chapter 4 of Statistical Rethinking, we build a regression model of height with parameters α\alpha, β\beta and σ\sigma. We can define the model for the prior probability of this model in monty by running

prior <- monty_dsl({
  alpha ~ Normal(178, 20)
  beta ~ Normal(0, 10)
  sigma ~ Uniform(0, 50)
})

This will define a new monty_model() object that represents the prior, but with all the bits that we might need depending on how we want to use it:

We have model parameters

prior$parameters
#> [1] "alpha" "beta"  "sigma"

These are defined in the order that they appear in your definition (so alpha is first and sigma is last)

We can compute the domain for your model:

prior$domain
#>       [,1] [,2]
#> alpha -Inf  Inf
#> beta  -Inf  Inf
#> sigma    0   50

We can draw samples from the model if we provide a monty_rng object

rng <- monty_rng$new()
theta <- prior$direct_sample(rng)
theta
#> [1] 167.700794  -5.085348   8.899977

We can compute the (log) density at a point in parameter space

prior$density(theta)
#> [1] -11.31011

The computed properties for the model are:

prior$properties
#> $has_gradient
#> [1] TRUE
#> 
#> $has_direct_sample
#> [1] TRUE
#> 
#> $is_stochastic
#> [1] FALSE
#> 
#> $has_parameter_groups
#> [1] FALSE
#> 
#> $allow_multiple_parameters
#> [1] FALSE
#> 
#> attr(,"class")
#> [1] "monty_model_properties"