Lean / Reader
Lean reader

WallisAverages.lean

154 lines16 declarationsno sorryimports WallisTransfer WallisEncoding Mathlib.Algebra.BigOperators.Group.Finset.Piecewise Mathlib.Analysis.SpecificLimits.Basic Mathlib.Data.Real.Archimedean

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.

  1. import WallisTransfer
  2. import WallisEncoding
  3. import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
  4. import Mathlib.Analysis.SpecificLimits.Basic
  5. import Mathlib.Data.Real.Archimedean
  6. open scoped BigOperators
  7. namespace WallisEncoding
  8. open Finset WallisGap Filter
  9. open scoped Topology
  10. noncomputable section
  11. open Classical
  12. abbrev MarkedCode (n m : ℕ) := Code n m × Fin n
  13. def triple {n m} (e : MarkedCode n m) : Triple n m := (e.1.1,e.1.2.1,e.2)
  14. def relabel (n m : ℕ) : MarkedCode n m ≃ Triple n m × Equiv.Perm (Fin m) where
  15. toFun e := (triple e, e.1.2.2)
  16. invFun z := ((z.1.1, z.1.2.1, z.2), z.1.2.2)
  17. left_inv := fun _ => rfl
  18. right_inv := fun _ => rfl
  19. def markedDecode {n m} (e : MarkedCode n m) : Equiv.Perm (Fin n) × Fin n := (decode e.1,e.2)
  20. def fixed {n} (z : Equiv.Perm (Fin n) × Fin n) := z.1 z.2 = z.2
  21. def target (n m : ℕ) : Finset (Equiv.Perm (Fin n) × Fin n) :=
  22. univ.filter fun z => AtDefect m z.1
  23. /-- Mean proportion of fixed points in uniform permutations of prescribed LIS length.
  24. The denominator counts all marked permutations satisfying the length condition. -/
  25. def expected (n m : ℕ) : ℝ :=
  26. (((target n m).filter fixed).card : ℝ) / (target n m).card
  27. lemma mark_count_lift {n m} (h : Triple n m → Prop) [DecidablePred h] :
  28. (univ.filter (fun e : MarkedCode n m => h (triple e))).card
  29. = (univ.filter h).card * Fintype.card (Equiv.Perm (Fin m)) := by
  30. let a := univ.filter (fun e : MarkedCode n m => h (triple e))
  31. let b := (univ.filter h) ×ˢ (univ : Finset (Equiv.Perm (Fin m)))
  32. have hmap : a.map (relabel n m).toEmbedding = b := by
  33. ext x
  34. obtain ⟨e,rfl⟩ := (relabel n m).surjective x
  35. simp [a,b,relabel,triple]
  36. rw [← Finset.card_univ, ← Finset.card_product]
  37. exact (Finset.card_map (relabel n m).toEmbedding).symm.trans (congrArg Finset.card hmap)
  38. lemma mark_count_lift_left {n m} (h : Code n m → Prop) [DecidablePred h] :
  39. (univ.filter fun e : MarkedCode n m => h e.1).card
  40. = (univ.filter h).card * n := by
  41. rw [show (univ : Finset (MarkedCode n m)) = univ ×ˢ univ from rfl,
  42. filter_product_left, card_product, card_univ, Fintype.card_fin]
  43. lemma count_marked {n m} :
  44. Fintype.card (MarkedCode n m) = Fintype.card (Triple n m) * Fintype.card (Equiv.Perm (Fin m)) := by
  45. rw [← Fintype.card_prod]
  46. exact Fintype.card_congr (relabel n m)
  47. lemma fixed_on_good {n m} (e : MarkedCode n m) (h : DisjointTriple (triple e)) :
  48. fixed (markedDecode e) ↔ MatchingRank (triple e) :=
  49. fixed_iff_matchingRank e.1 e.2 h.2.1 h.2.2
  50. def frequency {α} (s : Finset α) (h : α → Prop) : ℝ :=
  51. ((s.filter h).card : ℝ) / s.card
  52. lemma sum_indicator {α} (s : Finset α) (p : α → Prop) [DecidablePred p] :
  53. (∑ x ∈ s, if p x then (1:ℝ) else 0) = ((s.filter p).card : ℝ) := by
  54. rw [card_filter]
  55. push_cast
  56. rfl
  57. /-- Discarding an exceptional subset gives a quantitative estimate for any event
  58. whose numerator is determined on the complement. -/
  59. theorem ratio_from_restriction {α} (s : Finset α) (a b c : α → Prop)
  60. (hat : ∀ x ∈ s, a x → (b x ↔ c x)) (W : ℝ) (hw₀ : 0 ≤ W) (hw₁ : W ≤ 1)
  61. (ratio : (((s.filter fun x => a x ∧ c x).card : ℝ)) = ((s.filter a).card : ℝ) * W)
  62. (hs : s.Nonempty) :
  63. |frequency s b - W| ≤ ((s.filter fun x => ¬a x).card : ℝ)/s.card := by
  64. let good := s.filter fun x => a x ∧ c x
  65. let bad := s.filter fun x => ¬a x
  66. have lo : good ⊆ s.filter b := by
  67. intro x hx
  68. obtain ⟨hsx,hax,hcx⟩ := mem_filter.1 hx
  69. exact mem_filter.2 ⟨hsx,(hat x hsx hax).2 hcx⟩
  70. have up : s.filter b ⊆ good ∪ bad := by
  71. intro x hx
  72. obtain ⟨hsx,hbx⟩ := mem_filter.1 hx
  73. by_cases hax : a x
  74. · exact mem_union_left _ (mem_filter.2 ⟨hsx,hax,(hat x hsx hax).1 hbx⟩)
  75. · exact mem_union_right _ (mem_filter.2 ⟨hsx,hax⟩)
  76. have hLo : (good.card : ℝ) ≤ (s.filter b).card := by exact_mod_cast card_le_card lo
  77. have hUp : ((s.filter b).card : ℝ) ≤ good.card + bad.card := by
  78. exact_mod_cast (card_le_card up).trans (card_union_le good bad)
  79. have eqNat : (s.filter a).card + bad.card = s.card := by
  80. exact Finset.filter_card_add_filter_neg_card_eq_card _
  81. have eqReal : ((s.filter a).card : ℝ) + bad.card = s.card := by exact_mod_cast eqNat
  82. have pos : (0:ℝ) < s.card := by exact_mod_cast Finset.card_pos.2 hs
  83. have non : (0:ℝ) ≤ bad.card := Nat.cast_nonneg _
  84. change (good.card : ℝ) = _ at ratio
  85. change |((s.filter b).card:ℝ) / s.card - W| ≤ (bad.card:ℝ) / s.card
  86. rw [div_sub' pos.ne', abs_div, abs_of_pos pos, div_le_div_iff_of_pos_right pos, abs_le]
  87. constructor <;> nlinarith [mul_nonneg non hw₀, mul_nonneg non (sub_nonneg.mpr hw₁)]
  88. lemma transfer_marked {n m : ℕ} (hm : m ≤ n) (hp : (target n m).Nonempty) :
  89. |expected n m - frequency (univ : Finset (MarkedCode n m)) (fun e => fixed (markedDecode e))|
  90. ≤ ((univ.filter (fun c : Code n m => ¬Separated c.1 c.2.1)).card : ℝ) /
  91. Fintype.card (Code n m) := by
  92. let e : Finset (MarkedCode n m) := univ
  93. let good : MarkedCode n m → Prop := fun z => Separated z.1.1 z.1.2.1
  94. let g := e.filter good
  95. have into : g.image markedDecode ⊆ target n m := by
  96. intro z hz
  97. obtain ⟨v,hv,rfl⟩ := mem_image.1 hz
  98. exact mem_filter.2 ⟨mem_univ _,defect_of_separated _ (mem_filter.1 hv).2⟩
  99. have inj : Set.InjOn markedDecode (↑g : Set (MarkedCode n m)) := by
  100. intro x hx y hy heq
  101. exact 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)
  102. have onto : target n m ⊆ e.image markedDecode := by
  103. intro z hz
  104. obtain ⟨c,hc⟩ := exists_code hm z.1 (mem_filter.1 hz).2
  105. exact mem_image.2 ⟨(c,z.2),mem_univ _,Prod.ext hc rfl⟩
  106. have h := WallisTransfer.mean_transfer e g (target n m) markedDecode
  107. (fun x => if fixed x then 1 else 0) (filter_subset _ _) hp into inj onto
  108. (fun x => by dsimp only; split <;> norm_num) (fun x => by dsimp only; split <;> norm_num)
  109. rw [sum_indicator, sum_indicator] at h
  110. have d : g.card + (e.filter (fun z => ¬good z)).card = e.card :=
  111. filter_card_add_filter_neg_card_eq_card _
  112. have d' : (e.card : ℝ) - g.card = (e.filter (fun z => ¬good z)).card := by
  113. have de : (g.card : ℝ) + (e.filter (fun z => ¬good z)).card = e.card := by exact_mod_cast d
  114. linarith
  115. rw [d', show (e.filter fun z => ¬good z).card = _ from
  116. mark_count_lift_left (fun c : Code n m => ¬Separated c.1 c.2.1)] at h
  117. change |_ - _| ≤ _ at h
  118. have cardE : e.card = Fintype.card (Code n m)*n := by simp [e,MarkedCode]
  119. rw [cardE, Nat.cast_mul, Nat.cast_mul] at h
  120. have hn : n ≠ 0 := by
  121. obtain ⟨z,hz⟩ := hp
  122. exact Nat.ne_of_gt z.2.pos
  123. unfold expected frequency
  124. change |_ - _ / (e.card : ℝ)| ≤ _
  125. rw [cardE,Nat.cast_mul]
  126. simpa only [mul_div_mul_right _ _ (Nat.cast_ne_zero.mpr hn : (n : ℝ) ≠ 0)] using h
  127. lemma exists_marked {n m : ℕ} (hm : m ≤ n) (hn : 0 < n) : (univ : Finset (MarkedCode n m)).Nonempty := by
  128. obtain ⟨a,_,ha⟩ := exists_subset_card_eq (s := (univ : Finset (Fin n))) (n := m) (by simpa using hm)
  129. let A : SubsetOfSize n m := ⟨a,ha⟩
  130. exact ⟨((A,A,Equiv.refl _),⟨0,hn⟩),mem_univ _⟩
  131. end
  132. end WallisEncoding