Skewness.lean
Lean 4 source, lean/DataMiningAsObservation/Skewness.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Skewness.lean. Chapter 3 section 3.5 and chapter 10 section 10.2, the skewness.
The skewness of a column is the weighted mean cubed deviation from the mean over the cube
of the spread. It is unchanged by a shift and by a positive rescaling, it changes sign when
the column is negated, and it is zero for a column symmetric about its mean. The hubness
gate reads the skewness of the neighbour-count distribution.
-/
import Mathlib
namespace DMO.Skewness
open Finset
variable {Ω : Type*} [Fintype Ω]
/-- The skewness about `μ` in units of `σ`. -/
noncomputable def skew (w x : Ω → ℝ) (μ σ : ℝ) : ℝ := (∑ ω, w ω * (x ω - μ) ^ 3) / σ ^ 3
/-- **A shift does not change the skewness.** -/
theorem skew_shift (w x : Ω → ℝ) (μ σ c : ℝ) :
skew w (fun ω => x ω + c) (μ + c) σ = skew w x μ σ := by
unfold skew
congr 1
exact Finset.sum_congr rfl fun ω _ => by ring
/-- **A positive rescaling does not change the skewness.** -/
theorem skew_scale (w x : Ω → ℝ) (μ σ c : ℝ) (hc : 0 < c) :
skew w (fun ω => c * x ω) (c * μ) (c * σ) = skew w x μ σ := by
unfold skew
have h : ∑ ω, w ω * (c * x ω - c * μ) ^ 3 = c ^ 3 * ∑ ω, w ω * (x ω - μ) ^ 3 := by
rw [Finset.mul_sum]
exact Finset.sum_congr rfl fun ω _ => by ring
rw [h, mul_pow, mul_div_mul_left _ _ (pow_ne_zero 3 hc.ne')]
/-- **Negating the column negates the skewness.** -/
theorem skew_neg (w x : Ω → ℝ) (μ σ : ℝ) :
skew w (fun ω => -x ω) (-μ) σ = -skew w x μ σ := by
unfold skew
rw [← neg_div, ← Finset.sum_neg_distrib]
congr 1
exact Finset.sum_congr rfl fun ω _ => by ring
/-- **A column symmetric about its mean has zero skewness.** With `τ` a weight-preserving
pairing of rows that reflects each deviation. -/
theorem skew_symmetric (w x : Ω → ℝ) (μ σ : ℝ) (τ : Ω ≃ Ω) (hw : ∀ ω, w (τ ω) = w ω)
(hx : ∀ ω, x (τ ω) - μ = -(x ω - μ)) : skew w x μ σ = 0 := by
unfold skew
have h : ∑ ω, w ω * (x ω - μ) ^ 3 = -∑ ω, w ω * (x ω - μ) ^ 3 := by
conv_lhs => rw [← Equiv.sum_comp τ]
rw [← Finset.sum_neg_distrib]
refine Finset.sum_congr rfl fun ω _ => ?_
rw [hw, hx]
ring
have h0 : ∑ ω, w ω * (x ω - μ) ^ 3 = 0 := by linarith
rw [h0, zero_div]
end DMO.Skewness