|
| 1 | +/- |
| 2 | +Copyright (c) 2026 Paulo Rauber. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Paulo Rauber, Rémy Degenne |
| 5 | +-/ |
| 6 | +module |
| 7 | + |
| 8 | +public import LeanMachineLearning.ForMathlib.MeasureTheory.Constructions.BorelSpace.MeasurableArgMax |
| 9 | +public import LeanMachineLearning.Online.Bandit.Regret |
| 10 | + |
| 11 | +/-! |
| 12 | +# Bayesian regret |
| 13 | +
|
| 14 | +This file defines `actionMean`, `bestAction`, `gap`, and `regret` as random variables in a |
| 15 | +measurable space `Ω`. These definitions are useful when `IsBayesAlgEnvSeq Q κ alg E A Y P`. |
| 16 | +
|
| 17 | +Recall that `IsBayesAlgEnvSeq Q κ alg E A Y P` states that there is a measure `P : Measure Ω` such |
| 18 | +that the parameter `E : Ω → 𝓔` has law `Q` and that the sequences of actions `A : ℕ → Ω → 𝓐` and |
| 19 | +feedbacks `Y : ℕ → Ω → 𝓨` are generated by the algorithm `alg : Algorithm 𝓐 𝓨` interacting with an |
| 20 | +underlying environment that depends on `E` and `κ` (`stationaryEnv (κ.sectR (E ω))`) |
| 21 | +
|
| 22 | +## Main definitions |
| 23 | +
|
| 24 | +* `actionMean κ E a`: the mean feedback associated with action `a : 𝓐` based on the parameter `E`, |
| 25 | + which defines the underlying stationary environment together with the kernel `κ`. |
| 26 | +* `bestAction κ E`: (one of) the action(s) with the highest associated mean feedback based on `E`. |
| 27 | +* `gap κ E A n`: the difference between the highest mean feedback associated with an action and the |
| 28 | + mean feedback associated with the action at time `n` based on `E` and the sequence of actions `A`. |
| 29 | +* `regret κ E A n`: the regret at time `n` based on `E` and the sequence of actions `A`. If |
| 30 | + `IsBayesAlgEnvSeq Q κ alg E A Y P`, then `P[regret κ E A n]` is the so-called Bayesian regret of |
| 31 | + algorithm `alg` under the prior `Q`. |
| 32 | +
|
| 33 | +-/ |
| 34 | + |
| 35 | +@[expose] public section |
| 36 | + |
| 37 | +open MeasureTheory ProbabilityTheory Finset |
| 38 | + |
| 39 | +namespace Learning.IsBayesAlgEnvSeq |
| 40 | + |
| 41 | +variable {𝓔 𝓐 𝓨 Ω : Type*} |
| 42 | +variable [MeasurableSpace 𝓔] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] [MeasurableSpace Ω] |
| 43 | + |
| 44 | +/-- A random variable that gives the mean feedback of action `a`. -/ |
| 45 | +noncomputable |
| 46 | +def actionMean (κ : Kernel (𝓔 × 𝓐) ℝ) (E : Ω → 𝓔) (a : 𝓐) (ω : Ω) : ℝ := (κ (E ω, a))[id] |
| 47 | + |
| 48 | +@[fun_prop] |
| 49 | +lemma measurable_actionMean {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {a : 𝓐} (hE : Measurable E) : |
| 50 | + Measurable (actionMean κ E a) := |
| 51 | + stronglyMeasurable_id.integral_kernel.measurable.comp (by fun_prop) |
| 52 | + |
| 53 | +@[fun_prop] |
| 54 | +lemma measurable_uncurry_actionMean_comp [Countable 𝓐] [MeasurableSingletonClass 𝓐] |
| 55 | + {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} (hE : Measurable E) {f : Ω → 𝓐} (hf : Measurable f) : |
| 56 | + Measurable (fun ω ↦ actionMean κ E (f ω) ω) := by |
| 57 | + change Measurable ((fun aω ↦ actionMean κ E aω.1 aω.2) ∘ fun ω ↦ (f ω, ω)) |
| 58 | + apply Measurable.comp _ (by fun_prop) |
| 59 | + exact measurable_from_prod_countable_right (fun _ ↦ measurable_actionMean hE) |
| 60 | + |
| 61 | +lemma integrable_uncurry_actionMean_comp [Countable 𝓐] [MeasurableSingletonClass 𝓐] |
| 62 | + {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} (hE : Measurable E) {f : Ω → 𝓐} (hf : Measurable f) |
| 63 | + {P : Measure Ω} [IsFiniteMeasure P] {l u : ℝ} (hm : ∀ e a, (κ (e, a))[id] ∈ (Set.Icc l u)) : |
| 64 | + Integrable (fun ω ↦ actionMean κ E (f ω) ω) P := by |
| 65 | + refine ⟨(measurable_uncurry_actionMean_comp hE hf).aestronglyMeasurable, ?_⟩ |
| 66 | + apply HasFiniteIntegral.of_bounded |
| 67 | + filter_upwards with ω using abs_le_max_abs_abs (hm (E ω) (f ω)).1 (hm (E ω) (f ω)).2 |
| 68 | + |
| 69 | +/-- A random variable that gives the action with the highest mean feedback. -/ |
| 70 | +noncomputable |
| 71 | +def bestAction [Nonempty 𝓐] [Fintype 𝓐] [Encodable 𝓐] [MeasurableSingletonClass 𝓐] |
| 72 | + (κ : Kernel (𝓔 × 𝓐) ℝ) (E : Ω → 𝓔) (ω : Ω) : 𝓐 := |
| 73 | + measurableArgmax (fun ω' a ↦ actionMean κ E a ω') ω |
| 74 | + |
| 75 | +@[fun_prop] |
| 76 | +lemma measurable_bestAction [Nonempty 𝓐] [Fintype 𝓐] [Encodable 𝓐] [MeasurableSingletonClass 𝓐] |
| 77 | + {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} (hE : Measurable E) : Measurable (bestAction κ E) := |
| 78 | + measurable_measurableArgmax (by fun_prop) |
| 79 | + |
| 80 | +/-- A random variable that gives the gap at time `n`. -/ |
| 81 | +noncomputable |
| 82 | +def gap (κ : Kernel (𝓔 × 𝓐) ℝ) (E : Ω → 𝓔) (A : ℕ → Ω → 𝓐) (n : ℕ) (ω : Ω) : ℝ := |
| 83 | + Bandits.gap (κ.sectR (E ω)) (A n ω) |
| 84 | + |
| 85 | +omit [MeasurableSpace Ω] in |
| 86 | +/-- The gap is non-negative if the means are bounded by `u : ℝ` (even if `𝓐` is not `Finite`). -/ |
| 87 | +lemma gap_nonneg_of_le {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} {ω : Ω} {u : ℝ} |
| 88 | + (h : ∀ e a, (κ (e, a))[id] ≤ u) : 0 ≤ gap κ E A n ω := |
| 89 | + Bandits.gap_nonneg_of_le (h (E ω)) |
| 90 | + |
| 91 | +omit [MeasurableSpace Ω] in |
| 92 | +lemma gap_le_of_mem_Icc [Nonempty 𝓐] {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} |
| 93 | + {ω : Ω} {l u : ℝ} (h : ∀ e a, (κ (e, a))[id] ∈ Set.Icc l u) : gap κ E A n ω ≤ u - l := |
| 94 | + Bandits.gap_le_of_mem_Icc (h (E ω)) |
| 95 | + |
| 96 | +omit [MeasurableSpace Ω] in |
| 97 | +lemma gap_eq_sub [Nonempty 𝓐] [Fintype 𝓐] [Encodable 𝓐] [MeasurableSingletonClass 𝓐] |
| 98 | + {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} {ω : Ω} : |
| 99 | + gap κ E A n ω = actionMean κ E (bestAction κ E ω) ω - actionMean κ E (A n ω) ω := by |
| 100 | + rw [gap, Bandits.gap] |
| 101 | + congr |
| 102 | + apply le_antisymm |
| 103 | + · exact ciSup_le (isMaxOn_measurableArgmax (fun ω' a ↦ actionMean κ E a ω') ω) |
| 104 | + · exact Finite.le_ciSup (fun a ↦ actionMean κ E a ω) _ |
| 105 | + |
| 106 | +@[fun_prop] |
| 107 | +lemma measurable_gap [Countable 𝓐] {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} |
| 108 | + (hE : Measurable E) (hA : ∀ t, Measurable (A t)) : Measurable (gap κ E A n) := |
| 109 | + (Measurable.iSup fun _ ↦ stronglyMeasurable_id.integral_kernel.measurable.comp (by fun_prop)).sub |
| 110 | + (stronglyMeasurable_id.integral_kernel.measurable.comp (by fun_prop)) |
| 111 | + |
| 112 | +lemma integrable_gap [Countable 𝓐] [Nonempty 𝓐] {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} |
| 113 | + {A : ℕ → Ω → 𝓐} {n : ℕ} {P : Measure Ω} [IsFiniteMeasure P] (hE : Measurable E) |
| 114 | + (hA : ∀ t, Measurable (A t)) {l u : ℝ} (h : ∀ e a, (κ (e, a))[id] ∈ Set.Icc l u) : |
| 115 | + Integrable (gap κ E A n) P := by |
| 116 | + apply Integrable.of_bound (by fun_prop) (u - l) |
| 117 | + filter_upwards with ω |
| 118 | + rw [Real.norm_eq_abs, abs_of_nonneg (gap_nonneg_of_le (fun e a ↦ (h e a).2))] |
| 119 | + exact gap_le_of_mem_Icc h |
| 120 | + |
| 121 | +/-- A random variable that gives the regret at time `n`. -/ |
| 122 | +noncomputable |
| 123 | +def regret (κ : Kernel (𝓔 × 𝓐) ℝ) (E : Ω → 𝓔) (A : ℕ → Ω → 𝓐) (n : ℕ) (ω : Ω) : ℝ := |
| 124 | + Bandits.regret (κ.sectR (E ω)) A n ω |
| 125 | + |
| 126 | +omit [MeasurableSpace Ω] in |
| 127 | +lemma regret_eq_sum_gap {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} {ω : Ω} : |
| 128 | + regret κ E A n ω = ∑ s ∈ range n, gap κ E A s ω := by |
| 129 | + simp [regret, Bandits.regret, gap, Bandits.gap] |
| 130 | + |
| 131 | +omit [MeasurableSpace Ω] in |
| 132 | +lemma regret_eq_sum_gap' {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} : |
| 133 | + regret κ E A n = fun ω ↦ ∑ s ∈ range n, gap κ E A s ω := funext fun _ ↦ regret_eq_sum_gap |
| 134 | + |
| 135 | +@[fun_prop] |
| 136 | +lemma measurable_regret [Countable 𝓐] {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} {A : ℕ → Ω → 𝓐} {n : ℕ} |
| 137 | + (hE : Measurable E) (hA : ∀ t, Measurable (A t)) : Measurable (regret κ E A n) := by |
| 138 | + rw [regret_eq_sum_gap'] |
| 139 | + fun_prop |
| 140 | + |
| 141 | +lemma integrable_regret [Countable 𝓐] [Nonempty 𝓐] {κ : Kernel (𝓔 × 𝓐) ℝ} {E : Ω → 𝓔} |
| 142 | + {A : ℕ → Ω → 𝓐} {n : ℕ} {P : Measure Ω} [IsFiniteMeasure P] (hE : Measurable E) |
| 143 | + (hA : ∀ t, Measurable (A t)) {l u : ℝ} (h : ∀ e a, (κ (e, a))[id] ∈ Set.Icc l u) : |
| 144 | + Integrable (regret κ E A n) P := by |
| 145 | + rw [regret_eq_sum_gap'] |
| 146 | + exact integrable_finsetSum _ (fun _ _ ↦ integrable_gap hE hA h) |
| 147 | + |
| 148 | +end Learning.IsBayesAlgEnvSeq |
0 commit comments