Auroc.lean
Lean 4 source, lean/DataMiningAsObservation/Auroc.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Auroc.lean. Equation 0.16, the AUROC.
The AUROC is the probability that a random positive scores above a random negative, ties
counted one half. It lies in the unit interval, it is one when every positive scores above
every negative and zero when every negative scores above every positive, it is one half for
a constant score, and a strictly monotone transform of the score leaves it unchanged. The
file gathers these from the monotone invariance, reliability weight, and chance level files
and adds the two ends.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance
import DataMiningAsObservation.ReliabilityWeight
import DataMiningAsObservation.ChanceLevel
namespace DMO.Auroc
open Finset DMO.MonotoneInvariance
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
omit [Fintype ι] [DecidableEq ι] in
/-- Each pair contributes a nonnegative amount. -/
theorem pair_nonneg (a b : ℝ) :
0 ≤ (if b < a then (1 : ℝ) else 0) + (if b = a then (1 / 2 : ℝ) else 0) := by
split_ifs <;> norm_num
omit [Fintype ι] [DecidableEq ι] in
/-- **AUROC is nonnegative.** -/
theorem auroc_nonneg (pos neg : Finset ι) (s : ι → ℝ) : 0 ≤ auroc pos neg s := by
unfold auroc aurocNum
apply div_nonneg _ (by positivity)
exact Finset.sum_nonneg fun p _ => Finset.sum_nonneg fun n _ => pair_nonneg _ _
omit [Fintype ι] [DecidableEq ι] in
/-- **AUROC is at most one.** -/
theorem auroc_le_one (pos neg : Finset ι) (s : ι → ℝ) : auroc pos neg s ≤ 1 :=
DMO.ReliabilityWeight.auroc_le_one pos neg s
omit [Fintype ι] [DecidableEq ι] in
/-- **Perfect separation gives AUROC one.** -/
theorem auroc_perfect (pos neg : Finset ι) (hp : pos.Nonempty) (hn : neg.Nonempty) (s : ι → ℝ)
(h : ∀ p ∈ pos, ∀ n ∈ neg, s n < s p) : auroc pos neg s = 1 := by
unfold auroc aurocNum
have hsum : ∑ p ∈ pos, ∑ n ∈ neg,
((if s n < s p then (1 : ℝ) else 0) + (if s n = s p then (1 / 2 : ℝ) else 0))
= pos.card * neg.card := by
rw [Finset.sum_congr rfl fun p hp' => Finset.sum_congr rfl fun n hn' => by
rw [if_pos (h p hp' n hn'), if_neg (ne_of_lt (h p hp' n hn')), add_zero]]
simp
rw [hsum]
have : (0 : ℝ) < pos.card * neg.card := by
have := hp.card_pos; have := hn.card_pos; positivity
exact div_self this.ne'
omit [Fintype ι] [DecidableEq ι] in
/-- **Perfect reversal gives AUROC zero.** -/
theorem auroc_reversed (pos neg : Finset ι) (s : ι → ℝ)
(h : ∀ p ∈ pos, ∀ n ∈ neg, s p < s n) : auroc pos neg s = 0 := by
unfold auroc aurocNum
rw [Finset.sum_eq_zero fun p hp' => Finset.sum_eq_zero fun n hn' => by
rw [if_neg (not_lt.2 (h p hp' n hn').le), if_neg (ne_of_lt (h p hp' n hn')).symm, add_zero]]
simp
omit [Fintype ι] [DecidableEq ι] in
/-- **A constant score scores one half.** -/
theorem auroc_chance (pos neg : Finset ι) (hp : pos.Nonempty) (hn : neg.Nonempty) (c : ℝ) :
auroc pos neg (fun _ => c) = 1 / 2 :=
DMO.ChanceLevel.auroc_const pos neg hp hn c
/-- **A strictly monotone transform leaves AUROC unchanged.** -/
theorem auroc_monotone_invariant {g : ℝ → ℝ} (hg : StrictMono g) (pos neg : Finset ι) (s : ι → ℝ) :
auroc pos neg (g ∘ s) = auroc pos neg s :=
auroc_comp hg pos neg s
end DMO.Auroc