mixle.inference.posterior module¶
The Posterior algebra: parameter / predictive posteriors + the posterior() factory.
Inference produces posteriors; you draw from them through one interface – the
Posterior base in the compute layer. The latent q(z | x)
realizations live there with the base (they need no inference machinery); the realizations here are
the ones that do need it:
ParameterPosterior–q(theta | data), closed-form when the family is conjugate and MCMC otherwise, behind onesample/samples/mean/intervalinterface;PredictivePosterior– draws of new data from a fitted model (plug-in), or with parameter uncertainty integrated in viaPredictivePosterior.from_parameter_posterior().
posterior() is the front door that picks the right realization from over= and the family.
- class ParameterPosterior(draw_one, draw_many, *, mean_fn=None, chain=None, kind='')[source]
Bases:
Posteriorq(theta | data)over a model family’s parameters – exact (conjugate) or MCMC.Built by
posterior(model, data, over="params"); the exact-vs-MCMC distinction is hidden behind the sharedPosteriorinterface. A singlesample()returns one parameter set, andsamples()returnsnof them;mean()andinterval()summarize the posterior.- Parameters:
- classmethod from_conjugate(cp)[source]
Wrap a closed-form
ConjugatePosterior(each draw is a parameter dict).- Parameters:
cp (ConjugatePosterior)
- Return type:
ParameterPosterior
- classmethod from_mcmc(result)[source]
Wrap an MCMC
MCMCResult; draws resample the retained chain, summaries use it directly.- Parameters:
result (Any)
- Return type:
ParameterPosterior
- sample(rng=None)[source]
One parameter draw from the posterior.
- samples(n, rng=None)[source]
nparameter draws (a dict of length-narrays for conjugate; a list for MCMC).
- class PredictivePosterior(draw_one, draw_many)[source]
Bases:
PosteriorThe posterior-predictive: draws of new data from a fitted model.
plug_in()wraps a single fitted model’s sampler.from_parameter_posterior()integrates parameter uncertainty – each predictive draw first samplestheta ~ q(theta | data), rebuilds the model, then draws data – so the spread reflects both sampling and parameter uncertainty.- Parameters:
draw_one (Callable[[Any], Any])
draw_many (Callable[[int, Any], Any])
- classmethod plug_in(model)[source]
Plug-in predictive: draw new data from
modelat its fitted parameters.- Parameters:
model (Any)
- Return type:
PredictivePosterior
- classmethod from_parameter_posterior(param_post, build)[source]
Posterior-predictive integrating parameter uncertainty.
buildmaps one parameter draw (the objectParameterPosterior.sample()returns) to a distribution; each predictive draw rebuilds the model from a freshthetaand samples it.
- posterior(model, data=None, *, over='predictive', prior=None, method='auto', **kwargs)[source]
Build the
Posteriorofmodelover the requested variables.- Parameters:
model (Any) – a fitted mixle distribution (or a latent-variable model for
over='latent').data (Any) – observations – required for
over='params'and forover='latent'(thexthe latent posterior conditions on); ignored for plug-inover='predictive'.over (str) –
'latent'->q(z | x)(needs thelatent_posteriorcapability);'params'->q(theta | data);'predictive'-> draws of new data.prior (Any) – prior over parameters for
over='params'(seeconjugate_posterior/sample_parameter_posterior).method (str) – for
over='params'–'auto'(conjugate when the family supports it, else MCMC),'conjugate', or'mcmc'.**kwargs (Any) – forwarded to
sample_parameter_posteriorfor the MCMC path (sampler,steps…).
- Returns:
A
Posterior– aLatentPosterior,ParameterPosterior, orPredictivePosterior.- Return type:
Posterior