ViewWiki

The world, a little closer

Information and entropy.

A fair coin flip is pure surprise; a rigged one barely tells you anything. Why does predictable data shrink when you compress it?

Explore the scene Interactive article

01 / The experiment

Surprise is information.

Watch the coins become bits, rig the coin, then step through to the entropy graph and the compressed tape.

Still of the Information and entropy 3D scene, which explores: Why does a fair coin carry more information than a rigged one?
Interactive scene

Loading the interactive version…

The explanation below is available while the scene loads.

Every flip is independent and lands heads with exactly the chance you set. The code is built knowing that chance.The spin and bounce of the coins are drawn, not simulated; each face is decided by a fixed random draw against the chance of heads.

02 / Three coins

The less you can guess, the more you learn.

Each diagram shows 16 flips (filled for heads) and, below, the same flips in a code for blocks of four.

Anyone’s guess01
16 flips1100011110011111110001111001111116 code bits

Fair coin

Heads and tails are equally likely, so every flip is a full surprise. No block is more common than another, so the code cannot shorten anything.

Chance of heads
50%
Entropy
1.00 bits per flip
Surprise of heads
1.00 bits
Surprise of tails
1.00 bits
100 flips, coded
about 100 bits
Mostly heads02
16 flips11111111110111110010106 code bits

Biased coin

Heads is expected and carries little news; the rare tail carries a lot. Runs of heads get one-bit codewords, so on average the tape shrinks to about half.

Chance of heads
90%
Entropy
0.47 bits per flip
Surprise of heads
0.15 bits
Surprise of tails
3.32 bits
100 flips, coded
about 49 bits
No doubt at all03
16 flips111111111111111100004 code bits

Two-headed coin

You know every result before the flip. The entropy is zero; the code still spends one bit per block of four.

Chance of heads
100%
Entropy
0.00 bits per flip
Surprise of heads
0 bits
Surprise of tails
never lands
100 flips, coded
about 25 bits
The big idea

Information is surprise. Entropy, the average surprise per flip, is the fewest bits any code can spend on average, so data you can predict can be squeezed and data you cannot predict cannot. Summed over heads and tails, with p the chance of each:

H = −Σ p log₂p

A coin you cannot predict

Flip a fair coin and write down what you see: 1 for heads, 0 for tails. Before each flip you have no idea which digit comes next, and afterwards you know exactly. That change, from not knowing to knowing, is what information means in science and engineering. It has nothing to do with whether the message is important or true. It measures how much a message settles.

Now rig the coin so it lands heads nine times in ten. The tape fills with 1s, and you could guess most of it without looking. A flip you could have guessed tells you little. A coin that always lands heads tells you nothing at all: you knew the answer before it landed.

Surprise, measured in bits

Claude Shannon turned this into arithmetic in 1948. The surprise of an outcome that happens with probability p is −log₂ p bits. A fair coin’s heads has probability ½, so it carries exactly 1 bit: one yes-or-no question answered. On the rigged coin, heads (0.9) carries only 0.15 bits, while the rare tails (0.1) carries 3.32 bits. Rare events are big news; expected ones are small.

The base-2 logarithm is what makes the unit a bit. Two fair flips carry 2 bits, and four equally likely outcomes need two binary digits to tell apart. Surprises of independent events add.

Entropy is the average surprise

A rigged coin mostly produces small surprises with the occasional big one. Averaging over many flips gives the entropy of the coin:

H = −p log₂ p − (1 − p) log₂ (1 − p)

For the 90% coin that is 0.9 × 0.15 + 0.1 × 3.32 ≈ 0.47 bits per flip. The curve is a hump: 0 bits when the coin is certain either way, rising to its peak of exactly 1 bit at a fair coin, where you are least able to guess. A 75% coin still carries 0.81 bits per flip; a 99% coin, only 0.08.

Try it: step Measure and compress to Entropy, then slide Chance of heads up to 90%. The marker slides down the curve to 0.47 bits, and the readout shows how small the surprise of a head has become. Slide it to 100% and it drops to zero.

Back to the experiment

Predictable data compresses

Entropy is not just a definition. Shannon proved it is the smallest number of bits per flip that any code can use on average to record the flips exactly. That makes it a hard limit on compression, and it explains why predictable data shrinks.

The machine’s second tape shows a real compressor, a Huffman code, which David Huffman devised as a student and published in 1952. It reads the flips in blocks and gives each possible block a codeword, short for common blocks and long for rare ones, arranged so that no codeword is the start of another and the tape can be read back without gaps. For the 90% coin in blocks of four, all heads happens about 66% of the time and is written as a single bit. A block with one tail takes 3 or 4 bits, and four tails take 10. Most blocks shrink, a few grow, and on average the coin costs 0.49 bits per flip, against an entropy limit of 0.47. On one run of 100 flips the tape came to 48 bits.

A fair coin cannot be compressed at all. Every block is as likely as every other, so no block deserves a short codeword, and the lower tape is exactly as long as the upper one. Compression works by betting on what is likely; when nothing is more likely than anything else, there is nothing to bet on.

How close can a code get?

Slide Code block size down to 1 flip: the lower tape becomes a copy of the upper one, whatever the coin. A single flip has only two outcomes, and a codeword cannot be shorter than one bit, so coding flips one at a time saves nothing. Longer blocks give the code more to work with. For the 90% coin, blocks of 2, 3, 4 and 6 cost 0.65, 0.53, 0.49 and 0.47 bits per flip. A Huffman code over blocks of k flips always lands between the entropy and the entropy plus 1/k bits per flip.

The certain coin shows the same limit from the other side. Its entropy is zero, yet the code still spends one bit per block, a quarter of a bit per flip in blocks of four. Longer blocks, or the arithmetic coding used in modern compressors, approach zero.

On any short stretch of tape the count can land a little above or below the limit, because a lucky run of heads is cheap to write. The limit holds on average, and over long streams the counts close in on it.

What this model leaves out

Every flip here is independent of the others and has the same, known chance. Real data is rarely like that. In English text, a q is almost always followed by a u, and whole words repeat; general-purpose compressors such as those behind ZIP files find these patterns and repeats, not just an uneven mix of symbols, so they can do far better than a code that only knows how often each letter appears. The code here is also built knowing the coin’s true chance; a real compressor has to estimate it from the data. Huffman codes only approach the entropy for long blocks, and the machine stops at six flips. The coins’ spin, bounce and landing are drawn for clarity rather than computed from physics, and a real tossed coin is very slightly more likely to land the way it started.

Reuse with credit to ViewWiki and a link to this page.

04 / Learn this in a course

Practise it, step by step.

These 3 lessons teach the topic with checked questions, in course order.

  1. Surprise measured in bits Computer science · Information and codes
  2. Entropy Computer science · Information and codes
  3. Compression Computer science · Information and codes