mixle.stats.univariate.continuous.gamma module¶
Gamma distributions, estimators, accumulators, samplers, and encoders.
For positive real-valued observations, GammaDistribution(k, theta) uses
shape k > 0 and scale theta > 0 with log-density:
log(f(x;k,theta)) = -gammaln(k) - k*log(theta) + (k-1) * log(x) - x / theta.
Values outside the positive support score -inf.
Reference: Johnson, Kotz & Balakrishnan, Continuous Univariate Distributions (2nd ed., Wiley, 1994/95).
- class GammaFisherView(dist)[source]
Bases:
FixedFisherViewExpose Gamma sufficient statistics for Fisher-information utilities.
- Parameters:
dist (Any)
- class GammaDistribution(k, theta, name=None)[source]
Bases:
SequenceEncodableProbabilityDistributionGamma distribution parameterized by shape and scale.
- classmethod compute_capabilities()[source]
Declare backend support for generated Gamma density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Gamma distribution.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Gamma sufficient statistics for generated scoring.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Gamma sufficient statistics in accumulator order.
- static exp_family_natural_parameters(params, engine)[source]
Return Gamma natural parameters for generated scoring.
- static exp_family_log_partition(params, engine)[source]
Return Gamma log partition for generated scoring.
- static exp_family_base_measure(x, engine)[source]
Return Gamma support base measure for generated scoring.
- get_parameters()[source]
Return the (shape k, scale theta) pair.
Lets a GammaDistribution serve as a conjugate prior (on a Poisson/Exponential rate, or a Gamma scale) under the unified Bayesian estimation protocol.
- cross_entropy(dist)[source]
Cross entropy -E_self[log dist(x)] for a Gamma argument (closed form).
Used as the conjugate prior/posterior cross-entropy term in variational Bayes (e.g. the ELBO global term in DPM for Poisson/Exponential-rate components).
- Parameters:
dist (GammaDistribution)
- Return type:
- density(x)[source]
Density of gamma distribution evaluated at x.
See log_density() for details.
- log_density(x)[source]
Log-density of gamma distribution evaluated at x.
Log-density given by, If x > 0.0,
log(f(x;k,theta)) = -gammaln(k) - k*log(theta) + (k-1) * log(x) - x / theta,
- else,
-np.inf
- seq_log_density(x)[source]
Vectorized evaluation of sequence encoded observations from gamma distribution.
- Input must be x (Tuple[ndarray, ndarray]):
x[0]: Numpy array of floats containing observations from gamma distribution. x[1]: Numpy array of floats containing log of observation values.
- Parameters:
x (Tuple[np.ndarray, np.ndarray]) – See above for details.
- Returns:
Numpy array containing log-density evaluated at all observations of encoded sequence x.
- Return type:
- static backend_log_density_from_params(vals, log_vals, k, theta, engine)[source]
Engine-neutral gamma log-density from explicit parameters.
- backend_seq_log_density(x, engine)[source]
Engine-neutral vectorized log-density for encoded data.
- classmethod backend_stacked_params(dists, engine)[source]
Return stacked Gamma parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of Gamma log densities.
- classmethod backend_stacked_sufficient_statistics(x, weights, params, engine)[source]
Return stacked Gamma 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 this distribution’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
GammaSampler.- Return type:
GammaSampler
- 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 distribution moments.
- Returns:
A
GammaEstimator.- Return type:
GammaEstimator
- dist_to_encoder()[source]
Return an encoder for iid Gamma observations.
- Return type:
GammaDataEncoder
- class GammaSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
GammaDistribution.- Parameters:
dist (GammaDistribution)
seed (int | None)
- class GammaAccumulator(keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count, sum, and log-sum statistics for Gamma estimation.
- Parameters:
keys (str | None)
- initialize(x, weight, rng)[source]
Initialize sufficient statistics of GammaAccumulator with weighted observation.
This delegates to
update().
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of GammaAccumulator sufficient statistics with weighted observations.
This delegates to
seq_update().- Parameters:
x (Tuple[ndarray, ndarray]) – Tuple of Numpy array of observations and log(observations).
weights (ndarray) – Numpy array of positive floats.
rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- update(x, weight, estimate)[source]
Update sufficient statistics for GammaAccumulator with one weighted observation.
- seq_update(x, weights, estimate)[source]
Vectorized update of sufficient statistics from encoded sequence x.
- Parameters:
x (Tuple[ndarray, ndarray]) – Tuple of Numpy array of observations and log(observations).
weights (ndarray) – Numpy array of positive floats.
estimate (Optional[GammaDistribution]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- combine(suff_stat)[source]
Aggregates sufficient statistics with GammaAccumulator member sufficient statistics.
- value()[source]
Return
(count, sum, sum_of_logs)sufficient statistics.
- from_value(x)[source]
Sets sufficient statistics GammaAccumulator to x.
- 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 Gamma observations.
- Return type:
GammaDataEncoder
- class GammaAccumulatorFactory(keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Gamma accumulators with a shared optional merge key.
- Parameters:
keys (str | None)
- make()[source]
Return a fresh Gamma accumulator.
- Return type:
GammaAccumulator
- class GammaEstimator(pseudo_count=(0.0, 0.0), suff_stat=(1.0, 0.0), threshold=1.0e-8, name=None, keys=None)[source]
Bases:
ParameterEstimatorEstimate Gamma shape and scale parameters from sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Return an accumulator factory matching this estimator.
- Return type:
GammaAccumulatorFactory
- estimate(nobs, suff_stat)[source]
Estimate a Gamma distribution from aggregated sufficient statistics.
The tuple is interpreted as
(count, sum, sum_of_logs).
- static estimate_shape(avg_sum, avg_sum_of_logs, threshold)[source]
Estimates the shape parameter of GammaDistribution.