{"course":{"slug":"maths-you-need","title":"The Maths You Actually Need","url":"https://addaly.com/learn/maths-you-need","level":"intermediate","track":"deep"},"lesson":{"slug":"surprise-and-bits","title":"Surprise, bits, and why prediction is compression","url":"https://addaly.com/learn/maths-you-need/surprise-and-bits","position":42,"of":76,"minutes":9,"answer":"The surprise of an outcome is −log p, additive across independent events and zero for certainty, and because any distribution can be turned into a code of that length.","keyIdea":"The surprise of an outcome is −log p, additive across independent events and zero for certainty, and because any distribution can be turned into a code of that length, a model's loss in bits per token is literally the size it would compress the text to."},"body":{"format":"markdown","text":"## Measuring surprise\n\nIf something you were sure of happens, you learn nothing. If something you thought impossible happens, you learn a great deal. Information theory turns that into a number: the **surprise** of an outcome with probability `p` is\n\n```\nsurprise = −log2(p)   bits\n```\n\nA fair coin landing heads has `p = 0.5`, so `−log2(0.5) = 1` bit. A specific card from a shuffled pack has `p = 1/52`, so 5.7 bits. An outcome with `p = 0.9` is worth `−log2(0.9) = 0.152` bits: mild. An outcome with `p = 1` is worth exactly zero.\n\nWhy a log rather than, say, `1/p`? Two reasons that pin it down. Surprise should be additive for independent events: two fair coins should be twice as surprising as one, and `−log2(0.25) = 2` gives exactly that where `1/p` would give 4. And certainty should cost nothing, which the log gives for free. There is a theorem that the log is the only function with these properties, but the additivity is the part to keep.\n\n## Bits are yes-or-no questions\n\nA bit is one answer to a well-chosen yes-or-no question. Locating one item among 1,024 equally likely ones takes ten questions, and `log2(1024) = 10`. Locating one token among a 32,000-word vocabulary with no information at all takes `log2(32000) = 14.97` questions.\n\nA language model reduces that count. If it assigns probability 0.25 to the token that actually comes next, that token cost it `−log2(0.25) = 2` bits rather than fifteen. If it assigned 0.001, the token cost 10 bits. The average of those costs over a text is the model's cross-entropy loss in bits, which the next two lessons build up properly.\n\n## Nats\n\nPyTorch, TensorFlow and every paper's loss column report surprise in **nats**, using the natural log instead of base 2. One nat is `1 / ln 2 = 1.443` bits. A loss of 2.0 nats per token is 2.89 bits per token. Nothing about the model changes; only the unit does. When somebody quotes a loss without a unit, assume nats.\n\n## Prediction is compression\n\nHere is the fact that a product page will not tell you. Any probability distribution over messages can be turned into a code in which a message of probability `p` is written in about `−log2(p)` bits, and no code can do better on average. The technique is called arithmetic coding, and it is not exotic; it is inside every video codec you use.\n\nSo a model's loss in bits per token *is* the size it would compress the text to. Take a model scoring 2.0 nats per token on a corpus of one billion tokens:\n\n```\n2.0 nats × 1.443 = 2.89 bits per token\n2.89 × 1,000,000,000 = 2.89 × 10^9 bits = 361 MB\n```\n\nThe raw corpus, at roughly four characters per token and one byte per character, is about 4 GB. The model has compressed it eleven to one. For comparison, `gzip` gets ordinary English to about 2.5 bits per character, which is roughly 10 bits per token, so this model is compressing three and a half times better than `gzip`. Claude Shannon estimated in 1951 that a fluent human predicts English at about one bit per character, which is roughly four bits per token. A modern large model beats that.\n\nYou can measure the `gzip` side yourself in four lines, and it is a good number to have in your head:\n\n```python\nimport zlib\ntext = open(\"some_english.txt\", \"rb\").read()\nbits_per_char = 8 * len(zlib.compress(text, 9)) / len(text)\nprint(round(bits_per_char, 2))    # about 2.5 for ordinary prose\n```\n\n## Two honest limits\n\n**Surprise is about the model, not about the world.** A rare typo costs a model many bits because it did not expect it, yet the typo carries almost no meaning. High surprise means \"this model predicted poorly here\", nothing more profound. Two models can assign different surprises to the same token and both be reasonable.\n\n**The compressor must be counted.** The 361 MB above only decompresses if the receiver has the model. A seven-billion-parameter model is about 14 GB in half precision, forty times larger than the compressed corpus. As a way of storing one billion tokens, the model is a terrible deal. The claim that stands is narrower and still striking: *per token*, a good predictor and a good compressor are the same object, and improving one improves the other by exactly the same amount.\n\n## The arithmetic to carry forward\n\nSurprise is `−log p`. Bits use base 2, nats use base e, and one nat is 1.443 bits. A loss is an average surprise. That average is also a compressed size. Everything in the rest of this module is a consequence of those four sentences."},"check":{"question":"A language model assigns probability 1/8 to the token that actually comes next. What did that token cost, and why?","options":[{"text":"8 bits, one for each possible outcome the model was choosing between.","correct":false,"tempting":"Confuses the count of alternatives with the log of the probability; eight equally likely alternatives would cost log2(8) = 3 bits, not 8."},{"text":"0.125 bits, because the cost is the probability itself.","correct":false,"tempting":"Uses the probability directly instead of its negative log; a cost that fell as the probability fell would reward bad predictions."},{"text":"3 bits, because −log2(1/8) = 3.","correct":true},{"text":"It cannot be said without knowing the vocabulary size.","correct":false,"tempting":"Surprise depends only on the probability the model assigned to the outcome that occurred; the vocabulary size affects the uniform baseline, not this cost."}]},"prev":{"slug":"logs-and-loss","title":"Logs, and why losses are logged","url":"https://addaly.com/learn/maths-you-need/logs-and-loss"},"next":{"slug":"entropy-by-hand","title":"Entropy: the average surprise, computed by hand","url":"https://addaly.com/learn/maths-you-need/entropy-by-hand"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Surprise, bits, and why prediction is compression', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/surprise-and-bits (Accessed: 29 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Surprise, bits, and why prediction is compression","courseTitle":"The Maths You Actually Need","module":{"index":5,"slug":"logs-information-and-loss","title":"Logarithms, information and the shape of a loss"},"answer":null,"keyIdea":"The surprise of an outcome is −log p, additive across independent events and zero for certainty, and because any distribution can be turned into a code of that length, a model’s loss in bits per token is literally the size it would compress the text to.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"measuring-surprise","runs":[{"text":"Measuring surprise"}]},{"t":"p","runs":[{"text":"If something you were sure of happens, you learn nothing. If something you thought impossible happens, you learn a great deal. Information theory turns that into a number: the "},{"text":"surprise","b":true},{"text":" of an outcome with probability "},{"text":"p","code":true},{"text":" is"}]},{"t":"code","lang":"","label":"Text","text":"surprise = −log2(p)   bits"},{"t":"p","runs":[{"text":"A fair coin landing heads has "},{"text":"p = 0.5","code":true},{"text":", so "},{"text":"−log2(0.5) = 1","code":true},{"text":" bit. A specific card from a shuffled pack has "},{"text":"p = 1/52","code":true},{"text":", so 5.7 bits. An outcome with "},{"text":"p = 0.9","code":true},{"text":" is worth "},{"text":"−log2(0.9) = 0.152","code":true},{"text":" bits: mild. An outcome with "},{"text":"p = 1","code":true},{"text":" is worth exactly zero."}]},{"t":"p","runs":[{"text":"Why a log rather than, say, "},{"text":"1/p","code":true},{"text":"? Two reasons that pin it down. Surprise should be additive for independent events: two fair coins should be twice as surprising as one, and "},{"text":"−log2(0.25) = 2","code":true},{"text":" gives exactly that where "},{"text":"1/p","code":true},{"text":" would give 4. And certainty should cost nothing, which the log gives for free. There is a theorem that the log is the only function with these properties, but the additivity is the part to keep."}]},{"t":"h2","id":"bits-are-yes-or-no-questions","runs":[{"text":"Bits are yes-or-no questions"}]},{"t":"p","runs":[{"text":"A bit is one answer to a well-chosen yes-or-no question. Locating one item among 1,024 equally likely ones takes ten questions, and "},{"text":"log2(1024) = 10","code":true},{"text":". Locating one token among a 32,000-word vocabulary with no information at all takes "},{"text":"log2(32000) = 14.97","code":true},{"text":" questions."}]},{"t":"p","runs":[{"text":"A language model reduces that count. If it assigns probability 0.25 to the token that actually comes next, that token cost it "},{"text":"−log2(0.25) = 2","code":true},{"text":" bits rather than fifteen. If it assigned 0.001, the token cost 10 bits. The average of those costs over a text is the model’s cross-entropy loss in bits, which the next two lessons build up properly."}]},{"t":"h2","id":"nats","runs":[{"text":"Nats"}]},{"t":"p","runs":[{"text":"PyTorch, TensorFlow and every paper’s loss column report surprise in "},{"text":"nats","b":true},{"text":", using the natural log instead of base 2. One nat is "},{"text":"1 / ln 2 = 1.443","code":true},{"text":" bits. A loss of 2.0 nats per token is 2.89 bits per token. Nothing about the model changes; only the unit does. When somebody quotes a loss without a unit, assume nats."}]},{"t":"h2","id":"prediction-is-compression","runs":[{"text":"Prediction is compression"}]},{"t":"p","runs":[{"text":"Here is the fact that a product page will not tell you. Any probability distribution over messages can be turned into a code in which a message of probability "},{"text":"p","code":true},{"text":" is written in about "},{"text":"−log2(p)","code":true},{"text":" bits, and no code can do better on average. The technique is called arithmetic coding, and it is not exotic; it is inside every video codec you use."}]},{"t":"p","runs":[{"text":"So a model’s loss in bits per token "},{"text":"is","i":true},{"text":" the size it would compress the text to. Take a model scoring 2.0 nats per token on a corpus of one billion tokens:"}]},{"t":"code","lang":"","label":"Text","text":"2.0 nats × 1.443 = 2.89 bits per token\n2.89 × 1,000,000,000 = 2.89 × 10^9 bits = 361 MB"},{"t":"p","runs":[{"text":"The raw corpus, at roughly four characters per token and one byte per character, is about 4 GB. The model has compressed it eleven to one. For comparison, "},{"text":"gzip","code":true},{"text":" gets ordinary English to about 2.5 bits per character, which is roughly 10 bits per token, so this model is compressing three and a half times better than "},{"text":"gzip","code":true},{"text":". Claude Shannon estimated in 1951 that a fluent human predicts English at about one bit per character, which is roughly four bits per token. A modern large model beats that."}]},{"t":"p","runs":[{"text":"You can measure the "},{"text":"gzip","code":true},{"text":" side yourself in four lines, and it is a good number to have in your head:"}]},{"t":"code","lang":"python","label":"Python","text":"import zlib\ntext = open(\"some_english.txt\", \"rb\").read()\nbits_per_char = 8 * len(zlib.compress(text, 9)) / len(text)\nprint(round(bits_per_char, 2))    # about 2.5 for ordinary prose"},{"t":"h2","id":"two-honest-limits","runs":[{"text":"Two honest limits"}]},{"t":"p","runs":[{"text":"Surprise is about the model, not about the world.","b":true},{"text":" A rare typo costs a model many bits because it did not expect it, yet the typo carries almost no meaning. High surprise means “this model predicted poorly here”, nothing more profound. Two models can assign different surprises to the same token and both be reasonable."}]},{"t":"p","runs":[{"text":"The compressor must be counted.","b":true},{"text":" The 361 MB above only decompresses if the receiver has the model. A seven-billion-parameter model is about 14 GB in half precision, forty times larger than the compressed corpus. As a way of storing one billion tokens, the model is a terrible deal. The claim that stands is narrower and still striking: "},{"text":"per token","i":true},{"text":", a good predictor and a good compressor are the same object, and improving one improves the other by exactly the same amount."}]},{"t":"h2","id":"the-arithmetic-to-carry-forward","runs":[{"text":"The arithmetic to carry forward"}]},{"t":"p","runs":[{"text":"Surprise is "},{"text":"−log p","code":true},{"text":". Bits use base 2, nats use base e, and one nat is 1.443 bits. A loss is an average surprise. That average is also a compressed size. Everything in the rest of this module is a consequence of those four sentences."}]}],"check":{"question":[{"text":"A language model assigns probability 1/8 to the token that actually comes next. What did that token cost, and why?"}],"options":[{"text":[{"text":"8 bits, one for each possible outcome the model was choosing between."}],"correct":false,"tempting":[{"text":"Confuses the count of alternatives with the log of the probability; eight equally likely alternatives would cost log2(8) = 3 bits, not 8."}]},{"text":[{"text":"0.125 bits, because the cost is the probability itself."}],"correct":false,"tempting":[{"text":"Uses the probability directly instead of its negative log; a cost that fell as the probability fell would reward bad predictions."}]},{"text":[{"text":"3 bits, because −log2(1/8) = 3."}],"correct":true,"tempting":null},{"text":[{"text":"It cannot be said without knowing the vocabulary size."}],"correct":false,"tempting":[{"text":"Surprise depends only on the probability the model assigned to the outcome that occurred; the vocabulary size affects the uniform baseline, not this cost."}]}],"order":[1,0,3,2],"why":null},"prev":{"slug":"logs-and-loss","title":"Logs, and why losses are logged","minutes":8},"next":{"slug":"entropy-by-hand","title":"Entropy: the average surprise, computed by hand","minutes":9}}}