pymc.sample_posterior_predictive#
- 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.
- Parameters
- trace
backend,list,xarray.Dataset,arviz.InferenceData, orMultiTrace 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)
- model
Model(optionalifinwithcontext) 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.- var_names
Iterable[str] Names of variables for which to compute the posterior predictive samples.
- sample_dims
listofstr, 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_seed
int,RandomStateorGenerator, optional Seed for the random number generator.
- progressbarbool
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 totraceor not. If True,traceis modified inplace but still returned.- predictionsbool, default
False Flag used to set the location of posterior predictive samples within the returned
arviz.InferenceDataobject. If False, assumes samples are generated based on the fitting data to be used for posterior predictive checks, and samples are stored in theposterior_predictive. If True, assumes samples are generated based on out-of-sample data as predictions, and samples are stored in thepredictionsgroup.- idata_kwargs
dict, optional Keyword arguments for
pymc.to_inference_data()ifpredictions=Falseor topymc.predictions_to_inference_data()otherwise.- compile_kwargs: dict, optional
Keyword arguments for
pymc.pytensorf.compile_pymc().
- trace
- Returns
arviz.InferenceDataorDictAn ArviZ
InferenceDataobject containing the posterior predictive samples (default), or a dictionary with variable names as keys, and samples as numpy arrays.
Examples
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: idata.extend(pymc.sample_posterior_predictive(thinned_idata))
Generate 5 posterior predictive samples per posterior sample.
expanded_data = idata.posterior.expand_dims(pred_id=5) with model: idata.extend(pymc.sample_posterior_predictive(expanded_data))