mixle.stats.latent.gaussian_mixture module¶
Create, estimate, and sample from a mixture of multivariate Gaussian distributions.
Defines the GaussianMixtureDistribution, GaussianMixtureSampler, GaussianMixtureAccumulator, GaussianMixtureEstimatorAccumulatorFactory, GaussianMixtureEstimator, and the GaussianMixtureDataEncoder classes for use with mixle.
Data type: Union[List[float], np.ndarray]. Each observation is a length-d real vector.
A K-component multivariate Gaussian mixture has density
P(x) = sum_{k=1}^{K} w_k * N(x; mu_k, sigma_k),
where w is a vector of K mixture weights summing to 1.0, mu_k is the length-d mean of component k, and sigma_k is the d-by-d positive-definite covariance matrix of component k. For convenience the covariance argument ‘sig2’ may also be given as a (K, d) array of per-component diagonal variances, which is expanded to full diagonal covariance matrices.
Implementation note: GaussianMixtureDistribution and friends are thin specializations of the generic
mixture machinery in mixle.stats.latent.mixture. The vectorized E-step (seq_log_density,
seq_posterior, Accumulator.seq_update, the fused-EM _track_ll path), scalar update/posterior, weight
M-step, and key merge/replace are all inherited unchanged – the only Gaussian-specific pieces are
constructing the multivariate-Gaussian components from (mu, sig2), repacking mu/covar in estimate(),
the (n, d) ndarray sampler output, and the Gaussian data encoder.
- class GaussianMixtureDistribution(mu, sig2, w=MISSING, name=None, weights=MISSING)[source]
Bases:
MixtureDistributionGaussianMixtureDistribution object for a mixture of full-covariance multivariate Gaussians.
A thin specialization of
MixtureDistributionwhose components areMultivariateGaussianDistributionobjects built from(mu, sig2). All density/posterior machinery is inherited from the base mixture; only the (mu, sig2) constructor packing,__str__, and the Gaussian sampler/encoder wiring are Gaussian-specific.- Parameters:
mu (Union[Sequence[Sequence[float]], np.ndarray]) – Component means with shape (K, d).
sig2 (Union[Sequence[Any], np.ndarray]) – Component covariances. Either a (K, d, d) array of full covariance matrices or a (K, d) array of per-component diagonal variances.
w (Union[Sequence[float], np.ndarray]) – Mixture weights of length K, must sum to 1.0.
name (Optional[str]) – Assign string name to GaussianMixtureDistribution object.
- dim
Dimension d of the component Gaussians.
- Type:
- num_components
Number of mixture components K.
- Type:
- mu
Component means with shape (K, d).
- Type:
np.ndarray
- sig2
Component covariance matrices with shape (K, d, d).
- Type:
np.ndarray
- w
Mixture weights of length K.
- Type:
np.ndarray
- zw
Boolean array, True where a mixture weight is exactly 0.0.
- Type:
np.ndarray
- log_w
Log of mixture weights, -np.inf where zw is True.
- Type:
np.ndarray
- components
Component distributions.
- Type:
List[MultivariateGaussianDistribution]
- name
Name of object instance.
- Type:
Optional[str]
- density_semantics()[source]
What
log_densityreturns relative to the true log-density (default: exact).Override to declare that this distribution’s
log_densityis a variational lower bound (ELBO), an upper bound, or an approximation rather than the exactlog p(x). This is surfaced as theExactDensitycapability and noted inmixle.describe(), so code that needs an exact likelihood canrequire(x, ExactDensity)instead of silently trusting a bound.
- sampler(seed=None)[source]
Create GaussianMixtureSampler for sampling from GaussianMixtureDistribution instance.
- Parameters:
seed (Optional[int]) – Seed to set for sampling with RandomState.
- Returns:
GaussianMixtureSampler object.
- Return type:
GaussianMixtureSampler
- estimator(pseudo_count=None)[source]
Create GaussianMixtureEstimator for estimating GaussianMixtureDistribution.
- Parameters:
pseudo_count (Optional[float]) – Used to inflate sufficient statistics in estimation.
- Returns:
GaussianMixtureEstimator object.
- Return type:
GaussianMixtureEstimator
- dist_to_encoder()[source]
Returns a GaussianMixtureDataEncoder object for encoding sequences of iid observations.
- Return type:
GaussianMixtureDataEncoder
- class GaussianMixtureSampler(dist, seed=None)[source]
Bases:
DistributionSamplerGaussianMixtureSampler object used to generate samples from a GaussianMixtureDistribution.
- Parameters:
dist (GaussianMixtureDistribution) – GaussianMixtureDistribution to draw samples from.
seed (Optional[int]) – Seed to set for sampling with RandomState.
- dist
GaussianMixtureDistribution to draw samples from.
- Type:
GaussianMixtureDistribution
- rng
Seeded RandomState for sampling component labels.
- Type:
RandomState
- compSamplers
Samplers for each mixture component.
- Type:
List[MultivariateGaussianSampler]
- sample(size=None, *, batched=True)[source]
Draw iid samples from the Gaussian mixture.
If size is None, a single length-d numpy array is returned. Otherwise a numpy array with shape (size, d) containing ‘size’ iid samples is returned. With
batched=True(default) component draws are grouped and scattered – bit-identical to the per-draw loop (batched=False) but far faster, since each component sampler owns an independent RNG.
- class GaussianMixtureAccumulator(accumulators, keys=(None, None))[source]
Bases:
MixtureAccumulatorGaussianMixtureAccumulator object used to aggregate sufficient statistics of observed data.
A thin specialization of
MixtureAccumulator: the vectorized E-step (seq_update / seq_initialize, the fused-EM_track_llpath), scalar update/initialize, combine/value/from_value, and key merge/replace are all inherited unchanged. Only the Gaussian data encoder is Gaussian-specific.- Parameters:
- acc_to_encoder()[source]
Returns a GaussianMixtureDataEncoder object for encoding sequences of iid observations.
- Return type:
GaussianMixtureDataEncoder
- class GaussianMixtureEstimatorAccumulatorFactory(factories, dim, keys=(None, None))[source]
Bases:
MixtureAccumulatorFactoryGaussianMixtureEstimatorAccumulatorFactory object for creating GaussianMixtureAccumulator objects.
- Parameters:
- factories
Factories for the component accumulators.
- Type:
Sequence[StatisticAccumulatorFactory]
- dim
Number of mixture components K.
- Type:
- make()[source]
Returns a GaussianMixtureAccumulator with freshly made component accumulators.
- Return type:
GaussianMixtureAccumulator
- class GaussianMixtureEstimator(estimators, name=None, conj_prior_params=None, suff_stat=None, pseudo_count=None, keys=(None, None))[source]
Bases:
MixtureEstimatorGaussianMixtureEstimator object for estimating GaussianMixtureDistribution from sufficient statistics.
A thin specialization of
MixtureEstimator: the weight M-step (incl. pseudo_count / suff_stat regularization) is inherited unchanged; only the final repacking of the estimated components into aGaussianMixtureDistribution(reading each component’smu/covar) is Gaussian-specific.- Parameters:
estimators (Sequence[ParameterEstimator]) – ParameterEstimator objects for the mixture components (typically MultivariateGaussianEstimator objects).
name (Optional[str]) – Set name for object instance.
conj_prior_params (Optional[Any]) – Reserved for conjugate prior parameters (currently unused).
suff_stat (Optional[np.ndarray]) – Prior mixture weights used with pseudo_count to regularize the weight estimate. Must have length equal to the number of components.
pseudo_count (Optional[float]) – Used to re-weight the mixture weight sufficient statistics in estimation.
keys (Tuple[Optional[str], Optional[str]]) – Set keys for the weights and component statistics.
- num_components
Number of mixture components K.
- Type:
- estimators
Component estimators.
- Type:
Sequence[ParameterEstimator]
- pseudo_count
Weight regularization constant.
- Type:
Optional[float]
- suff_stat
Prior mixture weights.
- Type:
Optional[np.ndarray]
- conj_prior_params
Reserved for conjugate prior parameters (currently unused).
- Type:
Optional[Any]
- name
Name of object instance.
- Type:
Optional[str]
- accumulator_factory()[source]
Returns a GaussianMixtureEstimatorAccumulatorFactory built from the component estimators.
- Return type:
GaussianMixtureEstimatorAccumulatorFactory
- accumulatorFactory()[source]
Deprecated alias for accumulator_factory().
- Return type:
GaussianMixtureEstimatorAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a GaussianMixtureDistribution from aggregated sufficient statistics.
The component M-step and weight estimate (incl. pseudo_count / suff_stat regularization) reuse the generic MixtureEstimator path; the estimated components are then repacked into a GaussianMixtureDistribution via each component’s
mu/covar.
- class GaussianMixtureDataEncoder(encoder=None)[source]
Bases:
MixtureDataEncoderGaussianMixtureDataEncoder object for encoding sequences of iid Gaussian mixture observations.
- Parameters:
encoder (Optional[DataSequenceEncoder]) – DataSequenceEncoder for the component distributions. Defaults to a MultivariateGaussianDataEncoder.
- encoder
DataSequenceEncoder for the component distributions.
- Type:
DataSequenceEncoder
- GaussianMixtureAccumulatorFactory
alias of
GaussianMixtureEstimatorAccumulatorFactory