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: SequenceEncodableProbabilityDistribution

Dirichlet-multinomial over K-category count vectors summing to n (concentration alpha).

Parameters:
density(x)[source]

Return the probability mass at a single count vector x.

Parameters:

x (ndarray)

Return type:

float

log_density(x)[source]

Return the log-mass at x (-inf if any count is negative or the total is not n).

Parameters:

x (ndarray)

Return type:

float

seq_log_density(x)[source]

Vectorized log-mass for a stack of count vectors, shape (N, K).

Parameters:

x (ndarray)

Return type:

ndarray

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 alpha at the fixed number of trials n.

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: DistributionSampler

Draw counts as p ~ Dirichlet(alpha) then x ~ Multinomial(n, p).

Parameters:
  • dist (DirichletMultinomialDistribution)

  • seed (int | None)

sample(size=None)[source]

Draw one count vector or a stack of iid count vectors.

Parameters:

size (int | None)

Return type:

ndarray

class DirichletMultinomialAccumulator(dim, n, name=None, keys=None)[source]

Bases: SequenceEncodableStatisticAccumulator

Accumulate cumulative counts c[k, j] = sum_i w_i 1{x_ik > j} (the Minka digamma-recurrence stat).

Parameters:
update(x, weight, estimate)[source]

Accumulate Minka recurrence statistics for one count vector.

Parameters:
  • x (ndarray)

  • weight (float)

  • estimate (DirichletMultinomialDistribution | None)

Return type:

None

initialize(x, weight, rng)[source]

Initialize statistics from one count vector.

Parameters:
Return type:

None

seq_update(x, weights, estimate)[source]

Accumulate Minka recurrence statistics from encoded count vectors.

Parameters:
Return type:

None

seq_initialize(x, weights, rng)[source]

Initialize statistics from encoded count vectors.

Parameters:
Return type:

None

combine(suff_stat)[source]

Merge another Dirichlet-multinomial sufficient-statistic tuple.

Parameters:

suff_stat (tuple[ndarray, float])

Return type:

DirichletMultinomialAccumulator

value()[source]

Return cumulative recurrence counts and total weight.

Return type:

tuple[ndarray, float]

from_value(x)[source]

Replace accumulator contents from recurrence statistics.

Parameters:

x (tuple[ndarray, float])

Return type:

DirichletMultinomialAccumulator

key_merge(stats_dict)[source]

Merge keyed statistics into stats_dict when keys are configured.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

key_replace(stats_dict)[source]

Replace this accumulator from keyed statistics when available.

Parameters:

stats_dict (dict[str, Any])

Return type:

None

acc_to_encoder()[source]

Return the encoder used by this accumulator.

Return type:

DirichletMultinomialDataEncoder

class DirichletMultinomialAccumulatorFactory(dim, n, name=None, keys=None)[source]

Bases: StatisticAccumulatorFactory

Factory for DirichletMultinomialAccumulator.

Parameters:
make()[source]

Create a fresh Dirichlet-multinomial accumulator.

Return type:

DirichletMultinomialAccumulator

class DirichletMultinomialEstimator(dim, n, max_iter=500, tol=1.0e-9, name=None, keys=None)[source]

Bases: ParameterEstimator

Minka fixed-point maximum-likelihood estimator for the Dirichlet-multinomial concentration.

Parameters:
accumulator_factory()[source]

Return an accumulator factory for Dirichlet-multinomial statistics.

Return type:

DirichletMultinomialAccumulatorFactory

estimate(nobs, suff_stat)[source]

Estimate concentration parameters by Minka’s fixed-point update.

Parameters:
Return type:

DirichletMultinomialDistribution

class DirichletMultinomialDataEncoder[source]

Bases: DataSequenceEncoder

Encode a sequence of K-category count vectors as an (N, K) array.

seq_encode(x)[source]

Encode count vectors as a floating-point matrix.

Parameters:

x (Sequence[ndarray])

Return type:

ndarray