Model-based RL: values, discounted state distributions, model discrepancy and policy distances
DefinitionMBRLGuarantees_BasicObjects of Sections 2-4 and Appendices A, D.1 and E of Luo, Xu, Li, Tian, Darrell and Ma (arXiv:1807.03858v5).
- Markov decision process. States lie in a measurable space and actions in a measurable space . A policy is a Markov kernel from to , and a dynamical model is a Markov kernel from to ; a deterministic model is the kernel . The reward is known, is the discount factor, , and .
- Transition kernels. is the state-to-state kernel of on , and is its -step kernel ().
- Values (eq. 2.1). with , , and .
- Discounted state distributions. For a kernel and a distribution , . for (Definition 2.1), and (Definition A.1); the paper writes for the true model .
- Model discrepancy (eq. 4.5). against a reference model ( in Section 4.2), and .
- Discrepancy bounds. , , , and (eq. 4.7).
- Divergences. is the Kullback-Leibler divergence. is the Neyman distance (Definition E.1), equal to unless , and (Definition E.3). .
- Policy distances. (eq. 4.3) and (eq. A.1).
Formalization Note The value function is written as the discounted sum of the expected rewards at each step, computed from the -step kernels; for a bounded measurable reward this equals the paper's expectation of the discounted sum over trajectories. Divergences, and the policy distances take values in . and use , as in the proofs of Propositions 4.4 and A.2.
import Mathlib
/-!
Luo, Xu, Li, Tian, Darrell and Ma, *Algorithmic Framework for Model-based Deep Reinforcement
Learning with Theoretical Guarantees*, arXiv:1807.03858v5 (ICLR 2019): the value functions,
discounted state distributions, model discrepancy `G`, discrepancy bounds and policy distances of
Sections 2-4, Appendix A, Appendix D.1 and Appendix E.
States and actions live in arbitrary measurable spaces `S`, `A`. A policy `π(· | s)` is a kernel
`Kernel S A` and a dynamical model `M(· | s, a)` is a kernel `Kernel (S × A) S`; statements assume
them to be Markov kernels. The reward `R : S × A → ℝ` is known.
-/
open MeasureTheory ProbabilityTheory InformationTheory
open scoped ENNReal
noncomputable section
namespace MBRLGuarantees
variable {S A : Type*} [MeasurableSpace S] [MeasurableSpace A]
/-- Section 2: `κ = γ (1 - γ)⁻¹`, the constant the paper fixes "throughout the paper". -/
def kappa (γ : ℝ) : ℝ := γ / (1 - γ)
/-- Section 2: the state-to-state transition kernel of a policy `π` on a model `M`,
`s ↦ ∫ M(· | s, a) π(da | s)`, the law of `S_{t+1}` given `S_t = s`
(Appendix D.1 calls it "the transition kernel induced by some policy `π`"). -/
def stateKernel (pi : Kernel S A) (M : Kernel (S × A) S) : Kernel S S :=
Kernel.snd (pi ⊗ₖ M)
/-- The `t`-step kernel `P^t` of a transition kernel `P` on the state space (`P^0 = Id`);
`P^t(· | s)` is the law of `S_t` given `S_0 = s` (Section 2, Appendix D.1). -/
def iterKernel (P : Kernel S S) : ℕ → Kernel S S
| 0 => Kernel.id
| n + 1 => P ∘ₖ iterKernel P n
/-- Appendix D.1: `Ḡμ = (1 - γ) ∑_{t ≥ 0} γ^t P^t μ`, the discounted distribution of the states
visited by the Markov chain with transition kernel `P` started from the distribution `μ`. -/
def discOcc (γ : ℝ) (P : Kernel S S) (μ : Measure S) : Measure S :=
ENNReal.ofReal (1 - γ) • Measure.sum fun t : ℕ => ENNReal.ofReal (γ ^ t) • μ.bind (iterKernel P t)
/-- Definition A.1 (written for a general chain): `(1 - γ)² ∑_{t ≥ 1} t γ^{t-1} P^t μ`. -/
def reweightedOcc (γ : ℝ) (P : Kernel S S) (μ : Measure S) : Measure S :=
ENNReal.ofReal ((1 - γ) ^ 2) • Measure.sum fun t : ℕ =>
ENNReal.ofReal (((t : ℝ) + 1) * γ ^ t) • μ.bind (iterKernel P (t + 1))
/-- Section 2: the expected one-step reward `E_{a ∼ π(·|s)} R(s, a)` of policy `π` at state `s`. -/
def expReward (R : S × A → ℝ) (pi : Kernel S A) (s : S) : ℝ :=
∫ a, R (s, a) ∂(pi s)
/-- Section 2, eq. (2.1): the value function
`V^{π,M}(s) = E[∑_{t ≥ 0} γ^t R(S_t, A_t) | S_0 = s]` with `A_t ∼ π(· | S_t)` and
`S_{t+1} ∼ M(· | S_t, A_t)`, written as `∑_t γ^t E[R(S_t, A_t) | S_0 = s]`. -/
def stateValue (γ : ℝ) (R : S × A → ℝ) (pi : Kernel S A) (M : Kernel (S × A) S) (s : S) : ℝ :=
∑' t : ℕ, γ ^ t * ∫ s', expReward R pi s' ∂(iterKernel (stateKernel pi M) t s)
/-- Section 2: `V^{π,M} = E[V^{π,M}(S_0)]`, the expected reward-to-go averaged over the initial
state `S_0 ∼ μ₀`. -/
def value (γ : ℝ) (R : S × A → ℝ) (μ₀ : Measure S) (pi : Kernel S A) (M : Kernel (S × A) S) : ℝ :=
∫ s, stateValue γ R pi M s ∂μ₀
/-- Definition 2.1: `ρ^{π,M} = (1 - γ) ∑_{t ≥ 0} γ^t p_{S_t^{π,M}}`, the discounted distribution
of the states visited by `π` on `M` from `S_0 ∼ μ₀`; `ρ^π` is `ρ^{π,M⋆}`. -/
def occ (γ : ℝ) (μ₀ : Measure S) (pi : Kernel S A) (M : Kernel (S × A) S) : Measure S :=
discOcc γ (stateKernel pi M) μ₀
/-- Definition A.1: `β^{π} = (1 - γ)² ∑_{t ≥ 1} t γ^{t-1} p_{S_t^π}`, the re-weighted discounted
state distribution of `π` on the model `M` (the paper takes `M = M⋆`). -/
def beta (γ : ℝ) (μ₀ : Measure S) (pi : Kernel S A) (M : Kernel (S × A) S) : Measure S :=
reweightedOcc γ (stateKernel pi M) μ₀
/-- Section 4.1, eq. (4.5): `G^{π,M̂}(s, a) = E_{ŝ' ∼ M̂(·|s,a)} V^{π,M̂}(ŝ') - E_{s' ∼ M(·|s,a)} V^{π,M̂}(s')`,
the one-step discrepancy of the model `M̂` against the reference model `M` (the paper's `M = M⋆`;
Lemma 4.3 uses a general `M`). -/
def gapFn (γ : ℝ) (R : S × A → ℝ) (pi : Kernel S A) (Mhat M : Kernel (S × A) S) (s : S) (a : A) :
ℝ :=
(∫ s', stateValue γ R pi Mhat s' ∂(Mhat (s, a))) - ∫ s', stateValue γ R pi Mhat s' ∂(M (s, a))
/-- Section 4.2: `G^{π,M̂}(s) = E_{A ∼ π(·|s)} G^{π,M̂}(s, A)`. -/
def gapState (γ : ℝ) (R : S × A → ℝ) (pi : Kernel S A) (Mhat M : Kernel (S × A) S) (s : S) : ℝ :=
∫ a, gapFn γ R pi Mhat M s a ∂(pi s)
/-- Section 4.2, display before eq. (4.7): `ε₁ = E_{S ∼ ρ^{π_ref}} |G^{π,M̂}(S)|` (the form used in the proof of
Proposition 4.4, Appendix C.1). -/
def epsOne (γ : ℝ) (R : S × A → ℝ) (μ₀ : Measure S) (Mstar : Kernel (S × A) S)
(piref pi : Kernel S A) (Mhat : Kernel (S × A) S) : ℝ :=
∫ s, |gapState γ R pi Mhat Mstar s| ∂(occ γ μ₀ piref Mstar)
/-- Section 4.2, display before eq. (4.7): `ε_max = max_S |G^{π,M̂}(S)|`. -/
def epsMax (γ : ℝ) (R : S × A → ℝ) (Mstar : Kernel (S × A) S) (pi : Kernel S A)
(Mhat : Kernel (S × A) S) : ℝ :=
⨆ s, |gapState γ R pi Mhat Mstar s|
/-- Appendix A, after eq. (A.2): `ε₂ = E_{S ∼ β^{π_ref}} [G^{π,M̂}(S)²]` (the form used in the
proof of Proposition A.2, Appendix C.2). -/
def epsTwo (γ : ℝ) (R : S × A → ℝ) (μ₀ : Measure S) (Mstar : Kernel (S × A) S)
(piref pi : Kernel S A) (Mhat : Kernel (S × A) S) : ℝ :=
∫ s, gapState γ R pi Mhat Mstar s ^ 2 ∂(beta γ μ₀ piref Mstar)
/-- Section 4.2, eq. (4.7): `D^G_{π_ref,δ}(M̂, π) = κ ε₁ + κ² δ ε_max`. -/
def discG (γ : ℝ) (R : S × A → ℝ) (μ₀ : Measure S) (Mstar : Kernel (S × A) S) (δ : ℝ)
(piref : Kernel S A) (Mhat : Kernel (S × A) S) (pi : Kernel S A) : ℝ :=
kappa γ * epsOne γ R μ₀ Mstar piref pi Mhat + kappa γ ^ 2 * δ * epsMax γ R Mstar pi Mhat
/-- Section 4.1, eq. (4.3): `d^{KL}(π, π_ref) = E_{S ∼ ρ^π} [KL(π(·|S), π_ref(·|S))^{1/2}]`,
with `ρ^π` the discounted state distribution of `π` on the true model `M⋆`. -/
def dKL (γ : ℝ) (μ₀ : Measure S) (Mstar : Kernel (S × A) S) (pi piref : Kernel S A) : ℝ≥0∞ :=
∫⁻ s, klDiv (pi s) (piref s) ^ (1 / 2 : ℝ) ∂(occ γ μ₀ pi Mstar)
open Classical in
/-- Definition E.1: the Neyman `χ²` distance `χ²(p, q) = ∫ (p(x) - q(x))² / q(x) dx`, equal to
`∫ (dp/dq - 1)² dq` when `p ≪ q` and to `+∞` otherwise. -/
def chiSq {X : Type*} [MeasurableSpace X] (p q : Measure X) : ℝ≥0∞ :=
if p ≪ q then ∫⁻ x, ENNReal.ofReal (((p.rnDeriv q x).toReal - 1) ^ 2) ∂q else ∞
/-- Definition E.3: `χ²_μ(P', P) = ∫ μ(x) χ²(P'(·|x), P(·|x)) dx`, the `χ²` distance between two
transition kernels measured with respect to the distribution `μ`. -/
def chiSqTrans {X Y : Type*} [MeasurableSpace X] [MeasurableSpace Y] (μ : Measure X)
(P' P : Kernel X Y) : ℝ≥0∞ :=
∫⁻ x, chiSq (P' x) (P x) ∂μ
/-- Appendix A, eq. (A.1): `d^{χ²}(π, π_ref) = max { E_{S ∼ ρ^{π_ref}} χ²(π(·|S), π_ref(·|S)),
E_{S ∼ β^{π_ref}} χ²(π(·|S), π_ref(·|S)) }`, with `ρ^{π_ref}`, `β^{π_ref}` taken on `M⋆`. -/
def dChi (γ : ℝ) (μ₀ : Measure S) (Mstar : Kernel (S × A) S) (pi piref : Kernel S A) : ℝ≥0∞ :=
max (chiSqTrans (occ γ μ₀ piref Mstar) pi piref) (chiSqTrans (beta γ μ₀ piref Mstar) pi piref)
/-- The `ℓ₁` distance `|ρ - ρ'|₁` between two finite measures (Appendix C.3, Claim E.6,
Corollary E.7), in its dual form `sup { ∫ f dρ - ∫ f dρ' : f measurable, |f| ≤ 1 }`. -/
def lOneDist {X : Type*} [MeasurableSpace X] (ρ ρ' : Measure X) : ℝ≥0∞ :=
⨆ (f : X → ℝ) (_ : Measurable f) (_ : ∀ x, |f x| ≤ 1),
ENNReal.ofReal ((∫ x, f x ∂ρ) - ∫ x, f x ∂ρ')
end MBRLGuarantees
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Throughout, (states) and (actions) are arbitrary measurable spaces. A policy is a measurable kernel from to , written . A model is a measurable kernel from to , written . A reward is an arbitrary real function. None of the definitions below assume any of the following:
- that kernels are Markov (probability-valued), or even finite;
- that is measurable, bounded or integrable;
- that the initial measure on is a probability measure;
- that the discount lies in .
Two junk-value conventions recur below:
- A real-valued (Bochner) integral of a non-integrable function is .
- A real infinite series that is not unconditionally summable is .
kappa. For a real number ,
with real division, so at the value is (division by zero gives ).
stateKernel. Given a policy and a model , this is the kernel from to defined by
It is formed as the -marginal of the composition-product of and . In the underlying library, the composition-product is defined to be the zero kernel unless both and are s-finite kernels, in which case is identically the zero measure.
iterKernel. For a kernel from to and , is defined recursively:
- , the Dirac mass at ;
- , that is, first apply and then one more step of .
discOcc. For , a kernel on and a measure on , this is the measure
Here , since the real coefficients are converted to nonnegative extended reals by truncating negatives to . The sum is a countable sum of measures and may assign infinite mass. Edge cases:
- If , the prefactor is and the result is the zero measure.
- If , every odd- term is dropped.
- If , the result is , using .
reweightedOcc. For as above,
equivalently . Edge cases:
- The prefactor is always nonnegative, so it is not truncated. For the result is the zero measure. For it is a positive multiple of a sum with growing coefficients, which may be infinite.
- For , terms with odd are dropped.
expReward. For , and ,
which is if is not -integrable.
stateValue. For and ,
Here is expReward and is the -step iterate of stateKernel. Each integral is if not integrable. The whole series is if it is not unconditionally summable.
value. For an initial measure on ,
which is if is not -integrable. is any measure, not necessarily a probability.
occ. The measure on given by
with all the edge cases of discOcc.
beta. The measure on given by
with all the edge cases of reweightedOcc. Here is an arbitrary model.
gapFn. For a policy , two models and , and ,
In both integrals the integrand is the value function computed in the model ; only the integrating measure differs. Each integral is if not integrable.
gapState. The policy average of gapFn:
which is if not integrable.
epsOne. Given a "true" model , a reference policy , a policy and a model ,
This is a real-valued integral, equal to if not integrable. It is also whenever is the zero measure, for example when .
epsMax. The real supremum
It is a supremum, not an attained maximum. By the library's convention for real suprema, it equals if these values are unbounded above or if is empty.
epsTwo. The real-valued integral
which is if not integrable.
discG. For an additional real parameter , which is unrestricted in sign,
Here is computed with and with .
dKL. The extended-nonnegative quantity
Points to note:
- The state distribution is the occupancy of itself, not of , on .
- is the library's divergence. It equals unless and the log-likelihood ratio is -integrable. Otherwise it equals , clipped at , which is the usual KL divergence for probability measures.
- The square root of is .
chiSq. For measures on a measurable space :
Three details:
- The integral is taken against , not normalised, and need not be probability measures.
- At points where the Radon–Nikodym derivative is it is read as , so the integrand there is .
- The library's Radon–Nikodym derivative is the zero function when has no Lebesgue decomposition with respect to , for instance in non-σ-finite situations.
chiSqTrans. For a measure on and kernels from to ,
dChi. The quantity
Both state distributions are those of on .
lOneDist. For measures on , which are not assumed finite,
Each integral is a real integral, equal to if not integrable, which can happen when or has infinite mass. Since is admissible, the supremum is always at least .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.