mixle.inference.project module¶
Closed-form variational projections — compress a structured teacher onto a smaller student exactly.
mixle.ops.project already does the general forward-KL (M-)projection by SAMPLING the source and
fitting the target by maximum likelihood. That is universal but approximate and slow. This module adds
the cases where the projection has a closed form and needs no samples and no iteration:
collapse_mixture()— moment-match a mixture onto a single component. For Gaussians this is the law of total variance, exact to machine precision (the M-projection of the mixture onto one Gaussian).reduce_mixture()— Runnalls’ KL-greedy mixture reduction: repeatedly merge the pair of components whose merge costs the least KL, until the target count is reached. Each merge is a closed-form moment match, and the merge cost is Runnalls’ analytic dissimilarity — so the whole reduction is closed form. Every merge is moment-preserving, so the reduced mixture keeps the original’s overall mean and covariance exactly (a strong, checkable invariant).gaussian_kl()— the analytic KL between two Gaussians (the metric the above use).
Why Gaussians first: the mixtures that actually need compressing at frontier scale are Gaussian-ish –
mixture-of-experts routed in a latent, Kalman/SSM belief states, VLM latent fusion, GP posteriors. The
same moment-matching idea extends to any exponential family (via
mixle.stats.compute.exp_family.ExponentialFamilyForm.mean_parameters); those are added as their
closed-form moment algebra is filled in. Non-Gaussian sources fall back to the sampling projection with
a clear pointer, rather than silently pretending to be exact.
References: Runnalls, “Kullback-Leibler approach to Gaussian mixture reduction” (IEEE T-AES 2007); the moment-matching M-projection is the standard forward-KL result (Minka; Bishop PRML §10.7).
- gaussian_kl(p, q)[source]
KL(p || q)between two Gaussians (uni- or multivariate), in nats. Accepts distributions.0.5[ tr(Σq⁻¹ Σp) + (μq-μp)ᵀ Σq⁻¹ (μq-μp) - d + ln(det Σq / det Σp) ]– the analytic Gaussian KL.
- collapse_mixture(mixture)[source]
Moment-match a mixture onto a single distribution, in closed form (exact for Gaussians).
Returns the Gaussian minimizing
KL(mixture || Gaussian)– its mean and covariance are the mixture’s overall mean and covariance (law of total variance). This is the exact, sample-free counterpart tomixle.ops.project(mixture, GaussianDistribution(...).estimator()).
- reduce_mixture(mixture, n_components, *, method='runnalls')[source]
Reduce a Gaussian mixture to
n_componentsby greedily merging the least-costly pair (Runnalls).Repeatedly merges the two components whose merge costs the least KL (
_runnalls_cost()) untiln_componentsremain. Every merge is moment-preserving, so the reduced mixture has the same overall mean and covariance as the original – only higher moments are lost. Returns a mixture of the same kind as the input (aGaussianMixtureDistributionfor multivariate input).
- moment_project(teacher, target=None, *, exact=True, **sampling_kw)[source]
Project
teacheronto a smaller student – exactly when possible, else by sampling.If
teacheris a Gaussian mixture andtargetisNone(or a single Gaussian family), the projection is the closed-formcollapse_mixture()– no samples, machine-precision. Otherwise (or whenexact=False) it delegates tomixle.ops.project(), the sampling M-projection ontotarget’s family. This gives one entry point that is exact where the structure allows and honest (sampling, clearly) where it does not.
- fisher_merge(estimates, fishers=None)[source]
Fisher-weighted merge of parameter estimates – the closed-form Laplace-posterior combination.
Given estimates
θ_i(each a flat parameter vector) and their Fisher informationF_i, returnsθ* = (Σ F_i)⁻¹ (Σ F_i θ_i)– the point that maximizes the sum of the local Laplace log-posteriorsΣ_i -½(θ-θ_i)ᵀ F_i (θ-θ_i). This is Matena & Raffel Fisher merging (diagonalF) and, in general, the precision-weighted mean; for Gaussians it coincides with the product-of-experts mean. No gradient steps – a single linear solve.- Parameters:
estimates (Any) – sequence of parameter vectors
θ_i(each shape(p,)), or a stack(m, p).fishers (Any) – per-estimate Fisher information. Each may be a scalar/1-D vector (diagonal Fisher, per-coordinate precision) or a
(p, p)matrix (full Fisher).Noneuses unit Fisher (a plain average). A single value is broadcast to every estimate.
- Returns:
The merged parameter vector
θ*of shape(p,).- Return type: