Dbscan.lean
Lean 4 source, lean/DataMiningAsObservation/Dbscan.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Dbscan.lean. Chapter 9 section 9.1, DBSCAN.
DBSCAN calls a point a core point when at least `minPts` points lie within `ε` of it,
grows a cluster by reachability from core points, and leaves the rest as noise. A wider
radius or a smaller count makes more core points, every reachability chain starts at a
core point, and a point that is not a core point reaches nothing.
-/
import Mathlib
namespace DMO.Dbscan
open Finset
variable {P : Type*} [Fintype P]
open Classical in
/-- The points within `ε` of `p`. -/
noncomputable def ball (dist : P → P → ℝ) (ε : ℝ) (p : P) : Finset P :=
univ.filter fun q => dist p q ≤ ε
/-- A core point has at least `minPts` points within `ε`. -/
def IsCore (dist : P → P → ℝ) (ε : ℝ) (minPts : ℕ) (p : P) : Prop :=
minPts ≤ (ball dist ε p).card
/-- `q` is directly reachable from the core point `p`. -/
def DirectReach (dist : P → P → ℝ) (ε : ℝ) (minPts : ℕ) (p q : P) : Prop :=
IsCore dist ε minPts p ∧ dist p q ≤ ε
/-- Reachability, a chain of direct reaches. -/
def Reach (dist : P → P → ℝ) (ε : ℝ) (minPts : ℕ) : P → P → Prop :=
Relation.TransGen (DirectReach dist ε minPts)
/-- **A wider radius holds more points.** -/
theorem ball_mono (dist : P → P → ℝ) {ε ε' : ℝ} (h : ε ≤ ε') (p : P) :
ball dist ε p ⊆ ball dist ε' p := by
intro q hq
simp only [ball, Finset.mem_filter] at hq ⊢
exact ⟨hq.1, le_trans hq.2 h⟩
/-- **A core point at one radius is a core point at any wider radius.** -/
theorem core_mono (dist : P → P → ℝ) {ε ε' : ℝ} (h : ε ≤ ε') {minPts : ℕ} {p : P}
(hp : IsCore dist ε minPts p) : IsCore dist ε' minPts p :=
le_trans hp (Finset.card_le_card (ball_mono dist h p))
/-- **A core point at one count is a core point at any smaller count.** -/
theorem core_anti (dist : P → P → ℝ) (ε : ℝ) {m m' : ℕ} (h : m' ≤ m) {p : P}
(hp : IsCore dist ε m p) : IsCore dist ε m' p :=
le_trans h hp
/-- **Every reachability chain starts at a core point.** -/
theorem reach_from_core (dist : P → P → ℝ) (ε : ℝ) (minPts : ℕ) {p q : P}
(h : Reach dist ε minPts p q) : IsCore dist ε minPts p := by
induction h with
| single h => exact h.1
| tail _ _ ih => exact ih
/-- **Noise reaches nothing.** -/
theorem noise_unreachable (dist : P → P → ℝ) (ε : ℝ) (minPts : ℕ) {p q : P}
(hp : ¬ IsCore dist ε minPts p) : ¬ Reach dist ε minPts p q :=
fun h => hp (reach_from_core dist ε minPts h)
end DMO.Dbscan