Chapter 9 measured surprise in bits for a single distribution. What if you have two distributions — the real one, and your model's guess at it — and want to measure how far apart they are?
P is fixed. Drag Q's bars around and watch the readout shrink toward zero as Q starts to look like P — and grow the more they disagree.
The KL divergence — short for Kullback–Leibler divergence — measures the extra bits it costs to describe data from using a code built for instead of 's own optimal code:
- — the divergence itself, in bits: how many extra bits 's code wastes on data actually drawn from .
- — the true probability of outcome .
- — the model's (or approximate) probability of that same outcome .
- — one outcome, ranging over everything either distribution assigns probability to.
- Never negative, zero only at a perfect match
always, and it hits exactly only when everywhere — the only way to waste zero bits on 's code is for to already be the right code.
- Not symmetric — direction matters
in general. Despite measuring how "far apart" two distributions are, swapping which one is the reference changes the answer, so it isn't a true distance.
- Related to cross-entropy
Cross-entropy always decomposes as — Part II's cross-entropy loss chapter is exactly this , with the true one-hot label.
Now watch both directions at once. and move together, roughly, but they're never quite equal — drag until they're close and notice they still don't match exactly.
With and :
- KL divergence, one direction
Compute each outcome's term separately:
- The third term is exactly since and agree there ()
Summing:
- The reverse direction gives a different number
Same process, swapped:
- The third term is again
Summing:
— close to the forward value here, but not identical, confirming the asymmetry.
- Mutual information: KL applied to independence itself
For a joint distribution over two binary variables, and — and agree far more than chance. Both marginals are uniform, , so bit.
The joint entropy sums the same recipe over all four outcomes:
- , for each of the two outcomes
- , for each of the two outcomes
Mutual information is exactly the gap: bits — equivalently, it's the KL divergence between the real joint distribution and the independent one its own marginals would predict. Zero mutual information means knowing tells you nothing about ; here, it tells you a fair amount.
Drag Q until D_KL(P‖Q) drops under 0.02 bits — i.e., reshape it to closely match P = (0.5, 0.3, 0.2).
KL divergence is entropy's natural extension to comparing two distributions instead of describing one; mutual information is what happens when you apply that same comparison to a joint distribution against the independent version of itself. Both show up constantly downstream — in cross-entropy loss, in variational autoencoders' regularizer, and in feature-selection methods that rank inputs by how much information they share with the target.