Hubness.lean

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

/-
Hubness.lean. Equations 10.3 and 0.17.

A row's k-occurrence count is the number of queries whose top-k list contains it. Summed over
the corpus, the counts total the number of slots handed out, k per query, so the mean count
is `k |Q| / n`, the Poisson mean of equation 0.17. The count is a function of the query set,
which is the sense in which hubness is a query property. A row with count zero is an
anti-hub.
-/
import Mathlib

namespace DMO.Hubness

open Finset

variable {ι α : Type*} [DecidableEq α]

/-- **Equation 10.3.** The k-occurrence count of a row `x` under the queries `Q`, each query
`q` retrieving the list `top q`. -/
def count (Q : Finset ι) (top : ι → Finset α) (x : α) : ℕ :=
  (Q.filter fun q => x ∈ top q).card

/-- An anti-hub is a row no query retrieves. -/
def antiHub (Q : Finset ι) (top : ι → Finset α) (x : α) : Prop := count Q top x = 0

/-- The counts over a corpus `X` sum to the slots handed out inside `X`. -/
theorem sum_count (Q : Finset ι) (top : ι → Finset α) (X : Finset α) :
    ∑ x ∈ X, count Q top x = ∑ q ∈ Q, (top q ∩ X).card := by
  unfold count
  simp only [Finset.card_filter]
  rw [Finset.sum_comm]
  refine Finset.sum_congr rfl fun q _ => ?_
  rw [Finset.sum_ite_mem, Finset.card_eq_sum_ones, Finset.inter_comm]

/-- **The mean count is the Poisson mean of equation 0.17.** When every query retrieves `k`
rows of the corpus, the counts sum to `k |Q|`, so their mean over `n` rows is `k |Q| / n`. -/
theorem sum_count_eq (Q : Finset ι) (top : ι → Finset α) (X : Finset α) (k : ℕ)
    (hk : ∀ q ∈ Q, (top q).card = k) (hX : ∀ q ∈ Q, top q ⊆ X) :
    ∑ x ∈ X, count Q top x = k * Q.card := by
  rw [sum_count]
  rw [Finset.sum_congr rfl fun q hq => by rw [Finset.inter_eq_left.2 (hX q hq), hk q hq]]
  simp [mul_comm]

/-- **Hubness is a query property.** The count depends on the queries alone once the retrieval
rule is fixed. Two query sets that retrieve the same lists give every row the same count. -/
theorem count_congr (Q : Finset ι) (top top' : ι → Finset α) (h : ∀ q ∈ Q, top q = top' q)
    (x : α) : count Q top x = count Q top' x := by
  unfold count
  congr 1
  exact Finset.filter_congr fun q hq => by rw [h q hq]

/-- A row retrieved by no query has count zero, and one retrieved by some query does not. -/
theorem antiHub_iff (Q : Finset ι) (top : ι → Finset α) (x : α) :
    antiHub Q top x ↔ ∀ q ∈ Q, x ∉ top q := by
  unfold antiHub count
  rw [Finset.card_eq_zero, Finset.filter_eq_empty_iff]

end DMO.Hubness