The Bellman Equations

Value iteration raises a landscape of values out of the goal. Policy iteration flips arrows until none change, and the Bellman operator squeezes every guess toward one fixed point.

The Bellman Equations

The value of a state is the reward you get next plus the discounted value of where you land. That one self-referential sentence — the Bellman equation — turns planning into arithmetic you can repeat until it stops changing. Here you watch it repeat.

Value Iteration in 3D

Press Play and watch value rise out of the goal and flow back through the maze, one sweep at a time. Then switch to in-place sweeps, try the big maze, or click cells to build walls, pits and goals of your own.

Drag to orbit · click a cell to edit
0
sweep k
—
residual ‖Vₖ − Vₖ₋₁‖∞
9 / 32
states whose greedy action is optimal
28 vs 7
sweeps to converge: synchronous vs in place
All values start at zero. Each sweep replaces every value by the best one-step lookahead: reward now plus γ times the value of where you land.
World
Click a cell to place

Column height and colour are Vₖ(s): emerald above zero, rose below. Goals (glowing emerald) and pits (rose) are drawn at their reward. In-place sweeps overwrite each value as soon as it is computed, alternating direction, so news from the goal can cross the whole maze in one sweep. Changing a slider or the grid resets the values to zero.

Go deeper:The Bellman Optimality Equation

The optimal value function V* satisfies V*(s) = maxₐ Σₛ′ P(s′ | s, a) [R(s, a, s′) + γ V*(s′)]: the best you can expect from s is the best action’s immediate reward plus γ times the optimal value of wherever it takes you. The right-hand side is an operator T* applied to V*, so V* is a fixed point, V* = T*V*. Value iteration simply applies T* over and over from any starting guess: Vₖ₊₁ = T*Vₖ. In the demo the guess is zero everywhere, so after k sweeps Vₖ(s) is the best expected return over the next k steps — which is why value spreads from the goal one cell per sweep.

Each update is a backup: it pulls value back from successor states to their predecessor, a picture often drawn as a backup diagram, with the state at the top, the actions beneath it and the possible next states at the leaves, and a max over the actions. A synchronous sweep computes every new value from the old ones. An in-place (Gauss–Seidel) sweep overwrites each value immediately, so later states in the same sweep already use it; the demo alternates the sweep direction, and on a deterministic maze it converges in a fraction of the sweeps. Both converge to the same V*.

Richard Bellman named dynamic programming and set out its foundations in his 1957 book Dynamic Programming, built on the principle of optimality: “An optimal policy has the property that whatever the initial state and initial decision are, the remaining decisions must constitute an optimal policy with regard to the state resulting from the first decision.” The Bellman equation is that principle written as arithmetic.

Watch the readout of states whose greedy action is optimal: it usually reaches the full count long before the residual becomes small. The values only need to be good enough to rank the actions correctly, which is why stopping value iteration early still gives an optimal policy. The residual shrinks geometrically, by at least a factor of γ every sweep — see the third demo for why.

Policy Iteration

Start from a terrible policy — every arrow pointing left — and press Play. The values settle for the frozen arrows, then every arrow improves at once; the chart shows what that costs in sweeps compared with value iteration.

1
round
0
evaluation sweeps this round
—
arrows changed by the last improvement
0
total sweeps of work
Press Play. First the values of the starting policy settle — sweep after sweep with the arrows held fixed — then every arrow is improved at once.
World
Starting policy (click a cell to turn its arrow)
γ = 0.95 · slip 0.2 · step reward -0.04 · evaluation stops when a sweep changes no value by more than 10⁻⁴

Cells show Vπ for the current policy (emerald positive, rose negative); arrows are the policy. Changed arrows flash orange. On the chart, each curve is the error against V* as work accumulates — one unit per sweep, and one per improvement, which computes every action value — and a ring marks where that method’s policy first becomes optimal. The orange dot is this run.

Go deeper:Evaluate, Improve, Repeat

Policy iteration, introduced by Ronald Howard in his 1960 book Dynamic Programming and Markov Processes, alternates two steps. Evaluation computes Vπ for the current policy by solving the linear Bellman expectation equations Vπ(s) = Σₛ′ P(s′ | s, π(s)) [R + γ Vπ(s′)] — here by sweeping Vπ ← TπVπ until it stops changing. Improvement makes the policy greedy: π′(s) = argmaxₐ Σₛ′ P(s′ | s, a) [R + γ Vπ(s′)].

The policy improvement theorem guarantees progress: acting greedily with respect to Vπ gives a policy π′ with Vπ′(s) ≥ Vπ(s) in every state, and if nothing changes then Vπ satisfies the Bellman optimality equation, so π is optimal. A finite MDP has only finitely many deterministic policies, and each round strictly improves until the optimum, so policy iteration stops after finitely many rounds. In practice it needs remarkably few. In 2011 Yinyu Ye proved that for a fixed discount factor it is strongly polynomial: the number of rounds is bounded by a polynomial in the numbers of states and actions alone.

The catch is the price of a round. On the chart each evaluation sweep and each improvement counts as one unit of work. Policy iteration evaluates every policy to high precision, even early, poor ones, so it spends far more sweeps than value iteration on these small worlds; the exact total depends on the evaluation tolerance, here 10⁻⁴. Modified policy iteration evaluates for only m sweeps (here 3) before improving: value iteration is the extreme with no evaluation sweeps between improvements, and policy iteration the limit m → ∞. On the deterministic maze, policy iteration needs about one round per step of the longest path, because each improvement can only extend the good arrows by a cell.

The pattern — estimate values for the current behaviour, then make the behaviour greedier — is called generalised policy iteration. Almost every method later in this module is an instance of it, with evaluation done from sampled experience instead of a known model.

The Contraction

Each dot is a whole value function for a two-state MDP. Press Apply T and watch all two hundred rush into one point while the square around them shrinks by γ; then drag γ towards 1 and see everything slow down.

0
applications k
0
largest distance to the fixed point (solid square)
—
shrink factor this step (never above γ = 0.90)
1.00
guaranteed bound γᵏ R₀ (dashed square)
Every guess, however wild, is pulled to the same point V* = (12.25, 14.75). Its greedy policy — move in s₁, stay in s₂ — is the optimal policy.
s₁s₂stay +1stay +2move -1each action does the opposite with probability 0.1

Left: each blue dot is a whole value function (V(s₁), V(s₂)) and moves to T(V) with each press. The orange square is the smallest sup-norm ball around the fixed point that holds every dot; the dashed square is the bound γᵏ R₀ the contraction guarantees. Right: six random pairs U, V in a room with no exits; on a log scale every line stays under the dashed γᵏ guide and runs parallel to it.

Go deeper:Why Every Guess Ends Up in the Same Place

The Bellman operators are γ-contractions in the sup norm: ‖TU − TV‖∞ ≤ γ ‖U − V‖∞ for any two value functions U and V. The proof is one line. For each state, the difference between two maxima is at most the largest difference between the things being maximised, |maxₐ f(a) − maxₐ g(a)| ≤ maxₐ |f(a) − g(a)|, and each action’s backed-up values differ by at most γ Σₛ′ P(s′ | s, a) |U(s′) − V(s′)| ≤ γ ‖U − V‖∞. For Tπ, which averages instead of maximising, the same bound holds even more directly.

Banach’s fixed-point theorem (Stefan Banach, 1922) then does the rest: a contraction on a complete metric space has exactly one fixed point, and iterating it from any start converges to that point geometrically, with ‖Tᵏ V − V*‖∞ ≤ γᵏ ‖V − V*‖∞. That is the dashed square on the left. It is also why the distance between any two iterates, on the right, falls along a straight line on a log scale with slope at most log γ. In the room with no exits the lines run exactly parallel to the guide in the long run; worlds with terminal states can do better, because reaching a terminal state erases the difference.

The theorem also tells you when to stop. Since ‖Vₖ − V*‖∞ ≤ γ/(1 − γ) · ‖Vₖ − Vₖ₋₁‖∞, a small residual certifies a small error. A standard consequence: once ‖Vₖ₊₁ − Vₖ‖∞ < ε(1 − γ)/(2γ), the greedy policy with respect to Vₖ₊₁ is ε-optimal — its value is within ε of V* in every state. As γ → 1 both the convergence rate and this stopping test degrade, which is the price of caring about the far future.

Key Takeaways

  • The Bellman equation says a state’s value is the expected reward now plus γ times the value of where you land.
  • Value iteration repeats that backup until nothing changes; the greedy policy is often optimal long before.
  • Policy iteration alternates full evaluation and greedy improvement, and stops in a few rounds at an optimal policy.
  • The Bellman operator is a γ-contraction, so every starting guess converges to one fixed point at rate γᵏ.

Spot a mistake or have an idea?

Search