RobinHood.lean

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

/-
RobinHood.lean. Equation 0.35, the Robin Hood index.

The Robin Hood index of a list of counts is the fraction of the total that would have to
move from points above the mean to points below it to make every count equal. It is the
sum of the excesses over the mean, over the total, which equals half the sum of absolute
deviations over the total since the deviations sum to zero. It is nonnegative, zero
exactly when every count equals the mean, at most one, and unchanged when every count is
scaled by the same positive factor.
-/
import Mathlib

namespace DMO.RobinHood

open Finset

variable {ι : Type*}

/-- The mean count. -/
noncomputable def mean (S : Finset ι) (c : ι → ℝ) : ℝ := (∑ i ∈ S, c i) / S.card

/-- **Equation 0.35.** The Robin Hood index, the excess mass above the mean over the total. -/
noncomputable def robinHood (S : Finset ι) (c : ι → ℝ) : ℝ :=
  (∑ i ∈ S, max (c i - mean S c) 0) / ∑ i ∈ S, c i

/-- The deviations from the mean sum to zero. -/
theorem sum_dev (S : Finset ι) (hS : S.Nonempty) (c : ι → ℝ) : ∑ i ∈ S, (c i - mean S c) = 0 := by
  unfold mean
  rw [Finset.sum_sub_distrib, Finset.sum_const, nsmul_eq_mul]
  have : (S.card : ℝ) ≠ 0 := by exact_mod_cast hS.card_pos.ne'
  field_simp
  ring

/-- **The two forms agree.** The excess above the mean is half the total absolute deviation. -/
theorem excess_eq_half_abs (S : Finset ι) (hS : S.Nonempty) (c : ι → ℝ) :
    ∑ i ∈ S, max (c i - mean S c) 0 = (∑ i ∈ S, |c i - mean S c|) / 2 := by
  have h : ∀ t : ℝ, max t 0 = (t + |t|) / 2 := by
    intro t
    rcases le_or_gt 0 t with ht | ht
    · rw [max_eq_left ht, abs_of_nonneg ht]; ring
    · rw [max_eq_right ht.le, abs_of_neg ht]; ring
  simp_rw [h]
  rw [← Finset.sum_div, Finset.sum_add_distrib, sum_dev S hS c, zero_add]

/-- The index is nonnegative for nonnegative counts with positive total. -/
theorem robinHood_nonneg (S : Finset ι) (c : ι → ℝ) (hpos : 0 < ∑ i ∈ S, c i) :
    0 ≤ robinHood S c := by
  unfold robinHood
  exact div_nonneg (Finset.sum_nonneg fun i _ => le_max_right _ _) hpos.le

/-- **Zero exactly at equality.** With positive total, the index is zero exactly when no count
exceeds the mean, which with deviations summing to zero means every count equals it. -/
theorem robinHood_eq_zero_iff (S : Finset ι) (hS : S.Nonempty) (c : ι → ℝ) (hpos : 0 < ∑ i ∈ S, c i) :
    robinHood S c = 0 ↔ ∀ i ∈ S, c i = mean S c := by
  unfold robinHood
  rw [div_eq_zero_iff, or_iff_left hpos.ne',
    Finset.sum_eq_zero_iff_of_nonneg fun i _ => le_max_right _ _]
  constructor
  · intro h
    have hle : ∀ i ∈ S, c i ≤ mean S c := fun i hi => by
      have := h i hi
      rw [max_eq_right_iff] at this
      linarith
    intro i hi
    by_contra hne
    have hlt : c i < mean S c := lt_of_le_of_ne (hle i hi) hne
    have hsum := sum_dev S hS c
    have : ∑ j ∈ S, (c j - mean S c) < 0 := by
      apply Finset.sum_neg'
      · exact fun j hj => by linarith [hle j hj]
      · exact ⟨i, hi, by linarith⟩
    linarith
  · intro h i hi
    rw [h i hi, sub_self, max_self]

/-- **At most one** for nonnegative counts. -/
theorem robinHood_le_one (S : Finset ι) (hS : S.Nonempty) (c : ι → ℝ) (hc : ∀ i ∈ S, 0 ≤ c i)
    (hpos : 0 < ∑ i ∈ S, c i) : robinHood S c ≤ 1 := by
  unfold robinHood
  rw [div_le_one hpos]
  apply Finset.sum_le_sum
  intro i hi
  have hm : 0 ≤ mean S c := by
    unfold mean; exact div_nonneg (Finset.sum_nonneg hc) (by positivity)
  rw [max_le_iff]
  exact ⟨by linarith, hc i hi⟩

/-- **Scale invariance.** Scaling every count by the same positive factor leaves the index
unchanged. -/
theorem robinHood_scale (S : Finset ι) (c : ι → ℝ) (k : ℝ) (hk : 0 < k) :
    robinHood S (fun i => k * c i) = robinHood S c := by
  unfold robinHood mean
  have hmean : ∀ i, k * c i - (∑ j ∈ S, k * c j) / S.card = k * (c i - (∑ j ∈ S, c j) / S.card) := by
    intro i
    rw [← Finset.mul_sum]
    ring
  simp_rw [hmean]
  have hmax : ∀ t : ℝ, max (k * t) 0 = k * max t 0 := by
    intro t
    rcases le_or_gt 0 t with ht | ht
    · rw [max_eq_left (by positivity), max_eq_left ht]
    · rw [max_eq_right (by nlinarith), max_eq_right ht.le, mul_zero]
  simp_rw [hmax]
  rw [← Finset.mul_sum, ← Finset.mul_sum, mul_div_mul_left _ _ hk.ne']

end DMO.RobinHood