pymc.sample_posterior_predictive(trace, samples=None, model=None, var_names=None, keep_size=None, random_seed=None, progressbar=True, return_inferencedata=True, extend_inferencedata=False, predictions=False, idata_kwargs=None, compile_kwargs=None)[source]#

Generate posterior predictive samples from a model given a trace.

tracebackend, list, xarray.Dataset, arviz.InferenceData, or MultiTrace

Trace generated from MCMC sampling, or a list of dicts (eg. points or from find_MAP()), or xarray.Dataset (eg. InferenceData.posterior or InferenceData.prior)


Number of posterior predictive samples to generate. Defaults to one posterior predictive sample per posterior sample, that is, the number of draws times the number of chains.

It is not recommended to modify this value; when modified, some chains may not be represented in the posterior predictive sample. Instead, in cases when generating posterior predictive samples is too expensive to do it once per posterior sample, the recommended approach is to thin the trace argument before passing it to sample_posterior_predictive. In such cases it might be advisable to set extend_inferencedata to False and extend the inferencedata manually afterwards.

modelModel (optional if in with context)

Model to be used to generate the posterior predictive samples. It will generally be the model used to generate the trace, but it doesn’t need to be.


Names of variables for which to compute the posterior predictive samples.

keep_sizebool, default True

Force posterior predictive sample to have the same shape as posterior and sample stats data: (nchains, ndraws, ...). Overrides samples parameter.

random_seedint, RandomState or Generator, optional

Seed for the random number generator.


Whether or not to display a progress bar in the command line. The bar shows the percentage of completion, the sampling speed in samples per second (SPS), and the estimated remaining time until completion (“expected time of arrival”; ETA).

return_inferencedatabool, default True

Whether to return an arviz.InferenceData (True) object or a dictionary (False).

extend_inferencedatabool, default False

Whether to automatically use arviz.InferenceData.extend() to add the posterior predictive samples to trace or not. If True, trace is modified inplace but still returned.

predictionsbool, default False

Choose the function used to convert the samples to inferencedata. See idata_kwargs for more details.

idata_kwargsdict, optional

Keyword arguments for pymc.to_inference_data() if predictions=False or to pymc.predictions_to_inference_data() otherwise.

compile_kwargs: dict, optional

Keyword arguments for pymc.aesaraf.compile_pymc().

arviz.InferenceData or Dict

An ArviZ InferenceData object containing the posterior predictive samples (default), or a dictionary with variable names as keys, and samples as numpy arrays.


Thin a sampled inferencedata by keeping 1 out of every 5 draws before passing it to sample_posterior_predictive

thinned_idata = idata.sel(draw=slice(None, None, 5))
with model: