Lean / Reader
Lean reader

Erdos424.lean

6,418 lines552 declarationsno sorryimports Mathlib
  1. /- leanprover/lean4:v4.33.0 mathlib v4.33.0 -/
  2. /-
  3. This is a Lean formalization of a solution to Erdős Problem 424.
  4. https://www.erdosproblems.com/forum/thread/424
  5. Informal authors:
  6. - ChatGPT 5.6 Pro
  7. - Samuel Korsky
  8. Formal authors:
  9. - Codex
  10. - Boris Alexeev
  11. URLs:
  12. - https://www.erdosproblems.com/forum/thread/424/proof-claims#proof-claim-91
  13. - https://drive.google.com/file/d/1SGSUQhNB8KL75VPqdfAq9yzFNEYL6Mwn/view
  14. - https://github.com/google-deepmind/formal-conjectures/blob/main/FormalConjectures/ErdosProblems/424.lean
  15. -/
  16. import Mathlib
  17. /-!
  18. # Erdős Problem 424 (Hofstadter's `ab - 1` problem)
  19. -/
  20. namespace Erdos424
  21. /-- The recursively generated set in Erdős Problem 424. Using an inductive
  22. predicate makes the phrase "the smallest set" literal: its constructors are
  23. exactly the two seeds and the closure rule. -/
  24. inductive Generated : ℕ → Prop where
  25. | two : Generated 2
  26. | three : Generated 3
  27. | mul_sub_one {a b : ℕ} : Generated a → Generated b → a ≠ b →
  28. Generated (a * b - 1)
  29. namespace Generated
  30. theorem five : Generated 5 := by
  31. simpa using mul_sub_one two three (by norm_num)
  32. theorem nine : Generated 9 := by
  33. simpa using mul_sub_one two five (by norm_num)
  34. theorem fourteen : Generated 14 := by
  35. simpa using mul_sub_one three five (by norm_num)
  36. theorem seventeen : Generated 17 := by
  37. simpa using mul_sub_one two nine (by norm_num)
  38. /-- Every generated number is positive. -/
  39. theorem pos {n : ℕ} (hn : Generated n) : 0 < n := by
  40. induction hn with
  41. | two => norm_num
  42. | three => norm_num
  43. | @mul_sub_one a b ha hb hab iha ihb =>
  44. have hab' : 1 < a * b := by
  45. rcases lt_or_gt_of_ne hab with hab | hab <;> nlinarith
  46. omega
  47. end Generated
  48. /-- The affine generator `T_a(x) = ax - 1`. -/
  49. def T (a x : ℕ) : ℕ := a * x - 1
  50. /-- Apply a word of affine generators from left to right. -/
  51. def eval : List ℕ → ℕ → ℕ
  52. | [], x => x
  53. | a :: w, x => eval w (T a x)
  54. @[simp] theorem eval_cons (a : ℕ) (w : List ℕ) (x : ℕ) :
  55. eval (a :: w) x = eval w (T a x) := rfl
  56. theorem lt_T_self {a x : ℕ} (ha : 2 ≤ a) (hx : 1 < x) : x < T a x := by
  57. simp only [T]
  58. have hax : x + 2 ≤ a * x := by nlinarith
  59. omega
  60. /-- Starting above every multiplier, a word made from generated multipliers
  61. can be evaluated inside the generated set. The strict growth simultaneously
  62. guarantees that every use of the closure rule has distinct inputs. -/
  63. theorem eval_generated_of_lt
  64. (w : List ℕ) {x : ℕ} (hx : Generated x) (hx1 : 1 < x)
  65. (hgen : ∀ a ∈ w, Generated a) (htwo : ∀ a ∈ w, 2 ≤ a)
  66. (hlt : ∀ a ∈ w, a < x) :
  67. Generated (eval w x) ∧ x ≤ eval w x := by
  68. induction w generalizing x with
  69. | nil => exact ⟨hx, le_rfl⟩
  70. | cons a w ih =>
  71. have ha_gen : Generated a := hgen a (by simp)
  72. have ha_two : 2 ≤ a := htwo a (by simp)
  73. have ha_lt : a < x := hlt a (by simp)
  74. have hxT : x < T a x := lt_T_self ha_two hx1
  75. have ha_ne_x : a ≠ x := Nat.ne_of_lt ha_lt
  76. have hT_gen : Generated (T a x) := by
  77. exact Generated.mul_sub_one ha_gen hx ha_ne_x
  78. have hT1 : 1 < T a x := hx1.trans hxT
  79. have hgen' : ∀ b ∈ w, Generated b := by
  80. intro b hb
  81. exact hgen b (by simp [hb])
  82. have htwo' : ∀ b ∈ w, 2 ≤ b := by
  83. intro b hb
  84. exact htwo b (by simp [hb])
  85. have hlt' : ∀ b ∈ w, b < T a x := by
  86. intro b hb
  87. exact (hlt b (by simp [hb])).trans_le hxT.le
  88. obtain ⟨hfinal, hmono⟩ := ih hT_gen hT1 hgen' htwo' hlt'
  89. exact ⟨hfinal, hxT.le.trans hmono⟩
  90. /-- The five multipliers used by the paper. -/
  91. def multipliers : Finset ℕ := {2, 3, 5, 9, 14}
  92. theorem mem_multipliers_generated {a : ℕ} (ha : a ∈ multipliers) : Generated a := by
  93. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  94. rcases ha with rfl | rfl | rfl | rfl | rfl
  95. · exact Generated.two
  96. · exact Generated.three
  97. · exact Generated.five
  98. · exact Generated.nine
  99. · exact Generated.fourteen
  100. theorem mem_multipliers_two_le {a : ℕ} (ha : a ∈ multipliers) : 2 ≤ a := by
  101. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  102. rcases ha with rfl | rfl | rfl | rfl | rfl <;> norm_num
  103. theorem mem_multipliers_lt_seventeen {a : ℕ} (ha : a ∈ multipliers) : a < 17 := by
  104. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  105. rcases ha with rfl | rfl | rfl | rfl | rfl <;> norm_num
  106. /-- Every word over the paper's five multipliers evaluates at 17 to an element
  107. of the generated set, with distinct inputs at every operation. -/
  108. theorem eval_at_seventeen_generated (w : List ℕ)
  109. (hw : ∀ a ∈ w, a ∈ multipliers) : Generated (eval w 17) := by
  110. exact (eval_generated_of_lt w Generated.seventeen (by norm_num)
  111. (fun a ha ↦ mem_multipliers_generated (hw a ha))
  112. (fun a ha ↦ mem_multipliers_two_le (hw a ha))
  113. (fun a ha ↦ mem_multipliers_lt_seventeen (hw a ha))).1
  114. /-! ## Affine words -/
  115. /-- Evaluation over the integers, where subtraction does not truncate. -/
  116. def evalInt : List ℕ → ℤ → ℤ
  117. | [], x => x
  118. | a :: w, x => evalInt w ((a : ℤ) * x - 1)
  119. /-- Constant term of the affine map represented by a word. -/
  120. def offset : List ℕ → ℤ
  121. | [] => 0
  122. | _ :: w => offset w - (w.prod : ℤ)
  123. @[simp] theorem evalInt_cons (a : ℕ) (w : List ℕ) (x : ℤ) :
  124. evalInt (a :: w) x = evalInt w ((a : ℤ) * x - 1) := rfl
  125. /-- A word acts as `x ↦ slope * x + offset`. -/
  126. theorem evalInt_affine (w : List ℕ) (x : ℤ) :
  127. evalInt w x = (w.prod : ℤ) * x + offset w := by
  128. induction w generalizing x with
  129. | nil =>
  130. simp only [evalInt, offset, List.prod_nil, Nat.cast_one, one_mul, add_zero]
  131. | cons a w ih =>
  132. rw [evalInt_cons, ih]
  133. simp only [List.prod_cons, Nat.cast_mul, offset]
  134. ring
  135. /-- On inputs above 1, integer evaluation agrees with natural-number
  136. evaluation for words whose multipliers are at least 2. -/
  137. theorem eval_cast_eq_evalInt (w : List ℕ) {x : ℕ} (hx : 1 < x)
  138. (htwo : ∀ a ∈ w, 2 ≤ a) : (eval w x : ℤ) = evalInt w x := by
  139. induction w generalizing x with
  140. | nil => rfl
  141. | cons a w ih =>
  142. have ha : 2 ≤ a := htwo a (by simp)
  143. have hxT : x < T a x := lt_T_self ha hx
  144. have htwo' : ∀ b ∈ w, 2 ≤ b := by
  145. intro b hb
  146. exact htwo b (by simp [hb])
  147. rw [eval_cons, evalInt_cons, ih (hx.trans hxT) htwo']
  148. simp only [T]
  149. rw [Int.ofNat_sub]
  150. · norm_num
  151. · nlinarith
  152. /-- Two words of the same slope but different offsets give different values
  153. at every natural input where no subtraction truncates. -/
  154. theorem eval_ne_of_prod_eq_offset_ne
  155. {u v : List ℕ} {x : ℕ} (hx : 1 < x)
  156. (hu : ∀ a ∈ u, 2 ≤ a) (hv : ∀ a ∈ v, 2 ≤ a)
  157. (hprod : u.prod = v.prod) (hoffset : offset u ≠ offset v) :
  158. eval u x ≠ eval v x := by
  159. intro heval
  160. have hcast : (eval u x : ℤ) = (eval v x : ℤ) := by exact_mod_cast heval
  161. rw [eval_cast_eq_evalInt u hx hu, eval_cast_eq_evalInt v hx hv,
  162. evalInt_affine, evalInt_affine, hprod] at hcast
  163. exact hoffset (add_left_cancel hcast)
  164. /-! ## Prime exponents and the common slope -/
  165. /-- Exponents of `2,3,5,7` contributed by one allowed multiplier. -/
  166. def primeIncrement (a : ℕ) : Fin 4 → ℕ :=
  167. match a with
  168. | 2 => ![1, 0, 0, 0]
  169. | 3 => ![0, 1, 0, 0]
  170. | 5 => ![0, 0, 1, 0]
  171. | 9 => ![0, 2, 0, 0]
  172. | 14 => ![1, 0, 0, 1]
  173. | _ => ![0, 0, 0, 0]
  174. /-- Prime-exponent vector accumulated along a word. -/
  175. def exponents (w : List ℕ) : Fin 4 → ℕ :=
  176. fun i ↦ (w.map fun a ↦ primeIncrement a i).sum
  177. @[simp] theorem exponents_nil (i : Fin 4) : exponents [] i = 0 := by
  178. simp [exponents]
  179. @[simp] theorem exponents_cons (a : ℕ) (w : List ℕ) (i : Fin 4) :
  180. exponents (a :: w) i = primeIncrement a i + exponents w i := by
  181. simp [exponents]
  182. theorem allowed_factorization {a : ℕ} (ha : a ∈ multipliers) :
  183. a = 2 ^ primeIncrement a 0 * 3 ^ primeIncrement a 1 *
  184. 5 ^ primeIncrement a 2 * 7 ^ primeIncrement a 3 := by
  185. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  186. rcases ha with rfl | rfl | rfl | rfl | rfl <;> decide
  187. /-- Factorization of the slope of an allowed word. -/
  188. theorem prod_factorization (w : List ℕ) (hw : ∀ a ∈ w, a ∈ multipliers) :
  189. w.prod = 2 ^ exponents w 0 * 3 ^ exponents w 1 *
  190. 5 ^ exponents w 2 * 7 ^ exponents w 3 := by
  191. induction w with
  192. | nil => norm_num [exponents]
  193. | cons a w ih =>
  194. have ha : a ∈ multipliers := hw a (by simp)
  195. have hw' : ∀ b ∈ w, b ∈ multipliers := by
  196. intro b hb
  197. exact hw b (by simp [hb])
  198. rw [List.prod_cons, ih hw']
  199. simp only [exponents_cons]
  200. nth_rewrite 1 [allowed_factorization ha]
  201. simp only [pow_add]
  202. ring
  203. /-- The imbalance vector `H` from the paper. -/
  204. def imbalance (w : List ℕ) : Fin 3 → ℤ :=
  205. ![(exponents w 0 : ℤ) - 31 * exponents w 3,
  206. (exponents w 1 : ℤ) - 26 * exponents w 3,
  207. (exponents w 2 : ℤ) - 11 * exponents w 3]
  208. /-- The common base slope `q = 2^31 3^26 5^11 7`. -/
  209. def q : ℕ := 2 ^ 31 * 3 ^ 26 * 5 ^ 11 * 7
  210. theorem exponents_eq_of_imbalance_eq_zero {w : List ℕ}
  211. (hH : imbalance w = 0) :
  212. exponents w 0 = 31 * exponents w 3 ∧
  213. exponents w 1 = 26 * exponents w 3 ∧
  214. exponents w 2 = 11 * exponents w 3 := by
  215. have h0 := congrFun hH (0 : Fin 3)
  216. have h1 := congrFun hH (1 : Fin 3)
  217. have h2 := congrFun hH (2 : Fin 3)
  218. simp [imbalance, Matrix.cons_val_zero, Matrix.cons_val_one] at h0 h1 h2
  219. omega
  220. /-- Returning to the same imbalance forces slope `q^m`. -/
  221. theorem prod_eq_q_pow_of_imbalance_eq_zero (w : List ℕ)
  222. (hw : ∀ a ∈ w, a ∈ multipliers) (hH : imbalance w = 0) :
  223. w.prod = q ^ exponents w 3 := by
  224. obtain ⟨h2, h3, h5⟩ := exponents_eq_of_imbalance_eq_zero hH
  225. rw [prod_factorization w hw, h2, h3, h5]
  226. simp only [q, mul_pow, pow_mul]
  227. /-! ## The finite interval coding -/
  228. /-- The twenty rational endpoints displayed in Section 2 of the paper. -/
  229. def endpoint : Fin 20 → ℚ :=
  230. ![1 / 9, 10 / 81, 1 / 7, 1 / 5, 2 / 9, 2 / 7, 1 / 3,
  231. 10 / 27, 2 / 5, 11 / 27, 3 / 7, 37 / 81, 5 / 9, 3 / 5,
  232. 2 / 3, 19 / 27, 59 / 81, 4 / 5, 23 / 27, 1]
  233. def leftIndex (i : Fin 19) : Fin 20 := ⟨i, by omega⟩
  234. def rightIndex (i : Fin 19) : Fin 20 := ⟨i + 1, by omega⟩
  235. /-- The half-open state interval `K_i`; Lean uses zero-based indices. -/
  236. def K (i : Fin 19) : Set ℚ := Set.Ico (endpoint (leftIndex i)) (endpoint (rightIndex i))
  237. def width (i : Fin 19) : ℚ := endpoint (rightIndex i) - endpoint (leftIndex i)
  238. theorem endpoint_strictMono : StrictMono endpoint := by
  239. rw [Fin.strictMono_iff_lt_succ]
  240. intro i
  241. fin_cases i <;>
  242. norm_num [endpoint, Matrix.cons_val_two, Matrix.cons_val_three]
  243. theorem width_pos (i : Fin 19) : 0 < width i := by
  244. exact sub_pos.mpr (endpoint_strictMono (by simp [leftIndex, rightIndex]))
  245. structure Transition where
  246. source : Fin 19
  247. multiplier : ℕ
  248. first : Fin 19
  249. last : Fin 19
  250. /-- The 23 rows in the interval-transition table. -/
  251. def transitions : List Transition :=
  252. [⟨0, 14, 12, 15⟩,
  253. ⟨1, 14, 16, 18⟩, ⟨1, 9, 0, 4⟩,
  254. ⟨2, 9, 5, 16⟩,
  255. ⟨3, 9, 17, 18⟩,
  256. ⟨4, 5, 0, 9⟩,
  257. ⟨5, 5, 10, 13⟩,
  258. ⟨6, 5, 14, 17⟩,
  259. ⟨7, 5, 18, 18⟩, ⟨7, 3, 0, 2⟩,
  260. ⟨8, 3, 3, 3⟩,
  261. ⟨9, 3, 4, 4⟩,
  262. ⟨10, 3, 5, 6⟩,
  263. ⟨11, 3, 7, 13⟩,
  264. ⟨12, 3, 14, 16⟩, ⟨12, 2, 0, 2⟩,
  265. ⟨13, 3, 17, 18⟩, ⟨13, 2, 3, 5⟩,
  266. ⟨14, 2, 6, 8⟩,
  267. ⟨15, 2, 9, 10⟩,
  268. ⟨16, 2, 11, 12⟩,
  269. ⟨17, 2, 13, 14⟩,
  270. ⟨18, 2, 15, 18⟩]
  271. def T_rat (a : ℕ) (x : ℚ) : ℚ := a * x - 1
  272. /-- Endpoint form of an exact interval identity. Positivity of the multiplier
  273. then says that the image of the source interval is exactly the consecutive
  274. block from `first` through `last`. -/
  275. def Transition.Exact (tr : Transition) : Prop :=
  276. tr.first ≤ tr.last ∧ 0 < tr.multiplier ∧
  277. T_rat tr.multiplier (endpoint (leftIndex tr.source)) =
  278. endpoint (leftIndex tr.first) ∧
  279. T_rat tr.multiplier (endpoint (rightIndex tr.source)) =
  280. endpoint (rightIndex tr.last)
  281. /-- Machine-checked certificate for every rational interval identity in the
  282. table. -/
  283. theorem transitions_exact : ∀ tr ∈ transitions, tr.Exact := by
  284. intro tr htr
  285. simp only [transitions, List.mem_cons, List.not_mem_nil, or_false] at htr
  286. rcases htr with
  287. rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl |
  288. rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl | rfl
  289. all_goals
  290. simp [Transition.Exact, T_rat, endpoint, leftIndex, rightIndex,
  291. Matrix.cons_val_two, Matrix.cons_val_three] ; norm_num
  292. /-- The four choices `α₀,...,α₃`. -/
  293. abbrev Assignment := Fin 4
  294. /-- Multiplier prescribed by an assignment at a state. -/
  295. def prescribed (α : Assignment) (i : Fin 19) : ℕ :=
  296. if i = 0 then 14
  297. else if i = 1 then if α = 1 then 9 else 14
  298. else if i = 2 ∨ i = 3 then 9
  299. else if i = 4 ∨ i = 5 ∨ i = 6 then 5
  300. else if i = 7 then if α = 2 then 3 else 5
  301. else if i = 8 ∨ i = 9 ∨ i = 10 ∨ i = 11 then 3
  302. else if i = 12 ∨ i = 13 then if α = 3 then 2 else 3
  303. else 2
  304. theorem prescribed_mem_multipliers (α : Assignment) (i : Fin 19) :
  305. prescribed α i ∈ multipliers := by decide +revert
  306. theorem prescribed_ge_two (α : Assignment) (i : Fin 19) : 2 ≤ prescribed α i :=
  307. mem_multipliers_two_le (prescribed_mem_multipliers α i)
  308. /-- A destination state allowed by the selected row of the table. -/
  309. def allowedNext (α : Assignment) (i j : Fin 19) : Prop :=
  310. transitions.any (fun tr ↦ decide (tr.source = i ∧
  311. tr.multiplier = prescribed α i ∧ tr.first ≤ j ∧ j ≤ tr.last)) = true
  312. instance (α : Assignment) (i j : Fin 19) : Decidable (allowedNext α i j) := by
  313. unfold allowedNext
  314. infer_instance
  315. /-- Place-dependent transition probability from Section 2. -/
  316. def probability (α : Assignment) (i j : Fin 19) : ℚ :=
  317. if allowedNext α i j then width j / (prescribed α i * width i) else 0
  318. set_option maxHeartbeats 1000000 in
  319. -- For some reason, adding 19 rational numbers is nontrivial.
  320. private theorem probability_row_sum_zero (i : Fin 19) :
  321. ∑ j : Fin 19, probability 0 i j = 1 := by
  322. fin_cases i <;>
  323. simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>
  324. norm_num [width, endpoint, leftIndex, rightIndex,
  325. Matrix.cons_val_two, Matrix.cons_val_three]
  326. set_option maxHeartbeats 1000000 in
  327. -- For some reason, adding 19 rational numbers is nontrivial.
  328. private theorem probability_row_sum_one (i : Fin 19) :
  329. ∑ j : Fin 19, probability 1 i j = 1 := by
  330. fin_cases i <;>
  331. simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>
  332. norm_num [width, endpoint, leftIndex, rightIndex,
  333. Matrix.cons_val_two, Matrix.cons_val_three]
  334. set_option maxHeartbeats 1000000 in
  335. -- For some reason, adding 19 rational numbers is nontrivial.
  336. private theorem probability_row_sum_two (i : Fin 19) :
  337. ∑ j : Fin 19, probability 2 i j = 1 := by
  338. fin_cases i <;>
  339. simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>
  340. norm_num [width, endpoint, leftIndex, rightIndex,
  341. Matrix.cons_val_two, Matrix.cons_val_three]
  342. set_option maxHeartbeats 1000000 in
  343. -- For some reason, adding 19 rational numbers is nontrivial.
  344. private theorem probability_row_sum_three (i : Fin 19) :
  345. ∑ j : Fin 19, probability 3 i j = 1 := by
  346. fin_cases i <;>
  347. simp [probability, allowedNext, transitions, prescribed, Fin.sum_univ_succ] <;>
  348. norm_num [width, endpoint, leftIndex, rightIndex,
  349. Matrix.cons_val_two, Matrix.cons_val_three]
  350. /-- The transition probabilities in every row sum to one. -/
  351. theorem probability_row_sum (α : Assignment) (i : Fin 19) :
  352. ∑ j : Fin 19, probability α i j = 1 := by
  353. fin_cases α
  354. · exact probability_row_sum_zero i
  355. · exact probability_row_sum_one i
  356. · exact probability_row_sum_two i
  357. · exact probability_row_sum_three i
  358. /-! ## Stationary distributions and drift data -/
  359. /-- Increment of the imbalance vector caused by one multiplier. -/
  360. def imbalanceIncrement (a : ℕ) : Fin 3 → ℚ :=
  361. ![(primeIncrement a 0 : ℚ) - 31 * primeIncrement a 3,
  362. (primeIncrement a 1 : ℚ) - 26 * primeIncrement a 3,
  363. (primeIncrement a 2 : ℚ) - 11 * primeIncrement a 3]
  364. /-- The four stationary mean increments printed in the paper. -/
  365. def listedDrift (α : Assignment) : Fin 3 → ℚ :=
  366. match α with
  367. | 0 => ![82502/209179, 60472/209179, 22315/209179]
  368. | 1 => ![4122816/9240032, 3176276/9240032, 1187745/9240032]
  369. | 2 => ![-17374/154297, 2952/154297, -12847/154297]
  370. | 3 => ![-410774/938829, -384064/938829, -82315/938829]
  371. /-- Positive barycentric coordinates witnessing that zero lies strictly
  372. inside the tetrahedron of drift vectors. -/
  373. def baryWeight : Fin 4 → ℚ :=
  374. ![90039171528246091/965033798855459200,
  375. 48712935304722/153863807215475,
  376. 114688358060991/465076529568896,
  377. 118388840756943/344654928162664]
  378. theorem baryWeight_pos (α : Assignment) : 0 < baryWeight α := by
  379. fin_cases α <;> norm_num [baryWeight]
  380. /-- Exact positive barycentric relation among the four drift vectors. -/
  381. theorem weighted_drift_sum (k : Fin 3) :
  382. ∑ α : Assignment, baryWeight α * listedDrift α k = 0 := by
  383. fin_cases k <;>
  384. norm_num [baryWeight, listedDrift, Fin.sum_univ_succ,
  385. Matrix.cons_val_two, Matrix.cons_val_three] <;>
  386. simp <;> norm_num
  387. /-- Number of generated positive integers at most `x`. -/
  388. noncomputable def countUpTo (x : ℕ) : ℕ := by
  389. classical
  390. exact ((Finset.Icc 1 x).filter Generated).card
  391. /-- The exact conclusion called "positive lower density" in the paper. -/
  392. def HasPositiveLowerDensity : Prop :=
  393. ∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,
  394. c * (x : ℝ) ≤ (countUpTo x : ℝ)
  395. /-! ## From common-slope affine words to density -/
  396. theorem prod_pos_of_one_le (w : List ℕ) (hw : ∀ a ∈ w, 1 ≤ a) : 0 < w.prod := by
  397. induction w with
  398. | nil => simp
  399. | cons a w ih =>
  400. have ha : 1 ≤ a := hw a (by simp)
  401. have htail : ∀ b ∈ w, 1 ≤ b := by
  402. intro b hb
  403. exact hw b (by simp [hb])
  404. simp only [List.prod_cons]
  405. exact Nat.mul_pos (by omega) (ih htail)
  406. theorem eval_le_prod_mul (w : List ℕ) {x : ℕ} (hw : ∀ a ∈ w, 1 ≤ a) :
  407. eval w x ≤ w.prod * x := by
  408. induction w generalizing x with
  409. | nil =>
  410. simp only [eval, List.prod_nil, one_mul]
  411. exact le_rfl
  412. | cons a w ih =>
  413. have htail : ∀ b ∈ w, 1 ≤ b := by
  414. intro b hb
  415. exact hw b (by simp [hb])
  416. calc
  417. eval (a :: w) x = eval w (T a x) := rfl
  418. _ ≤ w.prod * T a x := ih htail
  419. _ ≤ w.prod * (a * x) := Nat.mul_le_mul_left _ (Nat.sub_le _ _)
  420. _ = (a :: w).prod * x := by simp [mul_assoc, mul_left_comm]
  421. theorem countUpTo_mono : Monotone countUpTo := by
  422. intro x y hxy
  423. classical
  424. simp only [countUpTo]
  425. apply Finset.card_le_card
  426. intro n hn
  427. simp only [Finset.mem_filter, Finset.mem_Icc] at hn ⊢
  428. exact ⟨⟨hn.1.1, hn.1.2.trans hxy⟩, hn.2⟩
  429. theorem card_le_countUpTo {S : Finset ℕ} {x : ℕ}
  430. (hS : ∀ n ∈ S, Generated n ∧ n ≤ x) : S.card ≤ countUpTo x := by
  431. classical
  432. simp only [countUpTo]
  433. apply Finset.card_le_card
  434. intro n hn
  435. have h := hS n hn
  436. simp only [Finset.mem_filter, Finset.mem_Icc]
  437. exact ⟨⟨Generated.pos h.1, h.2⟩, h.1⟩
  438. /-! ## Filling the gaps between geometric scales -/
  439. /-- A lower bound by a fixed proportion at all sufficiently large geometric
  440. scales implies the paper's positive-lower-density conclusion. -/
  441. theorem positiveLowerDensity_of_geometric_count {base denominator : ℕ}
  442. (hbase : 1 < base) (hdenominator : 0 < denominator)
  443. (hgeom : ∃ r₀ : ℕ, ∀ r ≥ r₀,
  444. base ^ r ≤ denominator * countUpTo (17 * base ^ r)) :
  445. HasPositiveLowerDensity := by
  446. obtain ⟨r₀, hgeom⟩ := hgeom
  447. let c : ℝ := 1 / (17 * base * denominator : ℕ)
  448. refine ⟨c, ?_, ?_⟩
  449. · dsimp [c]
  450. positivity
  451. · filter_upwards [Filter.eventually_ge_atTop (17 * base ^ r₀)] with x hx
  452. let y := x / 17
  453. let r := Nat.log base y
  454. have hyPow : base ^ r₀ ≤ y := by
  455. dsimp [y]
  456. apply (Nat.le_div_iff_mul_le (by norm_num : 0 < 17)).2
  457. simpa [mul_comm] using hx
  458. have hy : y ≠ 0 := by
  459. intro hy0
  460. rw [hy0] at hyPow
  461. exact (Nat.not_succ_le_zero _)
  462. ((Nat.pow_pos (Nat.zero_lt_one.trans hbase)).trans_le hyPow)
  463. have hr : r₀ ≤ r := by
  464. exact Nat.le_log_of_pow_le hbase hyPow
  465. have hscale : 17 * base ^ r ≤ x := by
  466. have hp := Nat.pow_log_le_self base hy
  467. have hmul : 17 * base ^ r ≤ 17 * y := Nat.mul_le_mul_left 17 hp
  468. exact hmul.trans (Nat.mul_div_le x 17)
  469. have hnext : y < base ^ (r + 1) := by
  470. simpa [r, Nat.succ_eq_add_one] using Nat.lt_pow_succ_log_self hbase y
  471. have hxnext : x < 17 * base ^ (r + 1) := by
  472. calc
  473. x < 17 * (x / 17 + 1) := Nat.lt_mul_div_succ x (by norm_num)
  474. _ = 17 * (y + 1) := by rfl
  475. _ ≤ 17 * base ^ (r + 1) := Nat.mul_le_mul_left 17 hnext
  476. have hcountMono : countUpTo (17 * base ^ r) ≤ countUpTo x :=
  477. countUpTo_mono hscale
  478. have hgeomr := hgeom r hr
  479. have hpowCount : base ^ r ≤ denominator * countUpTo x :=
  480. hgeomr.trans (Nat.mul_le_mul_left denominator hcountMono)
  481. have hxCount : x ≤ 17 * base * (denominator * countUpTo x) := by
  482. calc
  483. x ≤ 17 * base ^ (r + 1) := hxnext.le
  484. _ = 17 * base * base ^ r := by rw [pow_succ]; ring
  485. _ ≤ 17 * base * (denominator * countUpTo x) :=
  486. Nat.mul_le_mul_left (17 * base) hpowCount
  487. have hxCount' : x ≤ (17 * base * denominator) * countUpTo x := by
  488. calc
  489. x ≤ 17 * base * (denominator * countUpTo x) := hxCount
  490. _ = (17 * base * denominator) * countUpTo x := by ring
  491. have hxCountReal : (x : ℝ) ≤
  492. (17 * base * denominator : ℕ) * (countUpTo x : ℝ) := by
  493. exact_mod_cast hxCount'
  494. dsimp [c]
  495. rw [one_div, inv_mul_eq_div]
  496. apply (div_le_iff₀ ?_).2
  497. · simpa [mul_assoc, mul_comm, mul_left_comm] using hxCountReal
  498. · positivity
  499. /-! ## Typed interval paths and the telescoping identity -/
  500. /-- A path through the interval graph. The endpoints are type indices, so
  501. composability is enforced by Lean. Each step stores the assignment used and
  502. proof that its transition is allowed. -/
  503. inductive IntervalPath : Fin 19 → Fin 19 → Type where
  504. | nil (i : Fin 19) : IntervalPath i i
  505. | cons {i j k : Fin 19} (α : Assignment) (h : allowedNext α i j)
  506. (tail : IntervalPath j k) : IntervalPath i k
  507. namespace IntervalPath
  508. def word {i j : Fin 19} : IntervalPath i j → List ℕ
  509. | .nil _ => []
  510. | .cons α _ tail => prescribed α i :: tail.word
  511. def mass {i j : Fin 19} : IntervalPath i j → ℚ
  512. | .nil _ => 1
  513. | @IntervalPath.cons i j _ α _ tail => probability α i j * tail.mass
  514. @[simp] theorem word_cons {i j k : Fin 19} (α : Assignment)
  515. (h : allowedNext α i j) (tail : IntervalPath j k) :
  516. (IntervalPath.cons α h tail).word = prescribed α i :: tail.word := rfl
  517. @[simp] theorem mass_cons {i j k : Fin 19} (α : Assignment)
  518. (h : allowedNext α i j) (tail : IntervalPath j k) :
  519. (IntervalPath.cons α h tail).mass = probability α i j * tail.mass := rfl
  520. theorem probability_of_allowed {α : Assignment} {i j : Fin 19}
  521. (h : allowedNext α i j) :
  522. probability α i j = width j / (prescribed α i * width i) := by
  523. simp [probability, h]
  524. theorem word_mem_multipliers {i j : Fin 19} (p : IntervalPath i j) :
  525. ∀ a ∈ p.word, a ∈ multipliers := by
  526. induction p with
  527. | nil =>
  528. intro a ha
  529. simp only [word, List.not_mem_nil] at ha
  530. | @cons i j k α h tail ih =>
  531. intro a ha
  532. simp only [word_cons, List.mem_cons] at ha
  533. rcases ha with rfl | ha
  534. · exact prescribed_mem_multipliers α i
  535. · exact ih a ha
  536. theorem word_two_le {i j : Fin 19} (p : IntervalPath i j) :
  537. ∀ a ∈ p.word, 2 ≤ a := by
  538. intro a ha
  539. exact mem_multipliers_two_le (p.word_mem_multipliers a ha)
  540. /-- Equation (1): transition probabilities telescope to the ratio of endpoint
  541. interval lengths divided by the affine slope. The assignments may vary from
  542. step to step. -/
  543. theorem mass_eq_width_div {i j : Fin 19} (p : IntervalPath i j) :
  544. p.mass = width j / ((p.word.prod : ℚ) * width i) := by
  545. induction p with
  546. | nil i =>
  547. simp only [mass, word, List.prod_nil, Nat.cast_one, one_mul]
  548. field_simp [ne_of_gt (width_pos i)]
  549. | @cons i j k α h tail ih =>
  550. rw [mass_cons, probability_of_allowed h, ih, word_cons, List.prod_cons,
  551. Nat.cast_mul]
  552. have hi : width i ≠ 0 := ne_of_gt (width_pos i)
  553. have hj : width j ≠ 0 := ne_of_gt (width_pos j)
  554. have ha : (prescribed α i : ℚ) ≠ 0 := by
  555. exact_mod_cast (show prescribed α i ≠ 0 by
  556. have := prescribed_ge_two α i
  557. omega)
  558. have hp : (tail.word.prod : ℚ) ≠ 0 := by
  559. exact_mod_cast (Nat.ne_of_gt (prod_pos_of_one_le tail.word
  560. (fun a ha ↦ (tail.word_two_le a ha).trans' (by omega))))
  561. field_simp
  562. end IntervalPath
  563. /-! ## Finite-chain certificates -/
  564. theorem probability_nonneg (α : Assignment) (i j : Fin 19) :
  565. 0 ≤ probability α i j := by
  566. by_cases h : allowedNext α i j
  567. · rw [IntervalPath.probability_of_allowed h]
  568. have ha : (0 : ℚ) ≤ prescribed α i := by positivity
  569. exact div_nonneg (width_pos j).le (mul_nonneg ha (width_pos i).le)
  570. · simp [probability, h]
  571. /-! ## The drift tetrahedron -/
  572. def listedDriftReal (α : Assignment) (k : Fin 3) : ℝ := listedDrift α k
  573. def driftDot (v : Fin 3 → ℝ) (α : Assignment) : ℝ :=
  574. ∑ k : Fin 3, v k * listedDriftReal α k
  575. theorem weighted_drift_real (k : Fin 3) :
  576. ∑ α : Assignment, (baryWeight α : ℝ) * listedDriftReal α k = 0 := by
  577. simpa [listedDriftReal] using
  578. congrArg (fun x : ℚ ↦ (x : ℝ)) (weighted_drift_sum k)
  579. theorem weighted_driftDot (v : Fin 3 → ℝ) :
  580. ∑ α : Assignment, (baryWeight α : ℝ) * driftDot v α = 0 := by
  581. calc
  582. ∑ α : Assignment, (baryWeight α : ℝ) * driftDot v α =
  583. ∑ k : Fin 3, v k *
  584. (∑ α : Assignment, (baryWeight α : ℝ) * listedDriftReal α k) := by
  585. simp only [driftDot, Finset.mul_sum]
  586. rw [Finset.sum_comm]
  587. apply Finset.sum_congr rfl
  588. intro k _
  589. apply Finset.sum_congr rfl
  590. intro α _
  591. ring
  592. _ = 0 := by simp [weighted_drift_real]
  593. /-- The first three drift vectors span `ℝ³`. -/
  594. theorem eq_zero_of_first_three_driftDot_eq_zero {v : Fin 3 → ℝ}
  595. (h0 : driftDot v 0 = 0) (h1 : driftDot v 1 = 0)
  596. (h2 : driftDot v 2 = 0) : v = 0 := by
  597. norm_num [driftDot, listedDriftReal, listedDrift, Fin.sum_univ_succ] at h0 h1 h2
  598. funext k
  599. fin_cases k
  600. · change v 0 = 0
  601. linear_combination
  602. (2317276647762287 / 49432245464000 : ℝ) * h0 -
  603. (620783464892 / 15762833375 : ℝ) * h1 -
  604. (1460755466599 / 1977289818560 : ℝ) * h2
  605. · change v 1 = 0
  606. linear_combination
  607. -(3381371791278219 / 98864490928000 : ℝ) * h0 +
  608. (495150792302 / 15762833375 : ℝ) * h1 +
  609. (18486937517763 / 3954579637120 : ℝ) * h2
  610. · change v 2 = 0
  611. linear_combination
  612. -(1761160941937753 / 24716122732000 : ℝ) * h0 +
  613. (953310271496 / 15762833375 : ℝ) * h1 -
  614. (9824239195199 / 988644909280 : ℝ) * h2
  615. /-- Pointwise form of the statement that zero is in the interior of the drift
  616. tetrahedron: every nonzero direction has strictly negative dot product with at
  617. least one drift. -/
  618. theorem exists_driftDot_neg {v : Fin 3 → ℝ} (hv : v ≠ 0) :
  619. ∃ α : Assignment, driftDot v α < 0 := by
  620. by_contra h
  621. simp only [not_exists, not_lt] at h
  622. have hterm : ∀ α ∈ (Finset.univ : Finset Assignment),
  623. 0 ≤ (baryWeight α : ℝ) * driftDot v α := by
  624. intro α _
  625. exact mul_nonneg (by exact_mod_cast (baryWeight_pos α).le) (h α)
  626. have hzero := (Finset.sum_eq_zero_iff_of_nonneg hterm).1 (weighted_driftDot v)
  627. have hdot (α : Assignment) : driftDot v α = 0 := by
  628. have hz := hzero α (Finset.mem_univ α)
  629. exact (mul_eq_zero.mp hz).resolve_left (by
  630. exact_mod_cast (baryWeight_pos α).ne')
  631. exact hv (eq_zero_of_first_three_driftDot_eq_zero (hdot 0) (hdot 1) (hdot 2))
  632. /-- Minimum of the four drift dot products. -/
  633. def minDriftDot (v : Fin 3 → ℝ) : ℝ :=
  634. min (driftDot v 0) (min (driftDot v 1) (min (driftDot v 2) (driftDot v 3)))
  635. theorem minDriftDot_le (v : Fin 3 → ℝ) (α : Assignment) :
  636. minDriftDot v ≤ driftDot v α := by
  637. fin_cases α <;> simp [minDriftDot]
  638. theorem minDriftDot_neg {v : Fin 3 → ℝ} (hv : v ≠ 0) : minDriftDot v < 0 := by
  639. obtain ⟨α, hα⟩ := exists_driftDot_neg hv
  640. exact (minDriftDot_le v α).trans_lt hα
  641. theorem continuous_driftDot (α : Assignment) :
  642. Continuous (fun v : Fin 3 → ℝ ↦ driftDot v α) := by
  643. apply continuous_finsetSum
  644. intro k _
  645. exact (continuous_apply k).mul continuous_const
  646. theorem continuous_minDriftDot : Continuous minDriftDot := by
  647. exact (continuous_driftDot 0).min ((continuous_driftDot 1).min
  648. ((continuous_driftDot 2).min (continuous_driftDot 3)))
  649. /-- Quantitative drift consequence used in Lemma 2. Compactness of the unit
  650. sphere upgrades pointwise negativity to a uniform negative constant. -/
  651. theorem exists_uniform_negative_drift :
  652. ∃ δ : ℝ, 0 < δ ∧ ∀ v : Fin 3 → ℝ, ‖v‖ = 1 → minDriftDot v ≤ -δ := by
  653. let e : Fin 3 → ℝ := Pi.single 0 1
  654. have heNorm : ‖e‖ = 1 := by simp [e, Pi.norm_single]
  655. have hSphere : (Metric.sphere (0 : Fin 3 → ℝ) 1).Nonempty := by
  656. refine ⟨e, ?_⟩
  657. simpa [Metric.mem_sphere, dist_zero_left] using heNorm
  658. obtain ⟨v, hvSphere, hvMax⟩ :=
  659. (isCompact_sphere (0 : Fin 3 → ℝ) 1).exists_isMaxOn hSphere
  660. continuous_minDriftDot.continuousOn
  661. have hvNorm : ‖v‖ = 1 := by
  662. simpa [Metric.mem_sphere, dist_zero_left] using hvSphere
  663. have hvNe : v ≠ 0 := by
  664. intro hv
  665. rw [hv, norm_zero] at hvNorm
  666. norm_num at hvNorm
  667. have hvNeg : minDriftDot v < 0 := minDriftDot_neg hvNe
  668. refine ⟨-minDriftDot v, neg_pos.mpr hvNeg, ?_⟩
  669. intro u huNorm
  670. have huSphere : u ∈ Metric.sphere (0 : Fin 3 → ℝ) 1 := by
  671. simpa [Metric.mem_sphere, dist_zero_left] using huNorm
  672. simpa using hvMax huSphere
  673. /-! ## Exact finite Poisson equations
  674. The paper uses the Poisson equation for each of its four finite Markov
  675. chains. The following rational vectors are exact certificates for those
  676. equations, normalized to vanish at state 18.
  677. -/
  678. /-- A rational solution of the vector-valued Poisson equation for each
  679. stationary assignment. -/
  680. def poissonSolution (α : Assignment) : Fin 19 → Fin 3 → ℚ :=
  681. match α with
  682. | 0 =>
  683. ![![-92474603/2928506, -754100497/29285060, -157916873/14642530],
  684. ![-141489037/4601938, -1193514713/46019380, -1016602093/92038760],
  685. ![-1344010/627537, 18690823/7530444, 11052155/30121776],
  686. ![-134965/418358, 366968/209179, -223305/3346864],
  687. ![-5089025/836716, -4703447/1673432, 1128693/3346864],
  688. ![-847919/418358, 413833/418358, 3811471/3346864],
  689. ![-598777/418358, 164803/1045895, 20651439/16734320],
  690. ![-82502/209179, -60472/209179, 186864/209179],
  691. ![-299969/418358, 515675/209179, -580345/3346864],
  692. ![-5419033/836716, -3513791/1673432, 771653/3346864],
  693. ![-7231387/3346864, 44722847/33468640, 71878267/66937280],
  694. ![-1862623/836716, 3390443/2091790, 6428039/33468640],
  695. ![-812525/418358, 529703/418358, 1026335/3346864],
  696. ![-134965/418358, 157789/209179, -223305/3346864],
  697. ![-142033/418358, -16613/209179, 2845999/3346864],
  698. ![-1424395/418358, -1779727/4183580, 5068333/8367160],
  699. ![-18591689/12132382, 14821723/12132382, 11738933/97059056],
  700. ![811053/2928506, 245214/1464253, 515835/3346864],
  701. ![0, 0, 0]]
  702. | 1 =>
  703. ![![-18218137/577502, -59408041/2310008, -99531215/9240032],
  704. ![-244093727/44467654, 128903569/177870616, -271195465/711482464],
  705. ![-37590230/18191313, 745570207/291061008, 464215267/1164244032],
  706. ![-236545/577502, 1920191/1155004, -238977/2310008],
  707. ![-32776595/8085028, -43322975/64680224, 308810745/258720896],
  708. ![-8281797/4042514, 7810709/8085028, 4566315/4042514],
  709. ![-817701/577502, 401365/2310008, 11462711/9240032],
  710. ![-128838/288751, -794069/2310008, 8052287/9240032],
  711. ![-494221/577502, 5356321/2310008, -2143653/9240032],
  712. ![-36384059/8085028, -876683/64680224, 275553885/258720896],
  713. ![-71731617/32340112, 330044815/258720896, 1086194759/1034883584],
  714. ![-2450175/1155004, 31996559/18480064, 17466859/73920256],
  715. ![-1067185/577502, 6309223/4620016, 6402399/18480064],
  716. ![-236545/577502, 765187/1155004, -238977/2310008],
  717. ![-241517/577502, -187691/1155004, 471885/577502],
  718. ![-1525927/577502, 1752829/4620016, 17143453/18480064],
  719. ![-24831461/16747558, 21338725/16747558, 19016669/133980464],
  720. ![570537/4042514, 389745/16170056, 6259599/64680224],
  721. ![0, 0, 0]]
  722. | 2 =>
  723. ![![-70369625/2160158, -113547139/4320316, -6887181/617188],
  724. ![-104473735/3394534, -176188709/6789068, -75067917/6789068],
  725. ![-1645928/462891, 1333993/771485, -753691/4628910],
  726. ![-45427/308594, 5701947/3085940, -3119/3085940],
  727. ![-4738835/617188, -4518359/1234376, -321747/1234376],
  728. ![-768017/308594, 458693/617188, 596047/617188],
  729. ![-863695/308594, -352161/617188, 445457/617188],
  730. ![-2069882/154297, -5571617/771485, -12296049/3085940],
  731. ![-10679/308594, 8728847/3085940, 253821/3085940],
  732. ![-4669339/617188, -3307599/1234376, -218971/1234376],
  733. ![-6201025/2468752, 5674595/4937504, 4652415/4937504],
  734. ![-2382673/617188, 926495/1234376, -518733/1234376],
  735. ![-1092827/308594, 1279677/3085940, -902409/3085940],
  736. ![-45427/308594, 2616007/3085940, -3119/3085940],
  737. ![-1745479/308594, -1796229/617188, -704491/617188],
  738. ![-1100161/308594, -315251/617188, 336781/617188],
  739. ![-23705255/8949226, 56129683/89492260, -26607071/89492260],
  740. ![-2164725/2160158, -1584873/3085940, -7021593/21601580],
  741. ![0, 0, 0]]
  742. | 3 =>
  743. ![![-1115788487/32859015, -182354567/6571803, -372698614/32859015],
  744. ![-6383806027/206542380, -2153946893/82616952, -4578452623/413084760],
  745. ![-228316579/67595688, 190330739/135191376, 390245/135191376],
  746. ![1045843/2503544, 12388597/5007088, 1537835/5007088],
  747. ![-14159963/2503544, -12265589/5007088, 2221421/5007088],
  748. ![-34533503/7510632, -17583545/15021264, 8022361/15021264],
  749. ![-63544399/37553160, -888629/15021264, 88445129/75106320],
  750. ![410774/938829, 384064/938829, 1021144/938829],
  751. ![6423721/7510632, 58332079/15021264, 5930545/15021264],
  752. ![-39193697/7510632, -15630479/15021264, 7981303/15021264],
  753. ![-433980767/150212640, 43547075/60085056, 271371337/300425280],
  754. ![-294149719/75106320, 6110059/30042528, -30132271/150212640],
  755. ![-30532829/2503544, -41208115/5007088, -19551805/5007088],
  756. ![-21242167/7510632, -11462785/15021264, 8124545/15021264],
  757. ![6399233/7510632, 13991927/15021264, 17289929/15021264],
  758. ![-15333271/6258860, 944543/2503544, 10409221/12517720],
  759. ![-366525975/72602776, -291104273/145205552, -183323039/145205552],
  760. ![-1337967/17524808, 8804151/35049616, 29668185/35049616],
  761. ![0, 0, 0]]
  762. /-- The transition operator applied to one coordinate of a function on the
  763. nineteen interval states. -/
  764. def expectedNext (α : Assignment) (g : Fin 19 → ℚ) (i : Fin 19) : ℚ :=
  765. ∑ j : Fin 19, probability α i j * g j
  766. set_option maxHeartbeats 2000000 in
  767. -- This expands 57 entries of the first exact rational Poisson certificate.
  768. private theorem poisson_equation_zero (i : Fin 19) (k : Fin 3) :
  769. poissonSolution 0 i k - expectedNext 0 (fun j ↦ poissonSolution 0 j k) i =
  770. imbalanceIncrement (prescribed 0 i) k - listedDrift 0 k := by
  771. fin_cases i <;> fin_cases k <;>
  772. simp [poissonSolution, expectedNext, probability, allowedNext, transitions,
  773. prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,
  774. primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,
  775. Matrix.cons_val_three] <;>
  776. norm_num
  777. set_option maxHeartbeats 2000000 in
  778. -- This expands 57 entries of the second exact rational Poisson certificate.
  779. private theorem poisson_equation_one (i : Fin 19) (k : Fin 3) :
  780. poissonSolution 1 i k - expectedNext 1 (fun j ↦ poissonSolution 1 j k) i =
  781. imbalanceIncrement (prescribed 1 i) k - listedDrift 1 k := by
  782. fin_cases i <;> fin_cases k <;>
  783. simp [poissonSolution, expectedNext, probability, allowedNext, transitions,
  784. prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,
  785. primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,
  786. Matrix.cons_val_three] <;>
  787. norm_num
  788. set_option maxHeartbeats 2000000 in
  789. -- This expands 57 entries of the third exact rational Poisson certificate.
  790. private theorem poisson_equation_two (i : Fin 19) (k : Fin 3) :
  791. poissonSolution 2 i k - expectedNext 2 (fun j ↦ poissonSolution 2 j k) i =
  792. imbalanceIncrement (prescribed 2 i) k - listedDrift 2 k := by
  793. fin_cases i <;> fin_cases k <;>
  794. simp [poissonSolution, expectedNext, probability, allowedNext, transitions,
  795. prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,
  796. primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,
  797. Matrix.cons_val_three] <;>
  798. norm_num
  799. set_option maxHeartbeats 2000000 in
  800. -- This expands 57 entries of the fourth exact rational Poisson certificate.
  801. private theorem poisson_equation_three (i : Fin 19) (k : Fin 3) :
  802. poissonSolution 3 i k - expectedNext 3 (fun j ↦ poissonSolution 3 j k) i =
  803. imbalanceIncrement (prescribed 3 i) k - listedDrift 3 k := by
  804. fin_cases i <;> fin_cases k <;>
  805. simp [poissonSolution, expectedNext, probability, allowedNext, transitions,
  806. prescribed, width, endpoint, leftIndex, rightIndex, imbalanceIncrement,
  807. primeIncrement, listedDrift, Fin.sum_univ_succ, Matrix.cons_val_two,
  808. Matrix.cons_val_three] <;>
  809. norm_num
  810. /-- Exact verification of `g - P g = h - β` for all four assignments,
  811. nineteen states, and three imbalance coordinates. -/
  812. theorem poisson_equation (α : Assignment) (i : Fin 19) (k : Fin 3) :
  813. poissonSolution α i k - expectedNext α (fun j ↦ poissonSolution α j k) i =
  814. imbalanceIncrement (prescribed α i) k - listedDrift α k := by
  815. fin_cases α
  816. · exact poisson_equation_zero i k
  817. · exact poisson_equation_one i k
  818. · exact poisson_equation_two i k
  819. · exact poisson_equation_three i k
  820. /-! ## Finite blocks with a fixed assignment -/
  821. /-- Iteration of the transition operator belonging to one assignment. -/
  822. def iterateExpected (α : Assignment) : ℕ → (Fin 19 → ℚ) → Fin 19 → ℚ
  823. | 0, f => f
  824. | n + 1, f => fun i ↦ expectedNext α (iterateExpected α n f) i
  825. /-- Expected accumulated imbalance in one coordinate during `n` steps with
  826. the assignment held fixed. -/
  827. def expectedAccum (α : Assignment) : ℕ → Fin 19 → Fin 3 → ℚ
  828. | 0, _, _ => 0
  829. | n + 1, i, k =>
  830. imbalanceIncrement (prescribed α i) k +
  831. expectedNext α (fun j ↦ expectedAccum α n j k) i
  832. @[simp] theorem iterateExpected_succ (α : Assignment) (n : ℕ)
  833. (f : Fin 19 → ℚ) (i : Fin 19) :
  834. iterateExpected α (n + 1) f i =
  835. expectedNext α (iterateExpected α n f) i := rfl
  836. @[simp] theorem expectedAccum_succ (α : Assignment) (n : ℕ)
  837. (i : Fin 19) (k : Fin 3) :
  838. expectedAccum α (n + 1) i k =
  839. imbalanceIncrement (prescribed α i) k +
  840. expectedNext α (fun j ↦ expectedAccum α n j k) i := rfl
  841. theorem expectedNext_const (α : Assignment) (c : ℚ) (i : Fin 19) :
  842. expectedNext α (fun _ ↦ c) i = c := by
  843. simp only [expectedNext, ← Finset.sum_mul, probability_row_sum, one_mul]
  844. theorem expectedNext_add (α : Assignment) (f g : Fin 19 → ℚ) (i : Fin 19) :
  845. expectedNext α (fun j ↦ f j + g j) i =
  846. expectedNext α f i + expectedNext α g i := by
  847. simp only [expectedNext, mul_add, Finset.sum_add_distrib]
  848. theorem expectedNext_sub (α : Assignment) (f g : Fin 19 → ℚ) (i : Fin 19) :
  849. expectedNext α (fun j ↦ f j - g j) i =
  850. expectedNext α f i - expectedNext α g i := by
  851. simp only [expectedNext, mul_sub, Finset.sum_sub_distrib]
  852. /-- The finite Poisson certificate telescopes over an arbitrary block. -/
  853. theorem expectedAccum_eq_poisson (α : Assignment) (n : ℕ)
  854. (i : Fin 19) (k : Fin 3) :
  855. expectedAccum α n i k =
  856. (n : ℚ) * listedDrift α k + poissonSolution α i k -
  857. iterateExpected α n (fun j ↦ poissonSolution α j k) i := by
  858. induction n generalizing i with
  859. | zero => simp [expectedAccum, iterateExpected]
  860. | succ n ih =>
  861. rw [expectedAccum_succ]
  862. simp_rw [ih]
  863. rw [expectedNext_sub, expectedNext_add]
  864. simp only [expectedNext_const]
  865. rw [iterateExpected_succ]
  866. have hp := poisson_equation α i k
  867. dsimp [expectedNext] at hp ⊢
  868. norm_num only [Nat.cast_add, Nat.cast_one]
  869. linear_combination -hp
  870. set_option maxHeartbeats 800000 in
  871. -- This checks all 228 entries of the four exact rational Poisson tables.
  872. /-- Every entry of every exact Poisson solution is bounded by 40. -/
  873. theorem poissonSolution_abs_le (α : Assignment) (i : Fin 19) (k : Fin 3) :
  874. |poissonSolution α i k| ≤ 40 := by
  875. fin_cases α <;> fin_cases i <;> fin_cases k <;>
  876. simp [poissonSolution, Matrix.cons_val_two, Matrix.cons_val_three] <;>
  877. norm_num
  878. theorem expectedNext_mono (α : Assignment) {f g : Fin 19 → ℚ}
  879. (hfg : ∀ j, f j ≤ g j) (i : Fin 19) :
  880. expectedNext α f i ≤ expectedNext α g i := by
  881. apply Finset.sum_le_sum
  882. intro j _
  883. exact mul_le_mul_of_nonneg_left (hfg j) (probability_nonneg α i j)
  884. theorem iterateExpected_bounds (α : Assignment) (n : ℕ)
  885. {f : Fin 19 → ℚ} {B : ℚ} (hf : ∀ i, -B ≤ f i ∧ f i ≤ B)
  886. (i : Fin 19) :
  887. -B ≤ iterateExpected α n f i ∧ iterateExpected α n f i ≤ B := by
  888. induction n generalizing i with
  889. | zero => exact hf i
  890. | succ n ih =>
  891. rw [iterateExpected_succ]
  892. constructor
  893. · rw [← expectedNext_const α (-B) i]
  894. exact expectedNext_mono α (fun j ↦ (ih j).1) i
  895. · rw [← expectedNext_const α B i]
  896. exact expectedNext_mono α (fun j ↦ (ih j).2) i
  897. /-- The expected block increment differs from `n` times the stationary drift
  898. by at most 80 in every coordinate, uniformly in the initial interval. -/
  899. theorem expectedAccum_error_abs_le (α : Assignment) (n : ℕ)
  900. (i : Fin 19) (k : Fin 3) :
  901. |expectedAccum α n i k - (n : ℚ) * listedDrift α k| ≤ 80 := by
  902. rw [expectedAccum_eq_poisson]
  903. ring_nf
  904. have hi := poissonSolution_abs_le α i k
  905. have hi' := abs_le.mp hi
  906. have hj := iterateExpected_bounds α n
  907. (B := (40 : ℚ)) (f := fun j ↦ poissonSolution α j k)
  908. (fun j ↦ (abs_le.mp (poissonSolution_abs_le α j k))) i
  909. rw [abs_le]
  910. constructor <;> linarith
  911. /-! ### Enumerating finite blocks -/
  912. /-- The integer imbalance increment of one multiplier. -/
  913. def imbalanceIncrementInt (a : ℕ) : Fin 3 → ℤ :=
  914. ![(primeIncrement a 0 : ℤ) - 31 * primeIncrement a 3,
  915. (primeIncrement a 1 : ℤ) - 26 * primeIncrement a 3,
  916. (primeIncrement a 2 : ℤ) - 11 * primeIncrement a 3]
  917. theorem imbalanceIncrementInt_cast (a : ℕ) (k : Fin 3) :
  918. (imbalanceIncrementInt a k : ℚ) = imbalanceIncrement a k := by
  919. fin_cases k <;> simp [imbalanceIncrementInt, imbalanceIncrement]
  920. /-- Endpoint of a tuple of successive interval choices. -/
  921. def blockEndpoint : (n : ℕ) → Fin 19 → (Fin n → Fin 19) → Fin 19
  922. | 0, i, _ => i
  923. | n + 1, _, w => blockEndpoint n (w 0) (Fin.tail w)
  924. /-- Probability of a tuple of successive interval choices under a fixed
  925. assignment. Tuples containing a forbidden transition have weight zero. -/
  926. def blockWeight (α : Assignment) :
  927. (n : ℕ) → Fin 19 → (Fin n → Fin 19) → ℚ
  928. | 0, _, _ => 1
  929. | n + 1, i, w =>
  930. probability α i (w 0) * blockWeight α n (w 0) (Fin.tail w)
  931. /-- Net integer imbalance accumulated along a finite block. -/
  932. def blockIncrement (α : Assignment) :
  933. (n : ℕ) → Fin 19 → (Fin n → Fin 19) → Fin 3 → ℤ
  934. | 0, _, _, _ => 0
  935. | n + 1, i, w, k =>
  936. imbalanceIncrementInt (prescribed α i) k +
  937. blockIncrement α n (w 0) (Fin.tail w) k
  938. /-- Multiplier word read along a finite block. -/
  939. def blockWord (α : Assignment) :
  940. (n : ℕ) → Fin 19 → (Fin n → Fin 19) → List ℕ
  941. | 0, _, _ => []
  942. | n + 1, i, w =>
  943. prescribed α i :: blockWord α n (w 0) (Fin.tail w)
  944. @[simp] theorem blockWeight_cons (α : Assignment) (n : ℕ) (i j : Fin 19)
  945. (w : Fin n → Fin 19) :
  946. blockWeight α (n + 1) i (Fin.cons j w) =
  947. probability α i j * blockWeight α n j w := by
  948. simp [blockWeight]
  949. @[simp] theorem blockIncrement_cons (α : Assignment) (n : ℕ) (i j : Fin 19)
  950. (w : Fin n → Fin 19) (k : Fin 3) :
  951. blockIncrement α (n + 1) i (Fin.cons j w) k =
  952. imbalanceIncrementInt (prescribed α i) k + blockIncrement α n j w k := by
  953. simp [blockIncrement]
  954. @[simp] theorem blockWord_cons (α : Assignment) (n : ℕ) (i j : Fin 19)
  955. (w : Fin n → Fin 19) :
  956. blockWord α (n + 1) i (Fin.cons j w) =
  957. prescribed α i :: blockWord α n j w := by
  958. simp [blockWord]
  959. theorem sum_tuple_succ {A R : Type*} [Fintype A]
  960. [AddCommMonoid R] (n : ℕ) (F : (Fin (n + 1) → A) → R) :
  961. ∑ w, F w = ∑ a : A, ∑ u : Fin n → A, F (Fin.cons a u) := by
  962. let e := Fin.consEquiv (fun _ : Fin (n + 1) ↦ A)
  963. calc
  964. ∑ w, F w = ∑ z : A × (Fin n → A), F (e z) := (Equiv.sum_comp e F).symm
  965. _ = ∑ a : A, ∑ u : Fin n → A, F (Fin.cons a u) := by
  966. rw [Fintype.sum_prod_type]
  967. rfl
  968. /-- Block-path probabilities sum to one. -/
  969. theorem blockWeight_sum (α : Assignment) (n : ℕ) (i : Fin 19) :
  970. ∑ w : Fin n → Fin 19, blockWeight α n i w = 1 := by
  971. induction n generalizing i with
  972. | zero => simp [blockWeight]
  973. | succ n ih =>
  974. rw [sum_tuple_succ]
  975. simp_rw [blockWeight_cons, ← Finset.mul_sum, ih, mul_one]
  976. exact probability_row_sum α i
  977. theorem blockWeight_nonneg (α : Assignment) (n : ℕ) (i : Fin 19)
  978. (w : Fin n → Fin 19) : 0 ≤ blockWeight α n i w := by
  979. induction n generalizing i with
  980. | zero => simp [blockWeight]
  981. | succ n ih =>
  982. rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,
  983. blockWeight_cons]
  984. exact mul_nonneg (probability_nonneg α i (w 0)) (ih (w 0) (Fin.tail w))
  985. /-- The path enumeration realizes `expectedAccum`. -/
  986. theorem sum_blockWeight_mul_increment (α : Assignment) (n : ℕ)
  987. (i : Fin 19) (k : Fin 3) :
  988. ∑ w : Fin n → Fin 19,
  989. blockWeight α n i w * (blockIncrement α n i w k : ℚ) =
  990. expectedAccum α n i k := by
  991. induction n generalizing i with
  992. | zero => simp [blockWeight, blockIncrement, expectedAccum]
  993. | succ n ih =>
  994. rw [sum_tuple_succ, expectedAccum_succ]
  995. simp_rw [blockWeight_cons, blockIncrement_cons, Int.cast_add,
  996. imbalanceIncrementInt_cast, mul_add, Finset.sum_add_distrib]
  997. have hfirst :
  998. (∑ j : Fin 19, ∑ w : Fin n → Fin 19,
  999. probability α i j * blockWeight α n j w *
  1000. imbalanceIncrement (prescribed α i) k) =
  1001. imbalanceIncrement (prescribed α i) k := by
  1002. calc
  1003. _ = ∑ j : Fin 19, probability α i j *
  1004. (∑ w : Fin n → Fin 19, blockWeight α n j w *
  1005. imbalanceIncrement (prescribed α i) k) := by
  1006. apply Finset.sum_congr rfl
  1007. intro j _
  1008. rw [Finset.mul_sum]
  1009. simp only [mul_assoc]
  1010. _ = ∑ j : Fin 19, probability α i j *
  1011. ((∑ w : Fin n → Fin 19, blockWeight α n j w) *
  1012. imbalanceIncrement (prescribed α i) k) := by
  1013. simp_rw [Finset.sum_mul]
  1014. _ = ∑ j : Fin 19, probability α i j *
  1015. imbalanceIncrement (prescribed α i) k := by
  1016. simp_rw [blockWeight_sum, one_mul]
  1017. _ = imbalanceIncrement (prescribed α i) k := by
  1018. rw [← Finset.sum_mul, probability_row_sum, one_mul]
  1019. have hsecond :
  1020. (∑ j : Fin 19, ∑ w : Fin n → Fin 19,
  1021. probability α i j * blockWeight α n j w *
  1022. (blockIncrement α n j w k : ℚ)) =
  1023. ∑ j : Fin 19, probability α i j * expectedAccum α n j k := by
  1024. apply Finset.sum_congr rfl
  1025. intro j _
  1026. calc
  1027. _ = probability α i j *
  1028. (∑ w : Fin n → Fin 19,
  1029. blockWeight α n j w * (blockIncrement α n j w k : ℚ)) := by
  1030. rw [Finset.mul_sum]
  1031. simp only [mul_assoc]
  1032. _ = probability α i j * expectedAccum α n j k := by rw [ih]
  1033. rw [hfirst, hsecond]
  1034. rfl
  1035. theorem blockIncrement_eq_imbalance (α : Assignment) (n : ℕ)
  1036. (i : Fin 19) (w : Fin n → Fin 19) :
  1037. blockIncrement α n i w = imbalance (blockWord α n i w) := by
  1038. induction n generalizing i with
  1039. | zero =>
  1040. funext k
  1041. fin_cases k <;> simp [blockIncrement, blockWord, imbalance, exponents]
  1042. | succ n ih =>
  1043. rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm]
  1044. funext k
  1045. rw [blockIncrement_cons, blockWord_cons, ih]
  1046. fin_cases k <;>
  1047. simp [imbalanceIncrementInt, imbalance, exponents_cons] <;> ring
  1048. /-! ## Deterministic drift feedback -/
  1049. /-- The integer imbalance vector viewed in `ℝ³`. -/
  1050. def imbalanceReal (h : Fin 3 → ℤ) : Fin 3 → ℝ := fun k ↦ h k
  1051. /-- Squared Euclidean length, used as the Lyapunov function. -/
  1052. def squareLength (h : Fin 3 → ℤ) : ℝ :=
  1053. ∑ k : Fin 3, (h k : ℝ) ^ 2
  1054. /-- A deterministic minimizer among the four drift directions. -/
  1055. noncomputable def minAssignment (v : Fin 3 → ℝ) : Assignment :=
  1056. Classical.choose (Finset.exists_min_image Finset.univ (driftDot v)
  1057. Finset.univ_nonempty)
  1058. theorem minAssignment_le (v : Fin 3 → ℝ) (α : Assignment) :
  1059. driftDot v (minAssignment v) ≤ driftDot v α := by
  1060. exact (Classical.choose_spec (Finset.exists_min_image Finset.univ (driftDot v)
  1061. Finset.univ_nonempty)).2 α (Finset.mem_univ α)
  1062. /-- The feedback assignment used for the next block. -/
  1063. noncomputable def feedback (h : Fin 3 → ℤ) : Assignment :=
  1064. minAssignment (imbalanceReal h)
  1065. /-- A fixed positive uniform drift constant supplied by compactness of the
  1066. unit sphere. -/
  1067. noncomputable def driftDelta : ℝ := Classical.choose exists_uniform_negative_drift
  1068. theorem driftDelta_pos : 0 < driftDelta :=
  1069. (Classical.choose_spec exists_uniform_negative_drift).1
  1070. theorem minDriftDot_le_neg_delta {v : Fin 3 → ℝ} (hv : ‖v‖ = 1) :
  1071. minDriftDot v ≤ -driftDelta :=
  1072. (Classical.choose_spec exists_uniform_negative_drift).2 v hv
  1073. theorem exists_assignment_le_minDriftDot (v : Fin 3 → ℝ) :
  1074. ∃ α : Assignment, driftDot v α ≤ minDriftDot v := by
  1075. let α := minAssignment v
  1076. refine ⟨α, ?_⟩
  1077. apply le_min
  1078. · exact minAssignment_le v 0
  1079. · apply le_min
  1080. · exact minAssignment_le v 1
  1081. · exact le_min (minAssignment_le v 2) (minAssignment_le v 3)
  1082. theorem driftDot_smul (c : ℝ) (v : Fin 3 → ℝ) (α : Assignment) :
  1083. driftDot (c • v) α = c * driftDot v α := by
  1084. rw [driftDot, driftDot, Finset.mul_sum]
  1085. apply Finset.sum_congr rfl
  1086. intro k _
  1087. simp only [Pi.smul_apply, smul_eq_mul]
  1088. ring
  1089. theorem norm_imbalanceReal_pos {h : Fin 3 → ℤ} (hh : h ≠ 0) :
  1090. 0 < ‖imbalanceReal h‖ := by
  1091. rw [norm_pos_iff]
  1092. intro hz
  1093. apply hh
  1094. funext k
  1095. have hk := congrFun hz k
  1096. have hk' : (h k : ℝ) = 0 := by simpa [imbalanceReal] using hk
  1097. exact_mod_cast hk'
  1098. /-- The chosen stationary drift has a uniformly negative radial component. -/
  1099. theorem feedback_drift_le (h : Fin 3 → ℤ) :
  1100. driftDot (imbalanceReal h) (feedback h) ≤
  1101. -driftDelta * ‖imbalanceReal h‖ := by
  1102. by_cases hh : h = 0
  1103. · subst h
  1104. have hz : imbalanceReal (0 : Fin 3 → ℤ) = 0 := by
  1105. funext k
  1106. simp [imbalanceReal]
  1107. rw [hz, norm_zero]
  1108. simp [driftDot]
  1109. · let r := ‖imbalanceReal h‖
  1110. have hr : 0 < r := norm_imbalanceReal_pos hh
  1111. let v : Fin 3 → ℝ := r⁻¹ • imbalanceReal h
  1112. have hv : ‖v‖ = 1 := by
  1113. rw [show v = r⁻¹ • imbalanceReal h from rfl, norm_smul]
  1114. simp only [Real.norm_eq_abs, abs_inv, abs_of_pos hr]
  1115. exact inv_mul_cancel₀ hr.ne'
  1116. obtain ⟨α, hα⟩ := exists_assignment_le_minDriftDot v
  1117. have hαδ : driftDot v α ≤ -driftDelta :=
  1118. hα.trans (minDriftDot_le_neg_delta hv)
  1119. have hscale : imbalanceReal h = r • v := by
  1120. simp only [v, smul_smul]
  1121. rw [mul_inv_cancel₀ hr.ne']
  1122. simp
  1123. calc
  1124. driftDot (imbalanceReal h) (feedback h) ≤
  1125. driftDot (imbalanceReal h) α := minAssignment_le _ α
  1126. _ = r * driftDot v α := by rw [hscale, driftDot_smul]
  1127. _ ≤ r * (-driftDelta) := mul_le_mul_of_nonneg_left hαδ hr.le
  1128. _ = -driftDelta * ‖imbalanceReal h‖ := by rw [show r = ‖imbalanceReal h‖ from rfl]; ring
  1129. /-! ### Uniform finite-block estimates -/
  1130. theorem prescribed_increment_abs_le (α : Assignment) (i : Fin 19) (k : Fin 3) :
  1131. |imbalanceIncrementInt (prescribed α i) k| ≤ 30 := by
  1132. have ha := prescribed_mem_multipliers α i
  1133. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  1134. rcases ha with ha | ha | ha | ha | ha <;> rw [ha] <;>
  1135. fin_cases k <;>
  1136. simp [imbalanceIncrementInt, primeIncrement, Matrix.cons_val_two,
  1137. Matrix.cons_val_three]
  1138. theorem blockIncrement_abs_le (α : Assignment) (n : ℕ) (i : Fin 19)
  1139. (w : Fin n → Fin 19) (k : Fin 3) :
  1140. |blockIncrement α n i w k| ≤ 30 * n := by
  1141. induction n generalizing i with
  1142. | zero => simp [blockIncrement]
  1143. | succ n ih =>
  1144. rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,
  1145. blockIncrement_cons]
  1146. calc
  1147. |imbalanceIncrementInt (prescribed α i) k +
  1148. blockIncrement α n (w 0) (Fin.tail w) k| ≤
  1149. |imbalanceIncrementInt (prescribed α i) k| +
  1150. |blockIncrement α n (w 0) (Fin.tail w) k| := abs_add_le _ _
  1151. _ ≤ 30 + 30 * n := add_le_add (prescribed_increment_abs_le α i k)
  1152. (ih (w 0) (Fin.tail w))
  1153. _ = 30 * (n + 1) := by ring
  1154. theorem blockIncrement_square_sum_le (α : Assignment) (n : ℕ) (i : Fin 19)
  1155. (w : Fin n → Fin 19) :
  1156. (∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2) ≤ 2700 * n ^ 2 := by
  1157. have hbound (k : Fin 3) :
  1158. |(blockIncrement α n i w k : ℝ)| ≤ 30 * n := by
  1159. exact_mod_cast blockIncrement_abs_le α n i w k
  1160. have hsquare (k : Fin 3) :
  1161. (blockIncrement α n i w k : ℝ) ^ 2 ≤ (30 * (n : ℝ)) ^ 2 := by
  1162. rw [sq_le_sq]
  1163. simpa [abs_of_nonneg (by positivity : (0 : ℝ) ≤ 30 * n)] using hbound k
  1164. calc
  1165. _ ≤ ∑ _k : Fin 3, (30 * (n : ℝ)) ^ 2 :=
  1166. Finset.sum_le_sum fun k _ ↦ hsquare k
  1167. _ = 2700 * n ^ 2 := by norm_num [Fin.sum_univ_succ]; ring
  1168. theorem sum_blockWeight_real (α : Assignment) (n : ℕ) (i : Fin 19) :
  1169. ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) = 1 := by
  1170. exact_mod_cast blockWeight_sum α n i
  1171. theorem expected_block_square_le (α : Assignment) (n : ℕ) (i : Fin 19) :
  1172. (∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1173. ∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2) ≤
  1174. (2700 : ℝ) * (n : ℝ) ^ 2 := by
  1175. calc
  1176. _ ≤ ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1177. ((2700 : ℝ) * (n : ℝ) ^ 2) := by
  1178. apply Finset.sum_le_sum
  1179. intro w _
  1180. apply mul_le_mul_of_nonneg_left (blockIncrement_square_sum_le α n i w)
  1181. exact_mod_cast blockWeight_nonneg α n i w
  1182. _ = (2700 : ℝ) * (n : ℝ) ^ 2 := by
  1183. rw [← Finset.sum_mul, sum_blockWeight_real, one_mul]
  1184. theorem sum_blockWeight_mul_increment_real (α : Assignment) (n : ℕ)
  1185. (i : Fin 19) (k : Fin 3) :
  1186. ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1187. (blockIncrement α n i w k : ℝ) = expectedAccum α n i k := by
  1188. exact_mod_cast sum_blockWeight_mul_increment α n i k
  1189. theorem expectedAccum_error_abs_le_real (α : Assignment) (n : ℕ)
  1190. (i : Fin 19) (k : Fin 3) :
  1191. |(expectedAccum α n i k : ℝ) - n * listedDriftReal α k| ≤ 80 := by
  1192. simp only [listedDriftReal]
  1193. exact_mod_cast expectedAccum_error_abs_le α n i k
  1194. /-- Expected Lyapunov increment over a block, expressed as a finite path sum. -/
  1195. def expectedSquareIncrement (α : Assignment) (n : ℕ)
  1196. (i : Fin 19) (h : Fin 3 → ℤ) : ℝ :=
  1197. ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1198. (squareLength (fun k ↦ h k + blockIncrement α n i w k) - squareLength h)
  1199. theorem expectedSquareIncrement_le (α : Assignment) (n : ℕ)
  1200. (i : Fin 19) (h : Fin 3 → ℤ) :
  1201. expectedSquareIncrement α n i h ≤
  1202. 2 * n * driftDot (imbalanceReal h) α +
  1203. 480 * ‖imbalanceReal h‖ + 2700 * n ^ 2 := by
  1204. have hcoord (k : Fin 3) : |(h k : ℝ)| ≤ ‖imbalanceReal h‖ := by
  1205. have hk : ‖imbalanceReal h k‖₊ ≤
  1206. Finset.univ.sup (fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊) :=
  1207. Finset.le_sup (s := Finset.univ)
  1208. (f := fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊) (Finset.mem_univ k)
  1209. change ‖imbalanceReal h k‖ ≤ ‖imbalanceReal h‖
  1210. change ‖imbalanceReal h k‖ ≤
  1211. (↑(Finset.univ.sup (fun j : Fin 3 ↦ ‖imbalanceReal h j‖₊)) : ℝ)
  1212. exact_mod_cast hk
  1213. have herr (k : Fin 3) := expectedAccum_error_abs_le_real α n i k
  1214. have hdotError :
  1215. (∑ k : Fin 3, (h k : ℝ) * (expectedAccum α n i k : ℝ)) ≤
  1216. n * driftDot (imbalanceReal h) α + 240 * ‖imbalanceReal h‖ := by
  1217. have hterm (k : Fin 3) :
  1218. (h k : ℝ) * ((expectedAccum α n i k : ℝ) - n * listedDriftReal α k) ≤
  1219. 80 * ‖imbalanceReal h‖ := by
  1220. calc
  1221. _ ≤ |(h k : ℝ) * ((expectedAccum α n i k : ℝ) -
  1222. n * listedDriftReal α k)| := le_abs_self _
  1223. _ = |(h k : ℝ)| * |(expectedAccum α n i k : ℝ) -
  1224. n * listedDriftReal α k| := abs_mul _ _
  1225. _ ≤ ‖imbalanceReal h‖ * 80 :=
  1226. mul_le_mul (hcoord k) (herr k) (abs_nonneg _) (norm_nonneg _)
  1227. _ = 80 * ‖imbalanceReal h‖ := by ring
  1228. calc
  1229. _ = n * driftDot (imbalanceReal h) α +
  1230. ∑ k : Fin 3, (h k : ℝ) * ((expectedAccum α n i k : ℝ) -
  1231. n * listedDriftReal α k) := by
  1232. simp only [driftDot, imbalanceReal, Fin.sum_univ_succ]
  1233. ring
  1234. _ ≤ n * driftDot (imbalanceReal h) α +
  1235. ∑ _k : Fin 3, 80 * ‖imbalanceReal h‖ := by
  1236. exact add_le_add_right
  1237. (Finset.sum_le_sum fun k _ ↦ hterm k)
  1238. (n * driftDot (imbalanceReal h) α)
  1239. _ = n * driftDot (imbalanceReal h) α + 240 * ‖imbalanceReal h‖ := by
  1240. norm_num [Fin.sum_univ_succ]
  1241. ring
  1242. have hexpand : expectedSquareIncrement α n i h =
  1243. 2 * (∑ k : Fin 3, (h k : ℝ) * (expectedAccum α n i k : ℝ)) +
  1244. ∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1245. ∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2 := by
  1246. have hpoint (w : Fin n → Fin 19) :
  1247. squareLength (fun k ↦ h k + blockIncrement α n i w k) - squareLength h =
  1248. 2 * (∑ k : Fin 3, (h k : ℝ) * (blockIncrement α n i w k : ℝ)) +
  1249. ∑ k : Fin 3, (blockIncrement α n i w k : ℝ) ^ 2 := by
  1250. simp only [squareLength, Int.cast_add, Fin.sum_univ_succ]
  1251. ring
  1252. simp only [expectedSquareIncrement]
  1253. simp_rw [hpoint, mul_add, Finset.sum_add_distrib]
  1254. apply congrArg₂ (· + ·)
  1255. · calc
  1256. (∑ w : Fin n → Fin 19, (blockWeight α n i w : ℝ) *
  1257. (2 * ∑ k : Fin 3, (h k : ℝ) *
  1258. (blockIncrement α n i w k : ℝ))) =
  1259. ∑ w : Fin n → Fin 19, ∑ k : Fin 3,
  1260. 2 * (h k : ℝ) * ((blockWeight α n i w : ℝ) *
  1261. (blockIncrement α n i w k : ℝ)) := by
  1262. apply Finset.sum_congr rfl
  1263. intro w _
  1264. simp only [Fin.sum_univ_succ]
  1265. ring
  1266. _ = ∑ k : Fin 3, ∑ w : Fin n → Fin 19,
  1267. 2 * (h k : ℝ) * ((blockWeight α n i w : ℝ) *
  1268. (blockIncrement α n i w k : ℝ)) := Finset.sum_comm
  1269. _ = 2 * (∑ k : Fin 3, (h k : ℝ) *
  1270. (expectedAccum α n i k : ℝ)) := by
  1271. rw [Finset.mul_sum]
  1272. apply Finset.sum_congr rfl
  1273. intro k _
  1274. rw [← Finset.mul_sum, sum_blockWeight_mul_increment_real]
  1275. ring
  1276. · rfl
  1277. rw [hexpand]
  1278. calc
  1279. _ ≤ 2 * (n * driftDot (imbalanceReal h) α +
  1280. 240 * ‖imbalanceReal h‖) + 2700 * n ^ 2 :=
  1281. add_le_add (mul_le_mul_of_nonneg_left hdotError (by norm_num))
  1282. (expected_block_square_le α n i)
  1283. _ = 2 * n * driftDot (imbalanceReal h) α +
  1284. 480 * ‖imbalanceReal h‖ + 2700 * n ^ 2 := by ring
  1285. /-- A block length large enough for its negative stationary drift to dominate
  1286. the uniformly bounded Poisson correction. -/
  1287. noncomputable def controlBlockLength : ℕ :=
  1288. Classical.choose (exists_nat_gt (241 / driftDelta))
  1289. theorem controlBlockLength_drift :
  1290. 241 < (controlBlockLength : ℝ) * driftDelta := by
  1291. have h := Classical.choose_spec (exists_nat_gt (241 / driftDelta))
  1292. apply (div_lt_iff₀ driftDelta_pos).mp
  1293. simpa only [controlBlockLength] using h
  1294. theorem controlBlockLength_pos : 0 < controlBlockLength := by
  1295. by_contra h
  1296. have hz : controlBlockLength = 0 := Nat.eq_zero_of_not_pos h
  1297. have hN := controlBlockLength_drift
  1298. rw [hz] at hN
  1299. norm_num at hN
  1300. /-- A finite radius outside which the block Lyapunov drift is at most `-1`. -/
  1301. noncomputable def controlRadius : ℕ := 2700 * controlBlockLength ^ 2 + 1
  1302. theorem controlled_block_drift (i : Fin 19) (h : Fin 3 → ℤ)
  1303. (hout : controlRadius < ‖imbalanceReal h‖) :
  1304. expectedSquareIncrement (feedback h) controlBlockLength i h ≤ -1 := by
  1305. calc
  1306. expectedSquareIncrement (feedback h) controlBlockLength i h ≤
  1307. 2 * controlBlockLength * driftDot (imbalanceReal h) (feedback h) +
  1308. 480 * ‖imbalanceReal h‖ + 2700 * controlBlockLength ^ 2 :=
  1309. expectedSquareIncrement_le _ _ _ _
  1310. _ ≤ 2 * controlBlockLength *
  1311. (-driftDelta * ‖imbalanceReal h‖) +
  1312. 480 * ‖imbalanceReal h‖ + 2700 * controlBlockLength ^ 2 := by
  1313. have hm := mul_le_mul_of_nonneg_left (feedback_drift_le h)
  1314. (show 0 ≤ (2 : ℝ) * controlBlockLength by positivity)
  1315. norm_num at hm
  1316. linarith
  1317. _ ≤ -1 := by
  1318. have hN := controlBlockLength_drift
  1319. dsimp [controlRadius] at hout
  1320. push_cast at hout
  1321. nlinarith [norm_nonneg (imbalanceReal h)]
  1322. /-! ## Elementary finite-choice Markov chains
  1323. This file develops only the hitting-time facts needed below. Keeping the
  1324. sample space as a finite choice at every step makes all one-step expectations
  1325. finite sums and avoids importing an undeveloped general Markov-chain API.
  1326. -/
  1327. structure FiniteChoiceChain (S Ω : Type*) [Fintype Ω] where
  1328. next : S → Ω → S
  1329. probability : S → Ω → ℝ
  1330. probability_nonneg : ∀ x ω, 0 ≤ probability x ω
  1331. probability_sum : ∀ x, ∑ ω, probability x ω = 1
  1332. namespace FiniteChoiceChain
  1333. variable {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  1334. variable (K : FiniteChoiceChain S Ω) (C : Finset S)
  1335. /-- Probability of avoiding `C` through time `n`, when time zero is included. -/
  1336. def survival : ℕ → S → ℝ
  1337. | 0, x => if x ∈ C then 0 else 1
  1338. | n + 1, x =>
  1339. if x ∈ C then 0 else ∑ ω, K.probability x ω * survival n (K.next x ω)
  1340. /-- Probability of first hitting `C` exactly at time `n`, with the endpoint
  1341. summed over all members of `C`. -/
  1342. def hitTotal : ℕ → S → ℝ
  1343. | 0, x => if x ∈ C then 1 else 0
  1344. | n + 1, x =>
  1345. if x ∈ C then 0 else ∑ ω, K.probability x ω * hitTotal n (K.next x ω)
  1346. /-- Expected hitting time truncated at `n`. -/
  1347. def truncatedHit : ℕ → S → ℝ
  1348. | 0, _ => 0
  1349. | n + 1, x =>
  1350. if x ∈ C then 0 else
  1351. 1 + ∑ ω, K.probability x ω * truncatedHit n (K.next x ω)
  1352. @[simp] theorem survival_zero_of_mem {x : S} (hx : x ∈ C) :
  1353. K.survival C 0 x = 0 := by simp [survival, hx]
  1354. @[simp] theorem survival_zero_of_not_mem {x : S} (hx : x ∉ C) :
  1355. K.survival C 0 x = 1 := by simp [survival, hx]
  1356. @[simp] theorem survival_succ_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :
  1357. K.survival C (n + 1) x = 0 := by simp [survival, hx]
  1358. theorem survival_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :
  1359. K.survival C (n + 1) x =
  1360. ∑ ω, K.probability x ω * K.survival C n (K.next x ω) := by
  1361. simp [survival, hx]
  1362. @[simp] theorem hitTotal_succ_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :
  1363. K.hitTotal C (n + 1) x = 0 := by simp [hitTotal, hx]
  1364. theorem hitTotal_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :
  1365. K.hitTotal C (n + 1) x =
  1366. ∑ ω, K.probability x ω * K.hitTotal C n (K.next x ω) := by
  1367. simp [hitTotal, hx]
  1368. theorem truncatedHit_succ_of_not_mem (n : ℕ) {x : S} (hx : x ∉ C) :
  1369. K.truncatedHit C (n + 1) x =
  1370. 1 + ∑ ω, K.probability x ω * K.truncatedHit C n (K.next x ω) := by
  1371. simp [truncatedHit, hx]
  1372. theorem survival_of_mem (n : ℕ) {x : S} (hx : x ∈ C) :
  1373. K.survival C n x = 0 := by
  1374. cases n with
  1375. | zero => exact survival_zero_of_mem K C hx
  1376. | succ n => exact survival_succ_of_mem K C n hx
  1377. theorem hitTotal_nonneg (n : ℕ) (x : S) : 0 ≤ K.hitTotal C n x := by
  1378. induction n generalizing x with
  1379. | zero => simp only [hitTotal]; split_ifs <;> norm_num
  1380. | succ n ih =>
  1381. by_cases hx : x ∈ C
  1382. · simp [hitTotal, hx]
  1383. · rw [hitTotal_succ_of_not_mem K C n hx]
  1384. exact Finset.sum_nonneg fun ω _ ↦
  1385. mul_nonneg (K.probability_nonneg x ω) (ih (K.next x ω))
  1386. theorem survival_nonneg (n : ℕ) (x : S) : 0 ≤ K.survival C n x := by
  1387. induction n generalizing x with
  1388. | zero => simp only [survival]; split_ifs <;> norm_num
  1389. | succ n ih =>
  1390. by_cases hx : x ∈ C
  1391. · simp [survival, hx]
  1392. · rw [survival_succ_of_not_mem K C n hx]
  1393. exact Finset.sum_nonneg fun ω _ ↦
  1394. mul_nonneg (K.probability_nonneg x ω) (ih (K.next x ω))
  1395. theorem survival_le_one (n : ℕ) (x : S) : K.survival C n x ≤ 1 := by
  1396. induction n generalizing x with
  1397. | zero => simp only [survival]; split_ifs <;> norm_num
  1398. | succ n ih =>
  1399. by_cases hx : x ∈ C
  1400. · simp [survival, hx]
  1401. · rw [survival_succ_of_not_mem K C n hx, ← K.probability_sum x]
  1402. apply Finset.sum_le_sum
  1403. intro ω _
  1404. simpa only [mul_one] using
  1405. mul_le_mul_of_nonneg_left (ih (K.next x ω)) (K.probability_nonneg x ω)
  1406. /-- Exact one-step tail recursion. -/
  1407. theorem hitTotal_succ_add_survival_succ (n : ℕ) (x : S) :
  1408. K.hitTotal C (n + 1) x + K.survival C (n + 1) x =
  1409. K.survival C n x := by
  1410. induction n generalizing x with
  1411. | zero =>
  1412. by_cases hx : x ∈ C
  1413. · simp [hx]
  1414. · rw [hitTotal_succ_of_not_mem K C 0 hx,
  1415. survival_succ_of_not_mem K C 0 hx]
  1416. rw [← Finset.sum_add_distrib]
  1417. simp_rw [← mul_add]
  1418. have hbase (y : S) : K.hitTotal C 0 y + K.survival C 0 y = 1 := by
  1419. by_cases hy : y ∈ C <;> simp [hitTotal, survival, hy]
  1420. simp_rw [hbase, mul_one, K.probability_sum]
  1421. simp [survival, hx]
  1422. | succ n ih =>
  1423. by_cases hx : x ∈ C
  1424. · simp [hx]
  1425. · rw [hitTotal_succ_of_not_mem K C (n + 1) hx,
  1426. survival_succ_of_not_mem K C (n + 1) hx,
  1427. survival_succ_of_not_mem K C n hx, ← Finset.sum_add_distrib]
  1428. apply Finset.sum_congr rfl
  1429. intro ω _
  1430. rw [← mul_add, ih]
  1431. /-- The probabilities of hitting by time `N` and surviving through time `N`
  1432. partition the sample space. -/
  1433. theorem sum_hitTotal_add_survival (N : ℕ) (x : S) :
  1434. (∑ n ∈ Finset.range (N + 1), K.hitTotal C n x) +
  1435. K.survival C N x = 1 := by
  1436. induction N with
  1437. | zero =>
  1438. by_cases hx : x ∈ C <;> simp [hitTotal, survival, hx]
  1439. | succ N ih =>
  1440. rw [Finset.sum_range_succ]
  1441. have htail := hitTotal_succ_add_survival_succ K C N x
  1442. linarith
  1443. theorem sum_hitTotal_le_one (N : ℕ) (x : S) :
  1444. (∑ n ∈ Finset.range (N + 1), K.hitTotal C n x) ≤ 1 := by
  1445. have hpartition := sum_hitTotal_add_survival K C N x
  1446. linarith [survival_nonneg K C N x]
  1447. /-- Truncation increases by the current survival probability. -/
  1448. theorem truncatedHit_succ_eq_add_survival (n : ℕ) (x : S) :
  1449. K.truncatedHit C (n + 1) x =
  1450. K.truncatedHit C n x + K.survival C n x := by
  1451. induction n generalizing x with
  1452. | zero =>
  1453. by_cases hx : x ∈ C <;> simp [truncatedHit, survival, hx]
  1454. | succ n ih =>
  1455. by_cases hx : x ∈ C
  1456. · simp [hx, truncatedHit]
  1457. · rw [truncatedHit_succ_of_not_mem K C (n + 1) hx,
  1458. truncatedHit_succ_of_not_mem K C n hx,
  1459. survival_succ_of_not_mem K C n hx]
  1460. simp_rw [ih]
  1461. simp_rw [mul_add, Finset.sum_add_distrib]
  1462. ring
  1463. /-- Foster's drift inequality bounds all truncated hitting-time means. -/
  1464. theorem truncatedHit_le_of_drift (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1465. (hdrift : ∀ x, x ∉ C →
  1466. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1467. (n : ℕ) (x : S) : K.truncatedHit C n x ≤ V x := by
  1468. induction n generalizing x with
  1469. | zero => simpa [truncatedHit] using hV x
  1470. | succ n ih =>
  1471. by_cases hx : x ∈ C
  1472. · simp [truncatedHit, hx, hV x]
  1473. · rw [truncatedHit_succ_of_not_mem K C n hx]
  1474. calc
  1475. _ ≤ 1 + ∑ ω, K.probability x ω * V (K.next x ω) := by
  1476. apply add_le_add_right
  1477. apply Finset.sum_le_sum
  1478. intro ω _
  1479. exact mul_le_mul_of_nonneg_left (ih (K.next x ω))
  1480. (K.probability_nonneg x ω)
  1481. _ ≤ V x := by
  1482. have hd := hdrift x hx
  1483. simp_rw [mul_sub] at hd
  1484. rw [Finset.sum_sub_distrib, ← Finset.sum_mul, K.probability_sum,
  1485. one_mul] at hd
  1486. linarith
  1487. /-! ### Positive returns from a point already in the finite set -/
  1488. def returnSurvival (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ
  1489. | 0, _ => 1
  1490. | n + 1, x => ∑ ω, K.probability x ω * K.survival C n (K.next x ω)
  1491. def returnHitTotal (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ
  1492. | 0, _ => 0
  1493. | n + 1, x => ∑ ω, K.probability x ω * K.hitTotal C n (K.next x ω)
  1494. def returnTruncated (K : FiniteChoiceChain S Ω) (C : Finset S) : ℕ → S → ℝ
  1495. | 0, _ => 0
  1496. | n + 1, x => 1 + ∑ ω, K.probability x ω * K.truncatedHit C n (K.next x ω)
  1497. theorem returnSurvival_nonneg (n : ℕ) (x : S) :
  1498. 0 ≤ K.returnSurvival C n x := by
  1499. cases n with
  1500. | zero => simp [returnSurvival]
  1501. | succ n =>
  1502. simp only [returnSurvival]
  1503. exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)
  1504. (survival_nonneg K C n (K.next x ω))
  1505. theorem returnHitTotal_nonneg (n : ℕ) (x : S) :
  1506. 0 ≤ K.returnHitTotal C n x := by
  1507. cases n with
  1508. | zero => simp [returnHitTotal]
  1509. | succ n =>
  1510. simp only [returnHitTotal]
  1511. exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)
  1512. (hitTotal_nonneg K C n (K.next x ω))
  1513. theorem return_hit_add_survival (n : ℕ) (x : S) :
  1514. K.returnHitTotal C (n + 1) x + K.returnSurvival C (n + 1) x =
  1515. K.returnSurvival C n x := by
  1516. cases n with
  1517. | zero =>
  1518. simp only [returnHitTotal, returnSurvival]
  1519. rw [← Finset.sum_add_distrib]
  1520. simp_rw [← mul_add]
  1521. have hbase (y : S) : K.hitTotal C 0 y + K.survival C 0 y = 1 := by
  1522. by_cases hy : y ∈ C <;> simp [hitTotal, survival, hy]
  1523. simp_rw [hbase, mul_one, K.probability_sum]
  1524. | succ n =>
  1525. simp only [returnHitTotal, returnSurvival]
  1526. rw [← Finset.sum_add_distrib]
  1527. apply Finset.sum_congr rfl
  1528. intro ω _
  1529. rw [← mul_add, hitTotal_succ_add_survival_succ]
  1530. theorem returnTruncated_succ_eq_add_survival (n : ℕ) (x : S) :
  1531. K.returnTruncated C (n + 1) x =
  1532. K.returnTruncated C n x + K.returnSurvival C n x := by
  1533. cases n with
  1534. | zero => simp [returnTruncated, returnSurvival, truncatedHit]
  1535. | succ n =>
  1536. simp only [returnTruncated, returnSurvival]
  1537. simp_rw [truncatedHit_succ_eq_add_survival]
  1538. simp_rw [mul_add, Finset.sum_add_distrib]
  1539. ring
  1540. theorem return_partialMean_add_tail (N : ℕ) (x : S) :
  1541. (∑ n ∈ Finset.range (N + 1), (n : ℝ) * K.returnHitTotal C n x) +
  1542. N * K.returnSurvival C N x = K.returnTruncated C N x := by
  1543. induction N with
  1544. | zero => simp [returnTruncated]
  1545. | succ N ih =>
  1546. rw [Finset.sum_range_succ]
  1547. have hhit := return_hit_add_survival K C N x
  1548. have htrunc := returnTruncated_succ_eq_add_survival K C N x
  1549. norm_num only [Nat.cast_add, Nat.cast_one]
  1550. nlinarith
  1551. theorem returnTruncated_le (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1552. (hdrift : ∀ x, x ∉ C →
  1553. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1554. (n : ℕ) (x : S) :
  1555. K.returnTruncated C n x ≤ 1 + ∑ ω, K.probability x ω * V (K.next x ω) := by
  1556. cases n with
  1557. | zero =>
  1558. simp only [returnTruncated]
  1559. exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦
  1560. mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))
  1561. | succ n =>
  1562. simp only [returnTruncated]
  1563. apply add_le_add_right
  1564. apply Finset.sum_le_sum
  1565. intro ω _
  1566. exact mul_le_mul_of_nonneg_left
  1567. (truncatedHit_le_of_drift K C V hV hdrift n (K.next x ω))
  1568. (K.probability_nonneg x ω)
  1569. theorem return_mul_survival_le_truncated (n : ℕ) (x : S) :
  1570. (n : ℝ) * K.returnSurvival C n x ≤ K.returnTruncated C n x := by
  1571. induction n with
  1572. | zero => simp [returnTruncated]
  1573. | succ n ih =>
  1574. rw [returnTruncated_succ_eq_add_survival K C n x,
  1575. Nat.cast_add, Nat.cast_one]
  1576. have hmono : K.returnSurvival C (n + 1) x ≤ K.returnSurvival C n x := by
  1577. have h := return_hit_add_survival K C n x
  1578. linarith [returnHitTotal_nonneg K C (n + 1) x]
  1579. have hle : K.returnSurvival C (n + 1) x ≤ 1 := by
  1580. cases n with
  1581. | zero =>
  1582. simp only [returnSurvival]
  1583. rw [← K.probability_sum x]
  1584. apply Finset.sum_le_sum
  1585. intro ω _
  1586. simpa only [mul_one] using
  1587. mul_le_mul_of_nonneg_left (survival_le_one K C 0 (K.next x ω))
  1588. (K.probability_nonneg x ω)
  1589. | succ n =>
  1590. exact hmono.trans (by
  1591. simp only [returnSurvival]
  1592. rw [← K.probability_sum x]
  1593. apply Finset.sum_le_sum
  1594. intro ω _
  1595. simpa only [mul_one] using mul_le_mul_of_nonneg_left
  1596. (survival_le_one K C n (K.next x ω)) (K.probability_nonneg x ω))
  1597. nlinarith
  1598. theorem returnSurvival_tendsto_zero (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1599. (hdrift : ∀ x, x ∉ C →
  1600. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1601. (x : S) : Filter.Tendsto (fun n ↦ K.returnSurvival C n x)
  1602. Filter.atTop (nhds 0) := by
  1603. let B := 1 + ∑ ω, K.probability x ω * V (K.next x ω)
  1604. have hB : 0 ≤ B := by
  1605. dsimp [B]
  1606. exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦
  1607. mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))
  1608. have hbound (n : ℕ) :
  1609. K.returnSurvival C (n + 1) x ≤ B / (n + 1 : ℝ) := by
  1610. have hm := return_mul_survival_le_truncated K C (n + 1) x
  1611. have ht := returnTruncated_le K C V hV hdrift (n + 1) x
  1612. apply (le_div_iff₀ (by positivity : (0 : ℝ) < n + 1)).2
  1613. calc
  1614. K.returnSurvival C (n + 1) x * (n + 1 : ℝ) =
  1615. (n + 1 : ℝ) * K.returnSurvival C (n + 1) x := by ring
  1616. _ ≤ K.returnTruncated C (n + 1) x := by simpa using hm
  1617. _ ≤ B := by simpa [B] using ht
  1618. have hupper : Filter.Tendsto (fun n : ℕ ↦ B / (n + 1 : ℝ))
  1619. Filter.atTop (nhds 0) := by
  1620. simpa [div_eq_mul_inv] using
  1621. tendsto_const_nhds.mul
  1622. (tendsto_one_div_add_atTop_nhds_zero_nat (𝕜 := ℝ))
  1623. have hshift : Filter.Tendsto (fun n ↦ K.returnSurvival C (n + 1) x)
  1624. Filter.atTop (nhds 0) := by
  1625. apply squeeze_zero'
  1626. (Filter.Eventually.of_forall fun n ↦ returnSurvival_nonneg K C (n + 1) x)
  1627. (Filter.Eventually.of_forall hbound) hupper
  1628. exact (Filter.tendsto_add_atTop_iff_nat 1).mp (by simpa [Nat.add_comm] using hshift)
  1629. theorem returnHitTotal_hasSum_one (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1630. (hdrift : ∀ x, x ∉ C →
  1631. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1632. (x : S) : HasSum (fun n ↦ K.returnHitTotal C n x) 1 := by
  1633. rw [hasSum_iff_tendsto_nat_of_nonneg
  1634. (fun n ↦ returnHitTotal_nonneg K C n x) 1]
  1635. have hsurv := returnSurvival_tendsto_zero K C V hV hdrift x
  1636. have hpartial (N : ℕ) :
  1637. ∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x =
  1638. 1 - K.returnSurvival C N x := by
  1639. induction N with
  1640. | zero => simp [returnHitTotal, returnSurvival]
  1641. | succ N ih =>
  1642. rw [Finset.sum_range_succ, ih]
  1643. have htail := return_hit_add_survival K C N x
  1644. linarith
  1645. have hshift : Filter.Tendsto
  1646. (fun N ↦ ∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x)
  1647. Filter.atTop (nhds 1) := by
  1648. rw [show (fun N ↦ ∑ n ∈ Finset.range (N + 1), K.returnHitTotal C n x) =
  1649. fun N ↦ 1 - K.returnSurvival C N x from funext hpartial]
  1650. simpa using tendsto_const_nhds.sub hsurv
  1651. exact (Filter.tendsto_add_atTop_iff_nat 1).mp (by
  1652. simpa [Nat.add_comm] using hshift)
  1653. theorem weighted_returnHitTotal_summable (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1654. (hdrift : ∀ x, x ∉ C →
  1655. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1656. (x : S) : Summable (fun n : ℕ ↦ (n : ℝ) * K.returnHitTotal C n x) := by
  1657. let B := 1 + ∑ ω, K.probability x ω * V (K.next x ω)
  1658. apply summable_of_sum_range_le (c := B)
  1659. · intro n
  1660. exact mul_nonneg (Nat.cast_nonneg _) (returnHitTotal_nonneg K C n x)
  1661. · intro N
  1662. cases N with
  1663. | zero =>
  1664. dsimp [B]
  1665. exact add_nonneg (by norm_num) (Finset.sum_nonneg fun ω _ ↦
  1666. mul_nonneg (K.probability_nonneg x ω) (hV (K.next x ω)))
  1667. | succ N =>
  1668. have htail := return_partialMean_add_tail K C N x
  1669. have ht := returnTruncated_le K C V hV hdrift N x
  1670. have hzero : (0 : ℝ) ≤ N * K.returnSurvival C N x :=
  1671. mul_nonneg (Nat.cast_nonneg _) (returnSurvival_nonneg K C N x)
  1672. dsimp [B]
  1673. linarith
  1674. /-! ### Endpoints of finite-set excursions -/
  1675. /-- Probability of first hitting `C` at the specified point and exact time. -/
  1676. def hitAt (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : ℕ → S → ℝ
  1677. | 0, x => if x ∈ C ∧ x = y then 1 else 0
  1678. | n + 1, x => if x ∈ C then 0 else
  1679. ∑ ω, K.probability x ω * K.hitAt C y n (K.next x ω)
  1680. theorem hitAt_nonneg (y : S) (n : ℕ) (x : S) : 0 ≤ K.hitAt C y n x := by
  1681. induction n generalizing x with
  1682. | zero => simp only [hitAt]; split_ifs <;> norm_num
  1683. | succ n ih =>
  1684. by_cases hx : x ∈ C
  1685. · simp [hitAt, hx]
  1686. · simp only [hitAt, hx, if_false]
  1687. exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)
  1688. (ih (K.next x ω))
  1689. theorem sum_hitAt_eq_hitTotal (n : ℕ) (x : S) :
  1690. ∑ y ∈ C, K.hitAt C y n x = K.hitTotal C n x := by
  1691. induction n generalizing x with
  1692. | zero =>
  1693. by_cases hx : x ∈ C
  1694. · simp [hitAt, hitTotal, hx]
  1695. · simp [hitAt, hitTotal, hx]
  1696. | succ n ih =>
  1697. by_cases hx : x ∈ C
  1698. · simp [hitAt, hitTotal, hx]
  1699. · simp only [hitAt, hitTotal, hx, if_false]
  1700. rw [Finset.sum_comm]
  1701. apply Finset.sum_congr rfl
  1702. intro ω _
  1703. rw [← Finset.mul_sum, ih]
  1704. /-- Probability of a positive first return to `C` ending at `y`. -/
  1705. def returnAt (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : ℕ → S → ℝ
  1706. | 0, _ => 0
  1707. | n + 1, x => ∑ ω, K.probability x ω * K.hitAt C y n (K.next x ω)
  1708. theorem returnAt_nonneg (y : S) (n : ℕ) (x : S) :
  1709. 0 ≤ K.returnAt C y n x := by
  1710. cases n with
  1711. | zero => simp [returnAt]
  1712. | succ n =>
  1713. simp only [returnAt]
  1714. exact Finset.sum_nonneg fun ω _ ↦ mul_nonneg (K.probability_nonneg x ω)
  1715. (hitAt_nonneg K C y n (K.next x ω))
  1716. theorem sum_returnAt_eq_returnHitTotal (n : ℕ) (x : S) :
  1717. ∑ y ∈ C, K.returnAt C y n x = K.returnHitTotal C n x := by
  1718. cases n with
  1719. | zero => simp [returnAt, returnHitTotal]
  1720. | succ n =>
  1721. simp only [returnAt, returnHitTotal]
  1722. rw [Finset.sum_comm]
  1723. apply Finset.sum_congr rfl
  1724. intro ω _
  1725. rw [← Finset.mul_sum, sum_hitAt_eq_hitTotal]
  1726. theorem returnAt_le_returnHitTotal {y : S} (hy : y ∈ C) (n : ℕ) (x : S) :
  1727. K.returnAt C y n x ≤ K.returnHitTotal C n x := by
  1728. rw [← sum_returnAt_eq_returnHitTotal K C n x]
  1729. exact Finset.single_le_sum (fun z _ ↦ returnAt_nonneg K C z n x) hy
  1730. theorem returnAt_summable (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1731. (hdrift : ∀ x, x ∉ C →
  1732. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1733. {y : S} (hy : y ∈ C) (x : S) : Summable (fun n ↦ K.returnAt C y n x) := by
  1734. apply Summable.of_nonneg_of_le (fun n ↦ returnAt_nonneg K C y n x)
  1735. (fun n ↦ returnAt_le_returnHitTotal K C hy n x)
  1736. exact (returnHitTotal_hasSum_one K C V hV hdrift x).summable
  1737. /-- Transition probability of the chain induced by successive visits to `C`. -/
  1738. noncomputable def inducedProbability (K : FiniteChoiceChain S Ω) (C : Finset S)
  1739. (x y : S) : ℝ := ∑' n, K.returnAt C y n x
  1740. theorem inducedProbability_nonneg (x y : S) : 0 ≤ K.inducedProbability C x y := by
  1741. exact tsum_nonneg fun n ↦ returnAt_nonneg K C y n x
  1742. theorem inducedProbability_row_sum (V : S → ℝ) (hV : ∀ x, 0 ≤ V x)
  1743. (hdrift : ∀ x, x ∉ C →
  1744. (∑ ω, K.probability x ω * (V (K.next x ω) - V x)) ≤ -1)
  1745. (x : S) : ∑ y ∈ C, K.inducedProbability C x y = 1 := by
  1746. have hsummable (y : S) (hy : y ∈ C) :
  1747. Summable (fun n ↦ K.returnAt C y n x) :=
  1748. returnAt_summable K C V hV hdrift hy x
  1749. simp only [inducedProbability]
  1750. rw [← Summable.tsum_finsetSum hsummable]
  1751. simp_rw [sum_returnAt_eq_returnHitTotal]
  1752. exact (returnHitTotal_hasSum_one K C V hV hdrift x).tsum_eq
  1753. end FiniteChoiceChain
  1754. /-! ## The feedback-controlled augmented chain -/
  1755. abbrev ImbalanceState := Fin 3 → ℤ
  1756. abbrev AugmentedState := Fin 19 × ImbalanceState
  1757. abbrev BlockChoice := Fin controlBlockLength → Fin 19
  1758. /-- One controlled block, including its endpoint and net imbalance. -/
  1759. noncomputable def controlledNext (x : AugmentedState) (w : BlockChoice) :
  1760. AugmentedState :=
  1761. (blockEndpoint controlBlockLength x.1 w,
  1762. fun k ↦ x.2 k + blockIncrement (feedback x.2) controlBlockLength x.1 w k)
  1763. noncomputable def controlledProbability (x : AugmentedState) (w : BlockChoice) : ℝ :=
  1764. blockWeight (feedback x.2) controlBlockLength x.1 w
  1765. noncomputable def controlledChain : FiniteChoiceChain AugmentedState BlockChoice where
  1766. next := controlledNext
  1767. probability := controlledProbability
  1768. probability_nonneg := by
  1769. intro x w
  1770. change (0 : ℝ) ≤ (blockWeight (feedback x.2) controlBlockLength x.1 w : ℝ)
  1771. exact_mod_cast blockWeight_nonneg (feedback x.2) controlBlockLength x.1 w
  1772. probability_sum := by
  1773. intro x
  1774. exact sum_blockWeight_real (feedback x.2) controlBlockLength x.1
  1775. noncomputable abbrev BoundedCoordinate :=
  1776. {z : ℤ // z ∈ Finset.Icc (-(controlRadius : ℤ)) controlRadius}
  1777. /-- The finite collection of integer imbalance vectors in the control box. -/
  1778. noncomputable def boundedImbalances : Finset ImbalanceState :=
  1779. Finset.univ.image fun f : Fin 3 → BoundedCoordinate ↦ fun k ↦ (f k : ℤ)
  1780. theorem mem_boundedImbalances_iff (h : ImbalanceState) :
  1781. h ∈ boundedImbalances ↔
  1782. ∀ k, -(controlRadius : ℤ) ≤ h k ∧ h k ≤ controlRadius := by
  1783. constructor
  1784. · intro hh k
  1785. simp only [boundedImbalances, Finset.mem_image] at hh
  1786. obtain ⟨f, _, rfl⟩ := hh
  1787. exact Finset.mem_Icc.mp (f k).property
  1788. · intro hh
  1789. let f : Fin 3 → BoundedCoordinate := fun k ↦ ⟨h k, Finset.mem_Icc.mpr (hh k)⟩
  1790. simp only [boundedImbalances, Finset.mem_image]
  1791. exact ⟨f, Finset.mem_univ f, by rfl⟩
  1792. /-- The finite small set used in the Foster argument. -/
  1793. noncomputable def controlSet : Finset AugmentedState :=
  1794. Finset.univ.product boundedImbalances
  1795. theorem mem_controlSet_iff (x : AugmentedState) :
  1796. x ∈ controlSet ↔
  1797. ∀ k, -(controlRadius : ℤ) ≤ x.2 k ∧ x.2 k ≤ controlRadius := by
  1798. simp [controlSet, mem_boundedImbalances_iff]
  1799. theorem controlRadius_lt_norm_of_not_mem {x : AugmentedState} (hx : x ∉ controlSet) :
  1800. controlRadius < ‖imbalanceReal x.2‖ := by
  1801. by_contra h
  1802. apply hx
  1803. apply (mem_controlSet_iff x).mpr
  1804. have hnorm : ‖imbalanceReal x.2‖ ≤ controlRadius := le_of_not_gt h
  1805. have hcoords := (pi_norm_le_iff_of_nonneg
  1806. (by positivity : (0 : ℝ) ≤ controlRadius)).mp hnorm
  1807. intro k
  1808. have hk := hcoords k
  1809. rw [Real.norm_eq_abs, abs_le] at hk
  1810. have hk' : (-(controlRadius : ℝ) ≤ (x.2 k : ℝ) ∧
  1811. (x.2 k : ℝ) ≤ controlRadius) := by simpa [imbalanceReal] using hk
  1812. exact_mod_cast hk'
  1813. def controlledLyapunov (x : AugmentedState) : ℝ := squareLength x.2
  1814. theorem controlledLyapunov_nonneg (x : AugmentedState) : 0 ≤ controlledLyapunov x := by
  1815. simp only [controlledLyapunov, squareLength]
  1816. exact Finset.sum_nonneg fun k _ ↦ sq_nonneg (x.2 k : ℝ)
  1817. theorem controlled_drift (x : AugmentedState) (hx : x ∉ controlSet) :
  1818. (∑ w : BlockChoice, controlledChain.probability x w *
  1819. (controlledLyapunov (controlledChain.next x w) - controlledLyapunov x)) ≤ -1 := by
  1820. simpa [controlledChain, controlledNext, controlledProbability, controlledLyapunov,
  1821. expectedSquareIncrement] using
  1822. controlled_block_drift x.1 x.2 (controlRadius_lt_norm_of_not_mem hx)
  1823. theorem controlled_return_mean_finite (x : AugmentedState) :
  1824. Summable (fun n : ℕ ↦ (n : ℝ) *
  1825. controlledChain.returnHitTotal controlSet n x) :=
  1826. controlledChain.weighted_returnHitTotal_summable controlSet controlledLyapunov
  1827. controlledLyapunov_nonneg controlled_drift x
  1828. theorem controlled_returnAt_summable {y : AugmentedState} (hy : y ∈ controlSet)
  1829. (x : AugmentedState) :
  1830. Summable (fun n ↦ controlledChain.returnAt controlSet y n x) :=
  1831. controlledChain.returnAt_summable controlSet controlledLyapunov
  1832. controlledLyapunov_nonneg controlled_drift hy x
  1833. /-! ## Repeated excursions to the finite control set -/
  1834. /-- Cauchy convolution, written using the finite antidiagonal of a natural
  1835. number. -/
  1836. def convolution (f g : ℕ → ℝ) (n : ℕ) : ℝ :=
  1837. ∑ p ∈ Finset.HasAntidiagonal.antidiagonal n, f p.1 * g p.2
  1838. theorem summable_prod_of_nonnegative {f g : ℕ → ℝ}
  1839. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)
  1840. (hf : Summable f) (hg : Summable g) :
  1841. Summable fun p : ℕ × ℕ ↦ f p.1 * g p.2 := by
  1842. rw [summable_prod_of_nonneg (fun p ↦ mul_nonneg (hf0 p.1) (hg0 p.2))]
  1843. constructor
  1844. · intro n
  1845. exact hg.mul_left (f n)
  1846. · simpa only [hg.tsum_mul_left] using hf.mul_right (∑' m, g m)
  1847. theorem convolution_nonneg {f g : ℕ → ℝ}
  1848. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n) (n : ℕ) :
  1849. 0 ≤ convolution f g n := by
  1850. exact Finset.sum_nonneg fun p _ ↦ mul_nonneg (hf0 p.1) (hg0 p.2)
  1851. theorem convolution_summable {f g : ℕ → ℝ}
  1852. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)
  1853. (hf : Summable f) (hg : Summable g) : Summable (convolution f g) := by
  1854. have hprod := summable_prod_of_nonnegative hf0 hg0 hf hg
  1855. exact summable_sum_mul_antidiagonal_of_summable_mul hprod
  1856. theorem tsum_convolution {f g : ℕ → ℝ}
  1857. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)
  1858. (hf : Summable f) (hg : Summable g) :
  1859. ∑' n, convolution f g n = (∑' n, f n) * ∑' n, g n := by
  1860. exact (hf.tsum_mul_tsum_eq_tsum_sum_antidiagonal hg
  1861. (summable_prod_of_nonnegative hf0 hg0 hf hg)).symm
  1862. theorem weighted_convolution_eq (f g : ℕ → ℝ) (n : ℕ) :
  1863. (n : ℝ) * convolution f g n =
  1864. convolution (fun a ↦ (a : ℝ) * f a) g n +
  1865. convolution f (fun b ↦ (b : ℝ) * g b) n := by
  1866. simp only [convolution, Finset.mul_sum]
  1867. rw [← Finset.sum_add_distrib]
  1868. apply Finset.sum_congr rfl
  1869. intro p hp
  1870. have hpn : p.1 + p.2 = n := Finset.HasAntidiagonal.mem_antidiagonal.mp hp
  1871. rw [← hpn, Nat.cast_add]
  1872. ring
  1873. theorem weighted_convolution_summable {f g : ℕ → ℝ}
  1874. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)
  1875. (hf : Summable f) (hg : Summable g)
  1876. (hfw : Summable fun n : ℕ ↦ (n : ℝ) * f n)
  1877. (hgw : Summable fun n : ℕ ↦ (n : ℝ) * g n) :
  1878. Summable fun n : ℕ ↦ (n : ℝ) * convolution f g n := by
  1879. have hleft := convolution_summable
  1880. (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n)) hg0 hfw hg
  1881. have hright := convolution_summable hf0
  1882. (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgw
  1883. have heq : (fun n : ℕ ↦ (n : ℝ) * convolution f g n) =
  1884. fun n ↦ convolution (fun a ↦ (a : ℝ) * f a) g n +
  1885. convolution f (fun b ↦ (b : ℝ) * g b) n := by
  1886. funext n
  1887. exact weighted_convolution_eq f g n
  1888. rw [heq]
  1889. exact hleft.add hright
  1890. theorem tsum_weighted_convolution {f g : ℕ → ℝ}
  1891. (hf0 : ∀ n, 0 ≤ f n) (hg0 : ∀ n, 0 ≤ g n)
  1892. (hf : Summable f) (hg : Summable g)
  1893. (hfw : Summable fun n : ℕ ↦ (n : ℝ) * f n)
  1894. (hgw : Summable fun n : ℕ ↦ (n : ℝ) * g n) :
  1895. (∑' n : ℕ, (n : ℝ) * convolution f g n) =
  1896. (∑' n : ℕ, (n : ℝ) * f n) * (∑' n, g n) +
  1897. (∑' n, f n) * ∑' n : ℕ, (n : ℝ) * g n := by
  1898. have hleft := convolution_summable
  1899. (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n)) hg0 hfw hg
  1900. have hright := convolution_summable hf0
  1901. (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgw
  1902. rw [show (fun n : ℕ ↦ (n : ℝ) * convolution f g n) =
  1903. fun n ↦ convolution (fun a ↦ (a : ℝ) * f a) g n +
  1904. convolution f (fun b ↦ (b : ℝ) * g b) n from
  1905. funext (weighted_convolution_eq f g)]
  1906. rw [hleft.tsum_add hright,
  1907. tsum_convolution (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hf0 n))
  1908. hg0 hfw hg,
  1909. tsum_convolution hf0
  1910. (fun n ↦ mul_nonneg (Nat.cast_nonneg n) (hg0 n)) hf hgw]
  1911. theorem summable_finset_sum {A : Type*} (s : Finset A) (f : A → ℕ → ℝ)
  1912. (hf : ∀ a ∈ s, Summable (f a)) :
  1913. Summable fun n ↦ ∑ a ∈ s, f a n := by
  1914. classical
  1915. induction s using Finset.induction_on with
  1916. | empty => simp
  1917. | @insert a s ha ih =>
  1918. simp only [Finset.mem_insert] at hf
  1919. simpa [ha] using (hf a (Or.inl rfl)).add
  1920. (ih fun b hb ↦ hf b (Or.inr hb))
  1921. theorem controlled_weighted_returnAt_summable {y : AugmentedState}
  1922. (hy : y ∈ controlSet) (x : AugmentedState) :
  1923. Summable fun n : ℕ ↦ (n : ℝ) * controlledChain.returnAt controlSet y n x := by
  1924. apply Summable.of_nonneg_of_le
  1925. (fun n ↦ mul_nonneg (Nat.cast_nonneg n)
  1926. (controlledChain.returnAt_nonneg controlSet y n x))
  1927. (fun n ↦ mul_le_mul_of_nonneg_left
  1928. (controlledChain.returnAt_le_returnHitTotal controlSet hy n x)
  1929. (Nat.cast_nonneg n))
  1930. exact controlled_return_mean_finite x
  1931. /-- One plus the sum of the mean return times over the finite control set. -/
  1932. noncomputable def excursionMeanBound : ℝ :=
  1933. 1 + ∑ x ∈ controlSet,
  1934. ∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x
  1935. theorem excursionMeanBound_pos : 0 < excursionMeanBound := by
  1936. dsimp only [excursionMeanBound]
  1937. have hsum : 0 ≤ ∑ x ∈ controlSet,
  1938. ∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x :=
  1939. Finset.sum_nonneg fun x _ ↦ tsum_nonneg fun n ↦
  1940. mul_nonneg (Nat.cast_nonneg n)
  1941. (controlledChain.returnHitTotal_nonneg controlSet n x)
  1942. linarith
  1943. theorem returnMean_le_excursionMeanBound {x : AugmentedState} (hx : x ∈ controlSet) :
  1944. (∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x) ≤
  1945. excursionMeanBound := by
  1946. have hsingle :
  1947. (∑' n : ℕ, (n : ℝ) * controlledChain.returnHitTotal controlSet n x) ≤
  1948. ∑ y ∈ controlSet,
  1949. ∑' n : ℕ, (n : ℝ) *
  1950. controlledChain.returnHitTotal controlSet n y := by
  1951. exact Finset.single_le_sum (fun y _ ↦ tsum_nonneg fun n ↦
  1952. mul_nonneg (Nat.cast_nonneg n)
  1953. (controlledChain.returnHitTotal_nonneg controlSet n y)) hx
  1954. dsimp only [excursionMeanBound]
  1955. linarith
  1956. theorem sum_weighted_returnAt (x : AugmentedState) :
  1957. (∑ y ∈ controlSet,
  1958. ∑' n : ℕ, (n : ℝ) * controlledChain.returnAt controlSet y n x) =
  1959. ∑' n : ℕ, (n : ℝ) *
  1960. controlledChain.returnHitTotal controlSet n x := by
  1961. have hs (y : AugmentedState) (hy : y ∈ controlSet) :=
  1962. controlled_weighted_returnAt_summable hy x
  1963. rw [← Summable.tsum_finsetSum hs]
  1964. apply tsum_congr
  1965. intro n
  1966. rw [← Finset.mul_sum,
  1967. controlledChain.sum_returnAt_eq_returnHitTotal controlSet n x]
  1968. /-- The zero imbalance at interval zero is a concrete member of the finite
  1969. control set. -/
  1970. def originState : AugmentedState := (0, fun _ ↦ 0)
  1971. theorem originState_mem_controlSet : originState ∈ controlSet := by
  1972. rw [mem_controlSet_iff]
  1973. intro j
  1974. simp [originState]
  1975. /-! ## Finite-choice paths -/
  1976. /-- A noncomputable indicator, convenient for finite sums over predicates. -/
  1977. noncomputable def selectedWeight (P : Prop) (r : ℝ) : ℝ := by
  1978. classical
  1979. exact if P then r else 0
  1980. theorem selectedWeight_mul_left (P : Prop) (a r : ℝ) :
  1981. selectedWeight P (a * r) = a * selectedWeight P r := by
  1982. classical
  1983. by_cases hP : P <;> simp [selectedWeight, hP]
  1984. theorem sum_subtype_eq_selected {A : Type*} [Fintype A]
  1985. (P : A → Prop) [Fintype {a : A // P a}] (g : A → ℝ) :
  1986. (∑ x : {a : A // P a}, g x.1) = ∑ a, selectedWeight (P a) (g a) := by
  1987. classical
  1988. rw [← Finset.sum_subtype (Finset.univ.filter P) (by simp) g]
  1989. simpa only [selectedWeight] using Finset.sum_filter P g
  1990. theorem sum_finset_subtype {A : Type*} (s : Finset A) (g : A → ℝ) :
  1991. (∑ x : ↑s, g x.1) = ∑ x ∈ s, g x := by
  1992. classical
  1993. simpa using Finset.sum_attach s g
  1994. theorem sum_subtype_eq_sum_attach {A : Type*} (s : Finset A) (g : ↑s → ℝ) :
  1995. (∑ x : ↑s, g x) = ∑ x ∈ s.attach, g x := by
  1996. classical
  1997. rfl
  1998. namespace FiniteChoiceChain
  1999. variable {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  2000. /-- Endpoint after a tuple of choices. -/
  2001. def pathEndpoint (K : FiniteChoiceChain S Ω) :
  2002. (n : ℕ) → S → (Fin n → Ω) → S
  2003. | 0, x, _ => x
  2004. | n + 1, x, w => K.pathEndpoint n (K.next x (w 0)) (Fin.tail w)
  2005. /-- Product of the one-step choice probabilities along a tuple. -/
  2006. def pathWeight (K : FiniteChoiceChain S Ω) :
  2007. (n : ℕ) → S → (Fin n → Ω) → ℝ
  2008. | 0, _, _ => 1
  2009. | n + 1, x, w =>
  2010. K.probability x (w 0) * K.pathWeight n (K.next x (w 0)) (Fin.tail w)
  2011. /-- A path that hits `C` for the first time at its endpoint `y`; time zero
  2012. counts as a hit. -/
  2013. def IsHitPath (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) :
  2014. (n : ℕ) → S → (Fin n → Ω) → Prop
  2015. | 0, x, _ => x ∈ C ∧ x = y
  2016. | n + 1, x, w => x ∉ C ∧
  2017. K.IsHitPath C y n (K.next x (w 0)) (Fin.tail w)
  2018. /-- A positive first return to `C`, ending at `y`. -/
  2019. def IsReturnPath (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) :
  2020. (n : ℕ) → S → (Fin n → Ω) → Prop
  2021. | 0, _, _ => False
  2022. | n + 1, x, w =>
  2023. K.IsHitPath C y n (K.next x (w 0)) (Fin.tail w)
  2024. omit [DecidableEq S] in
  2025. @[simp] theorem pathEndpoint_cons (K : FiniteChoiceChain S Ω) (n : ℕ)
  2026. (x : S) (a : Ω) (w : Fin n → Ω) :
  2027. K.pathEndpoint (n + 1) x (Fin.cons a w) =
  2028. K.pathEndpoint n (K.next x a) w := by
  2029. simp [pathEndpoint]
  2030. omit [DecidableEq S] in
  2031. @[simp] theorem pathWeight_cons (K : FiniteChoiceChain S Ω) (n : ℕ)
  2032. (x : S) (a : Ω) (w : Fin n → Ω) :
  2033. K.pathWeight (n + 1) x (Fin.cons a w) =
  2034. K.probability x a * K.pathWeight n (K.next x a) w := by
  2035. simp [pathWeight]
  2036. omit [DecidableEq S] in
  2037. @[simp] theorem isHitPath_zero (K : FiniteChoiceChain S Ω) (C : Finset S)
  2038. (x y : S) (w : Fin 0 → Ω) : K.IsHitPath C y 0 x w ↔ x ∈ C ∧ x = y :=
  2039. Iff.rfl
  2040. omit [DecidableEq S] in
  2041. @[simp] theorem isHitPath_cons (K : FiniteChoiceChain S Ω) (C : Finset S)
  2042. (n : ℕ) (x y : S) (a : Ω) (w : Fin n → Ω) :
  2043. K.IsHitPath C y (n + 1) x (Fin.cons a w) ↔
  2044. x ∉ C ∧ K.IsHitPath C y n (K.next x a) w := by
  2045. simp [IsHitPath]
  2046. omit [DecidableEq S] in
  2047. @[simp] theorem isReturnPath_zero (K : FiniteChoiceChain S Ω) (C : Finset S)
  2048. (x y : S) (w : Fin 0 → Ω) : ¬ K.IsReturnPath C y 0 x w := by
  2049. simp [IsReturnPath]
  2050. omit [DecidableEq S] in
  2051. @[simp] theorem isReturnPath_cons (K : FiniteChoiceChain S Ω) (C : Finset S)
  2052. (n : ℕ) (x y : S) (a : Ω) (w : Fin n → Ω) :
  2053. K.IsReturnPath C y (n + 1) x (Fin.cons a w) ↔
  2054. K.IsHitPath C y n (K.next x a) w := by
  2055. simp [IsReturnPath]
  2056. theorem sum_hitPath_weight (K : FiniteChoiceChain S Ω) (C : Finset S)
  2057. (y : S) (n : ℕ) (x : S) :
  2058. ∑ w : Fin n → Ω,
  2059. selectedWeight (K.IsHitPath C y n x w) (K.pathWeight n x w) =
  2060. K.hitAt C y n x := by
  2061. classical
  2062. induction n generalizing x with
  2063. | zero =>
  2064. by_cases hxy : x ∈ C ∧ x = y <;>
  2065. simp [selectedWeight, IsHitPath, hitAt, pathWeight, hxy]
  2066. | succ n ih =>
  2067. rw [sum_tuple_succ]
  2068. by_cases hx : x ∈ C
  2069. · simp [selectedWeight, IsHitPath, hitAt, hx]
  2070. · simp only [isHitPath_cons, hx, not_false_eq_true,
  2071. true_and, pathWeight_cons, hitAt, if_false]
  2072. apply Finset.sum_congr rfl
  2073. intro a _
  2074. simp_rw [selectedWeight_mul_left]
  2075. rw [← Finset.mul_sum, ih]
  2076. theorem sum_returnPath_weight (K : FiniteChoiceChain S Ω) (C : Finset S)
  2077. (y : S) (n : ℕ) (x : S) :
  2078. ∑ w : Fin n → Ω,
  2079. selectedWeight (K.IsReturnPath C y n x w) (K.pathWeight n x w) =
  2080. K.returnAt C y n x := by
  2081. classical
  2082. cases n with
  2083. | zero => simp [selectedWeight, IsReturnPath, returnAt]
  2084. | succ n =>
  2085. rw [sum_tuple_succ]
  2086. simp only [isReturnPath_cons, pathWeight_cons, returnAt]
  2087. apply Finset.sum_congr rfl
  2088. intro a _
  2089. simp_rw [selectedWeight_mul_left]
  2090. rw [← Finset.mul_sum, sum_hitPath_weight]
  2091. end FiniteChoiceChain
  2092. /-! ## Recurrence in a finite stochastic kernel -/
  2093. structure FiniteKernel (F : Type*) [Fintype F] where
  2094. probability : F → F → ℝ
  2095. probability_nonneg : ∀ x y, 0 ≤ probability x y
  2096. probability_sum : ∀ x, ∑ y, probability x y = 1
  2097. namespace FiniteKernel
  2098. variable {F : Type*} [Fintype F] [DecidableEq F] [Nonempty F]
  2099. def Edge (P : FiniteKernel F) (x y : F) : Prop := 0 < P.probability x y
  2100. def EdgePath (P : FiniteKernel F) : F → List F → F → Prop
  2101. | x, [], y => x = y
  2102. | x, z :: zs, y => P.Edge x z ∧ P.EdgePath z zs y
  2103. omit [DecidableEq F] [Nonempty F] in
  2104. theorem EdgePath.append (P : FiniteKernel F) {x y z : F} {u v : List F}
  2105. (hu : P.EdgePath x u y) (hv : P.EdgePath y v z) :
  2106. P.EdgePath x (u ++ v) z := by
  2107. induction u generalizing x with
  2108. | nil => simpa [EdgePath] using hu ▸ hv
  2109. | cons a u ih =>
  2110. obtain ⟨hxa, hau⟩ := hu
  2111. exact ⟨hxa, ih hau⟩
  2112. noncomputable def reachable (P : FiniteKernel F) (x : F) : Finset F :=
  2113. by
  2114. classical
  2115. exact Finset.univ.filter fun y ↦ ∃ w, P.EdgePath x w y
  2116. omit [DecidableEq F] [Nonempty F] in
  2117. theorem mem_reachable_iff (P : FiniteKernel F) (x y : F) :
  2118. y ∈ P.reachable x ↔ ∃ w, P.EdgePath x w y := by
  2119. classical
  2120. simp [reachable]
  2121. omit [DecidableEq F] [Nonempty F] in
  2122. theorem self_mem_reachable (P : FiniteKernel F) (x : F) : x ∈ P.reachable x := by
  2123. exact (P.mem_reachable_iff x x).2 ⟨[], rfl⟩
  2124. omit [DecidableEq F] [Nonempty F] in
  2125. theorem reachable_mono {P : FiniteKernel F} {x y : F} (hxy : y ∈ P.reachable x) :
  2126. P.reachable y ⊆ P.reachable x := by
  2127. intro z hz
  2128. obtain ⟨u, hu⟩ := (P.mem_reachable_iff x y).1 hxy
  2129. obtain ⟨v, hv⟩ := (P.mem_reachable_iff y z).1 hz
  2130. exact (P.mem_reachable_iff x z).2 ⟨u ++ v, hu.append P hv⟩
  2131. /-- A state whose reachable set has minimum cardinality. -/
  2132. noncomputable def recurrentState (P : FiniteKernel F) : F :=
  2133. Classical.choose (Finset.exists_min_image Finset.univ
  2134. (fun x ↦ (P.reachable x).card) Finset.univ_nonempty)
  2135. omit [DecidableEq F] in
  2136. theorem recurrentState_minimal (P : FiniteKernel F) (x : F) :
  2137. (P.reachable P.recurrentState).card ≤ (P.reachable x).card := by
  2138. exact (Classical.choose_spec (Finset.exists_min_image Finset.univ
  2139. (fun x ↦ (P.reachable x).card) Finset.univ_nonempty)).2 x (Finset.mem_univ x)
  2140. omit [DecidableEq F] in
  2141. theorem recurrent_reachable_eq {P : FiniteKernel F} {y : F}
  2142. (hy : y ∈ P.reachable P.recurrentState) :
  2143. P.reachable y = P.reachable P.recurrentState := by
  2144. apply Finset.eq_of_subset_of_card_le (reachable_mono hy)
  2145. exact P.recurrentState_minimal y
  2146. omit [DecidableEq F] in
  2147. theorem recurrentState_reachable_from {P : FiniteKernel F} {y : F}
  2148. (hy : y ∈ P.reachable P.recurrentState) :
  2149. P.recurrentState ∈ P.reachable y := by
  2150. rw [recurrent_reachable_eq hy]
  2151. exact P.self_mem_reachable P.recurrentState
  2152. omit [DecidableEq F] in
  2153. theorem reachable_closed_edge {P : FiniteKernel F} {x y : F}
  2154. (hx : x ∈ P.reachable P.recurrentState) (hxy : P.Edge x y) :
  2155. y ∈ P.reachable P.recurrentState := by
  2156. obtain ⟨u, hu⟩ := (P.mem_reachable_iff P.recurrentState x).1 hx
  2157. exact (P.mem_reachable_iff P.recurrentState y).2
  2158. ⟨u ++ [y], hu.append P ⟨hxy, rfl⟩⟩
  2159. noncomputable abbrev RecurrentClass (P : FiniteKernel F) :=
  2160. {x : F // x ∈ P.reachable P.recurrentState}
  2161. omit [DecidableEq F] in
  2162. theorem probability_zero_outside {P : FiniteKernel F}
  2163. (x : P.RecurrentClass) {y : F}
  2164. (hy : y ∉ P.reachable P.recurrentState) : P.probability x.1 y = 0 := by
  2165. apply le_antisymm
  2166. · apply le_of_not_gt
  2167. intro hpos
  2168. exact hy (reachable_closed_edge x.2 hpos)
  2169. · exact P.probability_nonneg x.1 y
  2170. /-- Restriction to the closed minimal reachable class. -/
  2171. noncomputable def recurrentKernel (P : FiniteKernel F) :
  2172. FiniteKernel P.RecurrentClass where
  2173. probability x y := P.probability x.1 y.1
  2174. probability_nonneg x y := P.probability_nonneg x.1 y.1
  2175. probability_sum x := by
  2176. classical
  2177. rw [← P.probability_sum x.1]
  2178. rw [← Finset.sum_subtype (P.reachable P.recurrentState)
  2179. (fun y ↦ by simp) (fun y ↦ P.probability x.1 y)]
  2180. apply Finset.sum_subset (by simp)
  2181. intro y _ hy
  2182. exact probability_zero_outside x hy
  2183. omit [DecidableEq F] in
  2184. theorem lift_edgePath_recurrent {P : FiniteKernel F} {x y : F} {w : List F}
  2185. (hx : x ∈ P.reachable P.recurrentState)
  2186. (hy : y ∈ P.reachable P.recurrentState) (hw : P.EdgePath x w y) :
  2187. ∃ v : List P.RecurrentClass,
  2188. P.recurrentKernel.EdgePath ⟨x, hx⟩ v ⟨y, hy⟩ := by
  2189. induction w generalizing x with
  2190. | nil =>
  2191. subst x
  2192. exact ⟨[], rfl⟩
  2193. | cons z zs ih =>
  2194. have hz := reachable_closed_edge hx hw.1
  2195. obtain ⟨v, hv⟩ := ih hz hw.2
  2196. refine ⟨⟨z, hz⟩ :: v, ?_⟩
  2197. exact ⟨hw.1, hv⟩
  2198. noncomputable def recurrentClassBase (P : FiniteKernel F) : P.RecurrentClass :=
  2199. ⟨P.recurrentState, P.self_mem_reachable P.recurrentState⟩
  2200. omit [DecidableEq F] in
  2201. theorem recurrentKernel_reaches_base (P : FiniteKernel F)
  2202. (x : P.RecurrentClass) :
  2203. P.recurrentClassBase ∈ P.recurrentKernel.reachable x := by
  2204. have hs := P.recurrentState_reachable_from x.2
  2205. obtain ⟨w, hw⟩ := (P.mem_reachable_iff x.1 P.recurrentState).1 hs
  2206. obtain ⟨v, hv⟩ := lift_edgePath_recurrent x.2
  2207. (P.self_mem_reachable P.recurrentState) hw
  2208. exact (P.recurrentKernel.mem_reachable_iff x P.recurrentClassBase).2 ⟨v, hv⟩
  2209. /-! ### A uniform hitting estimate in a finite communicating class -/
  2210. noncomputable def toChain (P : FiniteKernel F) : FiniteChoiceChain F F where
  2211. next _ y := y
  2212. probability := P.probability
  2213. probability_nonneg := P.probability_nonneg
  2214. probability_sum := P.probability_sum
  2215. def pathProbability (P : FiniteKernel F) : F → List F → ℝ
  2216. | _, [] => 1
  2217. | x, y :: ys => P.probability x y * P.pathProbability y ys
  2218. omit [DecidableEq F] [Nonempty F] in
  2219. theorem probability_le_one (P : FiniteKernel F) (x y : F) :
  2220. P.probability x y ≤ 1 := by
  2221. rw [← P.probability_sum x]
  2222. exact Finset.single_le_sum (fun z _ ↦ P.probability_nonneg x z) (Finset.mem_univ y)
  2223. omit [DecidableEq F] [Nonempty F] in
  2224. theorem pathProbability_nonneg (P : FiniteKernel F) (x : F) (w : List F) :
  2225. 0 ≤ P.pathProbability x w := by
  2226. induction w generalizing x with
  2227. | nil => simp [pathProbability]
  2228. | cons y ys ih =>
  2229. exact mul_nonneg (P.probability_nonneg x y) (ih y)
  2230. omit [DecidableEq F] [Nonempty F] in
  2231. theorem pathProbability_le_one (P : FiniteKernel F) (x : F) (w : List F) :
  2232. P.pathProbability x w ≤ 1 := by
  2233. induction w generalizing x with
  2234. | nil => simp [pathProbability]
  2235. | cons y ys ih =>
  2236. simp only [pathProbability]
  2237. nlinarith [P.probability_nonneg x y, P.probability_le_one x y,
  2238. P.pathProbability_nonneg y ys, ih y]
  2239. omit [DecidableEq F] [Nonempty F] in
  2240. theorem pathProbability_pos {P : FiniteKernel F} {x y : F} {w : List F}
  2241. (hw : P.EdgePath x w y) : 0 < P.pathProbability x w := by
  2242. induction w generalizing x with
  2243. | nil => simp [pathProbability]
  2244. | cons z zs ih =>
  2245. exact mul_pos hw.1 (ih hw.2)
  2246. omit [Nonempty F] in
  2247. theorem survival_antitone (P : FiniteKernel F) (C : Finset F) (x : F) :
  2248. Antitone fun n ↦ P.toChain.survival C n x := by
  2249. apply antitone_nat_of_succ_le
  2250. intro n
  2251. have htail := P.toChain.hitTotal_succ_add_survival_succ C n x
  2252. linarith [P.toChain.hitTotal_nonneg C (n + 1) x]
  2253. omit [Nonempty F] in
  2254. theorem pathProbability_le_hitBy {P : FiniteKernel F} {x s : F} {w : List F}
  2255. (hw : P.EdgePath x w s) :
  2256. P.pathProbability x w ≤
  2257. 1 - P.toChain.survival {s} w.length x := by
  2258. induction w generalizing x with
  2259. | nil =>
  2260. subst x
  2261. simp [pathProbability]
  2262. | cons y ys ih =>
  2263. by_cases hxs : x = s
  2264. · subst x
  2265. rw [P.toChain.survival_of_mem {s} (y :: ys).length
  2266. (Finset.mem_singleton_self s)]
  2267. simp only [sub_zero]
  2268. exact P.pathProbability_le_one s (y :: ys)
  2269. · have hxmem : x ∉ ({s} : Finset F) := by simpa
  2270. have hrest := ih hw.2
  2271. have hhit0 (z : F) :
  2272. 0 ≤ 1 - P.toChain.survival {s} ys.length z := by
  2273. linarith [P.toChain.survival_le_one {s} ys.length z]
  2274. have hone :
  2275. 1 - P.toChain.survival {s} (y :: ys).length x =
  2276. ∑ z, P.probability x z *
  2277. (1 - P.toChain.survival {s} ys.length z) := by
  2278. rw [List.length_cons,
  2279. P.toChain.survival_succ_of_not_mem {s} ys.length hxmem]
  2280. calc
  2281. 1 - ∑ z, P.toChain.probability x z *
  2282. P.toChain.survival {s} ys.length (P.toChain.next x z) =
  2283. (∑ z, P.probability x z) -
  2284. ∑ z, P.toChain.probability x z *
  2285. P.toChain.survival {s} ys.length (P.toChain.next x z) := by
  2286. rw [P.probability_sum]
  2287. _ = ∑ z, (P.probability x z -
  2288. P.toChain.probability x z *
  2289. P.toChain.survival {s} ys.length (P.toChain.next x z)) := by
  2290. rw [Finset.sum_sub_distrib]
  2291. _ = ∑ z, P.probability x z *
  2292. (1 - P.toChain.survival {s} ys.length z) := by
  2293. apply Finset.sum_congr rfl
  2294. intro z _
  2295. simp only [toChain]
  2296. ring
  2297. rw [hone]
  2298. calc
  2299. P.pathProbability x (y :: ys) =
  2300. P.probability x y * P.pathProbability y ys := rfl
  2301. _ ≤ P.probability x y *
  2302. (1 - P.toChain.survival {s} ys.length y) :=
  2303. mul_le_mul_of_nonneg_left hrest (P.probability_nonneg x y)
  2304. _ ≤ ∑ z, P.probability x z *
  2305. (1 - P.toChain.survival {s} ys.length z) :=
  2306. Finset.single_le_sum (fun z _ ↦
  2307. mul_nonneg (P.probability_nonneg x z) (hhit0 z)) (Finset.mem_univ y)
  2308. noncomputable def route (P : FiniteKernel F) (s : F)
  2309. (hreach : ∀ x, s ∈ P.reachable x) (x : F) : List F :=
  2310. Classical.choose ((P.mem_reachable_iff x s).1 (hreach x))
  2311. omit [DecidableEq F] [Nonempty F] in
  2312. theorem route_spec (P : FiniteKernel F) (s : F)
  2313. (hreach : ∀ x, s ∈ P.reachable x) (x : F) :
  2314. P.EdgePath x (P.route s hreach x) s :=
  2315. Classical.choose_spec ((P.mem_reachable_iff x s).1 (hreach x))
  2316. noncomputable def routeLength (P : FiniteKernel F) (s : F)
  2317. (hreach : ∀ x, s ∈ P.reachable x) : ℕ :=
  2318. 1 + ∑ x, (P.route s hreach x).length
  2319. noncomputable def routeMinorization (P : FiniteKernel F) (s : F)
  2320. (hreach : ∀ x, s ∈ P.reachable x) : ℝ :=
  2321. ∏ x, P.pathProbability x (P.route s hreach x)
  2322. omit [DecidableEq F] [Nonempty F] in
  2323. theorem routeLength_pos (P : FiniteKernel F) (s : F)
  2324. (hreach : ∀ x, s ∈ P.reachable x) : 0 < P.routeLength s hreach := by
  2325. simp [routeLength]
  2326. omit [DecidableEq F] [Nonempty F] in
  2327. theorem route_length_lt (P : FiniteKernel F) (s : F)
  2328. (hreach : ∀ x, s ∈ P.reachable x) (x : F) :
  2329. (P.route s hreach x).length < P.routeLength s hreach := by
  2330. dsimp only [routeLength]
  2331. have hle : (P.route s hreach x).length ≤
  2332. ∑ y, (P.route s hreach y).length :=
  2333. Finset.single_le_sum
  2334. (fun y _ ↦ Nat.zero_le (P.route s hreach y).length) (Finset.mem_univ x)
  2335. omega
  2336. omit [DecidableEq F] [Nonempty F] in
  2337. theorem routeMinorization_pos (P : FiniteKernel F) (s : F)
  2338. (hreach : ∀ x, s ∈ P.reachable x) :
  2339. 0 < P.routeMinorization s hreach := by
  2340. apply Finset.prod_pos
  2341. intro x _
  2342. exact pathProbability_pos (P.route_spec s hreach x)
  2343. omit [DecidableEq F] [Nonempty F] in
  2344. theorem routeMinorization_le_route (P : FiniteKernel F) (s : F)
  2345. (hreach : ∀ x, s ∈ P.reachable x) (x : F) :
  2346. P.routeMinorization s hreach ≤
  2347. P.pathProbability x (P.route s hreach x) := by
  2348. classical
  2349. rw [routeMinorization, ← Finset.mul_prod_erase Finset.univ _ (Finset.mem_univ x)]
  2350. nth_rewrite 2 [← mul_one (P.pathProbability x (P.route s hreach x))]
  2351. apply mul_le_mul_of_nonneg_left
  2352. · exact Finset.prod_le_one (fun y _ ↦ P.pathProbability_nonneg y _)
  2353. (fun y _ ↦ P.pathProbability_le_one y _)
  2354. · exact P.pathProbability_nonneg x _
  2355. omit [Nonempty F] in
  2356. theorem uniform_survival_bound (P : FiniteKernel F) (s : F)
  2357. (hreach : ∀ x, s ∈ P.reachable x) (x : F) :
  2358. P.toChain.survival {s} (P.routeLength s hreach) x ≤
  2359. 1 - P.routeMinorization s hreach := by
  2360. have hpath := P.pathProbability_le_hitBy (P.route_spec s hreach x)
  2361. have hmono := P.survival_antitone {s} x
  2362. (Nat.le_of_lt (P.route_length_lt s hreach x))
  2363. have hminor := P.routeMinorization_le_route s hreach x
  2364. linarith
  2365. omit [Nonempty F] in
  2366. theorem survival_add_le (P : FiniteKernel F) (C : Finset F) (c : ℝ) {n : ℕ}
  2367. (hn : ∀ y, P.toChain.survival C n y ≤ c) (m : ℕ) (x : F) :
  2368. P.toChain.survival C (m + n) x ≤
  2369. c * P.toChain.survival C m x := by
  2370. induction m generalizing x with
  2371. | zero =>
  2372. by_cases hx : x ∈ C
  2373. · simp [P.toChain.survival_of_mem C n hx,
  2374. P.toChain.survival_zero_of_mem C hx]
  2375. · simpa [P.toChain.survival_zero_of_not_mem C hx] using hn x
  2376. | succ m ih =>
  2377. by_cases hx : x ∈ C
  2378. · simp [P.toChain.survival_of_mem C (m + 1 + n) hx,
  2379. P.toChain.survival_of_mem C (m + 1) hx]
  2380. · rw [show m + 1 + n = (m + n) + 1 by omega,
  2381. P.toChain.survival_succ_of_not_mem C (m + n) hx,
  2382. P.toChain.survival_succ_of_not_mem C m hx]
  2383. calc
  2384. ∑ y, P.probability x y * P.toChain.survival C (m + n) y ≤
  2385. ∑ y, P.probability x y *
  2386. (c * P.toChain.survival C m y) := by
  2387. apply Finset.sum_le_sum
  2388. intro y _
  2389. exact mul_le_mul_of_nonneg_left (ih y) (P.probability_nonneg x y)
  2390. _ = c * ∑ y, P.probability x y *
  2391. P.toChain.survival C m y := by
  2392. rw [Finset.mul_sum]
  2393. apply Finset.sum_congr rfl
  2394. intro y _
  2395. ring
  2396. omit [DecidableEq F] [Nonempty F] in
  2397. theorem minorization_le_one (P : FiniteKernel F) (s : F)
  2398. (hreach : ∀ x, s ∈ P.reachable x) :
  2399. P.routeMinorization s hreach ≤ 1 := by
  2400. classical
  2401. exact (P.routeMinorization_le_route s hreach s).trans
  2402. (P.pathProbability_le_one s (P.route s hreach s))
  2403. omit [Nonempty F] in
  2404. theorem survival_blocks (P : FiniteKernel F) (s : F)
  2405. (hreach : ∀ x, s ∈ P.reachable x) (k : ℕ) (x : F) :
  2406. P.toChain.survival {s} (P.routeLength s hreach * k) x ≤
  2407. (1 - P.routeMinorization s hreach) ^ k := by
  2408. let r := 1 - P.routeMinorization s hreach
  2409. have hr0 : 0 ≤ r := sub_nonneg.mpr (P.minorization_le_one s hreach)
  2410. induction k with
  2411. | zero => exact P.toChain.survival_le_one {s} 0 x
  2412. | succ k ih =>
  2413. have hcontract := P.survival_add_le {s} r
  2414. (P.uniform_survival_bound s hreach) (P.routeLength s hreach * k) x
  2415. rw [Nat.mul_succ]
  2416. calc
  2417. P.toChain.survival {s}
  2418. (P.routeLength s hreach * k + P.routeLength s hreach) x ≤
  2419. r * P.toChain.survival {s} (P.routeLength s hreach * k) x := hcontract
  2420. _ ≤ r * r ^ k := mul_le_mul_of_nonneg_left ih hr0
  2421. _ = r ^ (k + 1) := by rw [pow_succ']
  2422. omit [Nonempty F] in
  2423. theorem returnSurvival_antitone (P : FiniteKernel F) (C : Finset F) (x : F) :
  2424. Antitone fun n ↦ P.toChain.returnSurvival C n x := by
  2425. apply antitone_nat_of_succ_le
  2426. intro n
  2427. have htail := P.toChain.return_hit_add_survival C n x
  2428. linarith [P.toChain.returnHitTotal_nonneg C (n + 1) x]
  2429. omit [Nonempty F] in
  2430. theorem returnSurvival_blocks (P : FiniteKernel F) (s : F)
  2431. (hreach : ∀ x, s ∈ P.reachable x) (k : ℕ) :
  2432. P.toChain.returnSurvival {s} (P.routeLength s hreach * k + 1) s ≤
  2433. (1 - P.routeMinorization s hreach) ^ k := by
  2434. simp only [FiniteChoiceChain.returnSurvival]
  2435. calc
  2436. ∑ y, P.probability s y *
  2437. P.toChain.survival {s} (P.routeLength s hreach * k) y ≤
  2438. ∑ y, P.probability s y *
  2439. ((1 - P.routeMinorization s hreach) ^ k) := by
  2440. apply Finset.sum_le_sum
  2441. intro y _
  2442. exact mul_le_mul_of_nonneg_left (P.survival_blocks s hreach k y)
  2443. (P.probability_nonneg s y)
  2444. _ = (1 - P.routeMinorization s hreach) ^ k := by
  2445. rw [← Finset.sum_mul, P.probability_sum, one_mul]
  2446. theorem summable_pow_nat_div {L : ℕ} (hL : 0 < L) {r : ℝ}
  2447. (hr0 : 0 ≤ r) (hr1 : r < 1) :
  2448. Summable fun n : ℕ ↦ r ^ (n / L) := by
  2449. let : NeZero L := ⟨hL.ne'⟩
  2450. have hgeom : Summable fun n : ℕ ↦ r ^ n :=
  2451. summable_geometric_of_lt_one hr0 hr1
  2452. have hprod : Summable fun p : ℕ × Fin L ↦ r ^ p.1 := by
  2453. rw [summable_prod_of_nonneg (fun p ↦ pow_nonneg hr0 p.1)]
  2454. constructor
  2455. · intro n
  2456. exact (hasSum_fintype (fun _ : Fin L ↦ r ^ n)).summable
  2457. · simpa only [tsum_fintype, Finset.sum_const, nsmul_eq_mul,
  2458. Finset.card_univ, Fintype.card_fin, mul_comm] using
  2459. hgeom.mul_left (L : ℝ)
  2460. have hcomp := ((Nat.divModEquiv L).summable_iff).2 hprod
  2461. simpa [Function.comp_def, Nat.divModEquiv] using hcomp
  2462. omit [Nonempty F] in
  2463. theorem returnSurvival_summable (P : FiniteKernel F) (s : F)
  2464. (hreach : ∀ x, s ∈ P.reachable x) :
  2465. Summable fun n ↦ P.toChain.returnSurvival {s} n s := by
  2466. let L := P.routeLength s hreach
  2467. let r := 1 - P.routeMinorization s hreach
  2468. have hL : 0 < L := P.routeLength_pos s hreach
  2469. have hr0 : 0 ≤ r := sub_nonneg.mpr (P.minorization_le_one s hreach)
  2470. have hr1 : r < 1 := by
  2471. dsimp only [r]
  2472. linarith [P.routeMinorization_pos s hreach]
  2473. have hbound (n : ℕ) : P.toChain.returnSurvival {s} (n + 1) s ≤
  2474. r ^ (n / L) := by
  2475. have hindex : L * (n / L) + 1 ≤ n + 1 := by
  2476. apply Nat.add_le_add_right
  2477. simpa [Nat.mul_comm] using Nat.div_mul_le_self n L
  2478. exact (P.returnSurvival_antitone {s} s hindex).trans
  2479. (P.returnSurvival_blocks s hreach (n / L))
  2480. have hshift : Summable fun n ↦ P.toChain.returnSurvival {s} (n + 1) s := by
  2481. apply Summable.of_nonneg_of_le
  2482. (fun n ↦ P.toChain.returnSurvival_nonneg {s} (n + 1) s) hbound
  2483. exact summable_pow_nat_div hL hr0 hr1
  2484. exact (summable_nat_add_iff 1).1 (by simpa [Nat.add_comm] using hshift)
  2485. omit [Nonempty F] in
  2486. theorem returnTruncated_eq_sum_survival (P : FiniteKernel F)
  2487. (C : Finset F) (N : ℕ) (x : F) :
  2488. P.toChain.returnTruncated C N x =
  2489. ∑ n ∈ Finset.range N, P.toChain.returnSurvival C n x := by
  2490. induction N with
  2491. | zero => simp [FiniteChoiceChain.returnTruncated]
  2492. | succ N ih =>
  2493. rw [P.toChain.returnTruncated_succ_eq_add_survival C N x, ih,
  2494. Finset.sum_range_succ]
  2495. omit [Nonempty F] in
  2496. theorem returnHitTotal_hasSum_one_finite (P : FiniteKernel F) (s : F)
  2497. (hreach : ∀ x, s ∈ P.reachable x) :
  2498. HasSum (fun n ↦ P.toChain.returnHitTotal {s} n s) 1 := by
  2499. rw [hasSum_iff_tendsto_nat_of_nonneg
  2500. (fun n ↦ P.toChain.returnHitTotal_nonneg {s} n s) 1]
  2501. have hzero : Filter.Tendsto (fun n ↦ P.toChain.returnSurvival {s} n s)
  2502. Filter.atTop (nhds 0) :=
  2503. (P.returnSurvival_summable s hreach).tendsto_atTop_zero
  2504. have hpartial (N : ℕ) :
  2505. ∑ n ∈ Finset.range (N + 1), P.toChain.returnHitTotal {s} n s =
  2506. 1 - P.toChain.returnSurvival {s} N s := by
  2507. induction N with
  2508. | zero => simp [FiniteChoiceChain.returnHitTotal,
  2509. FiniteChoiceChain.returnSurvival]
  2510. | succ N ih =>
  2511. rw [Finset.sum_range_succ, ih]
  2512. have htail := P.toChain.return_hit_add_survival {s} N s
  2513. linarith
  2514. have hlim : Filter.Tendsto
  2515. (fun N ↦ ∑ n ∈ Finset.range (N + 1),
  2516. P.toChain.returnHitTotal {s} n s) Filter.atTop (nhds 1) := by
  2517. rw [show (fun N ↦ ∑ n ∈ Finset.range (N + 1),
  2518. P.toChain.returnHitTotal {s} n s) =
  2519. fun N ↦ 1 - P.toChain.returnSurvival {s} N s from funext hpartial]
  2520. simpa using tendsto_const_nhds.sub hzero
  2521. exact (Filter.tendsto_add_atTop_iff_nat 1).mp (by
  2522. simpa [Nat.add_comm] using hlim)
  2523. end FiniteKernel
  2524. /-! ## A recurrent augmented state in the induced control-set chain -/
  2525. abbrev ControlState := {x : AugmentedState // x ∈ controlSet}
  2526. noncomputable instance : Nonempty ControlState :=
  2527. ⟨⟨originState, originState_mem_controlSet⟩⟩
  2528. /-- The stochastic kernel obtained by observing successive positive returns
  2529. to the finite control set. -/
  2530. noncomputable def inducedControlKernel : FiniteKernel ControlState where
  2531. probability x y := controlledChain.inducedProbability controlSet x.1 y.1
  2532. probability_nonneg x y :=
  2533. controlledChain.inducedProbability_nonneg controlSet x.1 y.1
  2534. probability_sum x := by
  2535. rw [show (∑ y : ControlState,
  2536. controlledChain.inducedProbability controlSet x.1 y.1) =
  2537. ∑ y ∈ controlSet,
  2538. controlledChain.inducedProbability controlSet x.1 y by
  2539. exact sum_finset_subtype controlSet
  2540. (fun y ↦ controlledChain.inducedProbability controlSet x.1 y)]
  2541. exact controlledChain.inducedProbability_row_sum controlSet controlledLyapunov
  2542. controlledLyapunov_nonneg controlled_drift x.1
  2543. abbrev RecurrentControlClass := inducedControlKernel.RecurrentClass
  2544. /-- The base point of the minimal closed communicating class. -/
  2545. noncomputable def recurrentControlState : RecurrentControlClass :=
  2546. inducedControlKernel.recurrentClassBase
  2547. noncomputable def recurrentAugmentedState : AugmentedState :=
  2548. recurrentControlState.1.1
  2549. theorem recurrentAugmentedState_mem : recurrentAugmentedState ∈ controlSet :=
  2550. recurrentControlState.1.2
  2551. theorem recurrentClass_reaches_base (x : RecurrentControlClass) :
  2552. recurrentControlState ∈ inducedControlKernel.recurrentKernel.reachable x :=
  2553. inducedControlKernel.recurrentKernel_reaches_base x
  2554. theorem recurrent_visit_return_total :
  2555. HasSum (fun n ↦
  2556. inducedControlKernel.recurrentKernel.toChain.returnHitTotal
  2557. {recurrentControlState} n recurrentControlState) 1 :=
  2558. inducedControlKernel.recurrentKernel.returnHitTotal_hasSum_one_finite
  2559. recurrentControlState recurrentClass_reaches_base
  2560. /-! ## Lifting recurrence through control-set excursion durations -/
  2561. noncomputable section
  2562. abbrev EmbeddedKernel := inducedControlKernel.recurrentKernel
  2563. abbrev EmbeddedChain := EmbeddedKernel.toChain
  2564. abbrev EmbeddedState := RecurrentControlClass
  2565. /-- Joint mass of an excursion duration and its next embedded state. -/
  2566. noncomputable def holdingMass (x y : EmbeddedState) (n : ℕ) : ℝ :=
  2567. controlledChain.returnAt controlSet y.1.1 n x.1.1
  2568. theorem holdingMass_nonneg (x y : EmbeddedState) (n : ℕ) :
  2569. 0 ≤ holdingMass x y n :=
  2570. controlledChain.returnAt_nonneg controlSet y.1.1 n x.1.1
  2571. theorem holdingMass_summable (x y : EmbeddedState) :
  2572. Summable fun n ↦ holdingMass x y n :=
  2573. controlled_returnAt_summable y.1.2 x.1.1
  2574. theorem tsum_holdingMass (x y : EmbeddedState) :
  2575. (∑' n, holdingMass x y n) = EmbeddedKernel.probability x y := by
  2576. rfl
  2577. theorem weighted_holdingMass_summable (x y : EmbeddedState) :
  2578. Summable fun n : ℕ ↦ (n : ℝ) * holdingMass x y n :=
  2579. controlled_weighted_returnAt_summable y.1.2 x.1.1
  2580. theorem sum_holdingMean_le (x : EmbeddedState) :
  2581. (∑ y, ∑' n : ℕ, (n : ℝ) * holdingMass x y n) ≤
  2582. excursionMeanBound := by
  2583. let g : ControlState → ℝ := fun y ↦
  2584. ∑' n : ℕ, (n : ℝ) *
  2585. controlledChain.returnAt controlSet y.1 n x.1.1
  2586. have hsub₁ : (∑ y, ∑' n : ℕ, (n : ℝ) * holdingMass x y n) ≤
  2587. ∑ y : ControlState, g y := by
  2588. rw [show (∑ y : EmbeddedState,
  2589. ∑' n : ℕ, (n : ℝ) * holdingMass x y n) =
  2590. ∑ y ∈ inducedControlKernel.reachable inducedControlKernel.recurrentState,
  2591. g y by
  2592. simpa [holdingMass, g] using
  2593. (sum_finset_subtype
  2594. (inducedControlKernel.reachable inducedControlKernel.recurrentState)
  2595. g)]
  2596. apply Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ _)
  2597. intro y _ _
  2598. exact tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)
  2599. (controlledChain.returnAt_nonneg controlSet y.1 n x.1.1)
  2600. have hall : (∑ y : ControlState, g y) =
  2601. ∑ y ∈ controlSet,
  2602. ∑' n : ℕ, (n : ℝ) *
  2603. controlledChain.returnAt controlSet y n x.1.1 := by
  2604. simpa [g] using (sum_finset_subtype controlSet fun y ↦
  2605. ∑' n : ℕ, (n : ℝ) *
  2606. controlledChain.returnAt controlSet y n x.1.1)
  2607. calc
  2608. _ ≤ ∑ y : ControlState, g y := hsub₁
  2609. _ ≤ ∑ y ∈ controlSet,
  2610. ∑' n : ℕ, (n : ℝ) *
  2611. controlledChain.returnAt controlSet y n x.1.1 := hall.le
  2612. _ = ∑' n : ℕ, (n : ℝ) *
  2613. controlledChain.returnHitTotal controlSet n x.1.1 :=
  2614. sum_weighted_returnAt x.1.1
  2615. _ ≤ excursionMeanBound := returnMean_le_excursionMeanBound x.1.2
  2616. /-- First hit of the base state after exactly `k` embedded visits and total
  2617. underlying block duration `n`. -/
  2618. noncomputable def holdingHit : ℕ → ℕ → EmbeddedState → ℝ
  2619. | 0, n, x => if n = 0 ∧ x = recurrentControlState then 1 else 0
  2620. | k + 1, n, x =>
  2621. if x = recurrentControlState then 0 else
  2622. ∑ y, convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n
  2623. /-- Positive return of the base state after exactly `k` embedded visits. -/
  2624. noncomputable def holdingReturn : ℕ → ℕ → ℝ
  2625. | 0, _ => 0
  2626. | k + 1, n =>
  2627. ∑ y, convolution (holdingMass recurrentControlState y)
  2628. (fun b ↦ holdingHit k b y) n
  2629. theorem holdingHit_nonneg (k n : ℕ) (x : EmbeddedState) :
  2630. 0 ≤ holdingHit k n x := by
  2631. induction k generalizing n x with
  2632. | zero =>
  2633. simp only [holdingHit]
  2634. split_ifs <;> norm_num
  2635. | succ k ih =>
  2636. simp only [holdingHit]
  2637. split_ifs
  2638. · norm_num
  2639. · exact Finset.sum_nonneg fun y _ ↦
  2640. convolution_nonneg (holdingMass_nonneg x y) (fun b ↦ ih b y) n
  2641. theorem holdingReturn_nonneg (k n : ℕ) : 0 ≤ holdingReturn k n := by
  2642. cases k with
  2643. | zero => simp [holdingReturn]
  2644. | succ k =>
  2645. simp only [holdingReturn]
  2646. exact Finset.sum_nonneg fun y _ ↦ convolution_nonneg
  2647. (holdingMass_nonneg recurrentControlState y)
  2648. (fun b ↦ holdingHit_nonneg k b y) n
  2649. theorem holdingHit_summable_and_mass (k : ℕ) (x : EmbeddedState) :
  2650. Summable (fun n ↦ holdingHit k n x) ∧
  2651. (∑' n, holdingHit k n x) =
  2652. EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x := by
  2653. induction k generalizing x with
  2654. | zero =>
  2655. by_cases hx : x = recurrentControlState
  2656. · subst x
  2657. have hs : HasSum (fun n : ℕ ↦ if n = 0 then (1 : ℝ) else 0) 1 :=
  2658. hasSum_ite_eq 0 1
  2659. constructor
  2660. · simpa [holdingHit] using hs.summable
  2661. · rw [show (fun n ↦ holdingHit 0 n recurrentControlState) =
  2662. fun n ↦ if n = 0 then (1 : ℝ) else 0 by
  2663. funext n
  2664. simp [holdingHit]]
  2665. rw [hs.tsum_eq]
  2666. simp [FiniteChoiceChain.hitAt]
  2667. · have hs : HasSum (fun _ : ℕ ↦ (0 : ℝ)) 0 := hasSum_zero
  2668. constructor
  2669. · simp [holdingHit, hx]
  2670. · rw [show (fun n ↦ holdingHit 0 n x) = fun _ ↦ (0 : ℝ) by
  2671. funext n
  2672. simp [holdingHit, hx]]
  2673. rw [tsum_zero]
  2674. simp [FiniteChoiceChain.hitAt, hx]
  2675. | succ k ih =>
  2676. by_cases hx : x = recurrentControlState
  2677. · subst x
  2678. constructor
  2679. · have hz : (fun n ↦ holdingHit (k + 1) n recurrentControlState) =
  2680. fun _ ↦ (0 : ℝ) := by
  2681. funext n
  2682. simp [holdingHit]
  2683. rw [hz]
  2684. exact summable_zero
  2685. · simp [holdingHit, FiniteChoiceChain.hitAt]
  2686. · have hconv (y : EmbeddedState) : Summable
  2687. (convolution (holdingMass x y) (fun b ↦ holdingHit k b y)) :=
  2688. convolution_summable (holdingMass_nonneg x y)
  2689. (fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y) (ih y).1
  2690. have hsum : Summable (fun n ↦ holdingHit (k + 1) n x) := by
  2691. simp only [holdingHit, hx, if_false]
  2692. exact summable_finset_sum Finset.univ
  2693. (fun y n ↦ convolution (holdingMass x y)
  2694. (fun b ↦ holdingHit k b y) n) (by simpa using hconv)
  2695. refine ⟨hsum, ?_⟩
  2696. simp only [holdingHit, hx, if_false]
  2697. rw [Summable.tsum_finsetSum (fun y _ ↦ hconv y)]
  2698. have hxmem : x ∉ ({recurrentControlState} : Finset EmbeddedState) := by
  2699. simpa using hx
  2700. simp only [FiniteChoiceChain.hitAt, hxmem, if_false, EmbeddedChain,
  2701. FiniteKernel.toChain]
  2702. apply Finset.sum_congr rfl
  2703. intro y _
  2704. rw [tsum_convolution (holdingMass_nonneg x y)
  2705. (fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y) (ih y).1,
  2706. tsum_holdingMass, (ih y).2]
  2707. rfl
  2708. theorem holdingReturn_summable_and_mass (k : ℕ) :
  2709. Summable (fun n ↦ holdingReturn k n) ∧
  2710. (∑' n, holdingReturn k n) =
  2711. EmbeddedChain.returnAt {recurrentControlState} recurrentControlState k
  2712. recurrentControlState := by
  2713. cases k with
  2714. | zero =>
  2715. constructor
  2716. · simp [holdingReturn]
  2717. · simp [holdingReturn, FiniteChoiceChain.returnAt]
  2718. | succ k =>
  2719. have hconv (y : EmbeddedState) : Summable
  2720. (convolution (holdingMass recurrentControlState y)
  2721. (fun b ↦ holdingHit k b y)) :=
  2722. convolution_summable (holdingMass_nonneg recurrentControlState y)
  2723. (fun b ↦ holdingHit_nonneg k b y)
  2724. (holdingMass_summable recurrentControlState y)
  2725. (holdingHit_summable_and_mass k y).1
  2726. have hsum : Summable (fun n ↦ holdingReturn (k + 1) n) := by
  2727. simp only [holdingReturn]
  2728. exact summable_finset_sum Finset.univ
  2729. (fun y n ↦ convolution (holdingMass recurrentControlState y)
  2730. (fun b ↦ holdingHit k b y) n) (by simpa using hconv)
  2731. refine ⟨hsum, ?_⟩
  2732. simp only [holdingReturn]
  2733. rw [Summable.tsum_finsetSum (fun y _ ↦ hconv y)]
  2734. simp only [FiniteChoiceChain.returnAt, EmbeddedChain, FiniteKernel.toChain]
  2735. apply Finset.sum_congr rfl
  2736. intro y _
  2737. rw [tsum_convolution (holdingMass_nonneg recurrentControlState y)
  2738. (fun b ↦ holdingHit_nonneg k b y)
  2739. (holdingMass_summable recurrentControlState y)
  2740. (holdingHit_summable_and_mass k y).1, tsum_holdingMass,
  2741. (holdingHit_summable_and_mass k y).2]
  2742. rfl
  2743. theorem holdingHit_weighted_summable (k : ℕ) (x : EmbeddedState) :
  2744. Summable fun n : ℕ ↦ (n : ℝ) * holdingHit k n x := by
  2745. induction k generalizing x with
  2746. | zero =>
  2747. have hz : (fun n : ℕ ↦ (n : ℝ) * holdingHit 0 n x) =
  2748. fun _ ↦ (0 : ℝ) := by
  2749. funext n
  2750. by_cases h : n = 0 ∧ x = recurrentControlState <;>
  2751. simp [holdingHit, h]
  2752. rw [hz]
  2753. exact summable_zero
  2754. | succ k ih =>
  2755. by_cases hx : x = recurrentControlState
  2756. · subst x
  2757. have hz : (fun n : ℕ ↦ (n : ℝ) *
  2758. holdingHit (k + 1) n recurrentControlState) = fun _ ↦ (0 : ℝ) := by
  2759. funext n
  2760. simp [holdingHit]
  2761. rw [hz]
  2762. exact summable_zero
  2763. · have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *
  2764. convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n :=
  2765. weighted_convolution_summable (holdingMass_nonneg x y)
  2766. (fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)
  2767. (holdingHit_summable_and_mass k y).1
  2768. (weighted_holdingMass_summable x y) (ih y)
  2769. have hfinite := summable_finset_sum Finset.univ
  2770. (fun y n ↦ (n : ℝ) * convolution (holdingMass x y)
  2771. (fun b ↦ holdingHit k b y) n) (by simpa using hconv)
  2772. simpa only [holdingHit, hx, if_false, Finset.mul_sum] using hfinite
  2773. theorem holdingReturn_weighted_summable (k : ℕ) :
  2774. Summable fun n : ℕ ↦ (n : ℝ) * holdingReturn k n := by
  2775. cases k with
  2776. | zero =>
  2777. simp [holdingReturn]
  2778. | succ k =>
  2779. have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *
  2780. convolution (holdingMass recurrentControlState y)
  2781. (fun b ↦ holdingHit k b y) n :=
  2782. weighted_convolution_summable
  2783. (holdingMass_nonneg recurrentControlState y)
  2784. (fun b ↦ holdingHit_nonneg k b y)
  2785. (holdingMass_summable recurrentControlState y)
  2786. (holdingHit_summable_and_mass k y).1
  2787. (weighted_holdingMass_summable recurrentControlState y)
  2788. (holdingHit_weighted_summable k y)
  2789. have hfinite := summable_finset_sum Finset.univ
  2790. (fun y n ↦ (n : ℝ) * convolution
  2791. (holdingMass recurrentControlState y)
  2792. (fun b ↦ holdingHit k b y) n) (by simpa using hconv)
  2793. simpa only [holdingReturn, Finset.mul_sum] using hfinite
  2794. theorem tsum_weighted_holdingHit_succ {k : ℕ} {x : EmbeddedState}
  2795. (hx : x ≠ recurrentControlState) :
  2796. (∑' n : ℕ, (n : ℝ) * holdingHit (k + 1) n x) =
  2797. ∑ y : EmbeddedState,
  2798. ((∑' n : ℕ, (n : ℝ) * holdingMass x y n) *
  2799. EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k y +
  2800. EmbeddedKernel.probability x y *
  2801. ∑' n : ℕ, (n : ℝ) * holdingHit k n y) := by
  2802. have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *
  2803. convolution (holdingMass x y) (fun b ↦ holdingHit k b y) n :=
  2804. weighted_convolution_summable (holdingMass_nonneg x y)
  2805. (fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)
  2806. (holdingHit_summable_and_mass k y).1 (weighted_holdingMass_summable x y)
  2807. (holdingHit_weighted_summable k y)
  2808. simp only [holdingHit, hx, if_false, Finset.mul_sum]
  2809. rw [Summable.tsum_finsetSum (by simpa using hconv)]
  2810. apply Finset.sum_congr rfl
  2811. intro y _
  2812. rw [tsum_weighted_convolution (holdingMass_nonneg x y)
  2813. (fun b ↦ holdingHit_nonneg k b y) (holdingMass_summable x y)
  2814. (holdingHit_summable_and_mass k y).1 (weighted_holdingMass_summable x y)
  2815. (holdingHit_weighted_summable k y), (holdingHit_summable_and_mass k y).2,
  2816. tsum_holdingMass]
  2817. theorem tsum_weighted_holdingReturn_succ (k : ℕ) :
  2818. (∑' n : ℕ, (n : ℝ) * holdingReturn (k + 1) n) =
  2819. ∑ y : EmbeddedState,
  2820. ((∑' n : ℕ, (n : ℝ) * holdingMass recurrentControlState y n) *
  2821. EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k y +
  2822. EmbeddedKernel.probability recurrentControlState y *
  2823. ∑' n : ℕ, (n : ℝ) * holdingHit k n y) := by
  2824. have hconv (y : EmbeddedState) : Summable fun n : ℕ ↦ (n : ℝ) *
  2825. convolution (holdingMass recurrentControlState y)
  2826. (fun b ↦ holdingHit k b y) n :=
  2827. weighted_convolution_summable
  2828. (holdingMass_nonneg recurrentControlState y)
  2829. (fun b ↦ holdingHit_nonneg k b y)
  2830. (holdingMass_summable recurrentControlState y)
  2831. (holdingHit_summable_and_mass k y).1
  2832. (weighted_holdingMass_summable recurrentControlState y)
  2833. (holdingHit_weighted_summable k y)
  2834. simp only [holdingReturn, Finset.mul_sum]
  2835. rw [Summable.tsum_finsetSum (by simpa using hconv)]
  2836. apply Finset.sum_congr rfl
  2837. intro y _
  2838. rw [tsum_weighted_convolution (holdingMass_nonneg recurrentControlState y)
  2839. (fun b ↦ holdingHit_nonneg k b y)
  2840. (holdingMass_summable recurrentControlState y)
  2841. (holdingHit_summable_and_mass k y).1
  2842. (weighted_holdingMass_summable recurrentControlState y)
  2843. (holdingHit_weighted_summable k y), (holdingHit_summable_and_mass k y).2,
  2844. tsum_holdingMass]
  2845. noncomputable def holdingHitMean (k : ℕ) (x : EmbeddedState) : ℝ :=
  2846. ∑' n : ℕ, (n : ℝ) * holdingHit k n x
  2847. noncomputable def holdingReturnMean (k : ℕ) : ℝ :=
  2848. ∑' n : ℕ, (n : ℝ) * holdingReturn k n
  2849. @[simp] theorem holdingHitMean_zero (x : EmbeddedState) : holdingHitMean 0 x = 0 := by
  2850. have hz : (fun n : ℕ ↦ (n : ℝ) * holdingHit 0 n x) = fun _ ↦ (0 : ℝ) := by
  2851. funext n
  2852. by_cases h : n = 0 ∧ x = recurrentControlState <;> simp [holdingHit, h]
  2853. simp [holdingHitMean, hz]
  2854. @[simp] theorem holdingHitMean_base (k : ℕ) :
  2855. holdingHitMean k recurrentControlState = 0 := by
  2856. cases k with
  2857. | zero => simp
  2858. | succ k =>
  2859. have hz : (fun n : ℕ ↦ (n : ℝ) *
  2860. holdingHit (k + 1) n recurrentControlState) = fun _ ↦ (0 : ℝ) := by
  2861. funext n
  2862. simp [holdingHit]
  2863. simp [holdingHitMean, hz]
  2864. @[simp] theorem holdingReturnMean_zero : holdingReturnMean 0 = 0 := by
  2865. simp [holdingReturnMean, holdingReturn]
  2866. theorem embedded_hitAt_partial_le_one (N : ℕ) (x : EmbeddedState) :
  2867. (∑ k ∈ Finset.range (N + 1),
  2868. EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x) ≤ 1 := by
  2869. have heq (k : ℕ) :
  2870. EmbeddedChain.hitAt {recurrentControlState} recurrentControlState k x =
  2871. EmbeddedChain.hitTotal {recurrentControlState} k x := by
  2872. rw [← EmbeddedChain.sum_hitAt_eq_hitTotal
  2873. {recurrentControlState} k x]
  2874. simp
  2875. simp_rw [heq]
  2876. exact EmbeddedChain.sum_hitTotal_le_one {recurrentControlState} N x
  2877. theorem holdingHitMean_partial_le (N : ℕ) (x : EmbeddedState) :
  2878. (∑ k ∈ Finset.range (N + 1), holdingHitMean k x) ≤
  2879. excursionMeanBound *
  2880. EmbeddedChain.truncatedHit {recurrentControlState} N x := by
  2881. induction N generalizing x with
  2882. | zero => simp [FiniteChoiceChain.truncatedHit]
  2883. | succ N ih =>
  2884. by_cases hx : x = recurrentControlState
  2885. · subst x
  2886. simp [FiniteChoiceChain.truncatedHit]
  2887. · have hxmem : x ∉ ({recurrentControlState} : Finset EmbeddedState) := by
  2888. simpa using hx
  2889. rw [Finset.sum_range_succ']
  2890. simp only [holdingHitMean_zero, add_zero]
  2891. have hsplit :
  2892. (∑ k ∈ Finset.range (N + 1), holdingHitMean (k + 1) x) =
  2893. ∑ y : EmbeddedState,
  2894. ((∑' n : ℕ, (n : ℝ) * holdingMass x y n) *
  2895. (∑ k ∈ Finset.range (N + 1),
  2896. EmbeddedChain.hitAt {recurrentControlState}
  2897. recurrentControlState k y) +
  2898. EmbeddedKernel.probability x y *
  2899. (∑ k ∈ Finset.range (N + 1), holdingHitMean k y)) := by
  2900. simp_rw [holdingHitMean, tsum_weighted_holdingHit_succ hx]
  2901. rw [Finset.sum_comm]
  2902. apply Finset.sum_congr rfl
  2903. intro y _
  2904. rw [Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum]
  2905. rw [hsplit]
  2906. calc
  2907. _ ≤ ∑ y : EmbeddedState,
  2908. ((∑' n : ℕ, (n : ℝ) * holdingMass x y n) +
  2909. EmbeddedKernel.probability x y *
  2910. (excursionMeanBound *
  2911. EmbeddedChain.truncatedHit {recurrentControlState} N y)) := by
  2912. apply Finset.sum_le_sum
  2913. intro y _
  2914. apply add_le_add
  2915. · exact mul_le_of_le_one_right
  2916. (tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)
  2917. (holdingMass_nonneg x y n))
  2918. (embedded_hitAt_partial_le_one N y)
  2919. · exact mul_le_mul_of_nonneg_left (ih y)
  2920. (EmbeddedKernel.probability_nonneg x y)
  2921. _ = (∑ y : EmbeddedState,
  2922. ∑' n : ℕ, (n : ℝ) * holdingMass x y n) +
  2923. excursionMeanBound *
  2924. (∑ y : EmbeddedState, EmbeddedKernel.probability x y *
  2925. EmbeddedChain.truncatedHit {recurrentControlState} N y) := by
  2926. rw [Finset.sum_add_distrib]
  2927. congr 1
  2928. rw [Finset.mul_sum]
  2929. apply Finset.sum_congr rfl
  2930. intro y _
  2931. ring
  2932. _ ≤ excursionMeanBound + excursionMeanBound *
  2933. (∑ y : EmbeddedState, EmbeddedKernel.probability x y *
  2934. EmbeddedChain.truncatedHit {recurrentControlState} N y) := by
  2935. exact add_le_add_left (sum_holdingMean_le x) _
  2936. _ = excursionMeanBound *
  2937. EmbeddedChain.truncatedHit {recurrentControlState} (N + 1) x := by
  2938. rw [EmbeddedChain.truncatedHit_succ_of_not_mem
  2939. {recurrentControlState} N hxmem]
  2940. simp only [EmbeddedChain, FiniteKernel.toChain]
  2941. ring
  2942. theorem holdingReturnMean_partial_le (N : ℕ) :
  2943. (∑ k ∈ Finset.range (N + 1), holdingReturnMean k) ≤
  2944. excursionMeanBound * EmbeddedChain.returnTruncated
  2945. {recurrentControlState} N recurrentControlState := by
  2946. cases N with
  2947. | zero => simp [FiniteChoiceChain.returnTruncated]
  2948. | succ N =>
  2949. rw [Finset.sum_range_succ']
  2950. simp only [holdingReturnMean_zero, add_zero]
  2951. have hsplit :
  2952. (∑ k ∈ Finset.range (N + 1), holdingReturnMean (k + 1)) =
  2953. ∑ y : EmbeddedState,
  2954. ((∑' n : ℕ, (n : ℝ) *
  2955. holdingMass recurrentControlState y n) *
  2956. (∑ k ∈ Finset.range (N + 1),
  2957. EmbeddedChain.hitAt {recurrentControlState}
  2958. recurrentControlState k y) +
  2959. EmbeddedKernel.probability recurrentControlState y *
  2960. (∑ k ∈ Finset.range (N + 1), holdingHitMean k y)) := by
  2961. simp_rw [holdingReturnMean, holdingHitMean,
  2962. tsum_weighted_holdingReturn_succ]
  2963. rw [Finset.sum_comm]
  2964. apply Finset.sum_congr rfl
  2965. intro y _
  2966. rw [Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum]
  2967. rw [hsplit]
  2968. calc
  2969. _ ≤ ∑ y : EmbeddedState,
  2970. ((∑' n : ℕ, (n : ℝ) *
  2971. holdingMass recurrentControlState y n) +
  2972. EmbeddedKernel.probability recurrentControlState y *
  2973. (excursionMeanBound *
  2974. EmbeddedChain.truncatedHit {recurrentControlState} N y)) := by
  2975. apply Finset.sum_le_sum
  2976. intro y _
  2977. apply add_le_add
  2978. · exact mul_le_of_le_one_right
  2979. (tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)
  2980. (holdingMass_nonneg recurrentControlState y n))
  2981. (embedded_hitAt_partial_le_one N y)
  2982. · exact mul_le_mul_of_nonneg_left (holdingHitMean_partial_le N y)
  2983. (EmbeddedKernel.probability_nonneg recurrentControlState y)
  2984. _ = (∑ y : EmbeddedState,
  2985. ∑' n : ℕ, (n : ℝ) * holdingMass recurrentControlState y n) +
  2986. excursionMeanBound *
  2987. (∑ y : EmbeddedState,
  2988. EmbeddedKernel.probability recurrentControlState y *
  2989. EmbeddedChain.truncatedHit {recurrentControlState} N y) := by
  2990. rw [Finset.sum_add_distrib]
  2991. congr 1
  2992. rw [Finset.mul_sum]
  2993. apply Finset.sum_congr rfl
  2994. intro y _
  2995. ring
  2996. _ ≤ excursionMeanBound + excursionMeanBound *
  2997. (∑ y : EmbeddedState,
  2998. EmbeddedKernel.probability recurrentControlState y *
  2999. EmbeddedChain.truncatedHit {recurrentControlState} N y) := by
  3000. exact add_le_add_left (sum_holdingMean_le recurrentControlState) _
  3001. _ = excursionMeanBound * EmbeddedChain.returnTruncated
  3002. {recurrentControlState} (N + 1) recurrentControlState := by
  3003. simp only [FiniteChoiceChain.returnTruncated, EmbeddedChain,
  3004. FiniteKernel.toChain]
  3005. ring
  3006. noncomputable def recurrentReturnDurationBound : ℝ :=
  3007. excursionMeanBound *
  3008. ∑' k, EmbeddedChain.returnSurvival
  3009. {recurrentControlState} k recurrentControlState
  3010. theorem holdingReturnMean_summable : Summable holdingReturnMean := by
  3011. apply summable_of_sum_range_le (c := recurrentReturnDurationBound)
  3012. · intro k
  3013. exact tsum_nonneg fun n ↦ mul_nonneg (Nat.cast_nonneg n)
  3014. (holdingReturn_nonneg k n)
  3015. · intro N
  3016. cases N with
  3017. | zero =>
  3018. simp only [Finset.range_zero, Finset.sum_empty]
  3019. exact mul_nonneg excursionMeanBound_pos.le
  3020. (tsum_nonneg fun k ↦ EmbeddedChain.returnSurvival_nonneg
  3021. {recurrentControlState} k recurrentControlState)
  3022. | succ N =>
  3023. refine (holdingReturnMean_partial_le N).trans ?_
  3024. apply mul_le_mul_of_nonneg_left _ excursionMeanBound_pos.le
  3025. rw [EmbeddedKernel.returnTruncated_eq_sum_survival
  3026. {recurrentControlState} N recurrentControlState]
  3027. exact (EmbeddedKernel.returnSurvival_summable recurrentControlState
  3028. recurrentClass_reaches_base).sum_le_tsum (Finset.range N)
  3029. (fun k _ ↦ EmbeddedChain.returnSurvival_nonneg
  3030. {recurrentControlState} k recurrentControlState)
  3031. theorem embedded_returnAt_eq_returnHitTotal (k : ℕ) :
  3032. EmbeddedChain.returnAt {recurrentControlState} recurrentControlState k
  3033. recurrentControlState =
  3034. EmbeddedChain.returnHitTotal {recurrentControlState} k
  3035. recurrentControlState := by
  3036. rw [← EmbeddedChain.sum_returnAt_eq_returnHitTotal
  3037. {recurrentControlState} k recurrentControlState]
  3038. simp
  3039. end
  3040. /-! ## Injectivity of the interval coding -/
  3041. def inverseT (a : ℕ) (x : ℚ) : ℚ := (x + 1) / a
  3042. def inverseWord : List ℕ → ℚ → ℚ
  3043. | [], x => x
  3044. | a :: w, x => inverseT a (inverseWord w x)
  3045. def intervalMidpoint (i : Fin 19) : ℚ :=
  3046. (endpoint (leftIndex i) + endpoint (rightIndex i)) / 2
  3047. theorem intervalMidpoint_mem (i : Fin 19) : intervalMidpoint i ∈ K i := by
  3048. have h := width_pos i
  3049. simp only [width] at h
  3050. constructor <;> simp only [intervalMidpoint] <;> linarith
  3051. /-- Finite certificate saying that every allowed destination lies inside the
  3052. image interval of its source. -/
  3053. theorem allowed_endpoint_bounds (α : Assignment) (i j : Fin 19)
  3054. (h : allowedNext α i j) :
  3055. T_rat (prescribed α i) (endpoint (leftIndex i)) ≤ endpoint (leftIndex j) ∧
  3056. endpoint (rightIndex j) ≤
  3057. T_rat (prescribed α i) (endpoint (rightIndex i)) := by
  3058. obtain ⟨tr, htr, hrow⟩ := List.any_eq_true.mp h
  3059. have hrow' : tr.source = i ∧ tr.multiplier = prescribed α i ∧
  3060. tr.first ≤ j ∧ j ≤ tr.last := of_decide_eq_true hrow
  3061. rcases hrow' with ⟨hsource, hmultiplier, hfirst, hlast⟩
  3062. have hexact := transitions_exact tr htr
  3063. have hleftIndex : leftIndex tr.first ≤ leftIndex j := by
  3064. simpa [leftIndex] using hfirst
  3065. have hrightIndex : rightIndex j ≤ rightIndex tr.last := by
  3066. simpa [rightIndex] using hlast
  3067. constructor
  3068. · calc
  3069. T_rat (prescribed α i) (endpoint (leftIndex i)) =
  3070. T_rat tr.multiplier (endpoint (leftIndex tr.source)) := by
  3071. rw [hsource, hmultiplier]
  3072. _ = endpoint (leftIndex tr.first) := hexact.2.2.1
  3073. _ ≤ endpoint (leftIndex j) := endpoint_strictMono.monotone hleftIndex
  3074. · calc
  3075. endpoint (rightIndex j) ≤ endpoint (rightIndex tr.last) :=
  3076. endpoint_strictMono.monotone hrightIndex
  3077. _ = T_rat tr.multiplier (endpoint (rightIndex tr.source)) :=
  3078. hexact.2.2.2.symm
  3079. _ = T_rat (prescribed α i) (endpoint (rightIndex i)) := by
  3080. rw [hsource, hmultiplier]
  3081. theorem inverseT_mem_of_allowed {α : Assignment} {i j : Fin 19}
  3082. (h : allowedNext α i j) {x : ℚ} (hx : x ∈ K j) :
  3083. inverseT (prescribed α i) x ∈ K i := by
  3084. have ha : (0 : ℚ) < prescribed α i := by
  3085. exact_mod_cast (show 0 < prescribed α i by
  3086. exact (prescribed_ge_two α i).trans' (by omega))
  3087. have hb := allowed_endpoint_bounds α i j h
  3088. simp only [K, Set.mem_Ico] at hx ⊢
  3089. simp only [T_rat] at hb
  3090. constructor
  3091. · apply (le_div_iff₀ ha).2
  3092. linarith [hb.1, hx.1]
  3093. · apply (div_lt_iff₀ ha).2
  3094. linarith [hb.2, hx.2]
  3095. theorem mem_K_unique {i j : Fin 19} {x : ℚ}
  3096. (hi : x ∈ K i) (hj : x ∈ K j) : i = j := by
  3097. apply Fin.ext
  3098. by_contra hne
  3099. rcases lt_or_gt_of_ne hne with hij | hji
  3100. · have hindex : rightIndex i ≤ leftIndex j := by
  3101. apply Fin.mk_le_mk.mpr
  3102. omega
  3103. have hend := endpoint_strictMono.monotone hindex
  3104. simp only [K, Set.mem_Ico] at hi hj
  3105. linarith
  3106. · have hindex : rightIndex j ≤ leftIndex i := by
  3107. apply Fin.mk_le_mk.mpr
  3108. omega
  3109. have hend := endpoint_strictMono.monotone hindex
  3110. simp only [K, Set.mem_Ico] at hi hj
  3111. linarith
  3112. namespace IntervalPath
  3113. def destinations {i j : Fin 19} : IntervalPath i j → List (Fin 19)
  3114. | .nil _ => []
  3115. | @IntervalPath.cons _ j _ _ _ tail => j :: tail.destinations
  3116. def append {i j k : Fin 19} : IntervalPath i j → IntervalPath j k →
  3117. IntervalPath i k
  3118. | .nil _, q => q
  3119. | .cons α h tail, q => .cons α h (tail.append q)
  3120. @[simp] theorem word_append {i j k : Fin 19} (p : IntervalPath i j)
  3121. (q : IntervalPath j k) : (p.append q).word = p.word ++ q.word := by
  3122. induction p with
  3123. | nil => rfl
  3124. | cons α h tail ih => simp [append, ih]
  3125. @[simp] theorem destinations_append {i j k : Fin 19} (p : IntervalPath i j)
  3126. (q : IntervalPath j k) :
  3127. (p.append q).destinations = p.destinations ++ q.destinations := by
  3128. induction p with
  3129. | nil => rfl
  3130. | cons α h tail ih => simp [append, destinations, ih]
  3131. @[simp] theorem mass_append {i j k : Fin 19} (p : IntervalPath i j)
  3132. (q : IntervalPath j k) : (p.append q).mass = p.mass * q.mass := by
  3133. induction p with
  3134. | nil => simp [append, mass]
  3135. | cons α h tail ih => simp [append, ih, mul_assoc]
  3136. theorem inverseWord_mem_source {i j : Fin 19} (p : IntervalPath i j)
  3137. {x : ℚ} (hx : x ∈ K j) : inverseWord p.word x ∈ K i := by
  3138. induction p with
  3139. | nil => simpa [word, inverseWord] using hx
  3140. | @cons i j k α h tail ih =>
  3141. simp only [word_cons, inverseWord]
  3142. exact inverseT_mem_of_allowed h (ih hx)
  3143. /-- For fixed endpoints, the multiplier word uniquely determines the entire
  3144. sequence of interval destinations. Assignments themselves are deliberately
  3145. ignored here. -/
  3146. theorem destinations_eq_of_word_eq {i k : Fin 19} (p q : IntervalPath i k)
  3147. (hword : p.word = q.word) : p.destinations = q.destinations := by
  3148. induction p with
  3149. | nil i =>
  3150. cases q with
  3151. | nil => rfl
  3152. | cons α h tail => simp [word] at hword
  3153. | @cons i j k α h tail ih =>
  3154. cases q with
  3155. | nil => simp [word] at hword
  3156. | @cons _ j' _ β h' tail' =>
  3157. have htail : tail.word = tail'.word := (List.cons.inj hword).2
  3158. let z := inverseWord tail.word (intervalMidpoint k)
  3159. have hzj : z ∈ K j := by
  3160. exact tail.inverseWord_mem_source (intervalMidpoint_mem k)
  3161. have hzj' : z ∈ K j' := by
  3162. dsimp only [z]
  3163. rw [htail]
  3164. exact tail'.inverseWord_mem_source (intervalMidpoint_mem k)
  3165. have hj : j = j' := mem_K_unique hzj hzj'
  3166. subst j'
  3167. simp only [destinations]
  3168. rw [ih tail' htail]
  3169. theorem destinations_eq_of_word_eq_of_endpoint_eq {i j k : Fin 19}
  3170. (p : IntervalPath i j) (q : IntervalPath i k) (hend : j = k)
  3171. (hword : p.word = q.word) : p.destinations = q.destinations := by
  3172. cases hend
  3173. exact destinations_eq_of_word_eq p q hword
  3174. end IntervalPath
  3175. /-! ### Controlled blocks as typed interval paths -/
  3176. theorem FiniteChoiceChain.pathWeight_nonneg {S Ω : Type*} [Fintype Ω]
  3177. (K : FiniteChoiceChain S Ω) (n : ℕ) (x : S)
  3178. (w : Fin n → Ω) : 0 ≤ K.pathWeight n x w := by
  3179. induction n generalizing x with
  3180. | zero => simp [FiniteChoiceChain.pathWeight]
  3181. | succ n ih =>
  3182. rw [show w = Fin.cons (w 0) (Fin.tail w) from (Fin.cons_self_tail w).symm,
  3183. K.pathWeight_cons]
  3184. exact mul_nonneg (K.probability_nonneg x (w 0))
  3185. (ih (K.next x (w 0)) (Fin.tail w))
  3186. noncomputable def blockIntervalPath (α : Assignment) :
  3187. (n : ℕ) → (i : Fin 19) → (w : Fin n → Fin 19) →
  3188. 0 < blockWeight α n i w → IntervalPath i (blockEndpoint n i w)
  3189. | 0, i, _, _ => .nil i
  3190. | n + 1, i, w, hpos => by
  3191. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3192. (Fin.cons_self_tail w).symm, blockWeight_cons] at hpos
  3193. have hp0 := probability_nonneg α i (w 0)
  3194. have htail0 := blockWeight_nonneg α n (w 0) (Fin.tail w)
  3195. have hp : 0 < probability α i (w 0) := by nlinarith
  3196. have htail : 0 < blockWeight α n (w 0) (Fin.tail w) := by nlinarith
  3197. have hallowed : allowedNext α i (w 0) := by
  3198. by_contra hnot
  3199. simp [probability, hnot] at hp
  3200. exact .cons α hallowed (blockIntervalPath α n (w 0) (Fin.tail w) htail)
  3201. @[simp] theorem blockIntervalPath_word (α : Assignment) (n : ℕ)
  3202. (i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :
  3203. (blockIntervalPath α n i w hpos).word = blockWord α n i w := by
  3204. induction n generalizing i with
  3205. | zero => rfl
  3206. | succ n ih =>
  3207. simp only [blockIntervalPath, IntervalPath.word, blockWord]
  3208. congr 1
  3209. exact ih (w 0) (Fin.tail w) _
  3210. @[simp] theorem blockIntervalPath_destinations (α : Assignment) (n : ℕ)
  3211. (i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :
  3212. (blockIntervalPath α n i w hpos).destinations = List.ofFn w := by
  3213. induction n generalizing i with
  3214. | zero => rfl
  3215. | succ n ih =>
  3216. simp only [blockIntervalPath, IntervalPath.destinations, List.ofFn_succ]
  3217. congr 1
  3218. exact ih (w 0) (Fin.tail w) _
  3219. theorem inverseT_injective {a : ℕ} (ha : 0 < a) :
  3220. Function.Injective (inverseT a) := by
  3221. intro x y h
  3222. simp only [inverseT] at h
  3223. have haQ : (a : ℚ) ≠ 0 := by exact_mod_cast ha.ne'
  3224. exact add_right_cancel ((div_left_inj' haQ).mp h)
  3225. /-- Inside a block the assignment is fixed. Consequently, equality of two
  3226. pulled-back points forces equality of every interval destination in the
  3227. block, even when the terminal points are initially different. -/
  3228. theorem blockChoice_eq_of_inverse_eq (α : Assignment) (n : ℕ) (i : Fin 19)
  3229. {u v : Fin n → Fin 19} (hu : 0 < blockWeight α n i u)
  3230. (hv : 0 < blockWeight α n i v) {xu xv : ℚ}
  3231. (hxu : xu ∈ K (blockEndpoint n i u))
  3232. (hxv : xv ∈ K (blockEndpoint n i v))
  3233. (hinv : inverseWord (blockWord α n i u) xu =
  3234. inverseWord (blockWord α n i v) xv) :
  3235. u = v ∧ xu = xv := by
  3236. induction n generalizing i with
  3237. | zero =>
  3238. constructor
  3239. · exact Subsingleton.elim u v
  3240. · simpa [blockWord, inverseWord] using hinv
  3241. | succ n ih =>
  3242. rw [show u = Fin.cons (u 0) (Fin.tail u) from
  3243. (Fin.cons_self_tail u).symm, blockWeight_cons] at hu
  3244. rw [show v = Fin.cons (v 0) (Fin.tail v) from
  3245. (Fin.cons_self_tail v).symm, blockWeight_cons] at hv
  3246. have hpu0 := probability_nonneg α i (u 0)
  3247. have hpv0 := probability_nonneg α i (v 0)
  3248. have htu0 := blockWeight_nonneg α n (u 0) (Fin.tail u)
  3249. have htv0 := blockWeight_nonneg α n (v 0) (Fin.tail v)
  3250. have htu : 0 < blockWeight α n (u 0) (Fin.tail u) := by nlinarith
  3251. have htv : 0 < blockWeight α n (v 0) (Fin.tail v) := by nlinarith
  3252. change xu ∈ K (blockEndpoint n (u 0) (Fin.tail u)) at hxu
  3253. change xv ∈ K (blockEndpoint n (v 0) (Fin.tail v)) at hxv
  3254. have huInner : inverseWord (blockWord α n (u 0) (Fin.tail u)) xu ∈
  3255. K (u 0) := by
  3256. simpa only [blockIntervalPath_word] using
  3257. (blockIntervalPath α n (u 0) (Fin.tail u) htu).inverseWord_mem_source hxu
  3258. have hvInner : inverseWord (blockWord α n (v 0) (Fin.tail v)) xv ∈
  3259. K (v 0) := by
  3260. simpa only [blockIntervalPath_word] using
  3261. (blockIntervalPath α n (v 0) (Fin.tail v) htv).inverseWord_mem_source hxv
  3262. have hinner : inverseWord (blockWord α n (u 0) (Fin.tail u)) xu =
  3263. inverseWord (blockWord α n (v 0) (Fin.tail v)) xv := by
  3264. apply inverseT_injective (show 0 < prescribed α i by
  3265. exact (prescribed_ge_two α i).trans' (by omega))
  3266. simpa [blockWord, inverseWord] using hinv
  3267. have hfirst : u 0 = v 0 := mem_K_unique huInner (hinner ▸ hvInner)
  3268. rw [← hfirst] at htv hxv hinner
  3269. have htail := ih (u 0) htu htv hxu hxv hinner
  3270. constructor
  3271. · rw [← Fin.cons_self_tail u, ← Fin.cons_self_tail v]
  3272. exact Fin.cons_inj.mpr ⟨hfirst, htail.1⟩
  3273. · exact htail.2
  3274. noncomputable def controlledPathWord :
  3275. (n : ℕ) → AugmentedState → (Fin n → BlockChoice) → List ℕ
  3276. | 0, _, _ => []
  3277. | n + 1, x, w =>
  3278. blockWord (feedback x.2) controlBlockLength x.1 (w 0) ++
  3279. controlledPathWord n (controlledNext x (w 0)) (Fin.tail w)
  3280. noncomputable def flattenBlockChoices :
  3281. (n : ℕ) → (Fin n → BlockChoice) → List (Fin 19)
  3282. | 0, _ => []
  3283. | n + 1, w => List.ofFn (w 0) ++ flattenBlockChoices n (Fin.tail w)
  3284. theorem flattenBlockChoices_injective (n : ℕ) :
  3285. Function.Injective (flattenBlockChoices n) := by
  3286. induction n with
  3287. | zero =>
  3288. intro u v _
  3289. exact Subsingleton.elim u v
  3290. | succ n ih =>
  3291. intro u v huv
  3292. simp only [flattenBlockChoices] at huv
  3293. have hhead : List.ofFn (u 0) = List.ofFn (v 0) :=
  3294. List.append_inj_left huv (by simp)
  3295. have hzero : u 0 = v 0 := List.ofFn_injective hhead
  3296. have htailList : flattenBlockChoices n (Fin.tail u) =
  3297. flattenBlockChoices n (Fin.tail v) := by
  3298. rw [hzero] at huv
  3299. exact List.append_cancel_left huv
  3300. have htail : Fin.tail u = Fin.tail v := ih htailList
  3301. rw [← Fin.cons_self_tail u, ← Fin.cons_self_tail v]
  3302. exact Fin.cons_inj.mpr ⟨hzero, htail⟩
  3303. noncomputable def controlledIntervalPath :
  3304. (n : ℕ) → (x : AugmentedState) → (w : Fin n → BlockChoice) →
  3305. 0 < controlledChain.pathWeight n x w →
  3306. IntervalPath x.1 (controlledChain.pathEndpoint n x w).1
  3307. | 0, x, _, _ => .nil x.1
  3308. | n + 1, x, w, hpos => by
  3309. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3310. (Fin.cons_self_tail w).symm, controlledChain.pathWeight_cons] at hpos
  3311. change 0 < controlledChain.probability x (w 0) *
  3312. controlledChain.pathWeight n (controlledNext x (w 0)) (Fin.tail w) at hpos
  3313. have hp0 := controlledChain.probability_nonneg x (w 0)
  3314. have ht0 := controlledChain.pathWeight_nonneg n
  3315. (controlledNext x (w 0)) (Fin.tail w)
  3316. have hp : 0 < controlledChain.probability x (w 0) := by nlinarith
  3317. have ht : 0 < controlledChain.pathWeight n
  3318. (controlledNext x (w 0)) (Fin.tail w) := by nlinarith
  3319. have hpq : 0 < blockWeight (feedback x.2) controlBlockLength x.1 (w 0) := by
  3320. change 0 < (blockWeight (feedback x.2) controlBlockLength x.1 (w 0) : ℝ)
  3321. at hp
  3322. exact_mod_cast hp
  3323. exact (blockIntervalPath (feedback x.2) controlBlockLength x.1 (w 0) hpq).append
  3324. (controlledIntervalPath n (controlledNext x (w 0)) (Fin.tail w) ht)
  3325. set_option backward.isDefEq.respectTransparency false in
  3326. @[simp] theorem controlledIntervalPath_word (n : ℕ) (x : AugmentedState)
  3327. (w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w) :
  3328. (controlledIntervalPath n x w hpos).word = controlledPathWord n x w := by
  3329. induction n generalizing x with
  3330. | zero => rfl
  3331. | succ n ih =>
  3332. rw [controlledIntervalPath, controlledPathWord]
  3333. dsimp only [id]
  3334. rw [IntervalPath.word_append, blockIntervalPath_word]
  3335. exact congrArg
  3336. (fun z ↦ blockWord (feedback x.2) controlBlockLength x.1 (w 0) ++ z)
  3337. (ih (controlledNext x (w 0)) (Fin.tail w) _)
  3338. set_option backward.isDefEq.respectTransparency false in
  3339. @[simp] theorem controlledIntervalPath_destinations (n : ℕ)
  3340. (x : AugmentedState) (w : Fin n → BlockChoice)
  3341. (hpos : 0 < controlledChain.pathWeight n x w) :
  3342. (controlledIntervalPath n x w hpos).destinations = flattenBlockChoices n w := by
  3343. induction n generalizing x with
  3344. | zero => rfl
  3345. | succ n ih =>
  3346. rw [controlledIntervalPath, flattenBlockChoices]
  3347. dsimp only [id]
  3348. rw [IntervalPath.destinations_append, blockIntervalPath_destinations]
  3349. exact congrArg
  3350. (fun z ↦ List.ofFn (w 0) ++ z)
  3351. (ih (controlledNext x (w 0)) (Fin.tail w) _)
  3352. /-- Positive controlled paths with the same initial and final augmented state
  3353. are uniquely determined by their multiplier word. -/
  3354. theorem controlledPathWord_injective_of_return (n : ℕ) (x : AugmentedState)
  3355. {u v : Fin n → BlockChoice}
  3356. (hu : 0 < controlledChain.pathWeight n x u)
  3357. (hv : 0 < controlledChain.pathWeight n x v)
  3358. (huend : controlledChain.pathEndpoint n x u = x)
  3359. (hvend : controlledChain.pathEndpoint n x v = x)
  3360. (hword : controlledPathWord n x u = controlledPathWord n x v) :
  3361. u = v := by
  3362. have hend : (controlledChain.pathEndpoint n x u).1 =
  3363. (controlledChain.pathEndpoint n x v).1 := by rw [huend, hvend]
  3364. let pu := controlledIntervalPath n x u hu
  3365. let pv := controlledIntervalPath n x v hv
  3366. have hdest : pu.destinations = pv.destinations := by
  3367. apply IntervalPath.destinations_eq_of_word_eq_of_endpoint_eq pu pv hend
  3368. simpa [pu, pv] using hword
  3369. apply flattenBlockChoices_injective n
  3370. simpa [pu, pv] using hdest
  3371. /-! ### Algebra and probability of a positive controlled path -/
  3372. theorem exponents_append (u v : List ℕ) (i : Fin 4) :
  3373. exponents (u ++ v) i = exponents u i + exponents v i := by
  3374. simp [exponents]
  3375. theorem imbalance_append (u v : List ℕ) :
  3376. imbalance (u ++ v) = fun i ↦ imbalance u i + imbalance v i := by
  3377. funext i
  3378. fin_cases i <;> simp [imbalance, exponents_append] <;> ring
  3379. theorem blockWord_mem_multipliers (α : Assignment) (n : ℕ) (i : Fin 19)
  3380. (w : Fin n → Fin 19) : ∀ a ∈ blockWord α n i w, a ∈ multipliers := by
  3381. induction n generalizing i with
  3382. | zero => simp [blockWord]
  3383. | succ n ih =>
  3384. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3385. (Fin.cons_self_tail w).symm]
  3386. simp only [blockWord_cons, List.mem_cons]
  3387. intro a ha
  3388. rcases ha with rfl | ha
  3389. · exact prescribed_mem_multipliers α i
  3390. · exact ih (w 0) (Fin.tail w) a ha
  3391. theorem controlledPathWord_mem_multipliers (n : ℕ) (x : AugmentedState)
  3392. (w : Fin n → BlockChoice) :
  3393. ∀ a ∈ controlledPathWord n x w, a ∈ multipliers := by
  3394. induction n generalizing x with
  3395. | zero => simp [controlledPathWord]
  3396. | succ n ih =>
  3397. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3398. (Fin.cons_self_tail w).symm]
  3399. simp only [controlledPathWord, List.mem_append]
  3400. intro a ha
  3401. rcases ha with ha | ha
  3402. · exact blockWord_mem_multipliers _ _ _ _ a ha
  3403. · exact ih (controlledNext x (w 0)) (Fin.tail w) a ha
  3404. @[simp] theorem blockWord_length (α : Assignment) (n : ℕ) (i : Fin 19)
  3405. (w : Fin n → Fin 19) : (blockWord α n i w).length = n := by
  3406. induction n generalizing i with
  3407. | zero => rfl
  3408. | succ n ih =>
  3409. simp [blockWord, ih]
  3410. @[simp] theorem controlledPathWord_length (n : ℕ) (x : AugmentedState)
  3411. (w : Fin n → BlockChoice) :
  3412. (controlledPathWord n x w).length = controlBlockLength * n := by
  3413. induction n generalizing x with
  3414. | zero => simp [controlledPathWord]
  3415. | succ n ih =>
  3416. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3417. (Fin.cons_self_tail w).symm]
  3418. simp only [controlledPathWord, List.length_append, blockWord_length, ih]
  3419. ring
  3420. theorem controlledPathWord_imbalance (n : ℕ) (x : AugmentedState)
  3421. (w : Fin n → BlockChoice) :
  3422. imbalance (controlledPathWord n x w) = fun i ↦
  3423. (controlledChain.pathEndpoint n x w).2 i - x.2 i := by
  3424. induction n generalizing x with
  3425. | zero =>
  3426. funext i
  3427. fin_cases i <;> simp [controlledPathWord, FiniteChoiceChain.pathEndpoint,
  3428. imbalance, exponents]
  3429. | succ n ih =>
  3430. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3431. (Fin.cons_self_tail w).symm]
  3432. rw [controlledPathWord, imbalance_append,
  3433. ← blockIncrement_eq_imbalance]
  3434. rw [ih]
  3435. simp only [FiniteChoiceChain.pathEndpoint_cons, controlledChain,
  3436. controlledNext]
  3437. funext i
  3438. simp only [Fin.cons_zero, Fin.tail_cons]
  3439. ring
  3440. @[simp] theorem blockIntervalPath_mass (α : Assignment) (n : ℕ)
  3441. (i : Fin 19) (w : Fin n → Fin 19) (hpos : 0 < blockWeight α n i w) :
  3442. (blockIntervalPath α n i w hpos).mass = blockWeight α n i w := by
  3443. induction n generalizing i with
  3444. | zero => rfl
  3445. | succ n ih =>
  3446. simp only [blockIntervalPath, blockWeight]
  3447. exact congrArg
  3448. (fun z ↦ probability α i (w 0) * z)
  3449. (ih (w 0) (Fin.tail w) _)
  3450. set_option backward.isDefEq.respectTransparency false in
  3451. @[simp] theorem controlledIntervalPath_mass_real (n : ℕ) (x : AugmentedState)
  3452. (w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w) :
  3453. ((controlledIntervalPath n x w hpos).mass : ℝ) =
  3454. controlledChain.pathWeight n x w := by
  3455. induction n generalizing x with
  3456. | zero =>
  3457. norm_num [controlledIntervalPath, IntervalPath.mass,
  3458. FiniteChoiceChain.pathWeight]
  3459. | succ n ih =>
  3460. rw [controlledIntervalPath]
  3461. dsimp only [id]
  3462. rw [IntervalPath.mass_append, blockIntervalPath_mass, Rat.cast_mul]
  3463. simp only [controlledChain, controlledProbability,
  3464. FiniteChoiceChain.pathWeight]
  3465. exact congrArg
  3466. (fun z ↦
  3467. (blockWeight (feedback x.2) controlBlockLength x.1 (w 0) : ℝ) * z)
  3468. (ih (controlledNext x (w 0)) (Fin.tail w) _)
  3469. theorem controlledPathWeight_eq_reciprocal (n : ℕ) (x : AugmentedState)
  3470. (w : Fin n → BlockChoice) (hpos : 0 < controlledChain.pathWeight n x w)
  3471. (hend : controlledChain.pathEndpoint n x w = x) :
  3472. controlledChain.pathWeight n x w =
  3473. 1 / (controlledPathWord n x w).prod := by
  3474. let p := controlledIntervalPath n x w hpos
  3475. have hp : p.mass = 1 / (p.word.prod : ℚ) := by
  3476. rw [p.mass_eq_width_div]
  3477. have hwidth : width (controlledChain.pathEndpoint n x w).1 = width x.1 := by
  3478. rw [hend]
  3479. rw [hwidth]
  3480. have hw : width x.1 ≠ 0 := ne_of_gt (width_pos x.1)
  3481. field_simp
  3482. have hpReal : (p.mass : ℝ) = 1 / (p.word.prod : ℝ) := by
  3483. rw [hp]
  3484. simp [Function.comp_def]
  3485. simpa [p] using hpReal
  3486. theorem controlledPathWord_imbalance_zero (n : ℕ) (x : AugmentedState)
  3487. (w : Fin n → BlockChoice) (hend : controlledChain.pathEndpoint n x w = x) :
  3488. imbalance (controlledPathWord n x w) = 0 := by
  3489. rw [controlledPathWord_imbalance, hend]
  3490. funext i
  3491. simp
  3492. /-! ## Concrete first-return records -/
  3493. /-- A first return to the control set, viewed as an explicit tuple of
  3494. controlled block choices. -/
  3495. abbrev ControlReturnSegment (n : ℕ) (x y : AugmentedState) :=
  3496. {w : Fin n → BlockChoice //
  3497. controlledChain.IsReturnPath controlSet y n x w}
  3498. noncomputable instance controlReturnSegmentFintype (n : ℕ)
  3499. (x y : AugmentedState) : Fintype (ControlReturnSegment n x y) := by
  3500. classical
  3501. exact Fintype.ofInjective Subtype.val Subtype.val_injective
  3502. /-- A concrete realization of a first hit of the recurrent embedded state
  3503. after `k` visits to the control set and `n` original controlled blocks. -/
  3504. def HoldingHitRecord : ℕ → ℕ → EmbeddedState → Type
  3505. | 0, n, x => ULift {_unit : Unit // n = 0 ∧ x = recurrentControlState}
  3506. | k + 1, n, x =>
  3507. {_guard : Unit // x ≠ recurrentControlState} ×
  3508. Σ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3509. Σ y : EmbeddedState,
  3510. ControlReturnSegment p.1.1 x.1.1 y.1.1 ×
  3511. HoldingHitRecord k p.1.2 y
  3512. /-- A concrete positive first return to the recurrent embedded state. -/
  3513. def RecurrentLoopRecord : ℕ → ℕ → Type
  3514. | 0, _ => Empty
  3515. | k + 1, n =>
  3516. Σ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3517. Σ y : EmbeddedState,
  3518. ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×
  3519. HoldingHitRecord k p.1.2 y
  3520. noncomputable instance holdingHitRecordFintype (k n : ℕ) (x : EmbeddedState) :
  3521. Fintype (HoldingHitRecord k n x) := by
  3522. classical
  3523. induction k generalizing n x with
  3524. | zero =>
  3525. simp only [HoldingHitRecord]
  3526. letI : Fintype {_unit : Unit // n = 0 ∧ x = recurrentControlState} :=
  3527. Fintype.ofInjective Subtype.val Subtype.val_injective
  3528. exact Fintype.ofEquiv _ Equiv.ulift.symm
  3529. | succ k ih =>
  3530. simp only [HoldingHitRecord]
  3531. letI : Fintype {_guard : Unit // x ≠ recurrentControlState} :=
  3532. Fintype.ofInjective Subtype.val Subtype.val_injective
  3533. letI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3534. (y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=
  3535. ih p.1.2 y
  3536. letI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3537. (y : EmbeddedState) :
  3538. Fintype (ControlReturnSegment p.1.1 x.1.1 y.1.1) :=
  3539. Fintype.ofInjective Subtype.val Subtype.val_injective
  3540. infer_instance
  3541. noncomputable instance recurrentLoopRecordFintype (k n : ℕ) :
  3542. Fintype (RecurrentLoopRecord k n) := by
  3543. classical
  3544. cases k with
  3545. | zero =>
  3546. change Fintype Empty
  3547. exact Fintype.ofFinite Empty
  3548. | succ k =>
  3549. simp only [RecurrentLoopRecord]
  3550. letI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3551. (y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=
  3552. holdingHitRecordFintype k p.1.2 y
  3553. letI (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3554. (y : EmbeddedState) :
  3555. Fintype (ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1) :=
  3556. Fintype.ofInjective Subtype.val Subtype.val_injective
  3557. infer_instance
  3558. noncomputable def holdingHitRecordWeight :
  3559. {k n : ℕ} → {x : EmbeddedState} → HoldingHitRecord k n x → ℝ
  3560. | 0, _, _, _ => 1
  3561. | _ + 1, _, x, ⟨_, p, _, w, r⟩ =>
  3562. controlledChain.pathWeight p.1.1 x.1.1 w.1 * holdingHitRecordWeight r
  3563. noncomputable def recurrentLoopRecordWeight :
  3564. {k n : ℕ} → RecurrentLoopRecord k n → ℝ
  3565. | 0, _, r => r.elim
  3566. | _ + 1, _, ⟨p, _, w, r⟩ =>
  3567. controlledChain.pathWeight p.1.1 recurrentAugmentedState w.1 *
  3568. holdingHitRecordWeight r
  3569. noncomputable def holdingHitRecordChoices :
  3570. {k n : ℕ} → {x : EmbeddedState} → HoldingHitRecord k n x → List BlockChoice
  3571. | 0, _, _, _ => []
  3572. | _ + 1, _, _, ⟨_, _, _, w, r⟩ =>
  3573. List.ofFn w.1 ++ holdingHitRecordChoices r
  3574. noncomputable def recurrentLoopRecordChoices :
  3575. {k n : ℕ} → RecurrentLoopRecord k n → List BlockChoice
  3576. | 0, _, r => r.elim
  3577. | _ + 1, _, ⟨_, _, w, r⟩ =>
  3578. List.ofFn w.1 ++ holdingHitRecordChoices r
  3579. @[simp] theorem holdingHitRecordChoices_length {k n : ℕ} {x : EmbeddedState}
  3580. (r : HoldingHitRecord k n x) :
  3581. (holdingHitRecordChoices (k := k) (n := n) (x := x) r).length = n := by
  3582. induction k generalizing n x with
  3583. | zero =>
  3584. simp only [HoldingHitRecord] at r
  3585. have hn := r.down.2.1
  3586. subst n
  3587. rfl
  3588. | succ k ih =>
  3589. obtain ⟨guard, p, y, w, tail⟩ := r
  3590. simp only [holdingHitRecordChoices, List.length_append, List.length_ofFn,
  3591. ih tail]
  3592. exact Finset.HasAntidiagonal.mem_antidiagonal.mp p.2
  3593. @[simp] theorem recurrentLoopRecordChoices_length {k n : ℕ}
  3594. (r : RecurrentLoopRecord k n) :
  3595. (recurrentLoopRecordChoices (k := k) (n := n) r).length = n := by
  3596. cases k with
  3597. | zero => exact r.elim
  3598. | succ k =>
  3599. obtain ⟨p, y, w, tail⟩ := r
  3600. simp only [recurrentLoopRecordChoices, List.length_append, List.length_ofFn,
  3601. holdingHitRecordChoices_length]
  3602. exact Finset.HasAntidiagonal.mem_antidiagonal.mp p.2
  3603. theorem sum_controlReturnSegment_weight (n : ℕ) (x y : AugmentedState) :
  3604. (∑ w : ControlReturnSegment n x y,
  3605. controlledChain.pathWeight n x w.1) =
  3606. controlledChain.returnAt controlSet y n x := by
  3607. classical
  3608. rw [sum_subtype_eq_selected]
  3609. exact controlledChain.sum_returnPath_weight controlSet y n x
  3610. theorem sum_holdingHitRecordWeight (k n : ℕ) (x : EmbeddedState) :
  3611. (∑ r : HoldingHitRecord k n x, holdingHitRecordWeight r) =
  3612. holdingHit k n x := by
  3613. classical
  3614. induction k generalizing n x with
  3615. | zero =>
  3616. by_cases h : n = 0 ∧ x = recurrentControlState
  3617. · rcases h with ⟨rfl, rfl⟩
  3618. let r0 : HoldingHitRecord 0 0 recurrentControlState :=
  3619. ULift.up ⟨(), rfl, rfl⟩
  3620. have huniv : (Finset.univ : Finset (HoldingHitRecord 0 0
  3621. recurrentControlState)) = {r0} := by
  3622. ext r
  3623. simp only [Finset.mem_univ, true_iff, Finset.mem_singleton]
  3624. rcases r with ⟨⟨u, hu⟩⟩
  3625. cases u
  3626. rfl
  3627. rw [huniv]
  3628. simp [holdingHitRecordWeight, holdingHit]
  3629. · let : IsEmpty (HoldingHitRecord 0 n x) :=
  3630. ⟨fun r ↦ h r.down.2⟩
  3631. rw [show holdingHit 0 n x = 0 by simp [holdingHit, h]]
  3632. apply Finset.sum_eq_zero
  3633. intro r _
  3634. exact isEmptyElim r
  3635. | succ k ih =>
  3636. by_cases hx : x = recurrentControlState
  3637. · subst x
  3638. let : IsEmpty (HoldingHitRecord (k + 1) n recurrentControlState) :=
  3639. ⟨fun r ↦ r.1.2 rfl⟩
  3640. rw [show holdingHit (k + 1) n recurrentControlState = 0 by
  3641. simp [holdingHit]]
  3642. apply Finset.sum_eq_zero
  3643. intro r _
  3644. exact isEmptyElim r
  3645. · let : Fintype {_guard : Unit // x ≠ recurrentControlState} :=
  3646. Fintype.ofInjective Subtype.val Subtype.val_injective
  3647. let (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3648. (y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=
  3649. holdingHitRecordFintype k p.1.2 y
  3650. let (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3651. (y : EmbeddedState) :
  3652. Fintype (ControlReturnSegment p.1.1 x.1.1 y.1.1) :=
  3653. Fintype.ofInjective Subtype.val Subtype.val_injective
  3654. let expandedFintype :
  3655. Fintype (
  3656. {_guard : Unit // x ≠ recurrentControlState} ×
  3657. Σ p : {p : ℕ × ℕ //
  3658. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3659. Σ y : EmbeddedState,
  3660. ControlReturnSegment p.1.1 x.1.1 y.1.1 ×
  3661. HoldingHitRecord k p.1.2 y) :=
  3662. inferInstance
  3663. let recordEquiv :
  3664. HoldingHitRecord (k + 1) n x ≃
  3665. ({_guard : Unit // x ≠ recurrentControlState} ×
  3666. Σ p : {p : ℕ × ℕ //
  3667. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3668. Σ y : EmbeddedState,
  3669. ControlReturnSegment p.1.1 x.1.1 y.1.1 ×
  3670. HoldingHitRecord k p.1.2 y) :=
  3671. Equiv.refl _
  3672. have hsum := @Fintype.sum_equiv
  3673. (HoldingHitRecord (k + 1) n x)
  3674. ({_guard : Unit // x ≠ recurrentControlState} ×
  3675. Σ p : {p : ℕ × ℕ //
  3676. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3677. Σ y : EmbeddedState,
  3678. ControlReturnSegment p.1.1 x.1.1 y.1.1 ×
  3679. HoldingHitRecord k p.1.2 y) ℝ
  3680. (holdingHitRecordFintype (k + 1) n x) expandedFintype
  3681. inferInstance recordEquiv
  3682. (fun r ↦ holdingHitRecordWeight r)
  3683. (fun r ↦
  3684. controlledChain.pathWeight r.2.1.1.1 x.1.1 r.2.2.2.1.1 *
  3685. holdingHitRecordWeight r.2.2.2.2)
  3686. (fun _ ↦ rfl)
  3687. rw [hsum]
  3688. rw [Fintype.sum_prod_type]
  3689. simp_rw [Fintype.sum_sigma, Fintype.sum_prod_type]
  3690. simp_rw [← Finset.mul_sum, ih]
  3691. simp_rw [← Finset.sum_mul, sum_controlReturnSegment_weight]
  3692. rw [show (∑ _guard : {_guard : Unit // x ≠ recurrentControlState},
  3693. ∑ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3694. ∑ y : EmbeddedState,
  3695. controlledChain.returnAt controlSet y.1.1 p.1.1 x.1.1 *
  3696. holdingHit k p.1.2 y) =
  3697. ∑ p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3698. ∑ y : EmbeddedState,
  3699. controlledChain.returnAt controlSet y.1.1 p.1.1 x.1.1 *
  3700. holdingHit k p.1.2 y by simp [hx]]
  3701. rw [sum_subtype_eq_sum_attach]
  3702. rw [Finset.sum_comm]
  3703. simp only [holdingHit, hx, if_false, convolution]
  3704. apply Finset.sum_congr rfl
  3705. intro y _
  3706. simpa only [holdingMass] using
  3707. Finset.sum_attach (Finset.HasAntidiagonal.antidiagonal n)
  3708. (fun p ↦ controlledChain.returnAt controlSet y.1.1 p.1 x.1.1 *
  3709. holdingHit k p.2 y)
  3710. theorem sum_recurrentLoopRecordWeight (k n : ℕ) :
  3711. (∑ r : RecurrentLoopRecord k n, recurrentLoopRecordWeight r) =
  3712. holdingReturn k n := by
  3713. classical
  3714. cases k with
  3715. | zero =>
  3716. rw [show holdingReturn 0 n = 0 by simp [holdingReturn]]
  3717. apply Finset.sum_eq_zero
  3718. intro r _
  3719. exact r.elim
  3720. | succ k =>
  3721. let (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3722. (y : EmbeddedState) : Fintype (HoldingHitRecord k p.1.2 y) :=
  3723. holdingHitRecordFintype k p.1.2 y
  3724. let (p : {p : ℕ × ℕ // p ∈ Finset.HasAntidiagonal.antidiagonal n})
  3725. (y : EmbeddedState) :
  3726. Fintype (ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1) :=
  3727. Fintype.ofInjective Subtype.val Subtype.val_injective
  3728. let expandedFintype :
  3729. Fintype (
  3730. Σ p : {p : ℕ × ℕ //
  3731. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3732. Σ y : EmbeddedState,
  3733. ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×
  3734. HoldingHitRecord k p.1.2 y) :=
  3735. inferInstance
  3736. let recordEquiv :
  3737. RecurrentLoopRecord (k + 1) n ≃
  3738. (Σ p : {p : ℕ × ℕ //
  3739. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3740. Σ y : EmbeddedState,
  3741. ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×
  3742. HoldingHitRecord k p.1.2 y) :=
  3743. Equiv.refl _
  3744. have hsum := @Fintype.sum_equiv
  3745. (RecurrentLoopRecord (k + 1) n)
  3746. (Σ p : {p : ℕ × ℕ //
  3747. p ∈ Finset.HasAntidiagonal.antidiagonal n},
  3748. Σ y : EmbeddedState,
  3749. ControlReturnSegment p.1.1 recurrentAugmentedState y.1.1 ×
  3750. HoldingHitRecord k p.1.2 y) ℝ
  3751. (recurrentLoopRecordFintype (k + 1) n) expandedFintype
  3752. inferInstance recordEquiv
  3753. (fun r ↦ recurrentLoopRecordWeight r)
  3754. (fun r ↦
  3755. controlledChain.pathWeight r.1.1.1 recurrentAugmentedState
  3756. r.2.2.1.1 *
  3757. holdingHitRecordWeight r.2.2.2)
  3758. (fun _ ↦ rfl)
  3759. rw [hsum]
  3760. rw [Fintype.sum_sigma]
  3761. simp_rw [Fintype.sum_sigma, Fintype.sum_prod_type]
  3762. simp_rw [← Finset.mul_sum, sum_holdingHitRecordWeight]
  3763. simp_rw [← Finset.sum_mul, sum_controlReturnSegment_weight]
  3764. rw [sum_subtype_eq_sum_attach, Finset.sum_comm]
  3765. simp only [holdingReturn, convolution]
  3766. apply Finset.sum_congr rfl
  3767. intro y _
  3768. simpa only [holdingMass, recurrentAugmentedState] using
  3769. Finset.sum_attach (Finset.HasAntidiagonal.antidiagonal n)
  3770. (fun p ↦ controlledChain.returnAt controlSet y.1.1 p.1
  3771. recurrentAugmentedState * holdingHit k p.2 y)
  3772. /-! ### Flat finite-choice lists -/
  3773. namespace FiniteChoiceChain
  3774. def listEndpoint {S Ω : Type*} [Fintype Ω] (K : FiniteChoiceChain S Ω) :
  3775. S → List Ω → S
  3776. | x, [] => x
  3777. | x, a :: w => K.listEndpoint (K.next x a) w
  3778. def listWeight {S Ω : Type*} [Fintype Ω] (K : FiniteChoiceChain S Ω) :
  3779. S → List Ω → ℝ
  3780. | _, [] => 1
  3781. | x, a :: w => K.probability x a * K.listWeight (K.next x a) w
  3782. theorem listEndpoint_append {S Ω : Type*} [Fintype Ω]
  3783. (K : FiniteChoiceChain S Ω) (x : S) (u v : List Ω) :
  3784. K.listEndpoint x (u ++ v) = K.listEndpoint (K.listEndpoint x u) v := by
  3785. induction u generalizing x with
  3786. | nil => rfl
  3787. | cons a u ih => exact ih (K.next x a)
  3788. theorem listWeight_append {S Ω : Type*} [Fintype Ω]
  3789. (K : FiniteChoiceChain S Ω) (x : S) (u v : List Ω) :
  3790. K.listWeight x (u ++ v) =
  3791. K.listWeight x u * K.listWeight (K.listEndpoint x u) v := by
  3792. induction u generalizing x with
  3793. | nil => simp [listWeight, listEndpoint]
  3794. | cons a u ih =>
  3795. simp only [List.cons_append, listWeight, listEndpoint, ih]
  3796. ring
  3797. theorem listWeight_nonneg {S Ω : Type*} [Fintype Ω]
  3798. (K : FiniteChoiceChain S Ω) (x : S) (u : List Ω) :
  3799. 0 ≤ K.listWeight x u := by
  3800. induction u generalizing x with
  3801. | nil => simp [listWeight]
  3802. | cons a u ih =>
  3803. exact mul_nonneg (K.probability_nonneg x a) (ih (K.next x a))
  3804. def IsHitList {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3805. (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : S → List Ω → Prop
  3806. | x, [] => x ∈ C ∧ x = y
  3807. | x, a :: w => x ∉ C ∧ K.IsHitList C y (K.next x a) w
  3808. def IsReturnList {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3809. (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) : S → List Ω → Prop
  3810. | _, [] => False
  3811. | x, a :: w => K.IsHitList C y (K.next x a) w
  3812. theorem listEndpoint_ofFn {S Ω : Type*} [Fintype Ω]
  3813. (K : FiniteChoiceChain S Ω) (n : ℕ) (x : S) (w : Fin n → Ω) :
  3814. K.listEndpoint x (List.ofFn w) = K.pathEndpoint n x w := by
  3815. induction n generalizing x with
  3816. | zero => rfl
  3817. | succ n ih =>
  3818. rw [List.ofFn_succ, listEndpoint, ih]
  3819. have htail : Fin.tail w = fun i ↦ w i.succ := by
  3820. funext i
  3821. rfl
  3822. rw [← htail]
  3823. exact (K.pathEndpoint_cons n x (w 0) (Fin.tail w)).symm
  3824. theorem listWeight_ofFn {S Ω : Type*} [Fintype Ω]
  3825. (K : FiniteChoiceChain S Ω) (n : ℕ) (x : S) (w : Fin n → Ω) :
  3826. K.listWeight x (List.ofFn w) = K.pathWeight n x w := by
  3827. induction n generalizing x with
  3828. | zero => rfl
  3829. | succ n ih =>
  3830. rw [List.ofFn_succ, listWeight, ih]
  3831. have htail : Fin.tail w = fun i ↦ w i.succ := by
  3832. funext i
  3833. rfl
  3834. rw [← htail]
  3835. exact (K.pathWeight_cons n x (w 0) (Fin.tail w)).symm
  3836. theorem isHitList_of_isHitPath {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3837. (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) (n : ℕ)
  3838. (x : S) (w : Fin n → Ω) (h : K.IsHitPath C y n x w) :
  3839. K.IsHitList C y x (List.ofFn w) := by
  3840. induction n generalizing x with
  3841. | zero => simpa [IsHitList] using h
  3842. | succ n ih =>
  3843. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3844. (Fin.cons_self_tail w).symm] at h
  3845. rw [List.ofFn_succ]
  3846. change x ∉ C ∧ K.IsHitList C y (K.next x (w 0))
  3847. (List.ofFn (Fin.tail w))
  3848. exact ⟨h.1, ih _ _ h.2⟩
  3849. theorem isReturnList_of_isReturnPath {S Ω : Type*} [Fintype Ω]
  3850. [DecidableEq S] (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S)
  3851. (n : ℕ) (x : S) (w : Fin n → Ω) (h : K.IsReturnPath C y n x w) :
  3852. K.IsReturnList C y x (List.ofFn w) := by
  3853. cases n with
  3854. | zero => exact (K.isReturnPath_zero C x y w h).elim
  3855. | succ n =>
  3856. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3857. (Fin.cons_self_tail w).symm] at h
  3858. rw [List.ofFn_succ]
  3859. change K.IsHitList C y (K.next x (w 0))
  3860. (List.ofFn (Fin.tail w))
  3861. exact K.isHitList_of_isHitPath C y n _ _ h
  3862. theorem isReturnPath_length_pos {S Ω : Type*} [Fintype Ω]
  3863. (K : FiniteChoiceChain S Ω) (C : Finset S) (y : S) (n : ℕ)
  3864. (x : S) (w : Fin n → Ω) (h : K.IsReturnPath C y n x w) : 0 < n := by
  3865. classical
  3866. cases n with
  3867. | zero => exact (K.isReturnPath_zero C x y w h).elim
  3868. | succ n => omega
  3869. theorem isHitList_endpoint {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3870. (K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)
  3871. (h : K.IsHitList C y x w) : K.listEndpoint x w = y := by
  3872. induction w generalizing x with
  3873. | nil => exact h.2
  3874. | cons a w ih => exact ih _ h.2
  3875. theorem isReturnList_endpoint {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3876. (K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)
  3877. (h : K.IsReturnList C y x w) : K.listEndpoint x w = y := by
  3878. cases w with
  3879. | nil => exact h.elim
  3880. | cons a w => exact K.isHitList_endpoint C y _ w h
  3881. /-- Two first-hit prefixes of one common choice list have the same cut point. -/
  3882. theorem isHitList_append_unique {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  3883. (K : FiniteChoiceChain S Ω) (C : Finset S) {x y z : S}
  3884. {u v r s : List Ω} (hu : K.IsHitList C y x u)
  3885. (hv : K.IsHitList C z x v) (happend : u ++ r = v ++ s) :
  3886. u = v ∧ r = s := by
  3887. induction u generalizing x v with
  3888. | nil =>
  3889. cases v with
  3890. | nil => exact ⟨rfl, happend⟩
  3891. | cons b v => exact (hv.1 hu.1).elim
  3892. | cons a u ih =>
  3893. cases v with
  3894. | nil => exact (hu.1 hv.1).elim
  3895. | cons b v =>
  3896. have hab : a = b := (List.cons.inj happend).1
  3897. subst b
  3898. have h := ih hu.2 hv.2 (List.cons.inj happend).2
  3899. exact ⟨congrArg (List.cons a) h.1, h.2⟩
  3900. theorem isReturnList_append_unique {S Ω : Type*} [Fintype Ω]
  3901. [DecidableEq S] (K : FiniteChoiceChain S Ω) (C : Finset S)
  3902. {x y z : S} {u v r s : List Ω} (hu : K.IsReturnList C y x u)
  3903. (hv : K.IsReturnList C z x v) (happend : u ++ r = v ++ s) :
  3904. u = v ∧ r = s := by
  3905. cases u with
  3906. | nil => exact hu.elim
  3907. | cons a u =>
  3908. cases v with
  3909. | nil => exact hv.elim
  3910. | cons b v =>
  3911. have hab : a = b := (List.cons.inj happend).1
  3912. subst b
  3913. have htail := (List.cons.inj happend).2
  3914. have h := K.isHitList_append_unique C hu hv htail
  3915. exact ⟨congrArg (List.cons a) h.1, h.2⟩
  3916. end FiniteChoiceChain
  3917. noncomputable def controlledListWord : AugmentedState → List BlockChoice → List ℕ
  3918. | _, [] => []
  3919. | x, w :: tail =>
  3920. blockWord (feedback x.2) controlBlockLength x.1 w ++
  3921. controlledListWord (controlledNext x w) tail
  3922. theorem controlledListWord_ofFn (n : ℕ) (x : AugmentedState)
  3923. (w : Fin n → BlockChoice) :
  3924. controlledListWord x (List.ofFn w) = controlledPathWord n x w := by
  3925. induction n generalizing x with
  3926. | zero => rfl
  3927. | succ n ih =>
  3928. rw [show w = Fin.cons (w 0) (Fin.tail w) from
  3929. (Fin.cons_self_tail w).symm]
  3930. simp [controlledListWord, controlledPathWord, ih]
  3931. rfl
  3932. theorem holdingHitRecordWeight_eq_listWeight {k n : ℕ} {x : EmbeddedState}
  3933. (r : HoldingHitRecord k n x) :
  3934. holdingHitRecordWeight r =
  3935. controlledChain.listWeight x.1.1
  3936. (holdingHitRecordChoices (k := k) (n := n) (x := x) r) := by
  3937. induction k generalizing n x with
  3938. | zero =>
  3939. simp only [HoldingHitRecord] at r
  3940. have hn := r.down.2.1
  3941. subst n
  3942. rfl
  3943. | succ k ih =>
  3944. obtain ⟨guard, p, y, w, tail⟩ := r
  3945. simp only [holdingHitRecordWeight, holdingHitRecordChoices]
  3946. rw [controlledChain.listWeight_append,
  3947. controlledChain.listWeight_ofFn]
  3948. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  3949. p.1.1 x.1.1 w.1 w.2
  3950. rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1
  3951. (List.ofFn w.1) hw, ← ih tail]
  3952. theorem recurrentLoopRecordWeight_eq_listWeight {k n : ℕ}
  3953. (r : RecurrentLoopRecord k n) :
  3954. recurrentLoopRecordWeight r = controlledChain.listWeight
  3955. recurrentAugmentedState
  3956. (recurrentLoopRecordChoices (k := k) (n := n) r) := by
  3957. cases k with
  3958. | zero => exact r.elim
  3959. | succ k =>
  3960. obtain ⟨p, y, w, tail⟩ := r
  3961. simp only [recurrentLoopRecordWeight, recurrentLoopRecordChoices]
  3962. rw [controlledChain.listWeight_append,
  3963. controlledChain.listWeight_ofFn]
  3964. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  3965. p.1.1 recurrentAugmentedState w.1 w.2
  3966. rw [controlledChain.isReturnList_endpoint controlSet y.1.1
  3967. recurrentAugmentedState (List.ofFn w.1) hw,
  3968. ← holdingHitRecordWeight_eq_listWeight tail]
  3969. theorem holdingHitRecord_endpoint {k n : ℕ} {x : EmbeddedState}
  3970. (r : HoldingHitRecord k n x) : controlledChain.listEndpoint x.1.1
  3971. (holdingHitRecordChoices (k := k) (n := n) (x := x) r) =
  3972. recurrentAugmentedState := by
  3973. induction k generalizing n x with
  3974. | zero =>
  3975. simp only [HoldingHitRecord] at r
  3976. have hx := r.down.2.2
  3977. subst x
  3978. rfl
  3979. | succ k ih =>
  3980. obtain ⟨guard, p, y, w, tail⟩ := r
  3981. rw [holdingHitRecordChoices, controlledChain.listEndpoint_append]
  3982. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  3983. p.1.1 x.1.1 w.1 w.2
  3984. rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1
  3985. (List.ofFn w.1) hw]
  3986. exact ih tail
  3987. theorem recurrentLoopRecord_endpoint {k n : ℕ}
  3988. (r : RecurrentLoopRecord k n) : controlledChain.listEndpoint
  3989. recurrentAugmentedState
  3990. (recurrentLoopRecordChoices (k := k) (n := n) r) =
  3991. recurrentAugmentedState := by
  3992. cases k with
  3993. | zero => exact r.elim
  3994. | succ k =>
  3995. obtain ⟨p, y, w, tail⟩ := r
  3996. rw [recurrentLoopRecordChoices, controlledChain.listEndpoint_append]
  3997. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  3998. p.1.1 recurrentAugmentedState w.1 w.2
  3999. rw [controlledChain.isReturnList_endpoint controlSet y.1.1
  4000. recurrentAugmentedState (List.ofFn w.1) hw]
  4001. exact holdingHitRecord_endpoint tail
  4002. theorem holdingHitRecordChoices_injective (k n : ℕ) (x : EmbeddedState) :
  4003. Function.Injective (holdingHitRecordChoices :
  4004. HoldingHitRecord k n x → List BlockChoice) := by
  4005. induction k generalizing n x with
  4006. | zero =>
  4007. intro r s _
  4008. simp only [HoldingHitRecord] at r s
  4009. apply ULift.ext
  4010. apply Subtype.ext
  4011. exact Subsingleton.elim _ _
  4012. | succ k ih =>
  4013. intro r s hrs
  4014. obtain ⟨gr, p, y, w, tail⟩ := r
  4015. obtain ⟨gs, q, z, v, tail'⟩ := s
  4016. simp only [holdingHitRecordChoices] at hrs
  4017. have hwret := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4018. p.1.1 x.1.1 w.1 w.2
  4019. have hvret := controlledChain.isReturnList_of_isReturnPath controlSet z.1.1
  4020. q.1.1 x.1.1 v.1 v.2
  4021. have hsplit := controlledChain.isReturnList_append_unique controlSet
  4022. hwret hvret hrs
  4023. have hlen : p.1.1 = q.1.1 := by
  4024. simpa using congrArg List.length hsplit.1
  4025. have hpSum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2
  4026. have hqSum := Finset.HasAntidiagonal.mem_antidiagonal.mp q.2
  4027. have hrest : p.1.2 = q.1.2 := by omega
  4028. have hpval : p.1 = q.1 := Prod.ext hlen hrest
  4029. have hp : p = q := Subtype.ext hpval
  4030. subst q
  4031. have hwval : w.1 = v.1 := List.ofFn_injective hsplit.1
  4032. have hyval : y.1.1 = z.1.1 := by
  4033. have hyend := controlledChain.isReturnList_endpoint controlSet y.1.1
  4034. x.1.1 (List.ofFn w.1) hwret
  4035. have hzend := controlledChain.isReturnList_endpoint controlSet z.1.1
  4036. x.1.1 (List.ofFn v.1) hvret
  4037. rw [hsplit.1] at hyend
  4038. exact hyend.symm.trans hzend
  4039. have hy : y = z := by
  4040. apply Subtype.ext
  4041. apply Subtype.ext
  4042. exact hyval
  4043. subst z
  4044. have hw : w = v := Subtype.ext hwval
  4045. subst v
  4046. have ht : tail = tail' := ih p.1.2 y hsplit.2
  4047. subst tail'
  4048. have hg : gr = gs := Subsingleton.elim gr gs
  4049. subst gs
  4050. rfl
  4051. theorem recurrentLoopRecordChoices_injective (k n : ℕ) :
  4052. Function.Injective (recurrentLoopRecordChoices :
  4053. RecurrentLoopRecord k n → List BlockChoice) := by
  4054. cases k with
  4055. | zero =>
  4056. intro r
  4057. exact r.elim
  4058. | succ k =>
  4059. intro r s hrs
  4060. obtain ⟨p, y, w, tail⟩ := r
  4061. obtain ⟨q, z, v, tail'⟩ := s
  4062. simp only [recurrentLoopRecordChoices] at hrs
  4063. have hwret := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4064. p.1.1 recurrentAugmentedState w.1 w.2
  4065. have hvret := controlledChain.isReturnList_of_isReturnPath controlSet z.1.1
  4066. q.1.1 recurrentAugmentedState v.1 v.2
  4067. have hsplit := controlledChain.isReturnList_append_unique controlSet
  4068. hwret hvret hrs
  4069. have hlen : p.1.1 = q.1.1 := by
  4070. simpa using congrArg List.length hsplit.1
  4071. have hpSum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2
  4072. have hqSum := Finset.HasAntidiagonal.mem_antidiagonal.mp q.2
  4073. have hrest : p.1.2 = q.1.2 := by omega
  4074. have hpval : p.1 = q.1 := Prod.ext hlen hrest
  4075. have hp : p = q := Subtype.ext hpval
  4076. subst q
  4077. have hwval : w.1 = v.1 := List.ofFn_injective hsplit.1
  4078. have hyval : y.1.1 = z.1.1 := by
  4079. have hyend := controlledChain.isReturnList_endpoint controlSet y.1.1
  4080. recurrentAugmentedState (List.ofFn w.1) hwret
  4081. have hzend := controlledChain.isReturnList_endpoint controlSet z.1.1
  4082. recurrentAugmentedState (List.ofFn v.1) hvret
  4083. rw [hsplit.1] at hyend
  4084. exact hyend.symm.trans hzend
  4085. have hy : y = z := by
  4086. apply Subtype.ext
  4087. apply Subtype.ext
  4088. exact hyval
  4089. subst z
  4090. have hw : w = v := Subtype.ext hwval
  4091. subst v
  4092. have ht : tail = tail' := holdingHitRecordChoices_injective k p.1.2 y hsplit.2
  4093. subst tail'
  4094. rfl
  4095. /-! ### The controlled word carried by a loop record -/
  4096. noncomputable def recurrentLoopRecordTuple {k n : ℕ}
  4097. (r : RecurrentLoopRecord k n) : Fin n → BlockChoice := fun i ↦
  4098. (recurrentLoopRecordChoices (k := k) (n := n) r).get
  4099. (Fin.cast (recurrentLoopRecordChoices_length r).symm i)
  4100. @[simp] theorem ofFn_recurrentLoopRecordTuple {k n : ℕ}
  4101. (r : RecurrentLoopRecord k n) :
  4102. List.ofFn (recurrentLoopRecordTuple r) =
  4103. recurrentLoopRecordChoices (k := k) (n := n) r := by
  4104. apply List.ext_get
  4105. · simp
  4106. · intro i hi hj
  4107. simp [recurrentLoopRecordTuple]
  4108. noncomputable def recurrentLoopRecordWord {k n : ℕ}
  4109. (r : RecurrentLoopRecord k n) : List ℕ :=
  4110. controlledListWord recurrentAugmentedState
  4111. (recurrentLoopRecordChoices (k := k) (n := n) r)
  4112. @[simp] theorem recurrentLoopRecordWord_eq_controlledPathWord {k n : ℕ}
  4113. (r : RecurrentLoopRecord k n) : recurrentLoopRecordWord r =
  4114. controlledPathWord n recurrentAugmentedState
  4115. (recurrentLoopRecordTuple r) := by
  4116. rw [recurrentLoopRecordWord, ← controlledListWord_ofFn,
  4117. ofFn_recurrentLoopRecordTuple]
  4118. theorem recurrentLoopRecord_pathEndpoint {k n : ℕ}
  4119. (r : RecurrentLoopRecord k n) :
  4120. controlledChain.pathEndpoint n recurrentAugmentedState
  4121. (recurrentLoopRecordTuple r) = recurrentAugmentedState := by
  4122. rw [← controlledChain.listEndpoint_ofFn, ofFn_recurrentLoopRecordTuple]
  4123. exact recurrentLoopRecord_endpoint r
  4124. theorem recurrentLoopRecordWeight_eq_pathWeight {k n : ℕ}
  4125. (r : RecurrentLoopRecord k n) : recurrentLoopRecordWeight r =
  4126. controlledChain.pathWeight n recurrentAugmentedState
  4127. (recurrentLoopRecordTuple r) := by
  4128. rw [recurrentLoopRecordWeight_eq_listWeight,
  4129. ← controlledChain.listWeight_ofFn, ofFn_recurrentLoopRecordTuple]
  4130. theorem recurrentLoopRecordWeight_nonneg {k n : ℕ}
  4131. (r : RecurrentLoopRecord k n) : 0 ≤ recurrentLoopRecordWeight r := by
  4132. rw [recurrentLoopRecordWeight_eq_listWeight]
  4133. exact controlledChain.listWeight_nonneg _ _
  4134. theorem recurrentLoopRecordWord_mem_multipliers {k n : ℕ}
  4135. (r : RecurrentLoopRecord k n) :
  4136. ∀ a ∈ recurrentLoopRecordWord r, a ∈ multipliers := by
  4137. rw [recurrentLoopRecordWord_eq_controlledPathWord]
  4138. exact controlledPathWord_mem_multipliers n recurrentAugmentedState
  4139. (recurrentLoopRecordTuple r)
  4140. @[simp] theorem recurrentLoopRecordWord_length {k n : ℕ}
  4141. (r : RecurrentLoopRecord k n) :
  4142. (recurrentLoopRecordWord r).length = controlBlockLength * n := by
  4143. rw [recurrentLoopRecordWord_eq_controlledPathWord,
  4144. controlledPathWord_length]
  4145. theorem recurrentLoopRecord_imbalance_zero {k n : ℕ}
  4146. (r : RecurrentLoopRecord k n) : imbalance (recurrentLoopRecordWord r) = 0 := by
  4147. rw [recurrentLoopRecordWord_eq_controlledPathWord]
  4148. exact controlledPathWord_imbalance_zero n recurrentAugmentedState
  4149. (recurrentLoopRecordTuple r) (recurrentLoopRecord_pathEndpoint r)
  4150. theorem recurrentLoopRecordWeight_eq_reciprocal {k n : ℕ}
  4151. (r : RecurrentLoopRecord k n) (hpos : 0 < recurrentLoopRecordWeight r) :
  4152. recurrentLoopRecordWeight r = 1 / ((recurrentLoopRecordWord r).prod : ℝ) := by
  4153. rw [recurrentLoopRecordWeight_eq_pathWeight] at hpos ⊢
  4154. rw [recurrentLoopRecordWord_eq_controlledPathWord]
  4155. exact controlledPathWeight_eq_reciprocal n recurrentAugmentedState
  4156. (recurrentLoopRecordTuple r) hpos (recurrentLoopRecord_pathEndpoint r)
  4157. theorem recurrentLoopRecord_blockLength_pos {k n : ℕ}
  4158. (r : RecurrentLoopRecord k n) : 0 < n := by
  4159. cases k with
  4160. | zero => exact r.elim
  4161. | succ k =>
  4162. obtain ⟨p, y, w, tail⟩ := r
  4163. have hp : 0 < p.1.1 :=
  4164. controlledChain.isReturnPath_length_pos controlSet y.1.1 p.1.1
  4165. recurrentAugmentedState w.1 w.2
  4166. have hsum := Finset.HasAntidiagonal.mem_antidiagonal.mp p.2
  4167. omega
  4168. theorem recurrentLoopRecordWord_ne_nil {k n : ℕ}
  4169. (r : RecurrentLoopRecord k n) : recurrentLoopRecordWord r ≠ [] := by
  4170. intro hnil
  4171. have hlen := congrArg List.length hnil
  4172. simp only [recurrentLoopRecordWord_length, List.length_nil] at hlen
  4173. have hN := controlBlockLength_pos
  4174. have hn := recurrentLoopRecord_blockLength_pos r
  4175. have hprod : 0 < controlBlockLength * n := Nat.mul_pos hN hn
  4176. omega
  4177. noncomputable def recurrentLoopReward {k n : ℕ}
  4178. (r : RecurrentLoopRecord k n) : ℕ :=
  4179. exponents (recurrentLoopRecordWord r) 3
  4180. theorem recurrentLoopRecord_prod_eq_q_pow {k n : ℕ}
  4181. (r : RecurrentLoopRecord k n) :
  4182. (recurrentLoopRecordWord r).prod = q ^ recurrentLoopReward r := by
  4183. exact prod_eq_q_pow_of_imbalance_eq_zero (recurrentLoopRecordWord r)
  4184. (recurrentLoopRecordWord_mem_multipliers r)
  4185. (recurrentLoopRecord_imbalance_zero r)
  4186. theorem prod_two_le_of_ne_nil {w : List ℕ} (hne : w ≠ [])
  4187. (htwo : ∀ a ∈ w, 2 ≤ a) : 2 ≤ w.prod := by
  4188. obtain ⟨a, tail, rfl⟩ := List.exists_cons_of_ne_nil hne
  4189. have ha := htwo a (by simp)
  4190. have htail : 1 ≤ tail.prod := by
  4191. apply Nat.one_le_iff_ne_zero.mpr
  4192. apply List.prod_ne_zero
  4193. intro hzero
  4194. have hb2 := htwo 0 (by simp [hzero])
  4195. omega
  4196. simp only [List.prod_cons]
  4197. nlinarith
  4198. theorem recurrentLoopReward_pos {k n : ℕ} (r : RecurrentLoopRecord k n) :
  4199. 0 < recurrentLoopReward r := by
  4200. by_contra hnot
  4201. have hz : recurrentLoopReward r = 0 := Nat.eq_zero_of_not_pos hnot
  4202. have hprod := recurrentLoopRecord_prod_eq_q_pow r
  4203. rw [hz, pow_zero] at hprod
  4204. have htwo : ∀ a ∈ recurrentLoopRecordWord r, 2 ≤ a := by
  4205. intro a ha
  4206. exact mem_multipliers_two_le (recurrentLoopRecordWord_mem_multipliers r a ha)
  4207. have := prod_two_le_of_ne_nil (recurrentLoopRecordWord_ne_nil r) htwo
  4208. omega
  4209. theorem recurrentLoopRecordWeight_eq_q_reward {k n : ℕ}
  4210. (r : RecurrentLoopRecord k n) (hpos : 0 < recurrentLoopRecordWeight r) :
  4211. recurrentLoopRecordWeight r = 1 / (q ^ recurrentLoopReward r : ℕ) := by
  4212. rw [recurrentLoopRecordWeight_eq_reciprocal r hpos,
  4213. recurrentLoopRecord_prod_eq_q_pow]
  4214. /-! ### Recovering the number of embedded visits from the flat path -/
  4215. namespace FiniteChoiceChain
  4216. def listVisitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  4217. (K : FiniteChoiceChain S Ω) (C : Finset S) : S → List Ω → ℕ
  4218. | _, [] => 0
  4219. | x, a :: w => (if K.next x a ∈ C then 1 else 0) +
  4220. K.listVisitCount C (K.next x a) w
  4221. theorem listVisitCount_append {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  4222. (K : FiniteChoiceChain S Ω) (C : Finset S) (x : S)
  4223. (u v : List Ω) : K.listVisitCount C x (u ++ v) =
  4224. K.listVisitCount C x u +
  4225. K.listVisitCount C (K.listEndpoint x u) v := by
  4226. induction u generalizing x with
  4227. | nil => simp [listVisitCount, listEndpoint]
  4228. | cons a u ih =>
  4229. simp only [List.cons_append, listVisitCount, listEndpoint, ih]
  4230. omega
  4231. theorem isHitList_visitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  4232. (K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)
  4233. (h : K.IsHitList C y x w) :
  4234. K.listVisitCount C x w = if x ∈ C then 0 else 1 := by
  4235. cases w with
  4236. | nil => simp [listVisitCount, h.1]
  4237. | cons a w =>
  4238. have hx : x ∉ C := h.1
  4239. have ih := K.isHitList_visitCount C y (K.next x a) w h.2
  4240. simp only [listVisitCount, hx, if_false]
  4241. by_cases hn : K.next x a ∈ C <;> simp [hn] at ih ⊢ <;> omega
  4242. termination_by w.length
  4243. theorem isReturnList_visitCount {S Ω : Type*} [Fintype Ω] [DecidableEq S]
  4244. (K : FiniteChoiceChain S Ω) (C : Finset S) (y x : S) (w : List Ω)
  4245. (h : K.IsReturnList C y x w) : K.listVisitCount C x w = 1 := by
  4246. cases w with
  4247. | nil => exact h.elim
  4248. | cons a w =>
  4249. have ih := K.isHitList_visitCount C y (K.next x a) w h
  4250. simp only [listVisitCount]
  4251. by_cases hn : K.next x a ∈ C <;> simp [hn] at ih ⊢ <;> omega
  4252. end FiniteChoiceChain
  4253. theorem holdingHitRecord_visitCount {k n : ℕ} {x : EmbeddedState}
  4254. (r : HoldingHitRecord k n x) : controlledChain.listVisitCount controlSet
  4255. x.1.1 (holdingHitRecordChoices (k := k) (n := n) (x := x) r) = k := by
  4256. induction k generalizing n x with
  4257. | zero => simp [holdingHitRecordChoices,
  4258. FiniteChoiceChain.listVisitCount]
  4259. | succ k ih =>
  4260. obtain ⟨guard, p, y, w, tail⟩ := r
  4261. rw [holdingHitRecordChoices, controlledChain.listVisitCount_append]
  4262. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4263. p.1.1 x.1.1 w.1 w.2
  4264. rw [controlledChain.isReturnList_visitCount controlSet y.1.1 x.1.1
  4265. (List.ofFn w.1) hw]
  4266. rw [controlledChain.isReturnList_endpoint controlSet y.1.1 x.1.1
  4267. (List.ofFn w.1) hw, ih tail]
  4268. omega
  4269. theorem recurrentLoopRecord_visitCount {k n : ℕ}
  4270. (r : RecurrentLoopRecord k n) : controlledChain.listVisitCount controlSet
  4271. recurrentAugmentedState
  4272. (recurrentLoopRecordChoices (k := k) (n := n) r) = k := by
  4273. cases k with
  4274. | zero => exact r.elim
  4275. | succ k =>
  4276. obtain ⟨p, y, w, tail⟩ := r
  4277. rw [recurrentLoopRecordChoices, controlledChain.listVisitCount_append]
  4278. have hw := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4279. p.1.1 recurrentAugmentedState w.1 w.2
  4280. rw [controlledChain.isReturnList_visitCount controlSet y.1.1
  4281. recurrentAugmentedState (List.ofFn w.1) hw]
  4282. rw [controlledChain.isReturnList_endpoint controlSet y.1.1
  4283. recurrentAugmentedState (List.ofFn w.1) hw,
  4284. holdingHitRecord_visitCount tail]
  4285. omega
  4286. /-- A loop record is determined globally by its flat choice list, including
  4287. its two duration indices. -/
  4288. theorem recurrentLoopRecord_sigma_choices_injective :
  4289. Function.Injective (fun r : Σ k : ℕ, Σ n : ℕ, RecurrentLoopRecord k n ↦
  4290. recurrentLoopRecordChoices (k := r.1) (n := r.2.1) r.2.2) := by
  4291. intro r s hrs
  4292. obtain ⟨k, n, r⟩ := r
  4293. obtain ⟨l, m, s⟩ := s
  4294. have hnm : n = m := by
  4295. simpa using congrArg List.length hrs
  4296. subst m
  4297. have hkl : k = l := by
  4298. have hr := recurrentLoopRecord_visitCount r
  4299. have hs := recurrentLoopRecord_visitCount s
  4300. change recurrentLoopRecordChoices r = recurrentLoopRecordChoices s at hrs
  4301. rw [hrs] at hr
  4302. omega
  4303. subst l
  4304. have hrs' : r = s := recurrentLoopRecordChoices_injective k n hrs
  4305. subst s
  4306. rfl
  4307. /-! ### First-return status with respect to the recurrent base itself -/
  4308. namespace FiniteChoiceChain
  4309. theorem isHitList_append_of_subset {S Ω : Type*} [Fintype Ω]
  4310. [DecidableEq S] (K : FiniteChoiceChain S Ω) {B C : Finset S}
  4311. (hBC : B ⊆ C) {x y z : S} {u v : List Ω}
  4312. (hu : K.IsHitList C y x u) (hv : K.IsHitList B z y v) :
  4313. K.IsHitList B z x (u ++ v) := by
  4314. induction u generalizing x with
  4315. | nil =>
  4316. have hxy := hu.2
  4317. subst x
  4318. exact hv
  4319. | cons a u ih =>
  4320. have hxB : x ∉ B := fun hx ↦ hu.1 (hBC hx)
  4321. exact ⟨hxB, ih hu.2⟩
  4322. theorem isReturnList_append_hit_of_subset {S Ω : Type*} [Fintype Ω]
  4323. [DecidableEq S] (K : FiniteChoiceChain S Ω) {B C : Finset S}
  4324. (hBC : B ⊆ C) {x y z : S} {u v : List Ω}
  4325. (hu : K.IsReturnList C y x u) (hv : K.IsHitList B z y v) :
  4326. K.IsReturnList B z x (u ++ v) := by
  4327. cases u with
  4328. | nil => exact hu.elim
  4329. | cons a u =>
  4330. exact K.isHitList_append_of_subset hBC hu hv
  4331. theorem isHitList_of_returnList_of_not_mem {S Ω : Type*} [Fintype Ω]
  4332. [DecidableEq S] (K : FiniteChoiceChain S Ω) (B : Finset S)
  4333. {x y : S} {u : List Ω} (hx : x ∉ B) (hu : K.IsReturnList B y x u) :
  4334. K.IsHitList B y x u := by
  4335. cases u with
  4336. | nil => exact hu.elim
  4337. | cons a u => exact ⟨hx, hu⟩
  4338. end FiniteChoiceChain
  4339. theorem holdingHitRecord_isHitBase {k n : ℕ} {x : EmbeddedState}
  4340. (r : HoldingHitRecord k n x) : controlledChain.IsHitList
  4341. {recurrentAugmentedState} recurrentAugmentedState x.1.1
  4342. (holdingHitRecordChoices (k := k) (n := n) (x := x) r) := by
  4343. induction k generalizing n x with
  4344. | zero =>
  4345. simp only [HoldingHitRecord] at r
  4346. have hn := r.down.2.1
  4347. have hx := r.down.2.2
  4348. subst n
  4349. subst x
  4350. change recurrentAugmentedState ∈ ({recurrentAugmentedState} :
  4351. Finset AugmentedState) ∧ recurrentAugmentedState = recurrentAugmentedState
  4352. simp
  4353. | succ k ih =>
  4354. obtain ⟨guard, p, y, w, tail⟩ := r
  4355. have hseg := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4356. p.1.1 x.1.1 w.1 w.2
  4357. have hsub : ({recurrentAugmentedState} : Finset AugmentedState) ⊆
  4358. controlSet := by
  4359. intro z hz
  4360. simp only [Finset.mem_singleton] at hz
  4361. subst z
  4362. exact recurrentAugmentedState_mem
  4363. have hreturn := controlledChain.isReturnList_append_hit_of_subset hsub
  4364. hseg (ih tail)
  4365. have hx : x.1.1 ∉ ({recurrentAugmentedState} : Finset AugmentedState) := by
  4366. simp only [Finset.mem_singleton]
  4367. intro heq
  4368. apply guard.2
  4369. apply Subtype.ext
  4370. apply Subtype.ext
  4371. exact heq
  4372. exact controlledChain.isHitList_of_returnList_of_not_mem
  4373. {recurrentAugmentedState} hx hreturn
  4374. theorem recurrentLoopRecord_isReturnBase {k n : ℕ}
  4375. (r : RecurrentLoopRecord k n) : controlledChain.IsReturnList
  4376. {recurrentAugmentedState} recurrentAugmentedState recurrentAugmentedState
  4377. (recurrentLoopRecordChoices (k := k) (n := n) r) := by
  4378. cases k with
  4379. | zero => exact r.elim
  4380. | succ k =>
  4381. obtain ⟨p, y, w, tail⟩ := r
  4382. have hseg := controlledChain.isReturnList_of_isReturnPath controlSet y.1.1
  4383. p.1.1 recurrentAugmentedState w.1 w.2
  4384. have hsub : ({recurrentAugmentedState} : Finset AugmentedState) ⊆
  4385. controlSet := by
  4386. intro z hz
  4387. simp only [Finset.mem_singleton] at hz
  4388. subst z
  4389. exact recurrentAugmentedState_mem
  4390. exact controlledChain.isReturnList_append_hit_of_subset hsub hseg
  4391. (holdingHitRecord_isHitBase tail)
  4392. /-! ## The first-return reward distribution -/
  4393. abbrev FirstReturnLoop := Σ k : ℕ, Σ n : ℕ, RecurrentLoopRecord k n
  4394. noncomputable def firstReturnLoopWeight (r : FirstReturnLoop) : ℝ :=
  4395. recurrentLoopRecordWeight r.2.2
  4396. noncomputable def firstReturnLoopWord (r : FirstReturnLoop) : List ℕ :=
  4397. recurrentLoopRecordWord r.2.2
  4398. noncomputable def firstReturnLoopReward (r : FirstReturnLoop) : ℕ :=
  4399. recurrentLoopReward r.2.2
  4400. theorem firstReturnLoopWeight_nonneg (r : FirstReturnLoop) :
  4401. 0 ≤ firstReturnLoopWeight r :=
  4402. recurrentLoopRecordWeight_nonneg r.2.2
  4403. theorem firstReturnLoopReward_pos (r : FirstReturnLoop) :
  4404. 0 < firstReturnLoopReward r := recurrentLoopReward_pos r.2.2
  4405. theorem firstReturnLoopWeight_eq_q_reward (r : FirstReturnLoop)
  4406. (hpos : 0 < firstReturnLoopWeight r) :
  4407. firstReturnLoopWeight r = 1 / (q ^ firstReturnLoopReward r : ℕ) :=
  4408. recurrentLoopRecordWeight_eq_q_reward r.2.2 hpos
  4409. theorem firstReturnLoopWord_mem_multipliers (r : FirstReturnLoop) :
  4410. ∀ a ∈ firstReturnLoopWord r, a ∈ multipliers :=
  4411. recurrentLoopRecordWord_mem_multipliers r.2.2
  4412. theorem firstReturnLoopWord_length (r : FirstReturnLoop) :
  4413. (firstReturnLoopWord r).length = controlBlockLength * r.2.1 :=
  4414. recurrentLoopRecordWord_length r.2.2
  4415. theorem firstReturnLoopWeight_summable : Summable firstReturnLoopWeight := by
  4416. have hk (k : ℕ) : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦
  4417. recurrentLoopRecordWeight z.2 := by
  4418. rw [summable_sigma_of_nonneg fun z ↦ recurrentLoopRecordWeight_nonneg z.2]
  4419. constructor
  4420. · intro n
  4421. exact Summable.of_finite
  4422. · have heq : (fun n ↦ ∑' r : RecurrentLoopRecord k n,
  4423. recurrentLoopRecordWeight r) = fun n ↦ holdingReturn k n := by
  4424. funext n
  4425. rw [tsum_fintype, sum_recurrentLoopRecordWeight]
  4426. rw [heq]
  4427. exact (holdingReturn_summable_and_mass k).1
  4428. rw [summable_sigma_of_nonneg firstReturnLoopWeight_nonneg]
  4429. constructor
  4430. · exact hk
  4431. · have heq : (fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,
  4432. recurrentLoopRecordWeight z.2) =
  4433. fun k ↦ EmbeddedChain.returnHitTotal {recurrentControlState} k
  4434. recurrentControlState := by
  4435. funext k
  4436. rw [(hk k).tsum_sigma]
  4437. simp_rw [tsum_fintype, sum_recurrentLoopRecordWeight,
  4438. (holdingReturn_summable_and_mass k).2,
  4439. embedded_returnAt_eq_returnHitTotal]
  4440. change Summable fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,
  4441. recurrentLoopRecordWeight z.2
  4442. rw [heq]
  4443. exact recurrent_visit_return_total.summable
  4444. theorem tsum_firstReturnLoopWeight : (∑' r, firstReturnLoopWeight r) = 1 := by
  4445. have hk (k : ℕ) : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦
  4446. recurrentLoopRecordWeight z.2 := by
  4447. rw [summable_sigma_of_nonneg fun z ↦ recurrentLoopRecordWeight_nonneg z.2]
  4448. constructor
  4449. · intro n
  4450. exact Summable.of_finite
  4451. · simpa only [tsum_fintype, sum_recurrentLoopRecordWeight] using
  4452. (holdingReturn_summable_and_mass k).1
  4453. rw [firstReturnLoopWeight_summable.tsum_sigma]
  4454. change (∑' k, ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,
  4455. recurrentLoopRecordWeight z.2) = 1
  4456. simp_rw [(hk _).tsum_sigma, tsum_fintype, sum_recurrentLoopRecordWeight,
  4457. (holdingReturn_summable_and_mass _).2,
  4458. embedded_returnAt_eq_returnHitTotal]
  4459. exact recurrent_visit_return_total.tsum_eq
  4460. theorem weighted_firstReturnLoopDuration_summable :
  4461. Summable fun r : FirstReturnLoop ↦
  4462. (r.2.1 : ℝ) * firstReturnLoopWeight r := by
  4463. rw [summable_sigma_of_nonneg fun r ↦ mul_nonneg (Nat.cast_nonneg r.2.1)
  4464. (firstReturnLoopWeight_nonneg r)]
  4465. constructor
  4466. · intro k
  4467. change Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦
  4468. (z.1 : ℝ) * recurrentLoopRecordWeight z.2
  4469. rw [summable_sigma_of_nonneg fun z ↦ mul_nonneg (Nat.cast_nonneg z.1)
  4470. (recurrentLoopRecordWeight_nonneg z.2)]
  4471. constructor
  4472. · intro n
  4473. exact Summable.of_finite
  4474. · have heq : (fun n ↦ ∑' r : RecurrentLoopRecord k n,
  4475. (n : ℝ) * recurrentLoopRecordWeight r) =
  4476. fun n : ℕ ↦ (n : ℝ) * holdingReturn k n := by
  4477. funext n
  4478. rw [tsum_fintype, ← Finset.mul_sum, sum_recurrentLoopRecordWeight]
  4479. rw [heq]
  4480. exact holdingReturn_weighted_summable k
  4481. · have heq : (fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,
  4482. (z.1 : ℝ) * recurrentLoopRecordWeight z.2) = holdingReturnMean := by
  4483. funext k
  4484. have hk : Summable fun z : Σ n : ℕ, RecurrentLoopRecord k n ↦
  4485. (z.1 : ℝ) * recurrentLoopRecordWeight z.2 := by
  4486. rw [summable_sigma_of_nonneg fun z ↦ mul_nonneg (Nat.cast_nonneg z.1)
  4487. (recurrentLoopRecordWeight_nonneg z.2)]
  4488. constructor
  4489. · intro n
  4490. exact Summable.of_finite
  4491. · simpa only [tsum_fintype, ← Finset.mul_sum,
  4492. sum_recurrentLoopRecordWeight] using holdingReturn_weighted_summable k
  4493. rw [hk.tsum_sigma]
  4494. simp_rw [tsum_fintype, ← Finset.mul_sum, sum_recurrentLoopRecordWeight]
  4495. rfl
  4496. change Summable fun k ↦ ∑' z : Σ n : ℕ, RecurrentLoopRecord k n,
  4497. (z.1 : ℝ) * recurrentLoopRecordWeight z.2
  4498. rw [heq]
  4499. exact holdingReturnMean_summable
  4500. theorem primeIncrement_seven_le_one {a : ℕ} (ha : a ∈ multipliers) :
  4501. primeIncrement a 3 ≤ 1 := by
  4502. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  4503. rcases ha with rfl | rfl | rfl | rfl | rfl <;> decide
  4504. theorem exponent_seven_le_length (w : List ℕ)
  4505. (hw : ∀ a ∈ w, a ∈ multipliers) : exponents w 3 ≤ w.length := by
  4506. induction w with
  4507. | nil => simp
  4508. | cons a w ih =>
  4509. have ha := primeIncrement_seven_le_one (hw a (by simp))
  4510. have htail : ∀ b ∈ w, b ∈ multipliers := by
  4511. intro b hb
  4512. exact hw b (by simp [hb])
  4513. simp only [exponents_cons, List.length_cons]
  4514. have ht := ih htail
  4515. omega
  4516. theorem firstReturnLoopReward_le_duration (r : FirstReturnLoop) :
  4517. firstReturnLoopReward r ≤ controlBlockLength * r.2.1 := by
  4518. calc
  4519. firstReturnLoopReward r ≤ (firstReturnLoopWord r).length :=
  4520. exponent_seven_le_length _ (firstReturnLoopWord_mem_multipliers r)
  4521. _ = controlBlockLength * r.2.1 := firstReturnLoopWord_length r
  4522. theorem weighted_firstReturnLoopReward_summable :
  4523. Summable fun r : FirstReturnLoop ↦
  4524. (firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r := by
  4525. apply Summable.of_nonneg_of_le
  4526. (fun r ↦ mul_nonneg (Nat.cast_nonneg _) (firstReturnLoopWeight_nonneg r))
  4527. (fun r ↦ by
  4528. have hcast : (firstReturnLoopReward r : ℝ) ≤
  4529. (controlBlockLength * r.2.1 : ℕ) := by
  4530. exact_mod_cast firstReturnLoopReward_le_duration r
  4531. exact mul_le_mul_of_nonneg_right hcast
  4532. (firstReturnLoopWeight_nonneg r))
  4533. simpa only [Nat.cast_mul, mul_assoc] using
  4534. weighted_firstReturnLoopDuration_summable.mul_left
  4535. (controlBlockLength : ℝ)
  4536. theorem firstReturnLoopWord_injective_of_pos :
  4537. Set.InjOn firstReturnLoopWord {r | 0 < firstReturnLoopWeight r} := by
  4538. intro r hr s hs hword
  4539. obtain ⟨k, n, r⟩ := r
  4540. obtain ⟨l, m, s⟩ := s
  4541. have hnm : n = m := by
  4542. have hlen := congrArg List.length hword
  4543. simp only [firstReturnLoopWord, recurrentLoopRecordWord_length] at hlen
  4544. have hN := controlBlockLength_pos
  4545. exact Nat.mul_left_cancel hN hlen
  4546. subst m
  4547. have htuple : recurrentLoopRecordTuple r = recurrentLoopRecordTuple s := by
  4548. apply controlledPathWord_injective_of_return n recurrentAugmentedState
  4549. · rwa [← recurrentLoopRecordWeight_eq_pathWeight]
  4550. · rwa [← recurrentLoopRecordWeight_eq_pathWeight]
  4551. · exact recurrentLoopRecord_pathEndpoint r
  4552. · exact recurrentLoopRecord_pathEndpoint s
  4553. · simpa only [firstReturnLoopWord,
  4554. recurrentLoopRecordWord_eq_controlledPathWord] using hword
  4555. have hchoices : recurrentLoopRecordChoices r = recurrentLoopRecordChoices s := by
  4556. rw [← ofFn_recurrentLoopRecordTuple, ← ofFn_recurrentLoopRecordTuple,
  4557. htuple]
  4558. exact recurrentLoopRecord_sigma_choices_injective hchoices
  4559. noncomputable def loopRewardMass (m : ℕ) : ℝ :=
  4560. ∑' r : FirstReturnLoop,
  4561. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)
  4562. theorem selectedWeight_nonneg {P : Prop} {x : ℝ} (hx : 0 ≤ x) :
  4563. 0 ≤ selectedWeight P x := by
  4564. classical
  4565. by_cases hP : P <;> simp [selectedWeight, hP, hx]
  4566. theorem selectedWeight_le {P : Prop} {x : ℝ} (hx : 0 ≤ x) :
  4567. selectedWeight P x ≤ x := by
  4568. classical
  4569. by_cases hP : P <;> simp [selectedWeight, hP, hx]
  4570. theorem loopRewardMass_nonneg (m : ℕ) : 0 ≤ loopRewardMass m :=
  4571. tsum_nonneg fun r ↦ selectedWeight_nonneg (firstReturnLoopWeight_nonneg r)
  4572. theorem rewardIndicator_hasSum (r : FirstReturnLoop) :
  4573. HasSum (fun m ↦ selectedWeight (firstReturnLoopReward r = m)
  4574. (firstReturnLoopWeight r)) (firstReturnLoopWeight r) := by
  4575. simpa [selectedWeight, eq_comm] using
  4576. (hasSum_ite_eq (firstReturnLoopReward r) (firstReturnLoopWeight r))
  4577. theorem loopRewardMass_summable : Summable loopRewardMass := by
  4578. have hpair : Summable fun p : FirstReturnLoop × ℕ ↦
  4579. selectedWeight (firstReturnLoopReward p.1 = p.2)
  4580. (firstReturnLoopWeight p.1) := by
  4581. rw [summable_prod_of_nonneg fun p ↦
  4582. selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1)]
  4583. constructor
  4584. · exact fun r ↦ (rewardIndicator_hasSum r).summable
  4585. · have heq : (fun r ↦ ∑' m, selectedWeight
  4586. (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =
  4587. firstReturnLoopWeight := by
  4588. funext r
  4589. exact (rewardIndicator_hasSum r).tsum_eq
  4590. rw [heq]
  4591. exact firstReturnLoopWeight_summable
  4592. have hswap : Summable fun p : ℕ × FirstReturnLoop ↦
  4593. selectedWeight (firstReturnLoopReward p.2 = p.1)
  4594. (firstReturnLoopWeight p.2) := by
  4595. simpa [Function.comp_def] using hpair.comp_injective
  4596. (show Function.Injective
  4597. (fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) by
  4598. exact (Equiv.prodComm ℕ FirstReturnLoop).injective)
  4599. rw [summable_prod_of_nonneg fun p ↦
  4600. selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.2)] at hswap
  4601. change Summable fun m ↦
  4602. ∑' r : FirstReturnLoop,
  4603. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)
  4604. exact hswap.2
  4605. theorem tsum_loopRewardMass : (∑' m, loopRewardMass m) = 1 := by
  4606. have hpair : Summable fun p : FirstReturnLoop × ℕ ↦
  4607. selectedWeight (firstReturnLoopReward p.1 = p.2)
  4608. (firstReturnLoopWeight p.1) := by
  4609. rw [summable_prod_of_nonneg fun p ↦
  4610. selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1)]
  4611. exact ⟨fun r ↦ (rewardIndicator_hasSum r).summable, by
  4612. simpa only [(rewardIndicator_hasSum _).tsum_eq] using
  4613. firstReturnLoopWeight_summable⟩
  4614. have hswap : Summable fun p : ℕ × FirstReturnLoop ↦
  4615. selectedWeight (firstReturnLoopReward p.2 = p.1)
  4616. (firstReturnLoopWeight p.2) := by
  4617. simpa [Function.comp_def] using hpair.comp_injective
  4618. (show Function.Injective
  4619. (fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) by
  4620. exact (Equiv.prodComm ℕ FirstReturnLoop).injective)
  4621. calc
  4622. (∑' m, loopRewardMass m) = ∑' p : ℕ × FirstReturnLoop,
  4623. selectedWeight (firstReturnLoopReward p.2 = p.1)
  4624. (firstReturnLoopWeight p.2) := by
  4625. rw [hswap.tsum_prod]
  4626. rfl
  4627. _ = ∑' p : FirstReturnLoop × ℕ,
  4628. selectedWeight (firstReturnLoopReward p.1 = p.2)
  4629. (firstReturnLoopWeight p.1) :=
  4630. ((Equiv.prodComm FirstReturnLoop ℕ).tsum_eq _).symm
  4631. _ = ∑' r, ∑' m, selectedWeight (firstReturnLoopReward r = m)
  4632. (firstReturnLoopWeight r) := hpair.tsum_prod
  4633. _ = ∑' r, firstReturnLoopWeight r := by
  4634. apply tsum_congr
  4635. exact fun r ↦ (rewardIndicator_hasSum r).tsum_eq
  4636. _ = 1 := tsum_firstReturnLoopWeight
  4637. theorem weightedRewardIndicator_hasSum (r : FirstReturnLoop) :
  4638. HasSum (fun m : ℕ ↦ (m : ℝ) *
  4639. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r))
  4640. ((firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r) := by
  4641. have h := hasSum_ite_eq (firstReturnLoopReward r)
  4642. ((firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r)
  4643. have hfun : (fun m : ℕ ↦ (m : ℝ) *
  4644. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =
  4645. fun m ↦ if m = firstReturnLoopReward r then
  4646. (firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r else 0 := by
  4647. funext m
  4648. by_cases hm : firstReturnLoopReward r = m
  4649. · subst m
  4650. simp [selectedWeight]
  4651. · simp [selectedWeight, hm, Ne.symm hm]
  4652. rw [hfun]
  4653. exact h
  4654. theorem weighted_loopRewardMass_summable :
  4655. Summable fun m : ℕ ↦ (m : ℝ) * loopRewardMass m := by
  4656. have hpair : Summable fun p : FirstReturnLoop × ℕ ↦ (p.2 : ℝ) *
  4657. selectedWeight (firstReturnLoopReward p.1 = p.2)
  4658. (firstReturnLoopWeight p.1) := by
  4659. rw [summable_prod_of_nonneg fun p ↦ mul_nonneg (Nat.cast_nonneg p.2)
  4660. (selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.1))]
  4661. constructor
  4662. · exact fun r ↦ (weightedRewardIndicator_hasSum r).summable
  4663. · have heq : (fun r ↦ ∑' m : ℕ, (m : ℝ) * selectedWeight
  4664. (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =
  4665. fun r ↦ (firstReturnLoopReward r : ℝ) * firstReturnLoopWeight r := by
  4666. funext r
  4667. exact (weightedRewardIndicator_hasSum r).tsum_eq
  4668. rw [heq]
  4669. exact weighted_firstReturnLoopReward_summable
  4670. have hswap : Summable fun p : ℕ × FirstReturnLoop ↦ (p.1 : ℝ) *
  4671. selectedWeight (firstReturnLoopReward p.2 = p.1)
  4672. (firstReturnLoopWeight p.2) := by
  4673. simpa [Function.comp_def] using hpair.comp_injective
  4674. (show Function.Injective
  4675. (fun p : ℕ × FirstReturnLoop ↦ (p.2, p.1)) by
  4676. exact (Equiv.prodComm ℕ FirstReturnLoop).injective)
  4677. rw [summable_prod_of_nonneg fun p ↦ mul_nonneg (Nat.cast_nonneg p.1)
  4678. (selectedWeight_nonneg (firstReturnLoopWeight_nonneg p.2))] at hswap
  4679. have hslices (m : ℕ) : Summable fun r : FirstReturnLoop ↦
  4680. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r) :=
  4681. Summable.of_nonneg_of_le
  4682. (fun r ↦ selectedWeight_nonneg (firstReturnLoopWeight_nonneg r))
  4683. (fun r ↦ selectedWeight_le (firstReturnLoopWeight_nonneg r))
  4684. firstReturnLoopWeight_summable
  4685. have heq : (fun m : ℕ ↦ (m : ℝ) * loopRewardMass m) =
  4686. fun m : ℕ ↦ ∑' r : FirstReturnLoop, (m : ℝ) *
  4687. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r) := by
  4688. funext m
  4689. exact ((hslices m).tsum_mul_left (m : ℝ)).symm
  4690. rw [heq]
  4691. exact hswap.2
  4692. theorem loopRewardMass_zero : loopRewardMass 0 = 0 := by
  4693. rw [loopRewardMass]
  4694. calc
  4695. (∑' r : FirstReturnLoop,
  4696. selectedWeight (firstReturnLoopReward r = 0) (firstReturnLoopWeight r)) =
  4697. ∑' _r : FirstReturnLoop, (0 : ℝ) := by
  4698. apply tsum_congr
  4699. intro r
  4700. have hr := firstReturnLoopReward_pos r
  4701. simp [selectedWeight, hr.ne']
  4702. _ = 0 := tsum_zero
  4703. /-! ### Finiteness and the exact counting interpretation -/
  4704. theorem one_le_primeIncrement_sum {a : ℕ} (ha : a ∈ multipliers) :
  4705. 1 ≤ primeIncrement a 0 + primeIncrement a 1 +
  4706. primeIncrement a 2 + primeIncrement a 3 := by
  4707. simp only [multipliers, Finset.mem_insert, Finset.mem_singleton] at ha
  4708. rcases ha with rfl | rfl | rfl | rfl | rfl <;> decide
  4709. theorem length_le_exponent_sum (w : List ℕ)
  4710. (hw : ∀ a ∈ w, a ∈ multipliers) :
  4711. w.length ≤ exponents w 0 + exponents w 1 + exponents w 2 + exponents w 3 := by
  4712. induction w with
  4713. | nil => simp
  4714. | cons a w ih =>
  4715. have ha := one_le_primeIncrement_sum (hw a (by simp))
  4716. have htail : ∀ b ∈ w, b ∈ multipliers := by
  4717. intro b hb
  4718. exact hw b (by simp [hb])
  4719. simp only [List.length_cons, exponents_cons]
  4720. have ht := ih htail
  4721. omega
  4722. theorem firstReturnLoopWord_length_le (r : FirstReturnLoop) :
  4723. (firstReturnLoopWord r).length ≤ 69 * firstReturnLoopReward r := by
  4724. have hlen := length_le_exponent_sum (firstReturnLoopWord r)
  4725. (firstReturnLoopWord_mem_multipliers r)
  4726. have hzero := recurrentLoopRecord_imbalance_zero r.2.2
  4727. have heq := exponents_eq_of_imbalance_eq_zero hzero
  4728. rcases heq with ⟨h0, h1, h2⟩
  4729. dsimp only [firstReturnLoopWord, firstReturnLoopReward,
  4730. recurrentLoopReward] at hlen h0 h1 h2 ⊢
  4731. omega
  4732. abbrev AllowedMultiplier := {a : ℕ // a ∈ multipliers}
  4733. abbrev BoundedMultiplierWord (L : ℕ) :=
  4734. Σ n : Fin (L + 1), Fin n → AllowedMultiplier
  4735. noncomputable def boundedMultiplierWordCode (L : ℕ) (w : List ℕ)
  4736. (hL : w.length ≤ L) (hw : ∀ a ∈ w, a ∈ multipliers) :
  4737. BoundedMultiplierWord L :=
  4738. ⟨⟨w.length, by omega⟩, fun i ↦ ⟨w.get i, hw _ (List.get_mem w i)⟩⟩
  4739. def decodeBoundedMultiplierWord {L : ℕ} (c : BoundedMultiplierWord L) : List ℕ :=
  4740. List.ofFn fun i ↦ (c.2 i).1
  4741. @[simp] theorem decode_boundedMultiplierWordCode (L : ℕ) (w : List ℕ)
  4742. (hL : w.length ≤ L) (hw : ∀ a ∈ w, a ∈ multipliers) :
  4743. decodeBoundedMultiplierWord (boundedMultiplierWordCode L w hL hw) = w := by
  4744. exact List.ofFn_get w
  4745. abbrev PositiveLoopOfReward (m : ℕ) :=
  4746. {r : FirstReturnLoop // firstReturnLoopReward r = m ∧
  4747. 0 < firstReturnLoopWeight r}
  4748. noncomputable instance positiveLoopOfRewardFintype (m : ℕ) :
  4749. Fintype (PositiveLoopOfReward m) := by
  4750. let encode : PositiveLoopOfReward m → BoundedMultiplierWord (69 * m) :=
  4751. fun r ↦ boundedMultiplierWordCode (69 * m) (firstReturnLoopWord r.1)
  4752. (by simpa [r.2.1] using firstReturnLoopWord_length_le r.1)
  4753. (firstReturnLoopWord_mem_multipliers r.1)
  4754. apply Fintype.ofInjective encode
  4755. intro r s hrs
  4756. apply Subtype.ext
  4757. apply firstReturnLoopWord_injective_of_pos r.2.2 s.2.2
  4758. have hdecode := congrArg decodeBoundedMultiplierWord hrs
  4759. simpa [encode] using hdecode
  4760. noncomputable def firstReturnLoopCount (m : ℕ) : ℕ :=
  4761. Fintype.card (PositiveLoopOfReward m)
  4762. theorem loopRewardMass_eq_count (m : ℕ) :
  4763. loopRewardMass m = (firstReturnLoopCount m : ℝ) / (q ^ m : ℕ) := by
  4764. have hsub : loopRewardMass m =
  4765. ∑' r : PositiveLoopOfReward m, firstReturnLoopWeight r.1 := by
  4766. rw [loopRewardMass]
  4767. calc
  4768. (∑' r : FirstReturnLoop,
  4769. selectedWeight (firstReturnLoopReward r = m) (firstReturnLoopWeight r)) =
  4770. ∑' r : FirstReturnLoop,
  4771. ({r | firstReturnLoopReward r = m ∧ 0 < firstReturnLoopWeight r} :
  4772. Set FirstReturnLoop).indicator firstReturnLoopWeight r := by
  4773. apply tsum_congr
  4774. intro r
  4775. by_cases hr : firstReturnLoopReward r = m
  4776. · by_cases hp : 0 < firstReturnLoopWeight r
  4777. · simp [selectedWeight, hr, hp]
  4778. · have hz : firstReturnLoopWeight r = 0 :=
  4779. le_antisymm (le_of_not_gt hp) (firstReturnLoopWeight_nonneg r)
  4780. simp [selectedWeight, hr, hz]
  4781. · simp [selectedWeight, hr]
  4782. _ = ∑' r : PositiveLoopOfReward m, firstReturnLoopWeight r.1 :=
  4783. (tsum_subtype
  4784. {r : FirstReturnLoop | firstReturnLoopReward r = m ∧
  4785. 0 < firstReturnLoopWeight r} firstReturnLoopWeight).symm
  4786. rw [hsub, tsum_fintype]
  4787. have hterm (r : PositiveLoopOfReward m) :
  4788. firstReturnLoopWeight r.1 = 1 / (q ^ m : ℕ) := by
  4789. rw [firstReturnLoopWeight_eq_q_reward r.1 r.2.2, r.2.1]
  4790. simp_rw [hterm]
  4791. simp [firstReturnLoopCount, div_eq_mul_inv]
  4792. /-! ## An elementary bounded-window renewal lemma
  4793. Instead of importing the arithmetic renewal theorem, we use the simpler fact
  4794. that a positive-integer renewal process crosses every level, and finite mean
  4795. makes a fixed overshoot window carry uniformly positive mass. -/
  4796. section AbstractRenewal
  4797. variable (p : ℕ → ℝ)
  4798. variable (hp0 : ∀ n, 0 ≤ p n)
  4799. variable (hp : Summable p)
  4800. variable (hpMass : (∑' n, p n) = 1)
  4801. variable (hpZero : p 0 = 0)
  4802. noncomputable def distributionTail (d : ℕ) : ℝ :=
  4803. ∑' t, p (t + d)
  4804. include hp0 in
  4805. theorem distributionTail_nonneg (d : ℕ) :
  4806. 0 ≤ distributionTail p d := tsum_nonneg fun t ↦ hp0 (t + d)
  4807. include hp in
  4808. theorem distributionTail_summable (d : ℕ) :
  4809. Summable fun t ↦ p (t + d) := (summable_nat_add_iff d).2 hp
  4810. include hp in
  4811. theorem distributionTail_succ (d : ℕ) :
  4812. distributionTail p d = p d + distributionTail p (d + 1) := by
  4813. rw [distributionTail, (distributionTail_summable p hp d).tsum_eq_zero_add]
  4814. congr 1
  4815. · simp
  4816. apply tsum_congr
  4817. intro t
  4818. congr 1
  4819. omega
  4820. include hp hpMass hpZero in
  4821. theorem distributionTail_one : distributionTail p 1 = 1 := by
  4822. have h := distributionTail_succ p hp (d := 0)
  4823. have hzero : distributionTail p 0 = 1 := by
  4824. rw [distributionTail]
  4825. simpa using hpMass
  4826. rw [hzero, hpZero, zero_add] at h
  4827. exact h.symm
  4828. noncomputable def natAddEquivIci (d : ℕ) : ℕ ≃ {j : ℕ // d ≤ j} where
  4829. toFun t := ⟨d + t, by omega⟩
  4830. invFun j := j.1 - d
  4831. left_inv t := by simp
  4832. right_inv j := by
  4833. apply Subtype.ext
  4834. simp only
  4835. omega
  4836. theorem distributionTail_eq_tsum_ge (d : ℕ) :
  4837. distributionTail p d =
  4838. ∑' j, selectedWeight (d ≤ j) (p j) := by
  4839. calc
  4840. distributionTail p d = ∑' t : ℕ, p (d + t) := by
  4841. apply tsum_congr
  4842. intro t
  4843. rw [Nat.add_comm]
  4844. _ = ∑' j : {j : ℕ // d ≤ j}, p j.1 :=
  4845. (natAddEquivIci d).tsum_eq (fun j : {j : ℕ // d ≤ j} ↦ p j.1)
  4846. _ = ∑' j : ℕ, Set.indicator {j : ℕ | d ≤ j} p j :=
  4847. tsum_subtype {j : ℕ | d ≤ j} p
  4848. _ = ∑' j : ℕ, selectedWeight (d ≤ j) (p j) := by
  4849. apply tsum_congr
  4850. intro j
  4851. by_cases h : d ≤ j <;> simp [selectedWeight, Set.indicator, h]
  4852. include hp in
  4853. theorem distributionTail_split (d L : ℕ) :
  4854. distributionTail p d =
  4855. (∑ j ∈ Finset.range L, p (j + d)) + distributionTail p (d + L) := by
  4856. have h := (distributionTail_summable p hp d).sum_add_tsum_nat_add L
  4857. simp only [distributionTail]
  4858. rw [← h]
  4859. congr 1
  4860. apply tsum_congr
  4861. intro j
  4862. congr 1
  4863. omega
  4864. /-- Renewal mass at a level, including the empty renewal at zero. -/
  4865. noncomputable def renewalMass : ℕ → ℝ
  4866. | 0 => 1
  4867. | n + 1 => ∑ s : Fin (n + 1), renewalMass s * p (n + 1 - s)
  4868. termination_by n => n
  4869. decreasing_by exact s.isLt
  4870. @[simp] theorem renewalMass_zero : renewalMass p 0 = 1 := by
  4871. rw [renewalMass]
  4872. theorem renewalMass_succ (n : ℕ) : renewalMass p (n + 1) =
  4873. ∑ s ∈ Finset.range (n + 1), renewalMass p s * p (n + 1 - s) := by
  4874. rw [renewalMass]
  4875. exact Fin.sum_univ_eq_sum_range
  4876. (fun s ↦ renewalMass p s * p (n + 1 - s)) (n + 1)
  4877. include hp0 in
  4878. theorem renewalMass_nonneg (n : ℕ) : 0 ≤ renewalMass p n := by
  4879. induction n using Nat.strong_induction_on with
  4880. | h n ih =>
  4881. cases n with
  4882. | zero => simp
  4883. | succ n =>
  4884. rw [renewalMass_succ]
  4885. exact Finset.sum_nonneg fun s hs ↦ mul_nonneg
  4886. (ih s (by simpa using hs)) (hp0 _)
  4887. noncomputable def crossingMass (M : ℕ) : ℝ :=
  4888. ∑ s ∈ Finset.range M, renewalMass p s * distributionTail p (M - s)
  4889. include hp hpMass hpZero in
  4890. theorem crossingMass_one {M : ℕ} (hM : 0 < M) :
  4891. crossingMass p M = 1 := by
  4892. induction M, hM using Nat.le_induction with
  4893. | base =>
  4894. simp [crossingMass, distributionTail_one p hp hpMass hpZero]
  4895. | succ M hM ih =>
  4896. have hMpos : 0 < M := by omega
  4897. have htail (s : ℕ) (hs : s ∈ Finset.range M) :
  4898. distributionTail p (M - s) =
  4899. p (M - s) + distributionTail p (M + 1 - s) := by
  4900. rw [distributionTail_succ p hp]
  4901. simp only [Finset.mem_range] at hs
  4902. rw [show M - s + 1 = M + 1 - s by omega]
  4903. rw [crossingMass, Finset.sum_range_succ]
  4904. simp only [Nat.add_sub_cancel_left]
  4905. rw [distributionTail_one p hp hpMass hpZero, mul_one]
  4906. have hrenew := renewalMass_succ p M
  4907. have hsplit : crossingMass p M =
  4908. (∑ s ∈ Finset.range M, renewalMass p s * p (M - s)) +
  4909. ∑ s ∈ Finset.range M,
  4910. renewalMass p s * distributionTail p (M + 1 - s) := by
  4911. rw [crossingMass, ← Finset.sum_add_distrib]
  4912. apply Finset.sum_congr rfl
  4913. intro s hs
  4914. rw [htail s hs, mul_add]
  4915. rw [ih] at hsplit
  4916. rw [show renewalMass p M =
  4917. ∑ s ∈ Finset.range M, renewalMass p s * p (M - s) by
  4918. cases M with
  4919. | zero => omega
  4920. | succ m => exact renewalMass_succ p m]
  4921. linarith
  4922. include hp0 hp hpMass hpZero in
  4923. theorem renewalMass_le_one (n : ℕ) : renewalMass p n ≤ 1 := by
  4924. cases n with
  4925. | zero => simp
  4926. | succ n =>
  4927. rw [renewalMass_succ]
  4928. have hterm (s : ℕ) (hs : s ∈ Finset.range (n + 1)) :
  4929. renewalMass p s * p (n + 1 - s) ≤
  4930. renewalMass p s * distributionTail p (n + 1 - s) := by
  4931. apply mul_le_mul_of_nonneg_left _ (renewalMass_nonneg p hp0 s)
  4932. have ht := distributionTail_succ p hp (n + 1 - s)
  4933. linarith [distributionTail_nonneg p hp0 (n + 1 - s + 1)]
  4934. calc
  4935. _ ≤ ∑ s ∈ Finset.range (n + 1),
  4936. renewalMass p s * distributionTail p (n + 1 - s) :=
  4937. Finset.sum_le_sum hterm
  4938. _ = crossingMass p (n + 1) := rfl
  4939. _ = 1 := crossingMass_one p hp hpMass hpZero (by omega)
  4940. noncomputable def goodCrossingMass (M K : ℕ) : ℝ :=
  4941. ∑ s ∈ Finset.range M, renewalMass p s *
  4942. ∑ j ∈ Finset.range (K + 1), p (j + (M - s))
  4943. noncomputable def badCrossingMass (M K : ℕ) : ℝ :=
  4944. ∑ s ∈ Finset.range M, renewalMass p s *
  4945. distributionTail p (M + K + 1 - s)
  4946. include hp in
  4947. theorem good_add_bad_eq_crossing (M K : ℕ) :
  4948. goodCrossingMass p M K + badCrossingMass p M K = crossingMass p M := by
  4949. rw [goodCrossingMass, badCrossingMass, crossingMass,
  4950. ← Finset.sum_add_distrib]
  4951. apply Finset.sum_congr rfl
  4952. intro s hs
  4953. rw [← mul_add]
  4954. have hsplit := distributionTail_split p hp (M - s) (K + 1)
  4955. have heq : M - s + (K + 1) = M + K + 1 - s := by
  4956. simp only [Finset.mem_range] at hs
  4957. omega
  4958. rw [heq] at hsplit
  4959. rw [hsplit]
  4960. include hp0 in
  4961. theorem goodCrossingMass_le_window {M : ℕ} (hM : 0 < M) (K : ℕ) :
  4962. goodCrossingMass p M K ≤
  4963. ∑ j ∈ Finset.range (K + 1), renewalMass p (M + j) := by
  4964. rw [goodCrossingMass]
  4965. simp_rw [Finset.mul_sum]
  4966. rw [Finset.sum_comm]
  4967. apply Finset.sum_le_sum
  4968. intro j hj
  4969. have hrec : renewalMass p (M + j) =
  4970. ∑ s ∈ Finset.range (M + j), renewalMass p s * p (M + j - s) := by
  4971. cases M with
  4972. | zero => omega
  4973. | succ M =>
  4974. rw [Nat.succ_add]
  4975. exact renewalMass_succ p (M + j)
  4976. rw [hrec]
  4977. have hrewrite :
  4978. (∑ s ∈ Finset.range M, renewalMass p s * p (j + (M - s))) =
  4979. ∑ s ∈ Finset.range M, renewalMass p s * p (M + j - s) := by
  4980. apply Finset.sum_congr rfl
  4981. intro s hs
  4982. congr 2
  4983. simp only [Finset.mem_range] at hs
  4984. omega
  4985. rw [hrewrite]
  4986. apply Finset.sum_le_sum_of_subset_of_nonneg
  4987. · intro s hs
  4988. simp only [Finset.mem_range] at hs ⊢
  4989. omega
  4990. · intro s hs _
  4991. exact mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 _)
  4992. noncomputable def badJumpMass (M K j : ℕ) : ℝ :=
  4993. ∑ s ∈ Finset.range M,
  4994. selectedWeight (M + K + 1 ≤ s + j) (renewalMass p s * p j)
  4995. include hp0 in
  4996. theorem badJumpMass_nonneg (M K j : ℕ) :
  4997. 0 ≤ badJumpMass p M K j := by
  4998. exact Finset.sum_nonneg fun s _ ↦ selectedWeight_nonneg
  4999. (mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j))
  5000. include hp0 hp hpMass hpZero in
  5001. theorem badJumpMass_summable (M K : ℕ) :
  5002. Summable fun j ↦ badJumpMass p M K j := by
  5003. apply Summable.of_nonneg_of_le (badJumpMass_nonneg p hp0 M K)
  5004. · intro j
  5005. calc
  5006. badJumpMass p M K j ≤ ∑ _s ∈ Finset.range M, p j := by
  5007. apply Finset.sum_le_sum
  5008. intro s _
  5009. refine (selectedWeight_le
  5010. (mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j))).trans ?_
  5011. simpa only [one_mul] using mul_le_mul_of_nonneg_right
  5012. (renewalMass_le_one p hp0 hp hpMass hpZero s) (hp0 j)
  5013. _ = (M : ℝ) * p j := by simp
  5014. · exact hp.mul_left (M : ℝ)
  5015. include hp0 hp in
  5016. theorem badCrossingMass_eq_tsum_badJump (M K : ℕ) :
  5017. badCrossingMass p M K = ∑' j, badJumpMass p M K j := by
  5018. have hbase (s : ℕ) (hs : s ∈ Finset.range M) : Summable fun j ↦
  5019. selectedWeight (M + K + 1 - s ≤ j) (p j) := by
  5020. apply Summable.of_nonneg_of_le
  5021. (fun j ↦ selectedWeight_nonneg (hp0 j))
  5022. (fun j ↦ selectedWeight_le (hp0 j))
  5023. exact hp
  5024. have hsumm (s : ℕ) (hs : s ∈ Finset.range M) : Summable fun j ↦
  5025. selectedWeight (M + K + 1 - s ≤ j) (renewalMass p s * p j) := by
  5026. apply Summable.of_nonneg_of_le
  5027. (fun j ↦ selectedWeight_nonneg
  5028. (mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j)))
  5029. (fun j ↦ selectedWeight_le
  5030. (mul_nonneg (renewalMass_nonneg p hp0 s) (hp0 j)))
  5031. exact hp.mul_left (renewalMass p s)
  5032. have hmul (s : ℕ) (hs : s ∈ Finset.range M) :
  5033. renewalMass p s *
  5034. (∑' j, selectedWeight (M + K + 1 - s ≤ j) (p j)) =
  5035. ∑' j, selectedWeight (M + K + 1 - s ≤ j)
  5036. (renewalMass p s * p j) := by
  5037. rw [← (hbase s hs).tsum_mul_left]
  5038. apply tsum_congr
  5039. intro j
  5040. exact (selectedWeight_mul_left _ _ _).symm
  5041. rw [badCrossingMass]
  5042. simp_rw [distributionTail_eq_tsum_ge p]
  5043. have hfinite :
  5044. (∑ s ∈ Finset.range M, renewalMass p s *
  5045. ∑' j, selectedWeight (M + K + 1 - s ≤ j) (p j)) =
  5046. ∑ s ∈ Finset.range M, ∑' j,
  5047. selectedWeight (M + K + 1 - s ≤ j)
  5048. (renewalMass p s * p j) := by
  5049. apply Finset.sum_congr rfl
  5050. intro s hs
  5051. exact hmul s hs
  5052. rw [hfinite]
  5053. rw [← Summable.tsum_finsetSum hsumm]
  5054. apply tsum_congr
  5055. intro j
  5056. simp only [badJumpMass]
  5057. apply Finset.sum_congr rfl
  5058. intro s hs
  5059. congr 1
  5060. apply propext
  5061. exact Nat.sub_le_iff_le_add'
  5062. include hp0 hp hpMass hpZero in
  5063. theorem badJumpMass_le (M K j : ℕ) :
  5064. badJumpMass p M K j ≤ (j : ℝ) * p j := by
  5065. classical
  5066. by_cases hM : M = 0
  5067. · subst M
  5068. simpa [badJumpMass] using
  5069. (mul_nonneg (Nat.cast_nonneg j) (hp0 j))
  5070. let E := (Finset.range M).filter fun s ↦ M + K + 1 ≤ s + j
  5071. have hrewrite : badJumpMass p M K j =
  5072. ∑ s ∈ E, renewalMass p s * p j := by
  5073. simp only [badJumpMass, E, selectedWeight]
  5074. rw [Finset.sum_filter]
  5075. apply Finset.sum_congr rfl
  5076. intro s hs
  5077. by_cases h : M + K + 1 ≤ s + j <;> simp only [h, if_true, if_false]
  5078. rw [hrewrite]
  5079. have hcard : E.card ≤ j := by
  5080. have hsub : E ⊆ Finset.Icc (M + K + 1 - j) (M - 1) := by
  5081. intro s hs
  5082. simp only [E, Finset.mem_filter, Finset.mem_range] at hs
  5083. simp only [Finset.mem_Icc]
  5084. omega
  5085. have hc := Finset.card_le_card hsub
  5086. rw [Nat.card_Icc] at hc
  5087. omega
  5088. calc
  5089. (∑ s ∈ E, renewalMass p s * p j) ≤ ∑ _s ∈ E, p j := by
  5090. apply Finset.sum_le_sum
  5091. intro s _
  5092. simpa only [one_mul] using mul_le_mul_of_nonneg_right
  5093. (renewalMass_le_one p hp0 hp hpMass hpZero s) (hp0 j)
  5094. _ = (E.card : ℝ) * p j := by simp
  5095. _ ≤ (j : ℝ) * p j := by
  5096. exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcard) (hp0 j)
  5097. theorem badJumpMass_zero_of_le (M K j : ℕ) (hj : j ≤ K) :
  5098. badJumpMass p M K j = 0 := by
  5099. apply Finset.sum_eq_zero
  5100. intro s hs
  5101. have hslt : s < M := by simpa using hs
  5102. have hnot : ¬M + K + 1 ≤ s + j := by omega
  5103. simp [selectedWeight, hnot]
  5104. noncomputable def weightedDistributionTail (K : ℕ) : ℝ :=
  5105. ∑' t, ((t + K + 1 : ℕ) : ℝ) * p (t + K + 1)
  5106. theorem weightedDistributionTail_summable
  5107. (hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) (K : ℕ) :
  5108. Summable fun t ↦ ((t + K + 1 : ℕ) : ℝ) * p (t + K + 1) := by
  5109. simpa only [Nat.add_assoc] using
  5110. (summable_nat_add_iff (K + 1)).2 hpw
  5111. set_option maxHeartbeats 800000 in
  5112. -- Expanding the two infinite sums in the crossing estimate needs a larger bound.
  5113. include hp0 hp hpMass hpZero in
  5114. theorem badCrossingMass_le_weightedTail
  5115. (hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) (M K : ℕ) :
  5116. badCrossingMass p M K ≤ weightedDistributionTail p K := by
  5117. rw [badCrossingMass_eq_tsum_badJump p hp0 hp M K]
  5118. have hg := badJumpMass_summable p hp0 hp hpMass hpZero M K
  5119. have hsplit := hg.sum_add_tsum_nat_add (K + 1)
  5120. have hhead : (∑ j ∈ Finset.range (K + 1), badJumpMass p M K j) = 0 := by
  5121. apply Finset.sum_eq_zero
  5122. intro j hj
  5123. apply badJumpMass_zero_of_le p M K j
  5124. simpa only [Finset.mem_range, Nat.lt_succ_iff] using hj
  5125. rw [hhead, zero_add] at hsplit
  5126. rw [← hsplit]
  5127. have hbadTail : Summable fun t ↦ badJumpMass p M K (t + (K + 1)) :=
  5128. (summable_nat_add_iff (K + 1)).2 hg
  5129. exact hbadTail.tsum_le_tsum
  5130. (fun t ↦ badJumpMass_le p hp0 hp hpMass hpZero M K (t + (K + 1)))
  5131. (weightedDistributionTail_summable p hpw K)
  5132. theorem weightedDistributionTail_tendsto_zero :
  5133. Filter.Tendsto (weightedDistributionTail p) Filter.atTop (nhds 0) := by
  5134. let f : ℕ → ℝ := fun j ↦ (j : ℝ) * p j
  5135. have hbase := tendsto_sum_nat_add f
  5136. have hshift := (Filter.tendsto_add_atTop_iff_nat 1).mpr hbase
  5137. change Filter.Tendsto
  5138. (fun n ↦ ∑' k : ℕ, ((k + (n + 1) : ℕ) : ℝ) * p (k + (n + 1)))
  5139. Filter.atTop (nhds 0)
  5140. exact hshift
  5141. include hp0 hp hpMass hpZero in
  5142. theorem exists_uniform_renewal_window
  5143. (hpw : Summable fun j : ℕ ↦ (j : ℝ) * p j) :
  5144. ∃ K : ℕ, ∀ M : ℕ, 0 < M →
  5145. (1 : ℝ) / 2 <
  5146. ∑ j ∈ Finset.range (K + 1), renewalMass p (M + j) := by
  5147. have hevent : ∀ᶠ K : ℕ in Filter.atTop,
  5148. weightedDistributionTail p K < (1 : ℝ) / 2 :=
  5149. (weightedDistributionTail_tendsto_zero p).eventually
  5150. (by exact Iio_mem_nhds (by norm_num : (0 : ℝ) < 1 / 2))
  5151. obtain ⟨K, hK⟩ := hevent.exists
  5152. refine ⟨K, ?_⟩
  5153. intro M hM
  5154. have hcross := crossingMass_one p hp hpMass hpZero hM
  5155. have hsplit := good_add_bad_eq_crossing p hp M K
  5156. have hbad := badCrossingMass_le_weightedTail p hp0 hp hpMass hpZero hpw M K
  5157. have hgood : (1 : ℝ) / 2 < goodCrossingMass p M K := by
  5158. linarith
  5159. exact hgood.trans_le (goodCrossingMass_le_window p hp0 hM K)
  5160. end AbstractRenewal
  5161. /-! ## Finite concatenations of first-return loops -/
  5162. /-- Ordered concatenations whose total reward is exactly `n`. In the
  5163. successor case, `s` is the reward left for the tail. -/
  5164. inductive LoopSequence : ℕ → Type
  5165. | nil : LoopSequence 0
  5166. | cons {n : ℕ} (s : Fin (n + 1))
  5167. (atom : PositiveLoopOfReward (n + 1 - s.1))
  5168. (tail : LoopSequence s.1) : LoopSequence (n + 1)
  5169. def loopSequenceZeroEquiv : Unit ≃ LoopSequence 0 where
  5170. toFun _ := .nil
  5171. invFun r := by cases r; exact ()
  5172. left_inv u := by cases u; rfl
  5173. right_inv r := by cases r; rfl
  5174. def loopSequenceSuccEquiv (n : ℕ) :
  5175. (Σ s : Fin (n + 1),
  5176. PositiveLoopOfReward (n + 1 - s.1) × LoopSequence s.1) ≃
  5177. LoopSequence (n + 1) where
  5178. toFun r := .cons r.1 r.2.1 r.2.2
  5179. invFun r := by
  5180. cases r with
  5181. | cons s atom tail => exact ⟨s, atom, tail⟩
  5182. left_inv r := by cases r; rfl
  5183. right_inv r := by cases r; rfl
  5184. noncomputable instance loopSequenceFintype : (n : ℕ) → Fintype (LoopSequence n)
  5185. | 0 => Fintype.ofEquiv Unit loopSequenceZeroEquiv
  5186. | n + 1 => by
  5187. letI (s : Fin (n + 1)) : Fintype (LoopSequence s.1) :=
  5188. loopSequenceFintype s.1
  5189. exact Fintype.ofEquiv
  5190. (Σ s : Fin (n + 1),
  5191. PositiveLoopOfReward (n + 1 - s.1) × LoopSequence s.1)
  5192. (loopSequenceSuccEquiv n)
  5193. termination_by n => n
  5194. decreasing_by exact s.2
  5195. noncomputable def loopSequenceCount (n : ℕ) : ℕ :=
  5196. Fintype.card (LoopSequence n)
  5197. @[simp] theorem loopSequenceCount_zero : loopSequenceCount 0 = 1 := by
  5198. unfold loopSequenceCount
  5199. rw [Fintype.card_congr loopSequenceZeroEquiv.symm]
  5200. exact Fintype.card_unit
  5201. theorem loopSequenceCount_succ (n : ℕ) :
  5202. loopSequenceCount (n + 1) =
  5203. ∑ s ∈ Finset.range (n + 1),
  5204. firstReturnLoopCount (n + 1 - s) * loopSequenceCount s := by
  5205. unfold loopSequenceCount
  5206. rw [Fintype.card_congr (loopSequenceSuccEquiv n).symm]
  5207. simp only [Fintype.card_sigma, Fintype.card_prod, firstReturnLoopCount]
  5208. exact Fin.sum_univ_eq_sum_range
  5209. (fun s : ℕ => Fintype.card (PositiveLoopOfReward (n + 1 - s)) *
  5210. Fintype.card (LoopSequence s)) (n + 1)
  5211. theorem q_pos : 0 < q := by norm_num [q]
  5212. /-- The renewal mass is exactly the number of ordered loop concatenations
  5213. divided by their common reciprocal slope. -/
  5214. theorem renewalMass_eq_sequenceCount (n : ℕ) :
  5215. renewalMass loopRewardMass n = (loopSequenceCount n : ℝ) / (q ^ n : ℕ) := by
  5216. induction n using Nat.strong_induction_on with
  5217. | h n ih =>
  5218. cases n with
  5219. | zero => simp
  5220. | succ n =>
  5221. rw [renewalMass_succ, loopSequenceCount_succ]
  5222. rw [Nat.cast_sum]
  5223. simp only [Nat.cast_mul]
  5224. rw [Finset.sum_div]
  5225. apply Finset.sum_congr rfl
  5226. intro s hs
  5227. have hslt : s < n + 1 := by simpa using hs
  5228. rw [ih s hslt, loopRewardMass_eq_count]
  5229. have hsum : s + (n + 1 - s) = n + 1 := by omega
  5230. push_cast
  5231. rw [div_mul_div_comm, ← pow_add, hsum]
  5232. ring
  5233. noncomputable def loopSequenceChoices : {n : ℕ} → LoopSequence n → List BlockChoice
  5234. | 0, .nil => []
  5235. | _ + 1, .cons _ atom tail =>
  5236. recurrentLoopRecordChoices atom.1.2.2 ++ loopSequenceChoices tail
  5237. noncomputable def loopSequenceWord : {n : ℕ} → LoopSequence n → List ℕ
  5238. | 0, .nil => []
  5239. | _ + 1, .cons _ atom tail =>
  5240. firstReturnLoopWord atom.1 ++ loopSequenceWord tail
  5241. theorem controlledListWord_append (x : AugmentedState) (u v : List BlockChoice) :
  5242. controlledListWord x (u ++ v) = controlledListWord x u ++
  5243. controlledListWord (controlledChain.listEndpoint x u) v := by
  5244. induction u generalizing x with
  5245. | nil => rfl
  5246. | cons a u ih =>
  5247. simp only [List.cons_append, controlledListWord]
  5248. rw [ih]
  5249. simp only [FiniteChoiceChain.listEndpoint]
  5250. rw [List.append_assoc]
  5251. rfl
  5252. theorem inverseWord_append (u v : List ℕ) (z : ℚ) :
  5253. inverseWord (u ++ v) z = inverseWord u (inverseWord v z) := by
  5254. induction u with
  5255. | nil => rfl
  5256. | cons a u ih => simp [inverseWord, ih]
  5257. @[simp] theorem controlledListWord_length (x : AugmentedState)
  5258. (u : List BlockChoice) :
  5259. (controlledListWord x u).length = controlBlockLength * u.length := by
  5260. induction u generalizing x with
  5261. | nil => simp [controlledListWord]
  5262. | cons a u ih =>
  5263. simp only [controlledListWord, List.length_append, blockWord_length,
  5264. List.length_cons, ih]
  5265. ring
  5266. theorem controlledListWord_mem_multipliers (x : AugmentedState)
  5267. (u : List BlockChoice) :
  5268. ∀ a ∈ controlledListWord x u, a ∈ multipliers := by
  5269. have h := controlledPathWord_mem_multipliers u.length x u.get
  5270. simpa only [← controlledListWord_ofFn, List.ofFn_get] using h
  5271. theorem controlledListWord_prod_two_le (x : AugmentedState)
  5272. {u : List BlockChoice} (hu : u ≠ []) : 2 ≤ (controlledListWord x u).prod := by
  5273. apply prod_two_le_of_ne_nil
  5274. · intro hnil
  5275. have hlen := congrArg List.length hnil
  5276. simp only [controlledListWord_length, List.length_nil] at hlen
  5277. have hN := controlBlockLength_pos
  5278. have hulen : 0 < u.length := List.length_pos_iff.mpr hu
  5279. have hprod : 0 < controlBlockLength * u.length := Nat.mul_pos hN hulen
  5280. omega
  5281. · intro a ha
  5282. exact mem_multipliers_two_le (controlledListWord_mem_multipliers x u a ha)
  5283. theorem controlledList_inverse_mem (x : AugmentedState)
  5284. (u : List BlockChoice) (hu : 0 < controlledChain.listWeight x u)
  5285. {z : ℚ} (hz : z ∈ K (controlledChain.listEndpoint x u).1) :
  5286. inverseWord (controlledListWord x u) z ∈ K x.1 := by
  5287. have hpath : 0 < controlledChain.pathWeight u.length x u.get := by
  5288. rwa [← controlledChain.listWeight_ofFn, List.ofFn_get]
  5289. have hmem := (controlledIntervalPath u.length x u.get hpath).inverseWord_mem_source
  5290. (by simpa only [← controlledChain.listEndpoint_ofFn, List.ofFn_get] using hz)
  5291. simpa only [controlledIntervalPath_word, ← controlledListWord_ofFn,
  5292. List.ofFn_get] using hmem
  5293. set_option maxHeartbeats 800000 in
  5294. -- The proof recursively peels variable-length blocks and normalizes their products.
  5295. /-- Two positive controlled paths with the same pulled-back point and the
  5296. same slope have identical block choices. This is the graph-directed
  5297. injectivity statement that excludes global nonfree affine identities. -/
  5298. theorem controlledLists_eq_of_inverse_eq (x : AugmentedState)
  5299. (u v : List BlockChoice)
  5300. (hu : 0 < controlledChain.listWeight x u)
  5301. (hv : 0 < controlledChain.listWeight x v)
  5302. {zu zv : ℚ}
  5303. (hzu : zu ∈ K (controlledChain.listEndpoint x u).1)
  5304. (hzv : zv ∈ K (controlledChain.listEndpoint x v).1)
  5305. (hinv : inverseWord (controlledListWord x u) zu =
  5306. inverseWord (controlledListWord x v) zv)
  5307. (hprod : (controlledListWord x u).prod =
  5308. (controlledListWord x v).prod) : u = v ∧ zu = zv := by
  5309. induction u generalizing x v zu zv with
  5310. | nil =>
  5311. cases v with
  5312. | nil => exact ⟨rfl, by simpa [controlledListWord, inverseWord] using hinv⟩
  5313. | cons b v =>
  5314. have htwo := controlledListWord_prod_two_le x
  5315. (u := b :: v) (by simp)
  5316. change 1 = (controlledListWord x (b :: v)).prod at hprod
  5317. rw [← hprod] at htwo
  5318. omega
  5319. | cons a u ih =>
  5320. cases v with
  5321. | nil =>
  5322. have htwo := controlledListWord_prod_two_le x
  5323. (u := a :: u) (by simp)
  5324. change (controlledListWord x (a :: u)).prod = 1 at hprod
  5325. rw [hprod] at htwo
  5326. omega
  5327. | cons b v =>
  5328. simp only [FiniteChoiceChain.listWeight] at hu hv
  5329. have hpa0 := controlledChain.probability_nonneg x a
  5330. have hpb0 := controlledChain.probability_nonneg x b
  5331. have htu0 := controlledChain.listWeight_nonneg (controlledNext x a) u
  5332. have htv0 := controlledChain.listWeight_nonneg (controlledNext x b) v
  5333. have hpa : 0 < controlledChain.probability x a :=
  5334. pos_of_mul_pos_left hu htu0
  5335. have hpb : 0 < controlledChain.probability x b :=
  5336. pos_of_mul_pos_left hv htv0
  5337. have htu : 0 < controlledChain.listWeight (controlledNext x a) u :=
  5338. pos_of_mul_pos_right hu hpa0
  5339. have htv : 0 < controlledChain.listWeight (controlledNext x b) v :=
  5340. pos_of_mul_pos_right hv hpb0
  5341. let yu := inverseWord (controlledListWord (controlledNext x a) u) zu
  5342. let yv := inverseWord (controlledListWord (controlledNext x b) v) zv
  5343. have hyu : yu ∈ K (controlledNext x a).1 :=
  5344. controlledList_inverse_mem (controlledNext x a) u htu hzu
  5345. have hyv : yv ∈ K (controlledNext x b).1 :=
  5346. controlledList_inverse_mem (controlledNext x b) v htv hzv
  5347. have hblockA : 0 < blockWeight (feedback x.2) controlBlockLength x.1 a := by
  5348. change 0 < (blockWeight (feedback x.2) controlBlockLength x.1 a : ℝ)
  5349. at hpa
  5350. exact_mod_cast hpa
  5351. have hblockB : 0 < blockWeight (feedback x.2) controlBlockLength x.1 b := by
  5352. change 0 < (blockWeight (feedback x.2) controlBlockLength x.1 b : ℝ)
  5353. at hpb
  5354. exact_mod_cast hpb
  5355. have hblockInv : inverseWord
  5356. (blockWord (feedback x.2) controlBlockLength x.1 a) yu =
  5357. inverseWord (blockWord (feedback x.2) controlBlockLength x.1 b) yv := by
  5358. simpa only [controlledListWord, inverseWord_append, yu, yv] using hinv
  5359. have hblock := blockChoice_eq_of_inverse_eq (feedback x.2)
  5360. controlBlockLength x.1 hblockA hblockB hyu hyv hblockInv
  5361. have hab := hblock.1
  5362. subst b
  5363. have htailProd :
  5364. (controlledListWord (controlledNext x a) u).prod =
  5365. (controlledListWord (controlledNext x a) v).prod := by
  5366. simp only [controlledListWord, List.prod_append] at hprod
  5367. have hblockProd : 0 <
  5368. (blockWord (feedback x.2) controlBlockLength x.1 a).prod := by
  5369. exact List.prod_pos (fun z hz ↦ by
  5370. have hz2 := mem_multipliers_two_le
  5371. (blockWord_mem_multipliers _ _ _ _ z hz)
  5372. omega)
  5373. exact Nat.eq_of_mul_eq_mul_left hblockProd hprod
  5374. have hrec := ih (x := controlledNext x a) (v := v)
  5375. htu htv hzu hzv hblock.2 htailProd
  5376. exact ⟨congrArg (List.cons a) hrec.1, hrec.2⟩
  5377. theorem loopSequence_endpoint {n : ℕ} (r : LoopSequence n) :
  5378. controlledChain.listEndpoint recurrentAugmentedState
  5379. (loopSequenceChoices (n := n) r) =
  5380. recurrentAugmentedState := by
  5381. induction r with
  5382. | nil => rfl
  5383. | cons s atom tail ih =>
  5384. rw [loopSequenceChoices, controlledChain.listEndpoint_append,
  5385. recurrentLoopRecord_endpoint atom.1.2.2]
  5386. exact ih
  5387. theorem loopSequence_listWeight_pos {n : ℕ} (r : LoopSequence n) :
  5388. 0 < controlledChain.listWeight recurrentAugmentedState
  5389. (loopSequenceChoices (n := n) r) := by
  5390. induction r with
  5391. | nil => simp [loopSequenceChoices, FiniteChoiceChain.listWeight]
  5392. | cons s atom tail ih =>
  5393. rw [loopSequenceChoices, controlledChain.listWeight_append,
  5394. recurrentLoopRecord_endpoint atom.1.2.2,
  5395. ← recurrentLoopRecordWeight_eq_listWeight]
  5396. exact mul_pos atom.2.2 ih
  5397. theorem loopSequenceWord_eq_controlled {n : ℕ} (r : LoopSequence n) :
  5398. loopSequenceWord (n := n) r = controlledListWord recurrentAugmentedState
  5399. (loopSequenceChoices (n := n) r) := by
  5400. induction r with
  5401. | nil => rfl
  5402. | cons s atom tail ih =>
  5403. simp only [loopSequenceWord, loopSequenceChoices]
  5404. rw [controlledListWord_append, recurrentLoopRecord_endpoint, ← ih]
  5405. rfl
  5406. theorem loopSequenceWord_mem_multipliers {n : ℕ} (r : LoopSequence n) :
  5407. ∀ a ∈ loopSequenceWord (n := n) r, a ∈ multipliers := by
  5408. induction r with
  5409. | nil => simp [loopSequenceWord]
  5410. | cons s atom tail ih =>
  5411. simp only [loopSequenceWord, List.mem_append]
  5412. intro a ha
  5413. rcases ha with ha | ha
  5414. · exact firstReturnLoopWord_mem_multipliers atom.1 a ha
  5415. · exact ih a ha
  5416. theorem loopSequenceWord_prod {n : ℕ} (r : LoopSequence n) :
  5417. (loopSequenceWord (n := n) r).prod = q ^ n := by
  5418. induction r with
  5419. | nil => simp [loopSequenceWord]
  5420. | @cons n s atom tail ih =>
  5421. simp only [loopSequenceWord, List.prod_append]
  5422. have hatom := recurrentLoopRecord_prod_eq_q_pow atom.1.2.2
  5423. change (firstReturnLoopWord atom.1).prod = q ^ firstReturnLoopReward atom.1
  5424. at hatom
  5425. rw [hatom, atom.2.1, ih, ← pow_add]
  5426. congr 1
  5427. omega
  5428. theorem loopSequenceChoices_injective (n : ℕ) :
  5429. Function.Injective (loopSequenceChoices (n := n) :
  5430. LoopSequence n → List BlockChoice) := by
  5431. induction n using Nat.strong_induction_on with
  5432. | h n ih =>
  5433. intro r t hchoices
  5434. cases r with
  5435. | nil => cases t; rfl
  5436. | @cons n s atom tail =>
  5437. cases t with
  5438. | @cons _ s' atom' tail' =>
  5439. simp only [loopSequenceChoices] at hchoices
  5440. have hret := recurrentLoopRecord_isReturnBase atom.1.2.2
  5441. have hret' := recurrentLoopRecord_isReturnBase atom'.1.2.2
  5442. have hsplit := controlledChain.isReturnList_append_unique
  5443. {recurrentAugmentedState} hret hret' hchoices
  5444. have hatomVal : atom.1 = atom'.1 :=
  5445. recurrentLoopRecord_sigma_choices_injective hsplit.1
  5446. have hs : s = s' := by
  5447. apply Fin.ext
  5448. have hr := atom.2.1
  5449. have ht := atom'.2.1
  5450. rw [hatomVal] at hr
  5451. omega
  5452. subst s'
  5453. have hatom : atom = atom' := Subtype.ext hatomVal
  5454. subst atom'
  5455. have htail : tail = tail' := ih s.1 s.2 hsplit.2
  5456. subst tail'
  5457. rfl
  5458. theorem controlledListWord_injective_of_return {u v : List BlockChoice}
  5459. (hu : 0 < controlledChain.listWeight recurrentAugmentedState u)
  5460. (hv : 0 < controlledChain.listWeight recurrentAugmentedState v)
  5461. (huend : controlledChain.listEndpoint recurrentAugmentedState u =
  5462. recurrentAugmentedState)
  5463. (hvend : controlledChain.listEndpoint recurrentAugmentedState v =
  5464. recurrentAugmentedState)
  5465. (hword : controlledListWord recurrentAugmentedState u =
  5466. controlledListWord recurrentAugmentedState v) : u = v := by
  5467. have hz : intervalMidpoint recurrentAugmentedState.1 ∈
  5468. K recurrentAugmentedState.1 := intervalMidpoint_mem _
  5469. have hinv := congrArg
  5470. (fun w ↦ inverseWord w (intervalMidpoint recurrentAugmentedState.1)) hword
  5471. have hprod := congrArg List.prod hword
  5472. exact (controlledLists_eq_of_inverse_eq recurrentAugmentedState u v hu hv
  5473. (by simpa [huend] using hz) (by simpa [hvend] using hz)
  5474. hinv hprod).1
  5475. theorem loopSequenceWord_injective (n : ℕ) :
  5476. Function.Injective (loopSequenceWord (n := n) : LoopSequence n → List ℕ) := by
  5477. intro r s hword
  5478. apply loopSequenceChoices_injective n
  5479. apply controlledListWord_injective_of_return
  5480. · exact loopSequence_listWeight_pos r
  5481. · exact loopSequence_listWeight_pos s
  5482. · exact loopSequence_endpoint r
  5483. · exact loopSequence_endpoint s
  5484. · simpa only [← loopSequenceWord_eq_controlled] using hword
  5485. theorem inverseWord_eq_sub_div (w : List ℕ) (z : ℚ)
  5486. (hw : ∀ a ∈ w, 0 < a) :
  5487. inverseWord w z = (z - (offset w : ℚ)) / (w.prod : ℚ) := by
  5488. induction w with
  5489. | nil => simp [inverseWord, offset]
  5490. | cons a w ih =>
  5491. have ha : 0 < a := hw a (by simp)
  5492. have htail : ∀ b ∈ w, 0 < b := by
  5493. intro b hb
  5494. exact hw b (by simp [hb])
  5495. have hprod : (w.prod : ℚ) ≠ 0 := by
  5496. have hprodNat : w.prod ≠ 0 := by
  5497. apply List.prod_ne_zero
  5498. intro hzero
  5499. exact (htail 0 hzero).ne' rfl
  5500. exact_mod_cast hprodNat
  5501. have haQ : (a : ℚ) ≠ 0 := by exact_mod_cast ha.ne'
  5502. rw [inverseWord, ih htail]
  5503. simp only [inverseT, offset, Int.cast_sub, Int.cast_natCast,
  5504. List.prod_cons, Nat.cast_mul]
  5505. field_simp
  5506. ring
  5507. /-- At fixed total reward, different loop concatenations have different
  5508. affine offsets. -/
  5509. theorem loopSequence_offset_injective (n : ℕ) :
  5510. Set.InjOn (fun r : LoopSequence n ↦
  5511. offset (loopSequenceWord (n := n) r)) Set.univ := by
  5512. intro r _ s _ hoffset
  5513. change offset (loopSequenceWord (n := n) r) =
  5514. offset (loopSequenceWord (n := n) s) at hoffset
  5515. apply loopSequenceChoices_injective n
  5516. have hrpos : ∀ a ∈ loopSequenceWord (n := n) r, 0 < a := by
  5517. intro a ha
  5518. have ha2 := mem_multipliers_two_le (loopSequenceWord_mem_multipliers r a ha)
  5519. omega
  5520. have hspos : ∀ a ∈ loopSequenceWord (n := n) s, 0 < a := by
  5521. intro a ha
  5522. have ha2 := mem_multipliers_two_le (loopSequenceWord_mem_multipliers s a ha)
  5523. omega
  5524. have hprod : (loopSequenceWord (n := n) r).prod =
  5525. (loopSequenceWord (n := n) s).prod := by
  5526. rw [loopSequenceWord_prod, loopSequenceWord_prod]
  5527. have hinv : inverseWord (loopSequenceWord (n := n) r)
  5528. (intervalMidpoint recurrentAugmentedState.1) =
  5529. inverseWord (loopSequenceWord (n := n) s)
  5530. (intervalMidpoint recurrentAugmentedState.1) := by
  5531. rw [inverseWord_eq_sub_div _ _ hrpos, inverseWord_eq_sub_div _ _ hspos,
  5532. hoffset, hprod]
  5533. have hchoices := controlledLists_eq_of_inverse_eq recurrentAugmentedState
  5534. (loopSequenceChoices (n := n) r) (loopSequenceChoices (n := n) s)
  5535. (loopSequence_listWeight_pos r) (loopSequence_listWeight_pos s)
  5536. (by rw [loopSequence_endpoint]; exact intervalMidpoint_mem _)
  5537. (by rw [loopSequence_endpoint]; exact intervalMidpoint_mem _)
  5538. (by simpa only [← loopSequenceWord_eq_controlled] using hinv)
  5539. (by simpa only [← loopSequenceWord_eq_controlled] using hprod)
  5540. exact hchoices.1
  5541. noncomputable def loopSequenceWordFamily (n : ℕ) : Finset (List ℕ) :=
  5542. Finset.univ.image (loopSequenceWord : LoopSequence n → List ℕ)
  5543. theorem loopSequenceWordFamily_card (n : ℕ) :
  5544. (loopSequenceWordFamily n).card = loopSequenceCount n := by
  5545. rw [loopSequenceWordFamily, Finset.card_image_of_injective _
  5546. (loopSequenceWord_injective n)]
  5547. rfl
  5548. theorem mem_loopSequenceWordFamily {n : ℕ} {w : List ℕ}
  5549. (hw : w ∈ loopSequenceWordFamily n) :
  5550. ∃ r : LoopSequence n, loopSequenceWord (n := n) r = w := by
  5551. simpa [loopSequenceWordFamily] using hw
  5552. theorem loopSequenceWordFamily_data {n : ℕ} (hn : 0 < n) :
  5553. (∀ w ∈ loopSequenceWordFamily n,
  5554. w ≠ [] ∧ (∀ a ∈ w, a ∈ multipliers) ∧ w.prod = q ^ n) ∧
  5555. (∀ u ∈ loopSequenceWordFamily n, ∀ v ∈ loopSequenceWordFamily n,
  5556. u ≠ v → offset u ≠ offset v) := by
  5557. constructor
  5558. · intro w hw
  5559. obtain ⟨r, rfl⟩ := mem_loopSequenceWordFamily hw
  5560. refine ⟨?_, loopSequenceWord_mem_multipliers r, loopSequenceWord_prod r⟩
  5561. intro hempty
  5562. have hprod := loopSequenceWord_prod r
  5563. rw [hempty, List.prod_nil] at hprod
  5564. have hq : 1 < q ^ n := one_lt_pow₀ (by norm_num [q] : 1 < q) hn.ne'
  5565. omega
  5566. · intro u hu v hv huv hoff
  5567. obtain ⟨r, rfl⟩ := mem_loopSequenceWordFamily hu
  5568. obtain ⟨s, rfl⟩ := mem_loopSequenceWordFamily hv
  5569. have hrs := loopSequence_offset_injective n (Set.mem_univ r)
  5570. (Set.mem_univ s) hoff
  5571. exact huv (congrArg loopSequenceWord hrs)
  5572. /-- Pigeonholing the fixed renewal window selects one common slope at every
  5573. scale. -/
  5574. theorem exists_loopSequence_count_at_scale :
  5575. ∃ K : ℕ, ∀ M : ℕ, 0 < M → ∃ j ∈ Finset.range (K + 1),
  5576. q ^ (M + j) < 2 * (K + 1) * loopSequenceCount (M + j) := by
  5577. obtain ⟨K, hK⟩ := exists_uniform_renewal_window loopRewardMass
  5578. loopRewardMass_nonneg loopRewardMass_summable tsum_loopRewardMass
  5579. loopRewardMass_zero weighted_loopRewardMass_summable
  5580. refine ⟨K, ?_⟩
  5581. intro M hM
  5582. have hwindow := hK M hM
  5583. by_contra hnone
  5584. push Not at hnone
  5585. have hterm (j : ℕ) (hj : j ∈ Finset.range (K + 1)) :
  5586. renewalMass loopRewardMass (M + j) ≤
  5587. (1 : ℝ) / (2 * (K + 1)) := by
  5588. rw [renewalMass_eq_sequenceCount]
  5589. have hjbound := hnone j hj
  5590. have hqReal : (0 : ℝ) < q := by exact_mod_cast q_pos
  5591. have hq : (0 : ℝ) < (q ^ (M + j) : ℕ) := by
  5592. exact_mod_cast pow_pos q_pos (M + j)
  5593. have hjReal : (2 * (K + 1) : ℝ) * loopSequenceCount (M + j) ≤
  5594. (q ^ (M + j) : ℕ) := by exact_mod_cast hjbound
  5595. have hD : (0 : ℝ) < 2 * (K + 1) := by positivity
  5596. apply (div_le_div_iff₀ hq hD).2
  5597. simpa [mul_assoc, mul_comm, mul_left_comm] using hjReal
  5598. have hsum : (∑ j ∈ Finset.range (K + 1),
  5599. renewalMass loopRewardMass (M + j)) ≤ (1 : ℝ) / 2 := by
  5600. calc
  5601. _ ≤ ∑ _j ∈ Finset.range (K + 1),
  5602. (1 : ℝ) / (2 * (K + 1)) := Finset.sum_le_sum hterm
  5603. _ = (1 : ℝ) / 2 := by
  5604. simp
  5605. field_simp
  5606. linarith
  5607. /-! ## Unconditional completion of the proof -/
  5608. theorem q_one_lt_unconditional : 1 < q := by norm_num [q]
  5609. theorem geometric_count_from_loop_sequences :
  5610. ∃ denominator r₀ : ℕ, 0 < denominator ∧ ∀ r ≥ r₀,
  5611. q ^ r ≤ denominator * countUpTo (17 * q ^ r) := by
  5612. classical
  5613. obtain ⟨K, hscale⟩ := exists_loopSequence_count_at_scale
  5614. let D : ℕ := 2 * (K + 1)
  5615. let denominator : ℕ := q ^ K * D
  5616. refine ⟨denominator, K + 1, ?_, ?_⟩
  5617. · dsimp [denominator, D]
  5618. exact Nat.mul_pos (pow_pos q_pos K) (by omega)
  5619. · intro r hr
  5620. let M := r - K
  5621. have hM : 0 < M := by
  5622. dsimp [M]
  5623. omega
  5624. have hrEq : M + K = r := by
  5625. dsimp [M]
  5626. omega
  5627. obtain ⟨j, hj, hcount⟩ := hscale M hM
  5628. have hjle : j ≤ K := by
  5629. simpa only [Finset.mem_range, Nat.lt_succ_iff] using hj
  5630. let n := M + j
  5631. let W := loopSequenceWordFamily n
  5632. have hn : 0 < n := by dsimp [n]; omega
  5633. have hdata := loopSequenceWordFamily_data hn
  5634. let V : Finset ℕ := W.image fun w ↦ eval w 17
  5635. have hinj : Set.InjOn (fun w : List ℕ ↦ eval w 17) W := by
  5636. intro u hu v hv heval
  5637. by_contra huv
  5638. have huData := hdata.1 u hu
  5639. have hvData := hdata.1 v hv
  5640. have huTwo : ∀ a ∈ u, 2 ≤ a := by
  5641. intro a ha
  5642. exact mem_multipliers_two_le (huData.2.1 a ha)
  5643. have hvTwo : ∀ a ∈ v, 2 ≤ a := by
  5644. intro a ha
  5645. exact mem_multipliers_two_le (hvData.2.1 a ha)
  5646. have hprod : u.prod = v.prod := huData.2.2.trans hvData.2.2.symm
  5647. exact (eval_ne_of_prod_eq_offset_ne (by norm_num) huTwo hvTwo hprod
  5648. (hdata.2 u hu v hv huv)) heval
  5649. have hVcard : V.card = W.card := Finset.card_image_of_injOn hinj
  5650. have hV : ∀ x ∈ V, Generated x ∧ x ≤ 17 * q ^ r := by
  5651. intro x hx
  5652. simp only [V, Finset.mem_image] at hx
  5653. obtain ⟨w, hw, rfl⟩ := hx
  5654. have hwData := hdata.1 w hw
  5655. have hgenerated : Generated (eval w 17) :=
  5656. eval_at_seventeen_generated w hwData.2.1
  5657. have hone : ∀ a ∈ w, 1 ≤ a := by
  5658. intro a ha
  5659. exact (mem_multipliers_two_le (hwData.2.1 a ha)).trans' (by omega)
  5660. have heval : eval w 17 ≤ w.prod * 17 := eval_le_prod_mul w hone
  5661. rw [hwData.2.2] at heval
  5662. have hnle : n ≤ r := by
  5663. dsimp [n]
  5664. omega
  5665. have hpow : q ^ n ≤ q ^ r := Nat.pow_le_pow_right q_pos hnle
  5666. exact ⟨hgenerated, heval.trans (by
  5667. simpa [mul_comm] using Nat.mul_le_mul_left 17 hpow)⟩
  5668. have hVle : W.card ≤ countUpTo (17 * q ^ r) := by
  5669. rw [← hVcard]
  5670. exact card_le_countUpTo hV
  5671. have hWcard : W.card = loopSequenceCount n := by
  5672. dsimp [W]
  5673. exact loopSequenceWordFamily_card n
  5674. have hscaleNat : q ^ n ≤ D * W.card := by
  5675. rw [hWcard]
  5676. dsimp [n, D] at hcount ⊢
  5677. omega
  5678. have hMcount : q ^ M ≤ D * countUpTo (17 * q ^ r) := by
  5679. have hpowM : q ^ M ≤ q ^ n := by
  5680. apply Nat.pow_le_pow_right q_pos
  5681. dsimp [n]
  5682. omega
  5683. exact hpowM.trans (hscaleNat.trans (Nat.mul_le_mul_left D hVle))
  5684. dsimp [denominator]
  5685. calc
  5686. q ^ r = q ^ (M + K) := by rw [hrEq]
  5687. _ = q ^ M * q ^ K := pow_add q M K
  5688. _ ≤ (D * countUpTo (17 * q ^ r)) * q ^ K :=
  5689. Nat.mul_le_mul_right (q ^ K) hMcount
  5690. _ = q ^ K * D * countUpTo (17 * q ^ r) := by ring
  5691. /-- Erdős Problem 424, fully formalized with no recurrence or renewal
  5692. assumption left as a hypothesis. -/
  5693. theorem theorem_one : HasPositiveLowerDensity := by
  5694. obtain ⟨denominator, r₀, hdenominator, hgeom⟩ :=
  5695. geometric_count_from_loop_sequences
  5696. exact positiveLowerDensity_of_geometric_count q_one_lt_unconditional
  5697. hdenominator ⟨r₀, hgeom⟩
  5698. open Set
  5699. /-!
  5700. ## Compatibility with `formal-conjectures`
  5701. The three definitions below are the definitions used in
  5702. `FormalConjectures/ErdosProblems/424.lean`. They describe the generated set
  5703. by finite stages, whereas the main development uses the inductive predicate
  5704. `Generated`.
  5705. The linked conjecture currently uses `Set.HasPosDensity`, which asks for the
  5706. existence of a full natural-density limit. Erdős's intended question, and
  5707. the theorem proved by Hofstadter's argument, concern positive lower density.
  5708. The theorem `erdos_424` below states that conclusion directly
  5709. for the `formal-conjectures` generated set.
  5710. -/
  5711. /-- The new numbers obtained from `A` by applying `x * y - 1` to distinct
  5712. members of `A`. This agrees verbatim with the `formal-conjectures`
  5713. definition. -/
  5714. def nextGeneration (A : Set ℕ) : Set ℕ :=
  5715. {z : ℕ | ∃ x y, x ∈ A ∧ y ∈ A ∧ x ≠ y ∧ z = x * y - 1}
  5716. /-- The finite-stage closure used by `formal-conjectures`. -/
  5717. def sequenceSet (n : ℕ) : Set ℕ :=
  5718. Nat.rec {2, 3} (fun _ previous => previous ∪ nextGeneration previous) n
  5719. /-- The union of all finite stages, as in `formal-conjectures`. -/
  5720. def generatedSet : Set ℕ := ⋃ n : ℕ, sequenceSet n
  5721. theorem sequenceSet_subset_succ (n : ℕ) :
  5722. sequenceSet n ⊆ sequenceSet (n + 1) := by
  5723. intro x hx
  5724. exact Or.inl hx
  5725. theorem sequenceSet_mono : Monotone sequenceSet :=
  5726. monotone_nat_of_le_succ sequenceSet_subset_succ
  5727. theorem mem_sequenceSet_generated {n x : ℕ} (hx : x ∈ sequenceSet n) :
  5728. Generated x := by
  5729. induction n generalizing x with
  5730. | zero =>
  5731. simp only [sequenceSet] at hx
  5732. rcases hx with rfl | rfl
  5733. · exact Generated.two
  5734. · exact Generated.three
  5735. | succ n ih =>
  5736. rw [sequenceSet] at hx
  5737. rcases hx with hx | hx
  5738. · exact ih hx
  5739. · obtain ⟨a, b, ha, hb, hab, rfl⟩ := hx
  5740. exact Generated.mul_sub_one (ih ha) (ih hb) hab
  5741. theorem generated_mem_generatedSet {x : ℕ} (hx : Generated x) :
  5742. x ∈ generatedSet := by
  5743. rw [generatedSet, Set.mem_iUnion]
  5744. induction hx with
  5745. | two => exact ⟨0, by simp [sequenceSet]⟩
  5746. | three => exact ⟨0, by simp [sequenceSet]⟩
  5747. | @mul_sub_one a b ha hb hab iha ihb =>
  5748. obtain ⟨na, hna⟩ := iha
  5749. obtain ⟨nb, hnb⟩ := ihb
  5750. refine ⟨max na nb + 1, ?_⟩
  5751. rw [sequenceSet]
  5752. right
  5753. exact ⟨a, b,
  5754. sequenceSet_mono (Nat.le_max_left na nb) hna,
  5755. sequenceSet_mono (Nat.le_max_right na nb) hnb,
  5756. hab, rfl⟩
  5757. theorem mem_generatedSet_generated {x : ℕ} (hx : x ∈ generatedSet) :
  5758. Generated x := by
  5759. rw [generatedSet, Set.mem_iUnion] at hx
  5760. obtain ⟨n, hn⟩ := hx
  5761. exact mem_sequenceSet_generated hn
  5762. /-- The staged set in `formal-conjectures` is exactly the inductively
  5763. generated set used in the proof. -/
  5764. theorem mem_generatedSet_iff (x : ℕ) : x ∈ generatedSet ↔ Generated x :=
  5765. ⟨mem_generatedSet_generated, generated_mem_generatedSet⟩
  5766. open Classical in
  5767. theorem countUpTo_eq_generatedSet_count (x : ℕ) :
  5768. countUpTo x =
  5769. ((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card := by
  5770. classical
  5771. simp only [countUpTo]
  5772. apply congrArg Finset.card
  5773. ext n
  5774. simp only [Finset.mem_filter, Finset.mem_Icc]
  5775. rw [mem_generatedSet_iff]
  5776. open Classical in
  5777. /-- The intended positive-lower-density statement of Erdős Problem 424,
  5778. expressed using the exact `nextGeneration`, `sequenceSet`, and `generatedSet`
  5779. definitions from `formal-conjectures`. -/
  5780. theorem generatedSet_has_positive_lower_density :
  5781. ∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,
  5782. c * (x : ℝ) ≤
  5783. (((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card : ℝ) := by
  5784. obtain ⟨c, hc, hbound⟩ := theorem_one
  5785. refine ⟨c, hc, ?_⟩
  5786. filter_upwards [hbound] with x hx
  5787. rwa [← countUpTo_eq_generatedSet_count]
  5788. open Classical in
  5789. /-- The intended problem statement in the staged-set formulation. -/
  5790. theorem erdos_424 :
  5791. ∃ c : ℝ, 0 < c ∧ ∀ᶠ x : ℕ in Filter.atTop,
  5792. c * (x : ℝ) ≤
  5793. (((Finset.Icc 1 x).filter fun n ↦ n ∈ generatedSet).card : ℝ) :=
  5794. generatedSet_has_positive_lower_density
  5795. end Erdos424
  5796. alias _root_.Erdos424.erdos_424_lower_density := _root_.Erdos424.erdos_424