Dynamic programming (value - policy iteration)
Overview
Dynamic programming (DP) methods solve Markov Decision Processes (MDPs) by breaking them into subproblems and caching solutions. They compute optimal value functions and optimal policies through iterative updates that leverage the Bellman optimality equations.
Why DP matters: DP is theoretical foundation for modernRL algorithms. Even though pure DP requires a perfect model (transition probabilities and rewards), understanding it unlocks model-free methods like Q-learning and actor-critic, which approximate DP without a model.

Core Concepts
The Two Bellman Equations
Bellman Expectation Equation (for a given policy)
Derivation from first principles:
Start with the definition of value:
where is the return.
Separate the first reward from the rest:
The term in parentheses is just , the return from the next state:
By the definition of value function, :
Expand the expectation over actions (from policy ) and next states (from dynamics ):
WHY each component:
- : Policy probability of taking action in state
- : Environment dynamics (transition probability)
- : Immediate reward we actually receive
- : Discounted value of where we end up
Bellman Optimality Equation
Derivation:
The optimal value is the maximum over all possible policies:
For the optimal policy , it must choose the action that maximizes expected return:
where
Expand the expectation:
WHY the max operator: The optimal policy always picks the best action, so we maximize over actions rather than averaging over a policy distribution.
Algorithm1: Policy Iteration
Policy iteration alternates between two steps: policy evaluation (compute for current policy) and policy improvement (make policy greedy).
Step 1: Policy Evaluation
Goal: Compute for all states under current policy .
Algorithm:
- Initialize arbitrarily (e.g., 0 for all )
- Repeat until convergence (change < ):
- For each state :
WHY this works: Each iteration applies the Bellman expectation equation. Since it's a contraction mapping (due to ), repeated applications converge to the unique fixed point .
WHY this step: "The update shrinks the distance to the true value function by a factor of with each sweep, so it must converge."
Step 2: Policy Improvement
Goal: Make policy better by acting greedily w.r.t. current value function.
Algorithm: For each state :
WHY this improves the policy (Policy Improvement Theorem):
For the gredy action :
If we follow then continue with , we get at least as much value:
Since this holds for all states, is at least as good as . If is not strictly better, then is already optimal.
Full Policy Iteration Loop
1. Initialize π arbitrarily
2. Repeat:
a. V ← PolicyEvaluate(π) // converge to V^π
b. π' ← Gredy(V)
c. If π' = π, stop (optimal!)
d. π ← π'
Convergence: Policy iteration converges in a finite number of steps (at most policies, but usually much faster). Each iteration strictly improves the policy until optimality.
Algorithm 2: Value Iteration
Value iteration combines policy evaluation and improvement into a single update step. Instead of fully evaluating a policy, we do one backup per state and immediately use it for the next iteration.
Algorithm:
- Initialize arbitrarily (e.g., 0 for all )
- Repeat until convergence (change < ):
- For each state :
- Extract optimal policy:
WHY this works: Each update applies the Bellman optimality equation. We're directly computing without maintaining an explicit policy during iteration. The max operator implicitly improves the policy every step.
Key insight: Value iteration is policy iteration with truncated policy evaluation (just one sweep instead of converging fully). This is often more efficient because the policy might improve before fully converges.
Policy vs Value Iteration: The Tradeoff
| Aspect | Policy Iteration | Value Iteration | |--------|------------------| | Update | Full policy evaluation + improvement | Single optimality backup | | Iterations | Fewer policy changes (3-5 typically) | More backups (10-100s) but cheaper | | Per-iteration cost | High (many sweps to converge ) | Low (one sweep) | | Total cost | Often similar or better for small | Often better for large | | Intermediate policy | Always have a well-defined policy | No explicit policy until end |
When to use which:
- Policy iteration: When evaluating a policy is fast (small state space, high ), or when you want intermediate policies during computation.
- Value iteration: When policy evaluation is expensive, or when you only care about the final optimal policy.
- Asynchronous DP: Update states in any order (focus on high-error states), converges faster in practice.
Computational Complexity
Per iteration:
- Policy evaluation: per sweep (for each state, sum over actions and next states)
- Value iteration backup: per sweep
Total iterations:
- Policy iteration: worst case (every policy), but typically3-10 in practice
- Value iteration: to reach error threshold
WHY affects convergence: The contraction factor is . Each iteration reduces error by at least factor , so smaller (less far-sighted) converges faster.
Space complexity: to store and .
Connections to Other RL Methods
Relationship to temporal-difference learning:
- DP: Model-based (needs ), sweps all states
- TD learning: Model-free (learns from samples), updates one state at a time
- TD is essentially DP with sampled backups instead of full expectation
Generalized Policy Iteration (GPI): All RL methods can be viewed as GPI: interleaving policy evaluation (making value function consistent with policy) and policy improvement (making policy greedy w.r.t. value). DP does this with exact computation; TD/Q-learning approximate it with samples.
Monte Carlo methods:
- DP: Bootstrap (use estimated in updates)
- Monte Carlo: No bootstrap (wait for actual return )
- Tradeoff: DP has lower variance but needs a model
Modern deep RL:
- AlphaZero: Policy iteration with neural networks for and , Monte Carlo tree search for evaluation
- Actor-critic: Policy gradient (improvement) + TD learning (evaluation)
Connections
- 5.108-Markov-Decision-Process-(MDP) —DP solves MDPs
- 5.1.10-Temporal-difference-learning — Model-free version of DP
- 5.1.11-Q-learning — Value iteration without a model
- 5.1.12-SARSA — Policy iteration without a model
- 5.1.15-Policy-gradient-methods — Direct policy search (no value function)
- Bellman-equations — Foundation of DP
- Contraction-mapping-theorem — Why DP converges
Recall Explain to a 12-year-old
Imagine you're playing a board game where you want to reach the finish as fast as possible. You don't know the best strategy yet, so you try this: Plan 1 (Policy Iteration):
- Start with a random strategy: "I'll just roll the dice and move however I feel."
- Now pretend you follow that strategy for many games and calculate "How many turns does it take on average from each square?"
- Look at your strategy again: "Wait, if I'm on square 10, my strategy says go left, but if I go right instead, I reach the finish faster!"
- Update your strategy to always pick the move that gets to lower-turn squares.
- Repeat steps 2-4 until your strategy stops changing.
Plan 2 (Value Iteration):
- Start by guessing "0 turns from every square."
- Update each square: "If I'm here, what's the fastest I could finish?" Look at all possible moves and pick the best.
- Repeat step 2 until the numbers stop changing.
- Your final strategy: always move to the square with the lowest number.
Both plans find the perfect strategy! The first one is like trying a strategy, testing it, then fixing it. The second is like calculating the best possible score for each square and following that.
Flashcards
What are the two main DP algorithms for solving MDPs? :: Policy iteration and value iteration.
What is the key difference between policy evaluation and value iteration updates?
What is the Bellman expectation equation?
What is the Bellman optimality equation?
Why does policy improvement theorem guarantee that gredy policy is better?
What is the contraction factor inDP convergence?
What is the stopping criterion for value iteration given tolerance ?
What is generalized policy iteration (GPI)?
Why does value iteration often converge faster than policy iteration per wall-clock time?
What is the key requirement that makes DP infeasible for large real-world RL problems?
When should you prefer policy iteration over value iteration?
What is in-place/asynchronous DP and why use it?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Chalo is topic ko simple tarike se samajhte hain. Dynamic Programming ka core idea yeh hai ki bade problem ko chote-chote subproblems mein tod do, aur unke solutions ko cache (save) kar lo taaki baar-baar calculate na karna pade. Socho ki tum ek maze mein shortest path dhundh rahe ho — instead of har route try karna, tum ulta soch ke chalte ho: "Agar main exit se ek step door hoon, to best move kya hai?" Phir "Do step door hoon to?" Is tarah tum chote answers build karte ho aur unhe reuse karte ho bade problems ke liye. Yahi kaam DP karta hai MDPs (Markov Decision Processes) ke saath, aur iska dil hai Bellman equation — jo kehti hai ki kisi state ka value equals immediate reward plus aage jahan pahunchoge uska discounted value.
Ab yahan do important equations aati hain. Pehli hai Bellman Expectation Equation, jo ek given policy ke liye value nikalti hai — yaani agar tum ek fixed strategy follow kar rahe ho to har state ka expected return kya hoga. Dusri hai Bellman Optimality Equation, jismein max operator lagta hai — yeh best possible action choose karti hai har state par, na ki average leti hai. Yeh max hi difference hai: expectation equation average nikalti hai policy ke according, jabki optimality equation hamesha best action pick karti hai. In dono ke saath humein V (state value), Q (action value), aur optimal policy π* milti hai, jo har state mein sabse acchi action batati hai.
Yeh cheez isliye important hai kyunki DP modern Reinforcement Learning ka theoretical foundation hai. Sach hai ki pure DP ko ek perfect model chahiye (transition probabilities aur rewards ka poora pata hona chahiye), jo real world mein aksar nahi milta. Lekin agar tum DP ache se samajh loge, to tumhe Q-learning aur actor-critic jaise model-free methods bhi easily samajh aayenge — yeh saare methods basically DP ko approximate karte hain bina model ke. To yeh topic ek building block hai jispe aage ki puri RL ki knowledge tiki hui hai, isliye ise strong karna zaroori hai.