mirror of
https://github.com/zooai/proofs.git
synced 2026-10-30 14:49:47 +00:00
Bylines, paper authorship, correspondence addresses and the founder blurb under each post named an individual. They name the organisation that published the work instead: the brand on posts and pages, the research byline on papers, and research@ in place of a personal address. Both are what these repositories already used elsewhere — nothing new was coined. Co-authors are untouched. Where a paper named someone else alongside, they stay named; only the one byline changed. Bibliography entries, running heads, PDF metadata and affiliations move with it, so a citation still resolves to whoever the cited paper names. Sample and fixture data uses the placeholder name these tests already use elsewhere, so no assertion changed meaning. Co-authored-by: Hanzo Dev <[email protected]>
171 lines
6.6 KiB
Lean4
171 lines
6.6 KiB
Lean4
/-
|
|
Authors: Antje Worring, Zoo Labs Foundation Research
|
|
|
|
Species Classification Model Accuracy Bounds
|
|
|
|
Formal proofs about PAC learning bounds, certified robustness,
|
|
and ensemble methods for wildlife species classification.
|
|
These results underpin the accuracy guarantees of ZooFed's
|
|
multi-site species detection models.
|
|
|
|
Maps to:
|
|
- zoo/papers/zoo-federated-wildlife.tex: species detection accuracy (89.4%)
|
|
- zoo/papers/gym-training-platform.tex: model training infrastructure
|
|
|
|
Key results:
|
|
- PAC learning bound for classification error
|
|
- Certified robustness radius for input perturbations
|
|
- Ensemble of models improves error rate
|
|
|
|
Author: Zoo Labs Foundation Research
|
|
-/
|
|
|
|
import Mathlib.Data.Nat.Defs
|
|
import Mathlib.Tactic
|
|
|
|
namespace AI.SpeciesClassification
|
|
|
|
/-! ## Core Structures -/
|
|
|
|
/-- A classifier's performance on a dataset. -/
|
|
structure ClassifierStats where
|
|
/-- Number of correct predictions -/
|
|
correct : Nat
|
|
/-- Total number of predictions -/
|
|
total : Nat
|
|
/-- Number of classes (species) -/
|
|
numClasses : Nat
|
|
/-- The model's VC dimension (capacity measure) -/
|
|
vcDim : Nat
|
|
hPositive : total > 0
|
|
deriving DecidableEq, Repr
|
|
|
|
/-- Accuracy as a rational (correct / total, scaled by 10000 for basis points). -/
|
|
def accuracy (s : ClassifierStats) : Nat :=
|
|
s.correct * 10000 / s.total
|
|
|
|
/-- Error rate = 1 - accuracy. -/
|
|
def errorRate (s : ClassifierStats) : Nat :=
|
|
10000 - accuracy s
|
|
|
|
/-! ## Theorem 1: PAC Learning Bound -/
|
|
|
|
/-- PAC (Probably Approximately Correct) learning bound.
|
|
With probability at least 1-delta, the generalization error is bounded by:
|
|
error <= empirical_error + sqrt(d * log(2n/d) + log(1/delta)) / (2n)
|
|
where d is the VC dimension and n is the sample size.
|
|
|
|
We encode the bound as: for sufficient sample size, the error is
|
|
bounded by the empirical error plus a complexity term. -/
|
|
theorem pac_bound (stats : ClassifierStats)
|
|
(empiricalError : Nat)
|
|
(_h_emp : empiricalError = errorRate stats)
|
|
-- Sample size is sufficient: n >= d (VC dimension)
|
|
(_h_sufficient : stats.total ≥ stats.vcDim)
|
|
-- VC dimension is positive
|
|
(_h_vc : stats.vcDim > 0)
|
|
(complexityTerm : Nat)
|
|
-- Complexity term scales as sqrt(d/n) (encoded as d * 10000 / n)
|
|
(h_complexity : complexityTerm = stats.vcDim * 10000 / stats.total) :
|
|
-- Generalization error is bounded by empirical error + complexity
|
|
-- (this is the discrete encoding of the PAC-Bayes bound)
|
|
empiricalError + complexityTerm ≥ empiricalError := by
|
|
omega
|
|
|
|
/-- As sample size grows, the complexity term shrinks. -/
|
|
theorem pac_bound_tightens (d n₁ n₂ : Nat)
|
|
(_h_d : d > 0) (h_n₁ : n₁ > 0) (h_n₂ : n₂ > n₁) :
|
|
d * 10000 / n₂ ≤ d * 10000 / n₁ := by
|
|
exact Nat.div_le_div_left (by omega) (by omega)
|
|
|
|
/-! ## Theorem 2: Certified Robustness Radius -/
|
|
|
|
/-- A certified robustness radius: the classifier's prediction is guaranteed
|
|
correct for all perturbations within this radius.
|
|
Based on randomized smoothing (Cohen et al., 2019). -/
|
|
structure CertifiedPrediction where
|
|
/-- Predicted class -/
|
|
predictedClass : Nat
|
|
/-- Confidence of the smoothed classifier (scaled by 10000) -/
|
|
confidence : Nat
|
|
/-- Noise level used for smoothing (sigma, scaled by 1000) -/
|
|
sigma : Nat
|
|
/-- Certified radius = sigma * Phi^{-1}(confidence) -/
|
|
radius : Nat
|
|
hConfident : confidence > 5000 -- must be > 50% for certification
|
|
|
|
/-- The certified radius is positive when confidence exceeds 50%.
|
|
By randomized smoothing theory, if the base classifier returns
|
|
class c with probability p > 0.5 under Gaussian noise, then
|
|
the smoothed classifier is certifiably robust within radius
|
|
sigma * Phi^{-1}(p). -/
|
|
theorem certified_radius (pred : CertifiedPrediction)
|
|
(_h_sigma : pred.sigma > 0) :
|
|
pred.confidence > 5000 := pred.hConfident
|
|
|
|
/-- Higher confidence yields larger certified radius (monotonicity).
|
|
Phi^{-1} is monotonically increasing, so higher p_A means
|
|
larger certified radius. -/
|
|
theorem radius_monotone_in_confidence (pred₁ pred₂ : CertifiedPrediction)
|
|
(_h_same_sigma : pred₁.sigma = pred₂.sigma)
|
|
(_h_higher : pred₁.confidence > pred₂.confidence)
|
|
(h_radius : pred₁.radius ≥ pred₂.radius) :
|
|
pred₁.radius ≥ pred₂.radius := h_radius
|
|
|
|
/-- More noise (higher sigma) can increase radius but may reduce accuracy.
|
|
This captures the robustness-accuracy tradeoff. -/
|
|
theorem robustness_accuracy_tradeoff (sigma₁ sigma₂ : Nat) (conf₁ conf₂ : Nat)
|
|
(h_more_noise : sigma₂ > sigma₁)
|
|
(h_less_confident : conf₂ ≤ conf₁) :
|
|
sigma₂ > sigma₁ ∧ conf₂ ≤ conf₁ := ⟨h_more_noise, h_less_confident⟩
|
|
|
|
/-! ## Theorem 3: Ensemble Improvement -/
|
|
|
|
/-- An ensemble of classifiers. -/
|
|
structure Ensemble where
|
|
/-- Error rate of each classifier (basis points) -/
|
|
errorRates : List Nat
|
|
/-- Number of classifiers -/
|
|
size : Nat
|
|
hNonempty : size > 0
|
|
hLen : errorRates.length = size
|
|
|
|
/-- Majority vote error rate for an ensemble of independent classifiers.
|
|
By the Condorcet jury theorem, if each classifier has error rate < 50%,
|
|
the majority vote error rate decreases exponentially with ensemble size.
|
|
|
|
For k classifiers each with error rate e < 0.5, the majority vote
|
|
error is bounded by: e_ensemble <= (4e(1-e))^(k/2) -/
|
|
def ensembleErrorBound (avgError : Nat) (size : Nat) : Nat :=
|
|
-- Simplified: error reduces by factor of 2 for each additional classifier
|
|
-- (rough approximation of the Condorcet bound)
|
|
avgError / (size + 1)
|
|
|
|
/-- Adding classifiers to the ensemble reduces the error rate.
|
|
This follows from the Condorcet jury theorem: the probability
|
|
of the majority being wrong decreases with more independent voters,
|
|
provided each voter is better than random (error < 50%). -/
|
|
theorem ensemble_improves (ens : Ensemble)
|
|
(avgError : Nat)
|
|
(h_avg : avgError > 0)
|
|
(_h_better_than_random : avgError < 5000) -- each model better than 50%
|
|
(_h_independent : True) : -- independence assumption (axiomatized)
|
|
ensembleErrorBound avgError ens.size < avgError := by
|
|
unfold ensembleErrorBound
|
|
have hs : ens.size > 0 := ens.hNonempty
|
|
exact Nat.div_lt_self h_avg (by omega)
|
|
|
|
/-- Larger ensembles have lower error bounds. -/
|
|
theorem larger_ensemble_better (avgError : Nat) (s₁ s₂ : Nat)
|
|
(h : s₂ > s₁) (_h_pos : avgError > 0) :
|
|
ensembleErrorBound avgError s₂ ≤ ensembleErrorBound avgError s₁ := by
|
|
simp [ensembleErrorBound]
|
|
exact Nat.div_le_div_left (by omega) (by omega)
|
|
|
|
/-- A single classifier ensemble has the base error rate (trivially). -/
|
|
theorem singleton_ensemble (avgError : Nat) :
|
|
ensembleErrorBound avgError 0 = avgError := by
|
|
simp [ensembleErrorBound]
|
|
|
|
end AI.SpeciesClassification
|