RankCertificate.lean

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

/-
RankCertificate.lean. Equation 11.2, the strict setting.

The certificate records the range of the ratio of compressed to exact distance over its
anchor pairs, `r_min` to `r_max`, and `κ = r_max / r_min`. Within that range, a query's
nearest neighbour is preserved whenever the runner-up is more than `κ` times as far in the
exact metric, because the compressed distance to the neighbour is at most `r_max` times the
exact one and the compressed distance to the runner-up is at least `r_min` times its exact
one. This is the per-pair fact the strict floor is built from, and it holds for every pair
whose ratio lies in the recorded range. It says nothing about a pair whose ratio falls
outside it, which is why the percentile setting is an estimate and not a floor.
-/
import Mathlib

namespace DMO.RankCertificate

/-- **Nearest neighbour preserved under a bounded distance ratio.** With the exact distances
`da` to the neighbour and `db` to the runner-up, the compressed distances `ca` and `cb`
within the recorded ratio range, and `db > κ da` where `κ = rmax / rmin`, the compressed
ranking agrees with the exact one. -/
theorem nn_preserved (rmin rmax da db ca cb : ℝ) (hr : 0 < rmin)
    (hca : ca ≤ rmax * da) (hcb : rmin * db ≤ cb)
    (hsep : (rmax / rmin) * da < db) : ca < cb := by
  have h1 : rmax * da < rmin * db := by
    have := mul_lt_mul_of_pos_left hsep hr
    rwa [← mul_assoc, mul_div_cancel₀ _ hr.ne'] at this
  linarith

/-- The separation needed is `κ` itself, a number the certificate reports. A ratio range that
collapses to a point, `κ = 1`, preserves every strict ranking. -/
theorem nn_preserved_of_kappa_one (r da db ca cb : ℝ) (hr : 0 < r)
    (hca : ca ≤ r * da) (hcb : r * db ≤ cb) (hsep : da < db) : ca < cb :=
  nn_preserved r r da db ca cb hr hca hcb (by rw [div_self hr.ne', one_mul]; exact hsep)

/-- `κ` is at least one, so the certificate never asks for less than the exact separation. -/
theorem kappa_ge_one (rmin rmax : ℝ) (hr : 0 < rmin) (hrr : rmin ≤ rmax) : 1 ≤ rmax / rmin := by
  rw [le_div_iff₀ hr, one_mul]
  exact hrr

end DMO.RankCertificate