MonotoneInvariance.lean
Lean 4 source, lean/DataMiningAsObservation/MonotoneInvariance.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
MonotoneInvariance.lean. Equation 6.2, the Monotone Invariance Theorem.
A strictly monotone transform of a score cannot change its AUROC or its optimal thresholded
F1, because both depend only on the ranking the score induces. AUROC is defined here as the
book defines it in equation 0.16, the fraction of positive-negative pairs the score orders
correctly with ties counted one half. Optimal thresholded F1 sweeps the threshold over every
score value and takes the best F1, as chapter 0 section 0.14 describes.
-/
import Mathlib
set_option linter.unusedSectionVars false
namespace DMO.MonotoneInvariance
open Finset
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
/-- Pairwise AUROC numerator. A pair of a positive `p` and a negative `n` scores one when
`s p > s n`, one half on a tie, and zero otherwise. -/
noncomputable def aurocNum (pos neg : Finset ι) (s : ι → ℝ) : ℝ :=
∑ p ∈ pos, ∑ n ∈ neg, ((if s n < s p then (1 : ℝ) else 0) + (if s n = s p then (1 / 2 : ℝ) else 0))
/-- AUROC, equation 0.16, the numerator over the number of pairs. -/
noncomputable def auroc (pos neg : Finset ι) (s : ι → ℝ) : ℝ :=
aurocNum pos neg s / (pos.card * neg.card)
/-- A strictly monotone transform preserves every pairwise comparison, so the numerator is
unchanged. -/
theorem aurocNum_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos neg : Finset ι) (s : ι → ℝ) :
aurocNum pos neg (g ∘ s) = aurocNum pos neg s := by
unfold aurocNum
refine Finset.sum_congr rfl fun p _ => Finset.sum_congr rfl fun n _ => ?_
simp only [Function.comp_apply, hg.lt_iff_lt, hg.injective.eq_iff]
/-- **Monotone invariance of AUROC.** -/
theorem auroc_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos neg : Finset ι) (s : ι → ℝ) :
auroc pos neg (g ∘ s) = auroc pos neg s := by
unfold auroc
rw [aurocNum_comp hg]
/-- The rows predicted positive at threshold `τ`, those whose score is at least `τ`. -/
noncomputable def predicted (s : ι → ℝ) (τ : ℝ) : Finset ι := Finset.univ.filter fun i => τ ≤ s i
/-- F1 of a predicted set against the positives, twice the true positives over the sum of
the positive count and the predicted count, which equals 2P R / (P + R). -/
noncomputable def f1 (pos pred : Finset ι) : ℝ :=
2 * (pos ∩ pred).card / (pos.card + pred.card)
/-- The F1 values reached by sweeping the threshold over every score value. -/
noncomputable def sweptF1 (pos : Finset ι) (s : ι → ℝ) : Finset ℝ :=
Finset.univ.image fun j => f1 pos (predicted s (s j))
/-- The rows predicted positive when the threshold reads the other way, those whose score is
at most `τ`. Equation 6.2 maximizes over both directions. -/
noncomputable def predictedBelow (s : ι → ℝ) (τ : ℝ) : Finset ι := Finset.univ.filter fun i => s i ≤ τ
/-- The F1 values reached by sweeping the threshold over every score value in the other
direction. -/
noncomputable def sweptF1Below (pos : Finset ι) (s : ι → ℝ) : Finset ℝ :=
Finset.univ.image fun j => f1 pos (predictedBelow s (s j))
/-- The optimal thresholded F1 of equation 6.2, the best value over both directions of the
sweep. -/
noncomputable def optF1 (pos : Finset ι) (s : ι → ℝ) : WithBot ℝ :=
(sweptF1 pos s ∪ sweptF1Below pos s).max
/-- Thresholding a transformed score at a transformed value predicts the same rows. -/
theorem predicted_comp {g : ℝ → ℝ} (hg : StrictMono g) (s : ι → ℝ) (j : ι) :
predicted (g ∘ s) ((g ∘ s) j) = predicted s (s j) := by
ext i
simp [predicted, hg.le_iff_le]
theorem predictedBelow_comp {g : ℝ → ℝ} (hg : StrictMono g) (s : ι → ℝ) (j : ι) :
predictedBelow (g ∘ s) ((g ∘ s) j) = predictedBelow s (s j) := by
ext i
simp [predictedBelow, hg.le_iff_le]
/-- The sweep reaches the same F1 values. -/
theorem sweptF1_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos : Finset ι) (s : ι → ℝ) :
sweptF1 pos (g ∘ s) = sweptF1 pos s := by
unfold sweptF1
congr 1
funext j
rw [predicted_comp hg]
theorem sweptF1Below_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos : Finset ι) (s : ι → ℝ) :
sweptF1Below pos (g ∘ s) = sweptF1Below pos s := by
unfold sweptF1Below
congr 1
funext j
rw [predictedBelow_comp hg]
/-- **Monotone invariance of optimal thresholded F1**, equation 6.2. -/
theorem optF1_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos : Finset ι) (s : ι → ℝ) :
optF1 pos (g ∘ s) = optF1 pos s := by
unfold optF1
rw [sweptF1_comp hg, sweptF1Below_comp hg]
end DMO.MonotoneInvariance