lean/FeedbackChannel/MultigenMixed.lean

View on GitHub

  1import Mathlib.Tactic
  2
  3/-!
  4# Mixed gain laws: the window-sum recursion and the distance-two closure (multigen note §6, 0.5)
  5
  6Source: `paper/evaluation_feedback_multigen.md`, draft 0.5, §6 "Mixed gain laws (0.5)"; code
  7`src/spiral_ln/evaluation_feedback_multigen.py` (`drift_kernel`, `mixed_window_sums`,
  8`mixed_count_structure`).
  9
 10**The chain.** States `x = (c, i)`: incumbent count `c ∈ {0, …, n}` and level `i`. From level `i` the
 11best of the generation's `K` candidates has count `J` and, if accepted, moves the level to `ℓ`
 12(`ℓ = min(top, i + μ)` for the accepted candidate's gain multiple `μ`, which may be `0`); the joint
 13law of `(J, ℓ)` is `ρ i J ℓ` (`drift_kernel` writes it as `q_μ Bin(n, p(i+μ))(J) S_J`). A rule with
 14cap `a(c)` accepts iff `J > a(c)` and then moves to `(J, ℓ)`; the naive rule has `a(c) = c`, a
 15threshold rule `a(c) = c + t(c)`. This file works with an arbitrary finite joint law `ρ` (one per
 16level, total mass one); the note's hypotheses enter as `gain_nonneg` (`ρ i J ℓ ≠ 0 → i ≤ ℓ`) and the
 17monotonicity of `H` in the level ("the best of `K` is stochastically non-decreasing in the level").
 18
 19**Objects (note's notation).** Target `m`; `u_k = P^k 1{c ≥ m}` (naive kernel `P`); `s_k = 1 - u_k`;
 20`H(i)` the probability that a draw from level `i` has count `≥ m`; for `a < m` the window sums
 21`W^a_k(x) = ∑_{c < J ≤ a} ρ(y) [s_k(x) - s_k(y)]` (which equal `(P u_k - Q u_k)(x)` for the rule
 22with `a(c) = a`), `E^a_k(x) = ∑_{(c,a]} ρ(y) (H(i_y) - H(i)) s_k(y)`, `B_a = ∑_{(c,a]} ρ(y)`,
 23`A = P(stay)`.
 24
 25**Formalized.**
 26* `window_recursion`: the exact recursion
 27  `W^a_k = (1 - H(i)) W^a_{k-1} + E^a_{k-1} + ∑_{y ∈ (c,a]} ρ(y) W^{m-1}_{k-1}(y) - B_a W^{m-1}_{k-1}(x)`.
 28* `E_nonneg`: `E^a ≥ 0`; `full_window_nonneg` / `L1`: the full-window slacks `W^{m-1} ≥ 0`, i.e.
 29  (L1) `s_{k+1}(x) ≤ (1 - H(i)) s_k(x)`, for every gain law (`full_window_nonneg_of_landing` is
 30  the same under the weaker hypothesis that every landing level has `H(ℓ) ≥ H(i)`).
 31* `distance_two_closure` and `distance_two_nonneg`: at `c = m - 2`,
 32  `W_k = A W_{k-1} + E_{k-1} ≥ 0`.
 33* `gated_le_naive_near_target`: hence one step of any threshold rule followed by naive steps is
 34  dominated, `(Q u_k)(x) ≤ (P u_k)(x)`, at every state with `c ≥ m - 2`.
 35-/
 36
 37namespace FeedbackChannel.MultigenMixed
 38
 39open Finset
 40
 41/-- Sum over counts `J ∈ {0, …, n}` and landing levels `ℓ ∈ {0, …, L - 1}`. -/
 42def S2 (n L : ℕ) (g : ℕ → ℕ → ℝ) : ℝ := ∑ J ∈ range (n + 1), ∑ ℓ ∈ range L, g J ℓ
 43
 44theorem S2_congr {n L : ℕ} {g h : ℕ → ℕ → ℝ} (H : ∀ J ℓ, g J ℓ = h J ℓ) : S2 n L g = S2 n L h :=
 45  Finset.sum_congr rfl fun J _ => Finset.sum_congr rfl fun ℓ _ => H J ℓ
 46
 47theorem S2_add (n L : ℕ) (g h : ℕ → ℕ → ℝ) :
 48    S2 n L (fun J ℓ => g J ℓ + h J ℓ) = S2 n L g + S2 n L h := by
 49  unfold S2; simp_rw [Finset.sum_add_distrib]
 50
 51theorem S2_sub (n L : ℕ) (g h : ℕ → ℕ → ℝ) :
 52    S2 n L (fun J ℓ => g J ℓ - h J ℓ) = S2 n L g - S2 n L h := by
 53  unfold S2; simp_rw [Finset.sum_sub_distrib]
 54
 55theorem S2_mul_left (n L : ℕ) (r : ℝ) (g : ℕ → ℕ → ℝ) :
 56    S2 n L (fun J ℓ => r * g J ℓ) = r * S2 n L g := by
 57  unfold S2; simp_rw [← Finset.mul_sum]
 58
 59theorem S2_mono {n L : ℕ} {g h : ℕ → ℕ → ℝ} (H : ∀ J ℓ, g J ℓ ≤ h J ℓ) : S2 n L g ≤ S2 n L h :=
 60  Finset.sum_le_sum fun J _ => Finset.sum_le_sum fun ℓ _ => H J ℓ
 61
 62theorem S2_nonneg {n L : ℕ} {g : ℕ → ℕ → ℝ} (H : ∀ J ℓ, 0 ≤ g J ℓ) : 0 ≤ S2 n L g :=
 63  Finset.sum_nonneg fun J _ => Finset.sum_nonneg fun ℓ _ => H J ℓ
 64
 65theorem S2_zero (n L : ℕ) : S2 n L (fun _ _ => 0) = 0 := by simp [S2]
 66
 67theorem S2_eq_zero {n L : ℕ} {g : ℕ → ℕ → ℝ} (H : ∀ J ℓ, g J ℓ = 0) : S2 n L g = 0 :=
 68  (S2_congr H).trans (S2_zero n L)
 69
 70/-- One generation's draw law: from level `i`, the best candidate has count `J` and its acceptance
 71would move the level to `ℓ`, with probability `ρ i J ℓ`. -/
 72structure DrawLaw (n L : ℕ) where
 73  ρ : ℕ → ℕ → ℕ → ℝ
 74  nonneg : ∀ i J ℓ, 0 ≤ ρ i J ℓ
 75  total : ∀ i, S2 n L (ρ i) = 1
 76
 77variable {n L : ℕ} (D : DrawLaw n L)
 78
 79/-- The one-generation operator of the rule with cap `cap` (accept iff `J > cap c`):
 80`(Q f)(c, i) = ∑ ρ i J ℓ · (f(c, i) if J ≤ cap c, else f(J, ℓ))`. The naive kernel is `cap = id`. -/
 81def kernel (cap : ℕ → ℕ) (f : ℕ × ℕ → ℝ) (x : ℕ × ℕ) : ℝ :=
 82  S2 n L fun J ℓ => D.ρ x.2 J ℓ * (if J ≤ cap x.1 then f x else f (J, ℓ))
 83
 84/-- The hitting indicator `1{c ≥ m}`. -/
 85def hit (m : ℕ) (x : ℕ × ℕ) : ℝ := if m ≤ x.1 then 1 else 0
 86
 87/-- `u_k = P^k 1{c ≥ m}` under the naive kernel `P`. -/
 88def u (m : ℕ) : ℕ → ℕ × ℕ → ℝ
 89  | 0 => hit m
 90  | k + 1 => kernel D id (u m k)
 91
 92/-- `s_k = 1 - u_k`. -/
 93def s (m k : ℕ) (x : ℕ × ℕ) : ℝ := 1 - u D m k x
 94
 95/-- `H(i)`: probability that a draw from level `i` has count `≥ m`. -/
 96def H (m i : ℕ) : ℝ := S2 n L fun J ℓ => if m ≤ J then D.ρ i J ℓ else 0
 97
 98/-- `A(x)`: probability that the naive rule stays at `x`. -/
 99def A (x : ℕ × ℕ) : ℝ := S2 n L fun J ℓ => if J ≤ x.1 then D.ρ x.2 J ℓ else 0
100
101/-- `B_a(x)`: mass of the landings with count in the window `(c, a]`. -/
102def B (a : ℕ) (x : ℕ × ℕ) : ℝ := S2 n L fun J ℓ => if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ else 0
103
104/-- The window sum `W^a_k(x) = ∑_{c < J ≤ a} ρ(y) [s_k(x) - s_k(y)]`. -/
105def W (m a k : ℕ) (x : ℕ × ℕ) : ℝ :=
106  S2 n L fun J ℓ => if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0
107
108/-- `E^a_k(x) = ∑_{(c,a]} ρ(y) (H(i_y) - H(i)) s_k(y)`. -/
109def E (m a k : ℕ) (x : ℕ × ℕ) : ℝ :=
110  S2 n L fun J ℓ =>
111    if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * (H D m ℓ - H D m x.2) * s D m k (J, ℓ) else 0
112
113/-- `∑_{y ∈ (c,a]} ρ(y) W^{m-1}_k(y)`: the landings' own full-window slacks. -/
114def landingSlack (m a k : ℕ) (x : ℕ × ℕ) : ℝ :=
115  S2 n L fun J ℓ => if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * W D m (m - 1) k (J, ℓ) else 0
116
117/-! ## Basic facts -/
118
119theorem kernel_bounds (cap : ℕ → ℕ) (f : ℕ × ℕ → ℝ) (hf : ∀ y, 0 ≤ f y ∧ f y ≤ 1)
120    (x : ℕ × ℕ) : 0 ≤ kernel D cap f x ∧ kernel D cap f x ≤ 1 := by
121  constructor
122  · apply S2_nonneg
123    intro J ℓ
124    apply mul_nonneg (D.nonneg _ _ _)
125    split_ifs
126    · exact (hf _).1
127    · exact (hf _).1
128  · rw [← D.total x.2]
129    apply S2_mono
130    intro J ℓ
131    have := D.nonneg x.2 J ℓ
132    split_ifs
133    · nlinarith [(hf x).2]
134    · nlinarith [(hf (J, ℓ)).2]
135
136theorem u_bounds (m k : ℕ) (x : ℕ × ℕ) : 0 ≤ u D m k x ∧ u D m k x ≤ 1 := by
137  induction k generalizing x with
138  | zero => unfold u hit; split_ifs <;> norm_num
139  | succ k ih => exact kernel_bounds D id _ ih x
140
141theorem s_nonneg (m k : ℕ) (x : ℕ × ℕ) : 0 ≤ s D m k x := by
142  unfold s; linarith [(u_bounds D m k x).2]
143
144/-- Above the target the naive chain has already hit: `u_k = 1` on `c ≥ m`. -/
145theorem u_top (m k : ℕ) (x : ℕ × ℕ) (hx : m ≤ x.1) : u D m k x = 1 := by
146  induction k generalizing x with
147  | zero => simp [u, hit, hx]
148  | succ k ih =>
149    show kernel D id (u D m k) x = 1
150    rw [← D.total x.2]
151    apply S2_congr
152    intro J ℓ
153    split_ifs with h
154    · rw [ih x hx, mul_one]
155    · rw [ih (J, ℓ) (by simp only [id] at h; simp only; omega), mul_one]
156
157theorem s_top (m k : ℕ) (y : ℕ × ℕ) (hy : m ≤ y.1) : s D m k y = 0 := by
158  unfold s; rw [u_top D m k y hy]; ring
159
160theorem s_zero_below (m : ℕ) (x : ℕ × ℕ) (hx : x.1 < m) : s D m 0 x = 1 := by
161  simp [s, u, hit, Nat.not_le.mpr hx]
162
163theorem A_nonneg (x : ℕ × ℕ) : 0 ≤ A D x :=
164  S2_nonneg fun J ℓ => by split_ifs <;> simp [D.nonneg]
165
166/-- The three regions `J ≤ c`, `c < J ≤ m - 1`, `J ≥ m` partition the draws when `c < m`:
167`A + B_{m-1} + H = 1`. -/
168theorem partition (m : ℕ) (x : ℕ × ℕ) (hx : x.1 < m) :
169    A D x + B D (m - 1) x + H D m x.2 = 1 := by
170  rw [← D.total x.2]
171  unfold A B H
172  rw [← S2_add, ← S2_add]
173  apply S2_congr
174  intro J ℓ
175  by_cases h1 : J ≤ x.1
176  · rw [ite_eq_left h1, ite_eq_right (by omega), ite_eq_right (by omega)]; ring
177  · by_cases h2 : J ≤ m - 1
178    · rw [ite_eq_right h1, ite_eq_left ⟨by omega, h2⟩, ite_eq_right (by omega)]; ring
179    · rw [ite_eq_right h1, ite_eq_right (by omega), ite_eq_left (by omega)]; ring
180
181/-- The difference between the naive and a gated step is the window sum over `(c, cap c]`. -/
182theorem kernel_sub (cap : ℕ → ℕ) (f : ℕ × ℕ → ℝ) (x : ℕ × ℕ) (hcap : x.1 ≤ cap x.1) :
183    kernel D id f x - kernel D cap f x =
184      S2 n L fun J ℓ => if x.1 < J ∧ J ≤ cap x.1 then D.ρ x.2 J ℓ * (f (J, ℓ) - f x) else 0 := by
185  unfold kernel
186  rw [← S2_sub]
187  apply S2_congr
188  intro J ℓ
189  simp only [id]
190  by_cases h1 : J ≤ x.1
191  · rw [ite_eq_left h1, ite_eq_left (by omega), ite_eq_right (by omega)]; ring
192  · by_cases h2 : J ≤ cap x.1
193    · rw [ite_eq_right h1, ite_eq_left h2, ite_eq_left ⟨by omega, h2⟩]; ring
194    · rw [ite_eq_right h1, ite_eq_right h2, ite_eq_right (by omega)]; ring
195
196/-- For `f = u_k` the gated deficit is exactly the window sum `W^{cap c}_k(x)`. -/
197theorem kernel_sub_u (cap : ℕ → ℕ) (m k : ℕ) (x : ℕ × ℕ) (hcap : x.1 ≤ cap x.1) :
198    kernel D id (u D m k) x - kernel D cap (u D m k) x = W D m (cap x.1) k x := by
199  rw [kernel_sub D cap _ x hcap]
200  unfold W s
201  apply S2_congr
202  intro J ℓ
203  split_ifs <;> ring
204
205/-- One naive step of the survival function below the target:
206`s_{k+1}(x) = (1 - H(i)) s_k(x) - W^{m-1}_k(x)` for `c < m`. -/
207theorem s_succ (m k : ℕ) (x : ℕ × ℕ) (hx : x.1 < m) :
208    s D m (k + 1) x = (1 - H D m x.2) * s D m k x - W D m (m - 1) k x := by
209  have h1 : s D m (k + 1) x =
210      S2 n L fun J ℓ => D.ρ x.2 J ℓ * (if J ≤ x.1 then s D m k x else s D m k (J, ℓ)) := by
211    show 1 - kernel D id (u D m k) x = _
212    unfold kernel
213    rw [← D.total x.2, ← S2_sub]
214    apply S2_congr
215    intro J ℓ
216    simp only [id, s]
217    split_ifs <;> ring
218  have h2 : ∀ J ℓ, D.ρ x.2 J ℓ * (if J ≤ x.1 then s D m k x else s D m k (J, ℓ)) =
219      D.ρ x.2 J ℓ * s D m k x - (if m ≤ J then D.ρ x.2 J ℓ else 0) * s D m k x -
220        (if x.1 < J ∧ J ≤ m - 1 then D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0) := by
221    intro J ℓ
222    by_cases hJ1 : J ≤ x.1
223    · rw [ite_eq_left hJ1, ite_eq_right (by omega), ite_eq_right (by omega)]; ring
224    · by_cases hJ2 : m ≤ J
225      · rw [ite_eq_right hJ1, ite_eq_left hJ2, ite_eq_right (by omega), s_top D m k (J, ℓ) hJ2]; ring
226      · rw [ite_eq_right hJ1, ite_eq_right hJ2, ite_eq_left ⟨by omega, by omega⟩]; ring
227  rw [h1, S2_congr h2, S2_sub, S2_sub]
228  unfold H W
229  rw [show (S2 n L fun J ℓ => D.ρ x.2 J ℓ * s D m k x) = S2 n L (D.ρ x.2) * s D m k x by
230      unfold S2; simp_rw [Finset.sum_mul],
231    show (S2 n L fun J ℓ => (if m ≤ J then D.ρ x.2 J ℓ else 0) * s D m k x) =
232      (S2 n L fun J ℓ => if m ≤ J then D.ρ x.2 J ℓ else 0) * s D m k x by
233      unfold S2; simp_rw [Finset.sum_mul],
234    D.total x.2]
235  ring
236
237/-! ## The window-sum recursion -/
238
239/-- **The window-sum recursion** (note §6, mixed gain laws, exact for `a < m`):
240`W^a_{k+1}(x) = (1 - H(i)) W^a_k(x) + E^a_k(x) + ∑_{y ∈ (c,a]} ρ(y) W^{m-1}_k(y) - B_a W^{m-1}_k(x)`. -/
241theorem window_recursion (m a k : ℕ) (ha : a < m) (x : ℕ × ℕ) :
242    W D m a (k + 1) x = (1 - H D m x.2) * W D m a k x + E D m a k x + landingSlack D m a k x -
243      B D a x * W D m (m - 1) k x := by
244  have hterm : ∀ J ℓ,
245      (if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * (s D m (k + 1) x - s D m (k + 1) (J, ℓ)) else 0) =
246        (1 - H D m x.2) *
247            (if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0) +
248          (if x.1 < J ∧ J ≤ a then
249              D.ρ x.2 J ℓ * (H D m ℓ - H D m x.2) * s D m k (J, ℓ) else 0) +
250          (if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ * W D m (m - 1) k (J, ℓ) else 0) -
251          W D m (m - 1) k x * (if x.1 < J ∧ J ≤ a then D.ρ x.2 J ℓ else 0) := by
252    intro J ℓ
253    split_ifs with h
254    · rw [s_succ D m k x (by omega), s_succ D m k (J, ℓ) (by simp only; omega)]
255      ring
256    · ring
257  unfold W
258  rw [S2_congr hterm, S2_sub, S2_add, S2_add, S2_mul_left, S2_mul_left]
259  unfold E landingSlack B W
260  ring
261
262/-- `E^a ≥ 0`: landings never lower the level (`gain_nonneg`) and `H` is non-decreasing in the
263level. -/
264theorem E_nonneg (m a k : ℕ) (x : ℕ × ℕ)
265    (gain_nonneg : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → i ≤ ℓ) (H_mono : Monotone (H D m)) :
266    0 ≤ E D m a k x := by
267  apply S2_nonneg
268  intro J ℓ
269  split_ifs
270  · by_cases hρ : D.ρ x.2 J ℓ = 0
271    · rw [hρ]; ring_nf; exact le_rfl
272    · have hle := H_mono (gain_nonneg x.2 J ℓ hρ)
273      apply mul_nonneg (mul_nonneg (D.nonneg _ _ _) (by linarith)) (s_nonneg D m k _)
274  · exact le_rfl
275
276/-- **(L1) for every gain law**, window form: the full-window slacks are non-negative,
277`W^{m-1}_k(x) ≥ 0` for every state below the target, whenever every possible landing level is at
278least as good as the current one for reaching `m` (`H(i) ≤ H(ℓ)` if `ρ i J ℓ ≠ 0`). -/
279theorem full_window_nonneg_of_landing (m : ℕ)
280    (hH : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → H D m i ≤ H D m ℓ) (k : ℕ) :
281    ∀ x : ℕ × ℕ, x.1 < m → 0 ≤ W D m (m - 1) k x := by
282  induction k with
283  | zero =>
284    intro x hx
285    apply le_of_eq
286    symm
287    unfold W
288    apply S2_eq_zero
289    intro J ℓ
290    split_ifs with h
291    · rw [s_zero_below D m x hx, s_zero_below D m (J, ℓ) (by simp only; omega)]; ring
292    · rfl
293  | succ k ih =>
294    intro x hx
295    -- each windowed term is at least `(1 - H(i)) ρ (s_k x - s_k y) - ρ W_k(x)`
296    have hterm : ∀ J ℓ,
297        (1 - H D m x.2) *
298            (if x.1 < J ∧ J ≤ m - 1 then D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0) -
299          W D m (m - 1) k x * (if x.1 < J ∧ J ≤ m - 1 then D.ρ x.2 J ℓ else 0) ≤
300        (if x.1 < J ∧ J ≤ m - 1 then
301            D.ρ x.2 J ℓ * (s D m (k + 1) x - s D m (k + 1) (J, ℓ)) else 0) := by
302      intro J ℓ
303      split_ifs with h
304      · have hy : (J, ℓ).1 < m := by simp only; omega
305        rw [s_succ D m k x hx, s_succ D m k (J, ℓ) hy]
306        have hWy := ih (J, ℓ) hy
307        have hsy := s_nonneg D m k (J, ℓ)
308        have hρ := D.nonneg x.2 J ℓ
309        by_cases hρ0 : D.ρ x.2 J ℓ = 0
310        · rw [hρ0]; simp
311        · have hH := hH x.2 J ℓ hρ0
312          have : (1 - H D m ℓ) * s D m k (J, ℓ) - W D m (m - 1) k (J, ℓ) ≤
313              (1 - H D m x.2) * s D m k (J, ℓ) := by nlinarith
314          nlinarith
315      · simp
316    have hsum := S2_mono (n := n) (L := L) hterm
317    rw [S2_sub, S2_mul_left, S2_mul_left] at hsum
318    change (1 - H D m x.2) * W D m (m - 1) k x - W D m (m - 1) k x * B D (m - 1) x ≤
319      W D m (m - 1) (k + 1) x at hsum
320    have hpart := partition D m x hx
321    have hA := A_nonneg D x
322    have hW := ih x hx
323    have : (1 - H D m x.2) * W D m (m - 1) k x - W D m (m - 1) k x * B D (m - 1) x =
324        A D x * W D m (m - 1) k x := by rw [← hpart]; ring
325    nlinarith
326
327/-- **(L1) for every gain law**, window form, under the note's hypotheses (gains `≥ 0`, `H`
328non-decreasing in the level). -/
329theorem full_window_nonneg (m : ℕ) (gain_nonneg : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → i ≤ ℓ)
330    (H_mono : Monotone (H D m)) (k : ℕ) :
331    ∀ x : ℕ × ℕ, x.1 < m → 0 ≤ W D m (m - 1) k x :=
332  full_window_nonneg_of_landing D m (fun i J ℓ h => H_mono (gain_nonneg i J ℓ h)) k
333
334/-- **(L1) for every gain law**: `s_{k+1}(x) ≤ (1 - H(i)) s_k(x)` below the target. -/
335theorem L1 (m : ℕ) (gain_nonneg : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → i ≤ ℓ) (H_mono : Monotone (H D m))
336    (k : ℕ) (x : ℕ × ℕ) (hx : x.1 < m) : s D m (k + 1) x ≤ (1 - H D m x.2) * s D m k x := by
337  rw [s_succ D m k x hx]
338  linarith [full_window_nonneg D m gain_nonneg H_mono k x hx]
339
340/-! ## The distance-two closure -/
341
342/-- At `c = m - 2` the landings' own windows `(m - 1, m - 1]` are empty. -/
343theorem landingSlack_eq_zero (m k : ℕ) (x : ℕ × ℕ) (hx : x.1 + 2 = m) :
344    landingSlack D m (m - 1) k x = 0 := by
345  unfold landingSlack
346  apply S2_eq_zero
347  intro J ℓ
348  split_ifs with h
349  · have : W D m (m - 1) k (J, ℓ) = 0 := by
350      unfold W
351      apply S2_eq_zero
352      intro J' ℓ'
353      rw [ite_eq_right (by simp only; omega)]
354    rw [this, mul_zero]
355  · rfl
356
357/-- **The distance-two closure** (note §6): at `c = m - 2` the negative term cancels and
358`W_{k+1} = A W_k + E_k`, for every gain law. -/
359theorem distance_two_closure (m k : ℕ) (x : ℕ × ℕ) (hx : x.1 + 2 = m) :
360    W D m (m - 1) (k + 1) x = A D x * W D m (m - 1) k x + E D m (m - 1) k x := by
361  rw [window_recursion D m (m - 1) k (by omega) x, landingSlack_eq_zero D m k x hx]
362  have hpart := partition D m x (by omega)
363  have : A D x = 1 - H D m x.2 - B D (m - 1) x := by linarith
364  rw [this]
365  ring
366
367/-- **The distance-two theorem** (note §6): `W_k ≥ 0` at `c = m - 2` for every `k`, for every
368gain law with non-negative gains and `H` non-decreasing in the level. -/
369theorem distance_two_nonneg (m : ℕ) (gain_nonneg : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → i ≤ ℓ)
370    (H_mono : Monotone (H D m)) (x : ℕ × ℕ) (hx : x.1 + 2 = m) (k : ℕ) :
371    0 ≤ W D m (m - 1) k x := by
372  induction k with
373  | zero =>
374    apply le_of_eq
375    symm
376    unfold W
377    apply S2_eq_zero
378    intro J ℓ
379    split_ifs with h
380    · rw [s_zero_below D m x (by omega), s_zero_below D m (J, ℓ) (by simp only; omega)]; ring
381    · rfl
382  | succ k ih =>
383    rw [distance_two_closure D m k x hx]
384    have := E_nonneg D m (m - 1) k x gain_nonneg H_mono
385    have := A_nonneg D x
386    positivity
387
388/-- **Consequence:** one step of any threshold rule (cap `a(c) = c + t(c)`) followed by naive steps
389is dominated by the naive rule for the hitting objective `1{c ≥ m}`, `(Q u_k)(x) ≤ (P u_k)(x)`, at
390every state with `c ≥ m - 2`, for every gain law. (At `c ≥ m - 1` this is immediate; at `c = m - 2`
391it is the distance-two theorem.) -/
392theorem gated_le_naive_near_target (m : ℕ) (t : ℕ → ℕ)
393    (gain_nonneg : ∀ i J ℓ, D.ρ i J ℓ ≠ 0 → i ≤ ℓ) (H_mono : Monotone (H D m)) (k : ℕ)
394    (x : ℕ × ℕ) (hx : m ≤ x.1 + 2) :
395    kernel D (fun c => c + t c) (u D m k) x ≤ kernel D id (u D m k) x := by
396  have hdiff := kernel_sub D (fun c => c + t c) (u D m k) x (by simp)
397  suffices h : 0 ≤ S2 n L fun J ℓ =>
398      if x.1 < J ∧ J ≤ x.1 + t x.1 then D.ρ x.2 J ℓ * (u D m k (J, ℓ) - u D m k x) else 0 by
399    linarith
400  -- split the window at the target
401  have hsplit : ∀ J ℓ,
402      (if x.1 < J ∧ J ≤ x.1 + t x.1 then D.ρ x.2 J ℓ * (u D m k (J, ℓ) - u D m k x) else 0) =
403        (if x.1 < J ∧ J ≤ x.1 + t x.1 ∧ J < m then
404            D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0) +
405          (if x.1 < J ∧ J ≤ x.1 + t x.1 ∧ m ≤ J then D.ρ x.2 J ℓ * (1 - u D m k x) else 0) := by
406    intro J ℓ
407    by_cases h : x.1 < J ∧ J ≤ x.1 + t x.1
408    · by_cases hm : J < m
409      · rw [ite_eq_left h, ite_eq_left ⟨h.1, h.2, hm⟩, ite_eq_right (by omega)]
410        unfold s; ring
411      · rw [ite_eq_left h, ite_eq_right (by omega), ite_eq_left ⟨h.1, h.2, by omega⟩,
412          u_top D m k (J, ℓ) (by simp only; omega)]
413        ring
414    · rw [ite_eq_right h, ite_eq_right (by tauto), ite_eq_right (by tauto)]; ring
415  rw [S2_congr hsplit, S2_add]
416  apply add_nonneg
417  · -- the part below the target: empty, or the full window at `c = m - 2`
418    by_cases hc : x.1 + 2 = m ∧ m - 1 ≤ x.1 + t x.1
419    · have : (S2 n L fun J ℓ => if x.1 < J ∧ J ≤ x.1 + t x.1 ∧ J < m then
420            D.ρ x.2 J ℓ * (s D m k x - s D m k (J, ℓ)) else 0) = W D m (m - 1) k x := by
421        unfold W
422        apply S2_congr
423        intro J ℓ
424        by_cases h : x.1 < J ∧ J ≤ x.1 + t x.1 ∧ J < m
425        · rw [ite_eq_left h, ite_eq_left ⟨h.1, by omega⟩]
426        · rw [ite_eq_right h, ite_eq_right (by omega)]
427      rw [this]
428      exact distance_two_nonneg D m gain_nonneg H_mono x hc.1 k
429    · apply le_of_eq
430      symm
431      apply S2_eq_zero
432      intro J ℓ
433      rw [ite_eq_right (by omega)]
434  · apply S2_nonneg
435    intro J ℓ
436    split_ifs
437    · exact mul_nonneg (D.nonneg _ _ _) (by linarith [(u_bounds D m k x).2])
438    · exact le_rfl
439
440end FeedbackChannel.MultigenMixed