NearestNeighbour.lean

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

/-
NearestNeighbour.lean. Chapter 0 section 0.10, chapter 3 section 3.1, and chapter 6
section 6.1, the nearest neighbour.

A nearest neighbour under a distance is a row at minimal distance. A strictly increasing
transform of the distance names the same nearest neighbours, so a nearest-neighbour
classifier reads the distance's order and nothing else, a row is its own nearest neighbour
under a distance that is zero on the diagonal and nonnegative, and two nearest neighbours
sit at the same distance.
-/
import Mathlib

namespace DMO.NearestNeighbour

variable {ι : Type*}

/-- `j` is a nearest neighbour under the distances `d`. -/
def IsNearest (d : ι → ℝ) (j : ι) : Prop := ∀ k, d j ≤ d k

/-- **A strictly increasing transform of the distance names the same nearest neighbours.** -/
theorem nearest_comp (g : ℝ → ℝ) (hg : StrictMono g) (d : ι → ℝ) (j : ι) :
    IsNearest (g ∘ d) j ↔ IsNearest d j := by
  unfold IsNearest
  simp only [Function.comp]
  exact forall_congr' fun k => hg.le_iff_le

/-- **A positive rescaling of the distance names the same nearest neighbours.** -/
theorem nearest_smul (c : ℝ) (hc : 0 < c) (d : ι → ℝ) (j : ι) :
    IsNearest (fun k => c * d k) j ↔ IsNearest d j :=
  nearest_comp (fun t => c * t) (fun _ _ h => mul_lt_mul_of_pos_left h hc) d j

/-- **A row is its own nearest neighbour.** -/
theorem self_nearest (d : ι → ι → ℝ) (i : ι) (h0 : d i i = 0) (hnn : ∀ k, 0 ≤ d i k) :
    IsNearest (d i) i := fun k => by
  rw [h0]
  exact hnn k

/-- **Two nearest neighbours sit at the same distance.** -/
theorem nearest_same_distance (d : ι → ℝ) (j k : ι) (hj : IsNearest d j) (hk : IsNearest d k) :
    d j = d k :=
  le_antisymm (hj k) (hk j)

end DMO.NearestNeighbour