pymc.sample_posterior_predictive(trace, model=None, var_names=None, sample_dims=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)

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.

sample_dimslist of str, optional

Dimensions over which to loop and generate posterior predictive samples. When sample_dims is None (default) both “chain” and “draw” are considered sample dimensions. Only taken into account when trace is InferenceData or Dataset.

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

Flag used to set the location of posterior predictive samples within the returned arviz.InferenceData object. If False, assumes samples are generated based on the fitting data to be used for posterior predictive checks, and samples are stored in the posterior_predictive. If True, assumes samples are generated based on out-of-sample data as predictions, and samples are stored in the predictions group.

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.pytensorf.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:

Generate 5 posterior predictive samples per posterior sample.

expanded_data = idata.posterior.expand_dims(pred_id=5)
with model: