Skip to content

Information theory problem set, part b: coding and Huffman, mutual information, maximum entropy, rate-distortion

ModuleS-M11b · solve · none · Pass 3 · 4 to 5 h
You buildanswers in solve/S-M11b.toml (19 checked by SymPy) and 3 proofs in solve/S-M11b/q4.md, q7.md, and q9.md (self-graded against their rubrics)
Contractnone: a pen and paper set
Testscourse/solve/S-M11b/key.toml (hidden): typed answers plus reject canaries; the problems are in course/solve/S-M11b/problems.md and in section 4
Needsno module. Reading: S-M11a (entropy, KL, Gibbs’ inequality), M11.2 (perplexity and bits per byte), M11.4 (mutual information and PMI), and the Information Theory topic
Used byno call site (a solve set). It checks the definitions behind M11.4 (which L2.3 calls), the temperature semantics of L8.1’s sampler (the solve-only M11.5), the bits-per-sample view of quantization in L8.5 (the solve-only M11.6), and the coding theorems behind the optional M11.3
MilestoneMS-P3 (the Pass 3 gate runs ol check on every solve part of the pass)
Optional depthCover and Thomas, Elements of Information Theory, ch. 2.4 to 2.6, 5.1 to 5.8, 10.1 to 10.3, 12.1; MacKay, Information Theory, Inference, and Learning Algorithms (free), ch. 5 and 8; Jaynes, “Information theory and statistical mechanics” (1957)
  • The entropy is the best expected code length: a Huffman code reaches it exactly for dyadic probabilities and stays within one bit of it otherwise (q1, q2), and every prefix code obeys Kraft’s inequality (q3, q4).
  • Mutual information is the expected PMI; it is symmetric, zero only for independent variables, and equals H(X)+H(Y)−H(X,Y)H(X) + H(Y) - H(X, Y) (q5 to q7).
  • Softmax at temperature TT is the maximum-entropy distribution with a fixed expected logit; T→∞T \to \infty flattens it to uniform and T→0T \to 0 sharpens it to the argmax (q8, q9).
  • Rate-distortion gives the fewest bits per symbol for an allowed distortion; for a Gaussian, each extra bit divides the mean squared error by 4 (q10, q11).
Terminal window
ol start S-M11b # writes solve/S-M11b.toml and one file per proof
ol check S-M11b # SymPy checks the answers, then asks each proof rubric (y/n)
ol check S-M11b --regrade # ask the rubrics again after you change a proof

Pass 3 is where information theory becomes things your system does. M11.2 turned your models’ losses into bits per byte, which is literally a compression rate: a model with 2 bits per byte could drive an arithmetic coder that shrinks text to a quarter of its size, and this set proves why no code can beat the entropy. M11.4 built mutual information and PMI for the word-vector baseline of L2.3; here you compute them by hand. Two topics have no module of their own and live only in this set: the maximum-entropy reading of softmax, which is what the temperature knob of L8.1’s sampler means, and rate-distortion, the theory behind L8.5’s quantizers trading bits for error.

SymbolMeaningType / shape
pip_iprobability of symbol iiscalar
ℓi\ell_ilength in bits of symbol ii‘s codewordint
L=∑ipiℓiL = \sum_i p_i \ell_iexpected code lengthbits
H(p)=−∑ipilog⁡2piH(p) = -\sum_i p_i \log_2 p_ientropybits
∑i2−ℓi\sum_i 2^{-\ell_i}Kraft sumscalar
I(X;Y)I(X; Y)mutual informationbits or nats
PMI⁡(x,y)=log⁡P(x,y)P(x)P(y)\operatorname{PMI}(x, y) = \log \frac{P(x, y)}{P(x) P(y)}pointwise mutual informationscalar
ziz_i, TTlogits and temperaturescalars
EiE_i, β\betaenergies and inverse temperature (zi=−Eiz_i = -E_i, T=1/βT = 1/\beta)scalars
DDallowed average distortionscalar
R(D)R(D)rate-distortion function: fewest bits per symbol at distortion DDbits
Hb(q)H_b(q)binary entropy −qlog⁡2q−(1−q)log⁡2(1−q)-q\log_2 q - (1-q)\log_2(1-q)bits

A prefix code assigns each symbol a bit string so that no codeword is the start of another; a stream of codewords then decodes without separators. Kraft’s inequality (q4) says any prefix code’s lengths satisfy ∑i2−ℓi≤1\sum_i 2^{-\ell_i} \le 1, and conversely any lengths satisfying it have a prefix code. Minimizing LL under that constraint gives ℓi=−log⁡2pi\ell_i = -\log_2 p_i and L=H(p)L = H(p): no code beats the entropy. Lengths must be integers, so the Shannon code rounds up, ℓi=⌈−log⁡2pi⌉\ell_i = \lceil -\log_2 p_i \rceil, and lands within one bit: H≤L<H+1H \le L < H + 1. The Huffman code is optimal among prefix codes: repeatedly merge the two least likely symbols (or groups) into one node; each symbol’s length is the number of merges it took part in.

I(X;Y)=∑x,yP(x,y)log⁡P(x,y)P(x)P(y)I(X; Y) = \sum_{x, y} P(x, y) \log \frac{P(x, y)}{P(x)P(y)}: the KL divergence from the joint to the product of its marginals, the expected PMI, and also H(X)+H(Y)−H(X,Y)=H(Y)−H(Y∣X)H(X) + H(Y) - H(X, Y) = H(Y) - H(Y \mid X). It is symmetric, non-negative, and zero exactly when XX and YY are independent. A single pair’s PMI can be negative (seen together less than chance); only the average must be non-negative.

Among all distributions with a given expected energy, the one with the most entropy is the Gibbs distribution pi=e−βEi/Z(β)p_i = e^{-\beta E_i}/Z(\beta) (q9). With logits zi=−Eiz_i = -E_i and T=1/βT = 1/\beta, that is softmax at temperature TT: pi∝ezi/Tp_i \propto e^{z_i / T}. Dividing logits by T<1T < 1 sharpens the distribution, T>1T > 1 flattens it; as T→∞T \to \infty every zi/T→0z_i/T \to 0 and the distribution becomes uniform, and as T→0T \to 0 it concentrates on the largest logit, which is greedy decoding (D11 treats T=0T = 0 as greedy).

Lossy compression asks for the fewest bits per symbol that reproduce a source within an average distortion DD. For a fair coin with Hamming distortion (the fraction of flipped bits), R(D)=1−Hb(D)R(D) = 1 - H_b(D) for 0≤D≤1/20 \le D \le 1/2: allowing errors saves exactly the entropy of the error pattern, and at D=1/2D = 1/2 you can guess without sending anything. For a Gaussian source with variance σ2\sigma^2 and squared error, R(D)=12log⁡2(σ2/D)R(D) = \frac12 \log_2(\sigma^2/D), so D=σ2⋅2−2RD = \sigma^2 \cdot 2^{-2R}: every extra bit divides the error by 4, the “6 dB per bit” rule of quantization.

These are siblings of q1, q5, q8, and q10, not graded problems.

Huffman. Probabilities (1/2,1/4,1/4)(1/2, 1/4, 1/4): merge the two quarters (one node of 1/21/2), then that node with the 1/21/2. Lengths (1,2,2)(1, 2, 2), L=1/2+1/2+1/2=3/2L = 1/2 + 1/2 + 1/2 = 3/2 bits, and H=1/2⋅1+2⋅1/4⋅2=3/2H = 1/2 \cdot 1 + 2 \cdot 1/4 \cdot 2 = 3/2: dyadic probabilities are coded at exactly the entropy. Kraft sum: 1/2+1/4+1/4=11/2 + 1/4 + 1/4 = 1.

Mutual information. P(0,0)=P(1,1)=1/2P(0, 0) = P(1, 1) = 1/2, P(0,1)=P(1,0)=0P(0, 1) = P(1, 0) = 0 (Y=XY = X, a fair bit): the marginals are uniform, PMI⁡(0,0)=log⁡21/21/4=1\operatorname{PMI}(0, 0) = \log_2 \frac{1/2}{1/4} = 1 bit, and I=2⋅12⋅1=1I = 2 \cdot \frac12 \cdot 1 = 1 bit =H(X)= H(X): knowing XX removes all of YY‘s uncertainty. Written in solve/ as 1.

Temperature. Logits z=(0,ln⁡3)z = (0, \ln 3): at T=1T = 1, p=(1/4,3/4)p = (1/4, 3/4); at T=1/2T = 1/2, e2z=(1,9)e^{2 z} = (1, 9) and p=(1/10,9/10)p = (1/10, 9/10), sharper.

Rate-distortion. A fair coin at D=1/4D = 1/4: Hb(1/4)=2−34log⁡23H_b(1/4) = 2 - \frac34 \log_2 3, so R(1/4)=34log⁡23−1≈0.19R(1/4) = \frac34\log_2 3 - 1 \approx 0.19 bits per symbol, written 3*log(3, 2)/4 - 1. A Gaussian at D=σ2/4D = \sigma^2/4: R=12log⁡24=1R = \frac12\log_2 4 = 1 bit.

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

[q1.a]
answer = "[1, 2, 3, 3]"
[q5.a]
answer = "3*log(3, 2)/4 - 1"
[q8.c]
answer = "b"
[q4]
proof = "S-M11b/q4.md"

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

q1. A source emits four symbols with probabilities (1/2,1/4,1/8,1/8)(1/2, 1/4, 1/8, 1/8). (a) Give the codeword lengths of a binary Huffman code, in the order of the probabilities. [vector] (b) Give its expected length in bits. [number] (c) Is that expected length equal to the entropy of the source? [bool]

q2. For probabilities (0.4,0.3,0.2,0.1)(0.4, 0.3, 0.2, 0.1): (a) give the expected length in bits of a binary Huffman code; (b) give the Kraft sum ∑i2−ℓi\sum_i 2^{-\ell_i} of its codeword lengths ℓi\ell_i. [number]

q3. For the same probabilities (0.4,0.3,0.2,0.1)(0.4, 0.3, 0.2, 0.1): (a) give the expected length in bits of the Shannon code, whose lengths are ℓi=⌈log⁡2(1/pi)⌉\ell_i = \lceil \log_2 (1/p_i) \rceil. [number] (b) Does a binary prefix code with codeword lengths (1,2,2,3)(1, 2, 2, 3) exist? [bool]

q4. Prove the Kraft inequality: the codeword lengths ℓ1,…,ℓm\ell_1, \dots, \ell_m of any binary prefix code satisfy ∑i2−ℓi≤1\sum_i 2^{-\ell_i} \le 1. [proof]

q5. XX and YY take values in {0,1}\{0, 1\} with P(0,0)=P(1,1)=3/8P(0, 0) = P(1, 1) = 3/8 and P(0,1)=P(1,0)=1/8P(0, 1) = P(1, 0) = 1/8. In bits, give (a) the mutual information I(X;Y)I(X; Y), (b) the pointwise mutual information PMI⁡(0,0)=log⁡2P(0,0)PX(0)PY(0)\operatorname{PMI}(0, 0) = \log_2 \frac{P(0, 0)}{P_X(0) P_Y(0)}, (c) PMI⁡(0,1)\operatorname{PMI}(0, 1). [number]

q6. (a) XX is uniform on {0,1,2,3}\{0, 1, 2, 3\} and Y=X mod 2Y = X \bmod 2. Give I(X;Y)I(X; Y) in bits. [number] (b) Is I(X;Y)=I(Y;X)I(X; Y) = I(Y; X) for every joint distribution? [bool]

q7. Prove that I(X;Y)=H(X)+H(Y)−H(X,Y)I(X; Y) = H(X) + H(Y) - H(X, Y), where I(X;Y)=∑x,yP(x,y)log⁡P(x,y)P(x)P(y)I(X; Y) = \sum_{x, y} P(x, y) \log \frac{P(x, y)}{P(x) P(y)}, and that I(X;Y)≥0I(X; Y) \ge 0 with equality exactly when XX and YY are independent. You may use Gibbs’ inequality (S-M11a q9). [proof]

q8. A softmax at temperature TT turns logits zz into pi=ezi/T/∑jezj/Tp_i = e^{z_i / T} / \sum_j e^{z_j / T}. Let z=(0,ln⁡2,ln⁡3)z = (0, \ln 2, \ln 3). (a) Give p3p_3 at T=1T = 1. (b) Give p3p_3 at T=1/2T = 1/2. [number] (c) As T→∞T \to \infty, the distribution tends to: (a) the one-hot vector on the largest logit, (b) the uniform distribution, (c) the distribution at T=1T = 1. [choice]

q9. Derive, with a Lagrange multiplier for each constraint, that among distributions pp on {1,…,V}\{1, \dots, V\} with a fixed mean energy ∑ipiEi=Eˉ\sum_i p_i E_i = \bar{E}, the one with maximum entropy has the form pi=e−βEi/Z(β)p_i = e^{-\beta E_i} / Z(\beta) with Z(β)=∑je−βEjZ(\beta) = \sum_j e^{-\beta E_j}: a softmax of the logits −Ei-E_i at temperature 1/β1/\beta. [proof]

q10. A fair binary source (P(1)=1/2P(1) = 1/2) with Hamming distortion has the rate-distortion function R(D)=1−Hb(D)R(D) = 1 - H_b(D) bits per symbol for 0≤D≤1/20 \le D \le 1/2, where HbH_b is the binary entropy. Give (a) R(1/8)R(1/8) and (b) R(1/2)R(1/2). [number]

q11. A Gaussian source with variance σ2\sigma^2 and squared-error distortion has R(D)=12log⁡2(σ2/D)R(D) = \frac12 \log_2 (\sigma^2 / D) bits per sample for 0<D≤σ20 < D \le \sigma^2. (a) How many bits per sample are needed to reach D=σ2/16D = \sigma^2 / 16? (b) Each extra bit per sample divides the smallest achievable distortion by what factor? [number]

PitfallSymptomCaught by
Listing code lengths in an order other than the probabilities’the right multiset, the wrong codeq1 a (canary)
Reporting the entropy where the code length was askeda non-integer-length code that no prefix code achievesq2 a (canary)
Rounding −log⁡2p-\log_2 p down for Shannon lengthslengths that violate Kraftq3 a (canary 9/5)
Trusting lengths without checking Krafta “prefix code” that cannot existq3 b (canary)
Mixing nats into a bits answermutual information off by ln⁡2\ln 2q5 a (canary in nats)
Clipping PMI at zero when PMI was askedPPMI, not PMIq5 c (canary 0)
Confusing I(X;Y)I(X; Y) with H(X)H(X)“knowing X tells everything about Y” when it does notq5 a, q6 a (canaries)
Multiplying logits by TT instead of dividinga temperature that sharpens when it should flattenq8 b (canary)
Reading T→∞T \to \infty as greedythe opposite limitq8 c (canary a)
Hb(D)H_b(D) for R(D)R(D), or dropping the 1/21/2 in the Gaussian ratebit budgets off by a factor 2 or worseq10 a, q11 (canaries)
DirectionModuleHow it uses this
BackS-M11aentropy, KL, the chain rule, Gibbs’ inequality
BackM11.2bits per byte is a code length per byte
BackM11.4mutual_information and pmi_matrix are q5 to q7 as code
ForwardL2.3PPMI word vectors: positive PMI only
ForwardL8.1the sampler divides logits by the temperature; T=0T = 0 is greedy
ForwardL8.5quantizers trade bits per weight for squared error, about a factor 4 per bit