ParaphraseClass.lean

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

/-
ParaphraseClass.lean. Equation 14.3, the paraphrase class and re-description.

A paraphrase class is a set of rewordings of one input that preserve its meaning. A verdict
is invariant to re-description when it does not move across the class. Equation 14.3
measures the movement as the mean absolute change in score under re-description over the
mean absolute difference between distinct inputs. The ratio is nonnegative, it is zero
exactly when no item's score moves, a scorer constant on every class scores zero, and the
bar of one half says the movement under rewording is at most half the movement between
inputs.
-/
import Mathlib

namespace DMO.ParaphraseClass

open Finset

variable {ι : Type*}

/-- The mean absolute movement of the scores under re-description over the items `I`. -/
noncomputable def movement (I : Finset ι) (sA sB : ι → ℝ) : ℝ := (∑ i ∈ I, |sA i - sB i|) / I.card

/-- **Equation 14.3.** The re-description ratio, movement over the within-input spread. -/
noncomputable def ratio (I : Finset ι) (sA sB : ι → ℝ) (spread : ℝ) : ℝ := movement I sA sB / spread

/-- The movement is nonnegative. -/
theorem movement_nonneg (I : Finset ι) (sA sB : ι → ℝ) : 0 ≤ movement I sA sB := by
  unfold movement
  exact div_nonneg (Finset.sum_nonneg fun i _ => abs_nonneg _) (by positivity)

/-- **The ratio is nonnegative** for a positive spread. -/
theorem ratio_nonneg (I : Finset ι) (sA sB : ι → ℝ) (spread : ℝ) (h : 0 < spread) :
    0 ≤ ratio I sA sB spread :=
  div_nonneg (movement_nonneg I sA sB) h.le

/-- **Zero movement means no item's score moved.** -/
theorem movement_eq_zero_iff (I : Finset ι) (hI : I.Nonempty) (sA sB : ι → ℝ) :
    movement I sA sB = 0 ↔ ∀ i ∈ I, sA i = sB i := by
  unfold movement
  have hpos : (0 : ℝ) < I.card := by exact_mod_cast hI.card_pos
  rw [div_eq_zero_iff, or_iff_left hpos.ne',
    Finset.sum_eq_zero_iff_of_nonneg fun i _ => abs_nonneg _]
  constructor
  · intro h i hi
    exact sub_eq_zero.1 (abs_eq_zero.1 (h i hi))
  · intro h i hi
    rw [h i hi, sub_self, abs_zero]

/-- **A scorer invariant to re-description scores zero.** -/
theorem ratio_invariant (I : Finset ι) (sA sB : ι → ℝ) (spread : ℝ) (h : ∀ i ∈ I, sA i = sB i) :
    ratio I sA sB spread = 0 := by
  unfold ratio movement
  rw [Finset.sum_eq_zero fun i hi => by rw [h i hi, sub_self, abs_zero]]
  simp

/-- **The bar.** The ratio is at most one half exactly when the movement under rewording is
at most half the spread between inputs. -/
theorem bar_iff (I : Finset ι) (sA sB : ι → ℝ) (spread : ℝ) (h : 0 < spread) :
    ratio I sA sB spread ≤ 1 / 2 ↔ movement I sA sB ≤ spread / 2 := by
  unfold ratio
  rw [div_le_iff₀ h]
  constructor <;> intro hh <;> linarith

end DMO.ParaphraseClass