Dynamic Programming: Policy Evaluation, Value Iteration, and Policy Iteration
3441 words
17 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Dynamic Programming: Policy Evaluation, Value Iteration, and Policy Iteration ## 🎯 Learning Objectives - Derive and implement iterative policy evaluation for a given policy - Understand policy improvement via the Bellman optimality equation - Implement value iteration and policy iteration algorithms - Analyze the...

Dynamic Programming: Policy Evaluation, Value Iteration, and Policy Iteration
🎯 Learning Objectives
- Derive and implement iterative policy evaluation for a given policy
- Understand policy improvement via the Bellman optimality equation
- Implement value iteration and policy iteration algorithms
- Analyze the convergence properties of DP methods
- Apply GPI (Generalized Policy Iteration) to solve MDPs
📋 Prerequisites
- MDPs (Week 2): States, actions, transitions, rewards, discount factor, Bellman equations
- Basic probability: Expectation, conditional probability
- Linear algebra: Systems of equations, iterative methods
1. 📖 Core Content
1.1 Intuition: What is Dynamic Programming?
Dynamic programming (DP) is a collection of algorithms that compute optimal policies for MDPs when we have a perfect model of the environment. The "model" means we know the transition probabilities P(s′∣s,a) and reward function R(s,a,s′).
The core idea: solve subproblems and reuse solutions. For MDPs, the subproblem is the value of a state. The value of one state depends on the values of successor states — this recursive structure is made for DP.
Why does this matter? DP provides the theoretical foundation for all modern RL algorithms. Even when we don't have a perfect model (which is almost always), DP concepts like bootstrapping, policy iteration, and value iteration underpin Q-learning, DQN, and actor-critic methods.
1.2 Policy Evaluation (Prediction)
Policy evaluation computes the state-value function Vπ(s) for a given policy π.
1.2.1 The Bellman Expectation Equation for Vπ
Vπ(s)=Eπ[Rt+1+γVπ(St+1)∣St=s] =a∑π(a∣s)s′,r∑P(s′,r∣s,a)[r+γVπ(s′)]This is a system of ∣S∣ linear equations in ∣S∣ unknowns. For small MDPs, we can solve it directly (V=(I−γPπ)−1Rπ). For larger MDPs, we use iterative policy evaluation.
1.2.2 Iterative Policy Evaluation Algorithm
textInitialize V(s) = 0 for all s Δ = ∞ while Δ > θ (small threshold): Δ = 0 for each s ∈ S: v = V(s) V(s) = Σ_a π(a|s) Σ_{s',r} P(s',r|s,a) [r + γ V(s')] Δ = max(Δ, |v - V(s)|) return V ≈ V^π
Worked Example 1: Policy Evaluation on a Simple Grid
Consider a 2×2 grid world with 4 states (A, B, C, D). The agent moves in the intended direction with probability 0.8, randomly slips to a perpendicular direction with probability 0.2. Rewards: -1 per step. Goal: reach state D (terminal, reward 0). Discount γ=0.9.
Policy: Always move toward D (choose the direction that reduces Manhattan distance to D).
Step 1 (k=0): V0(s)=0 for all s.
Step 2 (k=1): Compute V1(s) for each state.
From state A: policy says move right (toward D).
- Move right with p=0.8, go to B: r=−1
- Move up with p=0.2 (slip), stay in A: r=−1 V1(A)=0.8[−1+0.9(0)]+0.2[−1+0.9(0)]=−1.0 From state B: policy says move down.
- Move down with p=0.8, go to D: r=0 (terminal)
- Move right with p=0.2, go to C: r=−1 V1(B)=0.8[0+0.9(0)]+0.2[−1+0.9(0)]=−0.2 From state C: policy says move right.
- Move right with p=0.8, go to D: r=0
- Move down with p=0.2, go to B: r=−1 V1(C)=0.8[0]+0.2[−1]=−0.2 State D is terminal: V1(D)=0. Step 3 (k=2): Use V1 to compute V2. V2(A)=0.8[−1+0.9(−0.2)]+0.2[−1+0.9(−1)] =0.8[−1.18]+0.2[−1.9]=−0.944−0.38=−1.324 V2(B)=0.8[0]+0.2[−1+0.9(−0.2)]=0.2[−1.18]=−0.236 V2(C)=0.8[0]+0.2[−1+0.9(−0.2)]=−0.236 V2(D)=0 Continue until convergence (typically 50-100 iterations for this small grid).
1.3 Policy Improvement
Once we have Vπ, we can ask: is the current policy optimal?
The policy improvement theorem says: if Qπ(s,π′(s))≥Vπ(s) for all s, then π′ is at least as good as π.
We construct a greedy policy:
This is greedy w.r.t. Vπ, meaning it picks the action that maximizes the expected return assuming the environment follows π thereafter.
Worked Example 2: Policy Improvement on the Grid
From Worked Example 1, after convergence (say after many iterations):
V(A)≈−3.5, V(B)≈−0.5, V(C)≈−0.5, V(D)=0
Now check if the current policy is optimal:
State A:
- Go right (current): 0.8[−1+0.9(−0.5)]+0.2[−1+0.9(−3.5)]=0.8(−1.45)+0.2(−4.15)=−1.99
- Go up: 0.8[−1+0.9(−3.5)]+0.2[−1+0.9(−0.5)]=0.8(−4.15)+0.2(−1.45)=−3.61
- Go down: 0.8[−1+0.9(−0.5)]+0.2[−1+0.9(−3.5)]=−1.99 (same as right due to symmetry) Going right gives the highest value. The current policy is already optimal for this simple grid.
1.4 Policy Iteration
Policy iteration alternates between evaluation and improvement until convergence:
textInitialize π(s) arbitrarily for all s loop: # Policy Evaluation V = V^π (using iterative policy evaluation) # Policy Improvement π'(s) = argmax_a Σ_{s',r} P(s',r|s,a) [r + γ V(s')] if π' == π for all s: return V, π else: π = π'
Convergence Properties
- Each iteration produces a strictly better policy (unless already optimal)
- Converges in finite number of iterations (bounded by ∣A∣∣S∣ possibilities)
- In practice, converges much faster than the theoretical bound
Worked Example 3: Policy Iteration for a Small MDP
Consider a 3-state MDP (states 1, 2, 3) with actions: move left (L) or move right (R).
Transitions:
- From state 1: R → state 2 (p=1), reward +1. L stays in 1 (p=1), reward 0.
- From state 2: R → state 3 (p=1), reward +1. L → state 1 (p=1), reward 0.
- From state 3: L → state 2 (p=1), reward 0. R stays in 3 (p=1), reward +5. γ=0.95. Initial policy: π0(1)=R, π0(2)=R, π0(3)=R. Iteration 1 - Evaluation: Solve V=Rπ+γPπV: State 3: π(3)=R → stays in 3: V3=5+0.95⋅V3 → V3=100 State 2: π(2)=R → goes to 3: V2=1+0.95⋅V3=1+95=96 State 1: π(1)=R → goes to 2: V1=1+0.95⋅V2=1+91.2=92.2 Iteration 1 - Improvement: State 1: Q(1,L)=0+0.95(92.2)=87.6, Q(1,R)=1+0.95(96)=92.2 → Keep R State 2: Q(2,L)=0+0.95(92.2)=87.6, Q(2,R)=1+0.95(100)=96 → Keep R State 3: Q(3,L)=0+0.95(96)=91.2, Q(3,R)=5+0.95(100)=100 → Keep R Policy unchanged. Converged to optimal policy: always move right.
1.5 Value Iteration
Value iteration combines policy evaluation and improvement into a single step:
This is the Bellman optimality equation turned into an update rule.
textInitialize V(s) = 0 for all s Δ = ∞ while Δ > θ: Δ = 0 for each s ∈ S: v = V(s) V(s) = max_a Σ_{s',r} P(s',r|s,a) [r + γ V(s')] Δ = max(Δ, |v - V(s)|) # Extract optimal policy π(s) = argmax_a Σ_{s',r} P(s',r|s,a) [r + γ V(s')] return V, π
Key Differences from Policy Iteration
| Aspect | Policy Iteration | Value Iteration |
|---|---|---|
| Update | Full evaluation + improvement | Directly computes optimal V |
| Convergence | Finite steps (polynomial in practice) | Infinite (asymptotic) |
| Per-iteration cost | $O( | S |
| Stopping criterion | Policy unchanged | Value change < threshold |
| Typical iterations | 5-20 | 50-500+ |
In practice, policy iteration converges in fewer iterations but each iteration is more expensive (requires full policy evaluation). Value iteration is simpler per iteration but needs more iterations.
1.6 Generalized Policy Iteration (GPI)
Most RL algorithms can be understood through the lens of Generalized Policy Iteration — the interaction between policy evaluation and policy improvement.
(Diagram)
In GPI:
- Evaluation pushes the value function toward matching the current policy
- Improvement pushes the policy toward being greedy with respect to the current value function The two processes interact:
- If we stop evaluation early (truncated), the improvement step is based on an approximate value function
- If we only partially improve, the evaluation starts from a near-optimal policy All modern RL algorithms (DQN, SARSA, Actor-Critic) are instances of GPI with different choices of:
- How thoroughly to evaluate (one step, full convergence, n steps)
- How to represent the value function (tabular, neural network)
- How to improve (greedy, ε-greedy, gradient-based)
1.7 Edge Cases & Gotchas
- Asynchronous DP: Instead of sweeping through all states, update states in any order (prioritize states with large Bellman error). Can converge faster.
- In-place vs out-of-place: In-place updates (V is updated immediately) converge faster than out-of-place (using Vk to compute Vk+1).
- Terminal states: Must be handled carefully — their value is always 0, and they have no outgoing transitions.
- Discount factor near 1: Slower convergence because future rewards matter more, making the value propagation slower.
- Deterministic vs stochastic: Stochastic environments require more iterations because the value is an expectation over outcomes.
1.8 Why This Matters
DP is the theoretical bedrock of RL. Every subsequent algorithm relaxes one of DP's assumptions:
| Algorithm | What's Relaxed |
|---|---|
| Monte Carlo | Unknown environment (no model needed) |
| TD Learning | Unknown environment + online updates |
| DQN | Tabular → function approximation |
| Policy Gradients | Direct policy optimization, no value function needed |
Understanding DP makes understanding these more complex algorithms much easier: they're all GPI with different engineering choices.
2. 📐 Key Formulas / Concepts
| Concept | Formula | Description |
|---|---|---|
| Policy evaluation | $V_{k+1}(s) = \sum_a \pi(a | s) \sum_{s',r} P(s',r |
| Policy improvement | $\pi'(s) = \arg\max_a \sum_{s',r} P(s',r | s,a)[r + \gamma V^\pi(s')]$ |
| Value iteration | $V_{k+1}(s) = \max_a \sum_{s',r} P(s',r | s,a)[r + \gamma V_k(s')]$ |
| Bellman optimality | $V^*(s) = \max_a \sum_{s',r} P(s',r | s,a)[r + \gamma V^*(s')]$ |
| GPI | Alternating evaluation ↔ improvement | Framework for understanding all RL algorithms |
3. ⚠️ Common Pitfalls
Pitfall 1: Using Value Iteration When Policy Iteration Would Be Faster
Mistake: Always using value iteration because it's simpler.
Why: Value iteration requires many iterations for the values to propagate from the goal across the entire state space. Each iteration updates all states once.
Correct approach: Use policy iteration for small-to-medium MDPs where full policy evaluation is feasible. Value iteration shines when the state space is very large and we can't afford many iterations of policy evaluation.
Pitfall 2: Incorrect Terminal State Handling
Mistake: Forgetting to handle terminal states in value updates.
Why: If a state has no outgoing transitions, the Bellman equation breaks (there's no V(s′) to bootstrap from).
Correct approach: Set terminal state values to 0 and exclude them from update sweeps. In the Bellman equation, treat transitions to terminal states as having V(s′)=0.
Pitfall 3: Setting the Convergence Threshold Too Strict
Mistake: Using θ=10−10 and waiting forever for convergence.
Why: The value function changes by small amounts for many iterations near convergence. An extremely tight threshold dramatically increases iteration count with negligible improvement in policy quality.
Correct approach: θ=10−3 to 10−6 is typically sufficient. The optimal policy often converges long before the optimal value function.
4. 📝 Practice Questions
Q1: For a 3×3 grid with terminal state at (3,3), discount γ=0.9, reward -1 per step, random slip probability 0.2. What's the optimal path from (1,1) to (3,3)?The optimal path is the shortest path: right-right-down-down or down-down-right-right. Due to the slip probability, the agent may deviate, but the optimal policy points toward (3,3) greedily. The shortest path length is 4 steps. The optimal value at (1,1) would be approximately:V(1,1)≈−1−0.9(1)−0.92(1)−0.93(0)=−2.71 (ignoring slip)With slip probability, the actual value is slightly worse (more negative) because the agent may take longer paths. Q2: Prove that policy iteration converges in a finite number of iterations for finite MDPs.Proof sketch:
- There are finitely many policies (∣A∣∣S∣ possible deterministic policies)
- Each policy iteration produces a strictly better policy (policy improvement theorem)
- Since policies are strictly ordered by their value functions and there are finitely many, the algorithm must terminate at the optimal policy
The strict improvement follows from the policy improvement theorem: if π′ is greedy w.r.t. Vπ, then Vπ′(s)≥Vπ(s) for all s, with strict inequality for at least one state if π is not optimal. Q3: Modify value iteration to detect when the policy has stabilized (not just the values).Add a policy extraction step after each complete sweep:textloop: # Value iteration update for each s: V_new(s) = max_a Σ P(s'|s,a)[R + γ V(s')] # Policy extraction for each s: π_new(s) = argmax_a Σ P(s'|s,a)[R + γ V_new(s')] if π_new == π for all s: return V_new, π_new V = V_new π = π_newThis stops once the policy stabilizes, which typically happens long before the values fully converge. It's more efficient than waiting for Δ<θ on values. Q4: For an MDP with |S|=1000, |A|=5, γ=0.99, compare the computational cost of one iteration of policy evaluation vs. one iteration of value iteration.Policy evaluation iteration: For each of 1000 states: sum over 5 actions, each summing over transition probabilities (could be up to 1000 next states). Cost: O(∣S∣2∣A∣)=O(10002×5)=O(5,000,000)Value iteration: For each of 1000 states: take max over 5 actions, each summing over up to 1000 next states. Cost: O(∣S∣2∣A∣)=O(5,000,000)Same per-iteration cost! The difference is:
- Value iteration: typically needs ~500 iterations to converge (γ=0.99 means slow propagation) → 2.5B operations
- Policy iteration: typically ~20 iterations, each needing ~50 evaluation steps → 20 × 50 × 5M = 5B operations
So they're roughly comparable in total, but policy iteration often wins for large, highly discounted MDPs. Q5: In a deterministic MDP, how does the speed of value iteration change with the discount factor γ?Value iteration updates propagate information one step at a time. For a reward at the goal:
- After 1 iteration: states one step from goal know the goal value
- After k iterations: states k steps from goal know the goal value
The number of iterations needed scales as O(D/(1−γ)) where D is the diameter of the MDP.For γ=0.9: effective horizon = 1/(1−0.9)=10 steps. Need ~10-20 iterations. For γ=0.99: effective horizon = 1/(1−0.99)=100 steps. Need ~100-200 iterations. For γ=0.999: effective horizon = 1000 steps. Need ~1000 iterations.The convergence speed is roughly linear in 1−γ1. Q6: Explain why value iteration can use the same update equation as policy evaluation but with a max over actions.Policy evaluation computes Vπ for a fixed policy: V(s)←∑aπ(a∣s)⋅Q(s,a).Value iteration computes V∗ directly: V(s)←maxaQ(s,a).The max operation implicitly performs policy improvement. After each value update, the implicit policy becomes greedy with respect to the current value. This is why value iteration combines evaluation and improvement into one step — the max over actions IS the improvement step.If you think of GPI as a cycle, value iteration moves both toward the optimal value and the optimal policy simultaneously in each update. Q7: Give an example where value iteration appears to converge (small Δ) but the policy is still suboptimal.Consider an MDP with two actions (A and B) that give nearly identical values. At state s:
- Action A leads to reward +99 then terminal
- Action B leads to reward +100 then terminal
After few iterations:
- V(s) from A: 99
- V(s) from B: 100 max = 100, choose B. Correct.
But consider a chain MDP where states s1 → s2 → s3 → s4 (terminal, reward +1). γ = 0.9.After 1 iteration: V(s3) = 1, V(s2) = 0, V(s1) = 0 After 2 iterations: V(s3) = 1, V(s2) = 0.9, V(s1) = 0 After 3 iterations: V(s3) = 1, V(s2) = 0.9, V(s1) = 0.81After each iteration, Δ decreases. But at iteration 2, the value of s1 is still 0 (suboptimal!). It takes 3 iterations for the reward to propagate to s1. Before that, the policy at s1 appears optimal with respect to the (poor) value estimate.The error is that Δ measures the maximum change in any state's value, but it doesn't measure how far the value is from optimal. A state far from the reward source might have a slowly changing value that's still far from convergence. Q8: Design a DP algorithm that balances between policy iteration and value iteration — perform a limited number of evaluation sweeps before improving.This is modified policy iteration (or truncated policy iteration):textInitialize V arbitrarily, π arbitrarily loop: # Truncated policy evaluation (k sweeps) for i = 1 to k: Δ = 0 for each s ∈ S: v = V(s) V(s) = Σ_a π(a|s) Σ P(s'|s,a)[R + γ V(s')] Δ = max(Δ, |v - V(s)|) if Δ < θ_eval: break # Policy improvement stable = True for each s ∈ S: old_action = π(s) π(s) = argmax_a Σ P(s'|s,a)[R + γ V(s')] if π(s) != old_action: stable = False if stable: return V, πWith k = 1, this is essentially value iteration (one evaluation sweep before improvement). With k = large or convergence, it's policy iteration. The parameter k controls the tradeoff: more evaluation per improvement (closer to PI) vs. faster initial progress (closer to VI).Typical choice: k = 5-10 evaluation sweeps per improvement step. This often gives faster overall convergence than either extreme. Q9: In a game of chess, why can't we apply DP directly? What assumptions would be violated?DP requires:
- Known transition probabilities: We know how pieces move, but we don't know the opponent's move probabilities. The opponent is part of the environment, and their strategy is unknown.
- Known reward function: We know checkmate = +1, but intermediate rewards (material advantage, position evaluation) are subjective.
- Full state enumeration: Chess has ~1047 states. We can't iterate over all of them.
- Markov property: The board state is Markovian (fully observed), so this assumption holds.
What we'd need:
- An approximate value function (neural network evaluation)
- Sample-based updates (self-play instead of exact expected values)
- Function approximation to generalize across similar positions
This is exactly what AlphaZero does: it uses a neural network to approximate Vπ(s) and Qπ(s,a), trained via self-play (sampling), bypassing the need for a known transition model. Q10: Suppose you have an MDP with |S| = 10^6 states and |A| = 10 actions. Can you use DP? If not, what alternatives exist?Can we use DP? No. Classical DP requires iterating over all states, which costs O(∣S∣2∣A∣) per iteration. With 10^6 states, each iteration costs 1013 operations — infeasible.Alternatives:
- Approximate DP: Use function approximation (neural network) to represent V(s) instead of a table. Update only states visited during sampling.
- Monte Carlo methods: Learn from complete episodes. No need to iterate over all states.
- Temporal Difference learning: Learn online from partial episodes. Bootstraps from current value estimates.
- Hierarchical RL: Decompose the MDP into subtasks, solve each with DP where feasible.
- Sample-based planning: Use a learned model to simulate trajectories and update values only along those trajectories (Dyna-style).
The key insight: when |S| is large, we trade exactness for scalability by using sampling and function approximation.
5. 🔗 Cross-References
- Previous: MDPs (Week 2) — Bellman equations foundation
- Next: Monte Carlo Methods (Week 4) — Sampling-based value estimation
- Related: TD Learning (Week 5) — Bootstrapping without a model
- External: Sutton & Barto, "Reinforcement Learning: An Introduction" — Chapters 4-5 on DP and MC Join Discord PreviousMDPs & Bellman EquationsNextMonte Carlo Methods