Documentation

CslibFrontier.Algorithms.Lean.Huffman.Entropy

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 #

References #

theorem Cslib.Algorithms.Lean.Huffman.codeTree_leafDepths_kraft_eq_one {α : Type u_1} (T : CodeTree α) :
(List.map (fun (p : α × ℕ) => 2 ^ (-↑p.2)) T.leafDepths).sum = 1

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.

theorem Cslib.Algorithms.Lean.Huffman.huffman_expected_length_bounds {α : Type} [Fintype α] [DecidableEq α] (p : PMF α) (hsupp : ∀ (a : α), a ∈ p.support) :
∃ (T : CodeTree α) (code : α → List Bool), huffman (fun (a : α) => (p a).toReal) Finset.univ.toList = some T ∧ (∀ (a : α), T.encodeSym a = some (code a)) ∧ ∑ a : α, 2 ^ (-↑(code a).length) = 1 ∧ CodeTree.weightedPathLength (fun (a : α) => (p a).toReal) T = ∑ a : α, (p a).toReal * ↑(code a).length ∧ Probability.PMF.entropy p ≤ CodeTree.weightedPathLength (fun (a : α) => (p a).toReal) T ∧ CodeTree.weightedPathLength (fun (a : α) => (p a).toReal) T < Probability.PMF.entropy p + 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:

  1. the codeword lengths satisfy the exact binary Kraft identity ∑ a, 2^{-|code a|} = 1;
  2. T.weightedPathLength (fun a => (p a).toReal) equals ∑ a, (p a).toReal * |code a|;
  3. 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).