import Mathlib import OAI.Probability.SKBarriers.SpinGlass.FiniteLaw namespace OAI section noncomputable section open scoped BigOperators open Classical MeasureTheory Set namespace SK.Analytic namespace FiniteLaw def uniformFin (n : ℕ) (hn : 0 < n) : FiniteLaw (Fin n) where weight _ := 1/(n:ℝ) nonneg _ := by positivity sum_one := by simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul] field_simp theorem uniformFin_prob (n : ℕ) (hn : 0 < n) (E : Fin n → Prop) : (uniformFin n hn).prob E=(Finset.univ.filter E).card/(n:ℝ) := by simp only [prob,expect,uniformFin,← Finset.mul_sum] rw [Finset.sum_boole] ring theorem markov {n : ℕ} (P : FiniteLaw (Fin n)) (f : Fin n → ℝ) (hf : ∀ x, 0 ≤ f x) (c : ℝ) (_hc : 0 < c) : c*P.prob (fun x => c < f x) ≤ P.expect f := by rw [prob,← expect_const_mul] apply expect_mono intro x split_ifs with h · simpa only [mul_one] using h.le · simpa only [mul_zero] using hf x end FiniteLaw def thresholdGrid (n : ℕ) (a ρ : ℝ) (i : Fin n) : ℝ := a+ρ*(i:ℕ)/(n:ℝ) theorem thresholdGrid_mem {n : ℕ} (hn : 0 < n) {ρ : ℝ} (hρ : 0 < ρ) (a : ℝ) (i : Fin n) : thresholdGrid n a ρ i ∈ Icc a (a+ρ) := by have hnR : (0:ℝ) |thresholdGrid n a ρ i-x| ≤ w)).card:ℝ) ≤ 2*w*(n:ℝ)/ρ+1 := by let S := Finset.univ.filter (fun i : Fin n => |thresholdGrid n a ρ i-x| ≤ w) have hnR : (0:ℝ) |thresholdGrid n a ρ i-x| ≤ w) ≤ 2*w/ρ+1/(n:ℝ) := by rw [FiniteLaw.uniformFin_prob] have h := div_le_div_of_nonneg_right (thresholdGrid_count hn hρ hw a x) (by positivity : (0:ℝ)≤n) refine h.trans_eq ?_ have hnR : (n:ℝ)≠0 := by exact_mod_cast hn.ne' field_simp theorem thresholdGrid_mass_expect {n : ℕ} (hn : 0 < n) (μ : ProbabilityMeasure ℝ) {ρ h : ℝ} (hρ : 0 < ρ) (hh : 0 ≤ h) (a : ℝ) : (FiniteLaw.uniformFin n hn).expect (fun i => (μ:Measure ℝ).real (Icc (thresholdGrid n a ρ i-h) (thresholdGrid n a ρ i+h))) ≤ 2*h/ρ+1/(n:ℝ) := by let f (i : Fin n) := (Icc (thresholdGrid n a ρ i-h) (thresholdGrid n a ρ i+h)).indicator (fun _ : ℝ => (1:ℝ)) have hi (i : Fin n) : Integrable (f i) (μ:Measure ℝ) := (integrable_const 1).indicator measurableSet_Icc have he : (FiniteLaw.uniformFin n hn).expect (fun i => (μ:Measure ℝ).real (Icc (thresholdGrid n a ρ i-h) (thresholdGrid n a ρ i+h))) = ∫ x, (1/(n:ℝ))*∑ i, f i x ∂(μ:Measure ℝ) := by unfold FiniteLaw.expect FiniteLaw.uniformFin rw [integral_const_mul,integral_finsetSum _ (fun i _ => hi i),Finset.mul_sum] apply Finset.sum_congr rfl intro i _ dsimp only [f] rw [integral_indicator_const (1:ℝ) measurableSet_Icc] simp only [smul_eq_mul,mul_one] rw [he] have hb (x : ℝ) : (1/(n:ℝ))*∑ i, f i x ≤ 2*h/ρ+1/(n:ℝ) := by have H := thresholdGrid_window hn hρ hh a x change (1/(n:ℝ))*(∑ i, f i x)≤_ convert H using 1 unfold FiniteLaw.prob FiniteLaw.expect FiniteLaw.uniformFin rw [← Finset.mul_sum] congr 1 apply Finset.sum_congr rfl intro i _ have he : x∈Icc (thresholdGrid n a ρ i-h) (thresholdGrid n a ρ i+h) ↔ |thresholdGrid n a ρ i-x|≤h := by rw [Set.mem_Icc,abs_le] constructor <;> intro H <;> constructor <;> linarith only [H.1,H.2] simp only [f,Set.indicator_apply,he] exact (integral_mono_of_nonneg (ae_of_all _ (fun x => by apply mul_nonneg (by positivity) exact Finset.sum_nonneg (fun i _ => Set.indicator_nonneg (fun _ _ => zero_le_one) x))) (integrable_const _) (ae_of_all _ hb)).trans_eq (by simp) end SK.Analytic end end end OAI