Typical Sets
Flip a coin with a thousand times. The single most likely outcome is all zeros, yet you will essentially never see it. What you see is a sequence with about 100 ones, and every such sequence has almost the same probability, close to . These sequences form the typical set, and it is the key to both halves of information theory.
The asymptotic equipartition property
For independent symbols from a distribution with entropy , the per-symbol surprise of the sequence
is an average of independent copies of the surprise, so by the law of large numbers it converges to its expectation, which is (The Law of Large Numbers). This is the asymptotic equipartition property (AEP).
import numpy as np
rng = np.random.default_rng(0)
f = 0.1
H = -f * np.log2(f) - (1 - f) * np.log2(1 - f)
for n in [10, 100, 1000, 10000]:
x = rng.random((2000, n)) < f
k = x.sum(axis=1)
surprise = -(k * np.log2(f) + (n - k) * np.log2(1 - f)) / n
print(f"n = {n:5d}: per-symbol surprise mean {surprise.mean():.4f}, std {surprise.std():.4f} (H = {H:.4f})")n = 10: per-symbol surprise mean 0.4744, std 0.2973 (H = 0.4690)
n = 100: per-symbol surprise mean 0.4712, std 0.0943 (H = 0.4690)
n = 1000: per-symbol surprise mean 0.4684, std 0.0301 (H = 0.4690)
n = 10000: per-symbol surprise mean 0.4691, std 0.0095 (H = 0.4690)
The spread shrinks like : long sequences all have surprise very close to bits per symbol.
The typical set
For a tolerance , the typical set contains the sequences whose per-symbol surprise is within of :
Its three properties, for large :
- It holds almost all the probability: .
- Its members are nearly equally likely: each has probability between and .
- It has about members: more precisely, , and for large also at least .
For a coin, membership depends only on the number of ones , so we can compute everything exactly:
import numpy as np
from scipy.special import gammaln
from scipy.stats import binom
f, beta = 0.1, 0.05
H = -f * np.log2(f) - (1 - f) * np.log2(1 - f)
for n in [100, 1000, 10000]:
k = np.arange(n + 1)
surprise = -(k * np.log2(f) + (n - k) * np.log2(1 - f)) / n
typical = np.abs(surprise - H) < beta
prob = binom.pmf(k[typical], n, f).sum()
log2_size = np.logaddexp2.reduce((gammaln(n + 1) - gammaln(k[typical] + 1) - gammaln(n - k[typical] + 1)) / np.log(2))
print(f"n = {n:5d}: P(typical) = {prob:.4f}, log2|T|/n = {log2_size / n:.4f}, out of 1 bit per symbol")n = 100: P(typical) = 0.3822, log2|T|/n = 0.4719, out of 1 bit per symbol
n = 1000: P(typical) = 0.8980, log2|T|/n = 0.5104, out of 1 bit per symbol
n = 10000: P(typical) = 1.0000, log2|T|/n = 0.5163, out of 1 bit per symbol
The size per symbol stays below , as property 3 says. The typical set is a vanishing fraction of all sequences, about of them, yet it carries almost all the probability.
The most likely sequence is not typical
The all-zeros sequence has per-symbol surprise bits, far from , so it is not in the typical set. Its probability is the largest of any single sequence, but it is one sequence against about typical ones.
import numpy as np
n, f = 1000, 0.1
print("P(all zeros) =", f"{0.9**n:.3e}")
print("P(one particular typical sequence with 100 ones) =", f"{2.0 ** (100 * np.log2(f) + 900 * np.log2(1 - f)):.3e}")P(all zeros) = 1.748e-46
P(one particular typical sequence with 100 ones) = 6.580e-142
Why it matters
- Compression: give each typical sequence a label of about bits and ignore (or escape) the rest. That is the proof of the source coding theorem (The Source Coding Theorem).
- Channel coding: decode by looking for the codeword that is jointly typical with the received signal (Random Codes and Typical-Set Decoding).
In software
- Simulation tests of compressors and codes should check average behaviour over long blocks, because short blocks fluctuate far from the typical-set picture.
scipy.stats.binomgives the exact distribution of the number of ones;np.logaddexp2.reduceadds huge counts stored as base-2 logs.
Exercises
Exercise 1: Size of the typical set
For a fair coin and , how big is the typical set, roughly?
Answer
All sequences.
Solution
Every sequence of a fair coin has probability with , so every sequence is typical. Compression is impossible.
Exercise 2: Probability of a typical sequence
A source has entropy 2 bits per symbol. About how probable is one typical sequence of length 50?
Answer
About .
Solution
Typical sequences have probability close to .
Exercise 3: AEP for a three-symbol source (code)
Draw 1000 sequences of length 5000 from and compute the mean and standard deviation of the per-symbol surprise. Compare with .
Answer
Mean about 1.2956, standard deviation about 0.0112, and .
Solution
import numpy as np
rng = np.random.default_rng(3)
P = np.array([0.6, 0.3, 0.1])
x = rng.choice(3, size=(1000, 5000), p=P)
s = -np.log2(P[x]).mean(axis=1)
print(round(float(s.mean()), 4), round(float(s.std()), 4), round(float(-np.sum(P * np.log2(P))), 4))1.2956 0.0112 1.2955
Test yourself
The asymptotic equipartition property is a consequence of…
- the law of large numbers
- Bayes' theorem
- Kraft's inequality
- the central limit theorem only for Gaussians
Approximately how many sequences are in the typical set of length n?
For a coin with P(1) = 0.1 and large n, the all-zeros sequence is…
- the most likely single sequence but not typical
- typical
- impossible
- the only typical sequence