1import Mathlib.Tactic
2
3/-!
4# Measuring the judge's shared error: bookkeeping and gold noise
5
6Source: `paper/judge_shared_error_measurement.md`, §2 (Proposition 1) and §5 (Proposition 2 and its
7Corollary, the gold-noise identity).
8
9The gold-noise model is stated on the eight latent cells `(C, J, F)`: `C` the controller is right,
10`J` the judge is right, `F` the gold label is flipped. `Pr[C] = a`; the judge is wrong with
11probability `e0` when the controller is right and `e1` when it is wrong; `F` is independent of
12`(C, J)` with `Pr[F] = η`. Measured against the gold, controller-right is `C ⊕ F` and judge-right
13is `J ⊕ F`. Every measured quantity below is a sum over the eight cells, not a closed form.
14-/
15
16namespace FeedbackChannel.Judge
17
18open Finset
19
20/-! ## Proposition 1: the bookkeeping identity on a count table -/
21
22/-- **Proposition 1.** For every table `(R₀, R₁, W₀, W₁)` with `m > 0` items,
23`(R₀ + W₁)/m = â - ε̂ + 2(1 - â) ε̂₁` with `â = (R₀ + R₁)/m`, `ε̂ = (R₁ + W₁)/m` and
24`ε̂₁ = W₁/W`, `W = W₀ + W₁` (Lean's `x / 0 = 0` makes the `W = 0` case the note's convention). -/
25theorem prop1_bookkeeping (R0 R1 W0 W1 : ℕ) (hm : 0 < R0 + R1 + W0 + W1) :
26 ((R0 : ℝ) + W1) / (R0 + R1 + W0 + W1) =
27 ((R0 : ℝ) + R1) / (R0 + R1 + W0 + W1) - ((R1 : ℝ) + W1) / (R0 + R1 + W0 + W1) +
28 2 * (1 - ((R0 : ℝ) + R1) / (R0 + R1 + W0 + W1)) * ((W1 : ℝ) / (W0 + W1)) := by
29 have hm' : ((R0 : ℝ) + R1 + W0 + W1) ≠ 0 := by
30 have : (0 : ℝ) < R0 + R1 + W0 + W1 := by exact_mod_cast hm
31 exact this.ne'
32 rcases Nat.eq_zero_or_pos (W0 + W1) with hW | hW
33 · have h0 : W0 = 0 := by omega
34 have h1 : W1 = 0 := by omega
35 subst h0 h1
36 simp only [Nat.cast_zero, add_zero, div_zero, mul_zero] at hm' ⊢
37 field_simp
38 ring
39 · have hW' : ((W0 : ℝ) + W1) ≠ 0 := by
40 have : (0 : ℝ) < W0 + W1 := by exact_mod_cast hW
41 exact this.ne'
42 field_simp
43 ring
44
45/-! ## Proposition 2: the gold-noise model on latent cells -/
46
47/-- Probability of the latent cell `(c, j, f)`: controller right `c`, judge right `j`, gold
48flipped `f`. -/
49def cell (a e0 e1 η : ℝ) (c j f : Bool) : ℝ :=
50 (if c then a * (if j then 1 - e0 else e0) else (1 - a) * (if j then 1 - e1 else e1)) *
51 (if f then η else 1 - η)
52
53/-- True controller accuracy `Pr[C]`. -/
54def trueAcc (a e0 e1 η : ℝ) : ℝ :=
55 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if c = true then cell a e0 e1 η c j f else 0
56
57/-- True judge error `Pr[¬J]`. -/
58def trueErr (a e0 e1 η : ℝ) : ℝ :=
59 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if j = false then cell a e0 e1 η c j f else 0
60
61/-- Measured controller accuracy `a' = Pr[C ⊕ F]`. -/
62def measAcc (a e0 e1 η : ℝ) : ℝ :=
63 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if (c ^^ f) = true then cell a e0 e1 η c j f else 0
64
65/-- Measured judge error `ε' = Pr[¬(J ⊕ F)]`. -/
66def measErr (a e0 e1 η : ℝ) : ℝ :=
67 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if (j ^^ f) = false then cell a e0 e1 η c j f else 0
68
69/-- Measured wrong–wrong rate `W₁'/m = Pr[¬(C ⊕ F) ∧ ¬(J ⊕ F)]`. -/
70def measW1 (a e0 e1 η : ℝ) : ℝ :=
71 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool,
72 if (c ^^ f) = false ∧ (j ^^ f) = false then cell a e0 e1 η c j f else 0
73
74/-- True agreement rate `Pr[C = J]`. -/
75def trueAgree (a e0 e1 η : ℝ) : ℝ :=
76 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if c = j then cell a e0 e1 η c j f else 0
77
78/-- Measured agreement rate `Pr[C ⊕ F = J ⊕ F]`. -/
79def measAgree (a e0 e1 η : ℝ) : ℝ :=
80 ∑ c : Bool, ∑ j : Bool, ∑ f : Bool, if (c ^^ f) = (j ^^ f) then cell a e0 e1 η c j f else 0
81
82/-- The model's parameters mean what the note says: `Pr[C] = a` and
83`Pr[¬J] = ε = a e0 + (1 - a) e1`. -/
84theorem model_params (a e0 e1 η : ℝ) :
85 trueAcc a e0 e1 η = a ∧ trueErr a e0 e1 η = a * e0 + (1 - a) * e1 := by
86 constructor <;> simp [trueAcc, trueErr, cell] <;> ring
87
88/-- **Proposition 2.** `a' = η + a(1 - 2η)`, `ε' = η + ε(1 - 2η)` and
89`W₁'/m = (1 - a) ε₁ + η (a - ε)`, with `ε = a ε₀ + (1 - a) ε₁`; and the agreement rate is
90invariant under gold noise. -/
91theorem prop2 (a e0 e1 η : ℝ) :
92 measAcc a e0 e1 η = η + a * (1 - 2 * η) ∧
93 measErr a e0 e1 η = η + (a * e0 + (1 - a) * e1) * (1 - 2 * η) ∧
94 measW1 a e0 e1 η = (1 - a) * e1 + η * (a - (a * e0 + (1 - a) * e1)) ∧
95 measAgree a e0 e1 η = trueAgree a e0 e1 η := by
96 refine ⟨?_, ?_, ?_, ?_⟩ <;>
97 simp [measAcc, measErr, measW1, measAgree, trueAgree, cell] <;> ring
98
99/-- **Proposition 2, inversion.** For `η ≠ 1/2`: `a = (a' - η)/(1 - 2η)` and
100`ε = (ε' - η)/(1 - 2η)`; for `a ≠ 1`: `ε₁ = (ε₁'(1 - a') - η (a - ε))/(1 - a)`, where
101`ε₁'(1 - a') = W₁'/m`. -/
102theorem prop2_inverse (a e0 e1 η : ℝ) (hη : η ≠ 1 / 2) (ha : a ≠ 1) :
103 a = (measAcc a e0 e1 η - η) / (1 - 2 * η) ∧
104 a * e0 + (1 - a) * e1 = (measErr a e0 e1 η - η) / (1 - 2 * η) ∧
105 e1 = (measW1 a e0 e1 η - η * (a - (a * e0 + (1 - a) * e1))) / (1 - a) := by
106 obtain ⟨hA, hE, hW, -⟩ := prop2 a e0 e1 η
107 have h2 : (1 - 2 * η) ≠ 0 := by
108 intro h; apply hη; linarith
109 have h1 : (1 - a) ≠ 0 := sub_ne_zero.2 (Ne.symm ha)
110 refine ⟨?_, ?_, ?_⟩
111 · rw [hA, eq_div_iff h2]; ring
112 · rw [hE, eq_div_iff h2]; ring
113 · rw [hW, eq_div_iff h1]; ring
114
115/-- **Corollary (gold-noise identity).** If the judge is independent of the controller in the
116note's sense `ε₁ = ε` (`ε = a ε₀ + (1 - a) ε₁`), the measured excess
117`(ε₁' - ε')(1 - a') = W₁'/m - ε'(1 - a')` equals `η (1 - η)(2a - 1)(1 - 2ε)` exactly. -/
118theorem gold_noise_excess (a e0 e1 η : ℝ) (hind : e1 = a * e0 + (1 - a) * e1) :
119 measW1 a e0 e1 η - measErr a e0 e1 η * (1 - measAcc a e0 e1 η) =
120 η * (1 - η) * (2 * a - 1) * (1 - 2 * (a * e0 + (1 - a) * e1)) := by
121 obtain ⟨hA, hE, hW, -⟩ := prop2 a e0 e1 η
122 rw [hA, hE, hW]
123 linear_combination (1 - a) * hind
124
125/-- **Corollary, with the measured shared error `ε₁' = (W₁'/m)/(1 - a')`** (for `a' ≠ 1`):
126`(ε₁' - ε')(1 - a') = η (1 - η)(2a - 1)(1 - 2ε)` for an independent judge. -/
127theorem gold_noise_excess_rate (a e0 e1 η : ℝ) (hind : e1 = a * e0 + (1 - a) * e1)
128 (ha' : measAcc a e0 e1 η ≠ 1) :
129 (measW1 a e0 e1 η / (1 - measAcc a e0 e1 η) - measErr a e0 e1 η) *
130 (1 - measAcc a e0 e1 η) =
131 η * (1 - η) * (2 * a - 1) * (1 - 2 * (a * e0 + (1 - a) * e1)) := by
132 have h : (1 - measAcc a e0 e1 η) ≠ 0 := sub_ne_zero.2 (Ne.symm ha')
133 rw [sub_mul, div_mul_cancel₀ _ h]
134 exact gold_noise_excess a e0 e1 η hind
135
136/-- The excess is positive whenever `0 < η < 1`, `a > 1/2` and `ε < 1/2`. -/
137theorem gold_noise_excess_pos {a ε η : ℝ} (hη0 : 0 < η) (hη1 : η < 1) (ha : 1 / 2 < a)
138 (hε : ε < 1 / 2) : 0 < η * (1 - η) * (2 * a - 1) * (1 - 2 * ε) := by
139 apply mul_pos (mul_pos (mul_pos hη0 (by linarith)) (by linarith)) (by linarith)
140
141/-- **Fixture** (`η = 1/20`, `a = 4/5`, independent judge `ε₀ = ε₁ = 1/10`): `a' = 77/100`,
142`ε' = 7/50`, `ε₁' = 11/46`, and the excess `ε₁' - ε' = 57/575`. For the maximally shared judge
143(`ε₀ = 0`, `ε₁ = 1/2`, marginal `1/10`) the inversion returns `ε₁ = 1/2`. -/
144theorem fixture :
145 measAcc (4 / 5) (1 / 10) (1 / 10) (1 / 20) = 77 / 100 ∧
146 measErr (4 / 5) (1 / 10) (1 / 10) (1 / 20) = 7 / 50 ∧
147 measW1 (4 / 5) (1 / 10) (1 / 10) (1 / 20) / (1 - measAcc (4 / 5) (1 / 10) (1 / 10) (1 / 20))
148 = 11 / 46 ∧
149 measW1 (4 / 5) (1 / 10) (1 / 10) (1 / 20) / (1 - measAcc (4 / 5) (1 / 10) (1 / 10) (1 / 20))
150 - measErr (4 / 5) (1 / 10) (1 / 10) (1 / 20) = 57 / 575 ∧
151 (measW1 (4 / 5) 0 (1 / 2) (1 / 20) - 1 / 20 * (4 / 5 - (4 / 5 * 0 + (1 - 4 / 5) * (1 / 2)))) /
152 (1 - 4 / 5) = 1 / 2 := by
153 obtain ⟨hA, hE, hW, -⟩ := prop2 (4 / 5 : ℝ) (1 / 10) (1 / 10) (1 / 20)
154 obtain ⟨-, -, hW', -⟩ := prop2 (4 / 5 : ℝ) 0 (1 / 2) (1 / 20)
155 rw [hA, hE, hW, hW']
156 norm_num
157
158end FeedbackChannel.Judge