Standardization.lean
Lean 4 source, lean/DataMiningAsObservation/Standardization.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Standardization.lean. Chapter 2 section 2.5, standardization.
Standardization subtracts a column's mean and divides by its spread. The standardized
column has weighted mean zero and weighted variance one, the transform is invertible, and
it preserves every ordering of the rows, so it changes the reader's geometry and not what
the table holds.
-/
import Mathlib
namespace DMO.Standardization
open Finset
variable {Ω : Type*} [Fintype Ω]
/-- The standardized value of row `ω`. -/
noncomputable def standardize (x : Ω → ℝ) (μ σ : ℝ) (ω : Ω) : ℝ := (x ω - μ) / σ
/-- **The standardized column has mean zero.** -/
theorem mean_zero (w x : Ω → ℝ) (μ σ : ℝ) (hw : ∑ ω, w ω = 1) (hμ : ∑ ω, w ω * x ω = μ) :
∑ ω, w ω * standardize x μ σ ω = 0 := by
unfold standardize
calc ∑ ω, w ω * ((x ω - μ) / σ) = ∑ ω, (w ω * x ω - μ * w ω) / σ :=
Finset.sum_congr rfl fun ω _ => by ring
_ = (∑ ω, w ω * x ω - μ * ∑ ω, w ω) / σ := by
rw [← Finset.sum_div, Finset.sum_sub_distrib, Finset.mul_sum]
_ = 0 := by rw [hμ, hw, mul_one, sub_self, zero_div]
/-- **The standardized column has variance one.** -/
theorem variance_one (w x : Ω → ℝ) (μ σ : ℝ) (hσ : σ ≠ 0)
(hvar : ∑ ω, w ω * (x ω - μ) ^ 2 = σ ^ 2) :
∑ ω, w ω * standardize x μ σ ω ^ 2 = 1 := by
unfold standardize
calc ∑ ω, w ω * ((x ω - μ) / σ) ^ 2 = (∑ ω, w ω * (x ω - μ) ^ 2) / σ ^ 2 := by
rw [Finset.sum_div]
refine Finset.sum_congr rfl fun ω _ => ?_
rw [div_pow]
ring
_ = 1 := by rw [hvar, div_self (pow_ne_zero 2 hσ)]
omit [Fintype Ω] in
/-- **Standardization is invertible.** -/
theorem standardize_inv (x : Ω → ℝ) (μ σ : ℝ) (hσ : σ ≠ 0) (ω : Ω) :
standardize x μ σ ω * σ + μ = x ω := by
unfold standardize
rw [div_mul_cancel₀ _ hσ, sub_add_cancel]
omit [Fintype Ω] in
/-- **Standardization preserves every ordering.** -/
theorem standardize_lt_iff (x : Ω → ℝ) (μ σ : ℝ) (hσ : 0 < σ) (a b : Ω) :
standardize x μ σ a < standardize x μ σ b ↔ x a < x b := by
unfold standardize
rw [div_lt_div_iff_of_pos_right hσ]
constructor <;> intro h <;> linarith
end DMO.Standardization