NadeauBengio.lean
Lean 4 source, lean/DataMiningAsObservation/NadeauBengio.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
NadeauBengio.lean. Equations 0.18 and 8.2, the Nadeau and Bengio correction.
Repeated cross-validation folds share training data, so the naive variance of a mean
difference, the per-fold variance over the fold count, understates the truth. The
correction multiplies the per-fold variance by one over the fold count plus the test-to-train
ratio, which is the naive variance times one plus the fold count times the ratio. The
corrected variance is never below the naive one, the inflation grows without bound in the
fold count, and with the chapter's numbers, a thousand folds at a ratio of one quarter, the
inflation is 251, the standard-error inflation is 15.84 to two decimals, and the effective
fold count is 3.98 to two decimals.
-/
import Mathlib
namespace DMO.NadeauBengio
/-- The naive variance of a mean over `J` folds. -/
noncomputable def naiveVar (J : ℕ) (σ2 : ℝ) : ℝ := σ2 / J
/-- **Equation 0.18.** The corrected variance. -/
noncomputable def correctedVar (J : ℕ) (ratio σ2 : ℝ) : ℝ := (1 / J + ratio) * σ2
/-- The corrected variance is the naive one times the inflation `1 + J ratio`. -/
theorem correctedVar_eq (J : ℕ) (hJ : 0 < J) (ratio σ2 : ℝ) :
correctedVar J ratio σ2 = (1 + J * ratio) * naiveVar J σ2 := by
unfold correctedVar naiveVar
have : (J : ℝ) ≠ 0 := by exact_mod_cast hJ.ne'
field_simp
/-- **The correction never shrinks the variance.** -/
theorem naiveVar_le_correctedVar (J : ℕ) (hJ : 0 < J) (ratio σ2 : ℝ) (hr : 0 ≤ ratio)
(hσ : 0 ≤ σ2) : naiveVar J σ2 ≤ correctedVar J ratio σ2 := by
rw [correctedVar_eq J hJ]
have h0 : 0 ≤ naiveVar J σ2 := by unfold naiveVar; positivity
nlinarith [mul_nonneg (Nat.cast_nonneg J : (0 : ℝ) ≤ J) hr]
/-- **More folds do not buy independence.** With a positive ratio the inflation exceeds any
bound once the fold count is large enough. -/
theorem inflation_unbounded (ratio : ℝ) (hr : 0 < ratio) (M : ℝ) :
∃ J : ℕ, M < 1 + J * ratio := by
obtain ⟨J, hJ⟩ := exists_nat_gt (M / ratio)
refine ⟨J, ?_⟩
have := (div_lt_iff₀ hr).1 hJ
linarith
/-- **The chapter's numbers.** A thousand folds at a ratio of one quarter inflate the variance
by 251, the standard error by 15.84 to two decimals, and leave 3.98 effective folds to two
decimals. -/
theorem book_numbers :
(1 + (1000 : ℝ) * (1 / 4)) = 251 ∧ |Real.sqrt 251 - 15.84| < 0.005 ∧
|(1000 : ℝ) / 251 - 3.98| < 0.005 := by
refine ⟨by norm_num, ?_, ?_⟩
· have h1 : (15.835 : ℝ) < Real.sqrt 251 := by
rw [Real.lt_sqrt (by norm_num)]; norm_num
have h2 : Real.sqrt 251 < (15.845 : ℝ) := by
rw [Real.sqrt_lt' (by norm_num)]; norm_num
rw [abs_lt]; constructor <;> linarith
· rw [abs_lt]; constructor <;> norm_num
end DMO.NadeauBengio