NeuroGenesys.
Information TheoryAcademy • Foundations • Lesson 1 of 3

Entropy: measuring surprise

7 min read • By Will Schulz • NeuroGenesys AI Academy

How much information does an outcome carry, and how few bits can you get away with to describe a random source? Entropy answers both questions at once, and the answer turns out to be the quantity every language model is trained to estimate.

Surprise as a number

Start with a single event of probability pp. We want a measure of how surprising it is, s(p)s(p), with three properties: certain events carry no surprise (s(1)=0s(1)=0), rarer events are more surprising (ss decreases in pp), and the surprise of two independent events adds (s(pq)=s(p)+s(q)s(pq) = s(p) + s(q)). The only continuous function satisfying all three is a logarithm:

s(p)=−log⁡p.s(p) = -\log p .

The base of the logarithm sets the unit. Base 2 gives bits; the natural log gives nats (1 nat≈1.4431\ \text{nat} \approx 1.443 bits). A fair coin flip landing heads has surprisal −log⁡212=1-\log_2 \tfrac12 = 1 bit. Drawing the ace of spades from a shuffled deck has surprisal log⁡252≈5.70\log_2 52 \approx 5.70 bits.

Entropy is expected surprise

For a discrete random variable XX with distribution pp, the entropy is the average surprisal:

H(X)=−∑xp(x)log⁡p(x)=Ex∼p[−log⁡p(x)],H(X) = -\sum_x p(x)\log p(x) = \mathbb{E}_{x\sim p}\big[-\log p(x)\big],

with the convention 0log⁡0=00 \log 0 = 0. Claude Shannon introduced this quantity in 1948 and showed that, up to the choice of unit, it is the unique measure of uncertainty satisfying a short list of natural requirements: continuity in the probabilities, growth with the number of equally likely outcomes, and consistency when a choice is broken into successive sub-choices.[1]

Three facts follow directly from the definition:[2]

  • H(X)≥0H(X) \ge 0, with equality only when one outcome has probability 1.
  • For KK outcomes, H(X)≤log⁡KH(X) \le \log K, with equality only for the uniform distribution. Uniform is maximally uncertain.
  • Entropy depends only on the probabilities, not on the labels. Relabelling the outcomes does not change it.

Worked example: a biased coin

A coin lands heads with probability 0.90.9. Its entropy in bits is

H=−0.9log⁡20.9−0.1log⁡20.1=0.9(0.152)+0.1(3.322)≈0.137+0.332=0.469 bits.H = -0.9\log_2 0.9 - 0.1\log_2 0.1 = 0.9(0.152) + 0.1(3.322) \approx 0.137 + 0.332 = 0.469 \text{ bits}.

The rare tails outcome is very surprising (3.32 bits), but it happens so seldom that it contributes only a third of a bit on average. The common heads outcome is barely surprising at all. Overall, the coin carries less than half the information of a fair coin.

Check your understanding

A die is loaded so that it shows 6 with probability 1/2 and each of 1–5 with probability 1/10. What is its entropy in bits, and how does it compare with a fair die?

Show answer +

H=−12log⁡212−5⋅110log⁡2110=0.5+0.5⋅3.322≈2.16H = -\tfrac12\log_2\tfrac12 - 5\cdot\tfrac{1}{10}\log_2\tfrac{1}{10} = 0.5 + 0.5\cdot 3.322 \approx 2.16 bits. A fair die has log⁡26≈2.585\log_2 6 \approx 2.585 bits, the maximum for six outcomes. The bias removes about 0.42 bits of uncertainty.

Entropy is the limit of compression

The definition would be only a curiosity if it did not also measure something operational. Shannon's source coding theorem shows that it does. For symbols drawn independently from pp, no lossless code can use fewer than H(X)H(X) bits per symbol on average. Codes exist that come arbitrarily close to that limit when symbols are encoded in long blocks.[1][2] Entropy is the irreducible cost of describing the source.

The connection is easiest to see when probabilities are powers of two. Take four symbols with probabilities 12,14,18,18\tfrac12, \tfrac14, \tfrac18, \tfrac18:

SymbolppSurprisal (bits)Codeword
A1/210
B1/4210
C1/83110
D1/83111

The entropy is 12(1)+14(2)+18(3)+18(3)=1.75\tfrac12(1)+\tfrac14(2)+\tfrac18(3)+\tfrac18(3) = 1.75 bits. The code gives each symbol a codeword whose length equals its surprisal, and no codeword is a prefix of another, so a stream of bits decodes unambiguously. Its average length is exactly 1.75 bits, compared with the 2 bits a fixed-length code needs. Huffman's 1952 algorithm builds an optimal prefix code of this kind for any finite distribution.[3] When the probabilities are not powers of two, a symbol-by-symbol code can lose up to one bit per symbol. Coding longer blocks, or using arithmetic coding (covered in Prediction is compression), closes that gap.

The lesson to keep is that surprisal is code length. An outcome with probability pp "deserves" a codeword of about −log⁡2p-\log_2 p bits, and entropy is the average length when the code matches the source.

Joint, conditional and mutual information

Real data are sequences, and symbols depend on their context. For two variables XX and YY:

  • Joint entropy H(X,Y)=−∑x,yp(x,y)log⁡p(x,y)H(X,Y) = -\sum_{x,y} p(x,y)\log p(x,y).
  • Conditional entropy H(Y∣X)=−∑x,yp(x,y)log⁡p(y∣x)H(Y\mid X) = -\sum_{x,y} p(x,y)\log p(y\mid x). This is the average uncertainty left in YY once XX is known.
  • Chain rule H(X,Y)=H(X)+H(Y∣X)H(X,Y) = H(X) + H(Y\mid X).
  • Mutual information I(X;Y)=H(Y)−H(Y∣X)≥0I(X;Y) = H(Y) - H(Y\mid X) \ge 0. This is how much knowing XX reduces uncertainty about YY.

Because mutual information is never negative, conditioning cannot increase entropy on average: H(Y∣X)≤H(Y)H(Y\mid X)\le H(Y).[2] More context can only help prediction, at least in expectation.

Applied repeatedly, the chain rule factors the entropy of a whole sequence:

H(X1,…,Xn)=∑t=1nH(Xt∣X1,…,Xt−1).H(X_1,\dots,X_n) = \sum_{t=1}^{n} H(X_t \mid X_1,\dots,X_{t-1}).

This is exactly the factorisation an autoregressive language model uses. The model predicts each token from everything before it, and the total uncertainty of a text is the sum of its per-token conditional uncertainties.

How much information is in English?

Shannon measured this directly in 1951. Treating letters as independent with their observed frequencies gives about 4.14 bits per letter, below the 4.7 bits of a uniform 26-letter alphabet. Conditioning on the previous letter (digram statistics) brings this down to about 3.56 bits. When people were asked to guess the next letter of a passage with up to about 100 letters of context, the estimate fell to roughly one bit per letter, which implies that English is around 75% redundant.[4]

That ladder of numbers describes what language modelling does. Each richer model of context lowers the conditional entropy it assigns to the next symbol. A modern language model's training loss, measured in bits per character or per token, places it on the same ladder. The true conditional entropy of the text sits underneath as a floor that no model can go below.

Check your understanding

Why can a language model's per-token loss never fall below the true conditional entropy H(Xt∣X<t)H(X_t \mid X_{<t}) of the text, no matter how large the model?

Show answer +

The loss is the average code length the model assigns, and the source coding theorem says no code can beat the source's entropy on average. The gap between the model's loss and the true entropy is exactly a KL divergence, which is never negative. This is developed in Cross-entropy and KL divergence.

Entropy in modern models

Entropy appears in AI systems in several places beyond the loss:

  • The predictive distribution. At each step a language model outputs a distribution over its vocabulary. The entropy of that distribution measures how uncertain the model is about the next token. A low value means one continuation dominates, and a high value means many are plausible.
  • Sampling temperature. Logits ziz_i are turned into probabilities with pi∝exp⁡(zi/T)p_i \propto \exp(z_i/T). As T→0T\to 0 the distribution collapses onto the argmax and its entropy goes to 0. As T→∞T\to\infty it approaches uniform, with entropy log⁡K\log K. Temperature is therefore a direct control over output entropy.
  • Continuous variables. For densities, the analogue h(X)=−∫flog⁡fh(X) = -\int f\log f (differential entropy) can be negative and changes under a change of variables, so it does not have the same "bits to describe" meaning.[2] Differences of differential entropies, such as mutual information, keep their meaning.

Key takeaways

  • Surprisal −log⁡p-\log p is the only additive, continuous measure of how unexpected an outcome is. Entropy is its expectation.
  • Entropy is the minimum average number of bits per symbol needed to describe a source losslessly. An outcome with probability pp deserves a codeword of about −log⁡2p-\log_2 p bits.
  • The chain rule breaks the entropy of a sequence into per-step conditional entropies, which is the structure autoregressive language models exploit.
  • More context never increases entropy on average. Shannon's estimates for English fell from about 4.14 to about 1 bit per letter as context grew.
  • Temperature and predictive entropy are the everyday faces of entropy in deployed models.

Sources

  1. C. E. Shannon (1948). "A Mathematical Theory of Communication." Bell System Technical Journal 27(3), 379–423. https://doi.org/10.1002/j.1538-7305.1948.tb01338.x ↩ ↩2

  2. T. M. Cover and J. A. Thomas (2006). Elements of Information Theory, 2nd ed. Wiley-Interscience. https://doi.org/10.1002/047174882X ↩ ↩2 ↩3 ↩4

  3. D. A. Huffman (1952). "A Method for the Construction of Minimum-Redundancy Codes." Proceedings of the IRE 40(9), 1098–1101. https://doi.org/10.1109/JRPROC.1952.273898 ↩

  4. C. E. Shannon (1951). "Prediction and Entropy of Printed English." Bell System Technical Journal 30(1), 50–64. https://www.nokia.com/bell-labs/publications-and-media/publications/prediction-and-entropy-of-printed-english/ ↩

Leadership

A word from the founder — on why the brain is the blueprint, and why the next decade of intelligence will be built here.

"The brain is the universe's only proof that general intelligence is possible. We treat it as the blueprint."

From cortical interfaces to photonic silicon, every program at NeuroGenesys exists to answer one question: how does intelligence arise — and how do we build it safely.

Will Schulz

Will Schulz

Founder, NeuroGenesys • AW3 Technology