Math for CS and ML · 15 min

Entropy and KL divergence: information in bits

Measure surprise in bits, then see what believing the wrong distribution costs, the loss every classifier minimises.

01Why it matters

How much is a message worth?

"It's sunny in the desert" tells you almost nothing; "it's snowing in the desert" is news. Information is surprise: the less likely an outcome, the more you learn when it happens.

In 1948 Claude Shannon turned that into a number, measured in bits. It sets the limit on how far any file can be compressed, and its cousin, cross-entropy, is the loss function that trains almost every classifier and language model.

02Key parts

The pieces you will see

surprise
log⁡2(1/p)\log_2(1/p) bits for an outcome with probability pp: 1 bit for a fair coin flip, 3 bits for a 1-in-8 event.
entropy H(P)
The average surprise of a distribution PP: ∑xp(x)log⁡21p(x)\sum_x p(x)\log_2\frac{1}{p(x)}.
cross-entropy H(P, Q)
The average surprise when outcomes follow PP but you expected QQ.
KL divergence
H(P,Q)−H(P)H(P, Q) - H(P): the extra bits you pay for believing QQ. Never negative.

03See it work

Measure the weather in bits

Each outcome's surprise is log₂(1/p); the entropy is their average, weighted by how often each happens.

Sun (1/2) is worth 1 bit, cloud (1/4) 2 bits, rain and snow (1/8) 3 bits each. Weighted by how often each happens, the average is 0.5⋅1+0.25⋅2+0.125⋅3+0.125⋅3=1.750.5 \cdot 1 + 0.25 \cdot 2 + 0.125 \cdot 3 + 0.125 \cdot 3 = 1.75 bits. The code 0, 10, 110, 111 achieves exactly that: short codes for common outcomes.

A forecaster who thinks all four are equally likely (QQ uniform) uses 2 bits each, so they pay 2−1.75=0.252 - 1.75 = 0.25 extra bits per day. That gap is the KL divergence.

04Predict

Your turn: count the bits

Make 4 predictions: step forward and answer each question before the next step plays.

0 of 4 made

05Try it

Drag the distributions

Drag the tops of the bars. The other bars rescale so each distribution still adds up to 1.

  • Make PP uniform: entropy reaches its maximum, log⁡2n\log_2 n.
  • Make one outcome almost certain: entropy drops towards 0.
  • Pull one of QQ's bars down to zero while PP's stays up: KL shoots to infinity.

06Check yourself

Check yourself

  1. Question 1 of 5

    How many bits of surprise is an outcome with probability 1/16?

    Your answer comes back in Review in a few days, so it sticks.

  2. Question 2 of 5

    Which distribution over 4 outcomes has the highest entropy?

  3. Question 3 of 5

    A classifier gives the correct class probability 0.25 on a sample. What is its cross-entropy loss there?

  4. Question 4 of 5

    When is KL(P‖Q) zero?

  5. Question 5 of 5

    KL(P‖Q) is 0.5 bits. What can you say about KL(Q‖P)?

07Explain it back

Say it in your own words

What does entropy measure, and why does training a model to minimise cross-entropy pull its beliefs towards the truth?

08Recap

What to remember

  • Surprise is log⁡2(1/p)\log_2(1/p) bits; entropy is the average surprise, at most log⁡2n\log_2 n for nn outcomes.
  • Cross-entropy is the average surprise under a model; KL is its excess over the entropy, never negative.
  • A model that rules out something that can happen pays infinite cross-entropy.
  • Machine learning minimises cross-entropy, which is the same as minimising KL from the data.
Back to path