mixle.ops module¶
Operations — the verbs that transform distributions, and how they change capabilities.
The missing third axis of mixle: alongside objects (distributions) and capabilities (what they support), the operations that move an object from one capability set to another. Each verb documents its capability signature — what it requires of its input and what the output gains — so “what can I do to a distribution, and what does it become?” has one home.
quantize(dist, bits) : any Distribution -> FiniteSupport · Enumerable · RankableByIndex truncate(dist, …) : Distribution -> Distribution (Enumerable preserved) condition(dist, observed) : Conditionable -> Distribution marginalize(dist, keep) : Marginalizable -> Distribution mixture(dists, w) : Distributions -> LatentStructured transform(dist, f) : Distribution + inv. f -> Distribution (Jacobian-corrected) tilt(dist, theta) : ExponentialFamily -> Distribution project(source, target) : Sampleable x Fittable -> Distribution (forward-KL M-projection) product_of_experts(dists) : tractable family -> Distribution (geometric/log-linear pool)
See docs/ARCHITECTURE.md and the operation table in docs/CAPABILITIES.md.
- quantize(dist, bits=8, *, lo_q=1e-3, hi_q=1.0 - 1e-3, n_samples=200_000, seed=0)[source]
Discretize a continuous
distinto a finite distribution over2**bitsbins.Capability signature:
any Distribution -> FiniteSupport · Enumerable · RankableByIndex. The result is aCategoricalDistributionover bin midpoints; the bin masses are exact (via the basecdfwhen available) or sampled otherwise. This is the concrete answer to “if I quantize a distribution, what does it become and how” —describe(quantize(g))shows the gained capabilities.
- truncate(dist, *, allowed=None, forbidden=None)[source]
Restrict
distto (allowed) or away from (forbidden) a finite set; mass renormalizes.
- condition(dist, observed)[source]
Condition
diston a subset of coordinates. Requires theConditionablecapability.
- marginalize(dist, keep)[source]
Marginalize
distto the kept coordinates. Requires theMarginalizablecapability.
- mixture(dists, w=None)[source]
Weighted mixture of
dists— a latent-variable model (LatentStructured).
- transform(dist, f)[source]
Change of variables
y = f(x)with a Jacobian-corrected density (fis aTransform).
- tilt(dist, theta)[source]
Exponentially tilt
distbytheta. Requires theExponentialFamilycapability.
- project(source, target, *, n_samples=20_000, seed=0, max_its=50)[source]
Variationally project
sourceonto the family oftarget(the sample-based M-projection).Capability signature:
Sampleable source x Fittable target -> Distribution. Drawsn_samplesfromsourceand fitstargetto them by maximum likelihood – which is exactly the projection minimizing the forward divergenceKL(source || target_family)(the M-/moment projection). Works for anysourceexposing asampler(a distribution, mixture, GP, or a trained neural model) onto any fittabletargetfamily, so e.g. a neural sequence model can be distilled onto an HMM.targetmay be a distribution (itsestimator()supplies the family) or an estimator directly. The returned model is a member of the target family;describe(project(...))shows its capabilities.
- product_of_experts(dists, weights=None)[source]
Geometric (log-linear) pooling of densities — a Product of Experts.
Capability signature:
tractable family -> Distribution. Multiplies the expert densities,p(x) ∝ ∏_k p_k(x)**w_ki.e.log p(x) = Σ_k w_k·log p_k(x) − log Z, with raw exponent weightsw_k(default1.0each — not normalized to sum to one, since PoE weights are exponents). Only the cases with a tractable normalizer are constructed exactly; the general continuous case raisesCapabilityError.Categorical / finite shared support (the LLM-vocab fusion case): exact over the intersection of supports —
new_pmap[x] ∝ ∏_k p_k(x)**w_kforxwith positive mass under every expert, then renormalized. Returns aCategoricalDistribution.Gaussian (closed form): a Gaussian with precision-weighted combination
1/σ² = Σ_k w_k/σ_k²andμ = σ²·Σ_k w_k·μ_k/σ_k². Returns aGaussianDistribution.
- Parameters:
- Raises:
CapabilityError – if the experts have no shared finite support and are not all Gaussian — the PoE normalizer
Z = ∫ ∏_k p_k(x)**w_k dxis then intractable in closed form.