13.5 Categorical distribution
13.5.1 Probability mass functions
If \(N \in \mathbb{N}\), \(N > 0\), and if \(\theta \in \mathbb{R}^N\) forms an \(N\)-simplex (i.e., has nonnegative entries summing to one), then for \(y \in \{1,\ldots,N\}\), \[ \text{Categorical}(y~|~\theta) = \theta_y. \] In addition, Stan provides a log-odds scaled categorical distribution, \[ \text{CategoricalLogit}(y~|~\beta) = \text{Categorical}(y~|~\text{softmax}(\beta)). \] See the definition of softmax for the definition of the softmax function.
13.5.2 Sampling statement
y ~
categorical
(theta)
Increment target log probability density with categorical_lupmf(y | theta)
dropping constant additive terms.
13.5.3 Sampling statement
y ~
categorical_logit
(beta)
Increment target log probability density with categorical_logit_lupmf(y | beta)
.
13.5.4 Stan functions
All of the categorical distributions are vectorized so that the
outcome y can be a single integer (type int
) or an array of integers
(type int[]
).
real
categorical_lpmf
(ints y | vector theta)
The log categorical probability mass function with outcome(s) y in
\(1:N\) given \(N\)-vector of outcome probabilities theta. The parameter
theta must have non-negative entries that sum to one, but it need not
be a variable declared as a simplex.
real
categorical_lupmf
(ints y | vector theta)
The log categorical probability mass function with outcome(s) y in
\(1:N\) given \(N\)-vector of outcome probabilities theta dropping constant
additive terms. The parameter theta must have non-negative entries that sum
to one, but it need not be a variable declared as a simplex.
real
categorical_logit_lpmf
(ints y | vector beta)
The log categorical probability mass function with outcome(s) y in
\(1:N\) given log-odds of outcomes beta.
real
categorical_logit_lupmf
(ints y | vector beta)
The log categorical probability mass function with outcome(s) y in
\(1:N\) given log-odds of outcomes beta dropping constant additive terms.
int
categorical_rng
(vector theta)
Generate a categorical variate with \(N\)-simplex distribution parameter
theta; may only be used in transformed data and generated quantities blocks
int
categorical_logit_rng
(vector beta)
Generate a categorical variate with outcome in range \(1:N\) from
log-odds vector beta; may only be used in transformed data and generated
quantities blocks