lean/FeedbackChannel/Signal.lean

View on GitHub

  1import Mathlib.Tactic
  2import Mathlib.Data.Nat.Log
  3import Mathlib.Analysis.SpecialFunctions.Log.Basic
  4
  5/-!
  6# Training-signal limits: counting and algebra (L1, L2, L3, L4, L6)
  7
  8Source: `paper/rl_training_signal_limits.md` §4, and for L3 the ceiling law of
  9`paper/evaluation_feedback_insertions.md` §D.
 10
 11* Adaptive strategies are depth-indexed decision trees (`Strategy`). The counting lemma
 12  `Strategy.card_le_pow` says that a strategy of `q` queries whose answers all lie in a set of `m`
 13  values tells apart at most `m ^ q` hypotheses. L1 and L2 are instances.
 14* L3 derives the ceiling-check thresholds from the score formulas of the insertions note.
 15* L4 is the shared-error judge algebra, including the feasibility characterisation.
 16* L6 is the garbling direction of Blackwell's ordering for deterministic channels, and also for
 17  stochastic garblings.
 18-/
 19
 20namespace FeedbackChannel.Signal
 21
 22open Finset
 23
 24universe u
 25
 26/-! ## Adaptive strategies as decision trees -/
 27
 28/-- An adaptive strategy that asks exactly `q` queries in `Q`, reads each answer in `A` and may
 29choose the next query from the answers so far, then outputs a label in `L`. A strategy that stops
 30earlier is padded with queries whose answers it ignores, so "exactly `q`" loses nothing. -/
 31inductive Strategy (Q A L : Type u) : ℕ → Type u
 32  | done : L → Strategy Q A L 0
 33  | ask {q : ℕ} : Q → (A → Strategy Q A L q) → Strategy Q A L (q + 1)
 34
 35namespace Strategy
 36
 37variable {Q A L : Type u}
 38
 39/-- The label a strategy outputs against the oracle `α`, which answers query `x` with `α x`. -/
 40def run : {q : ℕ} → Strategy Q A L q → (Q → A) → L
 41  | _, done l, _ => l
 42  | _, ask x k, α => run (k (α x)) α
 43
 44/-- The labels a strategy can output when every answer lies in `vals`. -/
 45def outcomes [DecidableEq L] (vals : Finset A) : {q : ℕ} → Strategy Q A L q → Finset L
 46  | _, done l => {l}
 47  | _, ask _ k => vals.biUnion fun a => outcomes vals (k a)
 48
 49theorem card_outcomes_le [DecidableEq L] (vals : Finset A) :
 50    ∀ {q : ℕ} (s : Strategy Q A L q), (s.outcomes vals).card ≤ vals.card ^ q
 51  | _, done l => by simp [outcomes]
 52  | _, @ask _ _ _ q x k => by
 53      simp only [outcomes]
 54      calc (vals.biUnion fun a => (k a).outcomes vals).card
 55          ≤ ∑ a ∈ vals, ((k a).outcomes vals).card := card_biUnion_le
 56        _ ≤ ∑ _a ∈ vals, vals.card ^ q := sum_le_sum fun a _ => card_outcomes_le vals (k a)
 57        _ = vals.card ^ (q + 1) := by rw [sum_const, smul_eq_mul, pow_succ']
 58
 59theorem run_mem_outcomes [DecidableEq L] (vals : Finset A) (α : Q → A) (hα : ∀ x, α x ∈ vals) :
 60    ∀ {q : ℕ} (s : Strategy Q A L q), s.run α ∈ s.outcomes vals
 61  | _, done l => by simp [run, outcomes]
 62  | _, ask x k => by
 63      simp only [run, outcomes, mem_biUnion]
 64      exact ⟨α x, hα x, run_mem_outcomes vals α hα (k (α x))⟩
 65
 66/-- **Counting bound for adaptive strategies.** If every candidate hypothesis `h` answers every
 67query `x` with a value `ans h x` in `vals`, and the strategy's output separates the candidates
 68(distinct candidates give distinct outputs), then there are at most `#vals ^ q` candidates. -/
 69theorem card_le_pow {H : Type*} [DecidableEq L] (vals : Finset A) (cands : Finset H)
 70    (ans : H → Q → A) (hvals : ∀ h ∈ cands, ∀ x, ans h x ∈ vals) {q : ℕ}
 71    (s : Strategy Q A L q) (hdist : Set.InjOn (fun h => s.run (ans h)) cands) :
 72    cands.card ≤ vals.card ^ q := by
 73  calc cands.card = (cands.image fun h => s.run (ans h)).card := (card_image_of_injOn hdist).symm
 74    _ ≤ (s.outcomes vals).card := by
 75        apply card_le_card
 76        intro y hy
 77        obtain ⟨h, hh, rfl⟩ := mem_image.1 hy
 78        exact run_mem_outcomes vals (ans h) (hvals h hh) s
 79    _ ≤ vals.card ^ q := card_outcomes_le vals s
 80
 81end Strategy
 82
 83/-- From `N ≤ m ^ q`: `q` is at least `⌈log N / log m⌉` (real logarithms; for `m ≤ 1` the quotient
 84is `0` by Lean's conventions `log 0 = log 1 = 0` and `x / 0 = 0`). -/
 85theorem ceil_log_div_le_of_le_pow {N m q : ℕ} (h : N ≤ m ^ q) :
 86    ⌈Real.log N / Real.log m⌉ ≤ (q : ℤ) := by
 87  rw [Int.ceil_le]
 88  push_cast
 89  rcases Nat.lt_or_ge m 2 with hm | hm
 90  · have hlog : Real.log m = 0 := by
 91      interval_cases m <;> simp
 92    rw [hlog, div_zero]
 93    positivity
 94  · rcases Nat.eq_zero_or_pos N with hN | hN
 95    · subst hN
 96      simp only [Nat.cast_zero, Real.log_zero, zero_div]
 97      positivity
 98    · have hm1 : (1 : ℝ) < m := by exact_mod_cast (by omega : 1 < m)
 99      have hlogm : 0 < Real.log m := Real.log_pos hm1
100      rw [div_le_iff₀ hlogm]
101      have hNm : (N : ℝ) ≤ (m : ℝ) ^ q := by exact_mod_cast h
102      calc Real.log N ≤ Real.log ((m : ℝ) ^ q) :=
103            Real.log_le_log (by exact_mod_cast hN) hNm
104        _ = q * Real.log m := by rw [Real.log_pow]
105
106/-! ## L1. Bits per episode -/
107
108/-- **L1.** A deterministic channel `ch` on step records, with `m = #(image of ch)` possible values.
109In each of `q` episodes the loop picks an action `x` (adaptively, from the earlier rewards), the
110hypothesis `h` produces the step record `record h x`, and the loop sees `ch (record h x)`. If the
111loop's output tells apart every hypothesis in `cands`, then `#cands ≤ m ^ q`. -/
112theorem L1_card_le {R O X H L : Type} [Fintype R] [DecidableEq O] [DecidableEq L]
113    (ch : R → O) (record : H → X → R) (cands : Finset H) {q : ℕ} (s : Strategy X O L q)
114    (hdist : Set.InjOn (fun h => s.run fun x => ch (record h x)) cands) :
115    cands.card ≤ (univ.image ch).card ^ q :=
116  Strategy.card_le_pow (univ.image ch) cands (fun h x => ch (record h x))
117    (fun _ _ _ => mem_image_of_mem ch (mem_univ _)) s hdist
118
119/-- **L1, episode count.** Under the hypotheses of `L1_card_le`, the loop needs at least
120`⌈log H / log m⌉` episodes, `H = #cands`, `m` the channel's number of values; equivalently
121`Nat.clog m H ≤ q` when `m ≥ 2`. -/
122theorem L1_episodes {R O X H L : Type} [Fintype R] [DecidableEq O] [DecidableEq L]
123    (ch : R → O) (record : H → X → R) (cands : Finset H) {q : ℕ} (s : Strategy X O L q)
124    (hdist : Set.InjOn (fun h => s.run fun x => ch (record h x)) cands) :
125    ⌈Real.log cands.card / Real.log (univ.image ch).card⌉ ≤ (q : ℤ) ∧
126      (1 < (univ.image ch).card → Nat.clog (univ.image ch).card cands.card ≤ q) := by
127  have h := L1_card_le ch record cands s hdist
128  exact ⟨ceil_log_div_le_of_le_pow h, fun hm => (Nat.clog_le_iff_le_pow hm).2 h⟩
129
130/-- Outcome reward on `T` steps: `true` iff every step succeeded. -/
131def outcomeReward {T : ℕ} (r : Fin T → Bool) : Bool := decide (∀ i, r i = true)
132
133/-- **L1, example.** With `T = 3` steps, identifying the step record (eight hypotheses) through the
134outcome-reward channel takes at least three episodes, whatever the adaptive strategy. -/
135theorem L1_example_outcome {X L : Type} [DecidableEq L]
136    (record : (Fin 3 → Bool) → X → (Fin 3 → Bool)) {q : ℕ} (s : Strategy X Bool L q)
137    (hdist : Set.InjOn (fun h => s.run fun x => outcomeReward (record h x))
138      ((univ : Finset (Fin 3 → Bool)) : Set (Fin 3 → Bool))) : 3 ≤ q := by
139  have h := L1_card_le outcomeReward record univ s hdist
140  have hm : (univ.image (outcomeReward (T := 3))).card ≤ 2 := by
141    simpa using card_le_univ (univ.image (outcomeReward (T := 3)))
142  have h8 : (univ : Finset (Fin 3 → Bool)).card = 8 := by simp
143  rw [h8] at h
144  have h2 : 8 ≤ 2 ^ q := h.trans (Nat.pow_le_pow_left hm q)
145  by_contra hq
146  have : 2 ^ q ≤ 2 ^ 2 := Nat.pow_le_pow_right (by norm_num) (by omega)
147  omega
148
149/-- **L1, example.** With `T = 3` steps, the process-reward channel (the record itself) needs at
150least one episode to identify the eight records. -/
151theorem L1_example_process {X L : Type} [DecidableEq L]
152    (record : (Fin 3 → Bool) → X → (Fin 3 → Bool)) {q : ℕ}
153    (s : Strategy X (Fin 3 → Bool) L q)
154    (hdist : Set.InjOn (fun h => s.run fun x => id (record h x))
155      ((univ : Finset (Fin 3 → Bool)) : Set (Fin 3 → Bool))) : 1 ≤ q := by
156  have h := L1_card_le id record univ s hdist
157  have h8 : (univ : Finset (Fin 3 → Bool)).card = 8 := by simp
158  rw [h8] at h
159  by_contra hq
160  have hq0 : q = 0 := by omega
161  rw [hq0, pow_zero] at h
162  omega
163
164/-- Partial credit: the number of successful steps (Hamming weight of the record). -/
165def partialCredit {T : ℕ} (r : Fin T → Bool) : ℕ := (univ.filter fun i => r i = true).card
166
167/-- First failure: the length of the all-success prefix, i.e. the (0-based) position of the first
168failed step, or `T` if no step failed. -/
169def firstFailure {T : ℕ} (r : Fin T → Bool) : ℕ :=
170  (univ.filter fun i : Fin T => ∀ j, j ≤ i → r j = true).card
171
172/-- `f` factors through `g`: `f = k ∘ g` for some `k`. -/
173def FactorsThrough {R B C : Type*} (f : R → B) (g : R → C) : Prop :=
174  ∃ k : C → B, ∀ r, f r = k (g r)
175
176theorem not_factorsThrough_of {R B C : Type*} {f : R → B} {g : R → C} (r r' : R)
177    (hg : g r = g r') (hf : f r ≠ f r') : ¬ FactorsThrough f g := by
178  rintro ⟨k, hk⟩
179  exact hf (by rw [hk r, hk r', hg])
180
181/-- **L1, incomparability.** For every `T ≥ 2`, partial credit and first-failure are incomparable:
182neither factors through the other. (For `T ≤ 1` they coincide, so `T ≥ 2` is needed.) -/
183theorem L1_incomparable {T : ℕ} (hT : 2 ≤ T) :
184    ¬ FactorsThrough (partialCredit (T := T)) (firstFailure (T := T)) ∧
185    ¬ FactorsThrough (firstFailure (T := T)) (partialCredit (T := T)) := by
186  obtain ⟨i0, hi0⟩ : ∃ i : Fin T, i.val = 0 := ⟨⟨0, by omega⟩, rfl⟩
187  obtain ⟨i1, hi1⟩ : ∃ i : Fin T, i.val = 1 := ⟨⟨1, by omega⟩, rfl⟩
188  have h01 : i0 ≠ i1 := fun h => by rw [h] at hi0; omega
189  have hle0 : ∀ i : Fin T, i0 ≤ i := fun i => by
190    rw [Fin.le_iff_val_le_val, hi0]; exact Nat.zero_le _
191  -- three witness records: all steps fail; only step 0 fails; only step 1 fails
192  have ffF : firstFailure (fun _ : Fin T => false) = 0 := by
193    rw [firstFailure, card_eq_zero, filter_eq_empty_iff]
194    intro i _ h
195    exact absurd (h i0 (hle0 i)) (by simp)
196  have ff0 : firstFailure (fun i : Fin T => decide (i ≠ i0)) = 0 := by
197    rw [firstFailure, card_eq_zero, filter_eq_empty_iff]
198    intro i _ h
199    exact absurd (h i0 (hle0 i)) (by simp)
200  have ff1 : firstFailure (fun i : Fin T => decide (i ≠ i1)) = 1 := by
201    rw [firstFailure, card_eq_one]
202    refine ⟨i0, ?_⟩
203    ext i
204    simp only [mem_filter, mem_univ, true_and, mem_singleton, decide_eq_true_eq]
205    constructor
206    · intro h
207      by_contra hne
208      have hi : i1 ≤ i := by
209        rw [Fin.le_iff_val_le_val, hi1]
210        have : i.val ≠ 0 := fun h0 => hne (Fin.ext (h0.trans hi0.symm))
211        omega
212      exact h i1 hi rfl
213    · intro hi j hj
214      rw [hi] at hj
215      have : j = i0 := le_antisymm hj (hle0 j)
216      rw [this]; exact h01
217  have pcF : partialCredit (fun _ : Fin T => false) = 0 := by simp [partialCredit]
218  have pc0 : partialCredit (fun i : Fin T => decide (i ≠ i0)) = T - 1 := by
219    simp [partialCredit, filter_ne', card_erase_of_mem]
220  have pc1 : partialCredit (fun i : Fin T => decide (i ≠ i1)) = T - 1 := by
221    simp [partialCredit, filter_ne', card_erase_of_mem]
222  constructor
223  · exact not_factorsThrough_of (fun _ : Fin T => false) (fun i : Fin T => decide (i ≠ i0))
224      (by rw [ffF, ff0]) (by rw [pcF, pc0]; omega)
225  · exact not_factorsThrough_of (fun i : Fin T => decide (i ≠ i0)) (fun i : Fin T => decide (i ≠ i1))
226      (by rw [pc0, pc1]) (by rw [ff0, ff1]; omega)
227
228/-! ## L2. Score queries -/
229
230/-- The selection set's score after patching the items in `Q`, when the failed items are `S`:
231the number of items that pass (unfailed, or failed and patched). -/
232def patchedScore (n : ℕ) (S Q : Finset (Fin n)) : ℕ :=
233  (univ.filter fun i => i ∉ S ∨ i ∈ Q).card
234
235/-- With `f` failures every score query returns one of the `f + 1` values `n - f, …, n`. -/
236theorem patchedScore_mem {n f : ℕ} {S : Finset (Fin n)} (hS : S.card = f) (Q : Finset (Fin n)) :
237    patchedScore n S Q ∈ Icc (n - f) n := by
238  rw [mem_Icc]
239  constructor
240  · have h1 : Sᶜ ⊆ univ.filter (fun i => i ∉ S ∨ i ∈ Q) := by
241      intro i hi
242      rw [mem_compl] at hi
243      simp [hi]
244    have h2 := card_le_card h1
245    rw [card_compl, Fintype.card_fin, hS] at h2
246    exact h2
247  · calc patchedScore n S Q ≤ (univ : Finset (Fin n)).card := card_filter_le _ _
248      _ = n := by simp
249
250/-- **L2, lower bound.** Any adaptive strategy of `q` score queries (each query patches a chosen
251subset and reads the set's score) whose output separates the `C(n, f)` possible failure sets of
252size `f` satisfies `C(n, f) ≤ (f + 1) ^ q`. -/
253theorem L2_lower_bound {n f q : ℕ} {L : Type} [DecidableEq L]
254    (s : Strategy (Finset (Fin n)) ℕ L q)
255    (hdist : Set.InjOn (fun S => s.run (patchedScore n S))
256      ((powersetCard f (univ : Finset (Fin n)) : Finset (Finset (Fin n))) : Set (Finset (Fin n)))) :
257    n.choose f ≤ (f + 1) ^ q := by
258  rcases le_or_gt f n with hf | hf
259  · have h := Strategy.card_le_pow (Icc (n - f) n) (powersetCard f univ) (patchedScore n)
260      (fun S hS Q => patchedScore_mem (mem_powersetCard.1 hS).2 Q) s hdist
261    rwa [card_powersetCard, card_univ, Fintype.card_fin, Nat.card_Icc,
262      (by omega : n + 1 - (n - f) = f + 1)] at h
263  · rw [Nat.choose_eq_zero_of_lt hf]
264    exact Nat.zero_le _
265
266/-- **L2, as stated in the note.** A strategy of `q` score queries that identifies every failure
267set of size `f` (its output is the failure set) has `q ≥ ⌈log C(n,f) / log (f+1)⌉`, and
268`q ≥ Nat.clog (f + 1) C(n, f)` (the exact integer ceiling) when `f ≥ 1`. -/
269theorem L2_qmin {n f q : ℕ} (s : Strategy (Finset (Fin n)) ℕ (Finset (Fin n)) q)
270    (hid : ∀ S ∈ powersetCard f (univ : Finset (Fin n)), s.run (patchedScore n S) = S) :
271    n.choose f ≤ (f + 1) ^ q ∧
272      ⌈Real.log (n.choose f) / Real.log (f + 1)⌉ ≤ (q : ℤ) ∧
273      (1 ≤ f → Nat.clog (f + 1) (n.choose f) ≤ q) := by
274  have hdist : Set.InjOn (fun S => s.run (patchedScore n S))
275      ((powersetCard f (univ : Finset (Fin n)) : Finset (Finset (Fin n))) : Set (Finset (Fin n))) := by
276    intro S hS S' hS' h
277    exact (hid S hS).symm.trans (h.trans (hid S' hS'))
278  have h := L2_lower_bound s hdist
279  refine ⟨h, ?_, fun hf => (Nat.clog_le_iff_le_pow (by omega)).2 h⟩
280  have := ceil_log_div_le_of_le_pow h
281  push_cast at this
282  exact this
283
284/-! ## L3. Noise ceiling and its blind range -/
285
286/-- Per-item ceiling: a predictor blind to label flips at rate `ε ≤ 1/2` that is right on the
287true label with probability `c ≤ 1` matches the observed label with probability at most `1 - ε`. -/
288theorem L3_ceiling {c ε : ℝ} (hc1 : c ≤ 1) (hε : ε ≤ 1 / 2) :
289    c * (1 - ε) + (1 - c) * ε ≤ 1 - ε := by
290  nlinarith [mul_nonneg (sub_nonneg.2 hc1) (by linarith : (0 : ℝ) ≤ 1 - 2 * ε)]
291
292/-- Score after memorizing a share `x` of the set chosen without regard to outcomes
293(insertions note: `m/n + (1 - m/n) v`). -/
294def blindScore (v x : ℝ) : ℝ := x + (1 - x) * v
295
296/-- Score after memorizing a share `x` of the set taken from the failed items
297(insertions note: `v + m/n`). -/
298def patchScore (v x : ℝ) : ℝ := v + x
299
300/-- **L3, blind memorization.** For `v < 1` the score crosses the ceiling `1 - ε` iff the memorized
301share exceeds `(1 - ε - v) / (1 - v)`. -/
302theorem L3_blind {v ε x : ℝ} (hv : v < 1) :
303    1 - ε < blindScore v x ↔ (1 - ε - v) / (1 - v) < x := by
304  have h : 0 < 1 - v := by linarith
305  rw [div_lt_iff₀ h, blindScore]
306  constructor <;> intro <;> linarith
307
308/-- **L3, patching failures.** The score crosses the ceiling iff the patched share of the set
309exceeds `1 - ε - v`. -/
310theorem L3_patch {v ε x : ℝ} : 1 - ε < patchScore v x ↔ 1 - ε - v < x := by
311  rw [patchScore]
312  constructor <;> intro <;> linarith
313
314/-- **L3, patching failures, as a share of the failed items.** Patching a share `y` of the failed
315items (a share `y (1 - v)` of the set) crosses the ceiling iff `y > (1 - ε - v)/(1 - v)`. -/
316theorem L3_patch_share_of_failed {v ε y : ℝ} (hv : v < 1) :
317    1 - ε < patchScore v (y * (1 - v)) ↔ (1 - ε - v) / (1 - v) < y := by
318  have h : 0 < 1 - v := by linarith
319  rw [div_lt_iff₀ h, patchScore]
320  constructor <;> intro <;> linarith
321
322/-- Item-level derivation of the patching score: with pass set `P` among `n` items, patching a
323set `M` of failed items gives the observed score `|P|/n + |M|/n`. -/
324theorem L3_patch_count {n : ℕ} (P M : Finset (Fin n)) (hM : M ⊆ Pᶜ) :
325    ((P ∪ M).card : ℝ) / n = (P.card : ℝ) / n + (M.card : ℝ) / n := by
326  have hd : Disjoint P M := by
327    rw [disjoint_iff_ne]
328    intro a ha b hb hab
329    subst hab
330    exact (mem_compl.1 (hM hb)) ha
331  rw [card_union_of_disjoint hd]
332  push_cast
333  rw [add_div]
334
335/-- Item-level derivation of the blind score: averaging over all `C(n, m)` memorized sets `M` of
336size `m`, the observed score `|P ∪ M| / n` is `m/n + (1 - m/n) v` with `v = |P|/n`. -/
337theorem L3_blind_average {n m : ℕ} (hn : 0 < n) (hm : m ≤ n) (P : Finset (Fin n)) :
338    (∑ M ∈ powersetCard m (univ : Finset (Fin n)), ((P ∪ M).card : ℝ)) / (n.choose m) / n =
339      (m : ℝ) / n + (1 - (m : ℝ) / n) * ((P.card : ℝ) / n) := by
340  -- count in ℕ first
341  have hcount : ∀ i : Fin n,
342      ((powersetCard m (univ : Finset (Fin n))).filter fun M => i ∉ M).card = (n - 1).choose m := by
343    intro i
344    have : ((powersetCard m (univ : Finset (Fin n))).filter fun M => i ∉ M) =
345        powersetCard m (univ.erase i) := by
346      ext M
347      simp only [mem_filter, mem_powersetCard, subset_erase, subset_univ, true_and]
348      tauto
349    rw [this, card_powersetCard, card_erase_of_mem (mem_univ i), card_univ, Fintype.card_fin]
350  have hsum : ∑ M ∈ powersetCard m (univ : Finset (Fin n)), (P ∪ M).card =
351      n.choose m * m + P.card * (n - 1).choose m := by
352    have h1 : ∀ M ∈ powersetCard m (univ : Finset (Fin n)),
353        (P ∪ M).card = m + ∑ i ∈ P, if i ∉ M then 1 else 0 := by
354      intro M hM
355      rw [← card_sdiff_add_card, (mem_powersetCard.1 hM).2, add_comm (P \ M).card m,
356        sdiff_eq_filter, card_filter]
357    rw [sum_congr rfl h1, sum_add_distrib, sum_const, card_powersetCard, card_univ,
358      Fintype.card_fin, smul_eq_mul, sum_comm]
359    congr 1
360    calc ∑ i ∈ P, ∑ M ∈ powersetCard m (univ : Finset (Fin n)), (if i ∉ M then 1 else 0)
361        = ∑ _i ∈ P, (n - 1).choose m :=
362          sum_congr rfl fun i _ => by rw [← card_filter, hcount i]
363      _ = P.card * (n - 1).choose m := by rw [sum_const, smul_eq_mul]
364  have hchoose : ((n - 1).choose m : ℝ) * n = (n.choose m : ℝ) * (n - m) := by
365    have h := Nat.choose_mul_succ_eq (n - 1) m
366    rw [Nat.sub_add_cancel hn] at h
367    rw [← Nat.cast_sub hm]
368    exact_mod_cast h
369  have hn' : (0 : ℝ) < n := by exact_mod_cast hn
370  have hC : (0 : ℝ) < n.choose m := by exact_mod_cast Nat.choose_pos hm
371  have hsumR : ∑ M ∈ powersetCard m (univ : Finset (Fin n)), ((P ∪ M).card : ℝ) =
372      (n.choose m : ℝ) * m + (P.card : ℝ) * ((n - 1).choose m : ℝ) := by
373    exact_mod_cast hsum
374  have hC' : ((n - 1).choose m : ℝ) = (n.choose m : ℝ) * (n - m) / n :=
375    (eq_div_iff hn'.ne').2 hchoose
376  rw [hsumR, hC']
377  field_simp
378
379/-- **L3, fixture.** Clean accuracy `4/5` and judge error `1/10` give the observed accuracy
380`v = 4/5 · 9/10 + 1/5 · 1/10 = 37/50`; the blind-memorization threshold is `8/13` of the set; the
381failure-patching threshold is `4/25` of the set, which is `8/13` of the failed items. -/
382theorem L3_fixture :
383    (4 / 5 : ℝ) * (1 - 1 / 10) + (1 - 4 / 5) * (1 / 10) = 37 / 50 ∧
384    ((1 : ℝ) - 1 / 10 - 37 / 50) / (1 - 37 / 50) = 8 / 13 ∧
385    (1 : ℝ) - 1 / 10 - 37 / 50 = 4 / 25 ∧
386    ((4 : ℝ) / 25) / (1 - 37 / 50) = 8 / 13 := by
387  norm_num
388
389/-- **L3, fixture as thresholds.** At `v = 37/50` and `ε = 1/10`: blind memorization crosses the
390ceiling iff more than `8/13` of the set is memorized; failure patching iff more than `4/25` of the
391set is patched, i.e. more than `8/13` of the failed items. -/
392theorem L3_fixture_thresholds :
393    (∀ x : ℝ, 1 - 1 / 10 < blindScore (37 / 50) x ↔ 8 / 13 < x) ∧
394    (∀ x : ℝ, 1 - 1 / 10 < patchScore (37 / 50) x ↔ 4 / 25 < x) ∧
395    (∀ y : ℝ, 1 - 1 / 10 < patchScore (37 / 50) (y * (1 - 37 / 50)) ↔ 8 / 13 < y) := by
396  refine ⟨fun x => ?_, fun x => ?_, fun y => ?_⟩
397  · rw [L3_blind (by norm_num)]; norm_num
398  · rw [L3_patch]; norm_num
399  · rw [L3_patch_share_of_failed (by norm_num)]; norm_num
400
401/-! ## L4. A judge that shares errors with the judged -/
402
403/-- Observed accuracy with a judge wrong at rate `e0` on items the controller gets right and `e1`
404on items it gets wrong, the controller being right with probability `a`. -/
405def observed (a e0 e1 : ℝ) : ℝ := a * (1 - e0) + (1 - a) * e1
406
407/-- The judge's marginal error rate `ε = a e0 + (1 - a) e1`. -/
408def marginal (a e0 e1 : ℝ) : ℝ := a * e0 + (1 - a) * e1
409
410/-- **L4, identity.** `a(1 - ε₀) + (1 - a)ε₁ = a - ε + 2(1 - a)ε₁`. -/
411theorem L4_identity (a e0 e1 : ℝ) :
412    observed a e0 e1 = a - marginal a e0 e1 + 2 * (1 - a) * e1 := by
413  unfold observed marginal; ring
414
415/-- **L4, excess.** Against an independent judge with the same marginal error `ε`, the observed
416accuracy is higher by exactly `2(1 - a)(ε₁ - ε)`. -/
417theorem L4_excess (a e0 e1 : ℝ) :
418    observed a e0 e1 - observed a (marginal a e0 e1) (marginal a e0 e1) =
419      2 * (1 - a) * (e1 - marginal a e0 e1) := by
420  unfold observed marginal; ring
421
422/-- **L4, ceiling.** For `a < 1` the observed accuracy reaches the ceiling `1 - ε` iff
423`ε₁ ≥ 1/2`, whatever `a` and `ε`. (The note states it for `0 < a < 1`; `a = 0` is included.) -/
424theorem L4_ceiling_iff {a e0 e1 : ℝ} (ha : a < 1) :
425    1 - marginal a e0 e1 ≤ observed a e0 e1 ↔ 1 / 2 ≤ e1 := by
426  rw [L4_identity]
427  have h : 0 < 1 - a := by linarith
428  constructor
429  · intro hc; nlinarith
430  · intro hc; nlinarith
431
432/-- **L4, feasibility cap.** A judge with `ε₀ ≥ 0` has `(1 - a) ε₁ ≤ ε`, i.e. `ε₁ ≤ ε / (1 - a)`. -/
433theorem L4_cap {a e0 e1 : ℝ} (ha : 0 ≤ a) (he0 : 0 ≤ e0) : (1 - a) * e1 ≤ marginal a e0 e1 := by
434  unfold marginal; nlinarith [mul_nonneg ha he0]
435
436/-- **L4, reachability.** For `0 < a < 1` and `ε ≤ 1`, some feasible judge (rates in
437`[0, 1]` with marginal `ε`) reports at least the ceiling `1 - ε` iff `ε ≥ (1 - a)/2`. -/
438theorem L4_reachable_iff {a ε : ℝ} (ha0 : 0 < a) (ha1 : a < 1) (hε1 : ε ≤ 1) :
439    (∃ e0 e1 : ℝ, 0 ≤ e0 ∧ e0 ≤ 1 ∧ 0 ≤ e1 ∧ e1 ≤ 1 ∧ marginal a e0 e1 = ε ∧
440        1 - ε ≤ observed a e0 e1) ↔ (1 - a) / 2 ≤ ε := by
441  constructor
442  · rintro ⟨e0, e1, h0, -, -, -, hm, hc⟩
443    rw [← hm] at hc
444    have he1 := (L4_ceiling_iff ha1).1 hc
445    have hcap := L4_cap (e1 := e1) ha0.le h0
446    rw [hm] at hcap
447    nlinarith
448  · intro hε
449    have ha0' : a ≠ 0 := ha0.ne'
450    have ha1' : 1 - a ≠ 0 := by linarith
451    rcases le_or_gt ε ((1 + a) / 2) with hle | hgt
452    · refine ⟨(ε - (1 - a) / 2) / a, 1 / 2, div_nonneg (by linarith) ha0.le, ?_, by norm_num,
453        by norm_num, ?_, ?_⟩
454      · rw [div_le_one ha0]; linarith
455      · unfold marginal; field_simp; ring
456      · have hm : marginal a ((ε - (1 - a) / 2) / a) (1 / 2) = ε := by
457          unfold marginal; field_simp; ring
458        have := (L4_ceiling_iff (e0 := (ε - (1 - a) / 2) / a) ha1).2 (le_refl _)
459        rwa [hm] at this
460    · have h1a : 0 < 1 - a := by linarith
461      refine ⟨1, (ε - a) / (1 - a), by norm_num, le_refl _, div_nonneg (by linarith) h1a.le,
462        ?_, ?_, ?_⟩
463      · rw [div_le_one h1a]; linarith
464      · unfold marginal; field_simp; ring
465      · have hm : marginal a 1 ((ε - a) / (1 - a)) = ε := by
466          unfold marginal; field_simp; ring
467        have hhalf : 1 / 2 ≤ (ε - a) / (1 - a) := by
468          rw [le_div_iff₀ h1a]; linarith
469        have := (L4_ceiling_iff (e0 := 1) ha1).2 hhalf
470        rwa [hm] at this
471
472/-- **L4, most correlated feasible judge.** With `a ≥ 0` and `ε₀ ≥ 0`, observed accuracy is at most
473`a + ε`. -/
474theorem L4_observed_le {a e0 e1 : ℝ} (ha : 0 ≤ a) (he0 : 0 ≤ e0) :
475    observed a e0 e1 ≤ a + marginal a e0 e1 := by
476  have := L4_cap (e1 := e1) ha he0
477  rw [L4_identity]; linarith
478
479/-- **L4, fixture** (`a = 4/5`, `ε = 1/10`): the independent judge reports `37/50`; the most
480correlated feasible judge (`ε₀ = 0`, `ε₁ = ε/(1 - a) = 1/2`) has marginal `1/10` and reports
481`9/10`, the ceiling; the inflation is `4/25`. -/
482theorem L4_fixture :
483    observed (4 / 5) (1 / 10) (1 / 10) = 37 / 50 ∧
484    marginal (4 / 5) 0 (1 / 2) = 1 / 10 ∧
485    observed (4 / 5) 0 (1 / 2) = 9 / 10 ∧
486    observed (4 / 5) 0 (1 / 2) = 1 - 1 / 10 ∧
487    observed (4 / 5) 0 (1 / 2) - observed (4 / 5) (1 / 10) (1 / 10) = 4 / 25 := by
488  unfold observed marginal; norm_num
489
490/-! ## L6. Finer feedback is worth at least as much (Blackwell, garbling direction) -/
491
492/-- Reordering a triple sum. -/
493theorem sum_comm3 {α β γ : Type*} [Fintype α] [Fintype β] [Fintype γ] (F : α → β → γ → ℝ) :
494    ∑ a, ∑ b, ∑ c, F a b c = ∑ c, ∑ a, ∑ b, F a b c :=
495  calc ∑ a, ∑ b, ∑ c, F a b c = ∑ a, ∑ c, ∑ b, F a b c :=
496        Finset.sum_congr rfl fun _ _ => Finset.sum_comm
497    _ = ∑ c, ∑ a, ∑ b, F a b c := Finset.sum_comm
498
499section Blackwell
500
501variable {Θ R Y Y' Act : Type*} [Fintype Θ] [Fintype R] [Fintype Act] [Nonempty Act]
502
503/-- Expected utility of the decision rule `δ` (signal ↦ action) when the loop sees `g r` for the
504record `r`, a type `θ` has prior weight `prior θ`, and a record `r` has likelihood `lik θ r`. -/
505def expUtil (prior : Θ → ℝ) (lik : Θ → R → ℝ) (u : Act → Θ → ℝ) (g : R → Y) (δ : Y → Act) : ℝ :=
506  ∑ θ, ∑ r, prior θ * lik θ r * u (δ (g r)) θ
507
508/-- Bayes value of the deterministic channel `g`: the best expected utility over all decision
509rules on `g`'s images. -/
510def bayesValue [Fintype Y] [DecidableEq Y] (prior : Θ → ℝ) (lik : Θ → R → ℝ)
511    (u : Act → Θ → ℝ) (g : R → Y) : ℝ :=
512  univ.sup' univ_nonempty (expUtil prior lik u g)
513
514/-- **L6.** For finite types, any prior, any likelihood of records given types and any utility, the
515Bayes value under channel `g` is at least the value under any channel `f` that factors through
516`g`. No sign condition on the weights is needed. -/
517theorem L6_garbling [Fintype Y] [DecidableEq Y] [Fintype Y'] [DecidableEq Y']
518    (prior : Θ → ℝ) (lik : Θ → R → ℝ) (u : Act → Θ → ℝ) (g : R → Y) (f : R → Y')
519    (k : Y → Y') (hf : ∀ r, f r = k (g r)) :
520    bayesValue prior lik u f ≤ bayesValue prior lik u g := by
521  apply Finset.sup'_le
522  intro δ _
523  have : expUtil prior lik u f δ = expUtil prior lik u g (δ ∘ k) := by
524    simp only [expUtil, hf, Function.comp_apply]
525  rw [this]
526  exact Finset.le_sup' _ (mem_univ _)
527
528/-- Expected utility of `δ` under a stochastic channel `G r y = Pr[signal y | record r]`. -/
529def expUtilK (prior : Θ → ℝ) (lik : Θ → R → ℝ) (u : Act → Θ → ℝ) (G : R → Y → ℝ)
530    (δ : Y → Act) [Fintype Y] : ℝ :=
531  ∑ θ, ∑ r, ∑ y, prior θ * lik θ r * G r y * u (δ y) θ
532
533/-- Bayes value of a stochastic channel. -/
534def bayesValueK [Fintype Y] [DecidableEq Y] (prior : Θ → ℝ) (lik : Θ → R → ℝ)
535    (u : Act → Θ → ℝ) (G : R → Y → ℝ) : ℝ :=
536  univ.sup' univ_nonempty fun δ => expUtilK prior lik u G δ
537
538/-- The garbling `K ∘ G` of a stochastic channel by a Markov kernel `K`. -/
539def garble [Fintype Y] (G : R → Y → ℝ) (K : Y → Y' → ℝ) : R → Y' → ℝ :=
540  fun r y' => ∑ y, G r y * K y y'
541
542/-- **L6, stochastic garbling (strengthening).** If `K` is a Markov kernel (non-negative entries,
543rows summing to one), the Bayes value of `K ∘ G` is at most that of `G`, for any prior, likelihood
544and utility. The deterministic case is `K y y' = [y' = k y]`. -/
545theorem L6_garbling_stochastic [Fintype Y] [DecidableEq Y] [Fintype Y'] [DecidableEq Y']
546    (prior : Θ → ℝ) (lik : Θ → R → ℝ) (u : Act → Θ → ℝ) (G : R → Y → ℝ) (K : Y → Y' → ℝ)
547    (hK0 : ∀ y y', 0 ≤ K y y') (hK1 : ∀ y, ∑ y', K y y' = 1) :
548    bayesValueK prior lik u (garble G K) ≤ bayesValueK prior lik u G := by
549  set h : Y → Act → ℝ := fun y a => ∑ θ, ∑ r, prior θ * lik θ r * G r y * u a θ with hh
550  have hG : ∀ δ : Y → Act, expUtilK prior lik u G δ = ∑ y, h y (δ y) := by
551    intro δ
552    simp only [expUtilK, hh]
553    exact sum_comm3 _
554  have hGK : ∀ δ : Y' → Act, expUtilK prior lik u (garble G K) δ =
555      ∑ y', ∑ y, K y y' * h y (δ y') := by
556    intro δ
557    calc expUtilK prior lik u (garble G K) δ
558        = ∑ θ, ∑ r, ∑ y', ∑ y, K y y' * (prior θ * lik θ r * G r y * u (δ y') θ) := by
559          simp only [expUtilK, garble, Finset.mul_sum, Finset.sum_mul]
560          refine sum_congr rfl fun θ _ => sum_congr rfl fun r _ => sum_congr rfl fun y' _ =>
561            sum_congr rfl fun y _ => by ring
562      _ = ∑ y', ∑ θ, ∑ r, ∑ y, K y y' * (prior θ * lik θ r * G r y * u (δ y') θ) :=
563          sum_comm3 _
564      _ = ∑ y', ∑ y, ∑ θ, ∑ r, K y y' * (prior θ * lik θ r * G r y * u (δ y') θ) :=
565          sum_congr rfl fun y' _ => sum_comm3 _
566      _ = ∑ y', ∑ y, K y y' * h y (δ y') := by
567          simp only [hh, Finset.mul_sum]
568  -- a best response for each signal of `G`
569  have hbest : ∀ y, ∃ a, ∀ a', h y a' ≤ h y a := fun y => Finite.exists_max (h y)
570  choose b hb using hbest
571  apply Finset.sup'_le
572  intro δ _
573  rw [hGK]
574  calc ∑ y', ∑ y, K y y' * h y (δ y') ≤ ∑ y', ∑ y, K y y' * h y (b y) :=
575        sum_le_sum fun y' _ => sum_le_sum fun y _ =>
576          mul_le_mul_of_nonneg_left (hb y (δ y')) (hK0 y y')
577    _ = ∑ y, (∑ y', K y y') * h y (b y) := by
578        rw [Finset.sum_comm]
579        refine Finset.sum_congr rfl fun y _ => ?_
580        rw [Finset.sum_mul]
581    _ = ∑ y, h y (b y) := by simp [hK1]
582    _ = expUtilK prior lik u G b := (hG b).symm
583    _ ≤ bayesValueK prior lik u G := Finset.le_sup' (fun δ => expUtilK prior lik u G δ) (mem_univ b)
584
585end Blackwell
586
587end FeedbackChannel.Signal