Value Functions and Bellman Equations
"How good is this state?" sounds unanswerable without simulating every possible future â except it isn't, because of a recursive identity that turns an infinite lookahead into a one-step relationship. That identity, the Bellman equation, is the single mathematical tool every algorithm in this section exploits in some form.
The value of a state equals the immediate reward plus the discounted value of what comes next; every algorithm in this section exploits that recursion.

The state-value function V^Īâ
The expected return, starting from state , if the agent follows policy thereafter â a single number per state, summarising "how good is it to be here, given how I'll act from now on."
The action-value function Q^Īâ
The expected return from taking a specific action in state , then following thereafter â one level more specific than , and, as later pages show, the more directly useful quantity for actually choosing actions.
The relationship between themâ
is exactly the expectation of over the policy's action distribution â knowing for every action in a state lets you recover by averaging, weighted by how often the policy takes each action.
The Bellman expectation equation for V and for Qâ
Both equations express the same idea: the value of "here" is the immediate reward, plus the discounted value of "wherever you end up next" â a recursive definition, not a closed-form one, but exactly the structure Dynamic Programming turns into an iterative algorithm.
The advantage function, and why it reduces varianceâ
The advantage measures how much better (or worse) action is than the policy's average action in state â centring around its own state-dependent mean. This centring is exactly what Actor-Critic Methods exploits: an advantage-based gradient signal has much lower variance than a raw return-based one, because it's answering "was this action better than typical," not "was the whole outcome good," which absorbs a lot of state-dependent noise that has nothing to do with the action itself.
Optimal value functions V* and Q*â
The best possible value achievable from state (or state-action pair ), maximised over every possible policy â the theoretical ceiling every algorithm is trying to reach.
The Bellman optimality equationsâ
The key structural change from the expectation equations above: instead of averaging over the policy's action distribution, these equations maximise over actions directly â encoding the fact that an optimal policy always takes the single best action, not a weighted mixture.
Extracting a policy from Q* by greedy selectionâ
Given , the optimal policy is simply â pick whichever action has the highest optimal action-value in each state. This is the entire reason -functions, not just -functions, matter so much practically: alone is enough to act optimally, with no additional model of the environment's dynamics required.
Why the optimality equations are non-linear (the max)â
The Bellman expectation equations are linear in (a fixed policy just weights terms by fixed probabilities) â solvable directly as a linear system, as the code below does. The Bellman optimality equations, because of the operator, are non-linear â no closed-form linear-algebra solution exists, which is exactly why Dynamic Programming's algorithms are iterative rather than a single matrix solve.
The two things every RL algorithm doesâ
Every algorithm in this section, regardless of family, is doing one (or both) of two things: policy evaluation â compute or for a fixed policy â and policy improvement â use those values to produce a better policy. Recognising which of the two a given method is doing (or how it interleaves them) is the fastest way to understand any new RL algorithm.
Generalised policy iteration as the unifying pictureâ
Generalised Policy Iteration (GPI) is the umbrella term for any process that alternates evaluation and improvement, in whatever granularity or order â full sweeps to convergence (Dynamic Programming's policy iteration), a single step of each interleaved (Q-Learning and SARSA), or anything in between. Nearly every algorithm in this entire section is a specific instance of GPI.
| Symbol | Meaning |
|---|---|
| state-value and action-value functions under policy | |
| optimal value functions | |
| the advantage function |
Code: solving V^Ī directly, and extracting the greedy policy from Q*â
import numpy as np
from mdp_demo import P, R, policy, n_states, n_actions
gamma = 0.95
# --- Solve the Bellman expectation equation for V^pi directly as a linear system ---
# V = R_pi + gamma * P_pi @ V => (I - gamma * P_pi) V = R_pi
R_pi = np.sum(policy * R, axis=1)
P_pi = np.einsum("sa,sat->st", policy, P)
V_direct = np.linalg.solve(np.eye(n_states) - gamma * P_pi, R_pi)
# --- Verify against iterative policy evaluation ---
V_iter = np.zeros(n_states)
for _ in range(1000):
V_iter = R_pi + gamma * P_pi @ V_iter
print("direct-solve vs iterative V^pi max difference:", np.abs(V_direct - V_iter).max())
# --- Compute Q^pi from V^pi, then extract the greedy policy over Q as an approximation of pi* ---
Q = R + gamma * np.einsum("sat,t->sa", P, V_direct)
greedy_actions = np.argmax(Q, axis=1)
arrows = {0: "^", 1: "v", 2: "<", 3: ">"}
grid_size = int(np.sqrt(n_states))
print("\ngreedy policy extracted from Q, rendered as arrows:")
for r in range(grid_size):
print(" ".join(arrows[greedy_actions[r * grid_size + c]] for c in range(grid_size)))
See alsoâ
- Dynamic Programming â turning these recursions into a convergent iterative algorithm.
- Markov Decision Processes â the formalism and are defined over.