MMMPlotSuite.posterior_predictive#
- MMMPlotSuite.posterior_predictive(var=None, idata=None, hdi_prob=0.85, backend=None)[source]#
Plot posterior predictive distributions over time.
Visualizes posterior predictive samples as time series, showing the median line and highest density interval (HDI) bands. Useful for checking model fit and understanding prediction uncertainty.
- Parameters:
- var
str, optional Variable name to plot from posterior_predictive group. If None, uses “y”.
- idata
xr.Dataset, optional Dataset containing posterior predictive samples with a “date” coordinate. If None, uses self.idata.posterior_predictive.
This parameter allows: - Testing with mock data without modifying self.idata - Plotting external posterior predictive samples - Comparing different model fits side-by-side
- hdi_prob
float, default 0.85 Probability mass for HDI interval (between 0 and 1).
- backend
str, optional Plotting backend to use. Options: “matplotlib”, “plotly”, “bokeh”. If None, uses global config via mmm_plot_config[“plot.backend”]. Default is “matplotlib”.
- var
- Returns:
PlotCollectionarviz_plots PlotCollection object containing the plot.
Use
.show()to display or.save("filename")to save. Unlike the legacy suite which returned(Figure, Axes), this provides a unified interface across all backends.
- Raises:
ValueErrorIf no posterior_predictive data found in self.idata and no idata provided.
ValueErrorIf hdi_prob is not between 0 and 1.
See also
LegacyMMMPlotSuite.posterior_predictiveLegacy matplotlib-only implementation
Notes
Breaking changes from legacy implementation:
Returns PlotCollection instead of (Figure, Axes)
Different interface for saving and displaying plots
Examples
Basic usage:
mmm.sample_posterior_predictive(X) pc = mmm.plot.posterior_predictive() pc.show()
Plot with different HDI probability:
pc = mmm.plot.posterior_predictive(hdi_prob=0.94) pc.show()
Save to file:
pc = mmm.plot.posterior_predictive() pc.save("posterior_predictive.png")
Use different backend:
pc = mmm.plot.posterior_predictive(backend="plotly") pc.show()
Provide explicit data:
external_pp = xr.Dataset(...) # Custom posterior predictive pc = mmm.plot.posterior_predictive(idata=external_pp) pc.show()
Direct instantiation pattern:
from pymc_marketing.mmm.plot import MMMPlotSuite mps = MMMPlotSuite(custom_idata) pc = mps.posterior_predictive() pc.show()