Value functions and the Bellman equation
How good is it to be here? A value function answers that for every state, and the Bellman equation links each state’s value to its neighbors’. Watch value iteration spread value outward from the goal, see it as a 3D landscape, and read a policy straight off it.
In this lecture you will
- Define state values V(s) and action values Q(s, a)
- Write the Bellman optimality equation in words and symbols
- Run value iteration and explain why values spread outward from rewards
- Extract the optimal policy from a value function
In the last lecture we compared policies by simulating them. That does not scale: a problem with thousands of states and dozens of actions has more policies than atoms in the universe. We need to reason about how good each state is directly. That quantity is the value function.
How good is it to be here?
The value of a state is the return the agent can expect from that state onward, if it acts as well as possible:
A closely related quantity scores each action as well:
- State value V(s)
- Expected return from state s when acting optimally. “How good is it to be here?”
- Action value Q(s, a)
- Expected return after taking action a in state s, then acting optimally. “How good is this move from here?”
- Optimal policy π*
- In every state, choose the action with the highest Q(s, a).
- Backup
- Updating a value using the values of the states that follow it — information flows backwards from the future.
The Bellman equation
Richard Bellman noticed in the 1950s that values have a recursive structure. The value of a state equals the best you can do with one action — the immediate reward plus the discounted value of wherever you land:
In a deterministic world the sum disappears: value here = best of (reward + γ × value there).
- How valuable is this tile?We want the value of the tile in the middle of the grid, (2,2). The Bellman equation says: look one step ahead.
- List the actionsFrom (2,2) the robot can try up, right, down or left.
- See where each leadsEach move costs −1 and lands on a tile whose value we have already estimated. Moving left hits a wall, so the robot stays on (2,2).
- Score every actionAn action is worth its reward plus the discounted value of where it leads: −1 + 0.9 × V(next).
- Keep the bestThe value of (2,2) is the best score, 4.58. Up and right tie; both are optimal. Repeating this backup for every tile is value iteration.
Value iteration
Value iteration turns the Bellman equation into an algorithm: start with all values at zero, then sweep through every state applying the backup, again and again, until nothing changes.
Colors show value: teal is good, coral is bad. The arrow on each tile is the greedy action under the current values. Click a tile to see its backup.
Press One sweep a few times. After the first sweep every tile knows only that moves cost −1, except the tiles beside the goal, which jump to +10. With each sweep, value spreads one tile further from the goal, because a tile can only become valuable once one of its neighbors is. After about eight sweeps the values stop changing: they have converged.
Then experiment:
- Lower γ to 0.5. Distant rewards barely count, so tiles far from the goal stay negative — the agent becomes short-sighted.
- Add slip. Tiles next to the pits lose value, and the arrows change: the robot prefers routes that keep a safe distance from the pits, even if they are longer.
Seeing values as a landscape
A value function is easiest to understand as terrain. Step through the 3D view: each column’s height is the value of that tile.
- All values start at zeroBefore any backups the agent knows nothing: every tile has value 0.
- After one sweepEvery move costs −1, so most tiles sink slightly below the floor. The two tiles next to the goal jump to +10.
- After three sweepsValue is spreading from the goal, one tile per sweep. Tiles further away are still waiting for the news.
- ConvergedEach column now shows the best achievable return from that tile. Values fall as you move away from the goal and around the pits.
- The policy climbs uphillThe optimal policy is simple: from any tile, step to the highest neighboring column. The ball follows it from the start to the goal.
Why this matters beyond grid worlds
The Bellman equation is one of the most widely used ideas in computing:
- Route planning and logistics: shortest paths are value functions where each step costs time.
- Inventory and pricing: dynamic programming decides how much stock to hold given future demand.
- Game-playing AI: systems such as AlphaGo combine learned value estimates with search.
- Finance: option pricing and optimal stopping problems use backward induction — Bellman backups over time.
Key takeaways
- V(s) is the expected return from state s when acting optimally; Q(s, a) is the expected return after taking action a first.
- The Bellman equation says a state’s value equals the best action’s reward plus the discounted value of where it leads.
- Value iteration applies that equation to every state repeatedly until the values stop changing.
- Once the values are known, the optimal policy is simply to move towards the highest-valued next state.
Check your understanding
4 questions · answers explainedQ1What does V(s) represent?
Q2A tile’s neighbors have values 6.2 (up), 3.1 (down) and 4.6 (left), and every move costs −1 with γ = 0.9. What does one Bellman backup give?
Q3Why do values spread outward from the goal one tile per sweep in value iteration?
Q4How do you get the optimal policy from the optimal values?