1import FeedbackChannel.PatchStatic
2
3/-!
4# Greedy filling is optimal (Proposition 3) and `m*` is monotone along refinement (Proposition 5)
5
6Source: `paper/evaluation_feedback_lattice.md` (version 0.6), §3, Propositions 3 and 5.
7
8An *allocation* is a list of pairs `(category, amount)` whose categories are those of the state
9(as a multiset: `P.map Prod.fst ~ L`), with `0 ≤ amount ≤ n`. Amounts are rational, so fractional
10allocations (the note's proportional allocation) are included. By Proposition 3
11(`prop3_expHits`), a whole-number amount `m_c` in category `c` hits `m_c F_c / n_c` in expectation
12whatever the order of the patches, so `alloc_value` is the expected number of hits.
13
14The proofs go through a Lagrangian bound: for every `ρ ≥ 0`, the value of any allocation of total
15`M`, and the greedy curve itself, are at most `ρ M + Σ_c n_c (F_c/n_c - ρ)⁺`, with equality for the
16greedy curve at a suitable `ρ` when the list is in nonincreasing order of rate.
17-/
18
19namespace FeedbackChannel.PatchGreedy
20
21open FeedbackChannel.PatchDP FeedbackChannel.PatchStatic
22
23/-- The categories are in the greedy order: nonincreasing hit rate. -/
24def Sorted (L : List Cat) : Prop := L.Pairwise fun c d => rate d ≤ rate c
25
26/-- Every category satisfies `F ≤ n`. -/
27def LValid (L : List Cat) : Prop := ∀ c ∈ L, c.2 ≤ c.1
28
29/-- The Lagrangian bound `ρ M + Σ_c n_c (rate c - ρ)⁺`. -/
30def dual (L : List Cat) (ρ M : ℚ) : ℚ := ρ * M + (L.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum
31
32/-- Expected hits of an allocation. -/
33def alloc_value (P : List (Cat × ℚ)) : ℚ := (P.map fun p => p.2 * rate p.1).sum
34
35/-- Total patches of an allocation. -/
36def alloc_total (P : List (Cat × ℚ)) : ℚ := (P.map Prod.snd).sum
37
38/-- `P` is an allocation of patches to the categories `L`. -/
39def IsAlloc (L : List Cat) (P : List (Cat × ℚ)) : Prop :=
40 (P.map Prod.fst).Perm L ∧ ∀ p ∈ P, 0 ≤ p.2 ∧ p.2 ≤ p.1.1
41
42lemma n_mul_rate {c : Cat} (h : c.2 ≤ c.1) : (c.1 : ℚ) * rate c = c.2 := by
43 unfold rate
44 rcases Nat.eq_zero_or_pos c.1 with h0 | hpos
45 · have : c.2 = 0 := by omega
46 simp [h0, this]
47 · field_simp
48
49lemma dual_tail_nonneg (L : List Cat) (ρ : ℚ) :
50 0 ≤ (L.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum := by
51 apply List.sum_nonneg
52 intro x hx
53 obtain ⟨d, _, rfl⟩ := List.mem_map.1 hx
54 exact mul_nonneg (by positivity) (le_max_right _ _)
55
56/-- The greedy curve of any list lies below every Lagrangian bound. -/
57theorem curve_le_dual : ∀ (L : List Cat), LValid L → ∀ ρ M : ℚ, 0 ≤ ρ → 0 ≤ M →
58 curve L M ≤ dual L ρ M := by
59 intro L
60 induction L with
61 | nil => intro _ ρ M hρ hM; simp [curve, dual]; positivity
62 | cons c L ih =>
63 intro hv ρ M hρ hM
64 have hc := hv c (by simp)
65 have hL : LValid L := fun d hd => hv d (by simp [hd])
66 have ht := dual_tail_nonneg L ρ
67 simp only [curve, dual, List.map_cons, List.sum_cons]
68 split_ifs with h
69 · have h1 : M * rate c ≤ ρ * M + M * max (rate c - ρ) 0 := by
70 have := le_max_left (rate c - ρ) 0
71 nlinarith
72 have h2 : M * max (rate c - ρ) 0 ≤ (c.1 : ℚ) * max (rate c - ρ) 0 :=
73 mul_le_mul_of_nonneg_right h (le_max_right _ _)
74 linarith
75 · push Not at h
76 have := ih hL ρ (M - c.1) hρ (by linarith)
77 simp only [dual] at this
78 have hF := n_mul_rate hc
79 have h3 : (c.1 : ℚ) * (rate c - ρ) ≤ (c.1 : ℚ) * max (rate c - ρ) 0 :=
80 mul_le_mul_of_nonneg_left (le_max_left _ _) (by positivity)
81 nlinarith
82
83/-- In greedy order the curve attains the Lagrangian bound at some `ρ ≥ 0` that is `0` or one of
84the rates. -/
85theorem curve_eq_dual : ∀ (L : List Cat), LValid L → Sorted L → ∀ M : ℚ, 0 ≤ M →
86 M ≤ litems L → ∃ ρ, (ρ = 0 ∨ ∃ d ∈ L, ρ = rate d) ∧ curve L M = dual L ρ M := by
87 intro L
88 induction L with
89 | nil =>
90 intro _ _ M hM hM'
91 simp only [litems, List.map_nil, List.sum_nil, Nat.cast_zero] at hM'
92 refine ⟨0, Or.inl rfl, ?_⟩
93 simp [curve, dual]
94 | cons c L ih =>
95 intro hv hs M hM hM'
96 have hc := hv c (by simp)
97 have hL : LValid L := fun d hd => hv d (by simp [hd])
98 rw [Sorted, List.pairwise_cons] at hs
99 simp only [litems, List.map_cons, List.sum_cons, Nat.cast_add] at hM'
100 by_cases h : M ≤ c.1
101 · refine ⟨rate c, Or.inr ⟨c, by simp, rfl⟩, ?_⟩
102 simp only [curve, dual, h, if_true, List.map_cons, List.sum_cons, sub_self, max_self, mul_zero,
103 zero_add]
104 have : (L.map fun d => (d.1 : ℚ) * max (rate d - rate c) 0).sum = 0 := by
105 apply List.sum_eq_zero
106 intro x hx
107 obtain ⟨d, hd, rfl⟩ := List.mem_map.1 hx
108 have := hs.1 d hd
109 rw [max_eq_right (by linarith), mul_zero]
110 rw [this]; ring
111 · push Not at h
112 obtain ⟨ρ, hρ, heq⟩ := ih hL hs.2 (M - c.1) (by linarith) (by simp only [litems]; linarith)
113 have hρc : ρ ≤ rate c := by
114 rcases hρ with rfl | ⟨d, hd, rfl⟩
115 · exact rate_nonneg c
116 · exact hs.1 d hd
117 refine ⟨ρ, ?_, ?_⟩
118 · rcases hρ with h0 | ⟨d, hd, rfl⟩
119 · exact Or.inl h0
120 · exact Or.inr ⟨d, by simp [hd], rfl⟩
121 simp only [curve, if_neg (not_le.2 h), heq, dual, List.map_cons, List.sum_cons]
122 rw [max_eq_left (by linarith)]
123 have hF := n_mul_rate hc
124 linarith [show (c.1 : ℚ) * (rate c - ρ) = c.2 - c.1 * ρ by rw [mul_sub, hF]]
125
126
127lemma sum_map_le {α : Type*} (l : List α) (f g : α → ℚ) (h : ∀ x ∈ l, f x ≤ g x) :
128 (l.map f).sum ≤ (l.map g).sum := by
129 induction l with
130 | nil => simp
131 | cons a l ih =>
132 simp only [List.map_cons, List.sum_cons]
133 exact add_le_add (h a (by simp)) (ih fun x hx => h x (by simp [hx]))
134
135lemma sum_flatMap_map {α β : Type*} (l : List α) (f : α → List β) (g : β → ℚ) :
136 ((l.flatMap f).map g).sum = (l.map fun a => ((f a).map g).sum).sum := by
137 induction l with
138 | nil => simp
139 | cons a l ih => simp [List.flatMap_cons, List.sum_append, ih]
140
141/-- Any allocation's value lies below every Lagrangian bound. -/
142theorem alloc_le_dual (L : List Cat) (P : List (Cat × ℚ)) (hP : IsAlloc L P) (ρ : ℚ) :
143 alloc_value P ≤ dual L ρ (alloc_total P) := by
144 have hperm : ((P.map Prod.fst).map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum =
145 (L.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum := (hP.1.map _).sum_eq
146 rw [List.map_map] at hperm
147 unfold dual alloc_value alloc_total
148 rw [← hperm]
149 have hsplit : ρ * (P.map Prod.snd).sum +
150 (P.map ((fun d => (d.1 : ℚ) * max (rate d - ρ) 0) ∘ Prod.fst)).sum =
151 (P.map fun p => ρ * p.2 + (p.1.1 : ℚ) * max (rate p.1 - ρ) 0).sum := by
152 clear hperm hP
153 induction P with
154 | nil => simp
155 | cons p P ih =>
156 simp only [List.map_cons, List.sum_cons, Function.comp] at ih ⊢
157 rw [← ih]; ring
158 rw [hsplit]
159 apply sum_map_le
160 intro p hp
161 obtain ⟨h0, h1⟩ := hP.2 p hp
162 have h2 : p.2 * (rate p.1 - ρ) ≤ p.2 * max (rate p.1 - ρ) 0 :=
163 mul_le_mul_of_nonneg_left (le_max_left _ _) h0
164 have h3 : p.2 * max (rate p.1 - ρ) 0 ≤ (p.1.1 : ℚ) * max (rate p.1 - ρ) 0 :=
165 mul_le_mul_of_nonneg_right h1 (le_max_right _ _)
166 nlinarith
167
168/-- The greedy allocation of `M` patches: fill the categories in list order. -/
169def galloc : List Cat → ℚ → List (Cat × ℚ)
170 | [], _ => []
171 | c :: L, M => if M ≤ c.1 then (c, M) :: L.map (fun d => (d, 0)) else (c, (c.1 : ℚ)) :: galloc L (M - c.1)
172
173lemma galloc_spec : ∀ (L : List Cat), LValid L → ∀ (M : ℚ), 0 ≤ M → M ≤ litems L →
174 IsAlloc L (galloc L M) ∧ alloc_value (galloc L M) = curve L M ∧ alloc_total (galloc L M) = M := by
175 intro L
176 induction L with
177 | nil =>
178 intro _ M hM hM'
179 simp only [litems, List.map_nil, List.sum_nil, Nat.cast_zero] at hM'
180 have : M = 0 := le_antisymm hM' hM
181 subst this
182 refine ⟨⟨by simp [galloc], by simp [galloc]⟩, by simp [galloc, alloc_value, curve],
183 by simp [galloc, alloc_total]⟩
184 | cons c L ih =>
185 intro hv M hM hM'
186 have hc := hv c (by simp)
187 have hL : LValid L := fun d hd => hv d (by simp [hd])
188 simp only [litems, List.map_cons, List.sum_cons, Nat.cast_add] at hM'
189 by_cases h : M ≤ c.1
190 · simp only [galloc, h, if_true, curve]
191 refine ⟨⟨?_, ?_⟩, ?_, ?_⟩
192 · simp [List.map_map, Function.comp_def]
193 · intro p hp
194 simp only [List.mem_cons, List.mem_map] at hp
195 rcases hp with rfl | ⟨d, _, rfl⟩
196 · exact ⟨hM, h⟩
197 · simp
198 · simp [alloc_value, Function.comp_def]
199 · simp [alloc_total, Function.comp_def]
200 · push Not at h
201 obtain ⟨⟨hperm, hbox⟩, hval, htot⟩ :=
202 ih hL (M - c.1) (by linarith) (by simp only [litems]; linarith)
203 simp only [galloc, if_neg (not_le.2 h), curve]
204 refine ⟨⟨?_, ?_⟩, ?_, ?_⟩
205 · simp only [List.map_cons]; exact hperm.cons c
206 · intro p hp
207 rcases List.mem_cons.1 hp with rfl | hp
208 · exact ⟨by positivity, le_rfl⟩
209 · exact hbox p hp
210 · simp only [alloc_value, List.map_cons, List.sum_cons] at hval ⊢
211 rw [hval, n_mul_rate hc]
212 · simp only [alloc_total, List.map_cons, List.sum_cons] at htot ⊢
213 rw [htot]; ring
214
215/-- **Proposition 3 (greedy is optimal).** With the categories in nonincreasing order of rate,
216filling them in turn maximizes the expected number of hits over all allocations of the same total
217`M`, at every patch count `0 ≤ M ≤ n`: the greedy curve is the greatest value. -/
218theorem prop3_greedy (L : List Cat) (hv : LValid L) (hs : Sorted L) (M : ℚ) (hM : 0 ≤ M)
219 (hM' : M ≤ litems L) :
220 IsGreatest {x | ∃ P, IsAlloc L P ∧ alloc_total P = M ∧ x = alloc_value P} (curve L M) := by
221 obtain ⟨hA, hval, htot⟩ := galloc_spec L hv M hM hM'
222 refine ⟨⟨galloc L M, hA, htot, hval.symm⟩, ?_⟩
223 rintro x ⟨P, hP, hPt, rfl⟩
224 obtain ⟨ρ, -, heq⟩ := curve_eq_dual L hv hs M hM hM'
225 rw [heq, ← hPt]
226 exact alloc_le_dual L P hP ρ
227
228/-! ### Proposition 5 -/
229
230/-- `Lf` refines `L`: the categories of `Lf` are grouped into parts, one part per category of `L`,
231whose item and failure counts add up to that category's (the finer map refines the coarser). -/
232def Refines (Lf L : List Cat) : Prop :=
233 ∃ parts : List (Cat × List Cat), (parts.map Prod.fst).Perm L ∧ (parts.flatMap Prod.snd).Perm Lf ∧
234 ∀ q ∈ parts, (q.2.map Prod.fst).sum = q.1.1 ∧ (q.2.map Prod.snd).sum = q.1.2
235
236lemma n_mul_pos {c : Cat} (h : c.2 ≤ c.1) (ρ : ℚ) :
237 (c.1 : ℚ) * max (rate c - ρ) 0 = max ((c.2 : ℚ) - ρ * c.1) 0 := by
238 rw [mul_max_of_nonneg _ _ (by positivity), mul_sub, n_mul_rate h, mul_zero, mul_comm]
239
240lemma max_sum_le (l : List ℚ) : max l.sum 0 ≤ (l.map fun x => max x 0).sum := by
241 induction l with
242 | nil => simp
243 | cons a l ih =>
244 simp only [List.sum_cons, List.map_cons]
245 have h1 := le_max_left a 0
246 have h2 := le_max_right a 0
247 have h4 : 0 ≤ (l.map fun x => max x 0).sum :=
248 List.sum_nonneg fun x hx => by obtain ⟨y, _, rfl⟩ := List.mem_map.1 hx; exact le_max_right _ _
249 have h5 := le_trans (le_max_left l.sum 0) ih
250 apply max_le <;> linarith
251
252lemma cast_sum_map {α : Type*} (l : List α) (f : α → ℕ) :
253 ((l.map f).sum : ℚ) = (l.map fun a => (f a : ℚ)).sum := by
254 induction l with
255 | nil => simp
256 | cons a l ih => simp [ih]
257
258/-- Refining can only raise the Lagrangian bound. -/
259lemma dual_tail_refine {L Lf : List Cat} (hR : Refines Lf L) (hv : LValid L) (hvf : LValid Lf) (ρ : ℚ) :
260 (L.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum ≤
261 (Lf.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0).sum := by
262 obtain ⟨parts, hp1, hp2, hp3⟩ := hR
263 rw [← (hp1.map _).sum_eq, ← (hp2.map _).sum_eq, List.map_map, sum_flatMap_map]
264 apply sum_map_le
265 intro q hq
266 have hqv : q.1.2 ≤ q.1.1 := hv q.1 (hp1.subset (List.mem_map_of_mem hq))
267 have hdv : ∀ d ∈ q.2, d.2 ≤ d.1 := fun d hd =>
268 hvf d (hp2.subset (List.mem_flatMap.2 ⟨q, hq, hd⟩))
269 simp only [Function.comp, n_mul_pos hqv]
270 rw [show (q.2.map fun d => (d.1 : ℚ) * max (rate d - ρ) 0) =
271 ((q.2.map fun d => (d.2 : ℚ) - ρ * d.1).map fun x => max x 0) by
272 rw [List.map_map]; exact List.map_congr_left fun d hd => n_mul_pos (hdv d hd) ρ]
273 refine le_trans (le_of_eq ?_) (max_sum_le _)
274 congr 1
275 obtain ⟨h1, h2⟩ := hp3 q hq
276 rw [← h1, ← h2, cast_sum_map, cast_sum_map]
277 clear h1 h2 hdv hqv hq
278 induction q.2 with
279 | nil => simp
280 | cons d l ih => simp only [List.map_cons, List.sum_cons]; linear_combination ih
281
282lemma litems_refine {L Lf : List Cat} (hR : Refines Lf L) : litems Lf = litems L := by
283 obtain ⟨parts, hp1, hp2, hp3⟩ := hR
284 unfold litems
285 rw [← (hp1.map _).sum_eq, ← (hp2.map _).sum_eq, List.map_map]
286 have : ((parts.flatMap Prod.snd).map Prod.fst).sum =
287 (parts.map fun q => (q.2.map Prod.fst).sum).sum := by
288 clear hp1 hp2 hp3
289 induction parts with
290 | nil => simp
291 | cons a l ih => simp [List.flatMap_cons, List.sum_append, ih]
292 rw [this]
293 congr 1
294 exact List.map_congr_left fun q hq => (hp3 q hq).1
295
296/-- Along refinement, the greedy expected-hit curve of the finer map (in its greedy order) lies
297above the curve of the coarser one (in any order), at every patch count. -/
298theorem curve_mono_refine {L Lf : List Cat} (hR : Refines Lf L) (hv : LValid L) (hvf : LValid Lf)
299 (hsf : Sorted Lf) (M : ℚ) (hM : 0 ≤ M) (hM' : M ≤ litems L) : curve L M ≤ curve Lf M := by
300 obtain ⟨ρ, hρ, heq⟩ := curve_eq_dual Lf hvf hsf M hM (by rw [litems_refine hR]; exact hM')
301 have hρ0 : 0 ≤ ρ := by
302 rcases hρ with rfl | ⟨d, _, rfl⟩
303 · exact le_rfl
304 · exact rate_nonneg d
305 calc curve L M ≤ dual L ρ M := curve_le_dual L hv ρ M hρ0 hM
306 _ ≤ dual Lf ρ M := by unfold dual; linarith [dual_tail_refine hR hv hvf ρ]
307 _ = curve Lf M := heq.symm
308
309lemma mstar_nonneg (pre : List Cat) (c : Cat) (H : ℕ) : 0 ≤ mstar pre c H := by
310 unfold mstar; positivity
311
312lemma mstar_le_items (pre post : List Cat) (c : Cat) (H : ℕ) (hc : c.2 ≤ c.1)
313 (hhi : H ≤ lfails pre + c.2) : mstar pre c H ≤ litems (pre ++ c :: post) := by
314 have hpost : (0 : ℚ) ≤ litems post := by positivity
315 have hsplit : (litems (pre ++ c :: post) : ℚ) = litems pre + c.1 + litems post := by
316 simp only [litems, List.map_append, List.map_cons, List.sum_append, List.sum_cons]
317 push_cast; ring
318 rw [hsplit]
319 unfold mstar
320 rcases Nat.eq_zero_or_pos c.2 with h0 | hpos
321 · rw [h0]; simp only [Nat.cast_zero, div_zero]
322 have : (0 : ℚ) ≤ c.1 := by positivity
323 linarith
324 have hF : (0 : ℚ) < c.2 := by exact_mod_cast hpos
325 have hHj : ((H - lfails pre : ℕ) : ℚ) ≤ c.2 := by exact_mod_cast (show H - lfails pre ≤ c.2 by omega)
326 have : ((H - lfails pre : ℕ) : ℚ) * c.1 / c.2 ≤ c.1 := by
327 rw [div_le_iff₀ hF]
328 have : (0 : ℚ) ≤ c.1 := by positivity
329 nlinarith
330 linarith
331
332/-- **Proposition 5 (monotonicity of `m*`).** If the finer map `Lf` refines `L`, the
333expected-score crossing of the greedy schedule under `Lf` comes no later than under `L`, for every
334failure pattern and every requirement `H` (`j` is the crossing category of each schedule). The
335coarse schedule may be in any order, in particular its greedy one. -/
336theorem prop5 (L Lf : List Cat) (hR : Refines Lf L) (hv : LValid L) (hvf : LValid Lf)
337 (hsf : Sorted Lf) (pre post pref postf : List Cat) (c cf : Cat)
338 (hL : L = pre ++ c :: post) (hLf : Lf = pref ++ cf :: postf) (H : ℕ)
339 (hlo : lfails pre < H) (hhi : H ≤ lfails pre + c.2)
340 (hlof : lfails pref < H) (hhif : H ≤ lfails pref + cf.2) :
341 mstar pref cf H ≤ mstar pre c H := by
342 by_contra hlt
343 push Not at hlt
344 subst hL hLf
345 have h1 := (prop4_crossing pre post c H hv hlo hhi).1
346 have h2 := (prop4_crossing pref postf cf H hvf hlof hhif).2 _ (mstar_nonneg pre c H) hlt
347 have h3 := curve_mono_refine hR hv hvf hsf _ (mstar_nonneg pre c H)
348 (mstar_le_items pre post c H (hv c (by simp)) hhi)
349 linarith
350
351end FeedbackChannel.PatchGreedy