RecallAtK.lean
Lean 4 source, lean/DataMiningAsObservation/RecallAtK.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
RecallAtK.lean. Equation 11.3, recall at k.
Recall at k is the fraction of a query's true k nearest neighbours, computed from the
uncompressed vectors, that the index returned. It lies in the unit interval, it is one
exactly when the returned list is the true list, and the aggregate over queries is a
weighted mean over strata, so a stratum that fails entirely moves the aggregate by no more
than its weight. With a stratum of weight one in twenty at recall zero and every other
stratum perfect, the aggregate is 0.95.
-/
import Mathlib
namespace DMO.RecallAtK
open Finset
variable {α : Type*} [DecidableEq α]
/-- **Equation 11.3.** Recall at `k` of the returned list against the true list. -/
noncomputable def recallAtK (k : ℕ) (returned truth : Finset α) : ℝ := ((returned ∩ truth).card : ℝ) / k
/-- Recall lies in the unit interval when the true list has `k` rows. -/
theorem recallAtK_mem_unit (k : ℕ) (hk : 0 < k) (returned truth : Finset α) (ht : truth.card = k) :
0 ≤ recallAtK k returned truth ∧ recallAtK k returned truth ≤ 1 := by
unfold recallAtK
have hk' : (0 : ℝ) < k := by exact_mod_cast hk
refine ⟨by positivity, ?_⟩
rw [div_le_one hk']
have : (returned ∩ truth).card ≤ truth.card := Finset.card_le_card Finset.inter_subset_right
exact_mod_cast this.trans_eq ht
/-- **Recall is one exactly when the index returned the true list**, both lists having `k`
rows. -/
theorem recallAtK_eq_one_iff (k : ℕ) (hk : 0 < k) (returned truth : Finset α)
(hr : returned.card = k) (ht : truth.card = k) :
recallAtK k returned truth = 1 ↔ returned = truth := by
unfold recallAtK
have hk' : (k : ℝ) ≠ 0 := by exact_mod_cast hk.ne'
rw [div_eq_one_iff_eq hk']
constructor
· intro h
have hcard : (returned ∩ truth).card = returned.card := by
rw [hr]; exact_mod_cast h
have hsub : returned ⊆ truth :=
Finset.inter_eq_left.1 (Finset.eq_of_subset_of_card_le Finset.inter_subset_left hcard.ge)
exact Finset.eq_of_subset_of_card_le hsub (by rw [hr, ht])
· intro h
rw [h, Finset.inter_self, ht]
/-- The aggregate recall over strata, a weighted mean. -/
noncomputable def aggregate {σ : Type*} (S : Finset σ) (w r : σ → ℝ) : ℝ := ∑ s ∈ S, w s * r s
/-- **A failing stratum moves the aggregate by at most its weight.** With every other stratum
at most one, the aggregate is at most one minus the failing stratum's weight. -/
theorem aggregate_le_of_failing {σ : Type*} [DecidableEq σ] (S : Finset σ) (w r : σ → ℝ)
(hw : ∀ s ∈ S, 0 ≤ w s) (hsum : ∑ s ∈ S, w s = 1) (hr : ∀ s ∈ S, r s ≤ 1) (a : σ)
(ha : a ∈ S) (hfail : r a = 0) : aggregate S w r ≤ 1 - w a := by
unfold aggregate
rw [← Finset.add_sum_erase S _ ha, hfail, mul_zero, zero_add]
have : ∑ s ∈ S.erase a, w s = 1 - w a := by
rw [← hsum, ← Finset.add_sum_erase S _ ha]; ring
rw [← this]
apply Finset.sum_le_sum
intro s hs
have hs' := Finset.mem_of_mem_erase hs
calc w s * r s ≤ w s * 1 := mul_le_mul_of_nonneg_left (hr s hs') (hw s hs')
_ = w s := mul_one _
/-- **The chapter's point in numbers.** A stratum of weight one in twenty at recall zero and
every other stratum perfect give aggregate recall 0.95. -/
theorem aggregate_example : (0.95 : ℝ) * 1 + 0.05 * 0 = 0.95 := by norm_num
end DMO.RecallAtK