mixle.stats.univariate.continuous.exgaussian module

Evaluate, estimate, and sample from an exponentially-modified Gaussian (EMG) distribution.

Defines the ExponentiallyModifiedGaussianDistribution, ExponentiallyModifiedGaussianSampler, ExponentiallyModifiedGaussianAccumulatorFactory, ExponentiallyModifiedGaussianAccumulator, ExponentiallyModifiedGaussianEstimator, and ExponentiallyModifiedGaussianDataEncoder classes for use with mixle.

Data type: (float): The EMG models X = N(mu, sigma2) + Exp(rate=lam) – a Gaussian convolved

with a (positive-shifting) exponential, giving a positively right-skewed real-valued density. Its stable log-density is

log f(x) = log(lam/2) - 0.5*u^2 + log(erfcx(z)),

where u = (x - mu)/sigma and z = (lam*sigma - u)/sqrt(2) and sigma = sqrt(sigma2). Using log_erfcx keeps the right tail (large z) from underflowing.

The MLE has no closed form (the score equations couple mu, sigma2, lam), so the estimator uses a method-of-moments fit from the accumulated first three moments, which is consistent and the usual practical choice for the EMG.

class ExponentiallyModifiedGaussianDistribution(mu, sigma2, lam, name=None, keys=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Exponentially-modified Gaussian: X = N(mu, sigma2) + Exp(rate=lam).

Parameters:
density(x)[source]

Density of the EMG at observation x (see log_density).

Parameters:

x (float)

Return type:

float

log_density(x)[source]

Stable log-density of the EMG at x.

log f(x) = log(lam/2) - 0.5*u^2 + log_erfcx(z) with u = (x - mu)/sigma and z = (lam*sigma - u)/sqrt(2).

Parameters:

x (float)

Return type:

float

seq_ld_lambda()[source]

Return vectorized log-density callables for encoded data.

Return type:

list[Callable]

seq_log_density(x)[source]

Vectorized EMG log-density at sequence-encoded input x.

Parameters:

x (ndarray)

Return type:

ndarray

classmethod compute_capabilities()[source]
backend_seq_log_density(x, engine)[source]

Engine-neutral vectorized EMG log-density for encoded data.

Parameters:
Return type:

Any

cdf(x)[source]

Cumulative distribution function P(X <= x) (exact, via scipy’s exponnorm).

Parameters:

x (float)

Return type:

float

quantile(q)[source]

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

Parameters:

q (float)

Return type:

float

sampler(seed=None)[source]

Return an ExponentiallyModifiedGaussianSampler for this distribution.

Parameters:

seed (int | None)

Return type:

ExponentiallyModifiedGaussianSampler

estimator(pseudo_count=None)[source]

Return an ExponentiallyModifiedGaussianEstimator (method-of-moments).

Parameters:

pseudo_count (float | None)

Return type:

ExponentiallyModifiedGaussianEstimator

dist_to_encoder()[source]

Returns an ExponentiallyModifiedGaussianDataEncoder object.

Return type:

ExponentiallyModifiedGaussianDataEncoder

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

Bases: DistributionSampler

Parameters:
  • dist (ExponentiallyModifiedGaussianDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw size iid EMG samples (a single float if size is None).

Parameters:

size (int | None)

Return type:

float | ndarray

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

Bases: SequenceEncodableStatisticAccumulator

Parameters:
  • keys (str | None)

  • name (str | None)

update(x, weight, estimate)[source]
Parameters:
  • x (float)

  • weight (float)

  • estimate (ExponentiallyModifiedGaussianDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]
Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]
Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]
Parameters:
  • x (ndarray)

  • weights (ndarray)

  • estimate (ExponentiallyModifiedGaussianDistribution | None)

Return type:

None

combine(suff_stat)[source]
Parameters:

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

Return type:

ExponentiallyModifiedGaussianAccumulator

value()[source]
Return type:

tuple[float, float, float, float]

from_value(x)[source]
Parameters:

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

Return type:

ExponentiallyModifiedGaussianAccumulator

scale(c)[source]

Scale linear sufficient statistics in-place by c.

The structural default is correct for ordinary weighted sums, nested tuples/lists/dicts, and numeric arrays. Families whose value() payload includes non-linear metadata such as support bounds must override this method and leave that metadata unscaled.

Parameters:

c (float)

Return type:

ExponentiallyModifiedGaussianAccumulator

key_merge(stats_dict)[source]

Pool this accumulator’s statistics into stats_dict under its merge key.

The structural default implements the common single-key pattern: store the accumulator under self.keys the first time the key is seen, else combine into the one already there. Accumulators with several named keys (e.g. an HMM’s init/trans/state keys) or a non-accumulator stats payload override this. A keys of None (the default) is a no-op.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator’s statistics from the pooled stats_dict entry (see key_merge).

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]
Return type:

ExponentiallyModifiedGaussianDataEncoder

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

Bases: StatisticAccumulatorFactory

Parameters:
  • name (str | None)

  • keys (str | None)

make()[source]
Return type:

ExponentiallyModifiedGaussianAccumulator

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

Bases: ParameterEstimator

Parameters:
  • name (str | None)

  • keys (str | None)

accumulator_factory()[source]
Return type:

ExponentiallyModifiedGaussianAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate an EMG from the accumulated (count, mean, M2, M3) via method of moments.

Parameters:
Return type:

ExponentiallyModifiedGaussianDistribution

class ExponentiallyModifiedGaussianDataEncoder[source]

Bases: DataSequenceEncoder

Encode sequences of iid EMG observations (data type float).

seq_encode(x)[source]

Encode the iid observation sequence x for vectorized evaluation.

Parameters:

x (list[float] | ndarray)

Return type:

ndarray