第 16 章

数据压缩与 Shannon 无噪声编码定理

把压缩定义成「把消息映射成二进制串」,用熵衡量理论极限;先证下界(任何编码都不能低于 nH),再用典型序列构造一个达到 nH + o(n) 的编码,从而证明熵就是一阶信源的每符号最优码长。

Data Compression and Shannon’s Noiseless Coding Theorem

Compression is defined as a map from messages to binary strings, and entropy measures the limit. We prove the converse (no code beats nH) and then build a typical-set code achieving nH + o(n), so entropy is the best asymptotic per-symbol length for a first-order source.

首发于 2025.12.18中 / EN
系统课程information-theoryentropysource-codingcompression
语言
字号
目录
篇内目录

问题:一条消息最短能有多短

早期的电报按词收费,一个词一美元。于是人们编出「电码本」:把一长串常用说法换成一个短词。课程举了几个真实条目——AZKHE 表示 clean bill of health,NBET 表示 captain is insane。今天发短信写 BRB,动机完全相同:把常出现的东西换成短符号。

电报时代的做法靠人工编表,凭经验挑选常用短语。本讲要回答的是它的数学版本:

给定一个信源,任何压缩方案的平均码长下限是多少?这个下限能不能达到?

答案是 Shannon 1948 年在同一栋楼里给出的:极限就是熵。本讲完整证明这个结论的两个方向。

时间轴

时间内容
0:00历史引子:电报电码本、短信缩写;信息论 1948 年诞生于 MIT 2 号楼
5:19一阶信源的定义:字母表 + 概率分布,独立同分布抽样
8:14编码函数与可解码性;一次性块编码与「END」结束标记
11:34例子:k=2k=2、p=(7/8,1/8)p=(7/8,1/8)、n=2n=2,朴素编码与改进编码
17:21二元熵的定义
18:38Shannon 无噪声编码定理的两个方向
22:47直觉:均匀分布最难压缩
26:41引理:均匀分布下期望码长至少为 log⁡m−O(1)\log m-O(1)(二叉树论证)
30:33固定每种符号的出现次数;多项式系数计数 MM
37:48Stirling 公式把 log⁡M\log M 化成 nH+O(log⁡n)nH+O(\log n)
44:11ε\varepsilon-典型序列与 Chebyshev 界
47:46用全期望公式拼出下界
55:12上界:显式构造「典型 / 非典型」两支编码
1:05:51这套构造为什么不实用;后续转向 Huffman 码

一阶信源

一个一阶信源(first-order source)由两部分组成:

  • 字母表 A={a1,…,ak}A=\{a_1,\dots,a_k\},共 kk 个符号;
  • 分布 p=(p1,…,pk)p=(p_1,\dots,p_k),满足 pi≥0p_i\ge 0、∑ipi=1\sum_i p_i=1。

课程把它比作一颗不均匀的 kk 面骰子:每掷一次得到一个符号,掷 nn 次并保持顺序,就得到一条长度 nn 的消息 x1x2⋯xnx_1x_2\cdots x_n。每次抽样独立且分布相同,所以

Pr⁡(x1⋯xn)=∏j=1npxj.\Pr(x_1\cdots x_n)=\prod_{j=1}^{n}p_{x_j}.

字母可以是字母表的字母,也可以是词;课程强调这是最简单的模型,真实语言需要马尔可夫链等更复杂的结构,但本讲的理论会原样推广过去。

编码函数与可解码

编码函数 φ:An→{0,1}∗\varphi:A^n\to\{0,1\}^* 把每条长度 nn 的消息映射成一个变长二进制串。

为什么允许变长?因为直观上高频消息应该配短码字。例子中 a1a1a_1a_1 的概率高达 49/6449/64,如果它能用 1 位表示,平均长度立刻下降。

变长带来一个必须写清的约束:映射必须单射。若两条不同消息编成同一个串,就无法解码。这里暂时不要求前缀无关:本讲的模型是一次性块编码,整条消息发完再发一个 END,解码器知道边界在哪里。等下讲的 Huffman 码才需要前缀无关,因为那里要逐符号流式解码。

用 L(φ,x)=∣φ(x)∣L(\varphi,x)=|\varphi(x)| 表示消息 xx 的码长,期望码长是

E[L]=∑x∈AnPr⁡(x) ∣φ(x)∣.\mathbb E[L]=\sum_{x\in A^n}\Pr(x)\,|\varphi(x)|.

这正是要最小化的成本。

例子:k=2k=2,p=(7/8,1/8)p=(7/8,1/8),n=2n=2

长度为 2 的消息有四条,概率分别是

消息概率朴素码改进码
a1a1a_1a_149/6449/64000
a1a2a_1a_27/647/640110
a2a1a_2a_17/647/6410110
a2a2a_2a_21/641/6411111

朴素编码逐符号替换,每条消息都是 2 位,期望长度恰好是 22。

改进编码把最可能的 a1a1a_1a_1 压到 1 位,代价是让两条较罕见的消息变成 3 位。期望长度:

E[L]=4964⋅1+764⋅2+764⋅3+164⋅3=49+14+21+364=8764≈1.359.\mathbb E[L]=\frac{49}{64}\cdot1+\frac{7}{64}\cdot2+\frac{7}{64}\cdot3+\frac{1}{64}\cdot3 =\frac{49+14+21+3}{64}=\frac{87}{64}\approx1.359 .

确实小于 22。注意改进码的四个码字互不相同,且没有一个是另一个的前缀——这不是巧合,后面会看到前缀结构正是可达性的来源。

那么极限是多少?对 k=2k=2、p=(7/8,1/8)p=(7/8,1/8),熵为

H ⁣(78,18)=−78log⁡278−18log⁡218≈0.169+0.375=0.544 比特.H\!\left(\tfrac78,\tfrac18\right) =-\tfrac78\log_2\tfrac78-\tfrac18\log_2\tfrac18 \approx 0.169+0.375=0.544\ \text{比特}.

每符号 0.544 比特,远低于 1 比特。n=2n=2 时只能做到 1.359/2≈0.68,差距来自 o(n)o(n) 修正项;nn 越大越接近熵。

熵

对分布 p=(p1,…,pk)p=(p_1,\dots,p_k),二元熵定义为

H(p)=−∑i=1kpilog⁡2pi.H(p)=-\sum_{i=1}^{k}p_i\log_2 p_i .

因为 0<pi≤10<p_i\le1,每个 log⁡pi≤0\log p_i\le0,负号使 H(p)≥0H(p)\ge0。约定 0log⁡0=00\log 0=0。

几个基本事实(后文会用到):

  • 均匀分布 pi=1/kp_i=1/k 使熵最大,H=log⁡2kH=\log_2 k;
  • 若某个 pi=1p_i=1,则 H=0H=0——信源没有不确定性,不需要传信息;
  • HH 只依赖概率,不依赖符号的名字。

Shannon 无噪声编码定理

设一阶信源的熵为 HH。定理包含两个方向。

下界(converse)。 对任意可解码的编码函数 φ\varphi,

E[L] ≥ Hn−o(n).\mathbb E[L]\ \ge\ Hn-o(n).

上界(achievability)。 存在可解码的编码函数 φ\varphi,使得

E[L] ≤ Hn+o(n).\mathbb E[L]\ \le\ Hn+o(n).

这里 o(n)o(n) 指随 nn 增长远慢于 nn 的量,例如 O(log⁡n)O(\log n) 或 O(n)O(\sqrt n)。两个方向合起来:每符号的渐近最优码长就是 HH,误差只占 o(n)/n→0o(n)/n\to0。

注意这是渐近结论。nn 很小时 o(n)o(n) 项可能比 HnHn 还大,定理不提供有用信息;它的力量在于 n→∞n\to\infty。

直觉:均匀分布最难压缩

哪个分布最难压缩?从熵的角度,是使 HH 最大的分布;从直观角度,是没有高频符号可占便宜的分布。两者给出同一个答案:均匀分布。

均匀分布时 pi=1/kp_i=1/k,故 H=log⁡2kH=\log_2 k。而朴素编码给每个符号 ⌈log⁡2k⌉\lceil\log_2 k\rceil 位,所以每符号码长已经是 log⁡2k\log_2 k 附近,无法再改进。

反过来,只要分布不均匀,就有「把高频事件压短」的空间。例子中 49/6449/64 的概率让我们把 a1a1a_1a_1 压到 1 位,这就是收益来源。

下界的证明

第一步:均匀分布的引理

引理。 设 mm 个等概率消息,任何单射编码的期望码长至少是 log⁡2m−O(1)\log_2 m-O(1)。

证明思路。 把所有码字看成二叉树的节点:根是空串,左孩子添 0,右孩子添 1。单射就是给 mm 个消息分配 mm 个互不相同的节点。要最小化平均深度,应当按层填满:第 0 层 1 个节点、第 1 层 2 个、第 2 层 4 个……直到装满 mm 个。数一下就知道,绝大多数消息的深度是 ⌊log⁡2m⌋\lfloor\log_2 m\rfloor 或 ⌈log⁡2m⌉\lceil\log_2 m\rceil,平均值与 log⁡2m\log_2 m 只差一个常数。

也可以在编码层面理解:深度为 dd 的节点最多 2d2^d 个,若所有码字深度都小于 log⁡2m−c\log_2 m-c,能容纳的码字总数不足 mm,矛盾。

第二步:固定每种符号的出现次数

设一条消息中符号 aia_i 出现 nin_i 次,∑ini=n\sum_i n_i=n。把这组 (n1,…,nk)(n_1,\dots,n_k) 称为消息的签名(signature)。

给定签名后,可能的字符串数目是多项式系数

M=(nn1,…,nk)=n!n1! n2!⋯nk!.M=\binom{n}{n_1,\dots,n_k}=\frac{n!}{n_1!\,n_2!\cdots n_k!}.

每个这样的字符串概率相同,都是 p1n1⋯pknkp_1^{n_1}\cdots p_k^{n_k}。换句话说,在固定签名的条件下,消息在 MM 个等概率字符串上均匀分布。于是引理给出

E[L∣签名为 (n1,…,nk)] ≥ log⁡2M−O(1).\mathbb E[L\mid \text{签名为 }(n_1,\dots,n_k)]\ \ge\ \log_2 M-O(1).

第三步:用 Stirling 公式把 log⁡M\log M 变成熵

对 log⁡M\log M 展开:

log⁡M=log⁡n!−∑i=1klog⁡ni!.\log M=\log n!-\sum_{i=1}^{k}\log n_i! .

用 Stirling 公式 log⁡n!=nlog⁡n−nlog⁡e+O(log⁡n)\log n!=n\log n-n\log e+O(\log n),得到

log⁡M=(nlog⁡n−nlog⁡e+O(log⁡n))−∑i=1k(nilog⁡ni−nilog⁡e+O(log⁡n))=nlog⁡n−∑i=1knilog⁡ni+O(log⁡n),\begin{aligned} \log M &=\Big(n\log n-n\log e+O(\log n)\Big) -\sum_{i=1}^{k}\Big(n_i\log n_i-n_i\log e+O(\log n)\Big)\\ &=n\log n-\sum_{i=1}^{k}n_i\log n_i+O(\log n), \end{aligned}

最后一步用了 ∑ini=n\sum_i n_i=n,所以两个 nlog⁡en\log e 项正好抵消。再把 n=∑inin=\sum_i n_i 拆开:

log⁡M=∑i=1knilog⁡nni+O(log⁡n)=n(−∑i=1kninlog⁡nin)+O(log⁡n).\log M=\sum_{i=1}^{k}n_i\log\frac{n}{n_i}+O(\log n) =n\left(-\sum_{i=1}^{k}\frac{n_i}{n}\log\frac{n_i}{n}\right)+O(\log n).

括号里正是经验分布 (n1/n,…,nk/n)\big(n_1/n,\dots,n_k/n\big) 的熵:

log⁡M=n H ⁣(n1n,…,nkn)+O(log⁡n).\log M=n\,H\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n).

这就是熵出现的位置:它不是被假设出来的,而是多项式系数在 Stirling 近似下的自然产物。

第四步:典型序列

第三步只处理了「签名已知」的情形。问题是:某个签名可能极少出现,那样得到的下界就用不上。

解决方法是典型性。称字符串是 ε\varepsilon-典型的,若对所有 ii

∣nin−pi∣≤ε.\left|\frac{n_i}{n}-p_i\right|\le\varepsilon .

也就是说,经验频率落在真实概率的 ε\varepsilon 邻域内。

由 Chebyshev 不等式(对每个坐标算方差,再用并集界合并 kk 个事件),

Pr⁡(非典型) ≤ kε2n.\Pr(\text{非典型})\ \le\ \frac{k}{\varepsilon^2 n}.

这里 kk 与 ε\varepsilon 在定理中都是固定的,只有 n→∞n\to\infty,所以非典型概率 →0\to0。这正是前面几讲尾界工具的用处:经验频率以 1/n1/n 的速度收敛到真实概率。

第五步:拼出下界

用全期望公式把期望拆成典型与非典型两部分:

E[L]=Pr⁡(典型) E[L∣典型]+Pr⁡(非典型) E[L∣非典型] ≥ Pr⁡(典型) E[L∣典型].\mathbb E[L] =\Pr(\text{典型})\,\mathbb E[L\mid\text{典型}] +\Pr(\text{非典型})\,\mathbb E[L\mid\text{非典型}] \ \ge\ \Pr(\text{典型})\,\mathbb E[L\mid\text{典型}].

典型时,每个 ni/nn_i/n 与 pip_i 只差 ε\varepsilon。熵在概率向量上是连续的,所以

log⁡M=nH ⁣(n1n,…,nkn)+O(log⁡n)=nH(p)±O(εn)+O(log⁡n).\log M=nH\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n) =nH(p)\pm O(\varepsilon n)+O(\log n).

因此

E[L] ≥ (1−kε2n)(nH(p)−O(εn)−O(log⁡n)).\mathbb E[L]\ \ge\ \left(1-\frac{k}{\varepsilon^2 n}\right)\Big(nH(p)-O(\varepsilon n)-O(\log n)\Big).

先令 n→∞n\to\infty(消掉 k/(ε2n)k/(\varepsilon^2n)),再令 ε→0\varepsilon\to0,得到

E[L] ≥ nH(p)−o(n).\mathbb E[L]\ \ge\ nH(p)-o(n).

下界证完。

上界的证明:显式构造

下界说明谁都不能更快;上界要造出一个真正达到它的编码。构造按分支进行,只需要三块信息。

第 1 位:消息是否 ε\varepsilon-典型。

若典型:

  • 用 klog⁡2nk\log_2 n 位写出签名 (n1,…,nk)(n_1,\dots,n_k)——每个 nin_i 有至多 nn 种取值,需要 log⁡2n\log_2 n 位,共 kk 个;
  • 再用 log⁡2M′\log_2 M' 位写出「在给定签名下是第几个字符串」,其中 M′M' 是签名落在 ε\varepsilon 邻域内时多项式系数的最大值。

若非典型:直接输出朴素编码,每符号 log⁡2k\log_2 k 位,共 nlog⁡2kn\log_2 k 位。

按分支计算期望:

E[L] ≤ 1+klog⁡2n+log⁡2M′⏟典型分支+kε2n⋅nlog⁡2k⏟非典型分支的贡献.\mathbb E[L]\ \le\ \underbrace{1+k\log_2 n+\log_2 M'}_{\text{典型分支}} +\underbrace{\frac{k}{\varepsilon^2 n}\cdot n\log_2 k}_{\text{非典型分支的贡献}}.

逐项估计:

  • log⁡2M′=nH(p)+O(εn)+O(log⁡n)\log_2 M'=nH(p)+O(\varepsilon n)+O(\log n),由第三步与熵的连续性;
  • 1+klog⁡2n=o(n)1+k\log_2 n=o(n);
  • 非典型项等于 klog⁡2kε2\dfrac{k\log_2 k}{\varepsilon^2},是常数,也是 o(n)o(n)。

于是

E[L] ≤ nH(p)+o(n).\mathbb E[L]\ \le\ nH(p)+o(n).

两个方向合起来,定理证完。

值得强调的是:这个构造不需要知道消息内容,只需要在解码时先看「是否典型」这一位。典型时,问题退化成在 M′M' 个等概率字符串中报出序号,而报序号的最优代价正是 log⁡2M′\log_2 M'——这又回到第一讲的均匀分布引理。

这个编码为什么不实用

定理证明里的编码函数是一本码表:对每条可能的消息预先指定一个二进制串。消息总数是 knk^n,指数级增长;实际使用时要存下整本码表,解码时还要在表里查找。这在理论上最优,在工程上完全不可行。

下一讲的 Huffman 码换一个思路:不预先固定 nn 再一次性编码,而是逐符号设计前缀码,用一棵树同时完成编码与解码,复杂度只与字母表大小有关。

本节结论

  • 一阶信源 = 字母表 + 分布 + 独立同分布抽样。
  • 编码函数把长度 nn 的消息映成变长二进制串,必须单射才能解码。
  • 熵 H(p)=−∑ipilog⁡2piH(p)=-\sum_i p_i\log_2 p_i 是每符号码长的渐近下界。
  • 下界证明的三件工具:均匀分布的二叉树引理、多项式系数 + Stirling、Chebyshev 典型性。
  • 上界用「典型 / 非典型」两支构造:典型时付 klog⁡n+log⁡M′k\log n+\log M',非典型时付朴素编码,后者的概率只有 O(k/ε2n)O(k/\varepsilon^2n)。
  • 结果:E[L]=nH(p)±o(n)\mathbb E[L]=nH(p)\pm o(n),即每符号 H(p)H(p) 比特。
  • 该构造理论最优但码表指数大,工程上要用 Huffman 之类的逐符号方案。

理解检验

1. 本讲的编码函数只要求单射,为什么不需要前缀无关?下讲的 Huffman 码为什么必须要?

参考答案:本讲的模型是一次性块编码:固定 nn,把整条长度 nn 的消息映射成一个二进制串,发完后发送 END,解码器先看到结束标记,再在码表中反查整条串。因为边界由外部标记给出,码字之间是否互为前缀不影响解码。

Huffman 码处理的是逐符号流式编码:消息长度事先未知,接收方必须从左到右即时切分。若某个码字是另一个的前缀,读到一半就无法判断当前码字是否结束,会产生歧义。前缀无关(等价于码字对应二叉树的叶子)保证任何比特流都有唯一解析。

2. 在 k=2k=2、p=(7/8,1/8)p=(7/8,1/8)、n=2n=2 的例子中,验证改进码的期望长度是 87/6487/64,并说明它为什么仍需 nn 很大才能逼近熵。

参考答案:四条消息的概率为 49/64,7/64,7/64,1/6449/64,7/64,7/64,1/64,码长为 1,2,3,31,2,3,3,故

E[L]=49⋅1+7⋅2+7⋅3+1⋅364=8764≈1.359.\mathbb E[L]=\frac{49\cdot1+7\cdot2+7\cdot3+1\cdot3}{64}=\frac{87}{64}\approx1.359.

每符号约 0.6800.680 比特,而熵约 0.5440.544 比特。差距来自 o(n)o(n) 项:编码必须用整数位,且在 n=2n=2 这种小块上,签名、边界等信息本身的开销占比很大。随 nn 增大,这部分摊薄到每条消息,平均值才收敛到 HH。

3. 为什么均匀分布是最难压缩的分布?请同时给出直观理由和熵的理由。

参考答案:直观上,压缩的收益来自「高频事件配短码」。均匀分布下没有高频事件,所有符号等价,任何把某个符号压短的尝试都会让另一个符号变长,期望长度不受益。

熵的角度:H(p)=−∑pilog⁡piH(p)=-\sum p_i\log p_i 在单纯形上的最大值在 pi=1/kp_i=1/k 处取得,值为 log⁡2k\log_2 k。由 Shannon 定理,最优每符号码长趋近 HH,所以熵最大即码长最大。而朴素编码给每符号约 log⁡2k\log_2 k 位,正好达到这个界,无法改进。

4. 下界证明为什么必须先固定签名 (n1,…,nk)(n_1,\dots,n_k)?直接对整个消息空间用均匀分布引理不行吗?

参考答案:整个消息空间上的分布不是均匀的:不同字符串的概率差别极大(例子中 49/6449/64 与 1/641/64 相差 49 倍),均匀分布引理不适用。

固定签名后,所有具有该签名的字符串概率相同,都是 p1n1⋯pknkp_1^{n_1}\cdots p_k^{n_k},于是可以把它们看成 MM 个等概率对象,套用引理得到 log⁡M−O(1)\log M-O(1)。剩下的问题只是「哪个签名会出现」——用典型性说明存在一族高概率的签名,其 ni/n≈pin_i/n\approx p_i,从而 log⁡M≈nH(p)\log M\approx nH(p)。

5. 用 Stirling 公式推导 log⁡M=nH(n1/n,…,nk/n)+O(log⁡n)\log M=nH(n_1/n,\dots,n_k/n)+O(\log n),并指出哪一步用到了 ∑ini=n\sum_i n_i=n。

参考答案:

log⁡M=log⁡n!−∑ilog⁡ni!=nlog⁡n−nlog⁡e−∑i(nilog⁡ni−nilog⁡e)+O(log⁡n).\log M=\log n!-\sum_i\log n_i! =n\log n-n\log e-\sum_i\big(n_i\log n_i-n_i\log e\big)+O(\log n).

两个 ee 项分别是 −nlog⁡e-n\log e 与 +(∑ini)log⁡e+\big(\sum_i n_i\big)\log e。只有在这里用到 ∑ini=n\sum_i n_i=n,它们相消,留下

log⁡M=nlog⁡n−∑inilog⁡ni+O(log⁡n).\log M=n\log n-\sum_i n_i\log n_i+O(\log n).

再把 nn 写成 ∑ini\sum_i n_i:

log⁡M=∑inilog⁡nni+O(log⁡n)=n(−∑ininlog⁡nin)+O(log⁡n)=nH ⁣(n1n,…,nkn)+O(log⁡n).\log M=\sum_i n_i\log\frac{n}{n_i}+O(\log n) =n\left(-\sum_i\frac{n_i}{n}\log\frac{n_i}{n}\right)+O(\log n)=nH\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n).

O(log⁡n)O(\log n) 来自 Stirling 的误差项;2πn\sqrt{2\pi n} 那部分取对数后是 O(log⁡n)O(\log n),被 o(n)o(n) 吸收。

6. 上界构造里,klog⁡2nk\log_2 n 位用来写签名,为什么它可以被忽略?非典型分支的朴素编码要 nlog⁡2kn\log_2 k 位,为什么也可以忽略?

参考答案:前者是 o(n)o(n):kk 固定,log⁡2n\log_2 n 增长远慢于 nn,所以 klog⁡2n=o(n)k\log_2 n=o(n),在 nH±o(n)nH\pm o(n) 的误差范围内。

后者看似线性,但只在非典型时使用,其概率由 Chebyshev 界为 O(k/(ε2n))O(k/(\varepsilon^2n))。乘起来:

kε2n⋅nlog⁡2k=klog⁡2kε2,\frac{k}{\varepsilon^2 n}\cdot n\log_2 k=\frac{k\log_2 k}{\varepsilon^2},

与 nn 无关,是常数,因而也是 o(n)o(n)。关键在于非典型是小概率事件,即使分支内部很浪费,摊到期望上也可忽略。

7. 定理说最优码长是 nHnH,为什么这个编码不能直接用于压缩文件?

参考答案:编码函数是一张码表,需要为 AnA^n 中每条消息预先指定码字。消息总数 knk^n 随 nn 指数增长,存表和查表都不可行,而且发送方必须先知道整条消息才能查表。

工程上需要的是可增量、可即时解码的方案:Huffman 码用一棵与字母表同规模的树,对每个符号独立编码,编解码都是树上的行走,存储与时间只与 kk 有关,与 nn 无关。代价是它只能达到 HH 与 H+1H+1 之间,而不是渐近最优的 H+o(1)H+o(1)——但对实际数据,这个差距通常可以接受。

与前后课的关系

本讲把压缩的理论极限钉死在熵上,但证明里的构造不可实现。下一讲 Huffman 码给出可实现的最优前缀码:用交换论证证明「最不常用的两个符号必须是兄弟」,再递归合并,得到一棵最优二叉树;同时用 Kraft 不等式把前缀码与码长联系起来。

再下一讲转向噪声信道:消息在传输中会被翻转,问题从「压得更短」变成「错得起」。Shannon 第二定理会说明,只要码率低于信道容量,就可以用足够长的码把错误概率压到任意小——证明同样依赖典型性与随机编码。

The question: how short can a message be?

Telegrams used to be billed by the word, about a dollar each. People therefore printed code books that replaced long stock phrases with short tokens: AZKHE meant clean bill of health, NBET meant captain is insane. Texting BRB today is the same move: give the frequent thing a short symbol.

The telegraph era did this by hand, choosing phrases by experience. This lecture asks the mathematical version:

For a given source, what is the lower limit on the average code length of any compression scheme, and can that limit be attained?

Shannon answered in 1948, in the same building: the limit is entropy. The lecture proves both directions in full.

Timeline

TimeTopic
0:00History: telegram code books, texting abbreviations; information theory born in MIT Building 2 in 1948
5:19Definition of a first-order source: alphabet plus distribution, IID sampling
8:14Coding functions and decodability; one-shot block codes and the END marker
11:34Example: k=2k=2, p=(7/8,1/8)p=(7/8,1/8), n=2n=2; naive versus improved code
17:21Definition of binary entropy
18:38The two directions of Shannon's noiseless coding theorem
22:47Intuition: the uniform distribution is hardest to compress
26:41Lemma: under a uniform distribution the expected length is at least log⁡m−O(1)\log m-O(1) (binary tree argument)
30:33Fixing symbol counts; the multinomial count MM
37:48Stirling's formula turns log⁡M\log M into nH+O(log⁡n)nH+O(\log n)
44:11ε\varepsilon-typical strings and the Chebyshev bound
47:46Assembling the lower bound with the law of total expectation
55:12Upper bound: an explicit two-branch code
1:05:51Why this construction is impractical; the road to Huffman codes

First-order sources

A first-order source has two parts:

  • an alphabet A={a1,…,ak}A=\{a_1,\dots,a_k\} of kk symbols;
  • a distribution p=(p1,…,pk)p=(p_1,\dots,p_k) with pi≥0p_i\ge0 and ∑ipi=1\sum_i p_i=1.

The lecture pictures a biased kk-sided die: roll it once to get a symbol, roll nn times keeping order, and obtain a message x1x2⋯xnx_1x_2\cdots x_n of length nn. Draws are independent and identically distributed, so

Pr⁡(x1⋯xn)=∏j=1npxj.\Pr(x_1\cdots x_n)=\prod_{j=1}^{n}p_{x_j}.

Symbols may be letters or whole words. The lecture stresses that this is the simplest model; real language needs Markov chains, but the theory carries over.

Coding functions and decodability

A coding function φ:An→{0,1}∗\varphi:A^n\to\{0,1\}^* maps every length-nn message to a variable-length binary string.

Why variable length? Because frequent messages should get short codewords. In the example below, a1a1a_1a_1 has probability 49/6449/64; giving it one bit instead of two immediately lowers the average.

Variable length forces one requirement: the map must be injective. Two distinct messages encoded identically cannot be decoded. Prefix-freeness is not required here, because this is a one-shot block code: the whole message is sent, then END, so the decoder knows where the message stops. Prefix-freeness becomes necessary in the next lecture, where Huffman codes must be decoded symbol by symbol on a stream.

Writing L(φ,x)=∣φ(x)∣L(\varphi,x)=|\varphi(x)|, the expected length is

E[L]=∑x∈AnPr⁡(x) ∣φ(x)∣,\mathbb E[L]=\sum_{x\in A^n}\Pr(x)\,|\varphi(x)|,

and this is the cost to minimize.

Example: k=2k=2, p=(7/8,1/8)p=(7/8,1/8), n=2n=2

There are four messages of length two:

MessageProbabilityNaive codeImproved code
a1a1a_1a_149/6449/64000
a1a2a_1a_27/647/640110
a2a1a_2a_17/647/6410110
a2a2a_2a_21/641/6411111

The naive code replaces each symbol by one bit, so every message costs two bits and the expected length is exactly 22.

The improved code gives the most likely message one bit, at the price of three bits for the two rarer messages. Its expected length is

E[L]=4964⋅1+764⋅2+764⋅3+164⋅3=49+14+21+364=8764≈1.359.\mathbb E[L]=\frac{49}{64}\cdot1+\frac{7}{64}\cdot2+\frac{7}{64}\cdot3+\frac{1}{64}\cdot3 =\frac{49+14+21+3}{64}=\frac{87}{64}\approx1.359 .

This is below 22. The four codewords are distinct and none is a prefix of another — not an accident, since prefix structure is what makes the construction work in general.

What is the limit? For this source,

H ⁣(78,18)=−78log⁡278−18log⁡218≈0.169+0.375=0.544 bits per symbol.H\!\left(\tfrac78,\tfrac18\right) =-\tfrac78\log_2\tfrac78-\tfrac18\log_2\tfrac18 \approx0.169+0.375=0.544\ \text{bits per symbol}.

That is far below one bit. At n=2n=2 the best we do is 1.359/2≈0.681.359/2\approx0.68; the gap is the o(n)o(n) correction, which shrinks as nn grows.

Entropy

For a distribution p=(p1,…,pk)p=(p_1,\dots,p_k), the binary entropy is

H(p)=−∑i=1kpilog⁡2pi.H(p)=-\sum_{i=1}^{k}p_i\log_2 p_i .

Since 0<pi≤10<p_i\le1, each log⁡pi≤0\log p_i\le0 and the minus sign makes H(p)≥0H(p)\ge0. The convention is 0log⁡0=00\log 0=0.

Three facts used later:

  • the uniform distribution pi=1/kp_i=1/k maximizes entropy, giving H=log⁡2kH=\log_2 k;
  • if some pi=1p_i=1, then H=0H=0 — the source carries no uncertainty;
  • HH depends only on the probabilities, not on the symbol names.

Shannon's noiseless coding theorem

Let the source have entropy HH. The theorem has two directions.

Converse. For every decodable coding function φ\varphi,

E[L] ≥ Hn−o(n).\mathbb E[L]\ \ge\ Hn-o(n).

Achievability. There exists a decodable coding function φ\varphi with

E[L] ≤ Hn+o(n).\mathbb E[L]\ \le\ Hn+o(n).

Here o(n)o(n) means a quantity growing strictly slower than nn, such as O(log⁡n)O(\log n) or O(n)O(\sqrt n). Together: the asymptotic optimum per symbol is HH, with relative error o(n)/n→0o(n)/n\to0.

The statement is asymptotic. For small nn the o(n)o(n) term may exceed HnHn and the theorem says nothing useful; its force is in the limit.

Intuition: the uniform distribution is hardest

Which distribution is hardest to compress? From entropy, the one maximizing HH; from intuition, the one with no frequent symbol to exploit. Both give the same answer: uniform.

For uniform pi=1/kp_i=1/k, H=log⁡2kH=\log_2 k, and the naive code already uses about log⁡2k\log_2 k bits per symbol, so nothing can improve. Conversely, any non-uniformity creates room: a frequent event can be given a short codeword.

The lower bound

Step 1: a lemma for uniform distributions

Lemma. For mm equally likely messages, every injective code has expected length at least log⁡2m−O(1)\log_2 m-O(1).

Proof idea. View codewords as nodes of a binary tree: the root is the empty string, appending 0 or 1 goes left or right. Injectivity means assigning mm distinct nodes. To minimize average depth, fill the tree level by level: 1 node at depth 0, 2 at depth 1, 4 at depth 2, and so on. Most of the mm messages end up at depth ⌊log⁡2m⌋\lfloor\log_2 m\rfloor or ⌈log⁡2m⌉\lceil\log_2 m\rceil, so the average differs from log⁡2m\log_2 m by a constant.

Equivalently, there are at most 2d2^d nodes at depth dd; if every codeword had depth below log⁡2m−c\log_2 m-c, the total capacity would be less than mm, a contradiction.

Step 2: fix the symbol counts

Let aia_i occur nin_i times, with ∑ini=n\sum_i n_i=n. The tuple (n1,…,nk)(n_1,\dots,n_k) is the signature of the message.

Given a signature, the number of strings is the multinomial

M=(nn1,…,nk)=n!n1! n2!⋯nk!.M=\binom{n}{n_1,\dots,n_k}=\frac{n!}{n_1!\,n_2!\cdots n_k!}.

Every such string has the same probability p1n1⋯pknkp_1^{n_1}\cdots p_k^{n_k}. So conditioned on the signature, the message is uniform over MM strings, and the lemma yields

E[L∣signature (n1,…,nk)] ≥ log⁡2M−O(1).\mathbb E[L\mid \text{signature }(n_1,\dots,n_k)]\ \ge\ \log_2 M-O(1).

Step 3: Stirling turns log⁡M\log M into entropy

Expand

log⁡M=log⁡n!−∑i=1klog⁡ni!.\log M=\log n!-\sum_{i=1}^{k}\log n_i! .

Using log⁡n!=nlog⁡n−nlog⁡e+O(log⁡n)\log n!=n\log n-n\log e+O(\log n),

log⁡M=(nlog⁡n−nlog⁡e+O(log⁡n))−∑i=1k(nilog⁡ni−nilog⁡e+O(log⁡n))=nlog⁡n−∑i=1knilog⁡ni+O(log⁡n),\begin{aligned} \log M &=\Big(n\log n-n\log e+O(\log n)\Big) -\sum_{i=1}^{k}\Big(n_i\log n_i-n_i\log e+O(\log n)\Big)\\ &=n\log n-\sum_{i=1}^{k}n_i\log n_i+O(\log n), \end{aligned}

where the two nlog⁡en\log e terms cancel because ∑ini=n\sum_i n_i=n. Now split n=∑inin=\sum_i n_i:

log⁡M=∑i=1knilog⁡nni+O(log⁡n)=n(−∑i=1kninlog⁡nin)+O(log⁡n).\log M=\sum_{i=1}^{k}n_i\log\frac{n}{n_i}+O(\log n) =n\left(-\sum_{i=1}^{k}\frac{n_i}{n}\log\frac{n_i}{n}\right)+O(\log n).

The bracket is the entropy of the empirical distribution:

log⁡M=n H ⁣(n1n,…,nkn)+O(log⁡n).\log M=n\,H\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n).

Entropy appears here not as an assumption but as the natural output of Stirling's approximation to a multinomial.

Step 4: typical strings

Step 3 handles a fixed signature, but some signatures may be so unlikely that the bound is useless. Typicality fixes this. A string is ε\varepsilon-typical if for every ii

∣nin−pi∣≤ε,\left|\frac{n_i}{n}-p_i\right|\le\varepsilon ,

that is, every empirical frequency lies within ε\varepsilon of its true probability.

By Chebyshev's inequality applied to each coordinate and a union bound over kk coordinates,

Pr⁡(not typical) ≤ kε2n.\Pr(\text{not typical})\ \le\ \frac{k}{\varepsilon^2 n}.

Both kk and ε\varepsilon are fixed while n→∞n\to\infty, so this probability tends to zero. This is precisely the tail-bound machinery from earlier lectures: empirical frequencies converge at rate 1/n1/n.

Step 5: assemble the bound

Split the expectation with the law of total expectation:

E[L]=Pr⁡(typical) E[L∣typical]+Pr⁡(not typical) E[L∣not typical] ≥ Pr⁡(typical) E[L∣typical].\mathbb E[L] =\Pr(\text{typical})\,\mathbb E[L\mid\text{typical}] +\Pr(\text{not typical})\,\mathbb E[L\mid\text{not typical}] \ \ge\ \Pr(\text{typical})\,\mathbb E[L\mid\text{typical}].

On a typical string, each ni/nn_i/n is within ε\varepsilon of pip_i. Entropy is continuous in the probability vector, so

log⁡M=nH ⁣(n1n,…,nkn)+O(log⁡n)=nH(p)±O(εn)+O(log⁡n).\log M=nH\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n) =nH(p)\pm O(\varepsilon n)+O(\log n).

Therefore

E[L] ≥ (1−kε2n)(nH(p)−O(εn)−O(log⁡n)).\mathbb E[L]\ \ge\ \left(1-\frac{k}{\varepsilon^2 n}\right)\Big(nH(p)-O(\varepsilon n)-O(\log n)\Big).

Let n→∞n\to\infty first (removing k/(ε2n)k/(\varepsilon^2n)), then ε→0\varepsilon\to0:

E[L] ≥ nH(p)−o(n).\mathbb E[L]\ \ge\ nH(p)-o(n).

The converse is proved.

The upper bound: an explicit code

The converse says nobody can do better; achievability must exhibit a code that does as well. The construction uses three pieces of information.

Bit 1: is the message ε\varepsilon-typical?

If typical:

  • write the signature (n1,…,nk)(n_1,\dots,n_k) using klog⁡2nk\log_2 n bits — each nin_i has at most nn values, so log⁡2n\log_2 n bits each;
  • write the index of the string among those with this signature, using log⁡2M′\log_2 M' bits, where M′M' is the largest multinomial over ε\varepsilon-typical signatures.

If not typical: fall back to the naive code at log⁡2k\log_2 k bits per symbol, nlog⁡2kn\log_2 k bits total.

Computing the expectation branch by branch:

E[L] ≤ 1+klog⁡2n+log⁡2M′⏟typical branch+kε2n⋅nlog⁡2k⏟contribution of the other branch.\mathbb E[L]\ \le\ \underbrace{1+k\log_2 n+\log_2 M'}_{\text{typical branch}} +\underbrace{\frac{k}{\varepsilon^2 n}\cdot n\log_2 k}_{\text{contribution of the other branch}}.

Estimate each term:

  • log⁡2M′=nH(p)+O(εn)+O(log⁡n)\log_2 M'=nH(p)+O(\varepsilon n)+O(\log n), by Step 3 and continuity of entropy;
  • 1+klog⁡2n=o(n)1+k\log_2 n=o(n);
  • the atypical term equals klog⁡2kε2\dfrac{k\log_2 k}{\varepsilon^2}, a constant, hence o(n)o(n).

Thus

E[L] ≤ nH(p)+o(n),\mathbb E[L]\ \le\ nH(p)+o(n),

which completes the theorem.

Note what the decoder needs: one bit saying whether the message is typical. If it is, the problem reduces to reporting the index of one of M′M' equally likely strings, whose optimal cost is log⁡2M′\log_2 M' — exactly the uniform-distribution lemma from Step 1.

Why this code is impractical

The code in the proof is a lookup table: every possible message is assigned a binary string in advance. There are knk^n messages, exponentially many, so storing the table and searching it are both infeasible.

The next lecture changes the approach. Instead of fixing nn and encoding the whole block at once, Huffman codes design a symbol-by-symbol prefix code represented by a tree, so encoding and decoding cost depend only on the alphabet size, not on nn.

What this lecture establishes

  • A first-order source is an alphabet, a distribution, and IID sampling.
  • A coding function maps length-nn messages to variable-length binary strings and must be injective to be decodable.
  • Entropy H(p)=−∑ipilog⁡2piH(p)=-\sum_i p_i\log_2 p_i is the asymptotic per-symbol lower bound.
  • The lower bound uses three tools: the uniform-distribution tree lemma, the multinomial plus Stirling, and Chebyshev typicality.
  • The upper bound branches on typicality: klog⁡n+log⁡M′k\log n+\log M' when typical, the naive code otherwise, and the latter has probability only O(k/ε2n)O(k/\varepsilon^2n).
  • The result is E[L]=nH(p)±o(n)\mathbb E[L]=nH(p)\pm o(n), i.e. H(p)H(p) bits per symbol.
  • The code is optimal but exponentially large; practical schemes use Huffman-type constructions.

Comprehension check

1. This lecture only requires injectivity, not prefix-freeness. Why do Huffman codes require prefix-freeness?

Reference answer: This lecture studies one-shot block coding: nn is fixed, the entire length-nn message is mapped to one binary string, then END is sent, and the decoder looks the whole string up in the codebook. Boundaries come from an external marker, so prefix relations among codewords do not create ambiguity.

Huffman codes handle streaming, symbol-by-symbol encoding: the message length is unknown in advance and the receiver must split the bit stream on the fly. If one codeword were a prefix of another, the decoder could not tell where the current codeword ends. Prefix-freeness (equivalently, codewords at leaves of a binary tree) guarantees a unique parsing of every bit stream.

2. In the example k=2k=2, p=(7/8,1/8)p=(7/8,1/8), n=2n=2, verify that the improved code has expected length 87/6487/64, and explain why large nn is still needed to approach entropy.

Reference answer: The probabilities are 49/64,7/64,7/64,1/6449/64,7/64,7/64,1/64 with lengths 1,2,3,31,2,3,3, so

E[L]=49⋅1+7⋅2+7⋅3+1⋅364=8764≈1.359.\mathbb E[L]=\frac{49\cdot1+7\cdot2+7\cdot3+1\cdot3}{64}=\frac{87}{64}\approx1.359.

That is about 0.6800.680 bits per symbol against an entropy of about 0.5440.544. The gap is the o(n)o(n) term: codewords have integer lengths, and for a block as small as n=2n=2 the overhead of the signature and boundary information is large relative to the payload. As nn grows this overhead is amortized, and the average approaches HH.

3. Why is the uniform distribution hardest to compress? Give both the intuitive and the entropy argument.

Reference answer: Intuitively, compression gains come from giving frequent events short codewords. Under a uniform distribution no symbol is frequent, every symbol is interchangeable, and any shortening forces another codeword to lengthen, so the expected length cannot improve.

Formally, H(p)=−∑ipilog⁡piH(p)=-\sum_i p_i\log p_i is maximized on the simplex at pi=1/kp_i=1/k, with value log⁡2k\log_2 k. Shannon's theorem says the optimal per-symbol length approaches HH, so maximal entropy means maximal length. The naive code already uses about log⁡2k\log_2 k bits per symbol and therefore attains this bound.

4. Why must the lower-bound proof first fix the signature (n1,…,nk)(n_1,\dots,n_k)? Why not apply the uniform lemma to the whole message space?

Reference answer: The distribution over messages is not uniform — probabilities differ enormously (in the example, 49/6449/64 versus 1/641/64). The uniform lemma does not apply.

Conditioning on a signature makes all strings with that signature equally likely, each with probability p1n1⋯pknkp_1^{n_1}\cdots p_k^{n_k}, so they become MM equiprobable objects and the lemma gives log⁡M−O(1)\log M-O(1). What remains is to argue that some signature family occurs with high probability — that is typicality, which guarantees ni/n≈pin_i/n\approx p_i and hence log⁡M≈nH(p)\log M\approx nH(p).

5. Derive log⁡M=nH(n1/n,…,nk/n)+O(log⁡n)\log M=nH(n_1/n,\dots,n_k/n)+O(\log n) with Stirling's formula, and identify where ∑ini=n\sum_i n_i=n is used.

Reference answer:

log⁡M=log⁡n!−∑ilog⁡ni!=nlog⁡n−nlog⁡e−∑i(nilog⁡ni−nilog⁡e)+O(log⁡n).\log M=\log n!-\sum_i\log n_i! =n\log n-n\log e-\sum_i\big(n_i\log n_i-n_i\log e\big)+O(\log n).

The exponential terms are −nlog⁡e-n\log e and +(∑ini)log⁡e+\big(\sum_i n_i\big)\log e. This is the only place where ∑ini=n\sum_i n_i=n is used; they cancel, leaving

log⁡M=nlog⁡n−∑inilog⁡ni+O(log⁡n).\log M=n\log n-\sum_i n_i\log n_i+O(\log n).

Writing n=∑inin=\sum_i n_i,

log⁡M=∑inilog⁡nni+O(log⁡n)=n(−∑ininlog⁡nin)+O(log⁡n)=nH ⁣(n1n,…,nkn)+O(log⁡n).\log M=\sum_i n_i\log\frac{n}{n_i}+O(\log n) =n\left(-\sum_i\frac{n_i}{n}\log\frac{n_i}{n}\right)+O(\log n)=nH\!\left(\frac{n_1}{n},\dots,\frac{n_k}{n}\right)+O(\log n).

The O(log⁡n)O(\log n) comes from the error term in Stirling; the 2πn\sqrt{2\pi n} factor contributes only O(log⁡n)O(\log n) after taking logs, which is absorbed by o(n)o(n).

6. In the upper bound, why can the klog⁡2nk\log_2 n bits for the signature be ignored? The atypical branch costs nlog⁡2kn\log_2 k bits, so why can that be ignored too?

Reference answer: The first is o(n)o(n): kk is fixed and log⁡2n\log_2 n grows far slower than nn, so klog⁡2n=o(n)k\log_2 n=o(n) and it fits inside the nH±o(n)nH\pm o(n) slack.

The second looks linear, but it is used only on atypical strings, an event of probability O(k/(ε2n))O(k/(\varepsilon^2n)) by Chebyshev. Multiplying,

kε2n⋅nlog⁡2k=klog⁡2kε2,\frac{k}{\varepsilon^2 n}\cdot n\log_2 k=\frac{k\log_2 k}{\varepsilon^2},

which is independent of nn, hence a constant and also o(n)o(n). The key is that atypicality is rare: however wasteful the fallback, its contribution to the expectation vanishes.

7. The theorem says the optimal length is nHnH. Why can this code not be used to compress a file?

Reference answer: The coding function is a codebook assigning a codeword to every message in AnA^n. There are knk^n messages, exponentially many, so storing and searching the table is infeasible, and the sender must know the whole message before encoding.

Practical schemes must be incremental and decodable on the fly. Huffman codes use one tree of size comparable to the alphabet, encoding each symbol independently and decoding by walking the tree, with storage and time depending only on kk. The price is that Huffman achieves between HH and H+1H+1 bits per symbol rather than the asymptotic H+o(1)H+o(1) — usually an acceptable trade.

Relation to neighbouring lectures

This lecture pins the theoretical limit of compression at entropy, but its construction is unimplementable. The next lecture gives Huffman coding, an implementable optimal prefix code: an exchange argument shows the two least likely symbols must be siblings, which yields a recursive merge, and the Kraft inequality links prefix codes to their length profile.

The lecture after that turns to noisy channels, where the question changes from "make it shorter" to "make it survive errors". Shannon's second theorem shows that below channel capacity one can drive the error probability arbitrarily low with long enough codes, again via typicality and random coding.

来源