ViewWiki

Lesson 3 of 5

Compression.

About 25 minutes · 5 practice questions

How Huffman's code shortens predictable data: short words for common blocks, why a fair coin does not compress, and why no code goes below the entropy.

Step 1 of 5 · Short words for what is common

Step 1 of 5

Short words for what is common

On the upper tape every flip takes one bit, even when the coin is very predictable. Compression makes use of the predictability: it records the same flips in fewer bits, without losing any.

Measure and compress is at Compressed. The lower tape is the same flips, written in Huffman’s code: the code reads the flips in blocks, here in pairs, and gives each block a codeword. The commonest pair, heads-heads, gets a one-bit word, and the rare pairs get longer ones. The lines between the tapes join each block to its word.

To the question

Step 2 of 5

Nothing to bet on

Compression is a bet: it gives short words to what it expects to see often, and pays with long words for what is rare. The bet pays off when some blocks are likelier than others.

The coin is now fair, and the code reads blocks of four flips. Think about what will happen to the lower tape.

To the question

Step 3 of 5

Longer blocks

A block of one flip has only two outcomes, and a codeword cannot be shorter than one bit. So a code that reads one flip at a time saves nothing, however biased the coin. The longer the block, the more different blocks there are, and the more their probabilities differ: the code has something to work with.

To the question

Step 4 of 5

No word starts another

The lower tape has no gaps between words, and still it can be read back. The secret: no codeword is the start of another. If 0 is a word, no other word starts with 0, so when the reader sees 0 it knows the word has ended.

That rule has a price. A one-bit word “uses up” half of all possible words, and all the other blocks must fit into the other half.

To the question

Step 5 of 5

The limit

Shannon proved that the entropy is not just an average: it is the fewest bits per flip that any code can spend on average without losing information. Huffman’s code on blocks of kk⁠ flips always lands between HH⁠ and H+1kH + \tfrac{1}{k}⁠. In blocks of six flips, as now, it is already at the limit.

Watch the count under the lower tape. Over a short stretch it can come out a little above the limit or a little below it, because a lucky run of heads is cheap to write. The limit holds on average, over long runs.

To the question

Still of the Information and entropy 3D scene
Interactive scene

Loading the interactive version…

Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.

To the experiment

Practice · Calculate

Chance of heads is 90% and Code block size is two flips. Huffman’s code gives each pair a codeword: heads-heads 0, heads-tails 10, tails-tails 110, tails-heads 111. The pairs’ probabilities are 0.810.81⁠, 0.090.09⁠, 0.010.01⁠ and 0.090.09⁠, in the same order. How many bits does a pair cost, on average?

To the experiment

Practice · Predict, then look

The coin is fair, and the code reads blocks of four flips. When you open the compressed tape, how many bits per flip will it cost?

To the experiment

Practice · Reach the target

Chance of heads is fixed at 90%. In blocks of one flip the code saves nothing: one bit per flip. Change Code block size until the code costs about 0.490.49⁠ bits per flip, then press Check.

To the experiment

Practice · Choose

In Huffman’s code for blocks of four flips of a 90% coin, the block four heads, the most common of all, gets a one-bit codeword. What does the block four tails, the rarest of all, get?

To the experiment

Practice · Choose

A coin lands heads 90% of the time. Someone offers a clever code that records its flips, losing none, at 0.40.4⁠ bits per flip on average, over millions of flips. What do you make of it?