Lean / Reader
Lean reader

WallisBounds.lean

168 lines13 declarationsno sorryimports WallisEncoding Mathlib.Tactic

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports 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 WallisEncoding
  2. import Mathlib.Tactic
  3. namespace WallisEncoding
  4. open Finset WallisGap
  5. open scoped Classical
  6. private def containing {n : ℕ} (m : ℕ) (i : Fin n) : Finset (SubsetOfSize n m) :=
  7. univ.filter fun a => i ∈ a.val
  8. private lemma uniform_containing {n m : ℕ} (i j : Fin n) :
  9. (containing m i).card = (containing m j).card := by
  10. let f := Equiv.swap i j
  11. let e : SubsetOfSize n m ≃ SubsetOfSize n m := f.finsetCongr.subtypeEquiv
  12. (fun a => by simp [Equiv.finsetCongr_apply])
  13. have he (a : SubsetOfSize n m) : j ∈ (e a).val ↔ i ∈ a.val := by
  14. change j ∈ a.val.map f.toEmbedding ↔ _
  15. simp [Finset.mem_map_equiv, f]
  16. simp only [containing, card_filter]
  17. simpa only [he] using e.sum_comp (fun a => if j ∈ a.val then (1:ℕ) else 0)
  18. /-- Each point has the same inclusion probability in an m-subset, including at m=0. -/
  19. private lemma incidence {n m : ℕ} (i : Fin n) :
  20. n * (containing m i).card = m * Fintype.card (SubsetOfSize n m) := by
  21. calc
  22. _ = ∑ _j : Fin n, (containing m i).card := by simp
  23. _ = ∑ j : Fin n, (containing m j).card := by
  24. apply sum_congr rfl
  25. intro j _
  26. exact uniform_containing i j
  27. _ = ∑ a : SubsetOfSize n m, ∑ j : Fin n, if j ∈ a.val then 1 else 0 := by
  28. simp only [containing,card_filter]
  29. rw [sum_comm]
  30. _ = ∑ _a : SubsetOfSize n m, m := by
  31. apply sum_congr rfl
  32. intro a _
  33. simpa using a.property
  34. _ = _ := by simp [mul_comm]
  35. private def hits {n : ℕ} (m : ℕ) (T : Finset (Fin n)) : Finset (SubsetOfSize n m) :=
  36. univ.filter fun b => ∃ i ∈ T, i ∈ b.val
  37. private lemma hit_bound {n m : ℕ} (T : Finset (Fin n)) :
  38. n * (hits m T).card ≤ T.card * (m * Fintype.card (SubsetOfSize n m)) := by
  39. have eq : hits m T = T.biUnion (containing m) := by
  40. ext b
  41. simp [hits,containing]
  42. rw [eq]
  43. calc
  44. _ ≤ n * ∑ i ∈ T, (containing m i).card := Nat.mul_le_mul_left _ card_biUnion_le
  45. _ = _ := by simp [mul_sum,incidence]
  46. private noncomputable def neighbours {n : ℕ} (m : ℕ) (i : Fin n) : Finset (Fin n) :=
  47. univ.filter fun j => near m i j
  48. private lemma near_count {n m : ℕ} (i : Fin n) :
  49. (neighbours m i).card ≤ 2*m+1 := by
  50. calc
  51. _ ≤ (Icc (i.val-m) (i.val+m)).card := by
  52. apply card_le_card_of_injOn Fin.val _ (Fin.val_injective.injOn)
  53. intro j hj
  54. obtain ⟨a,b⟩ := (mem_filter.mp hj).2
  55. exact mem_Icc.mpr ⟨by omega,b⟩
  56. _ ≤ _ := by rw [Nat.card_Icc]; omega
  57. private lemma near_target {n m : ℕ} (a : SubsetOfSize n m) :
  58. n * (univ.filter (fun b : SubsetOfSize n m => ¬ Separated a b)).card ≤
  59. (m*m*(2*m+1))*Fintype.card (SubsetOfSize n m) := by
  60. let T := a.val.biUnion (neighbours m)
  61. have hT : T.card ≤ m*(2*m+1) := by
  62. calc
  63. _ ≤ ∑ i ∈ a.val, (neighbours m i).card := card_biUnion_le
  64. _ ≤ ∑ _i ∈ a.val, (2*m+1) := sum_le_sum fun i _ => near_count i
  65. _ = _ := by simp [a.property]
  66. have eq : univ.filter (fun b : SubsetOfSize n m => ¬ Separated a b) = hits m T := by
  67. ext b
  68. simp only [hits, T, neighbours, mem_filter, mem_univ, true_and, mem_biUnion]
  69. unfold Separated
  70. push_neg
  71. constructor
  72. · rintro ⟨i,hi,j,hj,hn⟩
  73. exact ⟨j,⟨i,hi,hn⟩,hj⟩
  74. · rintro ⟨j,⟨i,hi,hn⟩,hj⟩
  75. exact ⟨i,hi,j,hj,hn⟩
  76. rw [eq]
  77. calc
  78. _ ≤ _ := hit_bound T
  79. _ ≤ (m*(2*m+1)) * (m*Fintype.card (SubsetOfSize n m)) := Nat.mul_le_mul_right _ hT
  80. _ = _ := by ring
  81. private lemma product_count {α β γ : Type*} [Fintype α] [Fintype β] [Fintype γ]
  82. (p : α × β × γ → Prop) [DecidablePred p] :
  83. (univ.filter p).card =
  84. ∑ a : α, ∑ b : β, (univ.filter fun c : γ => p (a,b,c)).card := by
  85. simp only [card_filter, Fintype.sum_prod_type]
  86. /-- Union bound for a close pair among the two exceptional m-subsets. -/
  87. theorem bad_separated_bound (n m : ℕ) :
  88. n * (univ.filter (fun c : Code n m => ¬ Separated c.1 c.2.1)).card ≤
  89. m*m*(2*m+1) * Fintype.card (Code n m) := by
  90. let N := Fintype.card (SubsetOfSize n m)
  91. let M := Fintype.card (Equiv.Perm (Fin m))
  92. let L := m*m*(2*m+1)
  93. have inner (a : SubsetOfSize n m) :
  94. (∑ b : SubsetOfSize n m, (univ.filter fun _ : Equiv.Perm (Fin m) =>
  95. ¬Separated a b).card) = (univ.filter (fun b => ¬Separated a b)).card * M := by
  96. rw [card_filter, sum_mul]
  97. apply sum_congr rfl
  98. intro b _
  99. by_cases hb : Separated a b <;> simp [hb,M]
  100. erw [product_count (fun c : Code n m => ¬ Separated c.1 c.2.1)]
  101. simp only [inner, mul_sum]
  102. calc
  103. _ ≤ ∑ _a : SubsetOfSize n m, (L*N)*M := by
  104. apply sum_le_sum
  105. intro a _
  106. simpa only [← mul_assoc] using Nat.mul_le_mul_right M (near_target a)
  107. _ = _ := by simp [L,N,M,Code,Fintype.card_prod]; ring
  108. private lemma bad_point {n m : ℕ} (a b : SubsetOfSize n m) :
  109. (univ.filter (fun u : Fin n => ¬ DisjointTriple (a,b,u))).card ≤
  110. (if ¬Disjoint a.val b.val then 1 else 0)*n + 2*m := by
  111. by_cases h : Disjoint a.val b.val
  112. · have eq : univ.filter (fun u : Fin n => ¬ DisjointTriple (a,b,u)) = a.val ∪ b.val := by
  113. ext u
  114. by_cases hu : u ∈ a.val <;> simp [DisjointTriple,h,hu]
  115. rw [eq]
  116. simpa [h,a.property,b.property,two_mul] using card_union_le a.val b.val
  117. · simp [DisjointTriple,h]
  118. private lemma overlap_target {n m : ℕ} (a : SubsetOfSize n m) :
  119. n * (univ.filter (fun b : SubsetOfSize n m => ¬ Disjoint a.val b.val)).card ≤
  120. m * (m * Fintype.card (SubsetOfSize n m)) := by
  121. have eq : univ.filter (fun b : SubsetOfSize n m => ¬ Disjoint a.val b.val) = hits m a.val := by
  122. ext b
  123. simp [hits,Finset.not_disjoint_iff]
  124. rw [eq]
  125. simpa only [a.property] using hit_bound (m := m) a.val
  126. /-- Union bound for intersection of the two m-subsets or membership of the mark in either. -/
  127. theorem bad_triple_bound (n m : ℕ) :
  128. n * (univ.filter (fun t : Triple n m => ¬ DisjointTriple t)).card ≤
  129. (m*m+2*m) * Fintype.card (Triple n m) := by
  130. let N := Fintype.card (SubsetOfSize n m)
  131. have inner (a : SubsetOfSize n m) :
  132. ∑ b : SubsetOfSize n m, (univ.filter (fun u : Fin n => ¬DisjointTriple (a,b,u))).card ≤
  133. (m*m+2*m)*N := by
  134. calc
  135. _ ≤ ∑ b : SubsetOfSize n m, ((if ¬Disjoint a.val b.val then 1 else 0)*n + 2*m) :=
  136. sum_le_sum fun b _ => bad_point a b
  137. _ = n * (univ.filter (fun b : SubsetOfSize n m => ¬Disjoint a.val b.val)).card + N*(2*m) := by
  138. rw [sum_add_distrib, ←sum_mul, sum_boole]
  139. simp [N, mul_comm]
  140. _ ≤ (m*(m*N))+N*(2*m) := Nat.add_le_add_right (overlap_target a) _
  141. _ = _ := by ring
  142. erw [product_count (fun t : Triple n m => ¬ DisjointTriple t),mul_sum]
  143. calc
  144. _ ≤ ∑ _a : SubsetOfSize n m, n*((m*m+2*m)*N) := by
  145. apply sum_le_sum
  146. intro a _
  147. exact Nat.mul_le_mul_left n (inner a)
  148. _ = _ := by simp [N,Triple,Fintype.card_prod]; ring
  149. #print axioms bad_separated_bound
  150. #print axioms bad_triple_bound
  151. end WallisEncoding