Part II · Chapter 6

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.

Interactive 6.1The Pigeonhole Counter
Slide the file length, then try to shrink every file
files of exactly n bits, and the shorter names they get a file left with no unused shorter name
3 bits
8 bits
 
With 3-bit files there are 8 files and only 7 shorter names (including the empty file). Assign them one by one and the last file always finds every shorter name taken. Slide n up: the gap is always exactly one, so no lossless compressor can shrink every file; any that shrinks some must leave others the same size or make them longer.

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.

The pigeonhole limit

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.

Interactive 6.2Compress the Compressed
Pick an input; each pass compresses the previous output
how often each of the 256 byte values occurs size of each pass, header included
 
English text has a spiky histogram (lots of spaces and e's, no byte values above 127), and the first pass removes a good share of it. After that the bytes are spread almost evenly over all 256 values: what this compressor could exploit is gone, and each further pass only adds another header (the readout shows its size). Random bytes grow from the very first pass. This toy writes a large table (13 bits for every byte value used), so the growth here is exaggerated. Real formats store the table compactly and fall back to storing incompressible data as-is, so a real zip of random data grows by only a few bytes, but it still does not shrink.

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:

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:

Interactive 6.3Lucky Messages
Change the message length and the source
bits per symbol for each of 2,000 random messagesthe code's expected length entropy of the source messages that came out below the entropy ("lucky")
20 symbols
 
Each message is drawn from the source and encoded with the Huffman code built for the source (not for the message). Short messages scatter widely: plenty land below the entropy line. But the code's expected length (white dashed line) is never below it, and as messages get longer the histogram tightens around that line. For the first source the expected length equals the entropy exactly, so about half the messages stay just below the line at every length; what shrinks is how far below they can get. For the second, Huffman's whole-bit lengths leave the expected length a little above the entropy, and lucky messages become rarer as messages get longer. (The sample average of these 2,000 messages wobbles slightly around the expected length, as any sample average does.)
What the floor does and does not say

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.

Intuition Check

A product claims to losslessly shrink every 1-megabyte file by at least one byte. What is wrong with the claim?

Intuition Check

In Interactive 6.3, some messages needed fewer bits per symbol than the entropy. Does that break Shannon's theorem?

Connections

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.