ReliabilityWeight.lean
Lean 4 source, lean/DataMiningAsObservation/ReliabilityWeight.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
ReliabilityWeight.lean. Equation 0.38, the reliability weight.
An encoder's reliability weight is zero or twice its held-out AUROC minus one, whichever is
larger. With AUROC as equation 0.16 defines it, the weight lies in the unit interval, it is
zero at chance level and below, and since it is a function of the ranking alone it is
unchanged by any strictly monotone transform of the encoder's score. It is a discrimination
weight, and it says nothing about calibration, which is why the expected calibration error
is reported beside it.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance
namespace DMO.ReliabilityWeight
open Finset DMO.MonotoneInvariance
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
/-- **Equation 0.38.** The reliability weight of a score. -/
noncomputable def weight (pos neg : Finset ι) (s : ι → ℝ) : ℝ :=
max 0 (2 * auroc pos neg s - 1)
omit [Fintype ι] [DecidableEq ι] in
/-- Each pair contributes at most one to the AUROC numerator. -/
theorem pair_le_one (a b : ℝ) :
(if b < a then (1 : ℝ) else 0) + (if b = a then (1 / 2 : ℝ) else 0) ≤ 1 := by
split_ifs with h1 h2 <;> linarith
omit [Fintype ι] [DecidableEq ι] in
/-- The AUROC numerator is at most the number of pairs. -/
theorem aurocNum_le (pos neg : Finset ι) (s : ι → ℝ) :
aurocNum pos neg s ≤ pos.card * neg.card := by
unfold aurocNum
calc ∑ p ∈ pos, ∑ n ∈ neg, ((if s n < s p then (1 : ℝ) else 0) + (if s n = s p then (1 / 2 : ℝ) else 0))
≤ ∑ p ∈ pos, ∑ n ∈ neg, (1 : ℝ) := by
apply Finset.sum_le_sum; intro p _
apply Finset.sum_le_sum; intro n _
exact pair_le_one _ _
_ = pos.card * neg.card := by simp
omit [Fintype ι] [DecidableEq ι] in
/-- **AUROC is at most one.** -/
theorem auroc_le_one (pos neg : Finset ι) (s : ι → ℝ) : auroc pos neg s ≤ 1 := by
unfold auroc
rcases eq_or_lt_of_le (by positivity : (0 : ℝ) ≤ pos.card * neg.card) with h | h
· rw [← h, div_zero]; norm_num
· rw [div_le_one h]; exact aurocNum_le pos neg s
omit [Fintype ι] [DecidableEq ι] in
/-- **The weight lies in the unit interval.** -/
theorem weight_mem_unit (pos neg : Finset ι) (s : ι → ℝ) :
0 ≤ weight pos neg s ∧ weight pos neg s ≤ 1 := by
unfold weight
refine ⟨le_max_left _ _, ?_⟩
rw [max_le_iff]
exact ⟨by norm_num, by linarith [auroc_le_one pos neg s]⟩
omit [Fintype ι] [DecidableEq ι] in
/-- **Chance level and below give weight zero.** -/
theorem weight_eq_zero_of_le_half (pos neg : Finset ι) (s : ι → ℝ) (h : auroc pos neg s ≤ 1 / 2) :
weight pos neg s = 0 := by
unfold weight
rw [max_eq_left]
linarith
/-- **The weight is a discrimination weight.** A strictly monotone transform of the score
leaves it unchanged, so recalibrating a score changes no encoder's authority. -/
theorem weight_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos neg : Finset ι) (s : ι → ℝ) :
weight pos neg (g ∘ s) = weight pos neg s := by
unfold weight
rw [auroc_comp hg]
end DMO.ReliabilityWeight