DeepMDP: MDPs, value functions, latent space models, DeepMDP losses, Wasserstein and bisimulation metrics
DefinitionDeepMDP_BasicObjects of Sections 2-6 and Appendix A of Gelada, Kumar, Buckman, Nachum and Bellemare (2019).
- Wasserstein metric (Definition 1). For measures on a space and a cost , , where is the set of couplings (measures on with marginals and ). On a metric space, with the metric. Values lie in .
- MDP and value functions (Section 2.1). An MDP has a finite action set , Markov transition kernels , bounded measurable rewards and a discount . A stationary policy is a Markov kernel . With and (the expected discounted reward of the first steps), and . A policy is optimal if pointwise for every policy , and . The policy's reward and transition functions are and .
- Lipschitz notions (Definitions 2-3). On a metric state space, is -Lipschitz-valued if and every are -Lipschitz. The MDP is -Lipschitz if every is -Lipschitz and .
- Latent space model (Section 2.2). has a countable discrete state space ; the DeepMDP has a metric state space with its Borel -algebra; is the embedding. A latent policy acts on by , and is the law of , .
- DeepMDP losses (eqs. (4)-(7)). Global: and . Local to a state-action distribution : and . All take values in .
- Stationary distributions (Section 2.1). is a stationary distribution of in if it is a probability on with ; the state-action distribution is .
- Bisimulation metric (Definition 5). For rewards and transitions , and ; the bisimulation metric is . The joined MDP of and (Lemma 5) has state space , each state moving and being rewarded according to its own MDP.
- Norm-MMD (Definitions 6-7). A function seminorm on real functions may take the value . The MMD of a class is over the integrable under and ; the Norm-MMD uses . A policy is -smooth-valued if and for all . The global Norm-MMD transition loss is .
Formalization Note The value functions are the limits of the finite-horizon expected discounted rewards, which is the paper's infinite sum of expected rewards. The bisimulation metric is the limit of the iterates of from , which the proof of Lemma 4 uses; under the paper's assumptions this is the unique fixed point of Definition 5. Losses, Wasserstein distances and bisimulation distances are -valued, so an infinite loss is represented as such.
import Mathlib
/-!
Gelada, Kumar, Buckman, Nachum and Bellemare, *DeepMDP: Learning Continuous Latent Space Models
for Representation Learning*, arXiv:1906.02736v1 (ICML 2019): Markov decision processes and their
value functions (Section 2.1), latent space models and the DeepMDP losses (Section 2.2, eqs. (1)-(2);
Section 3, eqs. (4)-(5); Section 4, eqs. (6)-(7)), the Wasserstein metric (Section 2.3,
Definition 1), Lipschitz-valued policies and Lipschitz DeepMDPs (Section 2.4, Definitions 2-3),
bisimulation metrics (Section 5.2, Definition 5, and the joined MDP of Lemma 5 in Appendix A.4) and
Norm-MMD metrics with smooth-valued policies (Section 6, Definitions 6-7).
-/
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal
noncomputable section
namespace DeepMDP
section General
variable {X A : Type*} [MeasurableSpace X] [MeasurableSpace A] [Fintype A]
/-- Definition 1 (Section 2.3, p. 3): `c` is a coupling of `P` and `Q`, i.e. a measure on `X × X`
whose marginals are `P` and `Q`; the set of these is `Γ(P,Q)`. -/
def IsCoupling (P Q : Measure X) (c : Measure (X × X)) : Prop :=
c.map Prod.fst = P ∧ c.map Prod.snd = Q
/-- Definition 1 (Section 2.3, p. 3) for an arbitrary cost `d`:
`W_d(P,Q) = inf_{λ ∈ Γ(P,Q)} ∫ d(x,y) dλ(x,y)`. Section 5.2 (p. 6) uses it for pseudometrics `d`. -/
def transportCost (d : X → X → ℝ≥0∞) (P Q : Measure X) : ℝ≥0∞ :=
⨅ (c : Measure (X × X)) (_ : IsCoupling P Q c), ∫⁻ z, d z.1 z.2 ∂c
/-- Definition 1 (Section 2.3, p. 3): the Wasserstein-1 metric `W(P,Q)` on a metric space, whose
cost is the metric itself. -/
def wasserstein [PseudoEMetricSpace X] (P Q : Measure X) : ℝ≥0∞ :=
transportCost (fun x y => edist x y) P Q
/-- Section 2.1 (p. 2): the standing conventions of an MDP `⟨X, A, r, P, γ⟩` with a finite action
set: a discount `0 ≤ γ < 1`, Markov transition kernels `P(·|x,a)`, and bounded measurable
rewards `r(x,a)`. -/
def IsMDP (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) : Prop :=
(0 ≤ γ ∧ γ < 1) ∧ (∀ a, IsMarkovKernel (P a)) ∧ (∀ a, Measurable fun x => r x a) ∧
∃ C : ℝ, ∀ x a, |r x a| ≤ C
/-- Section 2.1 (p. 2): the expected discounted reward of the first `n` steps of a stationary
policy `π(a|x)`, started in state `x` with first action `a`:
`Q_0 = 0`, `Q_{n+1}(x,a) = r(x,a) + γ E_{y ∼ P(·|x,a)} ∑_{a'} π(a'|y) Q_n(y,a')`. -/
def evalIter (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) (π : Kernel X A) :
ℕ → X → A → ℝ
| 0 => fun _ _ => 0
| n + 1 => fun x a =>
r x a + γ * ∫ y, ∑ a', (π y {a'}).toReal * evalIter r P γ π n y a' ∂(P a x)
/-- Section 2.1 (p. 2): the action value function
`Q^π(x,a) = E[∑_{t≥0} γ^t r(x_t,a_t) | x_0 = x, a_0 = a]`, the limit of the expected discounted
rewards of the first `n` steps. -/
def qValue (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) (π : Kernel X A) (x : X) (a : A) : ℝ :=
limUnder atTop fun n => evalIter r P γ π n x a
/-- Section 2.1 (p. 2): the value function `V^π(x) = E[∑_{t≥0} γ^t r(x_t,a_t) | x_0 = x]`,
`a_t ∼ π(·|x_t)`, i.e. `V^π(x) = ∑_a π(a|x) Q^π(x,a)`. -/
def vValue (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) (π : Kernel X A) (x : X) : ℝ :=
∑ a, (π x {a}).toReal * qValue r P γ π x a
/-- Section 2.1 (p. 2): `π` is an optimal policy, a stationary policy which maximizes the expected
future reward from every state. -/
def IsOptimalPolicy (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) (π : Kernel X A) : Prop :=
IsMarkovKernel π ∧
∀ π' : Kernel X A, IsMarkovKernel π' → ∀ x, vValue r P γ π' x ≤ vValue r P γ π x
/-- Section 2.1 (p. 2): the optimal value function `V^*(x)`, the largest value `V^π(x)` over all
stationary policies `π ∈ Π`. -/
def optValue (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ) (x : X) : ℝ :=
⨆ π : {π : Kernel X A // IsMarkovKernel π}, vValue r P γ π.1 x
/-- Section 2.1 (p. 2) and Section 2.4 (p. 4): the reward function of a policy,
`r_π(x) = ∑_a r(x,a) π(a|x)`. -/
def policyReward (r : X → A → ℝ) (π : Kernel X A) (x : X) : ℝ :=
∑ a, (π x {a}).toReal * r x a
/-- Section 2.1 (p. 2) and Section 2.4 (p. 4): the transition function of a policy,
`P_π(·|x) = ∑_a P(·|x,a) π(a|x)`. -/
def policyTrans (P : A → Kernel X X) (π : Kernel X A) (x : X) : Measure X :=
∑ a, π x {a} • P a x
/-- Definition 2 (Section 2.4, p. 3): the policy `π` is `K`-Lipschitz-valued:
`|V^π(x₁) - V^π(x₂)| ≤ K d(x₁,x₂)` and `|Q^π(x₁,a) - Q^π(x₂,a)| ≤ K d(x₁,x₂)` for every action `a`. -/
def IsLipschitzValued [PseudoEMetricSpace X] (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ)
(π : Kernel X A) (K : ℝ≥0) : Prop :=
LipschitzWith K (vValue r P γ π) ∧ ∀ a, LipschitzWith K fun x => qValue r P γ π x a
/-- Definition 3 (Section 2.4, p. 3): the MDP is `(K_R, K_P)`-Lipschitz:
`|r(x₁,a) - r(x₂,a)| ≤ K_R d(x₁,x₂)` and `W(P(·|x₁,a), P(·|x₂,a)) ≤ K_P d(x₁,x₂)`. -/
def IsLipschitzMDP [PseudoEMetricSpace X] (r : X → A → ℝ) (P : A → Kernel X X) (KR KP : ℝ≥0) :
Prop :=
∀ a, LipschitzWith KR (fun x => r x a) ∧
∀ x y, wasserstein (P a x) (P a y) ≤ (KP : ℝ≥0∞) * edist x y
/-- Definition 5 (Section 5.2, p. 6): the iterates `d_0 = 0`, `d_{n+1} = F(d_n)` of the operator
`F_d(x,y) = max_a (1-γ)|r(x,a) - r(y,a)| + γ W_d(T(·|x,a), T(·|y,a))`. -/
def bisimIter (r : X → A → ℝ) (T : X → A → Measure X) (γ : ℝ) : ℕ → X → X → ℝ≥0∞
| 0 => fun _ _ => 0
| n + 1 => fun x y => ⨆ a, (ENNReal.ofReal ((1 - γ) * |r x a - r y a|) +
ENNReal.ofReal γ * transportCost (bisimIter r T γ n) (T x a) (T y a))
/-- Definition 5 (Section 5.2, p. 6): the bisimulation metric `d̃`, the fixed point of `F`, obtained
as the limit of the nondecreasing iterates `d_n` started from `d_0 = 0` (as in the proof of Lemma 4,
Appendix A.4). -/
def bisimMetric (r : X → A → ℝ) (T : X → A → Measure X) (γ : ℝ) (x y : X) : ℝ≥0∞ :=
⨆ n, bisimIter r T γ n x y
/-- Section 6.1 (p. 7): a function seminorm `‖·‖_D` on real functions on `X`, allowed to take the
value `+∞` (as the Lipschitz seminorm does on non-Lipschitz functions). -/
structure FunSeminorm (X : Type*) where
/-- The value `‖f‖_D`. -/
toFun : (X → ℝ) → ℝ≥0∞
/-- Absolute homogeneity `‖c f‖_D = |c| ‖f‖_D`. -/
map_smul' : ∀ (c : ℝ) (f : X → ℝ), toFun (c • f) = ENNReal.ofReal |c| * toFun f
/-- Triangle inequality `‖f + g‖_D ≤ ‖f‖_D + ‖g‖_D`. -/
add_le' : ∀ f g : X → ℝ, toFun (f + g) ≤ toFun f + toFun g
/-- Definition 6 (Section 6.1, p. 7): the Maximum Mean Discrepancy generated by a class `F`,
`D(P,Q) = sup_{f ∈ F} |E_{x∼P} f(x) - E_{y∼Q} f(y)|`, over the members of `F` for which both
expectations exist. -/
def mmd (F : Set (X → ℝ)) (P Q : Measure X) : ℝ≥0∞ :=
⨆ (f : X → ℝ) (_ : f ∈ F ∧ Integrable f P ∧ Integrable f Q),
ENNReal.ofReal |∫ x, f x ∂P - ∫ x, f x ∂Q|
/-- Section 6.1 (p. 7): the Norm-MMD metric of a function seminorm, generated by the class
`F_D = {f : ‖f‖_D ≤ 1}`. -/
def normMMD (sn : FunSeminorm X) (P Q : Measure X) : ℝ≥0∞ :=
mmd {f | sn.toFun f ≤ 1} P Q
/-- Definition 7 (Section 6.2, p. 8): the policy `π` is `K`-smooth-valued for the Norm-MMD with
seminorm `‖·‖_D`: `‖V^π‖_D ≤ K` and `‖Q^π(·,a)‖_D ≤ K` for every action `a`. -/
def IsSmoothValued (sn : FunSeminorm X) (r : X → A → ℝ) (P : A → Kernel X X) (γ : ℝ)
(π : Kernel X A) (K : ℝ≥0) : Prop :=
sn.toFun (vValue r P γ π) ≤ K ∧ ∀ a, sn.toFun (fun x => qValue r P γ π x a) ≤ K
end General
section Latent
variable {S Sb A : Type*} [MeasurableSpace S] [MeasurableSpace Sb] [MeasurableSpace A] [Fintype A]
/-- Section 2.2 (p. 2): a policy `π̄` on the latent space, run on `M` through the embedding:
`π̄(·|s) := π̄(·|φ(s))`. -/
def liftPolicy [DiscreteMeasurableSpace S] (πb : Kernel Sb A) (φ : S → Sb) : Kernel S A :=
πb.comap φ Measurable.of_discrete
/-- Section 2.2 (p. 2): `φP(·|s,a)`, the law of `φ(s')` for `s' ∼ P(·|s,a)`. -/
def embedTrans (P : A → Kernel S S) (φ : S → Sb) (s : S) (a : A) : Measure Sb :=
(P a s).map φ
/-- Section 3, eq. (4) (p. 4): the global reward loss
`L^∞_R̄ = sup_{s,a} |R(s,a) - R̄(φ(s),a)|`. -/
def rewardLossGlobal (R : S → A → ℝ) (Rb : Sb → A → ℝ) (φ : S → Sb) : ℝ≥0∞ :=
⨆ s, ⨆ a, ENNReal.ofReal |R s a - Rb (φ s) a|
/-- Section 3, eq. (5) (p. 4): the global transition loss
`L^∞_P̄ = sup_{s,a} W(φP(·|s,a), P̄(·|φ(s),a))`. -/
def transLossGlobal [PseudoEMetricSpace Sb] (P : A → Kernel S S) (Pb : A → Kernel Sb Sb)
(φ : S → Sb) : ℝ≥0∞ :=
⨆ s, ⨆ a, wasserstein (embedTrans P φ s a) (Pb a (φ s))
/-- Section 6.3 (p. 8) and Lemma 7 (Appendix A.6): the global transition loss with a Norm-MMD `D`,
`L^∞_P̄ = sup_{s,a} D(φP(·|s,a), P̄(·|φ(s),a))`. -/
def transLossGlobalMMD (sn : FunSeminorm Sb) (P : A → Kernel S S) (Pb : A → Kernel Sb Sb)
(φ : S → Sb) : ℝ≥0∞ :=
⨆ s, ⨆ a, normMMD sn (embedTrans P φ s a) (Pb a (φ s))
/-- Section 4, eq. (6) (p. 5): the reward loss local to a state-action distribution `ξ`,
`L^ξ_R̄ = E_{(s,a)∼ξ} |R(s,a) - R̄(φ(s),a)|`. -/
def rewardLossLocal (ξ : Measure (S × A)) (R : S → A → ℝ) (Rb : Sb → A → ℝ) (φ : S → Sb) :
ℝ≥0∞ :=
∫⁻ z, ENNReal.ofReal |R z.1 z.2 - Rb (φ z.1) z.2| ∂ξ
/-- Section 4, eq. (7) (p. 5): the transition loss local to a state-action distribution `ξ`,
`L^ξ_P̄ = E_{(s,a)∼ξ} W(φP(·|s,a), P̄(·|φ(s),a))`. -/
def transLossLocal [PseudoEMetricSpace Sb] (ξ : Measure (S × A)) (P : A → Kernel S S)
(Pb : A → Kernel Sb Sb) (φ : S → Sb) : ℝ≥0∞ :=
∫⁻ z, wasserstein (embedTrans P φ z.1 z.2) (Pb z.2 (φ z.1)) ∂ξ
/-- Section 2.1 (p. 2): `ξ` is a stationary distribution of the policy `π` in `M`:
`ξ(s) = ∑_{ṡ,ȧ} P(s|ṡ,ȧ) π(ȧ|ṡ) ξ(ṡ)`. -/
def IsStationary (P : A → Kernel S S) (π : Kernel S A) (ξ : Measure S) : Prop :=
IsProbabilityMeasure ξ ∧ ∀ s, ξ {s} = ∫⁻ s', ∑ a', π s' {a'} * P a' s' {s} ∂ξ
/-- Section 2.1 (p. 2): the state-action distribution `ξ_π(s,a) = ξ(s) π(a|s)`. -/
def stateActionDist (ξ : Measure S) (π : Kernel S A) : Measure (S × A) :=
ξ.compProd π
/-- Lemma 5 (Appendix A.4, p. 19): reward of the MDP joining `M` and `M̄` on `S ⊕ S̄`. -/
def joinReward (R : S → A → ℝ) (Rb : Sb → A → ℝ) : S ⊕ Sb → A → ℝ
| .inl s, a => R s a
| .inr x, a => Rb x a
/-- Lemma 5 (Appendix A.4, p. 19): transition of the MDP joining `M` and `M̄` on `S ⊕ S̄`; each
state transitions according to the transition function of its own MDP. -/
def joinTrans (P : A → Kernel S S) (Pb : A → Kernel Sb Sb) : S ⊕ Sb → A → Measure (S ⊕ Sb)
| .inl s, a => (P a s).map Sum.inl
| .inr x, a => (Pb a x).map Sum.inr
end Latent
end DeepMDP
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Standing conventions (apply to every item below). and are arbitrary types, each with an arbitrary -algebra, and is finite. The -algebra on is not assumed discrete, so singletons need not be measurable. For a kernel from to , denotes the value that the measure assigns to the singleton ; if is not measurable, this is the outer-measure value. Where a real number is needed, denotes this value converted to a real number, with . For a family of kernels from to , denotes the measure . In the latent section, , and are arbitrary measurable spaces, with finite.
IsCoupling. Given measures on and a measure on , this is the proposition that the push-forward of under the first projection equals and the push-forward under the second projection equals . and are arbitrary measures, not necessarily probability measures. If and have different total masses, no coupling exists. The set of such is written .
transportCost. Given an arbitrary function and measures on , define
- is not assumed to be a metric, symmetric, or measurable.
- The integral is the lower Lebesgue integral: the supremum of integrals of measurable simple functions below .
- The infimum over an empty set is , so whenever no coupling exists, for example when and have different total masses.
wasserstein. Assume also carries an extended pseudometric . Distinct points may have distance , and distances may be . No compatibility is assumed between and the -algebra of . Then
which equals when .
IsMDP. For a reward , kernels () from to , and a real , this is the conjunction of four conditions:
- ;
- every is a Markov kernel, i.e. each is a probability measure;
- for every , the map is measurable;
- there is a real with for all .
evalIter. For , , as above and any kernel from to , define real numbers by
- No hypotheses are imposed: is any real, need not be Markov, need not be Markov, and need not be bounded or measurable.
- The integral is the Bochner integral, which is defined to be when the integrand is not integrable with respect to .
qValue. is defined as the limit of the real sequence from evalIter, if that limit exists. If the sequence does not converge, is an unspecified real number. No hypotheses on are made.
vValue. The value is
with as in qValue. This includes its junk value when the defining sequence diverges.
IsOptimalPolicy. This asserts two things:
- is a Markov kernel from to ;
- for every Markov kernel from to and every , .
is as in vValue. No assumption that satisfies IsMDP is included.
optValue. The value is
taken as a supremum of real numbers. By the real-valued convention, it equals if this set is empty or not bounded above. For example, the set is empty when and . No IsMDP assumption is included.
policyReward. The policy reward is
policyTrans. is the measure on
The weights are used unconverted, so a weight of produces an infinite multiple of the measure.
IsLipschitzValued. Assume carries an extended pseudometric . For , is -Lipschitz-valued when:
- for all ;
- for every , for all .
and are as above, including junk values. The inequalities are read in : they are automatic when and . When , they force equal values. is not required to be Markov, and no IsMDP assumption is included.
IsLipschitzMDP. Assume carries an extended pseudometric . For , the condition is that for every :
- for all ;
- for all , in with .
is the Wasserstein cost defined above, which is for measures without a coupling. The kernels are not required to be Markov.
bisimIter. Here assigns to each an arbitrary measure on ; it is not required to be a kernel, measurable in , or a probability measure. and are arbitrary. Define by and
- , so negative quantities are truncated to : the first term vanishes if , and the second vanishes if .
- is transportCost with cost .
- If , the supremum is .
bisimMetric. The value is
with from bisimIter. It is defined as this supremum; nothing in the definition asserts that it is a fixed point, a metric, or finite.
FunSeminorm. A function seminorm on is a map from all functions (no measurability required) to satisfying:
- for every real and every , with , so ;
- for all .
Nonzero functions may have seminorm .
mmd. For a set of real functions on and measures on , define
Members of that are not integrable with respect to both measures are ignored. The supremum of the empty set is .
normMMD. Given a function seminorm (as in FunSeminorm), the value is as in mmd, with
IsSmoothValued. Given a function seminorm on and , the condition is:
- ;
- for every .
and are as above, including junk values. No Markov or IsMDP assumptions are included.
liftPolicy. Assume every subset of is measurable. Given a kernel from to and an arbitrary map , the lifted kernel from to sends to the measure .
embedTrans. Given kernels from to and an arbitrary map , is the push-forward of under . is not assumed measurable. If is not almost-everywhere measurable with respect to , the push-forward is by convention the zero measure.
rewardLossGlobal. For , and , define
It equals if or is empty.
transLossGlobal. Assume carries an extended pseudometric. For kernels on , kernels on , and an arbitrary , define
- is the Wasserstein cost on , which is when the two measures admit no coupling. This happens, for instance, when their total masses differ, including when the push-forward is the junk zero measure.
- The value is if or is empty.
transLossGlobalMMD. This is the same as transLossGlobal, except that is replaced by the Norm-MMD of a given function seminorm on :
rewardLossLocal. For an arbitrary measure on (not required to be a probability measure), define
The integral is the lower Lebesgue integral; no measurability is assumed.
transLossLocal. Assume carries an extended pseudometric. For an arbitrary measure on , define
The integral is the lower Lebesgue integral, and is as in transLossGlobal, with the same zero-measure and no-coupling conventions.
IsStationary. For kernels on , a kernel from to , and a measure on , the conditions are:
- is a probability measure;
- for every single point ,
Only the masses of singletons are constrained; general measurable sets are not. If all relevant singleton masses are , as for atomless measures, the condition reduces to . Neither nor the are required to be Markov.
stateActionDist. is the composition-product measure on :
This holds when is -finite and is an -finite kernel. Otherwise, is by convention the zero measure.
joinReward. On the disjoint union , the joined reward is at a point from and at a point from .
joinTrans. On , the transition measures are:
- at a point from with action : the push-forward of under the inclusion ;
- at a point from : the push-forward of under the inclusion .
Hence the two components never transition into each other.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.