Skip to content

Information theory problem set, part a: entropy, chain rules, KL and cross-entropy

ModuleS-M11a · solve · none · Pass 2 · 3 to 4 h
You buildanswers in solve/S-M11a.toml (16 checked by SymPy) and 2 proofs in solve/S-M11a/qN.md (self-graded against their rubrics)
Contractnone: a pen and paper set
Testscourse/solve/S-M11a/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M11a/problems.md and in section 4
NeedsS-M07a (distributions, expectation, Bayes). Reading: the Information Theory topic, entropy and KL sections
Used byno call site (a solve set). It checks the definitions behind M11.1 (entropy, cross_entropy, kl, kl_from_logprobs, js, kl_k3), which M08.3, L0.3, L8.1, and L12.3 call; part b, S-M11b in Pass 3, covers coding, mutual information, and maximum entropy
MilestoneMS-P2 (the Pass 2 gate runs ol check on every solve part of the pass)
Optional depthCover and Thomas, Elements of Information Theory, ch. 2; MacKay, Information Theory, Inference, and Learning Algorithms (free), ch. 2 and 4; Schulman, “Approximating KL Divergence” (2020 blog note) for k3
  • Entropy is the expected surprise −log⁡p-\log p; it is log⁡V\log V for a uniform distribution over VV outcomes and less for anything else, so an untrained byte model starts at log⁡256≈5.55\log 256 \approx 5.55 nats (q1, q2, q7).
  • The unit is the base of the logarithm: bits for log⁡2\log_2, nats for ln⁡\ln; mixing them is the most common wrong answer (q1, q3, q7).
  • The chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) is why next-token cross-entropy summed over a sequence is the sequence’s log-loss (q4, q5).
  • Cross-entropy is entropy plus KL divergence, KL is never negative, and it is not symmetric (q6, q9).
  • The k3 estimator er−r−1e^r - r - 1 is nonnegative for every sample, unlike the plain log-ratio (q8).
Terminal window
ol start S-M11a # writes solve/S-M11a.toml and one file per proof
ol check S-M11a # SymPy checks the answers, then asks each proof rubric (y/n)
ol check S-M11a --regrade # ask the rubrics again after you change a proof

Your Pass 1 bigram reported a loss of about 3.2 nats per byte, and the number meant nothing yet. In Pass 2 it starts to carry weight: L0.3 trains with cross-entropy, M11.1 writes entropy, cross_entropy, and kl that the sampler (L8.1) and the evaluation suite (L6.7) log, and the gap between your model’s loss and the data’s entropy is the KL divergence your optimizer is shrinking. Later, L12.3 keeps a fine-tuned policy close to its reference with a KL penalty estimated by k3. This set fixes the definitions, the units, and the identities (chain rule, cross-entropy equals entropy plus KL, KL is nonnegative) before you code them.

SymbolMeaningType / shape
p,qp, qprobability distributions over a finite set, pi≥0p_i \ge 0, ∑ipi=1\sum_i p_i = 1float[V]
log⁡b\log_blogarithm in base bb: b=2b = 2 gives bits, b=eb = e gives nats
H(p)H(p)entropy, −∑ipilog⁡pi-\sum_i p_i \log p_iscalar ≥0\ge 0
H(p,q)H(p, q)cross-entropy, −∑ipilog⁡qi-\sum_i p_i \log q_iscalar
DKL(p ∥ q)D_{KL}(p \,\Vert\, q)KL divergence, ∑ipilog⁡(pi/qi)\sum_i p_i \log (p_i / q_i)scalar ≥0\ge 0
H(X,Y)H(X, Y)joint entropy of a pair of random variablesscalar
H(Y∣X)H(Y \mid X)conditional entropy, ∑xP(x)H(Y∣X=x)\sum_x P(x) H(Y \mid X = x)scalar
Hb(p)H_b(p)binary entropy of a coin with P(1)=pP(1) = pscalar
rrlog-ratio log⁡pref(x)−log⁡p(x)\log p_{\text{ref}}(x) - \log p(x) of one samplescalar

The surprise of an outcome with probability pip_i is −log⁡pi-\log p_i: certain outcomes surprise you not at all, rare ones a lot, and the surprises of independent outcomes add. Entropy is the expected surprise, H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i \log p_i, with 0log⁡0=00 \log 0 = 0 (the limit of tlog⁡tt \log t as t→0t \to 0). In bits it is the average number of yes/no questions an optimal strategy needs to identify the outcome: 1 for a fair coin, 8 for a uniform byte. It is 0 for a certain outcome and at most log⁡V\log V over VV outcomes, with the maximum exactly at the uniform distribution. Changing the base rescales it: Hnats=Hbits⋅ln⁡2H_{\text{nats}} = H_{\text{bits}} \cdot \ln 2.

For a coin, Hb(p)=−plog⁡2p−(1−p)log⁡2(1−p)H_b(p) = -p \log_2 p - (1-p)\log_2(1-p), symmetric about p=1/2p = 1/2, where it peaks at 1 bit.

For a pair (X,Y)(X, Y) the joint entropy is the entropy of the joint distribution. The conditional entropy H(Y∣X)H(Y \mid X) averages, over xx, the entropy of YY‘s distribution once X=xX = x is known. The chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) (q5) says that the surprise of a pair is the surprise of the first plus the surprise of the second given the first; for a sequence, H(X1,…,XT)=∑tH(Xt∣X<t)H(X_1, \dots, X_T) = \sum_t H(X_t \mid X_{<t}). A language model is a product of next-token conditionals, and its training loss is this sum. Conditioning never increases entropy on average: H(Y∣X)≤H(Y)H(Y \mid X) \le H(Y).

If data come from pp but you encode or predict with qq, the expected surprise is the cross-entropy H(p,q)=−∑ipilog⁡qiH(p, q) = -\sum_i p_i \log q_i. A model’s training loss on a corpus is an estimate of H(pdata,qmodel)H(p_{\text{data}}, q_{\text{model}}). The excess over the best possible, DKL(p ∥ q)=H(p,q)−H(p)=∑ipilog⁡piqiD_{KL}(p \,\Vert\, q) = H(p, q) - H(p) = \sum_i p_i \log \frac{p_i}{q_i}, is the KL divergence. Gibbs’ inequality (q9) says it is at least 0, with equality only when q=pq = p, so minimizing cross-entropy in qq drives qq toward pp. KL is not symmetric and is not a distance: DKL(p ∥ q)D_{KL}(p \,\Vert\, q) is infinite when qq gives zero probability to something pp can produce, which is why a model must never assign probability exactly 0.

In RL fine-tuning you see one sample x∼px \sim p at a time and want DKL(p ∥ pref)D_{KL}(p \,\Vert\, p_{\text{ref}}). With r=log⁡pref(x)−log⁡p(x)r = \log p_{\text{ref}}(x) - \log p(x), the estimator k1=−rk_1 = -r is unbiased but often negative for single samples. k3 =er−r−1= e^r - r - 1 is also unbiased, because Ep[er]=∑xp(x)pref(x)p(x)=1E_p[e^r] = \sum_x p(x) \frac{p_{\text{ref}}(x)}{p(x)} = 1, so E[k3]=1+E[−r]−1=DKLE[k_3] = 1 + E[-r] - 1 = D_{KL}; and since er≥1+re^r \ge 1 + r (the same tangent-line bound as Gibbs’), every single value is nonnegative.

This is a sibling of q4 and q6, not one of the graded problems.

Joint and conditional entropy. XX and YY in {0,1}\{0, 1\} with P(0,0)=1/4P(0,0) = 1/4, P(0,1)=1/4P(0,1) = 1/4, P(1,0)=1/2P(1,0) = 1/2, P(1,1)=0P(1,1) = 0.

  • Joint, in bits: H(X,Y)=14⋅2+14⋅2+12⋅1+0=3/2H(X, Y) = \tfrac14 \cdot 2 + \tfrac14 \cdot 2 + \tfrac12 \cdot 1 + 0 = 3/2.
  • Marginal: P(X=0)=1/2P(X=0) = 1/2, P(X=1)=1/2P(X=1) = 1/2, so H(X)=1H(X) = 1.
  • Conditional, computed directly: given X=0X = 0, YY is (1/2,1/2)(1/2, 1/2) with entropy 1; given X=1X = 1, Y=0Y = 0 surely, entropy 0. So H(Y∣X)=12⋅1+12⋅0=1/2H(Y \mid X) = \tfrac12 \cdot 1 + \tfrac12 \cdot 0 = 1/2.
  • Chain rule check: H(X)+H(Y∣X)=1+1/2=3/2=H(X,Y)H(X) + H(Y \mid X) = 1 + 1/2 = 3/2 = H(X, Y).

Cross-entropy and KL. p=(1,0)p = (1, 0) and q=(1/2,1/2)q = (1/2, 1/2): H(p)=0H(p) = 0, H(p,q)=−1⋅log⁡2(1/2)=1H(p, q) = -1 \cdot \log_2(1/2) = 1 bit, so DKL(p ∥ q)=1D_{KL}(p \,\Vert\, q) = 1 bit. The other direction, DKL(q ∥ p)=12log⁡21/21+12log⁡21/20D_{KL}(q \,\Vert\, p) = \tfrac12 \log_2 \frac{1/2}{1} + \tfrac12 \log_2 \frac{1/2}{0}, is infinite: pp gives zero probability to an outcome qq produces. In solve/ a bits answer such as 1−12log⁡231 - \tfrac12\log_2 3 is written 1 - log(3, 2)/2.

Write each answer in solve/S-M11a.toml; lettered parts are their own tables:

[q1.c]
answer = "3/2"
[q2]
answer = "log(V)"
[q6.b]
answer = "1 - log(3, 2)/2"
[q9]
proof = "S-M11a/q9.md"

Bits use log(x, 2); nats use log(x). Give exact values, not decimals.

q1. Give the entropy in bits of (a) a fair coin, (b) a uniform byte (256 equally likely values), (c) the distribution p=(1/2,1/4,1/4)p = (1/2, 1/4, 1/4). [number]

q2. Give the entropy in nats of the uniform distribution over VV outcomes. [expr in V]

q3. The binary entropy is Hb(p)=−plog⁡2p−(1−p)log⁡2(1−p)H_b(p) = -p \log_2 p - (1 - p) \log_2 (1 - p) bits. (a) Which pp maximizes it? (b) Give Hb(1/4)H_b(1/4). [number]

q4. XX and YY take values in {0,1}\{0, 1\} with joint distribution P(0,0)=1/2P(0,0) = 1/2, P(0,1)=1/4P(0,1) = 1/4, P(1,0)=0P(1,0) = 0, P(1,1)=1/4P(1,1) = 1/4. In bits, give (a) H(X,Y)H(X, Y), (b) H(X)H(X), (c) H(Y∣X)H(Y \mid X). [number]

q5. Prove the chain rule H(X,Y)=H(X)+H(Y∣X)H(X, Y) = H(X) + H(Y \mid X) for discrete random variables, where H(Y∣X)=∑xP(x)H(Y∣X=x)H(Y \mid X) = \sum_x P(x) H(Y \mid X = x). [proof]

q6. Let p=(1/2,1/2)p = (1/2, 1/2) and q=(1/4,3/4)q = (1/4, 3/4). In bits, give (a) the cross-entropy H(p,q)H(p, q), (b) DKL(p ∥ q)D_{KL}(p \,\Vert\, q), (c) DKL(q ∥ p)D_{KL}(q \,\Vert\, p). [number] (d) Is DKL(p ∥ q)=DKL(q ∥ p)D_{KL}(p \,\Vert\, q) = D_{KL}(q \,\Vert\, p) for all distributions p,qp, q? [bool]

q7. A byte-level model that assigns probability 1/2561/256 to every next byte is evaluated on any text. Give its cross-entropy loss in nats per byte. [number]

q8. The k3 estimator of KL used in RL fine-tuning is k3(r)=er−r−1k_3(r) = e^r - r - 1, with r=log⁡pref(x)−log⁡p(x)r = \log p_{\text{ref}}(x) - \log p(x) for a sample x∼px \sim p. (a) Give k3(log⁡2)k_3(\log 2). [number] (b) Is k3(r)≥0k_3(r) \ge 0 for every real rr? [bool]

q9. Prove Gibbs’ inequality: DKL(p ∥ q)≥0D_{KL}(p \,\Vert\, q) \ge 0 for distributions p,qp, q on a finite set with qi>0q_i > 0 wherever pi>0p_i > 0, with equality exactly when p=qp = q. [proof]

PitfallSymptomCaught by
Reporting nats where bits were asked, or the reversebits-per-byte off by a factor ln⁡2\ln 2q1 (canary log(2)), q2 (canary in bits), q7 (canary 8)
Mixing a natural log into a bits formulaa value that is neither unitq3 (canary 2 - 3*log(3)/4)
Counting a zero-probability cellentropy too high, or NaN from 0log⁡00 \log 0 in codeq4 (canary 2)
Taking H(Y)H(Y) or H(X,Y)H(X, Y) for H(Y∣X)H(Y \mid X)sequence losses that do not add upq4 (canaries)
Swapping the arguments of KLa penalty that weights the wrong distribution’s errorsq6 (canaries: the other direction)
Reporting the cross-entropy as the KL“the model is 1.2 bits from the data” when it is 0.2q6 (canary: the cross-entropy)
Using the raw log-ratio as a per-sample KLnegative KL values in training logsq8 (canary log(2))
DirectionModuleHow it uses this
BackS-M07adistributions, expectation, and conditioning
ForwardM11.1entropy, cross_entropy, kl, js, kl_k3 are this set as code, with the Gibbs property test
ForwardM08.3the cross-entropy VJP, softmax minus one-hot
ForwardL0.3fused cross-entropy, the training loss in nats
ForwardL8.1the sampler logs the entropy of each next-token distribution
ForwardM11.2perplexity eHe^{H} and bits per byte from the same NLL sums
ForwardL12.3the KL penalty to a reference policy, estimated with k3