Semester 1 · Week 04
Information theory
- Ch 5 Information Theory Basics
Chapter 5: Information theory
One idea, four angles: how surprising is an event?
Notes · slide 2
Time: 50 minutes for one chapter. About 25 on the four ideas, 15 on the worked example, 5 on the mental model, and 5 to check understanding.
Say: Why does a classifier's loss use a logarithm? Why do trees split on "information gain"? Why does RLHF use a penalty called KL divergence? One answer covers all three: information theory.
Key line: one idea, four angles. How surprising is an event?
GenAI link: The same ideas run an LLM. Its loss is cross-entropy on the next token. Its evaluation number, perplexity, comes from that loss. Its alignment step keeps it close to the base model with a KL penalty.
Next: measuring surprise.
Surprise, measured

Shannon, 1948. The information content of an event with probability p:
I(p)=−log2(p)
- Rare events are surprising: high information.
- Expected events carry little.
- Natural log gives nats. Base 2 gives bits.
Notes · slide 3
Say: Shannon, 1948. A fair coin landing heads isn't surprising. A coin that almost always lands heads landing tails is. Information content is minus the log of the probability. Rare events carry a lot. Expected ones carry little.
Say: The log base only sets the unit. Base 2 gives bits. Natural log gives nats. scikit-learn's mutual information reports nats, which trips people up.
Common question: "Are these the same bits as in data storage and networking?" Yes. Shannon's 1948 paper is where the word "bit" first appeared in print. He credited John Tukey for it, as short for "binary digit". The two uses measure slightly different things.
- A storage bit is a container: one binary digit, a 0 or a 1. A 1 GB drive has room for 8 billion of them.
- An information bit is a measure: how much uncertainty an outcome removes. A fair coin flip carries exactly 1 bit.
- How they connect: one stored bit holds at most one bit of information. It holds the full bit only when 0 and 1 are equally likely. A stored bit that is almost always 0 carries almost no information.
Networking uses the same unit. Shannon's channel capacity is the maximum rate a noisy link can carry reliably, in bits per second. Modern Wi-Fi and 5G codes get close to that limit.
The cube: in Star Wars: Episode I – The Phantom Menace, Watto rolls a chance cube to decide which slave Qui-Gon wins. Blue, the boy. Red, the mother. It lands blue, and the film hints that Qui-Gon nudged it with the Force.
Ask: "How many bits of surprise is that roll for Watto? For Qui-Gon?" Listen for: Watto thinks the cube is fair, so blue has probability 1/2: 1 bit. Qui-Gon knows how it will land, so blue has probability 1: 0 bits. Same event, different surprise. Information depends on the probabilities the observer holds, not on the event alone.
Ask: "An event with probability 1/8. How many bits?" Listen for: 3.
GenAI link: An LLM's loss on one token is its surprise at the real next token: minus the log of the probability it gave that token. Training lowers the average surprise. A surprise for the history: in 1951, Shannon estimated the entropy of English by asking people to guess the next letter of a text. That's next-token prediction, 70 years before ChatGPT.
Next: averaging surprise over a distribution.
Entropy: how uncertain is a distribution?
H(P)=−x∑P(x)logP(x)
- The average surprise over a distribution.
- Highest when every outcome is equally likely.
- Zero when one outcome is certain.
- Decision trees use it to choose splits: information gain.
Notes · slide 4
Say: Entropy is the average surprise. It measures how spread out a distribution is, not whether it's right. Uniform is the maximum: log K for K outcomes. One certain outcome is zero.
Say: Decision trees use it. At a node, entropy says how mixed the classes are. A good split makes children purer than the parent. The drop in entropy is the information gain.
Entropy is the limit on compression. This is where information bits meet storage bits. Shannon's source coding theorem says entropy is the lowest average number of storage bits per symbol that any lossless compression can reach. English text is stored at 8 bits per character in ASCII. Shannon estimated its entropy at roughly 1 bit per letter. That gap is why zip files shrink text so well. It's also why compressed or encrypted files barely compress again: their bits are already close to 50/50.
The quote: Shannon needed a name for his measure of uncertainty. The story goes that von Neumann told him to call it entropy, for two reasons. The formula already had that name in statistical mechanics. And no one knows what entropy is, so he'd always win a debate. Shannon told it to Myron Tribus, who published it in Scientific American in 1971. It's a story told years later, not a written record, so treat it as a good anecdote. The first reason holds: Shannon's formula has the same form as Gibbs's entropy in physics.
Ask: "Which has more entropy: a 50/50 class split or 95/5?" Listen for: 50/50.
GenAI link: Sampling temperature sets the entropy of an LLM's next-token distribution. Below 1, the distribution sharpens and entropy falls: more predictable text. Above 1, it flattens and entropy rises: more varied text, and more mistakes. Entropy also flags uncertainty. A 2024 Nature paper detected hallucinations with "semantic entropy": sample several answers, group the ones that mean the same thing, and measure the entropy over meanings. High entropy means the model is guessing.
Also a GenAI link: a good language model is a good compressor. Its cross-entropy in bits is the number of bits per token an encoder using the model would need. DeepMind made this point in a 2023 paper, "Language Modeling Is Compression".
Next: the other impurity measure.
Entropy or Gini?
Gini(P)=1−x∑P(x)2
- Both measure how mixed a tree node is.
- In practice they choose nearly identical splits.
- Gini is a little cheaper: no logarithm.
Notes · slide 5
Say: Gini impurity: pick an example at random and guess its label from the node's own class mix. Gini is the chance that guess is wrong. A pure node is 0. A 50/50 node is 0.5.
Say: Both measure how mixed a node is, and they choose nearly the same splits. Gini skips the logarithm, so it's a little quicker. scikit-learn uses it by default.
Key line: the choice rarely changes which feature a tree picks. Don't spend time tuning it.
Next: scoring a model's predictions.
Cross-entropy: the cost of being wrong
H(P,Q)=−x∑P(x)logQ(x)
- How well a predicted distribution Q matches the truth P.
- For one binary label it's the familiar loss:
L=−[ylog(y^)+(1−y)log(1−y^)]
Notes · slide 6
Say: Cross-entropy compares a predicted distribution Q with the truth P. When Q equals P, it equals the entropy, its minimum. As Q drifts from P, it grows.
Say: For one binary label, it's the loss you met last week. If the label is 1, only the first term counts: minus the log of the predicted probability. Predict 0.01 when the truth is 1, and the log is a large negative number. The loss explodes.
Key line: a small penalty for being unsure. A large one for being confidently wrong.
GenAI link: This is an LLM's pretraining loss, with one class per token in the vocabulary. The truth P puts all its probability on the token that came next, so the sum collapses to one term: minus the log of the probability given to that token. The original transformer paper softened that target with label smoothing: 90% on the real token, 10% spread over the rest. It stops the model growing overconfident.
Next: why not MSE.
Why not MSE for classification?
- Cross-entropy falls straight out of the likelihood of a Bernoulli or categorical distribution.
- It punishes confident mistakes hard.
- MSE barely notices the difference between "wrong" and "confidently wrong".
Notes · slide 7
Say: Cross-entropy comes straight from the likelihood of a Bernoulli or categorical distribution. It's the right loss for the problem.
Say: MSE on probabilities flattens out near 0 and 1. That's where a confident wrong prediction sits, and where the model most needs a strong push to fix it. MSE gives a weak one.
Key line: use cross-entropy for classification. Reaching for MSE is a common, avoidable mistake.
Ask: "A fraud model says 99% fraud and it's wrong. Which loss punishes that harder?" Listen for: cross-entropy.
GenAI link: Cross-entropy rewards honest probabilities, so a pretrained LLM tends to be well calibrated: when it says 70%, it's right about 70% of the time. The GPT-4 technical report showed this, and showed that post-training made calibration worse. The loss shapes what the probabilities mean.
Next: measuring the gap between two distributions.
KL divergence: how far is Q from P?
KL(P∥Q)=x∑P(x)logQ(x)P(x)
- Always ≥0. Zero only when Q=P.
- Asymmetric: KL(P∥Q)=KL(Q∥P). Each direction answers a different question.
- Used for drift detection, VAEs, knowledge distillation, and the RLHF penalty.
Notes · slide 8
Say: KL divergence measures how different Q is from a reference P. It's never negative, and it's zero only when they match. It is not a distance: swap the arguments and you get a different number.
Say: It's infinite wherever Q gives zero probability to something P allows. That's why code clips with a small epsilon.
Say: It's everywhere. Drift detection. Knowledge distillation, where a small model learns from a large one. RLHF's penalty, which keeps a fine-tuned model close to the base.
Key line: be explicit about which argument is the truth.
GenAI link: In RLHF, the reward is the human-preference score minus a KL penalty between the tuned model and the base model. Without it, the model drifts to odd text that games the reward. The direction matters. RLHF uses KL(tuned ‖ base), which lets the tuned model drop options the base model had. That's one explanation for why tuned models give less varied answers.
Next: how the three link up.
The identity that ties them together
H(P,Q)=H(P)+KL(P∥Q)
- H(P) is fixed by the data.
- So minimising cross-entropy is minimising KL divergence.
- When the loss reaches H(P), there's nothing left to learn.
Notes · slide 9
Say: Cross-entropy equals entropy plus KL divergence. The entropy of the true labels is fixed by the data. The model can't change it. So training to minimise cross-entropy is the same as minimising KL divergence from the truth.
Key line: when the loss reaches H(P), the model has learned everything the data holds. More training can't close a gap that isn't there.
Ask: "Your validation loss stops falling. Is the model done?" Listen for: maybe. It may have reached the data's entropy, or it may be stuck. Check the loss against H(P) if you can compute it.
GenAI link: This identity sets the floor for every LLM. Its loss can't fall below the entropy of text itself. DeepMind's Chinchilla scaling law estimated that floor at about 1.69 nats per token on its data. More parameters and more data shrink only the KL part.
Next: relationships between two variables.
Mutual information
I(X;Y)=H(Y)−H(Y∣X)
- How much knowing X reduces uncertainty about Y.
- Zero only if they're independent.
- Catches any relationship, linear or not. Correlation only sees linear ones.
- Used to rank features: Chapter 15 returns to it.
Notes · slide 10
Say: Mutual information asks how much knowing X reduces uncertainty about Y. Zero means independent. Larger means more dependent.
Say: Correlation is built from deviations from the mean, so it only sees straight lines. Mutual information is built from entropy, so it sees any relationship.
Say: It has no fixed scale. Its ceiling is the label's entropy, which changes from dataset to dataset. Use it to rank features within one dataset, not as a portable threshold.
GenAI link: Mutual information sits under the image side of GenAI. CLIP learns to match images with their captions using a contrastive loss that is a lower bound on the mutual information between them. Stable Diffusion's first versions used CLIP's text encoder to turn a prompt into the vector that steers the image.
Next: the four side by side.
How the four fit together
| Quantity | Question |
|---|---|
| H(P) | How hard is the problem? |
| H(P,Q) | How is the model scoring? |
| KL(P∥Q) | How far from optimal is it? |
| I(X;Y) | What does one variable tell you about another? |
Notes · slide 11
Say: Read down the table. Entropy is how hard the problem is before any model. Cross-entropy is how the model is scoring. KL divergence is how far from optimal it is. Mutual information asks the same question of two variables.
Key line: entropy is the baseline, cross-entropy the scorecard, KL the gap.
Ask: "Which would you use to check if a feature's distribution has drifted since training?" Listen for: KL divergence, with the training distribution as the reference.
GenAI link: Read an LLM training run with this table. H(P) is the entropy of text: unknown, but a floor. Cross-entropy is the loss on the training chart. KL is the gap between the two. Scaling laws predict how fast that gap closes as models and data grow.
Next: the worked example.
Worked example: entropy of a die
| Four-sided die | Entropy |
|---|---|
| Fair | 2.0000 bits, the maximum: log24 |
| Biased | 1.3568 bits |
| Always lands on one face | 0 bits |
Notes · slide 12
Say: A fair four-sided die has exactly 2 bits, log₂ 4, the maximum for four outcomes. A biased die drops to 1.3568 bits, because one face dominates. A die that always lands on one face has 0. Nothing left to be uncertain about.
Ask: "Rank these before I show the numbers." Most people get it right. The point is that entropy matches intuition about uncertainty.
GenAI link: Scale the fair die up to GPT-2's vocabulary of 50,257 tokens. A uniform guess over all of them is about 15.6 bits: log₂ 50,257. That's the entropy of a model that knows nothing. A trained model's loss sits far below it, because most tokens are unlikely in any given context.
Next: what confidence does to the loss.
Worked example: the cost of confidence
True label y=1.
| Prediction y^ | Loss |
|---|---|
| 0.99, confident and right | 0.0101 nats |
| 0.5, unsure | 0.6931 nats |
| 0.01, confident and wrong | 4.6052 nats |
Confidently wrong costs 458 times confidently right.
Notes · slide 13
Say: True label 1. Predict 0.99: loss 0.0101 nats. Predict 0.5: 0.6931, which is log 2, a coin flip. Predict 0.01: 4.6052.
Key line: confidently wrong costs 458 times confidently right, and about 6.6 times unsure.
Say: A rule of thumb for reading loss in nats. Around 0.69 on a balanced binary problem is no better than chance. Below 0.1 is confident and mostly right. Above 2 means confident mistakes on a meaningful share of examples.
GenAI link: Perplexity is e raised to the loss in nats. A loss of 0.6931 is a perplexity of 2: a coin flip. A loss of 4.6052 is a perplexity of 100: as unsure as picking from 100 equally likely tokens. When an LLM paper reports perplexity, this is the conversion behind it.
Next: checking the identity with numbers.
Worked example: the identity holds
P=[0.50,0.30,0.15,0.05], Q=[0.30,0.30,0.20,0.20]
- KL(P∥Q)=0.2062 bits. KL(Q∥P)=0.2619 bits. Asymmetric.
- H(P)+KL(P∥Q)=1.8540 bits =H(P,Q). Exactly.
Notes · slide 14
Say: Two four-outcome distributions. KL of P from Q is 0.2062 bits. Reverse it and you get 0.2619. Different numbers, so KL is asymmetric.
Say: Entropy of P is 1.6477 bits. Add KL and you get 1.8540. Compute cross-entropy directly: 1.8540. It matches exactly. Not an approximation.
GenAI link: The same identity explains distillation, which trains small models from large ones. The student matches the teacher's full distribution, not only the one correct token, by minimising KL to the teacher. The teacher's probabilities on the wrong answers carry information too. Hinton, Vinyals, and Dean called it "dark knowledge" in 2015.
Next: where correlation goes blind.
Worked example: correlation's blind spot
| Feature | Correlation | Mutual information |
|---|---|---|
| Spread depends on the label | 0.0161 | 0.3377 |
| Mean depends on the label | 0.7047 | 0.3303 |
| Pure noise | 0.0075 | 0.0430 |
Correlation calls the first feature noise. Mutual information ranks it most informative.
Notes · slide 15
Say: Three synthetic features. One's mean moves with the label. One's spread moves with the label but its mean doesn't. One is noise.
Say: Correlation scores the spread feature at 0.0161, indistinguishable from noise. Mutual information ranks it the most informative of the three.
Key line: correlation can't see a change in spread by construction. Mutual information can.
Ask: "Where might a real feature behave like this?" Listen for: anything where one group is more variable, such as transaction amounts that swing wildly for fraud.
Next: the chapter in one idea.
Mental model
- Entropy is the baseline: how hard is the problem before any model?
- Cross-entropy is the scorecard.
- KL divergence is the gap between the two.
- Mutual information asks the same question of two variables.
When a classifier's loss converges to H(P), it has learned everything the data holds.
Notes · slide 16
Say: Entropy is the baseline: how hard is the problem before any model? Cross-entropy is the scorecard. KL divergence is the gap between them. Mutual information asks the same question of two variables.
Key line: when a classifier's loss converges to the data's entropy, it has learned everything the data holds.
Say: Don't confuse entropy with cross-entropy. Entropy is about the truth alone. Cross-entropy is the price of using your prediction in its place.
GenAI link: For an LLM: the entropy of text is the floor, the training loss is the scorecard, and KL is the gap. RLHF adds one more KL term, which keeps the tuned model close to where it started.
Next: check yourself.
Check yourself
- A fair coin has 1 bit of entropy. What about a coin that lands heads 99% of the time: more or less?
- Why does a confidently wrong prediction cost so much more?
- Is KL(P∥Q) a distance? Why or why not?
- When would you rank features by mutual information instead of correlation?
Notes · slide 17
Do: pairs, three minutes, then take answers.
Answers: 1. Much less, about 0.08 bits. 2. The log of a small probability is a large negative number. 3. No. It's asymmetric and fails the triangle inequality. 4. When the relationship may not be a straight line.
Lab: watch for bits against nats. scikit-learn's mutual information is in nats. The mushroom lab has one feature that almost solves the problem. Use it to talk about a dataset that's too easy, and what that means for a capstone.
GenAI link: a bonus question. "Two LLMs report perplexity 8 and 10, with different tokenisers. Which is better?" Listen for: you can't tell. Perplexity is per token, and each tokeniser splits text into different tokens. Compare bits per character or per byte instead.
This week
- Read Chapter 5: Information Theory Basics.
- Take the Chapter 5 quiz.
- Run the notebooks: Chapter 5 (opens in a new tab).
- Optional: the further reading for week 4.
Slide 1 of 18: Information theory
Keys: → or Space next · ← back · Home/End · F full screen · N notes. Swipe on touch screens. "Save as PDF" opens the print dialog with one slide per page: choose "Save as PDF" as the printer. "Print handout" adds the speaker notes, in black on white.