Notes on Chapter 4 of Shiyu Zhao's Mathematical Foundations of Reinforcement Learning [1].

The last post showed that the Bellman optimality equation (BOE) \(v = \max_\pi (r_\pi + \gamma P_\pi v)\) has a unique solution. This post turns that into three algorithms. They all need the model \(p(r|s,a), p(s'|s,a)\), so they are dynamic programming algorithms, and they are the basis of the model-free methods in the next post.

1. Value iteration

Value iteration is the fixed-point iteration \(v_{k+1} = \max_\pi (r_\pi + \gamma P_\pi v_k)\). By the contraction mapping theorem it converges from any \(v_0\), with \(\|v_k - v^*\|_\infty \le \frac{\gamma^k}{1-\gamma}\|v_1 - v_0\|_\infty\). Each step has two parts:

  1. Policy update: \(\pi_{k+1} = \arg\max_\pi\,(r_\pi + \gamma P_\pi v_k)\).
  2. Value update: \(v_{k+1} = r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_k\).

Elementwise, the policy update maximizes \(\sum_a \pi(a|s)\, q_k(s,a)\) with \(q_k(s,a) = \sum_r p(r|s,a)\,r + \gamma \sum_{s'} p(s'|s,a)\, v_k(s')\). As shown in the last post, the solution is the greedy policy, which puts all its probability on \(\arg\max_a q_k(s,a)\) (ties can be broken arbitrarily). Plugging it into the value update gives \(v_{k+1}(s) = \max_a q_k(s,a)\). One iteration is therefore \(v_k \to q_k \to \pi_{k+1} \to v_{k+1}\):

Algorithm 1: Value iteration

From an initial \(v_0\), until \(\|v_{k+1} - v_k\|_\infty\) is small, for every state \(s\):

  • \(q_k(s,a) = \sum_r p(r|s,a)\,r + \gamma \sum_{s'} p(s'|s,a)\, v_k(s')\) for every action \(a\)
  • policy update: \(\pi_{k+1}(a|s) = 1\) if \(a = \arg\max_a q_k(s,a)\), and \(0\) otherwise
  • value update: \(v_{k+1}(s) = \max_a q_k(s,a)\)

Note that \(v_k\) is not a state value. It converges to \(v^*\), but along the way it need not satisfy the Bellman equation of any policy; it is only an intermediate vector of a numerical method. In the example below, \(v_1 = (0, 1, 1, 1)\), while the policy \(\pi_1\) it produces has the state value \((0, 10, 10, 10)\).

Example. On the \(2\times 2\) grid (\(s_2\) forbidden, \(s_4\) target, \(r_{\text{boundary}} = r_{\text{forbidden}} = -1\), \(r_{\text{target}} = 1\), \(\gamma = 0.9\)), the q-table as a function of \(v\) is

\(q(s,a)\) up right down left stay
\(s_1\) \(-1 + \gamma v(s_1)\) \(-1 + \gamma v(s_2)\) \(0 + \gamma v(s_3)\) \(-1 + \gamma v(s_1)\) \(0 + \gamma v(s_1)\)
\(s_2\) \(-1 + \gamma v(s_2)\) \(-1 + \gamma v(s_2)\) \(1 + \gamma v(s_4)\) \(0 + \gamma v(s_1)\) \(-1 + \gamma v(s_2)\)
\(s_3\) \(0 + \gamma v(s_1)\) \(1 + \gamma v(s_4)\) \(-1 + \gamma v(s_3)\) \(-1 + \gamma v(s_3)\) \(0 + \gamma v(s_3)\)
\(s_4\) \(-1 + \gamma v(s_2)\) \(-1 + \gamma v(s_4)\) \(-1 + \gamma v(s_4)\) \(0 + \gamma v(s_3)\) \(1 + \gamma v(s_4)\)

Starting from \(v_0 = (0, 0, 0, 0)\) and then using \(v_1\), the first two q-tables are (row maxima in bold):

\(q_0(s,a)\), from \(v_0 = (0,0,0,0)\) \(q_1(s,a)\), from \(v_1 = (0,1,1,1)\)
\(s_1\) \(-1\) \(-1\) \(\mathbf{0}\) \(-1\) \(\mathbf{0}\) \(-1\) \(-0.1\) \(\mathbf{0.9}\) \(-1\) \(0\)
\(s_2\) \(-1\) \(-1\) \(\mathbf{1}\) \(0\) \(-1\) \(-0.1\) \(-0.1\) \(\mathbf{1.9}\) \(0\) \(-0.1\)
\(s_3\) \(0\) \(\mathbf{1}\) \(-1\) \(-1\) \(0\) \(0\) \(\mathbf{1.9}\) \(-0.1\) \(-0.1\) \(0.9\)
\(s_4\) \(-1\) \(-1\) \(-1\) \(0\) \(\mathbf{1}\) \(-0.1\) \(-0.1\) \(-0.1\) \(0.9\) \(\mathbf{1.9}\)

(Columns: up, right, down, left, stay.) At \(k = 0\), "down" and "stay" tie at \(s_1\); picking "stay", \(\pi_1\) is not yet optimal. At \(k = 1\), "down" wins at \(s_1\), so \(\pi_2\) is already optimal (Figure 1), while \(v_2 = (0.9, 1.9, 1.9, 1.9)\) is still far from \(v^* = (9, 10, 10, 10)\). In larger problems more iterations are needed, and we stop when \(\|v_{k+1} - v_k\|\) is small.

(a) the grids1s2s3s4(b) π₁s1s2s3s4(c) π₂s1s2s3s4
Figure 1. Value iteration on the \(2\times 2\) grid: the policies \(\pi_1\) and \(\pi_2\).

2. Policy iteration

Policy iteration starts from a policy \(\pi_0\) and repeats

  1. Policy evaluation: solve \(v_{\pi_k} = r_{\pi_k} + \gamma P_{\pi_k} v_{\pi_k}\).
  2. Policy improvement: \(\pi_{k+1} = \arg\max_\pi\,(r_\pi + \gamma P_\pi v_{\pi_k})\).

The evaluation is the problem of the first post: either the closed form \((I - \gamma P_{\pi_k})^{-1} r_{\pi_k}\), or the inner iteration \(v^{(j+1)} = r_{\pi_k} + \gamma P_{\pi_k} v^{(j)}\). So policy iteration has an iterative algorithm nested inside it. In theory the inner loop needs infinitely many steps; in practice it is stopped when \(\|v^{(j+1)} - v^{(j)}\|\) is small. Whether stopping early is harmful is the question of Section 3. First, why does the outer loop work?

Lemma (policy improvement)

\(v_{\pi_{k+1}} \ge v_{\pi_k}\).

Proof. Since \(\pi_{k+1}\) maximizes \(r_\pi + \gamma P_\pi v_{\pi_k}\), it does at least as well as \(\pi_k\): \(v_{\pi_k} = r_{\pi_k} + \gamma P_{\pi_k} v_{\pi_k} \le r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_{\pi_k}\). Subtracting this from \(v_{\pi_{k+1}} = r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_{\pi_{k+1}}\),

\[ (I - \gamma P_{\pi_{k+1}})(v_{\pi_{k+1}} - v_{\pi_k}) \ge 0, \]

and multiplying by \((I - \gamma P_{\pi_{k+1}})^{-1} \ge 0\) (first post) gives the claim. \(\square\) This is the trick at \(s_1\) from the last post, "switch to the action with the largest action value", now applied at all states at once and proven to work.

Theorem (convergence)

Let \(v_k\) be value iteration started from \(v_0 = v_{\pi_0}\). Then \(v_k \le v_{\pi_k} \le v^*\) for all \(k\), so \(v_{\pi_k} \to v^*\).

Proof by induction. Assume \(v_{\pi_k} \ge v_k\), and let \(\pi'\) be the greedy policy for \(v_k\), so \(v_{k+1} = r_{\pi'} + \gamma P_{\pi'} v_k\). Then

\[ v_{\pi_{k+1}} = r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_{\pi_{k+1}} \ge r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_{\pi_k} \ge r_{\pi'} + \gamma P_{\pi'} v_{\pi_k} \ge r_{\pi'} + \gamma P_{\pi'} v_k = v_{k+1}, \]

using the lemma, the maximality of \(\pi_{k+1}\), and the induction hypothesis (each time with \(P \ge 0\)). \(\square\)

So policy iteration is never behind value iteration: \(\|v^* - v_{\pi_k}\|_\infty \le \|v^* - v_k\|_\infty \le \gamma^k \|v^* - v_0\|_\infty\). In a finite MDP it even stops after finitely many steps. If an improvement step does not increase the value, \(v_{\pi_{k+1}} = v_{\pi_k}\), then \(v_{\pi_k} = r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v_{\pi_k} = \max_\pi (r_\pi + \gamma P_\pi v_{\pi_k})\) solves the BOE, so it is \(v^*\). Until then the value strictly increases, so no deterministic policy is visited twice, and there are only finitely many of them.

Algorithm 2: Policy iteration

From an initial policy \(\pi_0\), until \(v_{\pi_k}\) converges:

  • Policy evaluation: iterate \(v^{(j+1)}(s) = \sum_a \pi_k(a|s)\big[\sum_r p(r|s,a)\,r + \gamma\sum_{s'} p(s'|s,a)\, v^{(j)}(s')\big]\) until convergence; this is \(v_{\pi_k}\).
  • Policy improvement: \(q_{\pi_k}(s,a) = \sum_r p(r|s,a)\,r + \gamma\sum_{s'} p(s'|s,a)\, v_{\pi_k}(s')\), and \(\pi_{k+1}\) is greedy in \(q_{\pi_k}\).

The state value is only an intermediate step: we compute \(v_{\pi_k}\) to get \(q_{\pi_k}\), and \(q_{\pi_k}\) to update the policy. This observation is the key to the next post.

12345123450.00.00.00.00.00.0−100.0−100.00.00.00.00.0−100.00.00.00.0−100.010.0−100.00.00.0−100.00.00.00.0(a) π0 and its state values12345123450.00.00.00.00.00.00.00.00.00.00.00.010.00.00.00.010.010.010.00.00.09.010.00.00.0(b) π1 and its state values12345123450.00.00.00.00.00.00.00.00.00.00.00.010.00.00.00.010.010.010.00.00.09.010.09.08.1(c) π3 and its state values12345123453.53.94.34.85.33.13.54.85.35.92.82.510.05.96.62.510.010.010.07.32.39.010.09.08.1(d) π8 and its state values
Figure 2. Policy iteration on the \(5\times 5\) grid world (\(r_{\text{forbidden}} = -10\), \(\gamma = 0.9\)), starting from "always stay". The policy is optimal after eight iterations.

Under \(\pi_0\), a free cell is worth \(0\), a forbidden cell \(-10/(1-\gamma) = -100\), and only the target \(10\). Two patterns are visible. States close to the target find their optimal action first, and the correct region grows outwards: a far state can only find a good path once the states along it already know how to reach the target. And values grow with proximity to the target, because from far away the first positive reward is discounted heavily.

3. Truncated policy iteration

The two algorithms look alike (PE: policy evaluation, PI: policy improvement, PU: policy update, VU: value update):

\[ \begin{aligned} \text{policy iteration:}\quad & \pi_0 \xrightarrow{\text{PE}} v_{\pi_0} \xrightarrow{\text{PI}} \pi_1 \xrightarrow{\text{PE}} v_{\pi_1} \xrightarrow{\text{PI}} \pi_2 \xrightarrow{\text{PE}} \cdots \\ \text{value iteration:}\quad & \phantom{\pi_0 \xrightarrow{\text{PE}}}\ \ v_0 \xrightarrow{\text{PU}} \pi_1' \xrightarrow{\text{VU}} v_1 \xrightarrow{\text{PU}} \pi_2' \xrightarrow{\text{VU}} \cdots \end{aligned} \]

Start both from \(v_0 = v_{\pi_0}\) and compare:

Policy iteration Value iteration
policy \(\pi_1 = \arg\max_\pi (r_\pi + \gamma P_\pi v_{\pi_0})\) \(\pi_1 = \arg\max_\pi (r_\pi + \gamma P_\pi v_0)\), the same
value \(v_{\pi_1} = (I - \gamma P_{\pi_1})^{-1} r_{\pi_1}\) \(v_1 = r_{\pi_1} + \gamma P_{\pi_1} v_0\)
policy \(\pi_2 = \arg\max_\pi (r_\pi + \gamma P_\pi v_{\pi_1})\) \(\pi_2' = \arg\max_\pi (r_\pi + \gamma P_\pi v_1)\)

They differ only in the value step. Write the evaluation of \(\pi_1\) as the inner iteration started from \(v_0\):

\[ \begin{aligned} v^{(1)} &= r_{\pi_1} + \gamma P_{\pi_1} v_0 = v_1 && \longleftarrow \text{value iteration} \\ v^{(j)} &= r_{\pi_1} + \gamma P_{\pi_1} v^{(j-1)} && \longleftarrow \text{truncated policy iteration} \\ v^{(\infty)} &= v_{\pi_1} && \longleftarrow \text{policy iteration} \end{aligned} \]

Value iteration does one inner step and policy iteration infinitely many. Stopping after \(j\) steps, and starting the next evaluation from the last estimate, gives truncated policy iteration. (This step-by-step comparison only works because both start from \(v_0 = v_{\pi_0}\).) Its values are not state values, so does it still converge? A first hint is that the inner steps never decrease the value:

Proposition (value improvement)

If the inner iteration \(v^{(j+1)} = r_{\pi_k} + \gamma P_{\pi_k} v^{(j)}\) starts from \(v^{(0)} = v_{\pi_{k-1}}\), then \(v^{(j+1)} \ge v^{(j)}\) for all \(j\).

Proof. \(v^{(j+1)} - v^{(j)} = \gamma P_{\pi_k}(v^{(j)} - v^{(j-1)}) = \cdots = (\gamma P_{\pi_k})^j (v^{(1)} - v^{(0)})\), and \(v^{(1)} = r_{\pi_k} + \gamma P_{\pi_k} v_{\pi_{k-1}} \ge r_{\pi_{k-1}} + \gamma P_{\pi_{k-1}} v_{\pi_{k-1}} = v^{(0)}\) because \(\pi_k\) is greedy for \(v_{\pi_{k-1}}\). \(\square\)

But truncated policy iteration never computes \(v_{\pi_{k-1}}\) exactly, so this is not yet a proof. The fix is to carry the property the proof actually used along with the algorithm. This is short with the Bellman operators \(T_\pi v = r_\pi + \gamma P_\pi v\) and \(T v = \max_\pi T_\pi v\). Both are monotone: \(u \le w\) implies \(T_\pi u \le T_\pi w\) and \(T u \le T w\).

Theorem (convergence of truncated policy iteration)

Start from \(v_0\) with \(v_0 \le T v_0\) (e.g. \(v_0 = v_{\pi_0}\)). Let \(\pi_{k+1}\) be greedy for \(v_k\) and \(v_{k+1} = T_{\pi_{k+1}}^{\,j} v_k\) with any \(j \ge 1\). Then \(T^k v_0 \le v_k \le v^*\), so \(v_k \to v^*\), at least as fast as value iteration.

Proof. Suppose \(v_k \le T v_k\). Let \(u_i = T_{\pi_{k+1}}^{\,i} v_k\); then \(u_1 = T v_k \ge v_k = u_0\).

  • The inner iterates increase: if \(u_i \ge u_{i-1}\) then \(u_{i+1} = T_{\pi_{k+1}} u_i \ge T_{\pi_{k+1}} u_{i-1} = u_i\). So \(v_{k+1} = u_j \ge u_1 = T v_k\).
  • The assumption is kept: \(T v_{k+1} \ge T_{\pi_{k+1}} u_j = u_{j+1} \ge u_j = v_{k+1}\).
  • Lower bound: by induction, \(v_{k+1} \ge T v_k \ge T(T^k v_0) = T^{k+1} v_0\).
  • Upper bound: \(v \le T v\) implies \(v \le T v \le T^2 v \le \cdots \to v^*\).

Since \(T^k v_0\) is value iteration, which converges to \(v^*\), so does \(v_k\). \(\square\)

The proof also explains why truncation helps. Value iteration applies the greedy policy's update once and moves on; truncated policy iteration applies it \(j\) times, and the bullets above show that each extra application can only raise the estimate towards \(v_{\pi_{k+1}}\), and never past \(v^*\). So more evaluation is never wasted, it only costs time.

With \(j = 1\) this is value iteration, and with \(j \to \infty\) policy iteration. (Truncated policy iteration converges from any \(v_0\); the assumption \(v_0 \le Tv_0\) only keeps the proof short. See Puterman [3, Section 6.5].)

Algorithm 3: Truncated policy iteration

From an initial policy \(\pi_0\) and \(v_0 = v_{\pi_0}\), with a number of inner steps \(j\), until \(v_k\) converges:

  • Policy improvement: \(\pi_{k+1}\) is greedy in \(q_k(s,a) = \sum_r p(r|s,a)\,r + \gamma\sum_{s'} p(s'|s,a)\, v_k(s')\).
  • Truncated evaluation: starting from \(v_k\), apply \(v \leftarrow r_{\pi_{k+1}} + \gamma P_{\pi_{k+1}} v\) \(j\) times; the result is \(v_{k+1}\).
23456024681012141618iteration kmean of vk(s) over all statesoptimal: mean of v*value iteration (j = 1)truncated policy iteration (j = 3)policy iteration (j = ∞)
Figure 3. Mean of \(v_k(s)\) over the 25 states of the grid world of Figure 2, all started from \(v_0 = v_{\pi_0}\) (mean \(-23.6\), off the chart). In this example policy iteration is never below truncated policy iteration, which is never below value iteration.

Each inner step costs about as much as one value-iteration step. Policy iteration needs fewer outer iterations but more work per iteration, and a few inner steps already capture most of the benefit. That is why truncated policy iteration with a small \(j\) is often the most efficient choice.

All three algorithms alternate a value step and a policy step without doing either exactly. This pattern is called generalized policy iteration [2], and most of RL fits into it. What they all still need is the model. Algorithms that work without it are called model-free. (Model-based RL, by contrast, does not mean "given the model"; it means learning a model from data and then planning with it.)

References

[1] Shiyu Zhao. Mathematical Foundations of Reinforcement Learning. Springer Nature Press and Tsinghua University Press, 2025. Chapter 4.

[2] Richard S. Sutton and Andrew G. Barto. Reinforcement Learning: An Introduction, 2nd edition. MIT Press, 2018. Chapter 4.

[3] Martin L. Puterman. Markov Decision Processes: Discrete Stochastic Dynamic Programming. Wiley, 1994. Sections 6.4–6.5.