Threshold.lean

Lean 4 source, lean/DataMiningAsObservation/Threshold.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
Threshold.lean. Chapter 6 section 6.1, the threshold.

A threshold turns a score into a decision. Raising it can only shrink the set predicted
positive, so both the true positives and the false positives can only fall, which is why
the ROC curve is traced by a single sweep. A strictly increasing transform of the score with
the matching transform of the threshold leaves every decision unchanged, which is the
Monotone Invariance Theorem at the level of one decision.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance

namespace DMO.Threshold

open Finset DMO.MonotoneInvariance

variable {ι : Type*} [Fintype ι] [DecidableEq ι]

omit [DecidableEq ι] in
/-- **Raising the threshold shrinks the predicted set.** -/
theorem predicted_anti (s : ι → ℝ) (τ τ' : ℝ) (h : τ ≤ τ') : predicted s τ' ⊆ predicted s τ := by
  intro i hi
  simp only [predicted, Finset.mem_filter, Finset.mem_univ, true_and] at hi ⊢
  exact h.trans hi

/-- **True positives can only fall as the threshold rises.** -/
theorem tp_anti (pos : Finset ι) (s : ι → ℝ) (τ τ' : ℝ) (h : τ ≤ τ') :
    (pos ∩ predicted s τ').card ≤ (pos ∩ predicted s τ).card :=
  Finset.card_le_card (Finset.inter_subset_inter_left (predicted_anti s τ τ' h))

/-- **False positives can only fall as the threshold rises.** -/
theorem fp_anti (neg : Finset ι) (s : ι → ℝ) (τ τ' : ℝ) (h : τ ≤ τ') :
    (neg ∩ predicted s τ').card ≤ (neg ∩ predicted s τ).card :=
  Finset.card_le_card (Finset.inter_subset_inter_left (predicted_anti s τ τ' h))

omit [DecidableEq ι] in
/-- **A decision survives a monotone recalibration** when the threshold is recalibrated with
it. -/
theorem decision_comp {g : ℝ → ℝ} (hg : StrictMono g) (s : ι → ℝ) (τ : ℝ) :
    predicted (g ∘ s) (g τ) = predicted s τ := by
  ext i
  simp [predicted, hg.le_iff_le]

omit [DecidableEq ι] in
/-- **The extreme thresholds.** Below every score everything is predicted positive, and above
every score nothing is. -/
theorem predicted_extremes (s : ι → ℝ) (τlo τhi : ℝ) (hlo : ∀ i, τlo ≤ s i) (hhi : ∀ i, s i < τhi) :
    predicted s τlo = Finset.univ ∧ predicted s τhi = ∅ := by
  constructor
  · ext i; simp [predicted, hlo i]
  · ext i; simp [predicted, not_le.2 (hhi i)]

end DMO.Threshold