Documentation

CslibFrontier.Probability.DDGTree.KnuthYao

The Knuth–Yao entropy bounds for sampling from fair coins #

Every DDG tree generating a distribution p on a finite type reads at least H(p) fair bits in expectation, and the Knuth–Yao tree knuthYao p reads fewer than H(p) + 2 ([KY76]; [CT06], Theorems 5.11.1 and 5.11.3).

knuthYao p places a leaf labelled a at depth k exactly when the k-th binary digit ⌊2ᵏ p a⌋ mod 2 of p a is 1. Equivalently, it outputs a within k flips with probability ⌊2ᵏ p a⌋ / 2ᵏ. Its stopping time is stochastically smallest. For every generating tree T and every n, P(T_KY > n) ≤ P(T > n), because after n fair bits T can only have produced a with dyadic probability c / 2ⁿ ≤ p a, so c ≤ ⌊2ⁿ p a⌋. Hence knuthYao p minimizes the expected number of flips. Its expected cost is ∑ₐ ν(p a), where ν(x) = ∑ₙ (x - ⌊2ⁿ x⌋ / 2ⁿ) is the sum of the binary-truncation errors of x. The bounds x log₂ (1 / x) ≤ ν(x) < x log₂ (1 / x) + 2x for 0 < x ≤ 1 then give both entropy bounds. Equality ν(x) = x log₂ (1 / x) holds exactly when x is 0 or a power of 1/2, which characterizes the dyadic case.

The entropy bounds are stated for distributions on a Fintype. The countable-support and infinite-entropy cases of [CT06], Section 5.11 are not formalized.

Main definitions #

Main results #

References #

Binary-truncation errors #

noncomputable def Cslib.Probability.DDGTree.truncErr (x : ℝ) (n : ℕ) :

Error of the n-digit binary truncation of x: x - ⌊2ⁿ x⌋ / 2ⁿ.

The Knuth–Yao cost ∑ₙ (x - ⌊2ⁿ x⌋ / 2ⁿ) of a probability x. It is the contribution of an outcome of probability x to the expected number of flips of the Knuth–Yao tree.

theorem Cslib.Probability.DDGTree.truncErr_eq {x : ℝ} (n : ℕ) :
truncErr x n = (2 ^ n * x - ↑⌊2 ^ n * x⌋₊) * 2⁻¹ ^ n
theorem Cslib.Probability.DDGTree.truncErr_eq_self {x : ℝ} {n : ℕ} (h : 2 ^ n * x < 1) :
truncErr x n = x

Lower bound: an outcome of probability x costs at least its surprisal contribution.

Upper bound: an outcome of probability x costs less than its surprisal contribution plus 2 x.

theorem Cslib.Probability.DDGTree.knuthYaoCost_eq_negMulLogb_iff {x : ℝ} (hx0 : 0 ≤ x) (hx1 : x ≤ 1) :
knuthYaoCost x = x * -Real.logb 2 x ↔ x = 0 ∨ ∃ (m : ℕ), x = 2⁻¹ ^ m

The lower bound is tight exactly at 0 and at powers of 1/2.

The Knuth–Yao tree #

noncomputable def Cslib.Probability.DDGTree.knuthYaoCounts {α : Type u_1} (p : PMF α) (k : ℕ) (a : α) :

The number ⌊2ᵏ p a⌋ of depth-k positions of the Knuth–Yao tree lying at or below a leaf labelled a (a leaf at depth j ≤ k counts 2 ^ (k - j) times). Equivalently, ⌊2ᵏ p a⌋ / 2ᵏ is the probability that the tree outputs a within k flips.

noncomputable def Cslib.Probability.DDGTree.knuthYao {α : Type u_1} [Fintype α] (p : PMF α) :

The Knuth–Yao tree of p: a leaf labelled a at depth k for each binary digit ⌊2ᵏ p a⌋ mod 2 = 1 of p a (position 0 is the units digit, so p a = 1 gives a root leaf).

theorem Cslib.Probability.DDGTree.run_knuthYao_some {α : Type u_1} [Fintype α] (p : PMF α) (n : ℕ) (a : α) :
((knuthYao p).run n) (some a) = ↑⌊2 ^ n * (p a).toReal⌋₊ / 2 ^ n

Within n flips, the Knuth–Yao tree outputs a with probability ⌊2ⁿ p a⌋ / 2ⁿ, the n-digit binary truncation of p a.

theorem Cslib.Probability.DDGTree.run_knuthYao_none {α : Type u_1} [Fintype α] (p : PMF α) (n : ℕ) :
((knuthYao p).run n) none = ENNReal.ofReal (∑ a : α, truncErr (p a).toReal n)

The Knuth–Yao tree needs more than n flips with probability ∑ₐ (p a - ⌊2ⁿ p a⌋ / 2ⁿ).

The expected number of flips of the Knuth–Yao tree is ∑ₐ ν(p a).

theorem Cslib.Probability.DDGTree.run_knuthYao_none_le {α : Type u_1} [Fintype α] {T : DDGTree α} {p : PMF α} (hT : T.Generates p) (n : ℕ) :
((knuthYao p).run n) none ≤ (T.run n) none

Stochastic dominance: for every tree generating p and every n, the Knuth–Yao tree is at least as likely to have stopped within n flips.

Knuth–Yao optimality: no tree generating p uses fewer expected flips.

Entropy lower bound ([CT06], Theorem 5.11.1): every tree generating p reads at least H(p) fair bits in expectation.

Knuth–Yao upper bound ([CT06], Theorem 5.11.3): the Knuth–Yao tree reads fewer than H(p) + 2 fair bits in expectation.

The Knuth–Yao tree attains the entropy exactly for dyadic distributions.

Dyadic case ([CT06], Theorem 5.11.2 and its converse): some tree generating p reads exactly H(p) fair bits in expectation if and only if every positive probability p a is a power of 1/2.