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

00.20.41234P(X = k)mean

CDF

00.250.50.7511234F(x)

mean 2.625 · variance 0.7344 · sd 0.857 · mode 3 · entropy 1.213 nats

Formulas

Parameters
  • , the number of categories
  • with
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

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?

← Discrete uniformLogarithmic →