import Mathlib import OAI.Combinatorics.MatchingCount.Graphs.RowPeak namespace OAI namespace MatchingFPRAS.Concentration open MeasureTheory ProbabilityTheory Finset variable {Ω I J : Type*} [MeasurableSpace Ω] [Fintype I] variable {μ : Measure Ω} [IsProbabilityMeasure μ] theorem hoeffding_mean (X : I → Ω → ℝ) (hi : iIndepFun X μ) (hm : ∀ i,AEMeasurable (X i) μ) (hb : ∀ i,∀ᵐ ω ∂μ,X i ω∈Set.Icc (0:ℝ) 1) (m : ℝ) (hmean : ∀ i,(∫ ω,X i ω ∂μ)=m) (hI : 0 X i ω-m) (1/4) μ := by have h := hasSubgaussianMGF_of_mem_Icc (hm i) (hb i) rw [hmean i] at h norm_num at h ⊢ exact h have hind : iIndepFun (fun i ω => X i ω-m) μ := by exact hi.comp (fun _ x => x-m) (fun _ => by fun_prop) have hs : HasSubgaussianMGF (fun ω => ∑ i,(X i ω-m)) ((Fintype.card I : NNReal)/4) μ := by simpa only [Finset.sum_const,nsmul_eq_mul,mul_one_div,Finset.card_univ] using HasSubgaussianMGF.sum_of_iIndepFun hind (s:=univ) (fun i _ => hc i) have hh := hs.measure_ge_le (mul_nonneg hN.le ht) have hl := hs.neg.measure_ge_le (mul_nonneg hN.le ht) have he : -((Fintype.card I : ℝ)*t)^2 / (2*((Fintype.card I : NNReal)/4 : NNReal)) = -2*(Fintype.card I : ℝ)*t^2 := by push_cast field_simp [ne_of_gt hN] ring rw [he] at hh hl calc _ ≤ μ.real ({ω | (Fintype.card I : ℝ)*t ≤ ∑ i,(X i ω-m)} ∪ {ω | (Fintype.card I : ℝ)*t ≤ -(∑ i,(X i ω-m))}) := by refine measureReal_mono ?_ (by finiteness) intro ω hω simp only [Set.mem_ofPred_eq,Set.mem_union] at hω ⊢ by_cases h : (Fintype.card I : ℝ)*t ≤ ∑ i,(X i ω-m) · exact Or.inl h · right simp only [sum_sub_distrib,sum_const,nsmul_eq_mul,Finset.card_univ] at h ⊢ rcases lt_abs.mp hω with hv | hv · have hv' := (lt_div_iff₀ hN).mp (by linarith : m+t < (∑ i,X i ω)/(Fintype.card I : ℝ)) nlinarith · have hv' := (div_lt_iff₀ hN).mp (by linarith : (∑ i,X i ω)/(Fintype.card I : ℝ) < m-t) nlinarith _ ≤ μ.real {ω | (Fintype.card I : ℝ)*t ≤ ∑ i,(X i ω-m)} + μ.real {ω | (Fintype.card I : ℝ)*t ≤ -(∑ i,(X i ω-m))} := measureReal_union_le _ _ _ ≤ _ := by simpa only [Pi.neg_apply, two_mul] using add_le_add hh hl theorem hoeffding_observables [Fintype J] (X : J → I → Ω → ℝ) (hi : ∀ j,iIndepFun (X j) μ) (hm : ∀ j i,AEMeasurable (X j i) μ) (hb : ∀ j i,∀ᵐ ω ∂μ,X j i ω∈Set.Icc (0:ℝ) 1) (m : J → ℝ) (hmean : ∀ j i,(∫ ω,X j i ω ∂μ)=m j) (hI : 0 ENNReal.ofReal (p x)) (by rw [←ENNReal.ofReal_sum_of_nonneg (fun x _ => hp x),hs] simp) lemma finitePMF_apply (p : Ω → ℝ) (hp : ∀ x,0 ≤ p x) (hs : ∑ x,p x=1) (x : Ω) : (finitePMF p hp hs x).toReal=p x := by simp [finitePMF,hp x] theorem finite_hoeffding [DecidableEq I] (p : Ω → ℝ) (hp : ∀ x,0 ≤ p x) (hs : ∑ x,p x=1) (f : J → Ω → ℝ) (hf : ∀ j x,f j x∈Set.Icc (0:ℝ) 1) (hI : 0 q.toMeasure) have hi (j : J) : iIndepFun (fun i (v : I → Ω) => f j (v i)) μ := by exact iIndepFun_pi (fun _ : I => (measurable_of_finite (f j)).aemeasurable) have hm (j : J) (i : I) : AEMeasurable (fun v : I → Ω => f j (v i)) μ := (measurable_of_finite _).aemeasurable have hb (j : J) (i : I) : ∀ᵐ v ∂μ,f j (v i)∈Set.Icc (0:ℝ) 1 := Filter.Eventually.of_forall (fun v => hf j (v i)) have he (j : J) (i : I) : (∫ v,f j (v i) ∂μ)=∑ x,p x*f j x := by have hev := (measurePreserving_eval (fun _ : I => q.toMeasure) i).map_eq have hmap := integral_map ((measurable_of_finite (Function.eval i)).aemeasurable (μ:=μ)) ((measurable_of_finite (f j)).aestronglyMeasurable (μ:=Measure.map (Function.eval i) μ)) rw [hev] at hmap rw [←hmap, PMF.integral_eq_sum] apply sum_congr rfl intro x hx simp [q,finitePMF_apply,smul_eq_mul] have h := hoeffding_observables (μ:=μ) (fun j i v => f j (v i)) hi hm hb (fun j => ∑ x,p x*f j x) he hI t ht have hsingle (v : I → Ω) : μ.real {v}=∏ i,p (v i) := by simp only [Measure.real,μ,Measure.pi_singleton,ENNReal.toReal_prod] apply prod_congr rfl intro i hi simp [q,finitePMF,hp (v i)] let B : Finset (I → Ω) := univ.filter (fun v => ∃ j,t < |(∑ i,f j (v i))/(Fintype.card I : ℝ)-∑ x,p x*f j x|) have hB : (B : Set (I → Ω))={v | ∃ j,t < |(∑ i,f j (v i))/(Fintype.card I : ℝ)-∑ x,p x*f j x|} := by ext v; simp [B] rw [←hB,←sum_measureReal_singleton] at h simpa only [hsingle, B, sum_filter] using h lemma observables_exp_tail (n K : ℕ) (hK : 0 exact hw _ _ have h₁ : 0 ≤ logicalActivity w₁ he.choose.1 := by unfold logicalActivity edgeActivity; split_ifs <;> exact hw₁ _ _ exact ⟨div_nonneg h₁ h₀.le,(div_le_one h₀).mpr (logicalActivity_le_off w w₁ hle _)⟩ · norm_num · norm_num lemma observableD_range_off (w w₁ : V → V → ℝ) (hw : ∀ x y,0 < w x y) (hw₁ : ∀ x y,0 ≤ w₁ x y) (hle : ∀ x y,x≠y → w₁ x y ≤ w x y) (K : ℕ) (a : SourceMarked w K) : 0 ≤ observableD w w₁ K a ∧ observableD w w₁ K a ≤ 1 := by unfold observableD split_ifs · exact ⟨Finset.prod_nonneg (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).1), Finset.prod_le_one₀ (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).1) (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).2)⟩ · norm_num lemma observableProbe_range_off (w w₁ : V → V → ℝ) (hw : ∀ x y,0 < w x y) (hw₁ : ∀ x y,0 ≤ w₁ x y) (hle : ∀ x y,x≠y → w₁ x y ≤ w x y) (K : ℕ) (e₀ : LogicalEdge V) (a : SourceMarked w K) : 0 ≤ observableProbe w w₁ K e₀ a ∧ observableProbe w w₁ K e₀ a ≤ 1 := by unfold observableProbe split_ifs · exact ⟨Finset.prod_nonneg (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).1), Finset.prod_le_one₀ (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).1) (fun e _ => (bagReweight_range_off w w₁ hw hw₁ hle K e).2)⟩ · norm_num abbrev ObservableIndex (V : Type*) [LinearOrder V] := Fin 2 ⊕ LogicalEdge V noncomputable def sourceObservable (w w₁ : V → V → ℝ) (K : ℕ) : ObservableIndex V → SourceMarked w K → ℝ | Sum.inl i => if i=0 then observableA w K else observableD w w₁ K | Sum.inr e => observableProbe w w₁ K e lemma sourceObservable_range (w w₁ : V → V → ℝ) (hw : ∀ x y,0 < w x y) (hw₁ : ∀ x y,0 ≤ w₁ x y) (hle : ∀ x y,x≠y → w₁ x y ≤ w x y) (K : ℕ) (j : ObservableIndex V) (a : SourceMarked w K) : sourceObservable w w₁ K j a∈Set.Icc (0:ℝ) 1 := by cases j with | inl i => dsimp [sourceObservable] split_ifs · exact observableA_range w K a · exact observableD_range_off w w₁ hw hw₁ hle K a | inr e => exact observableProbe_range_off w w₁ hw hw₁ hle K e a omit [Nontrivial V] in lemma observableIndex_card : Fintype.card (ObservableIndex V) ≤ (Fintype.card V)^2+2 := by have h := Fintype.card_subtype_le (fun e : V×V => e.1 ¬StageGood w w₁ K σ x) < 13/(1000*T) := by have hpos := sourceBagZ_pos w hw hz K (sourceD (V:=V) K) have hq := sourceLaw_nonneg w hw K _ hpos.le have hqs := sourceLaw_sum w K _ hpos.ne' have hiid := Variation.iid_tv p (sourceLaw w K (sourceD (V:=V) K)) hp hpsum hq hqs S have he := Variation.event_sub_le (Variation.iid p S) (Variation.iid (sourceLaw w K (sourceD (V:=V) K)) S) (by rw [Variation.iid_sum p hpsum,Variation.iid_sum _ hqs]) (fun x => ¬StageGood w w₁ K σ x) have hv := (le_abs_self _).trans (he.trans hiid) have hh := source_ideal_budget w w₁ hw hw₁ hle hz K T S hT hS σ hσ hsize have ht := mul_le_mul_of_nonneg_left htv (Nat.cast_nonneg S : (0:ℝ)≤S) have hid : (1:ℝ)/(100*T)+(S:ℝ)*(3/(1000*T*S))=13/(1000*T) := by have ht0 : (T:ℝ)≠0 := by exact_mod_cast hT.ne' have hs0 : (S:ℝ)≠0 := by exact_mod_cast hS.ne' field_simp ring have hh' : Variation.event (Variation.iid (sourceLaw w K (sourceD (V:=V) K)) S) (fun x => ¬StageGood w w₁ K σ x) < 1/(100*T) := by convert hh using 1 unfold Variation.event Variation.iid congr 1 funext x split_ifs <;> rfl linarith theorem source_run_stage_budget (hn : 2 ≤ Fintype.card V) (w w₁ : V → V → ℝ) (hw : ∀ x y,0 ¬StageGood w w₁ K σ x) < 13/(1000*T) := by dsimp only apply source_nearby_budget w w₁ hw hw₁ hle hz K T S hT hS σ hσ hsize · exact sourceRun_nonneg w hw K hK L _ hq z _ · exact sourceRun_sum w hw K hK L _ hq z _ · have ht : (1:ℝ)≤T := by exact_mod_cast hT have hs : (1:ℝ)≤S := by exact_mod_cast hS have hd : (1:ℝ)<1000*T*S := by nlinarith exact (sourceRun_tv hn w hw K hK L hL hq z _ (by positivity) ((div_lt_one (by positivity)).mpr hd)).trans (by gcongr; norm_num) end MatchingFPRAS.Enlargement end OAI