mixle.stats.univariate.continuous.exponential module

Create, estimate, and sample from an exponential distribution with scale beta.

Defines the ExponentialDistribution, ExponentialSampler, ExponentialAccumulatorFactory,ExponentialAccumulator, ExponentialEstimator, and the ExponentialDataEncoder classes for use with mixle.

Data type: (float): The ExponentialDistribution with scale beta > 0.0, has log-density

log(f(x;beta)) = -log(beta) - x/beta, for x >= 0, else -np.inf.

Reference: Johnson, Kotz & Balakrishnan, Continuous Univariate Distributions (2nd ed., Wiley, 1994/95).

class ExponentialDistribution(beta, name=None, prior=None)[source]

Bases: SequenceEncodableProbabilityDistribution

Exponential distribution on non-negative real values with scale beta.

Parameters:
  • beta (float)

  • name (str | None)

  • prior (SequenceEncodableProbabilityDistribution | None)

classmethod compute_capabilities()[source]
classmethod compute_declaration()[source]
static exp_family_sufficient_statistics(x, engine)[source]

Return Exponential sufficient statistics for generated scoring.

Parameters:
Return type:

tuple[Any, …]

static exp_family_legacy_sufficient_statistics(x, params, engine)[source]

Return per-row Exponential sufficient statistics in accumulator order.

Parameters:
Return type:

tuple[Any, …]

static exp_family_natural_parameters(params, engine)[source]

Return Exponential natural parameters for generated scoring.

Parameters:
Return type:

tuple[Any, …]

static exp_family_log_partition(params, engine)[source]

Return Exponential log partition for generated scoring.

Parameters:
Return type:

Any

static exp_family_base_measure(x, engine)[source]

Return Exponential support base measure for generated scoring.

Parameters:
Return type:

Any

set_prior(prior)[source]

Attach a parameter prior and precompute the conjugate Gamma expectations.

The exponential rate is 1/beta; with a Gamma(k, theta) prior on that rate this caches the variational expected natural parameters so that expected_log_density(x) = e1*x - ea where the natural parameter is eta = -rate, E[eta] = -k*theta and E[-log(-eta)] = psi(k) + ln theta (mapping the prior’s (k, theta) to the bstats [a, b] = [k, 1/theta] form). Any other prior (including None) 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 | rate)] under the Gamma prior.

Falls back to the plug-in log_density(x) when no conjugate prior is attached.

Parameters:

x (float)

Return type:

float

seq_expected_log_density(x)[source]

Vectorized expected_log_density over sequence-encoded observations.

Parameters:

x (ndarray)

Return type:

ndarray

density(x)[source]

Evaluate the density of exponential distribution with scale beta.

See log_density() for details.

Parameters:

x (float) – Non-negative real-valued number.

Returns:

Density evaluated at x.

Return type:

float

log_density(x)[source]

Evaluate the log-density of exponential distribution with scale beta.

log(f(x;beta)) = -log(beta) - x/beta, for x >= 0, else -np.inf.

Parameters:

x (float) – Non-negative real-valued number.

Returns:

Log-density evaluated at x.

Return type:

float

seq_log_density(x)[source]

Vectorized evaluation of log-density at sequence encoded input x.

Vectorized call to log-density on each observation value x.

Parameters:

x (np.ndarray) – Numpy array of floats.

Returns:

Numpy array of log-density (float) of len(x).

Return type:

ndarray

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

Engine-neutral exponential log-density from explicit parameters.

Parameters:
Return type:

Any

backend_seq_log_density(x, engine)[source]

Engine-neutral vectorized log-density for encoded data.

Parameters:
Return type:

Any

classmethod backend_stacked_params(dists, engine)[source]

Return stacked Exponential parameters for a homogeneous mixture kernel.

Parameters:
  • dists (Sequence[ExponentialDistribution])

  • engine (Any)

Return type:

dict[str, Any]

classmethod backend_stacked_log_density(x, params, engine)[source]

Return an (n, k) matrix of Exponential log densities.

Parameters:
Return type:

Any

classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]

Return stacked Exponential sufficient statistics using engine-resident arrays.

Parameters:
Return type:

tuple[Any, Any]

cdf(x)[source]

Cumulative distribution function P(X <= x) (exact). The continuous ‘index of’ a value.

Parameters:

x (float)

Return type:

float

quantile(q)[source]

Inverse CDF F^{-1}(q): the value at cumulative-probability index q (continuous unranking).

Parameters:

q (float)

Return type:

float

to_fisher(**kwargs)[source]

Return the Exponential’s count-family Fisher view.

mean()[source]

Mean E[X] of the distribution.

Return type:

float

variance()[source]

Variance Var[X] of the distribution.

Return type:

float

entropy()[source]

Differential entropy 1 + log(scale).

Return type:

float

skewness()[source]

Skewness (2).

Return type:

float

kurtosis()[source]

Excess kurtosis (6).

Return type:

float

mode()[source]

Mode (0).

Return type:

float

sampler(seed=None)[source]

Create an ExponentialSampler object with scale beta.

Parameters:

seed (Optional[int]) – Used to set seed in random sampler.

Returns:

ExponentialSampler object.

Return type:

ExponentialSampler

estimator(pseudo_count=None)[source]

Create ExponentialEstimator with beta passed as suff_stat.

Parameters:

pseudo_count (Optional[float]) – Used to inflate sufficient statistics.

Returns:

ExponentialEstimator.

Return type:

ExponentialEstimator

dist_to_encoder()[source]

Returns an ExponentialDataEncoder object.

Return type:

ExponentialDataEncoder

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

Bases: DistributionSampler

Parameters:
  • dist (ExponentialDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw ‘size’ iid samples from ExponentialSampler object.

Parameters:

size (Optional[int]) – Treated as 1 if None is passed.

Returns:

Numpy array of length ‘size’ from exponential distribution with scale beta if size not None. Else a single sample is returned as float.

Return type:

float | ndarray

class ExponentialAccumulator(keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Parameters:

keys (str | None)

update(x, weight, estimate)[source]

Update sufficient statistics for ExponentialAccumulator with one weighted observation.

Parameters:
  • x (float) – Observation from exponential distribution.

  • weight (float) – Weight for observation.

  • estimate (Optional['ExponentialDistribution']) – 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.

sum increased by sum of weighted observations. count increased by sum of weights.

Parameters:
  • x (ndarray) – Numpy array of positvie floats.

  • weights (ndarray) – Numpy array of positive floats.

  • estimate (Optional['ExponentialDistribution']) – Kept for consistency with SequenceEncodableStatisticAccumulator.

Returns:

None.

Return type:

None

initialize(x, weight, rng)[source]

Initialize sufficient statistics of ExponentialAccumulator with weighted observation.

Note: Just calls update.

Parameters:
  • x (float) – Positive real-valued observation of exponential.

  • weight (float) – Positive real-valued weight for observation x.

  • rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.

Returns:

None.

Return type:

None

seq_initialize(x, weights, rng)[source]

Vectorized initialization of ExponentialAccumulator sufficient statistics with weighted observations.

Note: Just calls seq_update().

Parameters:
  • x (ndarray) – Numpy array of positive floats.

  • weights (ndarray) – Numpy array of positive floats.

  • rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.

Returns:

None.

Return type:

None

combine(suff_stat)[source]

Aggregates sufficient statistics with ExponentialAccumulator member sufficient statistics.

Parameters:

suff_stat (Tuple[float, float]) – Aggregated count and sum.

Returns:

ExponentialAccumulator

Return type:

ExponentialAccumulator

value()[source]

Returns Tuple[float, float] containing sufficient statistics (count and sum) of ExponentialAccumulator.

Return type:

tuple[float, float]

from_value(x)[source]

Sets sufficient statistics (count and sum) of ExponentialAccumulator to x.

Parameters:

x (Tuple[float, float]) – Sufficient statistics tuple (count, sum).

Returns:

ExponentialAccumulator

Return type:

ExponentialAccumulator

key_merge(stats_dict)[source]

Merges ExponentialAccumulator sufficient statistics with sufficient statistics contained in suff_stat dict that share the same key.

Parameters:

stats_dict (Dict[str, Any]) – Dict containing ‘key’ string for ExponentialAccumulator objects that represent the same distribution.

Returns:

None.

Return type:

None

key_replace(stats_dict)[source]
Set the sufficient statistics of ExponentialAccumulator to stats_key sufficient statistics if key is in

stats_dict.

Parameters:

stats_dict (Dict[str, Any]) – Map key to sufficient statistics.

Returns:

None.

Return type:

None

acc_to_encoder()[source]

Returns an ExponentialDataEncoder object.

Return type:

ExponentialDataEncoder

class ExponentialAccumulatorFactory(keys=None)[source]

Bases: StatisticAccumulatorFactory

Parameters:

keys (str | None)

make()[source]

Create ExponentialAccumulator with keys passed.

Return type:

ExponentialAccumulator

class ExponentialEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]

Bases: ParameterEstimator

Parameters:
  • pseudo_count (float | None)

  • suff_stat (float | None)

  • name (str | None)

  • keys (str | None)

  • prior (SequenceEncodableProbabilityDistribution | None)

accumulator_factory()[source]

Create ExponentialAccumulatorFactory object with keys passed.

Return type:

ExponentialAccumulatorFactory

model_log_density(model)[source]

Log-density of the model’s rate (1/beta) under the Gamma prior (ELBO global term).

Parameters:

model (ExponentialDistribution)

Return type:

float

estimate(nobs, suff_stat)[source]

Estimate ExponentialDistribution from suff_stat arg.

Estimate ExponentialDistribution from sufficient statistic tuple suff_stat, counting a float value for count and sum. If pseudo_count is set, this is used to re-weight the member value “suff_stat”, which is the scale of ExponentialEstimator object.

Parameters:
  • nobs (Optional[float]) – Not used. Kept for consistency with ParameterEstimator.

  • suff_stat (Tuple[float, float]) – Tuple of count and sum. Both are positive real-valued floats.

Returns:

ExponentialDistribution object.

Return type:

ExponentialDistribution

class ExponentialDataEncoder[source]

Bases: DataSequenceEncoder

ExponentialDataEncoder object for encoding sequences of iid exponential observations with data type float.

seq_encode(x)[source]

Encode sequence of iid exponential observations.

Data type must be a float. Data must also be non-negative real-valued numbers.

Parameters:

x (Union[List[float], np.ndarray]) – IID numpy array or list of non-negative real-valued floats.

Returns:

Numpy array of floats.

Return type:

ndarray