lean/FeedbackChannel/PatchStatic.lean

View on GitHub

  1import FeedbackChannel.PatchDP
  2
  3/-!
  4# Static patch schedules: Propositions 3 and 4 of the lattice note
  5
  6Source: `paper/evaluation_feedback_lattice.md` (version 0.6), §3; Python
  7`conditional_hit_probability`, `expected_hits_after`, `expected_score_crossing`,
  8`expected_patches_static`, `crossing_error_bound`.
  9
 10A *static schedule* patches the categories one after another in a fixed list order, each completely,
 11each uniformly at random without replacement. All probabilities are obtained by first-step analysis
 12on `(n, F)` exactly as in the dynamic programme: a patch in `(n, F)` hits with probability `F / n`,
 13leaving `(n - 1, F - 1)`, else `(n - 1, F)`.
 14-/
 15
 16namespace FeedbackChannel.PatchStatic
 17
 18open FeedbackChannel.PatchDP
 19
 20/-! ### Proposition 3: every patch in a category has the same unconditional hit probability -/
 21
 22/-- Unconditional probability that the `(j + 1)`-th patch in a category with `n` items and `F`
 23failures hits (first-step analysis on the first patch). -/
 24def hitProb : ℕ → ℕ → ℕ → ℚ
 25  | 0, n, F => (F : ℚ) / n
 26  | j + 1, n, F => (F : ℚ) / n * hitProb j (n - 1) (F - 1) + (1 - (F : ℚ) / n) * hitProb j (n - 1) F
 27
 28/-- **Proposition 3 (exchangeability).** The `j`-th patch in a category hits with unconditional
 29probability `F / n` for every `j ≤ n`. (The conditional probability `(F - h) / (n - j + 1)` given
 30`h` earlier hits is the model's transition: after `j - 1` patches with `h` hits the category is
 31`(n - j + 1, F - h)` and `rate` of that pair is the stated ratio.) -/
 32theorem prop3_hitProb : ∀ (j n F : ℕ), j < n → F ≤ n → hitProb j n F = (F : ℚ) / n := by
 33  intro j
 34  induction j with
 35  | zero => intro n F _ _; rfl
 36  | succ j ih =>
 37    intro n F hj hF
 38    obtain ⟨m, rfl⟩ : ∃ m, n = m + 1 := ⟨n - 1, by omega⟩
 39    have hm : 0 < m := by omega
 40    simp only [hitProb, Nat.add_sub_cancel]
 41    have hmq : (m : ℚ) ≠ 0 := by exact_mod_cast hm.ne'
 42    rcases Nat.eq_zero_or_pos F with h0 | h1
 43    · subst h0; simp; rw [ih m 0 (by omega) (by omega)]; simp
 44    rw [ih m (F - 1) (by omega) (by omega)]
 45    rcases Nat.lt_or_ge F (m + 1) with hlt | hge
 46    · rw [ih m F (by omega) (by omega)]
 47      rw [Nat.cast_sub h1]
 48      push_cast
 49      field_simp
 50      ring
 51    · have : F = m + 1 := by omega
 52      subst this
 53      push_cast
 54      rw [div_self (by positivity)]
 55      simp only [Nat.add_sub_cancel, sub_self, zero_mul, add_zero, one_mul]
 56      field_simp
 57
 58/-- Expected hits of `m` patches in one category (first-step analysis). -/
 59def expHits : ℕ → ℕ → ℕ → ℚ
 60  | 0, _, _ => 0
 61  | m + 1, n, F => (F : ℚ) / n * (1 + expHits m (n - 1) (F - 1)) + (1 - (F : ℚ) / n) * expHits m (n - 1) F
 62
 63/-- **Proposition 3 (hypergeometric mean).** `m` patches in a category hit `m F / n` in
 64expectation, for every `m ≤ n`. -/
 65theorem prop3_expHits : ∀ (m n F : ℕ), m ≤ n → F ≤ n → expHits m n F = (m : ℚ) * F / n := by
 66  intro m
 67  induction m with
 68  | zero => intro n F _ _; simp [expHits]
 69  | succ m ih =>
 70    intro n F hm hF
 71    obtain ⟨k, rfl⟩ : ∃ k, n = k + 1 := ⟨n - 1, by omega⟩
 72    simp only [expHits, Nat.add_sub_cancel]
 73    rcases Nat.eq_zero_or_pos m with hm0 | hmpos
 74    · subst hm0; simp [expHits]
 75    have hk : 0 < k := by omega
 76    have hkq : (k : ℚ) ≠ 0 := by exact_mod_cast hk.ne'
 77    rcases Nat.eq_zero_or_pos F with h0 | h1
 78    · subst h0; rw [ih k 0 (by omega) (by omega)]; simp
 79    rw [ih k (F - 1) (by omega) (by omega)]
 80    rcases Nat.lt_or_ge F (k + 1) with hlt | hge
 81    · rw [ih k F (by omega) (by omega), Nat.cast_sub h1]
 82      push_cast
 83      field_simp
 84      ring
 85    · have : F = k + 1 := by omega
 86      subst this
 87      push_cast
 88      rw [div_self (by positivity)]
 89      simp only [Nat.add_sub_cancel, sub_self, zero_mul, add_zero, one_mul]
 90      field_simp
 91      ring
 92
 93/-! ### The static schedule's stopping time -/
 94
 95/-- Fuel for the static recursion: items plus categories (each step consumes an item or drops an
 96exhausted category). -/
 97def sfuel (L : List Cat) : ℕ := (L.map Prod.fst).sum + L.length
 98
 99/-- Expected patches to `H` hits under the static schedule that patches the categories of `L`
100completely, in list order (first-step analysis, fuel `sfuel`). -/
101def sdp : ℕ → ℕ → List Cat → ℚ
102  | 0, _, _ => 0
103  | _ + 1, 0, _ => 0
104  | _ + 1, _ + 1, [] => 0
105  | k + 1, H + 1, (0, _) :: L => sdp k (H + 1) L
106  | k + 1, H + 1, (n + 1, F) :: L =>
107    1 + (F : ℚ) / (n + 1) * sdp k H ((n, F - 1) :: L) + (1 - (F : ℚ) / (n + 1)) * sdp k (H + 1) ((n, F) :: L)
108
109/-- `E[T]`: expected number of patches until the realized hit count first reaches `H`. -/
110def staticT (H : ℕ) (L : List Cat) : ℚ := sdp (sfuel L) H L
111
112lemma sfuel_cons (n F : ℕ) (L : List Cat) : sfuel ((n, F) :: L) = n + sfuel L + 1 := by
113  simp [sfuel]; ring
114
115lemma staticT_zero (L : List Cat) : staticT 0 L = 0 := by
116  unfold staticT; cases sfuel L <;> simp [sdp]
117
118lemma staticT_empty (H : ℕ) (F : ℕ) (L : List Cat) (hH : 0 < H) :
119    staticT H ((0, F) :: L) = staticT H L := by
120  obtain ⟨h, rfl⟩ : ∃ h, H = h + 1 := ⟨H - 1, by omega⟩
121  unfold staticT
122  rw [sfuel_cons, zero_add]
123  rfl
124
125lemma staticT_step (H n F : ℕ) (L : List Cat) :
126    staticT (H + 1) ((n + 1, F) :: L) =
127      1 + (F : ℚ) / (n + 1) * staticT H ((n, F - 1) :: L) +
128        (1 - (F : ℚ) / (n + 1)) * staticT (H + 1) ((n, F) :: L) := by
129  unfold staticT
130  rw [sfuel_cons, sfuel_cons, sfuel_cons]
131  rw [show n + 1 + sfuel L + 1 = (n + sfuel L + 1) + 1 by ring]
132  rfl
133
134/-- Inside the crossing category: `E[T] = H (n + 1) / (F + 1)` when `1 ≤ H ≤ F ≤ n`, whatever
135follows (the run stops before leaving the category). -/
136theorem staticT_within : ∀ (n H F : ℕ) (L : List Cat), H ≤ F → F ≤ n →
137    staticT H ((n, F) :: L) = (H : ℚ) * ((n : ℚ) + 1) / ((F : ℚ) + 1) := by
138  intro n
139  induction n with
140  | zero =>
141    intro H F L h1 h2
142    have : H = 0 := by omega
143    subst this; simp [staticT_zero]
144  | succ n ih =>
145    intro H F L h1 h2
146    rcases H with _ | h
147    · simp [staticT_zero]
148    obtain ⟨G, rfl⟩ : ∃ G, F = G + 1 := ⟨F - 1, by omega⟩
149    rw [staticT_step, Nat.add_sub_cancel, ih h G L (by omega) (by omega)]
150    rcases Nat.lt_or_ge G n with hlt | hge
151    · rw [ih (h + 1) (G + 1) L (by omega) (by omega)]
152      push_cast
153      field_simp
154      ring
155    · have : G = n := by omega
156      subst this
157      push_cast
158      rw [div_self (by positivity)]
159      field_simp
160      ring
161
162/-- Past a category whose failures do not reach the requirement, the schedule spends all of its
163`n` items and continues with `H - F` hits still needed. -/
164theorem staticT_skip : ∀ (n H F : ℕ) (L : List Cat), F < H → F ≤ n →
165    staticT H ((n, F) :: L) = n + staticT (H - F) L := by
166  intro n
167  induction n with
168  | zero =>
169    intro H F L h1 h2
170    have : F = 0 := by omega
171    subst this
172    rw [staticT_empty _ _ _ (by omega)]; simp
173  | succ n ih =>
174    intro H F L h1 h2
175    obtain ⟨h, rfl⟩ : ∃ h, H = h + 1 := ⟨H - 1, by omega⟩
176    rw [staticT_step]
177    rcases Nat.eq_zero_or_pos F with h0 | hpos
178    · subst h0
179      rw [ih (h + 1) 0 L (by omega) (by omega)]
180      push_cast; ring
181    rcases Nat.lt_or_ge F (n + 1) with hlt | hge
182    · rw [ih h (F - 1) L (by omega) (by omega), ih (h + 1) F L (by omega) (by omega),
183        show h - (F - 1) = h + 1 - F by omega]
184      push_cast
185      field_simp
186      ring
187    · have : F = n + 1 := by omega
188      subst this
189      simp only [Nat.add_sub_cancel]
190      rw [ih h n L (by omega) (by omega), show h - n = h + 1 - (n + 1) by omega]
191      push_cast
192      rw [div_self (by positivity)]
193      ring
194
195/-- Failures in a list of categories. -/
196def lfails (L : List Cat) : ℕ := (L.map Prod.snd).sum
197
198/-- Items in a list of categories. -/
199def litems (L : List Cat) : ℕ := (L.map Prod.fst).sum
200
201/-- **Proposition 4 (stopping time of the static schedule).** Let the schedule patch the
202categories in the order `pre ++ c :: post`, where `c = (n_j, F_j)` is the first category at which
203the cumulative failures reach `H` (`F(pre) < H ≤ F(pre) + F_j`). Then the expected number of
204patches until the realized hit count first reaches `H` is
205`N_{<j} + H_j (n_j + 1) / (F_j + 1)`, with `N_{<j}` the items of `pre` and `H_j = H - F(pre)`.
206This holds for every fixed order, in particular for the greedy one. -/
207theorem prop4_staticT (pre post : List Cat) (c : Cat) (H : ℕ) (hv : ∀ d ∈ pre ++ c :: post, d.2 ≤ d.1)
208    (hlo : lfails pre < H) (hhi : H ≤ lfails pre + c.2) :
209    staticT H (pre ++ c :: post) =
210      (litems pre : ℚ) + ((H - lfails pre : ℕ) : ℚ) * ((c.1 : ℚ) + 1) / ((c.2 : ℚ) + 1) := by
211  induction pre generalizing H with
212  | nil =>
213    simp only [lfails, litems, List.map_nil, List.sum_nil, Nat.cast_zero, zero_add, Nat.sub_zero,
214      List.nil_append] at *
215    obtain ⟨n, F⟩ := c
216    exact staticT_within n H F post hhi (hv _ List.mem_cons_self)
217  | cons d pre ih =>
218    obtain ⟨n, F⟩ := d
219    simp only [lfails, litems, List.map_cons, List.sum_cons] at hlo hhi ⊢
220    have hd := hv (n, F) (by simp)
221    simp only [List.cons_append]
222    rw [staticT_skip n H F _ (by omega) hd]
223    rw [ih (H - F) (fun e he => hv e (by simp [he])) (by simp only [lfails]; omega)
224      (by simp only [lfails]; omega)]
225    simp only [lfails, litems]
226    rw [show H - F - (pre.map Prod.snd).sum = H - (F + (pre.map Prod.snd).sum) by omega]
227    push_cast
228    ring
229
230/-! ### The expected-score crossing `m*` -/
231
232/-- Expected hits after `m` patches of the static schedule, linear between integers: the
233`expected_hits_after` curve. -/
234def curve : List Cat → ℚ → ℚ
235  | [], _ => 0
236  | c :: L, m => if m ≤ c.1 then m * rate c else c.2 + curve L (m - c.1)
237
238/-- At whole patch counts inside one category the curve is the hypergeometric mean of
239Proposition 3, so it is the expected number of hits. -/
240theorem curve_single (n F m : ℕ) (L : List Cat) (hm : m ≤ n) (hF : F ≤ n) :
241    curve ((n, F) :: L) m = expHits m n F := by
242  rw [prop3_expHits m n F hm hF]
243  simp only [curve, rate_mk, Nat.cast_le.2 hm, if_true]
244  ring
245
246
247/-- The note's `m* = N_{<j} + H_j n_j / F_j` for the split `pre ++ c :: post`. -/
248def mstar (pre : List Cat) (c : Cat) (H : ℕ) : ℚ :=
249  (litems pre : ℚ) + ((H - lfails pre : ℕ) : ℚ) * (c.1 : ℚ) / (c.2 : ℚ)
250
251/-- **Proposition 4 (expected-score crossing).** With the split of `prop4_staticT`, the expected
252hit curve of the static schedule equals `H` at `m*` and is strictly below `H` at every earlier
253patch count, so `m*` is where the expected score reaches the ceiling. -/
254theorem prop4_crossing (pre post : List Cat) (c : Cat) (H : ℕ)
255    (hv : ∀ d ∈ pre ++ c :: post, d.2 ≤ d.1) (hlo : lfails pre < H) (hhi : H ≤ lfails pre + c.2) :
256    curve (pre ++ c :: post) (mstar pre c H) = H ∧
257      ∀ m : ℚ, 0 ≤ m → m < mstar pre c H → curve (pre ++ c :: post) m < H := by
258  induction pre generalizing H with
259  | nil =>
260    obtain ⟨n, F⟩ := c
261    simp only [lfails, litems, List.map_nil, List.sum_nil, Nat.sub_zero, mstar, Nat.cast_zero,
262      zero_add, List.nil_append] at hlo hhi ⊢
263    have hd := hv (n, F) (by simp)
264    simp only at hhi hd
265    have hFq : (0 : ℚ) < F := by exact_mod_cast (show 0 < F by omega)
266    have hnq : (0 : ℚ) < n := by exact_mod_cast (show 0 < n by omega)
267    have hHF : (H : ℚ) ≤ F := by exact_mod_cast hhi
268    have hle : (H : ℚ) * n / F ≤ n := by
269      rw [div_le_iff₀ hFq]; nlinarith
270    refine ⟨?_, ?_⟩
271    · simp only [curve, rate_mk, hle, if_true]
272      field_simp
273    · intro m hm hlt
274      have hmn : m ≤ n := (le_of_lt hlt).trans hle
275      simp only [curve, rate_mk, hmn, if_true]
276      rw [lt_div_iff₀ hFq] at hlt
277      rw [mul_div_assoc', div_lt_iff₀ hnq]
278      nlinarith
279  | cons d pre ih =>
280    obtain ⟨n, F⟩ := d
281    have hd := hv (n, F) (by simp)
282    simp only at hd
283    simp only [lfails, litems, List.map_cons, List.sum_cons] at hlo hhi
284    have ih' := ih (H - F) (fun e he => hv e (by simp [he])) (by simp only [lfails]; omega)
285      (by simp only [lfails]; omega)
286    have hms : mstar ((n, F) :: pre) c H = n + mstar pre c (H - F) := by
287      simp only [mstar, litems, lfails, List.map_cons, List.sum_cons]
288      rw [show H - (F + (pre.map Prod.snd).sum) = H - F - (pre.map Prod.snd).sum by omega]
289      push_cast; ring
290    -- the crossing lies strictly beyond this category
291    have hpos : 0 < mstar pre c (H - F) := by
292      have hc := hv c (by simp)
293      simp only [mstar]
294      have h1 : (0 : ℚ) ≤ litems pre := by positivity
295      have h2 : (0 : ℚ) < ((H - F - lfails pre : ℕ) : ℚ) := by
296        exact_mod_cast (show 0 < H - F - lfails pre by simp only [lfails]; omega)
297      have h3 : (0 : ℚ) < c.1 := by exact_mod_cast (show 0 < c.1 by simp only [lfails] at *; omega)
298      have h4 : (0 : ℚ) < c.2 := by exact_mod_cast (show 0 < c.2 by simp only [lfails] at *; omega)
299      have : (0 : ℚ) < ((H - F - lfails pre : ℕ) : ℚ) * c.1 / c.2 := by positivity
300      linarith
301    refine ⟨?_, ?_⟩
302    · rw [hms]
303      simp only [List.cons_append, curve, rate_mk]
304      rw [if_neg (by linarith), show (n : ℚ) + mstar pre c (H - F) - n = mstar pre c (H - F) by ring,
305        ih'.1]
306      push_cast [show F ≤ H by omega]
307      ring
308    · intro m hm hlt
309      rw [hms] at hlt
310      simp only [List.cons_append, curve, rate_mk]
311      split_ifs with h
312      · have hFH : (F : ℚ) < H := by exact_mod_cast (show F < H by omega)
313        rcases Nat.eq_zero_or_pos n with h0 | hnpos
314        · subst h0; simp only [Nat.cast_zero, div_zero, mul_zero]; exact_mod_cast (show 0 < H by omega)
315        have hnq : (0 : ℚ) < n := by exact_mod_cast hnpos
316        have : m * ((F : ℚ) / n) ≤ F := by
317          rw [mul_div_assoc', div_le_iff₀ hnq]
318          have : (0 : ℚ) ≤ F := by positivity
319          nlinarith
320        linarith
321      · push Not at h
322        have := ih'.2 (m - n) (by linarith) (by linarith)
323        push_cast [show F ≤ H by omega] at this
324        linarith
325
326/-- **Proposition 4 (the gap).** `m* - E[T] = H_j (n_j - F_j) / (F_j (F_j + 1))`, which lies in
327`[0, m* / (F_j + 1))`: the expected-score crossing never understates the expected patch count. -/
328theorem prop4_gap (pre post : List Cat) (c : Cat) (H : ℕ)
329    (hv : ∀ d ∈ pre ++ c :: post, d.2 ≤ d.1) (hlo : lfails pre < H) (hhi : H ≤ lfails pre + c.2) :
330    mstar pre c H - staticT H (pre ++ c :: post) =
331        ((H - lfails pre : ℕ) : ℚ) * ((c.1 : ℚ) - c.2) / ((c.2 : ℚ) * ((c.2 : ℚ) + 1)) ∧
332      0 ≤ mstar pre c H - staticT H (pre ++ c :: post) ∧
333      mstar pre c H - staticT H (pre ++ c :: post) < mstar pre c H / ((c.2 : ℚ) + 1) := by
334  rw [prop4_staticT pre post c H hv hlo hhi]
335  have hc := hv c (by simp)
336  set Hj : ℚ := ((H - lfails pre : ℕ) : ℚ) with hHj
337  have hHj1 : (1 : ℚ) ≤ Hj := by rw [hHj]; exact_mod_cast (show 1 ≤ H - lfails pre by omega)
338  have hF : (1 : ℚ) ≤ c.2 := by exact_mod_cast (show 1 ≤ c.2 by omega)
339  have hFn : (c.2 : ℚ) ≤ c.1 := by exact_mod_cast hc
340  have hN : (0 : ℚ) ≤ litems pre := by positivity
341  simp only [mstar, ← hHj]
342  have hF0 : (c.2 : ℚ) ≠ 0 := by positivity
343  have hF1 : (c.2 : ℚ) + 1 ≠ 0 := by positivity
344  have key : (litems pre : ℚ) + Hj * c.1 / c.2 - ((litems pre : ℚ) + Hj * (c.1 + 1) / (c.2 + 1)) =
345      Hj * (c.1 - c.2) / (c.2 * (c.2 + 1)) := by
346    field_simp; ring
347  refine ⟨key, ?_, ?_⟩
348  · rw [key]; apply div_nonneg <;> nlinarith
349  · rw [key]
350    have hdiff : ((litems pre : ℚ) + Hj * c.1 / c.2) / ((c.2 : ℚ) + 1) -
351        Hj * (c.1 - c.2) / (c.2 * (c.2 + 1)) = ((litems pre : ℚ) + Hj) / ((c.2 : ℚ) + 1) := by
352      field_simp; ring
353    have : (0 : ℚ) < ((litems pre : ℚ) + Hj) / ((c.2 : ℚ) + 1) := by
354      apply div_pos <;> linarith
355    linarith
356
357/-! ### The note's numbers -/
358
359/-- One category, `n = 50`, `F = 13`, `H = 8`: `m* / n = 8 / 13 = (1 - ε - v) / (1 - v)` with
360`ε = 1/10`, `v = 37/50`, and `E[T] = 204 / 7`; the dynamic programme gives the same `E[T]`. -/
361theorem prop4_one_category :
362    mstar [] (50, 13) 8 / 50 = 8 / 13 ∧ (1 - 1 / 10 - 37 / 50 : ℚ) / (1 - 37 / 50) = 8 / 13 ∧
363      staticT 8 [(50, 13)] = 204 / 7 ∧ val 8 {(50, 13)} = 204 / 7 := by
364  refine ⟨by norm_num [mstar, litems, lfails], by norm_num, ?_, ?_⟩
365  · rw [staticT_within 50 8 13 [] (by norm_num) (by norm_num)]; norm_num
366  · rw [val_single 50 8 13 (by norm_num) (by norm_num)]; norm_num
367
368/-- Singleton categories (13 failing, 37 passing items, failures first, `H = 8`): `m*` and `E[T]`
369both equal `H`, a share `4/25` of the 50 items. -/
370theorem prop4_singletons :
371    mstar (List.replicate 7 (1, 1)) (1, 1) 8 = 8 ∧
372      staticT 8 (List.replicate 7 (1, 1) ++ (1, 1) :: (List.replicate 5 (1, 1) ++ List.replicate 37 (1, 0))) = 8 ∧
373      (8 : ℚ) / 50 = 4 / 25 := by
374  have hv : ∀ d ∈ List.replicate 7 ((1 : ℕ), (1 : ℕ)) ++ (1, 1) ::
375      (List.replicate 5 (1, 1) ++ List.replicate 37 (1, 0)), d.2 ≤ d.1 := by
376    intro d hd
377    simp only [List.mem_append, List.mem_replicate, List.mem_cons] at hd
378    rcases hd with ⟨_, rfl⟩ | rfl | ⟨_, rfl⟩ | ⟨_, rfl⟩ <;> simp
379  have hl : lfails (List.replicate 7 ((1 : ℕ), (1 : ℕ))) = 7 := by
380    simp [lfails, List.map_replicate, List.sum_replicate]
381  have hi : litems (List.replicate 7 ((1 : ℕ), (1 : ℕ))) = 7 := by
382    simp [litems, List.map_replicate, List.sum_replicate]
383  refine ⟨?_, ?_, by norm_num⟩
384  · simp only [mstar, hl, hi]; norm_num
385  · rw [prop4_staticT _ _ (1, 1) 8 hv (by rw [hl]; norm_num) (by rw [hl]; norm_num)]
386    rw [hl, hi]; norm_num
387
388
389/-- **The static stopping time is not monotone along refinement** (the note's witness). One block
390`(4,2)` with `H = 2` gives `E[T] = 10/3`; its refinement into `(2,1), (2,1)` gives `7/2`. -/
391theorem static_not_monotone :
392    staticT 2 [(4, 2)] = 10 / 3 ∧ staticT 2 [(2, 1), (2, 1)] = 7 / 2 := by
393  refine ⟨?_, ?_⟩
394  · rw [staticT_within 4 2 2 [] (by norm_num) (by norm_num)]; norm_num
395  · have h := prop4_staticT [(2, 1)] [] (2, 1) 2 (by decide) (by decide) (by decide)
396    simp only [List.singleton_append] at h
397    rw [h]
398    norm_num [litems, lfails]
399
400end FeedbackChannel.PatchStatic