Module 03 · Part I · Foundations

Probability and Information

Random Variables, Expectation and Variance, Conditional Probability, Maximum Likelihood, Entropy, Cross-Entropy, KL Divergence, and the Sigmoid

By the end you can use the rules for expectation and variance, read conditional probability notation, and explain cross-entropy, perplexity and KL divergence.

Builds onModule 2·
Check yourself ▸

Before you start, can you already…

  • ☐compute gradients through a small computation graph by hand, and explain why a learning rate can be too large. (Module 2)

If any is shaky, the linked module teaches it.

Open in Colab
4 Hours·Core·Foundational·0/6

A language model's output is a probability distribution, its training objective is a statement about probabilities, and one of its key design choices (section 6.3) rests on a calculation with averages and spreads. This module teaches the probability the course uses, in the order it uses it. Nothing here is specific to transformers, and every later module leans on it.

3.1 Random Variables, Distributions, and SamplingRun this section

A random variable is a quantity whose value is uncertain until you observe it: the face of a die, the next token of a text, the initial value of a weight. Its distribution says how likely each value is.

For a variable with a list of possible values, the distribution gives each value a probability between 0 and 1, summing to 1. A fair die gives each face 16\tfrac{1}{6}; a language model gives one probability to each of its tokens. P(X=x)P(X = x), or just p(x)p(x), is "the probability that XX comes out as xx".

A variable that can take any real value, such as a weight, is described by a density p(x)p(x) instead: the probability of landing in a small interval around xx is about p(x)p(x) times the interval's width, and the total area under pp is 1. The most important density is the normal distribution (the bell curve), written :

p(x)=12πσ2exp⁡(−(x−μ)22σ2)p(x) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)

It is centred on μ\mu with width set by σ\sigma: about 68% of draws land within one σ\sigma of μ\mu, and 95% within two. Weights are usually initialized from a normal distribution with μ=0\mu = 0 and small σ\sigma (section 10.4). reads "xx is drawn from a normal distribution with mean 0 and variance 1".

Sampling draws a value at random according to the distribution. Generating text is sampling, one token at a time (section 12.3).

Two random variables are independent when knowing one tells you nothing about the other: two dice rolls are, the first and second tokens of a sentence are not.

3.2 Expectation and VarianceRun this section

The expectation is the average value of XX, weighting each value by its probability. It is also called the mean:

E[X]=∑xx p(x)\mathbb{E}[X] = \sum_x x \, p(x)

For a fair die, E[X]=16(1+2+3+4+5+6)=3.5\mathbb{E}[X] = \tfrac{1}{6}(1 + 2 + 3 + 4 + 5 + 6) = 3.5. (For a density the sum becomes an integral, and the rules below still hold.) The average of many independent samples settles toward the expectation.

The variance is the average squared distance from the mean. Its square root, the standard deviation σ\sigma, is in the same units as XX:

Var⁡(X)=E[(X−E[X])2]=E[X2]−(E[X])2\operatorname{Var}(X) = \mathbb{E}\left[(X - \mathbb{E}[X])^2\right] = \mathbb{E}[X^2] - \left(\mathbb{E}[X]\right)^2

The second form, "the average of the square minus the square of the average", is usually easier to compute.

The Rules

These rules are used repeatedly, most importantly in section 6.3. Here aa and bb are fixed numbers.

  1. Expectation is linear, always. E[aX+bY]=a E[X]+b E[Y]\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y], whether or not XX and YY are independent.
  2. For independent variables, the expectation of a product is the product of the expectations. E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y].
  3. Scaling a variable scales its variance by the square. Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2 \operatorname{Var}(X): doubling every value quadruples the squared distances. Adding a constant leaves the variance unchanged.
  4. For independent variables, variances add. Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y). So a sum of nn independent variables with variance s2s^2 each has standard deviation sns\sqrt{n}: spread grows like the square root of the number of terms.

By rules 3 and 4, the average of nn independent samples has standard deviation shrinking like 1/n1/\sqrt{n}. A gradient on a random batch is such an average: by rule 1 its expectation is the full-dataset gradient, and its noise shrinks as the batch grows. A quantity whose expectation equals what it estimates is unbiased.

Figure 3.2
The spread of a dot product grows like the square root of the length

Each sample is the dot product of two random vectors whose entries are drawn from N(0, 1). The dashed curve is the normal distribution with variance d that the rules predict.

4,000 samples per setting. The horizontal axis is fixed at ±48 so the widening is visible.
Predict, then try

The figure draws two random vectors of length dd, every entry from N(0,1)\mathcal{N}(0, 1), and records their dot product. Using rules 2 and 4, predict the standard deviation of the dot product at d=64d = 64.

In words. The figure repeatedly draws two random vectors of a chosen length and adds their dot product to a histogram, over a normal curve with variance equal to the length. The measured standard deviation tracks the predicted square root of the length as the length grows.

3.3 Joint and Conditional Probability

The joint probability P(A,B)P(A, B) is the probability that both AA and BB happen. The conditional probability , "the probability of AA given BB", is the share of BB's probability in which AA also happens:

P(A∣B)=P(A,B)P(B)P(A \mid B) = \frac{P(A, B)}{P(B)}

Worked example. In a corpus, the word the makes up 5% of tokens, and the followed by cat makes up 0.01% of adjacent pairs. Then P(cat∣the)=0.0001/0.05=0.002P(\texttt{cat} \mid \texttt{the}) = 0.0001 / 0.05 = 0.002: of all the places a the appears, cat follows in 0.2% of them.

Rearranged, P(A,B)=P(B) P(A∣B)P(A, B) = P(B)\,P(A \mid B). Applied repeatedly, this gives the chain rule of probability: a joint probability is a product of one-at-a-time conditionals.

P(w1,w2,w3)=P(w1) P(w2∣w1) P(w3∣w1,w2)P(w_1, w_2, w_3) = P(w_1)\, P(w_2 \mid w_1)\, P(w_3 \mid w_1, w_2)

This is exact, and it is the foundation of every text-writing model in this course: a model that answers "how likely is the next token, given everything before it?" can multiply its answers into the probability of a whole text (section 8.4).

Independence means P(A∣B)=P(A)P(A \mid B) = P(A), or equivalently P(A,B)=P(A) P(B)P(A, B) = P(A)\,P(B).

3.4 Maximum LikelihoodRun this section

A model with assigns a probability to each possible observation. The of θ\theta is the probability it gives the data actually observed, and maximum likelihood chooses the θ\theta that makes it largest.

For independent observations the likelihood is a product, which underflows in floating point and is awkward to differentiate. The logarithm turns it into a sum, and since the log only increases, maximizing it picks the same θ\theta. Flipping the sign gives the negative log-likelihood (NLL), a loss to minimize:

LNLL(θ)=−∑i=1nlog⁡pθ(xi)\mathcal{L}_{\text{NLL}}(\theta) = -\sum_{i=1}^{n} \log p_\theta(x_i)

Every log of a probability is at most 0, so the loss is positive, and zero only when every observation gets probability 1.

Worked example. A coin with unknown heads-probability pp is flipped 10 times and lands heads 7 times. The likelihood is p7(1−p)3p^7 (1 - p)^3, so the NLL is −7log⁡p−3log⁡(1−p)-7 \log p - 3 \log(1 - p). Its derivative is −7p+31−p-\frac{7}{p} + \frac{3}{1 - p}. Setting it to zero gives 7(1−p)=3p7(1 - p) = 3p, so p=0.7p = 0.7: the maximum-likelihood estimate is the observed frequency.

With many values, value kk seen nkn_k times out of nn gets probability nk/nn_k / n. A language model is fitted by the same principle, but its probabilities come from a neural network, so there is no formula for the best θ\theta and gradient descent (section 2.4) does the minimizing.

Browser lab

Maximum likelihood recovers the observed frequency

Flip a coin with a hidden heads-probability, then try every candidate p from 0.001 to 0.999 and keep the one with the lowest negative log-likelihood. Section 3.4 predicts the winner is exactly the fraction of heads observed, not the hidden true value.

Run it several times at 10 flips and then at 5000. The estimate always equals the observed frequency; how close that frequency is to the hidden truth depends on how much data you have.

3.5 Information: Entropy, Cross-Entropy, KL, PerplexityRun this section

The surprise of an outcome with probability pp is −log⁡p-\log p: 0 when p=1p = 1, growing without limit as pp shrinks. With base-2 logs it is in bits (a fair coin flip carries 1 bit); with natural logs, nats.

The of a distribution is its average surprise: how unpredictable it is.

H(p)=−∑xp(x)log⁡p(x)H(p) = -\sum_x p(x) \log p(x)

A distribution that puts all its probability on one value has entropy 0. A uniform distribution over KK values has the largest possible entropy, log⁡K\log K.

If the true distribution is pp but you predict with qq, your average surprise, using qq's probabilities over what really happens, is the :

H(p,q)=−∑xp(x)log⁡q(x)H(p, q) = -\sum_x p(x) \log q(x)

It is never smaller than H(p)H(p), and equals it only when q=pq = p. The excess is the Kullback–Leibler divergence, a measure of how far qq is from pp:

KL⁡(p ∥ q)=H(p,q)−H(p)=∑xp(x)log⁡p(x)q(x)\operatorname{KL}(p \,\|\, q) = H(p, q) - H(p) = \sum_x p(x) \log \frac{p(x)}{q(x)}

It is never negative, is 0 only when the distributions agree, and is not symmetric: KL⁡(p∥q)\operatorname{KL}(p \| q) and KL⁡(q∥p)\operatorname{KL}(q \| p) usually differ. Section 21.5 uses it to stop a model drifting far from where it started.

Why This Is the Training Loss

For one training example, the "true distribution" over the next token is one-hot: probability 1 on the token cc that actually came next. The cross-entropy collapses to one term:

H(one-hotc,q)=−log⁡q(c)H(\text{one-hot}_c, q) = -\log q(c)

That is the NLL of section 3.4. Minimizing cross-entropy, minimizing NLL and maximizing likelihood are the same thing. This cross-entropy loss trains every classifier and language model in this course.

Perplexity

is exp⁡\exp of the average NLL per token. A perplexity of 20 means the model is as unsure as if choosing uniformly among 20 tokens; 1 is perfect. Section 13.1 explains why perplexities across different tokenizers cannot be compared.

Figure 3.5
Cross-entropy never beats entropy; the gap is the KL divergence

The left bars are the true distribution p. Drag the right bars to change the model q; they rescale so they always add to 1.

All quantities in bits (log base 2).
Predict, then try

Drag the model's distribution qq until it matches the true distribution pp exactly. What will the cross-entropy and the KL divergence read then?

In words. Two bar charts over four outcomes: a fixed true distribution and a draggable model distribution. The readout shows entropy, cross-entropy and KL divergence. Cross-entropy never drops below entropy, and the gap, the KL divergence, closes only when the bars match.

3.6 The Sigmoid and Preferences Between PairsRun this section

The squashes any real score into a probability between 0 and 1:

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

σ(0)=0.5\sigma(0) = 0.5, and σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z). Its slope is σ(z)(1−σ(z))\sigma(z)(1 - \sigma(z)), the "p(1−p)p(1-p)" from Module 2's exercises, because the sigmoid is the two-class softmax: eaea+eb=σ(a−b)\frac{e^a}{e^a + e^b} = \sigma(a - b).

That makes it the natural model for comparisons. Give each item a score rr, and say item ii beats item jj with probability

P(i≻j)=σ(ri−rj)P(i \succ j) = \sigma(r_i - r_j)

Only the difference matters, so adding a constant to every score changes nothing. This is the Bradley–Terry model, long used to rate chess players from match results by maximum likelihood. Section 21.4 fits it to people's judgments of which of two answers is better, and calls the scores .

Exercises

Check

Which of these need the two variables to be independent? (a) E[X+Y]=E[X]+E[Y]\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y]; (b) E[XY]=E[X]E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\mathbb{E}[Y]; (c) Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y).

Apply

Prove that Var⁡(aX)=a2Var⁡(X)\operatorname{Var}(aX) = a^2 \operatorname{Var}(X) from the definition Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.

Apply

XX is a sum of 256 independent terms, each with mean 0 and variance 1. What is its standard deviation?

Apply

Factor P(a,b,c,d)P(a, b, c, d) with the chain rule of probability, in left-to-right order.

Apply

A model gives probability 120\tfrac{1}{20} to every token of a test text. What is its perplexity?

Apply

What is the entropy, in bits, of a distribution that gives probability 12,14,14\tfrac{1}{2}, \tfrac{1}{4}, \tfrac{1}{4} to three outcomes?

Apply

Show that KL⁡(p∥p)=0\operatorname{KL}(p \| p) = 0, and compute σ(2)+σ(−2)\sigma(2) + \sigma(-2).

Extend

Under the Bradley–Terry model, answer A has reward 1.5 and answer B has reward 0.5. What is the predicted probability that A is preferred, and what would it be if both rewards were raised by 10?

Core Academic References

  • Cover & Thomas (2006): Elements of Information Theory (2nd ed.). Wiley.
  • Shannon (1948): A Mathematical Theory of Communication. Bell System Technical Journal, 27: 379–423.
  • Bradley & Terry (1952): Rank Analysis of Incomplete Block Designs: I. The Method of Paired Comparisons. Biometrika, 39(3/4): 324–345.
  • Goodfellow, Bengio & Courville (2016): Deep Learning, chapter 3 (Probability and Information Theory). MIT Press.