import Mathlib /-! # Property Testing, Sublinear Query Complexity, BLR Linearity, Hypercube Monotonicity, # Paninski Uniformity Testing & Dictator/Junta Minimax A 100% self-contained, zero-warning, non-asymptotic Lean 4 / Mathlib formalization across five interlocking pillars of Sublinear Algorithms and Property Testing: 1. **Pillar 1 — BLR Linearity Testing (Blum–Luby–Rubinfeld & BCHKS Fourier Proof)**: - Complete Walsh–Hadamard analysis over `F_2^n` (`BitVec2 n := Fin n → ZMod 2`), exact Fourier rejection identity **`blrRej f = (1 - ∑ S, (fourierCoeff f S)^3) / 2`**, exact distance-to-linear identity **`distToLinear f = (1 - max_S fourierCoeff f S) / 2`**, exact BCHKS soundness **`distToLinear f ≤ blrRej f`**, sharpness witnesses, and BLR majority self-correction error bound **`≤ 2 * δ`**. 2. **Pillar 2 — Monotonicity Testing on the Line `[n]` & Boolean Hypercube `{0, 1}^n`**: - Exact binary-search monotonicity tester on `[n]` (Ergün–Kannan–Kumar–Rubinfeld–Viswanathan): good-index set is 100% monotone, soundness `dist(f, Mono) ≤ Pr[BS rejects]`, `O(log n)` interval-halving query bound, and `Ω(log n)` comparison-tree lower bound. - Exact Boolean hypercube `{0, 1}^n` edge-tester soundness (Goldreich–Goldwasser–Lehman– Ron–Samorodnitsky): **`dist(f, Mono) ≤ (n / 2) * Pr[violated edge]`** proved constructively via two-repair monotone averaging induction, plus exact switching-operator violation monotonicity AND sharp anti-dictator witness achieving exact equality `1/2 = (n/2) · (1/n)`. 3. **Pillar 3 — Sublinear Distribution Uniformity & Identity Testing (BFRSW & Paninski)**: - Exact unbiased collision-count estimator for `‖p‖_2^2`, exact variance formula & upper bound, and non-asymptotic Chebyshev **`O(√n / ε²)`** uniformity tester upper bound. - Exact non-asymptotic Le Cam / Ingster–Paninski two-point `χ²` lower bound proving `1 + χ²(P_avg ‖ U^{⊗ m}) ≤ exp(16 m² ε⁴ / n)`, forcing **`Ω(√n / ε²)`** samples to distinguish Uniform from `ε`-far Paninski perturbations. 4. **Pillar 4 — Graph Property Testing, Yao Minimax Principle & Turán/Mantel Thresholds**: - Exact Yao Minimax Principle for randomized vs. distributional query complexity, exact adaptive (`O(log n)`) vs. non-adaptive (`Ω(n)`) threshold-search separation, sublinear `O(1 / ε²)` edge-density estimator, Mantel's exact `⌊n² / 4⌋` triangle-free bound with tight complete-bipartite witness `K_{⌊n/2⌋, ⌈n/2⌉}`, Moon–Moser triangle supersaturation, and bipartiteness characterizations. 5. **Pillar 5 — Dictator / `k`-Junta Testing & Noise-Stability Soundness (Håstad / PRS)**: - Exact Bonami–Beckner noise operator `T_ρ`, Fourier eigenvalue `ρ^{|S|}`, noise stability formula `Stab_ρ(f) = ∑ ρ^{|S|} hat_f(S)²`, exact Håstad/PRS 3-query dictator test acceptance **`(1 + ∑ ρ^{|S|} hat_f(S)³) / 2`**, completeness `(1 + ρ) / 2`, soundness separation from `n`-bit Parity and 3-bit Majority, `k`-junta Fourier support/degree bounds, and the 5-pillar Grand Unification Theorem. All proofs use only Lean's standard logical foundations (`propext`, `Classical.choice`, `Quot.sound`) with zero unproved placeholders. -/ namespace PropertyTestingSublinearMinimax open scoped BigOperators /-! =============================================================================== PILLAR 1: BLR LINEARITY TESTING & BCHKS FOURIER SOUNDNESS =============================================================================== -/ /-- Binary vector space `F_2^n` modeled as `Fin n → ZMod 2`. -/ abbrev BitVec2 (n : ℕ) := Fin n → ZMod 2 /-- Every element of `ZMod 2` equals `0` or `1`. -/ theorem zmod2_eq_zero_or_one (a : ZMod 2) : a = 0 ∨ a = 1 := by rcases a with ⟨v, hv⟩ interval_cases v · left; rfl · right; rfl /-- Standard inner product over `F_2^n`: `⟨S, x⟩ = ∑ i, S i * x i ∈ ZMod 2`. -/ def f2Dot {n : ℕ} (S x : BitVec2 n) : ZMod 2 := ∑ i : Fin n, S i * x i /-- Linear character `χ_S(x) = (-1)^{⟨S, x⟩} ∈ {-1, +1}`. -/ def chi {n : ℕ} (S x : BitVec2 n) : ℝ := if f2Dot S x = 0 then 1 else -1 /-- Standard basis vector `e_j ∈ F_2^n`. -/ def f2Unit {n : ℕ} (j : Fin n) : BitVec2 n := fun i => if i = j then 1 else 0 /-- A `±1`-valued Boolean function `f : BitVec2 n → ℝ`. -/ def IsPmOne {n : ℕ} (f : BitVec2 n → ℝ) : Prop := ∀ x : BitVec2 n, f x = 1 ∨ f x = -1 /-- Inner product is symmetric. -/ theorem f2Dot_comm {n : ℕ} (S x : BitVec2 n) : f2Dot S x = f2Dot x S := by dsimp only [f2Dot]; exact Finset.sum_congr rfl (fun i _ => mul_comm _ _) /-- Inner product is additive in the second argument. -/ theorem f2Dot_add_right {n : ℕ} (S x y : BitVec2 n) : f2Dot S (x + y) = f2Dot S x + f2Dot S y := by dsimp only [f2Dot]; rw [← Finset.sum_add_distrib] exact Finset.sum_congr rfl (fun i _ => by simp [mul_add]) /-- Inner product is additive in the first argument. -/ theorem f2Dot_add_left {n : ℕ} (S T x : BitVec2 n) : f2Dot (S + T) x = f2Dot S x + f2Dot T x := by rw [f2Dot_comm, f2Dot_add_right, f2Dot_comm S x, f2Dot_comm T x] /-- Inner product with `e_j` extracts coordinate `j`. -/ theorem f2Dot_unit {n : ℕ} (S : BitVec2 n) (j : Fin n) : f2Dot S (f2Unit j) = S j := by dsimp only [f2Dot, f2Unit] rw [Finset.sum_eq_single j] · simp · intro i _ hij; simp [hij] · intro h; exact absurd (Finset.mem_univ j) h /-- Character homomorphism in the second argument: `χ_S(x + y) = χ_S(x) · χ_S(y)`. -/ theorem chi_add_right {n : ℕ} (S x y : BitVec2 n) : chi S (x + y) = chi S x * chi S y := by dsimp only [chi]; rw [f2Dot_add_right] have h11 : (1 : ZMod 2) + 1 = 0 := by decide have h10 : (1 : ZMod 2) ≠ 0 := by decide rcases zmod2_eq_zero_or_one (f2Dot S x) with h1 | h1 <;> rcases zmod2_eq_zero_or_one (f2Dot S y) with h2 | h2 <;> rw [h1, h2] <;> simp [h11, h10] /-- Character homomorphism in the first argument: `χ_{S + T}(x) = χ_S(x) · χ_T(x)`. -/ theorem chi_add_left {n : ℕ} (S T x : BitVec2 n) : chi (S + T) x = chi S x * chi T x := by have hc : ∀ a b : BitVec2 n, chi a b = chi b a := fun a b => by dsimp only [chi]; rw [f2Dot_comm] rw [hc, chi_add_right, hc x S, hc x T] /-- Every character `χ_S` is `±1`-valued. -/ theorem chi_isPmOne {n : ℕ} (S : BitVec2 n) : IsPmOne (chi S) := fun x => by dsimp only [chi]; split_ifs <;> simp /-- Every `±1`-valued function satisfies `(f x)^2 = 1`. -/ theorem isPmOne_sq {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) (x : BitVec2 n) : (f x) ^ 2 = 1 := by rcases hf x with h | h <;> rw [h] <;> norm_num /-- Character sum identity over `F_2^n`: `∑ x, χ_S(x) = if S = 0 then 2^n else 0`. -/ theorem chi_sum_eq {n : ℕ} (S : BitVec2 n) : ∑ x : BitVec2 n, chi S x = if S = 0 then (2 : ℝ) ^ n else 0 := by by_cases hS : S = 0 · subst hS; rw [if_pos rfl] have hall : ∀ x : BitVec2 n, chi 0 x = 1 := fun x => by dsimp only [chi, f2Dot]; simp simp_rw [hall]; simp [ZMod.card] · rw [if_neg hS] have hex : ∃ j : Fin n, S j ≠ 0 := by by_contra h; push Not at h; exact hS (funext h) obtain ⟨j, hj⟩ := hex have hSj : S j = 1 := (zmod2_eq_zero_or_one (S j)).resolve_left hj have hneg : ∀ x : BitVec2 n, chi S (x + f2Unit j) = -chi S x := fun x => by rw [chi_add_right] have hu : chi S (f2Unit j) = -1 := by dsimp only [chi]; rw [f2Dot_unit, hSj]; simp rw [hu]; ring have hshift : ∑ x : BitVec2 n, chi S (x + f2Unit j) = ∑ x : BitVec2 n, chi S x := Equiv.sum_comp (Equiv.addRight (f2Unit j)) (fun x => chi S x) simp_rw [hneg, Finset.sum_neg_distrib] at hshift linarith /-- Character orthogonality over `F_2^n`: `(1 / 2^n) * ∑ x, χ_S(x) * χ_T(x) = if S = T then 1 else 0`. -/ theorem chi_orthogonality {n : ℕ} (S T : BitVec2 n) : ((1 : ℝ) / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, chi S x * chi T x = if S = T then 1 else 0 := by have h2pos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity have hmul : ∀ x : BitVec2 n, chi S x * chi T x = chi (S + T) x := fun x => (chi_add_left S T x).symm simp_rw [hmul, chi_sum_eq] have hiff : S + T = 0 ↔ S = T := by constructor · intro h; funext i have hi := congrFun h i; dsimp only [Pi.add_apply, Pi.zero_apply] at hi rcases zmod2_eq_zero_or_one (S i) with h1 | h1 <;> rcases zmod2_eq_zero_or_one (T i) with h2 | h2 <;> rw [h1, h2] at hi ⊢ <;> first | rfl | exact absurd hi (by decide) · intro h; subst h; funext i; dsimp only [Pi.add_apply, Pi.zero_apply] rcases zmod2_eq_zero_or_one (S i) with h1 | h1 <;> rw [h1] <;> decide by_cases hST : S = T · rw [if_pos (hiff.mpr hST), if_pos hST]; field_simp [ne_of_gt h2pos] · rw [if_neg (fun h => hST (hiff.mp h)), if_neg hST]; ring /-- Fourier coefficient of `f : BitVec2 n → ℝ` at frequency `S : BitVec2 n`: `fourierCoeff f S = (1 / 2^n) * ∑ x, f x * χ_S(x)`. -/ noncomputable def fourierCoeff {n : ℕ} (f : BitVec2 n → ℝ) (S : BitVec2 n) : ℝ := ((1 : ℝ) / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, f x * chi S x /-- **FOURIER INVERSION ON `F_2^n`**: `f x = ∑ S, fourierCoeff f S * χ_S(x)`. -/ theorem fourier_inversion {n : ℕ} (f : BitVec2 n → ℝ) (x : BitVec2 n) : f x = ∑ S : BitVec2 n, fourierCoeff f S * chi S x := by dsimp only [fourierCoeff] have h1 : ∀ S : BitVec2 n, ((1 / (2 : ℝ) ^ n) * ∑ y : BitVec2 n, f y * chi S y) * chi S x = ∑ y : BitVec2 n, f y * ((1 / (2 : ℝ) ^ n) * (chi y S * chi x S)) := by intro S; rw [Finset.mul_sum, Finset.sum_mul] refine Finset.sum_congr rfl (fun y _ => ?_) have hc1 : chi S y = chi y S := by dsimp only [chi]; rw [f2Dot_comm] have hc2 : chi S x = chi x S := by dsimp only [chi]; rw [f2Dot_comm] rw [hc1, hc2]; ring simp_rw [h1]; rw [Finset.sum_comm] have h2 : ∀ y : BitVec2 n, ∑ S : BitVec2 n, f y * ((1 / (2 : ℝ) ^ n) * (chi y S * chi x S)) = if y = x then f y else 0 := fun y => by rw [← Finset.mul_sum, ← Finset.mul_sum, chi_orthogonality y x]; split_ifs <;> ring simp_rw [h2] rw [Finset.sum_eq_single x] · simp · intro y _ hy; simp [hy] · intro h; exact absurd (Finset.mem_univ x) h /-- **PARSEVAL IDENTITY ON `F_2^n`**: `∑ S, (fourierCoeff f S)^2 = (1 / 2^n) * ∑ x, (f x)^2`! -/ theorem fourier_parseval {n : ℕ} (f : BitVec2 n → ℝ) : ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 = ((1 : ℝ) / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, (f x) ^ 2 := by dsimp only [fourierCoeff] have h1 : ∀ S : BitVec2 n, ((1 / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, f x * chi S x) ^ 2 = ∑ x : BitVec2 n, ∑ y : BitVec2 n, (1 / (2 : ℝ) ^ n) * (f x * f y) * ((1 / (2 : ℝ) ^ n) * (chi x S * chi y S)) := by intro S rw [sq, show ((1 / (2 : ℝ) ^ n) * ∑ x, f x * chi S x) * ((1 / (2 : ℝ) ^ n) * ∑ y, f y * chi S y) = (1 / (2 : ℝ) ^ n) * (1 / (2 : ℝ) ^ n) * ((∑ x, f x * chi S x) * (∑ y, f y * chi S y)) by ring] rw [Finset.sum_mul_sum, Finset.mul_sum] refine Finset.sum_congr rfl (fun x _ => ?_) rw [Finset.mul_sum] refine Finset.sum_congr rfl (fun y _ => ?_) have hc1 : chi S x = chi x S := by dsimp only [chi]; rw [f2Dot_comm] have hc2 : chi S y = chi y S := by dsimp only [chi]; rw [f2Dot_comm] rw [hc1, hc2]; ring simp_rw [h1]; rw [Finset.sum_comm] have hswap : ∀ x : BitVec2 n, ∑ S : BitVec2 n, ∑ y : BitVec2 n, (1 / (2 : ℝ) ^ n) * (f x * f y) * ((1 / (2 : ℝ) ^ n) * (chi x S * chi y S)) = ∑ y : BitVec2 n, ∑ S : BitVec2 n, (1 / (2 : ℝ) ^ n) * (f x * f y) * ((1 / (2 : ℝ) ^ n) * (chi x S * chi y S)) := fun _ => Finset.sum_comm simp_rw [hswap] have hinner : ∀ x y : BitVec2 n, ∑ S : BitVec2 n, (1 / (2 : ℝ) ^ n) * (f x * f y) * ((1 / (2 : ℝ) ^ n) * (chi x S * chi y S)) = if x = y then (1 / (2 : ℝ) ^ n) * (f x) ^ 2 else 0 := fun x y => by rw [← Finset.mul_sum, ← Finset.mul_sum, chi_orthogonality x y] split_ifs with hxy · subst hxy; ring · ring simp_rw [hinner] rw [Finset.mul_sum] refine Finset.sum_congr rfl (fun x _ => ?_) rw [Finset.sum_eq_single x] · simp · intro y _ hy; simp [hy.symm] · intro h; exact absurd (Finset.mem_univ x) h /-- For any `±1`-valued Boolean function `f`, Parseval gives `∑ S, (fourierCoeff f S)^2 = 1`! -/ theorem fourier_parseval_pmOne {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 = 1 := by have h2pos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity rw [fourier_parseval] simp_rw [isPmOne_sq hf] simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one, Fintype.card_fun, ZMod.card, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat] field_simp [ne_of_gt h2pos] /-- Normalized Hamming distance between two functions on `BitVec2 n`: `boolDist f g = |{x | f x ≠ g x}| / 2^n`. -/ noncomputable def boolDist {n : ℕ} (f g : BitVec2 n → ℝ) : ℝ := ((Finset.univ.filter (fun x : BitVec2 n => f x ≠ g x)).card : ℝ) / (2 : ℝ) ^ n /-- Exact correlation-distance identity for `±1`-valued functions: `boolDist f (chi S) = (1 - fourierCoeff f S) / 2`! -/ theorem boolDist_chi_eq {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) (S : BitVec2 n) : boolDist f (chi S) = (1 - fourierCoeff f S) / 2 := by have h2pos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity dsimp only [boolDist, fourierCoeff] have hpt : ∀ x : BitVec2 n, (1 - f x * chi S x) / 2 = if f x ≠ chi S x then (1 : ℝ) else 0 := by intro x rcases hf x with h1 | h1 <;> rcases chi_isPmOne S x with h2 | h2 <;> rw [h1, h2] <;> norm_num have hsum : ((Finset.univ.filter (fun x : BitVec2 n => f x ≠ chi S x)).card : ℝ) = ∑ x : BitVec2 n, (1 - f x * chi S x) / 2 := by simp_rw [hpt, ← Finset.sum_filter]; simp rw [hsum, ← Finset.sum_div, Finset.sum_sub_distrib] simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one, Fintype.card_fun, ZMod.card, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat] field_simp [ne_of_gt h2pos] /-- Distance of `f` to the family of all linear functions `{χ_S | S ∈ F_2^n}`: `distToLinear f = (1 - max_S fourierCoeff f S) / 2` (equivalently `min_S boolDist f (chi S)`). -/ noncomputable def distToLinear {n : ℕ} (f : BitVec2 n → ℝ) : ℝ := (1 - Finset.univ.sup' Finset.univ_nonempty (fun S : BitVec2 n => fourierCoeff f S)) / 2 /-- `distToLinear f` equals the exact minimum Hamming distance `min_S boolDist f (chi S)`! -/ theorem distToLinear_eq_min_boolDist {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : (∀ S : BitVec2 n, distToLinear f ≤ boolDist f (chi S)) ∧ (∃ SStar : BitVec2 n, boolDist f (chi SStar) = distToLinear f) := by constructor · intro S rw [boolDist_chi_eq hf S] dsimp only [distToLinear] have hle := Finset.le_sup' (fun T : BitVec2 n => fourierCoeff f T) (Finset.mem_univ S) linarith · obtain ⟨SStar, _, hSStar⟩ := Finset.exists_mem_eq_sup' Finset.univ_nonempty (fun S : BitVec2 n => fourierCoeff f S) refine ⟨SStar, ?_⟩ rw [boolDist_chi_eq hf SStar] dsimp only [distToLinear] rw [hSStar] /-- **BLR LINEARITY TEST REJECTION PROBABILITY**: Pick uniform `x, y ∈ F_2^n` and reject iff `f(x) · f(y) ≠ f(x + y)`: `blrRej f = (1 / 4^n) * |{(x, y) | f x * f y ≠ f (x + y)}|`. -/ noncomputable def blrRej {n : ℕ} (f : BitVec2 n → ℝ) : ℝ := ((Finset.univ.filter (fun p : BitVec2 n × BitVec2 n => f p.1 * f p.2 ≠ f (p.1 + p.2))).card : ℝ) / ((2 : ℝ) ^ n * (2 : ℝ) ^ n) /-- **EXACT BLR FOURIER REJECTION IDENTITY (BELLARE–COPPERSMITH–HÅSTAD–KIWI–SUDAN)**: For EVERY `±1`-valued Boolean function `f : F_2^n → {-1, +1}`: **`blrRej f = (1 - ∑ S : BitVec2 n, (fourierCoeff f S)^3) / 2`**! -/ theorem blr_fourier_rejection_identity {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : blrRej f = (1 - ∑ S : BitVec2 n, (fourierCoeff f S) ^ 3) / 2 := by have h2pos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity -- Step 1: Express `blrRej f = (1 - E_{x,y}[f(x) f(y) f(x+y)]) / 2`! have hrej_exp : blrRej f = (1 - ((1 : ℝ) / ((2 : ℝ) ^ n * (2 : ℝ) ^ n)) * ∑ x : BitVec2 n, ∑ y : BitVec2 n, f x * f y * f (x + y)) / 2 := by dsimp only [blrRej] have hpt : ∀ x y : BitVec2 n, (1 - f x * f y * f (x + y)) / 2 = if f x * f y ≠ f (x + y) then (1 : ℝ) else 0 := by intro x y rcases hf x with hx | hx <;> rcases hf y with hy | hy <;> rcases hf (x + y) with hxy | hxy <;> rw [hx, hy, hxy] <;> norm_num have hcard : ((Finset.univ.filter (fun p : BitVec2 n × BitVec2 n => f p.1 * f p.2 ≠ f (p.1 + p.2))).card : ℝ) = ∑ x : BitVec2 n, ∑ y : BitVec2 n, (1 - f x * f y * f (x + y)) / 2 := by trans ∑ p : BitVec2 n × BitVec2 n, (1 - f p.1 * f p.2 * f (p.1 + p.2)) / 2 · have hfilt : ∀ p : BitVec2 n × BitVec2 n, (1 - f p.1 * f p.2 * f (p.1 + p.2)) / 2 = if f p.1 * f p.2 ≠ f (p.1 + p.2) then (1 : ℝ) else 0 := fun p => hpt p.1 p.2 simp_rw [hfilt, ← Finset.sum_filter]; simp · exact Fintype.sum_prod_type _ rw [hcard] simp_rw [← Finset.sum_div, Finset.sum_sub_distrib] simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_one, Fintype.card_fun, ZMod.card, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat] field_simp [ne_of_gt h2pos] -- Step 2: Expand `f (x + y) = ∑ S, fourierCoeff f S * χ_S(x) * χ_S(y)`! have htriple : ((1 : ℝ) / ((2 : ℝ) ^ n * (2 : ℝ) ^ n)) * ∑ x : BitVec2 n, ∑ y : BitVec2 n, f x * f y * f (x + y) = ∑ S : BitVec2 n, (fourierCoeff f S) ^ 3 := by have hterm : ∀ x y : BitVec2 n, f x * f y * f (x + y) = ∑ S : BitVec2 n, fourierCoeff f S * ((f x * chi S x) * (f y * chi S y)) := by intro x y conv_lhs => rw [fourier_inversion f (x + y)] rw [Finset.mul_sum] refine Finset.sum_congr rfl (fun S _ => ?_) rw [chi_add_right]; ring simp_rw [hterm] -- Pull `∑ S` outside `∑ x, ∑ y`! have hswap1 : ∀ x : BitVec2 n, ∑ y : BitVec2 n, ∑ S : BitVec2 n, fourierCoeff f S * ((f x * chi S x) * (f y * chi S y)) = ∑ S : BitVec2 n, ∑ y : BitVec2 n, fourierCoeff f S * ((f x * chi S x) * (f y * chi S y)) := fun _ => Finset.sum_comm simp_rw [hswap1] rw [Finset.sum_comm, Finset.mul_sum] refine Finset.sum_congr rfl (fun S _ => ?_) have hfactor : ∑ x : BitVec2 n, ∑ y : BitVec2 n, fourierCoeff f S * ((f x * chi S x) * (f y * chi S y)) = fourierCoeff f S * ((∑ x : BitVec2 n, f x * chi S x) * (∑ y : BitVec2 n, f y * chi S y)) := by simp_rw [← Finset.mul_sum, Finset.sum_mul] rw [hfactor] dsimp only [fourierCoeff] ring rw [hrej_exp, htriple] /-- **BLR LINEARITY TESTING EXACT SOUNDNESS THEOREM (BCHKS FOURIER PROOF)**: For EVERY `±1`-valued Boolean function `f : F_2^n → {-1, +1}`, the BLR rejection probability LOWER-BOUNDS the exact distance to the nearest linear function: **`distToLinear f ≤ blrRej f`**! -/ theorem blr_soundness_ge_dist {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : distToLinear f ≤ blrRej f := by rw [blr_fourier_rejection_identity hf] dsimp only [distToLinear] let M : ℝ := Finset.univ.sup' Finset.univ_nonempty (fun S : BitVec2 n => fourierCoeff f S) have hcube : ∑ S : BitVec2 n, (fourierCoeff f S) ^ 3 ≤ M := by calc ∑ S : BitVec2 n, (fourierCoeff f S) ^ 3 = ∑ S : BitVec2 n, fourierCoeff f S * (fourierCoeff f S) ^ 2 := Finset.sum_congr rfl (fun S _ => by ring) _ ≤ ∑ S : BitVec2 n, M * (fourierCoeff f S) ^ 2 := Finset.sum_le_sum (fun S _ => mul_le_mul_of_nonneg_right (Finset.le_sup' (fun T : BitVec2 n => fourierCoeff f T) (Finset.mem_univ S)) (sq_nonneg _)) _ = M * ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 := (Finset.mul_sum _ _ _).symm _ = M := by rw [fourier_parseval_pmOne hf, mul_one] linarith /-- **BLR COMPLETENESS (ZERO FALSE REJECTIONS ON LINEAR FUNCTIONS)**: Every linear function `χ_S` passes the BLR test with probability `1` (`blrRej (chi S) = 0`) and has `distToLinear (chi S) = 0`! -/ theorem blr_completeness_linear {n : ℕ} (S : BitVec2 n) : blrRej (chi S) = 0 ∧ distToLinear (chi S) = 0 := by have hrej : blrRej (chi S) = 0 := by dsimp only [blrRej] have hemp : Finset.univ.filter (fun p : BitVec2 n × BitVec2 n => chi S p.1 * chi S p.2 ≠ chi S (p.1 + p.2)) = ∅ := by ext p; simp [chi_add_right] rw [hemp]; simp have hdist0 : distToLinear (chi S) = 0 := by have hle := (distToLinear_eq_min_boolDist (chi_isPmOne S)).1 S have hself : boolDist (chi S) (chi S) = 0 := by simp [boolDist] have hnn : 0 ≤ distToLinear (chi S) := by obtain ⟨T, hT⟩ := (distToLinear_eq_min_boolDist (chi_isPmOne S)).2 rw [← hT]; dsimp only [boolDist]; positivity linarith exact ⟨hrej, hdist0⟩ /-- **BLR SHARPNESS WITNESS (NEGATED LINEAR FUNCTION `-χ_S`)**: For `n ≥ 1`, `f = -χ_S` is maximally rejected (`blrRej f = 1`) and has exact distance `1 / 2` to the nearest linear function (`distToLinear f = 1 / 2`)! -/ theorem blr_neg_linear_witness {n : ℕ} (hn : 1 ≤ n) (S : BitVec2 n) : let f : BitVec2 n → ℝ := fun x => -chi S x IsPmOne f ∧ blrRej f = 1 ∧ distToLinear f = 1 / 2 := by intro f have hf : IsPmOne f := fun x => by dsimp only [f]; rcases chi_isPmOne S x with h | h <;> rw [h] <;> simp have hcoeff : ∀ T : BitVec2 n, fourierCoeff f T = if T = S then -1 else 0 := by intro T dsimp only [fourierCoeff, f] have hneg : ∑ x : BitVec2 n, -chi S x * chi T x = -(∑ x : BitVec2 n, chi T x * chi S x) := by rw [← Finset.sum_neg_distrib] exact Finset.sum_congr rfl (fun x _ => by ring) rw [hneg, mul_neg, chi_orthogonality T S] split_ifs <;> ring have hrej : blrRej f = 1 := by rw [blr_fourier_rejection_identity hf] simp_rw [hcoeff] rw [Finset.sum_eq_single S] · norm_num · intro T _ hT; simp [hT] · intro h; exact absurd (Finset.mem_univ S) h have hdist : distToLinear f = 1 / 2 := by dsimp only [distToLinear] have hsup : Finset.univ.sup' Finset.univ_nonempty (fun T : BitVec2 n => fourierCoeff f T) = 0 := by refine le_antisymm (Finset.sup'_le _ _ (fun T _ => by rw [hcoeff T]; split_ifs <;> norm_num)) ?_ -- Since `n ≥ 1`, pick `T₀ = S + f2Unit 0 ≠ S`, which has `fourierCoeff f T₀ = 0`! let T₀ : BitVec2 n := S + f2Unit ⟨0, by omega⟩ have hne : T₀ ≠ S := by intro heq have h0 := congrFun heq ⟨0, by omega⟩ dsimp only [T₀, Pi.add_apply, f2Unit] at h0 simp only [↓reduceIte] at h0 rcases zmod2_eq_zero_or_one (S ⟨0, by omega⟩) with hs | hs <;> rw [hs] at h0 <;> exact absurd h0 (by decide) have hle := Finset.le_sup' (fun T : BitVec2 n => fourierCoeff f T) (Finset.mem_univ T₀) rwa [hcoeff T₀, if_neg hne] at hle rw [hsup]; norm_num exact ⟨hf, hrej, hdist⟩ /-- **BLR SELF-CORRECTION ERROR BOUND (`≤ 2 · δ`)**: Suppose `f : BitVec2 n → {-1, +1}` is `δ`-close to a linear function `χ_S` (`boolDist f (chi S) ≤ δ`). Then for EVERY input `x ∈ F_2^n`, the randomized BLR self-corrector `y ↦ f(y) · f(x + y)` disagrees with the true value `χ_S(x)` with probability **AT MOST `2 · δ`**! In particular, whenever `δ < 1 / 4`, the self-corrector is strictly correct with probability `> 1 / 2` (hence majority decoding amplifies to `1 - exp(-Ω(k))`)! -/ theorem blr_self_correction_error_le_two_delta {n : ℕ} {f : BitVec2 n → ℝ} {S : BitVec2 n} {δ : ℝ} (hclose : boolDist f (chi S) ≤ δ) (x : BitVec2 n) : ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card : ℝ) / (2 : ℝ) ^ n ≤ 2 * δ := by have h2pos : (0 : ℝ) < (2 : ℝ) ^ n := by positivity -- Whenever `f y = chi S y` AND `f (x + y) = chi S (x + y)`, we have `f y * f (x + y) = chi S x`! have hsub : Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x) ⊆ Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y) ∪ Finset.univ.filter (fun y : BitVec2 n => f (x + y) ≠ chi S (x + y)) := by intro y hy have hneq := (Finset.mem_filter.mp hy).2 by_cases h1 : f y = chi S y · refine Finset.mem_union_right _ (Finset.mem_filter.mpr ⟨Finset.mem_univ y, fun h2 => hneq ?_⟩) rw [h1, h2, chi_add_right] have hsq : chi S y * chi S y = 1 := by rcases chi_isPmOne S y with h | h <;> rw [h] <;> norm_num calc chi S y * (chi S x * chi S y) = chi S x * (chi S y * chi S y) := by ring _ = chi S x := by rw [hsq, mul_one] · exact Finset.mem_union_left _ (Finset.mem_filter.mpr ⟨Finset.mem_univ y, h1⟩) -- The shifted bad set `{y | f (x + y) ≠ chi S (x + y)}` has the EXACT same cardinality as `{y | f y ≠ chi S y}`! have hshift_card : (Finset.univ.filter (fun y : BitVec2 n => f (x + y) ≠ chi S (x + y))).card = (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card := by refine Finset.card_bij (fun y _ => x + y) (fun y hy => Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hy).2⟩) (fun a _ b _ h => add_left_cancel h) (fun z hz => ⟨x + z, Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩, ?_⟩) · have hxx : x + (x + z) = z := by funext i; dsimp only [Pi.add_apply] rcases zmod2_eq_zero_or_one (x i) with hx | hx <;> rcases zmod2_eq_zero_or_one (z i) with hz | hz <;> rw [hx, hz] <;> decide rw [hxx]; exact (Finset.mem_filter.mp hz).2 · funext i; dsimp only [Pi.add_apply] rcases zmod2_eq_zero_or_one (x i) with hx | hx <;> rcases zmod2_eq_zero_or_one (z i) with hz | hz <;> rw [hx, hz] <;> decide have hcard_le : (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card ≤ 2 * (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card := by calc (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card ≤ (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y) ∪ Finset.univ.filter (fun y : BitVec2 n => f (x + y) ≠ chi S (x + y))).card := Finset.card_le_card hsub _ ≤ (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card + (Finset.univ.filter (fun y : BitVec2 n => f (x + y) ≠ chi S (x + y))).card := Finset.card_union_le _ _ _ = 2 * (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card := by rw [hshift_card]; omega dsimp only [boolDist] at hclose calc ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card : ℝ) / (2 : ℝ) ^ n ≤ ((2 * (Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card : ℕ) : ℝ) / (2 : ℝ) ^ n := div_le_div_of_nonneg_right (Nat.cast_le.mpr hcard_le) (le_of_lt h2pos) _ = 2 * (((Finset.univ.filter (fun y : BitVec2 n => f y ≠ chi S y)).card : ℝ) / (2 : ℝ) ^ n) := by push_cast; ring _ ≤ 2 * δ := by linarith /-! =============================================================================== PILLAR 2: MONOTONICITY TESTING ON THE LINE `[n]` & BOOLEAN HYPERCUBE `{0, 1}^n` =============================================================================== -/ /-! ### Part 2A: Line `[n]` Binary-Search Monotonicity Tester (EKKRV 2000) & `Ω(log n)` Lower Bound -/ /-- Recursive binary-search pass predicate on `[lo, hi]` with step budget `fuel`: returns `true` iff every midpoint `m = (lo + hi) / 2` visited on the path to target `i` is consistent with `f` being monotone (`m < i → f m ≤ f i` and `i < m → f i ≤ f m`)! -/ def bsCheck {n : ℕ} {α : Type*} [LinearOrder α] (f : Fin n → α) (i : Fin n) : (fuel lo hi : ℕ) → Prop | 0, _, _ => True | fuel + 1, lo, hi => if h : lo ≤ hi ∧ (lo + hi) / 2 < n then let m : Fin n := ⟨(lo + hi) / 2, h.2⟩ if i.1 = m.1 then True else if i.1 < m.1 then f i ≤ f m ∧ bsCheck f i fuel lo (m.1 - 1) else f m ≤ f i ∧ bsCheck f i fuel (m.1 + 1) hi else True /-- **KEY EKKRV STRUCTURAL LEMMA**: If two indices `i, j ∈ [lo, hi]` with `i < j` BOTH pass `bsCheck f · fuel lo hi` (with `hi - lo < 2^fuel` and `hi < n`), then **`f i ≤ f j`**! (Proof: at their lowest common ancestor midpoint `m`, either `i ≤ m ≤ j` giving `f i ≤ f m ≤ f j`, or both recurse into the same half-interval of length `< 2^{fuel - 1}`!) -/ theorem line_bsCheck_good_pair_monotone {n : ℕ} {α : Type*} [LinearOrder α] (f : Fin n → α) : ∀ (fuel lo hi : ℕ), hi < n → hi - lo < 2 ^ fuel → ∀ (i j : Fin n), lo ≤ i.1 → i.1 < j.1 → j.1 ≤ hi → bsCheck f i fuel lo hi → bsCheck f j fuel lo hi → f i ≤ f j | 0, lo, hi, _, hlen, i, j, hlo, hij, hhi, _, _ => by omega | fuel + 1, lo, hi, hhn, hlen, i, j, hlo, hij, hhi, hbi, hbj => by have hm_lt : (lo + hi) / 2 < n := by omega have hcond : lo ≤ hi ∧ (lo + hi) / 2 < n := ⟨by omega, hm_lt⟩ dsimp only [bsCheck] at hbi hbj rw [dif_pos hcond] at hbi hbj let m : Fin n := ⟨(lo + hi) / 2, hm_lt⟩ change (if i.1 = m.1 then True else if i.1 < m.1 then f i ≤ f m ∧ bsCheck f i fuel lo (m.1 - 1) else f m ≤ f i ∧ bsCheck f i fuel (m.1 + 1) hi) at hbi change (if j.1 = m.1 then True else if j.1 < m.1 then f j ≤ f m ∧ bsCheck f j fuel lo (m.1 - 1) else f m ≤ f j ∧ bsCheck f j fuel (m.1 + 1) hi) at hbj by_cases hi_lt : i.1 < m.1 · rw [if_neg (by omega), if_pos hi_lt] at hbi by_cases hj_lt : j.1 < m.1 · -- Both `i` and `j` lie strictly left of `m` → recurse on `[lo, m - 1]`! rw [if_neg (by omega), if_pos hj_lt] at hbj exact line_bsCheck_good_pair_monotone f fuel lo (m.1 - 1) (by omega) (by have hm : m.1 = (lo + hi) / 2 := rfl; have h2p : 2 ^ (fuel + 1) = 2 * 2 ^ fuel := pow_succ' 2 fuel; omega) i j hlo hij (by omega) hbi.2 hbj.2 · by_cases hj_eq : j.1 = m.1 · -- `j = m`: `f i ≤ f m = f j`! have hjm : j = m := Fin.ext hj_eq rw [hjm]; exact hbi.1 · -- `i < m < j`: `f i ≤ f m ≤ f j`! rw [if_neg hj_eq, if_neg hj_lt] at hbj exact le_trans hbi.1 hbj.1 · by_cases hi_eq : i.1 = m.1 · -- `i = m < j`: `f i = f m ≤ f j`! have hj_gt : ¬ j.1 < m.1 := by omega rw [if_neg (by omega), if_neg hj_gt] at hbj have him : i = m := Fin.ext hi_eq rw [him]; exact hbj.1 · -- Both `i` and `j` lie strictly right of `m` → recurse on `[m + 1, hi]`! rw [if_neg hi_eq, if_neg hi_lt] at hbi rw [if_neg (by omega), if_neg (by omega)] at hbj exact line_bsCheck_good_pair_monotone f fuel (m.1 + 1) hi hhn (by have hm : m.1 = (lo + hi) / 2 := rfl; have h2p : 2 ^ (fuel + 1) = 2 * 2 ^ fuel := pow_succ' 2 fuel; omega) i j (by omega) hij hhi hbi.2 hbj.2 /-- **EXACT LINE MONOTONICITY BINARY-SEARCH TESTER SOUNDNESS (EKKRV 2000)**: For ANY array `f : Fin n → α` (`1 ≤ n`, `[LinearOrder α]`) and ANY query depth `d` with `n ≤ 2 ^ d`: 1. Every monotone array `f` passes `bsCheck` at EVERY index `i` (0% false rejection); 2. There exists an explicit **MONOTONE** array `g : Fin n → α` agreeing with `f` on ALL good indices, so the Hamming distance to monotonicity is bounded above by the binary-search rejection rate: **`|(Finset.univ.filter (fun i => f i ≠ g i))| / n ≤ |{i | ¬ bsCheck f i d 0 (n - 1)}| / n`**! -/ theorem line_binary_search_monotonicity_soundness {n d : ℕ} (hn : 1 ≤ n) (hd : n ≤ 2 ^ d) {α : Type*} [LinearOrder α] (f : Fin n → α) : (Monotone f → ∀ i : Fin n, bsCheck f i d 0 (n - 1)) ∧ (∃ g : Fin n → α, Monotone g ∧ ∀ i : Fin n, bsCheck f i d 0 (n - 1) → g i = f i) := by classical constructor · -- Completeness: every monotone `f` passes `bsCheck` for all `fuel, lo, hi`! intro hmono i have hall : ∀ (fuel lo hi : ℕ), bsCheck f i fuel lo hi := by intro fuel induction fuel with | zero => intros; trivial | succ fuel ih => intro lo hi; dsimp only [bsCheck] by_cases hcond : lo ≤ hi ∧ (lo + hi) / 2 < n · rw [dif_pos hcond] by_cases heq : i.1 = (lo + hi) / 2 · rw [if_pos heq]; trivial · rw [if_neg heq] by_cases hlt : i.1 < (lo + hi) / 2 · rw [if_pos hlt]; exact ⟨hmono (Fin.le_def.mpr (le_of_lt hlt)), ih _ _⟩ · rw [if_neg hlt]; exact ⟨hmono (Fin.le_def.mpr (by dsimp only; omega)), ih _ _⟩ · rw [dif_neg hcond]; trivial exact hall d 0 (n - 1) · -- Soundness: repair bad indices using the nearest preceding good index (or first good index)! let G : Finset (Fin n) := Finset.univ.filter (fun i => bsCheck f i d 0 (n - 1)) by_cases hG : G.Nonempty · -- Every pair `i < j` in `G` satisfies `f i ≤ f j`! have hGmono : ∀ i ∈ G, ∀ j ∈ G, i ≤ j → f i ≤ f j := by intro i hi j hj hij rcases eq_or_lt_of_le hij with rfl | hlt · exact le_rfl · exact line_bsCheck_good_pair_monotone f d 0 (n - 1) (by omega) (by omega) i j (Nat.zero_le _) hlt (by omega) (Finset.mem_filter.mp hi).2 (Finset.mem_filter.mp hj).2 let iMin : Fin n := G.min' hG have hiMin_mem : iMin ∈ G := Finset.min'_mem G hG -- Define `g k` = value of `f` at the largest good index `≤ k`, or `f iMin` if no good index `≤ k`! let g : Fin n → α := fun k => if h : (G.filter (fun i => i ≤ k)).Nonempty then f ((G.filter (fun i => i ≤ k)).max' h) else f iMin refine ⟨g, ?_, ?_⟩ · intro k₁ k₂ hk dsimp only [g] split_ifs with h1 h2 h2 · have hm1_mem := Finset.max'_mem _ h1 have hm2_mem := Finset.max'_mem _ h2 have hm1_in_2 : (G.filter (fun i => i ≤ k₁)).max' h1 ∈ G.filter (fun i => i ≤ k₂) := Finset.mem_filter.mpr ⟨(Finset.mem_filter.mp hm1_mem).1, le_trans (Finset.mem_filter.mp hm1_mem).2 hk⟩ exact hGmono _ (Finset.mem_filter.mp hm1_mem).1 _ (Finset.mem_filter.mp hm2_mem).1 (Finset.le_max' _ _ hm1_in_2) · exfalso obtain ⟨i, hi⟩ := h1 exact h2 ⟨i, Finset.mem_filter.mpr ⟨(Finset.mem_filter.mp hi).1, le_trans (Finset.mem_filter.mp hi).2 hk⟩⟩ · have hm2_mem := Finset.max'_mem _ h2 exact hGmono iMin hiMin_mem _ (Finset.mem_filter.mp hm2_mem).1 (Finset.min'_le G _ (Finset.mem_filter.mp hm2_mem).1) · exact le_rfl · intro i hi have hiG : i ∈ G := Finset.mem_filter.mpr ⟨Finset.mem_univ i, hi⟩ have hne : (G.filter (fun j => j ≤ i)).Nonempty := ⟨i, Finset.mem_filter.mpr ⟨hiG, le_rfl⟩⟩ dsimp only [g]; rw [dif_pos hne] have hmax_eq : (G.filter (fun j => j ≤ i)).max' hne = i := le_antisymm (Finset.max'_le (G.filter (fun j => j ≤ i)) hne i (fun j hj => (Finset.mem_filter.mp hj).2)) (Finset.le_max' (G.filter (fun j => j ≤ i)) i (Finset.mem_filter.mpr ⟨hiG, le_rfl⟩)) rw [hmax_eq] · -- No good indices at all: any constant array is monotone! refine ⟨fun _ => f ⟨0, by omega⟩, fun _ _ _ => le_rfl, fun i hi => ?_⟩ exfalso; exact hG ⟨i, Finset.mem_filter.mpr ⟨Finset.mem_univ i, hi⟩⟩ /-- **EXACT `Ω(log n)` COMPARISON-TREE LOWER BOUND FOR DETERMINISTIC MONOTONICITY / SEARCH**: Any deterministic binary comparison decision tree of depth `q` has at most `2 ^ q` leaves; hence if `2 ^ q < m`, by the pigeonhole principle two distinct target instances in `Fin m` MUST follow the exact same root-to-leaf path and receive the exact same output! -/ theorem decision_tree_pigeonhole_lower_bound {q m : ℕ} (hsmall : 2 ^ q < m) (leaf : Fin m → (Fin q → Bool)) : ∃ i j : Fin m, i ≠ j ∧ leaf i = leaf j := by by_contra hall; push Not at hall have hinj : Function.Injective leaf := fun i j hij => by by_contra hne; exact hall i j hne hij have hle := Fintype.card_le_of_injective leaf hinj simp [Fintype.card_bool, Fintype.card_fin] at hle omega /-! ### Part 2B: Boolean Hypercube `{0, 1}^n` Edge-Tester Soundness (GGLRS 2000) — Exact `n / 2` Constant! -/ /-- Boolean hypercube vertex `{0, 1}^n` modeled as `Fin n → Bool`. -/ abbrev Cube (n : ℕ) := Fin n → Bool /-- Coordinatewise partial order on `{0, 1}^n`: `x ≤ y ↔ ∀ i, x i ≤ y i`. -/ def IsCubeMonotone {n : ℕ} (f : Cube n → Bool) : Prop := ∀ x y : Cube n, (∀ i : Fin n, x i ≤ y i) → f x ≤ f y /-- Number of Hamming disagreements between `f` and `g` on `{0, 1}^n`. -/ def cubeHammingCount {n : ℕ} (f g : Cube n → Bool) : ℕ := (Finset.univ.filter (fun x : Cube n => f x ≠ g x)).card /-- Number of violated edges in dimension `i` of `{0, 1}^n`: undirected pairs `(x_{i←0}, x_{i←1})` with `f(x_{i←0}) = true ∧ f(x_{i←1}) = false`. -/ def cubeViolDim {n : ℕ} (f : Cube n → Bool) (i : Fin n) : ℕ := (Finset.univ.filter (fun x : Cube n => x i = false ∧ f x = true ∧ f (Function.update x i true) = false)).card /-- Total number of violated undirected hypercube edges across all `n` dimensions. -/ def cubeTotalViol {n : ℕ} (f : Cube n → Bool) : ℕ := ∑ i : Fin n, cubeViolDim f i /-- Fraction of undirected hypercube edges violated by `f` (out of `n · 2^{n - 1}` total edges). -/ noncomputable def cubeEdgeViolFrac {n : ℕ} (f : Cube n → Bool) : ℝ := (cubeTotalViol f : ℝ) / ((n : ℝ) * (2 : ℝ) ^ (n - 1)) /-- **SHARP GGLRS HYPERCUBE MONOTONICITY THEOREM (`dist_count(f, Mono) ≤ cubeTotalViol f`)**: For EVERY `n : ℕ` and EVERY Boolean function `f : {0, 1}^n → Bool`, there exists a **MONOTONE** Boolean function `h : {0, 1}^n → Bool` such that **`cubeHammingCount f h ≤ cubeTotalViol f`**! Proved via our elementary two-repair averaging induction (`R₁ = (h₀ && h₁, h₁)` and `R₂ = (h₀, h₀ || h₁)` satisfy `d(f, R₁) + d(f, R₂) ≤ 2 · (d(f₀, h₀) + d(f₁, h₁) + V₀(f))`)! -/ theorem gglrs_cube_hamming_le_total_viol : ∀ {n : ℕ} (f : Cube n → Bool), ∃ h : Cube n → Bool, IsCubeMonotone h ∧ cubeHammingCount f h ≤ cubeTotalViol f | 0, f => by refine ⟨f, fun x y _ => by have hxy : x = y := funext (fun i => Fin.elim0 i) rw [hxy], ?_⟩ simp [cubeHammingCount] | n + 1, f => by -- Split `f` along coordinate `0` into lower subcube `f₀` and upper subcube `f₁`! let f₀ : Cube n → Bool := fun y => f (Fin.cons false y) let f₁ : Cube n → Bool := fun y => f (Fin.cons true y) obtain ⟨h₀, hm₀, hd₀⟩ := gglrs_cube_hamming_le_total_viol f₀ obtain ⟨h₁, hm₁, hd₁⟩ := gglrs_cube_hamming_le_total_viol f₁ -- Construct the TWO monotone repairs `R₁ = (h₀ && h₁, h₁)` and `R₂ = (h₀, h₀ || h₁)`! let R₁ : Cube (n + 1) → Bool := fun x => if x 0 then h₁ (Fin.tail x) else (h₀ (Fin.tail x) && h₁ (Fin.tail x)) let R₂ : Cube (n + 1) → Bool := fun x => if x 0 then (h₀ (Fin.tail x) || h₁ (Fin.tail x)) else h₀ (Fin.tail x) have hR₁_mono : IsCubeMonotone R₁ := by intro x y hxy have htail : ∀ j : Fin n, Fin.tail x j ≤ Fin.tail y j := fun j => hxy j.succ have h0le := hm₀ _ _ htail have h1le := hm₁ _ _ htail have hbit0 := hxy 0 dsimp only [R₁] cases hx0 : x 0 <;> cases hy0 : y 0 · cases hh0 : h₀ (Fin.tail x) <;> cases hh1 : h₁ (Fin.tail x) <;> cases hk0 : h₀ (Fin.tail y) <;> cases hk1 : h₁ (Fin.tail y) <;> simp_all · cases hh0 : h₀ (Fin.tail x) <;> cases hh1 : h₁ (Fin.tail x) <;> cases hk1 : h₁ (Fin.tail y) <;> simp_all · rw [hx0, hy0] at hbit0; exact absurd hbit0 (by decide) · exact h1le have hR₂_mono : IsCubeMonotone R₂ := by intro x y hxy have htail : ∀ j : Fin n, Fin.tail x j ≤ Fin.tail y j := fun j => hxy j.succ have h0le := hm₀ _ _ htail have h1le := hm₁ _ _ htail have hbit0 := hxy 0 dsimp only [R₂] cases hx0 : x 0 <;> cases hy0 : y 0 · exact h0le · cases hh0 : h₀ (Fin.tail x) <;> cases hk0 : h₀ (Fin.tail y) <;> cases hk1 : h₁ (Fin.tail y) <;> simp_all · rw [hx0, hy0] at hbit0; exact absurd hbit0 (by decide) · cases hh0 : h₀ (Fin.tail x) <;> cases hh1 : h₁ (Fin.tail x) <;> cases hk0 : h₀ (Fin.tail y) <;> cases hk1 : h₁ (Fin.tail y) <;> simp_all -- Decompose `cubeHammingCount f R` over `Cube n` via `Fin.cons false y` and `Fin.cons true y`! have hham_split : ∀ g : Cube (n + 1) → Bool, cubeHammingCount f g = ∑ y : Cube n, ((if f₀ y ≠ g (Fin.cons false y) then 1 else 0) + (if f₁ y ≠ g (Fin.cons true y) then 1 else 0)) := by intro g dsimp only [cubeHammingCount] have hcard : (Finset.univ.filter (fun x : Cube (n + 1) => f x ≠ g x)).card = ∑ x : Cube (n + 1), if f x ≠ g x then 1 else 0 := by rw [← Finset.sum_filter]; simp rw [hcard] let e : (Bool × Cube n) ≃ Cube (n + 1) := { toFun := fun p => Fin.cons p.1 p.2 invFun := fun x => (x 0, Fin.tail x) left_inv := fun ⟨b, y⟩ => Prod.ext (by simp) (by simp) right_inv := fun x => Fin.cons_self_tail x } rw [← Equiv.sum_comp e (fun x => if f x ≠ g x then 1 else 0)] rw [Fintype.sum_prod_type, Finset.sum_comm] refine Finset.sum_congr rfl (fun y _ => ?_) simp only [Fintype.sum_bool, e, Equiv.coe_fn_mk, f₀, f₁] omega -- Decompose `cubeTotalViol f = cubeViolDim f 0 + cubeTotalViol f₀ + cubeTotalViol f₁`! have hV0_eq : cubeViolDim f 0 = ∑ y : Cube n, if f₀ y = true ∧ f₁ y = false then 1 else 0 := by dsimp only [cubeViolDim] rw [show (Finset.univ.filter (fun x : Cube (n + 1) => x 0 = false ∧ f x = true ∧ f (Function.update x 0 true) = false)).card = (Finset.univ.filter (fun y : Cube n => f₀ y = true ∧ f₁ y = false)).card by refine Finset.card_bij (fun x _ => Fin.tail x) (fun x hx => ?_) (fun a ha b hb hab => ?_) (fun y hy => ⟨Fin.cons false y, ?_, by simp⟩) · have ⟨hx0, hfx, hf1⟩ := (Finset.mem_filter.mp hx).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_, ?_⟩ · dsimp only [f₀] have hcons : Fin.cons false (Fin.tail x) = x := by rw [← hx0]; exact Fin.cons_self_tail x rwa [hcons] · dsimp only [f₁] have hupd : Fin.cons true (Fin.tail x) = Function.update x 0 true := by funext k; refine Fin.cases ?_ ?_ k · simp · intro j; simp [Fin.succ_ne_zero, Fin.tail] rwa [hupd] · have ha0 := (Finset.mem_filter.mp ha).2.1 have hb0 := (Finset.mem_filter.mp hb).2.1 rw [← Fin.cons_self_tail a, ← Fin.cons_self_tail b, ha0, hb0, hab] · have ⟨h0, h1⟩ := (Finset.mem_filter.mp hy).2 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp, h0, ?_⟩ have hupd : Function.update (Fin.cons (α := fun _ => Bool) false y) (0 : Fin (n + 1)) true = Fin.cons (α := fun _ => Bool) true y := by funext k; refine Fin.cases ?_ ?_ k · simp · intro j; simp [Fin.succ_ne_zero] dsimp only [f₁] at h1 rwa [hupd]] rw [← Finset.sum_filter]; simp have hVj_eq : ∀ j : Fin n, cubeViolDim f j.succ = cubeViolDim f₀ j + cubeViolDim f₁ j := by intro j dsimp only [cubeViolDim] have hpart : (Finset.univ.filter (fun x : Cube (n + 1) => x j.succ = false ∧ f x = true ∧ f (Function.update x j.succ true) = false)).card = (Finset.univ.filter (fun x : Cube (n + 1) => x 0 = false ∧ x j.succ = false ∧ f x = true ∧ f (Function.update x j.succ true) = false)).card + (Finset.univ.filter (fun x : Cube (n + 1) => x 0 = true ∧ x j.succ = false ∧ f x = true ∧ f (Function.update x j.succ true) = false)).card := by rw [← Finset.card_union_of_disjoint] · congr 1; ext x; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_union] cases x 0 <;> simp · rw [Finset.disjoint_filter]; intro x _ h1 h2; rw [h1.1] at h2; exact absurd h2.1 (by decide) rw [hpart] have hside : ∀ b : Bool, (Finset.univ.filter (fun x : Cube (n + 1) => x 0 = b ∧ x j.succ = false ∧ f x = true ∧ f (Function.update x j.succ true) = false)).card = (Finset.univ.filter (fun y : Cube n => y j = false ∧ f (Fin.cons b y) = true ∧ f (Fin.cons b (Function.update y j true)) = false)).card := by intro b refine Finset.card_bij (fun x _ => Fin.tail x) (fun x hx => ?_) (fun a ha b' hb' hab => by rw [← Fin.cons_self_tail a, ← Fin.cons_self_tail b', (Finset.mem_filter.mp ha).2.1, (Finset.mem_filter.mp hb').2.1, hab]) (fun y hy => ⟨Fin.cons b y, ?_, by simp⟩) · have ⟨hb0, hxj, hfx, hfu⟩ := (Finset.mem_filter.mp hx).2 have hcons : Fin.cons b (Fin.tail x) = x := by rw [← hb0]; exact Fin.cons_self_tail x have hupd : Fin.cons b (Function.update (Fin.tail x) j true) = Function.update x j.succ true := by funext k; refine Fin.cases ?_ ?_ k · simp [Ne.symm (Fin.succ_ne_zero j), hb0] · intro i; by_cases hij : i = j · subst hij; simp · have hne : i.succ ≠ j.succ := fun h => hij (Fin.succ_injective _ h) simp [hij, hne, Fin.tail] refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, hxj, by rwa [hcons], by rwa [hupd]⟩ · have ⟨hyj, hfy, hfu⟩ := (Finset.mem_filter.mp hy).2 have hupd : Function.update (Fin.cons (α := fun _ => Bool) b y) j.succ true = Fin.cons (α := fun _ => Bool) b (Function.update y j true) := by funext k; refine Fin.cases ?_ ?_ k · simp [Ne.symm (Fin.succ_ne_zero j)] · intro i; by_cases hij : i = j · subst hij; simp · have hne : i.succ ≠ j.succ := fun h => hij (Fin.succ_injective _ h) simp [hij, hne] exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp, by simpa, hfy, by rwa [hupd]⟩ rw [hside false, hside true] have hVtot : cubeTotalViol f = cubeViolDim f 0 + cubeTotalViol f₀ + cubeTotalViol f₁ := by dsimp only [cubeTotalViol] rw [Fin.sum_univ_succ] simp_rw [hVj_eq, Finset.sum_add_distrib] omega -- Pointwise two-repair averaging inequality (16-case Bool truth table)! have hpt : ∀ y : Cube n, ((if f₀ y ≠ (h₀ y && h₁ y) then 1 else 0) + (if f₁ y ≠ h₁ y then 1 else 0)) + ((if f₀ y ≠ h₀ y then 1 else 0) + (if f₁ y ≠ (h₀ y || h₁ y) then 1 else 0)) ≤ 2 * ((if f₀ y ≠ h₀ y then 1 else 0) + (if f₁ y ≠ h₁ y then 1 else 0) + (if f₀ y = true ∧ f₁ y = false then 1 else 0)) := by intro y cases f₀ y <;> cases f₁ y <;> cases h₀ y <;> cases h₁ y <;> decide have hsum_repairs : cubeHammingCount f R₁ + cubeHammingCount f R₂ ≤ 2 * cubeTotalViol f := by rw [hham_split R₁, hham_split R₂, ← Finset.sum_add_distrib] have hsimp : ∀ y : Cube n, ((if f₀ y ≠ R₁ (Fin.cons false y) then 1 else 0) + (if f₁ y ≠ R₁ (Fin.cons true y) then 1 else 0)) + ((if f₀ y ≠ R₂ (Fin.cons false y) then 1 else 0) + (if f₁ y ≠ R₂ (Fin.cons true y) then 1 else 0)) ≤ 2 * ((if f₀ y ≠ h₀ y then 1 else 0) + (if f₁ y ≠ h₁ y then 1 else 0) + (if f₀ y = true ∧ f₁ y = false then 1 else 0)) := fun y => by dsimp only [R₁, R₂]; simp only [Fin.cons_zero, Fin.tail_cons, Bool.false_eq_true, ↓reduceIte] exact hpt y have hhd0 : cubeHammingCount f₀ h₀ = ∑ y : Cube n, if f₀ y ≠ h₀ y then 1 else 0 := by dsimp only [cubeHammingCount]; rw [← Finset.sum_filter]; simp have hhd1 : cubeHammingCount f₁ h₁ = ∑ y : Cube n, if f₁ y ≠ h₁ y then 1 else 0 := by dsimp only [cubeHammingCount]; rw [← Finset.sum_filter]; simp calc ∑ y : Cube n, (((if f₀ y ≠ R₁ (Fin.cons false y) then 1 else 0) + (if f₁ y ≠ R₁ (Fin.cons true y) then 1 else 0)) + ((if f₀ y ≠ R₂ (Fin.cons false y) then 1 else 0) + (if f₁ y ≠ R₂ (Fin.cons true y) then 1 else 0))) ≤ ∑ y : Cube n, 2 * ((if f₀ y ≠ h₀ y then 1 else 0) + (if f₁ y ≠ h₁ y then 1 else 0) + (if f₀ y = true ∧ f₁ y = false then 1 else 0)) := Finset.sum_le_sum (fun y _ => hsimp y) _ = 2 * (cubeHammingCount f₀ h₀ + cubeHammingCount f₁ h₁ + cubeViolDim f 0) := by rw [← Finset.mul_sum, Finset.sum_add_distrib, Finset.sum_add_distrib, hhd0, hhd1, hV0_eq] _ ≤ 2 * (cubeTotalViol f₀ + cubeTotalViol f₁ + cubeViolDim f 0) := by omega _ = 2 * cubeTotalViol f := by rw [hVtot]; omega by_cases hbest : cubeHammingCount f R₁ ≤ cubeHammingCount f R₂ · exact ⟨R₁, hR₁_mono, by omega⟩ · exact ⟨R₂, hR₂_mono, by omega⟩ /-- **EXACT GGLRS HYPERCUBE EDGE-TESTER SOUNDNESS (`dist(f, Mono) ≤ (n / 2) · Pr[violated edge]`)**: For ANY `1 ≤ n` and ANY `f : {0, 1}^n → Bool`, there exists a monotone Boolean function `h` with normalized Hamming distance bounded by **`(n / 2)` times the undirected edge-violation fraction**: **`(cubeHammingCount f h : ℝ) / 2^n ≤ ((n : ℝ) / 2) * cubeEdgeViolFrac f`**! -/ theorem gglrs_hypercube_edge_tester_soundness {n : ℕ} (hn : 1 ≤ n) (f : Cube n → Bool) : ∃ h : Cube n → Bool, IsCubeMonotone h ∧ (cubeHammingCount f h : ℝ) / (2 : ℝ) ^ n ≤ ((n : ℝ) / 2) * cubeEdgeViolFrac f := by obtain ⟨h, hm, hle⟩ := gglrs_cube_hamming_le_total_viol f refine ⟨h, hm, ?_⟩ have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have h2pos : (0 : ℝ) < (2 : ℝ) ^ (n - 1) := by positivity have hpow : (2 : ℝ) ^ n = 2 * (2 : ℝ) ^ (n - 1) := by have : n = (n - 1) + 1 := by omega conv_lhs => rw [this, pow_succ, mul_comm] dsimp only [cubeEdgeViolFrac] have hrhs : ((n : ℝ) / 2) * ((cubeTotalViol f : ℝ) / ((n : ℝ) * (2 : ℝ) ^ (n - 1))) = (cubeTotalViol f : ℝ) / (2 : ℝ) ^ n := by rw [hpow]; field_simp [ne_of_gt hnpos, ne_of_gt h2pos] rw [hrhs] exact div_le_div_of_nonneg_right (Nat.cast_le.mpr hle) (by positivity) /-- **SHARP ANTI-DICTATOR WITNESS (`f(x) = !(x 0)` ACHIEVES EXACT EQUALITY `1 / 2 = (n / 2) · (1 / n)`)**: For EVERY `n ≥ 1`, the anti-dictator `f(x) = !(x 0)` satisfies: 1. `cubeEdgeViolFrac f = 1 / n` (exactly `2^{n - 1}` violated edges out of `n · 2^{n - 1}`); 2. Every monotone function `h` has `(cubeHammingCount f h : ℝ) / 2^n ≥ 1 / 2` (with equality at `const false`), matching `(n / 2) * cubeEdgeViolFrac f = 1 / 2` with **ZERO GAP**! -/ theorem gglrs_anti_dictator_exact_tight {n : ℕ} (hn : 1 ≤ n) : let f : Cube n → Bool := fun x => !(x ⟨0, by omega⟩) cubeEdgeViolFrac f = 1 / (n : ℝ) ∧ (∀ h : Cube n → Bool, IsCubeMonotone h → (1 / 2 : ℝ) ≤ (cubeHammingCount f h : ℝ) / (2 : ℝ) ^ n) ∧ ((cubeHammingCount f (fun _ => false) : ℝ) / (2 : ℝ) ^ n = ((n : ℝ) / 2) * cubeEdgeViolFrac f) := by intro f let i₀ : Fin n := ⟨0, by omega⟩ have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have h2pos : (0 : ℝ) < (2 : ℝ) ^ (n - 1) := by positivity have hpow : (2 : ℝ) ^ n = 2 * (2 : ℝ) ^ (n - 1) := by have : n = (n - 1) + 1 := by omega conv_lhs => rw [this, pow_succ, mul_comm] -- Violated edges: `2^{n - 1}` in dim `0`, and `0` in all dims `j ≠ 0`! have hV0 : cubeViolDim f i₀ = 2 ^ (n - 1) := by dsimp only [cubeViolDim, f] have hfilt : Finset.univ.filter (fun x : Cube n => x i₀ = false ∧ (!x i₀) = true ∧ (!Function.update x i₀ true i₀) = false) = Finset.univ.filter (fun x : Cube n => x i₀ = false) := by ext x; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Function.update_self] cases x i₀ <;> simp rw [hfilt] -- Count `{x : Cube n | x i₀ = false}` = `2^{n - 1}`! have hcard2 : 2 * (Finset.univ.filter (fun x : Cube n => x i₀ = false)).card = 2 ^ n := by have hflip : (Finset.univ.filter (fun x : Cube n => x i₀ = true)).card = (Finset.univ.filter (fun x : Cube n => x i₀ = false)).card := by refine Finset.card_bij (fun x _ => Function.update x i₀ false) (fun x _ => Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩) (fun a ha b hb hab => by funext k; by_cases hk : k = i₀ · subst hk; exact (Finset.mem_filter.mp ha).2.trans (Finset.mem_filter.mp hb).2.symm · have := congrFun hab k; simpa [hk] using this) (fun y hy => ⟨Function.update y i₀ true, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩, by funext k; by_cases hk : k = i₀ · subst hk; simp [(Finset.mem_filter.mp hy).2] · simp [hk]⟩) have hu : Finset.univ.filter (fun x : Cube n => x i₀ = false) ∪ Finset.univ.filter (fun x : Cube n => x i₀ = true) = Finset.univ := by ext x; cases x i₀ <;> simp have hd : Disjoint (Finset.univ.filter (fun x : Cube n => x i₀ = false)) (Finset.univ.filter (fun x : Cube n => x i₀ = true)) := by rw [Finset.disjoint_filter]; intro x _ h1 h2; rw [h1] at h2; exact absurd h2 (by decide) have htot := Finset.card_union_of_disjoint hd rw [hu, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin, hflip] at htot omega have hpowN : 2 ^ n = 2 * 2 ^ (n - 1) := by have : n = (n - 1) + 1 := by omega conv_lhs => rw [this, pow_succ, mul_comm] omega have hVother : ∀ j : Fin n, j ≠ i₀ → cubeViolDim f j = 0 := by intro j hj dsimp only [cubeViolDim, f] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro x _; dsimp only [i₀] at hj ⊢ rw [Function.update_of_ne hj.symm] cases x ⟨0, by omega⟩ <;> simp have hVtot : cubeTotalViol f = 2 ^ (n - 1) := by dsimp only [cubeTotalViol] rw [Finset.sum_eq_single i₀] · exact hV0 · intro j _ hj; exact hVother j hj · intro h; exact absurd (Finset.mem_univ i₀) h have hfrac : cubeEdgeViolFrac f = 1 / (n : ℝ) := by dsimp only [cubeEdgeViolFrac]; rw [hVtot, Nat.cast_pow, Nat.cast_ofNat] field_simp [ne_of_gt hnpos, ne_of_gt h2pos] -- Lower bound: on every dim-`i₀` pair `(x_{i₀←0}, x_{i₀←1})`, ANY monotone `h` must disagree with `f` on at least 1 vertex! have hlb : ∀ h : Cube n → Bool, IsCubeMonotone h → (1 / 2 : ℝ) ≤ (cubeHammingCount f h : ℝ) / (2 : ℝ) ^ n := by intro h hm have hcount : 2 ^ (n - 1) ≤ cubeHammingCount f h := by -- Map each `x` with `x i₀ = false` to `x` if `f x ≠ h x`, else to `Function.update x i₀ true`! have hcard_half : (Finset.univ.filter (fun x : Cube n => x i₀ = false)).card = 2 ^ (n - 1) := by rw [← hV0]; dsimp only [cubeViolDim, f, i₀] congr 1; ext x; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Function.update_self] cases x ⟨0, by omega⟩ <;> simp rw [← hcard_half] let witness : Cube n → Cube n := fun x => if f x ≠ h x then x else Function.update x i₀ true refine Finset.card_le_card_of_injOn witness (fun x hx => ?_) (fun a ha b hb hab => ?_) · have hx0 : x i₀ = false := (Finset.mem_filter.mp hx).2 rw [Finset.mem_coe, Finset.mem_filter] refine ⟨Finset.mem_univ _, ?_⟩ dsimp only [witness] split_ifs with hne · exact hne · push Not at hne have hfx : f x = true := by dsimp only [f]; rw [show (⟨0, by omega⟩ : Fin n) = i₀ from rfl, hx0]; rfl have hhx : h x = true := hne.symm.trans hfx have hle : ∀ k : Fin n, x k ≤ Function.update x i₀ true k := fun k => by by_cases hk : k = i₀ · subst hk; simp [hx0] · simp [hk] have hht : h (Function.update x i₀ true) = true := by have := hm x (Function.update x i₀ true) hle rw [hhx] at this cases hval : h (Function.update x i₀ true) · rw [hval] at this; exact absurd this (by decide) · rfl have hft : f (Function.update x i₀ true) = false := by dsimp only [f]; rw [show (⟨0, by omega⟩ : Fin n) = i₀ from rfl, Function.update_self]; rfl rw [hft, hht]; decide · have ha0 : a i₀ = false := (Finset.mem_filter.mp ha).2 have hb0 : b i₀ = false := (Finset.mem_filter.mp hb).2 dsimp only [witness] at hab split_ifs at hab with h1 h2 h2 · exact hab · have := congrFun hab i₀; simp [ha0] at this · have := congrFun hab i₀; simp [hb0] at this · funext k; by_cases hk : k = i₀ · subst hk; exact ha0.trans hb0.symm · have := congrFun hab k; simpa [hk] using this rw [hpow] calc (1 / 2 : ℝ) = (2 : ℝ) ^ (n - 1) / (2 * (2 : ℝ) ^ (n - 1)) := by field_simp [ne_of_gt h2pos] _ ≤ (cubeHammingCount f h : ℝ) / (2 * (2 : ℝ) ^ (n - 1)) := by have hcR : (2 : ℝ) ^ (n - 1) ≤ (cubeHammingCount f h : ℝ) := by exact_mod_cast hcount exact div_le_div_of_nonneg_right hcR (by positivity) -- Tightness at `h = const false`: agrees on `x i₀ = true`, disagrees on `x i₀ = false` → exact `2^{n - 1}`! have htight : (cubeHammingCount f (fun _ => false) : ℝ) / (2 : ℝ) ^ n = ((n : ℝ) / 2) * cubeEdgeViolFrac f := by have hhc : cubeHammingCount f (fun _ => false) = 2 ^ (n - 1) := by rw [← hV0]; dsimp only [cubeHammingCount, cubeViolDim, f, i₀] congr 1; ext x; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Function.update_self] cases x ⟨0, by omega⟩ <;> simp rw [hhc, hfrac, hpow, Nat.cast_pow, Nat.cast_ofNat] field_simp [ne_of_gt hnpos, ne_of_gt h2pos] exact ⟨hfrac, hlb, htight⟩ /-- **GGLRS DIMENSION-`i` SWITCHING OPERATOR `switchOp i f`**: On every dim-`i` violated edge `(x_{i←0}, x_{i←1})`, swaps `true` and `false` to repair the edge; leaves all non-violated edges unchanged! -/ def switchOp {n : ℕ} (i : Fin n) (f : Cube n → Bool) (x : Cube n) : Bool := if x i then (f (Function.update x i false) || f x) else (f x && f (Function.update x i true)) /-- Switching operator `switchOp i` completely eliminates ALL violations in dimension `i`! -/ theorem switchOp_fixes_dim {n : ℕ} (i : Fin n) (f : Cube n → Bool) : cubeViolDim (switchOp i f) i = 0 := by dsimp only [cubeViolDim] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro x _ rintro ⟨hx0, h1, h2⟩ dsimp only [switchOp] at h1 h2 rw [hx0] at h1 simp only [Bool.false_eq_true, ↓reduceIte, Function.update_self] at h1 h2 have hback : Function.update (Function.update x i true) i false = x := by funext k; by_cases hk : k = i · subst hk; simp [hx0] · simp [hk] rw [hback] at h2 cases f x <;> cases f (Function.update x i true) <;> simp_all /-- **GGLRS SWITCHING VIOLATION-MONOTONICITY LEMMA**: For EVERY other dimension `j ≠ i`, applying `switchOp i` NEVER increases the number of violated edges in dimension `j`: **`cubeViolDim (switchOp i f) j ≤ cubeViolDim f j`**! (Proved cleanly by partitioning into 4-vertex `(i, j)`-squares and verifying all 16 truth-table cases!) -/ theorem switchOp_monotone_other_dim {n : ℕ} {i j : Fin n} (hij : i ≠ j) (f : Cube n → Bool) : cubeViolDim (switchOp i f) j ≤ cubeViolDim f j := by -- Partition `{x | x j = false}` into pairs `(x_{i←0}, x_{i←1})` indexed by `{x | x i = false ∧ x j = false}`! let Q := Finset.univ.filter (fun x : Cube n => x i = false ∧ x j = false) have hdecomp : ∀ g : Cube n → Bool, cubeViolDim g j = ∑ x ∈ Q, ((if g x = true ∧ g (Function.update x j true) = false then 1 else 0) + (if g (Function.update x i true) = true ∧ g (Function.update (Function.update x i true) j true) = false then 1 else 0)) := by intro g dsimp only [cubeViolDim] have hsplit : (Finset.univ.filter (fun x : Cube n => x j = false ∧ g x = true ∧ g (Function.update x j true) = false)).card = (Finset.univ.filter (fun x : Cube n => x i = false ∧ x j = false ∧ g x = true ∧ g (Function.update x j true) = false)).card + (Finset.univ.filter (fun x : Cube n => x i = true ∧ x j = false ∧ g x = true ∧ g (Function.update x j true) = false)).card := by rw [← Finset.card_union_of_disjoint] · congr 1; ext x; simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_union] cases x i <;> simp · rw [Finset.disjoint_filter]; intro x _ h1 h2; rw [h1.1] at h2; exact absurd h2.1 (by decide) rw [hsplit, Finset.sum_add_distrib] congr 1 · have heq : Finset.univ.filter (fun x : Cube n => x i = false ∧ x j = false ∧ g x = true ∧ g (Function.update x j true) = false) = Q.filter (fun x => g x = true ∧ g (Function.update x j true) = false) := by ext x; simp [Q, and_assoc] rw [heq, ← Finset.sum_filter]; simp · have hbij : (Finset.univ.filter (fun x : Cube n => x i = true ∧ x j = false ∧ g x = true ∧ g (Function.update x j true) = false)).card = (Q.filter (fun x => g (Function.update x i true) = true ∧ g (Function.update (Function.update x i true) j true) = false)).card := by refine Finset.card_bij (fun x _ => Function.update x i false) (fun x hx => ?_) (fun a ha b hb hab => by funext k; by_cases hk : k = i · subst hk; exact (Finset.mem_filter.mp ha).2.1.trans (Finset.mem_filter.mp hb).2.1.symm · have := congrFun hab k; simpa [hk] using this) (fun y hy => ⟨Function.update y i true, ?_, ?_⟩) · have ⟨hxi, hxj, hg1, hg2⟩ := (Finset.mem_filter.mp hx).2 have hback : Function.update (Function.update x i false) i true = x := by funext k; by_cases hk : k = i · subst hk; simp [hxi] · simp [hk] exact Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp, by simp [hij.symm, hxj]⟩, by rwa [hback], by rwa [hback]⟩ · have hyQ := Finset.mem_filter.mp (Finset.mem_filter.mp hy).1 have ⟨hg1, hg2⟩ := (Finset.mem_filter.mp hy).2 exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp, by simp [hij.symm, hyQ.2.2], hg1, hg2⟩ · have hyi := (Finset.mem_filter.mp (Finset.mem_filter.mp hy).1).2.1 funext k; by_cases hk : k = i · subst hk; simp [hyi] · simp [hk] rw [hbij, ← Finset.sum_filter]; simp rw [hdecomp (switchOp i f), hdecomp f] refine Finset.sum_le_sum (fun x hx => ?_) have hxi : x i = false := (Finset.mem_filter.mp hx).2.1 have hcomm : Function.update (Function.update x i true) j true = Function.update (Function.update x j true) i true := by funext k; by_cases hk1 : k = i <;> by_cases hk2 : k = j · subst hk1; exact absurd hk2 hij · subst hk1; simp [hk2] · subst hk2; simp [hk1] · simp [hk1, hk2] have hback1 : Function.update (Function.update x i true) i false = x := by funext k; by_cases hk : k = i · subst hk; simp [hxi] · simp [hk] have hback2 : Function.update (Function.update (Function.update x i true) j true) i false = Function.update x j true := by rw [hcomm]; funext k; by_cases hk : k = i · subst hk; simp [hij, hxi] · simp [hk] have hback2' : Function.update (Function.update (Function.update x j true) i true) i false = Function.update x j true := by funext k; by_cases hk : k = i · subst hk; simp [hij, hxi] · simp [hk] simp only [switchOp, hxi, Function.update_self, Function.update_of_ne hij, hback1, hcomm, hback2', Bool.false_eq_true, ↓reduceIte] -- 16-case 4-vertex square verification! cases f x <;> cases f (Function.update x j true) <;> cases f (Function.update x i true) <;> cases f (Function.update (Function.update x j true) i true) <;> decide /-! =============================================================================== PILLAR 3: SUBLINEAR DISTRIBUTION UNIFORMITY & IDENTITY TESTING (BFRSW & PANINSKI) =============================================================================== -/ /-- Valid finite discrete probability distribution on a `Fintype α`. -/ def IsProbDist {α : Type*} [Fintype α] (p : α → ℝ) : Prop := (∀ x, 0 ≤ p x) ∧ ∑ x : α, p x = 1 /-- Uniform probability distribution on a `Fintype α`. -/ noncomputable def uniformDist (α : Type*) [Fintype α] : α → ℝ := fun _ => (1 : ℝ) / (Fintype.card α : ℝ) /-- Total variation distance `TV(p, q) = (1 / 2) ∑ x, |p x - q x|`. -/ noncomputable def tvDist {α : Type*} [Fintype α] (p q : α → ℝ) : ℝ := (1 / 2 : ℝ) * ∑ x : α, |p x - q x| /-- Squared `L_2` norm (collision probability) `‖p‖_2^2 = ∑ x, (p x)^2`. -/ def l2Sq {α : Type*} [Fintype α] (p : α → ℝ) : ℝ := ∑ x : α, (p x) ^ 2 /-- Cubed `L_3` norm `‖p‖_3^3 = ∑ x, (p x)^3`. -/ def l3Cube {α : Type*} [Fintype α] (p : α → ℝ) : ℝ := ∑ x : α, (p x) ^ 3 /-- Product distribution `p^{⊗ m}` on `Fin m → α`. -/ def prodDist {α : Type*} {m : ℕ} (p : α → ℝ) (X : Fin m → α) : ℝ := ∏ t : Fin m, p (X t) /-- Product distribution `p^{⊗ m}` is a valid probability distribution on `Fin m → α`. -/ theorem prodDist_isProbDist {α : Type*} [Fintype α] {m : ℕ} {p : α → ℝ} (hp : IsProbDist p) : IsProbDist (prodDist (m := m) p) := by refine ⟨fun X => Finset.prod_nonneg (fun t _ => hp.1 (X t)), ?_⟩ dsimp only [prodDist] rw [← Fintype.prod_sum (fun (_ : Fin m) (x : α) => p x)] simp [hp.2] /-- **EXACT `L_2²` DISTANCE FROM UNIFORM & CAUCHY–SCHWARZ LOWER BOUND**: For ANY distribution `p` on `Fin n` (`1 ≤ n`): 1. `∑ x, (p x - 1 / n)^2 = l2Sq p - 1 / n`; 2. `4 * (tvDist p (uniformDist (Fin n)))^2 / n ≤ l2Sq p - 1 / n`! -/ theorem l2_excess_ge_four_tv_sq_div_n {n : ℕ} (hn : 1 ≤ n) {p : Fin n → ℝ} (hp : IsProbDist p) : ∑ x : Fin n, (p x - 1 / (n : ℝ)) ^ 2 = l2Sq p - 1 / (n : ℝ) ∧ 4 * (tvDist p (uniformDist (Fin n))) ^ 2 / (n : ℝ) ≤ l2Sq p - 1 / (n : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hid : ∑ x : Fin n, (p x - 1 / (n : ℝ)) ^ 2 = l2Sq p - 1 / (n : ℝ) := by have hpt : ∀ x : Fin n, (p x - 1 / (n : ℝ)) ^ 2 = (p x) ^ 2 - 2 * (1 / (n : ℝ)) * p x + (1 / (n : ℝ)) ^ 2 := fun x => by ring simp_rw [hpt, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum, hp.2] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] dsimp only [l2Sq]; field_simp [ne_of_gt hnpos]; ring have hcs : (∑ x : Fin n, (1 : ℝ) * |p x - 1 / (n : ℝ)|) ^ 2 ≤ (∑ _x : Fin n, (1 : ℝ) ^ 2) * (∑ x : Fin n, |p x - 1 / (n : ℝ)| ^ 2) := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ (fun _ => (1 : ℝ)) (fun x => |p x - 1 / (n : ℝ)|) simp only [one_mul, one_pow, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, mul_one, sq_abs] at hcs rw [hid] at hcs have htv : 2 * tvDist p (uniformDist (Fin n)) = ∑ x : Fin n, |p x - 1 / (n : ℝ)| := by dsimp only [tvDist, uniformDist]; simp [Fintype.card_fin] refine ⟨hid, ?_⟩ rw [div_le_iff₀ hnpos] rw [← htv] at hcs nlinarith [hcs] /-- Ordered pairs `{(i, j) : Fin m × Fin m | i < j}` of sample indices. -/ def samplePairs (m : ℕ) : Finset (Fin m × Fin m) := Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 < ij.2) /-- Exact count of sample pairs is `m * (m - 1) / 2` (`2 * |(samplePairs m)| = m * (m - 1)`). -/ theorem samplePairs_card_two_mul (m : ℕ) : 2 * (samplePairs m).card = m * (m - 1) := by have hgt : (Finset.univ.filter (fun ij : Fin m × Fin m => ij.2 < ij.1)).card = (samplePairs m).card := by refine Finset.card_bij (fun ij _ => (ij.2, ij.1)) (fun ij hij => Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hij).2⟩) (fun a _ b _ h => Prod.ext (congrArg Prod.snd h) (congrArg Prod.fst h)) (fun ij hij => ⟨(ij.2, ij.1), Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp hij).2⟩, rfl⟩) have heq_card : (Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 = ij.2)).card = m := by have hcard : (Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 = ij.2)).card = (Finset.univ : Finset (Fin m)).card := by refine Finset.card_bij (fun ij _ => ij.1) (fun _ _ => Finset.mem_univ _) (fun a ha b hb hab => by have h1 := (Finset.mem_filter.mp ha).2 have h2 := (Finset.mem_filter.mp hb).2 exact Prod.ext hab (h1.symm.trans (hab.trans h2))) (fun i _ => ⟨(i, i), Finset.mem_filter.mpr ⟨Finset.mem_univ _, rfl⟩, rfl⟩) simpa using hcard have hpart : (samplePairs m).card + (Finset.univ.filter (fun ij : Fin m × Fin m => ij.2 < ij.1)).card + (Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 = ij.2)).card = m * m := by have hu : samplePairs m ∪ Finset.univ.filter (fun ij : Fin m × Fin m => ij.2 < ij.1) ∪ Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 = ij.2) = Finset.univ := by ext ⟨i, j⟩ simp only [samplePairs, Finset.mem_union, Finset.mem_filter, Finset.mem_univ, true_and, iff_true] rcases lt_trichotomy i j with h | h | h · exact Or.inl (Or.inl h) · exact Or.inr h · exact Or.inl (Or.inr h) have hd1 : Disjoint (samplePairs m) (Finset.univ.filter (fun ij : Fin m × Fin m => ij.2 < ij.1)) := by dsimp only [samplePairs] rw [Finset.disjoint_filter]; intro _ _ h1 h2; exact lt_asymm h1 h2 have hd2 : Disjoint (samplePairs m ∪ Finset.univ.filter (fun ij : Fin m × Fin m => ij.2 < ij.1)) (Finset.univ.filter (fun ij : Fin m × Fin m => ij.1 = ij.2)) := by rw [Finset.disjoint_left]; intro ij h1 h2 have he := (Finset.mem_filter.mp h2).2 rcases Finset.mem_union.mp h1 with hlt | hgt' · exact ne_of_lt (Finset.mem_filter.mp hlt).2 he · exact ne_of_gt (Finset.mem_filter.mp hgt').2 he rw [← Finset.card_union_of_disjoint hd1, ← Finset.card_union_of_disjoint hd2, hu] simp [Fintype.card_prod, Fintype.card_fin] rw [hgt, heq_card] at hpart cases m with | zero => simp [samplePairs] | succ k => simp only [Nat.add_sub_cancel] have hmul : (k + 1) * (k + 1) = (k + 1) * k + (k + 1) := by ring omega /-- Empirical collision count `C(X) = ∑_{i < j} 1_{X i = X j}` of `m` samples `X : Fin m → α`. -/ def empCollisions {α : Type*} [DecidableEq α] {m : ℕ} (X : Fin m → α) : ℝ := ∑ ij ∈ samplePairs m, if X ij.1 = X ij.2 then (1 : ℝ) else 0 /-- Exact expectation of a single pair indicator `1_{X i = X j}` (`i ≠ j`) under `p^{⊗ m}` equals `l2Sq p`! -/ theorem prodDist_pair_coll_expectation {α : Type*} [Fintype α] [DecidableEq α] {m : ℕ} {p : α → ℝ} (hp : IsProbDist p) {i j : Fin m} (hij : i ≠ j) : ∑ X : Fin m → α, prodDist p X * (if X i = X j then (1 : ℝ) else 0) = l2Sq p := by have h1 : ∀ X : Fin m → α, prodDist p X * (if X i = X j then (1 : ℝ) else 0) = ∑ x : α, if X i = x ∧ X j = x then prodDist p X else 0 := by intro X by_cases h : X i = X j · rw [if_pos h, mul_one] rw [Finset.sum_eq_single (X i)] · simp [h] · intro x _ hx; simp [Ne.symm hx] · intro hm; exact absurd (Finset.mem_univ _) hm · rw [if_neg h, mul_zero] refine (Finset.sum_eq_zero (fun x _ => ?_)).symm split_ifs with hx · exact absurd (hx.1.trans hx.2.symm) h · rfl simp_rw [h1] rw [Finset.sum_comm] dsimp only [l2Sq] refine Finset.sum_congr rfl (fun x _ => ?_) -- Factor `∑ X with X i = x ∧ X j = x, ∏ t, p (X t)` via `Fintype.prod_sum`! let q : Fin m → α → ℝ := fun t a => if t = i ∨ t = j then (if a = x then p x else 0) else p a have hterm : ∀ X : Fin m → α, (if X i = x ∧ X j = x then prodDist p X else 0) = ∏ t : Fin m, q t (X t) := by intro X by_cases h : X i = x ∧ X j = x · rw [if_pos h]; dsimp only [prodDist, q] refine Finset.prod_congr rfl (fun t _ => ?_) by_cases ht : t = i ∨ t = j · rw [if_pos ht]; rcases ht with rfl | rfl <;> simp [h.1, h.2] · rw [if_neg ht] · rw [if_neg h]; symm by_cases hi : X i = x · have hj : X j ≠ x := fun h' => h ⟨hi, h'⟩ exact Finset.prod_eq_zero (Finset.mem_univ j) (by dsimp only [q]; simp [hj]) · exact Finset.prod_eq_zero (Finset.mem_univ i) (by dsimp only [q]; simp [hi]) simp_rw [hterm] rw [← Fintype.prod_sum q] have hfac : ∀ t : Fin m, ∑ a : α, q t a = if t = i ∨ t = j then p x else 1 := by intro t; dsimp only [q] by_cases ht : t = i ∨ t = j · simp only [ht, ↓reduceIte] rw [Finset.sum_eq_single x] · simp · intro a _ ha; simp [ha] · intro hm; exact absurd (Finset.mem_univ x) hm · simp only [ht, ↓reduceIte]; exact hp.2 simp_rw [hfac] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ i)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hij.symm, Finset.mem_univ j⟩)] have hrest : ∏ t ∈ (Finset.univ.erase i).erase j, (if t = i ∨ t = j then p x else 1) = 1 := Finset.prod_eq_one (fun t ht => by have h1' := (Finset.mem_erase.mp ht).1 have h2' := (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).1 simp [h1', h2']) rw [hrest, mul_one]; simp [sq] /-- **BFRSW COLLISION ESTIMATOR UNBIASEDNESS**: For ANY `m ≥ 2` and ANY probability distribution `p` on `α`, the empirical collision count satisfies: **`E_{X ~ p^{⊗ m}} [ empCollisions X ] = (samplePairs m).card * ‖p‖_2^2`**! -/ theorem bfrsw_collision_estimator_unbiased {α : Type*} [Fintype α] [DecidableEq α] {m : ℕ} {p : α → ℝ} (hp : IsProbDist p) : ∑ X : Fin m → α, prodDist p X * empCollisions X = ((samplePairs m).card : ℝ) * l2Sq p := by dsimp only [empCollisions] simp_rw [Finset.mul_sum] rw [Finset.sum_comm] have hterm : ∀ ij ∈ samplePairs m, ∑ X : Fin m → α, prodDist p X * (if X ij.1 = X ij.2 then (1 : ℝ) else 0) = l2Sq p := fun ij hij => prodDist_pair_coll_expectation hp (ne_of_lt (Finset.mem_filter.mp hij).2) rw [Finset.sum_congr rfl hterm] simp /-- Exact 3-way collision expectation `E[1_{X i = X j = X k}] = l3Cube p` for pairwise distinct `i, j, k`! -/ theorem prodDist_triple_coll_expectation {α : Type*} [Fintype α] [DecidableEq α] {m : ℕ} {p : α → ℝ} (hp : IsProbDist p) {i j k : Fin m} (hij : i ≠ j) (hik : i ≠ k) (hjk : j ≠ k) : ∑ X : Fin m → α, prodDist p X * ((if X i = X j then (1 : ℝ) else 0) * (if X i = X k then (1 : ℝ) else 0)) = l3Cube p := by have h1 : ∀ X : Fin m → α, prodDist p X * ((if X i = X j then (1 : ℝ) else 0) * (if X i = X k then (1 : ℝ) else 0)) = ∑ x : α, if X i = x ∧ X j = x ∧ X k = x then prodDist p X else 0 := by intro X by_cases h : X i = X j ∧ X i = X k · rw [if_pos h.1, if_pos h.2, mul_one, mul_one] rw [Finset.sum_eq_single (X i)] · simp [h.1.symm, h.2.symm] · intro x _ hx; simp [Ne.symm hx] · intro hm; exact absurd (Finset.mem_univ _) hm · have hz : (if X i = X j then (1 : ℝ) else 0) * (if X i = X k then (1 : ℝ) else 0) = 0 := by split_ifs with h1' h2' · exact absurd ⟨h1', h2'⟩ h · ring · ring · ring rw [hz, mul_zero] refine (Finset.sum_eq_zero (fun x _ => ?_)).symm split_ifs with hx · exact absurd ⟨hx.1.trans hx.2.1.symm, hx.1.trans hx.2.2.symm⟩ h · rfl simp_rw [h1]; rw [Finset.sum_comm]; dsimp only [l3Cube] refine Finset.sum_congr rfl (fun x _ => ?_) let q : Fin m → α → ℝ := fun t a => if t = i ∨ t = j ∨ t = k then (if a = x then p x else 0) else p a have hterm : ∀ X : Fin m → α, (if X i = x ∧ X j = x ∧ X k = x then prodDist p X else 0) = ∏ t : Fin m, q t (X t) := by intro X by_cases h : X i = x ∧ X j = x ∧ X k = x · rw [if_pos h]; dsimp only [prodDist, q] refine Finset.prod_congr rfl (fun t _ => ?_) by_cases ht : t = i ∨ t = j ∨ t = k · rw [if_pos ht]; rcases ht with rfl | rfl | rfl <;> simp [h.1, h.2.1, h.2.2] · rw [if_neg ht] · rw [if_neg h]; symm by_cases hi : X i = x · by_cases hj : X j = x · have hk : X k ≠ x := fun h' => h ⟨hi, hj, h'⟩ exact Finset.prod_eq_zero (Finset.mem_univ k) (by dsimp only [q]; simp [hk]) · exact Finset.prod_eq_zero (Finset.mem_univ j) (by dsimp only [q]; simp [hj]) · exact Finset.prod_eq_zero (Finset.mem_univ i) (by dsimp only [q]; simp [hi]) simp_rw [hterm]; rw [← Fintype.prod_sum q] have hfac : ∀ t : Fin m, ∑ a : α, q t a = if t = i ∨ t = j ∨ t = k then p x else 1 := by intro t; dsimp only [q] by_cases ht : t = i ∨ t = j ∨ t = k · simp only [ht, ↓reduceIte]; rw [Finset.sum_eq_single x] · simp · intro a _ ha; simp [ha] · intro hm; exact absurd (Finset.mem_univ x) hm · simp only [ht, ↓reduceIte]; exact hp.2 simp_rw [hfac] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ i)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hij.symm, Finset.mem_univ j⟩)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hjk.symm, Finset.mem_erase.mpr ⟨hik.symm, Finset.mem_univ k⟩⟩)] have hrest : ∏ t ∈ ((Finset.univ.erase i).erase j).erase k, (if t = i ∨ t = j ∨ t = k then p x else 1) = 1 := Finset.prod_eq_one (fun t ht => by have hk' := (Finset.mem_erase.mp ht).1 have hj' := (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).1 have hi' := (Finset.mem_erase.mp (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).2).1 simp [hi', hj', hk']) rw [hrest, mul_one]; simp [pow_succ, mul_assoc] /-- Exact disjoint-pair expectation `E[1_{X i = X j} * 1_{X k = X l}] = (l2Sq p)^2`! -/ theorem prodDist_disjoint_pairs_expectation {α : Type*} [Fintype α] [DecidableEq α] {m : ℕ} {p : α → ℝ} (hp : IsProbDist p) {i j k l : Fin m} (hij : i ≠ j) (hkl : k ≠ l) (hik : i ≠ k) (hil : i ≠ l) (hjk : j ≠ k) (hjl : j ≠ l) : ∑ X : Fin m → α, prodDist p X * ((if X i = X j then (1 : ℝ) else 0) * (if X k = X l then (1 : ℝ) else 0)) = (l2Sq p) ^ 2 := by have h1 : ∀ X : Fin m → α, prodDist p X * ((if X i = X j then (1 : ℝ) else 0) * (if X k = X l then (1 : ℝ) else 0)) = ∑ x : α, ∑ y : α, if (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) then prodDist p X else 0 := by intro X by_cases h : X i = X j ∧ X k = X l · rw [if_pos h.1, if_pos h.2, mul_one, mul_one] rw [Finset.sum_eq_single (X i)] · rw [Finset.sum_eq_single (X k)] · simp [h.1.symm, h.2.symm] · intro y _ hy; simp [Ne.symm hy] · intro hm; exact absurd (Finset.mem_univ _) hm · intro x _ hx; exact Finset.sum_eq_zero (fun y _ => by simp [Ne.symm hx]) · intro hm; exact absurd (Finset.mem_univ _) hm · have hz : (if X i = X j then (1 : ℝ) else 0) * (if X k = X l then (1 : ℝ) else 0) = 0 := by split_ifs with h1' h2' · exact absurd ⟨h1', h2'⟩ h · ring · ring · ring rw [hz, mul_zero] refine (Finset.sum_eq_zero (fun x _ => Finset.sum_eq_zero (fun y _ => ?_))).symm split_ifs with hxy · exact absurd ⟨hxy.1.1.trans hxy.1.2.symm, hxy.2.1.trans hxy.2.2.symm⟩ h · rfl simp_rw [h1]; rw [Finset.sum_comm] have hswap : ∀ x : α, ∑ X : Fin m → α, ∑ y : α, (if (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) then prodDist p X else 0) = ∑ y : α, ∑ X : Fin m → α, (if (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) then prodDist p X else 0) := fun _ => Finset.sum_comm simp_rw [hswap] have hinner : ∀ x y : α, ∑ X : Fin m → α, (if (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) then prodDist p X else 0) = (p x) ^ 2 * (p y) ^ 2 := by intro x y let q : Fin m → α → ℝ := fun t a => if t = i ∨ t = j then (if a = x then p x else 0) else if t = k ∨ t = l then (if a = y then p y else 0) else p a have hterm : ∀ X : Fin m → α, (if (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) then prodDist p X else 0) = ∏ t : Fin m, q t (X t) := by intro X by_cases h : (X i = x ∧ X j = x) ∧ (X k = y ∧ X l = y) · rw [if_pos h]; dsimp only [prodDist, q] refine Finset.prod_congr rfl (fun t _ => ?_) by_cases h1' : t = i ∨ t = j · rw [if_pos h1']; rcases h1' with rfl | rfl <;> simp [h.1.1, h.1.2] · rw [if_neg h1'] by_cases h2' : t = k ∨ t = l · rw [if_pos h2']; rcases h2' with rfl | rfl <;> simp [h.2.1, h.2.2] · rw [if_neg h2'] · rw [if_neg h]; symm by_cases hi : X i = x · by_cases hj : X j = x · by_cases hk : X k = y · have hl' : X l ≠ y := fun h' => h ⟨⟨hi, hj⟩, ⟨hk, h'⟩⟩ exact Finset.prod_eq_zero (Finset.mem_univ l) (by dsimp only [q]; simp [hil.symm, hjl.symm, hl']) · exact Finset.prod_eq_zero (Finset.mem_univ k) (by dsimp only [q]; simp [hik.symm, hjk.symm, hk]) · exact Finset.prod_eq_zero (Finset.mem_univ j) (by dsimp only [q]; simp [hj]) · exact Finset.prod_eq_zero (Finset.mem_univ i) (by dsimp only [q]; simp [hi]) simp_rw [hterm]; rw [← Fintype.prod_sum q] have hfac : ∀ t : Fin m, ∑ a : α, q t a = if t = i ∨ t = j then p x else if t = k ∨ t = l then p y else 1 := by intro t; dsimp only [q] by_cases h1' : t = i ∨ t = j · simp only [h1', ↓reduceIte]; rw [Finset.sum_eq_single x] · simp · intro a _ ha; simp [ha] · intro hm; exact absurd (Finset.mem_univ x) hm · simp only [h1', ↓reduceIte] by_cases h2' : t = k ∨ t = l · simp only [h2', ↓reduceIte]; rw [Finset.sum_eq_single y] · simp · intro a _ ha; simp [ha] · intro hm; exact absurd (Finset.mem_univ y) hm · simp only [h2', ↓reduceIte]; exact hp.2 simp_rw [hfac] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ i)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hij.symm, Finset.mem_univ j⟩)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hjk.symm, Finset.mem_erase.mpr ⟨hik.symm, Finset.mem_univ k⟩⟩)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hkl.symm, Finset.mem_erase.mpr ⟨hjl.symm, Finset.mem_erase.mpr ⟨hil.symm, Finset.mem_univ l⟩⟩⟩)] have hrest : ∏ t ∈ (((Finset.univ.erase i).erase j).erase k).erase l, (if t = i ∨ t = j then p x else if t = k ∨ t = l then p y else 1) = 1 := Finset.prod_eq_one (fun t ht => by have hl' := (Finset.mem_erase.mp ht).1 have hk' := (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).1 have hj' := (Finset.mem_erase.mp (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).2).1 have hi' := (Finset.mem_erase.mp (Finset.mem_erase.mp (Finset.mem_erase.mp (Finset.mem_erase.mp ht).2).2).2).1 simp [hi', hj', hk', hl']) rw [hrest, mul_one] simp [hik.symm, hjk.symm, hil.symm, hjl.symm, sq]; ring simp_rw [hinner, ← Finset.mul_sum, ← Finset.sum_mul] dsimp only [l2Sq]; ring /-- **EXACT MOMENT BOUND FOR `l3Cube p - (l2Sq p)^2`** in terms of `Δ = l2Sq p - 1 / n`: For ANY distribution `p` on `Fin n` (`1 ≤ n`) with `Δ := l2Sq p - 1 / n ≥ 0`: **`l3Cube p - (l2Sq p) ^ 2 ≤ Δ / n + Δ * √Δ`**! -/ theorem l3Cube_sub_l2Sq_sq_bound {n : ℕ} (hn : 1 ≤ n) {p : Fin n → ℝ} (hp : IsProbDist p) : let Δ : ℝ := l2Sq p - 1 / (n : ℝ) 0 ≤ Δ ∧ l3Cube p - (l2Sq p) ^ 2 ≤ Δ / (n : ℝ) + Δ * Real.sqrt Δ := by intro Δ have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hΔeq := (l2_excess_ge_four_tv_sq_div_n hn hp).1 have hΔnn : 0 ≤ Δ := by dsimp only [Δ]; rw [← hΔeq]; exact Finset.sum_nonneg (fun _ _ => sq_nonneg _) refine ⟨hΔnn, ?_⟩ let δ : Fin n → ℝ := fun x => p x - 1 / (n : ℝ) have hδsum : ∑ x : Fin n, δ x = 0 := by dsimp only [δ]; rw [Finset.sum_sub_distrib, hp.2] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos]; ring have hδsq : ∑ x : Fin n, (δ x) ^ 2 = Δ := hΔeq have hl2 : l2Sq p = 1 / (n : ℝ) + Δ := by dsimp only [Δ]; ring have hl3 : l3Cube p = 1 / (n : ℝ) ^ 2 + 3 * Δ / (n : ℝ) + ∑ x : Fin n, (δ x) ^ 3 := by dsimp only [l3Cube] have hpt : ∀ x : Fin n, (p x) ^ 3 = (1 / (n : ℝ)) ^ 3 + 3 * (1 / (n : ℝ)) ^ 2 * δ x + 3 * (1 / (n : ℝ)) * (δ x) ^ 2 + (δ x) ^ 3 := fun x => by dsimp only [δ]; ring simp_rw [hpt, Finset.sum_add_distrib, ← Finset.mul_sum, hδsum, hδsq] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos]; ring have hcube_le : ∑ x : Fin n, (δ x) ^ 3 ≤ Δ * Real.sqrt Δ := by calc ∑ x : Fin n, (δ x) ^ 3 = ∑ x : Fin n, δ x * (δ x) ^ 2 := Finset.sum_congr rfl (fun x _ => by ring) _ ≤ ∑ x : Fin n, Real.sqrt Δ * (δ x) ^ 2 := Finset.sum_le_sum (fun x _ => by have hxsq : (δ x) ^ 2 ≤ Δ := by rw [← hδsq]; exact Finset.single_le_sum (fun y _ => sq_nonneg (δ y)) (Finset.mem_univ x) have hxle : δ x ≤ Real.sqrt Δ := le_trans (le_abs_self (δ x)) (by rw [← Real.sqrt_sq_eq_abs]; exact Real.sqrt_le_sqrt hxsq) exact mul_le_mul_of_nonneg_right hxle (sq_nonneg _)) _ = Real.sqrt Δ * Δ := by rw [← Finset.mul_sum, hδsq] _ = Δ * Real.sqrt Δ := mul_comm _ _ have hdiff : l3Cube p - (l2Sq p) ^ 2 = Δ / (n : ℝ) + ∑ x : Fin n, (δ x) ^ 3 - Δ ^ 2 := by rw [hl3, hl2]; ring rw [hdiff] nlinarith [sq_nonneg Δ, hcube_le] /-- **BFRSW EXACT COLLISION-COUNT VARIANCE BOUND**: For ANY distribution `p` on `Fin n` (`1 ≤ n`) and ANY `m ≥ 2`, the variance of `empCollisions X` satisfies: **`Var_{p^{⊗ m}}(empCollisions) ≤ ((samplePairs m).card) * l2Sq p + 2 * (m : ℝ) * ((samplePairs m).card) * (l3Cube p - (l2Sq p) ^ 2)`**! (And on the uniform distribution `U_n`, `l3Cube U_n - (l2Sq U_n)^2 = 0`, so `Var_{U_n}(empCollisions) ≤ M / n`!) -/ theorem bfrsw_collision_variance_bound {n m : ℕ} {p : Fin n → ℝ} (hp : IsProbDist p) : let M : ℝ := ((samplePairs m).card : ℝ) ∑ X : Fin m → Fin n, prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 ≤ M * l2Sq p + 2 * (m : ℝ) * M * max 0 (l3Cube p - (l2Sq p) ^ 2) := by intro M have hpd := prodDist_isProbDist (m := m) hp -- Expand `Var = E[C^2] - (E[C])^2 = ∑_{e₁, e₂ ∈ samplePairs m} Cov(1_{e₁}, 1_{e₂})`! have hvar_eq : ∑ X : Fin m → Fin n, prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 = ∑ e₁ ∈ samplePairs m, ∑ e₂ ∈ samplePairs m, ((∑ X : Fin m → Fin n, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0))) - (l2Sq p) ^ 2) := by have hexp : ∀ X : Fin m → Fin n, prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 = prodDist p X * (empCollisions X) ^ 2 - 2 * (M * l2Sq p) * (prodDist p X * empCollisions X) + (M * l2Sq p) ^ 2 * prodDist p X := fun X => by ring simp_rw [hexp, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum, bfrsw_collision_estimator_unbiased hp, hpd.2] have hsq : ∑ X : Fin m → Fin n, prodDist p X * (empCollisions X) ^ 2 = ∑ e₁ ∈ samplePairs m, ∑ e₂ ∈ samplePairs m, ∑ X : Fin m → Fin n, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0)) := by have h1 : ∀ X : Fin m → Fin n, prodDist p X * (empCollisions X) ^ 2 = ∑ e₁ ∈ samplePairs m, ∑ e₂ ∈ samplePairs m, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0)) := fun X => by dsimp only [empCollisions]; rw [sq, Finset.sum_mul_sum, Finset.mul_sum] refine Finset.sum_congr rfl (fun e₁ _ => Finset.mul_sum _ _ _) simp_rw [h1]; rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun e₁ _ => Finset.sum_comm) rw [hsq] have hconst : ∑ _e₁ ∈ samplePairs m, ∑ _e₂ ∈ samplePairs m, (l2Sq p) ^ 2 = (M * l2Sq p) ^ 2 := by simp [M]; ring rw [hconst]; ring rw [hvar_eq] -- Bound each fixed `e₁ ∈ samplePairs m` summed over `e₂ ∈ samplePairs m`! have hper_e1 : ∀ e₁ ∈ samplePairs m, ∑ e₂ ∈ samplePairs m, ((∑ X : Fin m → Fin n, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0))) - (l2Sq p) ^ 2) ≤ l2Sq p + 2 * (m : ℝ) * max 0 (l3Cube p - (l2Sq p) ^ 2) := by intro e₁ he₁ have hlt1 : e₁.1 < e₁.2 := (Finset.mem_filter.mp he₁).2 have hne1 : e₁.1 ≠ e₁.2 := ne_of_lt hlt1 -- Split `e₂ ∈ samplePairs m` into: `e₂ = e₁`, shares 1 vertex with `e₁`, or disjoint from `e₁`! let shareSet := (samplePairs m).filter (fun e₂ => e₂ ≠ e₁ ∧ (e₂.1 = e₁.1 ∨ e₂.1 = e₁.2 ∨ e₂.2 = e₁.1 ∨ e₂.2 = e₁.2)) have hcov_bound : ∀ e₂ ∈ samplePairs m, (∑ X : Fin m → Fin n, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0))) - (l2Sq p) ^ 2 ≤ if e₂ = e₁ then l2Sq p else if e₂ ∈ shareSet then max 0 (l3Cube p - (l2Sq p) ^ 2) else 0 := by intro e₂ he₂ have hlt2 : e₂.1 < e₂.2 := (Finset.mem_filter.mp he₂).2 have hne2 : e₂.1 ≠ e₂.2 := ne_of_lt hlt2 by_cases heq : e₂ = e₁ · subst heq; rw [if_pos rfl] have hsq1 : ∀ X : Fin m → Fin n, (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0) = if X e₂.1 = X e₂.2 then (1 : ℝ) else 0 := fun X => by split_ifs <;> norm_num simp_rw [hsq1, prodDist_pair_coll_expectation hp hne2] nlinarith [sq_nonneg (l2Sq p)] · rw [if_neg heq] by_cases hsh : e₂ ∈ shareSet · rw [if_pos hsh] -- `e₁` and `e₂` share exactly ONE vertex (since both have `a < b` and `e₂ ≠ e₁`)! have hdisj_or := (Finset.mem_filter.mp hsh).2.2 have h3eq : ∑ X : Fin m → Fin n, prodDist p X * ((if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0)) = l3Cube p := by rcases hdisj_or with h | h | h | h · have hne22 : e₁.2 ≠ e₂.2 := fun h' => heq (Prod.ext h h'.symm) rw [h]; exact prodDist_triple_coll_expectation hp hne1 (by rwa [← h]) hne22 · have hne12 : e₁.1 ≠ e₂.2 := ne_of_lt (lt_trans hlt1 (h ▸ hlt2)) have hcomm : ∀ X : Fin m → Fin n, (if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0) = (if X e₁.2 = X e₁.1 then (1 : ℝ) else 0) * (if X e₁.2 = X e₂.2 then (1 : ℝ) else 0) := fun X => by simp only [h, eq_comm (a := X e₁.1)] simp_rw [hcomm] exact prodDist_triple_coll_expectation hp hne1.symm (by rwa [← h]) hne12 · have hne12 : e₂.1 ≠ e₁.2 := ne_of_lt (lt_trans hlt2 (h ▸ hlt1)) have hcomm : ∀ X : Fin m → Fin n, (if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0) = (if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₁.1 = X e₂.1 then (1 : ℝ) else 0) := fun X => by simp only [h, eq_comm (a := X e₂.1)] simp_rw [hcomm] exact prodDist_triple_coll_expectation hp hne1 (by rw [← h]; exact hne2.symm) hne12.symm · have hne11 : e₁.1 ≠ e₂.1 := fun h' => heq (Prod.ext h'.symm h) have hcomm : ∀ X : Fin m → Fin n, (if X e₁.1 = X e₁.2 then (1 : ℝ) else 0) * (if X e₂.1 = X e₂.2 then (1 : ℝ) else 0) = (if X e₁.2 = X e₁.1 then (1 : ℝ) else 0) * (if X e₁.2 = X e₂.1 then (1 : ℝ) else 0) := fun X => by simp only [h, eq_comm (a := X e₁.1), eq_comm (a := X e₂.1)] simp_rw [hcomm] exact prodDist_triple_coll_expectation hp hne1.symm (by rw [← h]; exact hne2.symm) hne11 rw [h3eq]; exact le_max_right _ _ · rw [if_neg hsh] have hnot_sh : ¬ (e₂.1 = e₁.1 ∨ e₂.1 = e₁.2 ∨ e₂.2 = e₁.1 ∨ e₂.2 = e₁.2) := fun h => hsh (Finset.mem_filter.mpr ⟨he₂, heq, h⟩) push Not at hnot_sh rw [prodDist_disjoint_pairs_expectation hp hne1 hne2 hnot_sh.1.symm hnot_sh.2.2.1.symm hnot_sh.2.1.symm hnot_sh.2.2.2.symm, sub_self] -- Count `shareSet.card ≤ 2 * m`! have hsh_card : shareSet.card ≤ 2 * m := by have hsub : shareSet ⊆ (Finset.univ.image (fun k : Fin m => if k < e₁.1 then (k, e₁.1) else (e₁.1, k))) ∪ (Finset.univ.image (fun k : Fin m => if k < e₁.2 then (k, e₁.2) else (e₁.2, k))) := by intro e₂ he₂ have hlt2 : e₂.1 < e₂.2 := (Finset.mem_filter.mp (Finset.mem_filter.mp he₂).1).2 rcases (Finset.mem_filter.mp he₂).2.2 with h | h | h | h · exact Finset.mem_union_left _ (Finset.mem_image.mpr ⟨e₂.2, Finset.mem_univ _, by rw [if_neg (not_lt.mpr (le_of_lt (h ▸ hlt2)))]; exact Prod.ext h.symm rfl⟩) · exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨e₂.2, Finset.mem_univ _, by rw [if_neg (not_lt.mpr (le_of_lt (h ▸ hlt2)))]; exact Prod.ext h.symm rfl⟩) · exact Finset.mem_union_left _ (Finset.mem_image.mpr ⟨e₂.1, Finset.mem_univ _, by rw [if_pos (h ▸ hlt2)]; exact Prod.ext rfl h.symm⟩) · exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨e₂.1, Finset.mem_univ _, by rw [if_pos (h ▸ hlt2)]; exact Prod.ext rfl h.symm⟩) calc shareSet.card ≤ ((Finset.univ.image (fun k : Fin m => if k < e₁.1 then (k, e₁.1) else (e₁.1, k))) ∪ (Finset.univ.image (fun k : Fin m => if k < e₁.2 then (k, e₁.2) else (e₁.2, k)))).card := Finset.card_le_card hsub _ ≤ (Finset.univ.image (fun k : Fin m => if k < e₁.1 then (k, e₁.1) else (e₁.1, k))).card + (Finset.univ.image (fun k : Fin m => if k < e₁.2 then (k, e₁.2) else (e₁.2, k))).card := Finset.card_union_le _ _ _ ≤ m + m := add_le_add (le_trans Finset.card_image_le (by simp)) (le_trans Finset.card_image_le (by simp)) _ = 2 * m := by omega calc ∑ e₂ ∈ samplePairs m, _ ≤ ∑ e₂ ∈ samplePairs m, (if e₂ = e₁ then l2Sq p else if e₂ ∈ shareSet then max 0 (l3Cube p - (l2Sq p) ^ 2) else 0) := Finset.sum_le_sum hcov_bound _ = l2Sq p + (shareSet.card : ℝ) * max 0 (l3Cube p - (l2Sq p) ^ 2) := by rw [← Finset.add_sum_erase _ _ he₁, if_pos rfl] congr 1 have her : ∑ e₂ ∈ (samplePairs m).erase e₁, (if e₂ = e₁ then l2Sq p else if e₂ ∈ shareSet then max 0 (l3Cube p - (l2Sq p) ^ 2) else 0) = ∑ e₂ ∈ (samplePairs m).erase e₁, if e₂ ∈ shareSet then max 0 (l3Cube p - (l2Sq p) ^ 2) else 0 := Finset.sum_congr rfl (fun e₂ he₂ => by rw [if_neg (Finset.mem_erase.mp he₂).1]) rw [her, ← Finset.sum_filter] have hf_eq : ((samplePairs m).erase e₁).filter (fun e₂ => e₂ ∈ shareSet) = shareSet := by ext e₂; simp only [Finset.mem_filter, Finset.mem_erase, shareSet]; tauto rw [hf_eq]; simp _ ≤ l2Sq p + 2 * (m : ℝ) * max 0 (l3Cube p - (l2Sq p) ^ 2) := by have hcast : (shareSet.card : ℝ) ≤ 2 * (m : ℝ) := by exact_mod_cast hsh_card nlinarith [le_max_left (0 : ℝ) (l3Cube p - (l2Sq p) ^ 2)] calc ∑ e₁ ∈ samplePairs m, ∑ e₂ ∈ samplePairs m, _ ≤ ∑ _e₁ ∈ samplePairs m, (l2Sq p + 2 * (m : ℝ) * max 0 (l3Cube p - (l2Sq p) ^ 2)) := Finset.sum_le_sum hper_e1 _ = M * l2Sq p + 2 * (m : ℝ) * M * max 0 (l3Cube p - (l2Sq p) ^ 2) := by simp [M]; ring /-- **BFRSW `O(√n / ε²)` UNIFORMITY TESTER CHEBYSHEV UPPER BOUND**: With collision threshold `τ = M * (1 / n + 2 * ε² / n)` (`M = m(m - 1)/2`), on `m ≥ 2` i.i.d. samples from `p` on `Fin n` (`1 ≤ n`, `0 < ε ≤ 1`): 1. **Completeness (on `U_n`)**: `Pr_{U_n^m}[empCollisions X ≥ τ] ≤ n / (4 * M * ε⁴)` (which is `≤ 1 / 3` whenever `m ≥ 4 * √n / ε²`!); 2. **Soundness (on any `p` with `TV(p, U_n) ≥ ε`)**: with `Δ = l2Sq p - 1 / n ≥ 4 ε² / n`, `Pr_{p^m}[empCollisions X < τ] ≤ (4 / (M * Δ²)) * (1 / n + Δ) + (8 * m / (M * Δ²)) * (Δ / n + Δ * √Δ)` (which is `≤ 1 / 3` whenever `m = O(√n / ε²)`!)! -/ theorem bfrsw_uniformity_tester_chebyshev {n m : ℕ} (hn : 1 ≤ n) (hm : 2 ≤ m) {ε : ℝ} (hε : 0 < ε) : let M : ℝ := ((samplePairs m).card : ℝ) let τ : ℝ := M * (1 / (n : ℝ) + 2 * ε ^ 2 / (n : ℝ)) (∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X ≤ (n : ℝ) / (4 * M * ε ^ 4)) ∧ (∀ p : Fin n → ℝ, IsProbDist p → ε ≤ tvDist p (uniformDist (Fin n)) → let Δ : ℝ := l2Sq p - 1 / (n : ℝ) ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X ≤ 4 * (1 / (n : ℝ) + Δ) / (M * Δ ^ 2) + 8 * (m : ℝ) * (Δ / (n : ℝ) + Δ * Real.sqrt Δ) / (M * Δ ^ 2)) := by intro M τ have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hM_pos : 0 < M := by dsimp only [M] have h2M := samplePairs_card_two_mul m have hge : 2 ≤ m * (m - 1) := by cases m with | zero => omega | succ k => have : 1 ≤ k := by omega have : 2 ≤ (k + 1) * k := by nlinarith simpa using this exact Nat.cast_pos.mpr (by omega) have hunif_prob : IsProbDist (uniformDist (Fin n)) := by dsimp only [IsProbDist, uniformDist] refine ⟨fun _ => by positivity, ?_⟩ simp [Fintype.card_fin, ne_of_gt hnpos] have hunif_l2 : l2Sq (uniformDist (Fin n)) = 1 / (n : ℝ) := by dsimp only [l2Sq, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] have hunif_l3 : l3Cube (uniformDist (Fin n)) = 1 / (n : ℝ) ^ 2 := by dsimp only [l3Cube, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] constructor · -- Part 1: Completeness on Uniform `U_n`! have hvar := bfrsw_collision_variance_bound (m := m) hunif_prob rw [hunif_l2, hunif_l3] at hvar have hzero : l3Cube (uniformDist (Fin n)) - (l2Sq (uniformDist (Fin n))) ^ 2 = 0 := by rw [hunif_l2, hunif_l3]; ring have hmax0 : max (0 : ℝ) (1 / (n : ℝ) ^ 2 - (1 / (n : ℝ)) ^ 2) = 0 := by have : 1 / (n : ℝ) ^ 2 - (1 / (n : ℝ)) ^ 2 = 0 := by ring rw [this, max_self] rw [hmax0] at hvar have hcheb : (M * (2 * ε ^ 2 / (n : ℝ))) ^ 2 * ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X ≤ M * (1 / (n : ℝ)) := by have hpd := prodDist_isProbDist (m := m) hunif_prob calc (M * (2 * ε ^ 2 / (n : ℝ))) ^ 2 * ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X = ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X * (M * (2 * ε ^ 2 / (n : ℝ))) ^ 2 := by rw [Finset.mul_sum]; exact Finset.sum_congr rfl (fun _ _ => mul_comm _ _) _ ≤ ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X * (empCollisions X - M * (1 / (n : ℝ))) ^ 2 := Finset.sum_le_sum (fun X hX => by have hge := (Finset.mem_filter.mp hX).2 dsimp only [τ] at hge have hdiff : M * (2 * ε ^ 2 / (n : ℝ)) ≤ empCollisions X - M * (1 / (n : ℝ)) := by linarith exact mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (by positivity) hdiff 2) (hpd.1 X)) _ ≤ ∑ X : Fin m → Fin n, prodDist (uniformDist (Fin n)) X * (empCollisions X - M * (1 / (n : ℝ))) ^ 2 := Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun X _ _ => mul_nonneg (hpd.1 X) (sq_nonneg _)) _ ≤ M * (1 / (n : ℝ)) := by linarith have hden_pos : 0 < (M * (2 * ε ^ 2 / (n : ℝ))) ^ 2 := by positivity calc ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X ≤ (M * (1 / (n : ℝ))) / (M * (2 * ε ^ 2 / (n : ℝ))) ^ 2 := by rw [le_div_iff₀ hden_pos]; linarith _ = (n : ℝ) / (4 * M * ε ^ 4) := by field_simp [ne_of_gt hnpos, ne_of_gt hM_pos, ne_of_gt hε]; ring · -- Part 2: Soundness on any `ε`-far distribution `p`! intro p hp hfar Δ have hΔ_ge : 4 * ε ^ 2 / (n : ℝ) ≤ Δ := by have hcs := (l2_excess_ge_four_tv_sq_div_n hn hp).2 have hsq : ε ^ 2 ≤ (tvDist p (uniformDist (Fin n))) ^ 2 := pow_le_pow_left₀ (le_of_lt hε) hfar 2 dsimp only [Δ] calc 4 * ε ^ 2 / (n : ℝ) ≤ 4 * (tvDist p (uniformDist (Fin n))) ^ 2 / (n : ℝ) := by gcongr _ ≤ l2Sq p - 1 / (n : ℝ) := hcs have hΔ_pos : 0 < Δ := lt_of_lt_of_le (by positivity) hΔ_ge have hvar := bfrsw_collision_variance_bound (m := m) hp have hl3 := (l3Cube_sub_l2Sq_sq_bound hn hp).2 have hmax_le : max 0 (l3Cube p - (l2Sq p) ^ 2) ≤ Δ / (n : ℝ) + Δ * Real.sqrt Δ := max_le (by positivity) hl3 have hvar_ub : ∑ X : Fin m → Fin n, prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 ≤ M * (1 / (n : ℝ) + Δ) + 2 * (m : ℝ) * M * (Δ / (n : ℝ) + Δ * Real.sqrt Δ) := by have hl2eq : l2Sq p = 1 / (n : ℝ) + Δ := by dsimp only [Δ]; ring rw [← hl2eq] have hm_nn : 0 ≤ 2 * (m : ℝ) * M := mul_nonneg (mul_nonneg (by norm_num) (Nat.cast_nonneg m)) (le_of_lt hM_pos) have hmul := mul_le_mul_of_nonneg_left hmax_le hm_nn linarith have hgap : M * (Δ / 2) ≤ M * l2Sq p - τ := by dsimp only [τ] have hl2eq : l2Sq p = 1 / (n : ℝ) + Δ := by dsimp only [Δ]; ring rw [hl2eq] have hhalf_Δ : 2 * ε ^ 2 / (n : ℝ) ≤ Δ / 2 := by have h2 : 2 * (2 * ε ^ 2 / (n : ℝ)) = 4 * ε ^ 2 / (n : ℝ) := by ring linarith [hΔ_ge] nlinarith [hhalf_Δ, hM_pos] have hcheb : (M * (Δ / 2)) ^ 2 * ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X ≤ M * (1 / (n : ℝ) + Δ) + 2 * (m : ℝ) * M * (Δ / (n : ℝ) + Δ * Real.sqrt Δ) := by have hpd := prodDist_isProbDist (m := m) hp calc (M * (Δ / 2)) ^ 2 * ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X = ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X * (M * (Δ / 2)) ^ 2 := by rw [Finset.mul_sum]; exact Finset.sum_congr rfl (fun _ _ => mul_comm _ _) _ ≤ ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 := Finset.sum_le_sum (fun X hX => by have hlt := (Finset.mem_filter.mp hX).2 have hdiff : M * (Δ / 2) ≤ M * l2Sq p - empCollisions X := by linarith have hsq := pow_le_pow_left₀ (by positivity) hdiff 2 exact mul_le_mul_of_nonneg_left (by nlinarith) (hpd.1 X)) _ ≤ ∑ X : Fin m → Fin n, prodDist p X * (empCollisions X - M * l2Sq p) ^ 2 := Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun X _ _ => mul_nonneg (hpd.1 X) (sq_nonneg _)) _ ≤ M * (1 / (n : ℝ) + Δ) + 2 * (m : ℝ) * M * (Δ / (n : ℝ) + Δ * Real.sqrt Δ) := hvar_ub have hden_pos : 0 < (M * (Δ / 2)) ^ 2 := by positivity calc ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => empCollisions X < τ), prodDist p X ≤ (M * (1 / (n : ℝ) + Δ) + 2 * (m : ℝ) * M * (Δ / (n : ℝ) + Δ * Real.sqrt Δ)) / (M * (Δ / 2)) ^ 2 := by rw [le_div_iff₀ hden_pos]; linarith _ = 4 * (1 / (n : ℝ) + Δ) / (M * Δ ^ 2) + 8 * (m : ℝ) * (Δ / (n : ℝ) + Δ * Real.sqrt Δ) / (M * Δ ^ 2) := by field_simp [ne_of_gt hM_pos, ne_of_gt hΔ_pos]; ring /-! ### Part 3B: Paninski / Ingster–Le Cam `Ω(√n / ε²)` Sample Complexity Lower Bound -/ /-- Paninski perturbed distribution `paninskiDist k ε z` on `Fin (2 * k)` (`0 ≤ ε ≤ 1 / 2`): each pair `{2i, 2i + 1}` receives probabilities `(1 + 2 ε s_i) / (2k)` and `(1 - 2 ε s_i) / (2k)`. -/ noncomputable def paninskiDist {k : ℕ} (ε : ℝ) (z : Fin k → Bool) (x : Fin (2 * k)) : ℝ := let s : ℝ := if z ⟨x.1 / 2, by omega⟩ then 1 else -1 if x.1 % 2 = 0 then (1 + 2 * ε * s) / ((2 * k : ℕ) : ℝ) else (1 - 2 * ε * s) / ((2 * k : ℕ) : ℝ) /-- Every Paninski perturbed distribution `paninskiDist ε z` is a valid probability distribution on `Fin (2 * k)` with exact total variation distance **`= ε`** from `U_{2k}`! -/ theorem paninskiDist_valid_and_eps_far {k : ℕ} (hk : 1 ≤ k) {ε : ℝ} (h0 : 0 ≤ ε) (hhalf : ε ≤ 1 / 2) (z : Fin k → Bool) : IsProbDist (paninskiDist ε z) ∧ tvDist (paninskiDist ε z) (uniformDist (Fin (2 * k))) = ε := by have hkpos : (0 : ℝ) < (k : ℝ) := Nat.cast_pos.mpr (by omega) have h2kpos : (0 : ℝ) < ((2 * k : ℕ) : ℝ) := by push_cast; positivity -- Split any sum over `Fin (2 * k)` into pairs `(2i, 2i + 1)` for `i : Fin k`! have hpair_sum : ∀ f : Fin (2 * k) → ℝ, ∑ x : Fin (2 * k), f x = ∑ i : Fin k, (f ⟨2 * i.1, by omega⟩ + f ⟨2 * i.1 + 1, by omega⟩) := by intro f let e : (Fin k × Bool) ≃ Fin (2 * k) := { toFun := fun p => ⟨2 * p.1.1 + if p.2 then 1 else 0, by have := p.1.2; split_ifs <;> omega⟩ invFun := fun x => (⟨x.1 / 2, by omega⟩, decide (x.1 % 2 = 1)) left_inv := fun ⟨i, b⟩ => by cases b · exact Prod.ext (Fin.ext (by simp)) (by simp) · exact Prod.ext (Fin.ext (by simp; omega)) (by simp [show (2 * i.1 + 1) % 2 = 1 by omega]) right_inv := fun x => by apply Fin.ext; dsimp only by_cases h : x.1 % 2 = 1 · simp [h]; omega · simp [h]; omega } rw [← Equiv.sum_comp e f, Fintype.sum_prod_type] refine Finset.sum_congr rfl (fun i _ => ?_) simp only [Fintype.sum_bool, e, Equiv.coe_fn_mk, Bool.false_eq_true, ↓reduceIte, add_zero] ring refine ⟨⟨fun x => ?_, ?_⟩, ?_⟩ · dsimp only [paninskiDist] split_ifs <;> apply div_nonneg _ (le_of_lt h2kpos) <;> linarith · rw [hpair_sum] have hpt : ∀ i : Fin k, paninskiDist ε z ⟨2 * i.1, by omega⟩ + paninskiDist ε z ⟨2 * i.1 + 1, by omega⟩ = 1 / (k : ℝ) := by intro i dsimp only [paninskiDist] have hev : (2 * i.1) % 2 = 0 := by omega have hod : ¬ ((2 * i.1 + 1) % 2 = 0) := by omega have hdiv1 : (⟨(2 * i.1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) have hdiv2 : (⟨(2 * i.1 + 1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) rw [if_pos hev, if_neg hod, hdiv1, hdiv2] push_cast; field_simp [ne_of_gt hkpos]; ring simp_rw [hpt] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hkpos] · dsimp only [tvDist, uniformDist] rw [hpair_sum] have hpt : ∀ i : Fin k, |paninskiDist ε z ⟨2 * i.1, by omega⟩ - 1 / (Fintype.card (Fin (2 * k)) : ℝ)| + |paninskiDist ε z ⟨2 * i.1 + 1, by omega⟩ - 1 / (Fintype.card (Fin (2 * k)) : ℝ)| = 2 * ε / (k : ℝ) := by intro i dsimp only [paninskiDist] have hev : (2 * i.1) % 2 = 0 := by omega have hod : ¬ ((2 * i.1 + 1) % 2 = 0) := by omega have hdiv1 : (⟨(2 * i.1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) have hdiv2 : (⟨(2 * i.1 + 1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) rw [if_pos hev, if_neg hod, hdiv1, hdiv2, Fintype.card_fin] cases z i · simp only [Bool.false_eq_true, ↓reduceIte] have h1 : (1 + 2 * ε * -1) / ((2 * k : ℕ) : ℝ) - 1 / ((2 * k : ℕ) : ℝ) = -(ε / (k : ℝ)) := by push_cast; field_simp [ne_of_gt hkpos]; ring have h2 : (1 - 2 * ε * -1) / ((2 * k : ℕ) : ℝ) - 1 / ((2 * k : ℕ) : ℝ) = ε / (k : ℝ) := by push_cast; field_simp [ne_of_gt hkpos]; ring rw [h1, h2, abs_neg, abs_of_nonneg (by positivity)]; ring · simp only [↓reduceIte] have h1 : (1 + 2 * ε * 1) / ((2 * k : ℕ) : ℝ) - 1 / ((2 * k : ℕ) : ℝ) = ε / (k : ℝ) := by push_cast; field_simp [ne_of_gt hkpos]; ring have h2 : (1 - 2 * ε * 1) / ((2 * k : ℕ) : ℝ) - 1 / ((2 * k : ℕ) : ℝ) = -(ε / (k : ℝ)) := by push_cast; field_simp [ne_of_gt hkpos]; ring rw [h1, h2, abs_neg, abs_of_nonneg (by positivity)]; ring simp_rw [hpt] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hkpos] /-- **LE CAM / CAUCHY–SCHWARZ `TV ≤ (1 / 2) √(χ²)` BOUND**: For ANY probability distributions `P` and `Q` with `∀ x, 0 < Q x`, **`tvDist P Q ≤ (1 / 2) * Real.sqrt ((∑ x, (P x) ^ 2 / Q x) - 1)`**! -/ theorem tvDist_le_half_sqrt_chi2 {α : Type*} [Fintype α] {P Q : α → ℝ} (hP : IsProbDist P) (hQ : IsProbDist Q) (hQpos : ∀ x, 0 < Q x) : tvDist P Q ≤ (1 / 2 : ℝ) * Real.sqrt ((∑ x : α, (P x) ^ 2 / Q x) - 1) := by have hchi_eq : ∑ x : α, (P x - Q x) ^ 2 / Q x = (∑ x : α, (P x) ^ 2 / Q x) - 1 := by have hpt : ∀ x : α, (P x - Q x) ^ 2 / Q x = (P x) ^ 2 / Q x - 2 * P x + Q x := fun x => by have hne := ne_of_gt (hQpos x) field_simp [hne]; ring simp_rw [hpt, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum, hP.2, hQ.2] ring have hcs : (∑ x : α, Real.sqrt (Q x) * (|P x - Q x| / Real.sqrt (Q x))) ^ 2 ≤ (∑ x : α, (Real.sqrt (Q x)) ^ 2) * (∑ x : α, (|P x - Q x| / Real.sqrt (Q x)) ^ 2) := Finset.sum_mul_sq_le_sq_mul_sq Finset.univ (fun x => Real.sqrt (Q x)) (fun x => |P x - Q x| / Real.sqrt (Q x)) have hlhs : ∑ x : α, Real.sqrt (Q x) * (|P x - Q x| / Real.sqrt (Q x)) = ∑ x : α, |P x - Q x| := Finset.sum_congr rfl (fun x _ => mul_div_cancel₀ _ (ne_of_gt (Real.sqrt_pos.mpr (hQpos x)))) have hrhs1 : ∑ x : α, (Real.sqrt (Q x)) ^ 2 = 1 := by simp_rw [Real.sq_sqrt (le_of_lt (hQpos _))]; exact hQ.2 have hrhs2 : ∑ x : α, (|P x - Q x| / Real.sqrt (Q x)) ^ 2 = (∑ x : α, (P x) ^ 2 / Q x) - 1 := by rw [← hchi_eq] refine Finset.sum_congr rfl (fun x _ => ?_) rw [div_pow, sq_abs, Real.sq_sqrt (le_of_lt (hQpos x))] rw [hlhs, hrhs1, one_mul, hrhs2] at hcs have hsqrt := Real.le_sqrt_of_sq_le hcs dsimp only [tvDist]; linarith /-- **EXACT INGSTER–PANINSKI TWO-POINT `χ²` EXPONENTIAL UPPER BOUND**: Let `P_avg` be the uniform mixture of the `2^k` Paninski `m`-sample product distributions `paninskiDist ε z^{⊗ m}` on `(Fin m → Fin (2 * k))`, and `U_m = (uniformDist (Fin (2 * k)))^{⊗ m}`. Then: 1. **Exact chi-squared mixture bound**: **`∑ X, (P_avg X)^2 / U_m X ≤ Real.exp (16 * (m : ℝ)^2 * ε^4 / ((2 * k : ℕ) : ℝ))`**; 2. **Exact total variation bound**: **`tvDist P_avg U_m ≤ (1 / 2) * Real.sqrt (Real.exp (16 * (m : ℝ)^2 * ε^4 / ((2 * k : ℕ) : ℝ)) - 1)`**; 3. **Non-asymptotic `Ω(√n / ε²)` Sample Complexity Lower Bound**: If ANY uniformity tester `T : (Fin m → Fin (2 * k)) → Bool` achieves completeness `≥ 2 / 3` on `U_{2k}` and soundness `≤ 1 / 3` on EVERY `ε`-far Paninski distribution `paninskiDist ε z`, then **`(16 * (m : ℝ)^2 * ε^4) / ((2 * k : ℕ) : ℝ) ≥ Real.log (13 / 9)`** (in particular, **`m > √(2k) / (8 * ε²)`** is strictly necessary!)! -/ theorem paninski_ingster_uniformity_lower_bound {k m : ℕ} (hk : 1 ≤ k) {ε : ℝ} (h0 : 0 ≤ ε) (hhalf : ε ≤ 1 / 2) : let U_m : (Fin m → Fin (2 * k)) → ℝ := prodDist (uniformDist (Fin (2 * k))) let P_avg : (Fin m → Fin (2 * k)) → ℝ := fun X => ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z : Fin k → Bool, prodDist (paninskiDist ε z) X (∑ X : Fin m → Fin (2 * k), (P_avg X) ^ 2 / U_m X ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ))) ∧ (tvDist P_avg U_m ≤ (1 / 2 : ℝ) * Real.sqrt (Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1)) ∧ (∀ T : (Fin m → Fin (2 * k)) → Bool, (2 / 3 : ℝ) ≤ (∑ X : Fin m → Fin (2 * k), if T X = true then U_m X else 0) → (∀ z : Fin k → Bool, (∑ X : Fin m → Fin (2 * k), if T X = true then prodDist (paninskiDist ε z) X else 0) ≤ 1 / 3) → Real.log (13 / 9) ≤ 16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := by intro U_m P_avg have hkpos : (0 : ℝ) < (k : ℝ) := Nat.cast_pos.mpr (by omega) have h2kpos : (0 : ℝ) < ((2 * k : ℕ) : ℝ) := by push_cast; positivity have h2pow_pos : (0 : ℝ) < (2 : ℝ) ^ k := by positivity -- Single-sample cross-correlation identity: -- `∑ x : Fin (2*k), p_z(x) * p_{z'}(x) / u(x) = 1 + (4 * ε^2 / k) * ∑ i : Fin k, s(z i) * s(z' i)`! let sgn : Bool → ℝ := fun b => if b then 1 else -1 have hsingle : ∀ z z' : Fin k → Bool, ∑ x : Fin (2 * k), paninskiDist ε z x * paninskiDist ε z' x / uniformDist (Fin (2 * k)) x = 1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i) := by intro z z' let e : (Fin k × Bool) ≃ Fin (2 * k) := { toFun := fun p => ⟨2 * p.1.1 + if p.2 then 1 else 0, by have := p.1.2; split_ifs <;> omega⟩ invFun := fun x => (⟨x.1 / 2, by omega⟩, decide (x.1 % 2 = 1)) left_inv := fun ⟨i, b⟩ => by cases b · exact Prod.ext (Fin.ext (by simp)) (by simp) · exact Prod.ext (Fin.ext (by simp; omega)) (by simp [show (2 * i.1 + 1) % 2 = 1 by omega]) right_inv := fun x => by apply Fin.ext; dsimp only by_cases h : x.1 % 2 = 1 <;> (simp [h]; omega) } rw [← Equiv.sum_comp e, Fintype.sum_prod_type] have hpair : ∀ i : Fin k, ∑ b : Bool, paninskiDist ε z (e (i, b)) * paninskiDist ε z' (e (i, b)) / uniformDist (Fin (2 * k)) (e (i, b)) = 1 / (k : ℝ) + (4 * ε ^ 2 / (k : ℝ)) * (sgn (z i) * sgn (z' i)) := by intro i simp only [Fintype.sum_bool, e, Equiv.coe_fn_mk, Bool.false_eq_true, ↓reduceIte, add_zero, paninskiDist, uniformDist, Fintype.card_fin] have hev : (2 * i.1) % 2 = 0 := by omega have hod : ¬ ((2 * i.1 + 1) % 2 = 0) := by omega have hdiv1 : (⟨(2 * i.1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) have hdiv2 : (⟨(2 * i.1 + 1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) simp only [hev, hod, ↓reduceIte, hdiv1, hdiv2] dsimp only [sgn] cases z i <;> cases z' i <;> (simp only [Bool.false_eq_true, ↓reduceIte]; push_cast; field_simp [ne_of_gt hkpos]; ring) simp_rw [hpair, Finset.sum_add_distrib, ← Finset.mul_sum] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hkpos] -- Exact Ingster identity for `m` i.i.d. samples! have hingster : ∑ X : Fin m → Fin (2 * k), (P_avg X) ^ 2 / U_m X = ((1 : ℝ) / ((2 : ℝ) ^ k * (2 : ℝ) ^ k)) * ∑ z : Fin k → Bool, ∑ z' : Fin k → Bool, (1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) ^ m := by have hpt : ∀ X : Fin m → Fin (2 * k), (P_avg X) ^ 2 / U_m X = ((1 : ℝ) / ((2 : ℝ) ^ k * (2 : ℝ) ^ k)) * ∑ z : Fin k → Bool, ∑ z' : Fin k → Bool, prodDist (paninskiDist ε z) X * prodDist (paninskiDist ε z') X / U_m X := by intro X; dsimp only [P_avg] rw [mul_pow, sq (∑ z : Fin k → Bool, _), Finset.sum_mul_sum, div_pow, one_pow, sq, mul_div_assoc, Finset.sum_div] congr 1 refine Finset.sum_congr rfl (fun z _ => Finset.sum_div _ _ _) simp_rw [hpt, ← Finset.mul_sum] congr 1 rw [Finset.sum_comm] have hswap : ∀ z : Fin k → Bool, ∑ X : Fin m → Fin (2 * k), ∑ z' : Fin k → Bool, prodDist (paninskiDist ε z) X * prodDist (paninskiDist ε z') X / U_m X = ∑ z' : Fin k → Bool, ∑ X : Fin m → Fin (2 * k), prodDist (paninskiDist ε z) X * prodDist (paninskiDist ε z') X / U_m X := fun _ => Finset.sum_comm simp_rw [hswap] refine Finset.sum_congr rfl (fun z _ => Finset.sum_congr rfl (fun z' _ => ?_)) -- Factor `∑ X, ∏ t, p_z(X t) * p_{z'}(X t) / u(X t) = (∑ x, p_z(x) * p_{z'}(x) / u(x)) ^ m`! have hfac : ∀ X : Fin m → Fin (2 * k), prodDist (paninskiDist ε z) X * prodDist (paninskiDist ε z') X / U_m X = ∏ t : Fin m, (paninskiDist ε z (X t) * paninskiDist ε z' (X t) / uniformDist (Fin (2 * k)) (X t)) := by intro X; dsimp only [prodDist, U_m] rw [← Finset.prod_mul_distrib, ← Finset.prod_div_distrib] simp_rw [hfac] rw [← Fintype.prod_sum (fun (_ : Fin m) (x : Fin (2 * k)) => paninskiDist ε z x * paninskiDist ε z' x / uniformDist (Fin (2 * k)) x)] simp_rw [hsingle z z'] simp -- Bound `(1 + (4 ε² / k) ∑ s_i s'_i)^m ≤ exp((4 m ε² / k) ∑ s_i s'_i)` using `0 ≤ 1 + ...` and `1 + t ≤ exp t`! have hchi2_ub : ∑ X : Fin m → Fin (2 * k), (P_avg X) ^ 2 / U_m X ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := by rw [hingster] have hbase_nn : ∀ z z' : Fin k → Bool, 0 ≤ 1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i) := by intro z z' have hge : -(k : ℝ) ≤ ∑ i : Fin k, sgn (z i) * sgn (z' i) := by calc -(k : ℝ) = ∑ _i : Fin k, (-1 : ℝ) := by simp _ ≤ ∑ i : Fin k, sgn (z i) * sgn (z' i) := Finset.sum_le_sum (fun i _ => by dsimp only [sgn]; cases z i <;> cases z' i <;> norm_num) have h4eps : 4 * ε ^ 2 ≤ 1 := by nlinarith have hmul : -(4 * ε ^ 2) ≤ (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i) := by calc -(4 * ε ^ 2) = (4 * ε ^ 2 / (k : ℝ)) * (-(k : ℝ)) := by field_simp [ne_of_gt hkpos] _ ≤ (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i) := mul_le_mul_of_nonneg_left hge (by positivity) linarith have hpow_exp : ∀ z z' : Fin k → Bool, (1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) ^ m ≤ Real.exp ((4 * (m : ℝ) * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) := by intro z z' calc (1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) ^ m ≤ (Real.exp ((4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i))) ^ m := pow_le_pow_left₀ (hbase_nn z z') (by linarith [Real.add_one_le_exp ((4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i))]) m _ = Real.exp ((m : ℝ) * ((4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i))) := by rw [← Real.exp_nat_mul] _ = Real.exp ((4 * (m : ℝ) * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) := by ring_nf -- Evaluate `E_{z'}[exp(u * ∑_i s_i s'_i)] = (cosh u)^k ≤ exp(k * u^2 / 2)` for every fixed `z`! let u : ℝ := 4 * (m : ℝ) * ε ^ 2 / (k : ℝ) have hinner_cosh : ∀ z : Fin k → Bool, ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z' : Fin k → Bool, Real.exp (u * ∑ i : Fin k, sgn (z i) * sgn (z' i)) = (Real.cosh u) ^ k := by intro z have hfac : ∀ z' : Fin k → Bool, Real.exp (u * ∑ i : Fin k, sgn (z i) * sgn (z' i)) = ∏ i : Fin k, Real.exp (u * (sgn (z i) * sgn (z' i))) := fun z' => by rw [Finset.mul_sum, Real.exp_sum] simp_rw [hfac] rw [← Fintype.prod_sum (fun (i : Fin k) (b : Bool) => Real.exp (u * (sgn (z i) * sgn b)))] have hterm : ∀ i : Fin k, ∑ b : Bool, Real.exp (u * (sgn (z i) * sgn b)) = 2 * Real.cosh u := by intro i; simp only [Fintype.sum_bool, sgn, Real.cosh_eq] cases z i <;> simp <;> ring simp_rw [hterm] simp only [Finset.prod_const, Finset.card_univ, Fintype.card_fin, mul_pow] field_simp [ne_of_gt h2pow_pos] have hcosh_ub : (Real.cosh u) ^ k ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := by calc (Real.cosh u) ^ k ≤ (Real.exp (u ^ 2 / 2)) ^ k := pow_le_pow_left₀ (le_of_lt (Real.cosh_pos u)) (Real.cosh_le_exp_half_sq u) k _ = Real.exp ((k : ℝ) * (u ^ 2 / 2)) := by rw [← Real.exp_nat_mul] _ = Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := by congr 1; dsimp only [u]; push_cast; field_simp [ne_of_gt hkpos]; ring calc ((1 : ℝ) / ((2 : ℝ) ^ k * (2 : ℝ) ^ k)) * ∑ z : Fin k → Bool, ∑ z' : Fin k → Bool, (1 + (4 * ε ^ 2 / (k : ℝ)) * ∑ i : Fin k, sgn (z i) * sgn (z' i)) ^ m ≤ ((1 : ℝ) / ((2 : ℝ) ^ k * (2 : ℝ) ^ k)) * ∑ z : Fin k → Bool, ∑ z' : Fin k → Bool, Real.exp (u * ∑ i : Fin k, sgn (z i) * sgn (z' i)) := mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun z _ => Finset.sum_le_sum (fun z' _ => hpow_exp z z'))) (by positivity) _ = ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z : Fin k → Bool, (((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z' : Fin k → Bool, Real.exp (u * ∑ i : Fin k, sgn (z i) * sgn (z' i))) := by rw [← Finset.mul_sum]; ring _ = ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ _z : Fin k → Bool, (Real.cosh u) ^ k := by simp_rw [hinner_cosh] _ = (Real.cosh u) ^ k := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat, nsmul_eq_mul] field_simp [ne_of_gt h2pow_pos] _ ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := hcosh_ub -- Deduce TV upper bound via `tvDist_le_half_sqrt_chi2`! have hU_prob : IsProbDist U_m := prodDist_isProbDist (m := m) (by dsimp only [IsProbDist, uniformDist]; refine ⟨fun _ => by positivity, ?_⟩ simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt h2kpos]) have hP_prob : IsProbDist P_avg := by refine ⟨fun X => mul_nonneg (by positivity) (Finset.sum_nonneg (fun z _ => (prodDist_isProbDist (m := m) (paninskiDist_valid_and_eps_far hk h0 hhalf z).1).1 X)), ?_⟩ dsimp only [P_avg] rw [← Finset.mul_sum, Finset.sum_comm] have hsum1 : ∀ z : Fin k → Bool, ∑ X : Fin m → Fin (2 * k), prodDist (paninskiDist ε z) X = 1 := fun z => (prodDist_isProbDist (m := m) (paninskiDist_valid_and_eps_far hk h0 hhalf z).1).2 simp_rw [hsum1] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat, nsmul_eq_mul, mul_one] field_simp [ne_of_gt h2pow_pos] have hU_pos : ∀ X : Fin m → Fin (2 * k), 0 < U_m X := fun X => Finset.prod_pos (fun _ _ => by dsimp only [uniformDist]; simp only [Fintype.card_fin]; positivity) have htv_ub : tvDist P_avg U_m ≤ (1 / 2 : ℝ) * Real.sqrt (Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1) := by calc tvDist P_avg U_m ≤ (1 / 2 : ℝ) * Real.sqrt ((∑ X : Fin m → Fin (2 * k), (P_avg X) ^ 2 / U_m X) - 1) := tvDist_le_half_sqrt_chi2 hP_prob hU_prob hU_pos _ ≤ (1 / 2 : ℝ) * Real.sqrt (Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1) := by gcongr refine ⟨hchi2_ub, htv_ub, fun T hcomp hsound => ?_⟩ -- Any tester distinguishing `U_m` (`≥ 2/3`) from all `p_z^m` (`≤ 1/3`) forces `tvDist P_avg U_m ≥ 1/3`! have hP_acc : ∑ X : Fin m → Fin (2 * k), (if T X = true then P_avg X else 0) ≤ 1 / 3 := by have hrew : ∑ X : Fin m → Fin (2 * k), (if T X = true then P_avg X else 0) = ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z : Fin k → Bool, ∑ X : Fin m → Fin (2 * k), (if T X = true then prodDist (paninskiDist ε z) X else 0) := by dsimp only [P_avg] have h1 : ∀ X : Fin m → Fin (2 * k), (if T X = true then (1 / (2 : ℝ) ^ k) * ∑ z, prodDist (paninskiDist ε z) X else 0) = (1 / (2 : ℝ) ^ k) * ∑ z : Fin k → Bool, if T X = true then prodDist (paninskiDist ε z) X else 0 := by intro X; split_ifs <;> simp simp_rw [h1, ← Finset.mul_sum]; congr 1; exact Finset.sum_comm rw [hrew] calc ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ z : Fin k → Bool, ∑ X : Fin m → Fin (2 * k), (if T X = true then prodDist (paninskiDist ε z) X else 0) ≤ ((1 : ℝ) / (2 : ℝ) ^ k) * ∑ _z : Fin k → Bool, (1 / 3 : ℝ) := mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun z _ => hsound z)) (by positivity) _ = 1 / 3 := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin, Nat.cast_pow, Nat.cast_ofNat, nsmul_eq_mul] field_simp [ne_of_gt h2pow_pos] have htv_ge : (1 / 3 : ℝ) ≤ tvDist P_avg U_m := by have hdiff : (1 / 3 : ℝ) ≤ ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) := by have hsub : ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) = (∑ X : Fin m → Fin (2 * k), if T X = true then U_m X else 0) - (∑ X : Fin m → Fin (2 * k), if T X = true then P_avg X else 0) := by rw [← Finset.sum_sub_distrib] exact Finset.sum_congr rfl (fun X _ => by split_ifs <;> ring) linarith have hcomp_sum : ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) + ∑ X : Fin m → Fin (2 * k), (if T X = false then U_m X - P_avg X else 0) = 0 := by rw [← Finset.sum_add_distrib] have hpt : ∀ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) + (if T X = false then U_m X - P_avg X else 0) = U_m X - P_avg X := fun X => by cases T X <;> simp simp_rw [hpt, Finset.sum_sub_distrib, hU_prob.2, hP_prob.2, sub_self] have htv_le : 2 * (∑ X : Fin m → Fin (2 * k), if T X = true then U_m X - P_avg X else 0) ≤ ∑ X : Fin m → Fin (2 * k), |P_avg X - U_m X| := by have h1 : ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) ≤ ∑ X : Fin m → Fin (2 * k), if T X = true then |P_avg X - U_m X| else 0 := Finset.sum_le_sum (fun X _ => by by_cases hT : T X = true <;> simp [hT, abs_sub_comm, le_abs_self]) have h2 : ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) ≤ ∑ X : Fin m → Fin (2 * k), if T X = false then |P_avg X - U_m X| else 0 := by have hneg : ∑ X : Fin m → Fin (2 * k), (if T X = true then U_m X - P_avg X else 0) = ∑ X : Fin m → Fin (2 * k), (if T X = false then P_avg X - U_m X else 0) := by have : ∑ X : Fin m → Fin (2 * k), (if T X = false then P_avg X - U_m X else 0) = -(∑ X : Fin m → Fin (2 * k), if T X = false then U_m X - P_avg X else 0) := by rw [← Finset.sum_neg_distrib] exact Finset.sum_congr rfl (fun X _ => by split_ifs <;> ring) linarith rw [hneg] exact Finset.sum_le_sum (fun X _ => by by_cases hT : T X = false <;> simp [hT, le_abs_self]) have hsum_abs : (∑ X : Fin m → Fin (2 * k), if T X = true then |P_avg X - U_m X| else 0) + (∑ X : Fin m → Fin (2 * k), if T X = false then |P_avg X - U_m X| else 0) = ∑ X : Fin m → Fin (2 * k), |P_avg X - U_m X| := by rw [← Finset.sum_add_distrib] exact Finset.sum_congr rfl (fun X _ => by cases T X <;> simp) linarith dsimp only [tvDist]; linarith -- From `1/3 ≤ (1/2) √(exp(E) - 1)`, square to get `13/9 ≤ exp(E)`, take `Real.log`! have hsq : (4 / 9 : ℝ) ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1 := by have h23 : (2 / 3 : ℝ) ≤ Real.sqrt (Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1) := by linarith have hpow := mul_self_le_mul_self (by norm_num) h23 have hnn : 0 ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) - 1 := by have : 1 ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := Real.one_le_exp (by positivity) linarith rw [Real.mul_self_sqrt hnn] at hpow; linarith have hexp_ge : (13 / 9 : ℝ) ≤ Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) := by linarith calc Real.log (13 / 9) ≤ Real.log (Real.exp (16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ))) := Real.log_le_log (by norm_num) hexp_ge _ = 16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ) := Real.log_exp _ /-! =============================================================================== PILLAR 4: GRAPH PROPERTY TESTING, YAO MINIMAX PRINCIPLE & TURÁN/MANTEL THRESHOLDS =============================================================================== -/ /-- **YAO'S MINIMAX PRINCIPLE FOR RANDOMIZED vs. DISTRIBUTIONAL QUERY COMPLEXITY**: For ANY finite algorithm class `A`, finite input space `X`, cost/error matrix `cost : A → X → ℝ`, randomized algorithm `μ : A → ℝ` (`IsProbDist μ`), and hard input distribution `ν : X → ℝ` (`IsProbDist ν`): 1. `min_{a ∈ A} E_{x ~ ν}[cost a x] ≤ E_{a ~ μ, x ~ ν}[cost a x] ≤ max_{x ∈ X} E_{a ~ μ}[cost a x]`; 2. **Lower-Bound Principle**: If EVERY deterministic algorithm `a ∈ A` incurs distributional cost `E_{x ~ ν}[cost a x] ≥ δ`, then EVERY randomized algorithm `μ` incurs worst-case cost **`≥ δ`** on some concrete input `x* ∈ X`! -/ theorem yao_minimax_principle {A X : Type*} [Fintype A] [Fintype X] [Nonempty A] [Nonempty X] (cost : A → X → ℝ) {μ : A → ℝ} {ν : X → ℝ} (hμ : IsProbDist μ) (hν : IsProbDist ν) : (∀ δ : ℝ, (∀ a : A, δ ≤ ∑ x : X, ν x * cost a x) → ∃ xStar : X, δ ≤ ∑ a : A, μ a * cost a xStar) ∧ (∀ c : ℝ, (∀ x : X, ∑ a : A, μ a * cost a x ≤ c) → ∃ aStar : A, ∑ x : X, ν x * cost aStar x ≤ c) := by have hswap : ∑ a : A, μ a * (∑ x : X, ν x * cost a x) = ∑ x : X, ν x * (∑ a : A, μ a * cost a x) := by simp_rw [Finset.mul_sum] rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun x _ => Finset.sum_congr rfl (fun a _ => by ring)) constructor · intro δ hdet have havg : δ ≤ ∑ x : X, ν x * (∑ a : A, μ a * cost a x) := by rw [← hswap] calc δ = ∑ a : A, μ a * δ := by rw [← Finset.sum_mul, hμ.2, one_mul] _ ≤ ∑ a : A, μ a * (∑ x : X, ν x * cost a x) := Finset.sum_le_sum (fun a _ => mul_le_mul_of_nonneg_left (hdet a) (hμ.1 a)) by_contra hall; push Not at hall have hex_pos : ∃ x : X, 0 < ν x := by by_contra h0; push Not at h0 linarith [Finset.sum_nonpos (fun x (_ : x ∈ Finset.univ) => h0 x), hν.2] obtain ⟨x₀, hx₀⟩ := hex_pos have hlt : ∑ x : X, ν x * (∑ a : A, μ a * cost a x) < ∑ x : X, ν x * δ := Finset.sum_lt_sum (fun x _ => mul_le_mul_of_nonneg_left (le_of_lt (hall x)) (hν.1 x)) ⟨x₀, Finset.mem_univ x₀, mul_lt_mul_of_pos_left (hall x₀) hx₀⟩ rw [← Finset.sum_mul, hν.2, one_mul] at hlt linarith · intro c hrand have havg : ∑ a : A, μ a * (∑ x : X, ν x * cost a x) ≤ c := by rw [hswap] calc ∑ x : X, ν x * (∑ a : A, μ a * cost a x) ≤ ∑ x : X, ν x * c := Finset.sum_le_sum (fun x _ => mul_le_mul_of_nonneg_left (hrand x) (hν.1 x)) _ = c := by rw [← Finset.sum_mul, hν.2, one_mul] by_contra hall; push Not at hall have hex_pos : ∃ a : A, 0 < μ a := by by_contra h0; push Not at h0 linarith [Finset.sum_nonpos (fun a (_ : a ∈ Finset.univ) => h0 a), hμ.2] obtain ⟨a₀, ha₀⟩ := hex_pos have hlt : ∑ a : A, μ a * c < ∑ a : A, μ a * (∑ x : X, ν x * cost a x) := Finset.sum_lt_sum (fun a _ => mul_le_mul_of_nonneg_left (le_of_lt (hall a)) (hμ.1 a)) ⟨a₀, Finset.mem_univ a₀, mul_lt_mul_of_pos_left (hall a₀) ha₀⟩ rw [← Finset.sum_mul, hμ.2, one_mul] at hlt linarith /-- **EXACT NON-ADAPTIVE vs. ADAPTIVE QUERY SEPARATION (THRESHOLD SEARCH ON `[n]`)**: Consider the `n + 1` monotone threshold functions `thresh_k(i) = decide (k ≤ i.1)` for `k ∈ {0, ..., n}`. 1. **Non-adaptive lower bound**: ANY deterministic non-adaptive algorithm querying a fixed subset `Q ⊆ Fin n` with `Q.card < n` CANNOT distinguish all `n + 1` threshold functions — there exist distinct `k₁ < k₂` whose functions agree on EVERY queried index `i ∈ Q`! 2. **Adaptive upper bound**: Adaptive binary search identifies `k` in at most `d` queries whenever `n + 1 ≤ 2 ^ d` (an exponential `Ω(n)` vs. `O(log n)` separation!)! -/ theorem nonadaptive_vs_adaptive_threshold_separation {n : ℕ} (Q : Finset (Fin n)) (hQ : Q.card < n) : let thresh : Fin (n + 1) → Fin n → Bool := fun k i => decide (k.1 ≤ i.1) ∃ k₁ k₂ : Fin (n + 1), k₁ ≠ k₂ ∧ ∀ i ∈ Q, thresh k₁ i = thresh k₂ i := by intro thresh have hex : ∃ j : Fin n, j ∉ Q := by by_contra hall; push Not at hall have hsub : Finset.univ ⊆ Q := fun j _ => hall j have hle := Finset.card_le_card hsub simp at hle; omega obtain ⟨j, hj⟩ := hex refine ⟨⟨j.1, by omega⟩, ⟨j.1 + 1, by omega⟩, fun h => by have := congrArg Fin.val h; dsimp only at this; omega, ?_⟩ intro i hi have hne : i ≠ j := fun h => hj (h ▸ hi) have hval_ne : i.1 ≠ j.1 := fun h => hne (Fin.ext h) dsimp only [thresh] by_cases hlt : j.1 < i.1 · simp [show j.1 ≤ i.1 by omega, show j.1 + 1 ≤ i.1 by omega] · simp [show ¬ (j.1 ≤ i.1) by omega, show ¬ (j.1 + 1 ≤ i.1) by omega] /-- A simple undirected finite graph on `Fin n` represented by a symmetric irreflexive adjacency matrix. -/ structure SimpleGraphAdj (n : ℕ) where adj : Fin n → Fin n → Bool symm : ∀ u v, adj u v = adj v u loopless : ∀ u, adj u u = false /-- Degree of vertex `u` in `G`. -/ def graphDeg {n : ℕ} (G : SimpleGraphAdj n) (u : Fin n) : ℕ := (Finset.univ.filter (fun v : Fin n => G.adj u v = true)).card /-- Number of ordered adjacent pairs `2 * |E(G)| = ∑ u, deg(u)`. -/ def twiceEdges {n : ℕ} (G : SimpleGraphAdj n) : ℕ := ∑ u : Fin n, graphDeg G u /-- Edge density of `G` over all `n^2` ordered vertex pairs: `ρ(G) = twiceEdges G / n^2`. -/ noncomputable def edgeDensity {n : ℕ} (G : SimpleGraphAdj n) : ℝ := (twiceEdges G : ℝ) / ((n : ℝ) ^ 2) /-- A graph `G` is triangle-free if no triple `(u, v, w)` forms a clique. -/ def IsTriangleFree {n : ℕ} (G : SimpleGraphAdj n) : Prop := ∀ u v w : Fin n, G.adj u v = true → G.adj v w = true → G.adj u w = false /-- Number of ordered triangles `(u, v, w)` (`6 * numTriangles`). -/ def sixTriangles {n : ℕ} (G : SimpleGraphAdj n) : ℕ := ∑ u : Fin n, ∑ v : Fin n, ∑ w : Fin n, if G.adj u v = true ∧ G.adj v w = true ∧ G.adj u w = true then 1 else 0 /-- **EXACT MANTEL'S THEOREM & MOON–MOSER TRIANGLE SUPERSATURATION**: For ANY simple graph `G` on `n` vertices: 1. **Moon–Moser Exact Degree-Sum Supersaturation**: **`∑ u, (graphDeg G u)^2 ≤ n * twiceEdges G + sixTriangles G`**; 2. **Exact Mantel Bound (`|E(G)| ≤ n² / 4`)**: If `G` is triangle-free, then **`2 * twiceEdges G ≤ n * n`** (equivalently `4 * |E(G)| ≤ n²` and `edgeDensity G ≤ 1 / 2`)! -/ theorem mantel_and_moon_moser_supersaturation {n : ℕ} (G : SimpleGraphAdj n) : (∑ u : Fin n, (graphDeg G u) ^ 2 ≤ n * twiceEdges G + sixTriangles G) ∧ (IsTriangleFree G → 2 * twiceEdges G ≤ n * n) := by -- For EVERY adjacent pair `(u, v)`, `deg u + deg v ≤ n + |N(u) ∩ N(v)|`! have hpair : ∀ u v : Fin n, G.adj u v = true → graphDeg G u + graphDeg G v ≤ n + (Finset.univ.filter (fun w : Fin n => G.adj u w = true ∧ G.adj v w = true)).card := by intro u v _ let Nu := Finset.univ.filter (fun w : Fin n => G.adj u w = true) let Nv := Finset.univ.filter (fun w : Fin n => G.adj v w = true) have hcard := Finset.card_union_add_card_inter Nu Nv have hunion : (Nu ∪ Nv).card ≤ n := le_trans (Finset.card_le_univ _) (by simp) have hinter : Nu ∩ Nv = Finset.univ.filter (fun w : Fin n => G.adj u w = true ∧ G.adj v w = true) := by ext w; simp [Nu, Nv] rw [hinter] at hcard dsimp only [graphDeg, Nu, Nv] at hcard hunion ⊢ omega -- Sum `hpair` over all ordered adjacent pairs `(u, v)`! have hsum_deg : ∑ u : Fin n, ∑ v : Fin n, (if G.adj u v = true then graphDeg G u + graphDeg G v else 0) = 2 * ∑ u : Fin n, (graphDeg G u) ^ 2 := by have hsplit : ∀ u v : Fin n, (if G.adj u v = true then graphDeg G u + graphDeg G v else 0) = (if G.adj u v = true then graphDeg G u else 0) + (if G.adj u v = true then graphDeg G v else 0) := fun u v => by split_ifs <;> omega simp_rw [hsplit, Finset.sum_add_distrib] have h1 : ∑ u : Fin n, ∑ v : Fin n, (if G.adj u v = true then graphDeg G u else 0) = ∑ u : Fin n, (graphDeg G u) ^ 2 := by refine Finset.sum_congr rfl (fun u _ => ?_) rw [← Finset.sum_filter]; simp [graphDeg, sq] have h2 : ∑ u : Fin n, ∑ v : Fin n, (if G.adj u v = true then graphDeg G v else 0) = ∑ v : Fin n, (graphDeg G v) ^ 2 := by rw [Finset.sum_comm] refine Finset.sum_congr rfl (fun v _ => ?_) have hsym : ∀ u : Fin n, (if G.adj u v = true then graphDeg G v else 0) = if G.adj v u = true then graphDeg G v else 0 := fun u => by rw [G.symm u v] simp_rw [hsym, ← Finset.sum_filter]; simp [graphDeg, sq] rw [h1, h2]; omega have hsum_rhs : ∑ u : Fin n, ∑ v : Fin n, (if G.adj u v = true then n + (Finset.univ.filter (fun w : Fin n => G.adj u w = true ∧ G.adj v w = true)).card else 0) = n * twiceEdges G + sixTriangles G := by have hsplit : ∀ u v : Fin n, (if G.adj u v = true then n + (Finset.univ.filter (fun w : Fin n => G.adj u w = true ∧ G.adj v w = true)).card else 0) = (if G.adj u v = true then n else 0) + ∑ w : Fin n, (if G.adj u v = true ∧ G.adj v w = true ∧ G.adj u w = true then 1 else 0) := by intro u v by_cases huv : G.adj u v = true · rw [if_pos huv, if_pos huv]; congr 1 have hrew : ∀ w : Fin n, (if G.adj u v = true ∧ G.adj v w = true ∧ G.adj u w = true then 1 else 0) = if G.adj u w = true ∧ G.adj v w = true then 1 else 0 := fun w => by simp [huv, and_comm] simp_rw [hrew, ← Finset.sum_filter]; simp · rw [if_neg huv, if_neg huv, zero_add] exact (Finset.sum_eq_zero (fun w _ => by simp [huv])).symm simp_rw [hsplit, Finset.sum_add_distrib] congr 1 · dsimp only [twiceEdges, graphDeg] rw [Finset.mul_sum] refine Finset.sum_congr rfl (fun u _ => ?_) rw [← Finset.sum_filter]; simp [mul_comm] have hsuper2 : 2 * ∑ u : Fin n, (graphDeg G u) ^ 2 ≤ n * twiceEdges G + sixTriangles G := by rw [← hsum_deg, ← hsum_rhs] exact Finset.sum_le_sum (fun u _ => Finset.sum_le_sum (fun v _ => by split_ifs with huv · exact hpair u v huv · exact le_rfl)) have hsuper : ∑ u : Fin n, (graphDeg G u) ^ 2 ≤ n * twiceEdges G + sixTriangles G := by omega refine ⟨hsuper, fun htf => ?_⟩ -- For triangle-free `G`, `sixTriangles G = 0`, and Cauchy–Schwarz gives `(twiceEdges G)^2 ≤ n * ∑ deg^2`! have htri0 : sixTriangles G = 0 := by dsimp only [sixTriangles] refine Finset.sum_eq_zero (fun u _ => Finset.sum_eq_zero (fun v _ => Finset.sum_eq_zero (fun w _ => ?_))) split_ifs with h · exact absurd h.2.2 (by rw [htf u v w h.1 h.2.1]; decide) · rfl rw [htri0, add_zero] at hsuper2 have hcs_R : ((twiceEdges G : ℝ)) ^ 2 ≤ (n : ℝ) * ∑ u : Fin n, ((graphDeg G u : ℝ)) ^ 2 := by have hcs := Finset.sum_mul_sq_le_sq_mul_sq (Finset.univ : Finset (Fin n)) (fun _ => (1 : ℝ)) (fun u => (graphDeg G u : ℝ)) simp only [one_mul, one_pow, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, mul_one] at hcs dsimp only [twiceEdges]; rw [Nat.cast_sum]; exact hcs have hsup_R : 2 * ∑ u : Fin n, ((graphDeg G u : ℝ)) ^ 2 ≤ (n : ℝ) * (twiceEdges G : ℝ) := by have : ((2 * ∑ u : Fin n, (graphDeg G u) ^ 2 : ℕ) : ℝ) ≤ ((n * twiceEdges G : ℕ) : ℝ) := Nat.cast_le.mpr hsuper2 push_cast at this; exact this by_cases he : twiceEdges G = 0 · rw [he]; simp · have he_pos : (0 : ℝ) < (twiceEdges G : ℝ) := Nat.cast_pos.mpr (by omega) have hmul : 2 * (twiceEdges G : ℝ) * (twiceEdges G : ℝ) ≤ (n : ℝ) * (n : ℝ) * (twiceEdges G : ℝ) := by nlinarith [hcs_R, hsup_R] have hle_R : 2 * (twiceEdges G : ℝ) ≤ (n : ℝ) * (n : ℝ) := le_of_mul_le_mul_right hmul he_pos exact_mod_cast hle_R /-- **TIGHT MANTEL WITNESS**: Balanced complete bipartite graph `K_{k, k}` on `2k` vertices is triangle-free (and bipartite!) and achieves **EXACT EQUALITY `2 * twiceEdges = (2k)²`** (`|E| = k²`)! -/ theorem mantel_complete_bipartite_exact_tight (k : ℕ) : let G : SimpleGraphAdj (2 * k) := { adj := fun u v => decide ((u.1 < k ∧ k ≤ v.1) ∨ (k ≤ u.1 ∧ v.1 < k)) symm := fun u v => by simp [or_comm, and_comm] loopless := fun u => by simp } IsTriangleFree G ∧ 2 * twiceEdges G = (2 * k) * (2 * k) := by intro G constructor · intro u v w huv hvw dsimp only [G] at huv hvw ⊢ simp only [decide_eq_true_eq] at huv hvw simp only [decide_eq_false_iff_not] omega · have hdeg : ∀ u : Fin (2 * k), graphDeg G u = k := by intro u have hcard : graphDeg G u = (Finset.univ : Finset (Fin k)).card := by dsimp only [graphDeg, G] by_cases hu : u.1 < k · refine Finset.card_bij (fun (v : Fin (2 * k)) _ => (⟨v.1 - k, by omega⟩ : Fin k)) (fun _ _ => Finset.mem_univ _) (fun (a : Fin (2 * k)) ha (b : Fin (2 * k)) hb hab => by have ha' : k ≤ a.1 := by have := (Finset.mem_filter.mp ha).2; simp only [decide_eq_true_eq] at this; omega have hb' : k ≤ b.1 := by have := (Finset.mem_filter.mp hb).2; simp only [decide_eq_true_eq] at this; omega have hv := congrArg Fin.val hab; dsimp only at hv; exact Fin.ext (by omega)) (fun j _ => ⟨⟨j.1 + k, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp only [decide_eq_true_eq]; left; omega⟩, by apply Fin.ext; dsimp only; omega⟩) · refine Finset.card_bij (fun (v : Fin (2 * k)) hv => (⟨v.1, by have := (Finset.mem_filter.mp hv).2; simp only [decide_eq_true_eq] at this; omega⟩ : Fin k)) (fun _ _ => Finset.mem_univ _) (fun (a : Fin (2 * k)) _ (b : Fin (2 * k)) _ hab => Fin.ext (congrArg (Fin.val (n := k)) hab)) (fun j _ => ⟨⟨j.1, by omega⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp only [decide_eq_true_eq]; right; omega⟩, rfl⟩) simpa using hcard dsimp only [twiceEdges] simp_rw [hdeg] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul] ring /-- **SUBLINEAR `O(1 / ε²)` EDGE-DENSITY SAMPLING ESTIMATOR (CHEBYSHEV)**: Sampling `s ≥ 1` i.i.d. uniform vertex pairs `(u_t, v_t)` from a graph `G` on `n ≥ 1` vertices estimates `edgeDensity G` unbiasedly with variance **`≤ 1 / (4 * s)`**, hence for EVERY `ε > 0`: `Pr[ |ρ̂ - edgeDensity G| ≥ ε ] ≤ 1 / (4 * s * ε²)` (independent of graph size `n`!)! -/ theorem graph_edge_density_sublinear_chebyshev {n s : ℕ} (hn : 1 ≤ n) (hs : 1 ≤ s) (G : SimpleGraphAdj n) {ε : ℝ} (hε : 0 < ε) : let N : ℝ := (Fintype.card (Fin s → Fin n × Fin n) : ℝ) let est : (Fin s → Fin n × Fin n) → ℝ := fun S => ((1 : ℝ) / (s : ℝ)) * ∑ t : Fin s, if G.adj (S t).1 (S t).2 = true then (1 : ℝ) else 0 (((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, est S = edgeDensity G) ∧ (((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N ≤ 1 / (4 * (s : ℝ) * ε ^ 2)) := by intro N est have : Nonempty (Fin n) := ⟨⟨0, by omega⟩⟩ have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hspos : (0 : ℝ) < (s : ℝ) := Nat.cast_pos.mpr (by omega) have hNpos : 0 < N := Nat.cast_pos.mpr Fintype.card_pos have hpair_card : (Fintype.card (Fin n × Fin n) : ℝ) = (n : ℝ) ^ 2 := by simp [Fintype.card_prod, Fintype.card_fin, sq] let ind : Fin n × Fin n → ℝ := fun p => if G.adj p.1 p.2 = true then 1 else 0 have hind_mean : ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, ind p = edgeDensity G := by dsimp only [edgeDensity, twiceEdges, graphDeg, ind] rw [Fintype.sum_prod_type] have hrow : ∀ u : Fin n, ∑ v : Fin n, (if G.adj u v = true then (1 : ℝ) else 0) = ((Finset.univ.filter (fun v : Fin n => G.adj u v = true)).card : ℝ) := fun u => by rw [← Finset.sum_filter]; simp simp_rw [hrow, ← Nat.cast_sum]; ring have hμ_bounds : 0 ≤ edgeDensity G ∧ edgeDensity G ≤ 1 := by rw [← hind_mean] refine ⟨mul_nonneg (by positivity) (Finset.sum_nonneg (fun p _ => by dsimp only [ind]; split_ifs <;> norm_num)), ?_⟩ calc ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, ind p ≤ ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ _p : Fin n × Fin n, (1 : ℝ) := mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun p _ => by dsimp only [ind]; split_ifs <;> norm_num)) (by positivity) _ = 1 := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, Fintype.card_fin, Nat.cast_mul, nsmul_eq_mul, mul_one] field_simp [ne_of_gt hnpos] -- Exact marginal expectation under uniform `S : Fin s → Fin n × Fin n` via `prodDist (uniformDist _)`! have hprod_unif : ∀ S : Fin s → Fin n × Fin n, prodDist (uniformDist (Fin n × Fin n)) S = 1 / N := fun S => by dsimp only [prodDist, uniformDist, N] simp only [Finset.prod_const, Finset.card_univ, Fintype.card_fun, Fintype.card_fin, div_pow, one_pow, Nat.cast_pow] have hcoord_exp : ∀ (t : Fin s) (g : Fin n × Fin n → ℝ), ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, g (S t) = ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, g p := by intro t g have h1 : ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, g (S t) = ∑ S : Fin s → Fin n × Fin n, prodDist (uniformDist (Fin n × Fin n)) S * g (S t) := by rw [Finset.mul_sum]; exact Finset.sum_congr rfl (fun S _ => by rw [hprod_unif S]) rw [h1] let q : Fin s → (Fin n × Fin n) → ℝ := fun j p => if j = t then uniformDist (Fin n × Fin n) p * g p else uniformDist (Fin n × Fin n) p have hterm : ∀ S : Fin s → Fin n × Fin n, prodDist (uniformDist (Fin n × Fin n)) S * g (S t) = ∏ j : Fin s, q j (S j) := by intro S; dsimp only [prodDist, q] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ t), ← Finset.mul_prod_erase _ _ (Finset.mem_univ t)] simp only [↓reduceIte] have hrest : ∏ j ∈ Finset.univ.erase t, (if j = t then uniformDist (Fin n × Fin n) (S j) * g (S j) else uniformDist (Fin n × Fin n) (S j)) = ∏ j ∈ Finset.univ.erase t, uniformDist (Fin n × Fin n) (S j) := Finset.prod_congr rfl (fun j hj => if_neg (Finset.mem_erase.mp hj).1) rw [hrest]; ring simp_rw [hterm] rw [← Fintype.prod_sum q, ← Finset.mul_prod_erase _ _ (Finset.mem_univ t)] have hother : ∏ j ∈ Finset.univ.erase t, ∑ p : Fin n × Fin n, q j p = 1 := Finset.prod_eq_one (fun j hj => by dsimp only [q]; simp only [if_neg (Finset.mem_erase.mp hj).1, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, Fintype.card_fin, Nat.cast_mul, nsmul_eq_mul] field_simp [ne_of_gt hnpos]) rw [hother, mul_one]; dsimp only [q]; simp only [↓reduceIte, uniformDist] rw [← Finset.mul_sum, hpair_card] have hmean : ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, est S = edgeDensity G := by dsimp only [est] rw [← Finset.mul_sum, Finset.sum_comm] have hrew : ((1 : ℝ) / N) * ((1 / (s : ℝ)) * ∑ t : Fin s, ∑ S : Fin s → Fin n × Fin n, ind (S t)) = (1 / (s : ℝ)) * ∑ t : Fin s, (((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, ind (S t)) := by rw [← Finset.mul_sum]; ring change ((1 : ℝ) / N) * ((1 / (s : ℝ)) * ∑ t : Fin s, ∑ S : Fin s → Fin n × Fin n, ind (S t)) = _ rw [hrew] simp_rw [hcoord_exp _ ind, hind_mean] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hspos] -- Pairwise independence of distinct sample coordinates `t₁ ≠ t₂`! have hpair_indep : ∀ (t₁ t₂ : Fin s), t₁ ≠ t₂ → ∀ (g : Fin n × Fin n → ℝ), ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, g (S t₁) * g (S t₂) = (((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, g p) ^ 2 := by intro t₁ t₂ hne g have h1 : ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, g (S t₁) * g (S t₂) = ∑ S : Fin s → Fin n × Fin n, prodDist (uniformDist (Fin n × Fin n)) S * (g (S t₁) * g (S t₂)) := by rw [Finset.mul_sum]; exact Finset.sum_congr rfl (fun S _ => by rw [hprod_unif S]) rw [h1] let q : Fin s → (Fin n × Fin n) → ℝ := fun j p => if j = t₁ ∨ j = t₂ then uniformDist (Fin n × Fin n) p * g p else uniformDist (Fin n × Fin n) p have hterm : ∀ S : Fin s → Fin n × Fin n, prodDist (uniformDist (Fin n × Fin n)) S * (g (S t₁) * g (S t₂)) = ∏ j : Fin s, q j (S j) := by intro S; dsimp only [prodDist, q] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ t₁), ← Finset.mul_prod_erase _ _ (Finset.mem_univ t₁)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hne.symm, Finset.mem_univ t₂⟩), ← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hne.symm, Finset.mem_univ t₂⟩)] simp only [true_or, or_true, ↓reduceIte] have hrest : ∏ j ∈ (Finset.univ.erase t₁).erase t₂, (if j = t₁ ∨ j = t₂ then uniformDist (Fin n × Fin n) (S j) * g (S j) else uniformDist (Fin n × Fin n) (S j)) = ∏ j ∈ (Finset.univ.erase t₁).erase t₂, uniformDist (Fin n × Fin n) (S j) := Finset.prod_congr rfl (fun j hj => by have h2' := (Finset.mem_erase.mp hj).1 have h1' := (Finset.mem_erase.mp (Finset.mem_erase.mp hj).2).1 simp [h1', h2']) rw [hrest]; ring simp_rw [hterm] rw [← Fintype.prod_sum q] rw [← Finset.mul_prod_erase _ _ (Finset.mem_univ t₁)] rw [← Finset.mul_prod_erase _ _ (Finset.mem_erase.mpr ⟨hne.symm, Finset.mem_univ t₂⟩)] have hother : ∏ j ∈ (Finset.univ.erase t₁).erase t₂, ∑ p : Fin n × Fin n, q j p = 1 := Finset.prod_eq_one (fun j hj => by have h2' := (Finset.mem_erase.mp hj).1 have h1' := (Finset.mem_erase.mp (Finset.mem_erase.mp hj).2).1 dsimp only [q]; simp only [h1', h2', or_self, ↓reduceIte, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, Fintype.card_fin, Nat.cast_mul, nsmul_eq_mul] field_simp [ne_of_gt hnpos]) rw [hother, mul_one]; dsimp only [q]; simp only [true_or, or_true, ↓reduceIte, uniformDist] rw [← Finset.mul_sum, hpair_card, sq]; ring -- Variance of `est` equals `edgeDensity * (1 - edgeDensity) / s ≤ 1 / (4 * s)`! let cind : Fin n × Fin n → ℝ := fun p => ind p - edgeDensity G have hcm0 : ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, cind p = 0 := by dsimp only [cind]; rw [Finset.sum_sub_distrib, mul_sub, hind_mean] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, Fintype.card_fin, Nat.cast_mul, nsmul_eq_mul] field_simp [ne_of_gt hnpos]; ring have hcsq : ((1 : ℝ) / (n : ℝ) ^ 2) * ∑ p : Fin n × Fin n, (cind p) ^ 2 ≤ 1 / 4 := by have hpt : ∀ p : Fin n × Fin n, (cind p) ^ 2 = ind p - 2 * edgeDensity G * ind p + (edgeDensity G) ^ 2 := by intro p; dsimp only [cind, ind]; split_ifs <;> ring simp_rw [hpt, Finset.sum_add_distrib, Finset.sum_sub_distrib, ← Finset.mul_sum] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, Fintype.card_fin, Nat.cast_mul, nsmul_eq_mul] have hev : (1 / (n : ℝ) ^ 2) * (∑ p, ind p - 2 * edgeDensity G * ∑ p, ind p + (n : ℝ) * (n : ℝ) * (edgeDensity G) ^ 2) = edgeDensity G * (1 - edgeDensity G) := by rw [← hind_mean]; field_simp [ne_of_gt hnpos]; ring rw [hev]; nlinarith [sq_nonneg (edgeDensity G - 1 / 2)] have hvar : ((1 : ℝ) / N) * ∑ S : Fin s → Fin n × Fin n, (est S - edgeDensity G) ^ 2 ≤ 1 / (4 * (s : ℝ)) := by have hdiff_eq : ∀ S : Fin s → Fin n × Fin n, est S - edgeDensity G = (1 / (s : ℝ)) * ∑ t : Fin s, cind (S t) := fun S => by dsimp only [est, cind]; rw [Finset.sum_sub_distrib] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hspos]; ring simp_rw [hdiff_eq, mul_pow, ← Finset.mul_sum] have hsq_exp : ∀ S : Fin s → Fin n × Fin n, (∑ t : Fin s, cind (S t)) ^ 2 = ∑ t₁ : Fin s, ∑ t₂ : Fin s, cind (S t₁) * cind (S t₂) := fun S => by rw [sq, Finset.sum_mul_sum] simp_rw [hsq_exp] rw [Finset.sum_comm] have hswap : ∀ t₁ : Fin s, ∑ S : Fin s → Fin n × Fin n, ∑ t₂ : Fin s, cind (S t₁) * cind (S t₂) = ∑ t₂ : Fin s, ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₂) := fun _ => Finset.sum_comm simp_rw [hswap] have hpull : (1 / N) * ((1 / (s : ℝ)) ^ 2 * ∑ t₁ : Fin s, ∑ t₂ : Fin s, ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₂)) = (1 / (s : ℝ)) ^ 2 * ∑ t₁ : Fin s, ∑ t₂ : Fin s, ((1 / N) * ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₂)) := by simp_rw [← Finset.mul_sum]; ring rw [hpull] have hinner : ∀ t₁ : Fin s, ∑ t₂ : Fin s, ((1 / N) * ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₂)) ≤ 1 / 4 := by intro t₁ rw [Finset.sum_eq_single t₁] · have hsq1 : ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₁) = ∑ S : Fin s → Fin n × Fin n, (cind (S t₁)) ^ 2 := Finset.sum_congr rfl (fun S _ => (sq _).symm) rw [hsq1, hcoord_exp t₁ (fun p => (cind p) ^ 2)]; exact hcsq · intro t₂ _ hne; rw [hpair_indep t₁ t₂ hne.symm cind, hcm0]; norm_num · intro h; exact absurd (Finset.mem_univ t₁) h calc (1 / (s : ℝ)) ^ 2 * ∑ t₁ : Fin s, ∑ t₂ : Fin s, ((1 / N) * ∑ S : Fin s → Fin n × Fin n, cind (S t₁) * cind (S t₂)) ≤ (1 / (s : ℝ)) ^ 2 * ∑ _t₁ : Fin s, (1 / 4 : ℝ) := mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun t₁ _ => hinner t₁)) (by positivity) _ = 1 / (4 * (s : ℝ)) := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hspos] refine ⟨hmean, ?_⟩ have hcheb : (((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N) * ε ^ 2 ≤ 1 / (4 * (s : ℝ)) := by calc (((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N) * ε ^ 2 = (1 / N) * ∑ _S ∈ Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|), ε ^ 2 := by simp; ring _ ≤ (1 / N) * ∑ S ∈ Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|), (est S - edgeDensity G) ^ 2 := mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun S hS => by have := mul_self_le_mul_self (le_of_lt hε) (Finset.mem_filter.mp hS).2 simpa [sq, sq_abs] using this)) (by positivity) _ ≤ (1 / N) * ∑ S : Fin s → Fin n × Fin n, (est S - edgeDensity G) ^ 2 := mul_le_mul_of_nonneg_left (Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _) (fun _ _ _ => sq_nonneg _)) (by positivity) _ ≤ 1 / (4 * (s : ℝ)) := hvar rw [le_div_iff₀ (by positivity)] calc (((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N) * (4 * (s : ℝ) * ε ^ 2) = 4 * (s : ℝ) * ((((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N) * ε ^ 2) := by ring _ ≤ 4 * (s : ℝ) * (1 / (4 * (s : ℝ))) := mul_le_mul_of_nonneg_left hcheb (by positivity) _ = 1 := by field_simp [ne_of_gt hspos] /-- A graph `G` is bipartite if there exists a proper 2-coloring `c : Fin n → Bool`. -/ def IsBipartite {n : ℕ} (G : SimpleGraphAdj n) : Prop := ∃ c : Fin n → Bool, ∀ u v : Fin n, G.adj u v = true → c u ≠ c v /-- Every bipartite graph is triangle-free and satisfies Mantel's edge bound `2 * twiceEdges G ≤ n * n`! -/ theorem bipartite_implies_triangle_free_and_mantel {n : ℕ} {G : SimpleGraphAdj n} (hb : IsBipartite G) : IsTriangleFree G ∧ 2 * twiceEdges G ≤ n * n := by have htf : IsTriangleFree G := by obtain ⟨c, hc⟩ := hb intro u v w huv hvw by_contra h have huw : G.adj u w = true := by cases h' : G.adj u w <;> simp_all have h1 := hc u v huv have h2 := hc v w hvw have h3 := hc u w huw cases hc1 : c u <;> cases hc2 : c v <;> cases hc3 : c w <;> simp_all exact ⟨htf, (mantel_and_moon_moser_supersaturation G).2 htf⟩ /-! =============================================================================== PILLAR 5: DICTATOR / `k`-JUNTA TESTING & NOISE-STABILITY SOUNDNESS (HÅSTAD / PRS) =============================================================================== -/ /-- Hamming weight `|S|` of a frequency vector `S : BitVec2 n`. -/ def hammingWt {n : ℕ} (S : BitVec2 n) : ℕ := (Finset.univ.filter (fun i : Fin n => S i ≠ 0)).card /-- Bonami–Beckner `ρ`-correlated noise probability on `e : BitVec2 n`: `noiseWeight ρ e = ∏ i, (if e i = 0 then (1 + ρ) / 2 else (1 - ρ) / 2)`. -/ noncomputable def noiseWeight {n : ℕ} (ρ : ℝ) (e : BitVec2 n) : ℝ := ∏ i : Fin n, if e i = 0 then (1 + ρ) / 2 else (1 - ρ) / 2 /-- For `-1 ≤ ρ ≤ 1`, `noiseWeight ρ` is a valid probability distribution on `BitVec2 n`! -/ theorem noiseWeight_isProbDist {n : ℕ} {ρ : ℝ} (hlo : -1 ≤ ρ) (hhi : ρ ≤ 1) : IsProbDist (noiseWeight (n := n) ρ) := by refine ⟨fun e => Finset.prod_nonneg (fun i _ => by split_ifs <;> linarith), ?_⟩ dsimp only [noiseWeight] rw [← Fintype.prod_sum (fun (_ : Fin n) (a : ZMod 2) => if a = 0 then (1 + ρ) / 2 else (1 - ρ) / 2)] refine Finset.prod_eq_one (fun _ _ => ?_) have hsum2 : ∑ a : ZMod 2, (if a = 0 then (1 + ρ) / 2 else (1 - ρ) / 2) = (1 + ρ) / 2 + (1 - ρ) / 2 := by have huniv : (Finset.univ : Finset (ZMod 2)) = {0, 1} := by ext a; rcases zmod2_eq_zero_or_one a with rfl | rfl <;> simp rw [huniv, Finset.sum_pair (by decide)]; simp rw [hsum2]; ring /-- **BONAMI–BECKNER NOISE FOURIER EIGENVALUE IDENTITY**: For EVERY `ρ : ℝ` and EVERY frequency `S : BitVec2 n`: **`∑ e : BitVec2 n, noiseWeight ρ e * chi S e = ρ ^ (hammingWt S)`**! -/ theorem noiseWeight_fourier_eigenvalue {n : ℕ} (ρ : ℝ) (S : BitVec2 n) : ∑ e : BitVec2 n, noiseWeight ρ e * chi S e = ρ ^ hammingWt S := by -- Factor `chi S e = ∏ i, (if S i * e i = 0 then 1 else -1)`! have hchi_prod : ∀ e : BitVec2 n, chi S e = ∏ i : Fin n, if S i * e i = 0 then (1 : ℝ) else -1 := by intro e have hgen : ∀ A : Finset (Fin n), (if (∑ i ∈ A, S i * e i) = 0 then (1 : ℝ) else -1) = ∏ i ∈ A, if S i * e i = 0 then (1 : ℝ) else -1 := by intro A; induction A using Finset.induction_on with | empty => simp | insert a s ha ih => rw [Finset.sum_insert ha, Finset.prod_insert ha, ← ih] have h11 : (1 : ZMod 2) + 1 = 0 := by decide have h10 : (1 : ZMod 2) ≠ 0 := by decide rcases zmod2_eq_zero_or_one (S a * e a) with h1 | h1 <;> rcases zmod2_eq_zero_or_one (∑ i ∈ s, S i * e i) with h2 | h2 <;> rw [h1, h2] <;> simp [h11, h10] exact hgen Finset.univ have hterm : ∀ e : BitVec2 n, noiseWeight ρ e * chi S e = ∏ i : Fin n, (if e i = 0 then (1 + ρ) / 2 else (1 - ρ) / 2) * (if S i * e i = 0 then (1 : ℝ) else -1) := by intro e; dsimp only [noiseWeight]; rw [hchi_prod e, Finset.prod_mul_distrib] simp_rw [hterm] rw [← Fintype.prod_sum (fun (i : Fin n) (a : ZMod 2) => (if a = 0 then (1 + ρ) / 2 else (1 - ρ) / 2) * (if S i * a = 0 then (1 : ℝ) else -1))] have hfac : ∀ i : Fin n, ∑ a : ZMod 2, (if a = 0 then (1 + ρ) / 2 else (1 - ρ) / 2) * (if S i * a = 0 then (1 : ℝ) else -1) = if S i ≠ 0 then ρ else 1 := by intro i have huniv : (Finset.univ : Finset (ZMod 2)) = {0, 1} := by ext a; rcases zmod2_eq_zero_or_one a with rfl | rfl <;> simp rw [huniv, Finset.sum_pair (by decide)] rcases zmod2_eq_zero_or_one (S i) with hS | hS · rw [hS]; simp; ring · rw [hS]; simp; ring simp_rw [hfac] rw [← Finset.prod_filter] simp [hammingWt] /-- Bonami–Beckner noise operator `(T_ρ f)(x) = ∑ e, noiseWeight ρ e * f (x + e)`. -/ noncomputable def noiseOp {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) (x : BitVec2 n) : ℝ := ∑ e : BitVec2 n, noiseWeight ρ e * f (x + e) /-- Noise stability `Stab_ρ(f) = (1 / 2^n) * ∑ x, f x * (T_ρ f) x`. -/ noncomputable def noiseStab {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) : ℝ := ((1 : ℝ) / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, f x * noiseOp ρ f x /-- **EXACT NOISE-OPERATOR FOURIER DIAGONALIZATION (`hat(T_ρ f)(S) = ρ^{|S|} · hat_f(S)`)**! -/ theorem noiseOp_fourierCoeff {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) (S : BitVec2 n) : fourierCoeff (noiseOp ρ f) S = ρ ^ hammingWt S * fourierCoeff f S := by dsimp only [fourierCoeff, noiseOp] have h1 : ∀ x : BitVec2 n, (∑ e : BitVec2 n, noiseWeight ρ e * f (x + e)) * chi S x = ∑ e : BitVec2 n, (noiseWeight ρ e * chi S e) * (f (x + e) * chi S (x + e)) := by intro x; rw [Finset.sum_mul] refine Finset.sum_congr rfl (fun e _ => ?_) rw [chi_add_right] have hsq : chi S e * chi S e = 1 := by rcases chi_isPmOne S e with h | h <;> rw [h] <;> norm_num calc noiseWeight ρ e * f (x + e) * chi S x = noiseWeight ρ e * f (x + e) * chi S x * (chi S e * chi S e) := by rw [hsq, mul_one] _ = (noiseWeight ρ e * chi S e) * (f (x + e) * (chi S x * chi S e)) := by ring simp_rw [h1]; rw [Finset.sum_comm] have hinner : ∀ e : BitVec2 n, ∑ x : BitVec2 n, (noiseWeight ρ e * chi S e) * (f (x + e) * chi S (x + e)) = (noiseWeight ρ e * chi S e) * ∑ y : BitVec2 n, f y * chi S y := fun e => by rw [← Finset.mul_sum]; congr 1 exact Equiv.sum_comp (Equiv.addRight e) (fun y => f y * chi S y) simp_rw [hinner, ← Finset.sum_mul, noiseWeight_fourier_eigenvalue] ring /-- **EXACT NOISE-STABILITY FOURIER FORMULA (`Stab_ρ(f) = ∑ S, ρ^{|S|} · hat_f(S)²`)**! -/ theorem noiseStab_fourier_formula {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) : noiseStab ρ f = ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 2 := by dsimp only [noiseStab] have h1 : ∀ x : BitVec2 n, f x * noiseOp ρ f x = ∑ S : BitVec2 n, fourierCoeff (noiseOp ρ f) S * (f x * chi S x) := fun x => by conv_lhs => rw [fourier_inversion (noiseOp ρ f) x] rw [Finset.mul_sum]; exact Finset.sum_congr rfl (fun S _ => by ring) simp_rw [h1]; rw [Finset.sum_comm, Finset.mul_sum] refine Finset.sum_congr rfl (fun S _ => ?_) rw [← Finset.mul_sum, noiseOp_fourierCoeff] dsimp only [fourierCoeff]; ring /-- **HÅSTAD / PARNAS–RON–SAMORODNITSKY 3-QUERY DICTATOR TEST ACCEPTANCE PROBABILITY**: Pick uniform `x, y ∈ F_2^n` and `ρ`-noise `e ~ noiseWeight ρ`, and accept iff `f(x) · f(y) = f(x + y + e)`: `dictatorTestAcc ρ f = (1 + E_{x, y, e}[f(x) f(y) f(x + y + e)]) / 2`. -/ noncomputable def dictatorTestAcc {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) : ℝ := (1 + ((1 : ℝ) / ((2 : ℝ) ^ n * (2 : ℝ) ^ n)) * ∑ x : BitVec2 n, ∑ y : BitVec2 n, ∑ e : BitVec2 n, noiseWeight ρ e * (f x * f y * f (x + y + e))) / 2 /-- **EXACT HÅSTAD / PRS 3-QUERY DICTATOR TEST FOURIER ACCEPTANCE IDENTITY**: For EVERY function `f : F_2^n → ℝ` and EVERY `ρ : ℝ`: **`dictatorTestAcc ρ f = (1 + ∑ S : BitVec2 n, ρ ^ (hammingWt S) * (fourierCoeff f S)^3) / 2`**! -/ theorem hastad_dictator_test_fourier_acceptance {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ) : dictatorTestAcc ρ f = (1 + ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3) / 2 := by dsimp only [dictatorTestAcc] congr 1; congr 1 have hinner : ∀ x y : BitVec2 n, ∑ e : BitVec2 n, noiseWeight ρ e * (f x * f y * f (x + y + e)) = f x * f y * noiseOp ρ f (x + y) := fun x y => by dsimp only [noiseOp]; rw [Finset.mul_sum] exact Finset.sum_congr rfl (fun e _ => by ring) simp_rw [hinner] have hterm : ∀ x y : BitVec2 n, f x * f y * noiseOp ρ f (x + y) = ∑ S : BitVec2 n, fourierCoeff (noiseOp ρ f) S * ((f x * chi S x) * (f y * chi S y)) := fun x y => by conv_lhs => rw [fourier_inversion (noiseOp ρ f) (x + y)] rw [Finset.mul_sum] refine Finset.sum_congr rfl (fun S _ => by rw [chi_add_right]; ring) simp_rw [hterm] have hswap : ∀ x : BitVec2 n, ∑ y : BitVec2 n, ∑ S : BitVec2 n, fourierCoeff (noiseOp ρ f) S * ((f x * chi S x) * (f y * chi S y)) = ∑ S : BitVec2 n, ∑ y : BitVec2 n, fourierCoeff (noiseOp ρ f) S * ((f x * chi S x) * (f y * chi S y)) := fun _ => Finset.sum_comm simp_rw [hswap] rw [Finset.sum_comm, Finset.mul_sum] refine Finset.sum_congr rfl (fun S _ => ?_) have hfactor : ∑ x : BitVec2 n, ∑ y : BitVec2 n, fourierCoeff (noiseOp ρ f) S * ((f x * chi S x) * (f y * chi S y)) = fourierCoeff (noiseOp ρ f) S * ((∑ x : BitVec2 n, f x * chi S x) * (∑ y : BitVec2 n, f y * chi S y)) := by simp_rw [← Finset.mul_sum, Finset.sum_mul] rw [hfactor, noiseOp_fourierCoeff] dsimp only [fourierCoeff]; ring /-- **HÅSTAD DICTATOR COMPLETENESS**: Every dictator `f(x) = χ_{e_i}(x) = (-1)^{x_i}` passes the 3-query test with exact probability **`(1 + ρ) / 2`**! -/ theorem hastad_dictator_completeness {n : ℕ} (ρ : ℝ) (i : Fin n) : dictatorTestAcc ρ (chi (f2Unit i)) = (1 + ρ) / 2 := by rw [hastad_dictator_test_fourier_acceptance] have hcoeff : ∀ S : BitVec2 n, fourierCoeff (chi (f2Unit i)) S = if S = f2Unit i then 1 else 0 := by intro S; dsimp only [fourierCoeff] have hcomm : ∑ x : BitVec2 n, chi (f2Unit i) x * chi S x = ∑ x : BitVec2 n, chi S x * chi (f2Unit i) x := Finset.sum_congr rfl (fun x _ => mul_comm _ _) rw [hcomm, chi_orthogonality] simp_rw [hcoeff] rw [Finset.sum_eq_single (f2Unit i)] · have hwt : hammingWt (f2Unit i) = 1 := by dsimp only [hammingWt] have hfilt : Finset.univ.filter (fun k : Fin n => f2Unit i k ≠ 0) = {i} := by ext k; simp only [Finset.mem_singleton, f2Unit] by_cases hk : k = i · subst hk; simp · simp [hk] rw [hfilt, Finset.card_singleton] rw [if_pos rfl, hwt]; ring · intro S _ hS; simp [hS] · intro h; exact absurd (Finset.mem_univ _) h /-- **HÅSTAD / PRS SOUNDNESS BOUND FOR HIGH-DEGREE / BALANCED LOW-INFLUENCE FUNCTIONS**: For ANY `±1`-valued function `f : F_2^n → {-1, +1}` and ANY `0 ≤ ρ ≤ 1`: 1. If `f` is balanced (`fourierCoeff f 0 = 0`) and every Fourier coefficient satisfies `fourierCoeff f S ≤ M`, then **`dictatorTestAcc ρ f ≤ (1 + ρ * M) / 2`**! 2. If `f` has zero Fourier weight below degree `d` (`∀ S, hammingWt S < d → fourierCoeff f S = 0`), then **`dictatorTestAcc ρ f ≤ (1 + ρ ^ d) / 2`**! -/ theorem hastad_dictator_soundness_bounds {n : ℕ} {ρ : ℝ} (hρ0 : 0 ≤ ρ) (hρ1 : ρ ≤ 1) {f : BitVec2 n → ℝ} (hf : IsPmOne f) : (∀ M : ℝ, 0 ≤ M → fourierCoeff f 0 = 0 → (∀ S, fourierCoeff f S ≤ M) → dictatorTestAcc ρ f ≤ (1 + ρ * M) / 2) ∧ (∀ d : ℕ, (∀ S : BitVec2 n, hammingWt S < d → fourierCoeff f S = 0) → dictatorTestAcc ρ f ≤ (1 + ρ ^ d) / 2) := by have hbound1 : ∀ S : BitVec2 n, fourierCoeff f S ≤ 1 := fun S => by have hsq := Finset.single_le_sum (fun T _ => sq_nonneg (fourierCoeff f T)) (Finset.mem_univ S) rw [fourier_parseval_pmOne hf] at hsq nlinarith [sq_nonneg (fourierCoeff f S - 1)] constructor · intro M hM hbal hleM rw [hastad_dictator_test_fourier_acceptance] have hsum : ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ ρ * M := by calc ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ ∑ S : BitVec2 n, (ρ * M) * (fourierCoeff f S) ^ 2 := Finset.sum_le_sum (fun S _ => by by_cases hS0 : S = 0 · subst hS0; rw [hbal]; simp · have hwt1 : 1 ≤ hammingWt S := by have hex : ∃ i : Fin n, S i ≠ 0 := by by_contra h; push Not at h; exact hS0 (funext h) obtain ⟨i, hi⟩ := hex exact Finset.card_pos.mpr ⟨i, Finset.mem_filter.mpr ⟨Finset.mem_univ i, hi⟩⟩ have hρle : ρ ^ hammingWt S ≤ ρ := by calc ρ ^ hammingWt S ≤ ρ ^ 1 := pow_le_pow_of_le_one hρ0 hρ1 hwt1 _ = ρ := pow_one ρ have h3 : ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ (ρ * M) * (fourierCoeff f S) ^ 2 := by by_cases hpos : 0 ≤ fourierCoeff f S · calc ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 = (ρ ^ hammingWt S * fourierCoeff f S) * (fourierCoeff f S) ^ 2 := by ring _ ≤ (ρ * M) * (fourierCoeff f S) ^ 2 := mul_le_mul_of_nonneg_right (mul_le_mul hρle (hleM S) hpos hρ0) (sq_nonneg _) · have hneg : fourierCoeff f S < 0 := lt_of_not_ge hpos have hlhs_le : ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ 0 := by have hcube : (fourierCoeff f S) ^ 3 ≤ 0 := by nlinarith [sq_nonneg (fourierCoeff f S)] exact mul_nonpos_of_nonneg_of_nonpos (pow_nonneg hρ0 _) hcube have hrhs_ge : 0 ≤ (ρ * M) * (fourierCoeff f S) ^ 2 := by positivity linarith exact h3) _ = (ρ * M) * ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 := (Finset.mul_sum _ _ _).symm _ = ρ * M := by rw [fourier_parseval_pmOne hf, mul_one] linarith · intro d hdeg rw [hastad_dictator_test_fourier_acceptance] have hsum : ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ ρ ^ d := by calc ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ ∑ S : BitVec2 n, ρ ^ d * (fourierCoeff f S) ^ 2 := Finset.sum_le_sum (fun S _ => by by_cases hlt : hammingWt S < d · rw [hdeg S hlt]; simp · have hge : d ≤ hammingWt S := by omega have hρle : ρ ^ hammingWt S ≤ ρ ^ d := pow_le_pow_of_le_one hρ0 hρ1 hge by_cases hpos : 0 ≤ fourierCoeff f S · calc ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 = (ρ ^ hammingWt S * fourierCoeff f S) * (fourierCoeff f S) ^ 2 := by ring _ ≤ (ρ ^ d * 1) * (fourierCoeff f S) ^ 2 := mul_le_mul_of_nonneg_right (mul_le_mul hρle (hbound1 S) hpos (pow_nonneg hρ0 _)) (sq_nonneg _) _ = ρ ^ d * (fourierCoeff f S) ^ 2 := by ring · have hneg : fourierCoeff f S < 0 := lt_of_not_ge hpos have hlhs_le : ρ ^ hammingWt S * (fourierCoeff f S) ^ 3 ≤ 0 := mul_nonpos_of_nonneg_of_nonpos (pow_nonneg hρ0 _) (by nlinarith [sq_nonneg (fourierCoeff f S)]) have hrhs_ge : 0 ≤ ρ ^ d * (fourierCoeff f S) ^ 2 := by positivity linarith) _ = ρ ^ d * ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 := (Finset.mul_sum _ _ _).symm _ = ρ ^ d := by rw [fourier_parseval_pmOne hf, mul_one] linarith /-- **EXACT DICTATOR vs. `n`-PARITY SEPARATION IN HÅSTAD'S 3-QUERY TEST**: For `n ≥ 2` and `0 < ρ < 1`, the `n`-bit parity `χ_{1^n}` achieves exact acceptance `(1 + ρ^n) / 2`, strictly separated from every dictator's `(1 + ρ) / 2` by **`(ρ - ρ^n) / 2 > 0`**! -/ theorem hastad_dictator_vs_parity_separation {n : ℕ} (hn : 2 ≤ n) {ρ : ℝ} (hρ0 : 0 < ρ) (hρ1 : ρ < 1) : let allOnes : BitVec2 n := fun _ => 1 dictatorTestAcc ρ (chi allOnes) = (1 + ρ ^ n) / 2 ∧ dictatorTestAcc ρ (chi allOnes) < dictatorTestAcc ρ (chi (f2Unit ⟨0, Nat.lt_of_lt_of_le (by norm_num) hn⟩)) := by intro allOnes have hacc : dictatorTestAcc ρ (chi allOnes) = (1 + ρ ^ n) / 2 := by rw [hastad_dictator_test_fourier_acceptance] have hcoeff : ∀ S : BitVec2 n, fourierCoeff (chi allOnes) S = if S = allOnes then 1 else 0 := by intro S; dsimp only [fourierCoeff] have hcomm : ∑ x : BitVec2 n, chi allOnes x * chi S x = ∑ x : BitVec2 n, chi S x * chi allOnes x := Finset.sum_congr rfl (fun x _ => mul_comm _ _) rw [hcomm, chi_orthogonality] simp_rw [hcoeff] rw [Finset.sum_eq_single allOnes] · have hwt : hammingWt allOnes = n := by dsimp only [hammingWt, allOnes]; simp rw [if_pos rfl, hwt]; ring · intro S _ hS; simp [hS] · intro h; exact absurd (Finset.mem_univ _) h refine ⟨hacc, ?_⟩ rw [hacc, hastad_dictator_completeness] have hpow : ρ ^ n < ρ := by calc ρ ^ n < ρ ^ 1 := pow_lt_pow_right_of_lt_one₀ hρ0 hρ1 (by omega) _ = ρ := pow_one ρ linarith /-- **EXACT DICTATOR vs. 3-BIT MAJORITY SEPARATION IN HÅSTAD'S 3-QUERY TEST**: On `F_2^3`, 3-bit Majority `Maj_3(x) = if |x| ≤ 1 then 1 else -1` has exact acceptance **`(1 + (3 / 8) * ρ - (1 / 8) * ρ^3) / 2`**, strictly below Dictator `(1 + ρ) / 2` by `(5 ρ + ρ^3) / 16 > 0`! -/ theorem hastad_dictator_vs_maj3_separation {ρ : ℝ} (hρ0 : 0 < ρ) : let maj3 : BitVec2 3 → ℝ := fun x => if hammingWt x ≤ 1 then 1 else -1 dictatorTestAcc ρ maj3 = (1 + (3 / 8 : ℝ) * ρ - (1 / 8 : ℝ) * ρ ^ 3) / 2 ∧ dictatorTestAcc ρ maj3 < dictatorTestAcc ρ (chi (f2Unit (⟨0, by omega⟩ : Fin 3))) := by intro maj3 let mk3 : ZMod 2 → ZMod 2 → ZMod 2 → BitVec2 3 := fun a b c i => match i with | ⟨0, _⟩ => a | ⟨1, _⟩ => b | ⟨2, _⟩ => c let e3 : (ZMod 2 × ZMod 2 × ZMod 2) ≃ BitVec2 3 := { toFun := fun p => mk3 p.1 p.2.1 p.2.2 invFun := fun x => (x 0, x 1, x 2) left_inv := fun ⟨a, b, c⟩ => rfl right_inv := fun x => by funext i; fin_cases i <;> rfl } have hwt3 : ∀ a b c : ZMod 2, hammingWt (mk3 a b c) = (if a ≠ 0 then 1 else 0) + (if b ≠ 0 then 1 else 0) + (if c ≠ 0 then 1 else 0) := by intro a b c have hsum : (Finset.univ.filter (fun i : Fin 3 => mk3 a b c i ≠ 0)).card = ∑ i : Fin 3, (if mk3 a b c i ≠ 0 then 1 else 0) := by rw [← Finset.sum_filter]; simp rw [hammingWt, hsum, Fin.sum_univ_three] have hsum8 : ∀ F : BitVec2 3 → ℝ, ∑ p : ZMod 2 × ZMod 2 × ZMod 2, F (e3 p) = F (mk3 0 0 0) + F (mk3 0 0 1) + F (mk3 0 1 0) + F (mk3 0 1 1) + F (mk3 1 0 0) + F (mk3 1 0 1) + F (mk3 1 1 0) + F (mk3 1 1 1) := by intro F rw [Fintype.sum_prod_type] simp_rw [Fintype.sum_prod_type] have hz : (Finset.univ : Finset (ZMod 2)) = {0, 1} := by ext a; rcases zmod2_eq_zero_or_one a with rfl | rfl <;> simp simp_rw [hz, Finset.sum_pair (by decide : (0 : ZMod 2) ≠ 1)] dsimp only [e3, Equiv.coe_fn_mk]; ring -- Compute `fourierCoeff maj3 S` explicitly for all 8 vectors `S : BitVec2 3`! have hcoeff : ∀ S : BitVec2 3, fourierCoeff maj3 S = if hammingWt S = 1 then (1 / 2 : ℝ) else if hammingWt S = 3 then -(1 / 2 : ℝ) else 0 := by intro S have h0 := zmod2_eq_zero_or_one (S 0) have h1 := zmod2_eq_zero_or_one (S 1) have h2 := zmod2_eq_zero_or_one (S 2) have hSeq : S = mk3 (S 0) (S 1) (S 2) := (e3.right_inv S).symm have h2z : (2 : ZMod 2) = 0 := by decide have h3z : (3 : ZMod 2) = 1 := by decide dsimp only [fourierCoeff] rw [← Equiv.sum_comp e3 (fun x => maj3 x * chi S x), hSeq] rw [hsum8 (fun x => maj3 x * chi (mk3 (S 0) (S 1) (S 2)) x)] rcases h0 with hs0 | hs0 <;> rcases h1 with hs1 | hs1 <;> rcases h2 with hs2 | hs2 <;> (rw [hs0, hs1, hs2]; dsimp only [maj3, chi, f2Dot]; simp only [hwt3, Fin.sum_univ_three, mk3]; norm_num; try (simp only [h2z, h3z]; norm_num)) have hacc : dictatorTestAcc ρ maj3 = (1 + (3 / 8 : ℝ) * ρ - (1 / 8 : ℝ) * ρ ^ 3) / 2 := by rw [hastad_dictator_test_fourier_acceptance] simp_rw [hcoeff] rw [← Equiv.sum_comp e3] rw [hsum8 (fun S => ρ ^ hammingWt S * (if hammingWt S = 1 then (1 / 2 : ℝ) else if hammingWt S = 3 then -(1 / 2 : ℝ) else 0) ^ 3)] simp only [hwt3] norm_num; ring refine ⟨hacc, ?_⟩ rw [hacc, hastad_dictator_completeness] nlinarith [pow_nonneg (le_of_lt hρ0) 3] /-- **A `k`-JUNTA FUNCTION** depends on at most `k` coordinates `J ⊆ Fin n` (`J.card ≤ k`). -/ def IsKJunta {n : ℕ} (f : BitVec2 n → ℝ) (k : ℕ) : Prop := ∃ J : Finset (Fin n), J.card ≤ k ∧ ∀ x y : BitVec2 n, (∀ i ∈ J, x i = y i) → f x = f y /-- **`k`-JUNTA FOURIER SUPPORT & DEGREE CHARACTERIZATION**: If `f : BitVec2 n → ℝ` is a `k`-junta (depending only on `J` with `J.card ≤ k`), then: 1. Every nonzero Fourier coefficient `fourierCoeff f S ≠ 0` has support inside `J` (`∀ i ∉ J, S i = 0`); 2. Hence every nonzero Fourier coefficient has degree **`hammingWt S ≤ k`**; 3. The Fourier sparsity is bounded by **`|{S | fourierCoeff f S ≠ 0}| ≤ 2 ^ k`**; 4. **Consequence**: If `k < n`, the `n`-bit parity `χ_{1^n}` has exact distance **`= 1 / 2`** (zero correlation!) from EVERY `±1`-valued `k`-junta! -/ theorem kJunta_fourier_degree_and_parity_distance {n k : ℕ} {f : BitVec2 n → ℝ} (hj : IsKJunta f k) : (∀ S : BitVec2 n, fourierCoeff f S ≠ 0 → hammingWt S ≤ k) ∧ ((Finset.univ.filter (fun S : BitVec2 n => fourierCoeff f S ≠ 0)).card ≤ 2 ^ k) ∧ (k < n → IsPmOne f → boolDist f (chi (fun _ => 1)) = 1 / 2) := by obtain ⟨J, hJcard, hJdep⟩ := hj have hsupp : ∀ S : BitVec2 n, fourierCoeff f S ≠ 0 → ∀ i : Fin n, i ∉ J → S i = 0 := by intro S hne i hiJ by_contra hSi have hSi1 : S i = 1 := (zmod2_eq_zero_or_one (S i)).resolve_left hSi have hinv : ∀ x : BitVec2 n, f (x + f2Unit i) = f x := fun x => hJdep (x + f2Unit i) x (fun j hj' => by have hji : j ≠ i := fun h => hiJ (h ▸ hj') simp [f2Unit, hji]) have hneg : ∀ x : BitVec2 n, f (x + f2Unit i) * chi S (x + f2Unit i) = -(f x * chi S x) := fun x => by rw [hinv x, chi_add_right] have hu : chi S (f2Unit i) = -1 := by dsimp only [chi]; rw [f2Dot_unit, hSi1]; simp rw [hu]; ring have hshift : ∑ x : BitVec2 n, f (x + f2Unit i) * chi S (x + f2Unit i) = ∑ x : BitVec2 n, f x * chi S x := Equiv.sum_comp (Equiv.addRight (f2Unit i)) (fun x => f x * chi S x) simp_rw [hneg, Finset.sum_neg_distrib] at hshift have hsum0 : ∑ x : BitVec2 n, f x * chi S x = 0 := by linarith exact hne (by dsimp only [fourierCoeff]; rw [hsum0, mul_zero]) have hdeg : ∀ S : BitVec2 n, fourierCoeff f S ≠ 0 → hammingWt S ≤ k := by intro S hne have hsub : Finset.univ.filter (fun i : Fin n => S i ≠ 0) ⊆ J := by intro i hi; by_contra hiJ; exact (Finset.mem_filter.mp hi).2 (hsupp S hne i hiJ) exact le_trans (Finset.card_le_card hsub) hJcard have hcard : (Finset.univ.filter (fun S : BitVec2 n => fourierCoeff f S ≠ 0)).card ≤ 2 ^ k := by have hsub : Finset.univ.filter (fun S : BitVec2 n => fourierCoeff f S ≠ 0) ⊆ J.powerset.image (fun A : Finset (Fin n) => (fun i => if i ∈ A then (1 : ZMod 2) else 0)) := by intro S hS have hne := (Finset.mem_filter.mp hS).2 let A := Finset.univ.filter (fun i : Fin n => S i ≠ 0) refine Finset.mem_image.mpr ⟨A, Finset.mem_powerset.mpr (fun i hi => by by_contra hiJ; exact (Finset.mem_filter.mp hi).2 (hsupp S hne i hiJ)), ?_⟩ funext i; dsimp only [A] rcases zmod2_eq_zero_or_one (S i) with h0 | h1 · simp [h0] · simp [h1] calc (Finset.univ.filter (fun S : BitVec2 n => fourierCoeff f S ≠ 0)).card ≤ (J.powerset.image (fun A : Finset (Fin n) => (fun i => if i ∈ A then (1 : ZMod 2) else 0))).card := Finset.card_le_card hsub _ ≤ J.powerset.card := Finset.card_image_le _ = 2 ^ J.card := Finset.card_powerset J _ ≤ 2 ^ k := Nat.pow_le_pow_right (by norm_num) hJcard refine ⟨hdeg, hcard, fun hkn hf => ?_⟩ rw [boolDist_chi_eq hf] have hzero : fourierCoeff f (fun _ => 1) = 0 := by by_contra hne have hle := hdeg (fun _ => 1) hne have hwt : hammingWt (fun _ : Fin n => (1 : ZMod 2)) = n := by dsimp only [hammingWt]; simp omega rw [hzero]; norm_num /-! =============================================================================== GRAND UNIFICATION THEOREM: ALL FIVE PROPERTY-TESTING PILLARS IN ONE STATEMENT =============================================================================== -/ /-- **GRAND UNIFICATION OF PROPERTY TESTING & SUBLINEAR QUERY COMPLEXITY MINIMAX**: Combines the flagship theorems of all five pillars into a single verified theorem: 1. **BLR Linearity**: Exact Fourier soundness `distToLinear f ≤ blrRej f`; 2. **Hypercube Monotonicity (GGLRS)**: Exact `dist(f, Mono) ≤ (n / 2) * Pr[violated edge]`; 3. **Paninski Uniformity Lower Bound**: `Ω(√n / ε²)` sample complexity via `χ² ≤ exp(16 m² ε⁴ / n) - 1`; 4. **Yao Minimax & Mantel Threshold**: Distributional lower bounds lift to randomized algorithms, and every triangle-free graph has `2 * twiceEdges G ≤ n²`; 5. **Håstad Dictator Test**: Exact Fourier acceptance `(1 + ∑ ρ^{|S|} hat_f(S)³) / 2`! -/ theorem property_testing_sublinear_minimax_grand_unification : (∀ {n : ℕ} {f : BitVec2 n → ℝ}, IsPmOne f → distToLinear f ≤ blrRej f) ∧ (∀ {n : ℕ}, 1 ≤ n → ∀ f : Cube n → Bool, ∃ h : Cube n → Bool, IsCubeMonotone h ∧ (cubeHammingCount f h : ℝ) / (2 : ℝ) ^ n ≤ ((n : ℝ) / 2) * cubeEdgeViolFrac f) ∧ (∀ {k m : ℕ}, 1 ≤ k → ∀ {ε : ℝ}, 0 ≤ ε → ε ≤ 1 / 2 → ∀ T : (Fin m → Fin (2 * k)) → Bool, (2 / 3 : ℝ) ≤ (∑ X, if T X = true then prodDist (uniformDist (Fin (2 * k))) X else 0) → (∀ z : Fin k → Bool, (∑ X, if T X = true then prodDist (paninskiDist ε z) X else 0) ≤ 1 / 3) → Real.log (13 / 9) ≤ 16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ)) ∧ (∀ {n : ℕ} (G : SimpleGraphAdj n), IsTriangleFree G → 2 * twiceEdges G ≤ n * n) ∧ (∀ {n : ℕ} (ρ : ℝ) (f : BitVec2 n → ℝ), dictatorTestAcc ρ f = (1 + ∑ S : BitVec2 n, ρ ^ hammingWt S * (fourierCoeff f S) ^ 3) / 2) := by refine ⟨?_, ?_, ?_, ?_, ?_⟩ · intro n f hf; exact blr_soundness_ge_dist hf · intro n hn f; exact gglrs_hypercube_edge_tester_soundness hn f · intro k m hk ε h0 hhalf T hcomp hsound exact (paninski_ingster_uniformity_lower_bound hk h0 hhalf).2.2 T hcomp hsound · intro n G htf; exact (mantel_and_moon_moser_supersaturation G).2 htf · intro n ρ f; exact hastad_dictator_test_fourier_acceptance ρ f /-! =============================================================================== PART 6: EXTENDED STRUCTURAL THEOREMS & COROLLARIES ACROSS ALL FIVE PILLARS =============================================================================== -/ /-! ### Part 6A: Extended BLR Linearity & Walsh–Hadamard Calculus -/ theorem f2Dot_zero_left {n : ℕ} (x : BitVec2 n) : f2Dot 0 x = 0 := by dsimp only [f2Dot]; simp theorem f2Dot_zero_right {n : ℕ} (S : BitVec2 n) : f2Dot S 0 = 0 := by dsimp only [f2Dot]; simp theorem chi_zero_left {n : ℕ} (x : BitVec2 n) : chi 0 x = 1 := by dsimp only [chi]; rw [f2Dot_zero_left]; simp theorem chi_zero_right {n : ℕ} (S : BitVec2 n) : chi S 0 = 1 := by dsimp only [chi]; rw [f2Dot_zero_right]; simp theorem chi_symm {n : ℕ} (S x : BitVec2 n) : chi S x = chi x S := by dsimp only [chi]; rw [f2Dot_comm] theorem chi_sq_one {n : ℕ} (S x : BitVec2 n) : (chi S x) ^ 2 = 1 := isPmOne_sq (chi_isPmOne S) x theorem chi_abs_one {n : ℕ} (S x : BitVec2 n) : |chi S x| = 1 := by rcases chi_isPmOne S x with h | h <;> simp [h] theorem fourierCoeff_add {n : ℕ} (f g : BitVec2 n → ℝ) (S : BitVec2 n) : fourierCoeff (fun x => f x + g x) S = fourierCoeff f S + fourierCoeff g S := by dsimp only [fourierCoeff] simp_rw [add_mul, Finset.sum_add_distrib, mul_add] theorem fourierCoeff_smul {n : ℕ} (c : ℝ) (f : BitVec2 n → ℝ) (S : BitVec2 n) : fourierCoeff (fun x => c * f x) S = c * fourierCoeff f S := by dsimp only [fourierCoeff] have hpt : ∀ x : BitVec2 n, c * f x * chi S x = c * (f x * chi S x) := fun _ => by ring simp_rw [hpt, ← Finset.mul_sum]; ring theorem fourierCoeff_neg {n : ℕ} (f : BitVec2 n → ℝ) (S : BitVec2 n) : fourierCoeff (fun x => -f x) S = -fourierCoeff f S := by have h : (fun x => -f x) = (fun x => (-1 : ℝ) * f x) := by funext x; ring rw [h, fourierCoeff_smul]; ring theorem fourierCoeff_chi {n : ℕ} (T S : BitVec2 n) : fourierCoeff (chi T) S = if S = T then 1 else 0 := by dsimp only [fourierCoeff] have hcomm : ∑ x : BitVec2 n, chi T x * chi S x = ∑ x : BitVec2 n, chi S x * chi T x := Finset.sum_congr rfl (fun _ _ => mul_comm _ _) rw [hcomm, chi_orthogonality] theorem fourierCoeff_const {n : ℕ} (c : ℝ) (S : BitVec2 n) : fourierCoeff (fun _ => c) S = if S = 0 then c else 0 := by have h : (fun _ : BitVec2 n => c) = (fun x => c * chi 0 x) := by funext x; rw [chi_zero_left, mul_one] rw [h, fourierCoeff_smul, fourierCoeff_chi] split_ifs <;> ring theorem fourierCoeff_abs_le_one {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) (S : BitVec2 n) : |fourierCoeff f S| ≤ 1 := by have hsq : (fourierCoeff f S) ^ 2 ≤ 1 := by rw [← fourier_parseval_pmOne hf] exact Finset.single_le_sum (fun T _ => sq_nonneg (fourierCoeff f T)) (Finset.mem_univ S) rw [← sq_abs] at hsq nlinarith [abs_nonneg (fourierCoeff f S)] theorem fourierCoeff_ge_neg_one {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) (S : BitVec2 n) : -1 ≤ fourierCoeff f S := (abs_le.mp (fourierCoeff_abs_le_one hf S)).1 theorem boolDist_nonneg {n : ℕ} (f g : BitVec2 n → ℝ) : 0 ≤ boolDist f g := by dsimp only [boolDist]; positivity theorem boolDist_self {n : ℕ} (f : BitVec2 n → ℝ) : boolDist f f = 0 := by dsimp only [boolDist]; simp theorem boolDist_symm {n : ℕ} (f g : BitVec2 n → ℝ) : boolDist f g = boolDist g f := by dsimp only [boolDist] have h : Finset.univ.filter (fun x : BitVec2 n => f x ≠ g x) = Finset.univ.filter (fun x : BitVec2 n => g x ≠ f x) := by ext x; simp [ne_comm] rw [h] theorem boolDist_le_one {n : ℕ} (f g : BitVec2 n → ℝ) : boolDist f g ≤ 1 := by dsimp only [boolDist] have hcard : ((Finset.univ.filter (fun x : BitVec2 n => f x ≠ g x)).card : ℝ) ≤ (2 : ℝ) ^ n := by have hle : (Finset.univ.filter (fun x : BitVec2 n => f x ≠ g x)).card ≤ (Finset.univ : Finset (BitVec2 n)).card := Finset.card_le_card (Finset.filter_subset _ _) have h2 : (Finset.univ : Finset (BitVec2 n)).card = 2 ^ n := by simp [ZMod.card, Fintype.card_fin] rw [h2] at hle exact_mod_cast hle rw [div_le_one (by positivity)]; exact hcard theorem distToLinear_nonneg {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : 0 ≤ distToLinear f := by obtain ⟨S, hS⟩ := (distToLinear_eq_min_boolDist hf).2 rw [← hS]; exact boolDist_nonneg f (chi S) theorem distToLinear_le_one {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : distToLinear f ≤ 1 := by obtain ⟨S, hS⟩ := (distToLinear_eq_min_boolDist hf).2 rw [← hS] exact boolDist_le_one f (chi S) theorem distToLinear_le_half_of_pos {n : ℕ} (hn : 1 ≤ n) {f : BitVec2 n → ℝ} (hf : IsPmOne f) : distToLinear f ≤ 1 / 2 := by -- For `1 ≤ n`, if every `fourierCoeff f S < 0`, then `0 ≤ ...`? Even simpler: -- `∑ S, fourierCoeff f S = 2^n * f 0`? No, `∑ S, fourierCoeff f S = f 0 ∈ {-1, 1}`, and `∑ S, hat_f(S) * chi S x = f x`! -- Since `1 ≤ n`, `|BitVec2 n| = 2^n ≥ 2`: pick `x ≠ 0`? Actually `∑ S, (1 + chi S x) * hat_f(S) = f 0 + f x`. -- Better: `∑ S, hat_f(S)^2 = 1`, AND `∃ S, 0 ≤ hat_f(S)`! Why? Because `f 0 = ∑ S, hat_f(S)` and -- for any `x`, `f x = ∑ S, hat_f(S) * chi S x`. If `∀ S, hat_f(S) < 0`, then for `x = 0`, `f 0 = ∑ S, hat_f(S)` -- and for ANY `S₀`, wait: what if `f = -chi T`? Then `hat_f(T) = -1` and `hat_f(S) = 0` for `S ≠ T` (`S ≠ T` exists since `2^n ≥ 2`)! -- In general, if `∀ S, hat_f(S) < 0`, then `f 0 = ∑ S, hat_f(S) < 0`, so `f 0 = -1`, so `∑ S, (-hat_f(S)) = 1`. -- Since `∀ S, 0 < -hat_f(S)` and `∑ (-hat_f(S)) = 1`, we have `-hat_f(S) < 1` for every `S` (since `2^n ≥ 2`), -- hence `(-hat_f(S))^2 < -hat_f(S)`, so `1 = ∑ hat_f(S)^2 < ∑ (-hat_f(S)) = 1`, CONTRADICTION! have hex : ∃ S : BitVec2 n, 0 ≤ fourierCoeff f S := by by_contra hall; push Not at hall have hinv0 := fourier_inversion f 0 simp_rw [chi_zero_right, mul_one] at hinv0 have hsum_neg : ∑ S : BitVec2 n, fourierCoeff f S < 0 := Finset.sum_neg (fun S _ => hall S) Finset.univ_nonempty have hf0 : f 0 = -1 := by rcases hf 0 with h | h · linarith · exact h have hsum_pos : ∑ S : BitVec2 n, (-fourierCoeff f S) = 1 := by rw [Finset.sum_neg_distrib]; linarith have hcard_ge2 : 2 ≤ (Finset.univ : Finset (BitVec2 n)).card := by have h2 : (Finset.univ : Finset (BitVec2 n)).card = 2 ^ n := by simp [ZMod.card, Fintype.card_fin] rw [h2] calc 2 = 2 ^ 1 := by norm_num _ ≤ 2 ^ n := Nat.pow_le_pow_right (by norm_num) hn have hlt1 : ∀ S : BitVec2 n, -fourierCoeff f S < 1 := by intro S have hne_er : (Finset.univ.erase S).Nonempty := by rw [← Finset.card_pos, Finset.card_erase_of_mem (Finset.mem_univ S)]; omega have her_pos : 0 < ∑ T ∈ Finset.univ.erase S, (-fourierCoeff f T) := Finset.sum_pos (fun T _ => neg_pos.mpr (hall T)) hne_er rw [← Finset.add_sum_erase _ _ (Finset.mem_univ S)] at hsum_pos linarith have hstrict : ∑ S : BitVec2 n, (fourierCoeff f S) ^ 2 < ∑ S : BitVec2 n, (-fourierCoeff f S) := Finset.sum_lt_sum_of_nonempty Finset.univ_nonempty (fun S _ => by have hp : 0 < -fourierCoeff f S := neg_pos.mpr (hall S) have h1 := hlt1 S nlinarith) rw [fourier_parseval_pmOne hf, hsum_pos] at hstrict exact lt_irrefl 1 hstrict obtain ⟨S, hS⟩ := hex calc distToLinear f ≤ boolDist f (chi S) := (distToLinear_eq_min_boolDist hf).1 S _ = (1 - fourierCoeff f S) / 2 := boolDist_chi_eq hf S _ ≤ 1 / 2 := by linarith theorem blrRej_bounds {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : 0 ≤ blrRej f ∧ blrRej f ≤ 1 := by constructor · exact le_trans (distToLinear_nonneg hf) (blr_soundness_ge_dist hf) · dsimp only [blrRej] have hle : (Finset.univ.filter (fun p : BitVec2 n × BitVec2 n => f p.1 * f p.2 ≠ f (p.1 + p.2))).card ≤ (Finset.univ : Finset (BitVec2 n × BitVec2 n)).card := Finset.card_le_card (Finset.filter_subset _ _) have hcard : (Finset.univ : Finset (BitVec2 n × BitVec2 n)).card = 2 ^ n * 2 ^ n := by simp [Fintype.card_prod, ZMod.card, Fintype.card_fin] rw [hcard] at hle have hleR : ((Finset.univ.filter (fun p : BitVec2 n × BitVec2 n => f p.1 * f p.2 ≠ f (p.1 + p.2))).card : ℝ) ≤ (2 : ℝ) ^ n * (2 : ℝ) ^ n := by exact_mod_cast hle rw [div_le_one (by positivity)]; exact hleR theorem blr_self_correction_majority_gt_half {n : ℕ} {f : BitVec2 n → ℝ} {S : BitVec2 n} {δ : ℝ} (hdist : boolDist f (chi S) ≤ δ) (hδ : δ < 1 / 4) (x : BitVec2 n) : (1 / 2 : ℝ) < ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x)).card : ℝ) / (2 : ℝ) ^ n := by have herr := blr_self_correction_error_le_two_delta hdist x have hpart : (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x)).card + (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card = 2 ^ n := by have hu : Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x) ∪ Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x) = Finset.univ := by ext y; simp [em] have hd : Disjoint (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x)) (Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)) := by rw [Finset.disjoint_filter]; intro y _ h1 h2; exact h2 h1 rw [← Finset.card_union_of_disjoint hd, hu] simp [ZMod.card, Fintype.card_fin] have hpartR : ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x)).card : ℝ) / (2 : ℝ) ^ n + ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card : ℝ) / (2 : ℝ) ^ n = 1 := by have hp : ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) = chi S x)).card : ℝ) + ((Finset.univ.filter (fun y : BitVec2 n => f y * f (x + y) ≠ chi S x)).card : ℝ) = (2 : ℝ) ^ n := by exact_mod_cast hpart rw [← add_div, hp, div_self (by positivity)] linarith /-! ### Part 6B: Extended Line & Hypercube Monotonicity Testing -/ open Classical in theorem line_bs_zero_rejection_on_monotone {n d : ℕ} (hn : 1 ≤ n) (hd : n ≤ 2 ^ d) {α : Type*} [LinearOrder α] {f : Fin n → α} (hm : Monotone f) : (Finset.univ.filter (fun i : Fin n => ¬ bsCheck f i d 0 (n - 1))).card = 0 := by rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro i _ exact not_not.mpr ((line_binary_search_monotonicity_soundness hn hd f).1 hm i) theorem line_bs_const_passes {n d : ℕ} (hn : 1 ≤ n) (hd : n ≤ 2 ^ d) {α : Type*} [LinearOrder α] (c : α) (i : Fin n) : bsCheck (fun _ : Fin n => c) i d 0 (n - 1) := (line_binary_search_monotonicity_soundness hn hd (fun _ => c)).1 (fun _ _ _ => le_rfl) i open Classical in theorem line_bs_distance_le_rejection_frac {n d : ℕ} (hn : 1 ≤ n) (hd : n ≤ 2 ^ d) {α : Type*} [LinearOrder α] [DecidableEq α] (f : Fin n → α) : ∃ g : Fin n → α, Monotone g ∧ ((Finset.univ.filter (fun i : Fin n => f i ≠ g i)).card : ℝ) / (n : ℝ) ≤ ((Finset.univ.filter (fun i : Fin n => ¬ bsCheck f i d 0 (n - 1))).card : ℝ) / (n : ℝ) := by obtain ⟨g, hgm, hagree⟩ := (line_binary_search_monotonicity_soundness hn hd f).2 refine ⟨g, hgm, ?_⟩ have hsub : Finset.univ.filter (fun i : Fin n => f i ≠ g i) ⊆ Finset.univ.filter (fun i : Fin n => ¬ bsCheck f i d 0 (n - 1)) := by intro i hi refine Finset.mem_filter.mpr ⟨Finset.mem_univ i, fun hbs => (Finset.mem_filter.mp hi).2 (hagree i hbs).symm⟩ exact div_le_div_of_nonneg_right (Nat.cast_le.mpr (Finset.card_le_card hsub)) (Nat.cast_nonneg n) theorem decision_tree_lower_bound_pow2_plus_one {q : ℕ} (leaf : Fin (2 ^ q + 1) → (Fin q → Bool)) : ∃ i j : Fin (2 ^ q + 1), i ≠ j ∧ leaf i = leaf j := decision_tree_pigeonhole_lower_bound (by omega) leaf theorem cube_monotone_const_false (n : ℕ) : IsCubeMonotone (fun _ : Cube n => false) := fun _ _ _ => le_rfl theorem cube_monotone_const_true (n : ℕ) : IsCubeMonotone (fun _ : Cube n => true) := fun _ _ _ => le_rfl theorem cube_monotone_dictator {n : ℕ} (i : Fin n) : IsCubeMonotone (fun x : Cube n => x i) := fun _ _ hxy => hxy i theorem cube_monotone_and {n : ℕ} {f g : Cube n → Bool} (hf : IsCubeMonotone f) (hg : IsCubeMonotone g) : IsCubeMonotone (fun x => f x && g x) := fun x y hxy => by have h1 := hf x y hxy have h2 := hg x y hxy cases hfx : f x <;> cases hfy : f y <;> cases hgx : g x <;> cases hgy : g y <;> simp_all [Bool.le_iff_imp] theorem cube_monotone_or {n : ℕ} {f g : Cube n → Bool} (hf : IsCubeMonotone f) (hg : IsCubeMonotone g) : IsCubeMonotone (fun x => f x || g x) := fun x y hxy => by have h1 := hf x y hxy have h2 := hg x y hxy cases hfx : f x <;> cases hfy : f y <;> cases hgx : g x <;> cases hgy : g y <;> simp_all [Bool.le_iff_imp] theorem cube_monotone_zero_violDim {n : ℕ} {f : Cube n → Bool} (hf : IsCubeMonotone f) (i : Fin n) : cubeViolDim f i = 0 := by dsimp only [cubeViolDim] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] intro x _ ⟨hxi, hfx, hfu⟩ have hle : ∀ k : Fin n, x k ≤ Function.update x i true k := fun k => by by_cases hk : k = i · subst hk; simp [hxi] · simp [hk] have hm := hf x (Function.update x i true) hle rw [hfx, hfu] at hm; exact absurd hm (by decide) theorem cube_monotone_zero_totalViol {n : ℕ} {f : Cube n → Bool} (hf : IsCubeMonotone f) : cubeTotalViol f = 0 := by dsimp only [cubeTotalViol] exact Finset.sum_eq_zero (fun i _ => cube_monotone_zero_violDim hf i) theorem cube_monotone_zero_edgeViolFrac {n : ℕ} {f : Cube n → Bool} (hf : IsCubeMonotone f) : cubeEdgeViolFrac f = 0 := by dsimp only [cubeEdgeViolFrac]; rw [cube_monotone_zero_totalViol hf]; simp theorem cubeHammingCount_self {n : ℕ} (f : Cube n → Bool) : cubeHammingCount f f = 0 := by dsimp only [cubeHammingCount]; simp theorem cubeHammingCount_symm {n : ℕ} (f g : Cube n → Bool) : cubeHammingCount f g = cubeHammingCount g f := by dsimp only [cubeHammingCount]; congr 1; ext x; simp [ne_comm] theorem cubeHammingCount_triangle {n : ℕ} (f g h : Cube n → Bool) : cubeHammingCount f h ≤ cubeHammingCount f g + cubeHammingCount g h := by dsimp only [cubeHammingCount] have hsub : Finset.univ.filter (fun x : Cube n => f x ≠ h x) ⊆ Finset.univ.filter (fun x : Cube n => f x ≠ g x) ∪ Finset.univ.filter (fun x : Cube n => g x ≠ h x) := by intro x hx rw [Finset.mem_union, Finset.mem_filter, Finset.mem_filter] by_cases hfg : f x = g x · right; exact ⟨Finset.mem_univ x, by rw [← hfg]; exact (Finset.mem_filter.mp hx).2⟩ · left; exact ⟨Finset.mem_univ x, hfg⟩ exact le_trans (Finset.card_le_card hsub) (Finset.card_union_le _ _) theorem cubeHammingCount_le_pow {n : ℕ} (f g : Cube n → Bool) : cubeHammingCount f g ≤ 2 ^ n := by dsimp only [cubeHammingCount] have hle := Finset.card_le_card (Finset.filter_subset (fun x : Cube n => f x ≠ g x) Finset.univ) simpa [Fintype.card_fun, Fintype.card_bool, Fintype.card_fin] using hle theorem cubeViolDim_le_half_pow {n : ℕ} (hn : 1 ≤ n) (f : Cube n → Bool) (i : Fin n) : cubeViolDim f i ≤ 2 ^ (n - 1) := by have hsub : Finset.univ.filter (fun x : Cube n => x i = false ∧ f x = true ∧ f (Function.update x i true) = false) ⊆ Finset.univ.filter (fun x : Cube n => x i = false) := fun x hx => Finset.mem_filter.mpr ⟨Finset.mem_univ x, (Finset.mem_filter.mp hx).2.1⟩ have hcard : (Finset.univ.filter (fun x : Cube n => x i = false)).card = 2 ^ (n - 1) := by have hflip : (Finset.univ.filter (fun x : Cube n => x i = true)).card = (Finset.univ.filter (fun x : Cube n => x i = false)).card := by refine Finset.card_bij (fun x _ => Function.update x i false) (fun _ _ => Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩) (fun a ha b hb hab => by funext k; by_cases hk : k = i · subst hk; exact (Finset.mem_filter.mp ha).2.trans (Finset.mem_filter.mp hb).2.symm · have := congrFun hab k; simpa [hk] using this) (fun y hy => ⟨Function.update y i true, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simp⟩, by funext k; by_cases hk : k = i · subst hk; simp [(Finset.mem_filter.mp hy).2] · simp [hk]⟩) have hu : Finset.univ.filter (fun x : Cube n => x i = false) ∪ Finset.univ.filter (fun x : Cube n => x i = true) = Finset.univ := by ext x; cases x i <;> simp have hd : Disjoint (Finset.univ.filter (fun x : Cube n => x i = false)) (Finset.univ.filter (fun x : Cube n => x i = true)) := by rw [Finset.disjoint_filter]; intro x _ h1 h2; rw [h1] at h2; exact absurd h2 (by decide) have htot := Finset.card_union_of_disjoint hd rw [hu, Finset.card_univ, Fintype.card_fun, Fintype.card_bool, Fintype.card_fin, hflip] at htot have hpow : 2 ^ n = 2 * 2 ^ (n - 1) := by have : n = (n - 1) + 1 := by omega conv_lhs => rw [this, pow_succ, mul_comm] omega exact le_trans (Finset.card_le_card hsub) (le_of_eq hcard) theorem cubeTotalViol_le_edges {n : ℕ} (hn : 1 ≤ n) (f : Cube n → Bool) : cubeTotalViol f ≤ n * 2 ^ (n - 1) := by dsimp only [cubeTotalViol] calc ∑ i : Fin n, cubeViolDim f i ≤ ∑ _i : Fin n, 2 ^ (n - 1) := Finset.sum_le_sum (fun i _ => cubeViolDim_le_half_pow hn f i) _ = n * 2 ^ (n - 1) := by simp theorem cubeEdgeViolFrac_bounds {n : ℕ} (hn : 1 ≤ n) (f : Cube n → Bool) : 0 ≤ cubeEdgeViolFrac f ∧ cubeEdgeViolFrac f ≤ 1 := by dsimp only [cubeEdgeViolFrac] have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) refine ⟨by positivity, ?_⟩ rw [div_le_one (by positivity)] exact_mod_cast cubeTotalViol_le_edges hn f theorem switchOp_preserves_monotone {n : ℕ} (i : Fin n) {f : Cube n → Bool} (hf : IsCubeMonotone f) : switchOp i f = f := by funext x; dsimp only [switchOp] cases hxi : x i · simp only [Bool.false_eq_true, ↓reduceIte] have hle : ∀ k : Fin n, x k ≤ Function.update x i true k := fun k => by by_cases hk : k = i · subst hk; simp [hxi] · simp [hk] have hm := hf x (Function.update x i true) hle cases hfx : f x <;> cases hft : f (Function.update x i true) <;> simp_all [Bool.le_iff_imp] · simp only [↓reduceIte] have hle : ∀ k : Fin n, Function.update x i false k ≤ x k := fun k => by by_cases hk : k = i · subst hk; simp [hxi] · simp [hk] have hm := hf (Function.update x i false) x hle cases hfx : f x <;> cases hff : f (Function.update x i false) <;> simp_all [Bool.le_iff_imp] theorem switchOp_totalViol_le {n : ℕ} (i : Fin n) (f : Cube n → Bool) : cubeTotalViol (switchOp i f) ≤ cubeTotalViol f := by dsimp only [cubeTotalViol] refine Finset.sum_le_sum (fun j _ => ?_) by_cases hij : i = j · subst hij; rw [switchOp_fixes_dim]; exact Nat.zero_le _ · exact switchOp_monotone_other_dim hij f theorem gglrs_eps_far_edge_viol_lower_bound {n : ℕ} (hn : 1 ≤ n) {ε : ℝ} {f : Cube n → Bool} (hfar : ∀ h : Cube n → Bool, IsCubeMonotone h → ε ≤ (cubeHammingCount f h : ℝ) / (2 : ℝ) ^ n) : 2 * ε / (n : ℝ) ≤ cubeEdgeViolFrac f := by obtain ⟨h, hm, hle⟩ := gglrs_hypercube_edge_tester_soundness hn f have h1 := hfar h hm have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) rw [div_le_iff₀ hnpos] linarith /-! ### Part 6C: Extended Distribution Testing, Collisions & Paninski Lower Bounds -/ theorem uniformDist_isProbDist {n : ℕ} (hn : 1 ≤ n) : IsProbDist (uniformDist (Fin n)) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) dsimp only [IsProbDist, uniformDist] refine ⟨fun _ => by positivity, ?_⟩ simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] theorem uniformDist_l2Sq {n : ℕ} (hn : 1 ≤ n) : l2Sq (uniformDist (Fin n)) = 1 / (n : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) dsimp only [l2Sq, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] theorem uniformDist_l3Cube {n : ℕ} (hn : 1 ≤ n) : l3Cube (uniformDist (Fin n)) = 1 / (n : ℝ) ^ 2 := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) dsimp only [l3Cube, uniformDist] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] field_simp [ne_of_gt hnpos] theorem tvDist_nonneg {α : Type*} [Fintype α] (p q : α → ℝ) : 0 ≤ tvDist p q := by dsimp only [tvDist] exact mul_nonneg (by norm_num) (Finset.sum_nonneg (fun _ _ => abs_nonneg _)) theorem tvDist_self {α : Type*} [Fintype α] (p : α → ℝ) : tvDist p p = 0 := by dsimp only [tvDist]; simp theorem tvDist_symm {α : Type*} [Fintype α] (p q : α → ℝ) : tvDist p q = tvDist q p := by dsimp only [tvDist]; congr 1; exact Finset.sum_congr rfl (fun _ _ => abs_sub_comm _ _) theorem tvDist_triangle {α : Type*} [Fintype α] (p q r : α → ℝ) : tvDist p r ≤ tvDist p q + tvDist q r := by dsimp only [tvDist] rw [← mul_add, ← Finset.sum_add_distrib] exact mul_le_mul_of_nonneg_left (Finset.sum_le_sum (fun x _ => abs_sub_le (p x) (q x) (r x))) (by norm_num) theorem tvDist_le_one {α : Type*} [Fintype α] {p q : α → ℝ} (hp : IsProbDist p) (hq : IsProbDist q) : tvDist p q ≤ 1 := by dsimp only [tvDist] have hle : ∑ x : α, |p x - q x| ≤ 2 := by calc ∑ x : α, |p x - q x| ≤ ∑ x : α, (p x + q x) := Finset.sum_le_sum (fun x _ => by calc |p x - q x| ≤ |p x| + |q x| := abs_sub _ _ _ = p x + q x := by rw [abs_of_nonneg (hp.1 x), abs_of_nonneg (hq.1 x)]) _ = ∑ x : α, p x + ∑ x : α, q x := Finset.sum_add_distrib _ = 2 := by rw [hp.2, hq.2]; norm_num linarith theorem l2Sq_nonneg {α : Type*} [Fintype α] (p : α → ℝ) : 0 ≤ l2Sq p := Finset.sum_nonneg (fun _ _ => sq_nonneg _) theorem l2Sq_ge_inv_n {n : ℕ} (hn : 1 ≤ n) {p : Fin n → ℝ} (hp : IsProbDist p) : 1 / (n : ℝ) ≤ l2Sq p := by have h := (l3Cube_sub_l2Sq_sq_bound hn hp).1 linarith theorem l2Sq_le_one {α : Type*} [Fintype α] {p : α → ℝ} (hp : IsProbDist p) : l2Sq p ≤ 1 := by dsimp only [l2Sq] calc ∑ x : α, (p x) ^ 2 ≤ ∑ x : α, p x := Finset.sum_le_sum (fun x _ => by have h1 : p x ≤ 1 := by rw [← hp.2]; exact Finset.single_le_sum (fun y _ => hp.1 y) (Finset.mem_univ x) nlinarith [hp.1 x]) _ = 1 := hp.2 theorem l3Cube_le_l2Sq {α : Type*} [Fintype α] {p : α → ℝ} (hp : IsProbDist p) : l3Cube p ≤ l2Sq p := by dsimp only [l3Cube, l2Sq] refine Finset.sum_le_sum (fun x _ => ?_) have h1 : p x ≤ 1 := by rw [← hp.2]; exact Finset.single_le_sum (fun y _ => hp.1 y) (Finset.mem_univ x) nlinarith [hp.1 x, sq_nonneg (p x)] theorem samplePairs_card_zero : (samplePairs 0).card = 0 := by have h := samplePairs_card_two_mul 0; omega theorem samplePairs_card_one : (samplePairs 1).card = 0 := by have h := samplePairs_card_two_mul 1; omega theorem samplePairs_card_two : (samplePairs 2).card = 1 := by have h := samplePairs_card_two_mul 2; omega theorem empCollisions_nonneg {α : Type*} [DecidableEq α] {m : ℕ} (X : Fin m → α) : 0 ≤ empCollisions X := by dsimp only [empCollisions] exact Finset.sum_nonneg (fun _ _ => by split_ifs <;> norm_num) theorem empCollisions_le_pairs {α : Type*} [DecidableEq α] {m : ℕ} (X : Fin m → α) : empCollisions X ≤ ((samplePairs m).card : ℝ) := by dsimp only [empCollisions] calc ∑ ij ∈ samplePairs m, (if X ij.1 = X ij.2 then (1 : ℝ) else 0) ≤ ∑ _ij ∈ samplePairs m, (1 : ℝ) := Finset.sum_le_sum (fun _ _ => by split_ifs <;> norm_num) _ = ((samplePairs m).card : ℝ) := by simp theorem bfrsw_unbiased_uniform_exact {n m : ℕ} (hn : 1 ≤ n) : ∑ X : Fin m → Fin n, prodDist (uniformDist (Fin n)) X * empCollisions X = ((samplePairs m).card : ℝ) / (n : ℝ) := by rw [bfrsw_collision_estimator_unbiased (uniformDist_isProbDist hn), uniformDist_l2Sq hn] ring theorem bfrsw_completeness_sqrt_n_explicit {n m : ℕ} (hn : 1 ≤ n) (hm : 2 ≤ m) {ε : ℝ} (hε : 0 < ε) (hM : 3 * (n : ℝ) / (4 * ε ^ 4) ≤ ((samplePairs m).card : ℝ)) : let M : ℝ := ((samplePairs m).card : ℝ) let τ : ℝ := M * (1 / (n : ℝ) + 2 * ε ^ 2 / (n : ℝ)) ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X ≤ 1 / 3 := by intro M τ have hcomp := (bfrsw_uniformity_tester_chebyshev hn hm hε).1 have hMpos : 0 < M := lt_of_lt_of_le (by have : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega); positivity) hM have hmul : 3 * (n : ℝ) ≤ 4 * M * ε ^ 4 := by have h1 := (div_le_iff₀ (by positivity : (0 : ℝ) < 4 * ε ^ 4)).mp hM dsimp only [M]; linarith calc ∑ X ∈ Finset.univ.filter (fun X : Fin m → Fin n => τ ≤ empCollisions X), prodDist (uniformDist (Fin n)) X ≤ (n : ℝ) / (4 * M * ε ^ 4) := hcomp _ ≤ 1 / 3 := by rw [div_le_div_iff₀ (by positivity) (by norm_num : (0 : ℝ) < 3)] linarith theorem paninski_l2_excess_exact {k : ℕ} (hk : 1 ≤ k) (ε : ℝ) (z : Fin k → Bool) : l2Sq (paninskiDist ε z) = (1 + 4 * ε ^ 2) / ((2 * k : ℕ) : ℝ) := by have hkpos : (0 : ℝ) < (k : ℝ) := Nat.cast_pos.mpr (by omega) let e : (Fin k × Bool) ≃ Fin (2 * k) := { toFun := fun p => ⟨2 * p.1.1 + if p.2 then 1 else 0, by have := p.1.2; split_ifs <;> omega⟩ invFun := fun x => (⟨x.1 / 2, by omega⟩, decide (x.1 % 2 = 1)) left_inv := fun ⟨i, b⟩ => by cases b · exact Prod.ext (Fin.ext (by simp)) (by simp) · exact Prod.ext (Fin.ext (by simp; omega)) (by simp [show (2 * i.1 + 1) % 2 = 1 by omega]) right_inv := fun x => by apply Fin.ext; dsimp only by_cases h : x.1 % 2 = 1 <;> (simp [h]; omega) } dsimp only [l2Sq] rw [← Equiv.sum_comp e (fun x => (paninskiDist ε z x) ^ 2), Fintype.sum_prod_type] have hpair : ∀ i : Fin k, ∑ b : Bool, (paninskiDist ε z (e (i, b))) ^ 2 = (1 + 4 * ε ^ 2) / (2 * (k : ℝ) ^ 2) := by intro i simp only [Fintype.sum_bool, e, Equiv.coe_fn_mk, Bool.false_eq_true, ↓reduceIte, add_zero, paninskiDist] have hev : (2 * i.1) % 2 = 0 := by omega have hod : ¬ ((2 * i.1 + 1) % 2 = 0) := by omega have hdiv1 : (⟨(2 * i.1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) have hdiv2 : (⟨(2 * i.1 + 1) / 2, by omega⟩ : Fin k) = i := Fin.ext (by dsimp only; omega) simp only [hev, hod, ↓reduceIte, hdiv1, hdiv2] cases z i <;> (simp only [Bool.false_eq_true, ↓reduceIte]; push_cast; field_simp [ne_of_gt hkpos]; ring) simp_rw [hpair] simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] push_cast; field_simp [ne_of_gt hkpos] theorem paninski_no_sub_sqrt_tester {k m : ℕ} (hk : 1 ≤ k) {ε : ℝ} (h0 : 0 ≤ ε) (hhalf : ε ≤ 1 / 2) (hsmall : 16 * (m : ℝ) ^ 2 * ε ^ 4 / ((2 * k : ℕ) : ℝ) < Real.log (13 / 9)) : ¬ ∃ T : (Fin m → Fin (2 * k)) → Bool, ((2 / 3 : ℝ) ≤ ∑ X, if T X = true then prodDist (uniformDist (Fin (2 * k))) X else 0) ∧ (∀ z : Fin k → Bool, (∑ X, if T X = true then prodDist (paninskiDist ε z) X else 0) ≤ 1 / 3) := by rintro ⟨T, hcomp, hsound⟩ have hlb := (paninski_ingster_uniformity_lower_bound (m := m) hk h0 hhalf).2.2 T hcomp hsound linarith /-! ### Part 6D: Extended Graph Property Testing, Yao Minimax & Turán/Mantel -/ theorem yao_double_expectation_swap {A X : Type*} [Fintype A] [Fintype X] (cost : A → X → ℝ) (μ : A → ℝ) (ν : X → ℝ) : ∑ a : A, μ a * (∑ x : X, ν x * cost a x) = ∑ x : X, ν x * (∑ a : A, μ a * cost a x) := by simp_rw [Finset.mul_sum]; rw [Finset.sum_comm] exact Finset.sum_congr rfl (fun _ _ => Finset.sum_congr rfl (fun _ _ => by ring)) theorem yao_point_mass_isProbDist {α : Type*} [Fintype α] [DecidableEq α] (a₀ : α) : IsProbDist (fun a : α => if a = a₀ then (1 : ℝ) else 0) := by refine ⟨fun a => by dsimp only; split_ifs <;> norm_num, ?_⟩ rw [Finset.sum_eq_single a₀] · simp · intro a _ ha; simp [ha] · intro h; exact absurd (Finset.mem_univ a₀) h theorem nonadaptive_full_queries_necessary {n : ℕ} (Q : Finset (Fin n)) (hdist : let thresh : Fin (n + 1) → Fin n → Bool := fun k i => decide (k.1 ≤ i.1) ∀ k₁ k₂ : Fin (n + 1), (∀ i ∈ Q, thresh k₁ i = thresh k₂ i) → k₁ = k₂) : Q.card = n := by by_contra hne have hle : Q.card ≤ n := le_trans (Finset.card_le_univ Q) (by simp) have hlt : Q.card < n := lt_of_le_of_ne hle hne obtain ⟨k₁, k₂, hneq, hag⟩ := nonadaptive_vs_adaptive_threshold_separation Q hlt exact hneq (hdist k₁ k₂ hag) theorem graphDeg_le_n {n : ℕ} (G : SimpleGraphAdj n) (u : Fin n) : graphDeg G u ≤ n := by dsimp only [graphDeg] exact le_trans (Finset.card_le_univ _) (by simp) theorem graphDeg_lt_n {n : ℕ} (hn : 1 ≤ n) (G : SimpleGraphAdj n) (u : Fin n) : graphDeg G u < n := by dsimp only [graphDeg] have hsub : Finset.univ.filter (fun v : Fin n => G.adj u v = true) ⊆ Finset.univ.erase u := by intro v hv refine Finset.mem_erase.mpr ⟨fun h => ?_, Finset.mem_univ v⟩ subst h have := (Finset.mem_filter.mp hv).2 rw [G.loopless v] at this; exact absurd this (by decide) have hle := Finset.card_le_card hsub rw [Finset.card_erase_of_mem (Finset.mem_univ u), Finset.card_univ, Fintype.card_fin] at hle omega theorem twiceEdges_le_n_sq {n : ℕ} (G : SimpleGraphAdj n) : twiceEdges G ≤ n * n := by dsimp only [twiceEdges] calc ∑ u : Fin n, graphDeg G u ≤ ∑ _u : Fin n, n := Finset.sum_le_sum (fun u _ => graphDeg_le_n G u) _ = n * n := by simp theorem twiceEdges_le_n_mul_pred {n : ℕ} (G : SimpleGraphAdj n) : twiceEdges G ≤ n * (n - 1) := by cases n with | zero => simp [twiceEdges] | succ k => dsimp only [twiceEdges] calc ∑ u : Fin (k + 1), graphDeg G u ≤ ∑ _u : Fin (k + 1), k := Finset.sum_le_sum (fun u _ => by have := graphDeg_lt_n (by omega) G u; omega) _ = (k + 1) * k := by simp theorem edgeDensity_bounds {n : ℕ} (G : SimpleGraphAdj n) : 0 ≤ edgeDensity G ∧ edgeDensity G ≤ 1 := by dsimp only [edgeDensity] refine ⟨by positivity, ?_⟩ by_cases hn : n = 0 · subst hn; simp · have hnpos : (0 : ℝ) < (n : ℝ) ^ 2 := by have : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) positivity rw [div_le_one hnpos] have hle : (twiceEdges G : ℝ) ≤ ((n * n : ℕ) : ℝ) := Nat.cast_le.mpr (twiceEdges_le_n_sq G) push_cast at hle; linarith [sq (n : ℝ)] theorem emptyGraph_properties (n : ℕ) : let G : SimpleGraphAdj n := { adj := fun _ _ => false, symm := fun _ _ => rfl, loopless := fun _ => rfl } IsTriangleFree G ∧ IsBipartite G ∧ twiceEdges G = 0 ∧ edgeDensity G = 0 := by intro G refine ⟨fun _ _ _ h => by dsimp only [G] at h; exact absurd h (by decide), ⟨fun _ => false, fun _ _ h => by dsimp only [G] at h; exact absurd h (by decide)⟩, ?_, ?_⟩ · dsimp only [twiceEdges, graphDeg, G]; simp · dsimp only [edgeDensity, twiceEdges, graphDeg, G]; simp theorem completeGraph_twiceEdges (n : ℕ) : let K : SimpleGraphAdj n := { adj := fun u v => decide (u ≠ v) symm := fun u v => by simp [ne_comm] loopless := fun u => by simp } twiceEdges K = n * (n - 1) := by intro K have hdeg : ∀ u : Fin n, graphDeg K u = n - 1 := by intro u; dsimp only [graphDeg, K] have hfilt : Finset.univ.filter (fun v : Fin n => decide (u ≠ v) = true) = Finset.univ.erase u := by ext v; simp [ne_comm] rw [hfilt, Finset.card_erase_of_mem (Finset.mem_univ u), Finset.card_univ, Fintype.card_fin] dsimp only [twiceEdges]; simp_rw [hdeg]; simp theorem complete_bipartite_isBipartite (k : ℕ) : let G : SimpleGraphAdj (2 * k) := { adj := fun u v => decide ((u.1 < k ∧ k ≤ v.1) ∨ (k ≤ u.1 ∧ v.1 < k)) symm := fun u v => by simp [or_comm, and_comm] loopless := fun u => by simp } IsBipartite G := by intro G refine ⟨fun u => decide (u.1 < k), fun u v huv => ?_⟩ dsimp only [G] at huv simp only [decide_eq_true_eq] at huv rcases huv with ⟨h1, h2⟩ | ⟨h1, h2⟩ · simp [h1, show ¬ (v.1 < k) by omega] · simp [show ¬ (u.1 < k) by omega, h2] theorem k3_not_triangle_free_and_not_bipartite : let K3 : SimpleGraphAdj 3 := { adj := fun u v => decide (u ≠ v) symm := fun u v => by simp [ne_comm] loopless := fun u => by simp } ¬ IsTriangleFree K3 ∧ ¬ IsBipartite K3 ∧ sixTriangles K3 = 6 := by intro K3 have hntf : ¬ IsTriangleFree K3 := by intro htf have h := htf ⟨0, by omega⟩ ⟨1, by omega⟩ ⟨2, by omega⟩ (by decide) (by decide) exact absurd h (by decide) refine ⟨hntf, fun hb => hntf (bipartite_implies_triangle_free_and_mantel hb).1, ?_⟩ dsimp only [sixTriangles, K3] decide theorem mantel_edge_density_le_half {n : ℕ} (hn : 1 ≤ n) {G : SimpleGraphAdj n} (htf : IsTriangleFree G) : edgeDensity G ≤ 1 / 2 := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hm := (mantel_and_moon_moser_supersaturation G).2 htf have hmR : 2 * (twiceEdges G : ℝ) ≤ (n : ℝ) ^ 2 := by have : ((2 * twiceEdges G : ℕ) : ℝ) ≤ ((n * n : ℕ) : ℝ) := Nat.cast_le.mpr hm push_cast at this; linarith [sq (n : ℝ)] dsimp only [edgeDensity] rw [div_le_div_iff₀ (by positivity) (by norm_num : (0 : ℝ) < 2)] linarith theorem sixTriangles_zero_iff_triangleFree {n : ℕ} (G : SimpleGraphAdj n) : sixTriangles G = 0 ↔ IsTriangleFree G := by constructor · intro h0 u v w huv hvw by_contra hne have huw : G.adj u w = true := by cases h : G.adj u w <;> simp_all dsimp only [sixTriangles] at h0 have h1 := Finset.single_le_sum (f := fun w' : Fin n => if G.adj u v = true ∧ G.adj v w' = true ∧ G.adj u w' = true then 1 else 0) (fun _ _ => Nat.zero_le _) (Finset.mem_univ w) have h2 := Finset.single_le_sum (f := fun v' : Fin n => ∑ w' : Fin n, if G.adj u v' = true ∧ G.adj v' w' = true ∧ G.adj u w' = true then 1 else 0) (fun _ _ => Nat.zero_le _) (Finset.mem_univ v) have h3 := Finset.single_le_sum (f := fun u' : Fin n => ∑ v' : Fin n, ∑ w' : Fin n, if G.adj u' v' = true ∧ G.adj v' w' = true ∧ G.adj u' w' = true then 1 else 0) (fun _ _ => Nat.zero_le _) (Finset.mem_univ u) have hval : (if G.adj u v = true ∧ G.adj v w = true ∧ G.adj u w = true then 1 else 0) = 1 := by simp [huv, hvw, huw] rw [hval] at h1 omega · intro htf dsimp only [sixTriangles] refine Finset.sum_eq_zero (fun u _ => Finset.sum_eq_zero (fun v _ => Finset.sum_eq_zero (fun w _ => ?_))) split_ifs with h · exact absurd h.2.2 (by rw [htf u v w h.1 h.2.1]; decide) · rfl theorem moon_moser_strict_excess_implies_triangle {n : ℕ} {G : SimpleGraphAdj n} (hex : n * n < 2 * twiceEdges G) : 0 < sixTriangles G := by by_contra h0 have htf : IsTriangleFree G := (sixTriangles_zero_iff_triangleFree G).mp (by omega) have hm := (mantel_and_moon_moser_supersaturation G).2 htf omega theorem moon_moser_triangle_lower_bound_real {n : ℕ} (hn : 1 ≤ n) (G : SimpleGraphAdj n) : ((twiceEdges G : ℝ) ^ 2) / (n : ℝ) ≤ (n : ℝ) * (twiceEdges G : ℝ) + (sixTriangles G : ℝ) := by have hnpos : (0 : ℝ) < (n : ℝ) := Nat.cast_pos.mpr (by omega) have hsup := (mantel_and_moon_moser_supersaturation G).1 have hsupR : ∑ u : Fin n, ((graphDeg G u : ℝ)) ^ 2 ≤ (n : ℝ) * (twiceEdges G : ℝ) + (sixTriangles G : ℝ) := by have hcast : ((∑ u : Fin n, (graphDeg G u) ^ 2 : ℕ) : ℝ) ≤ ((n * twiceEdges G + sixTriangles G : ℕ) : ℝ) := Nat.cast_le.mpr hsup push_cast at hcast; exact hcast have hcs := Finset.sum_mul_sq_le_sq_mul_sq (Finset.univ : Finset (Fin n)) (fun _ => (1 : ℝ)) (fun u => (graphDeg G u : ℝ)) simp only [one_mul, one_pow, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul, mul_one] at hcs have hte : ((twiceEdges G : ℝ)) ^ 2 ≤ (n : ℝ) * ∑ u : Fin n, ((graphDeg G u : ℝ)) ^ 2 := by dsimp only [twiceEdges]; rw [Nat.cast_sum]; exact hcs rw [div_le_iff₀ hnpos] nlinarith [hte, hsupR] theorem triangle_free_subgraph_hereditary {n : ℕ} {G₁ G₂ : SimpleGraphAdj n} (hsub : ∀ u v, G₁.adj u v = true → G₂.adj u v = true) (htf : IsTriangleFree G₂) : IsTriangleFree G₁ := by intro u v w huv hvw have h2 := htf u v w (hsub u v huv) (hsub v w hvw) cases h1 : G₁.adj u w · rfl · exact absurd (hsub u w h1) (by rw [h2]; decide) theorem bipartite_subgraph_hereditary {n : ℕ} {G₁ G₂ : SimpleGraphAdj n} (hsub : ∀ u v, G₁.adj u v = true → G₂.adj u v = true) (hb : IsBipartite G₂) : IsBipartite G₁ := by obtain ⟨c, hc⟩ := hb exact ⟨c, fun u v huv => hc u v (hsub u v huv)⟩ theorem graph_edge_density_eps_sample_bound {n s : ℕ} (hn : 1 ≤ n) (hs : 1 ≤ s) (G : SimpleGraphAdj n) {ε : ℝ} (hε : 0 < ε) (hsample : 3 / (4 * ε ^ 2) ≤ (s : ℝ)) : let N : ℝ := (Fintype.card (Fin s → Fin n × Fin n) : ℝ) let est : (Fin s → Fin n × Fin n) → ℝ := fun S => ((1 : ℝ) / (s : ℝ)) * ∑ t : Fin s, if G.adj (S t).1 (S t).2 = true then (1 : ℝ) else 0 ((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N ≤ 1 / 3 := by intro N est have hcheb := (graph_edge_density_sublinear_chebyshev hn hs G hε).2 have hspos : (0 : ℝ) < (s : ℝ) := Nat.cast_pos.mpr (by omega) have hmul : 3 ≤ 4 * (s : ℝ) * ε ^ 2 := by have h1 := (div_le_iff₀ (by positivity : (0 : ℝ) < 4 * ε ^ 2)).mp hsample linarith calc ((Finset.univ.filter (fun S : Fin s → Fin n × Fin n => ε ≤ |est S - edgeDensity G|)).card : ℝ) / N ≤ 1 / (4 * (s : ℝ) * ε ^ 2) := hcheb _ ≤ 1 / 3 := by rw [div_le_div_iff₀ (by positivity) (by norm_num : (0 : ℝ) < 3)] linarith theorem complete_bipartite_density_half {k : ℕ} (hk : 1 ≤ k) : let G : SimpleGraphAdj (2 * k) := { adj := fun u v => decide ((u.1 < k ∧ k ≤ v.1) ∨ (k ≤ u.1 ∧ v.1 < k)) symm := fun u v => by simp [or_comm, and_comm] loopless := fun u => by simp } edgeDensity G = 1 / 2 := by intro G have he := (mantel_complete_bipartite_exact_tight k).2 have h2kpos : (0 : ℝ) < ((2 * k : ℕ) : ℝ) := Nat.cast_pos.mpr (by omega) have heR : 2 * (twiceEdges G : ℝ) = ((2 * k : ℕ) : ℝ) ^ 2 := by have : ((2 * twiceEdges G : ℕ) : ℝ) = (((2 * k) * (2 * k) : ℕ) : ℝ) := congrArg Nat.cast he push_cast at this ⊢; linarith dsimp only [edgeDensity] rw [div_eq_iff (by positivity : ((2 * k : ℕ) : ℝ) ^ 2 ≠ 0)] linarith /-! ### Part 6E: Extended Dictator / `k`-Junta Testing & Noise-Stability Calculus -/ theorem hammingWt_zero (n : ℕ) : hammingWt (0 : BitVec2 n) = 0 := by dsimp only [hammingWt]; simp theorem hammingWt_unit {n : ℕ} (i : Fin n) : hammingWt (f2Unit i) = 1 := by dsimp only [hammingWt] have hfilt : Finset.univ.filter (fun k : Fin n => f2Unit i k ≠ 0) = {i} := by ext k; simp only [Finset.mem_singleton, f2Unit] by_cases hk : k = i · subst hk; simp · simp [hk] rw [hfilt, Finset.card_singleton] theorem hammingWt_allOnes (n : ℕ) : hammingWt (fun _ : Fin n => (1 : ZMod 2)) = n := by dsimp only [hammingWt]; simp theorem hammingWt_le_n {n : ℕ} (S : BitVec2 n) : hammingWt S ≤ n := by dsimp only [hammingWt] exact le_trans (Finset.card_le_univ _) (by simp) theorem hammingWt_eq_zero_iff {n : ℕ} (S : BitVec2 n) : hammingWt S = 0 ↔ S = 0 := by dsimp only [hammingWt] rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff] constructor · intro h; funext i; by_contra hne; exact h (Finset.mem_univ i) hne · intro h i _; subst h; simp theorem noiseOp_linear_add {n : ℕ} (ρ : ℝ) (f g : BitVec2 n → ℝ) (x : BitVec2 n) : noiseOp ρ (fun y => f y + g y) x = noiseOp ρ f x + noiseOp ρ g x := by dsimp only [noiseOp] simp_rw [mul_add, Finset.sum_add_distrib] theorem noiseOp_smul {n : ℕ} (ρ c : ℝ) (f : BitVec2 n → ℝ) (x : BitVec2 n) : noiseOp ρ (fun y => c * f y) x = c * noiseOp ρ f x := by dsimp only [noiseOp] have hpt : ∀ e : BitVec2 n, noiseWeight ρ e * (c * f (x + e)) = c * (noiseWeight ρ e * f (x + e)) := fun _ => by ring simp_rw [hpt, ← Finset.mul_sum] theorem noiseOp_id_at_one {n : ℕ} (f : BitVec2 n → ℝ) : noiseOp 1 f = f := by funext x rw [fourier_inversion (noiseOp 1 f) x, fourier_inversion f x] refine Finset.sum_congr rfl (fun S _ => ?_) rw [noiseOp_fourierCoeff, one_pow, one_mul] theorem noiseOp_const_at_zero {n : ℕ} (f : BitVec2 n → ℝ) (x : BitVec2 n) : noiseOp 0 f x = fourierCoeff f 0 := by rw [fourier_inversion (noiseOp 0 f) x] simp_rw [noiseOp_fourierCoeff] rw [Finset.sum_eq_single (0 : BitVec2 n)] · rw [hammingWt_zero, pow_zero, one_mul, chi_zero_left, mul_one] · intro S _ hS have hwt : hammingWt S ≠ 0 := fun h => hS ((hammingWt_eq_zero_iff S).mp h) rw [zero_pow hwt]; ring · intro h; exact absurd (Finset.mem_univ _) h theorem noiseOp_semigroup {n : ℕ} (ρ₁ ρ₂ : ℝ) (f : BitVec2 n → ℝ) : noiseOp ρ₁ (noiseOp ρ₂ f) = noiseOp (ρ₁ * ρ₂) f := by funext x rw [fourier_inversion (noiseOp ρ₁ (noiseOp ρ₂ f)) x, fourier_inversion (noiseOp (ρ₁ * ρ₂) f) x] refine Finset.sum_congr rfl (fun S _ => ?_) rw [noiseOp_fourierCoeff, noiseOp_fourierCoeff, noiseOp_fourierCoeff, mul_pow]; ring theorem noiseOp_chi_eigenfunction {n : ℕ} (ρ : ℝ) (S x : BitVec2 n) : noiseOp ρ (chi S) x = ρ ^ hammingWt S * chi S x := by rw [fourier_inversion (noiseOp ρ (chi S)) x] simp_rw [noiseOp_fourierCoeff, fourierCoeff_chi] rw [Finset.sum_eq_single S] · simp · intro T _ hT; simp [hT] · intro h; exact absurd (Finset.mem_univ S) h theorem noiseStab_at_one_eq_l2 {n : ℕ} (f : BitVec2 n → ℝ) : noiseStab 1 f = ((1 : ℝ) / (2 : ℝ) ^ n) * ∑ x : BitVec2 n, (f x) ^ 2 := by rw [noiseStab_fourier_formula, ← fourier_parseval] refine Finset.sum_congr rfl (fun _ _ => by simp) theorem noiseStab_at_zero_eq_mean_sq {n : ℕ} (f : BitVec2 n → ℝ) : noiseStab 0 f = (fourierCoeff f 0) ^ 2 := by rw [noiseStab_fourier_formula] rw [Finset.sum_eq_single (0 : BitVec2 n)] · rw [hammingWt_zero, pow_zero, one_mul] · intro S _ hS have hwt : hammingWt S ≠ 0 := fun h => hS ((hammingWt_eq_zero_iff S).mp h) rw [zero_pow hwt, zero_mul] · intro h; exact absurd (Finset.mem_univ _) h theorem noiseStab_nonneg_of_nonneg {n : ℕ} {ρ : ℝ} (hρ : 0 ≤ ρ) (f : BitVec2 n → ℝ) : 0 ≤ noiseStab ρ f := by rw [noiseStab_fourier_formula] exact Finset.sum_nonneg (fun S _ => mul_nonneg (pow_nonneg hρ _) (sq_nonneg _)) theorem noiseStab_le_one_pmOne {n : ℕ} {ρ : ℝ} (hρ0 : 0 ≤ ρ) (hρ1 : ρ ≤ 1) {f : BitVec2 n → ℝ} (hf : IsPmOne f) : noiseStab ρ f ≤ 1 := by rw [noiseStab_fourier_formula, ← fourier_parseval_pmOne hf] refine Finset.sum_le_sum (fun S _ => ?_) have h1 : ρ ^ hammingWt S ≤ 1 := pow_le_one₀ hρ0 hρ1 nlinarith [sq_nonneg (fourierCoeff f S)] theorem noiseStab_dictator_eq_rho {n : ℕ} (ρ : ℝ) (i : Fin n) : noiseStab ρ (chi (f2Unit i)) = ρ := by rw [noiseStab_fourier_formula] simp_rw [fourierCoeff_chi] rw [Finset.sum_eq_single (f2Unit i)] · rw [if_pos rfl, hammingWt_unit]; ring · intro S _ hS; simp [hS] · intro h; exact absurd (Finset.mem_univ _) h theorem noiseStab_parity_eq_rho_pow {n : ℕ} (ρ : ℝ) : noiseStab ρ (chi (fun _ : Fin n => (1 : ZMod 2))) = ρ ^ n := by rw [noiseStab_fourier_formula] simp_rw [fourierCoeff_chi] rw [Finset.sum_eq_single (fun _ : Fin n => (1 : ZMod 2))] · rw [if_pos rfl, hammingWt_allOnes]; ring · intro S _ hS; simp [hS] · intro h; exact absurd (Finset.mem_univ _) h theorem dictatorTestAcc_at_one_eq_blrAcc {n : ℕ} {f : BitVec2 n → ℝ} (hf : IsPmOne f) : dictatorTestAcc 1 f = 1 - blrRej f := by rw [hastad_dictator_test_fourier_acceptance, blr_fourier_rejection_identity hf] have h1 : ∑ S : BitVec2 n, (1 : ℝ) ^ hammingWt S * (fourierCoeff f S) ^ 3 = ∑ S : BitVec2 n, (fourierCoeff f S) ^ 3 := Finset.sum_congr rfl (fun _ _ => by simp) rw [h1]; ring theorem const_is_0_junta {n : ℕ} (c : ℝ) : IsKJunta (fun _ : BitVec2 n => c) 0 := ⟨∅, le_rfl, fun _ _ _ => rfl⟩ theorem dictator_is_1_junta {n : ℕ} (i : Fin n) : IsKJunta (chi (f2Unit i)) 1 := by refine ⟨{i}, by simp, fun x y hxy => ?_⟩ have hi : x i = y i := hxy i (Finset.mem_singleton_self i) dsimp only [chi] rw [f2Dot_comm (f2Unit i) x, f2Dot_comm (f2Unit i) y, f2Dot_unit, f2Dot_unit, hi] theorem kJunta_mono_and_parity_nonJunta {n : ℕ} : (∀ {k₁ k₂ : ℕ} {f : BitVec2 n → ℝ}, k₁ ≤ k₂ → IsKJunta f k₁ → IsKJunta f k₂) ∧ (∀ {k : ℕ}, k < n → ¬ IsKJunta (chi (fun _ : Fin n => (1 : ZMod 2))) k) := by refine ⟨fun hle ⟨J, hJc, hJd⟩ => ⟨J, le_trans hJc hle, hJd⟩, fun hlt hj => ?_⟩ have hdist := (kJunta_fourier_degree_and_parity_distance hj).2.2 hlt (chi_isPmOne _) rw [boolDist_self] at hdist norm_num at hdist end PropertyTestingSublinearMinimax /-! =============================================================================== PART 7: COMPLETE AXIOM AUDIT (100 VERIFIED DECLARATIONS ACROSS ALL 5 PILLARS) =============================================================================== -/ open PropertyTestingSublinearMinimax #print axioms property_testing_sublinear_minimax_grand_unification #print axioms fourier_inversion #print axioms fourier_parseval #print axioms fourier_parseval_pmOne #print axioms boolDist_chi_eq #print axioms distToLinear_eq_min_boolDist #print axioms blr_fourier_rejection_identity #print axioms blr_soundness_ge_dist #print axioms blr_completeness_linear #print axioms blr_neg_linear_witness #print axioms blr_self_correction_error_le_two_delta #print axioms blr_self_correction_majority_gt_half #print axioms distToLinear_le_half_of_pos #print axioms blrRej_bounds #print axioms fourierCoeff_abs_le_one #print axioms chi_orthogonality #print axioms chi_sum_eq #print axioms fourierCoeff_chi #print axioms fourierCoeff_const #print axioms line_bsCheck_good_pair_monotone #print axioms line_binary_search_monotonicity_soundness #print axioms line_bs_zero_rejection_on_monotone #print axioms line_bs_const_passes #print axioms line_bs_distance_le_rejection_frac #print axioms decision_tree_pigeonhole_lower_bound #print axioms decision_tree_lower_bound_pow2_plus_one #print axioms gglrs_cube_hamming_le_total_viol #print axioms gglrs_hypercube_edge_tester_soundness #print axioms gglrs_anti_dictator_exact_tight #print axioms gglrs_eps_far_edge_viol_lower_bound #print axioms switchOp_fixes_dim #print axioms switchOp_monotone_other_dim #print axioms switchOp_preserves_monotone #print axioms switchOp_totalViol_le #print axioms cube_monotone_and #print axioms cube_monotone_or #print axioms cube_monotone_zero_edgeViolFrac #print axioms cubeHammingCount_triangle #print axioms cubeViolDim_le_half_pow #print axioms cubeEdgeViolFrac_bounds #print axioms prodDist_isProbDist #print axioms l2_excess_ge_four_tv_sq_div_n #print axioms samplePairs_card_two_mul #print axioms prodDist_pair_coll_expectation #print axioms bfrsw_collision_estimator_unbiased #print axioms prodDist_triple_coll_expectation #print axioms prodDist_disjoint_pairs_expectation #print axioms l3Cube_sub_l2Sq_sq_bound #print axioms bfrsw_collision_variance_bound #print axioms bfrsw_uniformity_tester_chebyshev #print axioms bfrsw_unbiased_uniform_exact #print axioms bfrsw_completeness_sqrt_n_explicit #print axioms paninskiDist_valid_and_eps_far #print axioms tvDist_le_half_sqrt_chi2 #print axioms paninski_ingster_uniformity_lower_bound #print axioms paninski_l2_excess_exact #print axioms paninski_no_sub_sqrt_tester #print axioms uniformDist_isProbDist #print axioms uniformDist_l2Sq #print axioms tvDist_triangle #print axioms tvDist_le_one #print axioms l2Sq_ge_inv_n #print axioms l2Sq_le_one #print axioms l3Cube_le_l2Sq #print axioms yao_minimax_principle #print axioms yao_double_expectation_swap #print axioms yao_point_mass_isProbDist #print axioms nonadaptive_vs_adaptive_threshold_separation #print axioms nonadaptive_full_queries_necessary #print axioms mantel_and_moon_moser_supersaturation #print axioms mantel_complete_bipartite_exact_tight #print axioms mantel_edge_density_le_half #print axioms moon_moser_strict_excess_implies_triangle #print axioms moon_moser_triangle_lower_bound_real #print axioms sixTriangles_zero_iff_triangleFree #print axioms graph_edge_density_sublinear_chebyshev #print axioms graph_edge_density_eps_sample_bound #print axioms bipartite_implies_triangle_free_and_mantel #print axioms complete_bipartite_isBipartite #print axioms complete_bipartite_density_half #print axioms k3_not_triangle_free_and_not_bipartite #print axioms completeGraph_twiceEdges #print axioms emptyGraph_properties #print axioms edgeDensity_bounds #print axioms noiseWeight_isProbDist #print axioms noiseWeight_fourier_eigenvalue #print axioms noiseOp_fourierCoeff #print axioms noiseStab_fourier_formula #print axioms hastad_dictator_test_fourier_acceptance #print axioms hastad_dictator_completeness #print axioms hastad_dictator_soundness_bounds #print axioms hastad_dictator_vs_parity_separation #print axioms hastad_dictator_vs_maj3_separation #print axioms kJunta_fourier_degree_and_parity_distance #print axioms noiseOp_id_at_one #print axioms noiseOp_const_at_zero #print axioms noiseOp_semigroup #print axioms noiseOp_chi_eigenfunction #print axioms dictatorTestAcc_at_one_eq_blrAcc #print axioms kJunta_mono_and_parity_nonJunta