Hierarchical.lean

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

/-
Hierarchical.lean. Chapter 9 section 9.1, hierarchical clustering.

Hierarchical clustering merges the closest pair of clusters at each step, under a linkage
that reads the nearest pair, the farthest pair, or the average. The file states the
Lance–Williams updates for the distance from a merged cluster to a third, and that when
the merged pair was the closest at height `h`, no updated distance falls below `h`, so the
merge heights of the dendrogram never decrease. Single linkage is never above complete,
and average linkage lies between them.
-/
import Mathlib

namespace DMO.Hierarchical

/-- Single linkage after merging `A` and `B`: the nearer of the two distances to `C`. -/
noncomputable def single (dAC dBC : ℝ) : ℝ := min dAC dBC

/-- Complete linkage: the farther of the two. -/
noncomputable def complete (dAC dBC : ℝ) : ℝ := max dAC dBC

/-- Average linkage: the size-weighted mean. -/
noncomputable def average (nA nB dAC dBC : ℝ) : ℝ := (nA * dAC + nB * dBC) / (nA + nB)

/-- **Single-link merge heights never decrease.** -/
theorem single_ge (h dAC dBC : ℝ) (h1 : h ≤ dAC) (h2 : h ≤ dBC) : h ≤ single dAC dBC :=
  le_min h1 h2

/-- **Complete-link merge heights never decrease.** -/
theorem complete_ge (h dAC dBC : ℝ) (h1 : h ≤ dAC) : h ≤ complete dAC dBC :=
  le_trans h1 (le_max_left _ _)

/-- **Average-link merge heights never decrease.** -/
theorem average_ge (nA nB : ℝ) (hA : 0 < nA) (hB : 0 < nB) (h dAC dBC : ℝ) (h1 : h ≤ dAC)
    (h2 : h ≤ dBC) : h ≤ average nA nB dAC dBC := by
  unfold average
  rw [le_div_iff₀ (by positivity)]
  nlinarith [mul_le_mul_of_nonneg_left h1 hA.le, mul_le_mul_of_nonneg_left h2 hB.le]

/-- **Single linkage is never above complete linkage.** -/
theorem single_le_complete (dAC dBC : ℝ) : single dAC dBC ≤ complete dAC dBC :=
  min_le_max

/-- **Average linkage lies between single and complete.** -/
theorem average_between (nA nB : ℝ) (hA : 0 < nA) (hB : 0 < nB) (dAC dBC : ℝ) :
    single dAC dBC ≤ average nA nB dAC dBC ∧ average nA nB dAC dBC ≤ complete dAC dBC := by
  refine ⟨average_ge nA nB hA hB _ _ _ (min_le_left _ _) (min_le_right _ _), ?_⟩
  unfold average complete
  rw [div_le_iff₀ (by positivity)]
  nlinarith [mul_le_mul_of_nonneg_left (le_max_left dAC dBC) hA.le,
    mul_le_mul_of_nonneg_left (le_max_right dAC dBC) hB.le]

end DMO.Hierarchical