0009 — Random Variables, Expectation & Distributions

Probability Pillar Ā· ~25 minutes

🧩 Puzzle Piece: The output of a transformer is a categorical random variable — it takes one of ~50,000 values (tokens), each with a specific probability. The model doesn't just say "the next word is X." It says "the next word is a random draw from this distribution." Understanding random variables and distributions is understanding what a transformer actually outputs.
"The next token" is not a fixed thing — it's uncertain. A random variable is the mathematical object that captures this uncertainty: a variable whose value is determined by chance, governed by a probability distribution. Every AI output is a random variable.

Random Variables

A random variable X is a function from the sample space Ī© to real numbers. For language models, X maps each possible next token to... well, the token itself (or its ID). The key idea: X has a distribution — a description of how probable each possible value is.

Expectation: The Weighted Average

The expected value E[X] is the probability-weighted average of all possible values:

E[X] = Ī£ x Ā· P(X = x)  (discrete)   E[X] = ∫ x Ā· f(x) dx  (continuous)

Think of expectation as the center of mass of the probability distribution. If you placed weights equal to probabilities at each possible value, the expectation is where the distribution would balance.

Loss functions are expectations. When training a model, the loss is E[āˆ’log P(correct_token | context)] — the expected negative log probability of the correct answer, averaged over the training data. Minimizing the loss means maximizing the expected log probability the model assigns to correct answers.

Key Distributions for AI

Distribution What It Models Where in AI
Categorical One of K categories Next-token prediction (K = vocab size)
Bernoulli Yes/no (success/failure) Binary classification, dropout masks
Gaussian (Normal) Continuous values around a mean Weight initialization, noise, latent spaces (VAEs, diffusion)
Multinomial Counts across K categories Bag-of-words, topic models

The categorical distribution is the most important for transformers: it's a probability vector of length K (the vocabulary size), where each entry pi ≄ 0 and Ī£pi = 1. This is exactly what softmax produces.

Continuous Distributions

For continuous random variables, probability is described by a probability density function (PDF) f(x). The probability of falling in an interval [a,b] is āˆ«ā‚įµ‡ f(x) dx. This uses your calculus background — integration as "area under the curve."

The Gaussian (normal) distribution N(μ, σ²) is the most important continuous distribution in AI:

f(x) = (1/√(2Ļ€ĻƒĀ²)) Ā· exp(āˆ’(xāˆ’Ī¼)²/(2σ²))

It appears in weight initialization (Kaiming/Xavier), variational autoencoders, diffusion models, and as the limiting distribution of sums (Central Limit Theorem). The bell curve is everywhere.

Check Your Understanding

Q1: A fair die has E[roll] = 3.5. What does 3.5 represent, given that you can never actually roll a 3.5?

A. The most likely outcome B. The long-run average if you rolled infinitely many times C. The median outcome D. An error in the calculation

Q2: A transformer's output for the next token is best described as what?

A. A single token ID B. A categorical random variable — a distribution over the vocabulary C. A Gaussian random variable D. A deterministic function of the input

Q3: When people say "the model was trained to minimize the loss," what mathematical object is the loss?

A. The variance of the model's outputs B. The expected negative log probability of the correct answers — an expectation over the training data C. The number of incorrect predictions D. The determinant of the weight matrix
šŸ“ŗ Primary Source Watch: Blitzstein, Statistics 110 — Lecture 5: Discrete Random Variables
šŸ“– Recommended Reading Blitzstein & Hwang, Introduction to Probability — Chapters 3–5 (Random Variables and Distributions)
The categorical distribution is the native output format of every language model. When you look at token probabilities in a chat interface, you're looking at a categorical distribution with ~50,000 categories. Every entry is a probability, and they sum to 1. That's not an implementation detail — that's what the model IS.
← 0008: Conditional Probability & Bayes Next: 0010 — Entropy & Cross-Entropy →