MDPs, Bellman Equations & Value Iteration

Derive the Markov property, the Bellman expectation and optimality recursive equations, the Banach fixed-point contraction mapping proof, and zero-dependency implementations in Python and Rust.

05. Reinforcement Learning & Autonomous Agents9 min read

Reinforcement Learning (RL) formalizes how an autonomous agent learns to make optimal decisions through trial-and-error interactions with an environment. Unlike supervised learning where target outputs yy are provided, the agent receives only scalar reward signals feedback.

The mathematical foundation of all reinforcement learning is the Markov Decision Process (MDP) and the recursive decomposition formalized by Richard Bellman in the Bellman Equations.

Contraction MappingDynamic Programming
||T* u - T* v||_inf <= gamma ||u - v||_inf

The Bellman optimality operator is a gamma-contraction, guaranteeing convergence to a unique optimal value function.

1. The Markov Decision Process (MDP) Framework#

An MDP is formally defined by the 5-tuple (S,A,P,R,γ)(\mathcal{S}, \mathcal{A}, \mathcal{P}, \mathcal{R}, \gamma):

  • S\mathcal{S}: Set of all valid environment states.
  • A\mathcal{A}: Set of all valid agent actions.
  • P\mathcal{P}: State transition probability function P(s′∣s,a)=P(St+1=s′∣St=s,At=a)P(s' | s, a) = P(S_{t+1} = s' | S_t = s, A_t = a).
  • R\mathcal{R}: Expected reward function R(s,a)=E[Rt+1∣St=s,At=a]R(s, a) = \mathbb{E}[R_{t+1} | S_t = s, A_t = a].
  • γ∈[0,1)\gamma \in [0, 1): Discount factor balancing immediate versus delayed future rewards.
Architecture Diagram
Rendering diagram...

The Markov Property#

A state transition process is Markovian if the conditional probability distribution of future states depends solely upon the present state and action, completely independent of past historical trajectories:

P(St+1=s′,Rt+1=r∣St,At,St−1,At−1,…,S0,A0)=P(St+1=s′,Rt+1=r∣St,At)P(S_{t+1} = s', R_{t+1} = r \mid S_t, A_t, S_{t-1}, A_{t-1}, \dots, S_0, A_0) = P(S_{t+1} = s', R_{t+1} = r \mid S_t, A_t)

The present state StS_t constitutes a sufficient statistic of the complete history.

The Cumulative Discounted Return#

The goal of the agent is to maximize the expected cumulative discounted return GtG_t:

Gt=Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k=0}^\infty \gamma^k R_{t+k+1}

For bounded rewards ∣Rt∣≤Rmax⁡|R_t| \le R_{\max}, the discount factor γ<1\gamma < 1 guarantees that the infinite geometric series converges to a finite maximum bound:

Gt≤∑k=0∞γkRmax⁡=Rmax⁡1−γG_t \le \sum_{k=0}^\infty \gamma^k R_{\max} = \frac{R_{\max}}{1 - \gamma}

2. Derivation of the Bellman Expectation Equations#

Let π(a∣s)=P(At=a∣St=s)\pi(a | s) = P(A_t = a | S_t = s) define a stochastic policy.

We define two fundamental value functions:

  1. State-Value Function Vπ(s)V^\pi(s): Expected return starting from state ss under policy π\pi:
Vπ(s)=Eπ[Gt∣St=s]V^\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s]
  1. Action-Value Function Qπ(s,a)Q^\pi(s, a): Expected return starting from state ss, taking action aa, and thereafter following policy π\pi:
Qπ(s,a)=Eπ[Gt∣St=s,At=a]Q^\pi(s, a) = \mathbb{E}_\pi[G_t \mid S_t = s, A_t = a]
1

Step 1: Decompose Return Recursively

Notice that return GtG_t can be unrolled into immediate reward plus discounted future return:

Gt=Rt+1+γ(Rt+2+γRt+3+… )=Rt+1+γGt+1G_t = R_{t+1} + \gamma \left( R_{t+2} + \gamma R_{t+3} + \dots \right) = R_{t+1} + \gamma G_{t+1}
2

Step 2: Expand the Expectation for $V^\pi(s)$

Substitute this recursive relation into the definition of Vπ(s)V^\pi(s):

Vπ(s)=Eπ[Rt+1+γGt+1∣St=s]V^\pi(s) = \mathbb{E}_\pi\Big[ R_{t+1} + \gamma G_{t+1} \mid S_t = s \Big]

Apply the Law of Total Expectation by marginalizing over action aa and subsequent state s′s':

Vπ(s)=∑a∈Aπ(a∣s)∑s′∈SP(s′∣s,a)[R(s,a)+γEπ[Gt+1∣St+1=s′]]V^\pi(s) = \sum_{a \in \mathcal{A}} \pi(a | s) \sum_{s' \in \mathcal{S}} P(s' \mid s, a) \left[ R(s, a) + \gamma \mathbb{E}_\pi[G_{t+1} \mid S_{t+1} = s'] \right]
3

Step 3: Substitute the Value of Next State $V^\pi(s')$

By definition, Eπ[Gt+1∣St+1=s′]=Vπ(s′)\mathbb{E}_\pi[G_{t+1} \mid S_{t+1} = s'] = V^\pi(s'). Substituting this yields the Bellman Expectation Equation for V(s)V(s):

Vπ(s)=∑a∈Aπ(a∣s)[R(s,a)+γ∑s′∈SP(s′∣s,a)Vπ(s′)]V^\pi(s) = \sum_{a \in \mathcal{A}} \pi(a | s) \left[ R(s, a) + \gamma \sum_{s' \in \mathcal{S}} P(s' \mid s, a) V^\pi(s') \right]
4

Step 4: Formulate the Action-Value Equation $Q^\pi(s, a)$

Similarly, for a fixed initial action aa:

Qπ(s,a)=R(s,a)+γ∑s′∈SP(s′∣s,a)Vπ(s′)Q^\pi(s, a) = R(s, a) + \gamma \sum_{s' \in \mathcal{S}} P(s' \mid s, a) V^\pi(s')

Because Vπ(s′)=∑a′∈Aπ(a′∣s′)Qπ(s′,a′)V^\pi(s') = \sum_{a' \in \mathcal{A}} \pi(a' | s') Q^\pi(s', a'):

Qπ(s,a)=R(s,a)+γ∑s′∈SP(s′∣s,a)∑a′∈Aπ(a′∣s′)Qπ(s′,a′)Q^\pi(s, a) = R(s, a) + \gamma \sum_{s' \in \mathcal{S}} P(s' \mid s, a) \sum_{a' \in \mathcal{A}} \pi(a' \mid s') Q^\pi(s', a')

3. Bellman Optimality Equations & Contraction Mapping#

The optimal state-value function V∗(s)=max⁡πVπ(s)V^*(s) = \max_\pi V^\pi(s) and optimal action-value function Q∗(s,a)=max⁡πQπ(s,a)Q^*(s, a) = \max_\pi Q^\pi(s, a) represent the maximum return achievable under any policy.

Under an optimal policy, the agent greedily selects the action that maximizes expected future return, replacing the expectation ∑aπ(a∣s)\sum_a \pi(a|s) with the max⁡a\max_a operator:

V∗(s)=max⁡a∈A[R(s,a)+γ∑s′∈SP(s′∣s,a)V∗(s′)]V^*(s) = \max_{a \in \mathcal{A}} \left[ R(s, a) + \gamma \sum_{s' \in \mathcal{S}} P(s' \mid s, a) V^*(s') \right]
Q∗(s,a)=R(s,a)+γ∑s′∈SP(s′∣s,a)max⁡a′∈AQ∗(s′,a′)Q^*(s, a) = R(s, a) + \gamma \sum_{s' \in \mathcal{S}} P(s' \mid s, a) \max_{a' \in \mathcal{A}} Q^*(s', a')

Mathematical Proof: The Contraction Mapping Theorem#

Let T∗T^* denote the Bellman optimality operator mapping value functions V→VV \to V:

(T∗V)(s)=max⁡a∈A[R(s,a)+γ∑s′P(s′∣s,a)V(s′)](T^* V)(s) = \max_{a \in \mathcal{A}} \left[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) V(s') \right]

We prove that T∗T^* is a contraction mapping in the infinity norm ∥u−v∥∞=max⁡s∣u(s)−v(s)∣\|u - v\|_\infty = \max_s |u(s) - v(s)|.

For any two value functions uu and vv:

∣(T∗u)(s)−(T∗v)(s)∣=∣max⁡a(R(s,a)+γ∑s′P(s′∣s,a)u(s′))−max⁡a(R(s,a)+γ∑s′P(s′∣s,a)v(s′))∣|(T^* u)(s) - (T^* v)(s)| = \left| \max_a \left( R(s, a) + \gamma \sum_{s'} P(s'|s, a) u(s') \right) - \max_a \left( R(s, a) + \gamma \sum_{s'} P(s'|s, a) v(s') \right) \right|

Using the algebraic inequality ∣max⁡af(a)−max⁡ag(a)∣≤max⁡a∣f(a)−g(a)∣|\max_a f(a) - \max_a g(a)| \le \max_a |f(a) - g(a)|:

≤max⁡a∣γ∑s′P(s′∣s,a)(u(s′)−v(s′))∣\le \max_a \left| \gamma \sum_{s'} P(s' \mid s, a) \Big( u(s') - v(s') \Big) \right|
≤γmax⁡a∑s′P(s′∣s,a)∣u(s′)−v(s′)∣≤γmax⁡a∑s′P(s′∣s,a)∥u−v∥∞\le \gamma \max_a \sum_{s'} P(s' \mid s, a) |u(s') - v(s')| \le \gamma \max_a \sum_{s'} P(s' \mid s, a) \|u - v\|_\infty

Since ∑s′P(s′∣s,a)=1\sum_{s'} P(s' \mid s, a) = 1:

∥(T∗u)−(T∗v)∥∞≤γ∥u−v∥∞\|(T^* u) - (T^* v)\|_\infty \le \gamma \|u - v\|_\infty
Guaranteed Convergence of Value Iteration
Because discount factor γ<1\gamma < 1, the operator T∗T^* is a strict contraction. By the Banach Fixed-Point Theorem: 1. There exists a unique fixed point V∗V^* such that T∗V∗=V∗T^* V^* = V^*. 2. Repeated iteration Vk+1=T∗VkV_{k+1} = T^* V_k from any arbitrary initialization V0V_0 is guaranteed to converge to V∗V^* at a linear rate bounded by γk\gamma^k.

4. From-Scratch Implementation: Zero Dependencies#

The following implementations construct an exact Value Iteration Algorithm for a deterministic GridWorld environment from scratch in pure Python and pure Rust.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import List, Dict, Tuple class GridWorldFromScratch: def __init__(self, size: int = 4, gamma: float = 0.9, tol: float = 1e-4): self.size = size self.gamma = gamma self.tol = tol # Actions: Up, Down, Left, Right self.actions = [(-1, 0), (1, 0), (0, -1), (0, 1)] self.terminal_states = {(0, 0), (size - 1, size - 1)} def is_terminal(self, r: int, c: int) -> bool: return (r, c) in self.terminal_states def step(self, r: int, c: int, action: Tuple[int, int]) -> Tuple[int, int, float]: if self.is_terminal(r, c): return r, c, 0.0 dr, dc = action next_r = max(0, min(self.size - 1, r + dr)) next_c = max(0, min(self.size - 1, c + dc)) reward = -1.0 # Step cost return next_r, next_c, reward def value_iteration(self) -> List[List[float]]: # Initialize value function table with zeros V = [[0.0] * self.size for _ in range(self.size)] while True: delta = 0.0 new_V = [[0.0] * self.size for _ in range(self.size)] for r in range(self.size): for c in range(self.size): if self.is_terminal(r, c): continue # Bellman Optimality: max over all actions [R + gamma * V(s')] action_values = [] for act in self.actions: next_r, next_c, reward = self.step(r, c, act) val = reward + self.gamma * V[next_r][next_c] action_values.append(val) best_val = max(action_values) new_V[r][c] = best_val delta = max(delta, abs(V[r][c] - best_val)) V = new_V if delta < self.tol: break return V if __name__ == "__main__": grid = GridWorldFromScratch(size=4, gamma=0.9) optimal_V = grid.value_iteration() print("Optimal State-Value Function V*(s) for 4x4 GridWorld:") for row in optimal_V: print(" ", [round(v, 2) for v in row])

5. Active Knowledge Checks#

Concept check

Why does the Contraction Mapping Theorem guarantee that Value Iteration converges to a unique optimal value function V*?

Concept check

In a Markov Decision Process, what is the defining structural property that distinguishes the Bellman Optimality Equation from the Bellman Expectation Equation?