Lean / Reader
Lean readerWallisAverages.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports WallisTransfer, WallisEncoding, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import WallisTransferimport WallisEncodingimport Mathlib.Algebra.BigOperators.Group.Finset.Piecewiseimport Mathlib.Analysis.SpecificLimits.Basicimport Mathlib.Data.Real.Archimedeanopen scoped BigOperatorsnamespace WallisEncodingopen Finset WallisGap Filteropen scoped Topologynoncomputable sectionopen Classicalabbrev MarkedCode (n m : ℕ) := Code n m × Fin ndef triple {n m} (e : MarkedCode n m) : Triple n m := (e.1.1,e.1.2.1,e.2)def relabel (n m : ℕ) : MarkedCode n m ≃ Triple n m × Equiv.Perm (Fin m) wheretoFun e := (triple e, e.1.2.2)invFun z := ((z.1.1, z.1.2.1, z.2), z.1.2.2)left_inv := fun _ => rflright_inv := fun _ => rfldef markedDecode {n m} (e : MarkedCode n m) : Equiv.Perm (Fin n) × Fin n := (decode e.1,e.2)def fixed {n} (z : Equiv.Perm (Fin n) × Fin n) := z.1 z.2 = z.2def target (n m : ℕ) : Finset (Equiv.Perm (Fin n) × Fin n) :=univ.filter fun z => AtDefect m z.1/-- Mean proportion of fixed points in uniform permutations of prescribed LIS length.The denominator counts all marked permutations satisfying the length condition. -/def expected (n m : ℕ) : ℝ :=(((target n m).filter fixed).card : ℝ) / (target n m).cardlemma mark_count_lift {n m} (h : Triple n m → Prop) [DecidablePred h] :(univ.filter (fun e : MarkedCode n m => h (triple e))).card= (univ.filter h).card * Fintype.card (Equiv.Perm (Fin m)) := bylet a := univ.filter (fun e : MarkedCode n m => h (triple e))let b := (univ.filter h) ×ˢ (univ : Finset (Equiv.Perm (Fin m)))have hmap : a.map (relabel n m).toEmbedding = b := byext xobtain ⟨e,rfl⟩ := (relabel n m).surjective xsimp [a,b,relabel,triple]rw [← Finset.card_univ, ← Finset.card_product]exact (Finset.card_map (relabel n m).toEmbedding).symm.trans (congrArg Finset.card hmap)lemma mark_count_lift_left {n m} (h : Code n m → Prop) [DecidablePred h] :(univ.filter fun e : MarkedCode n m => h e.1).card= (univ.filter h).card * n := byrw [show (univ : Finset (MarkedCode n m)) = univ ×ˢ univ from rfl,filter_product_left, card_product, card_univ, Fintype.card_fin]lemma count_marked {n m} :Fintype.card (MarkedCode n m) = Fintype.card (Triple n m) * Fintype.card (Equiv.Perm (Fin m)) := byrw [← Fintype.card_prod]exact Fintype.card_congr (relabel n m)lemma fixed_on_good {n m} (e : MarkedCode n m) (h : DisjointTriple (triple e)) :fixed (markedDecode e) ↔ MatchingRank (triple e) :=fixed_iff_matchingRank e.1 e.2 h.2.1 h.2.2def frequency {α} (s : Finset α) (h : α → Prop) : ℝ :=((s.filter h).card : ℝ) / s.cardlemma sum_indicator {α} (s : Finset α) (p : α → Prop) [DecidablePred p] :(∑ x ∈ s, if p x then (1:ℝ) else 0) = ((s.filter p).card : ℝ) := byrw [card_filter]push_castrfl/-- Discarding an exceptional subset gives a quantitative estimate for any eventwhose numerator is determined on the complement. -/theorem ratio_from_restriction {α} (s : Finset α) (a b c : α → Prop)(hat : ∀ x ∈ s, a x → (b x ↔ c x)) (W : ℝ) (hw₀ : 0 ≤ W) (hw₁ : W ≤ 1)(ratio : (((s.filter fun x => a x ∧ c x).card : ℝ)) = ((s.filter a).card : ℝ) * W)(hs : s.Nonempty) :|frequency s b - W| ≤ ((s.filter fun x => ¬a x).card : ℝ)/s.card := bylet good := s.filter fun x => a x ∧ c xlet bad := s.filter fun x => ¬a xhave lo : good ⊆ s.filter b := byintro x hxobtain ⟨hsx,hax,hcx⟩ := mem_filter.1 hxexact mem_filter.2 ⟨hsx,(hat x hsx hax).2 hcx⟩have up : s.filter b ⊆ good ∪ bad := byintro x hxobtain ⟨hsx,hbx⟩ := mem_filter.1 hxby_cases hax : a x· exact mem_union_left _ (mem_filter.2 ⟨hsx,hax,(hat x hsx hax).1 hbx⟩)· exact mem_union_right _ (mem_filter.2 ⟨hsx,hax⟩)have hLo : (good.card : ℝ) ≤ (s.filter b).card := by exact_mod_cast card_le_card lohave hUp : ((s.filter b).card : ℝ) ≤ good.card + bad.card := byexact_mod_cast (card_le_card up).trans (card_union_le good bad)have eqNat : (s.filter a).card + bad.card = s.card := byexact Finset.filter_card_add_filter_neg_card_eq_card _have eqReal : ((s.filter a).card : ℝ) + bad.card = s.card := by exact_mod_cast eqNathave pos : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.2 hshave non : (0:ℝ) ≤ bad.card := Nat.cast_nonneg _change (good.card : ℝ) = _ at ratiochange |((s.filter b).card:ℝ) / s.card - W| ≤ (bad.card:ℝ) / s.cardrw [div_sub' pos.ne', abs_div, abs_of_pos pos, div_le_div_iff_of_pos_right pos, abs_le]constructor <;> nlinarith [mul_nonneg non hw₀, mul_nonneg non (sub_nonneg.mpr hw₁)]lemma transfer_marked {n m : ℕ} (hm : m ≤ n) (hp : (target n m).Nonempty) :|expected n m - frequency (univ : Finset (MarkedCode n m)) (fun e => fixed (markedDecode e))|≤ ((univ.filter (fun c : Code n m => ¬Separated c.1 c.2.1)).card : ℝ) /Fintype.card (Code n m) := bylet e : Finset (MarkedCode n m) := univlet good : MarkedCode n m → Prop := fun z => Separated z.1.1 z.1.2.1let g := e.filter goodhave into : g.image markedDecode ⊆ target n m := byintro z hzobtain ⟨v,hv,rfl⟩ := mem_image.1 hzexact mem_filter.2 ⟨mem_univ _,defect_of_separated _ (mem_filter.1 hv).2⟩have inj : Set.InjOn markedDecode (↑g : Set (MarkedCode n m)) := byintro x hx y hy heqexact Prod.ext (decode_unique x.1 y.1 (mem_filter.1 hx).2 (congrArg Prod.fst heq)) (congrArg (fun z : Equiv.Perm (Fin n) × Fin n => z.2) heq)have onto : target n m ⊆ e.image markedDecode := byintro z hzobtain ⟨c,hc⟩ := exists_code hm z.1 (mem_filter.1 hz).2exact mem_image.2 ⟨(c,z.2),mem_univ _,Prod.ext hc rfl⟩have h := WallisTransfer.mean_transfer e g (target n m) markedDecode(fun x => if fixed x then 1 else 0) (filter_subset _ _) hp into inj onto(fun x => by dsimp only; split <;> norm_num) (fun x => by dsimp only; split <;> norm_num)rw [sum_indicator, sum_indicator] at hhave d : g.card + (e.filter (fun z => ¬good z)).card = e.card :=filter_card_add_filter_neg_card_eq_card _have d' : (e.card : ℝ) - g.card = (e.filter (fun z => ¬good z)).card := byhave de : (g.card : ℝ) + (e.filter (fun z => ¬good z)).card = e.card := by exact_mod_cast dlinarithrw [d', show (e.filter fun z => ¬good z).card = _ frommark_count_lift_left (fun c : Code n m => ¬Separated c.1 c.2.1)] at hchange |_ - _| ≤ _ at hhave cardE : e.card = Fintype.card (Code n m)*n := by simp [e,MarkedCode]rw [cardE, Nat.cast_mul, Nat.cast_mul] at hhave hn : n ≠ 0 := byobtain ⟨z,hz⟩ := hpexact Nat.ne_of_gt z.2.posunfold expected frequencychange |_ - _ / (e.card : ℝ)| ≤ _rw [cardE,Nat.cast_mul]simpa only [mul_div_mul_right _ _ (Nat.cast_ne_zero.mpr hn : (n : ℝ) ≠ 0)] using hlemma exists_marked {n m : ℕ} (hm : m ≤ n) (hn : 0 < n) : (univ : Finset (MarkedCode n m)).Nonempty := byobtain ⟨a,_,ha⟩ := exists_subset_card_eq (s := (univ : Finset (Fin n))) (n := m) (by simpa using hm)let A : SubsetOfSize n m := ⟨a,ha⟩exact ⟨((A,A,Equiv.refl _),⟨0,hn⟩),mem_univ _⟩endend WallisEncoding