Kayavak / Information Theory / IT·06 The Source Coding Theorem: Why Entropy Is the Limit / Deeper
Deeper · Information Theory
The Source Coding Theorem: Why Entropy Is the Limit
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Before 1948, engineers knew compression was possible (Morse code, code books, Hartley's logarithm) but not how far it could go. Shannon's source coding theorem gave the answer, the entropy, and proved it with an idea that has outlived the theorem: in long messages, randomness becomes regular. Almost every long sequence from a source looks the same in its statistics, so describing it needs only enough bits to say which of the roughly typical sequences it is. The idea was made rigorous for sources with memory by McMillan (1953) and Breiman (1957), and it became the "method of types" in the hands of Imre Csiszár and János Körner in the 1970s.
The practical story is a chain of inventions that each closed part of the gap between theory and software. Huffman (1952) for single symbols; Elias's unpublished arithmetic coding idea, made precise by Rissanen and Pasco (1976) and practical by Witten, Neal and Cleary (1987); Lempel and Ziv (1977, 1978) for compression without known statistics; context modelling (PPM, 1984) and context mixing (PAQ, from 2002) for better models; and Duda's ANS (2009 onwards) for speed. Patents shaped which ideas spread: arithmetic coding and LZW were patented for years, which is partly why DEFLATE used Huffman and why GIF gave way to PNG.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
In long messages, almost everything that happens is typical: about sequences, each with probability about . Naming one takes bits, and no code can do better on average. Arithmetic coding names it symbol by symbol; a model supplies the probabilities.
| Question | Answer |
|---|---|
| What is it? | The theorem that entropy is exactly the limit of lossless compression, and the codes that reach it |
| Problem it solves | How small a lossless compressor can make data, and how to get there |
| Parts | Typical sequences, the counting argument, arithmetic coding, adaptive models, Lempel–Ziv |
| How it works | The law of large numbers concentrates probability on typical sequences; an interval narrows by each symbol's probability |
| Assumptions | A stationary source (statistics that don't change); long messages; the model shared by encoder and decoder |
| When they change | Short messages pay fixed costs; changing statistics need adaptation; channel errors need protection |
| Where it fails | For one-off files there is no "true source"; the limit depends on the model chosen |
| Related ideas | Entropy (ch04), Huffman (ch05), conditional entropy and entropy rate (ch07), channel coding (ch09) |
What if?
What if a compressor were allowed to lose information?
Reason it through, then open
Then the limit isn't the entropy but a trade-off between bits and distortion: rate–distortion theory, which Shannon founded in 1959. A photo or a song can be coded far below its lossless entropy if small errors are allowed, and the theory says exactly how few bits per symbol are needed for a given average error. JPEG, MP3, AAC and every video codec live on this curve. The source coding theorem is the special case where the allowed distortion is zero.
What if the source changed its statistics halfway through?
Reason it through, then open
A code tuned to the first half would be wrong for the second, and a model that learned from the whole file would be a blend that fits neither. The theorem's guarantee assumes a stationary source. Practical compressors cope by adapting: counts that decay over time, per-block tables (DEFLATE), or models that mix several predictions and shift weight to whichever has been right recently (context mixing). Universal compressors handle any stationary source; for non-stationary data, how fast a compressor forgets is a design choice with no single right answer.
What if you tried to compress the output of a good compressor again?
Reason it through, then open
A good compressor's output is close to random: its bits are nearly independent and equally likely, because any pattern left would have been exploited. So a second pass finds nothing to remove and adds its own header: chapter 4's exercises measured lzma's output at 8.000 bits per byte, and compressing it again made it about 0.03 % bigger. If a second pass does help noticeably, the first compressor was weak (or the data was mixed, such as an archive of compressed and uncompressed files).
Where the boundary is
| Aspect | Huffman coding | Arithmetic coding |
|---|---|---|
| Unit of coding | One codeword per symbol | One number for the whole message |
| Cost per symbol | Whole digits, at least 1 | , fractions included |
| Distance from entropy | Up to 1 bit per symbol | About 2 bits per message, plus tiny rounding |
| Changing probabilities | Rebuild the code | Free: any model, any symbol |
| Speed | Fast, table lookups | Slower; ANS closes the gap |
| Error spread | Often resynchronises | Everything after an error is lost |
Derive it
Why an arithmetic code needs at most digits for a message of probability .
- After coding, the message owns an interval of width inside .
- Let , so . Binary fractions with digits are spaced apart.
- Take the smallest -digit fraction ; then . The whole range of numbers that start with those digits is , which ends below : inside the interval.
- So the digits identify the interval unambiguously, whatever digits might follow them, and the decoder recovers the message (given its length).
- The cost is : the message's total surprise plus less than 2 digits, however long the message.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Typical sequences hold almost all the probability, about of them | Theorem | Shannon (1948); the law of large numbers |
| Compression to per symbol is achievable, below impossible | Theorem | Shannon (1948); chapter 5's bound for blocks |
| No lossless compressor shortens every file | Theorem | Counting (pigeonhole) |
| The lab's arithmetic coder is within 1 bit of the entropy on 1.1 MB | Measured | The chapter's lab |
| Learning costs about bits | Theorem (asymptotic) | Krichevsky and Trofimov (1981); Rissanen's minimum description length |
| Lempel–Ziv approaches the entropy rate of stationary sources | Theorem (asymptotic) | Ziv and Lempel (1977, 1978) |
| Patents delayed arithmetic coding's adoption | Historical | The history of JPEG's optional arithmetic mode and of GIF's LZW |
Still open
The theorem is settled; the frontier is models. How low can the measured entropy of real data go with better models (the Hutter Prize, since 2006, rewards compressing a fixed slice of Wikipedia, and neural compressors keep lowering the figure)? How fast can near-optimal coding run on modern hardware? And how should compressors trade speed, memory and ratio for each workload, a question each new format (Zstandard, Brotli, LZ4) answers differently.
What would change your mind? The chapter says no compressor can beat the entropy on average. A compressor that consistently beat the measured entropy of some data would show that the model used to measure it was poor, not that the theorem fails: that's how better models are discovered. A compressor that shortened every possible file would contradict counting, and can't exist.
Recall
What is a typical sequence?
One whose surprise per symbol is close to the source's entropy; there are about 2^(nH) of them, holding almost all the probability.
What does the source coding theorem say?
A source can be compressed to H + ε bits per symbol with long blocks, and no lossless code averages fewer than H.
Why can't any compressor shrink every file?
There are 2^n files of n bits but only 2^n − 1 shorter files, so some file can't get shorter.
How long is an arithmetic code for a message of probability P?
Less than log₂(1/P) + 2 digits: its total surprise plus under 2 digits.
What does an adaptive coder pay for learning the probabilities?
About (K − 1)/2 × log₂ n bits in total, a fixed cost that vanishes per symbol.
Why are Lempel–Ziv methods called universal?
They approach the entropy rate of any stationary source without being told its statistics.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.