MCMC (Markov chain Monte Carlo) and related algorithms.
This module contains various classes that implement MCMC samplers:
MCMC: the base class for all MCMC samplers;
GenericRWHM: base class for random-walk Hastings-Metropolis;
GenericGibbs: base class for Gibbs samplers;
ParticleGibbs: base classes for the PMCMC (particle MCMC algorithms) with the same name.
For instance, here is how to run 200 iterations of an adaptive random-walk sampler:
# ... # define some_static_model, some_prior # ... my_mcmc = BasicRWHM(model=some_static_model, prior=some_prior, niter=200, adaptive=True) my_mcmc.run()
Upon completion, object
my_mcmc have an attribute called
ThetaParticles object (see module
smc_samplers). In particular,
my_mcmc.chain has the following attributes:
theta: a structured array that contains the 200 simulated parameters;
lpost: an array that contains the log-posterior density at these 200 parameters.
See the dedicated notebook tutorial (on Bayesian inference for state-space models) for more examples and explanations.
Random walk Metropolis¶
PMMH rely on a random walk proposal; that is, given
the current point theta, the proposed point is sampled from a Gaussian
distribution, with mean theta, and some covariance matrix Sigma (or
Sigma_t at iteration t the adaptive case, see below). Various parameters
may be set to tune this type of proposal:
to run a standard random walk Metropolis algorithm, set
adaptive=False, and specify the covariance matrix Sigma through parameter
rw_cov. Otherwise, Sigma is set by default to the identity matrix (which could a terrible choice in certain problems).
to run an adaptive random walk Metropolis algorithm, where the matrix Sigma_t is progressively adapted to the past states of the chain, set
adaptive=True. In that case, Sigma_t is set to a fraction of the running estimate of the covariance matrix of the target distribution, and
rw_covis used as a preliminary estimate for that target covariance. See parameter
scale(in the documentation of
GenericRWHM) for more details on the factor in front of the running estimate, and
VanishCovTrackerfor how the running estimate is computed recursively.
By default, the adaptive version is used.
Beyond the bootstrap filter within
PMMH runs, at each iteration, a particle filter to approximate the likelihood
of the considered state-space model. By default, a bootstrap filter is used
(with default choices for the resampling scheme, and so on). It is possible to
change the settings of this filtering algorithm, or run a different type of
algorithm, as follows:
You can set parameter
FeynmanKacsubclass such as e.g.
ssms.GuidedPFif you wish to use a guided filter (rather than a bootstrap filter). See module
state_space_modelsfor more details on these
FeynmanKacsubclasses derived from a given state-space model.
You can use parameter
smc_options(dict-like) to pass various parameters to class
SMCwhen the algorithm is instantiated.
You can even use parameter
smc_clsto specify a different class for the
algorithm itself (a
SMCsubclass instead of
Finally, if you need something even more general, you can also subclass
PMMHand redefine method
alg_instance, which takes as argument
theta(a dict-like object) and returns an algorithm (an instance of class
SMCor one its subclasses).
Mean squared jumping distance.
Basic random walk Hastings-Metropolis sampler.
Generic Gibbs sampler for a state-space model.
Base class for random walk Hasting-Metropolis samplers.
MCMC base class.
Particle Marginal Metropolis Hastings.
Particle Gibbs sampler (abstract class).
Tracks the vanishing mean and covariance of a sequence of points.