Value Functions & the Bellman Equations
What states and actions are worth, and the recursive equations that pin values down.
A chess position, a battery level, a cell in a maze: how good is it to be there? A value function answers with the return you can expect, and the Bellman equations pin every value down with one local rule: a state is worth its next reward plus the discounted worth of where you land. This lesson derives the equations, solves them by hand and as linear algebra, and shows how the optimal ones single out the best policy.
What you’ll learn
- Define the state-value, action-value and advantage functions , and , and convert between them.
- Derive the Bellman expectation equations from the return recursion, and read the four Bellman equations as backup diagrams.
- Solve a small MDP's Bellman equations by hand and in matrix form, , and explain why the inverse exists.
- State the Bellman optimality equations, and act optimally from without a model, or from with one.
What a state is worth
The return is a random variable: the same policy from the same state can be lucky or unlucky. A value function averages the luck away. For a stationary policy in an MDP , the state-value function and the action-value function are
commits to action for one step and follows afterward. Neither depends on : the dynamics and the policy are the same at every step and the horizon is infinite, so the future looks the same whenever you arrive in .
The running example of this lesson is the two-state MDP of the value-space widget. In , stay pays 1 and stays; go pays 0 and moves to . In , stay pays 2 and stays; go pays 0 and moves back to . Moves are deterministic and . A deterministic policy is a pair (action in , action in ). Under (stay, stay) the agent earns 1 per step forever from , so , and 2 per step from , so .
Under (stay, stay) the values are . What is , the value of going once from and then staying forever?
Show the answer
B.
Going pays 0 now and lands in , which is worth 20 under the policy: . The advantage is : deviating once beats the policy's own choice.
Think first Under (stay, stay), going from has advantage . Going from has , an advantage of . What should a policy that trusts these numbers do?
Switch to go in and keep stay in . A positive advantage means one deviation pays, and the policy improvement theorem says that making every such switch at once can only help, in every state. The new policy (go, stay) is worth , and it is optimal. The Fixed Point lesson Advantages & the Performance Difference Lemma computes exactly how much any switch gains.
The Bellman expectation equations
The return obeys (Agents, Environments & Rewards). Taking expectations turns this identity about one trajectory into an equation that links the values of neighboring states: the Bellman equation.
Condition the recursion on the current state and use linearity of expectation:
Split the second term by the action and the next state. Given , the Markov property and the stationarity of make the expected return from on equal to , whatever happened before:
The first term is the policy's expected reward, . Collect terms. This is the Bellman expectation equation, one equation per state:
The same argument conditioned on gives the equation for action values. The policy's average now sits at the next state:
| Equation | Root | First branching | Second branching | Leaves |
|---|---|---|---|---|
| , expectation | a state | actions, averaged with weights | next states, averaged with , after the reward | |
| , expectation | a pair | next states, averaged with , after | next actions, averaged with | |
| , optimality | a state | actions, maximized | next states, averaged with | |
| , optimality | a pair | next states, averaged with | next actions, maximized |
Write and for the reward vector and transition matrix of the chain the policy induces (Markov Decision Processes). Then the equations of all states together read : linear equations in unknowns.
Solve the Bellman equations of the policy (stay, go) by hand: stay in , go from . What is ?
Show the answer
9
The equations are and . The first involves only and gives ; then . State pays nothing under this policy: its whole value is the discounted value of where its action leads.
Solving the equations with linear algebra
Move the unknowns to one side:
is a stochastic matrix, with nonnegative entries and rows summing to 1, so for every vector . If , then , which forces . So is invertible whenever , and the Neumann series gives the inverse:
Each term has a meaning: is the expected reward steps after starting in . The closed form is the definition of value, summed in one go, the same expression Markov Decision Processes reached by summing expected rewards over time.
For (go, stay), both states lead to , and the matrices are small enough to invert by hand:
import numpy as np
gamma = 0.9
# P[s, a, s'] and r[s, a] for the two-state MDP; action 0 = stay, 1 = go
P = np.zeros((2, 2, 2))
P[0, 0, 0] = P[0, 1, 1] = P[1, 0, 1] = P[1, 1, 0] = 1.0
r = np.array([[1.0, 0.0],
[2.0, 0.0]])
def evaluate(pi):
"""Exact policy evaluation; pi[s, a] holds the action probabilities."""
P_pi = np.einsum("sa,sat->st", pi, P)
r_pi = (pi * r).sum(axis=1)
return np.linalg.solve(np.eye(2) - gamma * P_pi, r_pi)
stay, go = [1.0, 0.0], [0.0, 1.0]
for name, pi in [("(stay, stay)", [stay, stay]), ("(go, stay)", [go, stay]),
("(stay, go)", [stay, go]), ("(go, go)", [go, go])]:
print(name, evaluate(np.array(pi)))The four deterministic policies are worth , , and : the corners of the value-space widget. A linear solve costs , fine for thousands of states and hopeless for millions; Policy Evaluation in the Planning topic replaces it by repeated backups.
Think first The uniform random policy picks stay or go with probability ½ in both states. What are its values? (Hint: subtract the two Bellman equations.)
The equations are and . Subtracting cancels the discounted terms and leaves . Substituting back gives , so , less than half of what the best policy earns.
The Bellman optimality equations
The best you can do from each state defines the optimal value functions
A priori each state could need its own maximizing policy. It does not: in a finite discounted MDP a single stationary deterministic policy attains both maxima in every state at once (Policies & Why Simple Ones Suffice). Its values satisfy the Bellman equation of a policy that always picks a best action, so the average over actions becomes a maximum:
Given , acting optimally takes no further thought: any greedy policy is optimal. Given only , the agent must still look one step ahead, , and that needs the rewards and transition probabilities. This one difference is why model-free control methods such as Q-learning learn action values rather than state values.
| State | Greedy action | |||
|---|---|---|---|---|
| go | ||||
| stay |
A warehouse robot knows the exact optimal state values of its MDP but has no model of what its actions do. Can it act optimally?
- Yes: it moves toward the neighboring state with the highest
- Yes: and carry the same information about actions
- Not reliably: turning into a choice needs and for the one-step lookahead, while would let it take directly
- No: optimal behavior needs the full model even when is known
Show the answer
C. Not reliably: turning into a choice needs and for the one-step lookahead, while would let it take directly
ranks states, not actions. To choose, the agent must predict where each action leads and what it pays on the way, which is exactly the model it lacks. has that lookahead built in, which is why model-free control learns action values.
Both equations at work: policy iteration
The two kinds of equation do different jobs. The expectation equations evaluate a given policy; the optimality equations characterize the best one. Alternating them is policy iteration: solve the expectation equations of the current policy, then make the policy greedy with respect to the resulting . A greedy step only picks actions with nonnegative advantage, so no state gets worse, and since there are finitely many deterministic policies the process stops, at a policy whose values satisfy the optimality equation. The Planning topic develops it in full.
Key takeaways
- and are expected returns; , , and the advantage averages to zero under .
- Taking expectations of , with the Markov property, gives the Bellman expectation equation , solved exactly by .
- The optimality equations replace the policy's average over actions by a maximum; they are nonlinear and are solved by iteration, policy iteration or linear programming.
- A policy greedy with respect to is optimal and needs no model; acting from needs a one-step lookahead with the model.
- Policy iteration alternates the two kinds of equation: evaluate the policy exactly, make it greedy, and stop when nothing changes.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In state a policy takes with probability 0.25 and with probability 0.75, and , . What are and ?
- and
- and
- and
- and
Show the answer
B. and
, so and . Check: , as the policy average of the advantage must be.
Why can be computed with one linear solve, while cannot?
- Because depends on the start distribution
- Because is invertible and is not
- Because is not unique
- Because the expectation equations average over actions with fixed weights , which is linear in , while the optimality equations take a maximum, which is not
Show the answer
D. Because the expectation equations average over actions with fixed weights , which is linear in , while the optimality equations take a maximum, which is not
For a fixed , the map is affine, so its fixed point solves . The maximum in the optimality equation is piecewise linear, with pieces given by the greedy policy, so is found by iterating, by policy iteration (one linear solve per policy), or by linear programming.
In the backup diagram of the Bellman optimality equation for , where does the maximum sit?
- At the root, over the action of the pair
- Over the next actions , after averaging over the next states
- Over the next states
- Nowhere: is an average
Show the answer
B. Over the next actions , after averaging over the next states
fixes the first action, averages over where it leads with , and then assumes the best next action, . What the environment decides is averaged; what the agent decides is maximized.
A policy is not optimal, and is greedy with respect to . What does the policy improvement theorem guarantee?
- is optimal
- is better than in every state
- in every state, with strict inequality in at least one state
- Nothing: a greedy step can make some states worse
Show the answer
C. in every state, with strict inequality in at least one state
Greedy actions have , so no state gets worse. If is not optimal, some state has an action with positive advantage, and there is strictly better. That is why policy iteration terminates at an optimal policy: it can only stop when no advantage is positive.
Go deeper
- Bellman Operators & Contraction · Fixed points, Banach’s theorem, and why repeated backups converge.
- Advantages & the Performance Difference Lemma · An exact formula for how much better one policy is than another.
- Successor Representations & Features · Splitting value into where you will go and what you will find there.
Further reading
- R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 3–4 (MIT Press, 2nd ed., 2018).
- R. Bellman, Dynamic Programming (Princeton University Press, 1957).
- M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 6 (Wiley, 1994).
- C. Szepesvári, Algorithms for Reinforcement Learning (Morgan & Claypool, 2010).