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:

  • ParameterPosteriorq(theta | data), closed-form when the family is conjugate and MCMC otherwise, behind one sample / samples / mean / interval interface;

  • PredictivePosterior – draws of new data from a fitted model (plug-in), or with parameter uncertainty integrated in via PredictivePosterior.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: Posterior

q(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 shared Posterior interface. A single sample() returns one parameter set, and samples() returns n of them; mean() and interval() summarize the posterior.

Parameters:
  • draw_one (Callable[[Any], Any])

  • draw_many (Callable[[int, Any], Any])

  • mean_fn (Callable[[], Any] | None)

  • chain (np.ndarray | None)

  • kind (str)

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.

Parameters:

rng (Any)

Return type:

Any

samples(n, rng=None)[source]

n parameter draws (a dict of length-n arrays for conjugate; a list for MCMC).

Parameters:
Return type:

Any

mean()[source]

The posterior mean of the parameters.

Return type:

Any

interval(level=0.9)[source]

Central credible interval at level[lo, hi] over the chain (MCMC) or 2000 draws.

Parameters:

level (float)

Return type:

Any

class PredictivePosterior(draw_one, draw_many)[source]

Bases: Posterior

The 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 samples theta ~ 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 model at its fitted parameters.

Parameters:

model (Any)

Return type:

PredictivePosterior

classmethod from_parameter_posterior(param_post, build)[source]

Posterior-predictive integrating parameter uncertainty.

build maps one parameter draw (the object ParameterPosterior.sample() returns) to a distribution; each predictive draw rebuilds the model from a fresh theta and samples it.

Parameters:
Return type:

PredictivePosterior

sample(rng=None)[source]

One predictive draw of new data.

Parameters:

rng (Any)

Return type:

Any

samples(n, rng=None)[source]

n predictive draws of new data.

Parameters:
Return type:

Any

posterior(model, data=None, *, over='predictive', prior=None, method='auto', **kwargs)[source]

Build the Posterior of model over 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 for over='latent' (the x the latent posterior conditions on); ignored for plug-in over='predictive'.

  • over (str) – 'latent' -> q(z | x) (needs the latent_posterior capability); 'params' -> q(theta | data); 'predictive' -> draws of new data.

  • prior (Any) – prior over parameters for over='params' (see conjugate_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_posterior for the MCMC path (sampler, steps…).

Returns:

A Posterior – a LatentPosterior, ParameterPosterior, or PredictivePosterior.

Return type:

Posterior