lean/FeedbackChannel/MultigenCountDominance.lean

View on GitHub

  1import FeedbackChannel.MultigenMixed
  2
  3/-!
  4# Proposition 9: dominance in count under a fixed gain (multigen note §6)
  5
  6Source: `paper/evaluation_feedback_multigen.md`, draft 0.5, §6, Proposition 9; code
  7`src/spiral_ln/evaluation_feedback_multigen.py` (`count_dominance_lemma_fixture`).
  8
  9**Fixed gain.** Every accepted candidate raises the level by one lattice step: from level `i` the
 10best candidate's count has law `π_i` and acceptance moves the level to `nx i` (`i + 1`, or the top
 11level, which is absorbing in the level). The hypothesis of the note's proof is that the best of
 12`K` is stochastically non-decreasing in the level; it enters only as `hstoch`:
 13`P(M_i ≥ m) ≤ P(M_{nx i} ≥ m)` for every `m` and `i`.
 14
 15**Proposition 9.** For every threshold rule `t ≥ 0`, every state `x` and every `g`,
 16`P^t_x(c_g ≥ m) ≤ P^0_x(c_g ≥ m)` for every `m` (stochastic dominance of the count;
 17`gated_hit_le`), hence `E^t_x[ψ(c_g)] ≤ E^0_x[ψ(c_g)]` for every non-decreasing `ψ`
 18(`gated_expect_le`); one gated step followed by naive steps is already dominated
 19(`one_gated_step_le`).
 20
 21*Proof (the note's lemmas).* `u_k = P^k 1{c ≥ m}`, `s_k = 1 - u_k`.
 22(L1) `s_{k+1}(x) ≤ (1 - H(x)) s_k(x)` — `MultigenMixed.full_window_nonneg_of_landing`.
 23(L2) `s_k(c, i) ≤ s_k(c + 1, i)` for `c + 1 < m` — `L2`.
 24(L3) `s_k(c, nx i) ≤ s_k(c, i)` — `L3`, through the comparison sequence `R` (`R_le_s`, `s_le_R`).
 25(L4) every window sum `W_a = (P u_k - Q u_k)(x)` is `≥ 0` — `L4`; then
 26`Q^k f = Q(Q^{k-1} f) ≤ Q(P^{k-1} f) ≤ P^k f`.
 27-/
 28
 29namespace FeedbackChannel.MultigenCountDominance
 30
 31open Finset FeedbackChannel.MultigenMixed
 32
 33/-- A fixed-gain draw law: from level `i` the best count has law `π i`, and acceptance moves the
 34level to `nx i < L`. -/
 35structure FixedGain (n L : ℕ) where
 36  π : ℕ → ℕ → ℝ
 37  nx : ℕ → ℕ
 38  nonneg : ∀ i J, 0 ≤ π i J
 39  total : ∀ i, ∑ J ∈ range (n + 1), π i J = 1
 40  nx_lt : ∀ i, nx i < L
 41
 42variable {n L : ℕ} (F : FixedGain n L)
 43
 44/-- The fixed-gain law as a joint law of (count, landing level). -/
 45def FixedGain.toDrawLaw : DrawLaw n L where
 46  ρ i J ℓ := if ℓ = F.nx i then F.π i J else 0
 47  nonneg i J ℓ := by
 48    show 0 ≤ (if ℓ = F.nx i then F.π i J else 0)
 49    split_ifs
 50    · exact F.nonneg i J
 51    · exact le_rfl
 52  total i := by
 53    unfold S2
 54    rw [← F.total i]
 55    apply Finset.sum_congr rfl
 56    intro J _
 57    rw [Finset.sum_ite_eq' (range L) (F.nx i) (fun _ => F.π i J),
 58      ite_eq_left (mem_range.mpr (F.nx_lt i))]
 59
 60/-- `P(M_i ≥ m)`. -/
 61def tail (m i : ℕ) : ℝ := ∑ J ∈ range (n + 1), if m ≤ J then F.π i J else 0
 62
 63/-- `P(M_i ≤ c)`: the probability of staying at count `c`. -/
 64def stay (c i : ℕ) : ℝ := ∑ J ∈ range (n + 1), if J ≤ c then F.π i J else 0
 65
 66/-- `P(c < M_i ≤ m - 1)`. -/
 67def mid (m c i : ℕ) : ℝ := ∑ J ∈ range (n + 1), if c < J ∧ J ≤ m - 1 then F.π i J else 0
 68
 69theorem S2_conc (i : ℕ) (h : ℕ → ℕ → ℝ) (hz : ∀ J ℓ, ℓ ≠ F.nx i → h J ℓ = 0) :
 70    S2 n L h = ∑ J ∈ range (n + 1), h J (F.nx i) := by
 71  unfold S2
 72  apply Finset.sum_congr rfl
 73  intro J _
 74  rw [Finset.sum_eq_single (F.nx i)]
 75  · intro ℓ _ hℓ; exact hz J ℓ hℓ
 76  · intro h'; exact absurd (mem_range.mpr (F.nx_lt i)) h'
 77
 78theorem rho_eq (i J ℓ : ℕ) : F.toDrawLaw.ρ i J ℓ = if ℓ = F.nx i then F.π i J else 0 := rfl
 79
 80theorem H_eq (m i : ℕ) : H F.toDrawLaw m i = tail F m i := by
 81  unfold H tail
 82  rw [S2_conc F i]
 83  · simp [rho_eq]
 84  · intro J ℓ hℓ; simp [rho_eq, hℓ]
 85
 86theorem A_eq (c i : ℕ) : A F.toDrawLaw (c, i) = stay F c i := by
 87  unfold A stay
 88  rw [S2_conc F i]
 89  · simp [rho_eq]
 90  · intro J ℓ hℓ; simp [rho_eq, hℓ]
 91
 92theorem B_eq (m c i : ℕ) : B F.toDrawLaw (m - 1) (c, i) = mid F m c i := by
 93  unfold B mid
 94  rw [S2_conc F i]
 95  · simp [rho_eq]
 96  · intro J ℓ hℓ; simp [rho_eq, hℓ]
 97
 98theorem kernel_eq (cap : ℕ → ℕ) (f : ℕ × ℕ → ℝ) (c i : ℕ) :
 99    kernel F.toDrawLaw cap f (c, i) =
100      ∑ J ∈ range (n + 1), F.π i J * (if J ≤ cap c then f (c, i) else f (J, F.nx i)) := by
101  unfold kernel
102  rw [S2_conc F i]
103  · simp [rho_eq]
104  · intro J ℓ hℓ; simp [rho_eq, hℓ]
105
106theorem W_eq (m a k c i : ℕ) : W F.toDrawLaw m a k (c, i) =
107    ∑ J ∈ range (n + 1), if c < J ∧ J ≤ a then
108      F.π i J * (s F.toDrawLaw m k (c, i) - s F.toDrawLaw m k (J, F.nx i)) else 0 := by
109  unfold W
110  rw [S2_conc F i]
111  · apply Finset.sum_congr rfl; intro J _; simp [rho_eq]
112  · intro J ℓ hℓ; simp [rho_eq, hℓ]
113
114theorem stay_nonneg (c i : ℕ) : 0 ≤ stay F c i :=
115  Finset.sum_nonneg fun J _ => by split_ifs <;> simp [F.nonneg]
116
117/-- The landing level is never worse for reaching `m`. -/
118theorem landing_le (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) :
119    ∀ i J ℓ, F.toDrawLaw.ρ i J ℓ ≠ 0 → H F.toDrawLaw m i ≤ H F.toDrawLaw m ℓ := by
120  intro i J ℓ h
121  rw [rho_eq] at h
122  have : ℓ = F.nx i := by
123    by_contra hne; exact h (ite_eq_right hne)
124  rw [this, H_eq, H_eq]
125  exact hstoch i
126
127/-- One naive step of `s` in the fixed-gain chain, below the target:
128`s_{k+1}(c, i) = P(stay) s_k(c, i) + ∑_{c < J ≤ m-1} π_i(J) s_k(J, nx i)`. -/
129theorem s_step (m k c i : ℕ) (hc : c < m) :
130    s F.toDrawLaw m (k + 1) (c, i) = stay F c i * s F.toDrawLaw m k (c, i) +
131      ∑ J ∈ range (n + 1),
132        if c < J ∧ J ≤ m - 1 then F.π i J * s F.toDrawLaw m k (J, F.nx i) else 0 := by
133  have h1 : s F.toDrawLaw m (k + 1) (c, i) = ∑ J ∈ range (n + 1), F.π i J *
134      (if J ≤ c then s F.toDrawLaw m k (c, i) else s F.toDrawLaw m k (J, F.nx i)) := by
135    show 1 - kernel F.toDrawLaw id (u F.toDrawLaw m k) (c, i) = _
136    rw [kernel_eq, ← F.total i, ← Finset.sum_sub_distrib]
137    apply Finset.sum_congr rfl
138    intro J _
139    simp only [id, s]
140    split_ifs <;> ring
141  rw [h1]
142  unfold stay
143  rw [Finset.sum_mul, ← Finset.sum_add_distrib]
144  apply Finset.sum_congr rfl
145  intro J _
146  by_cases hJ1 : J ≤ c
147  · rw [ite_eq_left hJ1, ite_eq_left hJ1, ite_eq_right (by omega)]; ring
148  · by_cases hJ2 : m ≤ J
149    · rw [ite_eq_right hJ1, ite_eq_right hJ1, ite_eq_right (by omega),
150        s_top F.toDrawLaw m k (J, F.nx i) hJ2]
151      ring
152    · rw [ite_eq_right hJ1, ite_eq_right hJ1, ite_eq_left ⟨by omega, by omega⟩]; ring
153
154theorem partition_eq (m c i : ℕ) (hc : c < m) : stay F c i + mid F m c i + tail F m i = 1 := by
155  have := partition F.toDrawLaw m (c, i) hc
156  rwa [A_eq, B_eq, H_eq] at this
157
158/-- (L1) in the fixed-gain chain: `s_{k+1}(c, i) ≤ (1 - P(M_i ≥ m)) s_k(c, i)`. -/
159theorem L1 (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (k c i : ℕ) (hc : c < m) :
160    s F.toDrawLaw m (k + 1) (c, i) ≤ (1 - tail F m i) * s F.toDrawLaw m k (c, i) := by
161  rw [s_succ F.toDrawLaw m k (c, i) hc, H_eq]
162  have := full_window_nonneg_of_landing F.toDrawLaw m (landing_le F m hstoch) k (c, i) hc
163  linarith
164
165/-! ## (L2) and (L3) -/
166
167/-- (L2) at horizon `k`, as a proposition. -/
168def L2At (m k : ℕ) : Prop :=
169  ∀ c i, c + 1 < m → s F.toDrawLaw m k (c, i) ≤ s F.toDrawLaw m k (c + 1, i)
170
171theorem L2At_chain {m k : ℕ} (h : L2At F m k) (i : ℕ) {c J : ℕ} (hcJ : c ≤ J) (hJ : J < m) :
172    s F.toDrawLaw m k (c, i) ≤ s F.toDrawLaw m k (J, i) := by
173  induction J, hcJ using Nat.le_induction with
174  | base => exact le_rfl
175  | succ J hcJ ih => exact (ih (by omega)).trans (h J i hJ)
176
177/-- The comparison sequence of (L3): wait at `(c, i)`, then run the chain from `(c, nx i)`. -/
178noncomputable def R (m c i : ℕ) : ℕ → ℝ
179  | 0 => 1
180  | j + 1 => stay F c i * R m c i j + mid F m c i * s F.toDrawLaw m j (c, F.nx i)
181
182theorem R_le_s (m k c i : ℕ) (hc : c < m) (hL2 : ∀ j < k, L2At F m j) :
183    ∀ j ≤ k, R F m c i j ≤ s F.toDrawLaw m j (c, i) := by
184  intro j
185  induction j with
186  | zero => intro _; rw [R, s_zero_below F.toDrawLaw m (c, i) hc]
187  | succ j ih =>
188    intro hj
189    rw [R, s_step F m j c i hc]
190    have hR := ih (by omega)
191    have h1 : stay F c i * R F m c i j ≤ stay F c i * s F.toDrawLaw m j (c, i) :=
192      mul_le_mul_of_nonneg_left hR (stay_nonneg F c i)
193    have h2 : mid F m c i * s F.toDrawLaw m j (c, F.nx i) ≤ ∑ J ∈ range (n + 1),
194        if c < J ∧ J ≤ m - 1 then F.π i J * s F.toDrawLaw m j (J, F.nx i) else 0 := by
195      unfold mid
196      rw [Finset.sum_mul]
197      apply Finset.sum_le_sum
198      intro J _
199      split_ifs with h
200      · have := L2At_chain F (hL2 j (by omega)) (F.nx i) (le_of_lt h.1) (by omega)
201        exact mul_le_mul_of_nonneg_left this (F.nonneg i J)
202      · simp
203    linarith
204
205theorem s_le_R (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (c i : ℕ) (hc : c < m) :
206    ∀ j, s F.toDrawLaw m j (c, F.nx i) ≤ R F m c i j := by
207  intro j
208  induction j with
209  | zero => rw [R, s_zero_below F.toDrawLaw m (c, F.nx i) hc]
210  | succ j ih =>
211    rw [R]
212    have hL1 := L1 F m hstoch j c (F.nx i) hc
213    have hσ := s_nonneg F.toDrawLaw m j (c, F.nx i)
214    have hH := hstoch i
215    have hpart := partition_eq F m c i hc
216    have hA := stay_nonneg F c i
217    have : (1 - tail F m (F.nx i)) * s F.toDrawLaw m j (c, F.nx i) ≤
218        (stay F c i + mid F m c i) * s F.toDrawLaw m j (c, F.nx i) := by
219      apply mul_le_mul_of_nonneg_right _ hσ
220      linarith
221    nlinarith
222
223/-- (L3) at horizon `k`, given (L2) at every earlier horizon. -/
224theorem L3_of_L2 (m k : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i))
225    (hL2 : ∀ j < k, L2At F m j) (c i : ℕ) (hc : c < m) :
226    s F.toDrawLaw m k (c, F.nx i) ≤ s F.toDrawLaw m k (c, i) :=
227  (s_le_R F m hstoch c i hc k).trans (R_le_s F m k c i hc hL2 k le_rfl)
228
229/-- **(L2)** `s_k(c, i) ≤ s_k(c + 1, i)` for `c + 1 < m`, every `k`. -/
230theorem L2 (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (k : ℕ) : L2At F m k := by
231  induction k using Nat.strong_induction_on with
232  | _ k ih =>
233    intro c i hc
234    rcases k with _ | j
235    · rw [s_zero_below F.toDrawLaw m (c, i) (by simp only; omega),
236        s_zero_below F.toDrawLaw m (c + 1, i) (by simp only; omega)]
237    · have hL2j := ih j (by omega)
238      have hL3j := L3_of_L2 F m j hstoch (fun j' hj' => ih j' (by omega)) (c + 1) i hc
239      rw [s_step F m j c i (by omega), s_step F m j (c + 1) i hc]
240      unfold stay
241      rw [Finset.sum_mul, Finset.sum_mul, ← sub_nonneg, add_sub_add_comm, ← Finset.sum_sub_distrib,
242        ← Finset.sum_sub_distrib, ← Finset.sum_add_distrib]
243      apply Finset.sum_nonneg
244      intro J _
245      have hπ := F.nonneg i J
246      have hL2c := hL2j c i (by omega)
247      by_cases h1 : J ≤ c
248      · rw [ite_eq_left (by omega : J ≤ c + 1), ite_eq_left h1, ite_eq_right (by omega),
249          ite_eq_right (by omega)]
250        nlinarith
251      · by_cases h2 : J = c + 1
252        · subst h2
253          rw [ite_eq_left le_rfl, ite_eq_right h1, ite_eq_right (by omega),
254            ite_eq_left ⟨by omega, by omega⟩]
255          nlinarith
256        · rw [ite_eq_right (by omega : ¬ J ≤ c + 1), ite_eq_right h1]
257          by_cases h3 : J ≤ m - 1
258          · rw [ite_eq_left ⟨by omega, h3⟩, ite_eq_left ⟨by omega, h3⟩]; ring_nf; exact le_rfl
259          · rw [ite_eq_right (by omega), ite_eq_right (by omega)]; ring_nf; exact le_rfl
260
261/-- **(L3)** `s_k(c, nx i) ≤ s_k(c, i)` for `c < m`, every `k`. -/
262theorem L3 (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (k c i : ℕ) (hc : c < m) :
263    s F.toDrawLaw m k (c, F.nx i) ≤ s F.toDrawLaw m k (c, i) :=
264  L3_of_L2 F m k hstoch (fun j _ => L2 F m hstoch j) c i hc
265
266/-! ## (L4) and dominance -/
267
268/-- **(L4)** every window sum is non-negative: `W_a(x) = ∑_{c < J ≤ a} π_i(J) [u_k(J, nx i) - u_k(c, i)] ≥ 0`. -/
269theorem L4 (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (a k c i : ℕ) :
270    0 ≤ W F.toDrawLaw m a k (c, i) := by
271  rw [W_eq]
272  by_cases hc : m ≤ c
273  · apply Finset.sum_nonneg
274    intro J _
275    split_ifs with h
276    · rw [s_top F.toDrawLaw m k (c, i) hc, s_top F.toDrawLaw m k (J, F.nx i) (by simp only; omega)]
277      simp
278    · exact le_rfl
279  push Not at hc
280  set sx := s F.toDrawLaw m k (c, i)
281  set f : ℕ → ℝ := fun J => s F.toDrawLaw m k (J, F.nx i)
282  -- the full window below the target is non-negative (L1)
283  have hfull : 0 ≤ ∑ J ∈ range (n + 1),
284      if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0 := by
285    have := full_window_nonneg_of_landing F.toDrawLaw m (landing_le F m hstoch) k (c, i) hc
286    rwa [W_eq] at this
287  have hmono : ∀ J J', c < J → J ≤ J' → J' ≤ m - 1 → f J ≤ f J' := by
288    intro J J' hJ hJJ' hJ'
289    exact L2At_chain F (L2 F m hstoch k) (F.nx i) hJJ' (by omega)
290  -- split the window at the target
291  have hsplit : ∀ J, (if c < J ∧ J ≤ a then F.π i J * (sx - f J) else 0) =
292      (if J ≤ a then (if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0) else 0) +
293        (if c < J ∧ J ≤ a ∧ m ≤ J then F.π i J * sx else 0) := by
294    intro J
295    by_cases h1 : c < J ∧ J ≤ a
296    · by_cases h2 : m ≤ J
297      · rw [ite_eq_left h1, ite_eq_left h1.2, ite_eq_right (by omega), ite_eq_left ⟨h1.1, h1.2, h2⟩]
298        simp only [f]
299        rw [s_top F.toDrawLaw m k (J, F.nx i) h2]
300        ring
301      · rw [ite_eq_left h1, ite_eq_left h1.2, ite_eq_left ⟨h1.1, by omega⟩,
302          ite_eq_right (by omega)]
303        ring
304    · rw [ite_eq_right h1]
305      by_cases h3 : J ≤ a
306      · have h4 : ¬ c < J := fun h => h1 ⟨h, h3⟩
307        rw [ite_eq_left h3, ite_eq_right (fun h => h4 h.1), ite_eq_right (fun h => h4 h.1)]
308        ring
309      · rw [ite_eq_right h3, ite_eq_right (fun h => h3 h.2.1)]; ring
310  rw [Finset.sum_congr rfl (fun J _ => hsplit J), Finset.sum_add_distrib]
311  apply add_nonneg
312  · -- the prefix of the full window
313    by_contra hneg
314    push Not at hneg
315    obtain ⟨J0, hJ0, hlt⟩ := Finset.exists_lt_of_sum_lt (s := range (n + 1))
316      (f := fun J => if J ≤ a then (if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0) else 0)
317      (g := fun _ => (0 : ℝ)) (by simpa using hneg)
318    have hJ0a : J0 ≤ a := by by_contra h; rw [ite_eq_right h] at hlt; exact lt_irrefl _ hlt
319    rw [ite_eq_left hJ0a] at hlt
320    have hJ0w : c < J0 ∧ J0 ≤ m - 1 := by
321      by_contra h; rw [ite_eq_right h] at hlt; exact lt_irrefl _ hlt
322    rw [ite_eq_left hJ0w] at hlt
323    have hsx : sx < f J0 := by
324      by_contra h
325      push Not at h
326      have := mul_nonneg (F.nonneg i J0) (sub_nonneg.mpr h)
327      linarith
328    -- the suffix of the full window is non-positive
329    have hsuf : ∑ J ∈ range (n + 1),
330        (if J ≤ a then 0 else (if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0)) ≤ 0 := by
331      apply Finset.sum_nonpos
332      intro J _
333      split_ifs with ha hw
334      · exact le_rfl
335      · have := hmono J0 J hJ0w.1 (by omega) hw.2
336        exact mul_nonpos_of_nonneg_of_nonpos (F.nonneg i J) (by linarith)
337      · exact le_rfl
338    have hdecomp : (∑ J ∈ range (n + 1),
339        if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0) =
340        (∑ J ∈ range (n + 1),
341          if J ≤ a then (if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0) else 0) +
342        ∑ J ∈ range (n + 1),
343          (if J ≤ a then 0 else (if c < J ∧ J ≤ m - 1 then F.π i J * (sx - f J) else 0)) := by
344      rw [← Finset.sum_add_distrib]
345      apply Finset.sum_congr rfl
346      intro J _
347      split_ifs <;> ring
348    linarith
349  · apply Finset.sum_nonneg
350    intro J _
351    split_ifs
352    · exact mul_nonneg (F.nonneg i J) (s_nonneg F.toDrawLaw m k (c, i))
353    · exact le_rfl
354
355/-- **One gated step followed by naive steps is dominated:** `Q P^k 1{c ≥ m} ≤ P^{k+1} 1{c ≥ m}`
356for every threshold rule `t`. -/
357theorem one_gated_step_le (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (t : ℕ → ℕ)
358    (k : ℕ) (x : ℕ × ℕ) :
359    kernel F.toDrawLaw (fun c => c + t c) (u F.toDrawLaw m k) x ≤
360      kernel F.toDrawLaw id (u F.toDrawLaw m k) x := by
361  have h := kernel_sub_u F.toDrawLaw (fun c => c + t c) m k x (by simp)
362  have := L4 F m hstoch (x.1 + t x.1) k x.1 x.2
363  simp only [Prod.mk.eta] at this
364  linarith
365
366theorem kernel_mono (cap : ℕ → ℕ) {f g : ℕ × ℕ → ℝ} (h : ∀ y, f y ≤ g y) (x : ℕ × ℕ) :
367    kernel F.toDrawLaw cap f x ≤ kernel F.toDrawLaw cap g x := by
368  unfold kernel
369  apply S2_mono
370  intro J ℓ
371  apply mul_le_mul_of_nonneg_left _ (F.toDrawLaw.nonneg _ _ _)
372  split_ifs
373  · exact h x
374  · exact h (J, ℓ)
375
376/-- **Proposition 9, hitting form** (stochastic dominance of the count): for every threshold rule
377`t`, state `x`, target `m` and generation `g`, `P^t_x(c_g ≥ m) ≤ P^0_x(c_g ≥ m)`. -/
378theorem gated_hit_le (m : ℕ) (hstoch : ∀ i, tail F m i ≤ tail F m (F.nx i)) (t : ℕ → ℕ)
379    (g : ℕ) (x : ℕ × ℕ) :
380    (kernel F.toDrawLaw (fun c => c + t c))^[g] (hit m) x ≤ u F.toDrawLaw m g x := by
381  induction g generalizing x with
382  | zero => rfl
383  | succ g ih =>
384    rw [Function.iterate_succ_apply']
385    exact (kernel_mono F _ ih x).trans (one_gated_step_le F m hstoch t g x)
386
387/-! ## Non-decreasing objectives -/
388
389theorem S2_finsum (M : Finset ℕ) (g : ℕ → ℕ → ℕ → ℝ) :
390    S2 n L (fun J ℓ => ∑ m ∈ M, g m J ℓ) = ∑ m ∈ M, S2 n L (g m) := by
391  unfold S2
392  have : ∀ J, ∑ ℓ ∈ range L, ∑ m ∈ M, g m J ℓ = ∑ m ∈ M, ∑ ℓ ∈ range L, g m J ℓ :=
393    fun J => Finset.sum_comm
394  simp_rw [this]
395  exact Finset.sum_comm
396
397/-- The one-generation operator is affine: it fixes constants and is linear. -/
398theorem kernel_affine (cap : ℕ → ℕ) (a : ℝ) (b : ℕ → ℝ) (M : Finset ℕ) (f : ℕ → ℕ × ℕ → ℝ)
399    (x : ℕ × ℕ) :
400    kernel F.toDrawLaw cap (fun y => a + ∑ m ∈ M, b m * f m y) x =
401      a + ∑ m ∈ M, b m * kernel F.toDrawLaw cap (f m) x := by
402  unfold kernel
403  have hterm : ∀ J ℓ, F.toDrawLaw.ρ x.2 J ℓ * (if J ≤ cap x.1 then a + ∑ m ∈ M, b m * f m x
404      else a + ∑ m ∈ M, b m * f m (J, ℓ)) =
405      a * F.toDrawLaw.ρ x.2 J ℓ + ∑ m ∈ M, b m *
406        (F.toDrawLaw.ρ x.2 J ℓ * (if J ≤ cap x.1 then f m x else f m (J, ℓ))) := by
407    intro J ℓ
408    split_ifs
409    · rw [mul_add, Finset.mul_sum]
410      congr 1
411      · ring
412      · apply Finset.sum_congr rfl; intro m _; ring
413    · rw [mul_add, Finset.mul_sum]
414      congr 1
415      · ring
416      · apply Finset.sum_congr rfl; intro m _; ring
417  rw [S2_congr hterm, S2_add, S2_mul_left, F.toDrawLaw.total x.2, mul_one, S2_finsum]
418  congr 1
419  apply Finset.sum_congr rfl
420  intro m _
421  rw [S2_mul_left]
422
423theorem iterate_affine (cap : ℕ → ℕ) (a : ℝ) (b : ℕ → ℝ) (M : Finset ℕ) (g : ℕ) :
424    ∀ (f : ℕ → ℕ × ℕ → ℝ) (x : ℕ × ℕ),
425      (kernel F.toDrawLaw cap)^[g] (fun y => a + ∑ m ∈ M, b m * f m y) x =
426        a + ∑ m ∈ M, b m * (kernel F.toDrawLaw cap)^[g] (f m) x := by
427  induction g with
428  | zero => intro f x; rfl
429  | succ g ih =>
430    intro f x
431    rw [Function.iterate_succ_apply]
432    have : kernel F.toDrawLaw cap (fun y => a + ∑ m ∈ M, b m * f m y) =
433        fun y => a + ∑ m ∈ M, b m * kernel F.toDrawLaw cap (f m) y :=
434      funext fun y => kernel_affine F cap a b M f y
435    rw [this, ih (fun m => kernel F.toDrawLaw cap (f m)) x]
436    simp only [Function.iterate_succ_apply]
437
438theorem u_eq_iterate (m g : ℕ) : u F.toDrawLaw m g = (kernel F.toDrawLaw id)^[g] (hit m) := by
439  induction g with
440  | zero => rfl
441  | succ g ih =>
442    rw [Function.iterate_succ_apply', ← ih]
443    rfl
444
445/-- A non-decreasing objective on counts `{0, …, n}` as a combination of hitting indicators. -/
446theorem objective_decomp (ψ : ℕ → ℝ) (y : ℕ × ℕ) :
447    ψ (min y.1 n) = ψ 0 + ∑ m ∈ range n, (ψ (m + 1) - ψ m) * hit (m + 1) y := by
448  have hk : min y.1 n ≤ n := min_le_right _ _
449  rw [← Finset.sum_range_add_sum_Ico _ hk]
450  have h1 : ∑ m ∈ range (min y.1 n), (ψ (m + 1) - ψ m) * hit (m + 1) y =
451      ∑ m ∈ range (min y.1 n), (ψ (m + 1) - ψ m) := by
452    apply Finset.sum_congr rfl
453    intro m hm
454    rw [mem_range] at hm
455    unfold hit
456    rw [ite_eq_left (by omega), mul_one]
457  have h2 : ∑ m ∈ Ico (min y.1 n) n, (ψ (m + 1) - ψ m) * hit (m + 1) y = 0 := by
458    apply Finset.sum_eq_zero
459    intro m hm
460    rw [mem_Ico] at hm
461    unfold hit
462    rw [ite_eq_right (by omega), mul_zero]
463  rw [h1, h2, Finset.sum_range_sub (fun m => ψ m)]
464  ring
465
466/-- **Proposition 9** (dominance in count): for every non-decreasing `ψ` on counts, every threshold
467rule `t`, state `x` and generation `g`, `E^t_x[ψ(c_g)] ≤ E^0_x[ψ(c_g)]`. -/
468theorem gated_expect_le (hstoch : ∀ m i, tail F m i ≤ tail F m (F.nx i)) (t : ℕ → ℕ)
469    (ψ : ℕ → ℝ) (hψ : Monotone ψ) (g : ℕ) (x : ℕ × ℕ) :
470    (kernel F.toDrawLaw (fun c => c + t c))^[g] (fun y => ψ (min y.1 n)) x ≤
471      (kernel F.toDrawLaw id)^[g] (fun y => ψ (min y.1 n)) x := by
472  have hdec : (fun y : ℕ × ℕ => ψ (min y.1 n)) =
473      fun y => ψ 0 + ∑ m ∈ range n, (ψ (m + 1) - ψ m) * hit (m + 1) y :=
474    funext (objective_decomp (n := n) ψ)
475  rw [hdec, iterate_affine F _ (ψ 0) (fun m => ψ (m + 1) - ψ m) (range n) g
476      (fun m => hit (m + 1)) x,
477    iterate_affine F _ (ψ 0) (fun m => ψ (m + 1) - ψ m) (range n) g (fun m => hit (m + 1)) x]
478  apply add_le_add le_rfl
479  apply Finset.sum_le_sum
480  intro m _
481  apply mul_le_mul_of_nonneg_left _ (sub_nonneg.mpr (hψ (Nat.le_succ m)))
482  rw [← u_eq_iterate]
483  exact gated_hit_le F (m + 1) (hstoch (m + 1)) t g x
484
485end FeedbackChannel.MultigenCountDominance