Information Content and Surprise
How much information do you get from learning that an event happened? If the event was certain, none: you learned nothing new. If it was very unlikely, a lot. Shannon turned this intuition into a number, the information content or surprise of an outcome.
The definition
The information content of an outcome with probability is
| Event | Probability | Information |
|---|---|---|
| a fair coin lands heads | 1 bit | |
| a fair die shows 6 | 2.585 bits | |
| one specific card from a deck | 5.700 bits | |
| a certain event | 1 | 0 bits |
import numpy as np
for name, p in [("coin heads", 1 / 2), ("die shows 6", 1 / 6), ("one card", 1 / 52), ("lottery 6 of 49", 1 / 13_983_816)]:
print(f"{name:16s} p = {p:.3g} h = {-np.log2(p):.3f} bits")coin heads p = 0.5 h = 1.000 bits
die shows 6 p = 0.167 h = 2.585 bits
one card p = 0.0192 h = 5.700 bits
lottery 6 of 49 p = 7.15e-08 h = 23.737 bits
Why the logarithm?
Three natural requirements force the logarithm:
- Rarer means more informative: decreases as increases.
- Certain events carry nothing: when .
- Independent events add: learning two independent outcomes gives . Since , we need , and the only continuous functions turning products into sums are logarithms.
import numpy as np
h = lambda p: -np.log2(p)
p_coin, p_die = 0.5, 1 / 6
print("h(coin) + h(die) =", round(h(p_coin) + h(p_die), 4))
print("h(coin and die) =", round(h(p_coin * p_die), 4))h(coin) + h(die) = 3.585
h(coin and die) = 3.585
Surprise as a code length
The information content has an operational meaning: in an optimal code, an outcome with probability gets a codeword of about bits (Optimal Code Lengths). Common outcomes get short codewords and rare ones long codewords, like Morse code giving E a single dot.
The game of 20 questions
Guessing an object among equally likely ones with yes/no questions takes exactly questions if each answer halves the candidates. Each such answer has probability and so delivers 1 bit. A badly chosen question ("is it this one specific object?") is answered "no" almost always, which delivers very little information on average.
import numpy as np
N = 64
h = lambda p: -np.log2(p)
print("halving question: each answer gives", h(0.5), "bit, need", int(np.log2(N)), "questions")
p_no = (N - 1) / N
avg = p_no * h(p_no) + (1 - p_no) * h(1 - p_no)
print(f"'is it object 1?': 'no' gives {h(p_no):.4f} bits, 'yes' gives {h(1 - p_no):.1f} bits")
print(f"average information of that question: {avg:.4f} bits")halving question: each answer gives 1.0 bit, need 6 questions
'is it object 1?': 'no' gives 0.0227 bits, 'yes' gives 6.0 bits
average information of that question: 0.1161 bits
That average, the expected surprise, is the entropy, the subject of the next lesson (Entropy).
In software
-np.log2(p)is the surprise in bits;-np.log(p)in nats.- Language models report per-token log-probabilities; their negative is the surprise of each token, and their average is the cross-entropy (Perplexity).
- Anomaly detectors often flag events whose surprise under a model exceeds a threshold.
Exercises
Exercise 1: A rare event
An event has probability . What is its information content?
Answer
10 bits.
Solution
.
Exercise 2: Two dice
How much information is in learning the outcome of two fair dice (ordered)? Compare with one die.
Answer
About 5.17 bits, twice the 2.585 bits of one die.
Solution
Each of the 36 outcomes has probability and . Independent events add, so it is .
Exercise 3: Surprise of letters (code)
In English text, e has probability about 0.127 and z about 0.0007. Compute the surprise of each.
Answer
About 2.98 bits for e and 10.48 bits for z.
Solution
import numpy as np
for letter, p in [("e", 0.127), ("z", 0.0007)]:
print(letter, round(float(-np.log2(p)), 2))e 2.98
z 10.48
Test yourself
The information content of an event with probability 1/8 is…
- 3 bits
- 8 bits
- 1/8 bit
- 0.125 bits
Why is information content a logarithm?
- So that information from independent events adds
- Because logarithms are fast to compute
- So that it is always below 1
- Because probabilities are logarithmic
A certain event (probability 1) carries…
- 0 bits
- 1 bit
- infinite information
- a negative amount of information