CrossValidation.lean

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

/-
CrossValidation.lean. Chapter 8 section 8.5, cross-validation.

Cross-validation splits the rows into folds and evaluates on each fold a model trained on
the others, so every row is tested exactly once. When the folds have equal size, the mean
of the fold accuracies is the overall accuracy, and in general the overall accuracy is the
size-weighted mean of the fold accuracies. What the folds do not do is multiply
independent evidence, since they share training data, which is the Nadeau and Bengio
correction of the same chapter.
-/
import Mathlib

namespace DMO.CrossValidation

open Finset

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

/-- The accuracy of the decisions `pred` against the labels `y` on the rows `R`. -/
noncomputable def accuracy (R : Finset ι) (pred y : ι → Bool) : ℝ :=
  ((R.filter fun i => pred i = y i).card : ℝ) / R.card

/-- **Every row is tested exactly once** when the folds partition the rows. The fold sizes sum
to the row count. -/
theorem sizes_sum (F : Finset φ) (fold : φ → Finset ι) (R : Finset ι)
    (hcover : R = F.biUnion fold) (hdisj : (F : Set φ).PairwiseDisjoint fold) :
    ∑ f ∈ F, (fold f).card = R.card := by
  rw [hcover, Finset.card_biUnion hdisj]

/-- **The overall accuracy is the size-weighted mean of the fold accuracies.** -/
theorem accuracy_weighted (F : Finset φ) (fold : φ → Finset ι) (R : Finset ι)
    (hcover : R = F.biUnion fold) (hdisj : (F : Set φ).PairwiseDisjoint fold)
    (hne : ∀ f ∈ F, (fold f).Nonempty) (pred y : ι → Bool) :
    accuracy R pred y = ∑ f ∈ F, ((fold f).card / R.card : ℝ) * accuracy (fold f) pred y := by
  unfold accuracy
  have hfilt : R.filter (fun i => pred i = y i) = F.biUnion fun f => (fold f).filter fun i => pred i = y i := by
    rw [hcover, Finset.filter_biUnion]
  rw [hfilt, Finset.card_biUnion]
  · push_cast
    rw [Finset.sum_div]
    refine Finset.sum_congr rfl fun f hf => ?_
    have : ((fold f).card : ℝ) ≠ 0 := by exact_mod_cast (hne f hf).card_pos.ne'
    field_simp
  · intro a ha b hb hab
    exact Finset.disjoint_filter_filter (hdisj ha hb hab)

/-- **With equal folds the mean of the fold accuracies is the overall accuracy.** -/
theorem accuracy_mean_of_equal (F : Finset φ) (fold : φ → Finset ι) (R : Finset ι)
    (hcover : R = F.biUnion fold) (hdisj : (F : Set φ).PairwiseDisjoint fold) (s : ℕ) (hs : 0 < s)
    (hsize : ∀ f ∈ F, (fold f).card = s) (hF : F.Nonempty) (pred y : ι → Bool) :
    accuracy R pred y = (∑ f ∈ F, accuracy (fold f) pred y) / F.card := by
  have hne : ∀ f ∈ F, (fold f).Nonempty := fun f hf => by
    rw [← Finset.card_pos, hsize f hf]; exact hs
  rw [accuracy_weighted F fold R hcover hdisj hne pred y]
  have hR : R.card = F.card * s := by
    rw [← sizes_sum F fold R hcover hdisj, Finset.sum_congr rfl hsize, Finset.sum_const, smul_eq_mul]
  rw [Finset.sum_div]
  refine Finset.sum_congr rfl fun f hf => ?_
  rw [hsize f hf, hR]
  push_cast
  have hs' : (s : ℝ) ≠ 0 := by exact_mod_cast hs.ne'
  have hF' : (F.card : ℝ) ≠ 0 := by exact_mod_cast hF.card_pos.ne'
  field_simp

end DMO.CrossValidation