Search

Search papers and lessons

Categorical Distribution

Level 2 · 13 min read

The categorical distribution describes one draw from a set of named categories, where each category has its own probability and the probabilities add up to 1.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • Probability
  • ReadReady to readNot yet

TL;DR

A categorical distribution models one random choice among a fixed set of named categories, such as a colour, a species or a predicted label. Each category kk gets its own probability pkp_k, the probabilities are all between 0 and 1, and they add up to 1. It is the probability mass function of a variable whose values are categories instead of numbers, and it is the standard model for a single draw from a list of options.

Builds on: Probability Mass Function · Leads to: Softmax, Cross-Entropy Loss, Gumbel-Softmax Trick

Where it's used

Predicting a label in an image classifier. A model that sorts photos into cat, dog and bird outputs three numbers, one per class. Those numbers form a categorical distribution over the three labels. The predicted label is the one with the largest probability, and the rest of the distribution tells you how sure the model is.

Choosing the next word in a language model. A language model works over a vocabulary of tens of thousands of tokens. At each step it outputs one probability per token, and the next token is drawn from that categorical distribution. This is why the same prompt can give different continuations when the model samples instead of always taking the top token.

Surveys with several answers. A customer survey asks how satisfied a customer is, with four options: very satisfied, satisfied, neutral and dissatisfied. Treating each respondent as one independent draw from the population, the chance of picking each option is a categorical distribution over those four options, and the observed shares estimate it.

Simulating a fair die. A six-sided die is a categorical distribution in which each face has probability 1/6. Simulation code for board games and random tests uses this model whenever it needs one roll.

How it works

Take one tiny instance: a bag holds 10 balls, 5 red, 3 green and 2 blue. You reach in without looking and pull out one ball. The colour is the random outcome, and it has three possible categories.

  1. Name the categories. The categories are red, green and blue. Each draw lands in exactly one of them. Check: a ball is always one of the three colours, so no other outcome is possible.
  2. Find each category's probability. Each ball is equally likely to be drawn, so the chance of a colour is its count divided by the total. Red has 5 out of 10, so pred=0.5p_{\text{red}} = 0.5. Green has 3 out of 10, so pgreen=0.3p_{\text{green}} = 0.3. Blue has 2 out of 10, so pblue=0.2p_{\text{blue}} = 0.2. Check: 0.5 + 0.3 + 0.2 = 1.0, so the three probabilities cover every draw.
  3. Encode the outcome as a vector. To feed the result into arithmetic, give each category one slot. Red is (1,0,0)(1, 0, 0), green is (0,1,0)(0, 1, 0) and blue is (0,0,1)(0, 0, 1). Exactly one slot is 1 and the rest are 0. This is the one-hot form of the draw. Check: each vector has exactly one 1, which marks the category that was drawn.
  4. Read a question off the distribution. "What is the chance the ball is not red?" The non-red categories are green and blue, so add their probabilities: 0.3 + 0.2 = 0.5. Check: the answer is the same as 1 minus the probability of red, which is 1 - 0.5 = 0.5.
  5. Repeat the draw. If you put the ball back and draw again, the second draw has the same three probabilities. Over many draws, the share of red draws should drift toward 0.5, the share of green toward 0.3 and the share of blue toward 0.2. Check: after 10 draws, a result of 5 red, 3 green and 2 blue matches the probabilities exactly, but a short run need not match.

The flow below shows the same steps: the categories and their probabilities go in, one category comes out, and the result is written as a one-hot vector.

flowchart LR
  A["Bag: 5 red, 3 green, 2 blue"] --> P["p = (0.5, 0.3, 0.2)"]
  P --> D["Draw one category"]
  D --> R["Red: (1, 0, 0)"]
  D --> G["Green: (0, 1, 0)"]
  D --> B["Blue: (0, 0, 1)"]

The math

Definition

Definition: Categorical distribution

Let KK be a whole number of categories, labelled 1,2,…,K1, 2, \ldots, K. A random variable XX has a categorical distribution with parameters p1,…,pKp_1, \ldots, p_K when P(X=k)=pkP(X = k) = p_k for each category kk, where every pk≥0p_k \ge 0 and ∑k=1Kpk=1\sum_{k=1}^{K} p_k = 1. We write X∼Categorical(p1,…,pK)X \sim \mathrm{Categorical}(p_1, \ldots, p_K), or more compactly X∼Cat(p)X \sim \mathrm{Cat}(\mathbf{p}) with p=(p1,…,pK)\mathbf{p} = (p_1, \ldots, p_K).

The parameters are the probability vector p\mathbf{p}. Once it is known, the whole distribution is known, because the last probability is fixed by the others: pK=1−(p1+⋯+pK−1)p_K = 1 - (p_1 + \cdots + p_{K-1}).

Step 1 in symbols: the categories and their chances

For the bag, K=3K = 3 and the categories are red, green and blue. Let the outcome be XX, with P(X=red)=p1P(X = \text{red}) = p_1, P(X=green)=p2P(X = \text{green}) = p_2 and P(X=blue)=p3P(X = \text{blue}) = p_3. The counts give the parameters directly:

pk=nkN,N=10,n1=5,n2=3,n3=2.p_k = \frac{n_k}{N}, \qquad N = 10, \quad n_1 = 5, \quad n_2 = 3, \quad n_3 = 2.

Here NN is the total number of balls and nkn_k is the number of balls in category kk.

So p=(0.5,0.3,0.2)\mathbf{p} = (0.5, 0.3, 0.2).

Step 2 in symbols: the probabilities add up to 1

Each draw lands in exactly one category, so the categories cover every outcome and the probabilities must sum to 1, written with summation:

∑k=1Kpk=1.\sum_{k=1}^{K} p_k = 1.

For the bag, 0.5+0.3+0.2=10.5 + 0.3 + 0.2 = 1. This is the same check as step 2 of the walkthrough.

Step 3 in symbols: the one-hot vector

Write the outcome as a vector y\mathbf{y} with KK entries, where yk=1y_k = 1 if the draw is category kk and yj=0y_j = 0 for every other jj. For a draw of green, y=(0,1,0)\mathbf{y} = (0, 1, 0). The expected value of this vector is the probability vector itself:

E[yk]=P(X=k)=pk.\mathbb{E}[y_k] = P(X = k) = p_k.

This is why p\mathbf{p} is also described as the mean of the one-hot outcome.

Step 4 in symbols: an event is a sum

For any set of categories AA, the probability that the draw falls in AA is the sum of the entries for those categories:

P(X∈A)=∑k∈Apk.P(X \in A) = \sum_{k \in A} p_k.

For "not red", A={green,blue}A = \{\text{green}, \text{blue}\}, so P(X∈A)=0.3+0.2=0.5P(X \in A) = 0.3 + 0.2 = 0.5. This is the sum from step 4 of the walkthrough, written with a set. The formula is the PMF rule applied to a set of categories.

Step 5 in symbols: repeated draws

If y1,…,yn\mathbf{y}_1, \ldots, \mathbf{y}_n are the one-hot outcomes of nn independent draws from the same categorical distribution, the number of times category kk appears is Nk=∑i=1nyi,kN_k = \sum_{i=1}^{n} y_{i,k}, where yi,ky_{i,k} is the kk-th entry of the ii-th draw. Its expected value is

E[Nk/n]=pk.\mathbb{E}[N_k / n] = p_k.

The share of draws in each category has expected value equal to the probability of that category. This is the average-of-draws version of step 5.

Worked examples

Example
1. A bag with two colours

A bag holds one red ball and one green ball. You draw one ball and XX is its colour.

  1. The categories are red and green, each reached by one ball.
  2. Each ball is equally likely, so pred=1/2p_{\text{red}} = 1/2 and pgreen=1/2p_{\text{green}} = 1/2.
  3. Check: 1/2+1/2=11/2 + 1/2 = 1, so the distribution is valid.
  4. The chance of red is 1/21/2, the same as a fair coin, which shows that a categorical distribution with two categories is a Bernoulli distribution in disguise.
  5. Interpretation: with two categories the model is a coin flip, with one number, p=1/2p = 1/2, doing all the work.
Example
2. A classifier's three labels

A model looks at a photo and outputs probabilities for three labels: cat, dog and bird. The output is p=(0.7,0.2,0.1)\mathbf{p} = (0.7, 0.2, 0.1).

  1. Check that the output is valid. Every entry is between 0 and 1, and 0.7+0.2+0.1=1.00.7 + 0.2 + 0.1 = 1.0.
  2. The predicted label is the one with the largest probability, which is cat with p=0.7p = 0.7.
  3. The chance that the label is cat or dog is P(X∈{cat,dog})=0.7+0.2=0.9P(X \in \{\text{cat}, \text{dog}\}) = 0.7 + 0.2 = 0.9.
  4. The chance that the photo is not a cat is 1−0.7=0.31 - 0.7 = 0.3, which matches 0.2+0.1=0.30.2 + 0.1 = 0.3.
  5. Suppose the true label is dog. The one-hot vector is (0,1,0)(0, 1, 0). The model gave the true label probability 0.2. A lower number here means the model was more surprised by the correct answer, and training pushes this number up.
  6. Interpretation: a categorical output does more than name a label. It also says how sure the model is, and 0.7 is a much weaker claim than 0.99.
Example
3. Satisfaction survey with a score

A survey gives four options with a score for each: very satisfied (4), satisfied (3), neutral (2) and dissatisfied (1). A respondent's answer follows the categorical distribution p=(0.40,0.30,0.20,0.10)\mathbf{p} = (0.40, 0.30, 0.20, 0.10) over those four options.

  1. Check that the distribution is valid: 0.40+0.30+0.20+0.10=1.000.40 + 0.30 + 0.20 + 0.10 = 1.00.
  2. The chance of being at least satisfied is P(X∈{very satisfied,satisfied})=0.40+0.30=0.70P(X \in \{\text{very satisfied}, \text{satisfied}\}) = 0.40 + 0.30 = 0.70.
  3. The average score weights each score by its probability: E[score]=4(0.40)+3(0.30)+2(0.20)+1(0.10)\mathbb{E}[\text{score}] = 4(0.40) + 3(0.30) + 2(0.20) + 1(0.10).
  4. Compute each term: 1.60, then 0.90, then 0.40, then 0.10.
  5. Add them: 1.60+0.90+0.40+0.10=3.001.60 + 0.90 + 0.40 + 0.10 = 3.00.
  6. Check: the average must lie between the lowest score, 1, and the highest, 4. Three is in that range.
  7. Interpretation: 3.00 is the centre of the distribution. No single respondent scores exactly 3.00, but the average summarises where most of the probability sits.

Try it: using the same distribution, what is the chance that a respondent is neutral or worse? Answer: P(X∈{neutral,dissatisfied})=0.20+0.10=0.30P(X \in \{\text{neutral}, \text{dissatisfied}\}) = 0.20 + 0.10 = 0.30.

Common mistakes and tradeoffs

Mistake 1: "The categories are numbers, so their average is meaningful." It is tempting to read the labels 1, 2 and 3 as a scale and take an average over them. In fact the category labels are only names unless the categories have a natural order and a score attached, as the survey does. Labels such as cat, dog and bird have no order, so averaging them is meaningless. The expected value is only useful when you attach values to the categories yourself.

Mistake 2: "A categorical distribution with two categories is a different model from a coin flip." It is tempting to treat a yes-or-no choice as separate from a choice among many. In fact with two categories the categorical distribution is exactly the Bernoulli distribution, with one probability and its complement. The categorical view is the general form, and the Bernoulli is its special case.

Mistake 3: "The most likely category is the only one that matters." It is tempting to keep only the top label and drop the rest. In fact the other probabilities carry information about how confident the model is, and they are needed for losses, sampling and calibration. A top label of 0.4 out of four options leaves 0.6 for the other three, so it is a much weaker claim than a top label of 0.9 among the same four.

When to use it. Use a categorical distribution when one outcome must fall in exactly one of several named categories, and the categories are fixed and listed in advance. This is the standard output of a classifier and the standard model for a choice among options.

When not to use it. If the outcome is a count of how many items fall in each category over many draws, the right model is the multinomial distribution instead, which counts how many draws land in each category rather than describing one draw. If the categories are ordered and the gaps between them matter, a numeric model such as a uniform or a continuous one may fit better.

Alternatives. A binomial distribution covers the case of two categories and a count of successes. A Softmax layer is the usual way to turn arbitrary scores into the probability vector of a categorical distribution.

History

Probability began as a theory of games of chance, where the question was how to split the stakes of an unfinished game fairly by counting outcomes. That counting habit is the root of assigning a probability to each possible result. Jacob Bernoulli's work on repeated two-outcome trials, published after his death, gave the Bernoulli distribution its name. The step from two outcomes to many came from the same counting tradition. Modern machine-learning texts often call the categorical distribution the multinoulli distribution, a name that makes the link to the Bernoulli explicit. The categorical form became central once models had to produce a choice among many labels, such as the next word of a sentence.

Recap

  • A categorical distribution describes one draw from a fixed set of named categories, with one probability per category.
  • The probabilities are non-negative and add up to 1; these two checks catch most mistakes.
  • The outcome can be written as a one-hot vector, and its expected value is the probability vector.
  • The chance of any set of categories is the sum of their probabilities.
  • With two categories it is a Bernoulli distribution; with counts over many draws it becomes a multinomial distribution.

Read next: Softmax, which turns arbitrary scores into a categorical distribution, and Cross-Entropy Loss, which scores how well that distribution matches the true label.

Check yourself

QuestionWhat two properties must the probabilities of a categorical distribution have?Show answer

Each probability must be non-negative, and no larger than 1, and the probabilities over all categories must add up to exactly 1. Every draw lands in one category, so the total must be 1.

QuestionA categorical distribution over four categories has p1=0.1p_1 = 0.1, p2=0.4p_2 = 0.4 and p3=0.3p_3 = 0.3. What is p4p_4, and what is P(X≥2)P(X \ge 2) if the categories are numbered 1 to 4?Show answer

The probabilities must sum to 1, so 0.1+0.4+0.3+p4=10.1 + 0.4 + 0.3 + p_4 = 1, which gives p4=0.2p_4 = 0.2. Then P(X≥2)=p2+p3+p4=0.4+0.3+0.2=0.9P(X \ge 2) = p_2 + p_3 + p_4 = 0.4 + 0.3 + 0.2 = 0.9. A quick check is 1−p1=0.91 - p_1 = 0.9.

QuestionWhy does a categorical outcome get written as a one-hot vector instead of as its category number?Show answer

Category numbers are only labels, and arithmetic on them is misleading when the categories have no order. A one-hot vector gives each category its own slot with a 1 or 0, so arithmetic on the vector, such as taking an average or comparing it with a predicted probability vector, means what it should. Its expected value is exactly the probability vector.

QuestionA model outputs probabilities (0.6,0.3,0.1)(0.6, 0.3, 0.1) for three labels. The true label is the third one. What one-hot vector represents the truth, and what probability did the model give the true label?Show answer

The truth is the third category, so the one-hot vector is (0,0,1)(0, 0, 1). The model gave the third category probability 0.1, which is the probability the model assigned to the correct answer. A small number here means the model was surprised by the correct answer.

QuestionA survey has three options with probabilities (0.5,0.3,0.2)(0.5, 0.3, 0.2), and you ask 100 people independently. Roughly how many pick the first option, and why is the answer only roughly that?Show answer

The expected number is 100×0.5=50100 \times 0.5 = 50, because each person is an independent draw with chance 0.5 for the first option. The answer is only roughly 50 because each sample of 100 people varies by chance, so the count will often differ from its expected value. Over many more people, the share settles closer to 0.5.

Sources