class pymc.smc.kernels.SMC_KERNEL(draws=2000, start=None, model=None, random_seed=None, threshold=0.5)[source]#

Base class for the Sequential Monte Carlo kernels.

To create a new SMC kernel you should subclass from this.

Before sampling, the following methods are called once in order:


Choose initial population of SMC particles. Should return a dictionary with { : numpy array of size (draws, var.size)}. Defaults to sampling from the prior distribution. This method is only called if start is not specified.


Creates initial population of particles in the variable self.tempered_posterior and populates the self.var_info dictionary with information about model variables shape and size as { : (var.shape, var.size)}.

The functions self.prior_logp_func and self.likelihood_logp_func are created in this step. These expect a 1D numpy array with the summed sizes of each raveled model variable (in the order specified in pymc.Model.initial_point()).

Finally, this method computes the log prior and log likelihood for the initial particles, and saves them in self.prior_logp and self.likelihood_logp.

This method should not be modified.


May include any logic that should be performed before sampling starts.

During each sampling stage the following methods are called in order:


The inverse temperature self.beta is updated based on the self.likelihood_logp and threshold parameter.

The importance self.weights of each particle are computed from the old and newly selected inverse temperature.

The iteration number stored in self.iteration is updated by this method.

Finally the model log_marginal_likelihood of the tempered posterior is updated from these weights.


The particles in self.posterior are sampled with replacement based on self.weights, and the used resampling indexes are saved in self.resampling_indexes.

The arrays self.prior_logp and self.likelihood_logp are rearranged according to the order of the resampled particles. self.tempered_posterior_logp is computed from these and the current self.beta.


May include logic that should be performed before every mutation step.


Mutate particles in self.tempered_posterior.

This method is further responsible to update the self.prior_logp, self.likelihod_logp and self.tempered_posterior_logp, corresponding to each mutated particle.


Returns important sampling_stats at the end of each stage in a dictionary format. This will be saved in the final InferenceData object under sample_stats.

Finally, at the end of sampling the following methods are called:


Convert final population of particles to a posterior trace object. This method should not be modified.


Returns important sample_settings at the end of sampling in a dictionary format. This will be saved in the final InferenceData object under sample_stats.


SMC_KERNEL.__init__([draws, start, model, ...])

Initialize the SMC_kernel class.


Create an initial population from the prior distribution


Apply kernel-specific perturbation to the particles once per stage


Resample particles based on importance weights


SMC_kernel settings to be saved once at the end of sampling.


Stats to be saved at the end of each stage


Setup logic performed once before sampling starts


Tuning logic performed before every mutation step


Calculate the next inverse temperature (beta)