lean/FeedbackChannel/MultigenGate.lean

View on GitHub

  1import Mathlib.Tactic
  2
  3/-!
  4# One generation, gate versus naive, under a fixed true gain (multigen note, Proposition 7)
  5
  6Source: `paper/evaluation_feedback_multigen.md`, draft 0.5, §6; code
  7`src/spiral_ln/evaluation_feedback_multigen.py` (`one_step_gap_change`).
  8
  9From state `(c, p_inc)` every candidate has true probability `p_inc + δ`; `M` is the best of the
 10`K` candidate counts, with an arbitrary finite law `w` on a finite set `S` of counts (in the note,
 11the law of the maximum of `K` i.i.d. `Bin(n, p_inc + δ)` counts). A rule with cap `a` accepts iff
 12`M > a` (naive: `a = c`; gate: `a = c + t(c)`); on acceptance the state becomes `(M, p_inc + δ)`,
 13otherwise it stays. The readout gap of a state is `count/n - p`.
 14
 15**Proposition 7.** One generation changes the gap by `E[((M - c)/n - δ) 1_accept]`
 16(`gapChange_eq`), so
 17`Δρ_naive - Δρ_gate = E[((M - c)/n - δ) 1{c < M ≤ c + t(c)}]` (`naive_sub_gate`). Hence
 18(i) `δ ≤ 1/n` makes the difference `≥ 0` (`naive_sub_gate_nonneg`); (ii) `δ ≥ t(c)/n` makes it
 19`≤ 0` (`naive_sub_gate_nonpos`); both together make it zero; (iii) otherwise the sign depends on the
 20law of `M` on the window (`sign_undetermined`, stated for arbitrary finite laws).
 21-/
 22
 23namespace FeedbackChannel.MultigenGate
 24
 25open Finset
 26
 27variable (n : ℕ) (c : ℕ) (pInc δ : ℝ) (S : Finset ℕ) (w : ℕ → ℝ)
 28
 29/-- Readout gap of a state: reported score `count/n` minus true value `p`. -/
 30noncomputable def gap (count : ℕ) (p : ℝ) : ℝ := (count : ℝ) / n - p
 31
 32/-- Expected gap after one generation from `(c, p_inc)` under the rule that accepts iff `M > a`. -/
 33noncomputable def nextGap (a : ℕ) : ℝ :=
 34  ∑ j ∈ S, w j * (if a < j then gap n j (pInc + δ) else gap n c pInc)
 35
 36/-- One-generation change of the expected readout gap under the rule with cap `a`. -/
 37noncomputable def gapChange (a : ℕ) : ℝ := nextGap n c pInc δ S w a - gap n c pInc
 38
 39/-- **Proposition 7, first sentence:** the one-generation gap change is
 40`E[((M - c)/n - δ) 1{M > a}]`. -/
 41theorem gapChange_eq (hw : ∑ j ∈ S, w j = 1) (a : ℕ) :
 42    gapChange n c pInc δ S w a =
 43      ∑ j ∈ S, w j * (if a < j then ((j : ℝ) - c) / n - δ else 0) := by
 44  unfold gapChange nextGap
 45  have key : ∀ j, w j * (if a < j then gap n j (pInc + δ) else gap n c pInc) =
 46      w j * gap n c pInc + w j * (if a < j then ((j : ℝ) - c) / n - δ else 0) := by
 47    intro j
 48    unfold gap
 49    split_ifs <;> ring
 50  rw [Finset.sum_congr rfl (fun j _ => key j), Finset.sum_add_distrib, ← Finset.sum_mul, hw,
 51    one_mul]
 52  ring
 53
 54/-- **Proposition 7, the identity:**
 55`Δρ_naive - Δρ_gate = E[((M - c)/n - δ) 1{c < M ≤ c + t}]`. -/
 56theorem naive_sub_gate (hw : ∑ j ∈ S, w j = 1) (t : ℕ) :
 57    gapChange n c pInc δ S w c - gapChange n c pInc δ S w (c + t) =
 58      ∑ j ∈ S, w j * (if c < j ∧ j ≤ c + t then ((j : ℝ) - c) / n - δ else 0) := by
 59  rw [gapChange_eq n c pInc δ S w hw, gapChange_eq n c pInc δ S w hw, ← Finset.sum_sub_distrib]
 60  apply Finset.sum_congr rfl
 61  intro j _
 62  by_cases h1 : c < j <;> by_cases h2 : c + t < j
 63  · rw [ite_eq_left h1, ite_eq_left h2, ite_eq_right (by omega)]; ring
 64  · rw [ite_eq_left h1, ite_eq_right h2, ite_eq_left (by omega)]; ring
 65  · omega
 66  · rw [ite_eq_right h1, ite_eq_right h2, ite_eq_right (by omega)]; ring
 67
 68/-- **Proposition 7 (i):** if `δ ≤ 1/n`, the integrand is non-negative on the window, so the gate
 69lowers the one-generation gap change: `Δρ_naive - Δρ_gate ≥ 0`. -/
 70theorem naive_sub_gate_nonneg (hw0 : ∀ j ∈ S, 0 ≤ w j) (hw : ∑ j ∈ S, w j = 1) (hn : 0 < n)
 71    (hδ : δ ≤ 1 / n) (t : ℕ) :
 72    0 ≤ gapChange n c pInc δ S w c - gapChange n c pInc δ S w (c + t) := by
 73  rw [naive_sub_gate n c pInc δ S w hw]
 74  apply Finset.sum_nonneg
 75  intro j hj
 76  apply mul_nonneg (hw0 j hj)
 77  split_ifs with h
 78  · have hn' : (0 : ℝ) < n := by exact_mod_cast hn
 79    have hjc : (1 : ℝ) ≤ (j : ℝ) - c := by
 80      have : c + 1 ≤ j := h.1
 81      have : ((c + 1 : ℕ) : ℝ) ≤ j := by exact_mod_cast this
 82      push_cast at this
 83      linarith
 84    have : 1 / (n : ℝ) ≤ ((j : ℝ) - c) / n := div_le_div_of_nonneg_right hjc hn'.le
 85    linarith
 86  · exact le_rfl
 87
 88/-- **Proposition 7 (ii):** if `δ ≥ t/n`, the integrand is non-positive on the window, so the gate
 89raises the one-generation gap change: `Δρ_naive - Δρ_gate ≤ 0`. -/
 90theorem naive_sub_gate_nonpos (hw0 : ∀ j ∈ S, 0 ≤ w j) (hw : ∑ j ∈ S, w j = 1) (hn : 0 < n)
 91    (t : ℕ) (hδ : (t : ℝ) / n ≤ δ) :
 92    gapChange n c pInc δ S w c - gapChange n c pInc δ S w (c + t) ≤ 0 := by
 93  rw [naive_sub_gate n c pInc δ S w hw]
 94  apply Finset.sum_nonpos
 95  intro j hj
 96  apply mul_nonpos_of_nonneg_of_nonpos (hw0 j hj)
 97  split_ifs with h
 98  · have hn' : (0 : ℝ) < n := by exact_mod_cast hn
 99    have hjc : (j : ℝ) - c ≤ t := by
100      have : j ≤ c + t := h.2
101      have : (j : ℝ) ≤ ((c + t : ℕ) : ℝ) := by exact_mod_cast this
102      push_cast at this
103      linarith
104    have : ((j : ℝ) - c) / n ≤ (t : ℝ) / n := div_le_div_of_nonneg_right hjc hn'.le
105    linarith
106  · exact le_rfl
107
108/-- **Proposition 7, (i) and (ii) together:** if both `δ ≤ 1/n` and `δ ≥ t/n` (e.g. `t ≤ 1` and
109`δ = 1/n`, the note's `n = 8, z = 1` cells), the one-step difference is exactly zero. -/
110theorem naive_sub_gate_eq_zero (hw0 : ∀ j ∈ S, 0 ≤ w j) (hw : ∑ j ∈ S, w j = 1) (hn : 0 < n)
111    (t : ℕ) (h1 : δ ≤ 1 / n) (h2 : (t : ℝ) / n ≤ δ) :
112    gapChange n c pInc δ S w c - gapChange n c pInc δ S w (c + t) = 0 :=
113  le_antisymm (naive_sub_gate_nonpos n c pInc δ S w hw0 hw hn t h2)
114    (naive_sub_gate_nonneg n c pInc δ S w hw0 hw hn h1 t)
115
116/-- **Proposition 7 (iii)**, for arbitrary finite laws of `M`: if `1/n < δ < t/n`, a law
117concentrated at `c + 1` makes the difference negative and a law concentrated at `c + t` makes it
118positive, so the sign is not determined by `δ` and `t` alone. -/
119theorem sign_undetermined (hn : 0 < n) (t : ℕ) (h1 : 1 / (n : ℝ) < δ) (h2 : δ < (t : ℝ) / n) :
120    gapChange n c pInc δ {c + 1} (fun _ => 1) c -
121        gapChange n c pInc δ {c + 1} (fun _ => 1) (c + t) < 0 ∧
122      0 < gapChange n c pInc δ {c + t} (fun _ => 1) c -
123        gapChange n c pInc δ {c + t} (fun _ => 1) (c + t) := by
124  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
125  have ht : 1 ≤ t := by
126    by_contra h
127    have : t = 0 := by omega
128    subst this
129    simp at h2
130    have : 0 < 1 / (n : ℝ) := by positivity
131    linarith
132  constructor
133  · rw [naive_sub_gate n c pInc δ _ _ (by simp)]
134    rw [Finset.sum_singleton, ite_eq_left ⟨by omega, by omega⟩]
135    push_cast
136    rw [show ((c : ℝ) + 1 - c) = 1 by ring]
137    linarith
138  · rw [naive_sub_gate n c pInc δ _ _ (by simp)]
139    rw [Finset.sum_singleton, ite_eq_left ⟨by omega, le_rfl⟩]
140    push_cast
141    rw [show ((c : ℝ) + t - c) = t by ring]
142    linarith
143
144end FeedbackChannel.MultigenGate