Recognizer.lean

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

/-
Recognizer.lean. Equation 0.32, the template match of the recognizer.

The recognizer compares the measured list of eigenvalue ratios to a stored list for each
candidate shape by the root-mean-square distance between the logarithms. That distance is
zero between a list and itself, symmetric, and unchanged when both lists are scaled by the
same positive factor, which is why the match names a shape and not a size.
-/
import Mathlib

namespace DMO.Recognizer

open Finset

variable {n : ℕ}

/-- **Equation 0.32.** The template distance between a measured list `m` and a template `t`. -/
noncomputable def templateDist (m t : Fin n → ℝ) : ℝ :=
  Real.sqrt ((1 / n) * ∑ k, (Real.log (m k) - Real.log (t k)) ^ 2)

/-- A list is at distance zero from itself. -/
theorem templateDist_self (m : Fin n → ℝ) : templateDist m m = 0 := by
  simp [templateDist]

/-- The distance is symmetric. -/
theorem templateDist_comm (m t : Fin n → ℝ) : templateDist m t = templateDist t m := by
  unfold templateDist
  congr 2
  exact Finset.sum_congr rfl fun k _ => by ring

/-- **Scale invariance.** Scaling both lists by the same positive factor leaves the distance
unchanged, so the recognizer reads shape and not size. -/
theorem templateDist_scale (m t : Fin n → ℝ) (c : ℝ) (hc : 0 < c) (hm : ∀ k, 0 < m k)
    (ht : ∀ k, 0 < t k) :
    templateDist (fun k => c * m k) (fun k => c * t k) = templateDist m t := by
  unfold templateDist
  congr 2
  refine Finset.sum_congr rfl fun k _ => ?_
  rw [Real.log_mul hc.ne' (hm k).ne', Real.log_mul hc.ne' (ht k).ne']
  ring

/-- The distance is nonnegative. -/
theorem templateDist_nonneg (m t : Fin n → ℝ) : 0 ≤ templateDist m t :=
  Real.sqrt_nonneg _

end DMO.Recognizer