Search

Search papers and lessons

Bellman Equation

Level 3 · 9 min read

The Bellman equation says a state's best value equals the best immediate reward plus the discounted best value of the state you land in, which turns a long-horizon decision into a chain of one-step decisions.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Reinforcement learning
  • Stochastic processes
  • Discrete math
  • Sign in to see your progress

TL;DR

The Bellman equation says that the value of a state is the best immediate reward you can get there, plus the discounted value of the state you move into next. It is a one-step rule that holds for the whole future. Writing the value of a long decision sequence this way lets you solve it one step at a time.

Where you'll see it: in value iteration and Q-learning, in the update rules that turn a reward into a learned value, and in optimal control problems from inventory planning to route finding.

Intuition

Suppose you are deciding how to get out of a maze. You do not need to plan the whole route at once. You only need to know, from each junction, which neighbouring junction is worth the most. If you already know the value of every neighbour, the value of your current junction is just the best step plus what you expect from there.

That recursion is the whole idea. A long-horizon problem becomes a set of local consistency conditions. Each state's value must agree with its neighbours' values, after one step of reward and one step of discounting. A value function that satisfies all these conditions is the right answer, and the Bellman equation is the list of those conditions.

It exists because looking ahead by brute force is hopeless. A game with 20 moves and 3 choices per move has 3^20, about 3.5 billion, paths. The Bellman equation avoids enumerating paths: it reuses the answers already computed for the next state.

0510152000.20.40.60.81steps into the futureweight on that reward
  • gamma = 0.9
  • gamma = 0.5
The discount factor gamma sets how fast a future reward fades. With gamma = 0.9 a reward 10 steps away still counts for about 35 percent of its face value; with gamma = 0.5 it counts for about 0.1 percent.

The math

Setting

The environment is a Markov decision process. There are states s∈Ss \in \mathcal{S}, actions a∈Aa \in \mathcal{A}, transition probabilities P(s′∣s,a)P(s' \mid s, a) for the chance of landing in s′s' after taking aa in ss, and expected immediate rewards R(s,a)R(s, a). The discount factor γ\gamma lies between 0 and 1 and says how much a reward one step later is worth compared with one now.

A policy π(a∣s)\pi(a \mid s) gives the probability of each action. The value function vπ(s)v_\pi(s) is the expected discounted return from ss under π\pi. The optimal value function is the best any policy can do:

v∗(s)=max⁡πvπ(s).v^*(s) = \max_{\pi} v_\pi(s).

Bellman expectation equation

For a fixed policy, the value of ss is the average over actions of the reward plus the discounted value of the next state:

vπ(s)=∑aπ(a∣s)[R(s,a)+γ∑s′P(s′∣s,a) vπ(s′)].v_\pi(s) = \sum_{a} \pi(a \mid s) \Big[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, v_\pi(s') \Big].

This is the same equation as in the value function lesson. It is linear in the unknown values, so it can be solved directly or iterated.

Bellman optimality equation

The optimal value function satisfies the same kind of equation, except that the average over actions becomes a maximum. The agent is free to choose the best action, so

v∗(s)=max⁡a[R(s,a)+γ∑s′P(s′∣s,a) v∗(s′)].v^*(s) = \max_{a} \Big[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, v^*(s') \Big].

This is the Bellman equation proper. It is a fixed-point equation. The true v∗v^* is the one function that makes both sides equal for every state.

The same idea works for the action-value function q∗(s,a)q^*(s, a), which fixes the first action and then acts optimally:

q∗(s,a)=R(s,a)+γ∑s′P(s′∣s,a)max⁡a′q∗(s′,a′).q^*(s, a) = R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) \max_{a'} q^*(s', a').

Once you have q∗q^*, the optimal policy is simply to pick the action with the largest q∗(s,a)q^*(s, a) in each state.

Why the discount factor matters

The discount factor makes the infinite sum finite. If the reward is bounded by Rmax⁡R_{\max}, the discounted return is at most Rmax⁡/(1−γ)R_{\max} / (1 - \gamma), so the fixed-point equation has a unique solution. Values stay finite, and iterating the equation converges. Smaller γ\gamma makes the agent short-sighted; γ\gamma close to 1 makes it plan far ahead, and the numbers can take many iterations to settle.

Value iteration

Turning the optimality equation into an algorithm gives value iteration. Start with any guess v0v_0, and repeat

vk+1(s)=max⁡a[R(s,a)+γ∑s′P(s′∣s,a) vk(s′)]v_{k+1}(s) = \max_{a} \Big[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, v_k(s') \Big]

for every state. Each pass is a Bellman backup. Because the backup shrinks the error between guesses by a factor of γ\gamma each time, the values converge to v∗v^*.

Algorithm
Value iteration
  1. Set v(s)=0v(s) = 0 for every state ss.
  2. For each state ss, compute the backup max⁡a[R(s,a)+γ∑s′P(s′∣s,a) v(s′)]\max_a [R(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, v(s')] and store it as the new v(s)v(s).
  3. Stop when no value changes by more than a small threshold.
  4. For each state, choose the action that achieves the maximum. That action is optimal.

Worked examples

Example
Policy evaluation in one backup

Take a state ss with a policy that always takes action aa. Action aa gives reward R=2R = 2. From there the environment goes to state s1s_1 with probability 0.8 and to state s2s_2 with probability 0.2. Suppose v(s1)=10v(s_1) = 10 and v(s2)=0v(s_2) = 0. Let γ=0.9\gamma = 0.9.

  1. Expected next value: 0.8×10+0.2×0=80.8 \times 10 + 0.2 \times 0 = 8.
  2. Discount it: 0.9×8=7.20.9 \times 8 = 7.2.
  3. Add the immediate reward: 2+7.2=9.22 + 7.2 = 9.2.

So v(s)=9.2v(s) = 9.2. The number reads as: starting in ss, you expect to collect 9.2 in discounted reward, counting the immediate 2 and the discounted future.

Example
Finding the optimum on a two-action choice

State s0s_0 has two actions. Action "stay" gives reward 1 and returns to s0s_0 with certainty. Action "leave" gives reward 0 and moves to a state s1s_1 with certainty, where the optimal value is v∗(s1)=4v^*(s_1) = 4. Let γ=0.9\gamma = 0.9.

The optimality equation for s0s_0 is

v∗(s0)=max⁡(1+0.9 v∗(s0), 0+0.9×4)=max⁡(1+0.9 v∗(s0), 3.6).v^*(s_0) = \max\big(1 + 0.9\, v^*(s_0),\ 0 + 0.9 \times 4\big) = \max\big(1 + 0.9\, v^*(s_0),\ 3.6\big).
  1. Guess that "stay" is best, so v∗=1+0.9v∗v^* = 1 + 0.9 v^*. Solving: 0.1 v∗=10.1\, v^* = 1, so v∗(s0)=10v^*(s_0) = 10.
  2. Check the other branch: 3.63.6, which is less than 1010. The guess is consistent.
  3. So v∗(s0)=10v^*(s_0) = 10 and the optimal action is "stay".

Now run value iteration from v0(s0)=0v_0(s_0) = 0 to see the values climb:

IterationBackup for s0s_0Value
0start0
1max⁡(1+0.9×0, 3.6)\max(1 + 0.9 \times 0,\ 3.6)3.6
2max⁡(1+0.9×3.6, 3.6)=max⁡(4.24, 3.6)\max(1 + 0.9 \times 3.6,\ 3.6) = \max(4.24,\ 3.6)4.24
3max⁡(1+0.9×4.24, 3.6)=max⁡(4.816, 3.6)\max(1 + 0.9 \times 4.24,\ 3.6) = \max(4.816,\ 3.6)4.816

The values rise steadily toward 10. Once the "stay" branch wins, the gap to 10 shrinks by a factor of γ=0.9\gamma = 0.9 per iteration (5.76, then 5.184, and so on), so convergence is geometric.

Example
A three-state chain with a goal

Three states: AA, BB and a terminal goal GG with value 0. From AA the only action moves to BB with reward 0. From BB the only action moves to GG with reward 10. Take γ=0.5\gamma = 0.5.

  1. Value of BB: v(B)=10+0.5×v(G)=10+0.5×0=10v(B) = 10 + 0.5 \times v(G) = 10 + 0.5 \times 0 = 10.
  2. Value of AA: v(A)=0+0.5×v(B)=0.5×10=5v(A) = 0 + 0.5 \times v(B) = 0.5 \times 10 = 5.

The reward of 10 is worth 10 at BB but only 5 at AA, because AA is one step further away and each step of delay halves its weight. Working backwards from the goal, one backup per state, gives the answer without enumerating any paths.

Tradeoffs and failure modes

The Bellman equation is exact when you know PP and RR. Its cost grows with the number of states, because each backup sums over all next states and all actions. In problems with millions of states, you approximate the value function with a neural network or with samples instead.

SituationProblemBetter choice
Model is known, state space is smallFull sweeps are cheap and exactValue iteration or policy iteration
Model is unknownCannot compute PP or RRSample-based updates such as [[q-learning
Very large or continuous state spaceTable of values does not fitFunction approximation, such as a neural network
γ\gamma close to 1Values converge slowly and can be largeUse more iterations, or an algorithm that accelerates the backup

Two failure modes come up often. First, an approximate value function with small errors can produce a policy that is far from optimal, because the max amplifies errors where two actions are close in value. Second, sample-based backups can be unstable when the approximation and the bootstrapping interact, which is a known problem with off-policy training.

History

Richard Bellman worked at the RAND Corporation in the 1950s on multistage decisions where a choice now affects costs later. He called the general approach dynamic programming and published the core ideas in his 1957 book of that name. The principle that an optimal policy's remaining choices must themselves be optimal, which underlies the recursion, is known as the principle of optimality.

The equation was named after him later. Ronald Howard developed policy iteration around 1960, and the approach spread through operations research and control theory. In the late 1980s, Chris Watkins and others connected the Bellman recursion to learning from experience, which led to Q-learning and the wider use of the equation in reinforcement learning.

Check yourself

QuestionRecall: state the Bellman optimality equation for v∗(s)v^*(s) in words.Show answer
The optimal value of a state equals the maximum, over actions, of the immediate reward for that action plus the discounted optimal value of the expected next state.
QuestionComputation: a state has reward 1 for every action and always returns to itself. Find the optimal value for γ=0.8\gamma = 0.8 using v=1+0.8vv = 1 + 0.8 v.Show answer
Solve v=1+0.8vv = 1 + 0.8 v. Then 0.2v=10.2 v = 1, so v=5v = 5.
QuestionWhy: why does the discount factor γ\gamma need to be less than 1 for the Bellman equation to have a unique solution in an infinite-horizon problem?Show answer
With γ=1\gamma = 1 the return is an unbounded sum of rewards in an infinite horizon, so it may not be finite. With γ<1\gamma \lt 1 and bounded rewards, the discounted sum is at most Rmax⁡/(1−γ)R_{\max}/(1 - \gamma), which is finite. The backup then shrinks errors by γ\gamma each pass, so the fixed point is unique and value iteration converges.
QuestionApplied scenario: a robot in state S can stay and collect reward 1 every step forever, or move to a charger that pays a single reward of 5 and then ends the episode. With γ=0.9\gamma = 0.9, which should it choose?Show answer
Staying is worth 1+0.9⋅1+0.92⋅1+⋯=1/(1−0.9)=101 + 0.9 \cdot 1 + 0.9^2 \cdot 1 + \cdots = 1/(1 - 0.9) = 10. Moving is worth 55, since the episode ends after the payment. Staying wins. In general, moving beats staying only when the one-off reward exceeds 1/(1−γ)1/(1 - \gamma). The comparison is a short Bellman backup with the two actions.

Sources