Learn then Test: risk-controlling predictions, valid p-values and FWER-controlling algorithms
DefinitionLearnThenTest_BasicObjects of Sections 1.1, 2 and Appendix B of Angelopoulos, Bates, Candès, Jordan and Lei (2022).
- Risk-controlling prediction (Definition 1). Let be a set of parameters, let be the risk of the predictor , and let be a random element of defined on a probability space . Then is an -risk-controlling prediction (RCP) if .
- Valid p-value (Section 2.2). A real statistic is a valid p-value if for every , that is, its law stochastically dominates the uniform law on .
- FWER-controlling algorithm (Definition 2). An algorithm maps p-values to a set of rejected indices . It is FWER-controlling at level if, for every joint law of on (with arbitrary dependence) and every set of true nulls whose p-values are valid, , where . For a given family of p-values on and a given , the family-wise error rate is .
- Bonferroni (Proposition 2). , where .
- Fixed sequence testing (Algorithm 1). Given initializations , for each that is not yet rejected, reject as long as , stopping at the first acceptance or at index .
- Sequential graphical testing (Algorithm 2). Start from initial levels and edge weights . While some unrejected has , choose such an , reject it, and update for unrejected (else ) and for unrejected (else ). The choice of is made by an arbitrary choice rule.
- Hoeffding–Bentkus p-value (Proposition 1, eq. (1)). With empirical risk and ,
- CLT p-value (Appendix B). With the empirical standard deviation and the standard normal distribution function, .
Formalization Note Hypotheses are indexed by , and an algorithm is a map ; only its values on matter. Definition 2 quantifies over joint laws of the p-value vector, and it also requires each event to be Borel measurable so that the probability in Definition 2 is defined. Algorithm 1 visits in increasing order; its output does not depend on the order. Algorithm 2 is run for passes of its loop. Each pass rejects a new hypothesis, so the loop has stopped by then. Its loop condition ranges over unrejected hypotheses only. A division by in the weight update returns . The paper prints while calling it the empirical standard deviation. That printed value is the sample standard deviation divided by , and with it is not the CLT p-value (it is badly anti-conservative at finite ). The definition uses the empirical standard deviation the text names. A division by (for ) returns .
import Mathlib
/-!
Angelopoulos, Bates, Candès, Jordan and Lei, *Learn then Test: Calibrating Predictive Algorithms to
Achieve Risk Control*, arXiv:2110.01052v5 (2022): risk-controlling predictions (Definition 1,
Section 1.1), valid p-values (Section 2.2), the Hoeffding–Bentkus p-value (Proposition 1, eq. (1)),
FWER-controlling algorithms (Definition 2, Section 2.3), the Bonferroni correction
(Proposition 2), fixed sequence testing (Algorithm 1, Section 2.3.1), sequential graphical testing
(Algorithm 2, Section 2.3.2) and the CLT p-value (Appendix B).
-/
open MeasureTheory ProbabilityTheory
noncomputable section
namespace LearnThenTest
/-- Definition 1 (Section 1.1): for a random parameter `λ̂ : Ω → Λ`, `T_λ̂` is an
`(α, δ)`-risk-controlling prediction if `P(R(T_λ̂) ≤ α) ≥ 1 - δ`; here `R l` is the risk `R(T_l)`. -/
def IsRCP {Ω L : Type*} [MeasurableSpace Ω] (P : Measure Ω) (R : L → ℝ) (α δ : ℝ)
(lhat : Ω → L) : Prop :=
ENNReal.ofReal (1 - δ) ≤ P {ω | R (lhat ω) ≤ α}
/-- Section 2.2: a statistic `p` is a valid p-value (its law stochastically dominates the uniform
law on `[0,1]`, Theorem 1) if `P(p ≤ u) ≤ u` for every `u ∈ [0,1]`. -/
def IsValidPValue {Ω : Type*} [MeasurableSpace Ω] (P : Measure Ω) (p : Ω → ℝ) : Prop :=
∀ u ∈ Set.Icc (0 : ℝ) 1, P {ω | p ω ≤ u} ≤ ENNReal.ofReal u
/-- Definition 2 (Section 2.3): an algorithm `A : [0,1]^N → 2^{1,…,N}` (applied to the p-values
`x = (p_1, …, p_N)`, and returning the indices of the rejected hypotheses) is FWER-controlling at
level `δ` if `P(A(p_1, …, p_N) ⊆ J_1) ≥ 1 - δ` for every joint law of the p-values on `[0,1]^N`,
with arbitrary dependence, and every set `J_0` of true nulls whose p-values are valid
(`J_1` is the complement of `J_0`). The event `{j ∈ A(x)}` is required to be measurable so that
the probability is defined. -/
def IsFWERControlling (N : ℕ) (δ : ℝ) (A : (Fin N → ℝ) → Finset (Fin N)) : Prop :=
(∀ j : Fin N, MeasurableSet {x : Fin N → ℝ | j ∈ A x}) ∧
∀ μ : Measure (Fin N → ℝ), IsProbabilityMeasure μ → ∀ J0 : Finset (Fin N),
(∀ᵐ x ∂μ, ∀ j, x j ∈ Set.Icc (0 : ℝ) 1) →
(∀ j ∈ J0, IsValidPValue μ (fun x => x j)) →
ENNReal.ofReal (1 - δ) ≤ μ {x | A x ⊆ J0ᶜ}
/-- Section 2.3: the family-wise error rate of the rejection rule `A` applied to the p-values
`p_1, …, p_N`, when `J_0` is the set of true nulls: `P(A(p_1, …, p_N) ∩ J_0 ≠ ∅)`. -/
def fwer {Ω : Type*} [MeasurableSpace Ω] {N : ℕ} (P : Measure Ω)
(A : (Fin N → ℝ) → Finset (Fin N)) (p : Fin N → Ω → ℝ) (J0 : Finset (Fin N)) : ENNReal :=
P {ω | ∃ j ∈ A (fun i => p i ω), j ∈ J0}
/-- Proposition 2 (Section 2.3): the Bonferroni correction
`A^(Bf)(p_1, …, p_N) = {j : p_j ≤ δ / |Λ|}`, with `|Λ| = N`. -/
def bonferroni (N : ℕ) (δ : ℝ) (x : Fin N → ℝ) : Finset (Fin N) :=
Finset.univ.filter (fun j => x j ≤ δ / N)
/-- Algorithm 1, lines 5–8 (Section 2.3.1): the indices added by the inner `while` loop started at
`j` with threshold `thr`: all `i ≥ j` such that `p_k ≤ thr` for every `k` with `j ≤ k ≤ i`. The
loop stops at the last index `N`. -/
def fixedSeqChain {N : ℕ} (thr : ℝ) (x : Fin N → ℝ) (j : Fin N) : Finset (Fin N) :=
Finset.univ.filter (fun i => j ≤ i ∧ ∀ k : Fin N, j ≤ k → k ≤ i → x k ≤ thr)
/-- Algorithm 1 (Section 2.3.1), fixed sequence testing with initializations `J`: for each
`j ∈ J` (in increasing order) not already rejected, reject `j, j+1, …` while `p ≤ δ / |J|`. -/
def fixedSeq (N : ℕ) (δ : ℝ) (J : Finset (Fin N)) (x : Fin N → ℝ) : Finset (Fin N) :=
(J.sort (· ≤ ·)).foldl
(fun S j => if j ∈ S then S else S ∪ fixedSeqChain (δ / J.card) x j) ∅
/-- Algorithm 2 (Section 2.3.2): the state of sequential graphical testing: the rejected set
`Λ̂`, the current error levels `δ_j` and the current edge weights `g_{k,j}`. -/
structure SGTState (N : ℕ) where
/-- The indices rejected so far. -/
rej : Finset (Fin N)
/-- The current error levels `δ_j`. -/
lev : Fin N → ℝ
/-- The current edge weights `g_{k,j}`. -/
wt : Fin N → Fin N → ℝ
/-- Algorithm 2, lines 5–7: reject `i` and update the levels and the graph:
`δ_j ← δ_j + δ_i g_{i,j}` for unrejected `j` (else `0`), and
`g_{k,j} ← (g_{k,j} + g_{k,i} g_{i,j}) / (1 - g_{k,i} g_{i,k})` for unrejected `k ≠ j` (else `0`). -/
def sgtUpdate {N : ℕ} (s : SGTState N) (i : Fin N) : SGTState N where
rej := insert i s.rej
lev := fun j => if j ∉ insert i s.rej then s.lev j + s.lev i * s.wt i j else 0
wt := fun k j =>
if k ∉ insert i s.rej ∧ j ∉ insert i s.rej ∧ k ≠ j then
(s.wt k j + s.wt k i * s.wt i j) / (1 - s.wt k i * s.wt i k)
else 0
/-- Algorithm 2, line 3: the unrejected hypotheses `i` with `p_i ≤ δ_i`. -/
def sgtEligible {N : ℕ} (x : Fin N → ℝ) (s : SGTState N) : Finset (Fin N) :=
Finset.univ.filter (fun i => i ∉ s.rej ∧ x i ≤ s.lev i)
/-- One pass of the `while` loop of Algorithm 2: if some hypothesis is eligible, reject the one
chosen by the rule `pick` ("choose any `i` such that `p_i ≤ δ_i`") and update; else stop. -/
def sgtStep {N : ℕ} (pick : SGTState N → Finset (Fin N) → Fin N) (x : Fin N → ℝ)
(s : SGTState N) : SGTState N :=
if (sgtEligible x s).Nonempty then sgtUpdate s (pick s (sgtEligible x s)) else s
/-- Algorithm 2 (Section 2.3.2), sequential graphical testing with initial error budget `δ0`,
graph `g` and choice rule `pick`, returning `Λ̂`. Each pass rejects a new hypothesis, so the
loop has stopped after `N` passes. -/
def sgt (N : ℕ) (pick : SGTState N → Finset (Fin N) → Fin N) (δ0 : Fin N → ℝ)
(g : Fin N → Fin N → ℝ) (x : Fin N → ℝ) : Finset (Fin N) :=
((sgtStep pick x)^[N] ⟨∅, δ0, g⟩).rej
/-- Section 2.2: the empirical risk `R̂ = (1/n) ∑_{i=1}^n L_i` of the calibration losses. -/
def empRisk {n : ℕ} (l : Fin n → ℝ) : ℝ := (∑ i, l i) / n
/-- Proposition 1: `h_1(a, b) = a log(a/b) + (1 - a) log((1 - a)/(1 - b))` (with `0 log 0 = 0`). -/
def h1 (a b : ℝ) : ℝ := a * Real.log (a / b) + (1 - a) * Real.log ((1 - a) / (1 - b))
/-- The distribution function `P(Bin(n, a) ≤ x)` of the binomial law. -/
def binomCdf (n : ℕ) (a x : ℝ) : ℝ :=
∑ i ∈ Finset.range (n + 1),
if (i : ℝ) ≤ x then (n.choose i : ℝ) * a ^ i * (1 - a) ^ (n - i) else 0
/-- Proposition 1, eq. (1): the Hoeffding–Bentkus p-value
`p^HB = min(exp{-n h_1(R̂ ∧ α, α)}, e P(Bin(n, α) ≤ ⌈n R̂⌉))`. -/
def hbPValue (n : ℕ) (α Rhat : ℝ) : ℝ :=
min (Real.exp (-((n : ℝ) * h1 (min Rhat α) α)))
(Real.exp 1 * binomCdf n α (⌈(n : ℝ) * Rhat⌉ : ℝ))
/-- The standard normal distribution function `Φ`. -/
def stdNormalCdf (x : ℝ) : ℝ := cdf (gaussianReal 0 1) x
/-- Appendix B: the empirical standard deviation
`σ̂ = (((L_1 - R̂)^2 + … + (L_n - R̂)^2) / (n - 1))^{1/2}`. The printed
`(…)^{1/2} / (n - 1)` is a typo for this: it is not the empirical standard deviation the text
names, but that value divided by `√(n - 1)`. -/
def sigmaHat {n : ℕ} (l : Fin n → ℝ) : ℝ :=
Real.sqrt ((∑ i, (l i - empRisk l) ^ 2) / ((n : ℝ) - 1))
/-- Proposition B.1 (Appendix B): the CLT p-value `1 - Φ((α - R̂) / (σ̂ / √n))`. -/
def cltPValue {n : ℕ} (α : ℝ) (l : Fin n → ℝ) : ℝ :=
1 - stdNormalCdf ((α - empRisk l) / (sigmaHat l / Real.sqrt n))
end LearnThenTest
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
IsRCP (risk-controlling prediction). Fix the following data:
- a set with a σ-algebra, and a measure on it (not required to be a probability measure);
- an arbitrary set of parameters;
- a real-valued function ;
- real numbers ;
- a map (no measurability is assumed).
The property holds when
The left side is cut off below at , and the comparison is made in . Some edge cases follow from this:
- If , the condition holds trivially.
- If the set on the right is not measurable, is evaluated as an outer measure.
- If has total mass greater than , the condition can hold even when the set does not have "probability" at least in the usual sense.
IsValidPValue (valid p-value). Fix a set with a σ-algebra, a measure on it (not required to be a probability measure), and a function (not required to be measurable or to take values in ). The property holds when
Nothing is required for outside . Taking forces . Taking forces .
IsFWERControlling (FWER-controlling algorithm). Fix:
- a natural number , with index set in its usual order;
- a real number ;
- a map from vectors to finite subsets .
is FWER-controlling at level when both of the following hold:
-
For every index , the set is Borel measurable.
-
Take any probability measure on and any subset , and suppose
- -almost every has all coordinates , and
- for every , the coordinate map is a valid p-value under in the sense of
IsValidPValue.
Then
No assumption is made on the dependence between coordinates. Some edge cases follow from this:
- Taking makes the right side , so the property can only hold if .
- For , the property holds exactly when .
- If , condition 2 is automatic and only measurability matters.
fwer (family-wise error rate). Fix:
- a set with a σ-algebra and a measure ;
- ;
- a rule finite subsets of ;
- functions ;
- a subset .
The value is
No measurability or probability-measure assumption is imposed.
bonferroni (Bonferroni correction). For , and , the rule returns
For the index set is empty, so the result is .
fixedSeqChain (one inner loop of fixed sequence testing). Fix a threshold , a vector , and a starting index . The result is
This is the longest consecutive run (stopping at ) along which every coordinate is at most . In particular, it is empty when .
fixedSeq (fixed sequence testing, Algorithm 1). Fix , , a set of initializations , and .
- List in increasing order as , where .
- Start with .
- For each in turn: if , leave unchanged; otherwise replace by , with as in
fixedSeqChain. - Return the final .
Every chain uses the same threshold . If , the result is .
SGTState (state of sequential graphical testing). For , a state consists of three components, none of which carries any constraint (sign, normalization, diagonal, or otherwise):
- a finite set ;
- an arbitrary real vector of levels;
- an arbitrary real array of weights.
sgtUpdate (reject one index and update). Given a state and an index , let . The value of is not checked; it may already be in . The new state is , where:
All right-hand sides use the old values and . If , the fraction is taken to be , because division by zero yields .
sgtEligible (eligible hypotheses). For and a state , the result is
sgtStep (one pass of the loop). Fix an arbitrary choice function , which maps a state and a finite set of indices to a single index, and fix . From state :
- If is nonempty, the result is
sgtUpdateapplied to and the index . - Otherwise, the result is unchanged.
Nothing requires to return an element of . It may return a non-eligible or already-rejected index.
sgt (sequential graphical testing, Algorithm 2). Fix:
- ;
- a choice function ;
- an arbitrary real vector of initial levels ;
- an arbitrary real array of initial weights ;
- a vector .
Start from the state , apply sgtStep exactly times, and return the rejected set of the resulting state. No conditions are imposed on or , such as nonnegativity, , , or rows summing to at most . If ever returns an index that is not eligible, the passes need not have reached a point where no eligible index remains.
empRisk (empirical risk). For and losses , the value is
For this is .
h1. For real , the value is
where the logarithm follows these conventions:
- , so in particular ;
- for ;
- any division by zero gives .
No restriction to is imposed.
binomCdf. For and real , the value is
This is the binomial distribution function when . It is defined by the same formula for any real .
hbPValue (Hoeffding–Bentkus p-value). For and real , the value is
with and as defined above, including their conventions for out-of-range arguments. No range restrictions on or are imposed.
stdNormalCdf. This is the standard normal distribution function
sigmaHat (empirical standard deviation). For losses , with as in empRisk, the value is
The edge cases are:
- For , the division by zero gives , so .
- For , the empty sum gives .
- If all are equal, .
cltPValue (CLT p-value). For real and losses , the value is
Whenever (in particular when , or when all losses are equal), the quotient is taken to be . In that case , regardless of .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.