import Mathlib import OAI.Probability.CriticalPercolation.ProductMeasure.ExpectClosedFlip import OAI.Probability.CriticalPercolation.Entropy.BinaryLaw import OAI.Probability.CriticalPercolation.Modular.ClusterDiscrepancy namespace OAI section section noncomputable section open Set open scoped Classical namespace CriticalPercolation lemma capped_gap_nonneg {a b : ℝ} (ha : 0≤a) (hb : 0≤b) : 0 ≤ min a 1 + min b 1 - min (a+b) 1 := by rcases le_total a 1 with ha1|ha1 <;> rcases le_total b 1 with hb1|hb1 · rw [min_eq_left ha1,min_eq_left hb1] exact sub_nonneg.mpr (min_le_left _ _) · rw [min_eq_left ha1,min_eq_right hb1] linarith [min_le_right (a+b) 1] · rw [min_eq_right ha1,min_eq_left hb1] linarith [min_le_right (a+b) 1] · rw [min_eq_right ha1,min_eq_right hb1] linarith [min_le_right (a+b) 1] def cappedMass (s m : ℕ) : ℝ := min ((m:ℝ)/s) 1 lemma cappedMass_nonneg (s m : ℕ) : 0≤cappedMass s m := by unfold cappedMass positivity lemma cappedMass_gap (s a b : ℕ) (hs : 0 G.edgeDiscrepancy f p ω e) = expect w (fun ω => if ¬ω e then G.edgeClusterMass f ω e - G.edgeClusterMass f (insert e ω) e else 0) := by let : DecidableEq E := Classical.decEq E have hflip := expect_closed_flip_decidable w hw e hp h0 h1 (fun ω => G.edgeClusterMass f ω e / p) have hu (ω : Set E) : {a | Function.update ω e True a} = insert e ω := by ext a change Function.update (fun a => ω a) e True a ↔ a=e ∨ a∈ω by_cases ha : a=e · subst a; simp [Function.update] · simp only [Function.update_of_ne ha,ha,false_or]; rfl have hleft : (fun ω : Set E => if ¬ω e then p*(G.edgeClusterMass f (Function.update ω e True) e / p) else 0) = (fun ω => if ¬ω e then G.edgeClusterMass f (insert e ω) e else 0) := by funext ω rw [show (Function.update ω e True : Set E) = insert e ω from hu ω] split_ifs <;> field_simp [hp.1.ne'] erw [hleft] at hflip have heq (ω : Set E) : G.edgeDiscrepancy f p ω e = (if ¬ω e then G.edgeClusterMass f ω e else 0) - (if ω e then (1-p)*(G.edgeClusterMass f ω e / p) else 0) := by change (if ω e then -(1-p)/p else 1) * G.edgeClusterMass f ω e = _ by_cases hb : ω e <;> simp only [hb,ite_true,ite_false,not_true_eq_false,not_false_eq_true,sub_zero,zero_sub,one_mul] ring have hfunc : (fun ω : Set E => G.edgeDiscrepancy f p ω e) = (fun ω => (if ¬ω e then G.edgeClusterMass f ω e else 0) - (if ω e then (1-p)*(G.edgeClusterMass f ω e / p) else 0)) := funext heq erw [hfunc] erw [expect_sub,← hflip,← expect_sub] congr 1 funext ω by_cases hb : ω e <;> simp [hb] lemma endpoint_prob_le_discrepancy (p : ℝ) (hp : p ∈ Set.Ioo 0 1) (w : E → Prop → ℝ) (hw : ∀ e, ∑ b, w e b=1) (hn : ∀ e b, 0≤w e b) (e : E) (x y : V) (he : G.ends e = s(x,y)) (h0 : w e False=1-p) (h1 : w e True=p) (s : ℕ) (hs : 0 G.edgeDiscrepancy (cappedMass s) p ω e) := by rw [G.expect_edgeDiscrepancy _ p hp w hw e h0 h1] apply expect_mono hn intro ω by_cases hb : ω e · have hconn : (G.openGraph ω).Reachable x y := LazyDFS.prop_openEdge_reachable G ω hb he simp [hb,BinaryLaw.endpointEvent,hconn] · simp only [hb,not_false_eq_true,ite_true] exact G.capped_edge_gap ω e x y he s hs lemma sum_edgeDiscrepancy (f : ℕ → ℝ) (p : ℝ) (ω : Set E) : (∑ e, G.edgeDiscrepancy f p ω e) = ∑ v, G.rootMass f ω v * G.clusterDiscrepancy p ω v := by simp only [edgeDiscrepancy,edgeClusterMass,clusterDiscrepancy,Finset.mul_sum] rw [Finset.sum_comm] apply Finset.sum_congr rfl intro v _ apply Finset.sum_congr rfl intro e _ split_ifs <;> ring end BondGraph end CriticalPercolation end end end end OAI