No history yet

Bellman Equation Derivation

From Total Return to a Recursive Loop

You already know that the goal in reinforcement learning is to maximize the expected total return, GtG_t, from any given time step tt. It’s the sum of all future rewards, discounted by a factor γ\gamma at each step.

Gt=Rt+1+γRt+2+γ2Rt+3+=k=0γkRt+k+1G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}

This formula is correct, but it's not very practical for calculation. To make it useful, we need to rephrase it recursively. Notice that the expression for GtG_t contains a familiar-looking term. If we factor out γ\gamma from all terms except the first, we get something interesting.

Gt=Rt+1+γ(Rt+2+γRt+3+)G_t = R_{t+1} + \gamma (R_{t+2} + \gamma R_{t+3} + \dots)

The expression inside the parentheses is just the definition of the total return starting from the next time step, t+1t+1. This means we can rewrite the entire equation in a compact, recursive form.

Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}

The Bellman Expectation Equations

With our recursive definition of return, we can now define the value functions. The state-value function, Vπ(s)V_\pi(s), is the expected return an agent can get starting from state ss and then following policy π\pi. The action-value function, Qπ(s,a)Q_\pi(s, a), is the expected return from taking action aa in state ss and then following policy π\pi.

Let's derive the equation for Vπ(s)V_\pi(s). We start with its definition and substitute our recursive formula for GtG_t.

Vπ(s)=Eπ[GtSt=s]=Eπ[Rt+1+γGt+1St=s]=aπ(as)s,rp(s,rs,a)[r+γVπ(s)]\begin{aligned} V_\pi(s) &= \mathbb{E}_\pi [G_t | S_t=s] \\ &= \mathbb{E}_\pi [R_{t+1} + \gamma G_{t+1} | S_t=s] \\ &= \sum_a \pi(a|s) \sum_{s', r} p(s', r | s, a) [r + \gamma V_\pi(s')] \end{aligned}

This final line is the Bellman expectation equation for VπV_\pi. It expresses the value of a state in terms of the expected values of its successor states. It creates a system of linear equations, one for each state, whose unique solution is the value function VπV_\pi. A similar derivation gives us the equation for the action-value function, Qπ(s,a)Q_\pi(s, a).

Qπ(s,a)=s,rp(s,rs,a)[r+γaπ(as)Qπ(s,a)]Q_\pi(s, a) = \sum_{s', r} p(s', r | s, a) [r + \gamma \sum_{a'} \pi(a'|s') Q_\pi(s', a')]

The state-value and action-value functions are tightly linked. The value of a state is the expected value of the actions you can take from that state, weighted by your policy.

Vπ(s)=aπ(as)Qπ(s,a)V_\pi(s) = \sum_{a} \pi(a|s) Q_\pi(s,a)

Finding the Optimal Path

The expectation equations tell us the value of a given policy, but our goal is to find the best policy, π\pi_*. An optimal policy is one that achieves a value function greater than or equal to all other policies for all states. The corresponding optimal value functions are denoted V(s)V_*(s) and Q(s,a)Q_*(s, a).

To find these, we invoke the and introduce a max operator into our equations. The value of a state under an optimal policy must be equal to the expected return for the best action from that state.

V(s)=maxaQ(s,a)=maxas,rp(s,rs,a)[r+γV(s)]\begin{aligned} V_*(s) &= \max_a Q_*(s,a) \\ &= \max_a \sum_{s', r} p(s', r | s, a) [r + \gamma V_*(s')] \end{aligned}

This equation is the core of many reinforcement learning algorithms. Unlike the expectation equation, it is non-linear due to the max operator. Solving it requires iterative methods like value iteration or policy iteration, which are foundational to in RL.

Similarly, we can write the optimality equation for the action-value function.

Q(s,a)=s,rp(s,rs,a)[r+γmaxaQ(s,a)]Q_*(s, a) = \sum_{s', r} p(s', r | s, a) [r + \gamma \max_{a'} Q_*(s', a')]

Once we have Q(s,a)Q_*(s, a), determining the optimal policy is straightforward. For any state ss, the best action is the one that maximizes Q(s,a)Q_*(s, a). There is always at least one such action.

Ready to test your understanding?

Quiz Questions 1/5

What is the recursive formula for the expected total return, GtG_t, starting from time step tt?

Quiz Questions 2/5

The Bellman expectation equation for VπV_\pi is a system of linear equations, while the Bellman optimality equation for VV_* is non-linear.

These equations provide the theoretical foundation for finding optimal policies in any finite Markov Decision Process, forming the bedrock of modern reinforcement learning.