Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Algorithms 1 and §3.5 — forced initialization, greedy run, and regret on events

Definition
MostlyExplorationFree_GreedyBandit_Supplement_GreedyRun

by mikedeng1 · Nov 23, 2026 · Mathlib 0df444a (Lean v4.33.1)

contextual-banditgreedy-algorithmleast-squaresp2o-batch-supp500p2o-gran-per-chapterp2o-plan-paperp2o-v1

Give each of KKK arms exactly mmm context-independent samples in the first KmKmKm periods, using consecutive blocks of mmm periods. Thereafter select an arm maximizing the estimated reward, breaking ties uniformly. If Si,tS_{i,t}Si,t​ is the set of periods in which arm iii has been played, the estimate is

β^(Si,t)=(∑s∈Si,tXsXs⊤)−1∑s∈Si,tYi,sXs\hat\beta(S_{i,t})=\left(\sum_{s\in S_{i,t}}X_sX_s^\top\right)^{-1}\sum_{s\in S_{i,t}}Y_{i,s}X_sβ^​(Si,t​)=​s∈Si,t​∑​Xs​Xs⊤​​−1s∈Si,t​∑​Yi,s​Xs​

when the Gram matrix is invertible, and the initial estimate otherwise. Setting m=0m=0m=0 gives the uninitialized greedy algorithm. For an arm sequence π\piπ write

ρt=max⁡jXt⊤βj−Xt⊤βπt,RT(E)=E[IE∑t=1Tρt].\rho_t=\max_j X_t^\top\beta_j-X_t^\top\beta_{\pi_t},\quad R_T(E)=\mathbb E\left[\mathbb I_E\sum_{t=1}^T\rho_t\right].ρt​=jmax​Xt⊤​βj​−Xt⊤​βπt​​,RT​(E)=E[IE​t=1∑T​ρt​].

Logarithmic regret on EEE means ∃C∈R ∀T≥2, RT(E)≤Clog⁡T\exists C\in\mathbb R\ \forall T\ge2,\ R_T(E)\le C\log T∃C∈R ∀T≥2, RT​(E)≤ClogT.

These definitions fix the actual run and its regret, rather than taking estimates, Gram matrices, or a success event as unconstrained input data.

Formalization Note Gram matrices grow by positive semidefinite increments. Consequently retaining the initial value on a singular matrix is equivalent to updating only when invertible. The maximum is finite and nonempty for every permitted arm count. The regret integrand is measurable and bounded by 2xmax⁡bmax⁡2x_{\max}b_{\max}2xmax​bmax​ per period under the model and Assumption 1, so the real integrals are ordinary expectations. The logarithmic coefficient can depend on the instance but never on the horizon.

Definition code
import Mathlib
import Definitions.Def_MostlyExplorationFree_GreedyBandit_Supplement_Environment

namespace MostlyExplorationFree.GreedyBandit.Supplement

open Matrix MeasureTheory

/-- Select a maximum-score eligible arm of smallest random rank. A uniform random permutation
makes the choice uniform among ties. The fallback is used only on an empty eligible set. -/
noncomputable def selectArm {K : ℕ} [NeZero K] (eligible : Finset (Fin K))
    (score : Fin K → ℝ) (w : TieIndex K) : Fin K := by
  classical
  let rank := (Fintype.equivFin (Equiv.Perm (Fin K))).symm w
  let ties := eligible.filter (fun i => ∀ j ∈ eligible, score j ≤ score i)
  exact if h : (ties.image rank).Nonempty then
    rank.symm ((ties.image rank).min' h) else 0

def playedOf {K : ℕ} (a : ℕ → Fin K) (i : Fin K) (t : ℕ) : Finset ℕ :=
  (Finset.Icc 1 t).filter (fun s => a s = i)

def gramOn {Ω : Type*} {d K : ℕ} (E : Environment Ω d K) (ω : Ω) (S : Finset ℕ) :
    Matrix (Fin d) (Fin d) ℝ := ∑ s ∈ S, Matrix.vecMulVec (E.X s ω) (E.X s ω)

/-- OLS with the specified initial estimate retained while the Gram matrix is singular.
On samples of arm i, the potential reward below is its observed reward. -/
noncomputable def olsOn {Ω : Type*} {d K : ℕ} (E : Environment Ω d K) (ω : Ω)
    (initial : Fin d → ℝ) (i : Fin K) (S : Finset ℕ) : Fin d → ℝ :=
  if (gramOn E ω S).det ≠ 0 then
    (gramOn E ω S)⁻¹ *ᵥ ∑ s ∈ S, (E.X s ω ⬝ᵥ E.β i + E.ε i s ω) • E.X s ω
  else initial

noncomputable def estOf {Ω : Type*} {d K : ℕ} (E : Environment Ω d K) (ω : Ω)
    (a : ℕ → Fin K) (i : Fin K) (t : ℕ) : Fin d → ℝ :=
  olsOn E ω (E.initial ω i) i (playedOf a i t)

/-- Block schedule: the first m times play arm 0, then m times arm 1, and so on. -/
def initialArm {K : ℕ} [NeZero K] (m t : ℕ) : Fin K :=
  ⟨((t - 1) / m) % K, Nat.mod_lt _ (NeZero.pos K)⟩

/-- Forced initial samples, followed by the literal greedy recursion. The t=0 slot is unused. -/
noncomputable def arm {Ω : Type*} {d K : ℕ} [NeZero K]
    (E : Environment Ω d K) (m : ℕ) (t : ℕ) (ω : Ω) : Fin K :=
  Nat.strongRec (fun t ih =>
    if t = 0 then 0 else if t ≤ K * m then initialArm m t else
      selectArm Finset.univ
        (fun i => E.X t ω ⬝ᵥ estOf E ω
          (fun s => if hs : s < t then ih s hs else 0) i (t - 1)) (E.W t ω)) t

noncomputable def gram {Ω : Type*} {d K : ℕ} [NeZero K] (E : Environment Ω d K)
    (m : ℕ) (i : Fin K) (t : ℕ) (ω : Ω) : Matrix (Fin d) (Fin d) ℝ :=
  gramOn E ω (playedOf (fun s => arm E m s ω) i t)

noncomputable def est {Ω : Type*} {d K : ℕ} [NeZero K] (E : Environment Ω d K)
    (m : ℕ) (i : Fin K) (t : ℕ) (ω : Ω) : Fin d → ℝ :=
  estOf E ω (fun s => arm E m s ω) i t

/-- Finite maximum; K is nonzero, so this is never an empty supremum. -/
noncomputable def bestReward {Ω : Type*} {d K : ℕ} [NeZero K]
    (E : Environment Ω d K) (t : ℕ) (ω : Ω) : ℝ :=
  Finset.univ.sup' Finset.univ_nonempty (fun i => E.X t ω ⬝ᵥ E.β i)

noncomputable def pseudoRegret {Ω : Type*} {d K : ℕ} [NeZero K]
    (E : Environment Ω d K) (a : ℕ → Ω → Fin K) (T : ℕ) (ω : Ω) : ℝ :=
  ∑ t ∈ Finset.Icc 1 T, (bestReward E t ω - E.X t ω ⬝ᵥ E.β (a t ω))

noncomputable def regretOn {Ω : Type*} [MeasurableSpace Ω] {d K : ℕ} [NeZero K]
    (P : Measure Ω) (E : Environment Ω d K) (a : ℕ → Ω → Fin K)
    (A : Set Ω) (T : ℕ) : ℝ := ∫ ω, A.indicator (pseudoRegret E a T) ω ∂P

/-- One coefficient for the whole infinite sequence of horizons. -/
def LogarithmicOn {Ω : Type*} [MeasurableSpace Ω] {d K : ℕ} [NeZero K]
    (P : Measure Ω) (E : Environment Ω d K) (a : ℕ → Ω → Fin K) (A : Set Ω) : Prop :=
  ∃ C : ℝ, ∀ T : ℕ, 2 ≤ T → regretOn P E a A T ≤ C * Real.log T

end MostlyExplorationFree.GreedyBandit.Supplement
Source
Bastani, Bayati & Khosravi, arXiv:1704.09011v8, pp. 7, 11–12, 19 and 55, regret, Definition 3, Algorithm 1, initialization

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