Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

The Bellman Equation: Defining Optimal Value Functions

Introduction

In our previous lesson, we established the mathematical framework for reinforcement learning: the Markov Decision Process (MDP). We defined the problem of an agent interacting with an environment to maximize a cumulative reward. However, we haven't yet discussed the solution. How does an agent determine the best action to take in any given state? The key lies in being able to quantify the long-term "value" of being in a state.

This lesson introduces one of the most fundamental concepts in reinforcement learning: the Bellman equation. You will derive this equation and understand its central role in defining value functions. We will first explore the version for evaluating a given policy, and then, crucially, the version for finding the optimal value function. This recursive equation forms the theoretical basis for a vast array of RL algorithms, and understanding it is essential for designing intelligent agents. Given your background in computer science, you'll see deep connections to dynamic programming and fixed-point iteration.

The Value Function and The Bellman Expectation Equation

The agent's goal is to maximize the cumulative discounted reward, which we call the return. The value of a state is defined as the expected return an agent can get starting from that state.

First, let's formalize the concepts of return and the state-value function, , which is the expected return starting in state and then following a specific policy .

RL Course by David Silver - Lecture 2: Markov Decision Process

David Silver's second lecture in his RL course provides a very clear and intuitive introduction to these concepts.

Please watch two segments from this video: First, watch the section on Markov Reward Processes. This will formally define the return as a discounted sum of future rewards and explain the role of the discount factor \gamma. Next, watch from the introduction of the value function. Focus on how the state-value function, V(s), is defined as the expected return, which accounts for the stochasticity of the environment.

As you saw, the return from time step is the discounted sum of future rewards:

The state-value function for a policy , denoted , is the expected return, conditioned on starting in state and following policy thereafter:

The core insight of Richard Bellman was that this value function has a recursive structure. The value of the current state can be expressed in terms of the value of the next state. This relationship is what's known as the Bellman equation.

Let's see the derivation for a policy .

RL Course by David Silver - Lecture 2: Markov Decision Process

The next part of David Silver's lecture provides a step-by-step derivation of this fundamental recursive relationship.

Watch the segment from "the Bellman equation". Follow the algebraic decomposition of the value function into two parts: the immediate reward and the discounted value of the successor state.

Let's formalize this. The decomposition you just saw is:

This is the Bellman expectation equation. It states that the value of being in state under policy is the immediate expected reward plus the discounted expected value of the next state, where the expectation is taken over the actions selected by the policy and the environment's transition dynamics.

Expanding the expectation, we get:

Similarly, we can define the action-value function, , which is the expected return after taking action in state and following policy thereafter. Its Bellman equation is:

For a finite number of states, the Bellman expectation equation defines a system of linear equations. If you have states, you have linear equations with unknowns (the values for each state). This system can be solved directly.

RL Course by David Silver - Lecture 2: Markov Decision Process

This part of the video shows how the Bellman equation can be expressed concisely in matrix form, leading to a direct analytical solution.

Watch the section on the matrix form of the Bellman equation. This provides a closed-form solution for V^\pi, which should feel familiar given your background in solving linear systems.

The direct solution is . While elegant, the complexity of matrix inversion makes it impractical for large state spaces. This motivates the iterative algorithms we will discuss later.

The Bellman Optimality Equation

Evaluating a policy is useful, but the ultimate goal is to find the best policy. This leads us to the concept of the optimal value function.

The optimal state-value function, , is the maximum value achievable from state over all possible policies.

Similarly, the optimal action-value function is .

The Bellman optimality equation provides a recursive relationship for these optimal value functions. It looks similar to the expectation equation, but with a crucial difference: a maximization operator.

Lecture 17 - MDPs & Value/Policy Iteration | Stanford CS229: Machine Learning Andrew Ng (Autumn2018)

Andrew Ng's lecture on MDPs provides an excellent, clear derivation of the Bellman optimality equation.

Watch the segment from "define V star". Pay close attention to how the max over actions is introduced to relate the optimal value of the current state to the optimal values of future states.

As derived by Ng, the Bellman optimality equation for is:

This equation says that the optimal value of a state is found by taking the action that maximizes the expected immediate reward plus the discounted optimal value of the resulting state .

This equation is the key to solving RL problems. If we can find , we can determine the optimal policy. The optimal policy is simply to take the action a that achieves the maximum in the Bellman optimality equation:

There is also a Bellman optimality equation for . It expresses the optimal value of a state-action pair in terms of the optimal values of the next state-action pairs:

Having makes finding the optimal policy even simpler: .

The following diagram provides a visual intuition for these recursive relationships.

This diagram shows the backup operations for the Bellman optimality equations. (a) To find \(V^*(s)\), you look one step ahead to the \(Q^*\) values for all actions and take the maximum. (b) To find \(Q^*(s,a)\), you look one step ahead to the \(V^*\) values of all possible next states and take an expectation. (c) Combining these gives a full backup for \(V^*\), where you maximize over actions and average over resulting states.

Unlike the Bellman expectation equation, the optimality equation is non-linear due to the max operator. We cannot solve it with a simple matrix inversion. This necessitates different solution methods, which rely on the theoretical properties of this equation.

The Theory of Contraction Mappings

Why can we be sure that iterative algorithms will converge to the unique optimal value function? The answer lies in the mathematics of fixed-point theory, specifically the Banach fixed-point theorem. This theorem states that if an operator is a "contraction," it has a unique fixed point, and applying the operator repeatedly will converge to that fixed point from any starting point.

The Bellman equations can be rewritten using operators. For example, the Bellman optimality equation becomes , where is the Bellman optimality operator.

1 Dynamic Programming Algorithms

This text provides a rigorous mathematical justification for why algorithms based on the Bellman equation converge. This aligns with your preference for deep theoretical underpinnings.

In Chapter 4 of this book, please read the following sections: Section 1.3, Fixed-Point Theory: Start at the beginning of the section and read up to the Python code example. Focus on understanding the definition of a fixed point and the statement of the Banach Fixed-Point Theorem, especially the concept of a contraction. The theorem guarantees existence, uniqueness, and convergence for fixed-point problems involving contraction mappings. Section 1.4, Bellman Policy Operator: Read the first few paragraphs, from the start of the section to just before Theorem 1.4.1. This part defines the Bellman Policy Operator Bπ and shows that it is a contraction under the L∞ norm. This proves that iterative policy evaluation converges to a unique V^\pi. Section 1.8, Bellman Optimality Operator: Read from the start of the section until just before Theorem 1.8.1. Here, the text defines the Bellman Optimality Operator B* and then proves it is also a contraction mapping. This is the theoretical guarantee that algorithms like Value Iteration (which we'll see next) converge to the unique V^*.

This theoretical foundation is powerful. It assures us that by repeatedly applying the Bellman optimality operator to an arbitrary initial value function, we will provably converge to the true optimal value function.

Case Study: Optimal Asset Allocation

Let's see the Bellman optimality equation applied to a concrete, non-trivial problem from finance: dynamic asset allocation. The goal is to decide how much wealth to allocate between a risky and a risk-free asset over time to maximize the utility of final wealth.

[PDF] Foundations of Reinforcement Learning with Applications in Finance

This section formulates a discrete-time asset allocation problem as an MDP and solves it using the Bellman optimality equation.

Please read Section 1.4, A Discrete-Time Asset-Allocation Example. Follow the derivation closely: MDP Formulation: Note how the state is wealth (W_t), the action is the amount invested in the risky asset (x_t), and the reward is given only at the final time step T. Bellman Equation: See how the Bellman optimality equation is written for this finite-horizon problem. Solution by Backward Induction: The problem is solved by starting at the end (time T-1) and working backward. They make an educated guess for the functional form of V^*_t(W_t) and use the Bellman equation to find the coefficients, ultimately yielding a closed-form solution for the optimal policy \pi^*_t.

This example is a classic application of dynamic programming. It beautifully illustrates how the Bellman equation allows us to break a complex, multi-step optimization problem into a sequence of simpler, single-step problems.

Conclusion

In this lesson, we have derived the central equations of reinforcement learning. These equations provide the link between the value of a state and the values of its potential successor states, allowing us to solve for the values that guide optimal decision-making.

Here are the key takeaways:

  • The Bellman Expectation Equation provides a recursive definition for the value function of a given policy . It forms a system of linear equations that can be solved directly for small state spaces.
  • The Bellman Optimality Equation defines the optimal value function , which gives the maximum possible return from any state. It is a non-linear equation due to the maximization over actions.
  • Bellman Operators and Contraction Mappings: The Bellman equations can be framed as finding the fixed point of an operator. Both the policy operator and the optimality operator are -contractions, which guarantees the existence of a unique fixed point ( or ) and the convergence of iterative solution methods.
  • Solving for the Optimal Policy: Finding is the primary goal, as the optimal policy can be easily extracted from it by choosing the action that maximizes the one-step lookahead value.

We now have the theoretical tools to find optimal policies, assuming we have a perfect model of the environment (the MDP). In our next lesson, we will introduce Q-learning, a powerful algorithm that leverages the Bellman optimality equation to learn optimal policies even without a model of the environment's dynamics, learning directly from experience.

Can't find a good explanation? Sign up and we'll make it for you

Sign up