mixle.stats.univariate.continuous.gaussian module¶
Gaussian distributions, estimators, accumulators, samplers, and encoders.
For real-valued observations, GaussianDistribution(mu, sigma2) has
sigma2 > 0 and log-density:
log(f(x;mu, sigma2)) = -0.5*log(2*pi*sigma2) - 0.5*(x-mu)^2/sigma2, for real-valued x.
Reference: Johnson, Kotz & Balakrishnan, Continuous Univariate Distributions (2nd ed., Wiley, 1994/95).
- class GaussianFisherView(dist)[source]
Bases:
FixedFisherViewFisher view over the Gaussian’s (sum, sum2, count, count2) sufficient statistics.
- Parameters:
dist (Any)
- class GaussianDistribution(mu, sigma2, name=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionUnivariate Gaussian distribution.
- Parameters:
- classmethod compute_capabilities()[source]
Declare backend support for generated Gaussian density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Gaussian distribution.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Gaussian sufficient statistics for generated scoring.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Gaussian sufficient statistics in accumulator order.
- static exp_family_natural_parameters(params, engine)[source]
Return Gaussian natural parameters for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return Gaussian log partition for generated scoring.
- set_prior(prior)[source]
Attach a parameter prior and precompute conjugate-prior expectations.
With a NormalGamma(mu0, lam, a, b) prior over (mu, tau=1/sigma2) this caches the variational expected natural parameters [ea, eb, e1, e2] so that
expected_log_density(x) = x*(e1 + x*e2) - ea + eb(the VB E-step term). Any other prior (includingNone) leaves the distribution a plain point model.- Parameters:
prior (SequenceEncodableProbabilityDistribution | None)
- Return type:
None
- expected_log_density(x)[source]
Variational expectation E_q[log p(x | mu, tau)] under the NormalGamma prior.
Falls back to the plug-in
log_density(x)when no conjugate prior is attached.
- seq_expected_log_density(x)[source]
Vectorized
expected_log_densityover sequence-encoded observations.
- density(x)[source]
Density of Gaussian distribution at observation x.
See log_density() for details.
- log_density(x)[source]
Log-density of Gaussian distribution at observation x.
- Log-density of Gaussian with mean mu and variance sigma2 given by,
log(f(x;mu, sigma2)) = -0.5*log(2*pi*sigma2) - 0.5*(x-mu)^2/sigma2, for real-valued x.
- seq_ld_lambda()[source]
Return vectorized log-density callables for encoded data.
- seq_log_density(x)[source]
Vectorized evaluation of log-density at sequence encoded input x.
- Parameters:
x (np.ndarray) – Numpy array of floats.
- Returns:
Numpy array of log-density (float) of len(x).
- Return type:
- static backend_log_density_from_params(x, mu, sigma2, engine)[source]
Engine-neutral Gaussian log-density from explicit parameters.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded data.
- gradient_log_prior(priors, prior_strength, torch, engine)[source]
Distribution-owned MAP prior contribution for Gaussian parameters.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked Gaussian parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of Gaussian log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked Gaussian sufficient statistics using engine-resident arrays.
- cdf(x)[source]
Cumulative distribution function
P(X <= x)(exact). The continuous ‘index of’ a value.
- quantile(q)[source]
Inverse CDF
F^{-1}(q): the value at cumulative-probability indexq(continuous unranking).
- to_fisher(**kwargs)[source]
Return the Gaussian’s own Fisher view.
- sampler(seed=None)[source]
Return a sampler for iid draws from this distribution.
- Parameters:
seed (int | None) – Optional seed for the sampler’s random state.
- Returns:
A configured
GaussianSampler.- Return type:
GaussianSampler
- estimator(pseudo_count=None)[source]
Return an estimator initialized from this distribution’s shape.
- Parameters:
pseudo_count (float | None) – Optional smoothing count applied to the current mean and variance.
- Returns:
A
GaussianEstimator.- Return type:
GaussianEstimator
- dist_to_encoder()[source]
Return an encoder for iid scalar Gaussian observations.
- Return type:
GaussianDataEncoder
- class GaussianSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
GaussianDistribution.- Parameters:
dist (GaussianDistribution)
seed (int | None)
- class GaussianSuffStat(sum_, sum2_, count_, count2_, receipt=None)[source]
Bases:
tupleA
(sum, sum2, count, count2)sufficient statistic that also carries a numerics receipt.Behaves exactly like the plain 4-tuple everywhere it is indexed, unpacked, or iterated (it is one);
receiptis extra payload thatGaussianAccumulator.combine()reads to fold the Kahan error-bound bookkeeping (abs_total,nforsumandsum2) into the receiving accumulator when both sides arecompensated. Code that doesn’t know aboutcompensatedaccumulation (serialization, genericscale_suff_stat, …) sees an ordinary tuple.
- class GaussianAccumulator(keys=None, name=None, compensated=False)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted first and second moments for Gaussian estimation.
- update(x, weight, estimate)[source]
Update sufficient statistics for GaussianAccumulator with one weighted observation.
- initialize(x, weight, rng)[source]
Initialize with a weighted observation.
- seq_initialize(x, weights, rng)[source]
Vectorized initialization from encoded weighted observations.
- Parameters:
x (ndarray) – Numpy array of floats.
weights (ndarray) – Numpy array of positive floats.
rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from encoded sequence x.
- Parameters:
x (ndarray) – Numpy array of floats.
weights (ndarray) – Numpy array of positive floats.
estimate (Optional['GaussianDistribution']) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- combine(suff_stat)[source]
Merge sufficient statistics into this accumulator.
- Arg passed suff_stat is tuple of four floats:
suff_stat[0] (float): Sum of weighted observations (sum_i w_i*X_i), suff_stat[1] (float): Sum of weighted observations (sum_i w_i*X_i^2), suff_stat[2] (float): Sum of weighted observations (sum_i w_i), suff_stat[3] (float): Sum of weighted observations (sum_i w_i).
When this accumulator is
compensatedandsuff_statcarries a numerics-error receipt (seevalue()/GaussianSuffStat), the receipt is folded in too – its(abs_total, n)fields add exactly, just likesum/countabove, soerror_bound()composes correctly across combined partitions.
- error_bound()[source]
Return the running numerics-error bound receipt for
sum/sum2.Nonewhen this accumulator was not constructed withcompensated=True– the disabled default carries no receipt to report.
- value()[source]
Returns sufficient statistics of GaussianAccumulator object (Tuple[float, float, float, float]).
When
compensated, the returned value is aGaussianSuffStat– a drop-in 4-tuple (indexing/unpacking/iteration all behave identically) that additionally carries the numerics-error receipt in its.receiptattribute, socombine()can fold it in.
- from_value(x)[source]
Replace this accumulator’s sufficient statistics.
- Arg passed x is tuple of four floats:
x[0] (float): Sum of weighted observations (sum_i w_i*X_i), x[1] (float): Sum of weighted observations (sum_i w_i*X_i^2), x[2] (float): Sum of weighted observations (sum_i w_i), x[3] (float): Sum of weighted observations (sum_i w_i).
- key_merge(stats_dict)[source]
Merge sufficient statistics from
stats_dictwhen this accumulator’s key is present.- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to sufficient statistics.
- Returns:
None.
- Return type:
None
- key_replace(stats_dict)[source]
Replace sufficient statistics from
suff_statswhen this accumulator’s key is present.- Parameters:
stats_dict (Dict[str, Any]) – Dict mapping keys to sufficient statistics.
- Returns:
None.
- Return type:
None
- acc_to_encoder()[source]
Return an encoder compatible with Gaussian scalar observations.
- Return type:
GaussianDataEncoder
- class GaussianAccumulatorFactory(name=None, keys=None, compensated=False)[source]
Bases:
StatisticAccumulatorFactory- make()[source]
Return a GaussianAccumulator object with name and keys passed.
- Return type:
GaussianAccumulator
- class GaussianEstimator(pseudo_count=(None, None), suff_stat=(None, None), name=None, keys=None, prior=None, min_covar=None, compensated=False)[source]
Bases:
ParameterEstimatorEstimate Gaussian mean and variance from accumulated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Return GaussianAccumulatorFactory with name and keys passed.
- Return type:
GaussianAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model parameters under the NormalGamma prior (ELBO global term).
The prior is over (mu, tau=1/sigma2), so the model’s (mu, sigma2) is mapped accordingly.
- Parameters:
model (GaussianDistribution)
- Return type:
- estimate(nobs, suff_stat)[source]
Estimate a Gaussian distribution from aggregated sufficient statistics.
The tuple is interpreted as
(sum_x, sum_x2, count_for_mean, count_for_variance). Optional pseudo-counts smooth the corresponding mean and variance estimates.
- class GaussianDataEncoder[source]
Bases:
DataSequenceEncoderEncoder for iid scalar Gaussian observations.