mixle.stats.compute.posterior module¶
The Posterior hierarchy: a uniform, samplable object for any model-derived distribution.
Posterior is the shared base contract – inference produces posteriors; you draw from them
through one interface. Every realization answers the same questions where they are defined:
sample(rng) -> a single draw
samples(n, rng) -> n draws (loop by default; vectorized override where cheaper)
mean() / mode() -> the posterior mean / MAP configuration
marginals() -> per-component marginals (e.g. the EM E-step responsibilities)
entropy() -> H[q] (the ELBO entropy term)
interval(level) -> a credible interval
This base lives in the compute layer next to the sampler contracts (DistributionSampler /
ConditionalSampler in mixle.stats.compute.pdist) so both mixle.stats and
mixle.inference can build on it without a layering inversion. The richer realizations that need
inference machinery – parameter posteriors (conjugate / MCMC) and the posterior-predictive, plus the
posterior(model, ...) factory – live in mixle.inference.posterior.
LatentPosterior is the latent q(z | x) subtype implemented here: each latent model handles its
hidden variables implicitly inside EM (the E-step returns raw responsibility arrays); LatentPosterior
makes q(z | x) a single object – exact for mixtures/HMMs, mean-field for LDA/VMP – so the EM
E-step (marginals), latent sampling (sample), and the ELBO entropy term are methods on it.
Mean-field realizations additionally provide expected_complete_ll(dist) / update(dist) /
elbo(dist); for exact posteriors those are not needed.
- class Posterior[source]
Bases:
ABCA model-derived distribution exposing one uniform interface for draws and summaries.
Only
sample()(a single draw) is required.samples()loops it by default; vectorized subtypes override it.mean/mode/marginals/entropy/intervalraiseNotImplementedErrorunless a subtype defines them, so each realization implements exactly the summaries that are meaningful for it.- abstractmethod sample(rng=None)[source]
Draw a single sample from the posterior (
rngis a seed,RandomState, orNone).
- samples(n, rng=None)[source]
Draw
nsamples; loopssample()by default (override for a vectorized draw).
- mode()[source]
The maximum-a-posteriori configuration (not defined for every realization).
- Return type:
- marginals()[source]
Per-component marginals – e.g. the EM E-step responsibilities (not always defined).
- Return type:
- class LatentPosterior[source]
Bases:
PosteriorThe posterior
q(z | x)over a model’s latent variables (exact or mean-field).- abstractmethod marginals()[source]
Per-latent marginal responsibilities – the quantity the EM M-step consumes.
- Return type:
- abstractmethod sample(rng=None)[source]
Draw the latent variables
z ~ q(z | x)(rngis a seed, RandomState, or None).
- class CategoricalLatentPosterior(responsibilities, support=None)[source]
Bases:
LatentPosteriorIndependent categorical latents
q(z) = prod_i Cat(z_i; r_i).The exact posterior for a finite mixture’s component labels (and the per-token topic factor of an LDA document).
responsibilitiesis the row-stochastic(N, K)matrixr_ik = q(z_i = k | x_i);supportmaps columnkto its latent label (default0..K-1).- sample(rng=None)[source]
Draw one latent label per observation; returns an
(N,)array of support labels.
- class MarkovChainLatentPosterior(log_pi, log_A, log_b)[source]
Bases:
LatentPosteriorChain-structured latents
q(z_1..z_T | x)for an HMM – exact, via forward-backward.Built from the log initial distribution
log_pi(K,), the log transition matrixlog_A(K, K)(rowj-> columnk), and the per-position emission log-likelihoodslog_b(T, K). The latents are coupled (a Markov chain), so:marginals() -> the
(T, K)forward-backward smoothing probabilitiesq(z_t = k | x)sample(rng) -> a full state path(T,)by forward-filter / backward-sample (FFBS) mode() -> the Viterbi (max-product) path(T,)entropy() -> the exact scalar chain entropyH[q(z_1..z_T | x)]- log_likelihood()[source]
The sequence log-likelihood
log p(x)(the forward normalizer).- Return type:
- sample(rng=None)[source]
Draw a state path
z ~ q(z | x)via FFBS; returns(T,)state indices.
- class MeanFieldLDAPosterior(gamma, phi, counts)[source]
Bases:
LatentPosteriorMean-field variational posterior for one LDA document:
q(theta, z) = Dir(theta; gamma) prod_n Cat(z_n; phi_n).The Blei-Ng-Jordan variational factorization made into an object instead of loose
gamma/phiarrays.gamma(K,)is the document’s variational Dirichlet parameter (q(theta));phi(W, K)the per-distinct-word topic responsibilities (q(z_n), rows sum to 1);counts(W,)the word counts. Note the latents are heterogeneous (continuoustheta+ discretez), sosamplereturns the pair(theta, z)andentropyis a scalar.- topic_proportions()[source]
The mean document-topic distribution
E_q[theta] = gamma / sum(gamma)(K,).- Return type:
- marginals()[source]
The
(W, K)per-distinct-word topic responsibilitiesq(z_n).- Return type:
- sample(rng=None)[source]
Draw the full latent
(theta, z):theta ~ Dir(gamma)and per-token topicszfromphi.