Lean / Reader
Lean reader

ProductLayers.lean

296 lines24 declarationsno sorryimports Mathlib.Analysis.SpecialFunctions.Gamma.Basic Mathlib.Tactic
  1. import Mathlib.Analysis.SpecialFunctions.Gamma.Basic
  2. import Mathlib.Tactic
  3. namespace ProductLayers
  4. /-- The largest part is listed first. This recursion also makes sense without sorting. -/
  5. def profileCost : List ℕ → ℕ
  6. | [] => 0
  7. | r::rs => r^2+(r+1)*profileCost rs
  8. def target : ℕ → ℕ
  9. | 0 => 0
  10. | n+1 => n*(n+1)+1
  11. theorem target_polynomial {k : ℕ} (hk : 0<k) : target k+k=k^2+1 := by
  12. obtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hk)
  13. simp [target]; ring
  14. theorem step_gap (r n : ℕ) :
  15. r^2+(r+1)*target (n+2) = target (r+(n+2)) + r*(n+1)*n := by
  16. simp [target, Nat.add_assoc]; ring
  17. private theorem step_one (r : ℕ) : r^2+(r+1)*target 1=target (r+1) := by
  18. simp [target]; ring
  19. theorem profile_lower_bound (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r) :
  20. target rs.sum ≤ profileCost rs := by
  21. induction rs with
  22. | nil => simp [target,profileCost]
  23. | cons r rs ih =>
  24. have hr := hp r (by simp)
  25. have ht := ih (by intro i hi; exact hp i (by simp [hi]))
  26. simp only [List.sum_cons,profileCost]
  27. apply le_trans _ (Nat.add_le_add_left (Nat.mul_le_mul_left (r+1) ht) _)
  28. generalize h : rs.sum = s
  29. cases s with
  30. | zero =>
  31. obtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hr)
  32. simp [target]; nlinarith
  33. | succ s =>
  34. cases s with
  35. | zero => exact (step_one r).symm.le
  36. | succ n => rw [step_gap]; exact Nat.le_add_right _ _
  37. private theorem sum_zero_tail (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r)
  38. (hs : rs.sum=0) : rs=[] := by
  39. cases rs with
  40. | nil => rfl
  41. | cons i t =>
  42. have := hp i (by simp)
  43. simp only [List.sum_cons] at hs
  44. omega
  45. private theorem sum_one_tail (rs : List ℕ) (hp : ∀ r ∈ rs, 0<r)
  46. (hs : rs.sum=1) : rs=[1] := by
  47. cases rs with
  48. | nil => simp at hs
  49. | cons i t =>
  50. have := hp i (by simp)
  51. simp only [List.sum_cons] at hs
  52. have ht := sum_zero_tail t (by intro z hz; exact hp z (by simp [hz])) (by omega)
  53. simp [ht, show i=1 by omega]
  54. theorem profile_equality (r : ℕ) (rs : List ℕ) (hr : 0<r) (hp : ∀ s ∈ rs, 0<s) :
  55. profileCost (r::rs) = target (r+rs.sum) ↔
  56. (r=1 ∧ rs=[]) ∨ rs=[1] ∨ rs=[1,1] := by
  57. constructor
  58. · intro he
  59. change r^2+(r+1)*profileCost rs = _ at he
  60. have ht := profile_lower_bound rs hp
  61. rcases rs.sum.eq_zero_or_pos with h|h
  62. · have hy := sum_zero_tail rs hp h
  63. subst rs
  64. simp [profileCost] at he
  65. obtain ⟨n,rfl⟩ := Nat.exists_eq_succ_of_ne_zero (Nat.ne_of_gt hr)
  66. simp [target] at he
  67. left; constructor <;> first | rfl | nlinarith
  68. · by_cases h1 : rs.sum=1
  69. · exact Or.inr (Or.inl (sum_one_tail rs hp h1))
  70. · have h2 : 2≤rs.sum := by omega
  71. let n := rs.sum-2
  72. have hn : rs.sum=n+2 := by dsimp [n]; omega
  73. rw [hn] at he ht
  74. have hle := Nat.add_le_add_left (Nat.mul_le_mul_left (r+1) ht) (r^2)
  75. rw [step_gap,he] at hle
  76. have hz : n=0 := by
  77. by_contra! hpos
  78. have hpos' : 0<r*(n+1)*n := by positivity
  79. omega
  80. have hs : rs.sum=2 := by omega
  81. cases rs with
  82. | nil => simp at hs
  83. | cons a t =>
  84. have ha := hp a (by simp)
  85. simp only [List.sum_cons] at hs
  86. by_cases hzero : t.sum=0
  87. · have hy := sum_zero_tail t (by intro z h'; exact hp z (by simp [h'])) hzero
  88. subst t
  89. have ha2 : a=2 := by simpa using hs
  90. subst a
  91. rw [hz] at he
  92. simp [profileCost,target] at he
  93. nlinarith
  94. · have ht' := sum_one_tail t (by intro z h'; exact hp z (by simp [h'])) (by omega)
  95. exact Or.inr (Or.inr (by simp [ht',show a=1 by omega]))
  96. · rintro (⟨rfl,rfl⟩|rfl|rfl) <;> simp [profileCost,target] <;> ring
  97. /-- Triangular prefix data distinguish the label indexed by every tuple of prefix lengths. -/
  98. theorem prefix_injective {d : ℕ} (r : Fin d → ℕ)
  99. (P : (i : Fin d) → Fin (r i+1) → Fin d → ℕ)
  100. (tri : ∀ (i : Fin d) (n : Fin (r i+1)) (j : Fin d), j < i → P i n j = 0)
  101. (diag : ∀ i, StrictMono (fun n => P i n i)) :
  102. Function.Injective (fun (z : (i : Fin d) → Fin (r i+1)) =>
  103. fun j => ∑ i, P i (z i) j) := by
  104. classical
  105. intro z w he
  106. by_contra! hn
  107. let T : Finset (Fin d) := Finset.univ.filter (fun i => z i ≠ w i)
  108. have hT : T.Nonempty := by
  109. by_contra! hem
  110. apply hn; funext i
  111. by_contra! h
  112. have hx : i ∈ T := by simp [T,h]
  113. simp [hem] at hx
  114. let i := T.min' hT
  115. have him : i ∈ T := Finset.min'_mem _ _
  116. have hi := (Finset.mem_filter.mp him).2
  117. have hrest : (∑ j ∈ Finset.univ.erase i, P j (z j) i) =
  118. ∑ j ∈ Finset.univ.erase i, P j (w j) i := by
  119. apply Finset.sum_congr rfl
  120. intro j hj
  121. rcases lt_or_gt_of_ne (Finset.mem_erase.mp hj).1 with hj'|hj'
  122. · have hjw : z j=w j := by
  123. by_contra! hm
  124. have hmem : j∈T := by simp [T,hm]
  125. exact (not_le_of_gt hj') (Finset.min'_le T j hmem)
  126. rw [hjw]
  127. · rw [tri j _ i hj', tri j _ i hj']
  128. have heq := congrFun he i
  129. change ∑ j, P j (z j) i = ∑ j, P j (w j) i at heq
  130. rw [← Finset.sum_erase_add _ _ (Finset.mem_univ i),
  131. ← Finset.sum_erase_add _ _ (Finset.mem_univ i), hrest] at heq
  132. exact hi ((diag i).injective (by omega))
  133. /-- Products of the parameter factors in the degree layers recover their full weighted product. -/
  134. theorem layer_product {ι : Type*} [Fintype ι] (d y : ι → ℕ) (M : ℕ) (hd : ∀ i, d i ≤ M) :
  135. (∏ h ∈ Finset.range M, ∏ i, if h < d i then y i else 1) = ∏ i, y i ^ d i := by
  136. classical
  137. rw [Finset.prod_comm]
  138. apply Finset.prod_congr rfl
  139. intro i hi
  140. have hr : (Finset.range M).filter (fun h => h < d i) = Finset.range (d i) := by
  141. have := hd i
  142. ext h; simp; omega
  143. rw [Finset.prod_ite]
  144. simp [hr]
  145. theorem layer_count {ι : Type*} [Fintype ι] (cost : ι → ℕ) (M : ℕ) (hc : ∀ i, cost i ≤ M) :
  146. (∑ h ∈ Finset.range M, ∑ i, if h < cost i then 1 else 0) = ∑ i, cost i := by
  147. classical
  148. rw [Finset.sum_comm]
  149. apply Finset.sum_congr rfl
  150. intro i hi
  151. have hr : (Finset.range M).filter (fun h => h < cost i) = Finset.range (cost i) := by
  152. have := hc i
  153. ext h; simp; omega
  154. rw [Finset.sum_ite]
  155. simp [hr]
  156. theorem main_dimensions (r : ℕ) :
  157. target (r+2)=r^2+3*(r+1) ∧
  158. profileCost [r,1,1] = target (r+2) ∧
  159. profileCost [r+1,1] = target (r+2) := by
  160. constructor
  161. · simp [target]; ring
  162. · constructor <;> simp [target,profileCost] <;> ring
  163. /-- The barycentre meets every proper-subset cut strictly in the proper-product paper. -/
  164. theorem strict_barycentre_cut {u k : ℕ} (hu : 0<u) (hk : u<k) :
  165. k*(2^u-1)<u*(2^k-1) := by
  166. obtain ⟨s,hs⟩ := Nat.exists_eq_add_of_le (show u+1≤k by omega)
  167. subst k
  168. induction s with
  169. | zero =>
  170. simp only [Nat.add_zero, pow_succ]
  171. have hp : 1 ≤ 2^u := Nat.one_le_pow _ _ (by decide)
  172. have hp2 : 1 ≤ 2^u*2 := by omega
  173. have hmul := Nat.mul_le_mul_right (2^u) hu
  174. have ha := Nat.sub_add_cancel hp
  175. have hb := Nat.sub_add_cancel hp2
  176. nlinarith only [hmul, congrArg (fun t => u*t) ha, congrArg (fun t => u*t) hb,ha]
  177. | succ s ih =>
  178. have hpow : 2^u ≤ 2^(u+1+s) := Nat.pow_le_pow_right (by decide) (by omega)
  179. have hmul := Nat.mul_le_mul_right (2^(u+1+s)) hu
  180. have hp : 1≤2^(u+1+s) := Nat.one_le_pow _ _ (by decide)
  181. have hstep : 2^(u+1+s+1)-1=(2^(u+1+s)-1)+2^(u+1+s) := by
  182. rw [pow_succ]; omega
  183. change (u+1+s+1)*(2^u-1) < u*(2^(u+1+s+1)-1)
  184. rw [hstep, Nat.mul_add, Nat.add_mul, one_mul]
  185. omega
  186. /-- Number of prefix choices, including the empty one, on blocks above a degree cut. -/
  187. def activeCount (h : ℕ) : List ℕ → ℕ
  188. | [] => 1
  189. | r::rs => (if h<r then r+1 else 1)*activeCount h rs
  190. private theorem active_pos (rs : List ℕ) (h : ℕ) : 0<activeCount h rs := by
  191. induction rs with
  192. | nil => simp [activeCount]
  193. | cons r rs ih =>
  194. simp only [activeCount]; split <;> positivity
  195. private theorem active_one (rs : List ℕ) (h : ℕ) (hb : ∀ r ∈ rs, r≤h) :
  196. activeCount h rs=1 := by
  197. induction rs with
  198. | nil => rfl
  199. | cons r rs ih =>
  200. simp [activeCount,not_lt_of_ge (hb r (by simp)),
  201. ih (by intro x hx; exact hb x (by simp [hx]))]
  202. private theorem active_step (r h : ℕ) (rs : List ℕ) (hb : ∀ z ∈ rs, z≤r) :
  203. activeCount h (r::rs)-1 =
  204. (r+1)*(activeCount h rs-1)+(if h<r then r else 0) := by
  205. by_cases hr : h<r
  206. · simp only [activeCount,if_pos hr]
  207. have h1 := Nat.sub_add_cancel (show 1≤activeCount h rs from active_pos rs h)
  208. have h2 : 1≤(r+1)*activeCount h rs := Nat.one_le_iff_ne_zero.mpr (by
  209. have := active_pos rs h; positivity)
  210. have h3 := Nat.sub_add_cancel h2
  211. nlinarith only [congrArg (fun t => (r+1)*t) h1, h3]
  212. · have h1 := active_one rs h (fun z hz => (hb z hz).trans (by omega))
  213. simp [activeCount,hr,h1]
  214. theorem profile_layer_formula (rs : List ℕ) (hs : rs.Pairwise (· ≥ ·)) (M : ℕ)
  215. (hm : ∀ r ∈ rs, r≤M) :
  216. (∑ h ∈ Finset.range M, (activeCount h rs-1)) = profileCost rs := by
  217. induction rs with
  218. | nil => simp [activeCount,profileCost]
  219. | cons r rs ih =>
  220. obtain ⟨hb,ho⟩ := List.pairwise_cons.mp hs
  221. have hi := ih ho (by intro z hz; exact hm z (by simp [hz]))
  222. have hstep := fun h => active_step r h rs hb
  223. simp_rw [hstep]
  224. rw [Finset.sum_add_distrib, ← Finset.mul_sum, hi, Finset.sum_ite]
  225. have hr : (Finset.range M).filter (fun h => h<r) = Finset.range r := by
  226. have := hm r (by simp)
  227. ext h; simp; omega
  228. simp [hr,profileCost,pow_two,add_comm]
  229. /-- Any label counts that contain the prefix choices at every level pay the profile bound. -/
  230. theorem counted_lower_bound (rs : List ℕ) (hs : rs.Pairwise (· ≥ ·))
  231. (hp : ∀ r ∈ rs, 0<r) (M : ℕ) (hm : ∀ r ∈ rs, r≤M)
  232. (counts : ℕ → ℕ) (hcounts : ∀ h<M, activeCount h rs≤counts h+1) :
  233. target rs.sum ≤ ∑ h ∈ Finset.range M, counts h := by
  234. apply (profile_lower_bound rs hp).trans
  235. rw [← profile_layer_formula rs hs M hm]
  236. apply Finset.sum_le_sum
  237. intro h hh
  238. have := hcounts h (Finset.mem_range.mp hh)
  239. omega
  240. open Filter
  241. open scoped Topology
  242. /-- Indexed subset products allow equal generator values at distinct indices. -/
  243. def labels {k : ℕ} (a : Fin k → ℕ) : Finset ℕ :=
  244. ((Finset.univ : Finset (Fin k)).powerset.erase ∅).image (fun u => u.prod a)
  245. def occurs (k : ℕ) (A : Finset ℕ) : Prop :=
  246. ∃ a : Fin k → ℕ, (∀ i, 0<a i) ∧ labels a ⊆ A
  247. noncomputable def appearanceProbability (k N : ℕ) (p : ℝ) : ℝ := by
  248. classical
  249. exact ∑ A ∈ (Finset.Icc 1 N).powerset, if occurs k A then p^A.card*(1-p)^(N-A.card) else 0
  250. /-- The statement of the written limiting law for lengths r+2, r at least two. -/
  251. def UnrestrictedLaw : Prop :=
  252. ∀ (r : ℕ), 2≤r → ∀ (p : ℕ → ℝ) (lam : ℝ),
  253. (∀ N, p N ∈ Set.Icc 0 1) → 0<lam →
  254. Tendsto (fun N : ℕ => (N:ℝ)*Real.log N*(p N)^(target (r+2))) atTop (𝓝 lam) →
  255. Tendsto (fun N => appearanceProbability (r+2) N (p N)) atTop
  256. (𝓝 (1-Real.exp (-Real.Gamma (1-1/(r:ℝ)) *
  257. (((r:ℝ)+1)/(2*(target (r+2):ℝ))*lam)^(1/(r:ℝ)))))
  258. #print axioms profile_layer_formula
  259. #print axioms counted_lower_bound
  260. #print axioms strict_barycentre_cut
  261. #print axioms profile_lower_bound
  262. #print axioms profile_equality
  263. #print axioms prefix_injective
  264. #print axioms layer_product
  265. #print axioms layer_count
  266. #print axioms main_dimensions
  267. end ProductLayers