Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Open category detection: score mixtures, the alien-CDF estimate and Algorithm 1

Definition
OpenCategoryPAC_Basic

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

anomaly-detectiondkw-inequalityempirical-cdfopen-category-detectionpac-guarantee

Objects of Sections 3–5 and Algorithm 1 of Liu, Garrepalli, Dietterich, Fern and Hendrycks (2018).

  1. Score laws. A fixed anomaly detector assigns real scores. μ0\mu_0μ0​ is the law of the score of a nominal point (D0D_0D0​) and μa\mu_aμa​ that of an alien point (DaD_aDa​). For α∈(0,1]\alpha\in(0,1]α∈(0,1], the mixture law is μm=(1−α)μ0+αμa\mu_m=(1-\alpha)\mu_0+\alpha\mu_aμm​=(1−α)μ0​+αμa​ (DmD_mDm​). The CDFs are F0,Fa,FmF_0,F_a,F_mF0​,Fa​,Fm​, with F(t)=μ((−∞,t])F(t)=\mu((-\infty,t])F(t)=μ((−∞,t]).
  2. Recovered alien CDF. For functions Fm,F0F_m,F_0Fm​,F0​ and a weight α\alphaα, Fm(t)−(1−α)F0(t)α\dfrac{F_m(t)-(1-\alpha)F_0(t)}{\alpha}αFm​(t)−(1−α)F0​(t)​. With the true weight it equals FaF_aFa​; with a weight α′\alpha'α′ it is the function Fa′F'_aFa′​ of Appendix B.
  3. Empirical CDFs and eq. (1). For scores x1,…,xnx_1,\dots,x_nx1​,…,xn​, F^(t)=1n#{i:xi≤t}\hat F(t)=\frac1n\#\{i:x_i\le t\}F^(t)=n1​#{i:xi​≤t}. From the nominal scores xxx (sample S0S_0S0​) and the mixture scores yyy (sample SmS_mSm​), F^a(t)=F^m(t)−(1−α)F^0(t)α\hat F_a(t)=\dfrac{\hat F_m(t)-(1-\alpha)\hat F_0(t)}{\alpha}F^a​(t)=αF^m​(t)−(1−α)F^0​(t)​.
  4. Uniform distance. sup⁡t∈R∣F(t)−G(t)∣\sup_{t\in\mathbb R}|F(t)-G(t)|supt∈R​∣F(t)−G(t)∣.
  5. Algorithm 1. With S={x1,…,xn,y1,…,yn}S=\{x_1,\dots,x_n,y_1,\dots,y_n\}S={x1​,…,xn​,y1​,…,yn​}, the returned threshold is τ^q=max⁡{u∈S:F^a(u)≤q}\hat\tau_q=\max\{u\in S:\hat F_a(u)\le q\}τ^q​=max{u∈S:F^a​(u)≤q}.
  6. Alien detection rate of a threshold τ\tauτ: the fraction μa((τ,∞))\mu_a((\tau,\infty))μa​((τ,∞)) of aliens whose score exceeds τ\tauτ, i.e. that raise an alarm.
  7. Sample-size bound of Theorem 1: 12ln⁡21−1−δ(1ϵ)2(2−αα)2\frac12\ln\frac{2}{1-\sqrt{1-\delta}}\left(\frac1\epsilon\right)^2\left(\frac{2-\alpha}{\alpha}\right)^221​ln1−1−δ​2​(ϵ1​)2(α2−α​)2.
  8. Sampling law. S0S_0S0​ consists of nnn i.i.d. draws from μ0\mu_0μ0​ and, independently, SmS_mSm​ of nnn i.i.d. draws from μm\mu_mμm​: the law μ0⊗n⊗μm⊗n\mu_0^{\otimes n}\otimes\mu_m^{\otimes n}μ0⊗n​⊗μm⊗n​ on Rn×Rn\mathbb R^n\times\mathbb R^nRn×Rn.
  9. Admissibility (Section 5): F0(t)≥Fm(t)F_0(t)\ge F_m(t)F0​(t)≥Fm​(t) for all t∈Rt\in\mathbb Rt∈R.

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 S0S_0S0​ and SmS_mSm​, used in Appendix A but not written in the paper, is built into the product sampling law.

Definition code
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
Source
Liu, Garrepalli, Dietterich, Fern and Hendrycks, Open Category Detection with PAC Guarantees, arXiv:1808.00529v1 (ICML 2018), Sections 3-5 (pp. 3-4), eq. (1), Algorithm 1, Theorem 1, Appendices A-B (p. 8)
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Empirical CDF F^x(t)\widehat F_x(t)Fx​(t) (empCDF). Given a natural number nnn, a list of nnn real numbers x=(x1,…,xn)x = (x_1,\dots,x_n)x=(x1​,…,xn​) indexed by {1,…,n}\{1,\dots,n\}{1,…,n}, and a real number ttt, this defines

F^x(t)  =  #{ i∈{1,…,n}:xi≤t }n.\widehat F_x(t) \;=\; \frac{\#\{\, i \in \{1,\dots,n\} : x_i \le t \,\}}{n}.Fx​(t)=n#{i∈{1,…,n}:xi​≤t}​.

The count is over indices, so repeated values are counted with multiplicity, and the inequality is non-strict (xi≤tx_i \le txi​≤t). When n=0n = 0n=0 the expression is 0/00/00/0, which is defined to be 000. So for the empty sample, F^x(t)=0\widehat F_x(t) = 0Fx​(t)=0 for every ttt.


Mixture measure DmD_mDm​ (mixture). Given any real number α\alphaα and any two measures μ0,μa\mu_0, \mu_aμ0​,μa​ on R\mathbb RR, this defines the measure

Dm  =  (1−α)+ μ0  +  α+ μa,D_m \;=\; (1-\alpha)^+ \,\mu_0 \;+\; \alpha^+ \,\mu_a ,Dm​=(1−α)+μ0​+α+μa​,

where r+=max⁡(r,0)r^+ = \max(r,0)r+=max(r,0) is the truncation of a real number to a non-negative weight.

  • No hypothesis requires α∈[0,1]\alpha \in [0,1]α∈[0,1].
  • If α>1\alpha > 1α>1, the weight on μ0\mu_0μ0​ is 000.
  • If α<0\alpha < 0α<0, the weight on μa\mu_aμa​ is 000 and the weight on μ0\mu_0μ0​ is 1−α>11-\alpha > 11−α>1.
  • μ0\mu_0μ0​ and μa\mu_aμa​ are not required to be probability measures, finite, or even σ-finite.
  • Consequently DmD_mDm​ need not be a probability measure.

Alien CDF from a mixture FaF_aFa​ (alienCDF). Given a real number α\alphaα, two arbitrary functions Fm,F0:R→RF_m, F_0 : \mathbb R \to \mathbb RFm​,F0​:R→R and a real number ttt, this defines

Fa(t)  =  Fm(t)−(1−α) F0(t)α.F_a(t) \;=\; \frac{F_m(t) - (1-\alpha)\,F_0(t)}{\alpha}.Fa​(t)=αFm​(t)−(1−α)F0​(t)​.
  • FmF_mFm​ and F0F_0F0​ are not required to be CDFs or to have any properties at all.
  • When α=0\alpha = 0α=0 the division by zero is defined to give 000, so Fa≡0F_a \equiv 0Fa​≡0 in that case.

Estimated alien CDF F^a\widehat F_aFa​ (alienCDFEst). Given a real number α\alphaα, a natural number nnn, two samples x=(x1,…,xn)x = (x_1,\dots,x_n)x=(x1​,…,xn​) and y=(y1,…,yn)y = (y_1,\dots,y_n)y=(y1​,…,yn​) of the same length nnn, and t∈Rt \in \mathbb Rt∈R, this defines

F^a(t)  =  F^y(t)−(1−α) F^x(t)α,\widehat F_a(t) \;=\; \frac{\widehat F_y(t) - (1-\alpha)\,\widehat F_x(t)}{\alpha},Fa​(t)=αFy​(t)−(1−α)Fx​(t)​,

where F^y\widehat F_yFy​ and F^x\widehat F_xFx​ are the empirical CDFs defined above. The sample yyy plays the role of the mixture sample and xxx that of the nominal sample.

  • It equals 000 for every ttt when α=0\alpha = 0α=0.
  • It also equals 000 for every ttt when n=0n = 0n=0, since both empirical CDFs are then 000.
  • The two samples are forced to have equal size.

Uniform distance supDev⁡(F,G)\operatorname{supDev}(F,G)supDev(F,G) (supDev). For any two functions F,G:R→RF, G : \mathbb R \to \mathbb RF,G:R→R, this is the real-valued supremum

supDev⁡(F,G)  =  sup⁡t∈R ∣F(t)−G(t)∣.\operatorname{supDev}(F,G) \;=\; \sup_{t \in \mathbb R} \,\lvert F(t) - G(t)\rvert .supDev(F,G)=t∈Rsup​∣F(t)−G(t)∣.

No boundedness is assumed. Under the convention used for real suprema, if the set {∣F(t)−G(t)∣:t∈R}\{\lvert F(t)-G(t)\rvert : t\in\mathbb R\}{∣F(t)−G(t)∣:t∈R} is unbounded above, the value is 000 rather than +∞+\infty+∞. Otherwise it is the ordinary least upper bound, which is ≥0\ge 0≥0.


Pooled scores SSS (pooledScores). For two samples x,yx, yx,y of length nnn, this is the set

S  =  {x1,…,xn} ∪ {y1,…,yn}  ⊆  R,S \;=\; \{x_1,\dots,x_n\} \,\cup\, \{y_1,\dots,y_n\} \;\subseteq\; \mathbb R ,S={x1​,…,xn​}∪{y1​,…,yn​}⊆R,

that is, the set of all values taken by either sample, with duplicates merged. When n=0n = 0n=0, S=∅S = \varnothingS=∅.


Algorithm threshold τ\tauτ (IsAlgThreshold). Given real numbers α\alphaα and qqq, samples x,yx, yx,y of common length nnn, and a real number τ\tauτ, the proposition "τ\tauτ is an algorithm threshold" holds exactly when all three of the following are true:

  1. τ∈S\tau \in Sτ∈S, the pooled score set above;
  2. F^a(τ)≤q\widehat F_a(\tau) \le qFa​(τ)≤q, with F^a\widehat F_aFa​ the estimated alien CDF built from xxx (nominal), yyy (mixture) and α\alphaα;
  3. every u∈Su \in Su∈S with F^a(u)≤q\widehat F_a(u) \le qFa​(u)≤q satisfies u≤τu \le \tauu≤τ.

Equivalently, τ=max⁡{u∈S:F^a(u)≤q}\tau = \max\{u \in S : \widehat F_a(u) \le q\}τ=max{u∈S:Fa​(u)≤q}. This is a property of τ\tauτ, not a definition of a value, and some inputs admit no such τ\tauτ:

  • If no element of SSS satisfies F^a(u)≤q\widehat F_a(u) \le qFa​(u)≤q, no τ\tauτ qualifies.
  • In particular, no τ\tauτ qualifies when n=0n = 0n=0, since then SSS is empty.
  • No constraints are placed on α\alphaα or qqq (for example, α=0\alpha = 0α=0 makes F^a≡0\widehat F_a \equiv 0Fa​≡0).

Detection rate DR⁡μa(τ)\operatorname{DR}_{\mu_a}(\tau)DRμa​​(τ) (detectionRate). For a measure μa\mu_aμa​ on R\mathbb RR and a real number τ\tauτ, this is

DR⁡μa(τ)  =  μa((τ,∞)),\operatorname{DR}_{\mu_a}(\tau) \;=\; \mu_a\big((\tau,\infty)\big),DRμa​​(τ)=μa​((τ,∞)),

converted to a real number. The interval is open, so the score must strictly exceed τ\tauτ. μa\mu_aμa​ is not assumed to be a probability measure. If μa((τ,∞))=∞\mu_a((\tau,\infty)) = \inftyμa​((τ,∞))=∞, the conversion to a real number yields 000.


Sample-size bound N(α,ε,δ)N(\alpha,\varepsilon,\delta)N(α,ε,δ) (sampleSizeBound). For arbitrary real numbers α,ε,δ\alpha, \varepsilon, \deltaα,ε,δ, this is

N(α,ε,δ)  =  12 ln⁡ ⁣(21−1−δ)(1ε)2(2−αα)2.N(\alpha,\varepsilon,\delta) \;=\; \frac12 \,\ln\!\left(\frac{2}{1-\sqrt{1-\delta}}\right)\left(\frac1\varepsilon\right)^{2}\left(\frac{2-\alpha}{\alpha}\right)^{2}.N(α,ε,δ)=21​ln(1−1−δ​2​)(ε1​)2(α2−α​)2.

No ranges are imposed on the parameters, so the following junk conventions apply:

  • Division by zero gives 000, so N=0N = 0N=0 whenever ε=0\varepsilon = 0ε=0 or α=0\alpha = 0α=0.
  • If δ=0\delta = 0δ=0, then 1−1=01 - \sqrt{1} = 01−1​=0, the fraction 2/0=02/0 = 02/0=0, and ln⁡0\ln 0ln0 is taken to be 000, so N=0N = 0N=0.
  • If δ>1\delta > 1δ>1, the square root of a negative number is taken to be 000, so the logarithm term is ln⁡2\ln 2ln2.
  • If δ<0\delta < 0δ<0, then 1−1−δ<01-\sqrt{1-\delta} < 01−1−δ​<0, and the logarithm of a negative number is taken to be the logarithm of its absolute value.

Sampling law Pμ0,μmnP^n_{\mu_0,\mu_m}Pμ0​,μm​n​ (sampleLaw). Given measures μ0,μm\mu_0, \mu_mμ0​,μm​ on R\mathbb RR, with μm\mu_mμm​ assumed σ-finite, and a natural number nnn, this is the measure on pairs (x,y)∈Rn×Rn(x,y) \in \mathbb R^n \times \mathbb R^n(x,y)∈Rn×Rn

Pμ0,μmn  =  μ0⊗n⊗μm⊗n.P^n_{\mu_0,\mu_m} \;=\; \mu_0^{\otimes n} \otimes \mu_m^{\otimes n} .Pμ0​,μm​n​=μ0⊗n​⊗μm⊗n​.

Here μ⊗n\mu^{\otimes n}μ⊗n is the nnn-fold product measure on Rn\mathbb R^nRn, so the coordinates of xxx are i.i.d. with law μ0\mu_0μ0​ and the coordinates of yyy are i.i.d. with law μm\mu_mμm​, independently of xxx.

  • No assumption is made on μ0\mu_0μ0​: it need not be σ-finite or a probability measure.
  • μm\mu_mμm​ need not be a probability measure.
  • μm\mu_mμm​ is an arbitrary measure, not required to be the mixture DmD_mDm​ defined above.
  • When n=0n = 0n=0, the space R0×R0\mathbb R^0 \times \mathbb R^0R0×R0 is a single point and the measure is the unit point mass there.

Admissibility (IsAdmissible). For measures μ0,μm\mu_0, \mu_mμ0​,μm​ on R\mathbb RR, the pair is called admissible when

Fμm(t)  ≤  Fμ0(t)for every t∈R,F_{\mu_m}(t) \;\le\; F_{\mu_0}(t) \qquad \text{for every } t \in \mathbb R,Fμm​​(t)≤Fμ0​​(t)for every t∈R,

where FμF_\muFμ​ denotes Mathlib's cumulative distribution function of μ\muμ.

  • When μ\muμ is a probability measure, Fμ(t)=μ((−∞,t])F_\mu(t) = \mu((-\infty,t])Fμ​(t)=μ((−∞,t]).
  • When μ\muμ is not a probability measure, FμF_\muFμ​ is still some non-decreasing right-continuous function with limits 000 and 111, but in general it is not t↦μ((−∞,t])t \mapsto \mu((-\infty,t])t↦μ((−∞,t]).
  • No probability, finiteness, or mixture relationship between μ0\mu_0μ0​ and μm\mu_mμm​ is assumed.
  • The inequality is non-strict and points in the direction Fμm≤Fμ0F_{\mu_m} \le F_{\mu_0}Fμm​​≤Fμ0​​.
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