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.
A coin is fair: p(H)=p(T)=0.5. What is H(X) in bits?
Recall Solution
What we do: plug into H(X)=−∑plog2p.
H(X)=−[0.5log20.5+0.5log20.5]Why:log20.5=−1 because 2−1=0.5.
=−[0.5(−1)+0.5(−1)]=1 bitAnswer: 1 bit. One binary question ("heads?") settles it.
Which is true, and why? (a) DKL(p∥q)=DKL(q∥p) always. (b) DKL(p∥q)≥0 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 0 only when p=q.
Answer: (b).
A biased coin has p(H)=0.9,p(T)=0.1. Compute H(X) in bits.
Recall Solution
H(X)=−[0.9log20.9+0.1log20.1]log20.9=−0.152, log20.1=−3.322.
=−[0.9(−0.152)+0.1(−3.322)]=0.137+0.332=0.469 bitsAnswer: ≈0.469 bits. Less uncertainty than the fair coin's 1 bit — usually heads, so less to learn.
A fair 8-sided die: p(x)=81 for x=1,…,8. Find H(X)without summing eight terms.
Recall Solution
What we do: use the maximum-entropy shortcut. For a uniform distribution over n outcomes,
H(X)=log2n.Why: every term is n1log2n, and there are n of them, so they collapse to log2n.
H(X)=log28=3 bitsAnswer: 3 bits. Three yes/no questions (binary search) pin down one of eight faces.
True label p=[0,1,0] (class 2); model prediction q=[0.1,0.7,0.2]. Compute the cross-entropy H(p,q) in nats.
Recall Solution
H(p,q)=−[0⋅ln0.1+1⋅ln0.7+0⋅ln0.2]=−ln0.7Why the zeros vanish: a one-hot label puts all weight on class 2, so only its term survives.
=−(−0.357)=0.357 natsAnswer: ≈0.357 nats. This is exactly the classification loss for this one example.
True coin p=(0.5,0.5), model q=(0.7,0.3). Compute DKL(p∥q) in bits.
Recall Solution
DKL(p∥q)=0.5log20.70.5+0.5log20.30.5log2(0.5/0.7)=log20.714=−0.485, log2(0.5/0.3)=log21.667=+0.737.
=0.5(−0.485)+0.5(0.737)=−0.243+0.369=0.126 bitsAnswer: ≈0.126 bits. The extra cost per flip of using model q when truth is p.
Same two coins, but compute the reverseDKL(q∥p). Compare to Exercise 3.1.
Recall Solution
DKL(q∥p)=0.7log20.50.7+0.3log20.50.3log21.4=0.485, log20.6=−0.737.
=0.7(0.485)+0.3(−0.737)=0.340−0.221=0.119 bitsAnswer: ≈0.119 bits. Different from 0.126 — this is direct proof KL is asymmetric. Now the truth is q and p is the model; the weighting out front changed.
For a two-outcome distribution p=(t,1−t), the entropy is the binary entropyHb(t)=−tlog2t−(1−t)log2(1−t). Show it is maximized at t=0.5 by finding where its derivative is zero.
Recall Solution
What we do: differentiate w.r.t. t. Using natural log then converting, dtdHb(t)=log2t1−t.
Why the derivative: the maximum of a smooth concave curve sits where its slope is 0.
Set log2t1−t=0⇒t1−t=1⇒t=0.5.
What it looks like: the ∩-shaped curve in the figure below peaks dead center at t=0.5, height 1 bit, and drops to 0 at both ends.
Answer: t=0.5, giving Hb=1 bit — the maximum.
Cross-entropy loss for one training example with true label class 2, p=[0,1,0]. The model's logits give q=[0.2,0.5,0.3]. Compute the loss in nats. Then say what happens to the loss as the model improves so q(class 2)→1.
Recall Solution
H(p,q)=−lnq2=−ln0.5=0.693 natsLimiting behaviour: as q2→1, −lnq2→−ln1=0 — the loss vanishes.
As q2→0, −lnq2→+∞ — an infinitely confident wrong prediction is infinitely penalized (matches the KL convention: p>0,q=0⇒∞).
Answer: 0.693 nats; loss →0 as the model gets the right class certain, →∞ if it drives the true class to zero.
A source emits symbols {A,B,C,D} with true probabilities p=(0.5,0.25,0.125,0.125). (a) Find H(p) in bits. (b) You compress using a code built for the uniform model q=(0.25,0.25,0.25,0.25). Find the average bits used, H(p,q). (c) Find the wasted bits DKL(p∥q) and confirm H(p,q)=H(p)+DKL(p∥q).
Recall Solution
(a) The probabilities are powers of 2, so surprisals are clean integers:
H(p)=−[0.5log20.5+0.25log20.25+0.125log20.125+0.125log20.125]=−[0.5(−1)+0.25(−2)+0.125(−3)+0.125(−3)]=0.5+0.5+0.375+0.375=1.75 bits(b) Under q, each symbol costs −log20.25=2 bits, so average =2 bits regardless of p:
H(p,q)=−∑xp(x)log20.25=2∑xp(x)=2 bits(c) Wasted:
DKL(p∥q)=H(p,q)−H(p)=2−1.75=0.25 bits
Check the identity: H(p)+DKL=1.75+0.25=2=H(p,q). ✓
Answer: H(p)=1.75, H(p,q)=2, DKL=0.25 bits. The uniform code wastes a quarter bit per symbol because it ignores that A is common.
Mixture / connective reasoning. Define the mixture m=21(p+q) and the Jensen–Shannon quantity JSD(p,q)=21DKL(p∥m)+21DKL(q∥m). For p=(1,0) and q=(0,1), compute JSD in bits.
Recall Solution
Mixture:m=(0.5,0.5).
First KL:DKL(p∥m)=1⋅log20.51+0⋅(⋯)=log22=1 bit (the q-side term has p=0 weight → 0).
Second KL: by symmetry DKL(q∥m)=1 bit.
JSD=21(1)+21(1)=1 bitAnswer: JSD=1 bit — the maximum, correctly finite even though DKL(p∥q)=∞ here (since q=0 where p>0). This is exactly whyJensen-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 n outcomes ::: log2n
KL is zero exactly when ::: p=q everywhere
Cross-entropy minus entropy equals ::: DKL(p∥q)
Binary entropy peaks at t= ::: 0.5, giving 1 bit
A confident wrong prediction (q→0 where p>0) makes the loss ::: →∞