The Floor Beneath Compression
Could a clever enough program shrink any file? Shrink the result again, and again, until everything fits in a single byte? A child's counting argument says no, and Shannon's theorem says exactly how far down you can go.
Every so often someone announces a compressor that can squeeze any file, even one that has already been compressed. It is a tempting idea: if a program can turn 100 kilobytes of text into 40, why not run it again and get 16, then 6? This chapter shows, with nothing more than counting, why no lossless compressor can do that, and why random data in particular refuses to shrink.
"Lossless" is the key word. A lossless compressor must let you rebuild the original file exactly, bit for bit, as zip does for documents and programs. (Lossy formats such as JPEG photos and MP3 audio deliberately throw detail away; that is a different trade, studied by a separate branch of the theory.) Everything below is about lossless compression.
Not enough short names to go round
A lossless compressor gives every file a new name: its compressed version. Two different files must never get the same compressed version, or the decompressor could not tell which one to rebuild. Now count. There are 2n different files of exactly n bits. How many files are shorter than n bits, counting every length from 0 up to n − 1? 1 + 2 + 4 + … + 2n−1 = 2n − 1. One fewer. There are simply not enough shorter names to go round.
The second slider asks a sharper question. How many n-bit files can be shrunk by at least k bits? Their compressed versions must be at most n − k bits long, and there are fewer than 2n−k+1 of those. So fewer than 2 out of every 2k files can get that treatment. Fewer than 1 file in 128 can be shrunk by a byte; fewer than 1 in half a million by 20 bits. Most files cannot be compressed by any compressor in any meaningful way.
A lossless compressor can only shorten some files by lengthening others; it wins only on the files it was designed to expect.
So why does compression work so well in practice? Because the files people actually make are a tiny, very unusual corner of all possible files. Text, spreadsheets and program code are full of redundancy, as Chapter 3 showed. A good compressor spends its short names on that corner and lets the vast sea of random-looking files grow by a few bits, files almost nobody ever stores.
Compressing the compressed
What does a compressed file look like from the inside? Below is a small but honest compressor: it counts the bytes of its input, builds a Huffman code (Chapter 5), and writes a header (the table of symbols and code lengths) followed by the coded data. Run it on its own output, again and again, and watch the byte histogram.
This is why zipping a zip file, or a JPEG photo, gains you nearly nothing: their contents already look random to a compressor. "Looks random" is the right phrase. The output of a compressor is not truly random, but it no longer has the kind of pattern that the compressor knows how to exploit.
Shannon's floor
Counting tells us that not everything can shrink. Shannon's source coding theorem, in his 1948 paper, tells us exactly how much the typical output of a source can shrink. For long runs of symbols from a source with entropy H bits per symbol:
- there are lossless codes that use, on average, as close to H bits per symbol as you like, and
- no lossless code can use fewer than H bits per symbol on average.
For sources with memory, like English, H means the entropy rate: the average surprise of each symbol given everything before it. That is what the Chapter 3 ladder of models was approaching.
You met the second half already, as the budget argument in Chapter 4. Together they make entropy the exact price of describing a source. A fair coin's flips cost 1 bit each and cannot be squeezed at all. English letters, with all their context, cost about a bit each in Shannon's experiments, against 8 bits in a plain text file.
Note the words on average. The floor constrains the average over everything the source might produce. An individual message can come out shorter than the floor, if it happens to be full of likely symbols. It is luck, and the theorem guarantees that the luck is paid for by other messages. See it happen:
No lossless code beats the entropy on average. Individual messages can come out shorter; random data from a fair source, on average, cannot be compressed at all.
A product claims to losslessly shrink every 1-megabyte file by at least one byte. What is wrong with the claim?
In Interactive 6.3, some messages needed fewer bits per symbol than the entropy. Does that break Shannon's theorem?
Connections
- Probability, Chapter 8: Order from Chaos explains why the histogram in 6.3 narrows as messages get longer.
- Computer Science, Chapter 3: Counting by Doubling shows the doubling that makes 2n files of n bits.
- Chapter 2 introduced entropy as an average; here it becomes a price.
What you now see
- There are 2n files of n bits but only 2n − 1 shorter ones: no lossless compressor can shrink every file.
- Fewer than 2 in 2k files can be shrunk by k bits or more. Compression works because real files are a tiny, highly patterned corner of all files.
- Compressed output looks random to the compressor, so compressing it again only adds overhead.
- Shannon's source coding theorem: long messages from a source can be coded at close to its entropy per symbol on average, and never below it.
- Individual messages can beat the floor by luck; averages cannot.