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

The last post evaluated a given policy. This one asks which policy is best. The answer is the Bellman optimality equation (BOE), and the tool to analyse it is the contraction mapping theorem.

1. Optimal policies and the BOE

In the \(2\times 2\) example of the last post, the action values at \(s_1\) were largest for "down". Switching the policy at \(s_1\) to that action makes it better. This idea, select the action with the greatest action value, is behind most RL algorithms. To make it rigorous we first need to say what "best" means.

Definition (optimal policy)

A policy \(\pi^*\) is optimal if \(v_{\pi^*}(s) \ge v_\pi(s)\) for every state \(s\) and every policy \(\pi\). Its state values are the optimal state values.

This is a strong requirement: a single policy must win at every state simultaneously, and it is not obvious that one exists. Does such a policy exist? Is it unique? Is it deterministic? How do we find it? Intuitively, an optimal policy cannot be improved at any state, so its values should satisfy the Bellman equation with the policy chosen to maximize the right-hand side:

\[ \begin{equation} v(s) = \max_{\pi(\cdot|s)} \sum_a \pi(a\,|\,s)\, q(s,a), \qquad q(s,a) = \sum_r p(r\,|\,s,a)\, r + \gamma \sum_{s'} p(s'\,|\,s,a)\, v(s'). \label{eq:boe} \end{equation} \]

This is the BOE. So far it is only a guess; the rest of the post shows that it is exactly right. Unlike the Bellman equation, it has two unknowns, \(v\) and \(\pi\). The trick is to solve for them one at a time. As a toy example, find \(x, y\) with \(x = \max_y (2x - 1 - y^2)\). Whatever \(x\) is, the maximum is at \(y = 0\) and equals \(2x - 1\), so \(x = 2x - 1\) and \(x = 1\).

In the BOE, likewise, hold \(v\) fixed. Then the \(q(s,a)\) are fixed numbers, and we maximize a weighted average of them. The best weights put everything on the largest number: since \(\sum_a \pi(a|s) = 1\),

\[ \sum_a \pi(a\,|\,s)\, q(s,a) \le \sum_a \pi(a\,|\,s) \max_{a'} q(s, a') = \max_{a} q(s,a), \]

with equality for the greedy policy

\[ \begin{equation} \pi(a\,|\,s) = \begin{cases} 1, & a = a^*(s), \\ 0, & a \neq a^*(s), \end{cases} \qquad a^*(s) = \arg\max_a q(s,a). \label{eq:greedy} \end{equation} \]

After this inner maximization, the right-hand side depends on \(v\) only. In matrix form, with \(r_\pi, P_\pi\) as in the last post,

\[ v = f(v), \qquad f(v) \doteq \max_{\pi}\, (r_\pi + \gamma P_\pi v), \]

where the maximum is elementwise; it is attained by a single policy (the greedy one), because row \(i\) depends only on \(\pi(\cdot|s_i)\). The BOE is a nonlinear equation \(v = f(v)\).

2. The contraction mapping theorem

A point with \(f(x^*) = x^*\) is a fixed point. The map \(f\) is a contraction if, for some \(\gamma \in (0,1)\),

\[ \|f(x_1) - f(x_2)\| \le \gamma\, \|x_1 - x_2\| \qquad \text{for all } x_1, x_2 . \]

Any vector norm works; for the BOE we use \(\|x\|_\infty = \max_i |x_i|\). A vector norm induces the matrix norm \(\|A\| = \max_{x \neq 0} \|Ax\|/\|x\|\), so that \(\|Ax\| \le \|A\|\,\|x\|\). For the max norm it is the largest absolute row sum, \(\|A\|_\infty = \max_i \sum_j |a_{ij}|\). Some examples of contractions:

  • \(f(x) = Ax\) with \(\|A\| \le \gamma < 1\), since \(\|Ax_1 - Ax_2\| \le \|A\|\,\|x_1 - x_2\|\);
  • \(f(x) = 0.5\sin x\), since by the mean value theorem \(|0.5\sin x_1 - 0.5\sin x_2| = |0.5\cos\xi|\,|x_1 - x_2| \le 0.5|x_1 - x_2|\).

Theorem (contraction mapping theorem)

If \(f : \mathbb{R}^d \to \mathbb{R}^d\) is a contraction, then \(x = f(x)\) has a unique solution \(x^*\), and for any \(x_0\) the iteration \(x_{k+1} = f(x_k)\) converges to it, with \(\|x_k - x^*\| \le \frac{\gamma^k}{1-\gamma}\,\|x_1 - x_0\|\).

The theorem gives existence, uniqueness and an algorithm at once. In Figure 1, each step of the iteration goes up to the curve and across to the diagonal, and the staircase closes in on the intersection.

-10123-10123xf(x)y = xy = 0.5 sin x + 1x0x*
Figure 1. The iteration \(x_{k+1} = f(x_k)\) for the contraction \(f(x) = 0.5\sin x + 1\) closes in on the fixed point \(x^* \approx 1.50\), where the curve crosses \(y = x\).

Proof. Since \(x^*\) is not known in advance, we show that \(\{x_k\}\) is a Cauchy sequence: for every \(\varepsilon > 0\) there is an \(N\) with \(\|x_m - x_n\| < \varepsilon\) for all \(m, n > N\). In \(\mathbb{R}^d\) every Cauchy sequence converges. Two remarks:

  • The condition must hold for all pairs \(m, n > N\), not only for neighbours: \(\|x_{k+1} - x_k\| \to 0\) alone is not enough, since \(\sqrt{k}\) and \(\ln(k+1)\) have shrinking steps but diverge.
  • "Cauchy implies convergent" is a property of the space, called completeness. \(\mathbb{R}^d\) is complete, but the rationals are not: \(3, 3.1, 3.14, 3.141, \dots\) is a Cauchy sequence of rationals whose limit \(\pi\) is irrational.

Convergence. By contraction, \(\|x_{k+1} - x_k\| \le \gamma\|x_k - x_{k-1}\| \le \cdots \le \gamma^k \|x_1 - x_0\|\). For \(m > n\), the triangle inequality gives

\[ \|x_m - x_n\| \le \sum_{k=n}^{m-1} \|x_{k+1} - x_k\| \le (\gamma^n + \cdots + \gamma^{m-1})\,\|x_1 - x_0\| \le \frac{\gamma^n}{1-\gamma}\,\|x_1 - x_0\| \longrightarrow 0, \]

so the sequence is Cauchy and converges to some \(x^*\). Letting \(m \to \infty\) gives the error bound. In practice it says that the error falls below \(\varepsilon\) after about \(\log\big(\|x_1 - x_0\| / (\varepsilon(1-\gamma))\big) / \log(1/\gamma)\) steps. For the BOE, \(\gamma\) is the discount rate, so a far-sighted agent (\(\gamma\) close to \(1\)) needs many more iterations: with \(\gamma = 0.99\), each step only removes \(1\%\) of the error.

Fixed point. A contraction is continuous (\(\|f(x_k) - f(x^*)\| \le \gamma\|x_k - x^*\| \to 0\)), so \(f(x^*) = \lim f(x_k) = \lim x_{k+1} = x^*\).

Uniqueness. If \(x'\) is another fixed point, \(\|x' - x^*\| = \|f(x') - f(x^*)\| \le \gamma\|x' - x^*\|\), so \(x' = x^*\). \(\square\)

3. The BOE is a contraction

Theorem

\(\|f(v_1) - f(v_2)\|_\infty \le \gamma\, \|v_1 - v_2\|_\infty\), where \(\|x\|_\infty = \max_i |x_i|\).

Proof. Let \(\pi_1^*, \pi_2^*\) be the greedy policies for \(v_1, v_2\). Each maximizes its own right-hand side, so \(f(v_2) \ge r_{\pi_1^*} + \gamma P_{\pi_1^*} v_2\) and \(f(v_1) \ge r_{\pi_2^*} + \gamma P_{\pi_2^*} v_1\). Hence

\[ \gamma P_{\pi_2^*}(v_1 - v_2) \;\le\; f(v_1) - f(v_2) \;\le\; \gamma P_{\pi_1^*}(v_1 - v_2). \]

(For the upper bound, \(f(v_1) - f(v_2) \le (r_{\pi_1^*} + \gamma P_{\pi_1^*} v_1) - (r_{\pi_1^*} + \gamma P_{\pi_1^*} v_2)\); the lower bound is symmetric.) Let \(p_i^\top\) be row \(i\) of \(P_{\pi_1^*}\). It is a probability vector, so \(p_i^\top(v_1 - v_2)\) is a weighted average of the entries of \(v_1 - v_2\):

\[ |p_i^\top(v_1 - v_2)| \le p_i^\top |v_1 - v_2| \le \|v_1 - v_2\|_\infty , \]

and the same holds for the rows of \(P_{\pi_2^*}\). So every entry of \(f(v_1) - f(v_2)\) is squeezed between two numbers in \([-\gamma\|v_1 - v_2\|_\infty, \gamma\|v_1 - v_2\|_\infty]\). \(\square\)

The discount rate is exactly the contraction factor, and \(\gamma < 1\) really matters. With \(\gamma = 1\), \(P_\pi \mathbf{1} = \mathbf{1}\) gives \(f(v + c\mathbf{1}) = f(v) + c\mathbf{1}\), so if \(v\) solved the BOE, so would \(v + c\mathbf{1}\) for every constant \(c\), and uniqueness would be lost.

4. Solving the BOE

By the contraction mapping theorem, the BOE has a unique solution \(v^*\), and \(v_{k+1} = \max_\pi (r_\pi + \gamma P_\pi v_k)\) converges to it from any \(v_0\). This algorithm is value iteration, studied in the next post. Let \(\pi^* = \arg\max_\pi (r_\pi + \gamma P_\pi v^*)\). Then

\[ v^* = r_{\pi^*} + \gamma P_{\pi^*} v^*, \]

which is the Bellman equation of \(\pi^*\), so \(v^* = v_{\pi^*}\): the BOE is the Bellman equation of a special policy. In the \(2\times 2\) example, \(v^* = (9, 10, 10, 10)\) for \(\gamma = 0.9\), and the greedy policy goes down at \(s_1\), down at \(s_2\), right at \(s_3\) and stays at \(s_4\), exactly the improvement suggested by the action values at the start of the post.

Theorem (optimality)

For every policy \(\pi\), \(v^* = v_{\pi^*} \ge v_\pi\). So \(v^*\) is the optimal state value and \(\pi^*\) is an optimal policy.

Proof. Since \(\pi^*\) attains the maximum, \(v^* \ge r_\pi + \gamma P_\pi v^*\). Subtracting \(v_\pi = r_\pi + \gamma P_\pi v_\pi\),

\[ v^* - v_\pi \ge \gamma P_\pi (v^* - v_\pi) \ge \gamma^2 P_\pi^2 (v^* - v_\pi) \ge \cdots \ge \gamma^n P_\pi^n (v^* - v_\pi) \to 0, \]

using \(P_\pi \ge 0\) at each step, and \(\|\gamma^n P_\pi^n\|_\infty = \gamma^n \to 0\). (Equivalently, \((I - \gamma P_\pi)(v^* - v_\pi) \ge 0\), and \((I - \gamma P_\pi)^{-1} \ge 0\) from the last post.) \(\square\)

With \(q^*(s,a) = \sum_r p(r|s,a)\, r + \gamma \sum_{s'} p(s'|s,a)\, v^*(s')\), the greedy policy \(a^*(s) = \arg\max_a q^*(s,a)\) is optimal. This answers the questions above:

  • Existence: an optimal policy always exists: the greedy policy for the unique solution \(v^*\).
  • Stochastic or deterministic: it can be either, but a deterministic optimal policy always exists.
  • Algorithm: solve the BOE by value iteration, then act greedily with respect to \(q^*\).
  • Uniqueness: \(v^*\) is unique, but the optimal policy need not be. If two actions tie for the maximum of \(q^*(s,\cdot)\), any split between them is optimal, including stochastic ones.

5. What shapes the optimal policy

The BOE depends on the model, the rewards and \(\gamma\). The model is fixed by the environment, so let us vary the other two (Figure 2).

12345123455.85.66.26.55.86.57.28.07.26.57.28.010.08.07.28.010.010.010.08.07.29.010.09.08.1(a) baseline: rforbidden = −1, γ = 0.912345123450.00.00.00.00.00.00.00.00.00.10.00.02.00.10.10.02.02.02.00.20.01.02.01.00.5(b) γ = 0.512345123450.00.00.00.00.00.00.00.00.00.00.00.01.00.00.00.01.01.01.00.00.00.01.00.00.0(c) γ = 012345123453.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) rforbidden = −10, γ = 0.9
Figure 2. Optimal policies and optimal state values on the \(5\times 5\) grid world with \(r_{\text{boundary}} = -1\) and \(r_{\text{target}} = 1\). Where several actions are optimal, one of them is shown.
  • Discount rate. With \(\gamma = 0.9\) (a), the agent walks straight through a forbidden cell next to the target: paying \(-1\) once beats a long detour that delays all future \(+1\)'s. With \(\gamma = 0.5\) (b) it is more short-sighted and avoids every forbidden cell. With \(\gamma = 0\) (c) it only maximizes the immediate reward, so away from the target all zero-reward actions tie and the policy does not lead to the target. In all panels the values decrease with the distance to the target, since a longer path means more discounting.
  • Reward values. With \(r_{\text{forbidden}} = -10\) (d), the agent avoids all forbidden cells.

Only the relative values of rewards matter:

Theorem (invariance)

Replacing every reward \(r\) by \(\alpha r + \beta\) with \(\alpha > 0\) gives the optimal value \(\alpha v^* + \frac{\beta}{1-\gamma}\mathbf{1}\), and the same optimal policies.

Proof. Now \(r_\pi \to \alpha r_\pi + \beta \mathbf{1}\). Plug \(v' = \alpha v^* + c\mathbf{1}\) into the new BOE and use \(P_\pi \mathbf{1} = \mathbf{1}\): \(\max_\pi(\alpha r_\pi + \beta\mathbf{1} + \gamma P_\pi v') = \alpha v^* + (\beta + \gamma c)\mathbf{1}\), which equals \(v'\) when \(c = \beta/(1-\gamma)\). By uniqueness this is the solution. The new action values are \(\alpha q^*(s,a) + \beta/(1-\gamma)\), which have the same maximizing actions. \(\square\)

The condition \(\alpha > 0\) matters: a negative \(\alpha\) turns rewards into costs and the maximum into a minimum. A practical consequence is that making all rewards negative by subtracting a constant does not change what the agent learns.

Finally, since ordinary steps cost nothing, would an optimal agent wander around before reaching the target? Should we add a reward of \(-1\) per step to make it hurry? Neither is needed. On a \(2\times 2\) grid with the target at \(s_4\), compare two ways to leave \(s_2\):

\[ \text{direct } (s_2 \to s_4)\text{: } 1 + \gamma + \gamma^2 + \cdots = \frac{1}{1-\gamma} = 10, \qquad \text{detour } (s_2 \to s_1 \to s_3 \to s_4)\text{: } 0 + 0 + \gamma^2 + \gamma^3 + \cdots = \frac{\gamma^2}{1-\gamma} = 8.1 . \]

The detour is worse although none of its steps is punished: every step of delay multiplies all future rewards by \(\gamma\). And a \(-1\) per step is an affine change of the rewards, so by the invariance theorem it would not change the optimal policy anyway.

References

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

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