SpectralEmbedding.lean

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

/-
SpectralEmbedding.lean. Chapter 3 section 3.3 and chapter 9 section 9.1, the spectral
embedding.

The spectral embedding gives each node its values in the low eigenvectors of the graph
Laplacian. Chapter 3 shows its magnitude is degree noise and its angle keeps the geodesics,
and row normalization, the step spectral clustering takes before clustering, is the
projection onto the unit sphere. The file states that the normalized row has unit length,
that the dot product of two normalized rows is the cosine of the originals, and that a
positive rescaling of a row does not change its normalized form.
-/
import Mathlib

namespace DMO.SpectralEmbedding

variable {n : ℕ}

/-- Row normalization, the projection of an embedded row onto the unit sphere. -/
noncomputable def rowNormalize (v : Fin n → ℝ) : Fin n → ℝ := (1 / Real.sqrt (v ⬝ᵥ v)) • v

theorem dot_self_nonneg (v : Fin n → ℝ) : 0 ≤ v ⬝ᵥ v :=
  Finset.sum_nonneg fun i _ => mul_self_nonneg (v i)

/-- **A normalized row has unit length.** -/
theorem rowNormalize_unit (v : Fin n → ℝ) (hv : v ⬝ᵥ v ≠ 0) :
    rowNormalize v ⬝ᵥ rowNormalize v = 1 := by
  unfold rowNormalize
  rw [smul_dotProduct, dotProduct_smul, smul_eq_mul, smul_eq_mul,
    show (1 / Real.sqrt (v ⬝ᵥ v)) * ((1 / Real.sqrt (v ⬝ᵥ v)) * (v ⬝ᵥ v))
      = (v ⬝ᵥ v) / (Real.sqrt (v ⬝ᵥ v) * Real.sqrt (v ⬝ᵥ v)) by ring,
    Real.mul_self_sqrt (dot_self_nonneg v), div_self hv]

/-- **The dot product of two normalized rows is the cosine of the originals.** -/
theorem dot_rowNormalize (u v : Fin n → ℝ) :
    rowNormalize u ⬝ᵥ rowNormalize v
      = (u ⬝ᵥ v) / (Real.sqrt (u ⬝ᵥ u) * Real.sqrt (v ⬝ᵥ v)) := by
  unfold rowNormalize
  rw [smul_dotProduct, dotProduct_smul, smul_eq_mul, smul_eq_mul]
  ring

/-- **A positive rescaling of a row does not change its normalized form.** -/
theorem rowNormalize_smul (c : ℝ) (hc : 0 < c) (v : Fin n → ℝ) (hv : v ⬝ᵥ v ≠ 0) :
    rowNormalize (c • v) = rowNormalize v := by
  unfold rowNormalize
  have hs : Real.sqrt (v ⬝ᵥ v) ≠ 0 :=
    Real.sqrt_ne_zero'.mpr (lt_of_le_of_ne (dot_self_nonneg v) (Ne.symm hv))
  rw [smul_dotProduct, dotProduct_smul, smul_eq_mul, smul_eq_mul,
    show c * (c * (v ⬝ᵥ v)) = c ^ 2 * (v ⬝ᵥ v) by ring,
    Real.sqrt_mul (sq_nonneg c), Real.sqrt_sq hc.le, smul_smul]
  congr 1
  rw [div_mul_eq_mul_div, one_mul, div_eq_div_iff (mul_ne_zero hc.ne' hs) hs]
  ring

end DMO.SpectralEmbedding