TExcelTAD
Home › Information Theory › Typical Sets

Typical Sets

Flip a coin with P(1)=0.1P(1) = 0.1 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 2−nH2^{-nH}. These sequences form the typical set, and it is the key to both halves of information theory.

The asymptotic equipartition property

For nn independent symbols X1,…,XnX_1, \dots, X_n from a distribution with entropy HH, the per-symbol surprise of the sequence

−1nlog⁡2P(X1,…,Xn)=1n∑i=1nlog⁡21P(Xi)-\frac{1}{n}\log_2 P(X_1, \dots, X_n) = \frac{1}{n}\sum_{i=1}^n \log_2 \frac{1}{P(X_i)}

is an average of nn independent copies of the surprise, so by the law of large numbers it converges to its expectation, which is HH (The Law of Large Numbers). This is the asymptotic equipartition property (AEP).

python · try it yourself
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 1n\frac{1}{\sqrt{n}}: long sequences all have surprise very close to HH bits per symbol.

The typical set

For a tolerance β>0\beta > 0, the typical set contains the sequences whose per-symbol surprise is within β\beta of HH:

Tnβ={x:∣−1nlog⁡2P(x)−H∣<β}T_{n\beta} = \left\{ \mathbf{x} : \left| -\frac{1}{n}\log_2 P(\mathbf{x}) - H \right| < \beta \right\}

Its three properties, for large nn:

  1. It holds almost all the probability: P(X∈Tnβ)→1P(\mathbf{X} \in T_{n\beta}) \to 1.
  2. Its members are nearly equally likely: each has probability between 2−n(H+β)2^{-n(H + \beta)} and 2−n(H−β)2^{-n(H - \beta)}.
  3. It has about 2nH2^{nH} members: more precisely, ∣Tnβ∣≤2n(H+β)|T_{n\beta}| \le 2^{n(H + \beta)}, and for large nn also at least (1−ϵ)2n(H−β)(1 - \epsilon)2^{n(H - \beta)}.

For a coin, membership depends only on the number of ones kk, so we can compute everything exactly:

python · try it yourself
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 H+β=0.519H + \beta = 0.519, as property 3 says. The typical set is a vanishing fraction of all 2n2^n sequences, about 2−n(1−H)2^{-n(1 - H)} of them, yet it carries almost all the probability.

The most likely sequence is not typical

The all-zeros sequence has per-symbol surprise log⁡210.9=0.152\log_2 \frac{1}{0.9} = 0.152 bits, far from H=0.469H = 0.469, so it is not in the typical set. Its probability 0.9n0.9^n is the largest of any single sequence, but it is one sequence against about 20.469n2^{0.469n} typical ones.

python · try it yourself
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 nHnH 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.binom gives the exact distribution of the number of ones; np.logaddexp2.reduce adds huge counts stored as base-2 logs.

Exercises

Exercise 1: Size of the typical set

For a fair coin and n=100n = 100, how big is the typical set, roughly?

Answer

All 21002^{100} sequences.

Solution

Every sequence of a fair coin has probability 2−100=2−nH2^{-100} = 2^{-nH} with H=1H = 1, 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 2−1002^{-100}.

Solution

Typical sequences have probability close to 2−nH=2−50×22^{-nH} = 2^{-50 \times 2}.

Exercise 3: AEP for a three-symbol source (code)

Draw 1000 sequences of length 5000 from P=(0.6,0.3,0.1)P = (0.6, 0.3, 0.1) and compute the mean and standard deviation of the per-symbol surprise. Compare with HH.

Answer

Mean about 1.2956, standard deviation about 0.0112, and H=1.2955H = 1.2955.

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

  1. the law of large numbers
  2. Bayes' theorem
  3. Kraft's inequality
  4. the central limit theorem only for Gaussians

Approximately how many sequences are in the typical set of length n?

  1. 2nH2^{nH}
  2. 2n2^n
  3. nHn^H
  4. nHnH

For a coin with P(1) = 0.1 and large n, the all-zeros sequence is…

  1. the most likely single sequence but not typical
  2. typical
  3. impossible
  4. the only typical sequence