Every unit so far gave a crisp answer: this line, this projection, this side of the street. The real world rarely does. It gives maybes — a 70% chance of rain, a test that is right most of the time, a score that is "probably a cat". This unit teaches the language of maybes. You will see that uncertainty has a shape, that the covariance ellipse of Unit 12 was a bell curve all along, that "minimise the squared error" secretly meant "make the data least surprising", and how a machine turns raw scores into honest probabilities — the tools every network after this one is built on.
≈ 120 min read + play16 interactive widgets · 5 in 3D · a classifier you can train20 inline checks🧾 18 proofs, folded away — open "if you want the algebra" when you are ready✍ 14 solved practice problems
It is the monsoon. Two weather forecasters work in your town.
The first one says "rain" every single morning. The second one says things like "70% chance of rain" or "20% chance of rain".
You check the second one's record. On the days she said 70%, it rained on about 7 days out of 10. Now her numbers mean something. A farmer can decide whether to sow today. You can decide whether to carry an umbrella. The first forecaster is never quite wrong, but he is never useful.
Every unit so far gave crisp answers. Solve Ax=b: here is x. Project onto a line: here is the shadow. Unit 13's support vector machine says "+1" or "−1".
But look at how the SVM says it. A point deep inside the blue group gets "+1". A point that only just grazes the street also gets "+1". Same answer, same confident voice. The machine cannot say "probably blue, but I am not sure".
The world is full of such "probably". Will it rain? Is this patient sick? Is this photo a cat? The honest answer is a number between 0 and 1. So we need a language for uncertainty. That language is probability, and this whole unit teaches it.
Start with one word. A random variable is a rule that turns an uncertain outcome into a number. Toss a coin and write 1 for heads, 0 for tails. Roll two dice and write their sum. Measure tomorrow's rainfall in millimetres. Each time, you cannot know the number in advance. But you can say how likely each number is.
Here is a machine that makes a random variable. It is called a Galton board. A bead drops in at the top and meets a peg. It bounces left or right, like a coin toss. Then it meets another peg, and another — twelve rows of them. At the bottom it falls into a slot. The slot number is simply how many times it bounced right.
Where will one bead land? Nobody can say. But drop two thousand, and something surprising happens.
The Galton board: one bead is a surprise, a crowd is a shapeEvery bead bounces left or right at every peg. You cannot predict one bead. You can predict the pile.
Try: Drop 1 bead a few times — each lands somewhere new. Then drop 2000 and watch a bell rise out of the chaos, hugging the dashed outline. Now push the peg bias to the right: the whole bell slides, but it keeps its shape.
—
Why a bell? Count the routes. To reach slot 0 a bead must bounce left twelve times in a row: there is only one such route. To reach the middle slot, 6, it needs six lefts and six rights in any order, and there are 924 different orders. Out of all 212=4096 routes, 924 end in the middle. So about 23% of the beads land there, and almost none at the edges.
No bead knows about bells. Each one just flips a coin at each peg. The shape comes from counting. That is the first big idea of this unit: a single outcome is unpredictable, but the pattern of many outcomes is not. The pattern is called a distribution, and it is the thing we can know.
The realization
P(X=k)=all the beadsbeads in slot k
A random variable X turns an uncertain outcome into a number. You cannot know the next value of X. You can know its distribution: how the chances are spread over the possible values. The shape of the pile is the knowledge.
Pause & predict
A forecaster said "70% chance of rain" on 100 different days. When is she doing her job well?
The road ahead. The unit has four acts.
Uncertainty has a shape (§1–§3): distributions, their balance point and spread, and Bayes' rule for turning evidence around.
The Gaussian (§4–§6): the bell curve, why it keeps appearing, what it looks like in many dimensions (Unit 12's ellipse!), and how a computer draws samples from it.
Learning = the least surprising world (§7–§9): likelihood, fitting by maximum likelihood, and the discovery that least squares was maximum likelihood all along.
Scores into probabilities (§10–§13): softmax, surprise and entropy, cross-entropy and KL, and a classifier that trains itself with the cleanest gradient in machine learning.
In one sentence: A random variable turns an uncertain outcome into a number, and although one outcome cannot be predicted, the distribution of many outcomes can — the shape of the pile is the knowledge.
2
Distributions: where the probability lives
Imagine this
You are playing Ludo with two dice. You need a total of 7 to capture a piece. Your cousin needs a 12.
Who should be more hopeful? A 12 needs double six: one way. A 7 can come as 1+6, 2+5, 3+4, 4+3, 5+2 or 6+1: six ways. You are six times as likely to get your number.
Listing every total with its chance is exactly what a distribution is.
Two dice give 36 equally likely pairs. Count how many pairs make each total:
total
2
3
4
5
6
7
8
9
10
11
12
ways
1
2
3
4
5
6
5
4
3
2
1
chance
1/36
2/36
3/36
4/36
5/36
6/36
5/36
4/36
3/36
2/36
1/36
This list is the probability mass function, or pmf, written p(x)=P(X=x). "Mass" is a good word: think of each total carrying a small weight of probability. All the weights add up to 1, because some total always happens.
The balance point. Put the weights on a see-saw plank, each at its own total. Where must the pivot go for the plank to balance? At 7. That balance point is the expected value, or mean:
E[X]=x∑xp(x)=2⋅361+3⋅362+⋯+12⋅361=7.
For one die, every face has weight 61, and the balance point is 61+2+3+4+5+6=3.5. No die ever shows 3.5. The expected value is not a value you expect to see. It is where the weights balance — the long-run average.
The spread. The mean says where the pile sits. We also want how wide it is. Take each value's distance from the mean, square it, and average with the same weights. That is the variance:
Var[X]=E[(X−μ)2]=x∑(x−μ)2p(x).
One die: Var=1235≈2.917. Two dice: 635≈5.833 — exactly double, because the two dice wobble independently and independent spreads add. The square root, σ=Var, is the standard deviation. It is back in the same units as the totals: about 2.42 for two dice.
Dice on a see-sawEach total sits on the plank with its weight of probability. Drag the gold pivot until the plank is level: that is the mean.
Try: Drag the pivot left and right and watch the plank tip. Find where it levels. Then switch to 3 dice: the pile gets rounder and wider. Press ▶ roll to throw real dice and watch the rough outline of the rolls creep onto the exact bars.
drag the gold pivot
—
When the values are not countable. Now think of a school bus. It is usually about 5 minutes late. What is the chance it is late by exactly 5 minutes — not 5.01, not 4.999, but exactly 5.000000…? There are endlessly many possible times, so the chance of any single one is zero.
So for a quantity that can take any value — a time, a height, a rainfall — we stop asking about single values. We ask about windows: "late by between 4 and 6 minutes". The answer is an area under a curve called the probability density, p(x). The whole area under the curve is 1.
Here is the part that surprises everyone. The height of a density is not a probability. It is probability per minute. If a bus is always within a few seconds of 5 minutes late, all the area is squeezed into a tiny window, and the curve must shoot up to keep the area 1. A bell with a spread of 0.1 minutes peaks at about 3.989 — far above 1, and perfectly legal.
Area, not heightThe chance of a window is the shaded area. The height at a point is only a rate. Squeeze the curve and the peak climbs above 1 while the total area stays 1.
Try: Drag the two window handles to shade "between 4 and 6 minutes". Then slide the spread down to 0.1 and watch the peak shoot past 1 — the readout of total area never moves. Finally press exactly 5: a window of zero width holds zero probability.
drag the window handles
—
Watch out — the trap
"The density at 5 is 3.99, so the chance of 5 is 3.99." No: a chance can never be more than 1. A density height only turns into a chance after you multiply by a width. A height of 3.99 over a window 0.01 minutes wide gives a chance of about 0.04.
Probability lives in a mass for countable outcomes and in an area for smooth ones. The mean is the balance point of that mass. The variance is the average squared distance from it.
Pause & predict
A game pays you the total of two dice in rupees. Over many games, what do you earn per game on average, and what is the spread (variance) of one game's payout?
Pause & predict
A density curve reaches a height of 3.99 at x=5. Is something wrong?
If you want the algebra · 2 proofs, step by step
Prove it · the mean and variance of one die
Claim. A fair die has E[X]=27=3.5 and Var[X]=1235≈2.917. Two independent dice have mean 7 and variance 635.
1
Each face has weight 61:
E[X]=61(1+2+⋯+6)=621=3.5.
2
The squared distances from 3.5 are 6.25,2.25,0.25,0.25,2.25,6.25, which add to 17.5. Average them:
Var[X]=617.5=1235≈2.917.
3
For a sum, means always add: 3.5+3.5=7. Variances add when the two parts are independent (the cross term 2E[(X1−μ1)(X2−μ2)] is zero):
Var[X1+X2]=1235+1235=635≈5.833.∎Check against the table: ∑(x−7)2p(x)=362(25⋅1+16⋅2+9⋅3+4⋅4+1⋅5)=36210=635.
Prove it · the shortcut Var = E[X²] − (E X)²
Claim.Var[X]=E[X2]−μ2, where μ=E[X]. It saves subtracting μ from every value.
1
Open the square inside the average:
E[(X−μ)2]=E[X2−2μX+μ2].
2
An average of a sum is the sum of the averages, and constants come out:
E[X2]−2μE[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2.∎One die: E[X2]=61+4+9+16+25+36=691, so Var=691−449=12182−147=1235 ✓.
In one sentence: A distribution says where the probability lives — as masses that add to 1 or as an area of 1 under a density — and its mean is the balance point while its variance is the average squared distance from it.
3
Bayes: turning evidence around
Imagine this
It is dengue season. You feel a little tired, and the clinic runs a quick blood test. It comes back positive.
The doctor says the test catches 99% of people who have dengue, and only 5% of healthy people get a false alarm. Your heart sinks: surely you are 95% likely to be sick?
Not even close. Once you count properly, the chance is about 17%. This section shows why, and it is one of the most useful ideas in this course.
Say 1% of the people who come in actually have dengue. Picture 10 000 people walking into the clinic.
100 of them are sick. The test catches 99% of them: 99 test positive.
9 900 are healthy. 5% get a false alarm: 495 test positive.
Now you are one of the positives. There are 99+495=594 positives, and only 99 of them are sick. Your chance of being sick is
59499=61≈0.167.
What went wrong with "95%"? The healthy crowd is 99 times bigger than the sick crowd. Even a small false-alarm rate on a huge crowd makes more alarms (495) than the real cases (99). The test is good. It is the starting chance that is small.
Now the same thing in symbols. Read the bar "∣" as "given". P(+∣sick) is "the chance of a positive, given that you are sick" — that is the 99%. We want the reverse, P(sick∣+). Bayes' rule turns the question around:
Each piece has a name. P(sick)=0.01 is the prior: where you stood before the evidence. P(+∣sick) is the likelihood: how well "sick" explains the evidence. The answer P(sick∣+) is the posterior: where you stand after. The bottom line P(+) just makes the answers add up to 1.
A second test. Suppose you take another, independent test and it is positive again. Today's posterior becomes tomorrow's prior. Start from 61:
0.99×61+0.05×650.99×61≈0.798.
Two positives take you from 1% to about 80%. Evidence adds up — but it always starts from where you stood.
Ten thousand people walk into a clinicEvery square is a person. Orange = sick, bright = tested positive. The zoom on the right shows only the positives: your chance is their orange share.
Try: Look at the zoom: most of the positives are grey (healthy). Now slide the false-alarm rate down to 1% and watch the posterior jump. Then reset and press test again: the positives become the new crowd, and the answer climbs to about 0.8.
—
Watch out — the flip that fools doctors
P(+∣sick) and P(sick∣+) sound alike but are different numbers: 0.99 and 0.167 here. "Most sick people test positive" does not mean "most people who test positive are sick". To go from one to the other you must bring in the prior.
Bayes' rule turns "how likely is the evidence, if the idea is true?" into "how likely is the idea, now that I have seen the evidence?". The prior is where you stood before. The posterior is where you stand after, and it becomes the prior for the next piece of evidence.
Pause & predict
Same clinic: 1% are sick, the test catches 99% of them, and gives a false alarm to 5% of the healthy. Out of 10 000 people, how many test positive, and what share of those are sick?
Pause & predict
After one positive test your chance is 61. A second, independent test is also positive. What do you use as the prior for the second update?
If you want the algebra · 2 proofs, step by step
Prove it · Bayes' rule from one overlap counted two ways
Claim.P(H∣E)=P(E)P(E∣H)P(H), with P(E)=P(E∣H)P(H)+P(E∣not H)P(not H).
1
"Given E" means: keep only the cases where E happened, and ask what share of them also have H:
P(H∣E)=P(E)P(H and E).In the clinic: of the 594 positives, the 99 who are also sick.
2
The same overlap can be counted from the other side — first pick the H cases, then the share of them with E:
P(H and E)=P(E∣H)P(H).100 sick people × 99% = 99.
3
Put step 2 into step 1. For the bottom, every E case either has H or does not:
P(H∣E)P(E)=P(E)P(E∣H)P(H),=P(E∣H)P(H)+P(E∣not H)×P(not H).∎Clinic: 0.99⋅0.01+0.05⋅0.990.99⋅0.01=0.05940.0099=61.
Prove it · evidence multiplies the odds
Claim. In odds form, each independent test multiplies the odds of "sick" by the same factor P(+∣healthy)P(+∣sick)=0.050.99=19.8.
1
Write Bayes for "sick" and for "healthy" and divide; the bottom P(+) cancels:
P(healthy∣+)P(sick∣+)=P(+∣healthy)P(+∣sick)×P(healthy)P(sick).
2
Start at odds 1:99. One positive: 19.8:99=1:5, which is a chance of 61. A second positive multiplies again: 19.8:5=3.96:1, a chance of 4.963.96≈0.798.
∎The prior sets the starting odds; each piece of evidence multiplies them. That is why one test cannot overcome a very small prior, and two can.
In one sentence: Bayes' rule turns the evidence around — posterior ∝ likelihood × prior — and because the prior is where you stood before the evidence, a rare disease stays fairly unlikely even after a good test says yes.
4
The bell curve, and why it keeps showing up
Imagine this
Line up all the students of a big school by height. Most are somewhere in the middle. A few are very tall, a few are very short, and nobody is three metres tall.
Draw how many students have each height and you get a hill with gentle slopes on both sides: a bell.
Marks in a board result, errors when you weigh rice on a shop scale, the time a train is late — the same bell shows up again and again. Why?
Think about what makes your height. Hundreds of small things: many genes, food at age five, sleep, a growth spurt in class 8. Each one nudges you a little up or a little down, and mostly independently of the others.
That is the Galton board again. Each peg is one small nudge left or right, and the pile at the bottom is a bell. The same thing happens whenever many small, independent nudges add up. It does not matter what each nudge looks like. Their sum comes out bell-shaped. This fact has a grand name, the central limit theorem, and it is why the bell is everywhere.
The bell has a formula. It looks scary, so let us read it aloud, one step at a time:
p(x)=σ2π1exp(−21(σx−μ)2)
Reading the bell, left to right inside the bracket:
x−μ: how far x is from the middle μ.
σx−μ: that distance, measured in σ's. This number is called the z-score.
Square it: left or right does not matter, only how far.
exp(−21⋅): at the middle this is 1; far away it dies off very fast.
σ2π1: a fixed number that makes the total area exactly 1.
Two numbers set the whole bell: the middle μ and the spread σ. We write it N(μ,σ2).
Because the formula only cares about the z-score, every bell is the same bell, stretched and shifted. So one rule works for all of them, the 68–95–99.7 rule: about 68% of the area lies within 1σ of the middle, about 95% within 2σ, and about 99.7% within 3σ.
Example. In a class, heights have μ=160 cm and σ=8 cm. A student 172 cm tall has z=8172−160=1.5: one and a half spreads above the middle. About 95% of the class is between 160−16=144 cm and 160+16=176 cm.
The bell, measured in σ'sShade ±1σ, ±2σ or ±3σ and read the area. Drag the probe to read its z-score and the height of the curve there.
Try: Press ±2σ: 95.4% of the area, whatever μ and σ are. Change σ and check — the shaded share never moves. Then drag the probe to 172 cm and read its z-score.
drag the probe
—
Now watch the central limit theorem happen. Take a lopsided die: it lands on 1 very often (40%), on 6 often (30%), and rarely on the middle faces. Its pmf looks nothing like a bell — it is a U. Roll it n times and take the average. The widget below draws the exact distribution of that average.
A lopsided die becomes a bellThe exact distribution of the average of n rolls, with the matching bell drawn over it. At n = 1 it is a U. By n ≈ 10 it is a bell.
Try: Press ▶ add rolls and watch the U fill in, turn lumpy, then smooth into the bell. Notice the bell also gets narrower as n grows: averages wobble less than single rolls.
—
The realization
N(μ,σ2):p(x)=σ2π1e−z2/2,z=σx−μ
The bell only asks one question: how many σ's from the middle are you? That is why 68–95–99.7 holds for every bell. And it appears whenever many small independent nudges add up — the average of n rolls has spread σ/n and a bell's shape.
Pause & predict
Board marks in a subject follow a bell with μ=60 and σ=10. About what share of students scored between 40 and 80?
Pause & predict
A die is badly lopsided — its own pmf is U-shaped. What does the distribution of the average of 30 rolls look like?
In one sentence: The bell N(μ,σ2) measures everything in σ's from the middle, so 68–95–99.7 holds for every bell, and it shows up whenever many small independent nudges add up.
5
The Gaussian in many dimensions
Imagine this
Now measure two things for every student: height and weight. Plot each student as a dot.
The dots do not make a round blob. Tall students tend to be heavier, so the cloud is a tilted oval. A 180 cm student who weighs 75 kg is ordinary. A 150 cm student who weighs 75 kg is unusual — even though both are the same "distance" from the average in plain centimetres and kilograms.
We need a bell that knows the two measurements move together.
In two dimensions the middle becomes a mean vectorμ, and the spread becomes the covariance matrixΣ — the same matrix you built in Unit 12. Its diagonal holds each measurement's own variance. Its off-diagonal says how much they move together.
The formula is the 1-D bell with one change. Instead of "distance divided by σ, squared", we use "distance measured with Σ−1":
p(x)=2πdetΣ1×exp(−21(x−μ)⊤Σ−1(x−μ)).
The number d2=(x−μ)⊤Σ−1(x−μ) is a squared distance measured in the data's own ruler. It is called the Mahalanobis distance (after P. C. Mahalanobis, who founded the Indian Statistical Institute). Along the long axis of the cloud the ruler is long, so points count as close. Across the short axis the ruler is short, so the same step counts as far.
The picture. Where is the density equal to some fixed value? Where d2 is fixed. And x⊤Σ−1x=constant is an ellipse — the covariance ellipse of Unit 12. It was a contour of a probability mountain all along. Its axes point along the eigenvectors of Σ (Unit 4), and their lengths grow like λ.
Worked example. Take μ=0 and
Σ=[2112].
Its eigenvalues are 3 and 1, with eigenvectors (1,1)/2 and (1,−1)/2. So the ellipse is tilted at 45°, and the long axis is 3≈1.73 times the short one. detΣ=4−1=3, so the peak of the mountain is 2π31≈0.0919.
Now two points, both at ordinary distance 2≈1.414 from the middle: (1,1) and (1,−1). Using Σ−1=31[2−1−12]: the point (1,1) has d2=32, so d≈0.816. The point (1,−1) has d2=2, so d≈1.414. The first lies along the long axis — ordinary. The second cuts across the short axis — unusual.
Two special cases. If the off-diagonal is zero (correlation ρ=0), the ellipse's axes line up with the coordinate axes, and if also the two spreads are equal, it is a circle. Then the mountain is simply one bell in x1 times one bell in x2: the two measurements are independent. And whatever Σ is, every straight vertical slice through the mountain is itself a 1-D bell.
The probability mountain and its ellipseHeight = density. The floor shows its contour ellipses and the two eigen-axes. Drag the white probe: the plain distance and the Mahalanobis distance disagree.
Try: Press Σ = [[2,1],[1,2]], then drag the probe to (1, 1) and to (1, −1): the same plain distance, very different Mahalanobis distances. Turn ρ to 0 and the ellipse snaps to the axes. Switch on slice — every slice is a bell. Drag the gold mean to move the whole mountain.
drag the probe or the gold mean · drag elsewhere to orbit
—
The realization
p(x)∝exp(−21d2),d2=(x−μ)⊤Σ−1(x−μ)
The many-dimensional bell is the 1-D bell with the distance measured in the data's own ruler. Its contours are ellipses — Unit 12's covariance ellipse was a Gaussian's contour — with axes along the eigenvectors of Σ and lengths ∝λ.
Pause & predict
With μ=0 and Σ=[2112], which point is more unusual: (1,1) or (1,−1)?
Pause & predict
The correlation is ρ=0 but σ1=2 and σ2=1. What do the contours look like?
If you want the algebra · 2 proofs, step by step
Prove it · the contours are ellipses along the eigenvectors
Claim. For a symmetric positive-definite Σ=QΛQ⊤, the set x⊤Σ−1x=c2 is an ellipse whose axes point along the columns of Q (the eigenvectors), with half-lengths cλ1 and cλ2.
1
The spectral theorem (Unit 4) gives Σ−1=QΛ−1Q⊤. Turn to the eigenvector axes: u=Q⊤x. Then
x⊤Σ−1x=u⊤Λ−1u=λ1u12+λ2u22.Q⊤ is a rotation: it only turns the picture, it does not bend it.
2
Setting this to c2 gives
(cλ1)2u12+(cλ2)2u22=1,
the textbook ellipse with half-axes cλ1 and cλ2 along the u1,u2 directions — the eigenvectors.
∎For Σ=[2112]: half-axes c3 along (1,1)/2 and c along (1,−1)/2.
Prove it · the height of the peak
Claim. The 2-D Gaussian peaks at 2πdetΣ1. For detΣ=3 that is ≈0.0919.
1
In the eigen-coordinates of the previous proof, the density splits into two independent 1-D bells with variances λ1 and λ2:
p=2πλ1e−u12/2λ1⋅2πλ2e−u22/2λ2.A rotation does not change areas, so this still integrates to 1.
2
At the middle both exponentials are 1, and λ1λ2=detΣ (Unit 4: the determinant is the product of the eigenvalues):
pmax=2πλ1λ21=2πdetΣ1=2π31≈0.0919.∎A bigger determinant means a cloud spread over more area, so the same total volume 1 makes a lower mountain.
In one sentence: A many-dimensional Gaussian is a bell whose distance is measured with Σ−1, so its contours are ellipses along the eigenvectors of Σ — the covariance ellipse of Unit 12 is a contour of this mountain.
6
Making Gaussian data: stretch a round cloud
Imagine this
You are making a roti. The dough starts as a round ball. Roll it one way and it stretches into an oval. Push it at an angle and the oval tilts.
Every grain of flour moves with the dough. Nothing is added or taken away — the round shape is simply stretched and sheared.
A computer makes tilted Gaussian clouds exactly like this.
Computers are good at one kind of randomness: the round cloud, N(0,I). Each coordinate is an independent standard bell, and the cloud looks the same in every direction.
To get a cloud with mean μ and covariance Σ, take a round point z and push it through a matrix:
x=μ+Lz,where LL⊤=Σ.
Why does this work? Stretching a cloud by L turns its covariance I into LIL⊤=LL⊤. So we need a "square root" of Σ. The Cholesky factor from Unit 4 is exactly that: a lower-triangular L with LL⊤=Σ.
Worked example. For
Σ=[4222],L=[2101].
Check: LL⊤=[2⋅21⋅22⋅11+1]=[4222] ✓. Read L as a recipe: x1=2z1 (stretch the first coordinate by 2), and x2=z1+z2 (let the second one lean on the first). The lean is what tilts the cloud.
Hold on to this trick. In Unit 20 a picture-making network will need to sample from a Gaussian and still learn through the sampling. It will write x=μ+Lz and call it the reparameterisation trick.
Stretch a round cloud into any ellipse400 round points z. Each one is pushed to x = Lz. The dashed ellipse is where Σ = LLᵀ says the cloud should be; the readout compares the cloud's own covariance with Σ.
Try: Press ▶ stretch to watch the round cloud become the tilted one. Then set ℓ21=0: the lean is gone and the ellipse stands straight. Switch to set Σ and type in a covariance — L is computed for you.
—
The realization
z∼N(0,I)⟹μ+Lz∼N(μ,LL⊤)
Every Gaussian is a round cloud, stretched. To sample from N(μ,Σ), find a square root L of Σ (Cholesky), stretch round samples by it, and shift them by μ.
Pause & predict
You want samples with covariance Σ=[9332]. Which L should stretch the round cloud?
If you want the algebra · 2 proofs, step by step
Prove it · stretching by L makes covariance LLᵀ
Claim. If z has mean 0 and covariance I, then x=μ+Lz has mean μ and covariance LL⊤.
1
Mean: E[x]=μ+LE[z]=μ.
2
Covariance is the average of (deviation)(deviation)⊤. The deviation is x−μ=Lz:
Cov[x]=E[Lz(Lz)⊤]=LE[zz⊤]L⊤=LIL⊤=LL⊤.∎L is a fixed matrix, so it moves outside the average. And a sum of Gaussians is Gaussian, so x is not just any cloud with this covariance — it is exactly N(μ,LL⊤).
Prove it · Cholesky for a 2 × 2 covariance
Claim.[abbc]=LL⊤ with L=[ℓ11ℓ210ℓ22], ℓ11=a, ℓ21=b/ℓ11, ℓ22=c−ℓ212.
Match entry by entry, top-left first: ℓ112=a, then ℓ11ℓ21=b, then ℓ212+ℓ222=c. Each equation has one new unknown.
∎Σ=[4222]: ℓ11=2, ℓ21=1, ℓ22=2−1=1. The square root inside ℓ22 is real exactly when ac>b2 — when Σ is positive definite.
In one sentence: To draw from N(μ,Σ), stretch a round cloud by a square root L of Σ and shift it — x=μ+Lz has covariance LL⊤=Σ.
7
Likelihood: turning the question around
Imagine this
Before a gully-cricket match, your friend always tosses his own lucky coin — and he always seems to win. You borrow it and toss it 10 times. It shows 7 heads.
Is the coin fair? You cannot open it up and look. All you have is what you saw. So you ask a different question: which coin would most easily have produced 7 heads in 10?
Say the coin shows heads with chance p. The chance of one particular run with 7 heads and 3 tails is
p7(1−p)3.
This one formula can be read in two opposite ways.
Probability fixes the coin and asks about the data: "if p=0.5, how likely are 7 heads?"
Likelihood fixes the data and asks about the coin: "given 7 heads, how well does each p explain it?"
Written as a function of the coin, it is the likelihoodL(p)=p7(1−p)3. Try a few coins:
coin p
0.3
0.5 (fair)
0.7
0.9
L(p)=p7(1−p)3
0.0000750
0.000977
0.00222
0.000478
The fair coin explains your 7 heads less than half as well as a coin with p=0.7. The best explanation of all is p=0.7=107 — the share of heads you saw. Choosing the parameter that makes the data most likely is called maximum likelihood, and the winner is written p^.
Why we take logs. Look at the numbers: 0.00222 for just ten tosses. For a thousand tosses the likelihood is around 10−265, smaller than a computer can store. The fix is to take the logarithm:
logL(p)=7logp+3log(1−p).
Two good things happen. A product of many tiny numbers becomes a sum of ordinary numbers. And because log only ever goes up, the peak stays in the same place. So we always maximise the log-likelihood instead.
More data, more confidence. With 70 heads in 100 tosses the best coin is still 0.7, but the likelihood curve becomes a sharp spike. Coins like 0.6 or 0.8 now explain the data far worse. The width of the peak tells you how sure the data lets you be.
Which coin explains the tosses?Left: the likelihood L(p). Right: its log. Both peak at the same p — the share of heads.
Try: Drag the white marker across both panels and read how well each coin explains 7 heads in 10. Then press 70 of 100: same peak, much sharper. Press ▶ toss 10 more a few times with the mystery coin and watch the peak settle.
drag the marker
—
The realization
L(θ)=P(data∣θ),θ^=argθmaxL(θ)=argθmaxlogL(θ)
Probability goes from the model to the data. Likelihood goes back from the data to the model. Learning by maximum likelihood means choosing the model under which what you saw is least surprising. For a coin, that is simply the share of heads.
Pause & predict
A coin shows 3 heads in 12 tosses. What is the maximum-likelihood coin, and does taking the log of the likelihood move it?
If you want the algebra · 1 proof, step by step
Prove it · the best coin is the share of heads
Claim. With h heads in n tosses, logL(p)=hlogp+(n−h)log(1−p) is largest at p^=h/n.
1
At a peak the slope is zero. Differentiate:
dpdlogL=ph−1−pn−h.
2
Set it to zero and cross-multiply:
h(1−p)=(n−h)p⇒h=np⇒p^=nh.
3
It is a maximum, not a minimum: the second derivative −p2h−(1−p)2n−h is negative everywhere.
∎At p^ the second derivative is −p^(1−p^)n: it grows with n. A more curved peak is a sharper peak — more tosses, more confidence.
In one sentence: Likelihood reads P(data∣θ) as a function of the model, maximum likelihood picks the model that makes the data least surprising, and taking logs turns tiny products into sums without moving the peak.
8
Fitting a Gaussian by maximum likelihood
Imagine this
You time how long the chai stall near your college takes to serve a glass, on eight mornings: 2, 4, 4, 4, 5, 5, 7 and 9 minutes.
You want one bell curve that describes this stall. Where should its middle be, and how wide should it be? Try a few bells and ask of each: how well does it explain these eight mornings?
A bell N(μ,σ2) gives each data point a height. The likelihood of the bell is the product of the eight heights. Picture three bad bells:
Too narrow. The bell is a tall spike at 5. The points 2 and 9 sit far out on its tails, with heights almost zero. One near-zero factor ruins the whole product.
Too wide. Every point is covered, but the bell is low everywhere, so every factor is small.
Off-centre. Centred at 3, the points 7 and 9 are pushed onto a tail.
The best bell sits in the middle and is exactly as wide as the data. Maximum likelihood gives a clean answer (the drawer shows why):
μ^=N1i∑xi,σ^2=N1i∑(xi−μ^)2.
For the chai stall: μ^=82+4+4+4+5+5+7+9=840=5 minutes. The squared distances from 5 are 9,1,1,1,0,0,4,16, which add to 32, so σ^2=832=4 and σ^=2 minutes.
Maximum likelihood gives back the two most ordinary summaries there are: the average and the average squared spread. Nothing mysterious — but now we know why they are the right summaries for a bell.
A small catch. Many calculators divide by N−1=7 instead, giving 732≈4.571. Why? The spread is measured around μ^, which was chosen to sit as close to these very points as possible. Measured from their own centre, the points look a little tighter than they really are. Dividing by N−1 corrects that. It is the same "divide by N or N−1" note you met in Unit 12. For large N the difference vanishes.
The likelihood landscape over every possible bellEach spot on the floor is one bell (μ, σ); the height is its log-likelihood for the eight chai times. The panel shows that bell over the data.
Try: Press too narrow, too wide and off-centre and watch the bell on the right fail in three different ways, low on the landscape each time. Then press ▶ climb: a ball rolls uphill (gradient ascent, Unit 9) and stops at the peak, (5,2).
drag the ball · drag elsewhere to orbit
—
The realization
μ^=xˉ,σ^2=N1i∑(xi−xˉ)2
The bell that makes your data least surprising sits at the average and is as wide as the average squared spread. Fitting a model is climbing a likelihood landscape to its peak.
Pause & predict
Five measurements: 1, 3, 4, 6, 6. What are the maximum-likelihood μ^ and σ^2?
If you want the algebra · 2 proofs, step by step
Prove it · the best middle is the average
Claim. For data x1,…,xN and a bell N(μ,σ2), the log-likelihood is largest over μ at μ^=xˉ.
1
Take the log of the product of N bell heights:
ℓ(μ,σ)=−Nlogσ−2σ21i∑(xi−μ)2−2Nlog2π.
2
Only the middle term has μ. Its slope is
∂μ∂ℓ=σ21i∑(xi−μ)=0⇒μ^=N1i∑xi.∎Whatever σ is, the best middle is the average. The chai times give μ^=5.
Prove it · the best spread divides by N
Claim. With μ=μ^, the log-likelihood is largest at σ^2=N1∑i(xi−μ^)2.
1
Write S=∑i(xi−μ^)2. Then ℓ=−Nlogσ−2σ2S+const, and
∂σ∂ℓ=−σN+σ3S.
2
Set to zero: Nσ2=S, so σ^2=S/N.
∎Chai times: S=32, N=8, σ^2=4. The two terms pull against each other: −Nlogσ wants a narrow bell (tall peaks), −S/2σ2 wants a wide one (no point on a tail). The balance is at the data's own spread.
In one sentence: Maximum likelihood fits a bell by placing it at the average and making it as wide as the average squared spread — a climb to the top of the likelihood landscape, with a small downward bias that dividing by N−1 corrects.
9
Least squares falls out
Imagine this
An auto fare should be a fixed start plus a rate per kilometre. But real fares wobble: traffic, a detour, rounding to the nearest ten.
You note down four rides and want the best straight line through them. In Unit 3 you would have said "minimise the squared errors" and projected. But why squared? Why not the plain size of the errors, or their cubes?
Probability gives the answer — and it is a beautiful one.
Say what you believe about the wobble. The simplest belief: the true fare lies on the line, plus a random nudge from a bell,
yi=wxi+b+εi,εi∼N(0,σ2).
Then each data point yi is a draw from a bell centred on the line's prediction y^i=wxi+b. The likelihood of a line is the product of the heights of the data points on their bells. Take −log of it:
−logL(w,b)=2σ21i∑(yi−y^i)2+Nlog(σ2π).
The second part does not depend on the line at all. So making the data most likely is the same as making the sum of squared errors smallest. The projection of Unit 3 was maximum likelihood under bell-shaped noise all along.
Worked example. Four rides: x=0,1,2,3 and y=1,3,2,5. The averages are xˉ=1.5 and yˉ=2.75. The slope is
The predictions are 1.1,2.2,3.3,4.4, the errors −0.1,0.8,−1.3,0.6, and the sum of their squares is 0.01+0.64+1.69+0.36=2.70.
A different belief, a different loss. Suppose instead the nudges follow a Laplace distribution, a pointy tent 2s1e−∣ε∣/s with heavier tails than the bell. The same steps give
−logL=s1i∑yi−y^i+const.
Now the best line minimises the sum of absolute errors. Squares punish a big error enormously (an error of 10 costs 100); absolute values punish it only 10. So the Laplace fit shrugs off an outlier. Add a mistyped ride at (1,8): the least-squares line tilts from slope 1.1 to about 0.654, while the Laplace line stays exactly where it was, w=34, b=1.
Every point stands on its own bellAt each ride a small bell rises out of the floor, centred on the line. The gold bead is the data point's height on it — its likelihood. Move the line and watch all the beads at once.
Try: Drag the slope and start sliders: when a bell's centre moves away from its point, the bead sinks down the bell's side. Press ▶ best fit — the log-likelihood and the squared error move in lockstep. Then add the outlier and flip between Gaussian and Laplace noise.
drag the picture to orbit
—
The realization
bell noise⇒mini∑(yi−y^i)2,Laplace noise⇒mini∑∣yi−y^i∣
A loss function is a belief about noise in disguise. Squared error is maximum likelihood with bell-shaped noise — that is why least squares works so well, and why it is thrown off by outliers that a bell says should never happen.
Pause & predict
For x=0,1,2,3 and y=1,3,2,5 with bell-shaped noise, what is the maximum-likelihood line?
Pause & predict
Your sensor sometimes produces wild readings. Which noise model, and therefore which loss, protects the fit?
If you want the algebra · 2 proofs, step by step
Prove it · −log of the Gaussian likelihood is the squared error
Claim. If yi=wxi+b+εi with independent εi∼N(0,σ2), then −logL(w,b)=2σ21∑i(yi−y^i)2+Nlog(σ2π).
1
Each yi is a bell draw centred on y^i=wxi+b. Independence lets us multiply:
L=i∏σ2π1×exp(−2σ2(yi−y^i)2).
2
The log of a product is a sum, and −loge−a=a:
−logL=i∑2σ2(yi−y^i)2+Nlog(σ2π).∎The last term has no w or b, and 2σ21 is a positive constant, so the best line is the one with the smallest ∑(yi−y^i)2 — least squares.
Prove it · Laplace noise gives absolute error
Claim. With εi drawn from the Laplace density 2s1e−∣ε∣/s, −logL=s1∑i∣yi−y^i∣+Nlog2s.
1
Same steps, a different curve:
−logi∏2se−∣yi−y^i∣/s=s1i∑∣yi−y^i∣+Nlog2s.∎So the most likely line minimises the total absolute error. For a single constant (no slope) this picks the median instead of the mean — which is why it ignores a wild value.
In one sentence: If the noise is a bell, the most likely line is the least-squares line — Unit 3's projection was maximum likelihood all along — and a pointier, heavier-tailed noise turns the loss into absolute error, which shrugs off outliers.
10
Softmax: turning scores into probabilities
Imagine this
Three children helped at a family wedding and earned points: Asha 2, Bittu 1, Chintu 0. You have one box of laddoos to share.
You want three things. Everyone gets some, even Chintu. More points means more laddoos. And the whole box is given out, nothing left over.
Softmax is a recipe that does exactly this, and it is how almost every modern classifier turns its raw scores into probabilities.
A classifier looks at a photo and produces one raw score per class: cat 2, dog 1, rabbit 0. The scores can be any numbers, even negative. We want probabilities: positive, in the same order, adding to 1. Two steps:
Exponentiate.ez is always positive and keeps the order. Each extra point of score multiplies the share by e≈2.718. So e2=7.389, e1=2.718, e0=1.
Divide by the total. The total is 7.389+2.718+1=11.107. Dividing gives 0.665, 0.245, 0.090. They add to 1.
softmax(z)k=∑jezjezk.
Only differences matter. Add 10 to every score. Every ez gets multiplied by the same e10, which cancels when you divide. Nothing changes. Real code uses this: it subtracts the largest score first, so it never has to compute something like e1000, which no computer can hold.
The temperature dial. Divide the scores by a number T before the softmax. A small T stretches the differences; a big T squashes them.
temperature
scores used
cat
dog
rabbit
character
T→0
huge gaps
1
0
0
winner takes all (argmax)
T=0.5
4, 2, 0
0.867
0.117
0.016
confident
T=1
2, 1, 0
0.665
0.245
0.090
the plain softmax
T=2
1, 0.5, 0
0.506
0.307
0.186
relaxed
T→∞
all near 0
⅓
⅓
⅓
no opinion (uniform)
This is exactly the "temperature" setting on a chatbot. A language model picks each next word from a softmax, and turning T down makes it predictable, turning it up makes it adventurous. You will meet this dial again in Unit 19.
Two classes: the sigmoid. With only two scores z1,z2, divide top and bottom by ez1:
ez1+ez2ez1=1+e−(z1−z2)1=σ(z1−z2).
That S-shaped curve σ is the sigmoid (or logistic function). It finally gives Unit 13's raw score a meaning. The SVM said f(x)=1.5, "positive side". Pass it through the sigmoid: σ(1.5)≈0.818, "about 82% sure". A point on the boundary, f=0, gets exactly 50%.
The softmax machineDrag the three score bars on the left. The probability bars on the right always add to 1.
Try: Drag the dog's score up past the cat's and watch the shares swap. Slide the temperature to 0.1 (winner takes all) and to 10 (almost ⅓ each). Then tick subtract the max: the scores shift, the probabilities do not move at all.
drag a score bar up or down
—
Where do the answers live? Three probabilities that add to 1 are a point in 3-D space, and all such points lie on one flat triangle with corners (1,0,0), (0,1,0) and (0,0,1). It is called the probability simplex. A corner is total certainty; the centre is "no idea". Every softmax output is a point on this triangle, and the temperature moves it.
The probability triangleEvery softmax answer is a point on this glass triangle. Sweeping the temperature draws a path from the winner's corner to the centre.
Try: Press ▶ sweep T: the glowing point starts near the cat corner (T small) and slides to the centre (T large), drawing its path. Change the scores in the softmax machine above — the path here redraws.
drag the picture to orbit
—
The realization
qk=∑jezj/Tezk/T,two classes: q1=σ(z1−z2)
Exponentiate, then share out. Softmax turns any scores into a point on the probability triangle; only the differences between scores matter; and the temperature slides the answer between "winner takes all" and "no opinion".
Pause & predict
The scores (2,1,0) give probabilities (0.665,0.245,0.090). What do the scores (12,11,10) give?
Pause & predict
You lower the temperature from 1 to 0.5 for the scores (2,1,0). What happens to the top probability?
If you want the algebra · 2 proofs, step by step
Prove it · adding a constant to every score changes nothing
Claim.softmax(z+c1)=softmax(z) for any number c.
1
ezk+c=ecezk, so the common factor ec appears on top and in every term of the bottom:
∑jecezjecezk=∑jezjezk.∎With c=−maxjzj, every exponent is ≤0, so no ez can overflow. That is the "subtract the max" line in every softmax routine.
Prove it · two-class softmax is the sigmoid
Claim.ez1+ez2ez1=σ(z1−z2), where σ(t)=1+e−t1.
1
Divide top and bottom by ez1:
1+ez2−z11=1+e−(z1−z2)1=σ(z1−z2).∎Only the difference z1−z2 survives, as the shift rule promised. An SVM score f(x) can play that difference: σ(f) is 0.5 on the boundary and approaches 1 deep on the positive side.
In one sentence: Softmax exponentiates the scores and shares them out so they become probabilities — only score differences matter, temperature slides from winner-takes-all to uniform, and with two classes it is the sigmoid of the score difference.
11
Surprise and entropy
Imagine this
Three things happen tomorrow. The sun rises. The toss before the match comes up heads. Your neighbour wins the state lottery.
The sunrise surprises nobody. The toss surprises you a little — it could easily have gone the other way. The lottery win is shocking.
The rarer the event, the bigger the surprise. Can we put a number on surprise?
Yes. The surprise of an outcome with probability p is
surprise=−log2p.
Certain events (p=1) carry zero surprise. A fair toss (p=21) carries 1. An event with p=41 carries 2, and a one-in-1024 event carries 10. Why a log? Because when two independent things happen, their chances multiply, and we want their surprises to add. The log turns multiplying into adding.
With log2 the unit is the bit: the number of fair yes/no questions the news is worth. With the natural log ln the unit is the nat. They differ only by a fixed factor: 1 nat ≈1.443 bits. Machine-learning code usually uses nats.
Now average the surprise over all outcomes, weighting each by how often it happens. That average is the entropy:
H(p)=−k∑pklog2pk.
A fair coin: 21⋅1+21⋅1=1 bit.
A coin that shows heads 90% of the time: 0.9⋅0.152+0.1⋅3.32≈0.469 bits. Usually no surprise, occasionally a big one; on average, less than half a bit.
Four equally likely outcomes: 2 bits (two yes/no questions pin it down).
Something certain: 0 bits.
Entropy measures how uncertain a distribution is. It is largest when every outcome is equally likely, and zero when one outcome is certain.
Entropy: the average surpriseTop: a coin's entropy as its bias changes. Below: three outcomes — the entropy is a dome over the probability triangle, highest at the centre.
Try: Drag the coin marker to 0.9: the curve reads 0.469 bits. Then drag the glowing point on the dome toward a corner and watch the height drain to zero; at the centre it is log23≈1.585 bits, the most uncertain three outcomes can be.
Surprise is minus the log of the chance. Entropy is the average surprise — the true uncertainty of a distribution: zero when you are certain, largest when every outcome is equally likely.
Pause & predict
Which has more entropy: a fair coin, or a coin that shows heads 90% of the time?
In one sentence: The surprise of an outcome is −logp, and entropy — the average surprise — measures how uncertain a distribution is, from 0 for a sure thing up to its maximum when all outcomes are equally likely.
12
Cross-entropy and KL: paying for the wrong belief
Imagine this
You move to Mumbai in July, believing "90% of days are dry". You leave your umbrella at home most mornings.
In truth, July is about half rain, half dry. Every rainy day shocks you — you had given it only a 10% chance. You are not unlucky. You are paying for a wrong belief, and you pay in surprise.
Call the truth p and your belief q. Each day the weather comes from p, but your surprise is measured with q: −logq(what happened). Average it over the true weather and you get the cross-entropy:
H(p,q)=−k∑pklogqk.
Mumbai in July, in bits: truth p=(0.5,0.5) for (rain, dry), belief q=(0.1,0.9). Your average surprise is 0.5⋅3.32+0.5⋅0.152≈1.737 bits. Someone who knew the truth would average only H(p)=1 bit. The extra 0.737 bits a day is the price of your wrong belief. It has a name, the KL divergence (after Kullback and Leibler):
H(p,q)=H(p)+KL(p∥q),KL(p∥q)=k∑pklogqkpk.
Three facts about KL:
Never negative. A wrong belief can never make you less surprised on average than the truth.
Zero only when q=p. It is a way of measuring how far a belief is from the truth.
Not symmetric. Swap the roles: KL(q∥p)≈0.531 bits, not 0.737. "Believing 90/10 when the truth is 50/50" costs more than "believing 50/50 when the truth is 90/10". So KL is not a true distance — the direction matters.
Why the direction matters when you fit. Suppose the truth p has two humps, and you must describe it with one bell q. If you minimise KL(p∥q), every place where p has mass but q has almost none is punished hard, so qspreads out to cover both humps. If you minimise KL(q∥p), every place where q puts mass but p has none is punished, so qlocks onto one hump and ignores the other. Two humps at ±2 with spread 0.6: the first fit is a wide bell at 0 with spread about 2.09; the second is a narrow bell at about ±2 with spread about 0.6.
Paying for the wrong beliefTruth p in blue, belief q in orange. The stacked bar shows H(p,q) = H(p) + KL(p‖q). The second tab fits one bell to a two-humped truth, both ways.
Try: On bars, press Mumbai in July, then drag the orange bars toward the blue ones and watch the red KL slice shrink to nothing. On fit one bell, press both fit buttons and compare: one bell stretches over both humps, the other picks a side.
drag a bar up or down
—
The realization
H(p,q)=the world’s own uncertaintyH(p)+price of your wrong belief≥0KL(p∥q)
Cross-entropy is your average surprise when the truth is p and you believe q. It splits into the surprise nobody can avoid, H(p), plus the extra you pay for being wrong, KL. KL is never negative, zero only for a perfect belief, and not symmetric.
Pause & predict
The truth is p=(0.5,0.5) and your belief is q=(0.9,0.1). You compute KL(p∥q)≈0.737 bits. What is KL(q∥p)?
If you want the algebra · 2 proofs, step by step
Prove it · cross-entropy = entropy + KL
Claim.H(p,q)=H(p)+KL(p∥q).
1
Split the log with logqk=logpk−logqkpk:
−k∑pklogqk=−k∑pklogpk+k∑pklogqkpk.∎Mumbai in July: 1.737=1+0.737 bits. When we train a model q on data from p, H(p) is fixed, so minimising cross-entropy and minimising KL are the same job.
Prove it · KL is never negative (in outline)
Claim.KL(p∥q)≥0, with equality only when q=p.
1
The log is a curve that bends downward, so the log of an average is at least the average of the logs (Jensen's inequality). Apply it with weights pk to the numbers qk/pk:
−KL=k∑pklogpkqk≤logk∑pkpkqk=logk∑qk=log1=0.
2
Equality in Jensen needs all the numbers qk/pk to be equal, and since both add to 1, that means qk=pk for every k.
∎So a wrong belief always costs extra surprise on average, and only the truth itself costs nothing extra.
In one sentence: Cross-entropy is the average surprise of believing q when the truth is p, it equals the truth's own entropy plus KL(p∥q)≥0, and because KL is lopsided, fitting in one direction covers every hump while the other direction locks onto one.
13
Cross-entropy is the classifier's likelihood
Imagine this
A student guesses answers with confidence levels. On one question she writes "70% sure it's (a)", and the answer is (a). Good — little surprise.
On another she writes "1% sure it's (c)" — and the answer is (c). She should be very surprised, and a good teacher would mark that down hard: she was confidently wrong.
Training a classifier is teaching it to be less surprised by the right answers.
A classifier outputs a belief q=softmax(z). The truth for one example is a certainty: "it is class 1", written as a one-hot vector y=(1,0,0). The cross-entropy between them has only one term left, because the other yk are zero:
H(y,q)=−k∑yklogqk=−logqcorrect.
That is just the surprise of the right answer. And it is also the negative log-likelihood from §7: "how likely did the model say the true label was?". So minimising cross-entropy is maximum likelihood for a classifier. Every idea of Act III carries over.
Worked example. Scores (2,1,0), the true class is the first. Then q=(0.665,0.245,0.090) and
loss=−ln0.665≈0.408 nats.
The cleanest gradient in machine learning. How should each score change to lower the loss? The answer (the drawer derives it) is astonishingly simple:
Prediction minus truth. The correct class gets a negative gradient, so a step downhill raises its score. Each wrong class gets a positive gradient equal to the probability it stole, so it is pushed down by exactly that much. When the prediction is perfect, q=y and the gradient is zero.
A whole classifier. For a 2-D point x, let each class have its own score line, zk=wk⋅x+bk. Softmax turns the three scores into probabilities. The loss over the data is the average cross-entropy, and the gradient for wk is (qk−yk)x. Walk downhill with gradient descent (Unit 9). This is softmax regression, and the step "q−y" is the first move of every backward pass in Unit 15.
Train a softmax classifierThree classes of points. The colour at each spot is the classifier's belief — soft where it is unsure. The loss curve falls as it learns.
Try: Press ▶ train and watch the fog of the untrained model sharpen into three regions, with soft edges where the classes meet. Drag the white probe into a border zone: its three probabilities share out. Then turn the prediction temperature down to 0.3 — the same model, far more sure of itself.
drag the white probe
—
The realization
loss=−logqcorrect,∂z∂loss=q−y
With a one-hot truth, cross-entropy is the surprise of the right answer — the negative log-likelihood. Its gradient with respect to the scores is prediction minus truth, the simplest signal a learner could ask for.
Pause & predict
A model gives the true class probability 0.9 on one example and 0.1 on another. What are the two cross-entropy losses (in nats)?
Pause & predict
Scores (2,1,0) give q=(0.665,0.245,0.090), and the true class is the second. What is the gradient of the loss with respect to the scores?
If you want the algebra · 1 proof, step by step
Prove it · the gradient of −log softmax is q − y
Claim. For loss=−logqc with q=softmax(z) and true class c, ∂zk∂loss=qk−yk.
1
Write the loss without the fraction:
−logqc=−zc+logj∑ezj.
2
Differentiate with respect to zk. The first term gives −1 if k=c and 0 otherwise — that is −yk. The second gives
∑jezjezk=qk.
3
Add them: ∂zk∂loss=qk−yk.
∎Scores (2,1,0), class 1: (0.665−1,0.245,0.090)=(−0.335,0.245,0.090). The entries always add to 0: raising one score is paid for by the others. And for a linear model zk=wk⋅x+bk, the chain rule gives ∂loss/∂wk=(qk−yk)x.
In one sentence: For a classifier, cross-entropy with the one-hot truth is −logqcorrect, the negative log-likelihood, and its gradient with respect to the scores is simply prediction minus truth, q−y.
14
What to carry forward
Thirteen sections, one shift in thinking: from crisp answers to honest maybes. Here is the kit you now carry.
Distributions
E[X]=∑xp(x),Var=E[(X−μ)2]
Mass for counts, area for smooth quantities. The mean balances, the variance spreads.
Bayes
P(H∣E)∝P(E∣H)P(H)
Posterior ∝ likelihood × prior. The prior is where you stood before the evidence.
The Gaussian and its ellipse
p(x)∝e−21(x−μ)⊤Σ−1(x−μ)
Contours are Unit 12's ellipse, axes are Unit 4's eigenvectors, samples are μ+Lz.
Likelihood and MLE
θ^=argmaxlogP(data∣θ)
A coin: the share of heads. A bell: the average and the average squared spread.
Least squares = Gaussian MLE
−logL=2σ21∑(yi−y^i)2+const
A loss is a noise belief in disguise. Laplace noise gives absolute error.
Softmax and temperature
qk=∑jezj/Tezk/T
Only differences matter. Two classes: σ(z1−z2).
Entropy, cross-entropy, KL
H(p,q)=H(p)+KL(p∥q)
Average surprise; the price of a wrong belief; never negative, not symmetric.
The training signal
∂z∂(−logqy)=q−y
Prediction minus truth — the first step of every backward pass.
The thread to Units 15–20
Unit 15 wires softmax and cross-entropy onto the end of a whole network, and "q−y" is where its backward pass starts. Unit 16 predicts the next word with a softmax over the whole vocabulary. Unit 19 samples from that softmax with a temperature dial. Unit 20 builds pictures from Gaussian noise, using the stretch μ+Lz (the reparameterisation trick) and a KL penalty to keep its beliefs honest.
The one thing to remember
Learning is choosing the world that makes your data least surprising. Squared error, cross-entropy and maximum likelihood are three names for that one idea.
In one sentence: Uncertainty has a shape, the Gaussian's shape is the covariance ellipse, fitting a model means maximising the likelihood of the data, and softmax plus cross-entropy turn that idea into a classifier whose gradient is prediction minus truth.
15
Practice arena — fourteen problems, solved in full
Fourteen problems, easy to hard: a loaded die, two rounds of Bayes, a bell read with z-scores, a covariance matrix taken apart, a Cholesky factor, a coin and a bell fitted by maximum likelihood, a line fitted through its likelihood, softmax at two temperatures, the sigmoid, entropy, cross-entropy and both KLs, one training step of a classifier, and a question about which loss belongs to which noise. Every number was checked by machine.
Three habits do most of the work. Count before you divide: for Bayes, imagine 10 000 people and count them. Take the log early: products of chances become sums you can differentiate. And check that probabilities add to 1 — it catches most slips.
Problem 1easypmf · mean · variance
A loaded die shows 6 with probability 21 and each of the faces 1–5 with probability 101. (a) Check that it is a valid pmf. (b) Find E[X]. (c) Find Var[X] and σ. (d) Compare with a fair die.
What this tests. The balance point and the spread computed from a table. Plan. Add the weights; weighted average of x; then the shortcut Var=E[X2]−μ2.
Show the full solution
Step 1 — valid (a).5×101+21=1 ✓, and every weight is between 0 and 1.
Step 2 — the mean (b).E[X]=101+2+3+4+5+6⋅21=1.5+3=4.5.
Step 3 — the variance (c).E[X2]=101+4+9+16+25+36⋅21=5.5+18=23.5, so Var=23.5−4.52=23.5−20.25=3.25 and σ=3.25≈1.803.
Step 4 — compare (d). A fair die has mean 3.5 and variance 1235≈2.917. The loading moves the balance point up by 1 and makes the outcomes a little more spread out.
answers at a glance: (a) weights add to 1 ✓; (b) E[X]=4.5; (c) Var=3.25, σ≈1.803; (d) fair die: 3.5 and 2.917.
Problem 2easyBayes · two tests
A disease affects 2% of people. A test catches 90% of the sick and gives a false alarm to 10% of the healthy. (a) What fraction of all people test positive? (b) You test positive. What is the chance you are sick? (c) A second, independent test is also positive. Now what? (d) If instead the first test had been negative, what is the chance you are sick?
What this tests. Bayes' rule with natural counts, and chaining an update. Plan. Imagine 10 000 people and count. For (c), yesterday's posterior is today's prior.
Show the full solution
Step 1 — count (a). Of 10 000 people, 200 are sick and 180 of them test positive; 9 800 are healthy and 980 of them get a false alarm. Positives: 180+980=1160, which is 11.6%.
Step 2 — one positive (b).P(sick∣+)=1160180≈0.155. Only about 1 in 6.4 positives is sick: the false alarms on the big healthy crowd swamp the real cases.
Step 3 — a second positive (c). New prior 0.155:
0.9×0.155+0.1×0.8450.9×0.155≈0.623.
In counts: of the 1160 positives, 180 sick → 162 positive again; 980 healthy → 98 positive again; 260162≈0.623 ✓.
Step 4 — a negative (d). Negatives: 20 sick (missed) and 8 820 healthy. P(sick∣−)=884020≈0.0023 — a negative is very reassuring.
answers at a glance: (a) 11.6% test positive; (b) ≈0.155; (c) ≈0.623; (d) ≈0.0023.
Remember
A positive test on a rare condition is weak evidence on its own; two independent positives are strong. And the same test can be far more convincing when it says "no" than when it says "yes".
Problem 3easythe bell · z-scores
Marks in a subject follow N(60,102). (a) Roughly what share scored between 50 and 70? Between 40 and 80? (b) Roughly what share scored above 80? (c) A student scored 75. What is her z-score? (d) Another subject has μ=70, σ=5 and her friend scored 80 there. Who did better relative to their class?
What this tests. The 68–95–99.7 rule and comparing across different bells with z-scores. Plan. Turn every mark into "how many σ's from the middle".
Show the full solution
Step 1 — the rule (a). 50 to 70 is ±1σ: about 68% (68.3%). 40 to 80 is ±2σ: about 95% (95.4%).
Step 2 — the tail (b). Outside ±2σ is about 4.6%, split equally between the two tails by symmetry: about 2.3% scored above 80.
Step 3 — z (c).z=1075−60=1.5.
Step 4 — compare (d). Her friend: z=580−70=2. The friend is 2 spreads above the class middle, she is 1.5, so the friend did better relative to their class — even though 80 − 70 = 10 is less than 75 − 60 = 15.
answers at a glance: (a) ≈ 68% and ≈ 95%; (b) ≈ 2.3%; (c) z=1.5; (d) the friend (z=2 against 1.5).
Problem 4medium2-D Gaussian · Mahalanobis
A 2-D Gaussian has μ=0 and Σ=[5222]. (a) Find the eigenvalues and unit eigenvectors of Σ. (b) Describe the contour ellipse. (c) Find detΣ and the peak density. (d) Find the Mahalanobis distance of (2,1) and of (1,−2) from the mean, and compare with their ordinary distances.
What this tests. Reading a Gaussian's shape from Σ (Unit 4 meets §5). Plan. Trace and determinant give the eigenvalues; eigenvectors give the axes; Σ−1 gives the ruler.
Show the full solution
Step 1 — eigenvalues (a). Trace 7, determinant 10−4=6. So λ2−7λ+6=0, λ=6 or 1.
Step 2 — eigenvectors (a). For λ=6: (5−6)v1+2v2=0⇒v=(2,1)/5. For λ=1: 4v1+2v2=0⇒v=(1,−2)/5. They are at right angles, as the spectral theorem promises.
Step 3 — the ellipse (b). The long axis points along (2,1), the short axis along (1,−2). Half-lengths are in the ratio 6:1≈2.45:1.
Step 4 — the peak (c).detΣ=6, so pmax=2π61≈0.0650.
Step 5 — the ruler (d).Σ−1=61[2−2−25].
(2,1):d2d(1,−2):d2d=68−8+5=65,≈0.913;=62+8+20=5,≈2.236.
Both points have ordinary distance 5≈2.236. The first lies on the long axis (d2=5/6, i.e. ∥x∥2/λ1); the second on the short axis (d2=5/1).
answers at a glance: (a) λ=6 along (2,1)/5, λ=1 along (1,−2)/5; (b) ellipse tilted along (2,1), axes in ratio 6:1; (c) det=6, peak ≈0.0650; (d) Mahalanobis 0.913 and 2.236, ordinary distance 2.236 for both.
Remember
Along an eigenvector, the Mahalanobis distance is the ordinary distance divided by λ: long axes forgive, short axes do not.
Problem 5mediumsampling · Cholesky
You need samples from N(μ,Σ) with μ=(1,1) and Σ=[9665]. (a) Find the Cholesky factor L. (b) Check LL⊤=Σ. (c) The computer gives the round sample z=(1,−1). What sample x do you produce? (d) What is the correlation between x1 and x2?
What this tests. The stretch recipe x=μ+Lz. Plan. Fill L entry by entry, top-left first.
Show the full solution
Step 1 — L (a).ℓ11=9=3; ℓ21=6/3=2; ℓ22=5−22=1. So L=[3201].
Step 2 — check (b).LL⊤=[9664+1]=Σ ✓.
Step 3 — one sample (c).Lz=(3⋅1,2⋅1+1⋅(−1))=(3,1), so x=(1,1)+(3,1)=(4,2).
answers at a glance: (a) L=[3201]; (b) ✓; (c) x=(4,2); (d) ρ≈0.894.
Problem 6easylikelihood · coin
A coin shows 3 heads in 12 tosses. (a) Write L(p) and logL(p). (b) Find p^. (c) Compute logL at p^ and at p=0.5 (natural log). (d) How many times better does p^ explain the data than a fair coin?
What this tests. Maximum likelihood and comparing models by their likelihood. Plan. Differentiate the log; compare in logs and exponentiate the difference.
Show the full solution
Step 1 — the functions (a).L(p)=p3(1−p)9 and logL=3logp+9log(1−p).
Step 2 — the peak (b).p3−1−p9=0⇒3(1−p)=9p⇒p^=123=0.25.
Step 3 — two log-likelihoods (c).logL(0.25)=3ln0.25+9ln0.75≈−4.159−2.589=−6.748. logL(0.5)=12ln0.5≈−8.318.
Step 4 — the ratio (d).e−6.748−(−8.318)=e1.570≈4.81. The best coin explains the tosses about 4.8 times better than a fair one.
answers at a glance: (a) p3(1−p)9; (b) p^=0.25; (c) −6.748 and −8.318; (d) about 4.81 times.
Problem 7easyGaussian MLE · N vs N − 1
Measurements: 1, 3, 4, 6, 6. (a) Find the maximum-likelihood μ^ and σ^2. (b) Find the "divide by N−1" variance. (c) Which one is too small on average, and why?
What this tests. The Gaussian MLE formulas and the reason for the N−1 correction. Plan. Average; squared distances from the average; divide two ways.
Show the full solution
Step 1 — the middle (a).μ^=51+3+4+6+6=520=4.
Step 2 — the spread (a). Squared distances: 9,1,0,4,4, total 18. σ^2=518=3.6, σ^≈1.897.
Step 3 — N − 1 (b).418=4.5.
Step 4 — which is biased (c). The MLE, 3.6. It measures the spread around μ^=4, which was chosen to be as close to these very points as possible, so the points look slightly tighter than they would around the true middle. Dividing by N−1 undoes that on average.
answers at a glance: (a) μ^=4, σ^2=3.6; (b) 4.5; (c) the MLE is slightly too small on average.
Problem 8mediumleast squares via likelihood
Three points: (1,2), (2,3), (3,5). Assume y=wx+b+ε with ε∼N(0,σ2). (a) Why does maximising the likelihood mean minimising ∑(yi−y^i)2? (b) Find w and b. (c) Find the squared error. (d) The maximum-likelihood noise variance is σ^2=N1∑(yi−y^i)2. Compute it.
What this tests. The chain "bell noise → −log L → squared error → the least-squares formulas". Plan. Write −logL; use the centred slope formula.
Show the full solution
Step 1 — why (a).−logL=2σ21∑(yi−y^i)2+Nlog(σ2π). For any fixed σ the last term is fixed and 2σ21>0, so the line that makes −logL smallest is the one with the smallest squared error.
Step 2 — the line (b).xˉ=2, yˉ=310. Deviations: x: −1,0,1; y: −34,−31,35.
wb=1+0+134+0+35=23,=310−23⋅2=31.
answers at a glance: (a) the other term of −logL does not depend on the line; (b) w=1.5, b=31; (c) 61≈0.167; (d) σ^2=181≈0.0556.
Problem 9easysoftmax · temperature
Scores (3,1,1). (a) Compute the softmax at T=1. (b) At T=2. (c) What happens as T→0 and as T→∞? (d) What does softmax give for (103,101,101)?
What this tests. The recipe, the dial and shift invariance. Plan. Exponentiate, add, divide — after dividing the scores by T.
Show the full solution
Step 1 — T = 1 (a).e3=20.086, e1=2.718, e1=2.718; total 25.522. Probabilities (0.787,0.107,0.107).
Step 2 — T = 2 (b). Scores become (1.5,0.5,0.5): 4.482,1.649,1.649, total 7.779. Probabilities (0.576,0.212,0.212) — more relaxed.
Step 3 — the ends (c).T→0: all the probability goes to the first class, (1,0,0). T→∞: (31,31,31). The two tied classes stay tied at every temperature.
Step 4 — shift (d). Adding 100 to every score changes nothing: (0.787,0.107,0.107). Computing e103 directly would be foolish; subtract the max first.
answers at a glance: (a) (0.787,0.107,0.107); (b) (0.576,0.212,0.212); (c) (1,0,0) and (31,31,31); (d) same as (a).
Problem 10easytwo classes · sigmoid
A two-class model gives scores z1=2.5, z2=1. (a) Compute q1 with softmax. (b) Compute σ(z1−z2) and compare. (c) An SVM gives a new point f(x)=−1.5. What probability of class +1 does σ(f) suggest? (d) What does a point on the SVM's boundary get?
What this tests. That two-class softmax is the sigmoid of the score difference. Plan. Compute both ways; then read an SVM score as a difference.
Step 2 — sigmoid (b).σ(1.5)=1+e−1.51=1+0.2231≈0.818. The same number.
Step 3 — the SVM score (c).σ(−1.5)≈0.182: about an 18% chance of class +1, 82% of class −1.
Step 4 — the boundary (d).σ(0)=0.5: exactly undecided.
answers at a glance: (a) and (b) 0.818; (c) 0.182; (d) 0.5.
Remember
In practice the SVM score is first scaled and shifted (σ(af+b), fitted on held-out data) before it is trusted as a probability — but the idea is exactly this.
Problem 11easysurprise · entropy
A four-outcome distribution is (21,41,81,81). (a) Find the surprise of each outcome in bits. (b) Find the entropy in bits and in nats. (c) Compare with four equally likely outcomes.
What this tests. Surprise, average surprise, and the units. Plan.−log2 of each chance; weight by the chance; multiply by ln2 for nats.
Step 3 — compare (c). Four equal outcomes: 2 bits. The lopsided one is more predictable, so it carries less uncertainty. A neat reading: ask "is it the first?" (yes half the time), then "the second?", then "the third?" — on average 1.75 questions.
answers at a glance: (a) 1, 2, 3, 3 bits; (b) 1.75 bits ≈ 1.213 nats; (c) less than the 2 bits of the uniform.
Problem 12mediumcross-entropy · both KLs
The truth is p=(0.25,0.75) and a model believes q=(0.5,0.5). In bits: (a) find H(p); (b) find H(p,q); (c) find KL(p∥q) two ways; (d) find KL(q∥p).
What this tests. The identity H(p,q)=H(p)+KL and the lopsidedness of KL. Plan. Compute each sum directly, then check the identity.
Step 3 — KL two ways (c). Directly: 0.25log20.50.25+0.75log20.50.75=−0.25+0.439=0.189. From the identity: 1−0.811=0.189 ✓.
Step 4 — the other direction (d).0.5log20.250.5+0.5log20.750.5=0.5−0.292=0.208. Not the same as 0.189.
answers at a glance: (a) 0.811; (b) 1; (c) 0.189; (d) 0.208 bits.
Problem 13hardcross-entropy loss · q − y
A three-class model gives scores z=(0,1,3). The true class is the second. (a) Compute q. (b) Compute the loss in nats. (c) Compute the gradient with respect to z, and say what one step of size 1 does to each score. (d) Take that step and recompute the loss.
What this tests. The loss of a confidently wrong model, and one step of the cleanest gradient in the course. Plan. Softmax; −lnq2; q−y; z←z−η(q−y).
Show the full solution
Step 1 — the belief (a).e0=1, e1=2.718, e3=20.086; total 23.804. q=(0.042,0.114,0.844). The model is fairly sure of the third class.
Step 2 — the loss (b).−ln0.114≈2.170 nats. Large: the truth got only 11% belief.
Step 3 — the gradient (c).y=(0,1,0):
q−y=(0.042,−0.886,0.844).
A step downhill adds 0.886 to the true score, takes 0.844 off the third (the class that stole the most), and 0.042 off the first.
Step 4 — after the step (d).z=(−0.042,1.886,2.156), so q=(0.059,0.407,0.534) and the loss falls to −ln0.407≈0.899.
answers at a glance: (a) (0.042,0.114,0.844); (b) 2.170 nats; (c) (0.042,−0.886,0.844); (d) loss ≈0.899.
Remember
The gradient's entries always add to zero, and each wrong class is pushed down by exactly the probability it wrongly held. That is the whole training signal of a classifier.
Problem 14mediumwhich loss from which noise
Five recorded auto fares for the same short ride, in tens of rupees: 1, 2, 3, 4 and — one mistyped — 100. You model them as one true fare c plus noise. (a) With bell-shaped noise, which loss do you minimise, and what is c^? (b) With Laplace noise? (c) Which estimate would you trust, and why?
What this tests. That a loss is a noise belief, with a definite numerical answer for each. Plan. Bell → squared error → mean; Laplace → absolute error → median.
Show the full solution
Step 1 — bell noise (a).−logL is the squared error ∑(yi−c)2, smallest at the mean: c^=51+2+3+4+100=22.
Step 2 — Laplace noise (b).−logL is the absolute error ∑∣yi−c∣. Its slope is (number of points below c) − (number above), which is zero at the median: c^=3.
Step 3 — judge (c). The median, 3: four of the five fares agree with it. The mean, 22, is nowhere near any real fare — one wild value dragged it, because a bell says such a value is practically impossible and bends everything to explain it.
answers at a glance: (a) squared error, c^=22 (the mean); (b) absolute error, c^=3 (the median); (c) trust the median.
Remember
Choosing a loss is choosing a noise model. If wild values can happen, say so with a heavy-tailed noise — and the loss will follow.
Every tool of this unit — softmax, cross-entropy, likelihood — now gets wired into a full network. You will build one yourself and watch the forward pass carry an input to a probability, and the backward pass carry "prediction minus truth" back through every layer, node by node.