Finite MDP models, Bellman operators and value equivalence
DefinitionValueEquivalence_BasicObjects of Sections 2–4 and Appendix A.1.1 of Grimm, Barreto, Singh and Silver (2020), for finite state and action spaces.
- Policies and kernels. and are finite sets. A policy is a map , written with , and is the set of all policies. A transition kernel is with .
- Models and Bellman operators (eq. 2). A model is a pair of an expected reward and a transition kernel . With a discount , the Bellman operator of a policy induced by acts on by
The greedy Bellman operator of Appendix A.1.1 is . 3. Value equivalence (Definitions 1, 2). For a set of policies and a set of functions , models are value equivalent with respect to if for all , . For a set of models and the true model , is the set of models in value equivalent to . 4. Pointwise span (eq. 3). For a set of functions on with values in a vector space, iff for every . For policies, and . 5. Value-equivalent kernels. With the reward of known, is the set of kernels in a class such that is value equivalent to ; is the case where is the set of all kernels. 6. Dimension of a set (Section 3.2). For a subset of a finite-dimensional real vector space, . For matrices , , is the space of Lemma 1. 7. Maximum-likelihood loss (eq. 5). For a distribution on , , with , terms with equal to , and value if for some .
Formalization Note The paper states Sections 2–3.1 for general state and action spaces; here and are finite types, as in the paper's Section 3.2 from Proposition 2 on. A model carries the proof that is a transition kernel. The Bellman operator is defined for any , so that it can be evaluated on pointwise combinations of policies. The dimension uses of subspaces, which is the Hamel dimension in finite dimensions. The loss takes values in the extended reals.
import Mathlib
/-!
Grimm, Barreto, Singh and Silver, *The Value Equivalence Principle for Model-Based Reinforcement
Learning*, arXiv:2011.03506v1 (NeurIPS 2020): finite Markov decision processes, models and their
Bellman operators (Section 2), value equivalence and spaces of value-equivalent models
(Section 3, Definitions 1 and 2), pointwise span (eq. (3)), the dimension `dim[·]` of a set of
transition matrices (Section 3.1), the maximum-likelihood loss (eq. (5)) and the greedy Bellman
operator of Appendix A.1.1. State and action spaces are finite, as in Section 3.1 from
Proposition 2 on.
-/
noncomputable section
namespace ValueEquivalence
variable {S A : Type*} [Fintype S] [Fintype A]
/-- Section 2, p. 2: `p` is a transition kernel, i.e. `p(·|s,a)` (written `p s a`) is a
probability distribution on the finite state space `S` for every state `s` and action `a`. -/
def IsTransitionKernel (p : S → A → S → ℝ) : Prop :=
(∀ s a s', 0 ≤ p s a s') ∧ ∀ s a, ∑ s', p s a s' = 1
/-- Section 2, p. 2: a policy `π : S → 𝒫(A)`, written `π s a = π(a|s)`. -/
def IsPolicy (π : S → A → ℝ) : Prop :=
(∀ s a, 0 ≤ π s a) ∧ ∀ s, ∑ a, π s a = 1
/-- Section 2, p. 2: the set `𝚷` of all policies. -/
def policies : Set (S → A → ℝ) := {π | IsPolicy π}
/-- Section 3, p. 3 (and Section 3.1, p. 5, for finite `S`, `A`): a model `m = (r, p)`, an
expected reward `r(s,a)` together with a transition kernel `p(s'|s,a)`. -/
structure Model (S A : Type*) [Fintype S] where
/-- the expected reward `r(s,a)` -/
r : S → A → ℝ
/-- the transition kernel, `p s a s' = p(s'|s,a)` -/
p : S → A → S → ℝ
/-- `p` is a transition kernel -/
isKernel : IsTransitionKernel p
/-- Eq. (2), p. 2: the Bellman operator of policy `π` induced by model `m` and discount `γ`,
`T_π[v](s) = E_{A∼π(·|s), S'∼p(·|s,A)}[r(s,A) + γ v(S')]`. -/
def bellman (m : Model S A) (γ : ℝ) (π : S → A → ℝ) (v : S → ℝ) : S → ℝ :=
fun s => ∑ a, π s a * (m.r s a + γ * ∑ s', m.p s a s' * v s')
/-- Definition 1, p. 3: models `m` and `m'` are value equivalent with respect to a set of policies
`Pol` and a set of functions `V` if `T_π v = T'_π v` for all `π ∈ Pol` and all `v ∈ V`. -/
def IsValueEquivalent (γ : ℝ) (Pol : Set (S → A → ℝ)) (V : Set (S → ℝ)) (m m' : Model S A) :
Prop :=
∀ π ∈ Pol, ∀ v ∈ V, bellman m γ π v = bellman m' γ π v
/-- Definition 2, p. 3: the space `M_m(Pol, V) ⊆ M` of models in `M` that are value equivalent to
`m` with respect to `Pol` and `V`. With `m = m*` the true model this is `M(Pol, V)`. -/
def veSpace (γ : ℝ) (M : Set (Model S A)) (m : Model S A) (Pol : Set (S → A → ℝ))
(V : Set (S → ℝ)) : Set (Model S A) :=
{m' | m' ∈ M ∧ IsValueEquivalent γ Pol V m m'}
/-- Eq. (3), p. 4: the pointwise span of a set `H` of functions on `X`, in the equivalent form
stated right after eq. (3): `g ∈ p-span(H)` iff `g(x) ∈ span {h(x) : h ∈ H}` for every `x`.
For policies, `X = S` and `h(x) = π(·|s) ∈ ℝ^A`. -/
def pspan {X W : Type*} [AddCommGroup W] [Module ℝ W] (H : Set (X → W)) : Set (X → W) :=
{g | ∀ x, g x ∈ Submodule.span ℝ ((fun h => h x) '' H)}
/-- Section 3.1, p. 5: the set `ℙ(Pol, V)` (for `PP` the set of all transition kernels) or
`𝒫(Pol, V)` (Section 4, p. 5, for a class `PP` of kernels) of transition kernels in `PP` that,
with the known reward `r*` of the true model `m*`, are value equivalent to `m*`. -/
def veKernels (γ : ℝ) (mstar : Model S A) (PP : Set (S → A → S → ℝ)) (Pol : Set (S → A → ℝ))
(V : Set (S → ℝ)) : Set (S → A → S → ℝ) :=
Model.p '' veSpace γ {m | m.r = mstar.r ∧ m.p ∈ PP} mstar Pol V
/-- Section 3.1, p. 5: the dimension of a set `X`, the least Hamel dimension of a vector space
`W` containing a translate `X + c`: `dim[X] = min_{(W, c) : X + c ⊆ W} H-dim[W]`. Used on
finite-dimensional real spaces, where the Hamel dimension is `Module.finrank`. -/
def setDim {E : Type*} [AddCommGroup E] [Module ℝ E] [FiniteDimensional ℝ E] (X : Set E) : ℕ :=
sInf {d | ∃ (W : Submodule ℝ E) (c : E),
(fun x => x + c) '' X ⊆ (W : Set E) ∧ Module.finrank ℝ W = d}
/-- Lemma 1, p. 14: for `A ∈ ℝ^{k×n}` and `C ∈ ℝ^{m×l}`, the vector space
`𝓑 = {B ∈ ℝ^{n×m} : A B C = 0}`. -/
def twoSidedKernel {k n m l : ℕ} (A : Matrix (Fin k) (Fin n) ℝ) (C : Matrix (Fin m) (Fin l) ℝ) :
Submodule ℝ (Matrix (Fin n) (Fin m) ℝ) where
carrier := {B | A * B * C = 0}
add_mem' := by
intro B B' hB hB'
change A * B * C = 0 at hB
change A * B' * C = 0 at hB'
show A * (B + B') * C = 0
rw [Matrix.mul_add, Matrix.add_mul, hB, hB', add_zero]
zero_mem' := by simp
smul_mem' := by
intro c B hB
change A * B * C = 0 at hB
show A * (c • B) * C = 0
rw [Matrix.mul_smul, Matrix.smul_mul, hB, smul_zero]
/-- Eq. (5), p. 5: the Kullback–Leibler divergence `D_KL(q ‖ q')` of two distributions on the
finite set `S`, `∑_s q(s) log (q(s)/q'(s))`, with `0 log(0/·) = 0` and value `+∞` when
`q'(s) = 0 < q(s)` for some `s`. -/
def klDivFin (q q' : S → ℝ) : EReal :=
∑ s, if q s = 0 then 0 else if q' s = 0 then ⊤ else ((q s * Real.log (q s / q' s) : ℝ) : EReal)
/-- Eq. (5), p. 5: the maximum-likelihood loss
`ℓ_{p,D}(p, p') = E_{(S,A)∼D}[D_KL(p(·|S,A) ‖ p'(·|S,A))]` for a distribution `D` on `S × A`. -/
def mleLoss (D : S → A → ℝ) (p p' : S → A → S → ℝ) : EReal :=
∑ s, ∑ a, ((D s a : ℝ) : EReal) * klDivFin (p s a) (p' s a)
/-- Appendix A.1.1, proof of Proposition 4, p. 16: the greedy Bellman operator
`T v = max_a T_a v` of model `m`, where `T_a` is the Bellman operator of the policy that always
selects action `a` (meaningful for nonempty `A`). -/
def bellmanOpt (m : Model S A) (γ : ℝ) (v : S → ℝ) : S → ℝ :=
fun s => ⨆ a, (m.r s a + γ * ∑ s', m.p s a s' * v s')
end ValueEquivalence
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Common setting. Throughout, (states) and (actions) are arbitrary finite sets. Either may be empty. All functions below are real-valued unless stated otherwise.
IsTransitionKernel (transition kernel). Let , with written . Then is a transition kernel exactly when both of these hold:
- for all and ;
- for every and .
Edge cases: if is empty or is empty, both conditions are vacuous, so every such qualifies.
IsPolicy (policy). Let , with written . Then is a policy exactly when both of these hold:
- for all and ;
- for every .
Edge cases:
- If is empty, the unique (empty) function is a policy.
- If is nonempty and is empty, the sum is , so no policy exists.
policies (the set ). is the set of all functions that are policies in the above sense:
Model (a model ). A model consists of three pieces of data:
- a reward function , which is arbitrary (no boundedness or sign condition);
- a function , written ;
- a proof that is a transition kernel, meaning it is nonnegative and for all .
No other constraint is imposed.
bellman (Bellman operator ). The inputs are:
- a model ;
- a real number , which is arbitrary: it is not restricted to and may be negative or ;
- a function , which is not required to be a policy;
- a function .
The output is the function given by
If is empty, this is at every state.
IsValueEquivalent (value equivalence). The inputs are:
- a real number (arbitrary);
- a set of functions , which need not be policies;
- a set of functions ;
- two models and .
Then and are value equivalent when
Here is the Bellman operator above with the same , so the condition reads, for all :
If or is empty, the condition holds vacuously for any two models.
veSpace (space ). The inputs are:
- a real ;
- a set of models;
- a reference model ;
- a set of functions ;
- a set of functions .
The space is
The Bellman operators use the given . The reference model itself need not belong to . If or is empty, the space equals .
pspan (pointwise span). Let be any set and any real vector space. For a set of functions , the pointwise span is
Here denotes finite real linear combinations. The combination may depend on ; there is no single choice of coefficients for all .
Edge cases:
- If is empty, each pointwise span is , so only the zero function belongs.
- If is empty, every function (the empty one) belongs.
veKernels (value-equivalent kernels ). The inputs are:
- a real ;
- a reference model ;
- a set of functions , which need not consist of kernels;
- a set of functions ;
- a set of functions .
The output is the set of satisfying all three of:
- ;
- is a transition kernel (nonnegative, with for all );
- the model is value equivalent to , meaning for all , and :
The reward terms cancel because both models use . Elements of that are not transition kernels are excluded. If , condition 3 holds automatically.
setDim (dimension ). Let be a finite-dimensional real vector space and . Then is the natural number
The defining set always contains (take , ), so the minimum is attained. If is empty, or a single point, then .
twoSidedKernel (the space ). Fix natural numbers , any of which may be . Let be a real matrix; this is unrelated to the action set. Let be a real matrix. Then
regarded as a linear subspace of . It contains and is closed under addition and real scalar multiplication.
klDivFin (KL divergence ). Let be arbitrary functions. They are not required to be nonnegative or to sum to . The value lies in the extended reals :
Edge cases:
- The case is triggered by , which includes negative , not only .
- The logarithm is the total real function for . So if , the term is .
- No term is . Hence the sum is if any term is , and is the finite real sum otherwise.
- If is empty, the value is .
mleLoss (maximum-likelihood loss ). The inputs are:
- a function , which is not required to be nonnegative or to sum to ;
- two functions , which are not required to be transition kernels.
The loss is the extended real
Here is the divergence defined above, applied to and . Arithmetic is in with these conventions:
- , so pairs with contribute even when their divergence is infinite;
- a negative times gives ;
- in the sum, absorbs everything, including .
If or is empty, the loss is .
bellmanOpt (greedy Bellman operator ). The inputs are a model , a real (arbitrary), and . The output is
where the supremum is taken in the real numbers. Since is finite, this is the maximum over when is nonempty. When is empty, the real supremum of the empty family is by convention , so for every .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.