Information theory problem set, part b: coding and Huffman, mutual information, maximum entropy, rate-distortion
Overview
Section titled “Overview”| Module | S-M11b · solve · none · Pass 3 · 4 to 5 h |
| You build | answers 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) |
| Contract | none: a pen and paper set |
| Tests | course/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 |
| Needs | no 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 by | no 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 |
| Milestone | MS-P3 (the Pass 3 gate runs ol check on every solve part of the pass) |
| Optional depth | Cover 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) |
Key Takeaways
Section titled “Key Takeaways”- 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 (q5 to q7).
- Softmax at temperature is the maximum-entropy distribution with a fixed expected logit; flattens it to uniform and 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).
How to work this chapter
Section titled “How to work this chapter”ol start S-M11b # writes solve/S-M11b.toml and one file per proofol 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 proof1. Why now
Section titled “1. Why now”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.
2. Principles
Section titled “2. Principles”| Symbol | Meaning | Type / shape |
|---|---|---|
| probability of symbol | scalar | |
| length in bits of symbol ‘s codeword | int | |
| expected code length | bits | |
| entropy | bits | |
| Kraft sum | scalar | |
| mutual information | bits or nats | |
| pointwise mutual information | scalar | |
| , | logits and temperature | scalars |
| , | energies and inverse temperature (, ) | scalars |
| allowed average distortion | scalar | |
| rate-distortion function: fewest bits per symbol at distortion | bits | |
| binary entropy | bits |
2.1 Codes, Kraft, and Huffman
Section titled “2.1 Codes, Kraft, and Huffman”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 , and conversely any lengths satisfying it have a prefix code. Minimizing under that constraint gives and : no code beats the entropy. Lengths must be integers, so the Shannon code rounds up, , and lands within one bit: . 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.
2.2 Mutual information and PMI
Section titled “2.2 Mutual information and PMI”: the KL divergence from the joint to the product of its marginals, the expected PMI, and also . It is symmetric, non-negative, and zero exactly when and are independent. A single pair’s PMI can be negative (seen together less than chance); only the average must be non-negative.
2.3 Maximum entropy and temperature
Section titled “2.3 Maximum entropy and temperature”Among all distributions with a given expected energy, the one with the most entropy is the Gibbs distribution (q9). With logits and , that is softmax at temperature : . Dividing logits by sharpens the distribution, flattens it; as every and the distribution becomes uniform, and as it concentrates on the largest logit, which is greedy decoding (D11 treats as greedy).
2.4 Rate-distortion
Section titled “2.4 Rate-distortion”Lossy compression asks for the fewest bits per symbol that reproduce a source within an average distortion . For a fair coin with Hamming distortion (the fraction of flipped bits), for : allowing errors saves exactly the entropy of the error pattern, and at you can guess without sending anything. For a Gaussian source with variance and squared error, , so : every extra bit divides the error by 4, the “6 dB per bit” rule of quantization.
3. Worked example by hand
Section titled “3. Worked example by hand”These are siblings of q1, q5, q8, and q10, not graded problems.
Huffman. Probabilities : merge the two quarters (one node of ), then that node with the . Lengths , bits, and : dyadic probabilities are coded at exactly the entropy. Kraft sum: .
Mutual information. , (, a fair bit): the marginals are uniform, bit, and bit : knowing removes all of ‘s uncertainty. Written in solve/ as 1.
Temperature. Logits : at , ; at , and , sharper.
Rate-distortion. A fair coin at : , so bits per symbol, written 3*log(3, 2)/4 - 1. A Gaussian at : bit.
4. The problem set
Section titled “4. The problem set”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.
Coding theorems and Huffman
Section titled “Coding theorems and Huffman”q1. A source emits four symbols with probabilities . (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 : (a) give the expected length in bits of a binary Huffman code; (b) give the Kraft sum of its codeword lengths . [number]
q3. For the same probabilities : (a) give the expected length in bits of the Shannon code, whose lengths are . [number] (b) Does a binary prefix code with codeword lengths exist? [bool]
q4. Prove the Kraft inequality: the codeword lengths of any binary prefix code satisfy . [proof]
Mutual information
Section titled “Mutual information”q5. and take values in with and . In bits, give (a) the mutual information , (b) the pointwise mutual information , (c) . [number]
q6. (a) is uniform on and . Give in bits. [number] (b) Is for every joint distribution? [bool]
q7. Prove that , where , and that with equality exactly when and are independent. You may use Gibbs’ inequality (S-M11a q9). [proof]
Maximum entropy
Section titled “Maximum entropy”q8. A softmax at temperature turns logits into . Let . (a) Give at . (b) Give at . [number] (c) As , the distribution tends to: (a) the one-hot vector on the largest logit, (b) the uniform distribution, (c) the distribution at . [choice]
q9. Derive, with a Lagrange multiplier for each constraint, that among distributions on with a fixed mean energy , the one with maximum entropy has the form with : a softmax of the logits at temperature . [proof]
Rate-distortion
Section titled “Rate-distortion”q10. A fair binary source () with Hamming distortion has the rate-distortion function bits per symbol for , where is the binary entropy. Give (a) and (b) . [number]
q11. A Gaussian source with variance and squared-error distortion has bits per sample for . (a) How many bits per sample are needed to reach ? (b) Each extra bit per sample divides the smallest achievable distortion by what factor? [number]
5. Pitfalls
Section titled “5. Pitfalls”| Pitfall | Symptom | Caught by |
|---|---|---|
| Listing code lengths in an order other than the probabilities’ | the right multiset, the wrong code | q1 a (canary) |
| Reporting the entropy where the code length was asked | a non-integer-length code that no prefix code achieves | q2 a (canary) |
| Rounding down for Shannon lengths | lengths that violate Kraft | q3 a (canary 9/5) |
| Trusting lengths without checking Kraft | a “prefix code” that cannot exist | q3 b (canary) |
| Mixing nats into a bits answer | mutual information off by | q5 a (canary in nats) |
| Clipping PMI at zero when PMI was asked | PPMI, not PMI | q5 c (canary 0) |
| Confusing with | “knowing X tells everything about Y” when it does not | q5 a, q6 a (canaries) |
| Multiplying logits by instead of dividing | a temperature that sharpens when it should flatten | q8 b (canary) |
| Reading as greedy | the opposite limit | q8 c (canary a) |
| for , or dropping the in the Gaussian rate | bit budgets off by a factor 2 or worse | q10 a, q11 (canaries) |
6. Where it’s used next
Section titled “6. Where it’s used next”| Direction | Module | How it uses this |
|---|---|---|
| Back | S-M11a | entropy, KL, the chain rule, Gibbs’ inequality |
| Back | M11.2 | bits per byte is a code length per byte |
| Back | M11.4 | mutual_information and pmi_matrix are q5 to q7 as code |
| Forward | L2.3 | PPMI word vectors: positive PMI only |
| Forward | L8.1 | the sampler divides logits by the temperature; is greedy |
| Forward | L8.5 | quantizers trade bits per weight for squared error, about a factor 4 per bit |