lean/FeedbackChannel/Raters.lean

View on GitHub

  1import Mathlib.Tactic
  2import Mathlib.Analysis.Real.Sqrt
  3
  4/-!
  5# Gold calibration with independent raters: Walsh identities, Dawid–Skene moments
  6
  7Source: `paper/gold_calibration_second_grader.md`, §1–3 (symmetric model, Propositions 1–3),
  8§7 (asymmetric three-rater model, Proposition 4) and §8 (four raters: Proposition 5, the six
  9equalities, the copying grader). The Jacobian minor of §8 (Proposition 6b) is in
 10`FeedbackChannel.RatersJacobian`.
 11
 12**Model** (two-class Dawid–Skene on `n` raters). The true label `Y ∈ {0, 1}` has
 13`Pr[Y = 1] = π`. Given `Y`, the raters report independently; rater `i` has false-negative rate
 14`α i = Pr[X_i = 0 | Y = 1]` and false-positive rate `β i = Pr[X_i = 1 | Y = 0]`. Reports are
 15`Bool`s (`true` = 1). `prob π α β x` is the probability of the report vector `x`, obtained by
 16summing the latent label out. The symmetric model of §1 is `α = β = e` (rater `i` flips the label
 17with probability `e i`). Every moment below is a sum over the `2 ^ n` report vectors.
 18-/
 19
 20namespace FeedbackChannel.Raters
 21
 22open Finset
 23
 24/-- Mass a rater puts on report `b` when it reports `true` with probability `q`. -/
 25def bern (q : ℝ) (b : Bool) : ℝ := if b then q else 1 - q
 26
 27/-- Indicator of a positive report. -/
 28def ind (b : Bool) : ℝ := if b then 1 else 0
 29
 30/-- `(-1) ^ x` for a report `x ∈ {0, 1}`. -/
 31def sgn (b : Bool) : ℝ := if b then -1 else 1
 32
 33@[simp] lemma bern_true (q : ℝ) : bern q true = q := rfl
 34@[simp] lemma bern_false (q : ℝ) : bern q false = 1 - q := rfl
 35@[simp] lemma ind_true : ind true = 1 := rfl
 36@[simp] lemma ind_false : ind false = 0 := rfl
 37@[simp] lemma sgn_true : sgn true = -1 := rfl
 38@[simp] lemma sgn_false : sgn false = 1 := rfl
 39
 40variable {n : ℕ}
 41
 42/-- Probability of the report vector `x` in the two-class model. -/
 43def prob (π : ℝ) (α β : Fin n → ℝ) (x : Fin n → Bool) : ℝ :=
 44  π * ∏ i, bern (1 - α i) (x i) + (1 - π) * ∏ i, bern (β i) (x i)
 45
 46theorem sum_prod_bool (f : Fin n → Bool → ℝ) :
 47    ∑ x : Fin n → Bool, ∏ i, f i (x i) = ∏ i, (f i true + f i false) :=
 48  calc ∑ x : Fin n → Bool, ∏ i, f i (x i) = ∏ i, ∑ b, f i b := (Fintype.prod_sum f).symm
 49    _ = ∏ i, (f i true + f i false) := Finset.prod_congr rfl fun _ _ => Fintype.sum_bool _
 50
 51/-- Conditional independence multiplies: the expectation of a product of per-rater functions over
 52a set `S` of raters is the prior mixture of the products of per-rater conditional expectations. -/
 53theorem expect_prod_on (π : ℝ) (α β : Fin n → ℝ) (S : Finset (Fin n)) (h : Fin n → Bool → ℝ) :
 54    ∑ x, prob π α β x * ∏ i ∈ S, h i (x i) =
 55      π * ∏ i ∈ S, ((1 - α i) * h i true + α i * h i false) +
 56      (1 - π) * ∏ i ∈ S, (β i * h i true + (1 - β i) * h i false) := by
 57  have key : ∀ q : Fin n → ℝ, ∑ x : Fin n → Bool, (∏ i, bern (q i) (x i)) * ∏ i ∈ S, h i (x i) =
 58      ∏ i ∈ S, (q i * h i true + (1 - q i) * h i false) := by
 59    intro q
 60    have hx : ∀ x : Fin n → Bool, (∏ i, bern (q i) (x i)) * ∏ i ∈ S, h i (x i) =
 61        ∏ i, (bern (q i) (x i) * if i ∈ S then h i (x i) else 1) := by
 62      intro x
 63      rw [← Fintype.prod_ite_mem S (fun i => h i (x i)), ← Finset.prod_mul_distrib]
 64    rw [Finset.sum_congr rfl fun x _ => hx x,
 65      sum_prod_bool (fun i b => bern (q i) b * if i ∈ S then h i b else 1),
 66      ← Fintype.prod_ite_mem S (fun i => q i * h i true + (1 - q i) * h i false)]
 67    refine Finset.prod_congr rfl fun i _ => ?_
 68    by_cases hi : i ∈ S <;> simp [hi]
 69  have e1 := key (fun i => 1 - α i)
 70  have e2 := key β
 71  simp only [sub_sub_cancel] at e1
 72  simp only [prob, add_mul, Finset.sum_add_distrib, mul_assoc, ← Finset.mul_sum, e1, e2]
 73
 74/-- The report distribution sums to one. -/
 75theorem mass_one (π : ℝ) (α β : Fin n → ℝ) : ∑ x, prob π α β x = 1 := by
 76  have h := expect_prod_on π α β ∅ (fun _ _ => 1)
 77  simp only [prod_empty, mul_one] at h
 78  rw [h]; ring
 79
 80/-- **Label switching (§7).** `(π, α, β)` and `(1 - π, 1 - β, 1 - α)` give the same table. -/
 81theorem label_switch (π : ℝ) (α β : Fin n → ℝ) (x : Fin n → Bool) :
 82    prob (1 - π) (fun i => 1 - β i) (fun i => 1 - α i) x = prob π α β x := by
 83  simp only [prob, sub_sub_cancel]; ring
 84
 85/-! ## Symmetric model (§1–§3) -/
 86
 87/-- Walsh coefficient `p̂_S = E ∏_{i ∈ S} (-1)^{X_i}` in the symmetric model with error rates `e`. -/
 88def walsh (π : ℝ) (e : Fin n → ℝ) (S : Finset (Fin n)) : ℝ :=
 89  ∑ x, prob π e e x * ∏ i ∈ S, sgn (x i)
 90
 91/-- **Proposition 1** (for any number of raters). `p̂_S = τ^{[|S| odd]} ∏_{i∈S} d_i` with
 92`τ = 1 - 2π`, `d_i = 1 - 2 e_i`. -/
 93theorem prop1_walsh (π : ℝ) (e : Fin n → ℝ) (S : Finset (Fin n)) :
 94    walsh π e S = (if Odd S.card then 1 - 2 * π else 1) * ∏ i ∈ S, (1 - 2 * e i) := by
 95  rw [walsh, expect_prod_on π e e S (fun _ b => sgn b)]
 96  have h1 : ∀ i, (1 - e i) * sgn true + e i * sgn false = -1 * (1 - 2 * e i) := by
 97    intro i; rw [sgn_true, sgn_false]; ring
 98  have h2 : ∀ i, e i * sgn true + (1 - e i) * sgn false = 1 - 2 * e i := by
 99    intro i; rw [sgn_true, sgn_false]; ring
100  simp only [h1, h2, prod_mul_distrib, prod_const]
101  rcases Nat.even_or_odd S.card with h | h
102  · rw [h.neg_one_pow, if_neg (Nat.not_odd_iff_even.2 h)]; ring
103  · rw [h.neg_one_pow, if_pos h]; ring
104
105/-- Agreement rate `A_ij = Pr[X_i = X_j]`. -/
106def agree (π : ℝ) (e : Fin n → ℝ) (i j : Fin n) : ℝ :=
107  ∑ x, prob π e e x * (if x i = x j then 1 else 0)
108
109/-- Pair polarity `κ_ij = 2 A_ij - 1`. -/
110def kappa (π : ℝ) (e : Fin n → ℝ) (i j : Fin n) : ℝ := 2 * agree π e i j - 1
111
112/-- **Proposition 1, pair polarity.** `κ_ij = (1 - 2 e_i)(1 - 2 e_j)` for `i ≠ j`. -/
113theorem kappa_eq (π : ℝ) (e : Fin n → ℝ) {i j : Fin n} (hij : i ≠ j) :
114    kappa π e i j = (1 - 2 * e i) * (1 - 2 * e j) := by
115  have hw : walsh π e {i, j} = (1 - 2 * e i) * (1 - 2 * e j) := by
116    rw [prop1_walsh, card_pair_eq_two_iff.2 hij, prod_pair hij,
117      if_neg (Nat.not_odd_iff_even.2 even_two), one_mul]
118  have hx : ∀ x : Fin n → Bool, prob π e e x * ∏ k ∈ ({i, j} : Finset (Fin n)), sgn (x k) =
119      2 * (prob π e e x * (if x i = x j then 1 else 0)) - prob π e e x := by
120    intro x
121    rw [prod_pair hij]
122    cases x i <;> cases x j <;> simp <;> ring
123  rw [← hw, walsh, Finset.sum_congr rfl fun x _ => hx x, sum_sub_distrib, ← Finset.mul_sum,
124    mass_one, kappa, agree]
125
126/-- The agreement rate itself: `A_ij = 1 - e_i - e_j + 2 e_i e_j`. -/
127theorem agree_eq (π : ℝ) (e : Fin n → ℝ) {i j : Fin n} (hij : i ≠ j) :
128    agree π e i j = 1 - e i - e j + 2 * e i * e j := by
129  have h := kappa_eq π e hij
130  rw [kappa] at h
131  linarith
132
133/-- **Proposition 2** (the three-rater identity, for any three distinct raters among `n`).
134If `κ_jk ≠ 0` then `(1 - 2 e_i)^2 = κ_ij κ_ik / κ_jk`; with `i` the gold grader `G₁`, `j` the
135second grader `G₂` and `k` the controller `C`, this is `(1 - 2η₁)² = κ₁₂ κ₁C / κ₂C`. -/
136theorem prop2_three_rater (π : ℝ) (e : Fin n → ℝ) {i j k : Fin n} (hij : i ≠ j) (hik : i ≠ k)
137    (hjk : j ≠ k) (h : kappa π e j k ≠ 0) :
138    (1 - 2 * e i) ^ 2 = kappa π e i j * kappa π e i k / kappa π e j k := by
139  rw [kappa_eq π e hij, kappa_eq π e hik]
140  rw [kappa_eq π e hjk] at h ⊢
141  rw [eq_div_iff h]; ring
142
143/-- **Proposition 2, gold grader.** Three raters: gold grader `G₁` (index 0, error `η₁ = e 0`),
144second grader `G₂` (index 1) and controller `C` (index 2). If `κ₂C ≠ 0` then
145`(1 - 2η₁)² = κ₁₂ κ₁C / κ₂C`. -/
146theorem prop2_gold (π : ℝ) (e : Fin 3 → ℝ) (h : kappa π e 1 2 ≠ 0) :
147    (1 - 2 * e 0) ^ 2 = kappa π e 0 1 * kappa π e 0 2 / kappa π e 1 2 :=
148  prop2_three_rater π e (by decide) (by decide) (by decide) h
149
150/-- **Proposition 3** (three raters). `M₁κ₂₃ = M₂κ₁₃ = M₃κ₁₂ = P`, with the marginal biases
151`M_i = p̂_{i}` and the parity bias `P = p̂_{123}`. -/
152theorem prop3_constraints (π : ℝ) (e : Fin 3 → ℝ) :
153    walsh π e {0} * kappa π e 1 2 = walsh π e univ ∧
154    walsh π e {1} * kappa π e 0 2 = walsh π e univ ∧
155    walsh π e {2} * kappa π e 0 1 = walsh π e univ := by
156  have hodd1 : Odd (1 : ℕ) := odd_one
157  have hodd3 : Odd (3 : ℕ) := ⟨1, rfl⟩
158  simp only [prop1_walsh, card_singleton, prod_singleton, card_univ, Fintype.card_fin,
159    Fin.prod_univ_three, if_pos hodd1, if_pos hodd3]
160  rw [kappa_eq π e (by decide : (1 : Fin 3) ≠ 2), kappa_eq π e (by decide : (0 : Fin 3) ≠ 2),
161    kappa_eq π e (by decide : (0 : Fin 3) ≠ 1)]
162  refine ⟨by ring, by ring, by ring⟩
163
164/-! ## Central moments in the asymmetric model (§7–§8) -/
165
166/-- Mean report `p_i = Pr[X_i = 1]`. -/
167def mean (π : ℝ) (α β : Fin n → ℝ) (i : Fin n) : ℝ := ∑ x, prob π α β x * ind (x i)
168
169theorem mean_eq (π : ℝ) (α β : Fin n → ℝ) (i : Fin n) :
170    mean π α β i = π * (1 - α i) + (1 - π) * β i := by
171  have h := expect_prod_on π α β {i} (fun _ b => ind b)
172  simp only [prod_singleton, ind_true, ind_false, mul_one, mul_zero, add_zero] at h
173  rw [mean, h]
174
175/-- Central moment `k_S = E ∏_{i ∈ S} (X_i - p_i)`. -/
176def cmom (π : ℝ) (α β : Fin n → ℝ) (S : Finset (Fin n)) : ℝ :=
177  ∑ x, prob π α β x * ∏ i ∈ S, (ind (x i) - mean π α β i)
178
179/-- `μ_k = E (Y - π)^k = π (1 - π)^k + (1 - π)(-π)^k`. -/
180def mu (π : ℝ) (k : ℕ) : ℝ := π * (1 - π) ^ k + (1 - π) * (-π) ^ k
181
182theorem mu_two (π : ℝ) : mu π 2 = π * (1 - π) := by unfold mu; ring
183theorem mu_three (π : ℝ) : mu π 3 = π * (1 - π) * (1 - 2 * π) := by unfold mu; ring
184theorem mu_four (π : ℝ) : mu π 4 = π * (1 - π) * (1 - 3 * (π * (1 - π))) := by unfold mu; ring
185
186/-- `δ_i = 1 - α_i - β_i`, rater `i`'s hit rate minus its false-alarm rate. -/
187def del (α β : Fin n → ℝ) (i : Fin n) : ℝ := 1 - α i - β i
188
189/-- **Proposition 5** (for any number of raters and any set `S` of them).
190`k_S = μ_{|S|} ∏_{i∈S} δ_i` with `δ_i = 1 - α_i - β_i`; `μ₂ = u`, `μ₃ = u(1 - 2π)`,
191`μ₄ = u(1 - 3u)`, `u = π(1 - π)` are `mu_two`, `mu_three`, `mu_four`. -/
192theorem prop5 (π : ℝ) (α β : Fin n → ℝ) (S : Finset (Fin n)) :
193    cmom π α β S = mu π S.card * ∏ i ∈ S, del α β i := by
194  rw [cmom, expect_prod_on π α β S (fun i b => ind b - mean π α β i)]
195  have h1 : ∀ i, (1 - α i) * (ind true - mean π α β i) + α i * (ind false - mean π α β i) =
196      (1 - π) * del α β i := by
197    intro i; rw [mean_eq, ind_true, ind_false, del]; ring
198  have h2 : ∀ i, β i * (ind true - mean π α β i) + (1 - β i) * (ind false - mean π α β i) =
199      -π * del α β i := by
200    intro i; rw [mean_eq, ind_true, ind_false, del]; ring
201  simp only [h1, h2, prod_mul_distrib, prod_const, mu]
202  ring
203
204theorem cmom_pair (π : ℝ) (α β : Fin n → ℝ) {i j : Fin n} (hij : i ≠ j) :
205    cmom π α β {i, j} = π * (1 - π) * del α β i * del α β j := by
206  rw [prop5, card_pair_eq_two_iff.2 hij, prod_pair hij, mu_two]; ring
207
208theorem cmom_triple (π : ℝ) (α β : Fin n → ℝ) {i j k : Fin n} (hij : i ≠ j) (hik : i ≠ k)
209    (hjk : j ≠ k) :
210    cmom π α β {i, j, k} =
211      π * (1 - π) * (1 - 2 * π) * del α β i * del α β j * del α β k := by
212  have hi : i ∉ ({j, k} : Finset (Fin n)) := by simp [hij, hik]
213  rw [prop5, card_insert_of_notMem hi, card_pair_eq_two_iff.2 hjk, prod_insert hi,
214    prod_pair hjk, mu_three]; ring
215
216/-! ### Four raters (§8): the six equalities -/
217
218/-- `c_ij = k_{ij}` for four raters (indices `0..3` are raters `1..4`, rater 4 the controller). -/
219def c4 (π : ℝ) (α β : Fin 4 → ℝ) (i j : Fin 4) : ℝ := cmom π α β {i, j}
220
221/-- `k_{ijl}` for four raters. -/
222def k4 (π : ℝ) (α β : Fin 4 → ℝ) (i j l : Fin 4) : ℝ := cmom π α β {i, j, l}
223
224/-- `k_{1234}`. -/
225def k1234 (π : ℝ) (α β : Fin 4 → ℝ) : ℝ := cmom π α β univ
226
227theorem k1234_eq (π : ℝ) (α β : Fin 4 → ℝ) :
228    k1234 π α β = π * (1 - π) * (1 - 3 * (π * (1 - π))) *
229      (del α β 0 * del α β 1 * del α β 2 * del α β 3) := by
230  rw [k1234, prop5, card_univ, Fintype.card_fin, Fin.prod_univ_four, mu_four]
231
232/-- **The six equalities (§8)**, which hold identically on the four-rater model:
233tetrads `c₁₂c₃₄ = c₁₃c₂₄ = c₁₄c₂₃`; ratios `k₁₂₃c₁₄ = k₁₂₄c₁₃`, `k₁₂₃c₁₄ = k₁₃₄c₁₂`,
234`k₁₂₃c₂₄ = k₂₃₄c₁₂`; and `(k₁₂₃₄ - c₁₂c₃₄)c₁₂ = k₁₂₃k₁₂₄`. -/
235theorem six_equalities (π : ℝ) (α β : Fin 4 → ℝ) :
236    c4 π α β 0 1 * c4 π α β 2 3 = c4 π α β 0 2 * c4 π α β 1 3 ∧
237    c4 π α β 0 2 * c4 π α β 1 3 = c4 π α β 0 3 * c4 π α β 1 2 ∧
238    k4 π α β 0 1 2 * c4 π α β 0 3 = k4 π α β 0 1 3 * c4 π α β 0 2 ∧
239    k4 π α β 0 1 2 * c4 π α β 0 3 = k4 π α β 0 2 3 * c4 π α β 0 1 ∧
240    k4 π α β 0 1 2 * c4 π α β 1 3 = k4 π α β 1 2 3 * c4 π α β 0 1 ∧
241    (k1234 π α β - c4 π α β 0 1 * c4 π α β 2 3) * c4 π α β 0 1 =
242      k4 π α β 0 1 2 * k4 π α β 0 1 3 := by
243  have e01 := cmom_pair π α β (by decide : (0 : Fin 4) ≠ 1)
244  have e02 := cmom_pair π α β (by decide : (0 : Fin 4) ≠ 2)
245  have e03 := cmom_pair π α β (by decide : (0 : Fin 4) ≠ 3)
246  have e12 := cmom_pair π α β (by decide : (1 : Fin 4) ≠ 2)
247  have e13 := cmom_pair π α β (by decide : (1 : Fin 4) ≠ 3)
248  have e23 := cmom_pair π α β (by decide : (2 : Fin 4) ≠ 3)
249  have t012 := cmom_triple π α β (by decide : (0 : Fin 4) ≠ 1) (by decide : (0 : Fin 4) ≠ 2)
250    (by decide : (1 : Fin 4) ≠ 2)
251  have t013 := cmom_triple π α β (by decide : (0 : Fin 4) ≠ 1) (by decide : (0 : Fin 4) ≠ 3)
252    (by decide : (1 : Fin 4) ≠ 3)
253  have t023 := cmom_triple π α β (by decide : (0 : Fin 4) ≠ 2) (by decide : (0 : Fin 4) ≠ 3)
254    (by decide : (2 : Fin 4) ≠ 3)
255  have t123 := cmom_triple π α β (by decide : (1 : Fin 4) ≠ 2) (by decide : (1 : Fin 4) ≠ 3)
256    (by decide : (2 : Fin 4) ≠ 3)
257  have q := k1234_eq π α β
258  simp only [c4, k4, e01, e02, e03, e12, e13, e23, t012, t013, t023, t123, q]
259  refine ⟨by ring, by ring, by ring, by ring, by ring, by ring⟩
260
261/-! ## Asymmetric three-rater model (§7, Proposition 4) -/
262
263/-- Raw moment `Pr[X_i = 1 for every i ∈ T]`. -/
264def pm (π : ℝ) (α β : Fin n → ℝ) (T : Finset (Fin n)) : ℝ :=
265  ∑ x, prob π α β x * ∏ i ∈ T, ind (x i)
266
267theorem pm_eq (π : ℝ) (α β : Fin n → ℝ) (T : Finset (Fin n)) :
268    pm π α β T = π * ∏ i ∈ T, (1 - α i) + (1 - π) * ∏ i ∈ T, β i := by
269  rw [pm, expect_prod_on π α β T (fun _ b => ind b)]
270  simp only [ind_true, ind_false, mul_one, mul_zero, add_zero]
271
272/-- `p_i` for three raters. -/
273def p3 (π : ℝ) (α β : Fin 3 → ℝ) (i : Fin 3) : ℝ := pm π α β {i}
274
275/-- `c_ij = p_ij - p_i p_j` for three raters (raw form, as §7 defines it). -/
276def cov3 (π : ℝ) (α β : Fin 3 → ℝ) (i j : Fin 3) : ℝ :=
277  pm π α β {i, j} - pm π α β {i} * pm π α β {j}
278
279/-- `k₃ = p₁₂₃ - Σ_i p_i p_jk + 2 p₁p₂p₃` (raw form, as §7 defines it). -/
280def k3 (π : ℝ) (α β : Fin 3 → ℝ) : ℝ :=
281  pm π α β univ -
282    (pm π α β {0} * pm π α β {1, 2} + pm π α β {1} * pm π α β {0, 2} +
283      pm π α β {2} * pm π α β {0, 1}) +
284    2 * pm π α β {0} * pm π α β {1} * pm π α β {2}
285
286theorem p3_eq (π : ℝ) (α β : Fin 3 → ℝ) (i : Fin 3) :
287    p3 π α β i = π * (1 - α i) + (1 - π) * β i := by
288  rw [p3, pm_eq, prod_singleton, prod_singleton]
289
290/-- **Proposition 4, covariances.** `c_ij = u δ_i δ_j`. -/
291theorem prop4_cov (π : ℝ) (α β : Fin 3 → ℝ) {i j : Fin 3} (hij : i ≠ j) :
292    cov3 π α β i j = π * (1 - π) * del α β i * del α β j := by
293  simp only [cov3, pm_eq, prod_pair hij, prod_singleton, del]
294  ring
295
296/-- **Proposition 4, third moment.** `k₃ = u (1 - 2π) δ₁δ₂δ₃`. -/
297theorem prop4_k3 (π : ℝ) (α β : Fin 3 → ℝ) :
298    k3 π α β = π * (1 - π) * (1 - 2 * π) *
299      (del α β 0 * del α β 1 * del α β 2) := by
300  simp only [k3, pm_eq, prod_singleton, Fin.prod_univ_three, del,
301    prod_pair (by decide : (1 : Fin 3) ≠ 2), prod_pair (by decide : (0 : Fin 3) ≠ 2),
302    prod_pair (by decide : (0 : Fin 3) ≠ 1)]
303  ring
304
305/-- **Proposition 4, the square.** `R² := k₃² + 4c₁₂c₁₃c₂₃ = u²δ₁²δ₂²δ₃²`. -/
306theorem prop4_R2 (π : ℝ) (α β : Fin 3 → ℝ) :
307    k3 π α β ^ 2 + 4 * cov3 π α β 0 1 * cov3 π α β 0 2 * cov3 π α β 1 2 =
308      (π * (1 - π) * (del α β 0 * del α β 1 * del α β 2)) ^ 2 := by
309  rw [prop4_k3, prop4_cov π α β (by decide : (0 : Fin 3) ≠ 1),
310    prop4_cov π α β (by decide : (0 : Fin 3) ≠ 2), prop4_cov π α β (by decide : (1 : Fin 3) ≠ 2)]
311  ring
312
313/-- Algebraic core of the per-rater solution. -/
314theorem solve_rater {π si ti di dj dk R k c p : ℝ} (hπ0 : 0 < π) (hπ1 : π < 1)
315    (hdj : dj ≠ 0) (hdk : dk ≠ 0) (hdi : di = si - ti)
316    (hR : R = π * (1 - π) * (di * dj * dk))
317    (hk : k = π * (1 - π) * (1 - 2 * π) * (di * dj * dk))
318    (hc : c = π * (1 - π) * dj * dk) (hp : p = π * si + (1 - π) * ti) :
319    si = p + (R + k) / (2 * c) ∧ ti = p - (R - k) / (2 * c) := by
320  have hπ : π ≠ 0 := hπ0.ne'
321  have hπ' : 1 - π ≠ 0 := by linarith
322  subst hR hk hc hp hdi
323  constructor <;> field_simp <;> ring
324
325/-- **Proposition 4, the solution.** Under `0 < π < 1` and `δ₁δ₂δ₃ > 0`, with
326`R = √(k₃² + 4c₁₂c₁₃c₂₃)` computed from the table:
327`π = (R - k₃)/(2R)`, and for `{i, j, k} = {1, 2, 3}`, `s_i = 1 - α_i = p_i + (R + k₃)/(2c_jk)` and
328`t_i = β_i = p_i - (R - k₃)/(2c_jk)`. -/
329theorem prop4_solution (π : ℝ) (α β : Fin 3 → ℝ) (hπ0 : 0 < π) (hπ1 : π < 1)
330    (hδ : 0 < del α β 0 * del α β 1 * del α β 2) (R : ℝ)
331    (hR : R = Real.sqrt (k3 π α β ^ 2 + 4 * cov3 π α β 0 1 * cov3 π α β 0 2 * cov3 π α β 1 2)) :
332    π = (R - k3 π α β) / (2 * R) ∧
333    1 - α 0 = p3 π α β 0 + (R + k3 π α β) / (2 * cov3 π α β 1 2) ∧
334    β 0 = p3 π α β 0 - (R - k3 π α β) / (2 * cov3 π α β 1 2) ∧
335    1 - α 1 = p3 π α β 1 + (R + k3 π α β) / (2 * cov3 π α β 0 2) ∧
336    β 1 = p3 π α β 1 - (R - k3 π α β) / (2 * cov3 π α β 0 2) ∧
337    1 - α 2 = p3 π α β 2 + (R + k3 π α β) / (2 * cov3 π α β 0 1) ∧
338    β 2 = p3 π α β 2 - (R - k3 π α β) / (2 * cov3 π α β 0 1) := by
339  have hu : 0 < π * (1 - π) := mul_pos hπ0 (by linarith)
340  have hR' : R = π * (1 - π) * (del α β 0 * del α β 1 * del α β 2) := by
341    rw [hR, prop4_R2, Real.sqrt_sq (mul_pos hu hδ).le]
342  have hd0 : del α β 0 ≠ 0 := fun h => by rw [h] at hδ; simp at hδ
343  have hd1 : del α β 1 ≠ 0 := fun h => by rw [h] at hδ; simp at hδ
344  have hd2 : del α β 2 ≠ 0 := fun h => by rw [h] at hδ; simp at hδ
345  have hk := prop4_k3 π α β
346  have c01 := prop4_cov π α β (by decide : (0 : Fin 3) ≠ 1)
347  have c02 := prop4_cov π α β (by decide : (0 : Fin 3) ≠ 2)
348  have c12 := prop4_cov π α β (by decide : (1 : Fin 3) ≠ 2)
349  obtain ⟨s0, t0⟩ := solve_rater (si := 1 - α 0) (ti := β 0) (di := del α β 0)
350    (dj := del α β 1) (dk := del α β 2) (R := R) (k := k3 π α β) (c := cov3 π α β 1 2)
351    (p := p3 π α β 0) hπ0 hπ1 hd1 hd2 rfl hR' hk c12 (p3_eq π α β 0)
352  obtain ⟨s1, t1⟩ := solve_rater (si := 1 - α 1) (ti := β 1) (di := del α β 1)
353    (dj := del α β 0) (dk := del α β 2) (R := R) (k := k3 π α β) (c := cov3 π α β 0 2)
354    (p := p3 π α β 1) hπ0 hπ1 hd0 hd2 rfl (by rw [hR']; ring) (by rw [hk]; ring)
355    c02 (p3_eq π α β 1)
356  obtain ⟨s2, t2⟩ := solve_rater (si := 1 - α 2) (ti := β 2) (di := del α β 2)
357    (dj := del α β 0) (dk := del α β 1) (R := R) (k := k3 π α β) (c := cov3 π α β 0 1)
358    (p := p3 π α β 2) hπ0 hπ1 hd0 hd1 rfl (by rw [hR']; ring) (by rw [hk]; ring)
359    c01 (p3_eq π α β 2)
360  refine ⟨?_, s0, t0, s1, t1, s2, t2⟩
361  have hπ : π ≠ 0 := hπ0.ne'
362  have hπ' : 1 - π ≠ 0 := by linarith
363  rw [hR', hk]
364  field_simp
365  ring
366
367/-! ## The copying grader (§8) -/
368
369/-- Indicator that two reports agree. -/
370def same (a b : Bool) : ℝ := if a = b then 1 else 0
371
372@[simp] lemma same_tt : same true true = 1 := rfl
373@[simp] lemma same_tf : same true false = 0 := rfl
374@[simp] lemma same_ft : same false true = 0 := rfl
375@[simp] lemma same_ff : same false false = 1 := rfl
376
377/-- Four raters in which rater 2 (index 1) copies rater 1 (index 0) with probability `ρ` and grades
378independently (rates `α 1`, `β 1`) otherwise; raters 3 and 4 independent given `Y`. -/
379def probCopy (π ρ : ℝ) (α β : Fin 4 → ℝ) (x0 x1 x2 x3 : Bool) : ℝ :=
380  π * (bern (1 - α 0) x0 * (ρ * same x1 x0 + (1 - ρ) * bern (1 - α 1) x1) *
381      bern (1 - α 2) x2 * bern (1 - α 3) x3) +
382  (1 - π) * (bern (β 0) x0 * (ρ * same x1 x0 + (1 - ρ) * bern (β 1) x1) *
383      bern (β 2) x2 * bern (β 3) x3)
384
385/-- Expectation under the copying model. -/
386def ECopy (π ρ : ℝ) (α β : Fin 4 → ℝ) (F : Bool → Bool → Bool → Bool → ℝ) : ℝ :=
387  ∑ x0 : Bool, ∑ x1 : Bool, ∑ x2 : Bool, ∑ x3 : Bool, probCopy π ρ α β x0 x1 x2 x3 * F x0 x1 x2 x3
388
389/-- Report of rater `i`. -/
390def rep (i : Fin 4) (x0 x1 x2 x3 : Bool) : Bool := ![x0, x1, x2, x3] i
391
392@[simp] lemma rep_0 (x0 x1 x2 x3 : Bool) : rep 0 x0 x1 x2 x3 = x0 := rfl
393@[simp] lemma rep_1 (x0 x1 x2 x3 : Bool) : rep 1 x0 x1 x2 x3 = x1 := rfl
394@[simp] lemma rep_2 (x0 x1 x2 x3 : Bool) : rep 2 x0 x1 x2 x3 = x2 := rfl
395@[simp] lemma rep_3 (x0 x1 x2 x3 : Bool) : rep 3 x0 x1 x2 x3 = x3 := rfl
396
397/-- Mean report of rater `i` under the copying model. -/
398def meanCopy (π ρ : ℝ) (α β : Fin 4 → ℝ) (i : Fin 4) : ℝ :=
399  ECopy π ρ α β fun x0 x1 x2 x3 => ind (rep i x0 x1 x2 x3)
400
401/-- Covariance `c_ij` under the copying model. -/
402def covCopy (π ρ : ℝ) (α β : Fin 4 → ℝ) (i j : Fin 4) : ℝ :=
403  ECopy π ρ α β fun x0 x1 x2 x3 =>
404    (ind (rep i x0 x1 x2 x3) - meanCopy π ρ α β i) * (ind (rep j x0 x1 x2 x3) - meanCopy π ρ α β j)
405
406section Copy
407
408variable (π ρ : ℝ) (α β : Fin 4 → ℝ)
409
410theorem meanCopy_0 : meanCopy π ρ α β 0 = π * (1 - α 0) + (1 - π) * β 0 := by
411  simp only [meanCopy, ECopy, probCopy, Fintype.sum_bool, rep_0, bern_true, bern_false, ind_true,
412    ind_false, same_tt, same_tf, same_ft, same_ff]
413  ring
414
415theorem meanCopy_1 : meanCopy π ρ α β 1 =
416    π * (ρ * (1 - α 0) + (1 - ρ) * (1 - α 1)) + (1 - π) * (ρ * β 0 + (1 - ρ) * β 1) := by
417  simp only [meanCopy, ECopy, probCopy, Fintype.sum_bool, rep_1, bern_true, bern_false, ind_true,
418    ind_false, same_tt, same_tf, same_ft, same_ff]
419  ring
420
421theorem meanCopy_2 : meanCopy π ρ α β 2 = π * (1 - α 2) + (1 - π) * β 2 := by
422  simp only [meanCopy, ECopy, probCopy, Fintype.sum_bool, rep_2, bern_true, bern_false, ind_true,
423    ind_false, same_tt, same_tf, same_ft, same_ff]
424  ring
425
426theorem meanCopy_3 : meanCopy π ρ α β 3 = π * (1 - α 3) + (1 - π) * β 3 := by
427  simp only [meanCopy, ECopy, probCopy, Fintype.sum_bool, rep_3, bern_true, bern_false, ind_true,
428    ind_false, same_tt, same_tf, same_ft, same_ff]
429  ring
430
431/-- `V₁ = π α₁(1 - α₁) + (1 - π) β₁(1 - β₁)`, the conditional variance of rater 1. -/
432def V1 : ℝ := π * α 0 * (1 - α 0) + (1 - π) * β 0 * (1 - β 0)
433
434/-- `δ̃₂ = ρ δ₁ + (1 - ρ) δ₂`. -/
435def dTilde : ℝ := ρ * del α β 0 + (1 - ρ) * del α β 1
436
437/-- **Copying grader, covariances.** `c₁₂ = u δ₁ δ̃₂ + ρ V₁`; the other five covariances keep
438product form, with `δ̃₂` for rater 2. -/
439theorem massCopy : ECopy π ρ α β (fun _ _ _ _ => 1) = 1 := by
440  simp only [ECopy, probCopy, Fintype.sum_bool, bern_true, bern_false, same_tt, same_tf, same_ft,
441    same_ff]
442  ring
443
444/-- `c_ij = E[X_i X_j] - p_i p_j` (the distribution sums to one). -/
445theorem covCopy_raw (i j : Fin 4) :
446    covCopy π ρ α β i j =
447      ECopy π ρ α β (fun x0 x1 x2 x3 => ind (rep i x0 x1 x2 x3) * ind (rep j x0 x1 x2 x3)) -
448        meanCopy π ρ α β i * meanCopy π ρ α β j := by
449  have hm := massCopy π ρ α β
450  have hi : meanCopy π ρ α β i = ECopy π ρ α β fun x0 x1 x2 x3 => ind (rep i x0 x1 x2 x3) := rfl
451  have hj : meanCopy π ρ α β j = ECopy π ρ α β fun x0 x1 x2 x3 => ind (rep j x0 x1 x2 x3) := rfl
452  simp only [covCopy, ECopy, Fintype.sum_bool] at hm hi hj ⊢
453  linear_combination (meanCopy π ρ α β j) * hi + (meanCopy π ρ α β i) * hj +
454    (meanCopy π ρ α β i * meanCopy π ρ α β j) * hm
455
456/-- **Copying grader, covariances.** `c₁₂ = u δ₁ δ̃₂ + ρ V₁`; the other five covariances keep
457product form, with `δ̃₂` for rater 2. -/
458theorem covCopy_eq :
459    covCopy π ρ α β 0 1 = π * (1 - π) * del α β 0 * dTilde ρ α β + ρ * V1 π α β ∧
460    covCopy π ρ α β 0 2 = π * (1 - π) * del α β 0 * del α β 2 ∧
461    covCopy π ρ α β 0 3 = π * (1 - π) * del α β 0 * del α β 3 ∧
462    covCopy π ρ α β 1 2 = π * (1 - π) * dTilde ρ α β * del α β 2 ∧
463    covCopy π ρ α β 1 3 = π * (1 - π) * dTilde ρ α β * del α β 3 ∧
464    covCopy π ρ α β 2 3 = π * (1 - π) * del α β 2 * del α β 3 := by
465  refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ <;>
466  ( rw [covCopy_raw]
467    simp only [meanCopy_0, meanCopy_1, meanCopy_2, meanCopy_3, ECopy, probCopy,
468      Fintype.sum_bool, rep_0, rep_1, rep_2, rep_3, bern_true, bern_false, ind_true, ind_false,
469      same_tt, same_tf, same_ft, same_ff, V1, dTilde, del, mul_zero, mul_one, add_zero, zero_add]
470    ring )
471
472/-- **Copying grader, residual (§8).** `c₁₂c₃₄ - c₁₃c₂₄ = ρ V₁ c₃₄` and `c₁₃c₂₄ = c₁₄c₂₃`. -/
473theorem copy_grader_residual :
474    covCopy π ρ α β 0 1 * covCopy π ρ α β 2 3 - covCopy π ρ α β 0 2 * covCopy π ρ α β 1 3 =
475      ρ * V1 π α β * covCopy π ρ α β 2 3 ∧
476    covCopy π ρ α β 0 2 * covCopy π ρ α β 1 3 = covCopy π ρ α β 0 3 * covCopy π ρ α β 1 2 := by
477  obtain ⟨h01, h02, h03, h12, h13, h23⟩ := covCopy_eq π ρ α β
478  rw [h01, h02, h03, h12, h13, h23]
479  constructor <;> ring
480
481/-- **Copying grader, detection.** The residual is non-zero whenever `ρ ≠ 0`, `V₁ ≠ 0`,
482`0 < π < 1` and `δ₃δ₄ ≠ 0`; in particular at the balanced prior `π = 1/2`. -/
483theorem copy_grader_detected (hρ : ρ ≠ 0) (hV : V1 π α β ≠ 0) (hπ0 : 0 < π) (hπ1 : π < 1)
484    (hd : del α β 2 * del α β 3 ≠ 0) :
485    covCopy π ρ α β 0 1 * covCopy π ρ α β 2 3 - covCopy π ρ α β 0 2 * covCopy π ρ α β 1 3 ≠ 0 := by
486  rw [(copy_grader_residual π ρ α β).1, (covCopy_eq π ρ α β).2.2.2.2.2]
487  have hπ : π ≠ 0 := hπ0.ne'
488  have hπ' : 1 - π ≠ 0 := by linarith
489  have hd2 : del α β 2 ≠ 0 := left_ne_zero_of_mul hd
490  have hd3 : del α β 3 ≠ 0 := right_ne_zero_of_mul hd
491  exact mul_ne_zero (mul_ne_zero hρ hV) (mul_ne_zero (mul_ne_zero (mul_ne_zero hπ hπ') hd2) hd3)
492
493end Copy
494
495/-! ## Asymmetric gold-noise inversion (§7) -/
496
497section AsymInversion
498
499variable (π a1 a0 f1 f0 e0 e1 : ℝ)
500
501/-- Latent cell `(y, c, j, f)`: class `y` (`Pr[Y = 1] = π`); controller right `c`, with accuracy
502`a1` on class 1 and `a0` on class 0; judge right `j`, wrong at rate `e0` when the controller is
503right and `e1` when it is wrong (depending on correctness only); gold flipped `f`, at rate `f1` on
504class 1 and `f0` on class 0, independent of `(c, j)` given `y`. -/
505def acell (y c j f : Bool) : ℝ :=
506  (if y then π else 1 - π) *
507    (if c then (if y then a1 else a0) * (if j then 1 - e0 else e0)
508      else (1 - (if y then a1 else a0)) * (if j then 1 - e1 else e1)) *
509    (if f then (if y then f1 else f0) else 1 - (if y then f1 else f0))
510
511/-- Measured controller accuracy `a' = Pr[C ⊕ F]`. -/
512def asymAcc : ℝ :=
513  ∑ y : Bool, ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
514    if (c ^^ f) = true then acell π a1 a0 f1 f0 e0 e1 y c j f else 0
515
516/-- Measured judge error `ε' = Pr[¬(J ⊕ F)]`. -/
517def asymErr : ℝ :=
518  ∑ y : Bool, ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
519    if (j ^^ f) = false then acell π a1 a0 f1 f0 e0 e1 y c j f else 0
520
521/-- Measured wrong–wrong rate `w = Pr[¬(C ⊕ F) ∧ ¬(J ⊕ F)]`. -/
522def asymW : ℝ :=
523  ∑ y : Bool, ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
524    if (c ^^ f) = false ∧ (j ^^ f) = false then acell π a1 a0 f1 f0 e0 e1 y c j f else 0
525
526/-- True and measured agreement rates `Pr[C = J]`, `Pr[C ⊕ F = J ⊕ F]`. -/
527def asymAgree : ℝ :=
528  ∑ y : Bool, ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
529    if c = j then acell π a1 a0 f1 f0 e0 e1 y c j f else 0
530
531def asymAgreeMeas : ℝ :=
532  ∑ y : Bool, ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
533    if (c ^^ f) = (j ^^ f) then acell π a1 a0 f1 f0 e0 e1 y c j f else 0
534
535/-- **§7, measured quantities.** The note's expressions for `a'`, `ε'` and `w`, derived from the
536sixteen latent cells; and the agreement rate is invariant whatever the law of `F`. -/
537theorem asym_measured :
538    asymAcc π a1 a0 f1 f0 e0 e1 =
539      π * (a1 * (1 - f1) + (1 - a1) * f1) + (1 - π) * (a0 * (1 - f0) + (1 - a0) * f0) ∧
540    asymErr π a1 a0 f1 f0 e0 e1 =
541      π * (f1 + (1 - 2 * f1) * (a1 * e0 + (1 - a1) * e1)) +
542        (1 - π) * (f0 + (1 - 2 * f0) * (a0 * e0 + (1 - a0) * e1)) ∧
543    asymW π a1 a0 f1 f0 e0 e1 =
544      π * ((1 - a1) * (1 - f1) * e1 + a1 * f1 * (1 - e0)) +
545        (1 - π) * ((1 - a0) * (1 - f0) * e1 + a0 * f0 * (1 - e0)) ∧
546    asymAgreeMeas π a1 a0 f1 f0 e0 e1 = asymAgree π a1 a0 f1 f0 e0 e1 := by
547  refine ⟨?_, ?_, ?_, ?_⟩ <;>
548    simp [asymAcc, asymErr, asymW, asymAgree, asymAgreeMeas, acell] <;> ring
549
550/-- `F = Σ π_y f_y`. -/
551def asymF : ℝ := π * f1 + (1 - π) * f0
552/-- `E_a = Σ π_y (1 - 2f_y) a_y`. -/
553def asymEa : ℝ := π * (1 - 2 * f1) * a1 + (1 - π) * (1 - 2 * f0) * a0
554/-- `E_b = Σ π_y (1 - 2f_y)(1 - a_y)`. -/
555def asymEb : ℝ := π * (1 - 2 * f1) * (1 - a1) + (1 - π) * (1 - 2 * f0) * (1 - a0)
556/-- `G_a = Σ π_y a_y f_y`. -/
557def asymGa : ℝ := π * a1 * f1 + (1 - π) * a0 * f0
558/-- `G_b = Σ π_y (1 - a_y)(1 - f_y)`. -/
559def asymGb : ℝ := π * (1 - a1) * (1 - f1) + (1 - π) * (1 - a0) * (1 - f0)
560
561/-- **§7, asymmetric inversion.** With `Δ = E_a G_b + E_b G_a ≠ 0`:
562`ε₁ = (E_a(w - G_a) + G_a(ε' - F))/Δ` and `ε₀ = (G_b(ε' - F) - E_b(w - G_a))/Δ`. -/
563theorem asym_inversion
564    (hΔ : asymEa π a1 a0 f1 f0 * asymGb π a1 a0 f1 f0 + asymEb π a1 a0 f1 f0 * asymGa π a1 a0 f1 f0
565      ≠ 0) :
566    e1 = (asymEa π a1 a0 f1 f0 * (asymW π a1 a0 f1 f0 e0 e1 - asymGa π a1 a0 f1 f0) +
567        asymGa π a1 a0 f1 f0 * (asymErr π a1 a0 f1 f0 e0 e1 - asymF π f1 f0)) /
568      (asymEa π a1 a0 f1 f0 * asymGb π a1 a0 f1 f0 + asymEb π a1 a0 f1 f0 * asymGa π a1 a0 f1 f0) ∧
569    e0 = (asymGb π a1 a0 f1 f0 * (asymErr π a1 a0 f1 f0 e0 e1 - asymF π f1 f0) -
570        asymEb π a1 a0 f1 f0 * (asymW π a1 a0 f1 f0 e0 e1 - asymGa π a1 a0 f1 f0)) /
571      (asymEa π a1 a0 f1 f0 * asymGb π a1 a0 f1 f0 + asymEb π a1 a0 f1 f0 * asymGa π a1 a0 f1 f0) := by
572  obtain ⟨-, hE, hW, -⟩ := asym_measured π a1 a0 f1 f0 e0 e1
573  constructor <;> rw [eq_div_iff hΔ, hE, hW] <;> simp only [asymEa, asymEb, asymGa, asymGb, asymF] <;>
574    ring
575
576/-- **§7, symmetric case.** With `a₁ = a₀ = a` and `f₁ = f₀ = η`, `Δ = a(1 - a)(1 - 2η)`. -/
577theorem asym_delta_symm (π a η : ℝ) :
578    asymEa π a a η η * asymGb π a a η η + asymEb π a a η η * asymGa π a a η η =
579      a * (1 - a) * (1 - 2 * η) := by
580  simp only [asymEa, asymEb, asymGa, asymGb]; ring
581
582end AsymInversion
583
584end FeedbackChannel.Raters