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. An algorithm takes actions, to which the environment responds with feedback (e.g., rewards for the bandit case, the gradient of a function in optimization problems). In general, both action and feedback can depend on the entire history up to the current time and can be randomized. The Algorithm structure is defined as follows:

🔗structure
Learning.Algorithm.{u_4, u_5} (𝓐 : Type u_4) (𝓨 : Type u_5) [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max u_4 u_5)
Learning.Algorithm.{u_4, u_5} (𝓐 : Type u_4) (𝓨 : Type u_5) [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max u_4 u_5)
Learning.Algorithm.mk.{u_4, u_5}
policy : (n : )  ProbabilityTheory.Kernel (Fin n  𝓐 × 𝓨) 𝓐
h_policy :  (n : ), ProbabilityTheory.IsMarkovKernel (self.policy n)

This structure refers to two types, the type of actions 𝓐 and the type of feedback 𝓨. Both are measurable spaces, since we consider stochastic algorithms and environments. Before time n, there is a history of actions and feedbacks Fin n → 𝓐 × 𝓨 (the n pairs of action and feedback at times 0, ..., n - 1; the processes are 0-indexed). The policy field contains for each time n a kernel from that history to the action space. That is, it maps every possible history to a random action at time n (and that map is measurable). The h_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: Fin 0 → 𝓐 × 𝓨 has a unique element, and the distribution of the first action is policy 0 applied to that element. That distribution is called Algorithm.p0.

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. The first action is the value of that function at time 0 on the empty history.

🔗def
Learning.detAlgorithm.{u_1, u_2} {𝓐 : Type u_1} {𝓨 : Type u_2} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (nextA : (n : ) (Fin n 𝓐 × 𝓨) 𝓐) (h_next : (n : ), Measurable (nextA n)) : Algorithm 𝓐 𝓨
Learning.detAlgorithm.{u_1, u_2} {𝓐 : Type u_1} {𝓨 : Type u_2} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} (nextA : (n : ) (Fin 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 feedback instead of the actions.

🔗structure
Learning.Environment.{u_4, u_5} (𝓐 : Type u_4) (𝓨 : Type u_5) [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max u_4 u_5)
Learning.Environment.{u_4, u_5} (𝓐 : Type u_4) (𝓨 : Type u_5) [MeasurableSpace 𝓐] [MeasurableSpace 𝓨] : Type (max u_4 u_5)
Learning.Environment.mk.{u_4, u_5}
feedback : (n : )  ProbabilityTheory.Kernel ((Fin n  𝓐 × 𝓨) × 𝓐) 𝓨
h_feedback :  (n : ), ProbabilityTheory.IsMarkovKernel (self.feedback n)

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

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

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

(ν n).prodMkLeft _ is the kernel ν n seen as a Kernel ((Fin n → 𝓐 × 𝓨) × 𝓐) 𝓨 by ignoring the history.

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

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

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} {𝓐 : Type u_1} {𝓨 : Type u_2} {Ω : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} { : MeasurableSpace Ω} (A : Ω 𝓐) (Y : Ω 𝓨) (alg : Algorithm 𝓐 𝓨) (env : Environment 𝓐 𝓨) (P : MeasureTheory.Measure Ω) [MeasureTheory.IsFiniteMeasure P] : Prop
Learning.IsAlgEnvSeq.{u_1, u_2, u_3} {𝓐 : Type u_1} {𝓨 : Type u_2} {Ω : Type u_3} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} { : MeasurableSpace Ω} (A : Ω 𝓐) (Y : Ω 𝓨) (alg : Algorithm 𝓐 𝓨) (env : Environment 𝓐 𝓨) (P : MeasureTheory.Measure Ω) [MeasureTheory.IsFiniteMeasure P] : Prop
Learning.IsAlgEnvSeq.mk.{u_1, u_2, u_3}
measurable_action :  (n : ), Measurable (A n)
measurable_feedback :  (n : ), Measurable (Y n)
hasCondDistrib_action :  (n : ), ProbabilityTheory.HasCondDistrib (A n) (history A Y n) (alg.policy n) P
hasCondDistrib_feedback :  (n : ), ProbabilityTheory.HasCondDistrib (Y n) (fun ω  (history A Y n ω, A n ω)) (env.feedback n) P

This structure takes as input two sequences of random variables (two stochastic processes), A and Y, which represent the 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_4, u_5} {𝓐 : Type u_1} {𝓨 : Type u_2} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} {Ω : Type u_4} {Ω' : Type u_5} { : MeasurableSpace Ω} {mΩ' : MeasurableSpace Ω'} {alg : Algorithm 𝓐 𝓨} {env : Environment 𝓐 𝓨} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {P' : MeasureTheory.Measure Ω'} [MeasureTheory.IsProbabilityMeasure P'] {A₁ : Ω 𝓐} {R₁ : Ω 𝓨} {A₂ : Ω' 𝓐} {R₂ : Ω' 𝓨} (h1 : IsAlgEnvSeq A₁ R₁ alg env P) (h2 : IsAlgEnvSeq A₂ R₂ alg env P') : MeasureTheory.Measure.map (trajectory A₁ R₁) P = MeasureTheory.Measure.map (trajectory A₂ R₂) P'
Learning.isAlgEnvSeq_unique.{u_1, u_2, u_4, u_5} {𝓐 : Type u_1} {𝓨 : Type u_2} {m𝓐 : MeasurableSpace 𝓐} {m𝓨 : MeasurableSpace 𝓨} {Ω : Type u_4} {Ω' : Type u_5} { : MeasurableSpace Ω} {mΩ' : MeasurableSpace Ω'} {alg : Algorithm 𝓐 𝓨} {env : Environment 𝓐 𝓨} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {P' : MeasureTheory.Measure Ω'} [MeasureTheory.IsProbabilityMeasure P'] {A₁ : Ω 𝓐} {R₁ : Ω 𝓨} {A₂ : Ω' 𝓐} {R₂ : Ω' 𝓨} (h1 : IsAlgEnvSeq A₁ R₁ alg env P) (h2 : IsAlgEnvSeq A₂ R₂ alg env P') : MeasureTheory.Measure.map (trajectory A₁ R₁) P = MeasureTheory.Measure.map (trajectory 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.ucbWidth' {K : } (c : ) (n : ) (h : Fin n Fin K × ) (a : Fin K) :
Bandits.ucbWidth' {K : } (c : ) (n : ) (h : Fin n Fin K × ) (a : Fin K) :
🔗def
Bandits.UCB.nextArm {K : } (hK : 0 < K) (c : ) (n : ) (h : Fin n Fin K × ) : Fin K
Bandits.UCB.nextArm {K : } (hK : 0 < K) (c : ) (n : ) (h : Fin n Fin K × ) : Fin K
🔗axiom
Bandits.UCB.measurable_nextArm {K : } (hK : 0 < K) (c : ) (n : ) : Measurable (Bandits.UCB.nextArm hK c n)
Bandits.UCB.measurable_nextArm {K : } (hK : 0 < K) (c : ) (n : ) : Measurable (Bandits.UCB.nextArm hK c n)
🔗def
Bandits.ucbAlgorithm {K : } (hK : 0 < K) (c : ) : Algorithm (Fin K)
Bandits.ucbAlgorithm {K : } (hK : 0 < K) (c : ) : Algorithm (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 hK 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_1, u_2} {𝓐 : Type u_1} {Ω : Type u_2} {m𝓐 : MeasurableSpace 𝓐} (ν : ProbabilityTheory.Kernel 𝓐 ) (A : Ω 𝓐) (t : ) (ω : Ω) :
Bandits.regret.{u_1, u_2} {𝓐 : Type u_1} {Ω : Type u_2} {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 : } {hK : 0 < K} {c : } {ν : ProbabilityTheory.Kernel (Fin K) } [ProbabilityTheory.IsMarkovKernel ν] {Ω : Type u_1} { : MeasurableSpace Ω} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {A : Ω Fin K} {R : Ω } {σ2 : NNReal} (h : IsAlgEnvSeq A R (Bandits.ucbAlgorithm hK (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).toReal))
Bandits.UCB.regret_le.{u_1} {K : } {hK : 0 < K} {c : } {ν : ProbabilityTheory.Kernel (Fin K) } [ProbabilityTheory.IsMarkovKernel ν] {Ω : Type u_1} { : MeasurableSpace Ω} {P : MeasureTheory.Measure Ω} [MeasureTheory.IsProbabilityMeasure P] {A : Ω Fin K} {R : Ω } {σ2 : NNReal} (h : IsAlgEnvSeq A R (Bandits.ucbAlgorithm hK (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).toReal))

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 (Fin n → 𝓐 × R) to the action space 𝓐. Thus, to construct UCB, we used the following empirical mean function.

🔗def
Learning.empMean'.{u_1} {𝓐 : Type u_1} [DecidableEq 𝓐] (n : ) (h : Fin n 𝓐 × ) (a : 𝓐) :
Learning.empMean'.{u_1} {𝓐 : Type u_1} [DecidableEq 𝓐] (n : ) (h : Fin 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_1, u_3} {𝓐 : Type u_1} {Ω : Type u_3} [DecidableEq 𝓐] (A : Ω 𝓐) (R : Ω ) (a : 𝓐) (t : ) (ω : Ω) :
Learning.empMean.{u_1, u_3} {𝓐 : Type u_1} {Ω : Type u_3} [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.