Open category detection: score mixtures, the alien-CDF estimate and Algorithm 1
DefinitionOpenCategoryPAC_BasicObjects of Sections 3–5 and Algorithm 1 of Liu, Garrepalli, Dietterich, Fern and Hendrycks (2018).
- Score laws. A fixed anomaly detector assigns real scores. is the law of the score of a nominal point () and that of an alien point (). For , the mixture law is (). The CDFs are , with .
- Recovered alien CDF. For functions and a weight , . With the true weight it equals ; with a weight it is the function of Appendix B.
- Empirical CDFs and eq. (1). For scores , . From the nominal scores (sample ) and the mixture scores (sample ), .
- Uniform distance. .
- Algorithm 1. With , the returned threshold is .
- Alien detection rate of a threshold : the fraction of aliens whose score exceeds , i.e. that raise an alarm.
- Sample-size bound of Theorem 1: .
- Sampling law. consists of i.i.d. draws from and, independently, of i.i.d. draws from : the law on .
- Admissibility (Section 5): for all .
Formalization Note The CDF is Mathlib's cdf. The supremum is a real supremum and is only applied to bounded functions (CDFs and their estimates). The independence of and , used in Appendix A but not written in the paper, is built into the product sampling law.
import Mathlib
/-!
Liu, Garrepalli, Dietterich, Fern and Hendrycks, *Open Category Detection with PAC Guarantees*,
arXiv:1808.00529v1 (ICML 2018): the nominal/alien mixture of anomaly-score distributions
(Sections 3–4), the empirical CDFs and the alien-CDF estimate of eq. (1), the threshold returned
by Algorithm 1, the alien detection rate, the sample-size bound and the sampling law of
Theorem 1, and admissibility (Section 5).
-/
open MeasureTheory ProbabilityTheory
noncomputable section
namespace OpenCategoryPAC
/-- Section 4, p. 3: the empirical CDF `F̂(t) = (1/n) #{i : x_i ≤ t}` of the anomaly scores
`x_1, …, x_n` of a sample. -/
def empCDF {n : ℕ} (x : Fin n → ℝ) (t : ℝ) : ℝ :=
((Finset.univ.filter fun i => x i ≤ t).card : ℝ) / n
/-- Section 4, p. 3: the mixture `D_m` of the nominal score law `μ0` (weight `1 - α`) and the
alien score law `μa` (weight `α`). -/
def mixture (α : ℝ) (μ0 μa : Measure ℝ) : Measure ℝ :=
ENNReal.ofReal (1 - α) • μ0 + ENNReal.ofReal α • μa
/-- Section 4, p. 3 (display after `F_m = (1-α)F_0 + αF_a`) and Appendix B, p. 8: the alien CDF
recovered from a mixture CDF `Fm` and a nominal CDF `F0` with mixture weight `α`,
`(Fm(t) - (1 - α) F0(t)) / α`. -/
def alienCDF (α : ℝ) (Fm F0 : ℝ → ℝ) (t : ℝ) : ℝ :=
(Fm t - (1 - α) * F0 t) / α
/-- Eq. (1), p. 3: the estimate `F̂a = (F̂m - (1 - α) F̂0) / α` computed from the nominal scores `x`
(the sample `S_0`) and the mixture scores `y` (the sample `S_m`). -/
def alienCDFEst (α : ℝ) {n : ℕ} (x y : Fin n → ℝ) (t : ℝ) : ℝ :=
alienCDF α (empCDF y) (empCDF x) t
/-- Appendix A, p. 8: the uniform distance `sup_t |F(t) - G(t)|` between two functions
(used only for bounded functions such as CDFs and their estimates). -/
def supDev (F G : ℝ → ℝ) : ℝ :=
⨆ t : ℝ, |F t - G t|
/-- Algorithm 1, step 4, p. 3: the pooled score set `S = {x_1, …, x_k, y_1, …, y_n}`. -/
def pooledScores {n : ℕ} (x y : Fin n → ℝ) : Set ℝ :=
Set.range x ∪ Set.range y
/-- Algorithm 1, step 4, p. 3: `τ` is the threshold returned by Algorithm 1 run with mixture
weight `α` and input `q`, i.e. `τ = max {u ∈ S : F̂a(u) ≤ q}`. -/
def IsAlgThreshold (α q : ℝ) {n : ℕ} (x y : Fin n → ℝ) (τ : ℝ) : Prop :=
τ ∈ pooledScores x y ∧ alienCDFEst α x y τ ≤ q ∧
∀ u ∈ pooledScores x y, alienCDFEst α x y u ≤ q → u ≤ τ
/-- Section 3, p. 3: the alien detection rate of a threshold `τ`, the fraction of aliens (score law
`μa`) whose anomaly score exceeds `τ`, so that they raise an alarm. -/
def detectionRate (μa : Measure ℝ) (τ : ℝ) : ℝ :=
(μa (Set.Ioi τ)).toReal
/-- Theorem 1, p. 4: the sample-size bound
`(1/2) ln (2 / (1 - √(1 - δ))) (1/ε)² ((2 - α)/α)²`. -/
def sampleSizeBound (α ε δ : ℝ) : ℝ :=
1 / 2 * Real.log (2 / (1 - Real.sqrt (1 - δ))) * (1 / ε) ^ 2 * ((2 - α) / α) ^ 2
/-- Theorem 1, p. 4: the law of the two training samples, `S_0` of `n` i.i.d. nominal scores
(law `μ0`) and, independently, `S_m` of `n` i.i.d. mixture scores (law `μm`). -/
def sampleLaw (μ0 μm : Measure ℝ) [SigmaFinite μm] (n : ℕ) :
Measure ((Fin n → ℝ) × (Fin n → ℝ)) :=
(Measure.pi fun _ : Fin n => μ0).prod (Measure.pi fun _ : Fin n => μm)
/-- Section 5, p. 4: the anomaly detector is admissible if the score CDFs satisfy
`F_0(x) ≥ F_m(x)` for all `x ∈ ℝ`. -/
def IsAdmissible (μ0 μm : Measure ℝ) : Prop :=
∀ t : ℝ, cdf μm t ≤ cdf μ0 t
end OpenCategoryPAC
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Empirical CDF (empCDF). Given a natural number , a list of real numbers indexed by , and a real number , this defines
The count is over indices, so repeated values are counted with multiplicity, and the inequality is non-strict (). When the expression is , which is defined to be . So for the empty sample, for every .
Mixture measure (mixture). Given any real number and any two measures on , this defines the measure
where is the truncation of a real number to a non-negative weight.
- No hypothesis requires .
- If , the weight on is .
- If , the weight on is and the weight on is .
- and are not required to be probability measures, finite, or even σ-finite.
- Consequently need not be a probability measure.
Alien CDF from a mixture (alienCDF). Given a real number , two arbitrary functions and a real number , this defines
- and are not required to be CDFs or to have any properties at all.
- When the division by zero is defined to give , so in that case.
Estimated alien CDF (alienCDFEst). Given a real number , a natural number , two samples and of the same length , and , this defines
where and are the empirical CDFs defined above. The sample plays the role of the mixture sample and that of the nominal sample.
- It equals for every when .
- It also equals for every when , since both empirical CDFs are then .
- The two samples are forced to have equal size.
Uniform distance (supDev). For any two functions , this is the real-valued supremum
No boundedness is assumed. Under the convention used for real suprema, if the set is unbounded above, the value is rather than . Otherwise it is the ordinary least upper bound, which is .
Pooled scores (pooledScores). For two samples of length , this is the set
that is, the set of all values taken by either sample, with duplicates merged. When , .
Algorithm threshold (IsAlgThreshold). Given real numbers and , samples of common length , and a real number , the proposition " is an algorithm threshold" holds exactly when all three of the following are true:
- , the pooled score set above;
- , with the estimated alien CDF built from (nominal), (mixture) and ;
- every with satisfies .
Equivalently, . This is a property of , not a definition of a value, and some inputs admit no such :
- If no element of satisfies , no qualifies.
- In particular, no qualifies when , since then is empty.
- No constraints are placed on or (for example, makes ).
Detection rate (detectionRate). For a measure on and a real number , this is
converted to a real number. The interval is open, so the score must strictly exceed . is not assumed to be a probability measure. If , the conversion to a real number yields .
Sample-size bound (sampleSizeBound). For arbitrary real numbers , this is
No ranges are imposed on the parameters, so the following junk conventions apply:
- Division by zero gives , so whenever or .
- If , then , the fraction , and is taken to be , so .
- If , the square root of a negative number is taken to be , so the logarithm term is .
- If , then , and the logarithm of a negative number is taken to be the logarithm of its absolute value.
Sampling law (sampleLaw). Given measures on , with assumed σ-finite, and a natural number , this is the measure on pairs
Here is the -fold product measure on , so the coordinates of are i.i.d. with law and the coordinates of are i.i.d. with law , independently of .
- No assumption is made on : it need not be σ-finite or a probability measure.
- need not be a probability measure.
- is an arbitrary measure, not required to be the mixture defined above.
- When , the space is a single point and the measure is the unit point mass there.
Admissibility (IsAdmissible). For measures on , the pair is called admissible when
where denotes Mathlib's cumulative distribution function of .
- When is a probability measure, .
- When is not a probability measure, is still some non-decreasing right-continuous function with limits and , but in general it is not .
- No probability, finiteness, or mixture relationship between and is assumed.
- The inequality is non-strict and points in the direction .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.