Simple Algorithm
Reinforcement Learning · Bootstrapping · 22 min

Value Iteration

Back up the best action until the values settle.

Policy iteration evaluates a whole policy before it changes anything. Value iteration never holds a policy at all: it backs up the best action in every state, sweep after sweep, and the values close in on V⋆V^\star by a factor γ\gamma per sweep. After kk sweeps it has solved the kk-step problem exactly, and its greedy policy is usually optimal long before the values settle, which is why the right question is not "have the values converged?" but "is the policy good enough?"

What you’ll learn

  • Derive the Bellman optimality backup, implement value iteration in state-value and action-value form, and prove that it converges at rate γ\gamma in the max norm.
  • Interpret the kk-th iterate as the optimal kk-step value, and solve finite-horizon problems exactly by backward induction.
  • Run value iteration by hand on the two-state world, distinguishing the actions a backup used from the greedy policy of its result.
  • Prove that stopping when ∥Vk+1−Vk∥∞<ε(1−γ)/(2γ)\norm{V_{k+1}-V_k}_\infty<\varepsilon(1-\gamma)/(2\gamma) makes the greedy policy ε\varepsilon-optimal, and estimate how many sweeps that takes.

Backing up the best action

The optimal values satisfy the Bellman optimality equation: a state is worth the best action's reward plus the discounted value of where it leads. Write the right-hand side as an operator on value tables, the optimality Bellman operator

(T⋆V)(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a) V(s′)],(\mathcal T^\star V)(s)=\max_a\Big[r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)\,V(s')\Big],

so that V⋆V^\star is its fixed point, V⋆=T⋆V⋆V^\star=\mathcal T^\star V^\star. Value iteration applies the operator over and over, Vk+1=T⋆VkV_{k+1}=\mathcal T^\star V_k, from any starting table V0V_0. Each application is one sweep of backups, one per state. No policy is stored anywhere: the max inside every backup does the improving.

DerivationProof: the max is non-expansive
  1. ∣max⁡af(a)−max⁡ag(a)∣≤max⁡a∣f(a)−g(a)∣\Big|\max_af(a)-\max_ag(a)\Big|\le\max_a\big|f(a)-g(a)\big|

    For any two functions of the action, the maxima differ by at most the largest difference: if ff peaks at a1a_1, then max⁡f−max⁡g≤f(a1)−g(a1)\max f-\max g\le f(a_1)-g(a_1), and symmetrically.

  2. ∣(T⋆V)(s)−(T⋆U)(s)∣≤γmax⁡a∣∑s′P(s′∣s,a)(V(s′)−U(s′))∣≤γ∥V−U∥∞\big|(\mathcal T^\star V)(s)-(\mathcal T^\star U)(s)\big|\le\gamma\max_a\Big|\sum_{s'}P(s'\mid s,a)\big(V(s')-U(s')\big)\Big|\le\gamma\norm{V-U}_\infty

    Apply it in state ss, with ff and gg the bracketed lookaheads under VV and UU. The rewards cancel, and what is left is γ\gamma times an average of differences:

  3. ∥Vk−V⋆∥∞=∥T⋆Vk−1−T⋆V⋆∥∞≤γ∥Vk−1−V⋆∥∞≤⋯≤γk∥V0−V⋆∥∞\norm{V_k-V^\star}_\infty=\norm{\mathcal T^\star V_{k-1}-\mathcal T^\star V^\star}_\infty\le\gamma\norm{V_{k-1}-V^\star}_\infty\le\cdots\le\gamma^k\norm{V_0-V^\star}_\infty

    Take the maximum over ss. Two fixed points would be at most γ\gamma times their own distance apart, so there is at most one, and the Banach fixed-point theorem supplies it (Bellman Operators & Contraction). Since V⋆=T⋆V⋆V^\star=\mathcal T^\star V^\star, every sweep shrinks the error by at least γ\gamma:

python
import numpy as np

def value_iteration(P, r, gamma, eps=1e-3):
    """P[s, a, s2] transition probabilities, r[s, a] expected rewards.
    Returns values and a greedy policy that is eps-optimal."""
    V = np.zeros(r.shape[0])
    while True:
        Q = r + gamma * P @ V                 # S x A one-step lookaheads
        V_new = Q.max(axis=1)                 # the backup: best action everywhere
        if np.abs(V_new - V).max() < eps * (1 - gamma) / (2 * gamma):
            return V_new, (r + gamma * P @ V_new).argmax(axis=1)
        V = V_new

A sweep costs O(S2A)\order{S^2A} with a dense model, or O(SAb)\order{SAb} when each state–action pair has at most bb successors. The stopping test is derived in When to stop; the policy returned is greedy for the latest values.

Worked example: the two-state world

Take the two-state world of the earlier lessons with γ=0.9\gamma=0.9: in s1s_1, stay pays 1 and stays, go pays 0 and moves to s2s_2; in s2s_2, stay pays 2 and stays, go pays 0 and moves to s1s_1. Start from V0=(0,0)V_0=(0,0). The first backup gives V1=(1,2)V_1=(1,2): with nothing known about the future, the immediate rewards decide.

Backup kkVkV_kIn s1s_1: stay / go, using Vk−1V_{k-1}Action used in s1s_1Greedy policy for VkV_k
1(1, 2)(1,\ 2)1 / 0stay(stay, stay)
2(1.9, 3.8)(1.9,\ 3.8)1.9 / 1.8stay(go, stay)
3(3.42, 5.42)(3.42,\ 5.42)2.71 / 3.42go(go, stay)
4(4.878, 6.878)(4.878,\ 6.878)4.078 / 4.878go(go, stay)
∞\infty(18, 20)(18,\ 20)17.2 / 18go(go, stay)
In s2s_2, stay wins every backup. The second backup computes V2V_2 with stay in s1s_1, because it compares the actions using V1V_1. Yet the policy greedy for V2V_2 already goes, since 0.9×3.8=3.420.9\times3.8=3.42 beats 1+0.9×1.9=2.711+0.9\times1.9=2.71. The greedy policy is optimal from the second backup on, while ∥V2−V⋆∥∞\norm{V_2-V^\star}_\infty is still 16.2.
Think first Every backup comes with two policies: the actions that attained the max while computing Vk+1V_{k+1}, and the greedy policy for the new values Vk+1V_{k+1}. How are they related?

The actions used in backup k+1k+1 are exactly the greedy policy for VkV_k, one table behind. Backup 2 used stay in s1s_1 because stay is greedy for V1V_1; the greedy policy for V2V_2 is go, and backup 3 uses it. Stopping rules certify one of these two policies at a time, so it pays to say which one you mean (Error Bounds & Stopping Rules bounds both).

Predict before you look

From V0=(0,0)V_0=(0,0) the distance to V⋆=(18,20)V^\star=(18,20) is 20, and here it shrinks by exactly the factor 0.9 per backup. After how many backups is it below 0.01 for the first time?

Show the answer

73

Solve 20×0.9k<0.0120\times0.9^k<0.01: k>ln⁡2000/ln⁡(1/0.9)≈72.1k>\ln2000/\ln(1/0.9)\approx72.1, so 73 backups. In s2s_2, stay is used throughout, so Vk(s2)=20(1−0.9k)V_k(s_2)=20(1-0.9^k) loses exactly the factor 0.9 per backup. The policy was right after 2.

Looking k steps ahead

What is VkV_k? Unrolling the backups answers it exactly: Vk(s)V_k(s) is the largest expected discounted reward obtainable in kk steps from ss, with V0V_0 paid as a terminal value after the last step. For k=1k=1 this is the best immediate reward plus γV0\gamma V_0. If VkV_k is the best kk-step value from every state, then the best (k+1)(k+1)-step plan chooses its first action well and continues optimally from wherever it lands, which is worth (T⋆Vk)(s)=Vk+1(s)(\mathcal T^\star V_k)(s)=V_{k+1}(s). Bellman's principle of optimality is this induction.

Steps to go hhQh(s1,stay)Q_h(s_1,\text{stay})Qh(s1,go)Q_h(s_1,\text{go})Best first action in s1s_1
110stay
21.91.8stay
32.713.42go
44.0784.878go
The two-state world (γ=0.9\gamma=0.9) as a finite-horizon problem: the lookaheads of the previous table, read as a policy that depends on the time left. With one or two steps left, moving to s2s_2 cannot repay the step of zero reward it costs; with three or more it can. In s2s_2, stay is best at every horizon.
Check your understanding

An agent in s1s_1 has exactly two steps left, with γ=0.9\gamma=0.9. Why is stay optimal, although s2s_2 pays twice as much per step?

  1. Going earns 0 now and a single 2 one step later, worth 0.9×2=1.80.9\times2=1.8, against 1+0.9×1=1.91+0.9\times1=1.9 for staying twice
  2. With γ<1\gamma<1, rewards two steps away are worth nothing
  3. In finite-horizon problems the greedy action is always the one with the larger immediate reward
  4. It is not: go is optimal at every horizon, as in the discounted problem
Show the answer

A. Going earns 0 now and a single 2 one step later, worth 0.9×2=1.80.9\times2=1.8, against 1+0.9×1=1.91+0.9\times1=1.9 for staying twice

With two steps left, going spends one of them earning nothing and leaves only one step in s2s_2. The comparison flips at three steps left (2.712.71 against 3.423.42), which is why finite-horizon optimal policies depend on the time remaining.

When to stop

Values converge only in the limit, so value iteration needs a stopping rule, and the useful rule speaks about the policy you will act with. Write δk=∥Vk+1−Vk∥∞\delta_k=\norm{V_{k+1}-V_k}_\infty for the largest change in a sweep. Because the changes shrink geometrically, the one you just saw bounds all the changes still to come:

∥Vk+1−V⋆∥∞≤γ1−γ δk\norm{V_{k+1}-V^\star}_\infty\le\frac{\gamma}{1-\gamma}\,\delta_k
The a posteriori bound, as in Policy Evaluation: ∥Vk+1−V⋆∥∞≤γ∥Vk−V⋆∥∞≤γδk+γ∥Vk+1−V⋆∥∞\norm{V_{k+1}-V^\star}_\infty\le\gamma\norm{V_k-V^\star}_\infty\le\gamma\delta_k+\gamma\norm{V_{k+1}-V^\star}_\infty, then solve for the error.
DerivationProof
  1. ∥Vπ−Vk+1∥∞≤∥TπVπ−TπVk+1∥∞+∥T⋆Vk+1−T⋆Vk∥∞≤γ∥Vπ−Vk+1∥∞+γδk\norm{V^\pi-V_{k+1}}_\infty\le\norm{\mathcal T^\pi V^\pi-\mathcal T^\pi V_{k+1}}_\infty+\norm{\mathcal T^\star V_{k+1}-\mathcal T^\star V_k}_\infty\le\gamma\norm{V^\pi-V_{k+1}}_\infty+\gamma\delta_k

    Greediness means TπVk+1=T⋆Vk+1\mathcal T^\pi V_{k+1}=\mathcal T^\star V_{k+1}, and Vk+1=T⋆VkV_{k+1}=\mathcal T^\star V_k. Compare VπV^\pi, the fixed point of Tπ\mathcal T^\pi, with Vk+1V_{k+1} by inserting TπVk+1\mathcal T^\pi V_{k+1}:

  2. ∥V⋆−Vπ∥∞≤∥V⋆−Vk+1∥∞+∥Vk+1−Vπ∥∞≤2γ δk1−γ<ε\norm{V^\star-V^\pi}_\infty\le\norm{V^\star-V_{k+1}}_\infty+\norm{V_{k+1}-V^\pi}_\infty\le\frac{2\gamma\,\delta_k}{1-\gamma}<\varepsilon

    So the policy's true values lie within γδk/(1−γ)\gamma\delta_k/(1-\gamma) of Vk+1V_{k+1}, and by the a posteriori bound so does V⋆V^\star. The triangle inequality finishes the proof:

The rule is a certificate, not a detector. In the two-state world with ε=0.1\varepsilon=0.1 the threshold is 0.1×0.1/1.8≈0.00560.1\times0.1/1.8\approx0.0056, first met at backup 57, while the greedy policy has been optimal since backup 2. The guarantee is worst-case, and it holds in every MDP.

How many sweeps does it take? From V0=0V_0=0 with ∣r∣≤Rmax⁡\abs{r}\le R_{\max}, the starting error is at most Rmax⁡/(1−γ)R_{\max}/(1-\gamma), so the a priori bound gives an ε\varepsilon-accurate table after about 11−γln⁡Rmax⁡ε(1−γ)\frac{1}{1-\gamma}\ln\frac{R_{\max}}{\varepsilon(1-\gamma)} sweeps, using ln⁡1γ≥1−γ\ln\frac1\gamma\ge1-\gamma. The effective horizon sets the price: γ=0.99\gamma=0.99 needs roughly ten times as many sweeps as γ=0.9\gamma=0.9. Policy iteration needs far fewer iterations, each costing a linear solve (Policy Iteration).

Check your understanding

Value iteration with γ=0.9\gamma=0.9 stops after a sweep whose largest change is 0.010.01. What does the proof above guarantee about the greedy policy for the final values?

  1. It loses at most 0.010.01 in any state
  2. It loses at most 0.090.09 in any state
  3. It loses at most 0.180.18 in any state
  4. Nothing, until a sweep changes no value at all
Show the answer

C. It loses at most 0.180.18 in any state

∥V⋆−Vπ∥∞≤2γδ/(1−γ)=2×0.9×0.01/0.1=0.18\norm{V^\star-V^\pi}_\infty\le2\gamma\delta/(1-\gamma)=2\times0.9\times0.01/0.1=0.18: the factor γ/(1−γ)=9\gamma/(1-\gamma)=9 covers the changes still to come, and it appears twice because V⋆V^\star and VπV^\pi can lie on opposite sides of Vk+1V_{k+1}. A sharper analysis in Error Bounds & Stopping Rules improves this to 2γ2δ/(1−γ)≈0.162\gamma^2\delta/(1-\gamma)\approx0.16 for this policy.

Variations on the backup

Action values. Value iteration can store QQ instead of VV:

Qk+1(s,a)=r(s,a)+γ∑s′P(s′∣s,a)max⁡a′Qk(s′,a′).Q_{k+1}(s,a)=r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)\max_{a'}Q_k(s',a').

Started from Q0=r+γPV0Q_0=r+\gamma PV_0 it produces max⁡aQk(s,a)=Vk+1(s)\max_aQ_k(s,a)=V_{k+1}(s), so it converges at the same rate, at the cost of SASA numbers of memory instead of SS. What it buys is that acting greedily, π(s)∈arg max⁡aQ(s,a)\pi(s)\in\argmax_aQ(s,a), no longer needs the model. Replace the expectation by one sampled transition and you get Q-learning (SARSA & Q-Learning).

Other orders. Overwriting values in place, or backing up states in any order that visits each one infinitely often, also converges to V⋆V^\star. Asynchronous dynamic programming uses this freedom to put the backups where they matter (Asynchronous & Prioritized Dynamic Programming).

Between value and policy iteration. A value-iteration backup is one sweep of evaluation for the policy that is greedy right now, so value iteration is generalized policy iteration with the least possible evaluation; modified policy iteration does mm sweeps per improvement (Modified & Generalized Policy Iteration).

SettingWhat the iterates meanConvergence
Finite horizon HH, any γ≤1\gamma\le1VhV_h is the optimal value with hh steps to goExact after HH backups; the optimal policy depends on hh
Discounted, γ<1\gamma<1Optimal kk-step values with terminal value V0V_0∥Vk−V⋆∥∞≤γk∥V0−V⋆∥∞\norm{V_k-V^\star}_\infty\le\gamma^k\norm{V_0-V^\star}_\infty from any start
Episodic, γ=1\gamma=1Optimal values of reaching a terminal stateConverges if some policy terminates with probability 1 from every state and every policy that does not has value −∞-\infty somewhere (Bertsekas & Tsitsiklis, 1996)
Average rewardVkV_k grows like kk times the optimal gainRelative value iteration subtracts a reference value each sweep (Average-Reward Dynamic Programming)
The same loop in every setting; what changes is what the iterates mean and when they converge.

Deeper: the Fixed Point lessons

Key takeaways

  • Value iteration applies the optimality backup to every state, sweep after sweep. The max is non-expansive, so T⋆\mathcal T^\star is a γ\gamma-contraction and ∥Vk−V⋆∥∞≤γk∥V0−V⋆∥∞\norm{V_k-V^\star}_\infty\le\gamma^k\norm{V_0-V^\star}_\infty.
  • VkV_k is the optimal kk-step value with terminal value V0V_0: finite-horizon problems are solved exactly by HH backups (backward induction), with a policy that depends on the steps remaining.
  • The greedy policy usually settles long before the values: after 2 backups against 73 in the two-state world. The actions used by backup k+1k+1 are greedy for VkV_k, one table behind.
  • Stopping when ∥Vk+1−Vk∥∞<ε(1−γ)/(2γ)\norm{V_{k+1}-V_k}_\infty<\varepsilon(1-\gamma)/(2\gamma) makes the greedy policy ε\varepsilon-optimal; reaching that takes about 11−γln⁡Rmax⁡ε(1−γ)\frac{1}{1-\gamma}\ln\frac{R_{\max}}{\varepsilon(1-\gamma)} sweeps.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Value iteration on an MDP with γ=0.95\gamma=0.95 starts with ∥V0−V⋆∥∞=100\norm{V_0-V^\star}_\infty=100. What does the contraction property guarantee after 20 sweeps?

  1. ∥V20−V⋆∥∞≤100×0.9520≈35.8\norm{V_{20}-V^\star}_\infty\le100\times0.95^{20}\approx35.8
  2. ∥V20−V⋆∥∞≤100−20×0.95=81\norm{V_{20}-V^\star}_\infty\le100-20\times0.95=81
  3. V20=V⋆V_{20}=V^\star, because 20 sweeps reach the effective horizon
  4. ∥V20−V⋆∥∞≤5\norm{V_{20}-V^\star}_\infty\le5
Show the answer

A. ∥V20−V⋆∥∞≤100×0.9520≈35.8\norm{V_{20}-V^\star}_\infty\le100\times0.95^{20}\approx35.8

Each sweep multiplies the max-norm error by at most γ\gamma. With γ\gamma close to 1, value iteration needs many multiples of the effective horizon 1/(1−γ)=201/(1-\gamma)=20: about 20ln⁡(100/ε)20\ln(100/\varepsilon) sweeps for an error ε\varepsilon.

Question 2

A problem ends after exactly H=10H=10 steps. What does value iteration from V0=0V_0=0 deliver after 10 backups?

  1. An approximation whose error is about γ10\gamma^{10} times the initial error
  2. One stationary policy that is optimal at every step
  3. The exact optimal values, and an optimal policy that may depend on how many steps remain
  4. Nothing reliable unless γ<1\gamma<1
Show the answer

C. The exact optimal values, and an optimal policy that may depend on how many steps remain

VhV_h is the optimal value with hh steps to go, so 10 backups give the exact answer, and πh\pi_h, greedy for Vh−1V_{h-1}, is the optimal action with hh steps left. In the two-state world it stays in s1s_1 with one or two steps left and goes with three or more.

Question 3

Why run value iteration on action values QQ instead of state values VV, at AA times the memory?

  1. It converges at a faster rate
  2. The greedy policy can then be read off without the model, which is also what lets sampled versions such as Q-learning work
  3. It avoids the max over actions
  4. It is required when γ=1\gamma=1
Show the answer

B. The greedy policy can then be read off without the model, which is also what lets sampled versions such as Q-learning work

With QQ stored, arg max⁡aQ(s,a)\argmax_aQ(s,a) needs no transition probabilities, so an agent can act, and learn from sampled transitions, without a model. The rate is the same γ\gamma, because max⁡aQk=Vk+1\max_aQ_k=V_{k+1}.

Question 4

Backup k+1k+1 computes Vk+1(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a)Vk(s′)]V_{k+1}(s)=\max_a\big[r(s,a)+\gamma\sum_{s'}P(s'\mid s,a)V_k(s')\big]. Which policy do the actions attaining these maxima form?

  1. The greedy policy for Vk+1V_{k+1}
  2. An optimal policy, once k≥1k\ge1
  3. The policy evaluated by the previous sweep
  4. The greedy policy for VkV_k
Show the answer

D. The greedy policy for VkV_k

The max compares actions using VkV_k, so its maximizers are greedy for VkV_k, one table behind the new values. In the two-state world, backup 2 used stay in s1s_1 (greedy for V1V_1), while the greedy policy for V2V_2 already goes.

Go deeper

Further reading

  • R. Bellman, Dynamic Programming (Princeton University Press, 1957).
  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 4 (MIT Press, 2nd ed., 2018).
  • M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 4 and 6 (Wiley, 1994).
  • D. P. Bertsekas & J. N. Tsitsiklis, Neuro-Dynamic Programming, ch. 2 (Athena Scientific, 1996).