Discrete distribution · also called generalized Bernoulli
Categorical distribution
One draw from outcomes with probabilities : the word a language model emits next, the class a classifier predicts, the face of a loaded die. It is a Bernoulli with more than two outcomes. The plot lets you drag a weight for each of four categories; the probabilities are the weights divided by their total.
Categorical probability calculator
P(X ≤ 3) = 0.875
At the parameters set by the sliders. The shaded region on the plot is the probability; the dots on the CDF mark F at its ends. For a discrete distribution, < and ≤ differ.
PMF
CDF
mean 2.625 · variance 0.7344 · sd 0.857 · mode 3 · entropy 1.213 nats
Formulas
| Parameters |
|
|---|---|
| Support | |
| PMF | |
| CDF | |
| Mean | |
| Variance | |
| Mode | |
| Entropy |
The categories are often just labels, in which case the mean and variance mean little; the probabilities and the entropy are what matter. Cross-entropy loss is the negative log of for the true class.
Worked example
A four-sided die lands on its faces with probabilities . What is its entropy in bits?
against 2 bits for a fair four-sided die. An optimal code for its rolls needs 1.75 bits each on average. answer checked by code
How it relates to others
- Special case→ Bernoulli: A categorical with two outcomes is a Bernoulli. checked by code
- Sum→ Multinomial: The counts of each outcome in independent categorical draws are Multinomial(). checked by code
- Conjugate prior← Dirichlet: The Dirichlet is the conjugate prior for categorical probabilities: observing outcome adds 1 to .
All the relationships, as a diagram →
In Python
from scipy import stats
import numpy as np
X = stats.rv_discrete(values=([1, 2, 3, 4], [0.125, 0.25, 0.5, 0.125]))
X.pmf(3) # P(X = 3) = 0.5
X.cdf(3) # P(X ≤ 3) = 0.875
X.mean(), X.var() # 2.625, 0.7344
X.ppf(0.95) # 95th percentile
X.rvs(size=10, random_state=0)
rng = np.random.default_rng(0)
rng.choice([1, 2, 3, 4], p=[0.125, 0.25, 0.5, 0.125], size=10)SciPy has no named categorical distribution; rv_discrete builds one from values and probabilities.
Choosing between this and its neighbours? Which distribution for count data? · Which prior distribution should I use?