Migration from mcstate

monty is the spiritual replacement for mcstate, which was used for inference of odin.dust models from 2020 to 2024.

Unlike with the new versions of odin (odin2) and dust (dust2), we have not changed mcstate in place as we no longer felt that the name was appropriate; mcstate was explicitly about working with state space models, while monty is about Monte Carlo methods in general. And Wes has a goat called Monty, so here we are.

Where is everything?

Some features have moved out of this package and into dust2:

The particle filter

The particle filter has moved into dust2, see dust2::dust_filter_create

What was previously a method on the old particle_filter object has now changed to be a free function:

Action mcstate dust2
Allocate $new() dust_filter_create() (1)
Run $run() dust_likelihood_run()
Stateful run $run_begin() Not supported
Get final state $state() dust_likelihood_last_state()
Get trajectories $history() dust_likelihood_last_trajectories()
Get ODE statistics $ode_statistics() Not yet supported
Get intermediate state $restart_state() Not yet supported
Get inputs $inputs Not yet supported
Change number of threads set_n_threads() Not supported
  1. Or dust_unfilter_create for deterministic models

In addition, differentiable deterministic models have dust_likelihood_last_gradient() to get the gradient of the likelihood at the last point.

Other state space methods

In mcstate, we implemented two other sequential Monte Carlo methods: SMC^2 and IF2. Neither of these have been reimplemented yet, but these will appear in dust if and when we do so. Please let us know if you used these!

Running MCMC

How these are specified and configured has totally changed, to the point where it is not really meaningful to do a table-based comparison of methods.

A typical use with mcstate looked like this (adapted with simplification from the old SIR models vignette)

filter <- mcstate::particle_filter$new(data = ..., model = ..., compare = ...,
                                       n_particles = ...)
beta <- mcstate::pmcmc_parameter("beta", 0.2, min = 0)
gamma <- mcstate::pmcmc_parameter("gamma", 0.1, min = 0, prior = function(p)
  dgamma(p, shape = 1, scale = 0.2, log = TRUE))

proposal_matrix <- diag(0.1, 2)
mcmc_pars <- mcstate::pmcmc_parameters$new(list(beta = beta, gamma = gamma),
                                           proposal_matrix)
control <- mcstate::pmcmc_control(
    n_steps,
    save_state = TRUE,
    save_trajectories = TRUE,
    progress = TRUE)
samples <- mcstate::pmcmc(mcmc_pars, filter, control = control)

With monty this would look more like:

filter <- dust_filter_create(generator, time_start, data, n_particles)
packer <- monty_packer(c("beta", "gamma"))
model <- dust_likelihood_monty(filter, packer,
                               save_state = TRUE,
                               save_trajectories = TRUE)
prior <- monty_dsl({
  beta ~ Uniform(0, 100)
  gamma ~ Gamma(shape = 1, scale = 0.2)
})
sampler <- monty_sampler_random_walk(diag(0.2, 2))
samples <- monty_sample(model + prior, sampler, n_steps)

which contains roughly the same bits of information but in quite a different presentation:

The way that parameters are specified: in mcstate we had pmcmc_parameter and pmcmc_parameters, one of which was a function and the other was a combination that included the sampler’s proposal kernel! This is all a bit silly. Now: * we use our packer interface to smooth over the gap between how it is convenient to represent parameters in your odin model and how you need to represent them to think about moving around in parameter space * we use the DSL to specify priors, removing the need to write out distributions, to sample from these distributions automatically and to prevent forgetting the log = TRUE on densities.

The pmcmc_control object has gone: this contained information that really affected the filter (e.g., save_state which has moved into the dust_likelihood_monty function) and the running of the chain itself.

The control over samplers has moved into a new sampler object: we needed this additional level of control to allow different samplers to be used in different situations (e.g., to allow HMC in the case of a deterministic differentiable density).