mixle.stats.univariate.continuous.generalized_gaussian module

Generalized Gaussian (exponential-power) distribution.

A symmetric location-scale family with a tunable tail/peakedness shape beta that interpolates the Laplace (beta = 1), Gaussian (beta = 2), and uniform (beta -> inf) laws. With location mu, scale alpha > 0 and shape beta > 0,

f(x; mu, alpha, beta) = beta / (2 alpha Gamma(1/beta)) * exp(-(abs(x - mu) / alpha)^beta).

The normalizer is closed form (a Gamma function), so density/CDF/quantile/moments/entropy are all exact; it samples exactly via a Gamma draw with a random sign. Parameters are fit by the method of moments: mu is the mean, the excess kurtosis Gamma(5/beta)Gamma(1/beta)/Gamma(3/beta)^2 - 3 pins beta (monotone, solved by a bracketed root find), and alpha follows from the variance alpha^2 Gamma(3/beta)/Gamma(1/beta).

The M-step is shift-equivariant: fitting x + c returns mu + c with an unchanged alpha and beta. That does not come for free from raw power sums – the central fourth moment differenced out of E[x^4] loses about 4*log2(abs(mean)/sd) bits – so the accumulator carries a conditioning-gated shift-anchored moment track alongside the raw sums (see GeneralizedGaussianAccumulator).

References

  • Nadarajah, “A generalized normal distribution”, J. Applied Statistics 32 (2005).

  • Subbotin (1923), the original exponential-power family.

class GeneralizedGaussianSuffStat(count, s1, s2, s3, s4, anchored=None)[source]

Bases: tuple

A (count, s1, s2, s3, s4) sufficient statistic that also carries a side payload.

Behaves exactly like the plain 5-tuple everywhere it is indexed, unpacked, or iterated (it is one); anchored is the shift-anchored payload (anchor, sum_i w_i (x_i - anchor)^k for k = 1..4) the accumulator maintains alongside the raw power sums so the M-step survives large-offset data. Code that doesn’t know about the payload (generic scale_suff_stat, serializers, …) sees an ordinary tuple and the estimate falls back to the historical raw path.

Parameters:
Return type:

GeneralizedGaussianSuffStat

class GeneralizedGaussianDistribution(mu, alpha, beta, name=None, keys=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Generalized Gaussian (exponential power) with location mu, scale alpha and shape beta.

Parameters:
classmethod compute_declaration()[source]

Return the structured compute declaration for generalized Gaussian distributions.

static backend_log_density_from_params(x, mu, alpha, beta, engine)[source]

Engine-neutral generalized-Gaussian log-density: log_norm - (abs(x-mu)/alpha)**beta.

Parameters:
Return type:

Any

density(x)[source]

Return the probability density at x.

Parameters:

x (float)

Return type:

float

log_density(x)[source]

Return the log-density at x.

Parameters:

x (float)

Return type:

float

seq_log_density(x)[source]

Return vectorized log-density for a sequence-encoded array of observations.

Parameters:

x (ndarray)

Return type:

ndarray

cdf(x)[source]

Cumulative distribution function P(X <= x).

Parameters:

x (float)

Return type:

float

quantile(q)[source]

Inverse CDF F^{-1}(q).

Parameters:

q (float)

Return type:

float

mean()[source]

Mean (the location mu).

Return type:

float

variance()[source]

Variance alpha^2 Gamma(3/beta) / Gamma(1/beta).

Return type:

float

skewness()[source]

Skewness (0 – the law is symmetric).

Return type:

float

kurtosis()[source]

Excess kurtosis Gamma(5/beta)Gamma(1/beta)/Gamma(3/beta)^2 - 3.

Return type:

float

entropy()[source]

Differential entropy 1/beta - log(beta / (2 alpha Gamma(1/beta))).

Return type:

float

sampler(seed=None)[source]

Return a sampler (Gamma magnitude with a random sign).

Parameters:

seed (int | None)

Return type:

GeneralizedGaussianSampler

estimator(pseudo_count=None)[source]

Return a method-of-moments estimator for mu, alpha, beta.

Parameters:

pseudo_count (float | None)

Return type:

GeneralizedGaussianEstimator

dist_to_encoder()[source]

Return the data encoder used by this distribution (the raw value).

Return type:

GeneralizedGaussianDataEncoder

class GeneralizedGaussianSampler(dist, seed=None)[source]

Bases: DistributionSampler

Draw x = mu + sign * alpha * Gamma(1/beta)**(1/beta).

Parameters:
  • dist (GeneralizedGaussianDistribution)

  • seed (int | None)

sample(size=None, *, batched=True)[source]

Draw one sample or an array of iid samples.

Parameters:
Return type:

float | ndarray

class GeneralizedGaussianAccumulator(name=None, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate the weighted power sums (count, sum x, sum x^2, sum x^3, sum x^4) for the MoM.

Alongside the raw sums the accumulator keeps a CONDITIONING-GATED shift-anchored track, sum_i w_i (x_i - anchor)^k for k = 1..4 about a data anchor. The method of moments needs central moments, and differencing them out of raw power sums is the classic cancellation-prone form: the fourth reduced moment loses roughly 4*log2(abs(mean)/sd) bits, so data with sd ~0.7 at offset 1.7e9 has no correct digits left in m4 and the fit collapses onto the shape bound with a scale two orders of magnitude too small – silently. Anchoring keeps every term of the scatter O(count * spread^4), making the M-step shift-equivariant.

The gate keeps the historical path bit-identical for well-conditioned data: a chunk whose abs(mean)/spread ratio the raw form handles to ~1e-9 relative (see _needs_anchor()) accumulates exactly the way it always did, with no anchor and no second pass. The raw sums remain the exchange format, so the anchored track rides along as a payload on GeneralizedGaussianSuffStat; a consumer that drops the payload simply gets the historical raw estimate back.

Parameters:
  • name (str | None)

  • keys (str | None)

update(x, weight, estimate)[source]

Accumulate weighted raw moments up to order four for one observation.

Parameters:
  • x (float)

  • weight (float)

  • estimate (GeneralizedGaussianDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]

Initialize statistics from one observation.

Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate weighted raw moments up to order four from encoded data.

Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize statistics from encoded observations.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge another generalized-Gaussian sufficient-statistic tuple.

Parameters:

suff_stat (tuple[float, float, float, float, float])

Return type:

GeneralizedGaussianAccumulator

value()[source]

Return count and raw moment sums through order four.

The returned object is a plain 5-tuple for every consumer that treats it as one; once the shift-anchored track is live it additionally carries the anchored moments in its .anchored attribute, so combine() can fold them in and GeneralizedGaussianEstimator.estimate() can use them for the reduced moments. The payload is withheld when the track was seeded from raw statistics that had already lost their central moments (see _activate_anchor()), so a pool that cannot honestly claim shift-equivariance reports the historical raw statistics instead of a worse anchored guess.

Return type:

tuple[float, float, float, float, float]

from_value(x)[source]

Replace accumulator contents from a sufficient-statistic tuple.

Parameters:

x (tuple[float, float, float, float, float])

Return type:

GeneralizedGaussianAccumulator

scale(c)[source]

Scale accumulated evidence, keeping the shift-anchored track in step with the raw sums.

The generic base implementation round-trips through scale_suff_stat/from_value, which sees only a plain tuple and would silently drop the anchored payload – turning a scaled accumulator back into the ill-conditioned raw path.

Parameters:

c (float)

Return type:

GeneralizedGaussianAccumulator

acc_to_encoder()[source]

Return the encoder used by this accumulator.

Return type:

GeneralizedGaussianDataEncoder

class GeneralizedGaussianAccumulatorFactory(name=None, keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for GeneralizedGaussianAccumulator.

Parameters:
  • name (str | None)

  • keys (str | None)

make()[source]

Create a fresh generalized-Gaussian accumulator.

Return type:

GeneralizedGaussianAccumulator

class GeneralizedGaussianEstimator(pseudo_count=None, suff_stat=None, beta_bounds=(0.25, 50.0), name=None, keys=None, prior_central=None)[source]

Bases: ParameterEstimator

Method-of-moments estimator: mu = mean, beta from excess kurtosis, alpha from variance.

The reduced moments are formed about a reference point rather than about zero whenever the accumulated statistics carry a shift-anchored payload (see GeneralizedGaussianAccumulator), which makes the fit shift-equivariant: estimate on x + c returns mu + c with alpha and beta unchanged. With a plain raw tuple – statistics restored from an older serialization, or a hand-built one – the historical raw path is used unchanged.

Parameters:
accumulator_factory()[source]

Return an accumulator factory for generalized-Gaussian raw moments.

Return type:

GeneralizedGaussianAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate location, scale, and shape from weighted raw moments.

Parameters:
Return type:

GeneralizedGaussianDistribution

class GeneralizedGaussianDataEncoder[source]

Bases: DataSequenceEncoder

Encode observations as a float array.

seq_encode(x)[source]

Encode observations as a floating-point array.

Parameters:

x (Sequence[float])

Return type:

ndarray