mixle.stats.univariate.discrete.beta_binomial module¶
Beta-binomial distribution – an overdispersed binomial (Beta-binomial compound).
If the success probability of a binomial is itself Beta(a, b) distributed and integrated out, the
count k in n trials follows the beta-binomial:
P(k; n, a, b) = C(n, k) B(k + a, n - k + b) / B(a, b), k = 0, …, n,
with mean n a/(a+b) and a variance inflated over the binomial by the intra-class correlation
rho = 1/(a+b+1). It is the standard model for overdispersed bounded counts (clustered trials,
batch defect rates). The number of trials n is a fixed, known parameter; a and b are
estimated by moments – rho from the dispersion and the mean fixing a/(a+b).
- class BetaBinomialDistribution(n, a, b, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionBeta-binomial distribution over
{0, ..., n}with shape parametersa, b > 0.- density(x)[source]
Return the probability mass at a single count
x.
- log_density(x)[source]
Return the log probability mass at
x(-infoutside{0, ..., n}).
- seq_log_density(x)[source]
Return vectorized log-mass for an array of counts.
- sampler(seed=None)[source]
Return a sampler for drawing counts from this distribution.
- Parameters:
seed (int | None)
- Return type:
BetaBinomialSampler
- estimator(pseudo_count=None)[source]
Return a method-of-moments estimator for
a, bat the fixed number of trialsn.- Parameters:
pseudo_count (float | None)
- Return type:
BetaBinomialEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
BetaBinomialDataEncoder
- class BetaBinomialSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw counts as
p ~ Beta(a, b)thenk ~ Binomial(n, p).- Parameters:
dist (BetaBinomialDistribution)
seed (int | None)
- sample(size=None)[source]
Draw observations.
Combinator samplers (mixture/sequence/…) accept
batched. Withbatched=True(the default) each child stream is drawn in one vectorized call instead of a per-draw Python loop – far faster. Because every child sampler owns an independentRandomState, batching consumes each stream in the same order as the loop, so the draws are identical to the legacy path.batched=Falseforces that legacy per-draw loop as a guaranteed- stable reference. Leaf samplers are already vectorized and ignore the flag.
- class BetaBinomialAccumulator(name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate weighted first and second moments for beta-binomial estimation.
- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (int)
weight (float)
rng (RandomState | None)
- Return type:
None
- seq_update(x, weights, estimate)[source]
- seq_initialize(x, weights, rng)[source]
- Parameters:
x (ndarray)
weights (ndarray)
rng (RandomState | None)
- Return type:
None
- combine(suff_stat)[source]
- from_value(x)[source]
- key_merge(stats_dict)[source]
Pool this accumulator’s statistics into
stats_dictunder its merge key.The structural default implements the common single-key pattern: store the accumulator under
self.keysthe first time the key is seen, elsecombineinto 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. AkeysofNone(the default) is a no-op.
- key_replace(stats_dict)[source]
Replace this accumulator’s statistics from the pooled
stats_dictentry (see key_merge).
- acc_to_encoder()[source]
- Return type:
BetaBinomialDataEncoder
- class BetaBinomialAccumulatorFactory(name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for BetaBinomialAccumulator.
- make()[source]
- Return type:
BetaBinomialAccumulator