Multinomial
DistributionMultinomial(total_count: Tensor | int = 1, probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)Multinomial distribution over categories.
Multivariate generalisation of the lucid.distributions.Bernoulli
/Binomial: the joint distribution of the counts
obtained when
independent draws are made from a Categorical with probabilities
. The are non-negative integers
summing to .
The event dimension is the last axis of probs / logits; all
preceding axes form the batch shape.
Parameters
1 (a
one-hot Categorical sample).(..., K) —
normalised internally to sum to 1 along the last axis. Mutually
exclusive with logits.(..., K), converted
via softmax. Mutually exclusive with probs.validate_argsbool= NoneTrue, validate parameter constraints at construction time.Notes
Probability mass function (joint over the count vector with ):
Moments (per category):
Special cases:
- →
- → one-hot
lucid.distributions.Categorical
Conjugate prior: lucid.distributions.Dirichlet —
observing counts updates
Dirichlet(α) → Dirichlet(α + k).
Sampling is non-reparameterised (has_rsample = False) and
implemented by summing one-hot Categorical draws.
Examples
>>> import lucid
>>> from lucid.distributions import Multinomial
>>> d = Multinomial(total_count=10, probs=lucid.tensor([0.2, 0.3, 0.5]))
>>> d.mean # n * p
tensor([2., 3., 5.])
>>> x = d.sample((4,))
>>> x.shape
(4, 3)
>>> bool((x.sum(dim=-1) == 10).all()) # every draw places all n trials
TrueUsed by 1
Constructors
1__init__
→None__init__(total_count: Tensor | int = 1, probs: Tensor | None = None, logits: Tensor | None = None, validate_args: bool | None = None)Initialise a Multinomial distribution.
Parameters
1 (reduces to a one-hot Categorical).(..., K) — exactly one of the two. Either is
normalised over the last axis before it is stored.(..., K) — exactly one of the two. Either is
normalised over the last axis before it is stored.validate_argsbool | None= NoneRaises
ValueErrorprobs and logits are given, or —
under validation — a normalised parameter is not a probability
vector or total_count is not a non-negative whole number.Properties
4n · p (element-wise).
Count vectors of total_count draws.
Returns
Constraintmultinomial(total_count) — non-negative entries summing to at
most total_count, the reference framework's support.
Total number of trials per Multinomial draw.
Returns
TensorThe integer-valued n parameter. Each independent
Multinomial sums to this many trials across the K categories.
n · p · (1 − p) (element-wise).
Instance methods
5Shannon entropy of the Multinomial distribution (in nats).
where is the entropy of one categorical draw.
From with
:
each count is Binomial(n, p_i), so the expectation of
is a sum over its values — the
reference framework's formula.
Returns
TensorEntropy in nats, shape batch_shape.
Examples
>>> import lucid
>>> from lucid.distributions import Multinomial
>>> Multinomial(2, probs=lucid.tensor([0.5, 0.5])).entropy() # ln 4 - ln 2/2
tensor(1.04)log C(n; k₁,…,kK) + Σ kᵢ log pᵢ with .
The coefficient counts the arrangements of the counts given, as the
reference framework's does, so a vector summing to less than
total_count is scored as a multinomial of its own total. A
category with contributes 0 even where
(a zero in probs or a logit of -inf), rather
than 0 · (-inf) = NaN.
Log-probabilities — the logits argument shifted by its
logsumexp, or derived from probs on access.
Derived as log(probs) with probs clamped one epsilon inside
[0, 1], as the reference framework derives it.
Category probabilities, normalised along the last axis.
Draw samples by summing one-hot Categorical draws.