DistanceConcentration.lean

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

/-
DistanceConcentration.lean. Chapter 3 section 3.4, distance concentration.

When the squared distance between two rows is a sum of `d` independent coordinate
contributions, each with mean `m` and variance `v`, its mean is `d m` and its variance is
`d v`, so its relative spread, the variance over the squared mean, is `v / (d m²)`. That
quantity falls with the dimension and tends to zero, which is the sense in which the
distances concentrate and the reader runs out of resolution. The file checks the algebra of
the relative spread. The independence that gives the sum its mean and variance is the
model's assumption, and the book's chapter 3 says where real data depart from it.
-/
import Mathlib

namespace DMO.DistanceConcentration

open Filter Topology

/-- The mean of the squared distance over `d` coordinates. -/
def meanSq (d : ℕ) (m : ℝ) : ℝ := d * m

/-- The variance of the squared distance over `d` independent coordinates. -/
def varSq (d : ℕ) (v : ℝ) : ℝ := d * v

/-- The relative spread, variance over squared mean. -/
noncomputable def relSpread (d : ℕ) (m v : ℝ) : ℝ := varSq d v / meanSq d m ^ 2

/-- The relative spread in closed form. -/
theorem relSpread_eq (d : ℕ) (hd : 0 < d) (m v : ℝ) (hm : m ≠ 0) :
    relSpread d m v = v / (d * m ^ 2) := by
  unfold relSpread varSq meanSq
  have : (d : ℝ) ≠ 0 := by exact_mod_cast hd.ne'
  field_simp

/-- **The relative spread falls with the dimension.** -/
theorem relSpread_antitone (d : ℕ) (hd : 0 < d) (m v : ℝ) (hm : m ≠ 0) (hv : 0 ≤ v) :
    relSpread (d + 1) m v ≤ relSpread d m v := by
  rw [relSpread_eq d hd m v hm, relSpread_eq (d + 1) (Nat.succ_pos d) m v hm]
  have hm2 : 0 < m ^ 2 := by positivity
  apply div_le_div_of_nonneg_left hv (by positivity)
  have : (d : ℝ) ≤ (d + 1 : ℕ) := by push_cast; linarith
  nlinarith

/-- **The relative spread tends to zero**, so at large enough dimension every pair of rows
is at nearly the same distance. -/
theorem relSpread_tendsto_zero (m v : ℝ) :
    Tendsto (fun d : ℕ => v / (d * m ^ 2)) atTop (𝓝 0) := by
  have h : (fun d : ℕ => v / (d * m ^ 2)) = fun d : ℕ => (v / m ^ 2) / d := by
    funext d
    rw [div_div, mul_comm]
  rw [h]
  exact tendsto_const_div_atTop_nhds_zero_nat (v / m ^ 2)

/-- For any resolution there is a dimension beyond which the relative spread is below it. -/
theorem exists_dim_relSpread_lt (m v : ℝ) (hm : m ≠ 0) (hv : 0 ≤ v) (ε : ℝ) (hε : 0 < ε) :
    ∃ d : ℕ, 0 < d ∧ relSpread d m v < ε := by
  have hm2 : 0 < m ^ 2 := by positivity
  obtain ⟨d, hd⟩ := exists_nat_gt (v / (m ^ 2 * ε))
  have hdpos : 0 < d := by
    have : (0 : ℝ) ≤ v / (m ^ 2 * ε) := by positivity
    exact_mod_cast lt_of_le_of_lt this hd
  refine ⟨d, hdpos, ?_⟩
  rw [relSpread_eq d hdpos m v hm, div_lt_iff₀ (by positivity)]
  have := (div_lt_iff₀ (by positivity)).1 hd
  nlinarith

end DMO.DistanceConcentration