Skip to contents

Sample from a model. Uses a Monte Carlo method (or possibly something else in future) to generate samples from your distribution. This is going to change a lot in future, as we add support for distributing over workers, and for things like parallel reproducible streams of random numbers. For now it just runs a single chain as a proof of concept.

Usage

monty_sample(
  model,
  sampler,
  n_steps,
  initial = NULL,
  n_chains = 1L,
  runner = NULL,
  observer = NULL,
  restartable = FALSE
)

Arguments

model

The model to sample from; this should be a monty_model for now, but we might change this in future to test to see if things match an interface rather than a particular class attribute.

sampler

A sampler to use. These will be described later, but we hope to make these reasonably easy to implement so that we can try out different sampling ideas. For now, the only sampler implemented is monty_sampler_random_walk().

n_steps

The number of steps to run the sampler for.

initial

Optionally, initial parameter values for the sampling. If not given, we sample from the model (or its prior).

n_chains

Number of chains to run. The default is to run a single chain, but you will likely want to run more.

runner

A runner for your chains. The default option is to run chains in series (via monty_runner_serial). The only other current option is monty_runner_parallel which uses the parallel package to run chains in parallel. If you only run one chain then this argument is best left alone.

observer

An observer, created via monty_observer, which you can use to extract additional information from your model at points included in the chain (for example, trajectories from a dynamical model).

restartable

Logical, indicating if the chains should be restartable. This will add additional data to the chains object.

Value

A list of parameters and densities; we'll write tools for dealing with this later. Elements include:

  • pars: An array with three dimensions representing (in turn) parameter, sample and chain, so that pars[i, j, k] is the ith parameter from the jth sample from the kth chain. The rows will be named with the names of the parameters, from your model.

  • density: A matrix of model log densities, with n_steps rows and n_chains columns.

  • initial: A record of the initial conditions, a matrix with as many rows as you have parameters and n_chains columns (this is the same format as the matrix form of the initial input parameter)

  • details: Additional details reported by the sampler; this will be a list of length n_chains (or NULL) and the details depend on the sampler. This one is subject to change.

  • observations: Additional details reported by the model. This one is also subject to change.