TExcelTAD
Home › Information Theory › Information Content and Surprise

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 xx with probability P(x)P(x) is

h(x)=log⁡21P(x)=−log⁡2P(x)bitsh(x) = \log_2 \frac{1}{P(x)} = -\log_2 P(x) \quad \text{bits}
Event Probability Information
a fair coin lands heads 12\frac{1}{2} 1 bit
a fair die shows 6 16\frac{1}{6} 2.585 bits
one specific card from a deck 152\frac{1}{52} 5.700 bits
a certain event 1 0 bits
python · try it yourself
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:

  1. Rarer means more informative: hh decreases as P(x)P(x) increases.
  2. Certain events carry nothing: h=0h = 0 when P(x)=1P(x) = 1.
  3. Independent events add: learning two independent outcomes gives h(x)+h(y)h(x) + h(y). Since P(x,y)=P(x)P(y)P(x, y) = P(x)P(y), we need h(P(x)P(y))=h(P(x))+h(P(y))h(P(x)P(y)) = h(P(x)) + h(P(y)), and the only continuous functions turning products into sums are logarithms.
python · try it yourself
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 P(x)P(x) gets a codeword of about log⁡21P(x)\log_2 \frac{1}{P(x)} 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 2k2^k equally likely ones with yes/no questions takes exactly kk questions if each answer halves the candidates. Each such answer has probability 12\frac{1}{2} 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.

python · try it yourself
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 11024\frac{1}{1024}. What is its information content?

Answer

10 bits.

Solution

log⁡21024=10\log_2 1024 = 10.

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 136\frac{1}{36} and log⁡236=5.170\log_2 36 = 5.170. Independent events add, so it is 2×2.5852 \times 2.585.

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
python · try it yourself
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…

  1. 3 bits
  2. 8 bits
  3. 1/8 bit
  4. 0.125 bits

Why is information content a logarithm?

  1. So that information from independent events adds
  2. Because logarithms are fast to compute
  3. So that it is always below 1
  4. Because probabilities are logarithmic

A certain event (probability 1) carries…

  1. 0 bits
  2. 1 bit
  3. infinite information
  4. a negative amount of information