Part I · Foundations · Chapter 4

Bellman equations and dynamic programming

Solving an MDP when you know the rules: policy evaluation, policy iteration, value iteration.

~100 minBefore this: MDPs

Suppose someone hands you the complete rulebook of a world: every transition probability P(s′∣s,a)P(s' \mid s, a) and every expected reward r(s,a)r(s,a). You want the best possible policy. In Chapter 3 you learned to evaluate any policy you are given: write down the Bellman expectation equation and solve a linear system. So why not evaluate every policy and keep the best one?

Count them. The gridworld in this chapter’s demo has 22 cells where the agent must choose among 4 actions. That makes 422≈1.8×10134^{22} \approx 1.8 \times 10^{13} deterministic policies. At a million evaluations per second, you would finish in about seven months. Yet the algorithms in this chapter solve that gridworld exactly in about two dozen cheap passes over the grid, which takes a fraction of a millisecond. The trick is an idea Richard Bellman named in the 1950s: dynamic programming (DP). (“Programming” here means planning, as in “linear programming”, not writing code.) It is the mathematical core of everything that follows in this course. Q-learning, actor-critic, PPO and even the critic in RLHF are all approximate, sampled versions of what you will do here exactly.

What you bring from Chapter 3

Here is a quick recap, in the notation of the notation page. A policy π(a∣s)\pi(a \mid s) has a state-value function Vπ(s)V^\pi(s) (expected return from ss if you follow π\pi) and an action-value function Qπ(s,a)Q^\pi(s,a) (expected return if you take aa first and then follow π\pi). They are related by

Qπ(s,a)=r(s,a)+γ∑s′P(s′∣s,a) Vπ(s′),Vπ(s)=∑aπ(a∣s) Qπ(s,a).Q^\pi(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\pi(s'), \qquad V^\pi(s) = \sum_a \pi(a \mid s)\, Q^\pi(s,a).

Substituting one into the other gives the Bellman expectation equation:

Vπ(s)=∑aπ(a∣s)[r(s,a)+γ∑s′P(s′∣s,a) Vπ(s′)].V^\pi(s) = \sum_a \pi(a \mid s) \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\pi(s') \Big].

It is linear in VπV^\pi. In matrix form it reads Vπ=rπ+γPπVπV^\pi = r_\pi + \gamma P_\pi V^\pi, so Vπ=(I−γPπ)−1rπV^\pi = (I - \gamma P_\pi)^{-1} r_\pi. That is how Chapter 3’s demo recomputed values live.

The first formula deserves a name of its own, because we will use it constantly. Given any vector of state values VV, the quantity r(s,a)+γ∑s′P(s′∣s,a)V(s′)r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V(s') is a one-step lookahead. It means: take action aa, collect the expected reward, then pretend that VV correctly scores wherever you land. Every algorithm in this chapter is built from this one computation.

A corridor to think with

Here is the small MDP we will compute by hand all chapter:

   +0.5  ◄──  [ A ]  ◄──►  [ B ]  ◄──►  [ C ]  ──►  +1
  small exit                                     big exit

There are three states, A, B, C, and two actions, left and right. Moves are deterministic. Walking left out of A ends the episode with reward +0.5+0.5. Walking right out of C ends it with +1+1. Every other move pays 0. (In the lab, the “exit” is an extra absorbing state with value 0.)

From A, should you grab the small prize now or walk two steps for the big one? Walking right earns +1+1 on the third move, worth γ2⋅1\gamma^2 \cdot 1 today. So it depends on the discount: for γ=0.9\gamma = 0.9, γ2=0.81>0.5\gamma^2 = 0.81 > 0.5 and you should walk; for γ=0.5\gamma = 0.5, γ2=0.25<0.5\gamma^2 = 0.25 < 0.5 and you should take the small exit. The crossover is at γ=1/2≈0.71\gamma = 1/\sqrt{2} \approx 0.71. This tiny example already shows the central fact: the best action in A depends on what you will do afterwards in B and C. Dynamic programming organizes that dependency.

The Bellman optimality equation

The principle of optimality

Think about driving routes. If the fastest route from Boston to Los Angeles passes through Chicago, then its Chicago-to-LA part must be the fastest route from Chicago to LA. If it weren’t, you could swap in the faster one and beat the “fastest” route. Bellman called this the principle of optimality: an optimal plan, from any point along it onward, is optimal for the problem that starts at that point.

So, to act optimally in ss you don’t need to search over whole futures. You need two things:

  1. the best value you can get from each possible next state, and
  2. a one-step choice of the action that leads to the best mix of immediate reward plus that future value.

Writing it down

Chapter 3 defined the optimal state-value function and optimal action-value function as the best achievable over all policies, and stated the equation they satisfy. Here we derive that equation from the principle of optimality, and then learn to solve it. The definitions are:

V∗(s)=max⁡πVπ(s),Q∗(s,a)=max⁡πQπ(s,a).V^*(s) = \max_\pi V^\pi(s), \qquad Q^*(s,a) = \max_\pi Q^\pi(s,a).

The principle of optimality turns into equations. If you take aa in ss and then behave optimally, you get

Q∗(s,a)=r(s,a)+γ∑s′P(s′∣s,a) V∗(s′),Q^*(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^*(s'),

and behaving optimally in ss means picking the best aa: V∗(s)=max⁡aQ∗(s,a)V^*(s) = \max_a Q^*(s,a). Combine the two:

V∗(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a) V∗(s′)],V^*(s) = \max_a \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^*(s') \Big], Q∗(s,a)=r(s,a)+γ∑s′P(s′∣s,a) max⁡a′Q∗(s′,a′).Q^*(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, \max_{a'} Q^*(s',a').

These are the Bellman optimality equations for V∗V^* and Q∗Q^*. Compare them with the expectation equation above. The only change is that the average over actions, ∑aπ(a∣s)[⋯ ]\sum_a \pi(a \mid s)[\cdots], became a maximum, max⁡a[⋯ ]\max_a [\cdots]. That small change has a big consequence. The max makes the equation nonlinear, so there is no single matrix inversion that solves it. We need an iterative method, and the rest of the chapter builds two of them.

Solving the corridor by hand

For a small problem you can guess and check. Take γ=0.9\gamma = 0.9 and guess “always go right”. Then V(C)=1V(C) = 1, V(B)=0.9⋅1=0.9V(B) = 0.9 \cdot 1 = 0.9, V(A)=0.9⋅0.9=0.81V(A) = 0.9 \cdot 0.9 = 0.81. Now check each state against the optimality equation:

Stateleft: r+γV(next)r + \gamma V(\text{next})right: r+γV(next)r + \gamma V(\text{next})maxequals guess?
A0.50.50+0.9⋅0.9=0.810 + 0.9 \cdot 0.9 = 0.810.810.81yes
B0+0.9⋅0.81=0.7290 + 0.9 \cdot 0.81 = 0.7290+0.9⋅1=0.90 + 0.9 \cdot 1 = 0.90.90.9yes
C0+0.9⋅0.9=0.810 + 0.9 \cdot 0.9 = 0.811111yes

The guess satisfies the equation in every state. Later in the chapter we prove the equation has exactly one solution, so this is V∗V^*. The table’s left and right columns are exactly Q∗(s,left)Q^*(s, \text{left}) and Q∗(s,right)Q^*(s, \text{right}).

Now repeat with γ=0.5\gamma = 0.5. “Always right” gives V=(0.25,0.5,1)V = (0.25, 0.5, 1). In A the check fails: max⁡(0.5, 0.5⋅0.5)=0.5≠0.25\max(0.5,\ 0.5 \cdot 0.5) = 0.5 \neq 0.25. The equation itself tells you to switch A to left. With that switch, V=(0.5,0.5,1)V = (0.5, 0.5, 1), and every state checks out.

Acting greedily on V∗V^* is optimal

Once you have V∗V^*, the optimal policy takes one line. In each state, pick the action with the best one-step lookahead:

π∗(s)=arg⁡max⁡a[r(s,a)+γ∑s′P(s′∣s,a) V∗(s′)]=arg⁡max⁡aQ∗(s,a).\pi^*(s) = \arg\max_a \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^*(s') \Big] = \arg\max_a Q^*(s,a).

A policy chosen this way is called greedy with respect to V∗V^*. “Greedy” usually sounds short-sighted, so why is it optimal here? Because V∗V^* has already folded the whole future into a single number per state. A one-step lookahead on top of the right value function is a full lookahead. That is the real magic of value functions: they turn long-horizon planning into a one-step decision. (The proof is below, in the contraction section, once we have the tools.)

Three facts follow, and all of them matter later in the course:

  • One policy is best everywhere at once. The greedy policy achieves V∗(s)V^*(s) in every state simultaneously. You never have to trade off “good from A” against “good from B”.
  • That policy can be deterministic. Randomizing never helps when you know the model. (Stochastic policies matter for exploration and for learning, as in Chapter 7, but not for optimality here.)
  • Q∗Q^* lets you act without the model. Acting greedily on V∗V^* needs PP and rr to do the lookahead. Acting greedily on Q∗Q^* needs only an argmax over a table. That is why the model-free methods of Chapter 5 learn QQ rather than VV.

Quick check In the corridor with γ = 0.8, which exit should the agent standing in A head for?

Policy evaluation by sweeping

We’ll build up to solving the optimality equation in two steps. The first is a better way to do Chapter 3’s job: computing VπV^\pi for a fixed policy.

A linear solve costs about ∣S∣3\lvert\mathcal S\rvert^3 operations. That is fine for 22 states and hopeless for a million. There is a cheaper way. Take the Bellman expectation equation, which VπV^\pi satisfies exactly, and use it as an update rule on a guess:

Vk+1(s)=∑aπ(a∣s)[r(s,a)+γ∑s′P(s′∣s,a) Vk(s′)]for every s.V_{k+1}(s) = \sum_a \pi(a \mid s) \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V_k(s') \Big] \quad \text{for every } s.

One pass of this update over all states is a sweep, and each single-state update is called a backup (information is “backed up” from successor states to their predecessor). Start from V0=0V_0 = 0 and sweep until nothing changes. This is iterative policy evaluation. You met it briefly at the end of Chapter 3. Here we look at how it behaves, and later in the chapter we prove that it converges.

Worked example: “always left” in the corridor

Take γ=0.9\gamma = 0.9 and the policy that always goes left. Each state’s update reads one neighbor:

Vk+1(A)=0.5,Vk+1(B)=0.9 Vk(A),Vk+1(C)=0.9 Vk(B).V_{k+1}(A) = 0.5, \qquad V_{k+1}(B) = 0.9\, V_k(A), \qquad V_{k+1}(C) = 0.9\, V_k(B).
sweep kkVk(A)V_k(A)Vk(B)V_k(B)Vk(C)V_k(C)
0000
10.500
20.50.450
30.50.450.405
40.50.450.405

Watch how the information moves. The reward at the left exit reaches A in sweep 1, B in sweep 2, and C in sweep 3. Each sweep pushes news one step further back. In a deterministic chain the values become exact after (chain length) sweeps. When transitions are random, every state feeds back into every other through many paths, so the values only converge in the limit, geometrically fast. You’ll see why in the contraction section.

Iterative policy evaluation
input: P, r, policy π, discount γ, tolerance tol
1. V ← 0  (any start works)
2. repeat:
3.     for every state s:
4.         V_new(s) ← Σ_a π(a|s) [ r(s,a) + γ Σ_s' P(s'|s,a) V(s') ]
5.     Δ ← max_s |V_new(s) − V(s)|
6.     V ← V_new
7. until Δ < tol
8. return V

Quick check A deterministic chain of states with a single reward at one end, like the corridor's exits. From the farthest state it takes 5 moves to collect that reward. You run synchronous policy-evaluation sweeps starting from V = 0. After which sweep does the farthest state first get a nonzero value?

Policy improvement

Now we have VπV^\pi. Can we use it to find a better policy?

Here is the idea. Stand in state ss and consider deviating from π\pi for a single step: take action aa now, then go back to following π\pi. The value of that one-step deviation is exactly Qπ(s,a)=r(s,a)+γ∑s′P(s′∣s,a)Vπ(s′)Q^\pi(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V^\pi(s'). Suppose some aa has Qπ(s,a)>Vπ(s)Q^\pi(s,a) > V^\pi(s). Then deviating once is better than not deviating. It would be odd if deviating every time you visit ss (and doing the same in other states) were worse. It isn’t, and that is the theorem.

∂ Proof of the policy improvement theorem

Let π′\pi' be deterministic (the stochastic case is the same with an extra average over actions). Write Eπ′[ ⋅∣st=s]\mathbb{E}_{\pi'}[\,\cdot \mid s_t = s] for the expectation over trajectories that start in ss and follow π′\pi'. Start from the assumption and expand QπQ^\pi one step:

Vπ(s)≤Qπ(s,π′(s))=Eπ′[rt+γVπ(st+1)∣st=s].V^\pi(s) \le Q^\pi(s, \pi'(s)) = \mathbb{E}_{\pi'}\big[ r_t + \gamma V^\pi(s_{t+1}) \mid s_t = s \big].

Apply the assumption again, this time at st+1s_{t+1}: Vπ(st+1)≤Qπ(st+1,π′(st+1))V^\pi(s_{t+1}) \le Q^\pi(s_{t+1}, \pi'(s_{t+1})). Then expand that QπQ^\pi one step:

Vπ(s)≤Eπ′[rt+γrt+1+γ2Vπ(st+2)∣st=s].V^\pi(s) \le \mathbb{E}_{\pi'}\big[ r_t + \gamma r_{t+1} + \gamma^2 V^\pi(s_{t+2}) \mid s_t = s \big].

Repeating nn times:

Vπ(s)≤Eπ′[∑j=0n−1γjrt+j+γnVπ(st+n) ∣ st=s].V^\pi(s) \le \mathbb{E}_{\pi'}\Big[ \sum_{j=0}^{n-1} \gamma^j r_{t+j} + \gamma^n V^\pi(s_{t+n}) \,\Big|\, s_t = s \Big].

Values are bounded (rewards are bounded and γ<1\gamma < 1), so the last term is at most γnmax⁡s′′∣Vπ(s′′)∣→0\gamma^n \max_{s''} \lvert V^\pi(s'') \rvert \to 0. The sum converges to the return of π′\pi'. Letting n→∞n \to \infty gives Vπ(s)≤Vπ′(s)V^\pi(s) \le V^{\pi'}(s).

For strictness, suppose Qπ(s,π′(s))>Vπ(s)Q^\pi(s, \pi'(s)) > V^\pi(s) at some ss. Since we now know Vπ′≥VπV^{\pi'} \ge V^\pi everywhere,

Vπ′(s)=r(s,π′(s))+γ∑s′P(s′∣s,π′(s))Vπ′(s′)≥r(s,π′(s))+γ∑s′P(s′∣s,π′(s))Vπ(s′)=Qπ(s,π′(s))>Vπ(s).V^{\pi'}(s) = r(s,\pi'(s)) + \gamma \sum_{s'} P(s' \mid s, \pi'(s)) V^{\pi'}(s') \ge r(s,\pi'(s)) + \gamma \sum_{s'} P(s' \mid s, \pi'(s)) V^{\pi}(s') = Q^\pi(s,\pi'(s)) > V^\pi(s).

The natural choice of π′\pi' is to be greedy everywhere at once:

π′(s)=arg⁡max⁡aQπ(s,a).\pi'(s) = \arg\max_a Q^\pi(s,a).

This satisfies the theorem’s condition automatically, because max⁡aQπ(s,a)≥∑aπ(a∣s)Qπ(s,a)=Vπ(s)\max_a Q^\pi(s,a) \ge \sum_a \pi(a \mid s) Q^\pi(s,a) = V^\pi(s). So greedy improvement never makes things worse.

What if it doesn’t make things better either? Suppose the greedy policy is no better than π\pi, so Vπ′=VπV^{\pi'} = V^\pi. By the strict part of the theorem, no state can have max⁡aQπ(s,a)>Vπ(s)\max_a Q^\pi(s,a) > V^\pi(s), because that state’s value would have gone up. Combined with max⁡aQπ(s,a)≥Vπ(s)\max_a Q^\pi(s,a) \ge V^\pi(s), this gives Vπ(s)=max⁡aQπ(s,a)V^\pi(s) = \max_a Q^\pi(s,a) in every state. Written out, that is exactly the Bellman optimality equation. So Vπ=V∗V^\pi = V^* and π\pi was already optimal. Greedy improvement either strictly improves the policy somewhere or certifies that it is optimal.

Worked example

Improve “always left” in the corridor (γ=0.9\gamma = 0.9). From the table above, Vπ=(0.5,0.45,0.405)V^\pi = (0.5, 0.45, 0.405). The one-step lookaheads are:

StateQπ(s,left)Q^\pi(s, \text{left})Qπ(s,right)Q^\pi(s, \text{right})greedy
A0.50.50.9⋅0.45=0.4050.9 \cdot 0.45 = 0.405left
B0.9⋅0.5=0.450.9 \cdot 0.5 = 0.450.9⋅0.405=0.36450.9 \cdot 0.405 = 0.3645left
C0.9⋅0.45=0.4050.9 \cdot 0.45 = 0.40511right

Only C switches. Notice that B doesn’t switch yet, even though right is ultimately better from B. Under the current policy, C is a poor place to be: its value 0.405 comes from walking all the way back left. Improvement only trusts the current values, so it fixes the state whose problem is visible right now.

Policy iteration

Alternate the two steps: evaluate, improve, evaluate, improve, until the policy stops changing. That is policy iteration (Howard, 1960).

Policy iteration
1. π ← any deterministic policy
2. repeat:
3.     V ← evaluate(π)                                   # policy evaluation
4.     for every state s:                                # policy improvement
5.         π_new(s) ← argmax_a [ r(s,a) + γ Σ_s' P(s'|s,a) V(s') ]
6.             (keep π(s) unless another action is strictly better)
7.     stable ← (π_new == π);  π ← π_new
8. until stable
9. return π, V          # π is optimal and V = V*

Here is the corridor run from “always left” (L = left, R = right):

iterationpolicy (A, B, C)Vπ(A)V^\pi(A)Vπ(B)V^\pi(B)Vπ(C)V^\pi(C)states that switch
0L, L, L0.50.450.405C
1L, L, R0.50.451B
2L, R, R0.50.91A
3R, R, R0.810.91none: optimal

The good news about the big exit travels backward, one state per iteration, until A is convinced too.

Why it must stop. Each iteration either stops or produces a policy that is at least as good everywhere and strictly better somewhere. So no policy can ever appear twice. There are finitely many deterministic policies (∣A∣∣S∣\lvert\mathcal A\rvert^{\lvert\mathcal S\rvert}), so the loop must end, and when it ends the policy is optimal. (Exercise 4.4 makes this precise. It also explains line 6: without the “strictly better” rule, round-off can flip a state between two equally good actions forever.) The bound ∣A∣∣S∣\lvert\mathcal A\rvert^{\lvert\mathcal S\rvert} is absurdly pessimistic. In practice policy iteration converges in a handful of iterations, even on large problems. The catch is that each iteration contains a full policy evaluation.

Value iteration

Policy iteration spends most of its time in step 3, evaluating policies to high precision. Yet early policies are bad and about to be replaced, so precise values for them are wasted effort. What if we evaluate with just one sweep, then improve, then do one more sweep, and so on? Say the current values are VkV_k. The improvement step picks, in each state, the action aa with the largest lookahead r(s,a)+γ∑s′P(s′∣s,a)Vk(s′)r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V_k(s'). The single evaluation sweep for that new policy, starting from VkV_k, then computes exactly that same lookahead for the chosen action. So the new value is the largest lookahead. Improvement and the one evaluation sweep merge into a single update:

Vk+1(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a) Vk(s′)]for every s.V_{k+1}(s) = \max_a \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V_k(s') \Big] \quad \text{for every } s.

This is value iteration. Look at it closely: it is the Bellman optimality equation, used as an update rule. Policy evaluation turned the expectation equation into an update. Value iteration does the same to the optimality equation.

Value iteration
input: P, r, discount γ, tolerance tol
1. V ← 0  (any start works)
2. repeat:
3.     for every state s:
4.         V_new(s) ← max_a [ r(s,a) + γ Σ_s' P(s'|s,a) V(s') ]
5.     Δ ← max_s |V_new(s) − V(s)|
6.     V ← V_new
7. until Δ < tol
8. π(s) ← argmax_a [ r(s,a) + γ Σ_s' P(s'|s,a) V(s') ]   # extract the greedy policy
9. return V, π

Worked example: value iteration on the corridor

With γ=0.9\gamma = 0.9 and V0=0V_0 = 0, each sweep computes Vk+1(A)=max⁡(0.5, 0.9Vk(B))V_{k+1}(A) = \max(0.5,\ 0.9 V_k(B)), Vk+1(B)=max⁡(0.9Vk(A), 0.9Vk(C))V_{k+1}(B) = \max(0.9 V_k(A),\ 0.9 V_k(C)), and Vk+1(C)=max⁡(0.9Vk(B), 1)V_{k+1}(C) = \max(0.9 V_k(B),\ 1):

sweep kkVk(A)V_k(A)Vk(B)V_k(B)Vk(C)V_k(C)max changegreedy policy w.r.t. VkV_k
0000
10.5011.0L, R, R
20.50.910.9R, R, R (already optimal)
30.810.910.31R, R, R
40.810.910R, R, R

Two things to notice.

VkV_k has a meaning of its own. Starting from V0=0V_0 = 0, Vk(s)V_k(s) is the best expected discounted return you can collect in kk steps from ss. After one sweep, A knows it can grab 0.5 and C knows it can grab 1. B needs two steps to reach anything. Value iteration solves ever-longer finite-horizon problems, and the horizon grows by one each sweep. Discounting makes the far future matter less and less, which is why the sequence converges.

The policy settles before the values do. After sweep 2, V2(A)=0.5V_2(A) = 0.5 is still wrong (the truth is 0.81), yet the greedy policy is already optimal. The argmax only needs the ranking of actions to be right, not the values. Exercise 4.6 turns this into a theorem, and the demo shows it on a bigger problem: the arrows freeze long before the numbers do.

Quick check Value iteration can be read as policy iteration in which the evaluation step is cut down to…

Watch it run

The demo runs value iteration on the gridworld from the introduction. There is a goal worth +1+1 (right edge), a row of four pits worth −1-1, and a three-cell wall. The reward is paid on the step that enters the goal or a pit, and the episode then ends. With slip probability pp, the agent moves sideways (perpendicular to the intended direction, p/2p/2 each way) instead of where it intended. Bumping into a wall or the edge leaves it where it is. Rows are counted from the top and columns from the left; the inspector shows the coordinates of the selected cell. Each cell’s color shows its current value VkV_k, and the arrows show the greedy action(s). When several actions tie, all of them are drawn, so at the start you see crosses: no preference yet. The inspector shows the four one-step lookaheads for the selected cell, with the arithmetic for the best one. You can check every number against the backup formula. The chart plots the max change ∥Vk−Vk−1∥∞\lVert V_{k} - V_{k-1} \rVert_\infty on a log axis, together with a dashed line falling like γk\gamma^{k}. The next section explains why that line is there.

Interactive Value iteration, one sweep at a time
Algorithm
Tap a cell to

● max change ‖Vk − Vk−1‖∞- - contraction bound δ1·γk−1

What to try: press Step a few times and watch value spread outward from the goal, one cell per sweep, while the cells next to the pits learn to avoid them. Press Play and note the sweep at which the arrows stop changing, compared with the sweep at which the values converge. Raise slip to 0.3: most cells beside the pits stop walking along them, and several start 'hugging the wall', pushing into a wall or the edge so that only the safe sideways slips move them. Then set the reward per step to −0.1 and watch the agent take the risky shortcut again. Press Random start: the values begin anywhere, yet end at the same V*. Switch to Policy iteration and count iterations versus evaluation sweeps. Tap with Wall/Goal/Pit to redesign the grid.

A few things are worth seeing for yourself. With the defaults (γ=0.9\gamma = 0.9, slip 0.10.1), value iteration stops (max change below 10−610^{-6}) after 24 sweeps, but the arrows stop changing at sweep 10. With slip 0, the value of the cell in row 3, column 1 is exactly 0.94=0.6560.9^4 = 0.656. The goal is 5 moves away, and the reward arrives on the 5th move, so it is discounted 4 times. With slip 0.10.1 it falls to 0.3870.387: the risk of sliding into a pit costs you.

Why it works: the Bellman operator is a contraction

We have three algorithms and some faith that they converge. Now for the proof. It is short and beautiful, and it also answers the questions we skipped. Does the optimality equation have a solution? Is it unique? Is the greedy policy really optimal? How many sweeps do we need?

Operators on value vectors

Think of a value function as a vector V∈R∣S∣V \in \mathbb{R}^{\lvert\mathcal S\rvert}. Define two maps from vectors to vectors, the Bellman expectation operator Tπ\mathcal{T}^\pi and the Bellman optimality operator T∗\mathcal{T}^*:

(TπV)(s)=∑aπ(a∣s)[r(s,a)+γ∑s′P(s′∣s,a)V(s′)],(\mathcal{T}^\pi V)(s) = \sum_a \pi(a \mid s) \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V(s') \Big], (T∗V)(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a)V(s′)].(\mathcal{T}^* V)(s) = \max_a \Big[ r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V(s') \Big].

Now everything is compact. The Bellman equations say that VπV^\pi and V∗V^* are fixed points: Vπ=TπVπV^\pi = \mathcal{T}^\pi V^\pi and V∗=T∗V∗V^* = \mathcal{T}^* V^*. Iterative policy evaluation is Vk+1=TπVkV_{k+1} = \mathcal{T}^\pi V_k, and value iteration is Vk+1=T∗VkV_{k+1} = \mathcal{T}^* V_k. The question “does it converge?” becomes “does repeatedly applying the map approach a fixed point?”

Contraction in the sup norm

Measure the distance between two value functions by the largest disagreement in any state, the sup norm:

∥U−V∥∞=max⁡s∣U(s)−V(s)∣.\lVert U - V \rVert_\infty = \max_s \lvert U(s) - V(s) \rvert.

Here is why, in words. The backup builds each state’s new value from an average of next-state values (weights P(s′∣s,a)P(s' \mid s,a)), then multiplies by γ\gamma. Averaging can’t increase the worst-case disagreement, and the factor γ\gamma shrinks it. The max over actions doesn’t spoil this, because two maxima can’t differ by more than the largest difference between their arguments. The rewards are the same for both guesses, so they cancel.

∂ Proof that the Bellman optimality operator is a γ-contraction

Lemma. For any two functions f,gf, g on a finite set, ∣max⁡af(a)−max⁡ag(a)∣≤max⁡a∣f(a)−g(a)∣\lvert \max_a f(a) - \max_a g(a) \rvert \le \max_a \lvert f(a) - g(a) \rvert.

Proof. Suppose without loss of generality that max⁡f≥max⁡g\max f \ge \max g, and let a†a^\dagger be a maximizer of ff. Then 0≤max⁡f−max⁡g=f(a†)−max⁡g≤f(a†)−g(a†)≤max⁡a∣f(a)−g(a)∣0 \le \max f - \max g = f(a^\dagger) - \max g \le f(a^\dagger) - g(a^\dagger) \le \max_a \lvert f(a) - g(a) \rvert.

Theorem. ∥T∗U−T∗V∥∞≤γ∥U−V∥∞\lVert \mathcal{T}^* U - \mathcal{T}^* V \rVert_\infty \le \gamma \lVert U - V \rVert_\infty.

Proof. Fix a state ss. Apply the lemma with f(a)=r(s,a)+γ∑s′P(s′∣s,a)U(s′)f(a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) U(s') and g(a)g(a) the same with VV. The rewards cancel in f−gf - g:

∣(T∗U)(s)−(T∗V)(s)∣≤max⁡aγ∣∑s′P(s′∣s,a)(U(s′)−V(s′))∣.\lvert (\mathcal{T}^* U)(s) - (\mathcal{T}^* V)(s) \rvert \le \max_a \gamma \Big\lvert \sum_{s'} P(s' \mid s,a) \big(U(s') - V(s')\big) \Big\rvert.

The weights P(s′∣s,a)P(s' \mid s,a) are nonnegative and sum to 1, so the weighted sum is at most the largest ∣U(s′)−V(s′)∣\lvert U(s') - V(s') \rvert:

∣(T∗U)(s)−(T∗V)(s)∣≤γmax⁡a∑s′P(s′∣s,a)∣U(s′)−V(s′)∣≤γ∥U−V∥∞.\lvert (\mathcal{T}^* U)(s) - (\mathcal{T}^* V)(s) \rvert \le \gamma \max_a \sum_{s'} P(s' \mid s,a) \lvert U(s') - V(s') \rvert \le \gamma \lVert U - V \rVert_\infty.

This holds for every ss. Take the max over ss on the left. The same argument without the lemma (an average over aa instead of a max) proves it for Tπ\mathcal{T}^\pi; that’s Exercise 4.3.

Banach’s fixed-point theorem

A map that shrinks all distances by a fixed factor below one has a remarkable property.

∂ Complete proof of Banach's fixed-point theorem

Let Vk+1=TVkV_{k+1} = \mathcal{T} V_k, starting from any V0V_0. Write ∥⋅∥\lVert \cdot \rVert for the sup norm.

Step 1: successive steps shrink geometrically. ∥Vk+1−Vk∥=∥TVk−TVk−1∥≤γ∥Vk−Vk−1∥\lVert V_{k+1} - V_k \rVert = \lVert \mathcal{T} V_k - \mathcal{T} V_{k-1} \rVert \le \gamma \lVert V_k - V_{k-1} \rVert, so by induction ∥Vk+1−Vk∥≤γk∥V1−V0∥\lVert V_{k+1} - V_k \rVert \le \gamma^k \lVert V_1 - V_0 \rVert.

Step 2: the sequence is Cauchy. For m>km > k, by the triangle inequality and a geometric series,

∥Vm−Vk∥≤∑j=km−1∥Vj+1−Vj∥≤∑j=km−1γj∥V1−V0∥≤γk1−γ∥V1−V0∥,\lVert V_m - V_k \rVert \le \sum_{j=k}^{m-1} \lVert V_{j+1} - V_j \rVert \le \sum_{j=k}^{m-1} \gamma^j \lVert V_1 - V_0 \rVert \le \frac{\gamma^k}{1-\gamma} \lVert V_1 - V_0 \rVert,

which goes to 0 as k→∞k \to \infty, uniformly in mm.

Step 3: it has a limit. Rn\mathbb{R}^n with any norm is complete: every Cauchy sequence converges. Call the limit V⋆V_\star.

Step 4: the limit is a fixed point. A contraction is Lipschitz, hence continuous. So TV⋆=T(lim⁡kVk)=lim⁡kTVk=lim⁡kVk+1=V⋆\mathcal{T} V_\star = \mathcal{T}(\lim_k V_k) = \lim_k \mathcal{T} V_k = \lim_k V_{k+1} = V_\star.

Step 5: it is the only fixed point. If U=TUU = \mathcal{T} U and V=TVV = \mathcal{T} V, then ∥U−V∥=∥TU−TV∥≤γ∥U−V∥\lVert U - V \rVert = \lVert \mathcal{T} U - \mathcal{T} V \rVert \le \gamma \lVert U - V \rVert. Since γ<1\gamma < 1, this forces ∥U−V∥=0\lVert U - V \rVert = 0.

Step 6: the rate. ∥Vk−V⋆∥=∥TVk−1−TV⋆∥≤γ∥Vk−1−V⋆∥≤⋯≤γk∥V0−V⋆∥\lVert V_k - V_\star \rVert = \lVert \mathcal{T} V_{k-1} - \mathcal{T} V_\star \rVert \le \gamma \lVert V_{k-1} - V_\star \rVert \le \dots \le \gamma^k \lVert V_0 - V_\star \rVert.

What this buys us

Apply Banach to Tπ\mathcal{T}^\pi and to T∗\mathcal{T}^*:

  • Policy evaluation converges to VπV^\pi from any start. You also get, for free, that the Bellman expectation equation has a unique solution. That is the invertibility of I−γPπI - \gamma P_\pi from Chapter 3, seen from a different angle.
  • The optimality equation has exactly one solution, and value iteration converges to it from any start. That’s why the guess-and-check in the corridor was legitimate, and why the demo’s “Random start” always ends at the same values.
  • The rate is geometric, with ratio γ. On a log axis, the error falls at least as fast as a straight line with slope log⁡10γ\log_{10} \gamma per sweep. The same bound holds for the change between successive sweeps (Step 1 of the proof). The change is what the demo plots, since unlike the error it can be computed without knowing V∗V^*, and the dashed line is its bound ∥V1−V0∥∞ γk−1\lVert V_1 - V_0 \rVert_\infty \, \gamma^{k-1}.

How many sweeps does that mean? To shrink the initial error by a factor ϵ\epsilon, you need γk≤ϵ\gamma^k \le \epsilon, i.e.

k≥ln⁡(1/ϵ)ln⁡(1/γ)≈ln⁡(1/ϵ)1−γ.k \ge \frac{\ln(1/\epsilon)}{\ln(1/\gamma)} \approx \frac{\ln(1/\epsilon)}{1-\gamma}.

For ϵ=10−6\epsilon = 10^{-6}, that is 132 sweeps at γ=0.9\gamma = 0.9 and 1,375 at γ=0.99\gamma = 0.99. The factor 1/(1−γ)1/(1-\gamma) is the effective horizon from Chapter 3 again. Information must travel about that many steps, one step per sweep, before far-away rewards stop mattering.

We still need to close one gap. We defined V∗V^* as “the best over all policies” and then wrote down an equation it should satisfy. Is the unique solution of that equation really the best you can do, and does greedy play achieve it?

∂ Why the fixed point of T* is the optimal value, and greedy play achieves it

Let V⋆V_\star be the unique fixed point of T∗\mathcal{T}^*. We use two easy properties of the operators.

  • Max beats average: TπV≤T∗V\mathcal{T}^\pi V \le \mathcal{T}^* V for every VV and every π\pi (componentwise).
  • Monotonicity: if U≤VU \le V componentwise, then T∗U≤T∗V\mathcal{T}^* U \le \mathcal{T}^* V, because the weights P(s′∣s,a)P(s' \mid s,a) are nonnegative.

No policy beats V⋆V_\star. For any stationary policy π\pi: Vπ=TπVπ≤T∗VπV^\pi = \mathcal{T}^\pi V^\pi \le \mathcal{T}^* V^\pi. Apply T∗\mathcal{T}^* to both sides of Vπ≤T∗VπV^\pi \le \mathcal{T}^* V^\pi and use monotonicity: T∗Vπ≤(T∗)2Vπ\mathcal{T}^* V^\pi \le (\mathcal{T}^*)^2 V^\pi. Chaining, Vπ≤(T∗)nVπV^\pi \le (\mathcal{T}^*)^n V^\pi for all nn. The right side converges to V⋆V_\star by Banach, so Vπ≤V⋆V^\pi \le V_\star.

The greedy policy achieves V⋆V_\star. Let πg\pi_g be greedy with respect to V⋆V_\star. By definition of greedy, TπgV⋆=T∗V⋆=V⋆\mathcal{T}^{\pi_g} V_\star = \mathcal{T}^* V_\star = V_\star. So V⋆V_\star is a fixed point of Tπg\mathcal{T}^{\pi_g}. That operator is also a contraction, so its fixed point is unique, and that fixed point is VπgV^{\pi_g}. Hence Vπg=V⋆V^{\pi_g} = V_\star.

Together: V⋆=max⁡πVπ=V∗V_\star = \max_\pi V^\pi = V^*, and the deterministic greedy policy attains it in every state simultaneously. (A longer argument, in Puterman’s book, shows that even non-stationary or history-dependent policies can’t do better in a finite MDP.)

Quick check Value iteration with γ = 0.9 starts from a guess whose worst-state error is 10. What does the contraction property guarantee about the worst-state error after 20 sweeps?

A stopping rule you can trust. In practice you don’t know V∗V^*, but you can see the change ∥Vk+1−Vk∥∞\lVert V_{k+1} - V_k \rVert_\infty. The contraction property links the two (Exercise 4.5):

∥Vk+1−V∗∥∞≤γ1−γ∥Vk+1−Vk∥∞.\lVert V_{k+1} - V^* \rVert_\infty \le \frac{\gamma}{1-\gamma} \lVert V_{k+1} - V_k \rVert_\infty.

So to be within ϵ\epsilon of V∗V^*, stop when the change drops below ϵ(1−γ)/γ\epsilon (1-\gamma)/\gamma. That threshold is much smaller than ϵ\epsilon when γ\gamma is close to 1.

And policy iteration? It also converges, and usually in far fewer iterations than value iteration has sweeps. It is a classical result (Puterman and Brumelle, 1979) that policy iteration is Newton’s method applied to the equation V−T∗V=0V - \mathcal{T}^* V = 0. Like Newton’s method, it converges very fast once it is close. Each Newton step, however, needs a full policy evaluation. In the demo’s defaults, value iteration takes 24 sweeps, while policy iteration takes 8 iterations but over 300 evaluation sweeps. Which one is cheaper depends on the problem and on how you evaluate.

Generalized policy iteration

Step back and look at the two algorithms side by side. Both alternate two processes that pull against each other:

                 evaluation:  make V consistent with π   (V → V^π)
          ┌───────────────────────────────────────────────────┐
          │                                                   ▼
          π                                                   V
          ▲                                                   │
          └───────────────────────────────────────────────────┘
                 improvement: make π greedy with respect to V

Evaluation makes the value function agree with the policy, and that usually makes the policy no longer greedy. Improvement makes the policy greedy, and that makes the value function out of date. Each step undoes some of the other’s work, yet together they converge to the only pair that is consistent both ways: V∗V^* and a greedy π∗\pi^*. Policy iteration runs evaluation to convergence. Value iteration runs one sweep. Modified policy iteration runs mm sweeps, anything in between, and works for every mm. Sutton and Barto call this whole family generalized policy iteration (GPI): any interleaving of (possibly partial) evaluation and (possibly partial) improvement.

The updates don’t even have to be organized into sweeps. Asynchronous DP backs up states one at a time, in any order, using the latest values (Gauss–Seidel taken to its logical end). It still converges as long as every state keeps getting updated. This frees you to spend computation where it matters. Prioritized sweeping updates first the states whose values are changing most. Real-time DP updates the states an agent actually visits as it moves through the world. That last idea, backing up the states along real experience, is a short step from the model-free learning of Chapter 5.

GPI is the pattern behind almost all of reinforcement learning, so it’s worth naming where each later algorithm puts its two halves:

MethodEvaluationImprovement
Policy / value iteration (this chapter)exact backups with the modelgreedy argmax
SARSA, Q-learning (Ch 5)sampled backups of QQε-greedy w.r.t. QQ
DQN (Ch 6)a neural QQ fitted to sampled backupsgreedy w.r.t. the network
Actor-critic (Ch 8)the critic learns VπV^\pia small policy-gradient step for the actor
PPO (Ch 9)advantage estimates from a critica clipped, conservative policy step

What dynamic programming cannot do

DP is exact and elegant, and in practice you almost never get to use it directly. There are two reasons.

It needs the model. Every backup computes ∑s′P(s′∣s,a) V(s′)\sum_{s'} P(s' \mid s,a)\, V(s'), so you must know PP and rr. A robot doesn’t know the physics of its gripper to that precision. A game-playing agent may know the rules but not the opponent. Usually all we can do is try things and see what happens. Chapter 5 replaces the expected backup with a backup from a single sampled transition, which turns DP into learning.

It needs to touch every state, every sweep. One sweep costs about ∣S∣×∣A∣×\lvert\mathcal S\rvert \times \lvert\mathcal A\rvert \times (number of possible next states). That is fine for 22 cells and not fine for interesting problems. A robot arm with 10 joints, each discretized into 100 positions, has 10010=1020100^{10} = 10^{20} states. Backgammon has roughly 102010^{20} positions, and Go about 1017010^{170}. Bellman himself coined the name for this: the curse of dimensionality. The number of states grows exponentially with the number of variables that describe a state. Chapter 6 answers it with function approximation: instead of a table with one entry per state, a neural network that generalizes across similar states.

There is also a subtle requirement hiding in the math: γ<1\gamma < 1. With γ=1\gamma = 1, the operators are no longer contractions. Values can be infinite in tasks that never end, and the fixed point can stop being unique. Exercise 4.7 explores what goes wrong and what people do instead. Episodic tasks in which every policy eventually terminates are fine. LLM responses are of this kind, which is why LLM RL often uses γ=1\gamma = 1.

Summary

  • The Bellman optimality equation V∗(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a)V∗(s′)]V^*(s) = \max_a [r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V^*(s')] says that optimal value is the best one-step lookahead on optimal future value. It is the expectation equation with the average over actions replaced by a max, which makes it nonlinear.
  • Acting greedily on V∗V^* (or taking the argmax of Q∗Q^*) is optimal, in every state at once, with a deterministic policy. Value functions turn long-horizon planning into one-step decisions.
  • Iterative policy evaluation applies the expectation backup in sweeps. Information travels one step per sweep, like a relaxation method.
  • Policy improvement theorem: acting greedily on QπQ^\pi gives a policy at least as good everywhere, and strictly better somewhere unless π\pi is already optimal. Policy iteration alternates evaluation and improvement and ends in finitely many iterations.
  • Value iteration is the optimality equation used as an update. It equals policy iteration with one evaluation sweep, and VkV_k is the optimal kk-step value. The greedy policy is often optimal long before the values converge.
  • The Bellman operators are γ-contractions in the sup norm. By Banach’s theorem each has a unique fixed point, and iteration reaches it from any start, with error at most γk\gamma^k times the initial error, which takes about ln⁡(1/ϵ)/(1−γ)\ln(1/\epsilon)/(1-\gamma) sweeps.
  • Generalized policy iteration (interleaved evaluation and improvement, in any proportion and any order) is the template for nearly every RL algorithm in this course, including the ones used to train LLMs.
  • DP needs the model and touches every state. Chapter 5 removes the first requirement by sampling, and Chapter 6 tackles the second with function approximation.

Exercises

Exercise 4.1Linear or not?Concept●○○

(a) Which of these can you compute with a single linear solve, given PP and rr: VπV^\pi for a fixed stochastic policy; QπQ^\pi for a fixed policy; V∗V^*? Explain in one sentence each.

(b) A friend gives you V∗V^* for a gridworld but not the transition probabilities. Can you act optimally? What if they give you Q∗Q^* instead?

Solution

(a) VπV^\pi: yes. The expectation equation V=rπ+γPπVV = r_\pi + \gamma P_\pi V is linear, so Vπ=(I−γPπ)−1rπV^\pi = (I - \gamma P_\pi)^{-1} r_\pi. QπQ^\pi: yes. Either compute VπV^\pi first and then Qπ(s,a)=r(s,a)+γ∑s′P(s′∣s,a)Vπ(s′)Q^\pi(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V^\pi(s'), or solve the linear system Q=r+γPΠQQ = r + \gamma P \Pi Q directly, where Π\Pi averages over π\pi. V∗V^*: no. The optimality equation contains max⁡a\max_a, which is piecewise linear but not linear, so there is no single matrix to invert. (You could solve it as a linear program, a classic alternative to DP, but not with one linear solve.)

(b) With only V∗V^*, acting greedily requires the lookahead arg⁡max⁡a[r(s,a)+γ∑s′P(s′∣s,a)V∗(s′)]\arg\max_a [r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V^*(s')], which needs PP and rr. So you can’t, unless you can simulate the environment. With Q∗Q^* you just take arg⁡max⁡aQ∗(s,a)\arg\max_a Q^*(s,a), and no model is needed. Q∗Q^* has the lookahead “built in”. That’s why model-free control (Q-learning, Chapter 5) learns QQ.

Exercise 4.2Two sweeps of value iteration by handPen & paper●○○

Make the corridor slippery: every move succeeds with probability 0.8, and with probability 0.2 the agent stays where it is (reward 0). Exits pay +0.5 (left of A) and +1 (right of C) only when the move succeeds. Use γ=0.9\gamma = 0.9 and V0=0V_0 = 0.

(a) Write the one-step lookahead Q(s,a)Q(s,a) for all six state–action pairs in terms of VV.

(b) Compute V1V_1 and V2V_2, and the max change after each sweep.

(c) What is the greedy policy with respect to V2V_2? Is it already optimal? (The exact V∗V^* is about (0.752,0.857,0.976)(0.752, 0.857, 0.976).)

Hint
For A going left: with probability 0.8 you exit and collect 0.5 (and the exit has value 0); with probability 0.2 you stay in A and are worth γ·V(A).
Solution

(a) Writing VAV_A for V(A)V(A) and so on:

  • Q(A,L)=0.8⋅0.5+0.2⋅0.9VA=0.4+0.18VAQ(A,\text{L}) = 0.8 \cdot 0.5 + 0.2 \cdot 0.9 V_A = 0.4 + 0.18 V_A
  • Q(A,R)=0.9(0.8VB+0.2VA)Q(A,\text{R}) = 0.9 (0.8 V_B + 0.2 V_A)
  • Q(B,L)=0.9(0.8VA+0.2VB)Q(B,\text{L}) = 0.9 (0.8 V_A + 0.2 V_B)
  • Q(B,R)=0.9(0.8VC+0.2VB)Q(B,\text{R}) = 0.9 (0.8 V_C + 0.2 V_B)
  • Q(C,L)=0.9(0.8VB+0.2VC)Q(C,\text{L}) = 0.9 (0.8 V_B + 0.2 V_C)
  • Q(C,R)=0.8⋅1+0.2⋅0.9VC=0.8+0.18VCQ(C,\text{R}) = 0.8 \cdot 1 + 0.2 \cdot 0.9 V_C = 0.8 + 0.18 V_C

(b) Sweep 1 (V0=0V_0 = 0): V1(A)=max⁡(0.4,0)=0.4V_1(A) = \max(0.4, 0) = 0.4; V1(B)=max⁡(0,0)=0V_1(B) = \max(0, 0) = 0; V1(C)=max⁡(0,0.8)=0.8V_1(C) = \max(0, 0.8) = 0.8. So V1=(0.4,0,0.8)V_1 = (0.4, 0, 0.8), max change 0.80.8.

Sweep 2 (using V1V_1):

  • A: L =0.4+0.18⋅0.4=0.472= 0.4 + 0.18 \cdot 0.4 = 0.472; R =0.9(0+0.2⋅0.4)=0.072= 0.9(0 + 0.2 \cdot 0.4) = 0.072. So V2(A)=0.472V_2(A) = 0.472.
  • B: L =0.9(0.8⋅0.4+0)=0.288= 0.9(0.8 \cdot 0.4 + 0) = 0.288; R =0.9(0.8⋅0.8+0)=0.576= 0.9(0.8 \cdot 0.8 + 0) = 0.576. So V2(B)=0.576V_2(B) = 0.576.
  • C: L =0.9(0+0.2⋅0.8)=0.144= 0.9(0 + 0.2 \cdot 0.8) = 0.144; R =0.8+0.18⋅0.8=0.944= 0.8 + 0.18 \cdot 0.8 = 0.944. So V2(C)=0.944V_2(C) = 0.944.

V2=(0.472,0.576,0.944)V_2 = (0.472, 0.576, 0.944), max change =max⁡(0.072,0.576,0.144)=0.576= \max(0.072, 0.576, 0.144) = 0.576 (at B).

(c) Greedy with respect to V2V_2:

  • A: L =0.4+0.18⋅0.472=0.485= 0.4 + 0.18 \cdot 0.472 = 0.485; R =0.9(0.8⋅0.576+0.2⋅0.472)=0.9⋅0.555=0.500= 0.9(0.8 \cdot 0.576 + 0.2 \cdot 0.472) = 0.9 \cdot 0.555 = 0.500. So A goes right.
  • B: R =0.9(0.8⋅0.944+0.2⋅0.576)=0.783>= 0.9(0.8 \cdot 0.944 + 0.2 \cdot 0.576) = 0.783 > L =0.9(0.8⋅0.472+0.2⋅0.576)=0.443= 0.9(0.8 \cdot 0.472 + 0.2 \cdot 0.576) = 0.443. So B goes right.
  • C: R =0.8+0.18⋅0.944=0.970>= 0.8 + 0.18 \cdot 0.944 = 0.970 > L. So C goes right.

So the greedy policy is already “always right”, which is optimal. At V∗≈(0.752,0.857,0.976)V^* \approx (0.752, 0.857, 0.976), A’s right value 0.7520.752 beats left, 0.4+0.18⋅0.752=0.5350.4 + 0.18 \cdot 0.752 = 0.535. But V2(A)=0.472V_2(A) = 0.472 is still far from 0.7520.752. Once again the policy is right before the values are. (The decision at A is close at V2V_2: 0.500 vs 0.485. One sweep earlier, A would still have chosen left.)

Exercise 4.3The expectation operator is a contractionPen & paper●●○

(a) Prove that ∥TπU−TπV∥∞≤γ∥U−V∥∞\lVert \mathcal{T}^\pi U - \mathcal{T}^\pi V \rVert_\infty \le \gamma \lVert U - V \rVert_\infty.

(b) Prove that Tπ\mathcal{T}^\pi is monotone: U≤VU \le V componentwise implies TπU≤TπV\mathcal{T}^\pi U \le \mathcal{T}^\pi V.

(c) For γ=1\gamma = 1, give a one-state MDP in which Tπ\mathcal{T}^\pi has infinitely many fixed points.

Hint
Write (TπU−TπV)(s)(\mathcal{T}^\pi U - \mathcal{T}^\pi V)(s) as γ times a weighted average of U(s′)−V(s′)U(s') - V(s'). What are the weights, and what do they sum to?
Solution

(a) The rewards cancel:

(TπU−TπV)(s)=γ∑s′ws(s′)(U(s′)−V(s′)),ws(s′)=∑aπ(a∣s)P(s′∣s,a).(\mathcal{T}^\pi U - \mathcal{T}^\pi V)(s) = \gamma \sum_{s'} w_s(s') \big(U(s') - V(s')\big), \qquad w_s(s') = \sum_a \pi(a \mid s) P(s' \mid s,a).

The weights ws(s′)w_s(s') are nonnegative and ∑s′ws(s′)=∑aπ(a∣s)∑s′P(s′∣s,a)=1\sum_{s'} w_s(s') = \sum_a \pi(a \mid s) \sum_{s'} P(s' \mid s,a) = 1. So the absolute value is at most γ∑s′ws(s′)∣U(s′)−V(s′)∣≤γmax⁡s′∣U(s′)−V(s′)∣=γ∥U−V∥∞\gamma \sum_{s'} w_s(s') \lvert U(s') - V(s') \rvert \le \gamma \max_{s'} \lvert U(s') - V(s') \rvert = \gamma \lVert U - V \rVert_\infty. Take the max over ss.

(b) If U≤VU \le V, every term ws(s′)(V(s′)−U(s′))w_s(s')(V(s') - U(s')) is ≥0\ge 0, so (TπV−TπU)(s)≥0(\mathcal{T}^\pi V - \mathcal{T}^\pi U)(s) \ge 0 for every ss.

(c) One state ss, one action that loops back to ss with reward 0, and γ=1\gamma = 1. Then (TπV)(s)=0+1⋅V(s)=V(s)(\mathcal{T}^\pi V)(s) = 0 + 1 \cdot V(s) = V(s) for every number V(s)V(s), so every vector is a fixed point. The “contraction” factor is 1, and Banach’s theorem says nothing. With γ<1\gamma < 1 the only fixed point is V(s)=0V(s) = 0.

Exercise 4.4Policy iteration terminatesPen & paper●●○

Prove that policy iteration, with the rule “switch an action only if the new one is strictly better”, stops after at most ∣A∣∣S∣\lvert\mathcal A\rvert^{\lvert\mathcal S\rvert} iterations, and that the final policy is optimal. Then explain what could go wrong without the “strictly better” rule.

Hint
Show that the sequence of value functions Vπ0,Vπ1,…V^{\pi_0}, V^{\pi_1}, \dots is strictly increasing (in the componentwise partial order) until the policy stops changing. What does that imply about repeats?
Solution

Let πk+1\pi_{k+1} be the improved version of πk\pi_k. In every state, πk+1(s)\pi_{k+1}(s) is either πk(s)\pi_k(s), with Qπk(s,πk+1(s))=Vπk(s)Q^{\pi_k}(s, \pi_{k+1}(s)) = V^{\pi_k}(s), or a strictly better action, with Qπk(s,πk+1(s))>Vπk(s)Q^{\pi_k}(s, \pi_{k+1}(s)) > V^{\pi_k}(s). By the policy improvement theorem, Vπk+1≥VπkV^{\pi_{k+1}} \ge V^{\pi_k} everywhere. If at least one state switched, the inequality is strict in that state, so Vπk+1≠VπkV^{\pi_{k+1}} \ne V^{\pi_k}.

So while the policy keeps changing, the value functions form a strictly increasing sequence: each is ≥\ge the previous one everywhere and >> somewhere. A policy can’t appear twice. If πj=πk\pi_j = \pi_k with j<kj < k, then Vπj=VπkV^{\pi_j} = V^{\pi_k}, contradicting strict increase along j,j+1,…,kj, j+1, \dots, k. There are only ∣A∣∣S∣\lvert\mathcal A\rvert^{\lvert\mathcal S\rvert} deterministic policies, so after at most that many iterations no state switches.

When no state switches, no action beats the current one: max⁡aQπ(s,a)=Qπ(s,π(s))=Vπ(s)\max_a Q^{\pi}(s,a) = Q^\pi(s, \pi(s)) = V^\pi(s) for all ss. That is the Bellman optimality equation, whose unique solution is V∗V^*. So Vπ=V∗V^\pi = V^* and π\pi is optimal.

Without the strict rule, take a state with two actions that are exactly tied in true value. Computed values carry round-off (or come from evaluation stopped at a tolerance), so the argmax can pick action 1 in one iteration and action 2 in the next, then back again. The value never changes, but the policy keeps changing, so “stop when the policy is stable” never fires. The strict rule (switch only if better by more than a tiny threshold) breaks the tie in favor of the incumbent. The lab’s policy_iteration uses a threshold of 10−910^{-9}.

Exercise 4.5When can you stop?Pen & paper●●○

(a) Prove that for value iteration Vk+1=T∗VkV_{k+1} = \mathcal{T}^* V_k,

∥Vk+1−V∗∥∞≤γ1−γ∥Vk+1−Vk∥∞.\lVert V_{k+1} - V^* \rVert_\infty \le \frac{\gamma}{1-\gamma} \lVert V_{k+1} - V_k \rVert_\infty.

(b) With γ=0.99\gamma = 0.99, how small must the observed change ∥Vk+1−Vk∥∞\lVert V_{k+1} - V_k \rVert_\infty be to guarantee that Vk+1V_{k+1} is within 0.010.01 of V∗V^* in every state?

Hint
Start from ∥Vk+1−V∗∥=∥T∗Vk−T∗V∗∥\lVert V_{k+1} - V^* \rVert = \lVert \mathcal{T}^* V_k - \mathcal{T}^* V^* \rVert, use the contraction, then insert Vk+1V_{k+1} with the triangle inequality.
Solution

(a) Using V∗=T∗V∗V^* = \mathcal{T}^* V^*, the contraction, and the triangle inequality:

∥Vk+1−V∗∥≤γ∥Vk−V∗∥≤γ(∥Vk−Vk+1∥+∥Vk+1−V∗∥).\lVert V_{k+1} - V^* \rVert \le \gamma \lVert V_k - V^* \rVert \le \gamma \big( \lVert V_k - V_{k+1} \rVert + \lVert V_{k+1} - V^* \rVert \big).

Move the γ∥Vk+1−V∗∥\gamma \lVert V_{k+1} - V^* \rVert term to the left: (1−γ)∥Vk+1−V∗∥≤γ∥Vk+1−Vk∥(1-\gamma) \lVert V_{k+1} - V^* \rVert \le \gamma \lVert V_{k+1} - V_k \rVert. Divide by 1−γ>01 - \gamma > 0.

(b) We need 0.990.01Δ≤0.01\frac{0.99}{0.01} \Delta \le 0.01, i.e. Δ≤0.01⋅0.01/0.99≈1.0×10−4\Delta \le 0.01 \cdot 0.01 / 0.99 \approx 1.0 \times 10^{-4}. The change must be a hundred times smaller than the accuracy you want. When γ\gamma is near 1, a small change between sweeps doesn’t yet mean you are close: the values may be creeping slowly toward something far away.

Exercise 4.6The policy converges before the valuesPen & paper●●●

Let Δ=min⁡s(max⁡aQ∗(s,a)−max⁡a∉A∗(s)Q∗(s,a))\Delta = \min_s \big( \max_a Q^*(s,a) - \max_{a \notin \mathcal{A}^*(s)} Q^*(s,a) \big) be the smallest gap between the best and the second-best action value in any state, where A∗(s)\mathcal{A}^*(s) is the set of optimal actions in ss (ignore states where every action is optimal).

(a) Show that if ∥V−V∗∥∞<Δ/(2γ)\lVert V - V^* \rVert_\infty < \Delta / (2\gamma), then every greedy action with respect to VV is optimal.

(b) Compute Δ\Delta for the corridor with γ=0.9\gamma = 0.9. How close to V∗V^* does (a) require VV to be? Compare with V2=(0.5,0.9,1)V_2 = (0.5, 0.9, 1) from the value-iteration table, whose greedy policy is already optimal. What does this tell you about the bound?

Hint
Write QV(s,a)=r(s,a)+γ∑s′P(s′∣s,a)V(s′)Q_V(s,a) = r(s,a) + \gamma \sum_{s'} P(s' \mid s,a) V(s'). Bound ∣QV(s,a)−Q∗(s,a)∣\lvert Q_V(s,a) - Q^*(s,a) \rvert in terms of ∥V−V∗∥∞\lVert V - V^* \rVert_\infty.
Solution

(a) For every s,as, a: ∣QV(s,a)−Q∗(s,a)∣=γ∣∑s′P(s′∣s,a)(V(s′)−V∗(s′))∣≤γ∥V−V∗∥∞<Δ/2\lvert Q_V(s,a) - Q^*(s,a) \rvert = \gamma \lvert \sum_{s'} P(s' \mid s,a)(V(s') - V^*(s')) \rvert \le \gamma \lVert V - V^* \rVert_\infty < \Delta/2. Take an optimal action a⋆a^\star and a non-optimal action bb in state ss. Then Q∗(s,a⋆)−Q∗(s,b)≥ΔQ^*(s,a^\star) - Q^*(s,b) \ge \Delta, and so

QV(s,a⋆)>Q∗(s,a⋆)−Δ2≥Q∗(s,b)+Δ2>QV(s,b).Q_V(s,a^\star) > Q^*(s,a^\star) - \tfrac{\Delta}{2} \ge Q^*(s,b) + \tfrac{\Delta}{2} > Q_V(s,b).

Every optimal action scores strictly higher under QVQ_V than every non-optimal one, so the argmax of QVQ_V is an optimal action.

(b) From the guess-and-check table, the gaps are: A: 0.81−0.5=0.310.81 - 0.5 = 0.31; B: 0.9−0.729=0.1710.9 - 0.729 = 0.171; C: 1−0.81=0.191 - 0.81 = 0.19. So Δ=0.171\Delta = 0.171, and (a) requires ∥V−V∗∥∞<0.171/1.8≈0.095\lVert V - V^* \rVert_\infty < 0.171 / 1.8 \approx 0.095. But V2V_2 has error 0.81−0.5=0.310.81 - 0.5 = 0.31 at A, more than three times larger, and its greedy policy is still optimal. The condition is sufficient but not necessary. It guards against the worst case, where all the error lands on exactly the wrong actions. In typical problems the errors are correlated and largely cancel in the comparison between actions. You saw this in the demo and in the lab’s FrozenLake plot.

Exercise 4.7What goes wrong with γ = 1 in a continuing task?Concept●●○

Consider a continuing task (no terminal states) with γ=1\gamma = 1.

(a) Two states, X and Y. One action moves X → Y and Y → X, with reward +1 each step. What does value iteration from V0=0V_0 = 0 produce?

(b) Same states, but the rewards are 0. What are the solutions of the Bellman equation?

(c) Why does the corridor still work with γ=1\gamma = 1? What do people do for genuinely continuing tasks?

Hint
For (a) and (b), write out the Bellman equation for each state with γ=1\gamma = 1 and run two or three sweeps by hand. For (c), ask whether the optimal corridor policy ever runs forever, and what its return is.
Solution

(a) Vk(X)=Vk(Y)=kV_k(X) = V_k(Y) = k: each sweep adds 1. The values diverge, because the return is an infinite sum of +1s. There is no finite fixed point to converge to.

(b) The equations are V(X)=V(Y)V(X) = V(Y) and V(Y)=V(X)V(Y) = V(X), so every vector (c,c)(c, c) is a solution. The fixed point exists but is not unique. Iteration from V0V_0 stays at V0V_0 forever, so the answer depends on where you started. Both failures are what Exercise 4.3(c) predicts: with γ=1\gamma = 1 the operator is not a strict contraction.

(c) In the corridor, every sensible policy reaches an exit and the episode ends, so returns are finite sums. Some policies bounce between A and B forever, but they collect 0 and are never optimal, since rewards are nonnegative and exits are reachable. For episodic problems in which the optimal policy terminates and non-terminating policies are bad (sometimes called stochastic shortest-path problems), value iteration with γ=1\gamma = 1 still converges, though the proof is more delicate. Value iteration on the corridor at γ=1\gamma = 1 gives V∗=(1,1,1)V^* = (1, 1, 1) in 3 sweeps. For truly continuing tasks the standard options are: use γ<1\gamma < 1 (and accept that it changes the objective), or switch to the average-reward formulation, which optimizes the long-run reward per step and uses a “differential” value function relative to that average (Sutton & Barto, section 10.3).

Exercise 4.8Read the demo: risk, patience and hasteExplore the demo●○○

In the demo, use value iteration and press Play after each change (press Restore grid first).

(a) With slip 0, read the value of the cell in row 3, column 1. Explain the number exactly.

(b) Raise slip to 0.3. Which way do the arrows in row 3 (beside the pits) point now? And in the bottom row? Explain both.

(c) Keep slip at 0.3 and set the reward per step to −0.1. What happens to the route, and why?

(d) Set γ to 0.5 (slip 0.1, step reward 0). Why do most cells look almost uncolored, with values near 0?

Solution

(a) 0.656=0.940.656 = 0.9^4. The shortest route is 5 moves (along row 3 to the goal), the +1 arrives on the 5th move, and the reward on move jj is discounted by γj−1\gamma^{j-1}.

(b) Each move along row 3 or row 5 has a 15% chance of slipping sideways into a pit, which costs −1-1 plus the lost goal. The policy now avoids those moves wherever it can.

  • Row 3: four of the five arrows point up. Cell (3,1) leaves the pit-lined row and takes the long route up the left column and along the top row. The detour costs only a few extra steps of discounting. Cell (3,5) steps up out of the row before turning toward the goal. Cells (3,2) and (3,3) sit under the wall, so “up” bumps into it and never moves them up. Its slips, though, move them left or right along the row, and a slip can never send them down into a pit. Only cell (3,4) still risks walking right.
  • Row 5 (bottom): the middle four arrows point down, into the edge. The same trick applies: “down” never succeeds, its sideways slips move you along the row, and you can never slip up into a pit. The agent drifts randomly along the row until it reaches a corner column, where going up is safe.

Pushing into a wall on purpose, so that only harmless slips can move you, is a clever, slightly absurd strategy. The agent discovered it from the equations alone.

(c) The route goes back to running straight along the pits. With a cost of −0.1-0.1 per step, the long detour costs more in step penalties than the pit risk does, so haste wins. Values also become negative far from the goal. The optimal policy depends on the whole reward function, which is why reward design matters so much (Chapter 19 is about what happens when it goes wrong).

(d) With γ=0.5\gamma = 0.5 the effective horizon is about 2 steps. A reward 5 steps away is worth 0.54≈0.060.5^4 \approx 0.06 even with no slipping; with slip it’s about 0.002. Far cells barely “see” the goal, and the agent is very myopic. Values also converge fast, in about 16 sweeps, because the contraction factor is small.

Exercise 4.9Read the demo: contraction and the two algorithmsExplore the demo●●○

(a) With the defaults, press Play. At which sweep do the arrows stop changing, and when does value iteration stop? Is the solid curve of max changes above or below the dashed γk\gamma^k line? Why is that allowed?

(b) Press Random start a few times and play to convergence. What is the same every time, and which theorem promised it?

(c) Set γ to 0.99 and play (raise the speed). Compare the number of sweeps with the bound from the text, which says about 1,375 sweeps are needed to shrink the error by 10−610^{-6}.

(d) Switch to Policy iteration with the defaults and play. Compare iterations and evaluation sweeps with value iteration’s sweep count. Which one did less total work here?

Hint
For (a) and (c), reread the note “Why the demo’s curve falls faster than the dashed line”.
Solution

(a) The arrows freeze at sweep 10, and value iteration stops after 24 sweeps (max change below 10−610^{-6}). The solid curve lies on or below the dashed line. The contraction gives only an upper bound. Here the error shrinks faster, because probability leaks into the terminal goal and pits at every step (the effective rate is γρ\gamma\rho with ρ<1\rho < 1).

(b) The final values and arrows are identical every time. Banach’s theorem says T∗\mathcal{T}^* has a unique fixed point and that iteration reaches it from any start. The first deltas are larger (the random start is far from V∗V^*), but the slope on the log plot is the same.

(c) It takes about 90 sweeps, far fewer than 1,375. The bound assumes the worst case, a state from which you never terminate. Here every good path ends at the goal quickly, so the error decays at the rate γρ\gamma\rho, not γ\gamma. Problems with long or endless trajectories do approach the bound.

(d) Policy iteration stabilizes after 8 iterations, but those iterations use a little over 300 evaluation sweeps in total (each evaluation is warm-started from the previous values and run to a tight tolerance). Value iteration needs 24 sweeps. On this small, quickly-mixing grid, value iteration does less work. Policy iteration wins when evaluation is cheap relative to the number of iterations saved, for example when you can evaluate by an exact linear solve on a modest number of states, or when convergence is slow because γ is near 1. Modified policy iteration, with a few evaluation sweeps per improvement, sits between the two.

Exercise 4.10Code: the three DP algorithmsCode●●○

In the lab (exercises/04-dynamic-programming/), fill in dp.py:

  1. Task 1 q_from_v(P, R, V, gamma): the one-step lookahead.
  2. Task 2 policy_evaluation(P, R, policy, gamma): synchronous sweeps until the max change is below tol.
  3. Task 3 greedy_policy(P, R, V, gamma): one-hot argmax policy.
  4. Task 4 value_iteration(P, R, gamma): returns V, the greedy policy, and the per-sweep max changes.
  5. Task 5 policy_iteration(P, R, gamma): evaluate, improve with the strict-improvement rule, stop when stable.

The tests check the corridor tables from this chapter (including the value-iteration deltas [1.0, 0.9, 0.31, 0.0] and the policy sequence LLL → LLR → LRR → RRR). They also check the contraction property, convergence from a random start, and agreement with a brute-force search over all 256 policies of a tiny slippery grid. Then run python run.py to solve the slippery 8×8 FrozenLake and plot convergence for three values of γ.

Hint
With arrays P of shape (S, A, S) and V of shape (S,), P @ V has shape (S, A). Policy evaluation is then (policy * q_from_v(...)).sum(axis=1), and value iteration replaces the weighted sum with .max(axis=1).
Solution

The reference is in solutions/dp.py. Here are the key lines.

  • Task 1 is one line: return R + gamma * (P @ V).
  • Task 2 loops V_new = (policy * q_from_v(P, R, V, gamma)).sum(axis=1). It computes delta = np.max(np.abs(V_new - V)), replaces V, and breaks when delta < tol. Computing the whole V_new before replacing V is what makes it synchronous. The test test_task2_policy_evaluation_is_synchronous checks that after one sweep only A has a value.
  • Task 3: best = np.argmax(q_from_v(...), axis=1), then set policy[np.arange(S), best] = 1.
  • Task 4 is Task 2 with .max(axis=1) in place of the policy-weighted sum, appending each delta to deltas.
  • Task 5: evaluate; compute Q; current = argmax(policy), best = argmax(Q); switch = Q[rows, best] > Q[rows, current] + 1e-9. If nothing switches, stop; otherwise use np.where(switch, best, current) as the new actions.

run.py with the reference solution reports V∗(start)≈0.415V^*(\text{start}) \approx 0.415 for 8×8 FrozenLake at γ=0.99\gamma = 0.99. Value iteration takes 662 sweeps to reach a change of 10−1010^{-10}, though its greedy policy is optimal from sweep 127. Policy iteration needs 10 evaluations. The middle panel shows the optimal policy often aiming away from holes, preferring to slide along a safe row than to risk the ice next to a hole.

Files: README.md, dp.py, envs.py, test_dp.py, run.py

cd 04-dynamic-programming
python -m pytest -q          # your code: tests fail until you fill in the TODOs
RL_SOLUTIONS=1 python -m pytest -q   # reference solutions: all pass

First time? See Exercises & setup.

Further reading

Next, in Chapter 5, we throw away the model: the agent can only act and observe. You’ll see that TD learning and Q-learning are this chapter’s backups, run on single sampled transitions instead of full expectations.