CrossCorpusGate.lean

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

/-
CrossCorpusGate.lean. Equation 12.3, the cross-corpus gate.

An encoder is validated when its held-out AUROC on a corpus it was not trained on clears
both nulls, the untrained encoder and the bag-of-words baseline, by the preregistered
margin. A validated encoder therefore beats each null by the margin. Since AUROC is at
most one, a null above one minus the margin cannot be cleared by any encoder, which is the
gate's own saturation. And since AUROC depends on the ranking alone, the gate's verdict is
unchanged by any strictly monotone transform of the encoder's score.
-/
import Mathlib
import DataMiningAsObservation.MonotoneInvariance
import DataMiningAsObservation.ReliabilityWeight

namespace DMO.CrossCorpusGate

open DMO.MonotoneInvariance

/-- **Equation 12.3.** The gate on the cross-corpus AUROC `a` against the untrained null `u`
and the bag-of-words null `b`, with margin `m`. -/
def validated (a u b m : ℝ) : Prop := a - max u b ≥ m

/-- A validated encoder clears the bag-of-words null by the margin. -/
theorem clears_bow (a u b m : ℝ) (h : validated a u b m) : b + m ≤ a := by
  unfold validated at h
  linarith [le_max_right u b]

/-- A validated encoder clears the untrained null by the margin. -/
theorem clears_untrained (a u b m : ℝ) (h : validated a u b m) : u + m ≤ a := by
  unfold validated at h
  linarith [le_max_left u b]

/-- **Saturation.** With AUROC at most one, a null above `1 − m` cannot be cleared. -/
theorem not_validated_of_saturated (a u b m : ℝ) (ha : a ≤ 1) (hb : 1 - m < max u b) :
    ¬ validated a u b m := by
  unfold validated
  intro h
  linarith

/-- The margin the book preregisters, one tenth. -/
theorem margin_example : validated 0.8035 0.5 0.7 0.1 ∧ ¬ validated 0.467 0.5 0.4 0.1 := by
  unfold validated
  constructor
  · norm_num
  · norm_num

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

/-- **The gate reads the ranking.** A strictly monotone transform of the encoder's score
leaves the verdict unchanged. -/
theorem validated_comp {g : ℝ → ℝ} (hg : StrictMono g) (pos neg : Finset ι) (s : ι → ℝ)
    (u b m : ℝ) :
    validated (auroc pos neg (g ∘ s)) u b m ↔ validated (auroc pos neg s) u b m := by
  rw [auroc_comp hg]

end DMO.CrossCorpusGate