1.3.18 · D2Probability & Statistics

Visual walkthrough — Entropy and KL divergence

1,921 words9 min readBack to topic

This page rebuilds the central result of the parent topic — the KL divergence formula — starting from nothing but a coin and a question. We draw every step. By the end you will see why

is exactly "the extra bits you waste when you believe the wrong story about the world."

Nothing here assumes you know what a logarithm is for, what "bits" mean, or why we sum. We build each piece on a picture first.


Step 1 — What is a "message" and why do we count bits?

WHAT we set up: a random outcome (a coin, a die, a weather label) and a rule that turns each possible outcome into a string of 0s and 1s. That rule is a code.

WHY we care: fewer bits = cheaper messages = better compression = (later) better loss functions. Every quantity on this page is secretly a bit count.

PICTURE: below, four possible outcomes each get a little binary tag. Count how many yes/no splits it takes to reach each leaf — that count is the bit-length of that outcome.


Step 2 — Why the best length for outcome is

Here the logarithm enters. We must justify why the log, and not some other function.

WHAT we claim: if outcome happens with probability , the shortest possible average code gives it a length of

WHY the log? Each yes/no split cuts the number of still-possible outcomes roughly in half. To pin down one outcome out of equally likely ones you need to halve down to — that is halvings. If an outcome has probability , it behaves like one choice out of equally likely ones, so it needs splits. The log is literally "how many times can I halve until I'm sure?" No other function counts halvings.

Read the formula term by term:

  • — how common outcome is. Big → common → few bits.
  • — "one in how many" this outcome feels like.
  • — converts "one in how many" into "how many yes/no halvings."
  • the minus sign — because makes negative; we flip it so lengths come out positive.

PICTURE: the curve . Certain events () sit at height — no bits, no surprise. Rare events shoot up.


Step 3 — Averaging the surprises gives entropy

WHAT we do: outcomes don't all cost the same, so we take the average bit-length, weighting each outcome by how often it actually occurs.

WHY weight by ? Because a rare outcome is expensive but seldom sent, so it should count little in the long-run average. "Expected value" = probability-weighted sum. That is exactly what the pattern means.

PICTURE: bars of surprisal height , each shrunk by its width ; the shaded total area is — the average message length if you use the best code for .


Step 4 — The mistake: coding for when reality is

Now the twist that creates KL divergence.

WHAT happens: you believe the outcomes follow a distribution (your model, your guess). So you build the optimal code for : outcome gets length . But reality actually follows . Your messages still get sent at whatever frequency dictates.

WHY this matters: your bit-lengths come from , but the how-often weighting comes from . Average length is now

This mismatched average is the cross-entropy .

PICTURE: two codes side by side for the same outcomes. The left (blue) is built for — it makes the outcome thinks is common short. But reality (pink bars, following ) sends a different outcome most often, so on the wrong code the frequent real outcome landed on a long branch. Wasted length in yellow.


Step 5 — Subtract: the waste is KL divergence

WHAT we compute: the extra bits you pay for using the wrong code = (what you actually paid) − (the best you could have paid).

WHY subtract? Because "waste" only makes sense relative to the best possible. Both terms are average bit-lengths under the same real frequencies ; their difference is the pure penalty of your wrong belief.

Now expand both and watch the terms combine:

Pull the common and the common sum together:

The bracket used one rule: — subtracting logs = dividing inside. That is the whole algebra.

PICTURE: the tall cross-entropy bar minus the shorter entropy bar; the leftover slab (yellow) is — the extra bits, term by term.

Term-by-term, right where each symbol lives:

  • (outside) — real frequency: penalties for outcomes that never really happen are ignored.
  • — "how much more (or less) common reality is than your model thinks."
  • — turns that ratio into bits; ratio (you were right) → → no penalty.

Step 6 — Why it is never negative (a picture of Jensen)

WHAT we prove: , and it's only when everywhere. You can never save bits by being wrong.

WHY believe it? Flip the ratio and use one fact about the log curve: is concave (bends downward, like a frown). For a concave curve, the average of the curve's heights sits below the curve above the average input — that's Jensen's inequality.

The cancels the in the denominator, leaving because is a probability distribution. So the whole thing is .

PICTURE: the concave curve; a straight chord below it; the gap between "average of heights" and "height of average" is exactly the KL penalty — it closes to zero only when all the ratios equal .

Recall Why zero only when p = q

When does equality hold in Jensen ::: When the input is the same constant for every ; since both sum to 1, that constant is 1, i.e. .


Step 7 — The degenerate cases (never let the reader get stuck)

Formulas break at zeros. Here is every edge, drawn.

PICTURE: the ratio-to-penalty curve with the three regimes marked: pit of zero penalty, (model too confident against reality) rising, and the wall at .


Step 8 — Numbers on the picture (worked, both directions)


The one-picture summary

Everything above is one subtraction: your average message length (using the wrong story ) minus the shortest possible length (the true story ). The leftover is .

Recall Feynman retelling (say it out loud)

A friend keeps sending you coin results as yes/no answers. If you knew the true odds , you'd design a codebook that gives common outcomes short tags — your bill averages bits, the cheapest possible. But you only guessed the odds , so you built the codebook for . Reality still sends outcomes at rate , so your bill is now , which is bigger whenever your guess was off. The overcharge — the wasted bits from believing the wrong story — is . It's never negative (you can't beat the true codebook — Jensen guarantees it), it's exactly zero only when your guess was perfect, it explodes to infinity if you bet zero on something that actually happens, and it is not symmetric because the overcharge depends on who's actually flipping. Minimizing this overcharge is what training a classifier, an ELBO, or a VAE is quietly doing.

Where this goes next: Mutual Information (KL between a joint and its independent copy), Maximum Entropy Principle (Step 3 pushed to its limit), Information Gain (entropy drop from a split), and F-divergences (KL as one member of a whole family).