mixle.ppl.predictive module

Predictive checks for the mixle PPL – the model-criticism half of the Bayesian workflow.

A fit is not finished when the parameters are estimated; you check whether the model can reproduce the data. These helpers implement the two standard checks:

  • posterior_predictive_check() – simulate replicate datasets from the fitted model, compare a test statistic on the replicates against its observed value, and report the Bayesian p-value P(T(y_rep) >= T(y_obs)). A p-value near 0 or 1 means the model fails to capture that feature of the data (e.g. its skew or its tails); near 0.5 is a good fit.

  • prior_predictive() – simulate datasets from the prior (before seeing data) by drawing every prior parameter and sampling, so you can sanity-check that the prior implies plausible data.

Both accept a dict of named test statistics (callables on a dataset); the defaults cover location, spread, and the extremes.

posterior_predictive_check(fitted, data, *, statistics=None, n_rep=1000, seed=0)[source]

Posterior predictive check of a fitted PPL model against data.

Draws n_rep replicate datasets (each the size of data) from fitted.predict – which integrates over parameter uncertainty for a Bayesian fit (conjugate/mcmc/hmc) and is the plug-in predictive for a point fit (em/map) – evaluates each named statistic on every replicate and on the observed data, and returns the Bayesian p-value per statistic.

Returns {'observed', 'replicated', 'p_value', 'n_rep'}: observed[name] the statistic on the data, replicated[name] its (n_rep,) replicate values, p_value[name] = P(T_rep >= T_obs).

Parameters:
Return type:

dict[str, Any]

prior_predictive(model, size, *, n_rep=1000, statistics=None, seed=0)[source]

Prior predictive simulation: n_rep datasets of size drawn from model’s prior.

For each replicate it draws every prior parameter (and hyperparameter) and then size data points, so the result reflects what the model believes before seeing data – the check that a prior is neither absurdly tight nor absurdly diffuse. Returns {'replicated': {stat: (n_rep,)}, 'samples': (n_rep, size), 'n_rep'} with the per-replicate statistics and the raw simulated datasets.

Parameters:
Return type:

dict[str, Any]

prior_predictive_check(model, data, *, statistics=None, n_rep=1000, seed=0)[source]

Prior predictive check: where the observed statistics sit in the prior predictive distribution.

Like posterior_predictive_check() but the replicates come from the prior (via prior_predictive()), so a p-value near 0 or 1 flags a prior that is inconsistent with the data before any fitting – often a sign the prior is mis-scaled.

Parameters:
Return type:

dict[str, Any]