Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Learn then Test: risk-controlling predictions, valid p-values and FWER-controlling algorithms

Definition
LearnThenTest_Basic

by Xeroth · Nov 24, 2026 · Mathlib 0df444a (Lean v4.33.1)

distribution-free-inferencefamily-wise-error-ratemultiple-testingp-valuesrisk-control

Objects of Sections 1.1, 2 and Appendix B of Angelopoulos, Bates, Candès, Jordan and Lei (2022).

  1. Risk-controlling prediction (Definition 1). Let Λ\LambdaΛ be a set of parameters, let R(λ)=R(Tλ)R(\lambda)=R(\mathcal T_\lambda)R(λ)=R(Tλ​) be the risk of the predictor Tλ\mathcal T_\lambdaTλ​, and let λ^\hat\lambdaλ^ be a random element of Λ\LambdaΛ defined on a probability space (Ω,F,P)(\Omega,\mathcal F,\mathbb P)(Ω,F,P). Then Tλ^\mathcal T_{\hat\lambda}Tλ^​ is an (α,δ)(\alpha,\delta)(α,δ)-risk-controlling prediction (RCP) if P(R(λ^)≤α)≥1−δ\mathbb P\big(R(\hat\lambda)\le\alpha\big)\ge1-\deltaP(R(λ^)≤α)≥1−δ.
  2. Valid p-value (Section 2.2). A real statistic ppp is a valid p-value if P(p≤u)≤u\mathbb P(p\le u)\le uP(p≤u)≤u for every u∈[0,1]u\in[0,1]u∈[0,1], that is, its law stochastically dominates the uniform law on [0,1][0,1][0,1].
  3. FWER-controlling algorithm (Definition 2). An algorithm A\mathcal AA maps p-values (p1,…,pN)∈[0,1]N(p_1,\dots,p_N)\in[0,1]^N(p1​,…,pN​)∈[0,1]N to a set of rejected indices A(p1,…,pN)⊆{1,…,N}\mathcal A(p_1,\dots,p_N)\subseteq\{1,\dots,N\}A(p1​,…,pN​)⊆{1,…,N}. It is FWER-controlling at level δ\deltaδ if, for every joint law of (p1,…,pN)(p_1,\dots,p_N)(p1​,…,pN​) on [0,1]N[0,1]^N[0,1]N (with arbitrary dependence) and every set J0J_0J0​ of true nulls whose p-values are valid, P(A(p1,…,pN)⊆J1)≥1−δ\mathbb P\big(\mathcal A(p_1,\dots,p_N)\subseteq J_1\big)\ge1-\deltaP(A(p1​,…,pN​)⊆J1​)≥1−δ, where J1={1,…,N}∖J0J_1=\{1,\dots,N\}\setminus J_0J1​={1,…,N}∖J0​. For a given family of p-values on Ω\OmegaΩ and a given J0J_0J0​, the family-wise error rate is FWER=P(A(p1,…,pN)∩J0≠∅)\mathrm{FWER}=\mathbb P\big(\mathcal A(p_1,\dots,p_N)\cap J_0\ne\emptyset\big)FWER=P(A(p1​,…,pN​)∩J0​=∅).
  4. Bonferroni (Proposition 2). A(Bf)(p1,…,pN)={j:pj≤δ/N}\mathcal A^{(\mathrm{Bf})}(p_1,\dots,p_N)=\{j:p_j\le\delta/N\}A(Bf)(p1​,…,pN​)={j:pj​≤δ/N}, where N=∣Λ∣N=|\Lambda|N=∣Λ∣.
  5. Fixed sequence testing (Algorithm 1). Given initializations J⊆{1,…,N}\mathcal J\subseteq\{1,\dots,N\}J⊆{1,…,N}, for each j∈Jj\in\mathcal Jj∈J that is not yet rejected, reject j,j+1,…j,j+1,\dotsj,j+1,… as long as p≤δ/∣J∣p\le\delta/|\mathcal J|p≤δ/∣J∣, stopping at the first acceptance or at index NNN.
  6. Sequential graphical testing (Algorithm 2). Start from initial levels δi\delta_iδi​ and edge weights gi,jg_{i,j}gi,j​. While some unrejected iii has pi≤δip_i\le\delta_ipi​≤δi​, choose such an iii, reject it, and update δj←δj+δigi,j\delta_j\leftarrow\delta_j+\delta_ig_{i,j}δj​←δj​+δi​gi,j​ for unrejected jjj (else 000) and gk,j←gk,j+gk,igi,j1−gk,igi,kg_{k,j}\leftarrow\dfrac{g_{k,j}+g_{k,i}g_{i,j}}{1-g_{k,i}g_{i,k}}gk,j​←1−gk,i​gi,k​gk,j​+gk,i​gi,j​​ for unrejected k≠jk\ne jk=j (else 000). The choice of iii is made by an arbitrary choice rule.
  7. Hoeffding–Bentkus p-value (Proposition 1, eq. (1)). With empirical risk R^=1n∑i=1nLi\hat R=\frac1n\sum_{i=1}^nL_iR^=n1​∑i=1n​Li​ and h1(a,b)=alog⁡(a/b)+(1−a)log⁡1−a1−bh_1(a,b)=a\log(a/b)+(1-a)\log\frac{1-a}{1-b}h1​(a,b)=alog(a/b)+(1−a)log1−b1−a​,
pHB=min⁡(exp⁡{−n h1(R^∧α,α)}, e P(Bin(n,α)≤⌈nR^⌉)).p^{\mathrm{HB}}=\min\Big(\exp\{-n\,h_1(\hat R\wedge\alpha,\alpha)\},\ e\,\mathbb P\big(\mathrm{Bin}(n,\alpha)\le\lceil n\hat R\rceil\big)\Big).pHB=min(exp{−nh1​(R^∧α,α)}, eP(Bin(n,α)≤⌈nR^⌉)).
  1. CLT p-value (Appendix B). With the empirical standard deviation σ^=(((L1−R^)2+⋯+(Ln−R^)2)/(n−1))1/2\hat\sigma=\Big(\big((L_1-\hat R)^2+\dots+(L_n-\hat R)^2\big)/(n-1)\Big)^{1/2}σ^=(((L1​−R^)2+⋯+(Ln​−R^)2)/(n−1))1/2 and Φ\PhiΦ the standard normal distribution function, pCLT=1−Φ(α−R^σ^/n)p^{\mathrm{CLT}}=1-\Phi\Big(\dfrac{\alpha-\hat R}{\hat\sigma/\sqrt n}\Big)pCLT=1−Φ(σ^/n​α−R^​).

Formalization Note Hypotheses are indexed by Fin N={0,…,N−1}\mathrm{Fin}\,N=\{0,\dots,N-1\}FinN={0,…,N−1}, and an algorithm is a map RN→2{0,…,N−1}\mathbb R^N\to 2^{\{0,\dots,N-1\}}RN→2{0,…,N−1}; only its values on [0,1]N[0,1]^N[0,1]N matter. Definition 2 quantifies over joint laws μ\muμ of the p-value vector, and it also requires each event {j∈A(x)}\{j\in\mathcal A(x)\}{j∈A(x)} to be Borel measurable so that the probability in Definition 2 is defined. Algorithm 1 visits J\mathcal JJ in increasing order; its output does not depend on the order. Algorithm 2 is run for NNN 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 000 in the weight update returns 000. The paper prints σ^=(∑i(Li−R^)2)1/2/(n−1)\hat\sigma=\big(\sum_i(L_i-\hat R)^2\big)^{1/2}/(n-1)σ^=(∑i​(Li​−R^)2)1/2/(n−1) while calling it the empirical standard deviation. That printed value is the sample standard deviation divided by n−1\sqrt{n-1}n−1​, and with it pCLTp^{\mathrm{CLT}}pCLT is not the CLT p-value (it is badly anti-conservative at finite nnn). The definition uses the empirical standard deviation the text names. A division by 000 (for n≤1n\le1n≤1) returns 000.

Definition code
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
Source
Angelopoulos, Bates, Candès, Jordan and Lei, Learn then Test: Calibrating Predictive Algorithms to Achieve Risk Control, arXiv:2110.01052v5 (2022), Section 1.1 (Definition 1), Section 2.2 (valid p-values, Proposition 1, eq. (1)), Section 2.3 (Definition 2, Proposition 2, Algorithms 1 and 2), Appendix B (CLT p-value)
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 Ω\OmegaΩ with a σ-algebra, and a measure PPP on it (not required to be a probability measure);
  • an arbitrary set LLL of parameters;
  • a real-valued function R:L→RR : L \to \mathbb{R}R:L→R;
  • real numbers α,δ\alpha, \deltaα,δ;
  • a map λ^:Ω→L\hat\lambda : \Omega \to Lλ^:Ω→L (no measurability is assumed).

The property holds when

max⁡(1−δ, 0)  ≤  P({ω∈Ω:R(λ^(ω))≤α}).\max(1-\delta,\,0) \;\le\; P\bigl(\{\omega \in \Omega : R(\hat\lambda(\omega)) \le \alpha\}\bigr).max(1−δ,0)≤P({ω∈Ω:R(λ^(ω))≤α}).

The left side is 1−δ1-\delta1−δ cut off below at 000, and the comparison is made in [0,∞][0,\infty][0,∞]. Some edge cases follow from this:

  • If δ≥1\delta \ge 1δ≥1, the condition holds trivially.
  • If the set on the right is not measurable, PPP is evaluated as an outer measure.
  • If PPP has total mass greater than 111, the condition can hold even when the set does not have "probability" at least 1−δ1-\delta1−δ in the usual sense.

IsValidPValue (valid p-value). Fix a set Ω\OmegaΩ with a σ-algebra, a measure PPP on it (not required to be a probability measure), and a function p:Ω→Rp : \Omega \to \mathbb{R}p:Ω→R (not required to be measurable or to take values in [0,1][0,1][0,1]). The property holds when

P({ω:p(ω)≤u})  ≤  ufor every u∈[0,1].P\bigl(\{\omega : p(\omega) \le u\}\bigr) \;\le\; u \qquad \text{for every } u \in [0,1].P({ω:p(ω)≤u})≤ufor every u∈[0,1].

Nothing is required for uuu outside [0,1][0,1][0,1]. Taking u=0u = 0u=0 forces P(p≤0)=0P(p \le 0) = 0P(p≤0)=0. Taking u=1u = 1u=1 forces P(p≤1)≤1P(p \le 1) \le 1P(p≤1)≤1.


IsFWERControlling (FWER-controlling algorithm). Fix:

  • a natural number NNN, with index set {0,1,…,N−1}\{0,1,\dots,N-1\}{0,1,…,N−1} in its usual order;
  • a real number δ\deltaδ;
  • a map AAA from vectors x∈RNx \in \mathbb{R}^Nx∈RN to finite subsets A(x)⊆{0,…,N−1}A(x) \subseteq \{0,\dots,N-1\}A(x)⊆{0,…,N−1}.

AAA is FWER-controlling at level δ\deltaδ when both of the following hold:

  1. For every index jjj, the set {x∈RN:j∈A(x)}\{x \in \mathbb{R}^N : j \in A(x)\}{x∈RN:j∈A(x)} is Borel measurable.

  2. Take any probability measure μ\muμ on RN\mathbb{R}^NRN and any subset J0⊆{0,…,N−1}J_0 \subseteq \{0,\dots,N-1\}J0​⊆{0,…,N−1}, and suppose

    • μ\muμ-almost every xxx has all coordinates xj∈[0,1]x_j \in [0,1]xj​∈[0,1], and
    • for every j∈J0j \in J_0j∈J0​, the coordinate map x↦xjx \mapsto x_jx↦xj​ is a valid p-value under μ\muμ in the sense of IsValidPValue.

    Then

max⁡(1−δ,0)  ≤  μ({x:A(x)⊆{0,…,N−1}∖J0}).\max(1-\delta,0) \;\le\; \mu\bigl(\{x : A(x) \subseteq \{0,\dots,N-1\}\setminus J_0\}\bigr).max(1−δ,0)≤μ({x:A(x)⊆{0,…,N−1}∖J0​}).

No assumption is made on the dependence between coordinates. Some edge cases follow from this:

  • Taking J0=∅J_0 = \varnothingJ0​=∅ makes the right side 111, so the property can only hold if δ≥0\delta \ge 0δ≥0.
  • For N=0N = 0N=0, the property holds exactly when δ≥0\delta \ge 0δ≥0.
  • If δ≥1\delta \ge 1δ≥1, condition 2 is automatic and only measurability matters.

fwer (family-wise error rate). Fix:

  • a set Ω\OmegaΩ with a σ-algebra and a measure PPP;
  • N∈NN \in \mathbb{N}N∈N;
  • a rule A:RN→A : \mathbb{R}^N \toA:RN→ finite subsets of {0,…,N−1}\{0,\dots,N-1\}{0,…,N−1};
  • functions p0,…,pN−1:Ω→Rp_0,\dots,p_{N-1} : \Omega \to \mathbb{R}p0​,…,pN−1​:Ω→R;
  • a subset J0⊆{0,…,N−1}J_0 \subseteq \{0,\dots,N-1\}J0​⊆{0,…,N−1}.

The value is

FWER  =  P({ω:A(p0(ω),…,pN−1(ω))∩J0≠∅})∈[0,∞].\mathrm{FWER} \;=\; P\bigl(\{\omega : A(p_0(\omega),\dots,p_{N-1}(\omega)) \cap J_0 \neq \varnothing\}\bigr) \in [0,\infty].FWER=P({ω:A(p0​(ω),…,pN−1​(ω))∩J0​=∅})∈[0,∞].

No measurability or probability-measure assumption is imposed.


bonferroni (Bonferroni correction). For N∈NN \in \mathbb{N}N∈N, δ∈R\delta \in \mathbb{R}δ∈R and x∈RNx \in \mathbb{R}^Nx∈RN, the rule returns

ABf(x)  =  { j∈{0,…,N−1}:xj≤δ/N }.A^{\mathrm{Bf}}(x) \;=\; \{\, j \in \{0,\dots,N-1\} : x_j \le \delta / N \,\}.ABf(x)={j∈{0,…,N−1}:xj​≤δ/N}.

For N=0N = 0N=0 the index set is empty, so the result is ∅\varnothing∅.


fixedSeqChain (one inner loop of fixed sequence testing). Fix a threshold t∈Rt \in \mathbb{R}t∈R, a vector x∈RNx \in \mathbb{R}^Nx∈RN, and a starting index j∈{0,…,N−1}j \in \{0,\dots,N-1\}j∈{0,…,N−1}. The result is

Ct(x,j)  =  { i:j≤i and xk≤t for every k with j≤k≤i }.C_t(x,j) \;=\; \{\, i : j \le i \text{ and } x_k \le t \text{ for every } k \text{ with } j \le k \le i \,\}.Ct​(x,j)={i:j≤i and xk​≤t for every k with j≤k≤i}.

This is the longest consecutive run j,j+1,…j, j+1, \dotsj,j+1,… (stopping at N−1N-1N−1) along which every coordinate is at most ttt. In particular, it is empty when xj>tx_j > txj​>t.


fixedSeq (fixed sequence testing, Algorithm 1). Fix N∈NN \in \mathbb{N}N∈N, δ∈R\delta \in \mathbb{R}δ∈R, a set of initializations J⊆{0,…,N−1}J \subseteq \{0,\dots,N-1\}J⊆{0,…,N−1}, and x∈RNx \in \mathbb{R}^Nx∈RN.

  1. List JJJ in increasing order as j1<⋯<jmj_1 < \dots < j_mj1​<⋯<jm​, where m=∣J∣m = |J|m=∣J∣.
  2. Start with S=∅S = \varnothingS=∅.
  3. For each jrj_rjr​ in turn: if jr∈Sj_r \in Sjr​∈S, leave SSS unchanged; otherwise replace SSS by S∪Cδ/m(x,jr)S \cup C_{\delta/m}(x, j_r)S∪Cδ/m​(x,jr​), with CCC as in fixedSeqChain.
  4. Return the final SSS.

Every chain uses the same threshold δ/∣J∣\delta/|J|δ/∣J∣. If J=∅J = \varnothingJ=∅, the result is ∅\varnothing∅.


SGTState (state of sequential graphical testing). For N∈NN \in \mathbb{N}N∈N, a state consists of three components, none of which carries any constraint (sign, normalization, diagonal, or otherwise):

  • a finite set rej⊆{0,…,N−1}\mathrm{rej} \subseteq \{0,\dots,N-1\}rej⊆{0,…,N−1};
  • an arbitrary real vector (δj)j(\delta_j)_{j}(δj​)j​ of levels;
  • an arbitrary real N×NN\times NN×N array (gk,j)k,j(g_{k,j})_{k,j}(gk,j​)k,j​ of weights.

sgtUpdate (reject one index and update). Given a state (rej,δ,g)(\mathrm{rej}, \delta, g)(rej,δ,g) and an index iii, let rej′=rej∪{i}\mathrm{rej}' = \mathrm{rej} \cup \{i\}rej′=rej∪{i}. The value of iii is not checked; it may already be in rej\mathrm{rej}rej. The new state is (rej′,δ′,g′)(\mathrm{rej}', \delta', g')(rej′,δ′,g′), where:

δj′={δj+δi gi,jj∉rej′0otherwise,\delta'_j = \begin{cases} \delta_j + \delta_i\, g_{i,j} & j \notin \mathrm{rej}' \\ 0 & \text{otherwise,}\end{cases}δj′​={δj​+δi​gi,j​0​j∈/rej′otherwise,​ gk,j′={gk,j+gk,i gi,j1−gk,i gi,kk∉rej′, j∉rej′, k≠j0otherwise.g'_{k,j} = \begin{cases} \dfrac{g_{k,j} + g_{k,i}\,g_{i,j}}{1 - g_{k,i}\,g_{i,k}} & k \notin \mathrm{rej}',\ j \notin \mathrm{rej}',\ k \neq j \\[1ex] 0 & \text{otherwise.}\end{cases}gk,j′​=⎩⎨⎧​1−gk,i​gi,k​gk,j​+gk,i​gi,j​​0​k∈/rej′, j∈/rej′, k=jotherwise.​

All right-hand sides use the old values δ\deltaδ and ggg. If gk,i gi,k=1g_{k,i}\,g_{i,k} = 1gk,i​gi,k​=1, the fraction is taken to be 000, because division by zero yields 000.


sgtEligible (eligible hypotheses). For x∈RNx \in \mathbb{R}^Nx∈RN and a state (rej,δ,g)(\mathrm{rej}, \delta, g)(rej,δ,g), the result is

E(x,s)  =  { i:i∉rej and xi≤δi }.E(x,s) \;=\; \{\, i : i \notin \mathrm{rej} \text{ and } x_i \le \delta_i \,\}.E(x,s)={i:i∈/rej and xi​≤δi​}.

sgtStep (one pass of the loop). Fix an arbitrary choice function pick\mathrm{pick}pick, which maps a state and a finite set of indices to a single index, and fix x∈RNx \in \mathbb{R}^Nx∈RN. From state sss:

  • If E(x,s)E(x,s)E(x,s) is nonempty, the result is sgtUpdate applied to sss and the index pick(s,E(x,s))\mathrm{pick}(s, E(x,s))pick(s,E(x,s)).
  • Otherwise, the result is sss unchanged.

Nothing requires pick\mathrm{pick}pick to return an element of E(x,s)E(x,s)E(x,s). It may return a non-eligible or already-rejected index.


sgt (sequential graphical testing, Algorithm 2). Fix:

  • N∈NN \in \mathbb{N}N∈N;
  • a choice function pick\mathrm{pick}pick;
  • an arbitrary real vector of initial levels δ0\delta^0δ0;
  • an arbitrary real array of initial weights ggg;
  • a vector x∈RNx \in \mathbb{R}^Nx∈RN.

Start from the state (∅,δ0,g)(\varnothing, \delta^0, g)(∅,δ0,g), apply sgtStep exactly NNN times, and return the rejected set of the resulting state. No conditions are imposed on δ0\delta^0δ0 or ggg, such as nonnegativity, ∑jδj0≤δ\sum_j \delta^0_j \le \delta∑j​δj0​≤δ, gj,j=0g_{j,j}=0gj,j​=0, or rows summing to at most 111. If pick\mathrm{pick}pick ever returns an index that is not eligible, the NNN passes need not have reached a point where no eligible index remains.


empRisk (empirical risk). For n∈Nn \in \mathbb{N}n∈N and losses L1,…,Ln∈RL_1,\dots,L_n \in \mathbb{R}L1​,…,Ln​∈R, the value is

R^=1n∑i=1nLi.\hat R = \frac{1}{n}\sum_{i=1}^n L_i.R^=n1​i=1∑n​Li​.

For n=0n = 0n=0 this is 000.


h1. For real a,ba, ba,b, the value is

h1(a,b)=alog⁡ab+(1−a)log⁡1−a1−b,h_1(a,b) = a\log\frac{a}{b} + (1-a)\log\frac{1-a}{1-b},h1​(a,b)=alogba​+(1−a)log1−b1−a​,

where the logarithm follows these conventions:

  • log⁡0=0\log 0 = 0log0=0, so in particular 0⋅log⁡0=00\cdot\log 0 = 00⋅log0=0;
  • log⁡y=log⁡∣y∣\log y = \log|y|logy=log∣y∣ for y<0y < 0y<0;
  • any division by zero gives 000.

No restriction to a,b∈(0,1)a, b \in (0,1)a,b∈(0,1) is imposed.


binomCdf. For n∈Nn \in \mathbb{N}n∈N and real a,xa, xa,x, the value is

Fn,a(x)=∑0≤i≤ni≤x(ni)ai(1−a)n−i.F_{n,a}(x) = \sum_{\substack{0 \le i \le n \\ i \le x}} \binom{n}{i} a^i (1-a)^{n-i}.Fn,a​(x)=0≤i≤ni≤x​∑​(in​)ai(1−a)n−i.

This is the binomial distribution function when a∈[0,1]a \in [0,1]a∈[0,1]. It is defined by the same formula for any real aaa.


hbPValue (Hoeffding–Bentkus p-value). For n∈Nn \in \mathbb{N}n∈N and real α,R^\alpha, \hat Rα,R^, the value is

pHB=min⁡(exp⁡(−n h1(min⁡(R^,α), α)),  e⋅Fn,α(⌈nR^⌉)),p^{\mathrm{HB}} = \min\Bigl( \exp\bigl(-n\,h_1(\min(\hat R,\alpha),\,\alpha)\bigr),\; e \cdot F_{n,\alpha}\bigl(\lceil n\hat R\rceil\bigr) \Bigr),pHB=min(exp(−nh1​(min(R^,α),α)),e⋅Fn,α​(⌈nR^⌉)),

with h1h_1h1​ and FFF as defined above, including their conventions for out-of-range arguments. No range restrictions on α\alphaα or R^\hat RR^ are imposed.


stdNormalCdf. This is the standard normal distribution function

Φ(x)=P(Z≤x),Z∼N(0,1).\Phi(x) = \mathbb{P}(Z \le x), \qquad Z \sim \mathcal{N}(0,1).Φ(x)=P(Z≤x),Z∼N(0,1).

sigmaHat (empirical standard deviation). For losses L1,…,LnL_1,\dots,L_nL1​,…,Ln​, with R^\hat RR^ as in empRisk, the value is

σ^=∑i=1n(Li−R^)2n−1.\hat\sigma = \sqrt{\frac{\sum_{i=1}^n (L_i - \hat R)^2}{n-1}}.σ^=n−1∑i=1n​(Li​−R^)2​​.

The edge cases are:

  • For n=1n = 1n=1, the division by zero gives 000, so σ^=0\hat\sigma = 0σ^=0.
  • For n=0n = 0n=0, the empty sum gives σ^=0\hat\sigma = 0σ^=0.
  • If all LiL_iLi​ are equal, σ^=0\hat\sigma = 0σ^=0.

cltPValue (CLT p-value). For real α\alphaα and losses L1,…,LnL_1,\dots,L_nL1​,…,Ln​, the value is

pCLT=1−Φ ⁣(α−R^σ^/n).p^{\mathrm{CLT}} = 1 - \Phi\!\left(\frac{\alpha - \hat R}{\hat\sigma/\sqrt n}\right).pCLT=1−Φ(σ^/n​α−R^​).

Whenever σ^/n=0\hat\sigma/\sqrt{n} = 0σ^/n​=0 (in particular when n≤1n \le 1n≤1, or when all losses are equal), the quotient is taken to be 000. In that case pCLT=1−Φ(0)=12p^{\mathrm{CLT}} = 1 - \Phi(0) = \tfrac12pCLT=1−Φ(0)=21​, regardless of α\alphaα.

Human review
  • Endorsed by Shuze Chen · Nov 24, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 24, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me