Part IV · Chapter 12

Better Predictor, Better Compressor

Every compressor in this book secretly contained a model of its data: letter counts, a code tree, a memory of what came before. This last chapter makes the link exact. The cost of a message is the surprise of a model, a wrong model pays a measurable tax, and training a language model is training a compressor.

Think of a weather forecaster who is asked to bet on tomorrow, every day, for a year. A forecaster who says "70% chance of rain" and sees rain should be rewarded more than one who said "10%". A natural score is exactly the surprise from Chapter 1: if your forecast gave the actual weather probability q, you pay log₂(1/q) points. Confident and right costs almost nothing; confident and wrong costs a lot; and saying "0%" for something that happens costs an infinite amount.

That score is not just a nice way to grade forecasters. It is literally the number of bits a compressor would spend on the actual outcome if it built its code from the forecast. Chapter 4 showed that the ideal codeword length for an outcome is its surprise. Here the probabilities come from a model, and the model may be wrong.

The price of a wrong model

Suppose the world follows probabilities p, but your code is built from a model q. On average you pay the surprise under q, averaged over what actually happens according to p. That average is called the cross-entropy. When q matches p exactly, it equals the entropy; any mismatch costs extra bits. Drag the model's bars below and watch the bill.

Interactive 12.1The Wrong-Model Tax
Drag the outlined model bars, or pick a model
how often each outcome really happens (p, solid) and the model's belief (q, outline) bits paid when that outcome happens (log₂ 1/q) entropy, the best possible average extra bits from the wrong model
30%
 
Only "Model = truth" reaches the entropy; every other model pays a positive tax. The overconfident model is cheap on sunny days and ruinous on the others. "It never snows" puts a tiny probability on snow, so the rare snowy day costs over 13 bits. The closer the model's snow bar gets to zero, the higher that cost climbs; at exactly zero it would be infinite. A model should never call a possible event impossible.
H(p, q) = Σ p(x)·log₂(1/q(x)) = H(p) + extra "The average bits you pay is the surprise under your model, averaged over what really happens. It is never less than the entropy; the extra is called the Kullback–Leibler divergence, or relative entropy, and it is zero only when the model is right."
The model's bill

A model that predicts better pays fewer bits; the gap between its average and the entropy measures exactly how wrong it is.

Predict, then pay

Now apply this to text. In Chapter 3 you climbed a ladder of letter models. Each one makes a prediction before every character, a probability for each of the 27 symbols, and then pays the surprise of the character that actually comes. Type anything below and see where each model pays its bits.

Interactive 12.2Predict, Then Pay
Type, then switch models
bits paid for each character under the chosen model 4.75 bits: the cost with no model
 
With three letters of context, letters inside common words cost a fraction of a bit, while word beginnings and unusual words cost several. The total under each model is the size an ideal coder driven by that model would produce. The models here learned from only a few pages of text; a model trained on a vast library would pay far less, especially at word beginnings.

Can a real compressor actually achieve those totals? Almost exactly. Arithmetic coding turns any sequence of predictions into a single binary number whose length is the total surprise plus at most a couple of bits for the whole message, not per symbol. It works by narrowing an interval: start with the range from 0 to 1, and for each symbol zoom into the slice the model assigned to it. Watch it happen.

Interactive 12.3The Shrinking Interval
Add days of weather to the message
the slice the model gives each outcome the slice actually chosen the binary number that pins down the final interval
 
Each likely symbol shrinks the interval only a little; a rare one shrinks it a lot. The final width is the product of the probabilities, and naming a point inside it takes about log₂(1/width) bits: the sum of the surprises. For sunny-heavy messages the matching model beats the flat one; send a string of rain and the flat model wins, because the message no longer fits the first model's expectations.
Prediction is compression

Any predictor can drive an arithmetic coder, so a better predictor is a better compressor; and the bits a compressor saves are a measure of how much structure it has found.

The bridge to language models

A large language model is, at its core, a next-token predictor: given the text so far, it outputs a probability for every possible next token. During pretraining it is shown enormous amounts of text and adjusted to lower its average surprise at the token that actually came next. That training objective is the cross-entropy you met above, usually computed with natural logarithms and reported in nats rather than bits (divide by ln 2 ≈ 0.693 to convert nats to bits).

So lowering a language model's training loss is, quite literally, making it a better compressor of text like its training data. Pair such a model with an arithmetic coder and you get a working text compressor; when the size of the model itself is not counted, strong models compress text far better than zip-style tools. A related number you may meet is perplexity, 2 raised to the cross-entropy in bits: a perplexity of 20 means the model is, on average, as uncertain as if it were choosing evenly among 20 tokens.

Does compressing text well mean understanding it? Information theory does not answer that. Shannon deliberately set meaning aside, and every number in this book measures predictability, not meaning. What the theory does say is that you cannot predict well, and so cannot compress well, without capturing the patterns that are really there.

Intuition Check

A forecasting model says snow has probability 0. Then it snows. What happens to the model's cost (its cross-entropy)?

Intuition Check

On the same unseen text, model A averages 1.2 bits per character and model B averages 1.5. Which statement is right?

Connections

What you now see

  • Coding with a model q costs log₂(1/q) bits per outcome; averaged over reality p, that is the cross-entropy.
  • Cross-entropy is never below the entropy; the gap (the Kullback–Leibler divergence) is the tax for a wrong model, and assigning zero probability to a possible event makes it infinite.
  • Arithmetic coding turns any predictor into a compressor whose output is within a couple of bits of the total surprise.
  • Training a language model to lower its cross-entropy loss is training a better compressor of text.
  • From the first yes-or-no question to the last, information measures surprise: how much a message changes what you expected. Meaning is a different question.
Where this book has been

You started by halving a pile of possibilities and ended by grading a language model. Along the way: surprise and the bit, entropy as average surprise, codes that need no commas, Huffman's tree, the floor no compressor can break, look-back compression, noise, parity, Hamming's circles and Shannon's capacity. Every one of those ideas is still at work in the device you are reading this on.