{"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":"entropy-by-hand","title":"Entropy: the average surprise, computed by hand","url":"https://addaly.com/learn/maths-you-need/entropy-by-hand","position":43,"of":76,"minutes":9,"answer":"Entropy is the average surprise of a distribution, largest when outcomes are equally likely and zero when one is certain.","keyIdea":"Entropy is the average surprise of a distribution, largest when outcomes are equally likely and zero when one is certain, and the entropy of your label column is the loss of a model that has learned nothing but the base rate, which is the first number to compute."},"body":{"format":"markdown","text":"## The definition, and one calculation\n\nEntropy is the average surprise of a distribution: what you expect to pay per outcome, in bits, if outcomes arrive according to that distribution.\n\n```\nH(p) = − Σ p_i × log2(p_i)\n```\n\nDo one by hand and it stops being a formula. Four outcomes with probabilities 0.5, 0.25, 0.125 and 0.125:\n\n```\n0.5   × 1 bit  = 0.500\n0.25  × 2 bits = 0.500\n0.125 × 3 bits = 0.375\n0.125 × 3 bits = 0.375\n                 -----\nH = 1.75 bits\n```\n\nThe surprise of each outcome, weighted by how often it happens. Note that the rare outcomes cost three bits each but only occur an eighth of the time, so they contribute less than the common one.\n\nNow the same four outcomes, equally likely. Each costs `log2(4) = 2` bits and `H = 2` bits. That is the maximum: **entropy is largest when every outcome is equally likely**, and then it equals `log2` of the number of outcomes.\n\nAnd a nearly certain distribution, 0.97, 0.01, 0.01, 0.01:\n\n```\n0.97 × 0.044 = 0.043\n0.01 × 6.644 = 0.066   (three times)\nH = 0.043 + 0.199 = 0.242 bits\n```\n\nRare outcomes are very surprising when they happen, 6.6 bits each, but they almost never happen, so the average is small. Entropy is zero only when one outcome has probability 1.\n\n## Effective number of choices\n\nTwo to the power of the entropy is the **effective number of equally likely outcomes**. The 1.75-bit distribution above behaves like `2^1.75 = 3.36` equally likely choices; the 0.242-bit one like `2^0.242 = 1.18`. This number has a name when the distribution is a model's prediction of the next token: perplexity. The course `how-llms-work` treats what it means for a language model; here the point is only that it is `2^H`, or `e^H` if `H` is in nats, and nothing more mysterious.\n\n## The number to compute before training anything\n\nSuppose a binary label is 1 in ten per cent of rows and 0 in ninety. A model that knows nothing but that base rate predicts 0.1 every time, and its average loss is the entropy of the label:\n\n```\nH = −(0.1 × log2 0.1 + 0.9 × log2 0.9)\n  = −(0.1 × −3.322 + 0.9 × −0.152)\n  = 0.332 + 0.137 = 0.469 bits = 0.325 nats\n```\n\nThat is the loss of a model that has learned nothing except the frequency. Any model reporting a cross-entropy above 0.325 nats on that data is worse than a constant. People routinely train for hours, see a loss of 0.40, and call it progress, because they never computed the number the constant predictor gets. For a balanced binary label the floor is `ln 2 = 0.693` nats; for ten balanced classes it is `ln 10 = 2.303`. Compute it, write it on the plot, and judge the curve against it.\n\n## Where else entropy appears\n\n- **A model's own uncertainty.** The entropy of the next-token distribution says how decided the model is at that position. Low entropy on a factual token and high entropy on a name it is inventing is a pattern worth looking for.\n- **Choosing a split.** Decision trees pick the feature whose split most reduces the entropy of the labels; `machine-learning-foundations` works through it.\n- **How peaked a softmax is.** The entropy of an attention row tells you whether a head is looking at one token or spreading over many.\n\n## What entropy is blind to\n\nEntropy sees only the list of probabilities, not what they are attached to. A distribution split evenly between \"cat\" and \"kitten\" has exactly one bit of entropy, and so does one split between \"cat\" and \"lorry\", though the second is far more uncertain in any sense that matters. Entropy has no notion of two outcomes being close. If nearness matters, you need a metric on the outcomes as well, which is a different tool.\n\nA second caution concerns continuous quantities. The entropy of a density, called differential entropy, can be negative and changes when you change units; a Gaussian with standard deviation `σ` has entropy `½ log2(2πe σ²)`, which is below zero once `σ < 0.24`. It is still useful for comparing two densities in the same units, but the intuition \"entropy is a count of bits\" holds only for discrete outcomes.\n\n## Doing it in code\n\n```python\nimport numpy as np\np = np.array([0.5, 0.25, 0.125, 0.125])\nH = -(p * np.log2(p)).sum()      # 1.75\n```\n\nWith zeros in `p` this gives a `nan` from `0 × log 0`; mask them out, because the correct contribution of an impossible outcome is zero. That single line, applied to your label column, is the baseline that the next lesson turns into a floor no model can go beneath."},"check":{"question":"A binary label is positive in 10 per cent of rows. After an hour of training a model reports a cross-entropy of 0.40 nats. What does that number say?","options":[{"text":"It is a reasonable result, because 0.40 is well below the 0.693 nats of a coin flip.","correct":false,"tempting":"Uses the balanced-label baseline for an unbalanced label; the relevant floor is the entropy of a 10/90 split, 0.325 nats."},{"text":"It is worse than predicting the base rate, which scores 0.325 nats.","correct":true},{"text":"It is a good result, because any loss under 1 nat means the model is mostly right.","correct":false,"tempting":"Reads the loss as an absolute quality score; a cross-entropy only means something against the entropy of the labels themselves."},{"text":"Nothing can be concluded without the accuracy figure as well.","correct":false,"tempting":"The entropy of the labels is enough to judge this: a loss above it means the model is doing worse than a constant, whatever the accuracy."}]},"prev":{"slug":"surprise-and-bits","title":"Surprise, bits, and why prediction is compression","url":"https://addaly.com/learn/maths-you-need/surprise-and-bits"},"next":{"slug":"cross-entropy-and-kl-divergence","title":"Cross-entropy and KL: the cost of believing the wrong distribution","url":"https://addaly.com/learn/maths-you-need/cross-entropy-and-kl-divergence"},"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) 'Entropy: the average surprise, computed by hand', in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/entropy-by-hand (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":"Entropy: the average surprise, computed by hand","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":"Entropy is the average surprise of a distribution, largest when outcomes are equally likely and zero when one is certain, and the entropy of your label column is the loss of a model that has learned nothing but the base rate, which is the first number to compute.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"the-definition-and-one-calculation","runs":[{"text":"The definition, and one calculation"}]},{"t":"p","runs":[{"text":"Entropy is the average surprise of a distribution: what you expect to pay per outcome, in bits, if outcomes arrive according to that distribution."}]},{"t":"code","lang":"","label":"Text","text":"H(p) = − Σ p_i × log2(p_i)"},{"t":"p","runs":[{"text":"Do one by hand and it stops being a formula. Four outcomes with probabilities 0.5, 0.25, 0.125 and 0.125:"}]},{"t":"code","lang":"","label":"Text","text":"0.5   × 1 bit  = 0.500\n0.25  × 2 bits = 0.500\n0.125 × 3 bits = 0.375\n0.125 × 3 bits = 0.375\n                 -----\nH = 1.75 bits"},{"t":"p","runs":[{"text":"The surprise of each outcome, weighted by how often it happens. Note that the rare outcomes cost three bits each but only occur an eighth of the time, so they contribute less than the common one."}]},{"t":"p","runs":[{"text":"Now the same four outcomes, equally likely. Each costs "},{"text":"log2(4) = 2","code":true},{"text":" bits and "},{"text":"H = 2","code":true},{"text":" bits. That is the maximum: "},{"text":"entropy is largest when every outcome is equally likely","b":true},{"text":", and then it equals "},{"text":"log2","code":true},{"text":" of the number of outcomes."}]},{"t":"p","runs":[{"text":"And a nearly certain distribution, 0.97, 0.01, 0.01, 0.01:"}]},{"t":"code","lang":"","label":"Text","text":"0.97 × 0.044 = 0.043\n0.01 × 6.644 = 0.066   (three times)\nH = 0.043 + 0.199 = 0.242 bits"},{"t":"p","runs":[{"text":"Rare outcomes are very surprising when they happen, 6.6 bits each, but they almost never happen, so the average is small. Entropy is zero only when one outcome has probability 1."}]},{"t":"h2","id":"effective-number-of-choices","runs":[{"text":"Effective number of choices"}]},{"t":"p","runs":[{"text":"Two to the power of the entropy is the "},{"text":"effective number of equally likely outcomes","b":true},{"text":". The 1.75-bit distribution above behaves like "},{"text":"2^1.75 = 3.36","code":true},{"text":" equally likely choices; the 0.242-bit one like "},{"text":"2^0.242 = 1.18","code":true},{"text":". This number has a name when the distribution is a model’s prediction of the next token: perplexity. The course "},{"text":"how-llms-work","code":true},{"text":" treats what it means for a language model; here the point is only that it is "},{"text":"2^H","code":true},{"text":", or "},{"text":"e^H","code":true},{"text":" if "},{"text":"H","code":true},{"text":" is in nats, and nothing more mysterious."}]},{"t":"h2","id":"the-number-to-compute-before-training-anything","runs":[{"text":"The number to compute before training anything"}]},{"t":"p","runs":[{"text":"Suppose a binary label is 1 in ten per cent of rows and 0 in ninety. A model that knows nothing but that base rate predicts 0.1 every time, and its average loss is the entropy of the label:"}]},{"t":"code","lang":"","label":"Text","text":"H = −(0.1 × log2 0.1 + 0.9 × log2 0.9)\n  = −(0.1 × −3.322 + 0.9 × −0.152)\n  = 0.332 + 0.137 = 0.469 bits = 0.325 nats"},{"t":"p","runs":[{"text":"That is the loss of a model that has learned nothing except the frequency. Any model reporting a cross-entropy above 0.325 nats on that data is worse than a constant. People routinely train for hours, see a loss of 0.40, and call it progress, because they never computed the number the constant predictor gets. For a balanced binary label the floor is "},{"text":"ln 2 = 0.693","code":true},{"text":" nats; for ten balanced classes it is "},{"text":"ln 10 = 2.303","code":true},{"text":". Compute it, write it on the plot, and judge the curve against it."}]},{"t":"h2","id":"where-else-entropy-appears","runs":[{"text":"Where else entropy appears"}]},{"t":"ul","items":[[{"text":"A model’s own uncertainty.","b":true},{"text":" The entropy of the next-token distribution says how decided the model is at that position. Low entropy on a factual token and high entropy on a name it is inventing is a pattern worth looking for."}],[{"text":"Choosing a split.","b":true},{"text":" Decision trees pick the feature whose split most reduces the entropy of the labels; "},{"text":"machine-learning-foundations","code":true},{"text":" works through it."}],[{"text":"How peaked a softmax is.","b":true},{"text":" The entropy of an attention row tells you whether a head is looking at one token or spreading over many."}]]},{"t":"h2","id":"what-entropy-is-blind-to","runs":[{"text":"What entropy is blind to"}]},{"t":"p","runs":[{"text":"Entropy sees only the list of probabilities, not what they are attached to. A distribution split evenly between “cat” and “kitten” has exactly one bit of entropy, and so does one split between “cat” and “lorry”, though the second is far more uncertain in any sense that matters. Entropy has no notion of two outcomes being close. If nearness matters, you need a metric on the outcomes as well, which is a different tool."}]},{"t":"p","runs":[{"text":"A second caution concerns continuous quantities. The entropy of a density, called differential entropy, can be negative and changes when you change units; a Gaussian with standard deviation "},{"text":"σ","code":true},{"text":" has entropy "},{"text":"½ log2(2πe σ²)","code":true},{"text":", which is below zero once "},{"text":"σ < 0.24","code":true},{"text":". It is still useful for comparing two densities in the same units, but the intuition “entropy is a count of bits” holds only for discrete outcomes."}]},{"t":"h2","id":"doing-it-in-code","runs":[{"text":"Doing it in code"}]},{"t":"code","lang":"python","label":"Python","text":"import numpy as np\np = np.array([0.5, 0.25, 0.125, 0.125])\nH = -(p * np.log2(p)).sum()      # 1.75"},{"t":"p","runs":[{"text":"With zeros in "},{"text":"p","code":true},{"text":" this gives a "},{"text":"nan","code":true},{"text":" from "},{"text":"0 × log 0","code":true},{"text":"; mask them out, because the correct contribution of an impossible outcome is zero. That single line, applied to your label column, is the baseline that the next lesson turns into a floor no model can go beneath."}]}],"check":{"question":[{"text":"A binary label is positive in 10 per cent of rows. After an hour of training a model reports a cross-entropy of 0.40 nats. What does that number say?"}],"options":[{"text":[{"text":"It is a reasonable result, because 0.40 is well below the 0.693 nats of a coin flip."}],"correct":false,"tempting":[{"text":"Uses the balanced-label baseline for an unbalanced label; the relevant floor is the entropy of a 10/90 split, 0.325 nats."}]},{"text":[{"text":"It is worse than predicting the base rate, which scores 0.325 nats."}],"correct":true,"tempting":null},{"text":[{"text":"It is a good result, because any loss under 1 nat means the model is mostly right."}],"correct":false,"tempting":[{"text":"Reads the loss as an absolute quality score; a cross-entropy only means something against the entropy of the labels themselves."}]},{"text":[{"text":"Nothing can be concluded without the accuracy figure as well."}],"correct":false,"tempting":[{"text":"The entropy of the labels is enough to judge this: a loss above it means the model is doing worse than a constant, whatever the accuracy."}]}],"order":[2,1,0,3],"why":null},"prev":{"slug":"surprise-and-bits","title":"Surprise, bits, and why prediction is compression","minutes":9},"next":{"slug":"cross-entropy-and-kl-divergence","title":"Cross-entropy and KL: the cost of believing the wrong distribution","minutes":10}}}