Kendall.lean

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

/-
Kendall.lean. Equation 0.15, the Kendall correlation.

Two rankings of the same items agree on a pair when they order it the same way and disagree
when they order it opposite ways. The Kendall correlation is the number of agreeing pairs
minus the number of disagreeing pairs over the number of pairs. It lies between minus one
and one, it is one exactly when no pair disagrees, and since it depends only on the
orderings, a strictly increasing transform of either score leaves it unchanged. It is the
statistic the rank certificate reports.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance

namespace DMO.Kendall

open Finset

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

/-- A pair the two scores order the same way. -/
def concordant (s t : ι → ℝ) (p : ι × ι) : Prop :=
  (s p.1 < s p.2 ∧ t p.1 < t p.2) ∨ (s p.2 < s p.1 ∧ t p.2 < t p.1)

/-- A pair the two scores order opposite ways. -/
def discordant (s t : ι → ℝ) (p : ι × ι) : Prop :=
  (s p.1 < s p.2 ∧ t p.2 < t p.1) ∨ (s p.2 < s p.1 ∧ t p.1 < t p.2)

noncomputable instance (s t : ι → ℝ) : DecidablePred (concordant s t) := fun _ => by
  unfold concordant; infer_instance

noncomputable instance (s t : ι → ℝ) : DecidablePred (discordant s t) := fun _ => by
  unfold discordant; infer_instance

/-- The ordered pairs of distinct items. Each unordered pair appears twice, once in each
order, which cancels in the ratio. -/
def pairs : Finset (ι × ι) := Finset.univ.filter fun p => p.1 ≠ p.2

/-- **Equation 0.15.** The Kendall correlation of the scores `s` and `t`. -/
noncomputable def tau (s t : ι → ℝ) : ℝ :=
  (((pairs.filter (concordant s t)).card : ℝ) - (pairs.filter (discordant s t)).card) /
    (pairs : Finset (ι × ι)).card

omit [Fintype ι] [DecidableEq ι] in
/-- No pair is both concordant and discordant. -/
theorem not_both (s t : ι → ℝ) (p : ι × ι) : ¬ (concordant s t p ∧ discordant s t p) := by
  unfold concordant discordant
  rintro ⟨h1 | h1, h2 | h2⟩ <;> linarith [h1.1, h1.2, h2.1, h2.2]

/-- The concordant and discordant counts together do not exceed the pair count. -/
theorem card_add_le (s t : ι → ℝ) :
    (pairs.filter (concordant s t)).card + (pairs.filter (discordant s t)).card
      ≤ (pairs : Finset (ι × ι)).card := by
  rw [← Finset.card_union_of_disjoint]
  · exact Finset.card_le_card (Finset.union_subset (Finset.filter_subset _ _) (Finset.filter_subset _ _))
  · rw [Finset.disjoint_filter]
    intro p _ h1 h2
    exact not_both s t p ⟨h1, h2⟩

/-- **The correlation lies between minus one and one.** -/
theorem tau_mem (s t : ι → ℝ) : -1 ≤ tau s t ∧ tau s t ≤ 1 := by
  unfold tau
  have h := card_add_le s t
  rcases Nat.eq_zero_or_pos (pairs : Finset (ι × ι)).card with h0 | h0
  · simp [h0]
  · have hp : (0 : ℝ) < (pairs : Finset (ι × ι)).card := by exact_mod_cast h0
    have h' : ((pairs.filter (concordant s t)).card : ℝ) + (pairs.filter (discordant s t)).card
        ≤ (pairs : Finset (ι × ι)).card := by exact_mod_cast h
    constructor
    · rw [le_div_iff₀ hp]
      have : (0 : ℝ) ≤ (pairs.filter (concordant s t)).card := by positivity
      linarith
    · rw [div_le_one hp]
      have : (0 : ℝ) ≤ (pairs.filter (discordant s t)).card := by positivity
      linarith

omit [Fintype ι] [DecidableEq ι] in
/-- Concordance depends on the orderings alone. -/
theorem concordant_comp {g : ℝ → ℝ} (hg : StrictMono g) (s t : ι → ℝ) (p : ι × ι) :
    concordant (g ∘ s) t p ↔ concordant s t p := by
  unfold concordant
  simp only [Function.comp_apply, hg.lt_iff_lt]

omit [Fintype ι] [DecidableEq ι] in
theorem discordant_comp {g : ℝ → ℝ} (hg : StrictMono g) (s t : ι → ℝ) (p : ι × ι) :
    discordant (g ∘ s) t p ↔ discordant s t p := by
  unfold discordant
  simp only [Function.comp_apply, hg.lt_iff_lt]

/-- **Monotone invariance.** A strictly increasing transform of a score leaves the Kendall
correlation unchanged. -/
theorem tau_comp {g : ℝ → ℝ} (hg : StrictMono g) (s t : ι → ℝ) : tau (g ∘ s) t = tau s t := by
  unfold tau
  rw [Finset.filter_congr fun p _ => concordant_comp hg s t p,
    Finset.filter_congr fun p _ => discordant_comp hg s t p]

omit [Fintype ι] [DecidableEq ι] in
/-- A score compared with itself has no discordant pair. -/
theorem discordant_self (s : ι → ℝ) (p : ι × ι) : ¬ discordant s s p := by
  unfold discordant
  rintro (h | h) <;> linarith [h.1, h.2]

end DMO.Kendall