import Mathlib.Analysis.SpecialFunctions.Log.Basic import Mathlib.Analysis.SpecialFunctions.Log.Deriv import Mathlib.Analysis.Calculus.MeanValue import Mathlib.Analysis.Convex.SpecificFunctions.Basic import Mathlib.Analysis.Convex.Jensen import Mathlib.Algebra.BigOperators.Field import Mathlib.Topology.Algebra.InfiniteSum.Order import Mathlib.Topology.Algebra.InfiniteSum.Ring /-! # Pillar 2: Exact Solomonoff Universal Bayes Mixture — Cumulative KL, Bhattacharyya, # Hellinger & Total-Variation Bounds, Exact Sharpness Witnesses, Posterior Concentration, # Countable Mixtures, and Approximate-Solomonoff Robustness Self-contained Lean 4 formalization establishing exact, sharp, non-asymptotic results for: 1. **Elementary Sharp Divergence Inequalities**: KL nonnegativity, exact **two-stage Bhattacharyya–Hellinger chain** `H²(p, q) ≤ -ln(1 - H²(p, q)) ≤ (1/2) D_KL(p ‖ q)` (with exact Bhattacharyya equality witness), and self-contained **Pinsker's inequality** `‖p - q‖₁² ≤ 2 D_KL(p ‖ q)`. 2. **Exact Solomonoff Telescoping Identity & Cumulative Error Bounds** (for arbitrary finite OR countably infinite hypothesis classes with sub-probability prior `∑ w_i = W ≤ 1`): - **Exact KL + Posterior-Surprise Identity**: `∑_{t=1}^T E_{P_k}[D_KL(μ_k ‖ ξ)] + E_{P_k}[ln(1 / w_k(X_{1:T}))] = ln(W / w_k) ≤ ln(1 / w_k)`. - **Sharper Cumulative Bhattacharyya & Hellinger Bounds**: `∑_{t=1}^T E_{P_k}[H²(μ_k, ξ)] ≤ ∑_{t=1}^T E_{P_k}[-ln(1 - H²(μ_k, ξ))] ≤ (1/2) ln(W / w_k)`. - **Cumulative Squared L₁ / TV Bound**: `∑_{t=1}^T E_{P_k}[‖μ_k - ξ‖₁²] ≤ 2 ln(W / w_k) ≤ 2 ln(1 / w_k)`. 3. **Exact Sharpness Witnesses (Matching Every Constant!)**: - Exact equality for cumulative KL (`= ln(1 / w₀)`) and Bhattacharyya (`= (1/2) ln(1 / w₀)`), plus Hellinger ratio `→ 1` (arbitrary closeness to constant `1/2`). - **Multi-Round Bernoulli Sharpness Family for Constant `2` in `∑ E[‖μ₀ - ξ‖₁²] ≤ 2 ln(1/w₀)`**: proving that for EVERY `c < 2`, there exists an explicit two-hypothesis Bernoulli Solomonoff mixture and horizon `T` achieving `∑_{t=1}^T E[‖μ₀ - ξ‖₁²] > c ln(1/w₀)`. 4. **Exact Factor-1/2 Exponential Bayesian Posterior Concentration & Cesàro Merge Rates**: `E_{P_k}[1 - w_k(X_{1:T})] ≤ (1/2) ∑_{j ≠ k} √(w_j / w_k) BC_T(P_k, P_j)`, plus exact `O(1/T)` Hellinger and `O(1/√T)` Cesàro `L₁` merge rates. 5. **Exact Approximate-Solomonoff Robustness Theorem** (connecting to Nov 2026 Li–Teng): for ANY predictor `ρ` approximating the normalized Solomonoff mixture `ξ̃`: `D_KL(P_k ‖ ρ) ≤ ln(W / w_k) + (W / w_k) D_KL(ξ̃ ‖ ρ) + 1`. -/ namespace SolomonoffPrediction open Finset BigOperators Filter Topology /-! ## Section 1: Exact Divergence Definitions & Sharp Elementary Inequalities -/ /-- Single KL summand `p * ln(p / q)` (0 when `p = 0`). -/ noncomputable def klTerm (p q : ℝ) : ℝ := if p = 0 then 0 else p * Real.log (p / q) /-- Discrete Kullback–Leibler divergence `D_KL(p ‖ q)` on a finite alphabet `α`. -/ noncomputable def klDiv {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := ∑ a : α, klTerm (p a) (q a) /-- Bhattacharyya affinity `BC(p, q) = ∑_a √(p a * q a)`. -/ noncomputable def bhattAff {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := ∑ a : α, Real.sqrt (p a * q a) /-- Squared Hellinger distance `H²(p, q) = 1 - ∑_a √(p a * q a) = (1/2) ∑_a (√p a - √q a)²`. -/ noncomputable def hellingerSq {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := 1 - bhattAff p q /-- `L₁` distance `‖p - q‖₁ = ∑_a |p a - q a|` (`= 2 TV(p, q)`). -/ noncomputable def l1Dist {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := ∑ a : α, |p a - q a| lemma hellingerSq_eq_half_sum_sq {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (hq1 : ∑ a : α, q a = 1) : hellingerSq p q = (1 / 2 : ℝ) * ∑ a : α, (Real.sqrt (p a) - Real.sqrt (q a)) ^ 2 := by have hterm : ∀ a : α, (Real.sqrt (p a) - Real.sqrt (q a)) ^ 2 = p a + q a - 2 * Real.sqrt (p a * q a) := by intro a rw [sub_sq, Real.sq_sqrt (hp a), Real.sq_sqrt (hq a), Real.sqrt_mul (hp a)] ring simp_rw [hterm, Finset.sum_sub_distrib, Finset.sum_add_distrib, ← Finset.mul_sum, hp1, hq1, hellingerSq, bhattAff] ring lemma hellingerSq_nonneg {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (hq1 : ∑ a : α, q a = 1) : 0 ≤ hellingerSq p q := by rw [hellingerSq_eq_half_sum_sq hp hq hp1 hq1] exact mul_nonneg (by norm_num) (Finset.sum_nonneg fun _ _ => sq_nonneg _) /-- Exact elementary inequality `klTerm p q ≥ p - q` (for `p ≥ 0, q > 0`, or `p = 0, q ≥ 0`). -/ lemma klTerm_ge_sub {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) (habs : p > 0 → q > 0) : p - q ≤ klTerm p q := by by_cases hp0 : p = 0 · subst hp0 simp [klTerm, hq] · have hp_pos : 0 < p := lt_of_le_of_ne hp (Ne.symm hp0) have hq_pos : 0 < q := habs hp_pos simp only [klTerm, hp0, ↓reduceIte] have hlog := Real.log_le_sub_one_of_pos (div_pos hq_pos hp_pos) have h1 : Real.log (p / q) = -Real.log (q / p) := by rw [← Real.log_inv, inv_div] rw [h1] have h2 : p * (1 - q / p) ≤ p * (-Real.log (q / p)) := mul_le_mul_of_nonneg_left (by linarith) hp have h3 : p * (1 - q / p) = p - q := by field_simp linarith theorem klDiv_nonneg {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (hq1 : ∑ a : α, q a ≤ 1) (habs : ∀ a, 0 < p a → 0 < q a) : 0 ≤ klDiv p q := by have h := Finset.sum_le_sum fun a (_ : a ∈ Finset.univ) => klTerm_ge_sub (hp a) (hq a) (habs a) rw [Finset.sum_sub_distrib, hp1] at h change 0 ≤ ∑ a : α, klTerm (p a) (q a) linarith /-- **Exact Two-Stage Bhattacharyya–Hellinger–KL Chain**: `H²(p, q) ≤ -ln(1 - H²(p, q)) ≤ (1/2) D_KL(p ‖ q)` (and hence `2 H²(p, q) ≤ D_KL(p ‖ q)`). -/ theorem hellingerSq_bhatt_kl_chain {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (_hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (_hq1 : ∑ a : α, q a = 1) (habs : ∀ a, 0 < p a → 0 < q a) : hellingerSq p q ≤ -Real.log (bhattAff p q) ∧ -Real.log (bhattAff p q) ≤ (1 / 2 : ℝ) * klDiv p q := by have hbc_pos : 0 < bhattAff p q := by have hex : ∃ a : α, 0 < p a := by by_contra hall push Not at hall have h0 : ∑ a : α, p a ≤ 0 := Finset.sum_nonpos fun a _ => hall a linarith rcases hex with ⟨a0, ha0⟩ have hq0 := habs a0 ha0 exact lt_of_lt_of_le (Real.sqrt_pos.mpr (mul_pos ha0 hq0)) (Finset.single_le_sum (f := fun a : α => Real.sqrt (p a * q a)) (fun a _ => Real.sqrt_nonneg _) (Finset.mem_univ a0)) constructor · -- `1 - BC ≤ -ln(BC)` follows directly from `ln(BC) ≤ BC - 1` have hlog := Real.log_le_sub_one_of_pos hbc_pos simp only [hellingerSq] linarith · -- `-ln(BC) ≤ (1/2) D_KL(p ‖ q)` via Jensen on `Real.exp` over the support of `p` let S : Finset α := Finset.univ.filter (fun a => p a ≠ 0) have hS_sum : ∑ a ∈ S, p a = 1 := by rw [← hp1] refine Finset.sum_subset (Finset.filter_subset _ _) ?_ intro a _ ha simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, not_not] at ha exact ha have hS_pos : ∀ a ∈ S, 0 < p a := fun a ha => lt_of_le_of_ne (hp a) (Ne.symm (Finset.mem_filter.mp ha).2) have hjensen := ConvexOn.map_sum_le convexOn_exp (t := S) (w := p) (p := fun a => (1 / 2 : ℝ) * Real.log (q a / p a)) (fun a _ => hp a) hS_sum (fun _ _ => Set.mem_univ _) simp only [smul_eq_mul] at hjensen have hrhs : ∑ a ∈ S, p a * Real.exp ((1 / 2 : ℝ) * Real.log (q a / p a)) ≤ bhattAff p q := by have heq : ∑ a ∈ S, p a * Real.exp ((1 / 2 : ℝ) * Real.log (q a / p a)) = ∑ a ∈ S, Real.sqrt (p a * q a) := by refine Finset.sum_congr rfl fun a ha => ?_ have hpa := hS_pos a ha have hqa := habs a hpa rw [mul_comm (1 / 2 : ℝ), Real.exp_mul, Real.exp_log (div_pos hqa hpa), ← Real.sqrt_eq_rpow, Real.sqrt_div (le_of_lt hqa), Real.sqrt_mul (le_of_lt hpa)] have hsq : Real.sqrt (p a) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr hpa) have hsq2 : Real.sqrt (p a) * Real.sqrt (p a) = p a := Real.mul_self_sqrt (le_of_lt hpa) field_simp [hsq] nlinarith rw [heq] exact Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun _ _ _ => Real.sqrt_nonneg _) have hcomb := le_trans hjensen hrhs have hlog := Real.log_le_log (Real.exp_pos _) hcomb rw [Real.log_exp] at hlog have hlhs : ∑ a ∈ S, p a * ((1 / 2 : ℝ) * Real.log (q a / p a)) = -(1 / 2 : ℝ) * klDiv p q := by have hkl : klDiv p q = ∑ a ∈ S, p a * Real.log (p a / q a) := by rw [klDiv] rw [← Finset.sum_subset (s₁ := S) (s₂ := Finset.univ) (Finset.filter_subset _ _)] · refine Finset.sum_congr rfl fun a ha => ?_ have hne := (Finset.mem_filter.mp ha).2 simp [klTerm, hne] · intro a _ ha simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, not_not] at ha simp [klTerm, ha] rw [hkl, Finset.mul_sum] refine Finset.sum_congr rfl fun a ha => ?_ have hinv : Real.log (q a / p a) = -Real.log (p a / q a) := by rw [← Real.log_inv, inv_div] rw [hinv] ring rw [hlhs] at hlog linarith lemma two_mul_hellingerSq_le_klDiv {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (hq1 : ∑ a : α, q a = 1) (habs : ∀ a, 0 < p a → 0 < q a) : 2 * hellingerSq p q ≤ klDiv p q := by have ⟨h1, h2⟩ := hellingerSq_bhatt_kl_chain hp hq hp1 hq1 habs linarith /-- Elementary Topsøe–Pinsker scalar inequality: for `a, b > 0`, `3 * (a - b)^2 / (2 * (a + 2 * b)) ≤ a * ln(a / b) - (a - b)` (equivalently for `t > 0`, `t ln t - t + 1 ≥ 3 (t - 1)^2 / (2 (t + 2))`). -/ private lemma scalar_pinsker_poly {t : ℝ} (ht : 0 < t) : 3 * (t - 1) ^ 2 / (2 * (t + 2)) ≤ t * Real.log t - t + 1 := by -- Define `g(t) = t ln t - t + 1 - 3(t-1)^2 / (2(t+2))`. -- `g(1) = 0`, `g'(t) = ln t - 3(t-1)(t+5) / (2(t+2)^2)`... even simpler: -- Prove `f(x) = ln x - 3(x-1)(x+5)...` wait: prove directly via `g'(t)`! -- Actually `h(t) = (t + 2) * (t * ln t - t + 1) - (3/2) * (t - 1)^2`? No, check `g''(t)`: -- Let `d(t) = t * ln t - t + 1 - 3 * (t - 1)^2 / (2 * (t + 2))`. -- `d'(t) = ln t - 3 * (t - 1) * (t + 5) / (2 * (t + 2)^2)`. -- `d''(t) = 1/t - 3 * (t^2 + 4*t + 22?)` wait: let's compute carefully! -- d/dt [ (t-1)^2 / (t+2) ] = (2(t-1)(t+2) - (t-1)^2) / (t+2)^2 = (t-1)(t+5) / (t+2)^2. -- d/dt [ (t-1)(t+5) / (t+2)^2 ] = ((2t+4)(t+2)^2 - 2(t+2)(t-1)(t+5)) / (t+2)^4 -- = (2(t+2)^2 - 2(t^2+4t-5)) / (t+2)^3 = (2t^2+8t+8 - 2t^2-8t+10) / (t+2)^3 = 18 / (t+2)^3! -- SO d''(t) = 1/t - 27 / (t+2)^3 = ((t+2)^3 - 27*t) / (t*(t+2)^3) -- = (t^3 + 6t^2 - 15t + 8) / (t*(t+2)^3) -- = (t - 1)^2 * (t + 8) / (t * (t + 2)^3) ≥ 0 FOR ALL t > 0!! -- WHAT A CLEAN IDENTITY!! `d''(t) = (t - 1)^2 * (t + 8) / (t * (t + 2)^3) ≥ 0`! let d : ℝ → ℝ := fun x => x * Real.log x - x + 1 - 3 * (x - 1) ^ 2 / (2 * (x + 2)) let d1 : ℝ → ℝ := fun x => Real.log x - 3 * (x - 1) * (x + 5) / (2 * (x + 2) ^ 2) have hd_deriv : ∀ x ∈ Set.Ioi (0 : ℝ), HasDerivAt d (d1 x) x := by intro x hx have hx0 : x ≠ 0 := ne_of_gt hx have hx2 : x + 2 ≠ 0 := by linarith [Set.mem_Ioi.mp hx] have h1 : HasDerivAt (fun x => x * Real.log x - x + 1) (Real.log x) x := by have hid : HasDerivAt (fun y : ℝ => y) 1 x := hasDerivAt_id x have ha : HasDerivAt (fun y => y * Real.log y) (1 * Real.log x + x * x⁻¹) x := hid.mul (Real.hasDerivAt_log hx0) have hb : HasDerivAt (fun y => y * Real.log y - y) (1 * Real.log x + x * x⁻¹ - 1) x := ha.sub hid have hc : HasDerivAt (fun y => y * Real.log y - y + 1) (1 * Real.log x + x * x⁻¹ - 1) x := hb.add_const (1 : ℝ) have hval : 1 * Real.log x + x * x⁻¹ - 1 = Real.log x := by field_simp [hx0]; ring rwa [hval] at hc have h2 : HasDerivAt (fun x => 3 * (x - 1) ^ 2 / (2 * (x + 2))) (3 * (x - 1) * (x + 5) / (2 * (x + 2) ^ 2)) x := by have hid : HasDerivAt (fun y : ℝ => y) 1 x := hasDerivAt_id x have hnum : HasDerivAt (fun x => 3 * (x - 1) ^ 2) (6 * (x - 1)) x := by have h : HasDerivAt (fun y => 3 * (y - 1) ^ 2) (3 * (↑(2 : ℕ) * (x - 1) ^ (2 - 1) * 1)) x := ((hid.sub_const (1 : ℝ)).pow 2).const_mul 3 have hval : 3 * (↑(2 : ℕ) * (x - 1) ^ (2 - 1) * 1) = 6 * (x - 1) := by norm_num; ring rwa [hval] at h have hden : HasDerivAt (fun x => 2 * (x + 2)) 2 x := by have h : HasDerivAt (fun y => 2 * (y + 2)) (2 * 1) x := (hid.add_const (2 : ℝ)).const_mul 2 have hval : (2 : ℝ) * 1 = 2 := by ring rwa [hval] at h have h2x2 : 2 * (x + 2) ≠ 0 := by linarith [Set.mem_Ioi.mp hx] have hdiv := hnum.div hden h2x2 have hval : (6 * (x - 1) * (2 * (x + 2)) - 3 * (x - 1) ^ 2 * 2) / (2 * (x + 2)) ^ 2 = 3 * (x - 1) * (x + 5) / (2 * (x + 2) ^ 2) := by field_simp [hx2]; ring rwa [hval] at hdiv exact h1.sub h2 have hd1_deriv : ∀ x ∈ Set.Ioi (0 : ℝ), HasDerivAt d1 ((x - 1) ^ 2 * (x + 8) / (x * (x + 2) ^ 3)) x := by intro x hx have hx0 : x ≠ 0 := ne_of_gt hx have hx2 : x + 2 ≠ 0 := by linarith [Set.mem_Ioi.mp hx] have hid : HasDerivAt (fun y : ℝ => y) 1 x := hasDerivAt_id x have h1 := Real.hasDerivAt_log hx0 have h2 : HasDerivAt (fun x => 3 * (x - 1) * (x + 5) / (2 * (x + 2) ^ 2)) (27 / (x + 2) ^ 3) x := by have hnum : HasDerivAt (fun x => 3 * (x - 1) * (x + 5)) (6 * x + 12) x := by have ha : HasDerivAt (fun x => 3 * (x - 1)) 3 x := by have h : HasDerivAt (fun y => 3 * (y - 1)) (3 * 1) x := (hid.sub_const (1 : ℝ)).const_mul 3 have hval : (3 : ℝ) * 1 = 3 := by ring rwa [hval] at h have hb : HasDerivAt (fun x => x + 5) 1 x := hid.add_const (5 : ℝ) have hmul := ha.mul hb have hval : 3 * (x + 5) + 3 * (x - 1) * 1 = 6 * x + 12 := by ring rwa [hval] at hmul have hden : HasDerivAt (fun x => 2 * (x + 2) ^ 2) (4 * (x + 2)) x := by have h : HasDerivAt (fun y => 2 * (y + 2) ^ 2) (2 * (↑(2 : ℕ) * (x + 2) ^ (2 - 1) * 1)) x := ((hid.add_const (2 : ℝ)).pow 2).const_mul 2 have hval : 2 * (↑(2 : ℕ) * (x + 2) ^ (2 - 1) * 1) = 4 * (x + 2) := by norm_num; ring rwa [hval] at h have h2x2 : 2 * (x + 2) ^ 2 ≠ 0 := by positivity have hdiv := hnum.div hden h2x2 have hval : ((6 * x + 12) * (2 * (x + 2) ^ 2) - 3 * (x - 1) * (x + 5) * (4 * (x + 2))) / (2 * (x + 2) ^ 2) ^ 2 = 27 / (x + 2) ^ 3 := by field_simp [hx2]; ring rwa [hval] at hdiv have hsub := h1.sub h2 have hval : x⁻¹ - 27 / (x + 2) ^ 3 = (x - 1) ^ 2 * (x + 8) / (x * (x + 2) ^ 3) := by field_simp [hx0, hx2]; ring rwa [hval] at hsub have hd1_mono : MonotoneOn d1 (Set.Ioi 0) := by apply monotoneOn_of_deriv_nonneg (convex_Ioi 0) · exact fun x hx => (hd1_deriv x hx).continuousAt.continuousWithinAt · intro x hx rw [interior_Ioi] at hx exact (hd1_deriv x hx).differentiableAt.differentiableWithinAt · intro x hx rw [interior_Ioi] at hx rw [(hd1_deriv x hx).deriv] have hxpos : 0 < x := hx positivity have hd1_zero : d1 1 = 0 := by simp [d1] have hd_zero : d 1 = 0 := by simp [d] suffices h : 0 ≤ d t by simp only [d] at h; linarith rcases le_total 1 t with ht1 | ht1 · -- `1 ≤ t`: `d1 ≥ 0` on `[1, t]` so `d` is monotone on `[1, t]` have hd_mono : MonotoneOn d (Set.Icc 1 t) := by apply monotoneOn_of_deriv_nonneg (convex_Icc 1 t) · exact fun x hx => (hd_deriv x (by show (0 : ℝ) < x; linarith [hx.1])).continuousAt.continuousWithinAt · intro x hx exact (hd_deriv x (by show (0 : ℝ) < x; linarith [(interior_subset hx).1])).differentiableAt.differentiableWithinAt · intro x hx have hx1 : 1 ≤ x := (interior_subset hx).1 have hx0 : 0 < x := by linarith rw [(hd_deriv x hx0).deriv] rw [← hd1_zero] exact hd1_mono (by norm_num) hx0 hx1 rw [← hd_zero] exact hd_mono ⟨le_rfl, ht1⟩ ⟨ht1, le_rfl⟩ ht1 · -- `0 < t ≤ 1`: `d1 ≤ 0` on `[t, 1]` so `d` is antitone on `[t, 1]` have hd_anti : AntitoneOn d (Set.Icc t 1) := by apply antitoneOn_of_deriv_nonpos (convex_Icc t 1) · exact fun x hx => (hd_deriv x (by show (0 : ℝ) < x; linarith [hx.1])).continuousAt.continuousWithinAt · intro x hx exact (hd_deriv x (by show (0 : ℝ) < x; linarith [(interior_subset hx).1])).differentiableAt.differentiableWithinAt · intro x hx have hx1 : x ≤ 1 := (interior_subset hx).2 have hx0 : 0 < x := by linarith [(interior_subset hx).1] rw [(hd_deriv x hx0).deriv] rw [← hd1_zero] exact hd1_mono hx0 (by norm_num) hx1 rw [← hd_zero] exact hd_anti ⟨le_rfl, ht1⟩ ⟨ht1, le_rfl⟩ ht1 /-- **Self-Contained Sharp Pinsker's Inequality**: `‖p - q‖₁² ≤ 2 D_KL(p ‖ q)` on any finite alphabet `α`. -/ theorem pinsker_l1_sq_le_two_kl {α : Type*} [Fintype α] {p q : α → ℝ} (hp : ∀ a, 0 ≤ p a) (hq : ∀ a, 0 ≤ q a) (hp1 : ∑ a : α, p a = 1) (hq1 : ∑ a : α, q a = 1) (habs : ∀ a, 0 < p a → 0 < q a) : (l1Dist p q) ^ 2 ≤ 2 * klDiv p q := by -- Pointwise: `3 * (p a - q a)^2 / (2 * (p a + 2 * q a)) ≤ klTerm (p a) (q a) - (p a - q a)` have hpt : ∀ a : α, 3 * (p a - q a) ^ 2 / (2 * (p a + 2 * q a)) ≤ klTerm (p a) (q a) - (p a - q a) := by intro a by_cases hpa : p a = 0 · by_cases hqa : q a = 0 · simp [hpa, hqa, klTerm] · have hqpos : 0 < q a := lt_of_le_of_ne (hq a) (Ne.symm hqa) simp only [hpa, klTerm, ↓reduceIte, zero_sub, neg_sq, zero_add, neg_neg] rw [div_le_iff₀ (by positivity)] nlinarith · have hppos : 0 < p a := lt_of_le_of_ne (hp a) (Ne.symm hpa) have hqpos : 0 < q a := habs a hppos have hs := scalar_pinsker_poly (div_pos hppos hqpos) have hmul := mul_le_mul_of_nonneg_left hs (le_of_lt hqpos) simp only [klTerm, hpa, ↓reduceIte] have hL : q a * (3 * (p a / q a - 1) ^ 2 / (2 * (p a / q a + 2))) = 3 * (p a - q a) ^ 2 / (2 * (p a + 2 * q a)) := by have hden : p a + 2 * q a ≠ 0 := by linarith have hden2 : p a / q a + 2 ≠ 0 := by positivity field_simp [ne_of_gt hqpos, hden, hden2] have hR : q a * (p a / q a * Real.log (p a / q a) - p a / q a + 1) = p a * Real.log (p a / q a) - (p a - q a) := by field_simp [ne_of_gt hqpos] ring linarith -- Cauchy–Schwarz: `(∑ |p a - q a|)^2 ≤ (∑ (p a - q a)^2 / (p a + 2 q a)) * (∑ (p a + 2 q a))` let f : α → ℝ := fun a => if p a + 2 * q a = 0 then 0 else |p a - q a| / Real.sqrt (p a + 2 * q a) let g : α → ℝ := fun a => Real.sqrt (p a + 2 * q a) have hprod : ∀ a : α, f a * g a = |p a - q a| := by intro a by_cases h0 : p a + 2 * q a = 0 · have hp0 : p a = 0 := by linarith [hp a, hq a] have hq0 : q a = 0 := by linarith [hp a, hq a] simp [f, g, hp0, hq0] · have hpos : 0 < p a + 2 * q a := lt_of_le_of_ne (by linarith [hp a, hq a]) (Ne.symm h0) simp only [f, g, h0, ↓reduceIte] exact div_mul_cancel₀ _ (ne_of_gt (Real.sqrt_pos.mpr hpos)) have hcs := Finset.sum_mul_sq_le_sq_mul_sq (Finset.univ : Finset α) f g have hfg : ∑ a : α, f a * g a = l1Dist p q := by rw [l1Dist] exact Finset.sum_congr rfl fun a _ => hprod a have hg2 : ∑ a : α, g a ^ 2 = 3 := by have : ∀ a : α, g a ^ 2 = p a + 2 * q a := fun a => Real.sq_sqrt (by linarith [hp a, hq a]) simp_rw [this, Finset.sum_add_distrib, ← Finset.mul_sum, hp1, hq1] ring have hf2 : (3 / 2 : ℝ) * ∑ a : α, f a ^ 2 ≤ klDiv p q := by rw [Finset.mul_sum] have hsum_pt := Finset.sum_le_sum fun a (_ : a ∈ Finset.univ) => hpt a rw [Finset.sum_sub_distrib, Finset.sum_sub_distrib, hp1, hq1, sub_self, sub_zero] at hsum_pt refine le_trans (Finset.sum_le_sum fun a _ => ?_) hsum_pt by_cases h0 : p a + 2 * q a = 0 · have hp0 : p a = 0 := by linarith [hp a, hq a] have hq0 : q a = 0 := by linarith [hp a, hq a] simp [f, hp0, hq0] · have hpos : 0 < p a + 2 * q a := lt_of_le_of_ne (by linarith [hp a, hq a]) (Ne.symm h0) simp only [f, h0, ↓reduceIte, div_pow, sq_abs, Real.sq_sqrt (le_of_lt hpos)] exact le_of_eq (by field_simp [h0]) rw [hfg, hg2] at hcs linarith /-! ## Section 2: Sequential Stochastic Environments & Solomonoff Bayes Mixture -/ /-- A sequential environment over finite alphabet `α`: causal conditional next-symbol kernel. -/ structure SeqEnv (α : Type*) [Fintype α] where cond : List α → α → ℝ nonneg : ∀ h a, 0 ≤ cond h a sum_one : ∀ h, ∑ a : α, cond h a = 1 /-- Cylinder probability `P(x₁…x_t) = ∏_{s=1}^t μ(x_s | x_{ 1 | a :: rest => μ.cond [] a * (⟨fun h => μ.cond (a :: h), fun h => μ.nonneg (a :: h), fun h => μ.sum_one (a :: h)⟩ : SeqEnv α).cyl rest /-- Cleaner right-recursive cylinder probability `P(h ++ [a]) = P(h) * μ(a | h)`. -/ def SeqEnv.prob {α : Type*} [Fintype α] (μ : SeqEnv α) : List α → ℝ := fun x => List.foldl (fun (acc : ℝ × List α) a => (acc.1 * μ.cond acc.2 a, acc.2 ++ [a])) (1, []) x |>.1 private lemma foldl_snd {α : Type*} [Fintype α] (μ : SeqEnv α) (x : List α) (p : ℝ) (h : List α) : (List.foldl (fun (acc : ℝ × List α) a => (acc.1 * μ.cond acc.2 a, acc.2 ++ [a])) (p, h) x).2 = h ++ x := by induction x generalizing p h with | nil => simp | cons a xs ih => simp only [List.foldl_cons, ih, List.append_assoc, List.singleton_append] lemma SeqEnv.prob_nil {α : Type*} [Fintype α] (μ : SeqEnv α) : μ.prob [] = 1 := rfl lemma SeqEnv.prob_append_singleton {α : Type*} [Fintype α] (μ : SeqEnv α) (h : List α) (a : α) : μ.prob (h ++ [a]) = μ.prob h * μ.cond h a := by simp only [SeqEnv.prob, List.foldl_append, List.foldl_cons, List.foldl_nil] rw [foldl_snd, List.nil_append] lemma SeqEnv.prob_nonneg {α : Type*} [Fintype α] (μ : SeqEnv α) (x : List α) : 0 ≤ μ.prob x := by induction x using List.reverseRecOn with | nil => simp [SeqEnv.prob_nil] | append_singleton h a ih => rw [SeqEnv.prob_append_singleton] exact mul_nonneg ih (μ.nonneg h a) lemma SeqEnv.prob_le_one {α : Type*} [Fintype α] (μ : SeqEnv α) (x : List α) : μ.prob x ≤ 1 := by induction x using List.reverseRecOn with | nil => simp [SeqEnv.prob_nil] | append_singleton h a ih => rw [SeqEnv.prob_append_singleton] have hca : μ.cond h a ≤ 1 := by rw [← μ.sum_one h] exact Finset.single_le_sum (fun b _ => μ.nonneg h b) (Finset.mem_univ a) calc μ.prob h * μ.cond h a ≤ 1 * 1 := mul_le_mul ih hca (μ.nonneg h a) zero_le_one _ = 1 := one_mul 1 /-- Cylinder probability summed over all length-`T` words `Fin T → α`. -/ def wordsOfLen {α : Type*} [Fintype α] (T : ℕ) : Type _ := Fin T → α instance {α : Type*} [Fintype α] (T : ℕ) : Fintype (wordsOfLen (α := α) T) := inferInstanceAs (Fintype (Fin T → α)) lemma SeqEnv.sum_prob_eq_one {α : Type*} [Fintype α] (μ : SeqEnv α) (T : ℕ) : ∑ x : Fin T → α, μ.prob (List.ofFn x) = 1 := by induction T with | zero => simp [SeqEnv.prob_nil] | succ T ih => -- Split `Fin (T + 1) → α` into `(Fin T → α) × α` via `Fin.snoc` let e : (Fin T → α) × α ≃ (Fin (T + 1) → α) := { toFun := fun p => Fin.snoc p.1 p.2 invFun := fun x => (Fin.init x, x (Fin.last T)) left_inv := fun ⟨x, a⟩ => by ext i <;> simp [Fin.init, Fin.snoc] right_inv := fun x => Fin.snoc_init_self x } rw [← Equiv.sum_comp e, Fintype.sum_prod_type] calc ∑ x : Fin T → α, ∑ a : α, μ.prob (List.ofFn (Fin.snoc x a)) = ∑ x : Fin T → α, ∑ a : α, μ.prob (List.ofFn x) * μ.cond (List.ofFn x) a := by refine Finset.sum_congr rfl fun x _ => Finset.sum_congr rfl fun a _ => ?_ rw [List.ofFn_succ', Fin.snoc_last] have hinit : (fun i : Fin T => (Fin.snoc (α := fun _ => α) x a) i.castSucc) = x := by ext i; simp only [Fin.snoc_castSucc] rw [hinit, List.concat_eq_append, SeqEnv.prob_append_singleton] _ = ∑ x : Fin T → α, μ.prob (List.ofFn x) * ∑ a : α, μ.cond (List.ofFn x) a := by refine Finset.sum_congr rfl fun x _ => (Finset.mul_sum _ _ _).symm _ = ∑ x : Fin T → α, μ.prob (List.ofFn x) := by refine Finset.sum_congr rfl fun x _ => by rw [μ.sum_one, mul_one] _ = 1 := ih /-- **Exact Chain Rule for Sequential KL Divergence** (arbitrary two environments `μ`, `ν` with `ν` dominating `μ` on all length-`T` prefixes). -/ theorem klDiv_chain_rule {α : Type*} [Fintype α] (μ ν : SeqEnv α) (T : ℕ) (hdom : ∀ (t : ℕ) (_ : t < T) (x : Fin t → α) (a : α), 0 < μ.prob (List.ofFn x) → 0 < μ.cond (List.ofFn x) a → 0 < ν.cond (List.ofFn x) a) : ∑ t ∈ Finset.range T, ∑ x : Fin t → α, μ.prob (List.ofFn x) * klDiv (μ.cond (List.ofFn x)) (ν.cond (List.ofFn x)) = klDiv (fun x : Fin T → α => μ.prob (List.ofFn x)) (fun x : Fin T → α => ν.prob (List.ofFn x)) := by induction T with | zero => simp [klDiv, klTerm, SeqEnv.prob_nil] | succ T ih => rw [Finset.sum_range_succ, ih (fun t ht => hdom t (Nat.lt_succ_of_lt ht))] -- Expand RHS via `Fin.snoc` let e : (Fin T → α) × α ≃ (Fin (T + 1) → α) := { toFun := fun p => Fin.snoc p.1 p.2 invFun := fun x => (Fin.init x, x (Fin.last T)) left_inv := fun ⟨x, a⟩ => by ext i <;> simp [Fin.init, Fin.snoc] right_inv := fun x => Fin.snoc_init_self x } have hstep : ∀ x : Fin T → α, klTerm (μ.prob (List.ofFn x)) (ν.prob (List.ofFn x)) + μ.prob (List.ofFn x) * klDiv (μ.cond (List.ofFn x)) (ν.cond (List.ofFn x)) = ∑ a : α, klTerm (μ.prob (List.ofFn (Fin.snoc x a))) (ν.prob (List.ofFn (Fin.snoc x a))) := by intro x have hsnoc : ∀ a : α, List.ofFn (Fin.snoc x a) = List.ofFn x ++ [a] := by intro a rw [List.ofFn_succ', Fin.snoc_last, List.concat_eq_append] have hinit : (fun i : Fin T => (Fin.snoc (α := fun _ => α) x a) i.castSucc) = x := by ext i; simp only [Fin.snoc_castSucc] rw [hinit] simp_rw [hsnoc, SeqEnv.prob_append_singleton] by_cases hpx : μ.prob (List.ofFn x) = 0 · simp [klTerm, hpx] · have hpx_pos : 0 < μ.prob (List.ofFn x) := lt_of_le_of_ne (μ.prob_nonneg _) (Ne.symm hpx) -- Also `ν.prob (List.ofFn x) > 0` by induction along prefix `x` have hqx_pos : 0 < ν.prob (List.ofFn x) := by have haux : ∀ (m : ℕ) (hm : m ≤ T) (y : Fin m → α), 0 < μ.prob (List.ofFn y) → 0 < ν.prob (List.ofFn y) := by intro m induction m with | zero => intro _ _ _; simp [SeqEnv.prob_nil] | succ m ihm => intro hm y hy have hyl : List.ofFn y = List.ofFn (Fin.init y) ++ [y (Fin.last m)] := by rw [List.ofFn_succ', List.concat_eq_append]; rfl rw [hyl, SeqEnv.prob_append_singleton] at hy ⊢ have h1 : 0 < μ.prob (List.ofFn (Fin.init y)) := by by_contra hle have h0 : μ.prob (List.ofFn (Fin.init y)) = 0 := le_antisymm (not_lt.mp hle) (μ.prob_nonneg _) rw [h0, zero_mul] at hy; exact lt_irrefl _ hy have h2 : 0 < μ.cond (List.ofFn (Fin.init y)) (y (Fin.last m)) := by by_contra hle have h0 : μ.cond (List.ofFn (Fin.init y)) (y (Fin.last m)) = 0 := le_antisymm (not_lt.mp hle) (μ.nonneg _ _) rw [h0, mul_zero] at hy; exact lt_irrefl _ hy exact mul_pos (ihm (Nat.le_of_succ_le hm) _ h1) (hdom m (by omega) _ _ h1 h2) exact haux T le_rfl x hpx_pos rw [klDiv, Finset.mul_sum] have hsplit : klTerm (μ.prob (List.ofFn x)) (ν.prob (List.ofFn x)) = ∑ a : α, (μ.prob (List.ofFn x) * μ.cond (List.ofFn x) a) * Real.log (μ.prob (List.ofFn x) / ν.prob (List.ofFn x)) := by rw [← Finset.sum_mul, ← Finset.mul_sum, μ.sum_one, mul_one] simp [klTerm, hpx] rw [hsplit, ← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun a _ => ?_ by_cases hca : μ.cond (List.ofFn x) a = 0 · simp [klTerm, hca] · have hca_pos : 0 < μ.cond (List.ofFn x) a := lt_of_le_of_ne (μ.nonneg _ _) (Ne.symm hca) have hqa_pos : 0 < ν.cond (List.ofFn x) a := hdom T (Nat.lt_succ_self T) x a hpx_pos hca_pos have hmul_ne : μ.prob (List.ofFn x) * μ.cond (List.ofFn x) a ≠ 0 := ne_of_gt (mul_pos hpx_pos hca_pos) simp only [klTerm, hca, hmul_ne, ↓reduceIte] rw [mul_div_mul_comm, Real.log_mul (ne_of_gt (div_pos hpx_pos hqx_pos)) (ne_of_gt (div_pos hca_pos hqa_pos))] ring rw [klDiv, klDiv, ← Equiv.sum_comp e, Fintype.sum_prod_type, ← Finset.sum_add_distrib] exact Finset.sum_congr rfl fun x _ => hstep x /-! ## Section 3: Countable & Finite Solomonoff Bayes Mixture, Exact Telescoping & Bounds -/ /-- A countable (or finite) Solomonoff Bayes hypothesis class with sub-probability prior `w : ι → ℝ`, `w i > 0`, `Summable w`, and total prior mass `W = ∑' i, w i ≤ 1`. -/ structure SolomonoffClass (α ι : Type*) [Fintype α] where env : ι → SeqEnv α weight : ι → ℝ weight_pos : ∀ i, 0 < weight i summable_weight : Summable weight total_le_one : ∑' i, weight i ≤ 1 /-- Total prior mass `W = ∑' i, w i`. -/ noncomputable def SolomonoffClass.totalWeight {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) : ℝ := ∑' i, C.weight i /-- Solomonoff mixture cylinder semimeasure `ξ(h) = ∑' i, w_i P_i(h)`. -/ noncomputable def SolomonoffClass.mixProb {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) (h : List α) : ℝ := ∑' i, C.weight i * (C.env i).prob h lemma SolomonoffClass.summable_mix {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) (h : List α) : Summable (fun i => C.weight i * (C.env i).prob h) := by refine Summable.of_nonneg_of_le (fun i => mul_nonneg (le_of_lt (C.weight_pos i)) ((C.env i).prob_nonneg h)) (fun i => ?_) C.summable_weight exact mul_le_of_le_one_right (le_of_lt (C.weight_pos i)) ((C.env i).prob_le_one h) lemma SolomonoffClass.mixProb_ge_term {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) (k : ι) (h : List α) : C.weight k * (C.env k).prob h ≤ C.mixProb h := (C.summable_mix h).le_tsum k (fun i _ => mul_nonneg (le_of_lt (C.weight_pos i)) ((C.env i).prob_nonneg h)) lemma SolomonoffClass.mixProb_sum_next {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) (h : List α) : ∑ a : α, C.mixProb (h ++ [a]) = C.mixProb h := by simp only [SolomonoffClass.mixProb] rw [← Summable.tsum_finsetSum (fun a _ => C.summable_mix (h ++ [a]))] congr 1 ext i simp_rw [SeqEnv.prob_append_singleton, ← mul_assoc, ← Finset.mul_sum, (C.env i).sum_one, mul_one] /-- Posterior weight `w_k(h) = w_k P_k(h) / ξ(h)` (defined as `0` if `ξ(h) = 0`). -/ noncomputable def SolomonoffClass.posterior {α ι : Type*} [Fintype α] (C : SolomonoffClass α ι) (k : ι) (h : List α) : ℝ := if C.mixProb h = 0 then 0 else C.weight k * (C.env k).prob h / C.mixProb h /-- Solomonoff universal predictive distribution `ξ(a | h) = ξ(h ++ [a]) / ξ(h)` (uniform fallback on zero-measure unreachable histories). -/ noncomputable def SolomonoffClass.predictor {α ι : Type*} [Fintype α] [Nonempty α] (C : SolomonoffClass α ι) : SeqEnv α where cond h a := if C.mixProb h = 0 then (Fintype.card α : ℝ)⁻¹ else C.mixProb (h ++ [a]) / C.mixProb h nonneg h a := by split_ifs with h0 · positivity · exact div_nonneg (tsum_nonneg fun i => mul_nonneg (le_of_lt (C.weight_pos i)) ((C.env i).prob_nonneg _)) (tsum_nonneg fun i => mul_nonneg (le_of_lt (C.weight_pos i)) ((C.env i).prob_nonneg _)) sum_one h := by split_ifs with h0 · simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul] have hpos : (0 : ℝ) < (Fintype.card α : ℝ) := by positivity field_simp [ne_of_gt hpos] · rw [← Finset.sum_div, C.mixProb_sum_next, div_self h0] /-- Key telescoping lemma: for every reachable history `x` (where `ξ(x) > 0`), the predictor's cylinder probability equals the normalized mixture `ξ(x) / W`. -/ lemma SolomonoffClass.predictor_prob_eq {α ι : Type*} [Fintype α] [Nonempty α] (C : SolomonoffClass α ι) [Nonempty ι] (x : List α) (hx : 0 < C.mixProb x) : C.predictor.prob x = C.mixProb x / C.totalWeight := by have hW_pos : 0 < C.totalWeight := by obtain ⟨i0⟩ := ‹Nonempty ι› exact lt_of_lt_of_le (C.weight_pos i0) (C.summable_weight.le_tsum i0 (fun i _ => le_of_lt (C.weight_pos i))) induction x using List.reverseRecOn with | nil => have hnil : C.mixProb [] = C.totalWeight := by simp [SolomonoffClass.mixProb, SolomonoffClass.totalWeight, SeqEnv.prob_nil] rw [SeqEnv.prob_nil, hnil, div_self (ne_of_gt hW_pos)] | append_singleton h a ih => have hh_pos : 0 < C.mixProb h := by rw [← C.mixProb_sum_next h] exact lt_of_lt_of_le hx (Finset.single_le_sum (f := fun b : α => C.mixProb (h ++ [b])) (fun b _ => tsum_nonneg fun i => mul_nonneg (le_of_lt (C.weight_pos i)) ((C.env i).prob_nonneg _)) (Finset.mem_univ a)) rw [SeqEnv.prob_append_singleton, ih hh_pos] simp only [SolomonoffClass.predictor, ne_of_gt hh_pos, ↓reduceIte] field_simp [ne_of_gt hh_pos, ne_of_gt hW_pos] /-- **Exact Solomonoff Telescoping Identity** (valid for countable/finite classes and sub-probability priors `W = ∑' w_i ≤ 1`): `∑_{t=1}^T E_{P_k}[D_KL(μ_k(· | X_{ le_of_lt (C.weight_pos i))) have hdom : ∀ (t : ℕ) (_ : t < T) (x : Fin t → α) (a : α), 0 < (C.env k).prob (List.ofFn x) → 0 < (C.env k).cond (List.ofFn x) a → 0 < C.predictor.cond (List.ofFn x) a := by intro t _ x a hpx hca have hmix : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos hwk_pos hpx) (C.mixProb_ge_term k _) have hmix_next : 0 < C.mixProb (List.ofFn x ++ [a]) := by have hp_next : 0 < (C.env k).prob (List.ofFn x ++ [a]) := by rw [SeqEnv.prob_append_singleton]; exact mul_pos hpx hca exact lt_of_lt_of_le (mul_pos hwk_pos hp_next) (C.mixProb_ge_term k _) simp only [SolomonoffClass.predictor, ne_of_gt hmix, ↓reduceIte] exact div_pos hmix_next hmix rw [klDiv_chain_rule (C.env k) C.predictor T hdom, klDiv, ← Finset.sum_add_distrib] have hterm : ∀ x : Fin T → α, klTerm ((C.env k).prob (List.ofFn x)) (C.predictor.prob (List.ofFn x)) + (C.env k).prob (List.ofFn x) * Real.log (1 / C.posterior k (List.ofFn x)) = (C.env k).prob (List.ofFn x) * Real.log (C.totalWeight / C.weight k) := by intro x by_cases hpx : (C.env k).prob (List.ofFn x) = 0 · simp [klTerm, hpx] · have hpx_pos : 0 < (C.env k).prob (List.ofFn x) := lt_of_le_of_ne ((C.env k).prob_nonneg _) (Ne.symm hpx) have hmix_pos : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos hwk_pos hpx_pos) (C.mixProb_ge_term k _) rw [C.predictor_prob_eq (List.ofFn x) hmix_pos] simp only [klTerm, hpx, ↓reduceIte, SolomonoffClass.posterior, ne_of_gt hmix_pos] rw [← mul_add, ← Real.log_mul (ne_of_gt (div_pos hpx_pos (div_pos hmix_pos hW_pos))) (ne_of_gt (div_pos one_pos (div_pos (mul_pos hwk_pos hpx_pos) hmix_pos)))] congr 2 field_simp [ne_of_gt hpx_pos, ne_of_gt hmix_pos, ne_of_gt hwk_pos, ne_of_gt hW_pos] simp_rw [hterm, ← Finset.sum_mul, (C.env k).sum_prob_eq_one, one_mul] /-- **Exact Solomonoff Cumulative KL, Bhattacharyya, Hellinger & Squared-`L₁` Prediction Bounds** (for countable/finite classes and sub-probability priors `W ≤ 1`): 1. `∑_{t=1}^T E[D_KL(μ_k ‖ ξ)] ≤ ln(W / w_k) ≤ ln(1 / w_k)` 2. `∑_{t=1}^T E[H²(μ_k, ξ)] ≤ ∑_{t=1}^T E[-ln(1 - H²(μ_k, ξ))] ≤ (1/2) ln(W / w_k) ≤ (1/2) ln(1 / w_k)` 3. `∑_{t=1}^T E[‖μ_k - ξ‖₁²] ≤ 2 ln(W / w_k) ≤ 2 ln(1 / w_k)`. -/ theorem solomonoff_cumulative_kl_hellinger_tv_bounds {α ι : Type*} [Fintype α] [Nonempty α] (C : SolomonoffClass α ι) (k : ι) (T : ℕ) : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * klDiv ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ Real.log (C.totalWeight / C.weight k) ∧ (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * hellingerSq ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ (1 / 2 : ℝ) * Real.log (C.totalWeight / C.weight k) ∧ (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * (l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2) ≤ 2 * Real.log (C.totalWeight / C.weight k) ∧ Real.log (C.totalWeight / C.weight k) ≤ Real.log (1 / C.weight k) := by have hwk_pos : 0 < C.weight k := C.weight_pos k have hW_pos : 0 < C.totalWeight := lt_of_lt_of_le hwk_pos (C.summable_weight.le_tsum k (fun i _ => le_of_lt (C.weight_pos i))) have hpost_nonneg : 0 ≤ ∑ x : Fin T → α, (C.env k).prob (List.ofFn x) * Real.log (1 / C.posterior k (List.ofFn x)) := by refine Finset.sum_nonneg fun x _ => ?_ by_cases hpx : (C.env k).prob (List.ofFn x) = 0 · simp [hpx] · have hpx_pos : 0 < (C.env k).prob (List.ofFn x) := lt_of_le_of_ne ((C.env k).prob_nonneg _) (Ne.symm hpx) have hmix_pos : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos hwk_pos hpx_pos) (C.mixProb_ge_term k _) have hpost_le : C.posterior k (List.ofFn x) ≤ 1 := by simp only [SolomonoffClass.posterior, ne_of_gt hmix_pos, ↓reduceIte] exact (div_le_one hmix_pos).mpr (C.mixProb_ge_term k _) have hpost_pos : 0 < C.posterior k (List.ofFn x) := by simp only [SolomonoffClass.posterior, ne_of_gt hmix_pos, ↓reduceIte] exact div_pos (mul_pos hwk_pos hpx_pos) hmix_pos exact mul_nonneg (le_of_lt hpx_pos) (Real.log_nonneg ((one_le_div hpost_pos).mpr hpost_le)) have hkl : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * klDiv ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ Real.log (C.totalWeight / C.weight k) := by linarith [solomonoff_exact_kl_telescoping C k T] have hdom_step : ∀ (t : ℕ) (x : Fin t → α), 0 < (C.env k).prob (List.ofFn x) → ∀ a : α, 0 < (C.env k).cond (List.ofFn x) a → 0 < C.predictor.cond (List.ofFn x) a := by intro t x hpx a hca have hmix : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos hwk_pos hpx) (C.mixProb_ge_term k _) have hmix_next : 0 < C.mixProb (List.ofFn x ++ [a]) := by have hp_next : 0 < (C.env k).prob (List.ofFn x ++ [a]) := by rw [SeqEnv.prob_append_singleton]; exact mul_pos hpx hca exact lt_of_lt_of_le (mul_pos hwk_pos hp_next) (C.mixProb_ge_term k _) simp only [SolomonoffClass.predictor, ne_of_gt hmix, ↓reduceIte] exact div_pos hmix_next hmix have hH2 : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * hellingerSq ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ (1 / 2 : ℝ) * Real.log (C.totalWeight / C.weight k) := by have hle : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * hellingerSq ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ (1 / 2 : ℝ) * (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * klDiv ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) := by rw [Finset.mul_sum] refine Finset.sum_le_sum fun t _ => ?_ rw [Finset.mul_sum] refine Finset.sum_le_sum fun x _ => ?_ by_cases hpx : (C.env k).prob (List.ofFn x) = 0 · simp [hpx] · have hpx_pos : 0 < (C.env k).prob (List.ofFn x) := lt_of_le_of_ne ((C.env k).prob_nonneg _) (Ne.symm hpx) have h2 := two_mul_hellingerSq_le_klDiv ((C.env k).nonneg _) (C.predictor.nonneg _) ((C.env k).sum_one _) (C.predictor.sum_one _) (hdom_step t x hpx_pos) nlinarith linarith have hL1 : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * (l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2) ≤ 2 * Real.log (C.totalWeight / C.weight k) := by have hle : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * (l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2) ≤ 2 * (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * klDiv ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) := by rw [Finset.mul_sum] refine Finset.sum_le_sum fun t _ => ?_ rw [Finset.mul_sum] refine Finset.sum_le_sum fun x _ => ?_ by_cases hpx : (C.env k).prob (List.ofFn x) = 0 · simp [hpx] · have hpx_pos : 0 < (C.env k).prob (List.ofFn x) := lt_of_le_of_ne ((C.env k).prob_nonneg _) (Ne.symm hpx) have hp := pinsker_l1_sq_le_two_kl ((C.env k).nonneg _) (C.predictor.nonneg _) ((C.env k).sum_one _) (C.predictor.sum_one _) (hdom_step t x hpx_pos) nlinarith linarith have hW_le : Real.log (C.totalWeight / C.weight k) ≤ Real.log (1 / C.weight k) := Real.log_le_log (div_pos hW_pos hwk_pos) (div_le_div_of_nonneg_right C.total_le_one (le_of_lt hwk_pos)) exact ⟨hkl, hH2, hL1, hW_le⟩ /-! ## Section 4: Exact Sharpness Witnesses for KL, Bhattacharyya, Hellinger & Squared `L₁` -/ /-- Two-hypothesis deterministic orthogonal environment on `Bool`: `false` emits `false` w.p. 1, `true` emits `true` w.p. 1. -/ def detBoolEnv (b : Bool) : SeqEnv Bool where cond _ a := if a = b then 1 else 0 nonneg _ a := by split_ifs <;> norm_num sum_one _ := by cases b <;> simp /-- Two-hypothesis Solomonoff mixture on deterministic orthogonal environments with weights `(1 - δ, δ)`. -/ noncomputable def orthoTwoClass (δ : ℝ) (h0 : 0 < δ) (h1 : δ < 1) : SolomonoffClass Bool Bool where env := detBoolEnv weight := fun b => if b then δ else 1 - δ weight_pos := fun b => by cases b <;> simp <;> linarith summable_weight := summable_of_hasFiniteSupport (Set.toFinite _) total_le_one := by rw [tsum_fintype, Fintype.sum_bool] simp /-- **Exact Sharpness Witness for KL (= Exact Equality), Bhattacharyya (= Exact Equality), and Hellinger (Ratio `→ 1` to Constant `1/2`)**: At `T = 1` on `orthoTwoClass δ`: 1. `cumKL = ln(1 / w₀)` (EXACT EQUALITY for all `δ ∈ (0, 1)`!) 2. `-ln(1 - H²) = (1/2) ln(1 / w₀)` (EXACT EQUALITY for all `δ ∈ (0, 1)`!) 3. For every `c < 1/2`, choosing sufficiently small `δ > 0` yields `cumH² > c * ln(1 / w₀)`. -/ theorem solomonoff_kl_hellinger_sharp_witness : (∀ δ : ℝ, ∀ (h0 : 0 < δ) (h1 : δ < 1), let C := orthoTwoClass δ h0 h1 klDiv ((C.env false).cond []) (C.predictor.cond []) = Real.log (1 / C.weight false) ∧ -Real.log (bhattAff ((C.env false).cond []) (C.predictor.cond [])) = (1 / 2 : ℝ) * Real.log (1 / C.weight false) ∧ hellingerSq ((C.env false).cond []) (C.predictor.cond []) = 1 - Real.sqrt (1 - δ)) ∧ (∀ c : ℝ, c < 1 / 2 → ∃ (δ : ℝ) (h0 : 0 < δ) (h1 : δ < 1), let C := orthoTwoClass δ h0 h1 c * Real.log (1 / C.weight false) < hellingerSq ((C.env false).cond []) (C.predictor.cond [])) := by have hpart1 : ∀ δ : ℝ, ∀ (h0 : 0 < δ) (h1 : δ < 1), let C := orthoTwoClass δ h0 h1 klDiv ((C.env false).cond []) (C.predictor.cond []) = Real.log (1 / C.weight false) ∧ -Real.log (bhattAff ((C.env false).cond []) (C.predictor.cond [])) = (1 / 2 : ℝ) * Real.log (1 / C.weight false) ∧ hellingerSq ((C.env false).cond []) (C.predictor.cond []) = 1 - Real.sqrt (1 - δ) := by intro δ h0 h1 dsimp only set C := orthoTwoClass δ h0 h1 have hmix_nil : C.mixProb [] = 1 := by simp [SolomonoffClass.mixProb, tsum_fintype, C, orthoTwoClass, SeqEnv.prob_nil] have hmix_f : C.mixProb [false] = 1 - δ := by simp [SolomonoffClass.mixProb, tsum_fintype, C, orthoTwoClass, SeqEnv.prob, detBoolEnv] have hpred_f : C.predictor.cond [] false = 1 - δ := by simp [SolomonoffClass.predictor, hmix_nil, hmix_f] have hkl : klDiv ((C.env false).cond []) (C.predictor.cond []) = Real.log (1 / (1 - δ)) := by rw [klDiv, Fintype.sum_bool] have hct : (C.env false).cond [] true = 0 := rfl have hcf : (C.env false).cond [] false = 1 := rfl rw [hct, hcf, hpred_f] simp [klTerm] have hbc : bhattAff ((C.env false).cond []) (C.predictor.cond []) = Real.sqrt (1 - δ) := by rw [bhattAff, Fintype.sum_bool] have hct : (C.env false).cond [] true = 0 := rfl have hcf : (C.env false).cond [] false = 1 := rfl rw [hct, hcf, hpred_f] simp refine ⟨by simpa [C, orthoTwoClass] using hkl, ?_, by simp [hellingerSq, hbc]⟩ rw [hbc, Real.log_sqrt (by linarith : 0 ≤ 1 - δ)] have hw : C.weight false = 1 - δ := by simp [C, orthoTwoClass] rw [hw, Real.log_div one_ne_zero (by linarith), Real.log_one] ring refine ⟨hpart1, ?_⟩ intro c hc by_cases hc0 : c ≤ 0 · refine ⟨1 / 2, by norm_num, by norm_num, ?_⟩ dsimp only rw [(hpart1 (1 / 2) (by norm_num) (by norm_num)).2.2] have hw : (orthoTwoClass (1 / 2) (by norm_num) (by norm_num)).weight false = 1 / 2 := by simp [orthoTwoClass]; norm_num rw [hw, show (1 : ℝ) / (1 / 2) = 2 by norm_num] have hlog_pos : 0 < Real.log 2 := Real.log_pos (by norm_num) have hsq : Real.sqrt (1 - 1 / 2 : ℝ) < 1 := by rw [Real.sqrt_lt' zero_lt_one]; norm_num nlinarith · push Not at hc0 set δ : ℝ := (1 - 2 * c) / 2 have hδ0 : 0 < δ := by simp only [δ]; linarith have hδ1 : δ < 1 := by simp only [δ]; linarith refine ⟨δ, hδ0, hδ1, ?_⟩ dsimp only rw [(hpart1 δ hδ0 hδ1).2.2] have hw : (orthoTwoClass δ hδ0 hδ1).weight false = 1 - δ := by simp [orthoTwoClass] rw [hw] have h1m : 1 - δ ≠ 0 := by linarith have hlog_le : Real.log (1 / (1 - δ)) ≤ δ / (1 - δ) := by have h := Real.log_le_sub_one_of_pos (div_pos one_pos (by linarith : 0 < 1 - δ)) have heq : 1 / (1 - δ) - 1 = δ / (1 - δ) := by field_simp [h1m]; ring linarith have hsq_lt : Real.sqrt (1 - δ) < 1 := by rw [Real.sqrt_lt' zero_lt_one]; linarith have hsq_pos : 0 ≤ Real.sqrt (1 - δ) := Real.sqrt_nonneg _ have hH_gt : δ / 2 < 1 - Real.sqrt (1 - δ) := by have hid : (1 - Real.sqrt (1 - δ)) * (1 + Real.sqrt (1 - δ)) = δ := by have hsq2 : Real.sqrt (1 - δ) * Real.sqrt (1 - δ) = 1 - δ := Real.mul_self_sqrt (by linarith) nlinarith nlinarith have h2c : 2 * c ≤ 1 - δ := by simp only [δ]; linarith have hmul : c * (δ / (1 - δ)) ≤ δ / 2 := by rw [mul_div_assoc', div_le_div_iff₀ (by linarith : 0 < 1 - δ) two_pos] nlinarith nlinarith /-- Symmetric i.i.d. Bernoulli two-hypothesis class `Bern(1/2 + ε)` vs `Bern(1/2 - ε)` with equal prior `(1/2, 1/2)` — our sharp multi-round witness attaining constant `2` in `∑ E[‖μ₀ - ξ‖₁²]`. -/ def iidBernEnv (p : ℝ) (hp0 : 0 ≤ p) (hp1 : p ≤ 1) : SeqEnv Bool where cond _ a := if a then p else 1 - p nonneg _ a := by cases a <;> simp <;> linarith sum_one _ := by simp noncomputable def symBernTwoClass (ε : ℝ) (h0 : 0 < ε) (h1 : ε < 1 / 2) : SolomonoffClass Bool Bool where env := fun b => if b then iidBernEnv (1 / 2 - ε) (by linarith) (by linarith) else iidBernEnv (1 / 2 + ε) (by linarith) (by linarith) weight := fun _ => 1 / 2 weight_pos := fun _ => by norm_num summable_weight := summable_of_hasFiniteSupport (Set.toFinite _) total_le_one := by rw [tsum_fintype, Fintype.sum_bool]; norm_num /-- Elementary Bernoulli reverse-Pinsker bound on `[1/2 - ε, 1/2 + ε]`: `D_KL(Bern(a) ‖ Bern(b)) ≤ ‖Bern(a) - Bern(b)‖₁² / (2 * (1 - 4 ε²))`. -/ private lemma bern_kl_le_l1sq_div {a b ε : ℝ} (hε : 0 < ε) (hε' : ε < 1 / 2) (ha1 : 1 / 2 - ε ≤ a) (ha2 : a ≤ 1 / 2 + ε) (hb1 : 1 / 2 - ε ≤ b) (hb2 : b ≤ 1 / 2 + ε) : klDiv (fun x : Bool => if x then a else 1 - a) (fun x : Bool => if x then b else 1 - b) ≤ (l1Dist (fun x : Bool => if x then a else 1 - a) (fun x : Bool => if x then b else 1 - b)) ^ 2 / (2 * (1 - 4 * ε ^ 2)) := by have ha0 : 0 < a := by linarith have ha_1 : 0 < 1 - a := by linarith have hb0 : 0 < b := by linarith have hb_1 : 0 < 1 - b := by linarith have h14 : 0 < 1 - 4 * ε ^ 2 := by nlinarith have hl1 : l1Dist (fun x : Bool => if x then a else 1 - a) (fun x : Bool => if x then b else 1 - b) = 2 * |a - b| := by simp only [l1Dist, Fintype.sum_bool, ↓reduceIte, Bool.false_eq_true] have : |(1 - a) - (1 - b)| = |a - b| := by rw [show (1 - a) - (1 - b) = -(a - b) by ring, abs_neg] linarith rw [hl1] have hkl_eq : klDiv (fun x : Bool => if x then a else 1 - a) (fun x : Bool => if x then b else 1 - b) = a * Real.log (a / b) + (1 - a) * Real.log ((1 - a) / (1 - b)) := by simp [klDiv, klTerm, ne_of_gt ha0, ne_of_gt ha_1] rw [hkl_eq] let M : ℝ := 4 / (1 - 4 * ε ^ 2) let h : ℝ → ℝ := fun x => (M / 2) * (x - b) ^ 2 - (x * Real.log (x / b) + (1 - x) * Real.log ((1 - x) / (1 - b))) let h1 : ℝ → ℝ := fun x => M * (x - b) - (Real.log (x / b) - Real.log ((1 - x) / (1 - b))) have hdom : ∀ x ∈ Set.Icc (1 / 2 - ε) (1 / 2 + ε), 0 < x ∧ 0 < 1 - x := fun x hx => ⟨by linarith [hx.1], by linarith [hx.2]⟩ have hh_deriv : ∀ x ∈ Set.Icc (1 / 2 - ε) (1 / 2 + ε), HasDerivAt h (h1 x) x := by intro x hx obtain ⟨hx0, hx1⟩ := hdom x hx have hid : HasDerivAt (fun y : ℝ => y) 1 x := hasDerivAt_id x have hA : HasDerivAt (fun x => (M / 2) * (x - b) ^ 2) (M * (x - b)) x := by have hd : HasDerivAt (fun y => (M / 2) * (y - b) ^ 2) (M / 2 * (↑(2 : ℕ) * (x - b) ^ (2 - 1) * 1)) x := ((hid.sub_const b).pow 2).const_mul (M / 2) have hval : M / 2 * (↑(2 : ℕ) * (x - b) ^ (2 - 1) * 1) = M * (x - b) := by norm_num; ring rwa [hval] at hd have hB : HasDerivAt (fun x => x * Real.log (x / b)) (Real.log (x / b) + 1) x := by have hlog : HasDerivAt (fun x => Real.log (x / b)) (1 / x) x := by have hd : HasDerivAt (fun y => Real.log (y / b)) (1 / b / (x / b)) x := (hid.div_const b).log (ne_of_gt (div_pos hx0 hb0)) have hval : 1 / b / (x / b) = 1 / x := by field_simp [ne_of_gt hx0, ne_of_gt hb0] rwa [hval] at hd have hm : HasDerivAt (fun y => y * Real.log (y / b)) (1 * Real.log (x / b) + x * (1 / x)) x := hid.mul hlog have hval : 1 * Real.log (x / b) + x * (1 / x) = Real.log (x / b) + 1 := by field_simp [ne_of_gt hx0] rwa [hval] at hm have hC : HasDerivAt (fun x => (1 - x) * Real.log ((1 - x) / (1 - b))) (-(Real.log ((1 - x) / (1 - b)) + 1)) x := by have h1x : HasDerivAt (fun x => 1 - x) (-1) x := by simpa using hid.const_sub (1 : ℝ) have hlog : HasDerivAt (fun x => Real.log ((1 - x) / (1 - b))) (-(1 / (1 - x))) x := by have hd : HasDerivAt (fun y => Real.log ((1 - y) / (1 - b))) (-1 / (1 - b) / ((1 - x) / (1 - b))) x := (h1x.div_const (1 - b)).log (ne_of_gt (div_pos hx1 hb_1)) have hval : -1 / (1 - b) / ((1 - x) / (1 - b)) = -(1 / (1 - x)) := by field_simp [ne_of_gt hx1, ne_of_gt hb_1] rwa [hval] at hd have hm := h1x.mul hlog have hval : -1 * Real.log ((1 - x) / (1 - b)) + (1 - x) * -(1 / (1 - x)) = -(Real.log ((1 - x) / (1 - b)) + 1) := by field_simp [ne_of_gt hx1]; ring rwa [hval] at hm have hsub := hA.sub (hB.add hC) have hval : M * (x - b) - (Real.log (x / b) + 1 + -(Real.log ((1 - x) / (1 - b)) + 1)) = h1 x := by simp only [h1]; ring rwa [hval] at hsub have hh1_deriv : ∀ x ∈ Set.Icc (1 / 2 - ε) (1 / 2 + ε), HasDerivAt h1 (M - 1 / (x * (1 - x))) x := by intro x hx obtain ⟨hx0, hx1⟩ := hdom x hx have hid : HasDerivAt (fun y : ℝ => y) 1 x := hasDerivAt_id x have hA : HasDerivAt (fun x => M * (x - b)) M x := by have hd : HasDerivAt (fun y => M * (y - b)) (M * 1) x := (hid.sub_const b).const_mul M have hval : M * 1 = M := by ring rwa [hval] at hd have hB : HasDerivAt (fun x => Real.log (x / b)) (1 / x) x := by have hd : HasDerivAt (fun y => Real.log (y / b)) (1 / b / (x / b)) x := (hid.div_const b).log (ne_of_gt (div_pos hx0 hb0)) have hval : 1 / b / (x / b) = 1 / x := by field_simp [ne_of_gt hx0, ne_of_gt hb0] rwa [hval] at hd have hC : HasDerivAt (fun x => Real.log ((1 - x) / (1 - b))) (-(1 / (1 - x))) x := by have h1x : HasDerivAt (fun x => 1 - x) (-1) x := by simpa using hid.const_sub (1 : ℝ) have hd : HasDerivAt (fun y => Real.log ((1 - y) / (1 - b))) (-1 / (1 - b) / ((1 - x) / (1 - b))) x := (h1x.div_const (1 - b)).log (ne_of_gt (div_pos hx1 hb_1)) have hval : -1 / (1 - b) / ((1 - x) / (1 - b)) = -(1 / (1 - x)) := by field_simp [ne_of_gt hx1, ne_of_gt hb_1] rwa [hval] at hd have hsub := hA.sub (hB.sub hC) have hval : M - (1 / x - -(1 / (1 - x))) = M - 1 / (x * (1 - x)) := by field_simp [ne_of_gt hx0, ne_of_gt hx1]; ring rwa [hval] at hsub have hh1_mono : MonotoneOn h1 (Set.Icc (1 / 2 - ε) (1 / 2 + ε)) := by apply monotoneOn_of_deriv_nonneg (convex_Icc _ _) · exact fun x hx => (hh1_deriv x hx).continuousAt.continuousWithinAt · intro x hx exact (hh1_deriv x (interior_subset hx)).differentiableAt.differentiableWithinAt · intro x hx have hxI := interior_subset hx rw [(hh1_deriv x hxI).deriv] obtain ⟨hx0, hx1⟩ := hdom x hxI have hprod_ge : (1 - 4 * ε ^ 2) / 4 ≤ x * (1 - x) := by have hsq : (x - 1 / 2) ^ 2 ≤ ε ^ 2 := by rw [sq_le_sq, abs_of_pos hε] exact abs_le.mpr ⟨by linarith [hxI.1], by linarith [hxI.2]⟩ nlinarith have hinv : 1 / (x * (1 - x)) ≤ M := by simp only [M] rw [div_le_div_iff₀ (mul_pos hx0 hx1) h14] linarith linarith have hh1_b : h1 b = 0 := by simp [h1, div_self (ne_of_gt hb0), div_self (ne_of_gt hb_1)] have hh_b : h b = 0 := by simp [h, div_self (ne_of_gt hb0), div_self (ne_of_gt hb_1)] have hha : 0 ≤ h a := by rcases le_total b a with hba | hab · have hh_mono : MonotoneOn h (Set.Icc b a) := by apply monotoneOn_of_deriv_nonneg (convex_Icc b a) · intro x hx exact (hh_deriv x ⟨by linarith [hx.1], by linarith [hx.2]⟩).continuousAt.continuousWithinAt · intro x hx have hxI := interior_subset hx exact (hh_deriv x ⟨by linarith [hxI.1], by linarith [hxI.2]⟩).differentiableAt.differentiableWithinAt · intro x hx have hxI := interior_subset hx have hxJ : x ∈ Set.Icc (1 / 2 - ε) (1 / 2 + ε) := ⟨by linarith [hxI.1], by linarith [hxI.2]⟩ rw [(hh_deriv x hxJ).deriv, ← hh1_b] exact hh1_mono ⟨hb1, hb2⟩ hxJ hxI.1 rw [← hh_b] exact hh_mono ⟨le_rfl, hba⟩ ⟨hba, le_rfl⟩ hba · have hh_anti : AntitoneOn h (Set.Icc a b) := by apply antitoneOn_of_deriv_nonpos (convex_Icc a b) · intro x hx exact (hh_deriv x ⟨by linarith [hx.1], by linarith [hx.2]⟩).continuousAt.continuousWithinAt · intro x hx have hxI := interior_subset hx exact (hh_deriv x ⟨by linarith [hxI.1], by linarith [hxI.2]⟩).differentiableAt.differentiableWithinAt · intro x hx have hxI := interior_subset hx have hxJ : x ∈ Set.Icc (1 / 2 - ε) (1 / 2 + ε) := ⟨by linarith [hxI.1], by linarith [hxI.2]⟩ rw [(hh_deriv x hxJ).deriv, ← hh1_b] exact hh1_mono hxJ ⟨hb1, hb2⟩ hxI.2 rw [← hh_b] exact hh_anti ⟨le_rfl, hab⟩ ⟨hab, le_rfl⟩ hab have hrewrite : (M / 2) * (a - b) ^ 2 = (2 * |a - b|) ^ 2 / (2 * (1 - 4 * ε ^ 2)) := by simp only [M, mul_pow, sq_abs] field_simp [ne_of_gt h14] ring simp only [h] at hha linarith /-- Elementary inequality `ln(1 + u) ≤ 2 √u` for all `u ≥ 0`. -/ lemma log_one_add_le_two_sqrt {u : ℝ} (hu : 0 ≤ u) : Real.log (1 + u) ≤ 2 * Real.sqrt u := by have h1 : Real.log (1 + u) = 2 * Real.log (Real.sqrt (1 + u)) := by rw [Real.log_sqrt (by linarith)]; ring rw [h1] have h2 : Real.log (Real.sqrt (1 + u)) ≤ Real.sqrt (1 + u) - 1 := Real.log_le_sub_one_of_pos (Real.sqrt_pos.mpr (by linarith)) have h3 : Real.sqrt (1 + u) - 1 ≤ Real.sqrt u := by have hsq : Real.sqrt (1 + u) ≤ 1 + Real.sqrt u := by rw [Real.sqrt_le_left (by positivity)] nlinarith [Real.sq_sqrt hu, Real.sqrt_nonneg u] linarith linarith /-- Exact product formula for Bhattacharyya affinity of i.i.d. Bernoulli environments. -/ lemma iidBern_bhattAff_pow (p q : ℝ) (hp0 : 0 ≤ p) (hp1 : p ≤ 1) (hq0 : 0 ≤ q) (hq1 : q ≤ 1) (T : ℕ) : ∑ x : Fin T → Bool, Real.sqrt ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (iidBernEnv q hq0 hq1).prob (List.ofFn x)) = (Real.sqrt (p * q) + Real.sqrt ((1 - p) * (1 - q))) ^ T := by induction T with | zero => simp [SeqEnv.prob_nil] | succ T ih => let e : (Fin T → Bool) × Bool ≃ (Fin (T + 1) → Bool) := { toFun := fun s => Fin.snoc s.1 s.2 invFun := fun x => (Fin.init x, x (Fin.last T)) left_inv := fun ⟨x, a⟩ => by ext i <;> simp [Fin.init, Fin.snoc] right_inv := fun x => Fin.snoc_init_self x } rw [← Equiv.sum_comp e, Fintype.sum_prod_type] have hsnoc : ∀ (x : Fin T → Bool) (a : Bool), List.ofFn (Fin.snoc x a) = List.ofFn x ++ [a] := by intro x a rw [List.ofFn_succ', Fin.snoc_last, List.concat_eq_append] have hinit : (fun i : Fin T => (Fin.snoc (α := fun _ => Bool) x a) i.castSucc) = x := by ext i; simp only [Fin.snoc_castSucc] rw [hinit] simp only [e, Equiv.coe_fn_mk, hsnoc, SeqEnv.prob_append_singleton] have hfactor : ∀ (x : Fin T → Bool), ∑ a : Bool, Real.sqrt (((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (iidBernEnv p hp0 hp1).cond (List.ofFn x) a) * ((iidBernEnv q hq0 hq1).prob (List.ofFn x) * (iidBernEnv q hq0 hq1).cond (List.ofFn x) a)) = Real.sqrt ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (iidBernEnv q hq0 hq1).prob (List.ofFn x)) * (Real.sqrt (p * q) + Real.sqrt ((1 - p) * (1 - q))) := by intro x rw [Fintype.sum_bool] have hp_t : (iidBernEnv p hp0 hp1).cond (List.ofFn x) true = p := rfl have hp_f : (iidBernEnv p hp0 hp1).cond (List.ofFn x) false = 1 - p := rfl have hq_t : (iidBernEnv q hq0 hq1).cond (List.ofFn x) true = q := rfl have hq_f : (iidBernEnv q hq0 hq1).cond (List.ofFn x) false = 1 - q := rfl rw [hp_t, hp_f, hq_t, hq_f] have h1 := (iidBernEnv p hp0 hp1).prob_nonneg (List.ofFn x) have h2 := (iidBernEnv q hq0 hq1).prob_nonneg (List.ofFn x) have ha : ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * p) * ((iidBernEnv q hq0 hq1).prob (List.ofFn x) * q) = ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (iidBernEnv q hq0 hq1).prob (List.ofFn x)) * (p * q) := by ring have hb : ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (1 - p)) * ((iidBernEnv q hq0 hq1).prob (List.ofFn x) * (1 - q)) = ((iidBernEnv p hp0 hp1).prob (List.ofFn x) * (iidBernEnv q hq0 hq1).prob (List.ofFn x)) * ((1 - p) * (1 - q)) := by ring rw [ha, hb, Real.sqrt_mul (mul_nonneg h1 h2), Real.sqrt_mul (mul_nonneg h1 h2)] ring simp_rw [hfactor, ← Finset.sum_mul, ih, pow_succ] /-- **Universal Sharpness of Constant `2` in the Solomonoff Cumulative Squared-`L₁` Bound**: For EVERY `c < 2`, there exist `ε ∈ (0, 1/2)` and horizon `T` such that the two-hypothesis Solomonoff Bayes mixture `symBernTwoClass ε` satisfies `∑_{t=1}^T E_{P₀}[‖μ₀ - ξ‖₁²] > c * ln(1 / w₀)`. -/ theorem solomonoff_l1_sq_constant_two_sharp (c : ℝ) (hc : c < 2) : ∃ (ε : ℝ) (h0 : 0 < ε) (h1 : ε < 1 / 2) (T : ℕ), let C := symBernTwoClass ε h0 h1 c * Real.log (1 / C.weight false) < ∑ t ∈ Finset.range T, ∑ x : Fin t → Bool, (C.env false).prob (List.ofFn x) * (l1Dist ((C.env false).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 := by -- Pick `ε ∈ (0, 1/2)` small enough so `2 * (1 - 4 ε²) > (c + 2) / 2` have hgap : 0 < 2 - c := by linarith -- Choose `ε = min (1/4) (Real.sqrt ((2 - c) / 16))` set ε : ℝ := min (1 / 4 : ℝ) (Real.sqrt ((2 - c) / 16)) have hε0 : 0 < ε := lt_min (by norm_num) (Real.sqrt_pos.mpr (by linarith)) have hε1 : ε < 1 / 2 := lt_of_le_of_lt (min_le_left _ _) (by norm_num) have h4ε2 : 4 * ε ^ 2 ≤ (2 - c) / 4 := by have hle : ε ≤ Real.sqrt ((2 - c) / 16) := min_le_right _ _ have hsq : ε ^ 2 ≤ (2 - c) / 16 := by calc ε ^ 2 ≤ (Real.sqrt ((2 - c) / 16)) ^ 2 := sq_le_sq' (by linarith) hle _ = (2 - c) / 16 := Real.sq_sqrt (by linarith) linarith have h14_pos : 0 < 1 - 4 * ε ^ 2 := by nlinarith have h14_lt1 : 1 - 4 * ε ^ 2 < 1 := by nlinarith have hrho_pos : 0 < Real.sqrt (1 - 4 * ε ^ 2) := Real.sqrt_pos.mpr h14_pos have hrho_lt1 : Real.sqrt (1 - 4 * ε ^ 2) < 1 := by rw [Real.sqrt_lt' zero_lt_one]; linarith have hlog2_pos : 0 < Real.log 2 := Real.log_pos (by norm_num) have htend : Tendsto (fun T : ℕ => (Real.sqrt (1 - 4 * ε ^ 2)) ^ T) atTop (nhds 0) := tendsto_pow_atTop_nhds_zero_of_lt_one (le_of_lt hrho_pos) hrho_lt1 have htarget : 0 < Real.log 2 * ((2 - c) / 8) := by positivity obtain ⟨T, hT⟩ := eventually_atTop.mp (htend.eventually (gt_mem_nhds htarget)) specialize hT T le_rfl refine ⟨ε, hε0, hε1, T, ?_⟩ dsimp only set C := symBernTwoClass ε hε0 hε1 have hW : C.totalWeight = 1 := by simp [SolomonoffClass.totalWeight, tsum_fintype, C, symBernTwoClass] have hw0 : C.weight false = 1 / 2 := rfl -- Step 1: Lower bound `cumL1Sq ≥ 2 * (1 - 4 ε²) * cumKL` have hl1_ge : 2 * (1 - 4 * ε ^ 2) * (∑ t ∈ Finset.range T, ∑ x : Fin t → Bool, (C.env false).prob (List.ofFn x) * klDiv ((C.env false).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ ∑ t ∈ Finset.range T, ∑ x : Fin t → Bool, (C.env false).prob (List.ofFn x) * (l1Dist ((C.env false).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 := by rw [Finset.mul_sum] refine Finset.sum_le_sum fun t _ => ?_ rw [Finset.mul_sum] refine Finset.sum_le_sum fun x _ => ?_ have hpx_nn := (C.env false).prob_nonneg (List.ofFn x) by_cases hpx : (C.env false).prob (List.ofFn x) = 0 · simp [hpx] · have hpx_pos : 0 < (C.env false).prob (List.ofFn x) := lt_of_le_of_ne hpx_nn (Ne.symm hpx) have hmix_pos : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos (C.weight_pos false) hpx_pos) (C.mixProb_ge_term false _) set b := C.predictor.cond (List.ofFn x) true have hpred_eq : C.predictor.cond (List.ofFn x) = fun a : Bool => if a then b else 1 - b := by ext a cases a · have hsum := C.predictor.sum_one (List.ofFn x) simp only [Fintype.sum_bool] at hsum simp only [Bool.false_eq_true, ↓reduceIte] linarith · rfl have hmix_true : C.mixProb (List.ofFn x ++ [true]) = (1 / 2) * (C.env false).prob (List.ofFn x) * (1 / 2 + ε) + (1 / 2) * (C.env true).prob (List.ofFn x) * (1 / 2 - ε) := by simp only [SolomonoffClass.mixProb, tsum_fintype, Fintype.sum_bool, SeqEnv.prob_append_singleton, C, symBernTwoClass, iidBernEnv, ↓reduceIte, Bool.false_eq_true] ring have hmix_eq : C.mixProb (List.ofFn x) = (1 / 2) * (C.env false).prob (List.ofFn x) + (1 / 2) * (C.env true).prob (List.ofFn x) := by simp only [SolomonoffClass.mixProb, tsum_fintype, Fintype.sum_bool, C, symBernTwoClass] ring have hb_eq : b = C.mixProb (List.ofFn x ++ [true]) / C.mixProb (List.ofFn x) := by simp [b, SolomonoffClass.predictor, ne_of_gt hmix_pos] have hb1 : 1 / 2 - ε ≤ b := by rw [hb_eq, le_div_iff₀ hmix_pos, hmix_true, hmix_eq] nlinarith [(C.env false).prob_nonneg (List.ofFn x), (C.env true).prob_nonneg (List.ofFn x)] have hb2 : b ≤ 1 / 2 + ε := by rw [hb_eq, div_le_iff₀ hmix_pos, hmix_true, hmix_eq] nlinarith [(C.env false).prob_nonneg (List.ofFn x), (C.env true).prob_nonneg (List.ofFn x)] have hmu_eq : (C.env false).cond (List.ofFn x) = fun a : Bool => if a then 1 / 2 + ε else 1 - (1 / 2 + ε) := by ext a; cases a <;> simp [C, symBernTwoClass, iidBernEnv] have hbern := bern_kl_le_l1sq_div hε0 hε1 (by linarith) le_rfl hb1 hb2 rw [← hmu_eq, ← hpred_eq] at hbern have hmul := (le_div_iff₀ (by positivity : 0 < 2 * (1 - 4 * ε ^ 2))).mp hbern nlinarith -- Step 2: Upper bound posterior surprise `E[ln(1 / w₀(X_{1:T}))] ≤ 2 * (√(1 - 4 ε²))^T` have hsurp_le : (∑ x : Fin T → Bool, (C.env false).prob (List.ofFn x) * Real.log (1 / C.posterior false (List.ofFn x))) ≤ 2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T := by calc ∑ x : Fin T → Bool, (C.env false).prob (List.ofFn x) * Real.log (1 / C.posterior false (List.ofFn x)) ≤ ∑ x : Fin T → Bool, 2 * Real.sqrt ((C.env false).prob (List.ofFn x) * (C.env true).prob (List.ofFn x)) := by refine Finset.sum_le_sum fun x _ => ?_ have hp0 := (C.env false).prob_nonneg (List.ofFn x) have hp1 := (C.env true).prob_nonneg (List.ofFn x) by_cases h0 : (C.env false).prob (List.ofFn x) = 0 · simp only [h0, zero_mul] exact mul_nonneg (by norm_num) (Real.sqrt_nonneg _) · have hp0_pos : 0 < (C.env false).prob (List.ofFn x) := lt_of_le_of_ne hp0 (Ne.symm h0) have hmix_pos : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos (C.weight_pos false) hp0_pos) (C.mixProb_ge_term false _) have hinv : 1 / C.posterior false (List.ofFn x) = 1 + (C.env true).prob (List.ofFn x) / (C.env false).prob (List.ofFn x) := by have hmp : C.mixProb (List.ofFn x) = (1 / 2) * (C.env false).prob (List.ofFn x) + (1 / 2) * (C.env true).prob (List.ofFn x) := by simp only [SolomonoffClass.mixProb, tsum_fintype, Fintype.sum_bool, C, symBernTwoClass] ring have hwf : C.weight false = 1 / 2 := rfl simp only [SolomonoffClass.posterior, ne_of_gt hmix_pos, ↓reduceIte, hwf] rw [hmp] field_simp [ne_of_gt hp0_pos] rw [hinv] have hlog := log_one_add_le_two_sqrt (div_nonneg hp1 (le_of_lt hp0_pos)) have hmul := mul_le_mul_of_nonneg_left hlog (le_of_lt hp0_pos) have hsq : (C.env false).prob (List.ofFn x) * (2 * Real.sqrt ((C.env true).prob (List.ofFn x) / (C.env false).prob (List.ofFn x))) = 2 * Real.sqrt ((C.env false).prob (List.ofFn x) * (C.env true).prob (List.ofFn x)) := by rw [Real.sqrt_div hp1, Real.sqrt_mul (le_of_lt hp0_pos)] have hne : Real.sqrt ((C.env false).prob (List.ofFn x)) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr hp0_pos) field_simp [hne] rw [Real.sq_sqrt (le_of_lt hp0_pos)] ring linarith _ = 2 * ∑ x : Fin T → Bool, Real.sqrt ((C.env false).prob (List.ofFn x) * (C.env true).prob (List.ofFn x)) := (Finset.mul_sum _ _ _).symm _ = 2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T := by have hpow := iidBern_bhattAff_pow (1 / 2 + ε) (1 / 2 - ε) (by linarith) (by linarith) (by linarith) (by linarith) T have hbase : Real.sqrt ((1 / 2 + ε) * (1 / 2 - ε)) + Real.sqrt ((1 - (1 / 2 + ε)) * (1 - (1 / 2 - ε))) = Real.sqrt (1 - 4 * ε ^ 2) := by have h1 : (1 / 2 + ε) * (1 / 2 - ε) = (1 - 4 * ε ^ 2) / 4 := by ring have h2 : (1 - (1 / 2 + ε)) * (1 - (1 / 2 - ε)) = (1 - 4 * ε ^ 2) / 4 := by ring rw [h1, h2, Real.sqrt_div (le_of_lt h14_pos), show Real.sqrt 4 = 2 by rw [show (4 : ℝ) = 2 ^ 2 by norm_num, Real.sqrt_sq (by norm_num)]] ring rw [hbase] at hpow have hcf : C.env false = iidBernEnv (1 / 2 + ε) (by linarith) (by linarith) := rfl have hct : C.env true = iidBernEnv (1 / 2 - ε) (by linarith) (by linarith) := rfl rw [hcf, hct] exact congrArg (2 * ·) hpow -- Step 3: Combine telescoping identity + `hl1_ge` + `hsurp_le` have htel := solomonoff_exact_kl_telescoping C false T rw [hW, hw0, show (1 : ℝ) / (1 / 2) = 2 by norm_num] at htel have hlogw : Real.log (1 / C.weight false) = Real.log 2 := by rw [hw0]; norm_num rw [hlogw] have hkl_ge : Real.log 2 - 2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T ≤ ∑ t ∈ Finset.range T, ∑ x : Fin t → Bool, (C.env false).prob (List.ofFn x) * klDiv ((C.env false).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x)) := by linarith have hcomb : 2 * (1 - 4 * ε ^ 2) * (Real.log 2 - 2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T) ≤ ∑ t ∈ Finset.range T, ∑ x : Fin t → Bool, (C.env false).prob (List.ofFn x) * (l1Dist ((C.env false).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 := le_trans (mul_le_mul_of_nonneg_left hkl_ge (by positivity)) hl1_ge -- Check `c * ln 2 < 2 * (1 - 4 ε²) * (ln 2 - 2 * ρ^T)`: -- `2 * (1 - 4 ε²) ≥ 2 - (2 - c)/2 = (c + 2)/2`, and `2 * ρ^T < ln 2 * (2 - c)/4` -- so `2 * (1 - 4 ε²) * (ln 2 - 2 * ρ^T) > ((c+2)/2) * (1 - (2-c)/4) * ln 2`? -- Check algebra directly: since `1 - 4 ε² ≤ 1`, `2*(1-4ε²)*2*ρ^T ≤ 4*ρ^T < (2-c)/2 * ln 2`, -- and `2*(1-4ε²)*ln 2 ≥ (2 - (2-c)/2) * ln 2 = (c + (2-c)/2) * ln 2`! -- Subtracting gives `> c * ln 2`!! have hterm1 : (c + (2 - c) / 2) * Real.log 2 ≤ 2 * (1 - 4 * ε ^ 2) * Real.log 2 := by nlinarith have hterm2 : 2 * (1 - 4 * ε ^ 2) * (2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T) < ((2 - c) / 2) * Real.log 2 := by have h4 : 2 * (1 - 4 * ε ^ 2) * (2 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T) ≤ 4 * (Real.sqrt (1 - 4 * ε ^ 2)) ^ T := by have hpow_nn : 0 ≤ (Real.sqrt (1 - 4 * ε ^ 2)) ^ T := pow_nonneg (le_of_lt hrho_pos) T nlinarith linarith nlinarith /-! ## Section 5: Factor-1/2 Posterior Concentration & Cesàro Zero-Error Merge Rates -/ /-- Exact scalar inequality `u / (1 + u) ≤ (1/2) √u` for all `u ≥ 0`. -/ lemma div_one_add_le_half_sqrt {u : ℝ} (hu : 0 ≤ u) : u / (1 + u) ≤ (1 / 2 : ℝ) * Real.sqrt u := by rw [div_le_iff₀ (by linarith)] have hsq : Real.sqrt u * Real.sqrt u = u := Real.mul_self_sqrt hu nlinarith [sq_nonneg (1 - Real.sqrt u), Real.sqrt_nonneg u] /-- **Exact Factor-1/2 Non-Asymptotic Bayesian Posterior Concentration Bound** (finite class): `E_{P_k}[1 - w_k(X_{1:T})] ≤ (1/2) ∑_{j ≠ k} √(w_j / w_k) BC_T(P_k, P_j)`. -/ theorem solomonoff_posterior_concentration {α ι : Type*} [Fintype α] [Fintype ι] [DecidableEq ι] (C : SolomonoffClass α ι) (k : ι) (T : ℕ) : ∑ x : Fin T → α, (C.env k).prob (List.ofFn x) * (1 - C.posterior k (List.ofFn x)) ≤ (1 / 2 : ℝ) * ∑ j ∈ Finset.univ.erase k, Real.sqrt (C.weight j / C.weight k) * bhattAff (fun x : Fin T → α => (C.env k).prob (List.ofFn x)) (fun x : Fin T → α => (C.env j).prob (List.ofFn x)) := by have hwk_pos : 0 < C.weight k := C.weight_pos k -- Swap order of summation on RHS have hrhs : (1 / 2 : ℝ) * ∑ j ∈ Finset.univ.erase k, Real.sqrt (C.weight j / C.weight k) * bhattAff (fun x : Fin T → α => (C.env k).prob (List.ofFn x)) (fun x : Fin T → α => (C.env j).prob (List.ofFn x)) = ∑ x : Fin T → α, (1 / 2 : ℝ) * ∑ j ∈ Finset.univ.erase k, Real.sqrt (C.weight j / C.weight k) * Real.sqrt ((C.env k).prob (List.ofFn x) * (C.env j).prob (List.ofFn x)) := by simp only [bhattAff, Finset.mul_sum] rw [Finset.sum_comm] rw [hrhs] refine Finset.sum_le_sum fun x _ => ?_ by_cases hpx : (C.env k).prob (List.ofFn x) = 0 · rw [hpx, zero_mul] refine mul_nonneg (by norm_num) (Finset.sum_nonneg fun j _ => mul_nonneg (Real.sqrt_nonneg _) (Real.sqrt_nonneg _)) · have hpx_pos : 0 < (C.env k).prob (List.ofFn x) := lt_of_le_of_ne ((C.env k).prob_nonneg _) (Ne.symm hpx) have hmix_pos : 0 < C.mixProb (List.ofFn x) := lt_of_lt_of_le (mul_pos hwk_pos hpx_pos) (C.mixProb_ge_term k _) -- Let `S_other = ∑_{j ≠ k} w_j P_j(x)` set S_other := ∑ j ∈ Finset.univ.erase k, C.weight j * (C.env j).prob (List.ofFn x) have hS_nn : 0 ≤ S_other := Finset.sum_nonneg fun j _ => mul_nonneg (le_of_lt (C.weight_pos j)) ((C.env j).prob_nonneg _) have hmix_split : C.mixProb (List.ofFn x) = C.weight k * (C.env k).prob (List.ofFn x) + S_other := by simp only [SolomonoffClass.mixProb, tsum_fintype, S_other] rw [← Finset.add_sum_erase _ _ (Finset.mem_univ k)] have h1sub : 1 - C.posterior k (List.ofFn x) = (S_other / (C.weight k * (C.env k).prob (List.ofFn x))) / (1 + S_other / (C.weight k * (C.env k).prob (List.ofFn x))) := by simp only [SolomonoffClass.posterior, ne_of_gt hmix_pos, ↓reduceIte] rw [hmix_split] have hkp : C.weight k * (C.env k).prob (List.ofFn x) ≠ 0 := ne_of_gt (mul_pos hwk_pos hpx_pos) have hsum_ne : C.weight k * (C.env k).prob (List.ofFn x) + S_other ≠ 0 := by rw [← hmix_split]; exact ne_of_gt hmix_pos field_simp [hkp, hsum_ne] ring rw [h1sub] have hstep1 := div_one_add_le_half_sqrt (div_nonneg hS_nn (le_of_lt (mul_pos hwk_pos hpx_pos))) -- Subadditivity of `Real.sqrt`: `√(∑_{j ≠ k} a_j) ≤ ∑_{j ≠ k} √a_j` have hsqrt_sub : ∀ (s : Finset ι) (f : ι → ℝ), (∀ j ∈ s, 0 ≤ f j) → Real.sqrt (∑ j ∈ s, f j) ≤ ∑ j ∈ s, Real.sqrt (f j) := by intro s f hf rw [Real.sqrt_le_left (Finset.sum_nonneg fun j _ => Real.sqrt_nonneg _)] induction s using Finset.induction_on with | empty => simp | insert a s ha ih => rw [Finset.sum_insert ha, Finset.sum_insert ha, add_sq, Real.sq_sqrt (hf a (Finset.mem_insert_self _ _))] have hih := ih (fun j hj => hf j (Finset.mem_insert_of_mem hj)) have hnn : 0 ≤ 2 * Real.sqrt (f a) * ∑ j ∈ s, Real.sqrt (f j) := mul_nonneg (mul_nonneg (by norm_num) (Real.sqrt_nonneg _)) (Finset.sum_nonneg fun _ _ => Real.sqrt_nonneg _) linarith have hstep2 := hsqrt_sub (Finset.univ.erase k) (fun j => C.weight j * (C.env j).prob (List.ofFn x) / (C.weight k * (C.env k).prob (List.ofFn x))) (fun j _ => div_nonneg (mul_nonneg (le_of_lt (C.weight_pos j)) ((C.env j).prob_nonneg _)) (le_of_lt (mul_pos hwk_pos hpx_pos))) rw [← Finset.sum_div] at hstep2 refine le_trans (mul_le_mul_of_nonneg_left (le_trans hstep1 (mul_le_mul_of_nonneg_left hstep2 (by norm_num))) (le_of_lt hpx_pos)) ?_ rw [← mul_assoc, mul_comm ((C.env k).prob (List.ofFn x)) (1 / 2), mul_assoc, Finset.mul_sum] refine le_of_eq (congrArg ((1 / 2 : ℝ) * ·) (Finset.sum_congr rfl fun j _ => ?_)) have hdiv_eq : C.weight j * (C.env j).prob (List.ofFn x) / (C.weight k * (C.env k).prob (List.ofFn x)) = (C.weight j / C.weight k) * ((C.env j).prob (List.ofFn x) / (C.env k).prob (List.ofFn x)) := by ring rw [hdiv_eq, Real.sqrt_mul (div_nonneg (le_of_lt (C.weight_pos j)) (le_of_lt hwk_pos)), Real.sqrt_div ((C.env j).prob_nonneg _), Real.sqrt_mul (le_of_lt hpx_pos)] have hne : Real.sqrt ((C.env k).prob (List.ofFn x)) ≠ 0 := ne_of_gt (Real.sqrt_pos.mpr hpx_pos) field_simp [hne] rw [Real.sq_sqrt (le_of_lt hpx_pos)] ring /-- **Exact Cesàro Zero-Error Merge Rates** for Hellinger (`O(1/T)`) and `L₁` (`O(1/√T)`): `(1/T) ∑_{t=1}^T E[H²] ≤ ln(W / w_k) / (2T)` and `(1/T) ∑_{t=1}^T E[‖μ_k - ξ‖₁] ≤ √(2 ln(W / w_k) / T)`. -/ theorem solomonoff_cesaro_merge_rates {α ι : Type*} [Fintype α] [Nonempty α] (C : SolomonoffClass α ι) (k : ι) {T : ℕ} (hT : 1 ≤ T) : ((T : ℝ)⁻¹ * ∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * hellingerSq ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ Real.log (C.totalWeight / C.weight k) / (2 * T) ∧ ((T : ℝ)⁻¹ * ∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ≤ Real.sqrt (2 * Real.log (C.totalWeight / C.weight k) / T) := by have hTpos : (0 : ℝ) < T := by exact_mod_cast hT have ⟨_, hH2, hL1, _⟩ := solomonoff_cumulative_kl_hellinger_tv_bounds C k T constructor · rw [inv_mul_eq_div, div_le_div_iff₀ hTpos (by positivity)] nlinarith · -- Cauchy–Schwarz twice: first over `x` (weighted by `P_k(x)`), then over `t ∈ range T` have hsq_step : ∀ t : ℕ, (∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 ≤ ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * (l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 := by intro t have hcs := Finset.sum_mul_sq_le_sq_mul_sq (Finset.univ : Finset (Fin t → α)) (fun x => Real.sqrt ((C.env k).prob (List.ofFn x))) (fun x => Real.sqrt ((C.env k).prob (List.ofFn x)) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) have hl : ∑ x : Fin t → α, Real.sqrt ((C.env k).prob (List.ofFn x)) * (Real.sqrt ((C.env k).prob (List.ofFn x)) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) = ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x)) := by refine Finset.sum_congr rfl fun x _ => ?_ rw [← mul_assoc, Real.mul_self_sqrt ((C.env k).prob_nonneg _)] have hr1 : ∑ x : Fin t → α, (Real.sqrt ((C.env k).prob (List.ofFn x))) ^ 2 = 1 := by simp_rw [Real.sq_sqrt ((C.env k).prob_nonneg _)] exact (C.env k).sum_prob_eq_one t have hr2 : ∑ x : Fin t → α, (Real.sqrt ((C.env k).prob (List.ofFn x)) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 = ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * (l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 := by refine Finset.sum_congr rfl fun x _ => ?_ rw [mul_pow, Real.sq_sqrt ((C.env k).prob_nonneg _)] rw [hl, hr1, hr2, one_mul] at hcs exact hcs have hcsT := Finset.sum_mul_sq_le_sq_mul_sq (Finset.range T) (fun _ => (1 : ℝ)) (fun t => ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) simp only [one_mul, one_pow, Finset.sum_const, Finset.card_range, nsmul_eq_mul, mul_one] at hcsT have hsum_sq : (∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x))) ^ 2 ≤ (T : ℝ) * (2 * Real.log (C.totalWeight / C.weight k)) := le_trans hcsT (mul_le_mul_of_nonneg_left (le_trans (Finset.sum_le_sum fun t _ => hsq_step t) hL1) (le_of_lt hTpos)) have hnn : 0 ≤ (T : ℝ)⁻¹ * ∑ t ∈ Finset.range T, ∑ x : Fin t → α, (C.env k).prob (List.ofFn x) * l1Dist ((C.env k).cond (List.ofFn x)) (C.predictor.cond (List.ofFn x)) := by refine mul_nonneg (by positivity) (Finset.sum_nonneg fun t _ => Finset.sum_nonneg fun x _ => mul_nonneg ((C.env k).prob_nonneg _) (Finset.sum_nonneg fun _ _ => abs_nonneg _)) have hlog_nn : 0 ≤ Real.log (C.totalWeight / C.weight k) := Real.log_nonneg ((one_le_div (C.weight_pos k)).mpr (C.summable_weight.le_tsum k (fun i _ => le_of_lt (C.weight_pos i)))) rw [Real.le_sqrt hnn (by positivity)] rw [mul_pow, inv_pow, inv_eq_one_div, one_div_mul_eq_div, div_le_div_iff₀ (pow_pos hTpos 2) hTpos] nlinarith /-! ## Section 6: Exact Approximate-Solomonoff Robustness Theorem (Nov 2026 Frontier) -/ /-- **Exact Bregman / Jensen–Shannon Mixture Identity**: for any `α ∈ (0, 1)` and distributions `p, r, ρ` with mixture `ξ = α p + (1 - α) r`, `α D_KL(p ‖ ρ) + (1 - α) D_KL(r ‖ ρ) = D_KL(ξ ‖ ρ) + α D_KL(p ‖ ξ) + (1 - α) D_KL(r ‖ ξ)`. -/ theorem klDiv_mixture_bregman_identity {β : Type*} [Fintype β] {p r ρ : β → ℝ} {α : ℝ} (hα0 : 0 < α) (hα1 : α < 1) (hp : ∀ x, 0 ≤ p x) (hr : ∀ x, 0 ≤ r x) (hρ : ∀ x, 0 < α * p x + (1 - α) * r x → 0 < ρ x) : let ξ := fun x => α * p x + (1 - α) * r x α * klDiv p ρ + (1 - α) * klDiv r ρ = klDiv ξ ρ + α * klDiv p ξ + (1 - α) * klDiv r ξ := by dsimp only set ξ := fun x => α * p x + (1 - α) * r x simp only [klDiv, Finset.mul_sum, ← Finset.sum_add_distrib] refine Finset.sum_congr rfl fun x _ => ?_ by_cases hξ0 : ξ x = 0 · have hp0 : p x = 0 := by nlinarith [hp x, hr x] have hr0 : r x = 0 := by nlinarith [hp x, hr x] simp [klTerm, hξ0, hp0, hr0] · have hξpos : 0 < ξ x := lt_of_le_of_ne (by nlinarith [hp x, hr x]) (Ne.symm hξ0) have hρpos : 0 < ρ x := hρ x hξpos by_cases hp0 : p x = 0 · by_cases hr0 : r x = 0 · exfalso; exact hξ0 (by simp [ξ, hp0, hr0]) · have hrpos : 0 < r x := lt_of_le_of_ne (hr x) (Ne.symm hr0) simp only [klTerm, hp0, hr0, hξ0, ↓reduceIte, mul_zero, zero_add] have hξeq : ξ x = (1 - α) * r x := by simp [ξ, hp0] rw [Real.log_div (ne_of_gt hrpos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hξpos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hrpos) (ne_of_gt hξpos)] rw [hξeq]; ring · have hppos : 0 < p x := lt_of_le_of_ne (hp x) (Ne.symm hp0) by_cases hr0 : r x = 0 · simp only [klTerm, hp0, hr0, hξ0, ↓reduceIte, mul_zero, add_zero] have hξeq : ξ x = α * p x := by simp [ξ, hr0] rw [Real.log_div (ne_of_gt hppos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hξpos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hppos) (ne_of_gt hξpos)] rw [hξeq]; ring · have hrpos : 0 < r x := lt_of_le_of_ne (hr x) (Ne.symm hr0) simp only [klTerm, hp0, hr0, hξ0, ↓reduceIte] rw [Real.log_div (ne_of_gt hppos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hrpos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hξpos) (ne_of_gt hρpos), Real.log_div (ne_of_gt hppos) (ne_of_gt hξpos), Real.log_div (ne_of_gt hrpos) (ne_of_gt hξpos)] have hξeq : ξ x = α * p x + (1 - α) * r x := rfl rw [hξeq]; ring /-- **Exact Approximate-Solomonoff Robustness Theorem**: If `ξ̃` is any mixture dominating `p` via `ξ̃(x) ≥ α p(x)` (`α ∈ (0, 1]`, e.g. `α = w_k / W` in Solomonoff prediction) and `ρ` is ANY learned predictor approximating `ξ̃`, then `D_KL(p ‖ ρ) ≤ (1/α) D_KL(ξ̃ ‖ ρ) + ln(1/α) + 1` (with the exact sharper binary-entropy slack `((1-α)/α) ln(1/(1-α)) < 1`). -/ theorem approximate_solomonoff_robustness {β : Type*} [Fintype β] {p ξ ρ : β → ℝ} {α : ℝ} (hα0 : 0 < α) (hα1 : α ≤ 1) (hp : ∀ x, 0 ≤ p x) (_hξ : ∀ x, 0 ≤ ξ x) (hρ : ∀ x, 0 ≤ ρ x) (hp1 : ∑ x : β, p x = 1) (hξ1 : ∑ x : β, ξ x = 1) (hρ1 : ∑ x : β, ρ x ≤ 1) (hdom : ∀ x, α * p x ≤ ξ x) (habs : ∀ x, 0 < ξ x → 0 < ρ x) : klDiv p ρ ≤ (1 / α) * klDiv ξ ρ + Real.log (1 / α) + 1 := by by_cases hαeq : α = 1 · subst hαeq have hpeq : p = ξ := by ext x by_contra hne have hlt : p x < ξ x := lt_of_le_of_ne (by simpa using hdom x) hne have hsum_lt : ∑ y : β, p y < ∑ y : β, ξ y := Finset.sum_lt_sum (fun y _ => by simpa using hdom y) ⟨x, Finset.mem_univ x, hlt⟩ linarith rw [hpeq] simp · have hαlt : α < 1 := lt_of_le_of_ne hα1 hαeq let r : β → ℝ := fun x => (ξ x - α * p x) / (1 - α) have hr_nn : ∀ x, 0 ≤ r x := fun x => div_nonneg (by linarith [hdom x]) (by linarith) have h1α : 1 - α ≠ 0 := by linarith have hr1 : ∑ x : β, r x = 1 := by simp only [r, ← Finset.sum_div, Finset.sum_sub_distrib, ← Finset.mul_sum, hξ1, hp1] field_simp [h1α] have hξeq : ∀ x, α * p x + (1 - α) * r x = ξ x := fun x => by simp only [r]; field_simp [h1α]; ring have hid := klDiv_mixture_bregman_identity hα0 hαlt hp hr_nn (fun x hx => habs x (by rwa [← hξeq x])) simp only [hξeq] at hid have hkl_r_nn : 0 ≤ klDiv r ρ := klDiv_nonneg hr_nn hρ hr1 hρ1 (fun x hrx => habs x (by rw [← hξeq x]; nlinarith [hp x])) -- Bound `D_KL(p ‖ ξ) ≤ ln(1/α)` and `D_KL(r ‖ ξ) ≤ ln(1/(1-α))` have hkl_p_le : klDiv p ξ ≤ Real.log (1 / α) := by calc klDiv p ξ = ∑ x : β, klTerm (p x) (ξ x) := rfl _ ≤ ∑ x : β, p x * Real.log (1 / α) := by refine Finset.sum_le_sum fun x _ => ?_ by_cases hpx : p x = 0 · simp [klTerm, hpx] · have hpx_pos : 0 < p x := lt_of_le_of_ne (hp x) (Ne.symm hpx) have hξ_pos : 0 < ξ x := lt_of_lt_of_le (mul_pos hα0 hpx_pos) (hdom x) simp only [klTerm, hpx, ↓reduceIte] refine mul_le_mul_of_nonneg_left (Real.log_le_log (div_pos hpx_pos hξ_pos) ?_) (le_of_lt hpx_pos) rw [div_le_div_iff₀ hξ_pos hα0]; linarith [hdom x] _ = Real.log (1 / α) := by rw [← Finset.sum_mul, hp1, one_mul] have hkl_r_le : klDiv r ξ ≤ Real.log (1 / (1 - α)) := by have hdom_r : ∀ x, (1 - α) * r x ≤ ξ x := fun x => by rw [← hξeq x]; nlinarith [hp x] calc klDiv r ξ = ∑ x : β, klTerm (r x) (ξ x) := rfl _ ≤ ∑ x : β, r x * Real.log (1 / (1 - α)) := by refine Finset.sum_le_sum fun x _ => ?_ by_cases hrx : r x = 0 · simp [klTerm, hrx] · have hrx_pos : 0 < r x := lt_of_le_of_ne (hr_nn x) (Ne.symm hrx) have hξ_pos : 0 < ξ x := lt_of_lt_of_le (mul_pos (by linarith) hrx_pos) (hdom_r x) simp only [klTerm, hrx, ↓reduceIte] refine mul_le_mul_of_nonneg_left (Real.log_le_log (div_pos hrx_pos hξ_pos) ?_) (le_of_lt hrx_pos) rw [div_le_div_iff₀ hξ_pos (by linarith)]; linarith [hdom_r x] _ = Real.log (1 / (1 - α)) := by rw [← Finset.sum_mul, hr1, one_mul] have hent_le : (1 - α) * Real.log (1 / (1 - α)) ≤ α := by have h := Real.log_le_sub_one_of_pos (div_pos one_pos (by linarith : 0 < 1 - α)) have hmul := mul_le_mul_of_nonneg_left h (by linarith : 0 ≤ 1 - α) have hrhs_eq : (1 - α) * (1 / (1 - α) - 1) = α := by field_simp [h1α]; ring linarith have hαkl : α * klDiv p ρ ≤ klDiv ξ ρ + α * Real.log (1 / α) + α := by nlinarith have hdiv := (le_div_iff₀ hα0).mpr (by linarith : klDiv p ρ * α ≤ klDiv ξ ρ + α * Real.log (1 / α) + α) have hsimp : (klDiv ξ ρ + α * Real.log (1 / α) + α) / α = (1 / α) * klDiv ξ ρ + Real.log (1 / α) + 1 := by field_simp [ne_of_gt hα0] linarith /-! ## Section 7: Axiom Audit -/ #print axioms klDiv_nonneg #print axioms hellingerSq_bhatt_kl_chain #print axioms two_mul_hellingerSq_le_klDiv #print axioms pinsker_l1_sq_le_two_kl #print axioms klDiv_chain_rule #print axioms solomonoff_exact_kl_telescoping #print axioms solomonoff_cumulative_kl_hellinger_tv_bounds #print axioms solomonoff_kl_hellinger_sharp_witness #print axioms solomonoff_l1_sq_constant_two_sharp #print axioms solomonoff_posterior_concentration #print axioms solomonoff_cesaro_merge_rates #print axioms klDiv_mixture_bregman_identity #print axioms approximate_solomonoff_robustness end SolomonoffPrediction