StandardError.lean
Lean 4 source, lean/DataMiningAsObservation/StandardError.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
StandardError.lean. Chapter 0 section 0.9, the standard error.
The standard error of a mean over `n` independent draws with spread `σ` is `σ / √n`. It is
positive, it falls as `n` grows, halving it needs four times the samples, and it tends to
zero. That is the arithmetic behind the sample sizes the book asks for, and the reason
the Nadeau and Bengio correction matters, since dependent draws do not shrink it this way.
-/
import Mathlib
namespace DMO.StandardError
open Filter Topology
/-- The standard error of a mean over `n` independent draws with spread `σ`. -/
noncomputable def se (σ : ℝ) (n : ℕ) : ℝ := σ / Real.sqrt n
/-- The standard error is positive for positive spread and at least one draw. -/
theorem se_pos (σ : ℝ) (hσ : 0 < σ) (n : ℕ) (hn : 0 < n) : 0 < se σ n := by
unfold se
have : (0 : ℝ) < n := by exact_mod_cast hn
positivity
/-- **Halving the standard error needs four times the draws.** -/
theorem se_quarter (σ : ℝ) (n : ℕ) (hn : 0 < n) : se σ (4 * n) = se σ n / 2 := by
unfold se
have hn' : (0 : ℝ) < n := by exact_mod_cast hn
push_cast
rw [Real.sqrt_mul (by norm_num), show Real.sqrt 4 = 2 by
rw [show (4 : ℝ) = 2 ^ 2 by norm_num, Real.sqrt_sq (by norm_num)]]
field_simp
/-- **More draws never raise the standard error.** -/
theorem se_antitone (σ : ℝ) (hσ : 0 ≤ σ) (n : ℕ) (hn : 0 < n) : se σ (n + 1) ≤ se σ n := by
unfold se
have hn' : (0 : ℝ) < n := by exact_mod_cast hn
apply div_le_div_of_nonneg_left hσ (Real.sqrt_pos.2 hn')
apply Real.sqrt_le_sqrt
push_cast
linarith
/-- **The standard error tends to zero.** -/
theorem se_tendsto_zero (σ : ℝ) : Tendsto (fun n : ℕ => se σ n) atTop (𝓝 0) := by
unfold se
have h : Tendsto (fun n : ℕ => Real.sqrt (n : ℝ)) atTop atTop :=
Real.tendsto_sqrt_atTop.comp tendsto_natCast_atTop_atTop
exact Tendsto.div_atTop tendsto_const_nhds h
/-- **The chapter 8 numbers.** A thousand folds shrink the naive standard error by a factor
close to 31.6, and the corrected one by 15.84 less. -/
theorem thousand_folds : |Real.sqrt 1000 - 31.62| < 0.01 := by
have h1 : (31.61 : ℝ) < Real.sqrt 1000 := by rw [Real.lt_sqrt (by norm_num)]; norm_num
have h2 : Real.sqrt 1000 < (31.63 : ℝ) := by rw [Real.sqrt_lt' (by norm_num)]; norm_num
rw [abs_lt]; constructor <;> linarith
end DMO.StandardError