Documentation

CslibFrontier.MachineLearning.PACBayes.Basic

PAC-Bayes bounds for countable hypothesis spaces #

A prior P over hypotheses, fixed before the sample, controls the generalization gap of every posterior Q at once, through the Kullback–Leibler divergence KL(Q ‖ P) [McA99]. Hypotheses are indexed by a countable type ι, and ℓ i z ∈ [0, 1] is the loss of the hypothesis i on the example z; the true and empirical risks are L_D(i) = trueRisk ℓ D i and L_S(i) = empiricalRisk ℓ S i (Cslib.MachineLearning.trueRisk, Cslib.MachineLearning.empiricalRisk). Under m ≥ 2 i.i.d. examples S from D and for every δ > 0, with probability at least 1 - δ every probability measure Q on ι with KL(Q ‖ P) < ∞ satisfies

𝔼_{i∼Q} L_D(i) ≤ 𝔼_{i∼Q} L_S(i) + √((KL(Q ‖ P) + log (m / δ)) / (2 (m - 1))),

which is [ShalevShwartzBenDavid2014, Theorem 31.1] with the source's constants. Each bound is stated, as in the rest of the library, as an upper bound δ on the measure of the samples on which some posterior violates it. The posterior is quantified inside that event, so it may depend on the sample. The proof follows the source: the change-of-measure inequality (Cslib.Probability.integral_le_toReal_klDiv_add_log_integral_exp) and Jensen's inequality bound 2 (m - 1) (𝔼_Q Δ)² - KL(Q ‖ P) by log 𝔼_P exp (2 (m - 1) Δ²) for the gap Δ(i) = L_D(i) - L_S(i); because P does not depend on the sample, Tonelli's theorem and the one-hypothesis exponential moment bound the sample average of the right side; Markov's inequality finishes. The exponential moment comes from Hoeffding's lemma (Cslib.Probability.hasSubgaussianMGF_integral_sub_sum_div) and is at most √m, sharper than the source's m (its Exercise 31.1), so measure_pi_exists_integral_risk_gt proves the bound with log (√m / δ) and measure_pi_exists_integral_risk_gt_log_div derives the source's form.

Taking point-mass posteriors gives the Occam bound of [ShalevShwartzBenDavid2014, Exercise 31.2]: every hypothesis i with P {i} > 0 satisfies L_D(i) ≤ L_S(i) + √((log (1 / P {i}) + log (m / δ)) / (2 (m - 1))), using KL(δ_i ‖ P) = log (1 / P {i}) (Cslib.Probability.toReal_klDiv_dirac). For the uniform prior on a finite class this is the bound with log |H| of the exercise.

Differences from the source: the hypothesis space is countable with measurable singletons (the source also allows densities); δ < 1 is not needed; posteriors with infinite divergence are excluded, for which the source's bound is vacuous; each ℓ i must be measurable. The divergence is Mathlib's klDiv, in nats as in the source.

Main results #

References #

theorem Cslib.MachineLearning.PACBayes.measure_ofReal_div_le_lintegral_le_of_measurable {ι : Type u_1} {Ω : Type u_3} [MeasurableSpace Ω] [MeasurableSpace ι] (μ : MeasureTheory.Measure Ω) [MeasureTheory.SFinite μ] (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {F : Ω → ι → ENNReal} (hFm : Measurable (Function.uncurry F)) {B : ℝ} (hB : ∀ (i : ι), ∫⁻ (ω : Ω), F ω i ∂μ ≤ ENNReal.ofReal B) (hB0 : 0 < B) {δ : ℝ} (hδ : 0 < δ) :
μ {ω : Ω | ENNReal.ofReal (B / δ) ≤ ∫⁻ (i : ι), F ω i ∂P} ≤ ENNReal.ofReal δ

Markov's inequality over a prior on an arbitrary measurable space: if the nonnegative kernel F : Ω → ι → ℝ≥0∞ is jointly measurable (Measurable (Function.uncurry F)), every hypothesis i has 𝔼_ω F ω i ≤ B, and the prior P does not depend on ω, then 𝔼_ω 𝔼_{i∼P} F ω i ≤ B by Tonelli's theorem, so the prior average reaches B / δ with probability at most δ.

theorem Cslib.MachineLearning.PACBayes.measure_ofReal_div_le_lintegral_le {ι : Type u_1} {Ω : Type u_3} [MeasurableSpace Ω] [MeasurableSpace ι] [Countable ι] [MeasurableSingletonClass ι] (μ : MeasureTheory.Measure Ω) [MeasureTheory.SFinite μ] (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {F : Ω → ι → ENNReal} (hF : ∀ (i : ι), Measurable fun (ω : Ω) => F ω i) {B : ℝ} (hB : ∀ (i : ι), ∫⁻ (ω : Ω), F ω i ∂μ ≤ ENNReal.ofReal B) (hB0 : 0 < B) {δ : ℝ} (hδ : 0 < δ) :
μ {ω : Ω | ENNReal.ofReal (B / δ) ≤ ∫⁻ (i : ι), F ω i ∂P} ≤ ENNReal.ofReal δ

Markov's inequality over a prior [ShalevShwartzBenDavid2014, (31.4)–(31.5)] on a countable hypothesis space: specializes measure_ofReal_div_le_lintegral_le_of_measurable via measurable_from_prod_countable_left.

theorem Cslib.MachineLearning.PACBayes.measurable_trueRisk_of_measurable {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] [MeasurableSpace ι] {D : MeasureTheory.Measure Z} [MeasureTheory.IsProbabilityMeasure D] {ℓ : ι → Z → ℝ} (hℓ : Measurable (Function.uncurry ℓ)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) :

Measurability of i ↦ trueRisk ℓ D i from joint measurability of a [0, 1]-bounded loss.

theorem Cslib.MachineLearning.PACBayes.measurable_empiricalRisk_prod {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] [MeasurableSpace ι] {ℓ : ι → Z → ℝ} (hℓ : Measurable (Function.uncurry ℓ)) (m : ℕ) :
Measurable fun (p : (Fin m → Z) × ι) => empiricalRisk ℓ p.1 p.2

Joint measurability of (S, i) ↦ empiricalRisk ℓ S i from joint measurability of ℓ.

theorem Cslib.MachineLearning.PACBayes.measurable_empiricalRisk_arg {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] [MeasurableSpace ι] {ℓ : ι → Z → ℝ} (hℓ : Measurable (Function.uncurry ℓ)) {m : ℕ} (S : Fin m → Z) :
Measurable fun (i : ι) => empiricalRisk ℓ S i

Measurability of i ↦ empiricalRisk ℓ S i for a fixed sample S.

Integrability of trueRisk ℓ D under any probability measure Q on ι.

theorem Cslib.MachineLearning.PACBayes.integrable_empiricalRisk_of_measurable {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] [MeasurableSpace ι] {ℓ : ι → Z → ℝ} (hℓ : Measurable (Function.uncurry ℓ)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) {m : ℕ} (S : Fin m → Z) (Q : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure Q] :

Integrability of empiricalRisk ℓ S under any probability measure Q on ι.

theorem Cslib.MachineLearning.PACBayes.measure_pi_exists_integral_risk_gt_of_measurable {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] {D : MeasureTheory.Measure Z} [MeasureTheory.IsProbabilityMeasure D] {ℓ : ι → Z → ℝ} [MeasurableSpace ι] (hℓ : Measurable (Function.uncurry ℓ)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) {m : ℕ} (hm : 2 ≤ m) (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {δ : ℝ} (hδ : 0 < δ) :

McAllester's PAC-Bayes bound on an arbitrary measurable hypothesis space [McA99], with log (√m / δ): for a prior P on any measurable index space ι, jointly measurable losses ℓ i z ∈ [0, 1], m ≥ 2 and δ > 0, the samples on which some posterior Q with KL(Q ‖ P) < ∞ has 𝔼_Q L_S + √((KL(Q ‖ P) + log (√m / δ)) / (2 (m - 1))) < 𝔼_Q L_D have probability at most δ.

theorem Cslib.MachineLearning.PACBayes.measure_pi_exists_integral_risk_gt {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] {D : MeasureTheory.Measure Z} [MeasureTheory.IsProbabilityMeasure D] {ℓ : ι → Z → ℝ} [MeasurableSpace ι] [Countable ι] [MeasurableSingletonClass ι] (hℓ : ∀ (i : ι), Measurable (ℓ i)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) {m : ℕ} (hm : 2 ≤ m) (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {δ : ℝ} (hδ : 0 < δ) :

McAllester's PAC-Bayes bound [McA99], with log (√m / δ): for a prior P on a countable index, losses ℓ i z ∈ [0, 1], m ≥ 2 and δ > 0, the samples on which some posterior Q with KL(Q ‖ P) < ∞ has 𝔼_Q L_S + √((KL(Q ‖ P) + log (√m / δ)) / (2 (m - 1))) < 𝔼_Q L_D have probability at most δ.

theorem Cslib.MachineLearning.PACBayes.measure_pi_exists_integral_risk_gt_log_div {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] {D : MeasureTheory.Measure Z} [MeasureTheory.IsProbabilityMeasure D] {ℓ : ι → Z → ℝ} [MeasurableSpace ι] [Countable ι] [MeasurableSingletonClass ι] (hℓ : ∀ (i : ι), Measurable (ℓ i)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) {m : ℕ} (hm : 2 ≤ m) (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {δ : ℝ} (hδ : 0 < δ) :
(MeasureTheory.Measure.pi fun (x : Fin m) => D) {S : Fin m → Z | ∃ (Q : MeasureTheory.Measure ι), MeasureTheory.IsProbabilityMeasure Q ∧ InformationTheory.klDiv Q P ≠ ⊤ ∧ ∫ (i : ι), empiricalRisk ℓ S i ∂Q + √(((InformationTheory.klDiv Q P).toReal + Real.log (↑m / δ)) / (2 * (↑m - 1))) < ∫ (i : ι), trueRisk ℓ D i ∂Q} ≤ ENNReal.ofReal δ

McAllester's PAC-Bayes bound with the constants of [ShalevShwartzBenDavid2014, Theorem 31.1]: log (m / δ).

theorem Cslib.MachineLearning.PACBayes.measure_pi_exists_risk_gt {ι : Type u_1} {Z : Type u_2} [MeasurableSpace Z] {D : MeasureTheory.Measure Z} [MeasureTheory.IsProbabilityMeasure D] {ℓ : ι → Z → ℝ} [MeasurableSpace ι] [Countable ι] [MeasurableSingletonClass ι] (hℓ : ∀ (i : ι), Measurable (ℓ i)) (hℓ01 : ∀ (i : ι) (z : Z), ℓ i z ∈ Set.Icc 0 1) {m : ℕ} (hm : 2 ≤ m) (P : MeasureTheory.Measure ι) [MeasureTheory.IsProbabilityMeasure P] {δ : ℝ} (hδ : 0 < δ) :
(MeasureTheory.Measure.pi fun (x : Fin m) => D) {S : Fin m → Z | ∃ (i : ι), P {i} ≠ 0 ∧ empiricalRisk ℓ S i + √((Real.log (1 / P.real {i}) + Real.log (↑m / δ)) / (2 * (↑m - 1))) < trueRisk ℓ D i} ≤ ENNReal.ofReal δ

The Occam bound as a PAC-Bayes corollary [ShalevShwartzBenDavid2014, Exercise 31.2]: taking point-mass posteriors, with probability at least 1 - δ every index i with P {i} > 0 satisfies L_D(i) ≤ L_S(i) + √((log (1 / P {i}) + log (m / δ)) / (2 (m - 1))).