Skip to content
beginner

Deriving Bellman Equations and Value Updates With a Worked Example

You can read $V(s) = R + \gamma V(s')$ a hundred times and still freeze when a variant shows up with a max, an expectation, or a $Q$ instead of a $V$. The…

Published 2026-10-02Updated 2026-10-048 min read
Macro view of metallic gears with striking blue and red lighting showcasing precision engineering.
Macro view of metallic gears with striking blue and red lighting showcasing precision engineering. Photo by Rafael Minguet Delgado on Pexels.

You can read V(s)=R+γV(s′)V(s) = R + \gamma V(s') a hundred times and still freeze when a variant shows up with a max, an expectation, or a QQ instead of a VV. The formula is not the hard part. The hard part is that nobody showed you where it came from.

So let's derive it. We start from the definition of return, take one step of algebra at a time, and finish with a number you can check on paper. If you already have the intuition that value predicts future reward and that a Bellman relationship is one-step reward plus discounted continuation, you have everything you need. This article shows where that structure is forced to exist.

What You Need Before the Derivation

A derivation with hidden steps is just a formula wearing a costume. So we set up notation and assumptions first, and we name what breaks when they fail.

SymbolMeaning
s,as, acurrent state, current action
rrreward received
s′s'next state
π(a∣s)\pi(a \mid s)probability the policy picks action aa in state ss
P(s′∣s,a)P(s' \mid s, a)probability the environment lands in s′s' after aa in ss
γ\gammadiscount factor, 0≤γ<10 \le \gamma < 1 in continuing tasks

The return GtG_t is the discounted sum of everything the agent collects from time step tt onward:

Gt=Rt+1+γRt+2+γ2Rt+3+⋯G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots

That definition hides the single most useful fact in this whole article. Factor out the first term:

Gt=Rt+1+γ(Rt+2+γRt+3+⋯ )=Rt+1+γGt+1G_t = R_{t+1} + \gamma \left( R_{t+2} + \gamma R_{t+3} + \cdots \right) = R_{t+1} + \gamma G_{t+1}

This one line is the engine. Everything else is bookkeeping.

Two assumptions make the recursion valid. First, the Markov property: the next state and reward depend only on the current state and action, not on the history that produced them. Second, either the task is episodic with terminal states that end the sum, or it is continuing with γ<1\gamma < 1 so the infinite sum stays finite under bounded rewards.

Warning: These are standard sufficient conditions, not universal laws. If your state representation is not Markov, the recursion breaks for that state variable — but you can repair it by augmenting the state with the missing history. And γ=1\gamma = 1 in a continuing task does not automatically diverge; finiteness depends on the reward structure and the task itself. The safe default is γ<1\gamma < 1 with bounded rewards.

Knowledge check

Check your understanding

Answer this question before you continue.

For a continuing task with bounded rewards, which assumption set matches the article’s safe conditions for using the return recursion?
Misconception Check

Focus: Identify the conditions the article uses to justify the one-step return recursion in the stated setting.

From Return to the Bellman Expectation Equation

The state value under policy π\pi is the expected return starting from ss:

Vπ(s)=E[Gt∣St=s]V^\pi(s) = \mathbb{E}\left[ G_t \mid S_t = s \right]

Substitute the recursive identity for GtG_t:

Vπ(s)=E[Rt+1+γGt+1∣St=s]V^\pi(s) = \mathbb{E}\left[ R_{t+1} + \gamma G_{t+1} \mid S_t = s \right]

Expectation is linear, so split it:

Vπ(s)=E[Rt+1∣St=s]+γ E[Gt+1∣St=s]V^\pi(s) = \mathbb{E}\left[ R_{t+1} \mid S_t = s \right] + \gamma \, \mathbb{E}\left[ G_{t+1} \mid S_t = s \right]

Now expand each expectation over the two sources of randomness: the policy chooses aa, then the environment chooses s′s'. The first term becomes a sum over actions and next states weighted by their probabilities. The second term is the key move — the expected return from the next state is exactly Vπ(s′)V^\pi(s') by definition:

Vπ(s)=∑aπ(a∣s)∑s′P(s′∣s,a)[R(s,a)+γVπ(s′)]V^\pi(s) = \sum_a \pi(a \mid s) \sum_{s'} P(s' \mid s, a) \left[ R(s, a) + \gamma V^\pi(s') \right]

That is the Bellman expectation equation. Notice what happened: the unknown VπV^\pi appears on both sides. The equation is self-referential, which is what makes it a Bellman equation rather than a plain sum.

The action value follows the same logic, except the first action is fixed rather than sampled from the policy:

Qπ(s,a)=R(s,a)+γ∑s′P(s′∣s,a) Vπ(s′)Q^\pi(s, a) = R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) \, V^\pi(s')

And the two quantities bridge into each other. Average QQ over the policy's action choices and you recover VV:

Vπ(s)=∑aπ(a∣s) Qπ(s,a)V^\pi(s) = \sum_a \pi(a \mid s) \, Q^\pi(s, a)

Note: The expectation equation is a consistency condition, not an algorithm. It says what the values must satisfy. It does not say how to find them.

Knowledge check

Check your understanding

Answer this question before you continue.

When expanding the expected one-step return for a fixed policy, how are the possible first actions and next states combined?
Single Choice

Focus: Explain how the policy and environment probabilities enter the Bellman expectation equation for state value.

Where the Max Comes From: The Bellman Optimality Equation

The expectation equation describes a fixed policy. The optimality equation describes the best policy available, and the difference between them is one operator.

Define V∗(s)V^*(s) as the best return achievable from ss over all policies, and Q∗(s,a)Q^*(s, a) as the best return when you commit to action aa first and then act optimally forever after.

The argument is short. From state ss, the first action is yours to choose. Everything after that is, by definition, optimal. So you pick the action that maximizes immediate reward plus discounted optimal continuation:

V∗(s)=max⁡a[R(s,a)+γ∑s′P(s′∣s,a) V∗(s′)]V^*(s) = \max_a \left[ R(s, a) + \gamma \sum_{s'} P(s' \mid s, a) \, V^*(s') \right]

For action values, the max moves one step later, because the first action is already fixed:

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')

Put the two families side by side:

Expectation equationOptimality equation
Operatoraverage over π(a∣s)\pi(a \mid s)max over aa
Describesvalue of a fixed policybest achievable value
Used forpolicy evaluationcontrol

Same skeleton, one operator swapped. Once you have Q∗Q^*, the optimal policy is greedy with respect to it: a∗(s)=arg⁡max⁡aQ∗(s,a)a^*(s) = \arg\max_a Q^*(s, a).

Common mistake: Treating the optimality equation as a method. It assumes you can evaluate every action in every state. When the model is unknown, the equation tells you the target but not how to reach it.

Knowledge check

Check your understanding

Answer this question before you continue.

In the optimality equation for Q*(s,a), why is the first action not inside the maximization?
Comparison Reasoning

Focus: Distinguish where action maximization appears in optimal state-value and action-value equations.

A Worked Value Update You Can Check by Hand

For state A, the left-action target is −5.5 and the right-action target is −1. The expectation backup averages them to −3.25, while the optimality backup selects −1. An estimate of −5 moves partway toward the expectation target, reaching −4.125.
The same action targets produce different Bellman targets: average under the policy, or take the maximum for optimality.

Theory earns trust when the arithmetic agrees. Here is a two-state setup small enough to verify with a calculator.

State A has two actions. Left stays in A. Right moves to terminal state B. Every transition gives a reward of −1-1, transitions are deterministic, and γ=0.9\gamma = 0.9. The policy is a coin flip: π(left∣A)=0.5\pi(\text{left} \mid A) = 0.5 and π(right∣A)=0.5\pi(\text{right} \mid A) = 0.5. Suppose the current estimate is V(A)=−5V(A) = -5.

Expectation backup. Plug the current estimate into the right-hand side of the expectation equation. This evaluated right-hand side is the backup target, written TπV(A)T^\pi V(A):

TπV(A)=0.5[−1+0.9⋅V(A)]+0.5[−1+0.9⋅0]T^\pi V(A) = 0.5 \left[ -1 + 0.9 \cdot V(A) \right] + 0.5 \left[ -1 + 0.9 \cdot 0 \right]

The right branch ends at a terminal state, so it contributes no continuation term — that is why the recursion bottoms out. Evaluate:

TπV(A)=0.5(−1−4.5)+0.5(−1)=−2.75−0.5=−3.25T^\pi V(A) = 0.5(-1 - 4.5) + 0.5(-1) = -2.75 - 0.5 = -3.25

This is not the true value Vπ(A)V^\pi(A). It is the one-step target computed from the current estimate. The true value is the fixed point where the estimate equals its own backup — a different number that you would reach only after many sweeps.

Optimality backup. Same state, but now the agent picks the better action:

T∗V(A)=max⁡[−1+0.9⋅V(A),  −1+0.9⋅0]=max⁡(−5.5,  −1)=−1T^* V(A) = \max\left[ -1 + 0.9 \cdot V(A), \; -1 + 0.9 \cdot 0 \right] = \max(-5.5, \; -1) = -1

The difference between −3.25-3.25 and −1-1 is the max. The expectation equation averages the two branches; the optimality equation refuses the worse one.

The update. In practice you rarely jump straight to the target. You move toward it:

new estimate=old estimate+α(target−old estimate)\text{new estimate} = \text{old estimate} + \alpha \left( \text{target} - \text{old estimate} \right)

With α=0.5\alpha = 0.5 and the expectation target, the new estimate is −5+0.5(−3.25−(−5))=−4.125-5 + 0.5(-3.25 - (-5)) = -4.125. The estimate moves toward the target. Repeat the sweep and it keeps moving, converging to the fixed point of the expectation equation — not to −3.25-3.25 in one step.

A backup diagram makes the flow visible: open circles for states, filled circles for state-action pairs, lines for actions and transitions. Values flow backward from s′s' into ss along those lines.

Knowledge check

Check your understanding

Answer this question before you continue.

In the worked example, with V(A) = -5 and α = 0.5, what is the new estimate after using the expectation backup?
Output Prediction

Focus: Calculate the expectation backup target and distinguish it from a partial value update.

The expectation target is TπV(A) = -3.25. The update is new estimate = old estimate + α(target - old estimate).

What the Derivation Buys You

The equations are the reason dynamic programming, TD learning, and Q-learning look like relatives. Each one approximates the same fixed-point condition and substitutes something different for the expectation.

When you know the model — PP and RR — you can sweep the equations directly. When you do not, you replace the expectation with a sampled transition and get a stochastic update. The expectation equation is the target for policy evaluation; the optimality equation is the target for control. That is the prediction/control split written as algebra.

Common mistake: Treating the Bellman equation as a definition of value rather than a constraint value must satisfy. Early in training, estimates are wrong. The equation is not broken — your estimate has not converged to it yet.

And know when not to lean on these equations: large or continuous state spaces where exact sweeps are impossible, and settings where your state representation violates the Markov property.

When you meet a new value-based algorithm, ask two questions. Which Bellman relationship is it approximating? And what does it substitute for the expectation — a model, a sample, or a learned critic? Those two answers tell you more about the method than its name does.

Your next move: close this article and re-derive the optimality equation from memory on the two-state example above. If the max lands in the right place without looking, you own the derivation. If it does not, you now know exactly which line to re-read.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

A learner’s current value estimates do not yet equal their Bellman backups. Which interpretation matches the article?
Question 1 of 2Scenario Interpretation

Focus: Interpret a Bellman equation as a value constraint and distinguish it from an update algorithm.

A value-learning method lacks a known transition model and uses observed transitions instead. How does the article characterize this choice?
Question 2 of 2Comparison Reasoning

Focus: Relate model-based sweeps and sampled updates to their shared Bellman target.

References

  1. Part 1: Key Concepts in RL — Spinning Up documentationspinningup.openai.com
  2. Reinforcement learning Derivation from Bellman Equationwww.snn.ru.nl
  3. Bellman Equations and Dynamic Programmingwww.dailydoseofds.com
Practical resource

Build a stronger RL mental model

Use structured resources to connect equations, algorithms, and observable agent behavior.

Browse resources
Related sites

Continue across related AI foundations

Use LearnPyFast for Python foundations and LearnLLMFast for practical language-model and agent application concepts.

Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related reinforcement learning tutorials

Continue with nearby RL concepts, algorithms, and experiments that build on the same decision process.