Markov Decision Processes and Bellman Equations
Reinforcement learning trains an agent by reward instead of labeled answers. Its mathematical setting is the Markov decision process (MDP), and its central identities are the Bellman equations, which express the value of a state through the values of the states that follow. They are the foundation of game-playing agents and, through policy gradients, of the reinforcement-learning stage of LLM training.
The MDP
An MDP has states , actions , transition probabilities , rewards and a discount factor . A policy chooses actions. The agent wants to maximize the expected return
The discount makes the sum finite and prefers sooner rewards. Markov means the future depends on the past only through the current state.
Value functions and the Bellman equations
The state value and the action value . Splitting the return into the first reward plus the discounted rest gives the Bellman expectation equation:
For a fixed policy this is a linear system: , so . The optimal values satisfy the Bellman optimality equation, with a max instead of the average over actions:
import numpy as np
# A 5-state corridor: actions 0 = left, 1 = right; reaching state 4 gives reward 1 and ends the episode.
nS, gamma = 5, 0.9
P = np.zeros((2, nS, nS)); R = np.zeros((2, nS))
for s in range(nS - 1):
P[0, s, max(s - 1, 0)] = 1; P[1, s, s + 1] = 1
R[1, s] = 1.0 if s + 1 == nS - 1 else 0.0
# state 4 is terminal: no transitions, no reward
pi_uniform = np.full((nS, 2), 0.5)
P_pi = np.einsum("sa,ast->st", pi_uniform, P); r_pi = np.einsum("sa,as->s", pi_uniform, R)
v_uniform = np.linalg.solve(np.eye(nS) - gamma * P_pi, r_pi)
print("V of the coin-flip policy (exact linear solve):", np.round(v_uniform, 3))
V = np.zeros(nS) # value iteration for V*
for it in range(100):
Q = R + gamma * P @ V # Q[a, s]
V_new = Q.max(axis=0)
if np.max(np.abs(V_new - V)) < 1e-10: break
V = V_new
print("V* by value iteration:", np.round(V, 3), " after", it, "iterations")
print("greedy policy (1 = right):", Q.argmax(axis=0)[:-1])V of the coin-flip policy (exact linear solve): [0.275 0.336 0.472 0.712 0. ]
V* by value iteration: [0.729 0.81 0.9 1. 0. ] after 4 iterations
greedy policy (1 = right): [1 1 1 1]
The optimal values are for a state steps from the goal: .
Value iteration converges
The Bellman optimality operator is a contraction in the max norm with factor : applying it to two value estimates shrinks their maximum difference by at least . By the Banach fixed-point theorem, value iteration converges to the unique from any start, with error shrinking like .
When the model is unknown
Without and , the agent learns from experience. Temporal-difference learning nudges toward the one-sample target ; Q-learning uses ; deep Q-networks approximate with a network. Policy-gradient methods skip values and adjust the policy directly (Policy Gradients and RLHF).
In software
- Gymnasium (formerly OpenAI Gym) defines environments with
resetandstep(action) -> (obs, reward, terminated, truncated, info). - Stable-Baselines3 implements DQN, PPO and other algorithms on top.
- In RLHF the "environment" is a prompt, the "action" a generated response and the reward a learned reward model's score.
Exercises
Exercise 1: Discounted return
Rewards with . What is ?
Answer
.
Solution
.
Exercise 2: A constant reward stream
What is the value of receiving reward 1 forever with ?
Answer
Solution
A geometric series: . The "effective horizon" of a discount is about steps.
Exercise 3: Contraction (code)
Apply the Bellman optimality update to two different value vectors for the corridor and check that their max difference shrinks by at least .
Answer
The ratio is at most 0.9.
Solution
import numpy as np
nS, g = 5, 0.9
P = np.zeros((2, nS, nS)); R = np.zeros((2, nS))
for s in range(nS - 1):
P[0, s, max(s - 1, 0)] = 1; P[1, s, s + 1] = 1; R[1, s] = float(s + 1 == nS - 1)
T = lambda V: (R + g * P @ V).max(axis=0)
rng = np.random.default_rng(0)
U, W = rng.normal(size=nS), rng.normal(size=nS)
print(round(np.max(np.abs(T(U) - T(W))) / np.max(np.abs(U - W)), 4) <= 0.9)True
Test yourself
The Bellman expectation equation expresses as…
- the maximum reward
- the expected immediate reward plus the discounted value of the next state
- the number of states
- the policy entropy
For a fixed policy and known model, can be found by…
- sampling only
- solving the linear system
- gradient ascent on the reward
- sorting states
Why does value iteration converge?
- Because rewards are positive
- The Bellman optimality operator is a -contraction
- Because the policy is random
- It does not