mixle.stats.multivariate.dirichlet_multinomial module¶
Dirichlet-multinomial (Polya) distribution – an overdispersed multinomial.
The multivariate analogue of the beta-binomial: a multinomial whose category probabilities are
Dirichlet(alpha) distributed and integrated out. For a count vector x over K categories summing
to n,
P(x; alpha) = n!/prod_k x_k! * B(alpha + x) / B(alpha), B(a) = prod_k Gamma(a_k) / Gamma(sum a),
which adds overdispersion (and category correlation) over a plain multinomial. The number of trials
n is a fixed, known parameter; alpha is fit by Minka’s maximum-likelihood fixed point, run from
a cumulative-count sufficient statistic so it converges inside a single estimate call.
- class DirichletMultinomialDistribution(alpha, n, name=None, keys=None)[source]
Bases:
SequenceEncodableProbabilityDistributionDirichlet-multinomial over
K-category count vectors summing ton(concentrationalpha).- density(x)[source]
Return the probability mass at a single count vector
x.
- log_density(x)[source]
Return the log-mass at
x(-infif any count is negative or the total is notn).
- seq_log_density(x)[source]
Vectorized log-mass for a stack of count vectors, shape
(N, K).
- sampler(seed=None)[source]
Return a sampler for drawing count vectors from this distribution.
- Parameters:
seed (int | None)
- Return type:
DirichletMultinomialSampler
- estimator(pseudo_count=None)[source]
Return a Minka fixed-point MLE estimator for
alphaat the fixed number of trialsn.- Parameters:
pseudo_count (float | None)
- Return type:
DirichletMultinomialEstimator
- dist_to_encoder()[source]
Return the data encoder used by this distribution for vectorized methods.
- Return type:
DirichletMultinomialDataEncoder
- class DirichletMultinomialSampler(dist, seed=None)[source]
Bases:
DistributionSamplerDraw counts as
p ~ Dirichlet(alpha)thenx ~ Multinomial(n, p).- Parameters:
dist (DirichletMultinomialDistribution)
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 DirichletMultinomialAccumulator(dim, n, name=None, keys=None)[source]
Bases:
SequenceEncodableStatisticAccumulatorAccumulate cumulative counts
c[k, j] = sum_i w_i 1{x_ik > j}(the Minka digamma-recurrence stat).- update(x, weight, estimate)[source]
- initialize(x, weight, rng)[source]
- Parameters:
x (ndarray)
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:
DirichletMultinomialDataEncoder
- class DirichletMultinomialAccumulatorFactory(dim, n, name=None, keys=None)[source]
Bases:
StatisticAccumulatorFactoryFactory for DirichletMultinomialAccumulator.
- make()[source]
- Return type:
DirichletMultinomialAccumulator