Convexified Neyman-Pearson classification: surrogates, errors, NP classifier, chance constrained programs
DefinitionNeymanPearsonClassification_BasicObjects of Rigollet and Tong (2011), Sections 2-6.
- Convex surrogate (Definition 2.1). A function that is non-decreasing, continuous and convex with .
- Base classifiers and their convex hull (Section 2.1). Measurable functions . For in the flat simplex , , and .
- Errors (Section 2.2). Let and be the laws of given and . The type I error is and the type II error is . The -type I and II errors are and .
- Empirical errors (Sections 3.1-3.2). For a sample the empirical type I error is . The empirical -errors are and .
- Constraint sets. and . The optimal value is , written in the paper.
- Convexified NP classifier (eq. 3.4). is any solution of .
- Constants (Section 4). . is the smallest with , and (eq. 4.3).
- Label split (Section 4.3). For labels , the index sets and have sizes and .
- Binomial tail (Section 6.6). for .
- Chance constrained programs (Section 5). With and : is feasible for (5.1) if and . solves subject to . is the optimal value of (5.7).
Formalization Note The covariate space is taken to be (EuclideanSpace ℝ (Fin d)). Classifiers on a measurable extend by , and laws on are laws on carried by . A surrogate is a function whose properties are required only on , the only arguments ever used. Infima are real infima ( in Lean). Every statement that uses , or assumes or proves that the set involved is nonempty. An empirical average over an empty sample is in Lean. Theorems 4.1 and 4.2 assume nonempty samples; Corollary 4.1 guards the empty case explicitly in part 1, while part 2 includes it, where it is covered by the stated failure probability.
import Mathlib
/-!
Rigollet and Tong, *Neyman-Pearson classification, convexity and stochastic constraints*,
arXiv:1102.5750v1 (2011): convex surrogates (Definition 2.1), the convex hull of base classifiers
(Section 2.1), type I/II errors and their convexified versions (Section 2.2), empirical errors and
the convexified NP classifier (Sections 3.1-3.2, eq. (3.4)), the constants of Section 4
(`κ`, `ε̄`, `n₀` of eq. (4.3)), the label-split samples of Section 4.3, the binomial tails of
Section 6.6 and the chance constrained programs (5.1), (5.7) of Section 5.
-/
open MeasureTheory
noncomputable section
namespace NeymanPearsonClassification
/-- Definition 2.1 (p. 2): `φ : [-1,1] → ℝ₊` is a convex surrogate if it is non-negative,
non-decreasing, continuous and convex on `[-1,1]` and `φ(0) = 1`. Values of `φ` outside
`[-1,1]` are never used. -/
def IsConvexSurrogate (φ : ℝ → ℝ) : Prop :=
(∀ x ∈ Set.Icc (-1 : ℝ) 1, 0 ≤ φ x) ∧ MonotoneOn φ (Set.Icc (-1) 1) ∧
ContinuousOn φ (Set.Icc (-1) 1) ∧ ConvexOn ℝ (Set.Icc (-1) 1) φ ∧ φ 0 = 1
/-- Section 2.1 (p. 3): base classifiers `h₁, …, h_M : 𝒳 → [-1,1]` (measurable, the standing
convention). The same structure serves the functions `g_j : Ξ → [-1,1]` of Section 5 (p. 11). -/
def IsBaseFamily {X : Type*} [MeasurableSpace X] {M : ℕ} (h : Fin M → X → ℝ) : Prop :=
∀ j, Measurable (h j) ∧ ∀ x, h j x ∈ Set.Icc (-1 : ℝ) 1
/-- Section 2.1 (p. 3): the convex combination `h_λ = ∑_j λ_j h_j`; in Section 5 (p. 11) this is
`F(λ, ξ) = ∑_j λ_j g_j(ξ)`. -/
def convComb {X : Type*} {M : ℕ} (h : Fin M → X → ℝ) (lam : Fin M → ℝ) : X → ℝ :=
fun x => ∑ j, lam j * h j x
/-- Section 2.1 (p. 3): `H^conv = {h_λ : λ ∈ Λ}`, with `Λ` the flat simplex of `ℝ^M`. -/
def HConv {X : Type*} {M : ℕ} (h : Fin M → X → ℝ) : Set (X → ℝ) :=
convComb h '' stdSimplex ℝ (Fin M)
/-- Section 2.2 (p. 3): type I error `R⁻(h) = P⁻(h(X) ≥ 0)`, `P⁻` the law of `X` given `Y = -1`. -/
def typeI {X : Type*} [MeasurableSpace X] (Pm : Measure X) (f : X → ℝ) : ℝ :=
(Pm {x | 0 ≤ f x}).toReal
/-- Section 2.2 (p. 3): type II error `R⁺(h) = P⁺(h(X) ≤ 0)`, `P⁺` the law of `X` given `Y = 1`. -/
def typeII {X : Type*} [MeasurableSpace X] (Pp : Measure X) (f : X → ℝ) : ℝ :=
(Pp {x | f x ≤ 0}).toReal
/-- Section 2.2 (p. 4): `φ`-type I error `R⁻_φ(h) = E[φ(-Y h(X)) | Y = -1] = ∫ φ(h) dP⁻`. -/
def phiTypeI {X : Type*} [MeasurableSpace X] (Pm : Measure X) (φ : ℝ → ℝ) (f : X → ℝ) : ℝ :=
∫ x, φ (f x) ∂Pm
/-- Section 2.2 (p. 4): `φ`-type II error `R⁺_φ(h) = E[φ(-Y h(X)) | Y = 1] = ∫ φ(-h) dP⁺`. -/
def phiTypeII {X : Type*} [MeasurableSpace X] (Pp : Measure X) (φ : ℝ → ℝ) (f : X → ℝ) : ℝ :=
∫ x, φ (-f x) ∂Pp
open Classical in
/-- Section 3.1 (p. 5): empirical type I error `R̂⁻(h) = (1/n⁻) ∑_i 1(h(X⁻_i) ≥ 0)` of the
sample points `x i`, `i ∈ s`. -/
def empTypeI {ι X : Type*} (s : Finset ι) (x : ι → X) (f : X → ℝ) : ℝ :=
((s.filter fun i => 0 ≤ f (x i)).card : ℝ) / s.card
/-- Section 3.2 (p. 7): empirical `φ`-type I error `R̂⁻_φ(h) = (1/n⁻) ∑_i φ(h(X⁻_i))` of the sample
points `x i`, `i ∈ s`. -/
def empPhiTypeI {ι X : Type*} (φ : ℝ → ℝ) (s : Finset ι) (x : ι → X) (f : X → ℝ) : ℝ :=
(∑ i ∈ s, φ (f (x i))) / s.card
/-- Section 3.2 (p. 7): empirical `φ`-type II error `R̂⁺_φ(h) = (1/n⁺) ∑_i φ(-h(X⁺_i))` of the
sample points `x i`, `i ∈ s`. -/
def empPhiTypeII {ι X : Type*} (φ : ℝ → ℝ) (s : Finset ι) (x : ι → X) (f : X → ℝ) : ℝ :=
(∑ i ∈ s, φ (-f (x i))) / s.card
/-- Section 3.2 (p. 7): `H^{φ,a} = {h ∈ H^conv : R⁻_φ(h) ≤ a}`. -/
def Hphi {X : Type*} [MeasurableSpace X] {M : ℕ} (h : Fin M → X → ℝ) (Pm : Measure X)
(φ : ℝ → ℝ) (a : ℝ) : Set (X → ℝ) :=
{f | f ∈ HConv h ∧ phiTypeI Pm φ f ≤ a}
/-- Section 3.2 (p. 7): `H^{φ,a}_{n⁻} = {h ∈ H^conv : R̂⁻_φ(h) ≤ a}`. -/
def HphiEmp {ι X : Type*} {M : ℕ} (h : Fin M → X → ℝ) (φ : ℝ → ℝ) (s : Finset ι) (x : ι → X)
(a : ℝ) : Set (X → ℝ) :=
{f | f ∈ HConv h ∧ empPhiTypeI φ s x f ≤ a}
/-- Section 4.2 (p. 8) and Lemma 6.2 (p. 16): `γ(a) = inf_{h ∈ H^{φ,a}} R⁺_φ(h)`, written
`min_{h ∈ H^{φ,a}} R⁺_φ(h)` in the paper. Statements use it only where `H^{φ,a} ≠ ∅`. -/
def minPhiTypeII {X : Type*} [MeasurableSpace X] {M : ℕ} (h : Fin M → X → ℝ)
(Pm Pp : Measure X) (φ : ℝ → ℝ) (a : ℝ) : ℝ :=
sInf (phiTypeII Pp φ '' Hphi h Pm φ a)
/-- Eq. (3.4) (p. 7): `f` is a convexified NP classifier `h̃^τ`, i.e. a solution of
`min { R̂⁺_φ(h) : h ∈ H^conv, R̂⁻_φ(h) ≤ α - τ/√n⁻ }`, where the class `-1` sample is
`(xm i)_{i ∈ sm}` (`n⁻ = |sm|`) and the class `+1` sample is `(xp i)_{i ∈ sp}`. -/
def IsNPClassifier {ι ι' X : Type*} {M : ℕ} (h : Fin M → X → ℝ) (φ : ℝ → ℝ) (α τ : ℝ)
(sm : Finset ι) (xm : ι → X) (sp : Finset ι') (xp : ι' → X) (f : X → ℝ) : Prop :=
f ∈ HphiEmp h φ sm xm (α - τ / Real.sqrt sm.card) ∧
∀ g ∈ HphiEmp h φ sm xm (α - τ / Real.sqrt sm.card),
empPhiTypeII φ sp xp f ≤ empPhiTypeII φ sp xp g
/-- Theorem 4.1 (p. 8) and Theorem 5.1 (p. 12): `κ = 4√2 L √(log(2M/δ))`. -/
def kappa (L : ℝ) (M : ℕ) (δ : ℝ) : ℝ :=
4 * Real.sqrt 2 * L * Real.sqrt (Real.log (2 * M / δ))
/-- Section 4.2 (p. 9): `ε̄`, the smallest `ε` such that `H^{φ, εα} ≠ ∅` (defined under
Assumption 1). -/
def epsBar {X : Type*} [MeasurableSpace X] {M : ℕ} (h : Fin M → X → ℝ) (Pm : Measure X)
(φ : ℝ → ℝ) (α : ℝ) : ℝ :=
sInf {ε : ℝ | (Hphi h Pm φ (ε * α)).Nonempty}
/-- Eq. (4.3) (p. 9): `n₀`, the smallest integer with `n₀ ≥ (4κ / ((1 - ε̄)α))²`. -/
def nZero (κ εbar α : ℝ) : ℕ :=
⌈(4 * κ / ((1 - εbar) * α)) ^ 2⌉₊
open Classical in
/-- Section 4.3 (p. 10): the indices `i` with label `Y_i = c`; for `c = -1` its cardinality is
`N⁻ = card{Y_i : Y_i = -1}`, for `c = 1` it is `N⁺`. -/
def labelIdx {n : ℕ} (y : Fin n → ℝ) (c : ℝ) : Finset (Fin n) :=
Finset.univ.filter fun i => y i = c
open Classical in
/-- Section 6.6 (pp. 19-20): `P(N ≥ t)` for `N` binomial with parameters `n` and `q`,
`∑_{j ≤ n, j ≥ t} C(n,j) q^j (1-q)^(n-j)`. -/
def binomTail (n : ℕ) (q t : ℝ) : ℝ :=
∑ j ∈ (Finset.range (n + 1)).filter (fun j : ℕ => t ≤ (j : ℝ)),
(n.choose j : ℝ) * q ^ j * (1 - q) ^ (n - j)
/-- Eq. (5.1) (p. 11): `λ` is feasible for the chance constrained program, i.e. `λ ∈ Λ` and
`P{F(λ, ξ) ≤ 0} ≥ 1 - α`, with `F(λ, ξ) = ∑_j λ_j g_j(ξ)` and `ξ ∼ Pxi`. -/
def IsChanceFeasible {Xi : Type*} [MeasurableSpace Xi] {M : ℕ} (g : Fin M → Xi → ℝ)
(Pxi : Measure Xi) (α : ℝ) (lam : Fin M → ℝ) : Prop :=
lam ∈ stdSimplex ℝ (Fin M) ∧ ENNReal.ofReal (1 - α) ≤ Pxi {x | convComb g lam x ≤ 0}
/-- Section 5 (p. 12): `λ̃` solves the sampled convexified program
`min_{λ ∈ Λ} f(λ)` s.t. `∑_{i=1}^n φ(F(λ, ξ_i)) ≤ nα - κ√n`. -/
def IsScenarioSolution {Xi : Type*} {M n : ℕ} (g : Fin M → Xi → ℝ) (φ : ℝ → ℝ)
(f : (Fin M → ℝ) → ℝ) (α κ : ℝ) (xs : Fin n → Xi) (lam : Fin M → ℝ) : Prop :=
lam ∈ stdSimplex ℝ (Fin M) ∧
∑ i, φ (convComb g lam (xs i)) ≤ n * α - κ * Real.sqrt n ∧
∀ l ∈ stdSimplex ℝ (Fin M), ∑ i, φ (convComb g l (xs i)) ≤ n * α - κ * Real.sqrt n →
f lam ≤ f l
/-- Eq. (5.7) (p. 12): `f*_φ`, the optimal value of `min_{λ ∈ Λ} f(λ)` s.t.
`E[φ(F(λ, ξ))] ≤ α` (an infimum; used only where the constraint is feasible). -/
def ccpOptValue {Xi : Type*} [MeasurableSpace Xi] {M : ℕ} (g : Fin M → Xi → ℝ)
(Pxi : Measure Xi) (φ : ℝ → ℝ) (f : (Fin M → ℝ) → ℝ) (α : ℝ) : ℝ :=
sInf (f '' {l | l ∈ stdSimplex ℝ (Fin M) ∧ phiTypeI Pxi φ (convComb g l) ≤ α})
end NeymanPearsonClassification
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Convex surrogate (). For a function , " is a convex surrogate" means all five of the following hold:
- for every ;
- is non-decreasing on , i.e. in implies ;
- is continuous on ;
- is convex on ;
- .
Nothing is required of outside . It may be negative, decreasing, discontinuous or non-convex there.
Base family (). Let be a measurable space and . A family is a base family if, for every index :
- is measurable, with the Borel -algebra on ;
- for all .
When the family is empty and the condition holds vacuously.
Linear combination (). For functions and an arbitrary vector , define
No sign or sum constraint is placed on here. For , is the zero function.
Convex hull class (). Let be the standard simplex. Define
The are not required to be a base family. For the simplex is empty, so .
Type I error (). For an arbitrary measure on (not required to be a probability measure) and any , define
converted to a real number. If this measure is , the value is . If is not measurable, the set is measured by the outer measure. Points with count as errors.
Type II error (). For an arbitrary measure on and any , define
as a real number, with the same conventions as above. Here too counts as an error, so the boundary is counted in both and .
-type I error (). For any measure , any and any , define
This is a Bochner integral: if is not integrable, the value is defined to be . The function is evaluated at even when .
-type II error (). Define
with the same convention that a non-integrable integrand gives the value .
Empirical type I error (). Take an index set of any type, a finite set of indices, sample points and any . Define
If , the value is .
Empirical -type I error (). Define
The value is if .
Empirical -type II error (). Define
The value is if .
Constrained class (). For a family , a measure , a function and any real , define
using the integral convention above. No assumption is made on or .
Empirical constrained class (). For a finite index set , sample points and a real , define
If , the constraint becomes , so the class is all of when and empty when .
Optimal -type II value (). For measures and , a function and a real , define
This is a real infimum, not necessarily attained. By convention it equals if is empty, or if the set of values is unbounded below.
NP classifier (). The inputs are:
- a family , a function and reals ;
- a "class " sample , with a finite index set of any type;
- a "class " sample , with a finite index set of a possibly different type;
- a function .
Set the threshold
When , the convention gives . Then is an NP classifier if both of the following hold:
- , that is, and on the class sample;
- on the class sample for every .
Some edge cases:
- If , every empirical type II value is , so every feasible qualifies.
- If , feasibility reduces to and .
- No convex surrogate or base family assumption is made.
Constant (). For reals and , define
The real-number conventions produce these cases:
- means , with .
- of a negative number is .
- when .
Consequently, whenever , or , or . In particular this includes every . The constant may be any real number, including a negative one.
Constant (). For a family , a measure , a function and a real , define
This is a real infimum, not necessarily attained, and it is if the set is empty or unbounded below. In particular:
- whenever .
- whenever .
Sample-size threshold (). For arbitrary reals , define
If , division by zero yields , so . The inputs are free reals and are not tied to the definitions above.
Label index set (). For , real labels and any real , define
The labels are arbitrary reals; they are not restricted to .
Binomial tail (). For and arbitrary reals , define
with . The parameter is not restricted to . The extreme cases are:
- If , the sum equals .
- If , the sum equals .
Chance-constraint feasibility (). Let be a measurable space, be arbitrary functions, be an arbitrary measure (not necessarily a probability measure), and be real. A vector is feasible if both of the following hold:
- ;
- the measure of the region where the combination is non-positive satisfies
compared in .
When , the second condition is automatic, so feasibility reduces to .
Scenario solution (). The inputs are:
- functions and a function ;
- an arbitrary objective , with no continuity or convexity assumed;
- reals ;
- sample points .
A vector is a scenario solution if all three of the following hold:
- ;
- it satisfies the sampled constraint
- for every that satisfies the same constraint.
When , the constraint reads and always holds. In that case is simply a minimizer of over all of .
Optimal value of the convexified program (). For a measure , a function , an objective and a real , define
The integral is when its integrand is not integrable. The infimum is a real infimum, not necessarily attained, and it is if the feasible set is empty or the values are unbounded below.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.