Hello! Welcome back to our module on Reinforcement Learning Foundations.
In our previous lesson, we established the language of reinforcement learning by learning how to formulate problems as Markov Decision Processes (MDPs). We defined the 5-tuple and saw how it can represent a sequential decision-making problem like a grid world.
Today, we move from describing the problem to solving it. The central question is: given an MDP, how do we find the optimal policy, ? The answer lies in the Bellman equations, named after the mathematician Richard Bellman. These equations are arguably the most important theoretical tool in reinforcement learning. Our goal is to derive these fundamental equations and understand how they lead to algorithms that can solve for the optimal values of states.
1. The Value of Being in a State
Before we can find the best policy, we need a way to quantify how "good" it is to be in a particular state. We do this with value functions.
Recall from our last lesson the concept of the discounted return, , which is the sum of all future rewards from time step , discounted by :
Since the transitions and rewards can be stochastic, is a random variable. The "value" of a state is the expected return we can get starting from that state.
There are two main types of value functions:
- State-Value Function, : The expected return when starting in state and following policy thereafter.
- Action-Value Function, : The expected return after taking action in state and then following policy .
The action-value function, , is often called the "Q-function", and its values are "Q-values". It tells us not just how good a state is, but how good it is to take a specific action from that state.
2. The Bellman Expectation Equation: Evaluating a Policy
The core insight of the Bellman equation is that value functions have a recursive structure. The value of the current state is directly related to the value of the possible next states.
Let's derive this relationship. We start with the definition of the state-value function and break down the return into two parts: the immediate reward and the discounted return from the next step onward, .
The final step substitutes for inside the expectation, which is valid because, by definition, the value of the next state is the expected return from that point on.
To get the final equation, we need to average over all possibilities: the actions the policy might take, and the next states the environment might transition to.
Bellman Equations, Dynamic Programming, Generalized Policy Iteration | Reinforcement Learning Part 2
This video from the channel Mutual Information provides an excellent, step-by-step algebraic derivation of the Bellman equation. It's a clear and intuitive walkthrough of how to expand the expectation.
Watch from 03:18 to 07:27. The video uses a concrete example to motivate the derivation. Follow how it breaks down the expectation by action and then by the environment's dynamics (next state and reward).
The process shown in the video leads us to the Bellman expectation equation:
Let's break this down:
- : We sum over all possible actions, weighted by the probability of the policy choosing each action.
- : For each action, we sum over all possible next states and rewards , weighted by the environment's transition probability.
- : This is the core of the recursion. The value is the immediate reward plus the discounted value of whatever state we land in.
This equation provides a consistency condition. For a given policy , the value of every state must equal the expected value of its successors. If we have the full MDP dynamics (), this gives us a system of linear equations we can solve to find the value function for that policy. This process is called policy evaluation.
For those interested in the rigorous probabilistic derivation, the following resource provides several formal proofs. Given your mathematical background, you might find these satisfying.
Deriving Bellman's Equation in Reinforcement Learning
This StackExchange thread contains several rigorous derivations of the Bellman equation. It shows how to formally manipulate the conditional probabilities and expectations using the Markov property.
Skim through the top two answers (by Finncent Price and Jie Shi). You don't need to reproduce them, but notice how they use the law of total probability and explicitly apply the Markov property (e.g., $p(g_{t+1}|s', r, a, s) = p(g_{t+1}|s')$) to arrive at the final recursive form. This is the formal underpinning of the more intuitive steps shown in the video.
3. The Bellman Optimality Equation: Finding the Best Policy
Evaluating a policy is useful, but our ultimate goal is to find the best policy, . This policy corresponds to the optimal value functions, denoted and .
for all .
How does the Bellman equation change for an optimal policy? An optimal policy has a simple rule: it must greedily choose the action that yields the highest expected return. Instead of averaging over all actions according to a policy , it will simply pick the best one. This means we replace the summation over actions with a max operator.
Bellman Equations, Dynamic Programming, Generalized Policy Iteration | Reinforcement Learning Part 2
Let's return to the Mutual Information video, which now explains this crucial leap from the expectation equation to the optimality equation.
Watch from 07:27 to 08:40. The key insight here is that if you knew the optimal action-values (Q-values), choosing the optimal action is as simple as picking the one with the highest value.
This leads us to the Bellman optimality equations:
For the state-value function:
Notice this is the same as the expectation equation, but with instead of .
For the action-value function, the logic is similar. After taking action and landing in , the optimal policy will then choose the best possible next action from .
This last equation is particularly important and will be the foundation for many RL algorithms, like Q-Learning.
Test your understanding!
Let's use the Q-value optimality equation. Look at the grid world in the image below. The discount factor is 0.5. The states are numbered 1-6, and some states have immediate rewards. The numbers inside the states represent their current estimated optimal state values, .

Assume the transitions are deterministic (if you choose to move to a state, you get there with 100% probability). Calculate the optimal Q-value for being in state 1 and moving to state 4, i.e., .
Hint: The equation is . Here, , , so the next state is . The reward is given upon entry to the next state.
Show answer
Let's break it down using the formula:
- s: Current state is 1.
- a: Action is "move to state 4".
- s': The resulting state is 4.
- R(s, a, s'): The reward for entering state 4 is 0.
- γ: The discount factor is 0.5.
- v_*(s'): The optimal value of the next state (state 4) is given as 85.
Plugging these in:
This calculation tells us the expected total future reward if we start in state 1, move to state 4, and then continue to behave optimally from there is 42.5.
4. Solving the Bellman Equations: Dynamic Programming
The Bellman optimality equations give us a consistency condition for the optimal values, but they don't give us the values directly. The max operator makes this a system of non-linear equations.
However, we can turn the optimality equation into an iterative update rule. This is the core idea behind Value Iteration, a classic algorithm from the field of Dynamic Programming.
The idea is simple:
- Start with some initial guess for the value function, (e.g., all zeros).
- Repeatedly apply the Bellman optimality equation as an update rule to compute the next iteration of the value function, , from the previous one, .
We keep applying this update for all states until the value function converges.
Lecture 2 Markov Decision Processes -- CS287-FA19 Advanced Robotics at UC Berkeley
Professor Pieter Abbeel's lecture on MDPs provides an excellent explanation of Value Iteration, showing how it works in practice on a grid world and discussing its convergence properties.
Please watch the following three segments: (11:47 - 18:53): This part derives the value iteration update rule from first principles, viewing it as finding the optimal value for a problem with a finite number of steps left. (21:58 - 26:51): This is a great visualization showing how the values propagate out from the reward sources through the grid world over several iterations. (28:49 - 34:00): This section discusses the convergence guarantee. The key concept here is that the Bellman update is a γ-contraction, which mathematically guarantees that repeated applications will converge to a unique fixed point. Your engineering math background makes this a relevant and powerful concept to understand.
An alternative to Value Iteration is Policy Iteration. This algorithm iterates between two steps:
- Policy Evaluation: Given the current policy , calculate its exact value function by solving the linear Bellman expectation equation.
- Policy Improvement: Create a new, better policy by acting greedily with respect to . For each state, the new policy chooses the action that maximizes the expected one-step lookahead.
This process is guaranteed to converge to the optimal policy, often in fewer iterations than Value Iteration.
Lecture 2 Markov Decision Processes -- CS287-FA19 Advanced Robotics at UC Berkeley
Let's conclude with Professor Abbeel's explanation of Policy Iteration.
Watch from 48:01 to 59:06. Focus on understanding the two-step dance: evaluate the current policy, then improve it. He explains why this process is guaranteed to find a better policy at each step (unless it's already optimal).
Conclusion
Today, we've journeyed from defining the value of states to deriving the fundamental equations that govern them, and finally to outlining the algorithms that solve these equations.
Key Takeaways:
- Value Functions ( and ) quantify the expected long-term return of a policy.
- The Bellman Expectation Equation relates the value of a state to the values of its successor states for a fixed policy. It is used for policy evaluation.
- The Bellman Optimality Equation uses a
maxoperator to define the value of a state under an optimal policy. It gives us a target to aim for. - The Bellman equations are not solved directly but are used as the basis for iterative Dynamic Programming algorithms like Value Iteration and Policy Iteration. These methods are guaranteed to converge to the optimal value function.
Preview of the Next Lesson:
We have now covered the theory behind Value Iteration and Policy Iteration. In our next lesson, we will get our hands dirty. We will take the grid world MDP we formulated in the last lesson and implement these algorithms from scratch in Python. This will solidify your understanding by translating these mathematical update rules into working code.