lean/FeedbackChannel/SelectionKTwo.lean

View on GitHub

  1import Mathlib.Tactic
  2import Mathlib.Data.Nat.Choose.Central
  3import Mathlib.Data.Nat.Choose.Vandermonde
  4import Mathlib.Algebra.BigOperators.Intervals
  5import Mathlib.Algebra.BigOperators.NatAntidiagonal
  6import Mathlib.Analysis.Real.Pi.Wallis
  7import Mathlib.Analysis.Complex.ExponentialBounds
  8
  9/-!
 10# The `K = 2` monotone-approach theorem (selection-KL note, D.5)
 11
 12Source: `paper/selection_law_kl_containment.md`, draft 0.3, §4 D.5; code
 13`src/spiral_ln/evaluation_selection_kl.py` (`scaled_gap_squared`, `centered_max`).
 14
 15Setting: `X₁, X₂` i.i.d. `Bin(n, ½)`, `Y_k = X_k - n/2`, `M_n(2) = E max(Y₁, Y₂) = n γ(n, 2, ½)`,
 16`a_n(2) = M_n(2)/√n`.
 17
 18**Theorem (`K = 2`).** `a_n(2)² = n C(2n, n)² / (4·16ⁿ)`,
 19`a_{n+1}(2)²/a_n(2)² = (2n + 1)²/(4n(n + 1)) = 1 + 1/(4n(n + 1)) > 1`, and `a_n(2) ↑ 1/(2√π)`.
 20*Proof steps.* `M_n(2) = ½ E|S - n|` with `S ~ Bin(2n, ½)` (`M2_eq_half_meanAbsDev`); de Moivre's
 21identity `E|S - n| = n C(2n, n)/4ⁿ` (`meanAbsDev_eq`); hence the closed form (`aSq_eq`) and the step
 22ratio (`aSq_succ_div`, `step_ratio_eq`, `step_ratio_gt_one`), so `a_n(2)` increases strictly
 23(`a_strictMonoOn`). The limit uses Mathlib's Wallis product: `a_n(2)² = n/(4(2n + 1) W_n)`
 24(`aSq_eq_wallis`), `a_n(2)² → 1/(4π)` (`tendsto_aSq`), `a_n(2) → 1/(2√π)` (`tendsto_a`). For the
 25KL-normalized ratio `r(n, 2, ½) = 2 a_n(2)/√(2λ₂)`: strictly increasing (`r_strictMonoOn`), limit
 26`1/(√π √(2λ₂))` (`tendsto_r`), never attained (`r_lt_lim`). The identification of that limit with
 27`c₂ = E[M₂]/√(2λ₂)` uses the Gaussian value `E[M₂] = 1/√π`, which is not formalized.
 28
 29Remark on the note: the telescoped product `a_n(2)² = (1/16) ∏_{j<n} (2j+1)²/((2j)(2j+2))` must
 30start at `j = 1` (the `j = 0` factor has a zero denominator); with `j = 1, …, n - 1` it is the
 31step ratio telescoped from `a_1(2)² = 1/16`.
 32
 33All expectations are finite sums against the binomial masses `C(n, x)/2ⁿ`.
 34-/
 35
 36namespace FeedbackChannel.SelectionKTwo
 37
 38open Finset Filter Topology Real
 39
 40/-- Mass of `Bin(n, ½)` at `x`. -/
 41noncomputable def binHalf (n x : ℕ) : ℝ := (n.choose x : ℝ) / 2 ^ n
 42
 43/-- `M_n(2) = E[max(Y₁, Y₂)]` with `Y_k = X_k - n/2` and `X₁, X₂` i.i.d. `Bin(n, ½)`. -/
 44noncomputable def M2 (n : ℕ) : ℝ :=
 45  ∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1),
 46    binHalf n x * binHalf n y * (max (x : ℝ) y - n / 2)
 47
 48/-- `E|S - n|` for `S ~ Bin(2n, ½)`. -/
 49noncomputable def meanAbsDev (n : ℕ) : ℝ :=
 50  ∑ s ∈ range (2 * n + 1), binHalf (2 * n) s * |(s : ℝ) - n|
 51
 52/-- `a_n(2)² = M_n(2)²/n`. -/
 53noncomputable def aSq (n : ℕ) : ℝ := M2 n ^ 2 / n
 54
 55/-- `a_n(2) = M_n(2)/√n = √n · γ(n, 2, ½)`. -/
 56noncomputable def a (n : ℕ) : ℝ := M2 n / √(n : ℝ)
 57
 58/-! ## De Moivre's mean absolute deviation -/
 59
 60/-- Telescoping partial sums: `∑_{s ≤ j} (s - (m+1)) C(2m+2, s) = -(m+1) C(2m+1, j)`. -/
 61theorem partial_sum (m j : ℕ) :
 62    ∑ s ∈ range (j + 1), ((s : ℝ) - (m + 1)) * ((2 * m + 2).choose s : ℝ) =
 63      -((m : ℝ) + 1) * ((2 * m + 1).choose j : ℝ) := by
 64  induction j with
 65  | zero => simp
 66  | succ j ih =>
 67    rw [sum_range_succ, ih]
 68    have h1 : (2 * m + 2) * (2 * m + 1).choose j = (2 * m + 2).choose (j + 1) * (j + 1) :=
 69      Nat.add_one_mul_choose_eq (2 * m + 1) j
 70    have h2 : (2 * m + 2).choose (j + 1) = (2 * m + 1).choose j + (2 * m + 1).choose (j + 1) :=
 71      Nat.choose_succ_succ _ _
 72    have h1' : ((2 : ℝ) * m + 2) * ((2 * m + 1).choose j : ℝ) =
 73        ((2 * m + 2).choose (j + 1) : ℝ) * ((j : ℝ) + 1) := by exact_mod_cast h1
 74    have h2' : ((2 * m + 2).choose (j + 1) : ℝ) =
 75        ((2 * m + 1).choose j : ℝ) + ((2 * m + 1).choose (j + 1) : ℝ) := by exact_mod_cast h2
 76    push_cast
 77    linear_combination (-1 : ℝ) * h1' - ((m : ℝ) + 1) * h2'
 78
 79/-- **De Moivre's identity** (integer form): `∑_s C(2n, s) |s - n| = n C(2n, n)`. -/
 80theorem sum_choose_abs (n : ℕ) :
 81    ∑ s ∈ range (2 * n + 1), ((2 * n).choose s : ℝ) * |(s : ℝ) - n| =
 82      n * ((2 * n).choose n : ℝ) := by
 83  rcases n with _ | m
 84  · simp
 85  · rw [show 2 * (m + 1) = 2 * m + 2 by ring]
 86    rw [← Finset.sum_range_add_sum_Ico _ (show m + 1 ≤ 2 * m + 2 + 1 by omega)]
 87    have hlow : ∑ s ∈ range (m + 1), ((2 * m + 2).choose s : ℝ) * |(s : ℝ) - ((m + 1 : ℕ) : ℝ)|
 88        = -∑ s ∈ range (m + 1), ((s : ℝ) - (m + 1)) * ((2 * m + 2).choose s : ℝ) := by
 89      rw [← Finset.sum_neg_distrib]
 90      apply Finset.sum_congr rfl
 91      intro s hs
 92      rw [mem_range] at hs
 93      have : (s : ℝ) < m + 1 := by exact_mod_cast hs
 94      rw [abs_of_neg (by push_cast; linarith)]
 95      push_cast
 96      ring
 97    have hup : ∑ s ∈ Ico (m + 1) (2 * m + 2 + 1),
 98          ((2 * m + 2).choose s : ℝ) * |(s : ℝ) - ((m + 1 : ℕ) : ℝ)|
 99        = ∑ s ∈ Ico (m + 1) (2 * m + 2 + 1), ((s : ℝ) - (m + 1)) * ((2 * m + 2).choose s : ℝ) := by
100      apply Finset.sum_congr rfl
101      intro s hs
102      rw [mem_Ico] at hs
103      have : (m : ℝ) + 1 ≤ s := by exact_mod_cast hs.1
104      rw [abs_of_nonneg (by push_cast; linarith)]
105      push_cast
106      ring
107    have htot := Finset.sum_range_add_sum_Ico
108      (fun s => ((s : ℝ) - (m + 1)) * ((2 * m + 2).choose s : ℝ)) (show m + 1 ≤ 2 * m + 2 + 1 by omega)
109    rw [partial_sum m (2 * m + 2), Nat.choose_eq_zero_of_lt (by omega : 2 * m + 1 < 2 * m + 2),
110      partial_sum m m] at htot
111    rw [hlow, hup, partial_sum m m]
112    have hsym : (2 * m + 2).choose (m + 1) = (2 * m + 1).choose m + (2 * m + 1).choose m := by
113      rw [show (2 * m + 2).choose (m + 1) = (2 * m + 1).choose m + (2 * m + 1).choose (m + 1) from
114        Nat.choose_succ_succ _ _, Nat.choose_symm_half]
115    have hsym' : ((2 * m + 2).choose (m + 1) : ℝ) =
116        ((2 * m + 1).choose m : ℝ) + ((2 * m + 1).choose m : ℝ) := by exact_mod_cast hsym
117    rw [hsym']
118    push_cast at htot ⊢
119    linarith
120
121/-- **De Moivre's identity**: `E|S - n| = n C(2n, n)/4ⁿ` for `S ~ Bin(2n, ½)` (the denominator is
122`4ⁿ`, as the note stresses). -/
123theorem meanAbsDev_eq (n : ℕ) : meanAbsDev n = n * ((2 * n).choose n : ℝ) / 4 ^ n := by
124  unfold meanAbsDev binHalf
125  have h4 : (2 : ℝ) ^ (2 * n) = 4 ^ n := by rw [pow_mul]; norm_num
126  rw [h4, ← sum_choose_abs n, Finset.sum_div]
127  apply Finset.sum_congr rfl
128  intro s _
129  ring
130
131/-! ## `M_n(2) = ½ E|S - n|` -/
132
133theorem binHalf_sum_centered (n : ℕ) :
134    ∑ x ∈ range (n + 1), binHalf n x * ((x : ℝ) - n / 2) = 0 := by
135  set T := ∑ x ∈ range (n + 1), binHalf n x * ((x : ℝ) - n / 2) with hT
136  have hrefl : T = -T := by
137    have := Finset.sum_range_reflect (fun x => binHalf n x * ((x : ℝ) - n / 2)) (n + 1)
138    rw [hT]
139    conv_lhs => rw [← this]
140    rw [← Finset.sum_neg_distrib]
141    apply Finset.sum_congr rfl
142    intro x hx
143    rw [mem_range] at hx
144    have hx' : x ≤ n := by omega
145    rw [show n + 1 - 1 - x = n - x by omega]
146    unfold binHalf
147    rw [Nat.choose_symm hx', Nat.cast_sub hx']
148    ring
149  linarith
150
151theorem binHalf_sum (n : ℕ) : ∑ x ∈ range (n + 1), binHalf n x = 1 := by
152  unfold binHalf
153  rw [← Finset.sum_div, ← Nat.cast_sum, Nat.sum_range_choose]
154  push_cast
155  exact div_self (by positivity)
156
157/-- Vandermonde in the form used here: the sum of `X₁ + X₂` over two independent `Bin(n, ·)`
158weights is the `Bin(2n, ·)` weight. -/
159theorem double_sum_choose (n : ℕ) (g : ℕ → ℝ) :
160    ∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1), (n.choose x : ℝ) * (n.choose y : ℝ) * g (x + y) =
161      ∑ s ∈ range (2 * n + 1), ((2 * n).choose s : ℝ) * g s := by
162  have hV : ∀ s, ((2 * n).choose s : ℝ) * g s =
163      ∑ k ∈ range (s + 1), (n.choose k : ℝ) * (n.choose (s - k) : ℝ) * g (k + (s - k)) := by
164    intro s
165    rw [show 2 * n = n + n by ring, Nat.add_choose_eq, Nat.cast_sum, Finset.sum_mul,
166      Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk]
167    apply Finset.sum_congr rfl
168    intro k hk
169    rw [mem_range] at hk
170    push_cast
171    rw [show k + (s - k) = s by omega]
172  simp_rw [hV]
173  rw [Finset.sum_range_diag_flip (2 * n + 1)
174    (fun k j => (n.choose k : ℝ) * (n.choose j : ℝ) * g (k + j))]
175  symm
176  rw [← Finset.sum_subset (range_subset_range.mpr (by omega : n + 1 ≤ 2 * n + 1))]
177  · apply Finset.sum_congr rfl
178    intro k hk
179    rw [mem_range] at hk
180    symm
181    apply Finset.sum_subset (range_subset_range.mpr (by omega))
182    intro j _ hj
183    rw [mem_range] at hj
184    rw [Nat.choose_eq_zero_of_lt (by omega : n < j)]
185    simp
186  · intro k _ hk
187    rw [mem_range] at hk
188    apply Finset.sum_eq_zero
189    intro j _
190    rw [Nat.choose_eq_zero_of_lt (by omega : n < k)]
191    simp
192
193/-- **`M_n(2) = ½ E|S - n|`** with `S ~ Bin(2n, ½)`: `max(X₁, X₂) = (X₁ + X₂)/2 + |X₁ - X₂|/2` and
194`X₁ - X₂ + n = X₁ + (n - X₂) ~ Bin(2n, ½)`. -/
195theorem M2_eq_half_meanAbsDev (n : ℕ) : M2 n = meanAbsDev n / 2 := by
196  have hmax : ∀ u v : ℝ, max u v = (u + v) / 2 + |u - v| / 2 := by
197    intro u v
198    rcases le_total u v with h | h
199    · rw [max_eq_right h, abs_of_nonpos (by linarith)]; ring
200    · rw [max_eq_left h, abs_of_nonneg (by linarith)]; ring
201  -- split `M2` into the centred-sum part and the absolute-difference part
202  have hsplit : M2 n =
203      (∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1),
204          binHalf n x * binHalf n y * (((x : ℝ) - n / 2) + ((y : ℝ) - n / 2))) / 2 +
205        (∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1),
206          binHalf n x * binHalf n y * |(x : ℝ) - y|) / 2 := by
207    unfold M2
208    rw [Finset.sum_div, Finset.sum_div, ← Finset.sum_add_distrib]
209    apply Finset.sum_congr rfl
210    intro x _
211    rw [Finset.sum_div, Finset.sum_div, ← Finset.sum_add_distrib]
212    apply Finset.sum_congr rfl
213    intro y _
214    rw [hmax]
215    ring
216  have hcentred : ∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1),
217      binHalf n x * binHalf n y * (((x : ℝ) - n / 2) + ((y : ℝ) - n / 2)) = 0 := by
218    have : ∀ x y, binHalf n x * binHalf n y * (((x : ℝ) - n / 2) + ((y : ℝ) - n / 2)) =
219        binHalf n y * (binHalf n x * ((x : ℝ) - n / 2)) +
220          binHalf n x * (binHalf n y * ((y : ℝ) - n / 2)) := by
221      intro x y; ring
222    have h1 : ∀ x : ℕ, ∑ y ∈ range (n + 1), binHalf n y * (binHalf n x * ((x : ℝ) - n / 2)) =
223        binHalf n x * ((x : ℝ) - n / 2) := by
224      intro x; rw [← Finset.sum_mul, binHalf_sum, one_mul]
225    have h2 : ∀ x : ℕ, ∑ y ∈ range (n + 1), binHalf n x * (binHalf n y * ((y : ℝ) - n / 2)) =
226        0 := by
227      intro x; rw [← Finset.mul_sum, binHalf_sum_centered, mul_zero]
228    simp_rw [this, Finset.sum_add_distrib, h1, h2, binHalf_sum_centered]
229    simp
230  -- reflect `y ↦ n - y` and apply Vandermonde
231  have habs : ∑ x ∈ range (n + 1), ∑ y ∈ range (n + 1), binHalf n x * binHalf n y * |(x : ℝ) - y|
232      = meanAbsDev n := by
233    have hrefl : ∀ x, ∑ y ∈ range (n + 1), binHalf n x * binHalf n y * |(x : ℝ) - y| =
234        ∑ y ∈ range (n + 1), binHalf n x * binHalf n y * |((x + y : ℕ) : ℝ) - n| := by
235      intro x
236      rw [← Finset.sum_range_reflect _ (n + 1)]
237      apply Finset.sum_congr rfl
238      intro y hy
239      rw [mem_range] at hy
240      have hy' : y ≤ n := by omega
241      rw [show n + 1 - 1 - y = n - y by omega]
242      unfold binHalf
243      rw [Nat.choose_symm hy', Nat.cast_sub hy']
244      push_cast
245      congr 1
246      rw [show (x : ℝ) - (n - y) = x + y - n by ring]
247    simp_rw [hrefl]
248    unfold meanAbsDev binHalf
249    have h4 : (2 : ℝ) ^ (2 * n) = 2 ^ n * 2 ^ n := by rw [two_mul, pow_add]
250    rw [h4]
251    have := double_sum_choose n (fun s => |(s : ℝ) - n| / (2 ^ n * 2 ^ n))
252    convert this using 1
253    · apply Finset.sum_congr rfl; intro x _
254      apply Finset.sum_congr rfl; intro y _
255      field_simp
256    · apply Finset.sum_congr rfl; intro s _
257      field_simp
258  rw [hsplit, hcentred, habs]
259  ring
260
261/-! ## The closed form and the step ratio -/
262
263/-- `M_n(2) = n C(2n, n)/(2·4ⁿ)`. -/
264theorem M2_eq (n : ℕ) : M2 n = n * ((2 * n).choose n : ℝ) / (2 * 4 ^ n) := by
265  rw [M2_eq_half_meanAbsDev, meanAbsDev_eq]
266  field_simp
267
268theorem M2_nonneg (n : ℕ) : 0 ≤ M2 n := by
269  rw [M2_eq]; positivity
270
271/-- **Theorem (`K = 2`), closed form:** `a_n(2)² = n C(2n, n)²/(4·16ⁿ)`. -/
272theorem aSq_eq (n : ℕ) : aSq n = n * ((2 * n).choose n : ℝ) ^ 2 / (4 * 16 ^ n) := by
273  unfold aSq
274  rw [M2_eq]
275  rcases Nat.eq_zero_or_pos n with rfl | hn
276  · simp
277  · have hn' : (n : ℝ) ≠ 0 := by positivity
278    have h16 : (16 : ℝ) ^ n = 4 ^ n * 4 ^ n := by rw [← mul_pow]; norm_num
279    rw [h16]
280    field_simp
281    ring
282
283theorem aSq_pos {n : ℕ} (hn : 1 ≤ n) : 0 < aSq n := by
284  rw [aSq_eq]
285  have : 0 < (2 * n).choose n := Nat.choose_pos (by omega)
286  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
287  positivity
288
289/-- **Theorem (`K = 2`), step ratio:**
290`a_{n+1}(2)²/a_n(2)² = (2n + 1)²/(4n(n + 1))` for `n ≥ 1`. -/
291theorem aSq_succ_div {n : ℕ} (hn : 1 ≤ n) :
292    aSq (n + 1) / aSq n = (2 * n + 1) ^ 2 / (4 * n * (n + 1)) := by
293  have hc := Nat.succ_mul_centralBinom_succ n
294  rw [Nat.centralBinom_eq_two_mul_choose, Nat.centralBinom_eq_two_mul_choose] at hc
295  have hc' : ((n : ℝ) + 1) * ((2 * (n + 1)).choose (n + 1) : ℝ) =
296      2 * (2 * n + 1) * ((2 * n).choose n : ℝ) := by exact_mod_cast hc
297  have hpos : 0 < (2 * n).choose n := Nat.choose_pos (by omega)
298  have hpos' : (0 : ℝ) < ((2 * n).choose n : ℝ) := by exact_mod_cast hpos
299  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
300  have hC : ((2 * (n + 1)).choose (n + 1) : ℝ) =
301      2 * (2 * n + 1) * ((2 * n).choose n : ℝ) / ((n : ℝ) + 1) := by
302    rw [← hc']; field_simp
303  rw [aSq_eq, aSq_eq, hC]
304  push_cast
305  rw [pow_succ]
306  field_simp
307  ring
308
309/-- The step ratio exceeds one: `(2n + 1)²/(4n(n + 1)) = 1 + 1/(4n(n + 1)) > 1`. -/
310theorem step_ratio_eq {n : ℕ} (hn : 1 ≤ n) :
311    ((2 * n + 1 : ℝ)) ^ 2 / (4 * n * (n + 1)) = 1 + 1 / (4 * n * (n + 1)) := by
312  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
313  field_simp
314  ring
315
316theorem step_ratio_gt_one {n : ℕ} (hn : 1 ≤ n) :
317    1 < ((2 * n + 1 : ℝ)) ^ 2 / (4 * n * (n + 1)) := by
318  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
319  rw [step_ratio_eq hn]
320  have : 0 < 1 / (4 * (n : ℝ) * (n + 1)) := by positivity
321  linarith
322
323/-- `a_n(2)²` increases strictly in `n ≥ 1`. -/
324theorem aSq_lt_succ {n : ℕ} (hn : 1 ≤ n) : aSq n < aSq (n + 1) := by
325  have h := aSq_succ_div hn
326  have hpos := aSq_pos hn
327  have hgt := step_ratio_gt_one hn
328  rw [← h, lt_div_iff₀ hpos] at hgt
329  linarith
330
331theorem a_eq_sqrt (n : ℕ) : a n = √(aSq n) := by
332  unfold a aSq
333  rw [Real.sqrt_div' _ (Nat.cast_nonneg n), Real.sqrt_sq (M2_nonneg n)]
334
335/-- **Theorem (`K = 2`), monotone approach:** `a_n(2)` increases strictly in `n ≥ 1`. -/
336theorem a_lt_succ {n : ℕ} (hn : 1 ≤ n) : a n < a (n + 1) := by
337  rw [a_eq_sqrt, a_eq_sqrt]
338  exact Real.sqrt_lt_sqrt (aSq_pos hn).le (aSq_lt_succ hn)
339
340theorem a_strictMonoOn : StrictMonoOn a (Set.Ici 1) := by
341  intro i hi j hj hij
342  simp only [Set.mem_Ici] at hi hj
343  induction j, hij using Nat.le_induction with
344  | base => exact a_lt_succ hi
345  | succ j hj' ih => exact (ih (by omega)).trans (a_lt_succ (by omega))
346
347/-! ## The limit `a_n(2) → 1/(2√π)` (Wallis) -/
348
349/-- `a_n(2)² = n/(4(2n + 1) W_n)` with `W_n` Mathlib's Wallis partial product. -/
350theorem aSq_eq_wallis (n : ℕ) : aSq n = n / (4 * (2 * n + 1) * Real.Wallis.W n) := by
351  rw [aSq_eq, Real.Wallis.W_eq_factorial_ratio]
352  have hf := Nat.add_choose_mul_factorial_mul_factorial n n
353  rw [show n + n = 2 * n by ring] at hf
354  have hf' : ((2 * n).choose n : ℝ) * (n.factorial : ℝ) * (n.factorial : ℝ) =
355      ((2 * n).factorial : ℝ) := by exact_mod_cast hf
356  have hfac : (0 : ℝ) < (n.factorial : ℝ) := by exact_mod_cast Nat.factorial_pos n
357  rw [← hf']
358  have h16 : (2 : ℝ) ^ (4 * n) = 16 ^ n := by rw [pow_mul]; norm_num
359  rw [h16]
360  rcases Nat.eq_zero_or_pos n with rfl | hn
361  · simp
362  · have hC : (0 : ℝ) < ((2 * n).choose n : ℝ) := by exact_mod_cast Nat.choose_pos (by omega)
363    field_simp
364
365/-- **Theorem (`K = 2`), limit:** `a_n(2)² → 1/(4π)`. -/
366theorem tendsto_aSq : Tendsto aSq atTop (𝓝 (1 / (4 * π))) := by
367  have hW := Real.Wallis.tendsto_W_nhds_pi_div_two
368  have hfrac : Tendsto (fun n : ℕ => (n : ℝ) / (2 * n + 1)) atTop (𝓝 (1 / 2)) := by
369    have h : ∀ n : ℕ, (n : ℝ) / (2 * n + 1) = 1 / 2 - (1 / 2) / (2 * n + 1) := by
370      intro n
371      have : (0 : ℝ) < 2 * n + 1 := by positivity
372      field_simp
373      ring
374    simp_rw [h]
375    have : Tendsto (fun n : ℕ => (1 / 2 : ℝ) / (2 * n + 1)) atTop (𝓝 0) := by
376      apply tendsto_const_nhds.div_atTop
377      apply Tendsto.atTop_add _ tendsto_const_nhds
378      exact tendsto_natCast_atTop_atTop.const_mul_atTop two_pos
379    simpa using (tendsto_const_nhds (x := (1 / 2 : ℝ))).sub this
380  have hlim := hfrac.div (hW.const_mul 4) (by positivity)
381  have heq : (1 / 2 : ℝ) / (4 * (π / 2)) = 1 / (4 * π) := by
382    field_simp
383  rw [heq] at hlim
384  refine hlim.congr fun n => ?_
385  rw [Pi.div_apply, aSq_eq_wallis, div_div]
386  ring
387
388/-- **Theorem (`K = 2`), limit:** `a_n(2) → 1/(2√π)`, approached strictly from below
389(`a_strictMonoOn`). -/
390theorem tendsto_a : Tendsto a atTop (𝓝 (1 / (2 * √π))) := by
391  have h := tendsto_aSq.sqrt
392  have heq : √(1 / (4 * π)) = 1 / (2 * √π) := by
393    rw [Real.sqrt_div' _ (by positivity), Real.sqrt_one, Real.sqrt_mul' _ pi_pos.le]
394    norm_num
395  rw [heq] at h
396  exact h.congr fun n => (a_eq_sqrt n).symm
397
398/-! ## The KL-normalized ratio `r(n, 2, ½)` -/
399
400/-- `λ₂ = ln 2 - 1/2`. -/
401noncomputable def lam2 : ℝ := log 2 - 1 / 2
402
403theorem lam2_pos : 0 < lam2 := by
404  unfold lam2
405  have := Real.log_two_gt_d9
406  norm_num at this ⊢
407  linarith
408
409/-- `r(n, 2, ½) = γ(n, 2, ½)/(σ_n √(2λ₂)) = 2 a_n(2)/√(2λ₂)` (note §4, D.5, with `σ_n = 1/(2√n)`). -/
410noncomputable def r (n : ℕ) : ℝ := 2 * a n / √(2 * lam2)
411
412/-- `r(n, 2, ½)` increases strictly in `n ≥ 1`. -/
413theorem r_strictMonoOn : StrictMonoOn r (Set.Ici 1) := by
414  intro i hi j hj hij
415  unfold r
416  have hpos : 0 < √(2 * lam2) := Real.sqrt_pos.mpr (by linarith [lam2_pos])
417  rw [div_lt_div_iff_of_pos_right hpos]
418  linarith [a_strictMonoOn hi hj hij]
419
420/-- `r(n, 2, ½) → 1/(√π √(2λ₂))`, which is `c₂ = E[M₂]/√(2λ₂)` once `E[M₂] = 1/√π` (the Gaussian
421value, not formalized here). -/
422theorem tendsto_r : Tendsto r atTop (𝓝 (1 / (√π * √(2 * lam2)))) := by
423  have h := (tendsto_a.const_mul 2).div_const (√(2 * lam2))
424  have heq : 2 * (1 / (2 * √π)) / √(2 * lam2) = 1 / (√π * √(2 * lam2)) := by
425    have : 0 < √π := Real.sqrt_pos.mpr pi_pos
426    have : 0 < √(2 * lam2) := Real.sqrt_pos.mpr (by linarith [lam2_pos])
427    field_simp
428  rw [heq] at h
429  exact h
430
431/-- The supremum over `n` of `r(n, 2, ½)` is the limit and is not attained. -/
432theorem r_lt_lim {n : ℕ} (hn : 1 ≤ n) : r n < 1 / (√π * √(2 * lam2)) := by
433  have h1 := r_strictMonoOn (Set.mem_Ici.mpr hn) (Set.mem_Ici.mpr (by omega : 1 ≤ n + 1))
434    (Nat.lt_succ_self n)
435  have h2 : r (n + 1) ≤ 1 / (√π * √(2 * lam2)) := by
436    apply ge_of_tendsto tendsto_r
437    rw [Filter.eventually_atTop]
438    exact ⟨n + 1, fun k hk => r_strictMonoOn.monotoneOn (Set.mem_Ici.mpr (by omega))
439      (Set.mem_Ici.mpr (by omega)) hk⟩
440  linarith
441
442end FeedbackChannel.SelectionKTwo