YoudenF1.lean
Lean 4 source, lean/DataMiningAsObservation/YoudenF1.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
YoudenF1.lean. Chapter 6 section 6.3 and the theory-radar erratum of 2026-09-03.
At an operating point with `TP` true positives out of `P` positives and `FP` false positives
out of `N` negatives, thresholded F1 is `2 TP / (P + TP + FP)`. If the score's Youden index,
the largest `TP/P − FP/N` over thresholds, is at most `J`, then `FP ≥ N (TP/P − J)` at every
operating point, which gives the ceiling the corrected search uses. That ceiling holds for
every score. The earlier AUROC form substituted `2 A − 1` for `J`, which is valid only for a
concave ROC curve, and the file ends with the counterexample from the erratum, a score whose
Youden index exceeds `2 A − 1`.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance
namespace DMO.YoudenF1
open Finset
/-- F1 from counts. With `P` positives, `TP` true positives, and `FP` false positives,
`F1 = 2 TP / (2 TP + FP + FN) = 2 TP / (P + TP + FP)`. -/
noncomputable def f1 (P TP FP : ℝ) : ℝ := 2 * TP / (P + TP + FP)
/-- The two forms of F1 agree. -/
theorem f1_eq (P TP FP : ℝ) : f1 P TP FP = 2 * TP / (2 * TP + FP + (P - TP)) := by
unfold f1
ring_nf
/-- **The Youden ceiling.** If the operating point's `TP/P − FP/N` is at most `J`, thresholded
F1 is at most `2 TP / (P + TP + N (TP/P − J))`. No concavity of the ROC curve is assumed. -/
theorem f1_le_of_youden (P N TP FP J : ℝ) (hN : 0 < N) (hTP : 0 ≤ TP)
(hJ : TP / P - FP / N ≤ J) (hden : 0 < P + TP + N * (TP / P - J)) :
f1 P TP FP ≤ 2 * TP / (P + TP + N * (TP / P - J)) := by
unfold f1
apply div_le_div_of_nonneg_left (by linarith) hden
have h2 : TP / P - J ≤ FP / N := by linarith
have h3 : N * (TP / P - J) ≤ FP := by
calc N * (TP / P - J) ≤ N * (FP / N) := mul_le_mul_of_nonneg_left h2 hN.le
_ = FP := by field_simp
linarith
/-! ### The counterexample to the AUROC form -/
/-- Four positives and one negative. Three positives score above the negative, one below. -/
def pos : Finset (Fin 5) := {0, 1, 2, 3}
def neg : Finset (Fin 5) := {4}
/-- The scores. -/
noncomputable def s : Fin 5 → ℝ := ![3, 3, 3, 0, 1]
/-- The AUROC of this score is 3/4. -/
theorem auroc_eq : DMO.MonotoneInvariance.auroc pos neg s = 3 / 4 := by
simp [DMO.MonotoneInvariance.auroc, DMO.MonotoneInvariance.aurocNum, pos, neg, s]
norm_num
/-- The Youden index at a threshold, `TP/P − FP/N` with the rows at or above the threshold
predicted positive. -/
noncomputable def youdenAt (pos neg : Finset (Fin 5)) (s : Fin 5 → ℝ) (τ : ℝ) : ℝ :=
((pos.filter fun i => τ ≤ s i).card : ℝ) / pos.card
- ((neg.filter fun i => τ ≤ s i).card : ℝ) / neg.card
/-- At threshold 3 the Youden index is 3/4. -/
theorem youden_eq : youdenAt pos neg s 3 = 3 / 4 := by
have h : ({0, 1, 2} : Finset (Fin 5)).card = 3 := by decide
simp [youdenAt, pos, neg, s, Finset.filter_insert, Finset.filter_singleton]
norm_num [h]
/-- **The erratum.** The Youden index exceeds `2 A − 1`, so a ceiling built from `2 A − 1` in
place of the Youden index is not a bound for this score. -/
theorem youden_exceeds_auroc_form :
2 * DMO.MonotoneInvariance.auroc pos neg s - 1 < youdenAt pos neg s 3 := by
rw [auroc_eq, youden_eq]
norm_num
end DMO.YoudenF1