Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Finite MDPs, total variation, returns and branched rollouts (MBPO)

Definition
WhenToTrustYourModel_Basic

by Xeroth · Nov 24, 2026 · Mathlib 0df444a (Lean v4.33.1)

markov-decision-processmodel-based-rlreinforcement-learningtotal-variation

Objects of Sections 3-4 and Appendices A-B of Janner, Fu, Zhang and Levine (2019).

  1. Distributions and total variation. A distribution on a finite set XXX is a function μ:X→[0,∞)\mu:X\to[0,\infty)μ:X→[0,∞) with ∑xμ(x)=1\sum_x\mu(x)=1∑x​μ(x)=1. The total variation distance is DTV(μ∥ν)=12∑x∣μ(x)−ν(x)∣D_{TV}(\mu\|\nu)=\tfrac12\sum_x|\mu(x)-\nu(x)|DTV​(μ∥ν)=21​∑x​∣μ(x)−ν(x)∣. From a marginal p(x)p(x)p(x) and a conditional p(y∣x)p(y\mid x)p(y∣x) one forms the joint p(x,y)=p(x) p(y∣x)p(x,y)=p(x)\,p(y\mid x)p(x,y)=p(x)p(y∣x).
  2. Markov chains. For an initial distribution μ0\mu_0μ0​ and transition kernels Kt(s,s′)K_t(s,s')Kt​(s,s′), the state marginals are p0=μ0p^0=\mu_0p0=μ0​ and pt+1(s′)=∑spt(s)Kt(s,s′)p^{t+1}(s')=\sum_s p^t(s)K_t(s,s')pt+1(s′)=∑s​pt(s)Kt​(s,s′).
  3. Markov decision process. Finite state and action sets S,A\mathcal S,\mathcal AS,A, a reward r(s,a)r(s,a)r(s,a), an initial distribution ρ0\rho_0ρ0​ and a discount γ\gammaγ. A policy is a family of distributions π(⋅∣s)\pi(\cdot\mid s)π(⋅∣s) on A\mathcal AA; dynamics (the true ppp or a learned model p^\hat pp^​) are a family of distributions p(⋅∣s,a)p(\cdot\mid s,a)p(⋅∣s,a) on S\mathcal SS. A process that acts with policy πt\pi_tπt​ and moves with dynamics ptp_tpt​ at time ttt has state marginals ptp^tpt given by the chain with kernel Kt(s,s′)=∑aπt(a∣s)pt(s′∣s,a)K_t(s,s')=\sum_a\pi_t(a\mid s)p_t(s'\mid s,a)Kt​(s,s′)=∑a​πt​(a∣s)pt​(s′∣s,a), state-action marginals pt(s,a)=pt(s)πt(a∣s)p^t(s,a)=p^t(s)\pi_t(a\mid s)pt(s,a)=pt(s)πt​(a∣s), and return
η=∑t≥0γt∑s,apt(s,a) r(s,a).\eta=\sum_{t\ge0}\gamma^t\sum_{s,a}p^t(s,a)\,r(s,a).η=t≥0∑​γts,a∑​pt(s,a)r(s,a).

For a stationary policy π\piπ this is η[π]\eta[\pi]η[π] under the true dynamics and the model return η^[π]\hat\eta[\pi]η^​[π] under p^\hat pp^​. 4. Error quantities (Section 4.1). For a state distribution ddd, a policy π\piπ and two dynamics p1,p2p_1,p_2p1​,p2​, the expected total variation is Es∼d, a∼π(⋅∣s)[DTV(p1(⋅∣s,a)∥p2(⋅∣s,a))]E_{s\sim d,\,a\sim\pi(\cdot\mid s)}\big[D_{TV}(p_1(\cdot\mid s,a)\|p_2(\cdot\mid s,a))\big]Es∼d,a∼π(⋅∣s)​[DTV​(p1​(⋅∣s,a)∥p2​(⋅∣s,a))]. 5. Branched rollouts (Section 4.2, Lemma B.4). For a branch length kkk, a two-phase process uses the branch (post-branch) policy and dynamics at the time steps t<kt<kt<k and the pre-branch policy and dynamics at the time steps t≥kt\ge kt≥k. The branched return ηbranch[π]\eta^{\mathrm{branch}}[\pi]ηbranch[π] is the return of the process that runs π\piπ under the model p^\hat pp^​ for the kkk branch steps and the data-collecting policy πD\pi_DπD​ under the true dynamics ppp 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 πD\pi_DπD​ until a branch point and then runs kkk model steps under π\piπ, but the proof of Lemma B.4 bounds the marginals at the times t≤kt\le kt≤k by the post-branch errors alone, which places the branch segment at the times 0,…,k−10,\dots,k-10,…,k−1. The definitions follow the proof.

Definition code
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
Source
Janner, Fu, Zhang and Levine, When to Trust Your Model: Model-Based Policy Optimization, arXiv:1906.08253v3 (NeurIPS 2019), Section 3, Sections 4.1-4.3, Appendix A (Theorems A.1-A.3) and Appendix B (Lemmas B.1-B.4)
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Setting. Throughout, SSS (states) and AAA (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 XXX and a function μ:X→R\mu : X \to \mathbb{R}μ:X→R, "μ\muμ is a distribution" means both of the following hold:

μ(x)≥0 for every x∈X,∑x∈Xμ(x)=1.\mu(x) \ge 0 \text{ for every } x \in X, \qquad \sum_{x \in X} \mu(x) = 1.μ(x)≥0 for every x∈X,x∈X∑​μ(x)=1.

If XXX is empty, the sum is 000, so no function on an empty type satisfies this condition.

Total variation. For a finite type XXX and any two functions μ,ν:X→R\mu, \nu : X \to \mathbb{R}μ,ν:X→R (not required to be distributions):

DTV(μ ∥ ν)=12∑x∈X∣μ(x)−ν(x)∣.D_{TV}(\mu \,\|\, \nu) = \tfrac12 \sum_{x \in X} |\mu(x) - \nu(x)|.DTV​(μ∥ν)=21​x∈X∑​∣μ(x)−ν(x)∣.

This equals 000 when XXX is empty.

Joint distribution. For arbitrary types X,YX, YX,Y (not required to be finite), a function p:X→Rp : X \to \mathbb{R}p:X→R and a function q:X→Y→Rq : X \to Y \to \mathbb{R}q:X→Y→R, the joint function on X×YX \times YX×Y is

pjoint(x,y)=p(x) q(x,y).p_{\mathrm{joint}}(x, y) = p(x)\, q(x, y).pjoint​(x,y)=p(x)q(x,y).

No normalization or nonnegativity is assumed of ppp or qqq.

Chain marginals. Take an initial function μ0:S→R\mu_0 : S \to \mathbb{R}μ0​:S→R and a time-indexed family Kt(s,s′)K_t(s, s')Kt​(s,s′), for t∈Nt \in \mathbb{N}t∈N and s,s′∈Ss, s' \in Ss,s′∈S. Neither is required to be a distribution or a stochastic kernel. The sequence pt:S→Rp^t : S \to \mathbb{R}pt:S→R is defined recursively by

p0=μ0,pt+1(s′)=∑s∈Spt(s) Kt(s,s′).p^0 = \mu_0, \qquad p^{t+1}(s') = \sum_{s \in S} p^t(s)\, K_t(s, s').p0=μ0​,pt+1(s′)=s∈S∑​pt(s)Kt​(s,s′).

The kernel indexed by ttt is the one used to pass from time ttt to time t+1t+1t+1.

Policy. A function π:S→A→R\pi : S \to A \to \mathbb{R}π:S→A→R, written π(a∣s)\pi(a \mid s)π(a∣s), is a policy if, for every state sss, the function a↦π(a∣s)a \mapsto \pi(a \mid s)a↦π(a∣s) is a distribution on AAA.

  • If SSS is empty, every π\piπ is a policy, vacuously.
  • If SSS is nonempty and AAA is empty, no policy exists.

Dynamics. A function P:S→A→S→RP : S \to A \to S \to \mathbb{R}P:S→A→S→R, written P(s′∣s,a)P(s' \mid s, a)P(s′∣s,a), is a dynamics if, for every state sss and every action aaa, the function s′↦P(s′∣s,a)s' \mapsto P(s' \mid s, a)s′↦P(s′∣s,a) is a distribution on SSS.

  • If SSS is empty or AAA is empty, every PPP qualifies, vacuously.

Induced kernel. Take a time-indexed family of functions πt(a∣s)\pi_t(a \mid s)πt​(a∣s) and a time-indexed family Pt(s′∣s,a)P_t(s' \mid s, a)Pt​(s′∣s,a). Neither is required to be a policy or a dynamics. The induced state-to-state kernel at time ttt is

Kt(s,s′)=∑a∈Aπt(a∣s) Pt(s′∣s,a).K_t(s, s') = \sum_{a \in A} \pi_t(a \mid s)\, P_t(s' \mid s, a).Kt​(s,s′)=a∈A∑​πt​(a∣s)Pt​(s′∣s,a).

It is 000 if AAA is empty.

State marginal. Given ρ0:S→R\rho_0 : S \to \mathbb{R}ρ0​:S→R and families (πt)(\pi_t)(πt​) and (Pt)(P_t)(Pt​) as above, pρ0,π,Ptp^t_{\rho_0, \pi, P}pρ0​,π,Pt​ is the chain marginal from the previous definitions, started at ρ0\rho_0ρ0​ and driven by the induced kernel KtK_tKt​. Concretely:

p0=ρ0,pt+1(s′)=∑s pt(s)∑aπt(a∣s) Pt(s′∣s,a).p^0 = \rho_0, \qquad p^{t+1}(s') = \sum_{s}\, p^t(s) \sum_{a} \pi_t(a \mid s)\, P_t(s' \mid s, a).p0=ρ0​,pt+1(s′)=s∑​pt(s)a∑​πt​(a∣s)Pt​(s′∣s,a).

Expected reward at time ttt. For a reward function r:S×A→Rr : S \times A \to \mathbb{R}r:S×A→R (unrestricted), ρ0\rho_0ρ0​, (πt)(\pi_t)(πt​), (Pt)(P_t)(Pt​) and t∈Nt \in \mathbb{N}t∈N:

Rt=∑s∈S∑a∈Apt(s) πt(a∣s) r(s,a).R_t = \sum_{s \in S} \sum_{a \in A} p^t(s)\, \pi_t(a \mid s)\, r(s, a).Rt​=s∈S∑​a∈A∑​pt(s)πt​(a∣s)r(s,a).

Here ptp^tpt is the state marginal above, and the policy used is the one indexed by the same time ttt.

Discounted return. For any real γ\gammaγ (no restriction such as 0≤γ<10 \le \gamma < 10≤γ<1 is imposed) and the data above, the return is

η=∑t=0∞γtRt,\eta = \sum_{t=0}^{\infty} \gamma^t R_t,η=t=0∑∞​γtRt​,

with γ0=1\gamma^0 = 1γ0=1, even when γ=0\gamma = 0γ=0. The infinite sum is the unconditional sum over N\mathbb{N}N. If the family (γtRt)t(\gamma^t R_t)_{t}(γtRt​)t​ is not summable, the value is defined to be 000, rather than being undefined or infinite.

Stationary policy return. For γ\gammaγ, rrr, ρ0\rho_0ρ0​, a single function π(a∣s)\pi(a \mid s)π(a∣s) and a single function P(s′∣s,a)P(s' \mid s, a)P(s′∣s,a), ηP[π]\eta_P[\pi]ηP​[π] is the discounted return above, using the constant families πt=π\pi_t = \piπt​=π and Pt=PP_t = PPt​=P for all ttt. No policy or dynamics conditions are assumed, and the non-summable-gives-000 convention is inherited.

Expected total variation. For a function d:S→Rd : S \to \mathbb{R}d:S→R, a function π(a∣s)\pi(a \mid s)π(a∣s) and two functions P,Q:S→A→S→RP, Q : S \to A \to S \to \mathbb{R}P,Q:S→A→S→R (none required to be distributions):

ϵ=∑s∈Sd(s)∑a∈Aπ(a∣s) DTV(P(⋅∣s,a) ∥ Q(⋅∣s,a)).\epsilon = \sum_{s \in S} d(s) \sum_{a \in A} \pi(a \mid s)\, D_{TV}\big(P(\cdot \mid s, a) \,\|\, Q(\cdot \mid s, a)\big).ϵ=s∈S∑​d(s)a∈A∑​π(a∣s)DTV​(P(⋅∣s,a)∥Q(⋅∣s,a)).

Two-phase schedule. For k∈Nk \in \mathbb{N}k∈N and two values "post" and "pre" of any type, the schedule at time ttt is "post" if t<kt < kt<k and "pre" if t≥kt \ge kt≥k.

  • If k=0k = 0k=0, it is "pre" at every time.
  • The value named "post" is the one used first in time, at times 0,…,k−10, \dots, k-10,…,k−1.

Branched return. Take γ\gammaγ, rrr, ρ0\rho_0ρ0​, k∈Nk \in \mathbb{N}k∈N and four functions πpost\pi_{\mathrm{post}}πpost​, PpostP_{\mathrm{post}}Ppost​, πpre\pi_{\mathrm{pre}}πpre​, PpreP_{\mathrm{pre}}Ppre​. No policy or dynamics conditions are assumed. The branched return is the discounted return of the time-varying process with

πt={πpostt<kπpret≥k,Pt={Ppostt<kPpret≥k.\pi_t = \begin{cases} \pi_{\mathrm{post}} & t < k \\ \pi_{\mathrm{pre}} & t \ge k \end{cases}, \qquad P_t = \begin{cases} P_{\mathrm{post}} & t < k \\ P_{\mathrm{pre}} & t \ge k \end{cases}.πt​={πpost​πpre​​t<kt≥k​,Pt​={Ppost​Ppre​​t<kt≥k​.

This has the following consequences:

  • The process starts from ρ0\rho_0ρ0​ at time 000.
  • The transitions from time ttt to t+1t+1t+1 use PpostP_{\mathrm{post}}Ppost​ and πpost\pi_{\mathrm{post}}πpost​ for t<kt < kt<k.
  • Rewards at times 0,…,k−10, \dots, k-10,…,k−1 are weighted by πpost\pi_{\mathrm{post}}πpost​.
  • From time kkk onward, both the dynamics and the policy switch to the "pre" pair. They continue from the state marginal reached after kkk "post" steps; there is no reset.
  • When k=0k = 0k=0, the branched return equals the return of the constant process (πpre,Ppre)(\pi_{\mathrm{pre}}, P_{\mathrm{pre}})(πpre​,Ppre​).
  • The value is 000 if the discounted series is not summable.
Human review
  • Endorsed by Shuze Chen · Nov 24, 2026

    Confirmed by the moderator at approval.

  • Endorsed by Xeroth · Nov 24, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me