Download data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/Entropy.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 12.3 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/Entropy.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/Entropy.lean
-
curl -L -o Entropy.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/LowSpaceSubsetSum/Counting/Entropy.lean
12.3 kB
| import Mathlib.Analysis.SpecialFunctions.BinaryEntropy | |
| import Mathlib.Analysis.SpecialFunctions.Log.Base | |
| import Mathlib.Analysis.SpecialFunctions.Pow.Real | |
| import Mathlib.Tactic | |
| import OAI.Probability.TraceReconstruction.Binomial | |
| namespace OAI | |
| /-! | |
| # Binary entropy estimates used by the subset-sum algorithm | |
| The paper measures entropy in bits, whereas Mathlib's `Real.binEntropy` | |
| uses natural logarithms. Normalization gives the elementary estimates in | |
| the paper's preliminary section. | |
| -/ | |
| namespace LowSpaceSubsetSum | |
| /-- Binary entropy measured in bits. -/ | |
| noncomputable def binaryEntropy (p : β) : β := Real.binEntropy p / Real.log 2 | |
| private theorem log_two_pos : 0 < Real.log 2 := Real.log_pos (by norm_num) | |
| theorem binaryEntropy_eq_logb (p : β) : | |
| binaryEntropy p = p * Real.logb 2 pβ»ΒΉ + (1 - p) * Real.logb 2 (1 - p)β»ΒΉ := by | |
| unfold binaryEntropy Real.binEntropy Real.logb | |
| ring | |
| @[simp] theorem binaryEntropy_zero : binaryEntropy 0 = 0 := by | |
| simp [binaryEntropy] | |
| @[simp] theorem binaryEntropy_one : binaryEntropy 1 = 0 := by | |
| simp [binaryEntropy] | |
| @[simp] theorem binaryEntropy_half : binaryEntropy (1 / 2) = 1 := by | |
| simpa only [one_div] using | |
| (show binaryEntropy (2β»ΒΉ) = 1 by | |
| simp [binaryEntropy, ne_of_gt log_two_pos]) | |
| @[simp] theorem binaryEntropy_one_sub (p : β) : | |
| binaryEntropy (1 - p) = binaryEntropy p := by | |
| simp [binaryEntropy] | |
| theorem binaryEntropy_nonneg {p : β} (hp0 : 0 β€ p) (hp1 : p β€ 1) : | |
| 0 β€ binaryEntropy p := | |
| div_nonneg (Real.binEntropy_nonneg hp0 hp1) log_two_pos.le | |
| theorem binaryEntropy_le_one (p : β) : binaryEntropy p β€ 1 := by | |
| exact (div_le_one log_two_pos).mpr Real.binEntropy_le_log_two | |
| theorem binaryEntropy_concaveOn : ConcaveOn β (Set.Icc 0 1) binaryEntropy := by | |
| convert Real.strictConcave_binEntropy.concaveOn.smul | |
| (inv_nonneg.mpr log_two_pos.le) using 1 | |
| ext p | |
| simp only [binaryEntropy, smul_eq_mul, div_eq_mul_inv, mul_comm] | |
| theorem binaryEntropy_strictMonoOn : | |
| StrictMonoOn binaryEntropy (Set.Icc 0 (1 / 2)) := by | |
| intro x hx y hy hxy | |
| exact (div_lt_div_iff_of_pos_right log_two_pos).mpr | |
| (Real.binEntropy_strictMonoOn (by simpa only [one_div] using hx) | |
| (by simpa only [one_div] using hy) hxy) | |
| theorem binaryEntropy_monoOn : MonotoneOn binaryEntropy (Set.Icc 0 (1 / 2)) := | |
| binaryEntropy_strictMonoOn.monotoneOn | |
| private theorem logb_two_self : Real.logb 2 2 = 1 := | |
| Real.logb_self_eq_one (by norm_num) | |
| private theorem logb_two_four : Real.logb 2 4 = 2 := by | |
| have h := Real.logb_pow 2 2 2 | |
| norm_num [logb_two_self] at h β’ | |
| exact h | |
| theorem binaryEntropy_quarter : | |
| binaryEntropy (1 / 4) = 2 - (3 / 4) * Real.logb 2 3 := by | |
| rw [binaryEntropy_eq_logb] | |
| norm_num only [show (1 - (1 / 4 : β)) = 3 / 4 by norm_num, | |
| show (1 / 4 : β)β»ΒΉ = 4 by norm_num, | |
| show (3 / 4 : β)β»ΒΉ = 4 / 3 by norm_num] | |
| rw [Real.logb_div (by norm_num) (by norm_num), logb_two_four] | |
| ring | |
| theorem binaryEntropy_third : | |
| binaryEntropy (1 / 3) = Real.logb 2 3 - 2 / 3 := by | |
| rw [binaryEntropy_eq_logb] | |
| norm_num only [show (1 - (1 / 3 : β)) = 2 / 3 by norm_num, | |
| show (1 / 3 : β)β»ΒΉ = 3 by norm_num, | |
| show (2 / 3 : β)β»ΒΉ = 3 / 2 by norm_num] | |
| rw [Real.logb_div (by norm_num) (by norm_num), logb_two_self] | |
| ring | |
| theorem binaryEntropy_quarter_complement : | |
| (3 / 4) * binaryEntropy (1 / 3) - 1 / 2 = 1 - binaryEntropy (1 / 4) := by | |
| rw [binaryEntropy_third, binaryEntropy_quarter] | |
| ring | |
| theorem binaryEntropy_sixth : | |
| binaryEntropy (1 / 6) = | |
| (1 / 6) * Real.logb 2 6 + (5 / 6) * Real.logb 2 (6 / 5) := by | |
| rw [binaryEntropy_eq_logb] | |
| norm_num | |
| /-- The integer power comparison certifying the paper's decimal entropy bound. -/ | |
| theorem binaryEntropy_quarter_lt : binaryEntropy (1 / 4) < 4057 / 5000 := by | |
| have h := Real.logb_lt_logb (b := (2 : β)) (by norm_num : (1 : β) < 2) | |
| (by positivity : 0 < (2 : β) ^ 84) | |
| (by norm_num : (2 : β) ^ 84 < 3 ^ 53) | |
| rw [Real.logb_pow, Real.logb_pow, logb_two_self] at h | |
| norm_num at h | |
| rw [binaryEntropy_quarter] | |
| linarith | |
| theorem binaryEntropy_third_lt : binaryEntropy (1 / 3) < 14 / 15 := by | |
| have h := Real.logb_lt_logb (b := (2 : β)) (by norm_num : (1 : β) < 2) | |
| (by positivity : 0 < (3 : β) ^ 5) | |
| (by norm_num : (3 : β) ^ 5 < 2 ^ 8) | |
| rw [Real.logb_pow, Real.logb_pow, logb_two_self] at h | |
| norm_num at h | |
| rw [binaryEntropy_third] | |
| linarith | |
| theorem binaryEntropy_sixth_gt : 5 / 8 < binaryEntropy (1 / 6) := by | |
| have h6 := Real.logb_lt_logb (b := (2 : β)) (by norm_num : (1 : β) < 2) | |
| (by positivity : 0 < (2 : β) ^ 5) | |
| (by norm_num : (2 : β) ^ 5 < 6 ^ 2) | |
| have h65 := Real.logb_lt_logb (b := (2 : β)) (by norm_num : (1 : β) < 2) | |
| (by norm_num : 0 < (2 : β)) | |
| (by norm_num : (2 : β) < (6 / 5) ^ 4) | |
| rw [Real.logb_pow, Real.logb_pow, logb_two_self] at h6 | |
| rw [Real.logb_pow, logb_two_self] at h65 | |
| norm_num at h6 h65 | |
| rw [binaryEntropy_sixth] | |
| linarith | |
| /-- The paired entropy budget in the representation tree is maximized | |
| when the two cardinality densities coincide. -/ | |
| theorem binaryEntropy_pair_le_quarter {p : β} (hp0 : 0 β€ p) (hp1 : p β€ 1 / 2) : | |
| (binaryEntropy p + binaryEntropy (1 / 2 - p)) / 2 β€ binaryEntropy (1 / 4) := by | |
| have h := binaryEntropy_concaveOn.2 | |
| (show p β Set.Icc 0 1 from β¨hp0, by linarithβ©) | |
| (show 1 / 2 - p β Set.Icc 0 1 from β¨by linarith, by linarithβ©) | |
| (show (0 : β) β€ 1 / 2 by norm_num) (show (0 : β) β€ 1 / 2 by norm_num) | |
| (show (1 / 2 : β) + 1 / 2 = 1 by norm_num) | |
| norm_num only [smul_eq_mul] at h | |
| convert h using 1 <;> congr 1 <;> ring | |
| /-- The two child entropy values differ by less than the paper's `3/8` | |
| margin throughout the allowed density interval. -/ | |
| theorem binaryEntropy_abs_sub_lt {p q : β} (hp : p β Set.Icc (1 / 6) (1 / 3)) | |
| (hq : q β Set.Icc (1 / 6) (1 / 3)) : | |
| |binaryEntropy p - binaryEntropy q| < 3 / 8 := by | |
| have hp_lower := binaryEntropy_monoOn (show (1 / 6 : β) β Set.Icc 0 (1 / 2) | |
| by norm_num) (show p β Set.Icc 0 (1 / 2) from β¨by linarith [hp.1], by linarith [hp.2]β©) | |
| hp.1 | |
| have hq_lower := binaryEntropy_monoOn (show (1 / 6 : β) β Set.Icc 0 (1 / 2) | |
| by norm_num) (show q β Set.Icc 0 (1 / 2) from β¨by linarith [hq.1], by linarith [hq.2]β©) | |
| hq.1 | |
| rw [abs_lt] | |
| constructor <;> linarith [binaryEntropy_le_one p, binaryEntropy_le_one q, | |
| binaryEntropy_sixth_gt] | |
| open scoped BigOperators | |
| /-- The binomial mass at its integral mean lies between `1/(n+1)` and `1`. -/ | |
| theorem binomialMass_mean_bounds (n k : β) {p : β} (hp : p β Set.Ioo 0 1) | |
| (hk : k β€ n) (hmean : (n : β) * p = k) : | |
| 1 β€ ((n : β) + 1) * TraceReconstruction.binomialMass n p k β§ | |
| TraceReconstruction.binomialMass n p k β€ 1 := by | |
| have hnonneg (j : β) := TraceReconstruction.binomialMass_nonneg n j | |
| (show p β Set.Icc 0 1 from β¨hp.1.le, hp.2.leβ©) | |
| constructor | |
| Β· have hs := Finset.sum_le_sum (s := Finset.range (n + 1)) | |
| (fun j _ => TraceReconstruction.binomialMass_le_mode n k j hp hk hmean) | |
| simpa only [TraceReconstruction.sum_binomialMass, Finset.sum_const, | |
| Finset.card_range, nsmul_eq_mul, Nat.cast_add, Nat.cast_one] using hs | |
| Β· have hs := Finset.single_le_sum (fun j _ => hnonneg j) | |
| (Finset.mem_range.mpr (show k < n + 1 by omega)) | |
| simpa only [TraceReconstruction.sum_binomialMass] using hs | |
| /-- The inverse of the entropy power is the probability of an individual | |
| binary string with `k` successes, when the Bernoulli parameter is `k/n`. -/ | |
| theorem binomialMass_eq_choose_div_entropy (n k : β) (hk0 : 0 < k) (hkn : k < n) : | |
| TraceReconstruction.binomialMass n ((k : β) / n) k = | |
| (n.choose k : β) / (2 : β) ^ ((n : β) * binaryEntropy ((k : β) / n)) := by | |
| let p : β := (k : β) / n | |
| have hn0 : (0 : β) < n := by exact_mod_cast (lt_trans hk0 hkn) | |
| have hp0 : 0 < p := div_pos (by exact_mod_cast hk0) hn0 | |
| have hp1 : p < 1 := (div_lt_one hn0).mpr (by exact_mod_cast hkn) | |
| have hmean : (n : β) * p = k := by | |
| dsimp [p] | |
| field_simp [ne_of_gt hn0] | |
| have hprod : p ^ k * (1 - p) ^ (n - k) = | |
| Real.exp (-(n : β) * Real.binEntropy p) := by | |
| conv_lhs => rw [β Real.exp_log (pow_pos hp0 k), | |
| β Real.exp_log (pow_pos (sub_pos.mpr hp1) (n - k))] | |
| rw [β Real.exp_add] | |
| congr 1 | |
| rw [Real.log_pow, Real.log_pow, Real.binEntropy, Real.log_inv, Real.log_inv, | |
| Nat.cast_sub hkn.le] | |
| linear_combination (Real.log (1 - p) - Real.log p) * hmean | |
| have hpower : (2 : β) ^ ((n : β) * binaryEntropy p) = | |
| Real.exp ((n : β) * Real.binEntropy p) := by | |
| rw [Real.rpow_def_of_pos (by norm_num), binaryEntropy] | |
| congr 1 | |
| field_simp [ne_of_gt log_two_pos] | |
| change TraceReconstruction.binomialMass n p k = _ | |
| unfold TraceReconstruction.binomialMass | |
| rw [mul_assoc, hprod, hpower, div_eq_mul_inv, β Real.exp_neg] | |
| congr 2 | |
| ring | |
| /-- Exact entropy bounds for binomial coefficients, including both endpoints. -/ | |
| theorem choose_entropy_bounds (n k : β) (hk : k β€ n) : | |
| (2 : β) ^ ((n : β) * binaryEntropy ((k : β) / n)) / ((n : β) + 1) β€ | |
| (n.choose k : β) β§ | |
| (n.choose k : β) β€ (2 : β) ^ ((n : β) * binaryEntropy ((k : β) / n)) := by | |
| have hn1 : (0 : β) < (n : β) + 1 := by positivity | |
| by_cases hk0 : k = 0 | |
| Β· subst k | |
| simp only [Nat.cast_zero, zero_div, binaryEntropy_zero, mul_zero, Real.rpow_zero, | |
| Nat.choose_zero_right, Nat.cast_one, le_refl, and_true] | |
| exact (div_le_one hn1).mpr (by linarith [Nat.cast_nonneg (Ξ± := β) n]) | |
| by_cases hkn : k = n | |
| Β· subst k | |
| have hn0 : (n : β) β 0 := by exact_mod_cast hk0 | |
| simp only [div_self hn0, binaryEntropy_one, mul_zero, Real.rpow_zero, | |
| Nat.choose_self, Nat.cast_one, le_refl, and_true] | |
| exact (div_le_one hn1).mpr (by linarith [Nat.cast_nonneg (Ξ± := β) n]) | |
| have hkn' : k < n := lt_of_le_of_ne hk hkn | |
| have hn0 : (0 : β) < n := by exact_mod_cast (lt_trans (Nat.pos_of_ne_zero hk0) hkn') | |
| have hp : (k : β) / n β Set.Ioo 0 1 := | |
| β¨div_pos (by exact_mod_cast Nat.pos_of_ne_zero hk0) hn0, | |
| (div_lt_one hn0).mpr (by exact_mod_cast hkn')β© | |
| have hmean : (n : β) * ((k : β) / n) = k := by field_simp [ne_of_gt hn0] | |
| obtain β¨hl, huβ© := binomialMass_mean_bounds n k hp hk hmean | |
| rw [binomialMass_eq_choose_div_entropy n k (Nat.pos_of_ne_zero hk0) hkn'] at hl hu | |
| have hpower : 0 < (2 : β) ^ ((n : β) * binaryEntropy ((k : β) / n)) := | |
| Real.rpow_pos_of_pos (by norm_num) _ | |
| constructor | |
| Β· rw [β mul_div_assoc] at hl | |
| have h := (le_div_iffβ hpower).mp hl | |
| exact (div_le_iffβ hn1).mpr (by simpa only [one_mul, mul_comm] using h) | |
| Β· exact (div_le_one hpower).mp hu | |
| /-- Logarithmic form used in the background allocation formulas. -/ | |
| theorem logb_choose_bounds (n k : β) (hk : k β€ n) : | |
| (n : β) * binaryEntropy ((k : β) / n) - Real.logb 2 ((n : β) + 1) β€ | |
| Real.logb 2 (n.choose k) β§ | |
| Real.logb 2 (n.choose k) β€ (n : β) * binaryEntropy ((k : β) / n) := by | |
| obtain β¨hl, huβ© := choose_entropy_bounds n k hk | |
| have hn1 : (0 : β) < (n : β) + 1 := by positivity | |
| have hpower : 0 < (2 : β) ^ ((n : β) * binaryEntropy ((k : β) / n)) := | |
| Real.rpow_pos_of_pos (by norm_num) _ | |
| constructor | |
| Β· have h := Real.logb_le_logb_of_le (by norm_num : (1 : β) < 2) | |
| (div_pos hpower hn1) hl | |
| simpa only [Real.logb_div (ne_of_gt hpower) (ne_of_gt hn1), | |
| Real.logb_rpow (by norm_num : (0 : β) < 2) (by norm_num : (2 : β) β 1)] using h | |
| Β· have h := Real.logb_le_logb_of_le (by norm_num : (1 : β) < 2) | |
| (show (0 : β) < n.choose k by exact_mod_cast Nat.choose_pos hk) hu | |
| simpa only [Real.logb_rpow (by norm_num : (0 : β) < 2) | |
| (by norm_num : (2 : β) β 1)] using h | |
| /-- Rounding the binomial logarithm costs less than `logβ(n+1)+1` bits. -/ | |
| theorem floor_logb_choose_error (n k : β) (hk : k β€ n) : | |
| |((βReal.logb 2 (n.choose k)β : β€) : β) - | |
| (n : β) * binaryEntropy ((k : β) / n)| < Real.logb 2 ((n : β) + 1) + 1 := by | |
| obtain β¨hl, huβ© := logb_choose_bounds n k hk | |
| have hf := Int.floor_le (Real.logb 2 (n.choose k)) | |
| have hfloor := Int.sub_one_lt_floor (Real.logb 2 (n.choose k)) | |
| rw [abs_of_nonpos (by linarith)] | |
| linarith | |
| end LowSpaceSubsetSum | |
| end OAI | |