mixle.stats.univariate.discrete.geometric module¶
Geometric distributions, estimators, samplers, accumulators, and encoders.
The observation type is int on support {1, 2, ...}. The log-density is
(k - 1) * log(1 - p) + log(p) for k >= 1.
Reference: Johnson, Kemp & Kotz, Univariate Discrete Distributions (3rd ed., Wiley, 2005).
- class GeometricDistribution(p, name=None, prior=None)[source]
Bases:
SequenceEncodableProbabilityDistributionGeometric distribution on
{1, 2, ...}with success probabilityp.- classmethod compute_capabilities()[source]
Declare backend support for generated Geometric density kernels.
- classmethod compute_declaration()[source]
Return the generated-compute declaration for the Geometric distribution.
- static exp_family_legacy_sufficient_statistics(x, params, engine)[source]
Return per-row Geometric sufficient statistics in accumulator order.
- static exp_family_sufficient_statistics(x, engine)[source]
Return Geometric sufficient statistic
T(x) = (x,)(support x = 1, 2, …).
- static exp_family_natural_parameters(params, engine)[source]
Return Geometric natural parameter
eta = log(1 - p).
- static exp_family_log_partition(params, engine)[source]
Return Geometric log partition
A = log(1 - p) - log(p).
- static exp_family_from_natural(eta)[source]
Return the Geometric with natural parameter
eta = log(1 - p).- Parameters:
eta (Any)
- Return type:
GeometricDistribution
- set_prior(prior)[source]
Attach a Beta parameter prior and precompute conjugate-prior expectations.
With a Beta(a, b) prior on the success probability
pthis caches the digamma terms(digamma(a), digamma(b), digamma(a+b))so thatexpected_log_densityevaluates the variational Bayes expectationE_q[log p(x | p)]viaE[log p] = digamma(a) - digamma(a+b)andE[log(1-p)] = digamma(b) - digamma(a+b). 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 | p)]under the Beta prior.Uses the cached digamma expectations of
log pandlog(1-p); falls back to the plug-inlog_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 geometric distribution evaluated at x.
P(x=k) = (k-1)*log(1-p) + log(p), for x = 1,2,…, else 0.0.
- log_density(x)[source]
Log-density of geometric distribution evaluated at x.
See density() for details.
- seq_log_density(x)[source]
Vectorized log-density evaluated on sequence encoded x.
- static backend_log_density_from_params(x, p, engine)[source]
Engine-neutral geometric 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 geometric parameters for a homogeneous mixture kernel.
- classmethod backend_stacked_log_density(x, params, engine)[source]
Return an
(n, k)matrix of geometric log densities.
- to_fisher(**kwargs)[source]
Return the Geometric’s count-family Fisher view.
- cdf(x)[source]
Cumulative distribution function P(X <= x) = 1 - (1-p)^floor(x), support x >= 1.
- quantile(q)[source]
Inverse CDF F^{-1}(q), support >= 1 (via scipy geom).
- sampler(seed=None)[source]
Create a sampler from this geometric distribution.
- Parameters:
seed (Optional[int]) – Used to set seed on random number generator.
- Returns:
GeometricSampler configured from this distribution.
- Return type:
GeometricSampler
- estimator(pseudo_count=None)[source]
Create an estimator for a geometric distribution.
- Parameters:
pseudo_count (Optional[float]) – Regularize empirical summary statistics.
- Returns:
GeometricEstimator configured with this distribution’s prior and name.
- Return type:
GeometricEstimator
- dist_to_encoder()[source]
Return the encoder for geometric observations.
- Return type:
GeometricDataEncoder
- enumerator()[source]
Returns GeometricEnumerator iterating the support {1, 2, …} in descending probability order.
- Return type:
GeometricEnumerator
- quantized_index(max_bits, bin_width_bits=1.0)[source]
Build a bounded bit-quantized index directly from the geometric tail formula.
- class GeometricEnumerator(dist)[source]
Bases:
DistributionEnumeratorEnumerate geometric support values in descending probability order.
- Parameters:
dist (GeometricDistribution)
- class GeometricSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw independent samples from a
GeometricDistribution.- Parameters:
dist (GeometricDistribution)
seed (int | None)
- sample(size=None)[source]
Generate iid samples from geometric distribution.
Generates a single geometric sample (int) if size is None, else a numpy array of integers of length size, iid samples, from the geometric distribution.
- class GeometricAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted count and sum statistics for Geometric estimation.
- update(x, weight, estimate)[source]
Update sufficient statistics for GeometricAccumulator with one weighted observation.
- 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 positive integers.
weights (ndarray) – Numpy array of positive floats.
estimate (Optional[GeometricDistribution]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- initialize(x, weight, rng)[source]
Initialize sufficient statistics of GeometricAccumulator with weighted observation.
This delegates to
update().
- seq_initialize(x, weights, rng)[source]
Vectorized initialization of GeometricAccumulator sufficient statistics with weighted observations.
This delegates to
seq_update().- Parameters:
x (ndarray) – Numpy array of positive integers.
weights (ndarray) – Numpy array of positive floats.
rng (Optional[RandomState]) – Kept for consistency with SequenceEncodableStatisticAccumulator.
- Returns:
None.
- Return type:
None
- combine(suff_stat)[source]
Combine aggregated sufficient statistics with sufficient statistics of GeometricAccumulator instance.
- Input suff_stat is Tuple[float, float] with:
suff_stat[0] (float): sum of observation weights, suff_stat[1] (float): weighted sum of observations.
- value()[source]
Returns sufficient statistics Tuple[float, float] of GeometricAccumulator instance.
- from_value(x)[source]
Sets GeometricAccumulator instance sufficient statistic member variables 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
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
- acc_to_encoder()[source]
Return the encoder associated with this accumulator.
- Return type:
GeometricDataEncoder
- class GeometricAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryCreate Geometric accumulators with shared name and merge-key metadata.
- make()[source]
Return GeometricAccumulator with name and keys passed.
- Return type:
GeometricAccumulator
- class GeometricEstimator(pseudo_count=None, suff_stat=None, name=None, keys=None, prior=None)[source]
Bases:
ParameterEstimatorEstimate Geometric success probabilities from accumulated sufficient statistics.
- Parameters:
- accumulator_factory()[source]
Create a geometric accumulator factory with this estimator’s name and keys.
- Return type:
GeometricAccumulatorFactory
- model_log_density(model)[source]
Log-density of the model’s success probability under the Beta prior (ELBO global term).
- Parameters:
model (GeometricDistribution)
- Return type:
- estimate(nobs, suff_stat)[source]
Estimate a geometric distribution from weighted count and sum statistics.
nobsis accepted for estimator API consistency but is not used.suff_statis(count, sum). Without a pseudo-count, the estimate isp = count / sum; with a pseudo-count, the empirical statistic is shrunk toward the estimator’s prior statistic when available.