NullModel.lean
Lean 4 source, lean/DataMiningAsObservation/NullModel.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
NullModel.lean. Chapter 0 section 0.9 and chapter 8 section 8.4, the null model.
A null model keeps everything about the data except the structure under test. The label
permutation null is the case the book uses most. Permuting the labels keeps every class
size and keeps the bag of scores, so any statistic that reads only the class sizes or only
the scores is unchanged by it, and only a statistic that reads the pairing between score
and label can move. That is what makes the permuted AUROC the right comparison for a
measured AUROC, and why a number without its null is not yet a finding.
-/
import Mathlib
namespace DMO.NullModel
open Finset
variable {ι : Type*} [Fintype ι] [DecidableEq ι]
/-- The labels after a permutation `π` of the rows. -/
def permuted (y : ι → Bool) (π : Equiv.Perm ι) : ι → Bool := fun i => y (π i)
omit [DecidableEq ι] in
/-- **A permutation null keeps every class size.** -/
theorem card_pos_perm (y : ι → Bool) (π : Equiv.Perm ι) :
(Finset.univ.filter fun i => permuted y π i = true).card
= (Finset.univ.filter fun i => y i = true).card := by
unfold permuted
exact Finset.card_equiv π (by simp)
omit [DecidableEq ι] in
/-- **A permutation null keeps the bag of scores.** The multiset of scores is unchanged when
the rows are permuted. -/
theorem scores_perm (s : ι → ℝ) (π : Equiv.Perm ι) :
(Finset.univ.val.map fun i => s (π i)) = Finset.univ.val.map s := by
rw [show (fun i => s (π i)) = s ∘ π from rfl, ← Multiset.map_map, Multiset.map_univ_val_equiv]
omit [DecidableEq ι] in
/-- **A statistic of the class sizes alone is constant under the null.** -/
theorem stat_of_counts_const (y : ι → Bool) (π : Equiv.Perm ι) (f : ℕ → ℝ) :
f (Finset.univ.filter fun i => permuted y π i = true).card
= f (Finset.univ.filter fun i => y i = true).card := by
rw [card_pos_perm]
/-- **The measured statistic minus its null mean** is zero exactly when the measurement is
at the null, which is the excess a chapter reports. -/
theorem excess_eq_zero_iff (stat nullMean : ℝ) : stat - nullMean = 0 ↔ stat = nullMean :=
sub_eq_zero
end DMO.NullModel