1.3.18 · D4Probability & Statistics

Exercises — Entropy and KL divergence

1,886 words9 min readBack to topic

This is a self-testing page for Entropy and KL divergence. Every problem hides a full worked solution — try it first, then reveal. Levels climb from L1 Recognition to L5 Mastery. Each level ends with a trap that is easy to fall into, steel-manned so you see why the wrong path feels right.

Figure — Entropy and KL divergence

Level 1 — Recognition

Exercise 1.1 (L1)

A coin is fair: . What is in bits?

Recall Solution

What we do: plug into . Why: because . Answer: bit. One binary question ("heads?") settles it.

Exercise 1.2 (L1)

A variable is deterministic: and every other outcome has probability . What is ?

Recall Solution

Only the term survives (others use the convention ): Answer: bits. No surprise — you already know the outcome.

Exercise 1.3 (L1)

Which is true, and why? (a) always. (b) always.

Recall Solution

(a) is false — KL is asymmetric; the two arguments play different roles (which is "truth" vs "model"). (b) is true — by Gibbs' inequality, KL is never negative, and equals only when . Answer: (b).


Level 2 — Application

Exercise 2.1 (L2)

A biased coin has . Compute in bits.

Recall Solution

, . Answer: bits. Less uncertainty than the fair coin's bit — usually heads, so less to learn.

Exercise 2.2 (L2)

A fair 8-sided die: for . Find without summing eight terms.

Recall Solution

What we do: use the maximum-entropy shortcut. For a uniform distribution over outcomes, Why: every term is , and there are of them, so they collapse to . Answer: bits. Three yes/no questions (binary search) pin down one of eight faces.

Exercise 2.3 (L2)

True label (class 2); model prediction . Compute the cross-entropy in nats.

Recall Solution

Why the zeros vanish: a one-hot label puts all weight on class 2, so only its term survives. Answer: nats. This is exactly the classification loss for this one example.


Level 3 — Analysis

Exercise 3.1 (L3)

True coin , model . Compute in bits.

Recall Solution

, . Answer: bits. The extra cost per flip of using model when truth is .

Exercise 3.2 (L3)

Same two coins, but compute the reverse . Compare to Exercise 3.1.

Recall Solution

, . Answer: bits. Different from — this is direct proof KL is asymmetric. Now the truth is and is the model; the weighting out front changed.

Exercise 3.3 (L3)

Verify the identity numerically for , (use bits).

Recall Solution

bit (fair coin, Ex 1.1). Then bits — matches Exercise 3.1. ✓ Answer: identity confirmed; bits both ways.


Level 4 — Synthesis

Exercise 4.1 (L4)

For a two-outcome distribution , the entropy is the binary entropy . Show it is maximized at by finding where its derivative is zero.

Recall Solution

What we do: differentiate w.r.t. . Using natural log then converting, . Why the derivative: the maximum of a smooth concave curve sits where its slope is . Set . What it looks like: the -shaped curve in the figure below peaks dead center at , height bit, and drops to at both ends. Answer: , giving bit — the maximum.

Figure — Entropy and KL divergence

Exercise 4.2 (L4)

Cross-entropy loss for one training example with true label class 2, . The model's logits give . Compute the loss in nats. Then say what happens to the loss as the model improves so .

Recall Solution

Limiting behaviour: as , — the loss vanishes. As , — an infinitely confident wrong prediction is infinitely penalized (matches the KL convention: ). Answer: nats; loss as the model gets the right class certain, if it drives the true class to zero.


Level 5 — Mastery

Exercise 5.1 (L5)

A source emits symbols with true probabilities . (a) Find in bits. (b) You compress using a code built for the uniform model . Find the average bits used, . (c) Find the wasted bits and confirm .

Recall Solution

(a) The probabilities are powers of , so surprisals are clean integers: (b) Under , each symbol costs bits, so average bits regardless of : (c) Wasted: Check the identity: . ✓ Answer: , , bits. The uniform code wastes a quarter bit per symbol because it ignores that is common.

Exercise 5.2 (L5)

Mixture / connective reasoning. Define the mixture and the Jensen–Shannon quantity . For and , compute JSD in bits.

Recall Solution

Mixture: . First KL: bit (the -side term has weight → ). Second KL: by symmetry bit. Answer: bit — the maximum, correctly finite even though here (since where ). This is exactly why Jensen-Shannon Divergence is preferred as a symmetric, always-finite alternative, and how KL enters the Evidence Lower Bound (ELBO) and Variational Autoencoders.


Recall Quick self-check (cloze)

Uniform entropy over outcomes ::: KL is zero exactly when ::: everywhere Cross-entropy minus entropy equals ::: Binary entropy peaks at ::: , giving bit A confident wrong prediction ( where ) makes the loss :::

See also: Mutual Information, Maximum Entropy Principle, Cross-Entropy Loss, Information Gain, F-divergences.