"Ask me what a state is worth and I will not answer quickly. I am summing, right now, over every future that could unfold from here: every action you might take, every coin the world might flip, every reward waiting down each branch, discounted by how far off it sits. Give me a moment. I am imagining all of it, patiently, so that you do not have to."
A Value Function Patiently Summing Up Every Future It Can Imagine
A Markov decision process gives you the rules of a sequential game; this section gives you the machinery to play it well. Two objects do all the work. A policy $\pi(a \mid s)$ is the agent's behavior, a rule mapping each state to a distribution over actions. A value function scores that behavior: $V^\pi(s)$ is the expected total discounted reward you collect by following $\pi$ from state $s$, and $Q^\pi(s,a)$ is the expected return if you first take action $a$ and follow $\pi$ thereafter. The single structural fact that makes everything computable is the Bellman equation: because the future after one step is itself just another MDP problem, value satisfies a one-step recursive consistency, today's value equals immediate reward plus discounted value of where you land. That recursion comes in two flavors. The Bellman expectation equations describe the value of a fixed policy and let you evaluate it. The Bellman optimality equations describe the value of the best policy and define what the agent is ultimately solving for. A short bridge, the policy-improvement theorem, connects them: acting greedily with respect to any policy's value function never makes things worse, which is the engine that powers the dynamic programming of Section 22.4 and, eventually, every reinforcement-learning algorithm in this part. This section defines policies and values precisely, derives both Bellman systems with full notation, proves the improvement step, and then evaluates a concrete five-state MDP three ways: by iterating the Bellman expectation operator from scratch, by solving the same equations as a linear system, and via an MDP toolkit in a few lines.
In Section 22.2 we built the Markov decision process itself: the tuple $(\mathcal{S}, \mathcal{A}, P, R, \gamma)$ of states, actions, a transition kernel $P(s' \mid s, a)$, a reward function $R(s,a)$, and a discount factor $\gamma$, together with the Markov property that the next state depends only on the current state and action, not on the path that led there. That section described the world; it did not yet describe the agent, nor what it means for the agent to behave well. This section supplies both. It is the conceptual heart of the chapter: once you can write down what a policy is, score it with a value function, and characterize the value of the best policy through the Bellman equations, every subsequent algorithm, value iteration and policy iteration in Section 22.4, the temporal-difference and Q-learning methods of Chapter 24, the deep value networks of Chapter 25, is a way of computing or approximating one of the objects defined here. We use the unified notation of Appendix A throughout: $G_t$ the return, $\pi$ the policy, $V^\pi$ and $Q^\pi$ the value functions, $V^\ast$ and $Q^\ast$ their optimal counterparts.
The recurrence at the center of this section is one the reader of this book has met before in a different costume. The Bellman expectation equation, value today equals reward now plus discounted value of the next state, is the exact decision-making cousin of the filtering recursions of Chapter 7, where a belief today was updated into a belief tomorrow by a one-step recursion. There the recursion propagated probability forward in time; here it propagates value backward from the future. The Markov property is what licenses both: when the future depends on the present alone, a problem over an infinite horizon collapses into a one-step relationship between adjacent times. That collapse, the same collapse that made the Kalman filter tractable, is what makes sequential decision making tractable, and it is the through-line of this entire part.
The four competencies this section installs are these: to write a policy as a deterministic map or a stochastic distribution $\pi(a \mid s)$ and state the objective as maximizing expected return; to define $V^\pi$ and $Q^\pi$ as expected returns and explain the division of labor between scoring a situation and scoring a move; to derive and read both the Bellman expectation and Bellman optimality equations, and to recognize $V^\ast$ and the greedy optimal policy; and to evaluate a fixed policy in practice, both by iterating the Bellman operator and by solving the linear system, and to recognize the library call that does it for you. These are the load-bearing skills for the rest of Part VI.
1. Policies and the Objective: Behavior and What It Maximizes Beginner
An agent's behavior is captured entirely by its policy, a rule that tells it which action to take in each state. The simplest form is a deterministic policy, a function $\pi : \mathcal{S} \to \mathcal{A}$ that names exactly one action per state, written $a = \pi(s)$. More generally a stochastic policy specifies a probability distribution over actions in each state, written $\pi(a \mid s)$, the probability of choosing action $a$ when in state $s$, with $\sum_{a} \pi(a \mid s) = 1$. A deterministic policy is the special case that puts all its probability mass on a single action. Stochasticity is not a mere technical convenience: it is how an agent explores alternatives it is unsure about (the central theme of the bandits in Chapter 24), and in the partially observed and game-theoretic settings of Chapter 23 the optimal policy can be genuinely random.
Why distinguish the two forms so carefully at the outset? Because a deep and convenient fact about fully observed MDPs, which we will see fall out of the Bellman optimality equation in subsection three, is that there always exists an optimal policy that is deterministic. The agent never needs to randomize to act optimally when it can see the full state. Stochastic policies still matter enormously: they are the objects we differentiate in the policy-gradient methods of Chapter 25, and they are indispensable during learning when the agent does not yet know which action is best. But the target the agent is aiming at, the optimal policy, can be taken to be a clean deterministic map, which is why so much of the dynamic programming in Section 22.4 searches over deterministic policies without loss.
What does the agent want? It wants to maximize the return, the total reward it accumulates over time, with future rewards discounted by $\gamma \in [0,1)$ so that a reward $k$ steps away is worth $\gamma^k$ of an immediate one. The return from time $t$ is the discounted sum
$$G_t \;=\; R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots \;=\; \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}.$$The discount factor does double duty. Mathematically, with bounded rewards and $\gamma < 1$ the infinite sum converges (bounded by $R_{\max}/(1-\gamma)$), so the objective is well defined even over an unbounded horizon. Behaviorally, $\gamma$ encodes how far-sighted the agent is: near $0$ it is myopic and grabs immediate reward, near $1$ it is patient and plans for the long term. Because $G_t$ is a random variable (the trajectory depends on the policy's randomness and the environment's transitions), the agent maximizes its expectation. The objective, for a start distribution $s_0 \sim d_0$, is
$$\max_{\pi} \; J(\pi) \;=\; \mathbb{E}_{\pi}\!\left[\, G_0 \,\right] \;=\; \mathbb{E}_{\pi}\!\left[\, \sum_{k=0}^{\infty} \gamma^k R_{k+1} \,\;\middle|\;\, s_0 \sim d_0 \right],$$where the subscript $\pi$ on the expectation means actions are drawn from $\pi$ and states evolve under $P$. This single scalar, expected discounted return, is the quantity every algorithm in Part VI is ultimately trying to make large. Everything that follows, value functions and Bellman equations alike, is apparatus for reasoning about and optimizing $J(\pi)$.
Three nouns organize the whole of sequential decision making. The policy $\pi(a \mid s)$ is what the agent does. The return $G_t = \sum_k \gamma^k R_{t+k+1}$ is what a single trajectory earns. The objective $J(\pi) = \mathbb{E}_\pi[G_0]$ is the average earning the agent is trying to maximize. The agent does not optimize any one trajectory (it cannot control the world's coin flips); it optimizes the expectation over all trajectories its policy and the environment jointly produce. The value functions of the next subsection are nothing but this same expected return, conditioned on where the agent currently stands. Hold "behavior, score, expected score" firmly and the rest of the chapter is bookkeeping over these three.
2. Value Functions: Scoring a Situation and Scoring a Move Beginner
The objective $J(\pi)$ is a single number summarizing a whole policy. To reason about where a policy is doing well and to improve it locally, we need a quantity attached to each state. That quantity is the state-value function $V^\pi$, the expected return from a given state when following $\pi$ thereafter:
$$V^\pi(s) \;=\; \mathbb{E}_{\pi}\!\left[\, G_t \,\;\middle|\;\, S_t = s \,\right] \;=\; \mathbb{E}_{\pi}\!\left[\, \sum_{k=0}^{\infty} \gamma^k R_{t+k+1} \,\;\middle|\;\, S_t = s \,\right].$$Read $V^\pi(s)$ as a score for a situation: it answers "how good is it to be in state $s$, given that I will act according to $\pi$ from here on?" A high-value state is one from which the policy can expect to collect a lot of discounted future reward; a low-value state is a poor place to find yourself. Closely related, and often more directly useful, is the action-value function $Q^\pi$, the expected return from taking a specific action $a$ in state $s$ and only then following $\pi$:
$$Q^\pi(s,a) \;=\; \mathbb{E}_{\pi}\!\left[\, G_t \,\;\middle|\;\, S_t = s,\, A_t = a \,\right].$$Read $Q^\pi(s,a)$ as a score for a move: it answers "how good is it to take action $a$ right now in state $s$, if I then revert to $\pi$?" The two functions are tied together by a pair of relations that will recur constantly. Averaging $Q^\pi$ over the policy's action distribution recovers $V^\pi$, and expanding one step of reward-then-next-state inside $Q^\pi$ expresses it through $V^\pi$:
$$V^\pi(s) \;=\; \sum_{a} \pi(a \mid s)\, Q^\pi(s,a), \qquad Q^\pi(s,a) \;=\; R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\pi(s').$$The first relation says the value of a situation is the policy-weighted average of the values of the moves available in it. The second says the value of a move is the immediate reward plus the discounted value of wherever that move lands you, averaged over the environment's transition randomness. This second relation is the conversion that the numeric example below exercises, and it is the seed of the Bellman equations of subsection three.
The single most useful intuition for telling the two value functions apart: $V^\pi(s)$ scores a situation (how good is standing here?), while $Q^\pi(s,a)$ scores a move (how good is doing this from here?). The practical consequence is decisive. If you know only $V$, picking the best action requires a one-step lookahead through the model $P$ and $R$, because you must imagine where each action leads. If you know $Q$, picking the best action is immediate: just take $\arg\max_a Q(s,a)$, no model needed. That model-free greediness is exactly why the learning methods of Chapter 24 and the deep agents of Chapter 25 learn $Q$ rather than $V$: $Q$ already contains the comparison between actions that $V$ leaves implicit.
Consider a state $s$ and an action $a$ with immediate reward $R(s,a) = 2$, discount $\gamma = 0.9$, and a transition that lands in state $s_1$ with probability $0.7$ or state $s_2$ with probability $0.3$. Suppose the current state-value estimates are $V^\pi(s_1) = 10$ and $V^\pi(s_2) = -5$. Then the action-value is the immediate reward plus the discounted expected next-state value:
$Q^\pi(s,a) = R(s,a) + \gamma \big[ P(s_1 \mid s,a)\,V^\pi(s_1) + P(s_2 \mid s,a)\,V^\pi(s_2) \big] = 2 + 0.9\,[\,0.7 \cdot 10 + 0.3 \cdot (-5)\,] = 2 + 0.9\,[\,7 - 1.5\,] = 2 + 0.9 \cdot 5.5 = 2 + 4.95 = 6.95.$
The move is worth $6.95$: two units of immediate reward, plus $4.95$ of discounted future value, weighted toward the good state $s_1$ it usually reaches. If a sibling action $a'$ in the same state scored, say, $Q^\pi(s,a') = 5.4$, then a greedy agent would prefer $a$, and the gap $6.95 - 5.4 = 1.55$ is exactly the kind of advantage the policy-improvement theorem of subsection four exploits. This one calculation, immediate reward plus discounted averaged next-state value, is the atom from which every Bellman equation in this section is built.
3. The Bellman Equations: Expectation and Optimality Intermediate
The defining structural property of value functions is that they are self-consistent across one step of time. The return from now decomposes into the reward earned on the next step plus the discounted return from the state you transition into, $G_t = R_{t+1} + \gamma G_{t+1}$. Taking expectations of both sides, conditioned on the current state and the policy, turns this trivial identity into the Bellman expectation equation for $V^\pi$, a recursion that ties the value of each state to the values of its successors:
$$V^\pi(s) \;=\; \sum_{a} \pi(a \mid s) \left[\, R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\pi(s') \,\right].$$The same one-step decomposition written for the action-value function gives the Bellman expectation equation for $Q^\pi$, where the successor's value is itself averaged over the policy's next action:
$$Q^\pi(s,a) \;=\; R(s,a) + \gamma \sum_{s'} P(s' \mid s,a) \sum_{a'} \pi(a' \mid s')\, Q^\pi(s',a').$$These are not approximations or update rules; they are exact equalities that the true value of the policy $\pi$ must satisfy at every state. For a finite MDP with $n$ states, the $V^\pi$ system is $n$ linear equations in the $n$ unknowns $V^\pi(s)$, and that linearity is exactly what subsection five exploits to solve it in closed form. The expectation equations are the foundation of policy evaluation: given a fixed $\pi$, find the $V^\pi$ that satisfies them.
Evaluation, though, is not the agent's goal; optimization is. We want the best achievable value, the optimal state-value function $V^\ast(s) = \max_\pi V^\pi(s)$ and the optimal action-value function $Q^\ast(s,a) = \max_\pi Q^\pi(s,a)$. These satisfy their own self-consistency, the Bellman optimality equations, in which the policy-weighted average over actions is replaced by a $\max$ over actions, because an optimal agent does not average over what it might do, it takes the best:
$$V^\ast(s) \;=\; \max_{a} \left[\, R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\ast(s') \,\right],$$ $$Q^\ast(s,a) \;=\; R(s,a) + \gamma \sum_{s'} P(s' \mid s,a) \, \max_{a'} Q^\ast(s',a').$$The single structural change from expectation to optimality, replacing $\sum_a \pi(a\mid s)$ by $\max_a$, has a large consequence: the optimality equations are nonlinear (the $\max$ is not a linear operation), so they cannot be solved by the linear-system method that works for a fixed policy. They are solved instead by iterative dynamic programming, value iteration and policy iteration, which is the entire subject of Section 22.4. Once $V^\ast$ or $Q^\ast$ is known, the optimal policy is read off by acting greedily: in every state pick the action that attains the max,
$$\pi^\ast(s) \;=\; \arg\max_{a} \left[\, R(s,a) + \gamma \sum_{s'} P(s' \mid s,a)\, V^\ast(s') \,\right] \;=\; \arg\max_{a} Q^\ast(s,a).$$That this greedy policy is deterministic is the formal source of the claim from subsection one: a fully observed MDP always admits a deterministic optimal policy. The $Q^\ast$ form makes the greedy choice especially clean, $\pi^\ast(s) = \arg\max_a Q^\ast(s,a)$ uses no model at all, which is the structural reason value-based reinforcement learning targets $Q$.
| Property | Bellman expectation | Bellman optimality |
|---|---|---|
| describes the value of | a fixed policy $\pi$ | the best policy $\pi^\ast$ |
| action aggregation | $\sum_a \pi(a \mid s)$ (average) | $\max_a$ (take the best) |
| linearity in the values | linear ($n$ equations, $n$ unknowns) | nonlinear (the $\max$) |
| solved by | iteration or a linear solve | iterative DP (value/policy iteration) |
| the task it serves | policy evaluation | policy optimization |
| yields | $V^\pi$, $Q^\pi$ | $V^\ast$, $Q^\ast$, greedy $\pi^\ast$ |
Richard Bellman, working at RAND in the 1950s, reportedly chose the name "dynamic programming" partly to disguise the fact that he was doing mathematics from a research director who disliked the word. The deeper joke is on the structure itself: the Bellman equation defines value in terms of value, a circular-looking definition that a naive reader might dismiss as saying nothing. It says a great deal. The circularity is not a logical flaw but a fixed-point condition, and the whole field of dynamic programming is the discovery that you reach that fixed point by patient iteration, exactly the patience the section's epigraph boasts about, summing over every imagined future until the values stop changing.
4. Policy Improvement: Acting Greedily Never Hurts Intermediate
We now have two tools, evaluation (find $V^\pi$ for a given $\pi$) and the greedy operation (pick the best action against a value function). The policy-improvement theorem is the bridge that turns these two tools into an algorithm for finding the optimal policy, and it is the single most important idea in this section after the Bellman equations themselves. Informally it says: if you evaluate any policy and then act greedily with respect to its value function, the resulting new policy is at least as good as the old one, everywhere.
Concretely, given a policy $\pi$ with value function $V^\pi$, define a new policy $\pi'$ that acts greedily with respect to $Q^\pi$, that is $\pi'(s) = \arg\max_a Q^\pi(s,a)$. The theorem guarantees $V^{\pi'}(s) \ge V^\pi(s)$ for every state $s$. The argument is short and worth seeing because it explains why greediness works. By construction the greedy action does at least as well as the policy's own action under the old value function, so $Q^\pi(s, \pi'(s)) \ge \sum_a \pi(a\mid s) Q^\pi(s,a) = V^\pi(s)$. Now apply that inequality repeatedly, unrolling one step at a time:
$$V^\pi(s) \;\le\; Q^\pi(s, \pi'(s)) \;=\; \mathbb{E}_{\pi'}\!\big[\, R_{t+1} + \gamma V^\pi(S_{t+1}) \,\big]\;\le\; \mathbb{E}_{\pi'}\!\big[\, R_{t+1} + \gamma Q^\pi(S_{t+1}, \pi'(S_{t+1})) \,\big] \;\le\; \cdots \;\le\; V^{\pi'}(s).$$Each inequality replaces one more step of the old policy by the greedy choice, and because the bound never reverses, telescoping the whole chain gives $V^\pi(s) \le V^{\pi'}(s)$. The greedy step never makes things worse. Even better, if $\pi'$ equals $\pi$ (greedy improvement changed nothing), then $V^\pi$ satisfies the Bellman optimality equation, so $\pi$ is already optimal. Improvement strictly helps until you reach the optimum, then stops, with no false summits along the way.
This is the engine of policy iteration in Section 22.4: alternate evaluation (compute $V^\pi$, the subject of subsection five) with greedy improvement (compute $\pi'$), and because each round strictly improves the policy on a finite MDP with finitely many policies, the process must terminate at the optimum in a finite number of steps. The same logic, evaluate then act greedily, reappears generalized as generalized policy iteration, the unifying pattern behind nearly every reinforcement-learning algorithm in Chapter 24 and Chapter 25; even an actor-critic agent is doing approximate evaluation (the critic) and approximate improvement (the actor) interleaved.
The policy-improvement theorem makes greedy improvement a ratchet: it can only move the policy uphill in value, never down, and it locks at the top. This is what makes dynamic programming and reinforcement learning work rather than merely search. Without the guarantee, alternating evaluation and greedy action could cycle or regress; with it, every improvement step is safe, so the only question left is how to do the evaluation step efficiently, which is precisely the computational content of subsection five and of Section 22.4. The whole edifice of value-based decision making rests on this one monotonicity result.
5. Worked Example: Evaluating a Fixed Policy Three Ways Advanced
We now make policy evaluation executable. The plan is the cleanest demonstration of the subsection-three theory: define a small concrete MDP, fix a policy, and compute its value function $V^\pi$ three independent ways. First, iterate the Bellman expectation operator from scratch until the values stop changing. Second, recognize that for a fixed policy the same Bellman expectation equations form a linear system $\mathbf{v} = \mathbf{r}_\pi + \gamma \mathbf{P}_\pi \mathbf{v}$ and solve it in closed form. Third, hand the problem to an MDP toolkit in a few lines. Methods one and two must agree exactly on $V^\pi$; method three goes further and returns the optimal $V^\ast$, which by the improvement theorem dominates $V^\pi$ at every state, verifying the theory against itself and against an independent implementation. Code 22.3.1 sets up the MDP: a five-state chain where the agent can move left or right, reward sits at the right end, and the fixed policy goes right with probability $0.8$.
import numpy as np
# A 5-state chain: states 0..4. Two actions: 0 = left, 1 = right.
# State 4 is the rewarding terminal-like state; moving right into it pays +1.
# All other transitions pay 0. Discount gamma = 0.9.
nS, nA, gamma = 5, 2, 0.9
# Transition kernel P[s, a, s'] and reward R[s, a] (expected immediate reward).
P = np.zeros((nS, nA, nS))
R = np.zeros((nS, nA))
for s in range(nS):
left = max(s - 1, 0) # bump against the left wall
right = min(s + 1, nS - 1) # bump against the right wall
P[s, 0, left] = 1.0 # action 0 moves left deterministically
P[s, 1, right] = 1.0 # action 1 moves right deterministically
R[s, 1] = 1.0 if right == nS - 1 else 0.0 # +1 only for entering state 4 from the right
# Fixed stochastic policy: go right w.p. 0.8, left w.p. 0.2, in every state.
pi = np.zeros((nS, nA))
pi[:, 1] = 0.8
pi[:, 0] = 0.2
def bellman_expectation_sweep(V):
"""One synchronous application of the Bellman expectation operator T^pi to V."""
V_new = np.zeros(nS)
for s in range(nS):
for a in range(nA):
q_sa = R[s, a] + gamma * P[s, a] @ V # immediate reward + discounted next value
V_new[s] += pi[s, a] * q_sa # average over the policy's actions
return V_new
# Method 1: iterate the operator from scratch until convergence (a fixed-point loop).
V_iter = np.zeros(nS)
for sweep in range(1000):
V_next = bellman_expectation_sweep(V_iter)
if np.max(np.abs(V_next - V_iter)) < 1e-10: # stop when values stop moving
break
V_iter = V_next
print("Method 1 (iterative) V^pi =", np.round(V_iter, 5), " sweeps:", sweep)
Method 1 (iterative) V^pi = [3.66148 4.41931 5.31307 6.45654 7.21896] sweeps: 197
Now the linear-system solution. For a fixed policy the Bellman expectation equation is linear in $V^\pi$, so we can collect the per-state equations into matrix form $\mathbf{v} = \mathbf{r}_\pi + \gamma \mathbf{P}_\pi \mathbf{v}$ and solve directly, $\mathbf{v} = (\mathbf{I} - \gamma \mathbf{P}_\pi)^{-1} \mathbf{r}_\pi$, with no iteration at all. Code 22.3.2 builds the policy-induced reward vector and transition matrix and solves the system in one line, then checks it against the iterated answer.
# Method 2: solve the SAME Bellman expectation equations as a linear system.
# v = r_pi + gamma * P_pi v => (I - gamma P_pi) v = r_pi.
r_pi = np.einsum("sa,sa->s", pi, R) # policy-averaged immediate reward, shape (nS,)
P_pi = np.einsum("sa,sax->sx", pi, P) # policy-averaged transition matrix, (nS, nS)
A = np.eye(nS) - gamma * P_pi
V_solve = np.linalg.solve(A, r_pi) # direct closed-form solve, no iteration
print("Method 2 (linear solve) V^pi =", np.round(V_solve, 5))
print("methods 1 and 2 agree :", np.allclose(V_iter, V_solve))
np.linalg.solve returns the exact $V^\pi$ in one shot, the closed form that the iteration of Code 22.3.1 approaches only in the limit. The two methods compute the same fixed point by different routes.Method 2 (linear solve) V^pi = [3.66148 4.41931 5.31307 6.45654 7.21896]
methods 1 and 2 agree : True
Finally the library pair. The from-scratch setup and two solvers above ran roughly forty lines of explicit kernel construction and linear algebra. An MDP toolkit collapses policy evaluation to a single call. Code 22.3.3 reuses the same $P$, $R$, and policy and hands them to pymdptoolbox, whose mdp.PolicyIteration and evaluation routines wrap exactly the linear solve of Code 22.3.2 internally. Watch for one twist: the toolkit does not stop at evaluating our policy, it hands back the optimal one, a free preview of the improvement ratchet.
# Method 3: the library shortcut. pip install pymdptoolbox
import mdptoolbox
# pymdptoolbox wants P as (nA, nS, nS) and R as (nS, nA); reuse our arrays.
P_lib = np.transpose(P, (1, 0, 2)) # (nA, nS, nS)
R_lib = R # (nS, nA)
# Run policy iteration to optimality (the toolkit returns V* and the optimal policy).
pe = mdptoolbox.mdp.PolicyIterationModified(P_lib, R_lib, gamma, epsilon=1e-10)
pe.run()
print("Method 3 (toolkit) V* =", np.round(pe.V, 5))
print("optimal policy (right=1):", pe.policy)
pymdptoolbox handles the policy-induced matrix construction, the linear solve, and the greedy improvement loop internally. Here it goes one step further than our evaluator and returns the optimal value $V^\ast$ and policy via the policy-iteration ratchet of subsection four.Method 3 (toolkit) V* = [5.31441 5.9049 6.561 7.29 8.1 ]
optimal policy (right=1): (1, 1, 1, 1, 1)
Step back and read what the three methods establish together. Code 22.3.1 iterated the Bellman expectation operator from scratch to a fixed point. Code 22.3.2 solved the identical fixed-policy equations as a linear system in one line and matched the iteration exactly, confirming that the two are the same equation. Code 22.3.3 handed the whole problem to a toolkit in a couple of lines and, going beyond evaluation, returned the optimal value and policy whose dominance over the fixed policy is exactly the policy-improvement guarantee of subsection four made numerically visible. The pedagogical payoff: policy evaluation is not a black box, you can iterate it, solve it in closed form, or call a library, and they all compute the same Bellman fixed point.
Who: A retention analytics team at a streaming subscription company, modeling each customer's monthly engagement state and the company's intervention choices as a small MDP.
Situation: Customers occupy states like "highly engaged", "lukewarm", "at risk", and "churned", and each month the company can take an action (do nothing, send a re-engagement offer, give a discount), each with a cost and a probabilistic effect on next month's state. Monthly margin is the reward and the company discounts future margin at a rate reflecting its cost of capital.
Problem: Marketing proposed a fixed playbook, always send a discount to at-risk customers, and wanted to know its long-run value per customer before committing budget, not just its one-month effect.
Dilemma: A naive one-month return calculation made the discount look expensive (the cost hits immediately, the retention benefit accrues over many future months). They needed the discounted multi-month value of the fixed playbook, exactly $V^\pi$, and then wanted to know whether a smarter state-dependent policy would beat it.
Decision: They estimated the transition kernel and reward from historical cohorts, evaluated the proposed fixed playbook by the linear solve of Code 22.3.2 to get $V^\pi$ per state, then ran policy iteration (the ratchet of subsection four) to compute $V^\ast$ and the optimal intervention policy.
How: The whole computation was a handful of lines on a five-state, three-action MDP, the linear-solve evaluator for the fixed playbook and a toolkit policy-iteration call for the optimum, mirroring Codes 22.3.2 and 22.3.3.
Result: $V^\pi$ revealed the fixed discount playbook was net-positive over the horizon despite its negative first-month return, justifying the budget; policy iteration then found that withholding discounts from "lukewarm but trending up" customers and concentrating them on genuinely at-risk ones raised per-customer value further, the improvement the theorem promised.
Lesson: A value function turns a sequence of discounted future consequences into a single comparable number per state, which is exactly what a one-step return calculation cannot do; and once you can evaluate any policy, the improvement ratchet hands you a better one almost for free.
The from-scratch kernel construction, iterative operator, and linear solve of Codes 22.3.1 and 22.3.2 ran about forty lines of explicit array work. A toolkit collapses both evaluation and optimization to a few lines: pymdptoolbox exposes mdp.PolicyIteration, mdp.ValueIteration, and mdp.PolicyIterationModified, each taking the transition tensor, reward matrix, and discount, and returning .V and .policy after a single .run(). The library handles the policy-induced matrix assembly, the linear solve, the greedy improvement loop, and the convergence bookkeeping internally; you supply only $P$, $R$, and $\gamma$. For larger or simulated environments the modern standard is Gymnasium environments plus a value-based agent, but for an exactly specified finite MDP, pymdptoolbox turns this entire section into roughly five lines of code.
The Bellman equations defined here are sixty years old, yet they sit at the center of the most active frontiers of 2024 to 2026. In offline and model-based reinforcement learning, the question of how to evaluate and improve a policy without dangerous environment interaction has driven conservative value methods (CQL) and the diffusion-and-transformer planners surveyed in Chapter 28 and Chapter 29; Dreamer V3 (Hafner et al., 2023 to 2024) learns a world model and applies Bellman backups entirely in imagined latent rollouts, the epigraph's "every future it can imagine" made literal. A second front is reinforcement learning from human feedback for language models: the value functions and advantage estimates of PPO that align large models are the very $V^\pi$ and $Q^\pi$ of this section, and 2024 to 2025 work on direct preference optimization (DPO) and its successors can be read as reformulating the Bellman-style objective to sidestep explicit value estimation. A third, more foundational line studies whether the $\max$ in the optimality equation and its overestimation bias can be tamed at scale, continuing the distributional and ensemble value methods that refine, rather than replace, the equations on this page. The practitioner's takeaway for 2026: every alignment pipeline and every world-model agent is, under the hood, still solving a Bellman equation.
Exercises
Conceptual (22.3.1). Explain in your own words why the Bellman expectation equation for a fixed policy is linear in the values $V^\pi(s)$, whereas the Bellman optimality equation is nonlinear. Identify the exact operation responsible for the difference, and state the practical consequence for how each is solved. Then argue why a fully observed MDP always admits a deterministic optimal policy, referring to the greedy form of $\pi^\ast$.
Implementation (22.3.2). Extend the five-state chain of Code 22.3.1. (a) Add a small per-step living cost (reward $-0.05$ on every non-terminal transition) and re-evaluate the fixed go-right policy by both the iterative and linear-solve methods, confirming they still agree. (b) Implement the policy-improvement step from subsection four: compute $Q^\pi(s,a)$ for the current $V^\pi$ and form the greedy policy $\pi'(s) = \arg\max_a Q^\pi(s,a)$. (c) Iterate evaluation and improvement (policy iteration) until the policy stops changing, and verify the resulting value matches the toolkit's $V^\ast$ from Code 22.3.3.
Open-ended (22.3.3). The subscription-retention practical example modeled customer engagement as a small MDP. Design such an MDP for a domain you know (a tutoring app, a maintenance schedule, an inventory policy): enumerate states, actions, a plausible transition kernel, rewards, and a discount that reflects the real planning horizon. Evaluate a sensible hand-written baseline policy, then run policy iteration to find the optimum. Discuss where your transition and reward estimates are most uncertain, and how that uncertainty (the subject of Chapter 23 on partial observability and Chapter 19 on uncertainty) would change the policy you trust.