Finite MDPs, total variation, returns and branched rollouts (MBPO)
DefinitionWhenToTrustYourModel_BasicObjects of Sections 3-4 and Appendices A-B of Janner, Fu, Zhang and Levine (2019).
- Distributions and total variation. A distribution on a finite set is a function with . The total variation distance is . From a marginal and a conditional one forms the joint .
- Markov chains. For an initial distribution and transition kernels , the state marginals are and .
- Markov decision process. Finite state and action sets , a reward , an initial distribution and a discount . A policy is a family of distributions on ; dynamics (the true or a learned model ) are a family of distributions on . A process that acts with policy and moves with dynamics at time has state marginals given by the chain with kernel , state-action marginals , and return
For a stationary policy this is under the true dynamics and the model return under . 4. Error quantities (Section 4.1). For a state distribution , a policy and two dynamics , the expected total variation is . 5. Branched rollouts (Section 4.2, Lemma B.4). For a branch length , a two-phase process uses the branch (post-branch) policy and dynamics at the time steps and the pre-branch policy and dynamics at the time steps . The branched return is the return of the process that runs under the model for the branch steps and the data-collecting policy under the true dynamics otherwise.
Formalization Note The paper writes every expectation as a sum over states and actions; the state and action spaces are finite types. The paper never defines the branched return formally. Its text describes a rollout that follows until a branch point and then runs model steps under , but the proof of Lemma B.4 bounds the marginals at the times by the post-branch errors alone, which places the branch segment at the times . The definitions follow the proof.
import Mathlib
/-!
Janner, Fu, Zhang and Levine, *When to Trust Your Model: Model-Based Policy Optimization*,
arXiv:1906.08253v3 (NeurIPS 2019): discrete distributions and total variation (Appendix B),
Markov chains and their state marginals (Lemma B.2), the discounted Markov decision process of
Section 3, its returns, the model-error and policy-divergence quantities of Section 4.1, and the
two-phase (branched) rollouts of Section 4.2 and Lemma B.4.
State and action spaces are finite types: the paper writes every expectation as a sum over states
and actions. A distribution on a finite type is a nonnegative real function with total mass `1`.
-/
noncomputable section
namespace WhenToTrustYourModel
variable {S A : Type*} [Fintype S] [Fintype A]
/-- Appendix B: `μ` is a probability distribution on the finite type `X` (nonnegative, total mass
`1`). -/
def IsDist {X : Type*} [Fintype X] (μ : X → ℝ) : Prop :=
(∀ x, 0 ≤ μ x) ∧ ∑ x, μ x = 1
/-- Appendix B, proof of Lemma B.1: the total variation distance
`D_TV(μ ‖ ν) = ½ ∑_x |μ(x) - ν(x)|`. -/
def tvDist {X : Type*} [Fintype X] (μ ν : X → ℝ) : ℝ :=
(1 / 2) * ∑ x, |μ x - ν x|
/-- Lemma B.1: the joint distribution `p(x, y) = p(x) p(y | x)` built from a marginal `p` and a
conditional `q x y = p(y | x)`. -/
def jointDist {X Y : Type*} (p : X → ℝ) (q : X → Y → ℝ) : X × Y → ℝ :=
fun z => p z.1 * q z.1 z.2
/-- Lemma B.2: the state marginal `p^t(s)` at time `t` of a Markov chain with initial distribution
`μ0` and (possibly time-dependent) transition kernels `K t s s' = p(s_{t+1} = s' | s_t = s)`:
`p^0 = μ0` and `p^{t+1}(s') = ∑_s p^t(s) K_t(s, s')`. -/
def chainDist (μ0 : S → ℝ) (K : ℕ → S → S → ℝ) : ℕ → S → ℝ
| 0 => μ0
| t + 1 => fun s' => ∑ s, chainDist μ0 K t s * K t s s'
/-- Section 3: `π s a = π(a | s)` is a policy, i.e. a distribution over actions in every state. -/
def IsPolicy (π : S → A → ℝ) : Prop :=
∀ s, IsDist (π s)
/-- Section 3: `P s a s' = p(s' | s, a)` is a transition distribution (true dynamics or a learned
model), i.e. a distribution over next states for every state-action pair. -/
def IsDynamics (P : S → A → S → ℝ) : Prop :=
∀ s a, IsDist (P s a)
/-- Section 4.2 and Lemma B.4: the state-to-state kernel at time `t` of the (possibly
nonstationary) process that acts with policy `πs t` and moves with dynamics `Ps t`:
`∑_a π_t(a | s) p_t(s' | s, a)`. -/
def inducedKernel (πs : ℕ → S → A → ℝ) (Ps : ℕ → S → A → S → ℝ) : ℕ → S → S → ℝ :=
fun t s s' => ∑ a, πs t s a * Ps t s a s'
/-- Lemmas B.3 and B.4: the state marginal `p^t(s)` of the process started from `ρ0` that acts at
time `t` with policy `πs t` and moves with dynamics `Ps t`. -/
def stateDist (ρ0 : S → ℝ) (πs : ℕ → S → A → ℝ) (Ps : ℕ → S → A → S → ℝ) : ℕ → S → ℝ :=
chainDist ρ0 (inducedKernel πs Ps)
/-- Proof of Lemma B.3: the expected reward `∑_{s,a} p^t(s, a) r(s, a)` at time `t`, where
`p^t(s, a) = p^t(s) π_t(a | s)`. -/
def expReward (r : S → A → ℝ) (ρ0 : S → ℝ) (πs : ℕ → S → A → ℝ) (Ps : ℕ → S → A → S → ℝ)
(t : ℕ) : ℝ :=
∑ s, ∑ a, stateDist ρ0 πs Ps t s * πs t s a * r s a
/-- Section 3 and proof of Lemma B.3: the expected discounted return
`E[∑_{t ≥ 0} γ^t r(s_t, a_t)] = ∑_t γ^t ∑_{s,a} p^t(s, a) r(s, a)` of a (possibly nonstationary)
process. -/
def discReturn (γ : ℝ) (r : S → A → ℝ) (ρ0 : S → ℝ) (πs : ℕ → S → A → ℝ)
(Ps : ℕ → S → A → S → ℝ) : ℝ :=
∑' t : ℕ, γ ^ t * expReward r ρ0 πs Ps t
/-- Section 3 and Section 4.1: the return `η[π]` of the stationary policy `π` under dynamics `P`
(the true dynamics give `η[π]`, a learned model `p̂` gives the model return `η̂[π]`). -/
def policyReturn (γ : ℝ) (r : S → A → ℝ) (ρ0 : S → ℝ) (π : S → A → ℝ) (P : S → A → S → ℝ) : ℝ :=
discReturn γ r ρ0 (fun _ => π) (fun _ => P)
/-- Section 4.1 and Theorems A.1-A.3: the expected total variation between two transition
distributions, `E_{s ∼ d, a ∼ π(·|s)} [D_TV(P(·|s,a) ‖ Q(·|s,a))]`, for a state distribution `d`
and a policy `π`. -/
def expectedTV (d : S → ℝ) (π : S → A → ℝ) (P Q : S → A → S → ℝ) : ℝ :=
∑ s, d s * ∑ a, π s a * tvDist (P s a) (Q s a)
/-- Section 4.2 and Lemma B.4: a two-phase schedule that uses `post` (the `k`-step branch) at the
time steps `t < k` and `pre` at the time steps `t ≥ k`. This is the time ordering under which the
proof of Lemma B.4 bounds the marginals ("for `t ≤ k`" only the post-branch errors enter). -/
def phase {α : Type*} (k : ℕ) (post pre : α) (t : ℕ) : α :=
if t < k then post else pre
/-- Section 4.2 and Theorems A.2-A.3: the return of a `k`-branched rollout whose branch runs policy
`πpost` with dynamics `Ppost` for `k` steps, the rest of the rollout running policy `πpre` with
dynamics `Ppre`. The branched return `η^branch[π]` is
`branchedReturn γ r ρ0 k π p̂ π_D p`. -/
def branchedReturn (γ : ℝ) (r : S → A → ℝ) (ρ0 : S → ℝ) (k : ℕ) (πpost : S → A → ℝ)
(Ppost : S → A → S → ℝ) (πpre : S → A → ℝ) (Ppre : S → A → S → ℝ) : ℝ :=
discReturn γ r ρ0 (phase k πpost πpre) (phase k Ppost Ppre)
end WhenToTrustYourModel
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Setting. Throughout, (states) and (actions) are arbitrary finite types; either may be empty. All functions below take real values. Except where a definition explicitly says so, nothing requires these functions to be nonnegative, normalized or bounded.
Probability distribution. For a finite type and a function , " is a distribution" means both of the following hold:
If is empty, the sum is , so no function on an empty type satisfies this condition.
Total variation. For a finite type and any two functions (not required to be distributions):
This equals when is empty.
Joint distribution. For arbitrary types (not required to be finite), a function and a function , the joint function on is
No normalization or nonnegativity is assumed of or .
Chain marginals. Take an initial function and a time-indexed family , for and . Neither is required to be a distribution or a stochastic kernel. The sequence is defined recursively by
The kernel indexed by is the one used to pass from time to time .
Policy. A function , written , is a policy if, for every state , the function is a distribution on .
- If is empty, every is a policy, vacuously.
- If is nonempty and is empty, no policy exists.
Dynamics. A function , written , is a dynamics if, for every state and every action , the function is a distribution on .
- If is empty or is empty, every qualifies, vacuously.
Induced kernel. Take a time-indexed family of functions and a time-indexed family . Neither is required to be a policy or a dynamics. The induced state-to-state kernel at time is
It is if is empty.
State marginal. Given and families and as above, is the chain marginal from the previous definitions, started at and driven by the induced kernel . Concretely:
Expected reward at time . For a reward function (unrestricted), , , and :
Here is the state marginal above, and the policy used is the one indexed by the same time .
Discounted return. For any real (no restriction such as is imposed) and the data above, the return is
with , even when . The infinite sum is the unconditional sum over . If the family is not summable, the value is defined to be , rather than being undefined or infinite.
Stationary policy return. For , , , a single function and a single function , is the discounted return above, using the constant families and for all . No policy or dynamics conditions are assumed, and the non-summable-gives- convention is inherited.
Expected total variation. For a function , a function and two functions (none required to be distributions):
Two-phase schedule. For and two values "post" and "pre" of any type, the schedule at time is "post" if and "pre" if .
- If , it is "pre" at every time.
- The value named "post" is the one used first in time, at times .
Branched return. Take , , , and four functions , , , . No policy or dynamics conditions are assumed. The branched return is the discounted return of the time-varying process with
This has the following consequences:
- The process starts from at time .
- The transitions from time to use and for .
- Rewards at times are weighted by .
- From time onward, both the dynamics and the policy switch to the "pre" pair. They continue from the state marginal reached after "post" steps; there is no reset.
- When , the branched return equals the return of the constant process .
- The value is if the discounted series is not summable.
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.