Silhouette.lean

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

/-
Silhouette.lean. Equation 0.30, the silhouette.

A row's silhouette is the difference between its mean distance to the nearest other
cluster and its mean distance to its own cluster, over the larger of the two. It lies
between minus one and one, it is positive exactly when the row is closer to its own cluster,
and scaling every distance by the same positive factor leaves it unchanged, so it reads
shape and not size, like the recognizer's template distance.
-/
import Mathlib

namespace DMO.Silhouette

/-- **Equation 0.30.** The silhouette of a row with mean own-cluster distance `a` and mean
nearest-other-cluster distance `b`. -/
noncomputable def silhouette (a b : ℝ) : ℝ := (b - a) / max a b

/-- **The silhouette lies between minus one and one** for positive distances. -/
theorem silhouette_mem (a b : ℝ) (ha : 0 < a) (hb : 0 < b) :
    -1 ≤ silhouette a b ∧ silhouette a b ≤ 1 := by
  unfold silhouette
  have hm : 0 < max a b := lt_max_of_lt_left ha
  constructor
  · rw [le_div_iff₀ hm]
    have := le_max_left a b
    have := le_max_right a b
    nlinarith
  · rw [div_le_one hm]
    have h2 := le_max_right a b
    linarith

/-- **A positive silhouette means the row is closer to its own cluster.** -/
theorem silhouette_pos_iff (a b : ℝ) (ha : 0 < a) : 0 < silhouette a b ↔ a < b := by
  unfold silhouette
  have hm : 0 < max a b := lt_max_of_lt_left ha
  rw [div_pos_iff_of_pos_right hm]
  exact sub_pos

/-- **Scale invariance.** Scaling every distance by the same positive factor leaves the
silhouette unchanged. -/
theorem silhouette_scale (a b c : ℝ) (hc : 0 < c) (ha : 0 < a) :
    silhouette (c * a) (c * b) = silhouette a b := by
  unfold silhouette
  rw [← mul_max_of_nonneg a b hc.le]
  have hm : max a b ≠ 0 := (lt_max_of_lt_left ha).ne'
  field_simp

/-- A row equidistant from its own cluster and the nearest other has silhouette zero. -/
theorem silhouette_self (a : ℝ) : silhouette a a = 0 := by
  simp [silhouette]

end DMO.Silhouette