ai theory
Reinforcement Learning Basics (5) — The Bellman Equation and Dynamic Programming
Junyoung Park · 2024-04-12 · 9 min
Introduction
The previous article defined the state value as
It is the expected return from state when policy is followed.
From this definition alone, it seems that calculating a value requires expanding every future reward through the end of an episode.
If an episode lasts three steps, we can simply add the terms. If it lasts hundreds of steps or never ends, repeatedly calculating the entire future for every state becomes unwieldy.
The Bellman equation identifies a recurring structure in this long future.
The entire future can be divided into the very next reward and the remaining future beginning from the next state.
It sounds obvious in words, but this idea underlies nearly every value update in reinforcement learning.
Folding the Tail of the Return
Let us group the return into the first reward and the remainder.
The expression in parentheses is precisely the return beginning at time .
Taking the expectation turns the expected into the value of the next state.
This is the Bellman expectation equation for state values.
The equation may look strange because it contains itself: appears on both the left and right. This recursive structure is natural because it expresses the current state's value in terms of the values of next states.
It is like saying, “the team's total workload is the work that arrived today plus the work carried into tomorrow.” Tomorrow's workload follows the same rule and connects to the following day. The Bellman equation exploits this repeated structure.
Reading the Bellman Equation with Small Numbers
Suppose the next reward from state is always , and the policy and environment lead to one of two states.
- With probability , the next state is , where .
- With probability , the next state is , where .
First calculate the expected value of the next state.
Then discount it and add the immediate reward.
The value of the current state is . The is the next reward, and is the present value of the future after the next state.
If the transition probabilities and reward function are known, we can expand the Bellman equation as sums.
Although the equation is longer, it contains only four steps.
- Consider the probability that the policy selects action .
- Consider that action's immediate reward.
- Average the values of possible next states according to their transition probabilities.
- Average over all actions according to the policy probabilities.
A Bellman Backup Corrects the Left Side with the Right
Recalculating the current value with the rewards and next-state values on the right is called a backup.
We can initialize every value to zero.
After one backup, information about the immediately following reward enters the value. Repeated backups propagate reward information one step farther across the states each time.
Suppose a Gridworld exit gives .
- First iteration: states immediately beside the exit become valuable.
- Second iteration: states two cells away become valuable.
- As iterations continue, information about the exit spreads throughout the grid.
This is why value iteration is often described as “calculating backward from the final reward.” Even when the real environment contains loops and stochastic transitions, repeated backups preserve the same intuition.
The Bellman Equation for Action Values
An action value can likewise be divided into one step and the remaining future.
From the current , the agent receives a reward and reaches the next state. Its policy then chooses the next action . The expected future from that point is .
Later, the SARSA update target will have the form
This formula does not appear from nowhere. It approximates the Bellman expectation equation for action values with one actual experience.
The Bellman Optimality Equation for Finding the Best Policy
The Bellman expectation equation evaluates a fixed policy . To find an optimal policy, we select the greatest value among the actions available in the next state.
For action values,
We will meet the target in the second equation again, unchanged, in Q-Learning.
The expectation equation averages over the next actions that the current policy will actually select. The optimality equation chooses the largest possible next action. This small difference eventually leads to the distinction between on-policy SARSA and off-policy Q-Learning.
What Does Dynamic Programming Know in Advance?
Dynamic programming, or DP, divides a complex problem into overlapping smaller problems and reuses their results.
To use DP in reinforcement learning, we must know the model of the MDP.
- Transition probabilities
- Reward function
In other words, we assume that we already know where an action in state might lead and what its average reward is.
With this information, a Bellman backup can sum over every possible next state without repeatedly running the actual environment.
Knowing the model is a strong assumption, but DP provides a reference point for later algorithms. To understand what Monte Carlo and TD approximate with a single experience, it helps to first see how DP calculates the result when the entire model is available.
Policy Evaluation: How Good Is This Policy?
Policy evaluation calculates the value of a fixed policy .
Beginning from an initial estimate , it repeatedly updates every state.
is the estimate at iteration . The right side uses the previous iteration's values , and the newly calculated values are stored in .
When the changes become sufficiently small, we stop and regard as close to .
Policy Improvement: Switch to a Better Action
Once we know a policy's values, we can look one step ahead in each state and choose a better action.
This is a greedy improvement: choose the action that looks best under the current policy's values.
Alternating policy evaluation and policy improvement gives policy iteration.
The flow is
When the policy no longer changes, its current action is already greedy in every state, and it has reached an optimal policy.
Value Iteration: Evaluate Once and Improve Immediately
Must policy evaluation converge completely before we improve the policy?
David Silver's lecture explains that the extreme case—performing only one evaluation update before immediately improving—is equivalent to value iteration.
Instead of explicitly storing and evaluating a policy, value iteration repeatedly applies the Bellman optimality backup to the values.
| Method | Work performed in an iteration |
|---|---|
| Policy iteration | Evaluate the policy sufficiently, then improve greedily |
| Value iteration | Reflect improvement immediately in every optimality backup |
Both are DP methods for finding an optimal policy in an MDP whose model is known. They differ in how long evaluation runs before improvement is reflected.
The Limits of DP, Which Calculates Everything
DP is conceptually clear, but it has requirements.
- The transition and reward models must be known.
- It must be possible to sweep repeatedly over every state.
- The numbers of states and actions cannot be too large.
In the real world, we may not know the exact probability that a robot will slip, and the set of possible image states may be effectively infinite. Then we cannot use a backup that sums over every .
The next step is to use samples that were actually experienced instead of calculating every possible outcome.
- Monte Carlo: use the actual return after an episode ends as the target
- TD: use one reward and the next-state estimate as the target
Both methods learn values even when the environment model is unknown.
What to Remember
The central idea of this article is
The Bellman equation divides a long future into the immediate reward and the discounted value of the next state.
More specifically:
- reveals the recursive structure of the return.
- The Bellman expectation equation represents the value of a fixed policy.
- The Bellman optimality equation uses a over the next action.
- Policy iteration alternates evaluation and improvement.
- Value iteration repeatedly applies the Bellman optimality backup.
- DP requires the transition and reward models to be known.
The next article moves to a setting where the environment's transition matrix is unknown. Instead of calculating every possible future, Monte Carlo prediction experiences complete episodes and learns values from the average of actual returns.