Lean Machine Learning

4. Defining an Algorithm🔗

This tutorial explains the structures used in the Lean Machine Learning library to describe algorithms, the environment they interact with, and how to state theorems about their interaction. We then illustrate them with the UCB bandit algorithm.

4.1. Algorithm and environment🔗

In LML, we prove theorems about the interaction of an algorithm with an environment. Each round of the interaction consists of three stages: the environment draws an observation (e.g., the context in a contextual bandit), the algorithm takes an action based on that observation, and the environment responds with feedback (e.g., rewards for the bandit case, the gradient of a function in optimization problems). In general, observation, action and feedback can depend on the entire history up to the current time and can be randomized. One round is recorded by the Round abbreviation and a history of n rounds by the Hist abbreviation.

🔗def
Learning.Round.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) : Type (max u_5 u_7 u_6)
Learning.Round.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) : Type (max u_5 u_7 u_6)
🔗def
Learning.Hist.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) (n : ) : Type (max (max u_7 u_6) u_5)
Learning.Hist.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) (n : ) : Type (max (max u_7 u_6) u_5)

The Algorithm structure is defined as follows:

🔗structure
Learning.Algorithm.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) [MeasurableSpace 𝓞] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max (max u_5 u_6) u_7)
Learning.Algorithm.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) [MeasurableSpace 𝓞] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max (max u_5 u_6) u_7)
Learning.Algorithm.mk.{u_5, u_6, u_7}
policy : (n : )  ProbabilityTheory.Kernel (Hist 𝓞 𝓐 𝓨 n × 𝓞) 𝓐
isMarkovKernel_policy :  (n : ), ProbabilityTheory.IsMarkovKernel (self.policy n)

This structure refers to three types, the type of observations 𝓞, the type of actions 𝓐 and the type of feedback 𝓨. All three are measurable spaces, since we consider stochastic algorithms and environments. Before time n, there is a history of n complete rounds Hist 𝓞 𝓐 𝓨 n = Fin n → 𝓞 × 𝓐 × 𝓨 (the observation-action-feedback triples at times 0, ..., n - 1; the processes are 0-indexed). The policy field contains for each time n a kernel from that history together with the observation at time n to the action space. That is, it maps every possible history and current observation to a random action at time n (and that map is measurable). The isMarkovKernel_policy field records that the measure describing the action is a probability measure (and it is in square brackets to tell Lean to infer it automatically whenever possible). At time 0 the history is empty: Hist 𝓞 𝓐 𝓨 0 has a unique element, and the distribution of the first action given the first observation is policy 0 applied to that element. That kernel is called Algorithm.p0.

Many settings have no observations at all: the algorithm sees only the past rounds. Those are described by taking 𝓞 = Unit, and we write noObs Ω for the corresponding (constant) observation process.

If the algorithms actions are not random, we can use the detAlgorithm definition to build an algorithm from the data of a measurable function for the action at each time, as a function of the history before that time and of the current observation. The first action is the value of that function at time 0 on the empty history.

🔗def
Learning.detAlgorithm.{u_1, u_2, u_3} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (nextA : (n : ) Hist 𝓞 𝓐 𝓨 n × 𝓞 𝓐) (h_next : (n : ), Measurable (nextA n)) : Algorithm 𝓞 𝓐 𝓨
Learning.detAlgorithm.{u_1, u_2, u_3} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (nextA : (n : ) Hist 𝓞 𝓐 𝓨 n × 𝓞 𝓐) (h_next : (n : ), Measurable (nextA n)) : Algorithm 𝓞 𝓐 𝓨

We can see here that we did not need to prove that the kernels are IsMarkovKernel. Lean knows that deterministic kernels are Markov.

The Environment structure is the mirror of the Algorithm structure, with a kernel for the observation and a kernel for the feedback instead of the actions.

🔗structure
Learning.Environment.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) [MeasurableSpace 𝓞] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max (max u_5 u_6) u_7)
Learning.Environment.{u_5, u_6, u_7} (𝓞 : Type u_5) (𝓐 : Type u_6) (𝓨 : Type u_7) [MeasurableSpace 𝓞] [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max (max u_5 u_6) u_7)
Learning.Environment.mk.{u_5, u_6, u_7}
obs : (n : )  ProbabilityTheory.Kernel (Hist 𝓞 𝓐 𝓨 n) 𝓞
feedback : (n : )  ProbabilityTheory.Kernel ((Hist 𝓞 𝓐 𝓨 n × 𝓞) × 𝓐) 𝓨
isMarkovKernel_obs :  (n : ), ProbabilityTheory.IsMarkovKernel (self.obs n)
isMarkovKernel_feedback :  (n : ), ProbabilityTheory.IsMarkovKernel (self.feedback n)

obs n gives the distribution of the observation at time n given the history before n. feedback n gives the distribution of the feedback at time n given the history before n, the observation and the action at time n. The distribution of the first observation is obs 0 applied to the empty history; it is called Environment.obs0. The distribution of the first feedback given the first observation and action is feedback 0 applied to the empty history; it is called Environment.ν0.

In many applications there is no observation and the feedback depends only on the last action, not on the prior history. We provide an obliviousEnv definition that builds an environment for those cases.

🔗def
Learning.obliviousEnv.{u_2, u_3} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (ν : ProbabilityTheory.Kernel 𝓐 𝓨) [ (n : ), ProbabilityTheory.IsMarkovKernel (ν n)] : Environment Unit 𝓐 𝓨
Learning.obliviousEnv.{u_2, u_3} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (ν : ProbabilityTheory.Kernel 𝓐 𝓨) [ (n : ), ProbabilityTheory.IsMarkovKernel (ν n)] : Environment Unit 𝓐 𝓨

(ν n).prodMkLeft _ is the kernel ν n seen as a Kernel ((Hist Unit 𝓐 𝓨 n × Unit) × 𝓐) 𝓨 by ignoring the history and the observation.

If furthermore the feedback kernel does not change with time, we can use the stationaryEnv definition to build the environment.

🔗def
Learning.stationaryEnv.{u_2, u_3} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (ν : ProbabilityTheory.Kernel 𝓐 𝓨) [ProbabilityTheory.IsMarkovKernel ν] : Environment Unit 𝓐 𝓨
Learning.stationaryEnv.{u_2, u_3} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (ν : ProbabilityTheory.Kernel 𝓐 𝓨) [ProbabilityTheory.IsMarkovKernel ν] : Environment Unit 𝓐 𝓨

4.2. Sequences of actions and feedback, probability space🔗

Once algorithm and environment are defined, we can introduce sequences of actions and feedback and assume that they are generated by the interaction of the algorithm with the environment. This is done by the IsAlgEnvSeq structure.

🔗structure
Learning.IsAlgEnvSeq.{u_1, u_2, u_3, u_4} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {Ω : Type u_4} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} { : MeasurableSpace Ω} (O : Ω 𝓞) (A : Ω 𝓐) (Y : Ω 𝓨) (alg : Algorithm 𝓞 𝓐 𝓨) (env : Environment 𝓞 𝓐 𝓨) (P : MeasureTheory.Measure Ω) [MeasureTheory.IsFiniteMeasure P] : Prop
Learning.IsAlgEnvSeq.{u_1, u_2, u_3, u_4} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {Ω : Type u_4} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} { : MeasurableSpace Ω} (O : Ω 𝓞) (A : Ω 𝓐) (Y : Ω 𝓨) (alg : Algorithm 𝓞 𝓐 𝓨) (env : Environment 𝓞 𝓐 𝓨) (P : MeasureTheory.Measure Ω) [MeasureTheory.IsFiniteMeasure P] : Prop
Learning.IsAlgEnvSeq.mk.{u_1, u_2, u_3, u_4}
measurable_obs :  (n : ), Measurable (O n)
measurable_action :  (n : ), Measurable (A n)
measurable_feedback :  (n : ), Measurable (Y n)
hasCondDistrib_obs :  (n : ), ProbabilityTheory.HasCondDistrib (O n) (history O A Y n) (env.obs n) P
hasCondDistrib_action :  (n : ), ProbabilityTheory.HasCondDistrib (A n) (fun ω  (history O A Y n ω, O n ω)) (alg.policy n) P
hasCondDistrib_feedback :  (n : ), ProbabilityTheory.HasCondDistrib (Y n) (fun ω  ((history O A Y n ω, O n ω), A n ω)) (env.feedback n) P

This structure takes as input three sequences of random variables (three stochastic processes), O, A and Y, which represent the observations, actions and feedback generated by the interaction of the algorithm with the environment. It states that those sequences are measurable and that they have the correct conditional distributions given by the algorithm and environment. The measurable space Ω and the measure P are not imposed: they can be chosen as we want, as long as the conditions of IsAlgEnvSeq are satisfied. This definition requires 𝓐 and 𝓨 to be nonempty standard Borel spaces, because Mathlib's theory about conditional distributions requires those assumptions. All spaces of interest in machine learning are standard Borel, so this is not a restriction.

Given any algorithm and environment, there always exists a sequence of actions and feedback that satisfies IsAlgEnvSeq by the Ionescu-Tulcea theorem. However other constructions of such sequences are possible, and it is easier to work with generic sequences satisfying IsAlgEnvSeq than with a specific construction. Which sequence we choose does not matter for the results we prove, since all such sequences are equal in distribution, as stated by the following theorem.

🔗axiom
Learning.isAlgEnvSeq_unique.{u_1, u_2, u_3, u_5, u_6} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} {Ω : Type u_5} {Ω' : Type u_6} { : MeasurableSpace Ω} {mΩ' : MeasurableSpace Ω'} {alg : Algorithm 𝓞 𝓐 𝓨} {env : Environment 𝓞 𝓐 𝓨} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {P' : MeasureTheory.Measure Ω'} [MeasureTheory.IsProbabilityMeasure P'] {O₁ : Ω 𝓞} {A₁ : Ω 𝓐} {R₁ : Ω 𝓨} {O₂ : Ω' 𝓞} {A₂ : Ω' 𝓐} {R₂ : Ω' 𝓨} (h1 : IsAlgEnvSeq O₁ A₁ R₁ alg env P) (h2 : IsAlgEnvSeq O₂ A₂ R₂ alg env P') : MeasureTheory.Measure.map (trajectory O₁ A₁ R₁) P = MeasureTheory.Measure.map (trajectory O₂ A₂ R₂) P'
Learning.isAlgEnvSeq_unique.{u_1, u_2, u_3, u_5, u_6} {𝓞 : Type u_1} {𝓐 : Type u_2} {𝓨 : Type u_3} {m𝓞 : MeasurableSpace 𝓞} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} {Ω : Type u_5} {Ω' : Type u_6} { : MeasurableSpace Ω} {mΩ' : MeasurableSpace Ω'} {alg : Algorithm 𝓞 𝓐 𝓨} {env : Environment 𝓞 𝓐 𝓨} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {P' : MeasureTheory.Measure Ω'} [MeasureTheory.IsProbabilityMeasure P'] {O₁ : Ω 𝓞} {A₁ : Ω 𝓐} {R₁ : Ω 𝓨} {O₂ : Ω' 𝓞} {A₂ : Ω' 𝓐} {R₂ : Ω' 𝓨} (h1 : IsAlgEnvSeq O₁ A₁ R₁ alg env P) (h2 : IsAlgEnvSeq O₂ A₂ R₂ alg env P') : MeasureTheory.Measure.map (trajectory O₁ A₁ R₁) P = MeasureTheory.Measure.map (trajectory O₂ A₂ R₂) P'

4.3. Example: the UCB algorithm and a stochastic bandit environment🔗

We now illustrate the use of Algorithm, Environment, and IsAlgEnvSeq by defining the UCB bandit algorithm, a bandit environment, and stating a theorem about the regret of UCB in that environment.

In a stochastic bandit, an algorithm chooses at each time an action from a finite set (here Fin K, the type of natural numbers less than K) and receives a reward drawn from a distribution that depends only on the action, not on the prior history.

The environment is thus simply stationaryEnv ν for some kernel ν : Kernel (Fin K) ℝ.

4.3.1. Algorithm🔗

The UCB algorithm chooses at time n the action that maximizes the sum of the empirical mean reward and an exploration bonus. It starts by choosing each action once and then chooses \arg\max_a (\hat{\mu}_{n,a} + \sqrt{\frac{2c \log (n + 1)}{N_{n,a}}}), in which \hat{\mu}_{n,a} is the empirical mean reward of action a before time n (empMean' in the code), N_{n,a} is the number of times action a has been chosen before time n (pullCount' in the code), and c is a parameter of the algorithm.

To define the algorithm, we first define the exploration bonus and the next action function, and then we use detAlgorithm to build the algorithm. We also need to prove that the next action function is measurable, which is done by the measurable_nextArm lemma. Note that we are careful to use a measurable version of the argmax function, argmax.

🔗def
Bandits.UCB.ucbWidth' {K : } (c : ) (n : ) (h : Hist Unit (Fin K) n) (a : Fin K) :
Bandits.UCB.ucbWidth' {K : } (c : ) (n : ) (h : Hist Unit (Fin K) n) (a : Fin K) :
🔗def
Bandits.UCB.nextArm (K : ) [NeZero K] (c : ) (n : ) (h : Hist Unit (Fin K) n) : Fin K
Bandits.UCB.nextArm (K : ) [NeZero K] (c : ) (n : ) (h : Hist Unit (Fin K) n) : Fin K
🔗axiom
Bandits.UCB.measurable_nextArm {K : } [NeZero K] (c : ) (n : ) : Measurable (Bandits.UCB.nextArm K c n)
Bandits.UCB.measurable_nextArm {K : } [NeZero K] (c : ) (n : ) : Measurable (Bandits.UCB.nextArm K c n)
🔗def
Bandits.ucbAlgorithm (K : ) [NeZero K] (c : ) : Algorithm Unit (Fin K)
Bandits.ucbAlgorithm (K : ) [NeZero K] (c : ) : Algorithm Unit (Fin K)

The last line builds the algorithm using detAlgorithm and the function UCB.nextArm. Its measurability is proved by the fun_prop tactic, which proves measurability of functions by using lemmas tagged with @[fun_prop]. The first action of the algorithm is UCB.nextArm K c 0 applied to the empty history, which is 0 as an element of Fin K.

4.3.2. A theorem about UCB🔗

We can now state a theorem about the regret of UCB in a stochastic bandit environment (which we won't prove here). Let's first define the regret, which for stochastic bandits is the difference between the mean rewar that the algorithm would have obtained if it played always the best action, and the sum of mean rewards of the actions played.

🔗def
Bandits.regret.{u_2, u_3} {𝓐 : Type u_2} {Ω : Type u_3} {m𝓐 : MeasurableSpace 𝓐} (ν : ProbabilityTheory.Kernel 𝓐 ) (A : Ω 𝓐) (t : ) (ω : Ω) :
Bandits.regret.{u_2, u_3} {𝓐 : Type u_2} {Ω : Type u_3} {m𝓐 : MeasurableSpace 𝓐} (ν : ProbabilityTheory.Kernel 𝓐 ) (A : Ω 𝓐) (t : ) (ω : Ω) :

The quantity (ν a)[id] is the mean reward of action a in the environment defined by ν.

We can now state the regret bound for UCB.

🔗axiom
Bandits.UCB.regret_le.{u_1} {K : } [NeZero K] {c : } {ν : ProbabilityTheory.Kernel (Fin K) } [ProbabilityTheory.IsMarkovKernel ν] {Ω : Type u_1} { : MeasurableSpace Ω} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {O : Ω Unit} {A : Ω Fin K} {R : Ω } {σ2 : NNReal} (h : IsAlgEnvSeq O A R (Bandits.ucbAlgorithm K (c * σ2)) (stationaryEnv ν) P) ( : (a : Fin K), ProbabilityTheory.HasSubgaussianMGF (fun x x - (x : ), id x ν a) σ2 (ν a)) (hσ2 : σ2 0) (hc : 0 < c) (n : ) : (x : Ω), Bandits.regret ν A n x P a, (8 * c * σ2 * Real.log (n + 1) / Bandits.gap ν a + Bandits.gap ν a * (2 + 2 * Bandits.UCB.constSum c n))
Bandits.UCB.regret_le.{u_1} {K : } [NeZero K] {c : } {ν : ProbabilityTheory.Kernel (Fin K) } [ProbabilityTheory.IsMarkovKernel ν] {Ω : Type u_1} { : MeasurableSpace Ω} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {O : Ω Unit} {A : Ω Fin K} {R : Ω } {σ2 : NNReal} (h : IsAlgEnvSeq O A R (Bandits.ucbAlgorithm K (c * σ2)) (stationaryEnv ν) P) ( : (a : Fin K), ProbabilityTheory.HasSubgaussianMGF (fun x x - (x : ), id x ν a) σ2 (ν a)) (hσ2 : σ2 0) (hc : 0 < c) (n : ) : (x : Ω), Bandits.regret ν A n x P a, (8 * c * σ2 * Real.log (n + 1) / Bandits.gap ν a + Bandits.gap ν a * (2 + 2 * Bandits.UCB.constSum c n))

The arguments of the theorem are the following:

  • h states that the sequence of actions and rewards we are considering is generated by the interaction of UCB with parameter c * σ2 with the stationary environment defined by ν.

  • states that the reward distribution of each arm is subgaussian with variance proxy σ2.

  • hσ2 and hc state that the parameters σ2 and c are positive.

  • n is the time horizon.

The theorem gives an upper bound on the expected regret of UCB at time n.

4.4. Building vs analyzing algorithms🔗

When building an algorithm, we describe it with functions from the history and the current observation (Hist 𝓞 𝓐 R n × 𝓞) to the action space 𝓐. Thus, to construct UCB, we used the following empirical mean function.

🔗def
Learning.empMean'.{u_1, u_2} {𝓞 : Type u_1} {𝓐 : Type u_2} [DecidableEq 𝓐] (n : ) (h : Hist 𝓞 𝓐 n) (a : 𝓐) :
Learning.empMean'.{u_1, u_2} {𝓞 : Type u_1} {𝓐 : Type u_2} [DecidableEq 𝓐] (n : ) (h : Hist 𝓞 𝓐 n) (a : 𝓐) :

When analyzing an algorithm, we work with sequences of actions and rewards A : ℕ → Ω → 𝓐 and R' : ℕ → Ω → R that satisfy IsAlgEnvSeq. For the analysis, the empirical mean is defined as a stochastic process on the same probability space Ω.

🔗def
Learning.empMean.{u_2, u_4} {𝓐 : Type u_2} {Ω : Type u_4} [DecidableEq 𝓐] (A : Ω 𝓐) (R : Ω ) (a : 𝓐) (t : ) (ω : Ω) :
Learning.empMean.{u_2, u_4} {𝓐 : Type u_2} {Ω : Type u_4} [DecidableEq 𝓐] (A : Ω 𝓐) (R : Ω ) (a : 𝓐) (t : ) (ω : Ω) :

empMean A R' a is a stochastic process with type ℕ → Ω → ℝ that gives the empirical mean of action a at each time.