Shannon Entropy Bounds for Optimal Binary Huffman Codes #
This module connects the optimal binary prefix-free code tree constructed by
Cslib.Algorithms.Lean.Huffman.huffman to the library's base-2 Shannon entropy
Cslib.Probability.PMF.entropy ([Sha48]; [Huf52]; [CT06], Theorems 5.3.1,
5.4.1, and 5.8.1, specialized to binary alphabet Bool, $D = 2$).
Using the exact binary Kraft identity for full binary code trees, the Kraft–Gibbs source coding
lower bound (Cslib.Probability.Coding.entropy_le_expected_length_of_isPrefixFree), and comparing
against Shannon codeword lengths ⌈Cslib.Probability.PMF.surprisal p a⌉₊ via the binary Kraft
converse and Huffman optimality, it shows that for any finite full-support distribution p : PMF α,
the canonical codewords assigned by T.encodeSym satisfy the exact Kraft identity and have
expected codeword length T.weightedPathLength (fun a => (p a).toReal) in
[PMF.entropy p, PMF.entropy p + 1).
Main results #
Cslib.Algorithms.Lean.Huffman.codeTree_leafDepths_kraft_eq_one: every full binaryCodeTreesatisfies the exact binary Kraft identity∑_{(a, d)} 2^{-d} = 1([Kra49]; [CT06], Section 5.2), including the one-leaf treeCodeTree.leaf awith depth0(empty codeword[]).Cslib.Algorithms.Lean.Huffman.huffman_expected_length_bounds: for any finite full-support distributionp : PMF α,huffmanon(Finset.univ : Finset α).toListreturns a treeTwhose encoderT.encodeSymassigns binary codewordscode : α → List Boolsatisfying the exact binary Kraft identity andPMF.entropy p ≤ T.weightedPathLength (fun a => (p a).toReal) < PMF.entropy p + 1([Sha48]; [Huf52]; [CT06], Theorems 5.3.1, 5.4.1, and 5.8.1).
References #
- [Sha48] Claude E. Shannon, A Mathematical Theory of Communication, 1948.
- [Kra49] Leon G. Kraft, A Device for Quantizing, Grouping, and Coding Amplitude-Modulated Pulses, M.S. thesis, MIT, 1949.
- [Huf52] David A. Huffman, A Method for the Construction of Minimum-Redundancy Codes, 1952.
- [CT06] Thomas M. Cover and Joy A. Thomas, Elements of Information Theory (2nd ed.), Section 5.2 and Theorems 5.3.1, 5.4.1, and 5.8.1, 2006.
Every full binary CodeTree α satisfies the exact binary Kraft identity
∑_{(a, d) ∈ T.leafDepths} 2^{-d} = 1 ([Kra49]; [CT06], Section 5.2).
This includes the one-leaf tree CodeTree.leaf a, whose unique leaf has depth 0 (empty codeword
[]) and contributes 2^0 = 1.
Shannon Entropy Bounds for Optimal Binary Huffman Codes ([Sha48]; [Huf52];
[CT06], Theorems 5.3.1, 5.4.1, and 5.8.1, specialized to binary alphabet Bool,
$D = 2$): for any finite full-support probability distribution p : PMF α (∀ a, a ∈ p.support),
Cslib.Algorithms.Lean.Huffman.huffman on (Finset.univ : Finset α).toList with real masses
fun a => (p a).toReal returns a full binary code tree T whose canonical encoder T.encodeSym
assigns every symbol a a binary codeword code a such that:
- the codeword lengths satisfy the exact binary Kraft identity
∑ a, 2^{-|code a|} = 1; T.weightedPathLength (fun a => (p a).toReal)equals∑ a, (p a).toReal * |code a|;PMF.entropy p ≤ T.weightedPathLength (fun a => (p a).toReal) < PMF.entropy p + 1.
On a one-symbol alphabet (Fintype.card α = 1), T is a single leaf assigning the empty codeword
[] (length 0, prefix-free in Mathlib, satisfying 0 = H(p) ≤ 0 < H(p) + 1).