1import Mathlib.Tactic
2import Mathlib.Analysis.SpecialFunctions.Log.NegMulLog
3import Mathlib.Analysis.Calculus.Deriv.MeanValue
4
5/-!
6# Theorem B: best-of-`K` stays inside the KL ball of radius `ln K - (K - 1)/K`
7
8Source: `paper/selection_law_kl_containment.md`, draft 0.3, §0 and §2; code
9`src/spiral_ln/evaluation_selection_kl.py`.
10
11Setting (§0): finite `Ω`, base law `p₀`, score `S : Ω → ℝ`; `q(s) = P₀(S = s)`, `F(s) = P₀(S ≤ s)`,
12`F(s-) = P₀(S < s)`, `m_K(s) = F(s)^K - F(s-)^K` (law of the maximum of `K` scores);
13`λ_K = ln K - (K - 1)/K`. By Theorem A (formalized in `SelectionTieRules`) the kept record of every
14tie rule has law `π_K(ω) = p₀(ω) m_K(S ω)/q(S ω)`.
15
16**Theorem B.** `KL(π_K ‖ p₀) ≤ λ_K` for every finite `p₀`, score and `K ≥ 1`, with equality iff
17`K = 1`.
18
19The note's proof jitters the scores to a tie-free continuous law, where the KL is exactly
20`∫₀¹ K u^{K-1} ln(K u^{K-1}) du = λ_K`, and removes the jitter by Jensen. Here the Jensen step is
21done atom by atom without measure theory: on an atom with `F(s-) = a < b = F(s)`,
22`m_K(s)/q(s)` is the average of `h(u) = K u^{K-1}` over `[a, b]`, and the tangent-line form of
23Jensen's inequality for `φ(y) = y ln y` gives
24`(b^K - a^K) ln((b^K - a^K)/(b - a)) ≤ G(b) - G(a)` (`atom_le`), where
25`G(x) = x^K (ln K + (K - 1) ln x) - (K - 1) x^K/K` is an antiderivative of `h ln h` on `(0, ∞)`
26(`hasDerivAt_G`). Summing over atoms telescopes to `G(1) - G(0) = λ_K` (`telescope`), which is the
27tie-free integral (`G_one`, `G_zero`; the integral itself is `SelectionTieFree.tieFree_integral`).
28For `K ≥ 2` the step is strict on every atom of positive mass, because `h` is not constant there
29(`atom_lt`), which is the note's strict-convexity argument.
30
31* `atom_le`, `atom_lt`: the per-atom Jensen inequality and its strict form.
32* `kl_scores_le`, `kl_scores_lt`: `KL(m_K ‖ q) ≤ λ_K` (`< λ_K` for `K ≥ 2`) for every finite
33 score law `q`.
34* `theoremB`, `theoremB_strict`, `theoremB_eq_iff`: the same for `KL(π_K ‖ p₀)` with
35 `π_K(ω) = p₀(ω) m_K(S ω)/q(S ω)`; equality iff `K = 1`.
36* `kl_record_eq_kl_scores`: `KL(π_K ‖ p₀) = KL(m_K ‖ q)` (the last sentence of Theorem A).
37
38Not formalized: the supremum statement (uniform laws on `N` atoms approach `λ_K`) and the
39measure-theoretic statement that every atomless score law attains `λ_K`.
40-/
41
42namespace FeedbackChannel.SelectionContainment
43
44open Finset Real
45
46/-- `λ_K = ln K - (K - 1)/K`. -/
47noncomputable def lam (K : ℕ) : ℝ := log K - (K - 1) / K
48
49/-- An antiderivative of `h ln h`, `h(x) = K x^{K-1}`, on `(0, ∞)`, continuous on `ℝ`:
50`G(x) = x^K ln K + (K - 1) x^K ln x - (K - 1) x^K / K`. -/
51noncomputable def G (K : ℕ) (x : ℝ) : ℝ :=
52 x ^ K * log K + (K - 1) * (x ^ K * log x) - (K - 1) / K * x ^ K
53
54theorem G_zero {K : ℕ} (hK : 1 ≤ K) : G K 0 = 0 := by
55 simp [G, zero_pow (by omega : K ≠ 0)]
56
57/-- `G(1) - G(0) = λ_K`: the tie-free value `∫₀¹ K u^{K-1} ln(K u^{K-1}) du`. -/
58theorem G_one (K : ℕ) : G K 1 = lam K := by
59 simp [G, lam]
60
61theorem continuous_pow_mul_log {K : ℕ} (hK : 1 ≤ K) : Continuous fun x : ℝ => x ^ K * log x := by
62 have : (fun x : ℝ => x ^ K * log x) = fun x => x ^ (K - 1) * (x * log x) := by
63 funext x
64 conv_lhs => rw [show K = (K - 1) + 1 by omega, pow_succ]
65 ring
66 rw [this]
67 exact (continuous_pow _).mul continuous_mul_log
68
69theorem continuous_G {K : ℕ} (hK : 1 ≤ K) : Continuous (G K) := by
70 unfold G
71 exact (((continuous_pow K).mul continuous_const).add
72 (continuous_const.mul (continuous_pow_mul_log hK))).sub
73 (continuous_const.mul (continuous_pow K))
74
75/-- `G' = h ln h` with `h(x) = K x^{K-1}`, on `(0, ∞)`. -/
76theorem hasDerivAt_G {K : ℕ} (hK : 1 ≤ K) {x : ℝ} (hx : 0 < x) :
77 HasDerivAt (G K) (K * x ^ (K - 1) * (log K + (K - 1) * log x)) x := by
78 have hp := hasDerivAt_pow K x
79 have hl := Real.hasDerivAt_log hx.ne'
80 have h := ((hp.mul_const (log K)).add ((hp.mul hl).const_mul ((K : ℝ) - 1))).sub
81 (hp.const_mul (((K : ℝ) - 1) / K))
82 refine h.congr_deriv ?_
83 have hK' : (K : ℝ) ≠ 0 := by
84 have : (1 : ℝ) ≤ K := by exact_mod_cast hK
85 linarith
86 have hxK : x ^ K = x ^ (K - 1) * x := by
87 conv_lhs => rw [show K = (K - 1) + 1 by omega, pow_succ]
88 rw [hxK]
89 field_simp
90 ring
91
92/-- `φ(h) ≥ φ(y₀) + φ'(y₀)(h - y₀)` for `φ(y) = y ln y`, `h, y₀ > 0` (tangent line). -/
93theorem tangent_mul_log {h y0 : ℝ} (hh : 0 < h) (hy : 0 < y0) :
94 y0 * log y0 + (log y0 + 1) * (h - y0) ≤ h * log h := by
95 have key := Real.one_sub_inv_le_log_of_pos (div_pos hh hy)
96 rw [Real.log_div hh.ne' hy.ne', inv_div] at key
97 have : h * (1 - y0 / h) = h - y0 := by field_simp
98 nlinarith [mul_le_mul_of_nonneg_left key hh.le]
99
100/-- The tangent-line inequality is strict for `h ≠ y₀` (`φ` strictly convex). -/
101theorem tangent_mul_log_strict {h y0 : ℝ} (hh : 0 < h) (hy : 0 < y0) (hne : h ≠ y0) :
102 y0 * log y0 + (log y0 + 1) * (h - y0) < h * log h := by
103 have hne' : y0 / h ≠ 1 := by
104 intro h'
105 rw [div_eq_one_iff_eq hh.ne'] at h'
106 exact hne h'.symm
107 have key := Real.log_lt_sub_one_of_pos (div_pos hy hh) hne'
108 rw [Real.log_div hy.ne' hh.ne'] at key
109 have : h * (y0 / h - 1) = y0 - h := by field_simp
110 nlinarith [mul_lt_mul_of_pos_left key hh]
111
112/-- The comparison function of the tangent-line Jensen step on `[a, b]`:
113`D(x) = G(x) - G(a) - (x - a) φ(y₀) - φ'(y₀) ((x^K - a^K) - (x - a) y₀)`. -/
114noncomputable def Dfun (K : ℕ) (a y0 x : ℝ) : ℝ :=
115 G K x - G K a - (x - a) * (y0 * log y0) - (log y0 + 1) * ((x ^ K - a ^ K) - (x - a) * y0)
116
117/-- `D'(x) = φ(h(x)) - φ(y₀) - φ'(y₀)(h(x) - y₀)`, `h(x) = K x^{K-1}`. -/
118noncomputable def Dder (K : ℕ) (y0 x : ℝ) : ℝ :=
119 K * x ^ (K - 1) * (log K + (K - 1) * log x) - y0 * log y0 -
120 (log y0 + 1) * (K * x ^ (K - 1) - y0)
121
122theorem hasDerivAt_Dfun {K : ℕ} (hK : 1 ≤ K) (a y0 : ℝ) {x : ℝ} (hx : 0 < x) :
123 HasDerivAt (Dfun K a y0) (Dder K y0 x) x := by
124 have h1 := hasDerivAt_G hK hx
125 have h2 := ((hasDerivAt_id x).sub_const a).mul_const (y0 * log y0)
126 have h3 := (((hasDerivAt_pow K x).sub_const (a ^ K)).sub
127 (((hasDerivAt_id x).sub_const a).mul_const y0)).const_mul (log y0 + 1)
128 refine (((h1.sub_const (G K a)).sub h2).sub h3).congr_deriv ?_
129 unfold Dder
130 ring
131
132theorem log_h {K : ℕ} (hK : 1 ≤ K) {x : ℝ} (hx : 0 < x) :
133 log ((K : ℝ) * x ^ (K - 1)) = log K + (K - 1) * log x := by
134 have hK0 : (0 : ℝ) < K := by exact_mod_cast hK
135 rw [Real.log_mul (by positivity) (by positivity), Real.log_pow]
136 push_cast [Nat.cast_sub hK]
137 ring
138
139theorem Dder_eq {K : ℕ} (hK : 1 ≤ K) (y0 : ℝ) {x : ℝ} (hx : 0 < x) :
140 Dder K y0 x = (K * x ^ (K - 1)) * log (K * x ^ (K - 1)) -
141 (y0 * log y0 + (log y0 + 1) * (K * x ^ (K - 1) - y0)) := by
142 unfold Dder
143 rw [log_h hK hx]
144 ring
145
146theorem Dder_nonneg {K : ℕ} (hK : 1 ≤ K) {y0 : ℝ} (hy0 : 0 < y0) {x : ℝ} (hx : 0 < x) :
147 0 ≤ Dder K y0 x := by
148 rw [Dder_eq hK y0 hx]
149 have hK0 : (0 : ℝ) < K := by exact_mod_cast hK
150 have hpos : 0 < (K : ℝ) * x ^ (K - 1) := by positivity
151 linarith [tangent_mul_log hpos hy0]
152
153theorem Dfun_monotoneOn {K : ℕ} (hK : 1 ≤ K) {a y0 : ℝ} (ha : 0 ≤ a) (hy0 : 0 < y0) (b : ℝ) :
154 MonotoneOn (Dfun K a y0) (Set.Icc a b) := by
155 apply monotoneOn_of_deriv_nonneg (convex_Icc a b)
156 · apply Continuous.continuousOn
157 unfold Dfun
158 have := continuous_G hK
159 fun_prop
160 · intro x hx
161 rw [interior_Icc] at hx
162 exact (hasDerivAt_Dfun hK a y0 (by linarith [hx.1])).differentiableAt.differentiableWithinAt
163 · intro x hx
164 rw [interior_Icc] at hx
165 have hx0 : 0 < x := by linarith [hx.1]
166 rw [(hasDerivAt_Dfun hK a y0 hx0).deriv]
167 exact Dder_nonneg hK hy0 hx0
168
169theorem Dfun_self (K : ℕ) (a y0 : ℝ) : Dfun K a y0 a = 0 := by
170 simp [Dfun]
171
172theorem Dfun_at_b (K : ℕ) {a b : ℝ} (hab : a < b) :
173 Dfun K a ((b ^ K - a ^ K) / (b - a)) b =
174 G K b - G K a - (b ^ K - a ^ K) * log ((b ^ K - a ^ K) / (b - a)) := by
175 have hq : (b - a) * ((b ^ K - a ^ K) / (b - a)) = b ^ K - a ^ K := by
176 field_simp [(sub_pos.mpr hab).ne']
177 unfold Dfun
178 linear_combination hq
179
180/-- **The Jensen step on one atom.** For `0 ≤ a ≤ b` and `K ≥ 1`:
181`(b^K - a^K) ln((b^K - a^K)/(b - a)) ≤ G(b) - G(a)`, i.e. `q φ(m/q) ≤ ∫_a^b φ(K u^{K-1}) du` with
182`q = b - a`, `m = b^K - a^K`, `φ(y) = y ln y`. -/
183theorem atom_le {K : ℕ} (hK : 1 ≤ K) {a b : ℝ} (ha : 0 ≤ a) (hab : a ≤ b) :
184 (b ^ K - a ^ K) * log ((b ^ K - a ^ K) / (b - a)) ≤ G K b - G K a := by
185 rcases eq_or_lt_of_le hab with rfl | hlt
186 · simp
187 have hbK : a ^ K < b ^ K := pow_lt_pow_left₀ hlt ha (by omega)
188 have hy0 : 0 < (b ^ K - a ^ K) / (b - a) := div_pos (by linarith) (by linarith)
189 have h := Dfun_monotoneOn hK ha hy0 b ⟨le_rfl, hab⟩ ⟨hab, le_rfl⟩ hab
190 rw [Dfun_self, Dfun_at_b K hlt] at h
191 linarith
192
193/-- **Strict Jensen step** for `K ≥ 2` on an atom of positive mass: `h(u) = K u^{K-1}` is not
194constant on `(a, b)`. -/
195theorem atom_lt {K : ℕ} (hK : 2 ≤ K) {a b : ℝ} (ha : 0 ≤ a) (hab : a < b) :
196 (b ^ K - a ^ K) * log ((b ^ K - a ^ K) / (b - a)) < G K b - G K a := by
197 have hK1 : 1 ≤ K := by omega
198 have hbK : a ^ K < b ^ K := pow_lt_pow_left₀ hab ha (by omega)
199 have hy0 : 0 < (b ^ K - a ^ K) / (b - a) := div_pos (by linarith) (by linarith)
200 have hmono := Dfun_monotoneOn hK1 ha hy0 b
201 have hDb := Dfun_at_b K hab
202 by_contra hcon
203 have hDb0 : Dfun K a ((b ^ K - a ^ K) / (b - a)) b ≤ 0 := by
204 rw [hDb]; linarith [not_lt.mp hcon]
205 -- `D` vanishes on `[a, b]`
206 have hzero : ∀ x ∈ Set.Icc a b, Dfun K a ((b ^ K - a ^ K) / (b - a)) x = 0 := by
207 intro x hx
208 have h1 := hmono ⟨le_rfl, hab.le⟩ hx hx.1
209 have h2 := hmono hx ⟨hab.le, le_rfl⟩ hx.2
210 rw [Dfun_self] at h1
211 linarith
212 -- hence `h(x) = y₀` on `(a, b)`
213 have heq : ∀ x ∈ Set.Ioo a b, (K : ℝ) * x ^ (K - 1) = (b ^ K - a ^ K) / (b - a) := by
214 intro x hx
215 have hx0 : 0 < x := by linarith [hx.1]
216 have hloc : Dfun K a ((b ^ K - a ^ K) / (b - a)) =ᶠ[nhds x] fun _ => 0 := by
217 filter_upwards [Ioo_mem_nhds hx.1 hx.2] with z hz
218 exact hzero z ⟨hz.1.le, hz.2.le⟩
219 have h0 : HasDerivAt (Dfun K a ((b ^ K - a ^ K) / (b - a))) 0 x :=
220 (hasDerivAt_const x (0 : ℝ)).congr_of_eventuallyEq hloc
221 have hd := (hasDerivAt_Dfun hK1 a ((b ^ K - a ^ K) / (b - a)) hx0).unique h0
222 by_contra hne
223 have hK0 : (0 : ℝ) < K := by exact_mod_cast hK1
224 have hpos : 0 < (K : ℝ) * x ^ (K - 1) := by positivity
225 have := tangent_mul_log_strict hpos hy0 hne
226 rw [Dder_eq hK1 _ hx0] at hd
227 linarith
228 -- but `h` is strictly increasing for `K ≥ 2`
229 have hx1 : (2 * a + b) / 3 ∈ Set.Ioo a b := ⟨by linarith, by linarith⟩
230 have hx2 : (a + 2 * b) / 3 ∈ Set.Ioo a b := ⟨by linarith, by linarith⟩
231 have hlt12 : (2 * a + b) / 3 < (a + 2 * b) / 3 := by linarith
232 have hpow : ((2 * a + b) / 3) ^ (K - 1) < ((a + 2 * b) / 3) ^ (K - 1) :=
233 pow_lt_pow_left₀ hlt12 (by linarith) (by omega)
234 have hK0 : (0 : ℝ) < K := by exact_mod_cast hK1
235 have e1 := heq _ hx1
236 have e2 := heq _ hx2
237 nlinarith
238
239/-! ## Telescoping over the atoms -/
240
241/-- `F(s) = ∑_{t ∈ T, t ≤ s} q(t)` for a law `q` on the finite set of atoms `T`. -/
242noncomputable def Fle (T : Finset ℝ) (q : ℝ → ℝ) (s : ℝ) : ℝ := ∑ t ∈ T with t ≤ s, q t
243
244/-- `F(s-) = ∑_{t ∈ T, t < s} q(t)`. -/
245noncomputable def Flt (T : Finset ℝ) (q : ℝ → ℝ) (s : ℝ) : ℝ := ∑ t ∈ T with t < s, q t
246
247theorem telescope (g : ℝ → ℝ) (q : ℝ → ℝ) (T : Finset ℝ) :
248 ∑ s ∈ T, (g (Fle T q s) - g (Flt T q s)) = g (∑ t ∈ T, q t) - g 0 := by
249 induction T using Finset.induction_on_max with
250 | empty => simp
251 | insert x T hx ih =>
252 have hxT : x ∉ T := fun h => lt_irrefl x (hx x h)
253 rw [Finset.sum_insert hxT]
254 have h1 : ∀ s ∈ T, Fle (insert x T) q s = Fle T q s := by
255 intro s hs
256 unfold Fle
257 rw [Finset.filter_insert, ite_eq_right (not_le.mpr (hx s hs))]
258 have h2 : ∀ s ∈ T, Flt (insert x T) q s = Flt T q s := by
259 intro s hs
260 unfold Flt
261 rw [Finset.filter_insert, ite_eq_right (not_lt.mpr (hx s hs).le)]
262 rw [Finset.sum_congr rfl (fun s hs => by rw [h1 s hs, h2 s hs]), ih]
263 have h3 : Fle (insert x T) q x = ∑ t ∈ insert x T, q t := by
264 unfold Fle
265 rw [Finset.filter_true_of_mem]
266 intro y hy
267 rcases Finset.mem_insert.mp hy with rfl | hy
268 · exact le_rfl
269 · exact (hx y hy).le
270 have h4 : Flt (insert x T) q x = ∑ t ∈ T, q t := by
271 unfold Flt
272 rw [Finset.filter_insert, ite_eq_right (lt_irrefl x), Finset.filter_true_of_mem hx]
273 rw [h3, h4, Finset.sum_insert hxT]
274 ring
275
276theorem Fle_eq_Flt_add {T : Finset ℝ} (q : ℝ → ℝ) {s : ℝ} (hs : s ∈ T) :
277 Fle T q s = Flt T q s + q s := by
278 unfold Fle Flt
279 rw [Finset.sum_filter, Finset.sum_filter]
280 have : ∀ t, (if t ≤ s then q t else 0) = (if t < s then q t else 0) + (if t = s then q t else 0) := by
281 intro t
282 rcases lt_trichotomy t s with h | rfl | h
283 · rw [ite_eq_left h.le, ite_eq_left h, ite_eq_right h.ne]; ring
284 · simp
285 · rw [ite_eq_right (not_le.mpr h), ite_eq_right (not_lt.mpr h.le), ite_eq_right h.ne']; ring
286 rw [Finset.sum_congr rfl (fun t _ => this t), Finset.sum_add_distrib, Finset.sum_ite_eq' T s q,
287 ite_eq_left hs]
288
289theorem Flt_nonneg {T : Finset ℝ} {q : ℝ → ℝ} (hq : ∀ t ∈ T, 0 ≤ q t) (s : ℝ) :
290 0 ≤ Flt T q s :=
291 Finset.sum_nonneg fun t ht => hq t (Finset.mem_filter.mp ht).1
292
293/-- **Theorem B, score form:** for every finite score law `q` on atoms `T` and `K ≥ 1`,
294`KL(m_K ‖ q) = ∑_s m_K(s) ln(m_K(s)/q(s)) ≤ ln K - (K - 1)/K`. -/
295theorem kl_scores_le {K : ℕ} (hK : 1 ≤ K) (T : Finset ℝ) (q : ℝ → ℝ) (hq : ∀ s ∈ T, 0 ≤ q s)
296 (hsum : ∑ s ∈ T, q s = 1) :
297 ∑ s ∈ T, (Fle T q s ^ K - Flt T q s ^ K) * log ((Fle T q s ^ K - Flt T q s ^ K) / q s) ≤
298 lam K := by
299 calc ∑ s ∈ T, (Fle T q s ^ K - Flt T q s ^ K) * log ((Fle T q s ^ K - Flt T q s ^ K) / q s)
300 ≤ ∑ s ∈ T, (G K (Fle T q s) - G K (Flt T q s)) := by
301 apply Finset.sum_le_sum
302 intro s hs
303 have hle := Fle_eq_Flt_add q hs
304 have h0 := Flt_nonneg hq s
305 have := atom_le hK h0 (show Flt T q s ≤ Fle T q s by linarith [hq s hs])
306 rwa [show Fle T q s - Flt T q s = q s by linarith] at this
307 _ = G K 1 - G K 0 := by rw [telescope, hsum]
308 _ = lam K := by rw [G_one, G_zero hK, sub_zero]
309
310/-- **Theorem B, score form, strict:** for `K ≥ 2`, `KL(m_K ‖ q) < λ_K` on every finite law. -/
311theorem kl_scores_lt {K : ℕ} (hK : 2 ≤ K) (T : Finset ℝ) (q : ℝ → ℝ) (hq : ∀ s ∈ T, 0 ≤ q s)
312 (hsum : ∑ s ∈ T, q s = 1) :
313 ∑ s ∈ T, (Fle T q s ^ K - Flt T q s ^ K) * log ((Fle T q s ^ K - Flt T q s ^ K) / q s) <
314 lam K := by
315 have hK1 : 1 ≤ K := by omega
316 obtain ⟨s0, hs0, hq0⟩ : ∃ s ∈ T, 0 < q s := by
317 by_contra h
318 have : ∑ s ∈ T, q s ≤ 0 :=
319 Finset.sum_nonpos fun s hs => not_lt.mp fun hlt => h ⟨s, hs, hlt⟩
320 linarith
321 calc ∑ s ∈ T, (Fle T q s ^ K - Flt T q s ^ K) * log ((Fle T q s ^ K - Flt T q s ^ K) / q s)
322 < ∑ s ∈ T, (G K (Fle T q s) - G K (Flt T q s)) := by
323 apply Finset.sum_lt_sum
324 · intro s hs
325 have hle := Fle_eq_Flt_add q hs
326 have h0 := Flt_nonneg hq s
327 have := atom_le hK1 h0 (show Flt T q s ≤ Fle T q s by linarith [hq s hs])
328 rwa [show Fle T q s - Flt T q s = q s by linarith] at this
329 · refine ⟨s0, hs0, ?_⟩
330 have hle := Fle_eq_Flt_add q hs0
331 have h0 := Flt_nonneg hq s0
332 have := atom_lt hK h0 (show Flt T q s0 < Fle T q s0 by linarith)
333 rwa [show Fle T q s0 - Flt T q s0 = q s0 by linarith] at this
334 _ = G K 1 - G K 0 := by rw [telescope, hsum]
335 _ = lam K := by rw [G_one, G_zero hK1, sub_zero]
336
337/-! ## Records -/
338
339section Records
340
341variable {Ω : Type*} [Fintype Ω] (p0 : Ω → ℝ) (S : Ω → ℝ)
342
343/-- `q(s) = P₀(S = s)`. -/
344noncomputable def scoreMass (s : ℝ) : ℝ := ∑ ω with S ω = s, p0 ω
345
346/-- `F(s) = P₀(S ≤ s)`. -/
347noncomputable def cdf (s : ℝ) : ℝ := ∑ ω with S ω ≤ s, p0 ω
348
349/-- `F(s-) = P₀(S < s)`. -/
350noncomputable def cdfLt (s : ℝ) : ℝ := ∑ ω with S ω < s, p0 ω
351
352/-- `m_K(s) = F(s)^K - F(s-)^K`, the law of the maximum of `K` i.i.d. scores. -/
353noncomputable def maxLaw (K : ℕ) (s : ℝ) : ℝ := cdf p0 S s ^ K - cdfLt p0 S s ^ K
354
355/-- The kept record's law given by Theorem A, `π_K(ω) = p₀(ω) m_K(S ω)/q(S ω)` (zero where
356`p₀(ω) = 0`). -/
357noncomputable def bestOfK (K : ℕ) (ω : Ω) : ℝ := p0 ω * maxLaw p0 S K (S ω) / scoreMass p0 S (S ω)
358
359/-- Discrete KL divergence `∑ π log(π/p)` (with `0 log 0 = 0`; used here only for `π ≪ p`). -/
360noncomputable def kl {α : Type*} [Fintype α] (μ ν : α → ℝ) : ℝ := ∑ x, μ x * log (μ x / ν x)
361
362/-- The atoms of the score law. -/
363noncomputable def atoms : Finset ℝ := univ.image S
364
365theorem cdf_eq_Fle (s : ℝ) : cdf p0 S s = Fle (atoms S) (scoreMass p0 S) s := by
366 unfold cdf Fle scoreMass atoms
367 rw [← Finset.sum_fiberwise_of_maps_to (s := univ.filter fun ω => S ω ≤ s)
368 (t := (univ.image S).filter fun t => t ≤ s) (g := S)]
369 · apply Finset.sum_congr rfl
370 intro t ht
371 have hts : t ≤ s := (Finset.mem_filter.mp ht).2
372 apply Finset.sum_congr
373 · ext ω; simp only [Finset.mem_filter, Finset.mem_univ, true_and]
374 constructor
375 · exact fun h => h.2
376 · intro h; exact ⟨h ▸ hts, h⟩
377 · intro _ _; rfl
378 · intro ω hω
379 simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hω
380 simp [hω]
381
382theorem cdfLt_eq_Flt (s : ℝ) : cdfLt p0 S s = Flt (atoms S) (scoreMass p0 S) s := by
383 unfold cdfLt Flt scoreMass atoms
384 rw [← Finset.sum_fiberwise_of_maps_to (s := univ.filter fun ω => S ω < s)
385 (t := (univ.image S).filter fun t => t < s) (g := S)]
386 · apply Finset.sum_congr rfl
387 intro t ht
388 have hts : t < s := (Finset.mem_filter.mp ht).2
389 apply Finset.sum_congr
390 · ext ω; simp only [Finset.mem_filter, Finset.mem_univ, true_and]
391 constructor
392 · exact fun h => h.2
393 · intro h; exact ⟨h ▸ hts, h⟩
394 · intro _ _; rfl
395 · intro ω hω
396 simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hω
397 simp [hω]
398
399theorem sum_scoreMass : ∑ t ∈ atoms S, scoreMass p0 S t = ∑ ω, p0 ω := by
400 unfold scoreMass atoms
401 exact Finset.sum_fiberwise_of_maps_to (fun ω _ => Finset.mem_image_of_mem S (mem_univ ω)) p0
402
403theorem sum_comp_score (g : ℝ → ℝ) :
404 ∑ ω, p0 ω * g (S ω) = ∑ t ∈ atoms S, scoreMass p0 S t * g t := by
405 unfold scoreMass atoms
406 rw [← Finset.sum_fiberwise_of_maps_to (fun ω _ => Finset.mem_image_of_mem S (mem_univ ω))
407 (fun ω => p0 ω * g (S ω))]
408 apply Finset.sum_congr rfl
409 intro t _
410 rw [Finset.sum_mul]
411 apply Finset.sum_congr rfl
412 intro ω hω
413 rw [(Finset.mem_filter.mp hω).2]
414
415theorem scoreMass_nonneg (hp0 : ∀ ω, 0 ≤ p0 ω) (s : ℝ) : 0 ≤ scoreMass p0 S s :=
416 Finset.sum_nonneg fun ω _ => hp0 ω
417
418theorem p0_le_scoreMass (hp0 : ∀ ω, 0 ≤ p0 ω) (ω : Ω) : p0 ω ≤ scoreMass p0 S (S ω) := by
419 unfold scoreMass
420 exact Finset.single_le_sum (fun ω' _ => hp0 ω') (Finset.mem_filter.mpr ⟨mem_univ ω, rfl⟩)
421
422theorem maxLaw_eq (K : ℕ) (t : ℝ) : maxLaw p0 S K t =
423 Fle (atoms S) (scoreMass p0 S) t ^ K - Flt (atoms S) (scoreMass p0 S) t ^ K := by
424 unfold maxLaw; rw [cdf_eq_Fle, cdfLt_eq_Flt]
425
426/-- **Theorem A, KL sentence:** `KL(π_K ‖ p₀) = KL(m_K ‖ q)`: the likelihood ratio depends on the
427record only through its score. -/
428theorem kl_record_eq_kl_scores (K : ℕ) :
429 kl (bestOfK p0 S K) p0 =
430 ∑ t ∈ atoms S, maxLaw p0 S K t * log (maxLaw p0 S K t / scoreMass p0 S t) := by
431 unfold kl
432 have hrec : ∀ ω, bestOfK p0 S K ω * log (bestOfK p0 S K ω / p0 ω) =
433 p0 ω * ((maxLaw p0 S K (S ω) / scoreMass p0 S (S ω)) *
434 log (maxLaw p0 S K (S ω) / scoreMass p0 S (S ω))) := by
435 intro ω
436 unfold bestOfK
437 by_cases h : p0 ω = 0
438 · simp [h]
439 · rw [show p0 ω * maxLaw p0 S K (S ω) / scoreMass p0 S (S ω) / p0 ω =
440 maxLaw p0 S K (S ω) / scoreMass p0 S (S ω) by field_simp]
441 ring
442 rw [Finset.sum_congr rfl (fun ω _ => hrec ω), sum_comp_score p0 S
443 (fun s => maxLaw p0 S K s / scoreMass p0 S s * log (maxLaw p0 S K s / scoreMass p0 S s))]
444 apply Finset.sum_congr rfl
445 intro t ht
446 by_cases hq : scoreMass p0 S t = 0
447 · -- an atom of mass zero has `m_K = 0`
448 have hm : maxLaw p0 S K t = 0 := by
449 unfold maxLaw
450 rw [cdf_eq_Fle, cdfLt_eq_Flt, Fle_eq_Flt_add _ ht, hq, add_zero, sub_self]
451 simp [hq, hm]
452 · field_simp
453
454/-- **Theorem B.** For every finite base law `p₀`, score `S` and `K ≥ 1`, the best-of-`K` record law
455`π_K` of Theorem A satisfies `KL(π_K ‖ p₀) ≤ ln K - (K - 1)/K`. With `SelectionTieRules.keptLaw_eq`
456this holds for every tie rule (`SelectionTieRules.theoremB_every_tie_rule`). -/
457theorem theoremB (hp0 : ∀ ω, 0 ≤ p0 ω) (hsum : ∑ ω, p0 ω = 1) {K : ℕ} (hK : 1 ≤ K) :
458 kl (bestOfK p0 S K) p0 ≤ lam K := by
459 rw [kl_record_eq_kl_scores p0 S K]
460 simp_rw [maxLaw_eq p0 S K]
461 exact kl_scores_le hK (atoms S) (scoreMass p0 S) (fun s _ => scoreMass_nonneg p0 S hp0 s)
462 (by rw [sum_scoreMass, hsum])
463
464/-- **Theorem B, strictness:** for `K ≥ 2`, `KL(π_K ‖ p₀) < λ_K` on every finite `p₀`. -/
465theorem theoremB_strict (hp0 : ∀ ω, 0 ≤ p0 ω) (hsum : ∑ ω, p0 ω = 1) {K : ℕ} (hK : 2 ≤ K) :
466 kl (bestOfK p0 S K) p0 < lam K := by
467 rw [kl_record_eq_kl_scores p0 S K]
468 simp_rw [maxLaw_eq p0 S K]
469 exact kl_scores_lt hK (atoms S) (scoreMass p0 S) (fun s _ => scoreMass_nonneg p0 S hp0 s)
470 (by rw [sum_scoreMass, hsum])
471
472/-- **Theorem B, equality case:** for `K ≥ 1`, `KL(π_K ‖ p₀) = λ_K` iff `K = 1`. -/
473theorem theoremB_eq_iff (hp0 : ∀ ω, 0 ≤ p0 ω) (hsum : ∑ ω, p0 ω = 1) {K : ℕ} (hK : 1 ≤ K) :
474 kl (bestOfK p0 S K) p0 = lam K ↔ K = 1 := by
475 constructor
476 · intro h
477 by_contra hne
478 have := theoremB_strict p0 S hp0 hsum (show 2 ≤ K by omega)
479 linarith
480 · rintro rfl
481 rw [kl_record_eq_kl_scores p0 S 1]
482 have : lam 1 = 0 := by simp [lam]
483 rw [this]
484 apply Finset.sum_eq_zero
485 intro t ht
486 rw [maxLaw_eq, pow_one, pow_one, Fle_eq_Flt_add _ ht, add_sub_cancel_left]
487 by_cases hq : scoreMass p0 S t = 0
488 · simp [hq]
489 · rw [div_self hq, Real.log_one, mul_zero]
490
491end Records
492
493end FeedbackChannel.SelectionContainment