import Mathlib.Analysis.SpecialFunctions.Log.Basic import Mathlib.Analysis.SpecialFunctions.Log.Base import Mathlib.Analysis.SpecialFunctions.Log.Deriv import Mathlib.Analysis.Calculus.MeanValue import Mathlib.Analysis.Convex.SpecificFunctions.Basic import Mathlib.Analysis.Convex.Jensen import Mathlib.Data.Nat.Choose.Sum import Mathlib.Data.Nat.Choose.Central import Mathlib.Data.Nat.Factorial.Basic /-! # Pillar 3: MDL-vs-Memorization Compression Phase Transitions, Barron–Cover Risk Bounds, # Exact Binomial NML / Shtarkov Redundancy, and Exact KT & Laplace Sequential Code Lengths Self-contained Lean 4 formalization establishing exact, sharp, non-asymptotic results for: 1. **Exact Two-Part MDL vs. Verbatim Memorization Phase Transitions**: - Linear regime: exact critical sample size `n_c = κ / (log₂ q - h)` with sharp strict phase-transition separation (memorization regime `n < n_c` vs. compression regime `n > n_c`), plus explicit memorization & generalization witnesses. - Nonlinear BIC / Rissanen complexity-penalty regime `L_MDL(n) = κ₀ + (d/2) ln n + n h`: explicit closed-form non-asymptotic compression threshold `n_gen` proving structured compression strictly beats verbatim memorization for all `n > n_gen`, paired with an explicit small-`n` memorization witness. 2. **Exact Non-Asymptotic Barron–Cover Two-Part MDL Hellinger Risk & Tail Bounds**: for any finite hypothesis class with Kraft-feasible code lengths `∑_h 2^{-L(h)} ≤ 1`, the two-part MDL estimator `ĥ_MDL` minimizing `2 L(h) ln 2 - ln P_h^{(n)}(x)` satisfies: - Exact MGF bound `E_{P*}[exp(n H²(P*, P_{ĥ}) - L(h*) ln 2)] ≤ 1`, - Exact expected squared Hellinger risk `E_{P*}[H²(P*, P_{ĥ})] ≤ L(h*) ln 2 / n`, - Exact exponential tail bound `P*(H²(P*, P_{ĥ}) ≥ ε) ≤ exp(L(h*) ln 2 - n ε)`. 3. **Exact Binomial NML / Shtarkov Minimax Regret Equality**: `L_NML(x) - (-ln P_ML(x)) = ln S_Bern(n)` identically for all `x ∈ {0,1}^n`, and exact minimax optimality: every sub-probability code `Q` suffers worst-case regret `≥ ln S_Bern(n)`. 4. **Exact Laplace Sequential Predictor (Rule of Succession)**: exact closed form `P_Lap(x) = 1 / ((n + 1) C(n, k))`, exact normalization `∑_x P_Lap(x) = 1`, exact non-asymptotic regret bound `≤ ln(n + 1)`, and exact equality sharpness at `0^n` and `1^n`. 5. **Exact Krichevsky–Trofimov (KT) Sequential Predictor & Two-Sided Binomial Shtarkov Sandwich**: - Exact rational closed form `P_KT(x) = (2k)!(2(n-k))! / (4^n k!(n-k)! n!)` and exact normalization `∑_x P_KT(x) = 1`. - **Exact Unimodality of KT Regret Ratio** (via strict monotonicity of `h(m) = (m+1)^(m+1) / (m^m (2m+1))`, proved via `ln((1+u)/(1-u)) > 2u`): maximized at `k = 0, n` (`4^n / C(2n, n)`) and minimized at `k = ⌊n/2⌋`! - **Exact Non-Asymptotic KT Regret Bound**: `R_KT(x) ≤ (1/2) ln n + ln 2` for ALL `n ≥ 1`, attained with equality at `n = 1` and **strictly beating Laplace's `ln(n + 1)` for EVERY `n ≥ 2`**! - **Exact Elementary Two-Sided Binomial Shtarkov Sandwich** (zero Stirling needed!): `(1/2) ln(n + 1) ≤ ln S_Bern(n) ≤ (1/2) ln n + ln 2` for ALL `n ≥ 1`. -/ namespace MDLPhaseTransition open Finset BigOperators /-! ## Section 1: Exact Two-Part MDL vs. Verbatim Memorization Phase Transitions -/ /-- Two-part MDL code length `L_MDL(H, D) = L(H) + L(D | H) = κ + n * h`. -/ def mdlLength (κ h : ℝ) (n : ℝ) : ℝ := κ + n * h /-- Verbatim memorization code length over a `q`-ary alphabet: `n * log₂ q` bits. -/ noncomputable def memLengthBits (q : ℕ) (n : ℝ) : ℝ := n * Real.logb 2 q /-- Critical sample-size threshold `n_c = κ / (log₂ q - h)` where two-part MDL matches verbatim memorization. -/ noncomputable def criticalSampleSize (q : ℕ) (κ h : ℝ) : ℝ := κ / (Real.logb 2 q - h) /-- **Exact Linear Two-Part MDL vs. Memorization Phase Transition**: whenever per-sample residual entropy `h < log₂ q` (strictly positive compression margin `Δ = log₂ q - h > 0`), the critical threshold `n_c = κ / Δ` sharply separates: 1. Memorization regime (`n < n_c`): `L_mem(n) < L_MDL(n)` strictly. 2. Critical point (`n = n_c`): `L_MDL(n_c) = L_mem(n_c)`. 3. Compression / generalization regime (`n > n_c`): `L_MDL(n) < L_mem(n)` strictly. -/ theorem mdl_vs_memorization_phase_transition {q : ℕ} {κ h : ℝ} (hmargin : h < Real.logb 2 q) (n : ℝ) : (n < criticalSampleSize q κ h ↔ memLengthBits q n < mdlLength κ h n) ∧ (n = criticalSampleSize q κ h ↔ mdlLength κ h n = memLengthBits q n) ∧ (criticalSampleSize q κ h < n ↔ mdlLength κ h n < memLengthBits q n) := by have hΔ : 0 < Real.logb 2 q - h := sub_pos.mpr hmargin simp only [criticalSampleSize, memLengthBits, mdlLength] refine ⟨?_, ?_, ?_⟩ · rw [lt_div_iff₀ hΔ] constructor <;> intro H <;> nlinarith · rw [eq_div_iff (ne_of_gt hΔ)] constructor <;> intro H <;> nlinarith · rw [div_lt_iff₀ hΔ] constructor <;> intro H <;> nlinarith /-- **Sharp Explicit Phase-Transition Witnesses** (binary alphabet `q = 2`, `log₂ 2 = 1`, model cost `κ = 10` bits, residual rate `h = 1/2` bits/sample → `n_c = 20`): - Memorization witness at `n = 19`: `L_mem = 19 < 19.5 = L_MDL`, - Critical equality at `n = 20`: `L_MDL = 20 = L_mem`, - Generalization/compression witness at `n = 21`: `L_MDL = 20.5 < 21 = L_mem`. -/ theorem mdl_phase_transition_sharp_witnesses : criticalSampleSize 2 10 (1 / 2) = 20 ∧ memLengthBits 2 19 < mdlLength 10 (1 / 2) 19 ∧ mdlLength 10 (1 / 2) 20 = memLengthBits 2 20 ∧ mdlLength 10 (1 / 2) 21 < memLengthBits 2 21 := by have hlog : Real.logb 2 (2 : ℕ) = 1 := by norm_cast exact Real.logb_self_eq_one (by norm_num) simp only [criticalSampleSize, memLengthBits, mdlLength, hlog] norm_num /-- Nonlinear parametric (BIC / Rissanen) two-part MDL objective in nats: `L_BIC(n) = κ₀ + (d / 2) ln n + n h` vs. verbatim memorization `n ln q`. -/ noncomputable def bicMDLLength (κ₀ d h n : ℝ) : ℝ := κ₀ + (d / 2) * Real.log n + n * h /-- **Exact Nonlinear BIC / Rissanen MDL Compression Threshold**: For any positive compression margin `Δ = ln q - h > 0`, dimension `d > 0`, and constant overhead `κ₀ ≥ 0`, structured parametric MDL strictly beats verbatim memorization `n ln q` for ALL `n > (2 κ₀ + d (ln(d / Δ) - 1)) / Δ` (with `n > 0`). -/ theorem bic_mdl_beats_memorization_above_threshold {q : ℕ} {κ₀ d h n : ℝ} (hd : 0 < d) (hmargin : h < Real.log q) (hn : 0 < n) (hthresh : (2 * κ₀ + d * (Real.log (d / (Real.log q - h)) - 1)) / (Real.log q - h) < n) : bicMDLLength κ₀ d h n < n * Real.log q := by set Δ := Real.log q - h have hΔ : 0 < Δ := sub_pos.mpr hmargin -- Elementary tangent bound: `ln(n) = ln(Δ n / d) + ln(d / Δ) ≤ Δ n / d - 1 + ln(d / Δ)` have hlog : Real.log n ≤ Δ * n / d - 1 + Real.log (d / Δ) := by have hsplit : Real.log n = Real.log (Δ * n / d) + Real.log (d / Δ) := by rw [← Real.log_mul (ne_of_gt (div_pos (mul_pos hΔ hn) hd)) (ne_of_gt (div_pos hd hΔ))] congr 1 field_simp [ne_of_gt hd, ne_of_gt hΔ] have hle := Real.log_le_sub_one_of_pos (div_pos (mul_pos hΔ hn) hd) linarith have hthresh' : 2 * κ₀ + d * (Real.log (d / Δ) - 1) < Δ * n := by have := (div_lt_iff₀ hΔ).mp hthresh linarith simp only [bicMDLLength] have hmul : (d / 2) * Real.log n ≤ (d / 2) * (Δ * n / d - 1 + Real.log (d / Δ)) := mul_le_mul_of_nonneg_left hlog (by linarith) have hsimp : (d / 2) * (Δ * n / d - 1 + Real.log (d / Δ)) = (Δ * n) / 2 + (d / 2) * (Real.log (d / Δ) - 1) := by field_simp [ne_of_gt hd] ring nlinarith /-- **Explicit Memorization-Regime Witness for Nonlinear BIC MDL**: At `n = 1` with `q = 2, κ₀ = 1, d = 4, h = 0`, `L_BIC(1) = 1 > ln 2 = L_mem(1)` (memorization strictly wins at small sample size `n = 1`), whereas at `n = 16`, `L_BIC(16) < 16 ln 2` (structured compression strictly wins). -/ theorem bic_mdl_phase_transition_witness : (1 : ℝ) * Real.log (2 : ℕ) < bicMDLLength 1 4 0 1 ∧ bicMDLLength 1 4 0 16 < (16 : ℝ) * Real.log (2 : ℕ) := by -- Elementary bounds `1/2 < ln 2 < 1`: -- `ln 2 < 1` because `2 < e` (`exp(1) ≥ 1 + 1 + 1/2 > 2`, or `Real.exp_one_gt_d9`) have hlog2_lt : Real.log 2 < 1 := by have h1 : (2 : ℝ) < Real.exp 1 := by have := Real.sum_le_exp_of_nonneg (by norm_num : (0 : ℝ) ≤ 1) 3 norm_num [Finset.sum_range_succ, Nat.factorial] at this linarith have h2 := Real.log_lt_log (by norm_num) h1 rwa [Real.log_exp] at h2 -- `ln 2 > 1/2` because `ln 2 = -ln(1/2) ≥ 1 - 1/2 = 1/2`, and strictly `2 > exp(1/2)` since `4 > e`! -- Actually even simpler: `ln 2 ≥ 1/2` is enough for `1 + 2 * 4 * ln 2 < 16 * ln 2 <=> 1 < 8 * ln 2`! have hlog2_ge_half : (1 / 2 : ℝ) ≤ Real.log 2 := by have h := Real.log_le_sub_one_of_pos (by norm_num : (0 : ℝ) < 1 / 2) rw [one_div, Real.log_inv] at h linarith constructor · simp only [bicMDLLength, Real.log_one, mul_zero, add_zero, one_mul, Nat.cast_ofNat] exact hlog2_lt · simp only [bicMDLLength, mul_zero, add_zero, Nat.cast_ofNat] have hlog16 : Real.log 16 = 4 * Real.log 2 := by rw [show (16 : ℝ) = (2 : ℝ) ^ (4 : ℕ) by norm_num, Real.log_pow] ring rw [hlog16] nlinarith /-! ## Section 2: Exact Non-Asymptotic Barron–Cover Two-Part MDL Hellinger Risk & Tail Bounds -/ /-- Squared Hellinger distance `H²(p, q) = 1 - ∑_a √(p a * q a)`. -/ noncomputable def hellingerSq {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := 1 - ∑ a : α, Real.sqrt (p a * q a) /-- i.i.d. `n`-sample product likelihood `P^{(n)}(x) = ∏_{i : Fin n} P(x i)`. -/ def iidProd {α : Type*} (p : α → ℝ) {n : ℕ} (x : Fin n → α) : ℝ := ∏ i : Fin n, p (x i) lemma iidProd_nonneg {α : Type*} {p : α → ℝ} (hp : ∀ a, 0 ≤ p a) {n : ℕ} (x : Fin n → α) : 0 ≤ iidProd p x := Finset.prod_nonneg fun i _ => hp (x i) lemma sum_iidProd_eq_one {α : Type*} [Fintype α] {p : α → ℝ} (hp1 : ∑ a : α, p a = 1) (n : ℕ) : ∑ x : Fin n → α, iidProd p x = 1 := by simp only [iidProd] rw [← Fintype.prod_sum (fun (_ : Fin n) (a : α) => p a)] simp [hp1] lemma sum_sqrt_iidProd {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (n : ℕ) : ∑ x : Fin n → α, Real.sqrt (iidProd p x * iidProd q x) = (1 - hellingerSq p q) ^ n := by have hterm : ∀ x : Fin n → α, Real.sqrt (iidProd p x * iidProd q x) = ∏ i : Fin n, Real.sqrt (p (x i) * q (x i)) := by intro x simp only [iidProd, ← Finset.prod_mul_distrib] rw [Real.sqrt_prod Finset.univ (fun i _ => mul_nonneg (hp (x i)) (hq (x i)))] simp_rw [hterm] rw [← Fintype.prod_sum (fun (_ : Fin n) (a : α) => Real.sqrt (p a * q a))] simp [hellingerSq] /-- **Exact Non-Asymptotic Barron–Cover (1991) Two-Part MDL Risk & Exponential Tail Theorem**: For any finite hypothesis class `ι` of distributions `P : ι → α → ℝ` with Kraft-feasible nat code lengths `L : ι → ℝ` (`∑_h exp(-L h) ≤ 1`), true model `h* ∈ ι`, and ANY two-part MDL estimator `ĥ : (Fin n → α) → ι` satisfying the exact two-part optimality criterion `exp(-2 L(h*)) P_{h*}^{(n)}(x) ≤ exp(-2 L(ĥ(x))) P_{ĥ(x)}^{(n)}(x)` for all `x`: 1. **Exact MGF Bound**: `E_{P_{h*}}[exp(n H²(P_{h*}, P_{ĥ(X)}) - L(h*))] ≤ 1`. 2. **Exact Expected Hellinger Risk**: `E_{P_{h*}}[H²(P_{h*}, P_{ĥ(X)})] ≤ L(h*) / n` (for `n ≥ 1`). 3. **Exact Exponential Tail**: for every `ε ≥ 0`, `P_{h*}(H²(P_{h*}, P_{ĥ(X)}) ≥ ε) ≤ exp(L(h*) - n ε)`. -/ theorem barron_cover_two_part_mdl_bounds {α ι : Type*} [Fintype α] [Fintype ι] [DecidableEq ι] (P : ι → α → ℝ) (L : ι → ℝ) (hstar : ι) (n : ℕ) (hP_nn : ∀ h a, 0 ≤ P h a) (hP_sum : ∀ h, ∑ a : α, P h a = 1) (hKraft : ∑ h : ι, Real.exp (-L h) ≤ 1) (hhat : (Fin n → α) → ι) (hMDL : ∀ x : Fin n → α, Real.exp (-2 * L hstar) * iidProd (P hstar) x ≤ Real.exp (-2 * L (hhat x)) * iidProd (P (hhat x)) x) : (∑ x : Fin n → α, iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) ≤ 1) ∧ (1 ≤ n → ∑ x : Fin n → α, iidProd (P hstar) x * hellingerSq (P hstar) (P (hhat x)) ≤ L hstar / (n : ℝ)) ∧ (∀ ε : ℝ, ∑ x ∈ (Finset.univ : Finset (Fin n → α)).filter (fun x => ε ≤ hellingerSq (P hstar) (P (hhat x))), iidProd (P hstar) x ≤ Real.exp (L hstar - (n : ℝ) * ε)) := by -- Step 1: Pointwise key inequality from MDL optimality have hpt : ∀ x : Fin n → α, iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) ≤ Real.exp (-L (hhat x)) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * Real.sqrt (iidProd (P hstar) x * iidProd (P (hhat x)) x) := by intro x have hopt := hMDL x have hsqrt := Real.sqrt_le_sqrt hopt have he1 : Real.sqrt (Real.exp (-2 * L hstar) * iidProd (P hstar) x) = Real.exp (-L hstar) * Real.sqrt (iidProd (P hstar) x) := by rw [Real.sqrt_mul (le_of_lt (Real.exp_pos _)), ← Real.exp_half] congr 2; ring have he2 : Real.sqrt (Real.exp (-2 * L (hhat x)) * iidProd (P (hhat x)) x) = Real.exp (-L (hhat x)) * Real.sqrt (iidProd (P (hhat x)) x) := by rw [Real.sqrt_mul (le_of_lt (Real.exp_pos _)), ← Real.exp_half] congr 2; ring rw [he1, he2] at hsqrt have hmul := mul_le_mul_of_nonneg_left hsqrt (mul_nonneg (Real.sqrt_nonneg (iidProd (P hstar) x)) (le_of_lt (Real.exp_pos ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)))))) have hL : Real.sqrt (iidProd (P hstar) x) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * (Real.exp (-L hstar) * Real.sqrt (iidProd (P hstar) x)) = iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) := by rw [sub_eq_add_neg, Real.exp_add] have hself := Real.mul_self_sqrt (iidProd_nonneg (hP_nn hstar) x) calc Real.sqrt (iidProd (P hstar) x) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * (Real.exp (-L hstar) * Real.sqrt (iidProd (P hstar) x)) = (Real.sqrt (iidProd (P hstar) x) * Real.sqrt (iidProd (P hstar) x)) * (Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * Real.exp (-L hstar)) := by ring _ = iidProd (P hstar) x * (Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * Real.exp (-L hstar)) := by rw [hself] have hR : Real.sqrt (iidProd (P hstar) x) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * (Real.exp (-L (hhat x)) * Real.sqrt (iidProd (P (hhat x)) x)) = Real.exp (-L (hhat x)) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * Real.sqrt (iidProd (P hstar) x * iidProd (P (hhat x)) x) := by rw [Real.sqrt_mul (iidProd_nonneg (hP_nn hstar) x)] ring linarith -- Step 2: Bound by sum over all `h : ι` and evaluate via Bhattacharyya product have hmgf : ∑ x : Fin n → α, iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) ≤ 1 := by calc ∑ x : Fin n → α, iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) ≤ ∑ x : Fin n → α, Real.exp (-L (hhat x)) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x))) * Real.sqrt (iidProd (P hstar) x * iidProd (P (hhat x)) x) := Finset.sum_le_sum fun x _ => hpt x _ ≤ ∑ x : Fin n → α, ∑ h : ι, Real.exp (-L h) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P h)) * Real.sqrt (iidProd (P hstar) x * iidProd (P h) x) := by refine Finset.sum_le_sum fun x _ => ?_ exact Finset.single_le_sum (f := fun h : ι => Real.exp (-L h) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P h)) * Real.sqrt (iidProd (P hstar) x * iidProd (P h) x)) (fun h _ => mul_nonneg (mul_nonneg (le_of_lt (Real.exp_pos _)) (le_of_lt (Real.exp_pos _))) (Real.sqrt_nonneg _)) (Finset.mem_univ (hhat x)) _ = ∑ h : ι, Real.exp (-L h) * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P h)) * ∑ x : Fin n → α, Real.sqrt (iidProd (P hstar) x * iidProd (P h) x) := by rw [Finset.sum_comm] refine Finset.sum_congr rfl fun h _ => (Finset.mul_sum _ _ _).symm _ = ∑ h : ι, Real.exp (-L h) * (Real.exp ((n : ℝ) * hellingerSq (P hstar) (P h)) * (1 - hellingerSq (P hstar) (P h)) ^ n) := by refine Finset.sum_congr rfl fun h _ => ?_ rw [sum_sqrt_iidProd (hP_nn hstar) (hP_nn h) n, mul_assoc] _ ≤ ∑ h : ι, Real.exp (-L h) * 1 := by refine Finset.sum_le_sum fun h _ => mul_le_mul_of_nonneg_left ?_ (le_of_lt (Real.exp_pos _)) -- `(1 - H²)^n ≤ exp(-n H²)` since `0 ≤ 1 - H² ≤ exp(-H²)` have h1sub_nn : 0 ≤ 1 - hellingerSq (P hstar) (P h) := by simp only [hellingerSq, sub_sub_cancel] exact Finset.sum_nonneg fun _ _ => Real.sqrt_nonneg _ have h1sub_le : 1 - hellingerSq (P hstar) (P h) ≤ Real.exp (-hellingerSq (P hstar) (P h)) := by have := Real.add_one_le_exp (-hellingerSq (P hstar) (P h)) linarith have hpow := pow_le_pow_left₀ h1sub_nn h1sub_le n rw [← Real.exp_nat_mul] at hpow have hmul := mul_le_mul_of_nonneg_left hpow (le_of_lt (Real.exp_pos ((n : ℝ) * hellingerSq (P hstar) (P h)))) rw [← Real.exp_add] at hmul have hzero : (n : ℝ) * hellingerSq (P hstar) (P h) + (n : ℝ) * -hellingerSq (P hstar) (P h) = 0 := by ring rw [hzero, Real.exp_zero] at hmul exact hmul _ = ∑ h : ι, Real.exp (-L h) := by simp _ ≤ 1 := hKraft refine ⟨hmgf, ?_, ?_⟩ · -- Part 2: Jensen on `Real.exp` intro hn have hn_pos : (0 : ℝ) < n := by exact_mod_cast hn have hjensen := ConvexOn.map_sum_le convexOn_exp (t := (Finset.univ : Finset (Fin n → α))) (w := fun x => iidProd (P hstar) x) (p := fun x => (n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) (fun x _ => iidProd_nonneg (hP_nn hstar) x) (sum_iidProd_eq_one (hP_sum hstar) n) (fun _ _ => Set.mem_univ _) have hexp_le : Real.exp (∑ x : Fin n → α, iidProd (P hstar) x * ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar)) ≤ 1 := le_trans hjensen hmgf have hlog_le : ∑ x : Fin n → α, iidProd (P hstar) x * ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) ≤ 0 := by have := Real.log_le_log (Real.exp_pos _) hexp_le simpa using this have hsplit : ∑ x : Fin n → α, iidProd (P hstar) x * ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) = (n : ℝ) * (∑ x : Fin n → α, iidProd (P hstar) x * hellingerSq (P hstar) (P (hhat x))) - L hstar := by simp_rw [mul_sub, Finset.sum_sub_distrib, ← Finset.sum_mul, sum_iidProd_eq_one (hP_sum hstar) n, one_mul] congr 1 rw [Finset.mul_sum] refine Finset.sum_congr rfl fun x _ => by ring rw [hsplit] at hlog_le rw [le_div_iff₀ hn_pos] linarith · -- Part 3: Exponential Markov / Chernoff tail bound intro ε set A := (Finset.univ : Finset (Fin n → α)).filter (fun x => ε ≤ hellingerSq (P hstar) (P (hhat x))) have hlow : (∑ x ∈ A, iidProd (P hstar) x) * Real.exp ((n : ℝ) * ε - L hstar) ≤ ∑ x : Fin n → α, iidProd (P hstar) x * Real.exp ((n : ℝ) * hellingerSq (P hstar) (P (hhat x)) - L hstar) := by rw [Finset.sum_mul] refine le_trans (Finset.sum_le_sum fun x hx => ?_) (Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun _ _ _ => mul_nonneg (iidProd_nonneg (hP_nn hstar) _) (le_of_lt (Real.exp_pos _)))) have hε := (Finset.mem_filter.mp hx).2 refine mul_le_mul_of_nonneg_left (Real.exp_le_exp.mpr ?_) (iidProd_nonneg (hP_nn hstar) x) have hn_nn : (0 : ℝ) ≤ n := Nat.cast_nonneg n nlinarith have h1 : (∑ x ∈ A, iidProd (P hstar) x) * Real.exp ((n : ℝ) * ε - L hstar) ≤ 1 := le_trans hlow hmgf have h2 := mul_le_mul_of_nonneg_right h1 (le_of_lt (Real.exp_pos (L hstar - (n : ℝ) * ε))) rw [mul_assoc, ← Real.exp_add, one_mul] at h2 have h0 : ((n : ℝ) * ε - L hstar) + (L hstar - (n : ℝ) * ε) = 0 := by ring rw [h0, Real.exp_zero, mul_one] at h2 exact h2 /-! ## Section 3: Exact Binomial NML / Shtarkov, Laplace & Krichevsky–Trofimov Predictors -/ /-- Number of `true` entries (ones) in a binary sequence `x : Fin n → Bool`. -/ def onesCount {n : ℕ} (x : Fin n → Bool) : ℕ := (Finset.univ.filter (fun i : Fin n => x i = true)).card lemma onesCount_le {n : ℕ} (x : Fin n → Bool) : onesCount x ≤ n := by calc onesCount x ≤ (Finset.univ : Finset (Fin n)).card := Finset.card_filter_le _ _ _ = n := by simp lemma card_onesCount_eq (n k : ℕ) : ((Finset.univ : Finset (Fin n → Bool)).filter (fun x => onesCount x = k)).card = Nat.choose n k := by -- Bijection with `powersetCard k (univ : Finset (Fin n))` let f : (Fin n → Bool) → Finset (Fin n) := fun x => Finset.univ.filter (fun i => x i = true) have hbij : Finset.card ((Finset.univ : Finset (Fin n → Bool)).filter (fun x => onesCount x = k)) = (Finset.powersetCard k (Finset.univ : Finset (Fin n))).card := by refine Finset.card_bij (fun x _ => f x) ?_ ?_ ?_ · intro x hx rw [Finset.mem_powersetCard] exact ⟨Finset.subset_univ _, (Finset.mem_filter.mp hx).2⟩ · intro x _ y _ hxy ext i have hi : i ∈ f x ↔ i ∈ f y := by rw [hxy] simp only [f, Finset.mem_filter, Finset.mem_univ, true_and] at hi cases hx : x i <;> cases hy : y i <;> simp_all · intro S hS rw [Finset.mem_powersetCard] at hS refine ⟨fun i => decide (i ∈ S), Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, ?_⟩ · simp only [onesCount, decide_eq_true_eq] convert hS.2 ext i; simp · ext i; simp [f] rw [hbij, Finset.card_powersetCard, Finset.card_univ, Fintype.card_fin] /-- Maximum-likelihood Bernoulli probability of a sequence with `k` ones out of `n`: `pML k n = (k / n)^k * ((n - k) / n)^(n - k)` (with `0^0 = 1`). -/ noncomputable def pML (k n : ℕ) : ℝ := ((k : ℝ) / (n : ℝ)) ^ k * (((n - k : ℕ) : ℝ) / (n : ℝ)) ^ (n - k) lemma pML_pos {n k : ℕ} (hn : 1 ≤ n) (hk : k ≤ n) : 0 < pML k n := by simp only [pML] have h1 : 0 < ((k : ℝ) / (n : ℝ)) ^ k := by rcases Nat.eq_zero_or_pos k with rfl | hkpos · simp · exact pow_pos (div_pos (by exact_mod_cast hkpos) (by exact_mod_cast hn)) k have h2 : 0 < (((n - k : ℕ) : ℝ) / (n : ℝ)) ^ (n - k) := by rcases Nat.eq_zero_or_pos (n - k) with h0 | hpos · rw [h0]; simp · exact pow_pos (div_pos (by exact_mod_cast hpos) (by exact_mod_cast hn)) (n - k) exact mul_pos h1 h2 lemma pML_zero {n : ℕ} (hn : 1 ≤ n) : pML 0 n = 1 := by simp only [pML, Nat.cast_zero, zero_div, pow_zero, Nat.sub_zero, one_mul] rw [div_self (by exact_mod_cast (by omega : n ≠ 0)), one_pow] lemma pML_self {n : ℕ} (hn : 1 ≤ n) : pML n n = 1 := by simp only [pML, Nat.sub_self, Nat.cast_zero, zero_div, pow_zero, mul_one] rw [div_self (by exact_mod_cast (by omega : n ≠ 0)), one_pow] lemma pML_symm {n k : ℕ} (hk : k ≤ n) : pML (n - k) n = pML k n := by simp only [pML, Nat.sub_sub_self hk] ring /-- **Exact Binomial Shtarkov Sum (NML Normalizer)**: `S_Bern(n) = ∑_{x ∈ {0,1}^n} P_ML(x) = ∑_{k=0}^n C(n, k) (k/n)^k ((n-k)/n)^(n-k)`. -/ noncomputable def shtarkovBern (n : ℕ) : ℝ := ∑ k ∈ Finset.range (n + 1), (Nat.choose n k : ℝ) * pML k n lemma sum_pML_eq_shtarkovBern (n : ℕ) : ∑ x : Fin n → Bool, pML (onesCount x) n = shtarkovBern n := by rw [shtarkovBern] have hmap : ∀ x ∈ (Finset.univ : Finset (Fin n → Bool)), onesCount x ∈ Finset.range (n + 1) := fun x _ => Finset.mem_range.mpr (Nat.lt_succ_of_le (onesCount_le x)) rw [← Finset.sum_fiberwise_of_maps_to hmap] refine Finset.sum_congr rfl fun k _ => ?_ have hconst : ∑ x ∈ Finset.univ.filter (fun x : Fin n → Bool => onesCount x = k), pML (onesCount x) n = ∑ _x ∈ Finset.univ.filter (fun x : Fin n → Bool => onesCount x = k), pML k n := Finset.sum_congr rfl fun x hx => by rw [(Finset.mem_filter.mp hx).2] rw [hconst, Finset.sum_const, nsmul_eq_mul, card_onesCount_eq] lemma shtarkovBern_pos {n : ℕ} (hn : 1 ≤ n) : 0 < shtarkovBern n := by rw [shtarkovBern] have h0 : 0 ∈ Finset.range (n + 1) := Finset.mem_range.mpr (by omega) refine lt_of_lt_of_le ?_ (Finset.single_le_sum (fun k hk => mul_nonneg (Nat.cast_nonneg _) (le_of_lt (pML_pos hn (Nat.lt_succ_iff.mp (Finset.mem_range.mp hk))))) h0) simp [pML_zero hn] /-- **Exact NML / Shtarkov Minimax Pointwise Regret Theorem**: 1. `P_NML(x) = P_ML(x) / S_Bern(n)` sums to `1` and achieves exact constant pointwise regret `-ln P_NML(x) - (-ln P_ML(x)) = ln S_Bern(n)` for EVERY `x ∈ {0,1}^n`. 2. **Exact Minimax Optimality**: for EVERY sub-probability code `Q : (Fin n → Bool) → ℝ` (`Q x > 0`, `∑_x Q x ≤ 1`), there exists `x` with `-ln Q(x) - (-ln P_ML(x)) ≥ ln S_Bern(n)`. -/ theorem nml_shtarkov_minimax_exact {n : ℕ} (hn : 1 ≤ n) : (∑ x : Fin n → Bool, pML (onesCount x) n / shtarkovBern n = 1) ∧ (∀ x : Fin n → Bool, -Real.log (pML (onesCount x) n / shtarkovBern n) - (-Real.log (pML (onesCount x) n)) = Real.log (shtarkovBern n)) ∧ (∀ Q : (Fin n → Bool) → ℝ, (∀ x, 0 < Q x) → (∑ x : Fin n → Bool, Q x ≤ 1) → ∃ x : Fin n → Bool, Real.log (shtarkovBern n) ≤ -Real.log (Q x) - (-Real.log (pML (onesCount x) n))) := by have hSpos := shtarkovBern_pos hn refine ⟨?_, ?_, ?_⟩ · rw [← Finset.sum_div, sum_pML_eq_shtarkovBern, div_self (ne_of_gt hSpos)] · intro x have hpx := pML_pos hn (onesCount_le x) rw [Real.log_div (ne_of_gt hpx) (ne_of_gt hSpos)] ring · intro Q hQpos hQsum by_contra hall push Not at hall have hlt_all : ∀ x : Fin n → Bool, pML (onesCount x) n / shtarkovBern n < Q x := by intro x have hx := hall x have hpx := pML_pos hn (onesCount_le x) have hlog : Real.log (pML (onesCount x) n / shtarkovBern n) < Real.log (Q x) := by rw [Real.log_div (ne_of_gt hpx) (ne_of_gt hSpos)] linarith exact (Real.log_lt_log_iff (div_pos hpx hSpos) (hQpos x)).mp hlog have hsum_lt : ∑ x : Fin n → Bool, pML (onesCount x) n / shtarkovBern n < ∑ x : Fin n → Bool, Q x := Finset.sum_lt_sum_of_nonempty Finset.univ_nonempty (fun x _ => hlt_all x) rw [← Finset.sum_div, sum_pML_eq_shtarkovBern, div_self (ne_of_gt hSpos)] at hsum_lt linarith /-! ## Section 4: Exact Laplace Sequential Predictor & Sharp `ln(n + 1)` Bound -/ /-- Exact Laplace sequential probability (Rule of Succession): `P_Lap(k, n) = 1 / ((n + 1) C(n, k))`. -/ noncomputable def pLaplace (k n : ℕ) : ℝ := 1 / (((n + 1 : ℕ) : ℝ) * (Nat.choose n k : ℝ)) /-- Exact Laplace sequential product over a binary sequence `x : Fin n → Bool` with stepwise conditionals `(k + 1) / (t + 2)` for `true` and `(t - k + 1) / (t + 2)` for `false`. -/ noncomputable def laplaceSeqProb : List Bool → ℝ | [] => 1 | bs => let rec go (ones zeros : ℕ) : List Bool → ℝ | [] => 1 | true :: rest => ((ones + 1 : ℕ) : ℝ) / ((ones + zeros + 2 : ℕ) : ℝ) * go (ones + 1) zeros rest | false :: rest => ((zeros + 1 : ℕ) : ℝ) / ((ones + zeros + 2 : ℕ) : ℝ) * go ones (zeros + 1) rest go 0 0 bs private lemma laplace_go_closed (ones zeros : ℕ) (bs : List Bool) : let o := bs.count true let z := bs.count false laplaceSeqProb.go ones zeros bs = ((Nat.factorial (ones + o) * Nat.factorial (zeros + z) * Nat.factorial (ones + zeros + 1) : ℕ) : ℝ) / ((Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros + o + z + 1) : ℕ) : ℝ) := by induction bs generalizing ones zeros with | nil => simp only [laplaceSeqProb.go, List.count_nil, add_zero] have hne : ((Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros + 1) : ℕ) : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (by positivity : 0 < Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros + 1)) rw [div_self hne] | cons b rest ih => cases b with | true => have hct : (true :: rest).count true = rest.count true + 1 := by simp have hcf : (true :: rest).count false = rest.count false := by simp simp only [laplaceSeqProb.go, hct, hcf] rw [ih (ones + 1) zeros] have h1 : ones + 1 + rest.count true = ones + (rest.count true + 1) := by omega have h2 : ones + 1 + zeros + 1 = ones + zeros + 2 := by omega have h3 : ones + 1 + zeros + rest.count true + rest.count false + 1 = ones + zeros + (rest.count true + 1) + rest.count false + 1 := by omega rw [h1, h2, h3] push_cast have hf1 : (Nat.factorial (ones + 1) : ℝ) = ((ones : ℝ) + 1) * (Nat.factorial ones : ℝ) := by rw [Nat.factorial_succ]; push_cast; ring have hf2 : (Nat.factorial (ones + zeros + 2) : ℝ) = ((ones : ℝ) + (zeros : ℝ) + 2) * (Nat.factorial (ones + zeros + 1) : ℝ) := by rw [show ones + zeros + 2 = (ones + zeros + 1) + 1 by omega, Nat.factorial_succ] push_cast; ring rw [hf1, hf2] have hne1 : (ones : ℝ) + 1 ≠ 0 := by positivity have hne2 : (ones : ℝ) + (zeros : ℝ) + 2 ≠ 0 := by positivity have hne3 : (Nat.factorial ones : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero ones have hne4 : (Nat.factorial zeros : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero zeros have hne5 : (Nat.factorial (ones + zeros + (rest.count true + 1) + rest.count false + 1) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ field_simp [hne1, hne2, hne3, hne4, hne5] | false => have hct : (false :: rest).count true = rest.count true := by simp have hcf : (false :: rest).count false = rest.count false + 1 := by simp simp only [laplaceSeqProb.go, hct, hcf] rw [ih ones (zeros + 1)] have h1 : zeros + 1 + rest.count false = zeros + (rest.count false + 1) := by omega have h2 : ones + (zeros + 1) + 1 = ones + zeros + 2 := by omega have h3 : ones + (zeros + 1) + rest.count true + rest.count false + 1 = ones + zeros + rest.count true + (rest.count false + 1) + 1 := by omega rw [h1, h2, h3] push_cast have hf1 : (Nat.factorial (zeros + 1) : ℝ) = ((zeros : ℝ) + 1) * (Nat.factorial zeros : ℝ) := by rw [Nat.factorial_succ]; push_cast; ring have hf2 : (Nat.factorial (ones + zeros + 2) : ℝ) = ((ones : ℝ) + (zeros : ℝ) + 2) * (Nat.factorial (ones + zeros + 1) : ℝ) := by rw [show ones + zeros + 2 = (ones + zeros + 1) + 1 by omega, Nat.factorial_succ] push_cast; ring rw [hf1, hf2] have hne1 : (zeros : ℝ) + 1 ≠ 0 := by positivity have hne2 : (ones : ℝ) + (zeros : ℝ) + 2 ≠ 0 := by positivity have hne3 : (Nat.factorial ones : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero ones have hne4 : (Nat.factorial zeros : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero zeros have hne5 : (Nat.factorial (ones + zeros + rest.count true + (rest.count false + 1) + 1) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ field_simp [hne1, hne2, hne3, hne4, hne5] /-- **Exact Closed Form of Laplace Sequential Predictor**: `laplaceSeqProb bs = 1 / ((|bs| + 1) * C(|bs|, k))`. -/ theorem laplace_seq_prob_exact (bs : List Bool) : laplaceSeqProb bs = pLaplace (bs.count true) bs.length := by have hsum_cnt : bs.count true + bs.count false = bs.length := by induction bs with | nil => simp | cons b rest ih => cases b <;> simp <;> omega have hle : bs.count true ≤ bs.length := by omega have hfalse : bs.count false = bs.length - bs.count true := by omega cases bs with | nil => simp [laplaceSeqProb, pLaplace] | cons b rest => simp only [laplaceSeqProb] rw [laplace_go_closed 0 0 (b :: rest)] simp only [zero_add, Nat.factorial_zero, Nat.factorial_one, one_mul, mul_one] rw [hsum_cnt, hfalse] simp only [pLaplace] have hch := Nat.choose_mul_factorial_mul_factorial hle have hch_r : (Nat.choose (b :: rest).length ((b :: rest).count true) : ℝ) * (Nat.factorial ((b :: rest).count true) : ℝ) * (Nat.factorial ((b :: rest).length - (b :: rest).count true) : ℝ) = (Nat.factorial (b :: rest).length : ℝ) := by exact_mod_cast hch have hfac : (Nat.factorial ((b :: rest).length + 1) : ℝ) = (((b :: rest).length + 1 : ℕ) : ℝ) * (Nat.factorial (b :: rest).length : ℝ) := by rw [Nat.factorial_succ]; push_cast; ring push_cast rw [hfac, ← hch_r] have hne1 : (((b :: rest).length : ℝ) + 1) ≠ 0 := by positivity have hne2 : (Nat.choose (b :: rest).length ((b :: rest).count true) : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (Nat.choose_pos hle) have hne3 : (Nat.factorial ((b :: rest).count true) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne4 : (Nat.factorial ((b :: rest).length - (b :: rest).count true) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ field_simp [hne1, hne2, hne3, hne4] push_cast rfl /-- **Exact Laplace Normalization & Sharp `ln(n + 1)` Pointwise Regret Bound** (attained with exact equality at `0^n` and `1^n`): 1. `∑_{x ∈ {0,1}^n} P_Lap(x) = 1`. 2. For EVERY `k ≤ n` (`n ≥ 1`): `P_ML(k, n) / P_Lap(k, n) ≤ n + 1` (so regret `≤ ln(n + 1)`). 3. Exact sharpness at `k = 0` and `k = n`: `P_ML(0, n) / P_Lap(0, n) = n + 1`. -/ theorem laplace_exact_regret_and_sharpness {n : ℕ} (hn : 1 ≤ n) : (∑ x : Fin n → Bool, pLaplace (onesCount x) n = 1) ∧ (∀ k ≤ n, pML k n / pLaplace k n ≤ (n + 1 : ℝ) ∧ -Real.log (pLaplace k n) - (-Real.log (pML k n)) ≤ Real.log (n + 1 : ℝ)) ∧ (pML 0 n / pLaplace 0 n = (n + 1 : ℝ) ∧ pML n n / pLaplace n n = (n + 1 : ℝ)) := by refine ⟨?_, ?_, ?_⟩ · have hmap : ∀ x ∈ (Finset.univ : Finset (Fin n → Bool)), onesCount x ∈ Finset.range (n + 1) := fun x _ => Finset.mem_range.mpr (Nat.lt_succ_of_le (onesCount_le x)) rw [← Finset.sum_fiberwise_of_maps_to hmap] have hterm : ∀ k ∈ Finset.range (n + 1), ∑ x ∈ Finset.univ.filter (fun x : Fin n → Bool => onesCount x = k), pLaplace (onesCount x) n = ((n + 1 : ℕ) : ℝ)⁻¹ := by intro k hk have hkle : k ≤ n := Nat.lt_succ_iff.mp (Finset.mem_range.mp hk) have hconst : ∑ x ∈ Finset.univ.filter (fun x : Fin n → Bool => onesCount x = k), pLaplace (onesCount x) n = (Nat.choose n k : ℝ) * pLaplace k n := by calc _ = ∑ _x ∈ Finset.univ.filter (fun x : Fin n → Bool => onesCount x = k), pLaplace k n := Finset.sum_congr rfl fun x hx => by rw [(Finset.mem_filter.mp hx).2] _ = (Nat.choose n k : ℝ) * pLaplace k n := by rw [Finset.sum_const, nsmul_eq_mul, card_onesCount_eq] rw [hconst, pLaplace] have hne : (Nat.choose n k : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (Nat.choose_pos hkle) have hne2 : ((n + 1 : ℕ) : ℝ) ≠ 0 := by positivity field_simp [hne, hne2] rw [Finset.sum_congr rfl hterm, Finset.sum_const, Finset.card_range, nsmul_eq_mul] have hne : ((n + 1 : ℕ) : ℝ) ≠ 0 := by positivity field_simp [hne] · intro k hk have hch_pos : (0 : ℝ) < Nat.choose n k := by exact_mod_cast Nat.choose_pos hk have hlap_pos : 0 < pLaplace k n := by simp only [pLaplace]; positivity have hml_pos := pML_pos hn hk -- Binomial theorem: `C(n, k) * (k/n)^k * ((n-k)/n)^(n-k) ≤ (k/n + (n-k)/n)^n = 1` have hbinom : (Nat.choose n k : ℝ) * pML k n ≤ 1 := by have h1 : (((k : ℝ) / (n : ℝ)) + (((n - k : ℕ) : ℝ) / (n : ℝ))) ^ n = 1 := by rw [← add_div] have hsum : (k : ℝ) + ((n - k : ℕ) : ℝ) = (n : ℝ) := by exact_mod_cast Nat.add_sub_of_le hk rw [hsum, div_self (by exact_mod_cast (by omega : n ≠ 0)), one_pow] rw [← h1, add_pow] have hmem : k ∈ Finset.range (n + 1) := Finset.mem_range.mpr (Nat.lt_succ_of_le hk) have hle := Finset.single_le_sum (f := fun m => ((k : ℝ) / (n : ℝ)) ^ m * (((n - k : ℕ) : ℝ) / (n : ℝ)) ^ (n - m) * (Nat.choose n m : ℝ)) (fun m _ => by positivity) hmem simpa [pML, mul_comm, mul_left_comm, mul_assoc] using hle have hratio : pML k n / pLaplace k n ≤ (n + 1 : ℝ) := by simp only [pLaplace, div_div_eq_mul_div, div_one, Nat.cast_add, Nat.cast_one] have : pML k n * (((n : ℝ) + 1) * (Nat.choose n k : ℝ)) = ((n : ℝ) + 1) * ((Nat.choose n k : ℝ) * pML k n) := by ring rw [this] nlinarith refine ⟨hratio, ?_⟩ have hlog := Real.log_le_log (div_pos hml_pos hlap_pos) hratio rw [Real.log_div (ne_of_gt hml_pos) (ne_of_gt hlap_pos)] at hlog linarith · simp only [pML_zero hn, pML_self hn, pLaplace, Nat.choose_zero_right, Nat.choose_self, Nat.cast_one, mul_one, one_div, div_inv_eq_mul, one_mul, Nat.cast_add, and_self] /-! ## Section 5: Exact Krichevsky–Trofimov (KT) Predictor, Unimodality & Shtarkov Sandwich -/ /-- Exact rational closed form of the Krichevsky–Trofimov (KT) sequential predictor: `P_KT(k, n) = (2k)! (2(n-k))! / (4^n k! (n-k)! n!)`. -/ noncomputable def pKT (k n : ℕ) : ℝ := ((Nat.factorial (2 * k) * Nat.factorial (2 * (n - k)) : ℕ) : ℝ) / (((4 : ℕ) ^ n * Nat.factorial k * Nat.factorial (n - k) * Nat.factorial n : ℕ) : ℝ) /-- Sequential KT predictor product with conditionals `(k + 1/2) / (t + 1)`. -/ noncomputable def ktSeqProb : List Bool → ℝ | [] => 1 | bs => let rec go (ones zeros : ℕ) : List Bool → ℝ | [] => 1 | true :: rest => ((2 * ones + 1 : ℕ) : ℝ) / ((2 * (ones + zeros + 1) : ℕ) : ℝ) * go (ones + 1) zeros rest | false :: rest => ((2 * zeros + 1 : ℕ) : ℝ) / ((2 * (ones + zeros + 1) : ℕ) : ℝ) * go ones (zeros + 1) rest go 0 0 bs private lemma double_fac_step (m : ℕ) : ((Nat.factorial (2 * (m + 1)) : ℕ) : ℝ) = ((2 * m + 1 : ℕ) : ℝ) * (2 : ℝ) * ((m + 1 : ℕ) : ℝ) * (Nat.factorial (2 * m) : ℝ) := by have h1 : 2 * (m + 1) = (2 * m + 1) + 1 := by omega rw [h1, Nat.factorial_succ, Nat.factorial_succ] push_cast ring private lemma kt_go_closed (ones zeros : ℕ) (bs : List Bool) : let o := bs.count true let z := bs.count false ktSeqProb.go ones zeros bs = ((Nat.factorial (2 * (ones + o)) * Nat.factorial (2 * (zeros + z)) * (4 ^ (ones + zeros) * Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros)) : ℕ) : ℝ) / ((Nat.factorial (2 * ones) * Nat.factorial (2 * zeros) * (4 ^ (ones + zeros + o + z) * Nat.factorial (ones + o) * Nat.factorial (zeros + z) * Nat.factorial (ones + zeros + o + z)) : ℕ) : ℝ) := by induction bs generalizing ones zeros with | nil => simp only [ktSeqProb.go, List.count_nil, add_zero] have hne : ((Nat.factorial (2 * ones) * Nat.factorial (2 * zeros) * (4 ^ (ones + zeros) * Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros)) : ℕ) : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (by positivity : 0 < Nat.factorial (2 * ones) * Nat.factorial (2 * zeros) * (4 ^ (ones + zeros) * Nat.factorial ones * Nat.factorial zeros * Nat.factorial (ones + zeros))) exact (div_self hne).symm | cons b rest ih => cases b with | true => have hct : (true :: rest).count true = rest.count true + 1 := by simp have hcf : (true :: rest).count false = rest.count false := by simp simp only [ktSeqProb.go, hct, hcf] rw [ih (ones + 1) zeros] have h1 : ones + 1 + rest.count true = ones + (rest.count true + 1) := by omega have h2 : ones + 1 + zeros = ones + zeros + 1 := by omega have h3 : ones + zeros + 1 + rest.count true + rest.count false = ones + zeros + (rest.count true + 1) + rest.count false := by omega rw [h1, h2, h3] push_cast have hf2k : (Nat.factorial (2 * (ones + 1)) : ℝ) = (2 * (ones : ℝ) + 1) * 2 * ((ones : ℝ) + 1) * (Nat.factorial (2 * ones) : ℝ) := by simpa [Nat.cast_add, Nat.cast_mul, Nat.cast_ofNat, Nat.cast_one] using double_fac_step ones rw [hf2k, Nat.factorial_succ ones, Nat.factorial_succ (ones + zeros), pow_succ] push_cast have hne1 : (2 * (ones : ℝ) + 1) ≠ 0 := by positivity have hne2 : ((ones : ℝ) + (zeros : ℝ) + 1) ≠ 0 := by positivity have hne3 : ((ones : ℝ) + 1) ≠ 0 := by positivity have hne4 : (Nat.factorial (2 * ones) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne5 : (Nat.factorial (2 * zeros) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne6 : (Nat.factorial (ones + (rest.count true + 1)) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne7 : (Nat.factorial (zeros + rest.count false) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne8 : (Nat.factorial (ones + zeros + (rest.count true + 1) + rest.count false) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne9 : (4 : ℝ) ^ (ones + zeros + (rest.count true + 1) + rest.count false) ≠ 0 := by positivity field_simp [hne1, hne2, hne3, hne4, hne5, hne6, hne7, hne8, hne9] ring | false => have hct : (false :: rest).count true = rest.count true := by simp have hcf : (false :: rest).count false = rest.count false + 1 := by simp simp only [ktSeqProb.go, hct, hcf] rw [ih ones (zeros + 1)] have h1 : zeros + 1 + rest.count false = zeros + (rest.count false + 1) := by omega have h2 : ones + (zeros + 1) = ones + zeros + 1 := by omega have h3 : ones + zeros + 1 + rest.count true + rest.count false = ones + zeros + rest.count true + (rest.count false + 1) := by omega rw [h1, h2, h3] push_cast have hf2z : (Nat.factorial (2 * (zeros + 1)) : ℝ) = (2 * (zeros : ℝ) + 1) * 2 * ((zeros : ℝ) + 1) * (Nat.factorial (2 * zeros) : ℝ) := by simpa [Nat.cast_add, Nat.cast_mul, Nat.cast_ofNat, Nat.cast_one] using double_fac_step zeros rw [hf2z, Nat.factorial_succ zeros, Nat.factorial_succ (ones + zeros), pow_succ] push_cast have hne1 : (2 * (zeros : ℝ) + 1) ≠ 0 := by positivity have hne2 : ((ones : ℝ) + (zeros : ℝ) + 1) ≠ 0 := by positivity have hne3 : ((zeros : ℝ) + 1) ≠ 0 := by positivity have hne4 : (Nat.factorial (2 * ones) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne5 : (Nat.factorial (2 * zeros) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne6 : (Nat.factorial (ones + rest.count true) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne7 : (Nat.factorial (zeros + (rest.count false + 1)) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne8 : (Nat.factorial (ones + zeros + rest.count true + (rest.count false + 1)) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne9 : (4 : ℝ) ^ (ones + zeros + rest.count true + (rest.count false + 1)) ≠ 0 := by positivity field_simp [hne1, hne2, hne3, hne4, hne5, hne6, hne7, hne8, hne9] ring /-- **Exact Closed Form of Krichevsky–Trofimov (KT) Sequential Predictor**: `ktSeqProb bs = (2k)! (2(n-k))! / (4^n k! (n-k)! n!)`. -/ theorem kt_seq_prob_exact (bs : List Bool) : ktSeqProb bs = pKT (bs.count true) bs.length := by have hsum_cnt : bs.count true + bs.count false = bs.length := by induction bs with | nil => simp | cons b rest ih => cases b <;> simp <;> omega have hfalse : bs.count false = bs.length - bs.count true := by omega cases bs with | nil => simp [ktSeqProb, pKT] | cons b rest => simp only [ktSeqProb] rw [kt_go_closed 0 0 (b :: rest)] simp only [zero_add, mul_zero, Nat.factorial_zero, pow_zero, one_mul, mul_one, hsum_cnt] rw [hfalse] rfl lemma pKT_pos (k n : ℕ) : 0 < pKT k n := by simp only [pKT] have h1 : 0 < Nat.factorial (2 * k) * Nat.factorial (2 * (n - k)) := by positivity have h2 : 0 < 4 ^ n * Nat.factorial k * Nat.factorial (n - k) * Nat.factorial n := by positivity exact div_pos (by exact_mod_cast h1) (by exact_mod_cast h2) lemma pKT_symm {k n : ℕ} (hk : k ≤ n) : pKT (n - k) n = pKT k n := by simp only [pKT, Nat.sub_sub_self hk] push_cast ring /-- Exact KT normalization: `∑_{x ∈ {0,1}^n} P_KT(x) = 1` (by sequential induction on `n`). -/ theorem sum_pKT_eq_one (n : ℕ) : ∑ x : Fin n → Bool, pKT (onesCount x) n = 1 := by have haux : ∀ (m : ℕ) (ones zeros : ℕ), ∑ x : Fin m → Bool, ktSeqProb.go ones zeros (List.ofFn x) = 1 := by intro m induction m with | zero => intro _ _; simp [ktSeqProb.go] | succ m ih => intro ones zeros let e : Bool × (Fin m → Bool) ≃ (Fin (m + 1) → Bool) := { toFun := fun p => Fin.cons p.1 p.2 invFun := fun x => (x 0, Fin.tail x) left_inv := fun ⟨b, x⟩ => by ext i <;> simp [Fin.tail, Fin.cons] right_inv := fun x => Fin.cons_self_tail x } rw [← Equiv.sum_comp e, Fintype.sum_prod_type, Fintype.sum_bool] have hcons : ∀ (b : Bool) (x : Fin m → Bool), List.ofFn (Fin.cons b x) = b :: List.ofFn x := fun b x => by rw [List.ofFn_succ] rfl simp only [e, Equiv.coe_fn_mk, hcons, ktSeqProb.go, ← Finset.mul_sum, ih, mul_one] have hne : ((2 * (ones + zeros + 1) : ℕ) : ℝ) ≠ 0 := by positivity rw [← add_div] have hnum : ((2 * ones + 1 : ℕ) : ℝ) + ((2 * zeros + 1 : ℕ) : ℝ) = ((2 * (ones + zeros + 1) : ℕ) : ℝ) := by push_cast; ring rw [hnum, div_self hne] have heq : ∀ x : Fin n → Bool, pKT (onesCount x) n = ktSeqProb.go 0 0 (List.ofFn x) := by intro x have hcnt : (List.ofFn x).count true = onesCount x := by clear haux induction n with | zero => simp [onesCount] | succ m ihm => rw [List.ofFn_succ] have hstep : onesCount x = (if x 0 = true then 1 else 0) + onesCount (fun i : Fin m => x i.succ) := by simp only [onesCount, Finset.card_filter] rw [Fin.sum_univ_succ] rw [hstep, ← ihm (fun i => x i.succ)] cases h0 : x 0 <;> simp [Nat.add_comm] have hlen : (List.ofFn x).length = n := List.length_ofFn have hkt := kt_seq_prob_exact (List.ofFn x) rw [hcnt, hlen] at hkt cases hlist : List.ofFn x with | nil => have hn0 : n = 0 := by rw [← hlen, hlist]; rfl subst hn0; simp [pKT, ktSeqProb.go, onesCount] | cons b rest => rw [hlist] at hkt simpa [ktSeqProb] using hkt.symm simp_rw [heq] exact haux n 0 0 /-- Exact KT boundary value at `k = 0`: `P_KT(0, n) = C(2n, n) / 4^n`. -/ lemma pKT_zero (n : ℕ) : pKT 0 n = (Nat.choose (2 * n) n : ℝ) / (4 : ℝ) ^ n := by simp only [pKT, mul_zero, Nat.sub_zero, Nat.factorial_zero, one_mul, mul_one] have hch := Nat.choose_mul_factorial_mul_factorial (show n ≤ 2 * n by omega) have hsub : 2 * n - n = n := by omega rw [hsub] at hch have hch_r : (Nat.choose (2 * n) n : ℝ) * (Nat.factorial n : ℝ) * (Nat.factorial n : ℝ) = (Nat.factorial (2 * n) : ℝ) := by exact_mod_cast hch push_cast rw [← hch_r] have hne1 : (Nat.factorial n : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero n have hne2 : (4 : ℝ) ^ n ≠ 0 := by positivity field_simp [hne1, hne2] /-- Exact KT central value at `n = 2m, k = m`: `P_KT(m, 2m) = C(2m, m) / 4^(2m)`. -/ lemma pKT_half_even (m : ℕ) : pKT m (2 * m) = (Nat.choose (2 * m) m : ℝ) / (4 : ℝ) ^ (2 * m) := by simp only [pKT, show 2 * m - m = m by omega] have hch := Nat.choose_mul_factorial_mul_factorial (show m ≤ 2 * m by omega) have hsub : 2 * m - m = m := by omega rw [hsub] at hch have hch_r : (Nat.choose (2 * m) m : ℝ) * (Nat.factorial m : ℝ) * (Nat.factorial m : ℝ) = (Nat.factorial (2 * m) : ℝ) := by exact_mod_cast hch push_cast rw [← hch_r] have hne1 : (Nat.factorial m : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero m have hne2 : (Nat.choose (2 * m) m : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (Nat.choose_pos (show m ≤ 2 * m by omega)) have hne3 : (4 : ℝ) ^ (2 * m) ≠ 0 := by positivity field_simp [hne1, hne2, hne3] /-- Exact KT central value at `n = 2m + 1, k = m`: `P_KT(m, 2m + 1) = C(2m, m) / (2 * 4^(2m))`. -/ lemma pKT_half_odd (m : ℕ) : pKT m (2 * m + 1) = (Nat.choose (2 * m) m : ℝ) / (2 * (4 : ℝ) ^ (2 * m)) := by simp only [pKT, show 2 * m + 1 - m = m + 1 by omega] have hch := Nat.choose_mul_factorial_mul_factorial (show m ≤ 2 * m by omega) have hsub : 2 * m - m = m := by omega rw [hsub] at hch have hch_r : (Nat.choose (2 * m) m : ℝ) * (Nat.factorial m : ℝ) * (Nat.factorial m : ℝ) = (Nat.factorial (2 * m) : ℝ) := by exact_mod_cast hch have hf1 : (Nat.factorial (2 * (m + 1)) : ℝ) = (2 * ((m : ℝ) + 1)) * (Nat.factorial (2 * m + 1) : ℝ) := by rw [show 2 * (m + 1) = (2 * m + 1) + 1 by omega, Nat.factorial_succ] push_cast; ring have hf2 : (Nat.factorial (m + 1) : ℝ) = ((m : ℝ) + 1) * (Nat.factorial m : ℝ) := by rw [Nat.factorial_succ]; push_cast; ring push_cast rw [hf1, hf2, pow_succ, ← hch_r] have hne1 : (Nat.factorial m : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero m have hne2 : (Nat.factorial (2 * m + 1) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have hne3 : ((m : ℝ) + 1) ≠ 0 := by positivity have hne4 : (4 : ℝ) ^ (2 * m) ≠ 0 := by positivity field_simp [hne1, hne2, hne3, hne4] ring /-- Elementary central binomial two-sided `ℕ` inequalities (zero Stirling needed!): for all `n ≥ 1`, `(3 * n + 1) * C(2n, n)^2 ≤ (4^n)^2 ≤ (4 * n) * C(2n, n)^2`. -/ theorem centralBinom_nat_sandwich (n : ℕ) (hn : 1 ≤ n) : (3 * n + 1) * (Nat.choose (2 * n) n) ^ 2 ≤ (4 ^ n) ^ 2 ∧ (4 ^ n) ^ 2 ≤ (4 * n) * (Nat.choose (2 * n) n) ^ 2 := by induction n, hn using Nat.le_induction with | base => norm_num [Nat.choose] | succ k hk ih => -- Exact recurrence: `(k + 1) * C(2k + 2, k + 1) = 2 * (2k + 1) * C(2k, k)` have hrec : (k + 1) * Nat.choose (2 * (k + 1)) (k + 1) = 2 * (2 * k + 1) * Nat.choose (2 * k) k := by have h1 := Nat.choose_mul_factorial_mul_factorial (show k + 1 ≤ 2 * (k + 1) by omega) have h2 := Nat.choose_mul_factorial_mul_factorial (show k ≤ 2 * k by omega) rw [show 2 * (k + 1) - (k + 1) = k + 1 by omega] at h1 rw [show 2 * k - k = k by omega] at h2 have hfac1 : Nat.factorial (2 * (k + 1)) = (2 * k + 2) * (2 * k + 1) * Nat.factorial (2 * k) := by rw [show 2 * (k + 1) = (2 * k + 1) + 1 by omega, Nat.factorial_succ, Nat.factorial_succ] ring have hfac2 : Nat.factorial (k + 1) = (k + 1) * Nat.factorial k := Nat.factorial_succ k have hmul : (Nat.factorial k * Nat.factorial k * (k + 1)) * ((k + 1) * Nat.choose (2 * (k + 1)) (k + 1)) = (Nat.factorial k * Nat.factorial k * (k + 1)) * (2 * (2 * k + 1) * Nat.choose (2 * k) k) := by calc (Nat.factorial k * Nat.factorial k * (k + 1)) * ((k + 1) * Nat.choose (2 * (k + 1)) (k + 1)) = Nat.choose (2 * (k + 1)) (k + 1) * Nat.factorial (k + 1) * Nat.factorial (k + 1) := by rw [hfac2]; ring _ = Nat.factorial (2 * (k + 1)) := h1 _ = (2 * k + 2) * (2 * k + 1) * Nat.factorial (2 * k) := hfac1 _ = (2 * k + 2) * (2 * k + 1) * (Nat.choose (2 * k) k * Nat.factorial k * Nat.factorial k) := by rw [h2] _ = (Nat.factorial k * Nat.factorial k * (k + 1)) * (2 * (2 * k + 1) * Nat.choose (2 * k) k) := by ring exact Nat.eq_of_mul_eq_mul_left (by positivity) hmul have hrec_sq : (k + 1) ^ 2 * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2 = 4 * (2 * k + 1) ^ 2 * (Nat.choose (2 * k) k) ^ 2 := by have := congrArg (· ^ 2) hrec nlinarith have hpow : (4 ^ (k + 1)) ^ 2 = 16 * (4 ^ k) ^ 2 := by rw [pow_succ]; ring have hpos : 0 < (k + 1) ^ 2 := by positivity constructor · -- `(3k + 4) * C(2k+2, k+1)^2 ≤ 16 * (4^k)^2` -- Multiply by `(k + 1)^2`: suffices `(3k + 4) * 4 * (2k + 1)^2 ≤ 16 * (k + 1)^2 * (3k + 1)` have hpoly : (3 * (k + 1) + 1) * (4 * (2 * k + 1) ^ 2) ≤ 16 * (k + 1) ^ 2 * (3 * k + 1) := by nlinarith have h1 : (k + 1) ^ 2 * ((3 * (k + 1) + 1) * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) ≤ (k + 1) ^ 2 * (4 ^ (k + 1)) ^ 2 := by calc (k + 1) ^ 2 * ((3 * (k + 1) + 1) * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) = (3 * (k + 1) + 1) * ((k + 1) ^ 2 * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) := by ring _ = (3 * (k + 1) + 1) * (4 * (2 * k + 1) ^ 2) * (Nat.choose (2 * k) k) ^ 2 := by rw [hrec_sq]; ring _ ≤ 16 * (k + 1) ^ 2 * (3 * k + 1) * (Nat.choose (2 * k) k) ^ 2 := Nat.mul_le_mul_right _ hpoly _ = (k + 1) ^ 2 * 16 * ((3 * k + 1) * (Nat.choose (2 * k) k) ^ 2) := by ring _ ≤ (k + 1) ^ 2 * 16 * (4 ^ k) ^ 2 := Nat.mul_le_mul_left _ ih.1 _ = (k + 1) ^ 2 * (4 ^ (k + 1)) ^ 2 := by rw [hpow]; ring exact Nat.le_of_mul_le_mul_left h1 hpos · -- `16 * (4^k)^2 ≤ 4(k+1) * C(2k+2, k+1)^2` -- Multiply by `(k+1)^2`: suffices `16 * (k+1)^2 * (4k) ≤ 4*(k+1) * 4*(2k+1)^2` -- i.e. `k * (k + 1) ≤ (2k + 1)^2`, trivial! have hpoly : 16 * (k + 1) ^ 2 * (4 * k) ≤ 4 * (k + 1) * (4 * (2 * k + 1) ^ 2) := by nlinarith have h1 : (k + 1) ^ 2 * (4 ^ (k + 1)) ^ 2 ≤ (k + 1) ^ 2 * (4 * (k + 1) * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) := by calc (k + 1) ^ 2 * (4 ^ (k + 1)) ^ 2 = (k + 1) ^ 2 * 16 * (4 ^ k) ^ 2 := by rw [hpow]; ring _ ≤ (k + 1) ^ 2 * 16 * (4 * k * (Nat.choose (2 * k) k) ^ 2) := Nat.mul_le_mul_left _ ih.2 _ = 16 * (k + 1) ^ 2 * (4 * k) * (Nat.choose (2 * k) k) ^ 2 := by ring _ ≤ 4 * (k + 1) * (4 * (2 * k + 1) ^ 2) * (Nat.choose (2 * k) k) ^ 2 := Nat.mul_le_mul_right _ hpoly _ = 4 * (k + 1) * ((k + 1) ^ 2 * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) := by rw [hrec_sq]; ring _ = (k + 1) ^ 2 * (4 * (k + 1) * (Nat.choose (2 * (k + 1)) (k + 1)) ^ 2) := by ring exact Nat.le_of_mul_le_mul_left h1 hpos /-- Helper: `ln((1 + u) / (1 - u)) ≥ 2 u` for all `u ∈ [0, 1)`. -/ private lemma log_one_add_div_one_sub_ge {u : ℝ} (hu0 : 0 ≤ u) (hu1 : u < 1) : 2 * u ≤ Real.log ((1 + u) / (1 - u)) := by let g : ℝ → ℝ := fun t => Real.log ((1 + t) / (1 - t)) - 2 * t have hg_deriv : ∀ t ∈ Set.Icc (0 : ℝ) u, HasDerivAt g (2 * t ^ 2 / (1 - t ^ 2)) t := by intro t ht have h1p : 0 < 1 + t := by linarith [ht.1] have h1m : 0 < 1 - t := by linarith [ht.2] have hq : HasDerivAt (fun t => (1 + t) / (1 - t)) (2 / (1 - t) ^ 2) t := by have ha : HasDerivAt (fun t => 1 + t) 1 t := by simpa using (hasDerivAt_id t).const_add (1 : ℝ) have hb : HasDerivAt (fun t => 1 - t) (-1) t := by simpa using (hasDerivAt_id t).const_sub (1 : ℝ) have hdiv := ha.div hb (ne_of_gt h1m) have hval : (1 * (1 - t) - (1 + t) * -1) / (1 - t) ^ 2 = 2 / (1 - t) ^ 2 := by ring rwa [hval] at hdiv have hlog := hq.log (ne_of_gt (div_pos h1p h1m)) have hlin : HasDerivAt (fun t => 2 * t) 2 t := by simpa using (hasDerivAt_id t).const_mul 2 have hsub := hlog.sub hlin have h1m2 : 1 - t ^ 2 ≠ 0 := by nlinarith [ht.1, ht.2] have hval : 2 / (1 - t) ^ 2 / ((1 + t) / (1 - t)) - 2 = 2 * t ^ 2 / (1 - t ^ 2) := by field_simp [ne_of_gt h1p, ne_of_gt h1m, h1m2] ring rwa [hval] at hsub have hg_mono : MonotoneOn g (Set.Icc 0 u) := by apply monotoneOn_of_deriv_nonneg (convex_Icc 0 u) · exact fun t ht => (hg_deriv t ht).continuousAt.continuousWithinAt · exact fun t ht => (hg_deriv t (interior_subset ht)).differentiableAt.differentiableWithinAt · intro t ht have htI := interior_subset ht rw [(hg_deriv t htI).deriv] have h1m2 : 0 < 1 - t ^ 2 := by nlinarith [htI.1, htI.2] positivity have h0 : g 0 = 0 := by simp [g] have hgu : 0 ≤ g u := by rw [← h0]; exact hg_mono ⟨le_rfl, hu0⟩ ⟨hu0, le_rfl⟩ hu0 simp only [g] at hgu linarith /-- Key monotonicity lemma: `m ↦ (m + 1)^(m + 1) / (m^m * (2m + 1))` is monotone increasing on `ℕ`, proved cleanly via `f'(x) = ln((1+u)/(1-u)) - 2u ≥ 0` (`u = 1/(2x+1)`). -/ private lemma kt_step_ratio_mono {a b : ℕ} (hab : a ≤ b) : ((a + 1 : ℝ) ^ (a + 1)) / ((a : ℝ) ^ a * (2 * (a : ℝ) + 1)) ≤ ((b + 1 : ℝ) ^ (b + 1)) / ((b : ℝ) ^ b * (2 * (b : ℝ) + 1)) := by suffices hstep : ∀ m : ℕ, ((m + 1 : ℝ) ^ (m + 1)) / ((m : ℝ) ^ m * (2 * (m : ℝ) + 1)) ≤ ((m + 2 : ℝ) ^ (m + 2)) / ((m + 1 : ℝ) ^ (m + 1) * (2 * (m : ℝ) + 3)) by induction hab with | refl => exact le_rfl | @step m h ih => refine le_trans ih ?_ have hs := hstep m have hcast : ((m + 1 : ℕ) : ℝ) = (m : ℝ) + 1 := by push_cast; ring rw [hcast] have h1 : (m : ℝ) + 1 + 1 = (m : ℝ) + 2 := by ring have h3 : 2 * ((m : ℝ) + 1) + 1 = 2 * (m : ℝ) + 3 := by ring rw [h1, h3] exact hs intro m cases m with | zero => norm_num | succ m' => set x : ℝ := ((m' + 1 : ℕ) : ℝ) have hx : 1 ≤ x := by simp only [x]; exact_mod_cast Nat.succ_le_succ (Nat.zero_le m') have hx0 : 0 < x := by linarith -- Prove `ln h(x) ≤ ln h(x + 1)` where `f(t) = (t + 1) ln(t + 1) - t ln t - ln(2t + 1)` let f : ℝ → ℝ := fun t => (t + 1) * Real.log (t + 1) - t * Real.log t - Real.log (2 * t + 1) have hf_deriv : ∀ t ∈ Set.Icc x (x + 1), HasDerivAt f (Real.log ((t + 1) / t) - 2 / (2 * t + 1)) t := by intro t ht have ht0 : 0 < t := by linarith [ht.1] have ht1 : 0 < t + 1 := by linarith have ht2 : 0 < 2 * t + 1 := by linarith have ha : HasDerivAt (fun t => (t + 1) * Real.log (t + 1)) (Real.log (t + 1) + 1) t := by have h1 : HasDerivAt (fun t => t + 1) 1 t := by simpa using (hasDerivAt_id t).add_const (1 : ℝ) have hm := h1.mul (h1.log (ne_of_gt ht1)) have hval : 1 * Real.log (t + 1) + (t + 1) * (1 / (t + 1)) = Real.log (t + 1) + 1 := by field_simp [ne_of_gt ht1] rwa [hval] at hm have hb : HasDerivAt (fun t => t * Real.log t) (Real.log t + 1) t := by have hm : HasDerivAt (fun t => t * Real.log t) (1 * Real.log t + t * t⁻¹) t := (hasDerivAt_id t).mul (Real.hasDerivAt_log (ne_of_gt ht0)) have hval : 1 * Real.log t + t * t⁻¹ = Real.log t + 1 := by field_simp [ne_of_gt ht0] rwa [hval] at hm have hc : HasDerivAt (fun t => Real.log (2 * t + 1)) (2 / (2 * t + 1)) t := by have h2 : HasDerivAt (fun t => 2 * t + 1) 2 t := by simpa using ((hasDerivAt_id t).const_mul 2).add_const (1 : ℝ) exact h2.log (ne_of_gt ht2) have hsub := (ha.sub hb).sub hc have hval : Real.log (t + 1) + 1 - (Real.log t + 1) - 2 / (2 * t + 1) = Real.log ((t + 1) / t) - 2 / (2 * t + 1) := by rw [Real.log_div (ne_of_gt ht1) (ne_of_gt ht0)]; ring rwa [hval] at hsub have hf_mono : MonotoneOn f (Set.Icc x (x + 1)) := by apply monotoneOn_of_deriv_nonneg (convex_Icc x (x + 1)) · exact fun t ht => (hf_deriv t ht).continuousAt.continuousWithinAt · exact fun t ht => (hf_deriv t (interior_subset ht)).differentiableAt.differentiableWithinAt · intro t ht have htI := interior_subset ht rw [(hf_deriv t htI).deriv] have ht0 : 0 < t := by linarith [htI.1] set u : ℝ := 1 / (2 * t + 1) have hu0 : 0 ≤ u := by positivity have hu1 : u < 1 := by simp only [u] rw [div_lt_one (by linarith)] linarith have hrat : (1 + u) / (1 - u) = (t + 1) / t := by simp only [u] have h2t : 2 * t + 1 ≠ 0 := by linarith field_simp [h2t, ne_of_gt ht0] ring have hge := log_one_add_div_one_sub_ge hu0 hu1 rw [hrat] at hge have h2u : 2 * u = 2 / (2 * t + 1) := by simp only [u]; ring linarith have hfx : f x ≤ f (x + 1) := hf_mono ⟨le_rfl, by linarith⟩ ⟨by linarith, le_rfl⟩ (by linarith) have hexp := Real.exp_le_exp.mpr hfx have hexp_eq : ∀ k : ℕ, 1 ≤ k → Real.exp (f (k : ℝ)) = ((k + 1 : ℝ) ^ (k + 1)) / ((k : ℝ) ^ k * (2 * (k : ℝ) + 1)) := by intro k hk have hk0 : (0 : ℝ) < k := by exact_mod_cast hk have hk1 : (0 : ℝ) < (k : ℝ) + 1 := by linarith have hk2 : (0 : ℝ) < 2 * (k : ℝ) + 1 := by linarith simp only [f] rw [Real.exp_sub, Real.exp_sub, Real.exp_log hk2] have h1 : Real.exp (((k : ℝ) + 1) * Real.log ((k : ℝ) + 1)) = ((k : ℝ) + 1) ^ (k + 1) := by rw [show ((k : ℝ) + 1) = ((k + 1 : ℕ) : ℝ) by push_cast; ring, Real.exp_nat_mul, Real.exp_log (by exact_mod_cast (by positivity : 0 < k + 1))] have h2 : Real.exp ((k : ℝ) * Real.log (k : ℝ)) = (k : ℝ) ^ k := by rw [Real.exp_nat_mul, Real.exp_log hk0] rw [h1, h2, div_div] have hL := hexp_eq (m' + 1) (Nat.succ_le_succ (Nat.zero_le m')) have hR := hexp_eq (m' + 2) (by omega) have hx_eq : x = ((m' + 1 : ℕ) : ℝ) := rfl have hx1_eq : x + 1 = ((m' + 2 : ℕ) : ℝ) := by simp [x]; ring rw [hx_eq, hL] at hexp rw [hx1_eq, hR] at hexp have hgoalL : ((m' + 2 : ℕ) : ℝ) = ((m' + 1 : ℕ) : ℝ) + 1 := by push_cast; ring have hgoalR : (((m' + 2 : ℕ) : ℝ) + 1) ^ (m' + 2 + 1) / (((m' + 2 : ℕ) : ℝ) ^ (m' + 2) * (2 * ((m' + 2 : ℕ) : ℝ) + 1)) = (((m' + 1 : ℕ) : ℝ) + 2) ^ (m' + 1 + 2) / ((((m' + 1 : ℕ) : ℝ) + 1) ^ (m' + 1 + 1) * (2 * ((m' + 1 : ℕ) : ℝ) + 3)) := by push_cast; ring rw [hgoalR, hgoalL] at hexp exact hexp /-- **Exact Unimodality of KT Regret Ratio** `R_KT(k, n) = P_ML(k, n) / P_KT(k, n)`: strictly decreasing on `0 ≤ k ≤ ⌊n/2⌋` and symmetric (`R_KT(n - k, n) = R_KT(k, n)`), so its global maximum over ALL `x ∈ {0, 1}^n` is at `k = 0, n` (`4^n / C(2n, n)`) and its global minimum is at `k = ⌊n/2⌋`! -/ theorem kt_regret_ratio_unimodal {n : ℕ} (hn : 1 ≤ n) : (∀ k : ℕ, k + 1 ≤ n - k → pML (k + 1) n / pKT (k + 1) n ≤ pML k n / pKT k n) ∧ (∀ k ≤ n, pML (n / 2) n / pKT (n / 2) n ≤ pML k n / pKT k n ∧ pML k n / pKT k n ≤ pML 0 n / pKT 0 n) := by have hstep : ∀ k : ℕ, k + 1 ≤ n - k → pML (k + 1) n / pKT (k + 1) n ≤ pML k n / pKT k n := by intro k hk have hkn : k < n := by omega have hnk1 : 1 ≤ n - k := by omega set m := n - k - 1 have hm : n - k = m + 1 := by omega have hkm : k ≤ m := by omega have hmono := kt_step_ratio_mono hkm -- Rewrite the ratio `pKT (k+1) n / pKT k n = (2k + 1) / (2m + 1)` have hkt_ratio : pKT (k + 1) n / pKT k n = (2 * (k : ℝ) + 1) / (2 * (m : ℝ) + 1) := by simp only [pKT, show n - (k + 1) = m by omega, hm] push_cast rw [double_fac_step k, double_fac_step m, Nat.factorial_succ k, Nat.factorial_succ m] push_cast have h1 : (4 : ℝ) ^ n ≠ 0 := by positivity have h2 : (Nat.factorial k : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero k have h3 : (Nat.factorial m : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero m have h4 : (Nat.factorial n : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero n have h5 : (Nat.factorial (2 * k) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have h6 : (Nat.factorial (2 * m) : ℝ) ≠ 0 := by exact_mod_cast Nat.factorial_ne_zero _ have h7 : (k : ℝ) + 1 ≠ 0 := by positivity have h8 : (m : ℝ) + 1 ≠ 0 := by positivity have h9 : 2 * (m : ℝ) + 1 ≠ 0 := by positivity field_simp [h1, h2, h3, h4, h5, h6, h7, h8, h9] -- Rewrite `pML (k+1) n / pML k n = ((k+1)^(k+1) / k^k) / ((m+1)^(m+1) / m^m)` have hml_ratio : pML (k + 1) n / pML k n = (((k + 1 : ℝ) ^ (k + 1)) / (k : ℝ) ^ k) / (((m + 1 : ℝ) ^ (m + 1)) / (m : ℝ) ^ m) := by simp only [pML, show n - (k + 1) = m by omega, hm] rw [div_pow, div_pow, div_pow, div_pow] have hn0 : (n : ℝ) ≠ 0 := by exact_mod_cast (by omega : n ≠ 0) have hpow_eq : (n : ℝ) ^ (k + 1) * (n : ℝ) ^ m = (n : ℝ) ^ k * (n : ℝ) ^ (m + 1) := by rw [← pow_add, ← pow_add]; congr 1; omega have hkk : (k : ℝ) ^ k ≠ 0 := by rcases Nat.eq_zero_or_pos k with rfl | hpos · simp · exact pow_ne_zero _ (by exact_mod_cast Nat.ne_of_gt hpos) have hmm : (m : ℝ) ^ m ≠ 0 := by rcases Nat.eq_zero_or_pos m with hm0 | hpos · simp [hm0] · exact pow_ne_zero _ (by exact_mod_cast Nat.ne_of_gt hpos) have hk1 : ((k : ℝ) + 1) ^ (k + 1) ≠ 0 := by positivity have hm1 : ((m : ℝ) + 1) ^ (m + 1) ≠ 0 := by positivity push_cast field_simp [hn0, hkk, hmm, hk1, hm1] exact hpow_eq.symm -- We want `pML (k+1) n / pML k n ≤ pKT (k+1) n / pKT k n` have hle_ratio : pML (k + 1) n / pML k n ≤ pKT (k + 1) n / pKT k n := by rw [hml_ratio, hkt_ratio] have hpos1 : 0 < (k : ℝ) ^ k := by rcases Nat.eq_zero_or_pos k with rfl | hpos · simp · exact pow_pos (by exact_mod_cast hpos) k have hpos2 : 0 < (m : ℝ) ^ m := by rcases Nat.eq_zero_or_pos m with hm0 | hpos · simp [hm0] · exact pow_pos (by exact_mod_cast hpos) m have hpos3 : 0 < ((m + 1 : ℝ) ^ (m + 1)) := by positivity have hpos4 : 0 < 2 * (k : ℝ) + 1 := by positivity have hpos5 : 0 < 2 * (m : ℝ) + 1 := by positivity rw [div_le_div_iff₀ (div_pos hpos3 hpos2) hpos5] rw [div_le_div_iff₀ (mul_pos hpos1 hpos4) (mul_pos hpos2 hpos5)] at hmono have hL : (k + 1 : ℝ) ^ (k + 1) / (k : ℝ) ^ k * (2 * (m : ℝ) + 1) = ((k + 1 : ℝ) ^ (k + 1) * (2 * (m : ℝ) + 1)) / (k : ℝ) ^ k := by ring have hR : (2 * (k : ℝ) + 1) * ((m + 1 : ℝ) ^ (m + 1) / (m : ℝ) ^ m) = ((m + 1 : ℝ) ^ (m + 1) * (2 * (k : ℝ) + 1)) / (m : ℝ) ^ m := by ring rw [hL, hR, div_le_div_iff₀ hpos1 hpos2] linarith have hpk := pKT_pos k n have hpk1 := pKT_pos (k + 1) n have hmlk := pML_pos hn (le_of_lt hkn) rw [div_le_div_iff₀ hpk1 hpk] rw [div_le_div_iff₀ hmlk hpk] at hle_ratio linarith have hanti : ∀ a b : ℕ, a ≤ b → b ≤ n / 2 → pML b n / pKT b n ≤ pML a n / pKT a n := by intro a b hab hb induction hab with | refl => exact le_rfl | step h ih => refine le_trans (hstep _ ?_) (ih (by omega)) omega refine ⟨hstep, fun k hk => ?_⟩ by_cases hhalf : k ≤ n / 2 · exact ⟨hanti k (n / 2) hhalf le_rfl, hanti 0 k (Nat.zero_le k) hhalf⟩ · have hsymm : n - k ≤ n / 2 := by omega rw [← pML_symm hk, ← pKT_symm hk] exact ⟨hanti (n - k) (n / 2) hsymm le_rfl, hanti 0 (n - k) (Nat.zero_le _) hsymm⟩ /-- **Exact Non-Asymptotic Krichevsky–Trofimov Regret Bound & Strict Dominance Over Laplace**: For EVERY `n ≥ 1` and EVERY `k ≤ n`: 1. `P_ML(k, n) / P_KT(k, n) ≤ 4^n / C(2n, n) ≤ 2 √n`, hence `-ln P_KT(x) - (-ln P_ML(x)) ≤ (1/2) ln n + ln 2` (exact equality at `n = 1`!). 2. **Strict Dominance Over Laplace** for EVERY `n ≥ 2`: `2 √n < n + 1`, so KT's worst-case regret is strictly smaller than Laplace's `ln(n + 1)`. 3. **Exact Two-Sided Non-Asymptotic Binomial Shtarkov Sandwich** (all `n ≥ 1`): `√(n + 1) ≤ S_Bern(n) ≤ 2 √n`, i.e., `(1/2) ln(n + 1) ≤ ln S_Bern(n) ≤ (1/2) ln n + ln 2`! -/ theorem kt_exact_regret_and_shtarkov_sandwich {n : ℕ} (hn : 1 ≤ n) : (∀ k ≤ n, pML k n / pKT k n ≤ 2 * Real.sqrt n ∧ -Real.log (pKT k n) - (-Real.log (pML k n)) ≤ (1 / 2 : ℝ) * Real.log n + Real.log 2) ∧ (2 ≤ n → (1 / 2 : ℝ) * Real.log n + Real.log 2 < Real.log (n + 1 : ℝ)) ∧ (Real.sqrt (n + 1 : ℝ) ≤ shtarkovBern n ∧ shtarkovBern n ≤ 2 * Real.sqrt n) ∧ ((1 / 2 : ℝ) * Real.log (n + 1 : ℝ) ≤ Real.log (shtarkovBern n) ∧ Real.log (shtarkovBern n) ≤ (1 / 2 : ℝ) * Real.log n + Real.log 2) := by have hn_pos : (0 : ℝ) < n := by exact_mod_cast hn have hcb := centralBinom_nat_sandwich n hn -- Step 1: Upper bound at `k = 0`: `pML 0 n / pKT 0 n = 4^n / C(2n, n) ≤ 2 √n` have hmax_le : pML 0 n / pKT 0 n ≤ 2 * Real.sqrt n := by rw [pML_zero hn, pKT_zero n, one_div_div] have hch_pos : (0 : ℝ) < Nat.choose (2 * n) n := by exact_mod_cast Nat.choose_pos (show n ≤ 2 * n by omega) rw [div_le_iff₀ hch_pos] have hsq_r : ((4 : ℝ) ^ n) ^ 2 ≤ (2 * Real.sqrt n * (Nat.choose (2 * n) n : ℝ)) ^ 2 := by rw [mul_pow, mul_pow, Real.sq_sqrt (le_of_lt hn_pos)] have h2 : ((4 ^ n : ℕ) : ℝ) ^ 2 ≤ ((4 * n : ℕ) : ℝ) * ((Nat.choose (2 * n) n : ℕ) : ℝ) ^ 2 := by exact_mod_cast hcb.2 push_cast at h2 nlinarith have h1 := le_of_sq_le_sq hsq_r (by positivity) exact h1 -- Step 2: Lower bound at `k = n / 2`: `√(n + 1) ≤ pML (n / 2) n / pKT (n / 2) n` have hmin_ge : Real.sqrt (n + 1 : ℝ) ≤ pML (n / 2) n / pKT (n / 2) n := by rcases Nat.even_or_odd' n with ⟨m, rfl | rfl⟩ · -- Even case `n = 2m` (`m ≥ 1`) have hm : 1 ≤ m := by omega have hdiv : 2 * m / 2 = m := by omega rw [hdiv, pKT_half_even m] have hml_even : pML m (2 * m) = 1 / (4 : ℝ) ^ m := by simp only [pML, show 2 * m - m = m by omega] have hm0 : (m : ℝ) ≠ 0 := by exact_mod_cast (by omega : m ≠ 0) have hrat : (m : ℝ) / ((2 * m : ℕ) : ℝ) = 1 / 2 := by push_cast; field_simp [hm0] rw [hrat, ← mul_pow, show (1 / 2 : ℝ) * (1 / 2 : ℝ) = 1 / 4 by norm_num, one_div, inv_pow, one_div] rw [hml_even] have hsimp : (1 / (4 : ℝ) ^ m) / ((Nat.choose (2 * m) m : ℝ) / (4 : ℝ) ^ (2 * m)) = (4 : ℝ) ^ m / (Nat.choose (2 * m) m : ℝ) := by have h1 : (4 : ℝ) ^ (2 * m) = (4 : ℝ) ^ m * (4 : ℝ) ^ m := by rw [← pow_add]; congr 1; omega have hne1 : (4 : ℝ) ^ m ≠ 0 := by positivity have hne2 : (Nat.choose (2 * m) m : ℝ) ≠ 0 := by exact_mod_cast Nat.ne_of_gt (Nat.choose_pos (show m ≤ 2 * m by omega)) rw [h1]; field_simp [hne1, hne2] rw [hsimp] have hcb_m := (centralBinom_nat_sandwich m hm).1 have hch_pos : (0 : ℝ) < Nat.choose (2 * m) m := by exact_mod_cast Nat.choose_pos (show m ≤ 2 * m by omega) rw [le_div_iff₀ hch_pos] have hsq : (Real.sqrt ((2 * m : ℕ) + 1 : ℝ) * (Nat.choose (2 * m) m : ℝ)) ^ 2 ≤ ((4 : ℝ) ^ m) ^ 2 := by rw [mul_pow, Real.sq_sqrt (by positivity)] have h1 : ((3 * m + 1 : ℕ) : ℝ) * (Nat.choose (2 * m) m : ℝ) ^ 2 ≤ ((4 : ℝ) ^ m) ^ 2 := by exact_mod_cast hcb_m have h2 : ((2 * m : ℕ) + 1 : ℝ) ≤ ((3 * m + 1 : ℕ) : ℝ) := by push_cast; linarith nlinarith [sq_nonneg (Nat.choose (2 * m) m : ℝ)] exact le_of_sq_le_sq hsq (by positivity) · -- Odd case `n = 2m + 1` have hdiv : (2 * m + 1) / 2 = m := by omega rw [hdiv] cases m with | zero => simp only [mul_zero, zero_add, Nat.cast_one] rw [pML_zero (by omega), pKT_zero 1] norm_num [Nat.choose] | succ m' => set m := m' + 1 have hm : 1 ≤ m := Nat.succ_le_succ (Nat.zero_le m') rw [pKT_half_odd m] -- Show `pML m (2m+1) / pKT m (2m+1) ≥ 4^m / C(2m, m) ≥ √(2m + 2)`! have hch_pos : (0 : ℝ) < Nat.choose (2 * m) m := by exact_mod_cast Nat.choose_pos (show m ≤ 2 * m by omega) have hratio_ge : (4 : ℝ) ^ m / (Nat.choose (2 * m) m : ℝ) ≤ pML m (2 * m + 1) / ((Nat.choose (2 * m) m : ℝ) / (2 * (4 : ℝ) ^ (2 * m))) := by rw [div_div_eq_mul_div, div_le_div_iff_of_pos_right hch_pos] -- Need `4^m ≤ pML m (2m+1) * 2 * 4^(2m)`, i.e. -- `(2m+1)^(2m+1) ≤ 2 * 4^m * m^m * (m+1)^(m+1)` simp only [pML, show 2 * m + 1 - m = m + 1 by omega] rw [div_pow, div_pow] have hden : ((2 * m + 1 : ℕ) : ℝ) ^ m * ((2 * m + 1 : ℕ) : ℝ) ^ (m + 1) = ((2 * m + 1 : ℕ) : ℝ) ^ (2 * m + 1) := by rw [← pow_add]; congr 1; omega have hden_pos : (0 : ℝ) < ((2 * m + 1 : ℕ) : ℝ) ^ (2 * m + 1) := by positivity have hL : (m : ℝ) ^ m / ((2 * m + 1 : ℕ) : ℝ) ^ m * (((m + 1 : ℕ) : ℝ) ^ (m + 1) / ((2 * m + 1 : ℕ) : ℝ) ^ (m + 1)) * (2 * (4 : ℝ) ^ (2 * m)) = ((m : ℝ) ^ m * ((m + 1 : ℕ) : ℝ) ^ (m + 1) * (2 * (4 : ℝ) ^ (2 * m))) / ((2 * m + 1 : ℕ) : ℝ) ^ (2 * m + 1) := by rw [div_mul_div_comm, hden, div_mul_eq_mul_div] rw [hL, le_div_iff₀ hden_pos] -- Cancel `4^m`: suffices `(2m+1)^(2m+1) ≤ 2 * (m+1) * (4*m*(m+1))^m` -- Write `4*m*(m+1) = (2m+1)^2 - 1` and apply Bernoulli `(1 - u)^m ≥ 1 - m*u`! have hbase : (4 : ℝ) ^ (2 * m) = (4 : ℝ) ^ m * (4 : ℝ) ^ m := by rw [← pow_add]; congr 1; omega rw [hbase] have hcore : ((2 * m + 1 : ℕ) : ℝ) ^ (2 * m + 1) ≤ 2 * ((m + 1 : ℕ) : ℝ) * (4 * (m : ℝ) * ((m + 1 : ℕ) : ℝ)) ^ m := by set N : ℝ := ((2 * m + 1 : ℕ) : ℝ) have hNpos : 0 < N := by positivity have hNsq : 4 * (m : ℝ) * ((m + 1 : ℕ) : ℝ) = N ^ 2 * (1 - 1 / N ^ 2) := by simp only [N]; push_cast; field_simp [ne_of_gt hNpos]; ring rw [hNsq, mul_pow, ← pow_mul, show 2 * m + 1 = 2 * m + 1 from rfl, pow_succ] -- Bernoulli: `(1 - 1/N^2)^m ≥ 1 - m / N^2` have hbern : 1 - (m : ℝ) * (1 / N ^ 2) ≤ (1 - 1 / N ^ 2) ^ m := by have h1 : (-2 : ℝ) ≤ -(1 / N ^ 2) := by have : 1 ≤ N ^ 2 := by have hN1 : 1 ≤ N := by simp only [N]; push_cast; linarith nlinarith have : 1 / N ^ 2 ≤ 1 := (div_le_one (by positivity)).mpr this linarith have hb := one_add_mul_le_pow h1 m simpa [sub_eq_add_neg, mul_neg] using hb have hstep : N ≤ 2 * ((m + 1 : ℕ) : ℝ) * (1 - (m : ℝ) * (1 / N ^ 2)) := by have hN2 : 0 < N ^ 2 := by positivity rw [show 2 * ((m + 1 : ℕ) : ℝ) * (1 - (m : ℝ) * (1 / N ^ 2)) = (2 * ((m + 1 : ℕ) : ℝ) * (N ^ 2 - m)) / N ^ 2 by field_simp [ne_of_gt hN2]] rw [le_div_iff₀ hN2] simp only [N]; push_cast have hm_r : (1 : ℝ) ≤ m := by exact_mod_cast hm nlinarith have hmul := mul_le_mul_of_nonneg_left hbern (mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 2) (Nat.cast_nonneg (m + 1))) (pow_nonneg (le_of_lt hNpos) (2 * m))) have hmul2 := mul_le_mul_of_nonneg_left hstep (pow_nonneg (le_of_lt hNpos) (2 * m)) linarith have hrewrite : (m : ℝ) ^ m * ((m + 1 : ℕ) : ℝ) ^ (m + 1) * (2 * ((4 : ℝ) ^ m * (4 : ℝ) ^ m)) = (4 : ℝ) ^ m * (2 * ((m + 1 : ℕ) : ℝ) * (4 * (m : ℝ) * ((m + 1 : ℕ) : ℝ)) ^ m) := by rw [pow_succ ((m + 1 : ℕ) : ℝ) m, mul_pow, mul_pow] ring rw [hrewrite] exact mul_le_mul_of_nonneg_left hcore (by positivity) -- Now `√(2m + 2) ≤ 4^m / C(2m, m)` since `2m + 2 ≤ 3m + 1` for `m ≥ 1`! have hcb_m := (centralBinom_nat_sandwich m hm).1 have hsq_le : Real.sqrt (((2 * m + 1 : ℕ) : ℝ) + 1) ≤ (4 : ℝ) ^ m / (Nat.choose (2 * m) m : ℝ) := by rw [le_div_iff₀ hch_pos] have hsq : (Real.sqrt (((2 * m + 1 : ℕ) : ℝ) + 1) * (Nat.choose (2 * m) m : ℝ)) ^ 2 ≤ ((4 : ℝ) ^ m) ^ 2 := by rw [mul_pow, Real.sq_sqrt (by positivity)] have h1 : ((3 * m + 1 : ℕ) : ℝ) * (Nat.choose (2 * m) m : ℝ) ^ 2 ≤ ((4 : ℝ) ^ m) ^ 2 := by exact_mod_cast hcb_m have h2 : (((2 * m + 1 : ℕ) : ℝ) + 1) ≤ ((3 * m + 1 : ℕ) : ℝ) := by push_cast have hm_r : (1 : ℝ) ≤ m := by exact_mod_cast hm linarith nlinarith [sq_nonneg (Nat.choose (2 * m) m : ℝ)] exact le_of_sq_le_sq hsq (by positivity) exact le_trans hsq_le hratio_ge -- Assemble all 4 parts! have huni := (kt_regret_ratio_unimodal hn).2 have hS_lower : Real.sqrt (n + 1 : ℝ) ≤ shtarkovBern n := by rw [← sum_pML_eq_shtarkovBern] calc Real.sqrt (n + 1 : ℝ) = Real.sqrt (n + 1 : ℝ) * ∑ x : Fin n → Bool, pKT (onesCount x) n := by rw [sum_pKT_eq_one, mul_one] _ = ∑ x : Fin n → Bool, Real.sqrt (n + 1 : ℝ) * pKT (onesCount x) n := Finset.mul_sum _ _ _ _ ≤ ∑ x : Fin n → Bool, pML (onesCount x) n := by refine Finset.sum_le_sum fun x _ => ?_ have hle := le_trans hmin_ge (huni (onesCount x) (onesCount_le x)).1 exact (le_div_iff₀ (pKT_pos (onesCount x) n)).mp hle have hS_upper : shtarkovBern n ≤ 2 * Real.sqrt n := by rw [← sum_pML_eq_shtarkovBern] calc ∑ x : Fin n → Bool, pML (onesCount x) n ≤ ∑ x : Fin n → Bool, (2 * Real.sqrt n) * pKT (onesCount x) n := by refine Finset.sum_le_sum fun x _ => ?_ have hle := le_trans (huni (onesCount x) (onesCount_le x)).2 hmax_le exact (div_le_iff₀ (pKT_pos (onesCount x) n)).mp hle _ = (2 * Real.sqrt n) * ∑ x : Fin n → Bool, pKT (onesCount x) n := (Finset.mul_sum _ _ _).symm _ = 2 * Real.sqrt n := by rw [sum_pKT_eq_one, mul_one] refine ⟨?_, ?_, ⟨hS_lower, hS_upper⟩, ?_⟩ · intro k hk have hrat := le_trans (huni k hk).2 hmax_le refine ⟨hrat, ?_⟩ have hlog := Real.log_le_log (div_pos (pML_pos hn hk) (pKT_pos k n)) hrat rw [Real.log_div (ne_of_gt (pML_pos hn hk)) (ne_of_gt (pKT_pos k n)), Real.log_mul (by norm_num) (ne_of_gt (Real.sqrt_pos.mpr hn_pos)), Real.log_sqrt (le_of_lt hn_pos)] at hlog linarith · intro hn2 have hn2_r : (2 : ℝ) ≤ n := by exact_mod_cast hn2 have hlt : 2 * Real.sqrt n < (n : ℝ) + 1 := by have hsq : (2 * Real.sqrt n) ^ 2 < ((n : ℝ) + 1) ^ 2 := by rw [mul_pow, Real.sq_sqrt (le_of_lt hn_pos)] nlinarith nlinarith [Real.sqrt_nonneg (n : ℝ)] have hlog := Real.log_lt_log (by positivity) hlt rw [Real.log_mul (by norm_num) (ne_of_gt (Real.sqrt_pos.mpr hn_pos)), Real.log_sqrt (le_of_lt hn_pos)] at hlog linarith · constructor · have hlog := Real.log_le_log (Real.sqrt_pos.mpr (by positivity)) hS_lower rw [Real.log_sqrt (by positivity)] at hlog linarith · have hlog := Real.log_le_log (shtarkovBern_pos hn) hS_upper rw [Real.log_mul (by norm_num) (ne_of_gt (Real.sqrt_pos.mpr hn_pos)), Real.log_sqrt (le_of_lt hn_pos)] at hlog linarith /-! ## Section 6: Axiom Audit -/ #print axioms mdl_vs_memorization_phase_transition #print axioms mdl_phase_transition_sharp_witnesses #print axioms bic_mdl_beats_memorization_above_threshold #print axioms bic_mdl_phase_transition_witness #print axioms barron_cover_two_part_mdl_bounds #print axioms nml_shtarkov_minimax_exact #print axioms laplace_seq_prob_exact #print axioms laplace_exact_regret_and_sharpness #print axioms kt_seq_prob_exact #print axioms sum_pKT_eq_one #print axioms centralBinom_nat_sandwich #print axioms kt_regret_ratio_unimodal #print axioms kt_exact_regret_and_shtarkov_sandwich end MDLPhaseTransition