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 gets its own probability , 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.
- 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.
- 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 . Green has 3 out of 10, so . Blue has 2 out of 10, so . Check: 0.5 + 0.3 + 0.2 = 1.0, so the three probabilities cover every draw.
- Encode the outcome as a vector. To feed the result into arithmetic, give each category one slot. Red is , green is and blue is . 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.
- 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.
- 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
Let be a whole number of categories, labelled . A random variable has a categorical distribution with parameters when for each category , where every and . We write , or more compactly with .
The parameters are the probability vector . Once it is known, the whole distribution is known, because the last probability is fixed by the others: .
Step 1 in symbols: the categories and their chances
For the bag, and the categories are red, green and blue. Let the outcome be , with , and . The counts give the parameters directly:
Here is the total number of balls and is the number of balls in category .
So .
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:
For the bag, . 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 with entries, where if the draw is category and for every other . For a draw of green, . The expected value of this vector is the probability vector itself:
This is why 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 , the probability that the draw falls in is the sum of the entries for those categories:
For "not red", , so . 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 are the one-hot outcomes of independent draws from the same categorical distribution, the number of times category appears is , where is the -th entry of the -th draw. Its expected value is
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
A bag holds one red ball and one green ball. You draw one ball and is its colour.
- The categories are red and green, each reached by one ball.
- Each ball is equally likely, so and .
- Check: , so the distribution is valid.
- The chance of red is , the same as a fair coin, which shows that a categorical distribution with two categories is a Bernoulli distribution in disguise.
- Interpretation: with two categories the model is a coin flip, with one number, , doing all the work.
A model looks at a photo and outputs probabilities for three labels: cat, dog and bird. The output is .
- Check that the output is valid. Every entry is between 0 and 1, and .
- The predicted label is the one with the largest probability, which is cat with .
- The chance that the label is cat or dog is .
- The chance that the photo is not a cat is , which matches .
- Suppose the true label is dog. The one-hot vector is . 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.
- 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.
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 over those four options.
- Check that the distribution is valid: .
- The chance of being at least satisfied is .
- The average score weights each score by its probability: .
- Compute each term: 1.60, then 0.90, then 0.40, then 0.10.
- Add them: .
- Check: the average must lie between the lowest score, 1, and the highest, 4. Three is in that range.
- 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: .
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 , and . What is , and what is if the categories are numbered 1 to 4?Show answer
The probabilities must sum to 1, so , which gives . Then . A quick check is .
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 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 . 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 , 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 , 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.