Workbench / Information Theory Exercises
B1·06
The Source Coding Theorem: Why Entropy Is the Limit
Exercises for chapter 6: typical sequences, both halves of the theorem, the counting argument, arithmetic coding's intervals, models and coders, and the cost of learning; the arithmetic coder tested on 200 random sources, surprise per symbol concentrating as messages lengthen, compressing compressed and random data, sensor reports at the entropy, a sorted set coded within 8 bits of its information, and senior exercises on incompressible backups, compressing small messages, and what one wrong bit does to an arithmetic code.
Start here: how these exercises work
Write your answer in the box first; hints open one at a time as you write, and the solution a couple of minutes after the last hint. Theory is in your own words, practice and programs come with real output (Python 3.13), cumulative projects use chapters 1 to 6 together, what the chapter leaves out explains what it skips, and senior exercises put the ideas into designs and reviews. The programs import the chapter's ac.py.
Theory
T1. Typical is not the same as likely
Explain, for 1,000 tosses of a coin that lands heads 90 % of the time, why the single most likely sequence is almost never what you see, and what you do see instead.
T2. The two halves of the theorem
State the source coding theorem's two halves in your own words, and say for each what would be wrong if it failed.
T3. Counting, without probability
Explain the counting argument that no compressor shrinks every file, to someone who knows no probability. Then explain why it doesn't stop compressors from being useful.
T4. Why arithmetic coding beats Huffman
Explain how arithmetic coding can spend 0.08 bits per symbol on a source where Huffman must spend at least 1.
T5. Model and coder
Explain the split between a compressor's model and its coder, and why arithmetic coding makes improving compression "a modelling problem".
T6. Learning for free?
An adaptive coder learns the probabilities as it goes and sends no table. Is its compression "free" of the model's cost? Explain where the cost goes and how big it is.
Practice and programs
P1. By hand
(a) A source has three symbols with probabilities 0.5, 0.25, 0.25. How many typical sequences of length 20 are there, roughly, and what's each one's probability? (b) Find the arithmetic-coding interval for B A with P(A) = 0.7, P(B) = 0.3 (A's slice first). (c) How many digits does it need? (d) An adaptive coder learns a 2-symbol source over 1,000,000 symbols. Estimate the learning cost.
P2. Testing the arithmetic coder
Run the chapter's arithmetic coder on 200 random messages, with 2 to 30 symbols and skewed probabilities, from 1 to 3,000 symbols long. Check every round trip, and find the largest amount by which a code exceeds the message's total surprise under its own counts.
P3. Surprise concentrates
For a source with probabilities 0.5, 0.3, 0.2, generate 2,000 sequences each of length 10, 100, 1,000 and 10,000, and measure each sequence's surprise per symbol. How does the spread change, and what fraction falls within 0.05 bits of the entropy?
P4. Compressing the incompressible
Compress Shakespeare with lzma, then compress the result again with lzma, zlib and bz2. Then compress 1,000 random files of each of several sizes with zlib. What happens, and which part of chapter 6 explains each result?
Cumulative projects (chapters so far: 01–06)
C1. Sensor reports at the entropy
Chapter 4's sensor reports one of 8 states (0.65, 0.2, 0.1, 0.02, 0.01, 0.01, 0.005, 0.005). Arithmetic-code 100,000 simulated reports with the agreed probabilities, check the round trip, and compare the cost per report with the entropy, with Huffman per report (1.610) and with Huffman on blocks of 3 (1.537).
C2. A set coded at its information
Chapter 2's exercises found that a set of 1,000 ids from a million carries 11,401 bits, and chapter 5 got within 1 % with a Rice code. Arithmetic-code the set as a million-bit bitmap with , and compare.
What the chapter leaves out
Rate–distortion theory
The chapter is about lossless compression. Shannon (1959) asked how few bits are needed if the decoded data may differ from the original by some average distortion, and found a function , the rate–distortion function, that answers exactly. Lossy formats (JPEG, MP3, video codecs) are practical approximations to it; the Mathematics shelf's later books and signal-processing books cover the transforms they use.
The method of types
The chapter counts typical sequences of a coin with binomial coefficients. The method of types, developed by Imre Csiszár and János Körner, groups all sequences by their exact symbol counts (their "type") and counts each group with a multinomial coefficient. There are only polynomially many types but exponentially many sequences, which makes many proofs (including the coding theorems and error exponents) short and exact. The challenge exercise is its simplest case.
Context mixing and neural compression
The best text compressors today mix the predictions of many models (different context lengths, word models, models of structure) with learned weights, and feed the mixed probability to an arithmetic coder: PAQ and its descendants since 2002, and more recently compressors whose model is a neural network, including large language models. They reach well under 2 bits per character on English text, and their results on the Hutter Prize's Wikipedia sample track progress in modelling language.
The Kolmogorov view of incompressibility
The counting argument shows most strings can't be compressed by any method. Kolmogorov complexity (chapter 1's exercises) makes it a property of individual strings: a string is incompressible if no program shorter than it prints it. Most strings are incompressible in this sense, by the same count, but no algorithm can certify that a particular long string is.
Senior exercises
S1. Backups full of compressed files
A backup system compresses every file with lzma at a high level. Users' files are 70 % photos and videos (already compressed). The backup servers are CPU-bound. What do you change?
S2. Compressing many small messages
A messaging service wants to compress individual chat messages (median 60 bytes) before storing them. Using chapter 6, explain why a general-purpose compressor will do badly, and design something better.
S3. One wrong bit in an arithmetic code
Arithmetic-code 5,000 bytes of the plays, flip a single bit of the code at three positions, decode, and compare with chapter 5's measurement for Huffman (one or two characters garbled, then back in step). What does this mean for formats that use arithmetic coding?