import Mathlib namespace OAI namespace IndependentSetsGames.Foundations.PCP.PoweringDecoding open scoped BigOperators variable {Ω A : Type*} [Fintype Ω] [Nonempty Ω] [Fintype A] [Nonempty A] noncomputable def mass (opinion : Ω → A) (a : A) : ℝ := by classical exact Finset.univ.expect (fun ω => if opinion ω = a then 1 else 0) omit [Nonempty A] in theorem mass_sum (opinion : Ω → A) : (∑ a, mass opinion a) = 1 := by classical calc (∑ a, mass opinion a) = (Finset.univ : Finset Ω).expect (fun ω => ∑ a : A, if opinion ω = a then (1 : ℝ) else 0) := (Finset.expect_sum_comm (Finset.univ : Finset Ω) (Finset.univ : Finset A) (fun ω a => if opinion ω = a then (1 : ℝ) else 0)).symm _ = (Finset.univ : Finset Ω).expect (fun _ => (1 : ℝ)) := by apply Finset.expect_congr rfl intro ω _ simp _ = 1 := Finset.expect_const Finset.univ_nonempty 1 omit [Nonempty Ω] in theorem exists_mode (opinion : Ω → A) : ∃ a : A, ∀ b : A, mass opinion b ≤ mass opinion a := by obtain ⟨a, _, ha⟩ := Finset.exists_max_image (Finset.univ : Finset A) (mass opinion) Finset.univ_nonempty exact ⟨a, fun b => ha b (Finset.mem_univ b)⟩ noncomputable def mode (opinion : Ω → A) : A := Classical.choose (exists_mode opinion) omit [Nonempty Ω] in theorem mode_max (opinion : Ω → A) (b : A) : mass opinion b ≤ mass opinion (mode opinion) := Classical.choose_spec (exists_mode opinion) b theorem mode_mass_lower (opinion : Ω → A) : 1 / (Fintype.card A : ℝ) ≤ mass opinion (mode opinion) := by have hk : 0 < (Fintype.card A : ℝ) := Nat.cast_pos.mpr Fintype.card_pos have hsum : (1 : ℝ) ≤ mass opinion (mode opinion) * (Fintype.card A : ℝ) := by calc (1 : ℝ) = ∑ a : A, mass opinion a := (mass_sum opinion).symm _ ≤ ∑ _a : A, mass opinion (mode opinion) := Finset.sum_le_sum (fun a _ => mode_max opinion a) _ = mass opinion (mode opinion) * (Fintype.card A : ℝ) := by simp [mul_comm] exact (div_le_iff₀ hk).2 hsum theorem exists_letter_mass_lower (opinion : Ω → A) : ∃ a : A, 1 / (Fintype.card A : ℝ) ≤ mass opinion a := ⟨mode opinion, mode_mass_lower opinion⟩ end IndependentSetsGames.Foundations.PCP.PoweringDecoding noncomputable section namespace IndependentSetsGames.Foundations.PCP.PoweringMoment open scoped BigOperators def bit (p : Prop) : ℝ := by classical exact if p then 1 else 0 def hits {I Ω : Type*} [Fintype I] (E : I → Ω → Prop) (ω : Ω) : ℝ := ∑ i, bit (E i ω) def mean {Ω : Type*} [Fintype Ω] (f : Ω → ℝ) : ℝ := Finset.univ.expect f theorem bit_nonneg (p : Prop) : 0 ≤ bit p := by classical by_cases hp : p <;> simp [bit, hp] theorem bit_sq (p : Prop) : bit p ^ 2 = bit p := by classical by_cases hp : p <;> simp [bit, hp] theorem bit_mul (p q : Prop) : bit p * bit q = bit (p ∧ q) := by classical by_cases hp : p <;> by_cases hq : q <;> simp [bit, hp, hq] theorem bit_mono {p q : Prop} (hpq : p → q) : bit p ≤ bit q := by classical by_cases hp : p · simp [bit, hp, hpq hp] · have hz : bit p = 0 := by simp [bit, hp] rw [hz] exact bit_nonneg q theorem hits_nonneg {I Ω : Type*} [Fintype I] (E : I → Ω → Prop) (ω : Ω) : 0 ≤ hits E ω := Finset.sum_nonneg (fun i _ => bit_nonneg (E i ω)) theorem mean_nonneg {Ω : Type*} [Fintype Ω] (f : Ω → ℝ) (hf : ∀ ω, 0 ≤ f ω) : 0 ≤ mean f := Finset.expect_nonneg (fun ω _ => hf ω) theorem mean_hits {I Ω : Type*} [Fintype I] [Fintype Ω] (E : I → Ω → Prop) : mean (hits E) = ∑ i, mean (fun ω => bit (E i ω)) := by exact Finset.expect_sum_comm _ _ _ theorem second_moment_eq {I Ω : Type*} [Fintype I] [Fintype Ω] (E : I → Ω → Prop) : mean (fun ω => hits E ω ^ 2) = ∑ i, ∑ j, mean (fun ω => bit (E i ω ∧ E j ω)) := by simp only [hits, pow_two, Finset.sum_mul, Finset.mul_sum, bit_mul, mean, Finset.expect_sum_comm] exact Finset.sum_comm theorem mean_witness_sq_le {I Ω : Type*} [Fintype I] [Fintype Ω] (E W : I → Ω → Prop) (reject : Ω → Prop) (hWE : ∀ i ω, W i ω → E i ω) (hWR : ∀ i ω, W i ω → reject ω) : mean (hits W) ^ 2 ≤ mean (fun ω => bit (reject ω)) * mean (fun ω => hits E ω ^ 2) := by classical have hNH (ω : Ω) : hits W ω ≤ hits E ω := Finset.sum_le_sum (fun i _ => bit_mono (hWE i ω)) have hzero (ω : Ω) (hr : ¬ reject ω) : hits W ω = 0 := by apply Finset.sum_eq_zero intro i _ have hw : ¬ W i ω := fun h => hr (hWR i ω h) simp [bit, hw] have hsupport (ω : Ω) : bit (reject ω) * hits W ω = hits W ω := by by_cases hr : reject ω · simp [bit, hr] · rw [hzero ω hr] simp have hCS := Finset.expect_mul_sq_le_sq_mul_sq Finset.univ (fun ω => bit (reject ω)) (hits W) have hsquares : mean (fun ω => hits W ω ^ 2) ≤ mean (fun ω => hits E ω ^ 2) := by apply Finset.expect_le_expect intro ω _ simpa only [pow_two] using (mul_le_mul (hNH ω) (hNH ω) (hits_nonneg W ω) (hits_nonneg E ω)) calc _ ≤ mean (fun ω => bit (reject ω)) * mean (fun ω => hits W ω ^ 2) := by simpa only [mean, hsupport, bit_sq] using hCS _ ≤ _ := mul_le_mul_of_nonneg_left hsquares (mean_nonneg _ (fun ω => bit_nonneg (reject ω))) theorem rejection_lower_bound {I Ω : Type*} [Fintype I] [Fintype Ω] (E W : I → Ω → Prop) (reject : Ω → Prop) (hWE : ∀ i ω, W i ω → E i ω) (hWR : ∀ i ω, W i ω → reject ω) (a b : ℝ) (ha : 0 < a) (hb : 0 < b) (hfirst : a ≤ mean (hits W)) (hsecond : mean (fun ω => hits E ω ^ 2) ≤ b) : a ^ 2 / b ≤ mean (fun ω => bit (reject ω)) := by have hN : 0 ≤ mean (hits W) := mean_nonneg _ (hits_nonneg W) have hρ : 0 ≤ mean (fun ω => bit (reject ω)) := mean_nonneg _ (fun ω => bit_nonneg (reject ω)) have haSq : a ^ 2 ≤ mean (hits W) ^ 2 := by simpa only [pow_two] using mul_le_mul hfirst hfirst ha.le hN have hCS := mean_witness_sq_le E W reject hWE hWR apply (div_le_iff₀ hb).2 exact haSq.trans (hCS.trans (mul_le_mul_of_nonneg_left hsecond hρ)) end IndependentSetsGames.Foundations.PCP.PoweringMoment end end OAI