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 thatpars[i, j, k]
is thei
th parameter from thej
th sample from thek
th chain. The rows will be named with the names of the parameters, from your model.density
: A matrix of model log densities, withn_steps
rows andn_chains
columns.initial
: A record of the initial conditions, a matrix with as many rows as you have parameters andn_chains
columns (this is the same format as the matrix form of theinitial
input parameter)details
: Additional details reported by the sampler; this will be a list of lengthn_chains
(orNULL
) 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.