particles.mcmc¶
MCMC (Markov chain Monte Carlo) and related algorithms.
Overview¶
This module contains various classes that implement MCMC samplers:
MCMC
: the base class for all MCMC samplers;
GenericRWHM
: base class for randomwalk HastingsMetropolis;
GenericGibbs
: base class for Gibbs samplers;
PMMH
,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 randomwalk 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 chain
, which
is a 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 logposterior density at these 200 parameters.
See the dedicated notebook tutorial (on Bayesian inference for statespace models) for more examples and explanations.
Random walk Metropolis¶
Both GenericRWHM
and 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 parameterrw_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, andrw_cov
is used as a preliminary estimate for that target covariance. See parameterscale
(in the documentation ofGenericRWHM
) for more details on the factor in front of the running estimate, andVanishCovTracker
for how the running estimate is computed recursively.
By default, the adaptive version is used.
Beyond the bootstrap filter within PMMH
¶
PMMH
runs, at each iteration, a particle filter to approximate the likelihood
of the considered statespace 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
fk_cls
to aFeynmanKac
subclass such as e.g.ssms.GuidedPF
if you wish to use a guided filter (rather than a bootstrap filter). See modulestate_space_models
for more details on theseFeynmanKac
subclasses derived from a given statespace model.You can use parameter
smc_options
(dictlike) to pass various parameters to classSMC
when the algorithm is instantiated.You can even use parameter
smc_cls
to specify a different class for the
algorithm itself (a
SMC
subclass instead ofSMC
itself).
Finally, if you need something even more general, you can also subclass
PMMH
and redefine methodalg_instance
, which takes as argumenttheta
(a dictlike object) and returns an algorithm (an instance of classSMC
or one its subclasses).
Functions

Mean squared jumping distance. 
Classes

Basic random walk HastingsMetropolis sampler. 

Conditional SMC. 

Generic Gibbs sampler for a statespace model. 

Base class for random walk HastingMetropolis samplers. 

MCMC base class. 

Particle Marginal Metropolis Hastings. 

Particle Gibbs sampler (abstract class). 

Tracks the vanishing mean and covariance of a sequence of points. 