Simple Algorithm
Reinforcement Learning · Bootstrapping · 24 min

Value Functions & the Bellman Equations

What states and actions are worth, and the recursive equations that pin values down.

A chess position, a battery level, a cell in a maze: how good is it to be there? A value function answers with the return you can expect, and the Bellman equations pin every value down with one local rule: a state is worth its next reward plus the discounted worth of where you land. This lesson derives the equations, solves them by hand and as linear algebra, and shows how the optimal ones single out the best policy.

What you’ll learn

  • Define the state-value, action-value and advantage functions VπV^\pi, QπQ^\pi and AπA^\pi, and convert between them.
  • Derive the Bellman expectation equations from the return recursion, and read the four Bellman equations as backup diagrams.
  • Solve a small MDP's Bellman equations by hand and in matrix form, Vπ=(I−γPπ)−1rπV^\pi = (I-\gamma P^\pi)^{-1}r^\pi, and explain why the inverse exists.
  • State the Bellman optimality equations, and act optimally from Q⋆Q^\star without a model, or from V⋆V^\star with one.

What a state is worth

The return GtG_t is a random variable: the same policy from the same state can be lucky or unlucky. A value function averages the luck away. For a stationary policy π\pi in an MDP (S,A,P,r,γ,μ)(\mathcal S, \mathcal A, P, r, \gamma, \mu), the state-value function and the action-value function are

Vπ(s)=Eπ[Gt∣St=s],Qπ(s,a)=Eπ[Gt∣St=s, At=a].V^\pi(s) = \E_\pi\big[G_t \mid S_t = s\big],\qquad Q^\pi(s,a) = \E_\pi\big[G_t \mid S_t = s,\ A_t = a\big].

Qπ(s,a)Q^\pi(s,a) commits to action aa for one step and follows π\pi afterward. Neither depends on tt: the dynamics and the policy are the same at every step and the horizon is infinite, so the future looks the same whenever you arrive in ss.

The running example of this lesson is the two-state MDP of the value-space widget. In s1s_1, stay pays 1 and stays; go pays 0 and moves to s2s_2. In s2s_2, stay pays 2 and stays; go pays 0 and moves back to s1s_1. Moves are deterministic and γ=0.9\gamma = 0.9. A deterministic policy is a pair (action in s1s_1, action in s2s_2). Under (stay, stay) the agent earns 1 per step forever from s1s_1, so V(s1)=1/(1−0.9)=10V(s_1) = 1/(1-0.9) = 10, and 2 per step from s2s_2, so V(s2)=20V(s_2) = 20.

Check your understanding

Under (stay, stay) the values are V=(10,20)V = (10, 20). What is Q(s1,go)Q(s_1, \text{go}), the value of going once from s1s_1 and then staying forever?

  1. 00
  2. 1818
  3. 1010
  4. 2020
Show the answer

B. 1818

Going pays 0 now and lands in s2s_2, which is worth 20 under the policy: Q(s1,go)=0+0.9⋅20=18Q(s_1, \text{go}) = 0 + 0.9 \cdot 20 = 18. The advantage is A(s1,go)=18−10=8>0A(s_1, \text{go}) = 18 - 10 = 8 > 0: deviating once beats the policy's own choice.

Think first Under (stay, stay), going from s1s_1 has advantage +8+8. Going from s2s_2 has Q(s2,go)=0+0.9⋅10=9Q(s_2, \text{go}) = 0 + 0.9 \cdot 10 = 9, an advantage of 9−20=−119 - 20 = -11. What should a policy that trusts these numbers do?

Switch to go in s1s_1 and keep stay in s2s_2. A positive advantage means one deviation pays, and the policy improvement theorem says that making every such switch at once can only help, in every state. The new policy (go, stay) is worth (18,20)(18, 20), and it is optimal. The Fixed Point lesson Advantages & the Performance Difference Lemma computes exactly how much any switch gains.

The Bellman expectation equations

The return obeys Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} (Agents, Environments & Rewards). Taking expectations turns this identity about one trajectory into an equation that links the values of neighboring states: the Bellman equation.

DerivationFrom the return recursion to the Bellman equation
  1. Vπ(s)=Eπ[Rt+1∣St=s]+γ Eπ[Gt+1∣St=s]V^\pi(s) = \E_\pi\big[R_{t+1} \mid S_t = s\big] + \gamma\,\E_\pi\big[G_{t+1} \mid S_t = s\big]

    Condition the recursion on the current state and use linearity of expectation:

  2. Eπ[Gt+1∣St=s]=∑aπ(a∣s)∑s′P(s′∣s,a) Eπ[Gt+1∣St+1=s′]=∑aπ(a∣s)∑s′P(s′∣s,a) Vπ(s′)\E_\pi\big[G_{t+1} \mid S_t = s\big] = \sum_a \pi(a \mid s)\sum_{s'}P(s' \mid s,a)\,\E_\pi\big[G_{t+1} \mid S_{t+1} = s'\big] = \sum_a \pi(a \mid s)\sum_{s'}P(s' \mid s,a)\,V^\pi(s')

    Split the second term by the action and the next state. Given St+1=s′S_{t+1} = s', the Markov property and the stationarity of π\pi make the expected return from t+1t+1 on equal to Vπ(s′)V^\pi(s'), whatever happened before:

  3. Vπ(s)=∑aπ(a∣s)[r(s,a)+γ∑s′P(s′∣s,a) Vπ(s′)]for every s∈SV^\pi(s) = \sum_a \pi(a \mid s)\Big[r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,V^\pi(s')\Big]\qquad\text{for every } s \in \mathcal S

    The first term is the policy's expected reward, ∑aπ(a∣s) r(s,a)\sum_a\pi(a \mid s)\,r(s,a). Collect terms. This is the Bellman expectation equation, one equation per state:

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

    The same argument conditioned on (St,At)=(s,a)(S_t, A_t) = (s, a) gives the equation for action values. The policy's average now sits at the next state:

EquationRootFirst branchingSecond branchingLeaves
VπV^\pi, expectationa state ssactions, averaged with weights π(a∣s)\pi(a \mid s)next states, averaged with P(s′∣s,a)P(s' \mid s,a), after the reward r(s,a)r(s,a)Vπ(s′)V^\pi(s')
QπQ^\pi, expectationa pair (s,a)(s,a)next states, averaged with P(s′∣s,a)P(s' \mid s,a), after r(s,a)r(s,a)next actions, averaged with π(a′∣s′)\pi(a' \mid s')Qπ(s′,a′)Q^\pi(s',a')
V⋆V^\star, optimalitya state ssactions, maximizednext states, averaged with PPV⋆(s′)V^\star(s')
Q⋆Q^\star, optimalitya pair (s,a)(s,a)next states, averaged with PPnext actions, maximizedQ⋆(s′,a′)Q^\star(s',a')
Sutton and Barto draw each Bellman equation as a backup diagram: a root, a level of action branches and a level of state branches, with values flowing up from the leaves. The four diagrams differ only in where they average and where they maximize. A backup computes the root from the leaves; every algorithm in this course is built from backups, computed exactly from a model or estimated from samples.

Write rπ(s)=∑aπ(a∣s) r(s,a)r^\pi(s) = \sum_a \pi(a \mid s)\,r(s,a) and Pπ(s,s′)=∑aπ(a∣s) P(s′∣s,a)P^\pi(s,s') = \sum_a \pi(a \mid s)\,P(s' \mid s,a) for the reward vector and transition matrix of the chain the policy induces (Markov Decision Processes). Then the equations of all states together read Vπ=rπ+γPπVπV^\pi = r^\pi + \gamma P^\pi V^\pi: SS linear equations in SS unknowns.

Predict before you look

Solve the Bellman equations of the policy (stay, go) by hand: stay in s1s_1, go from s2s_2. What is V(s2)V(s_2)?

Show the answer

9

The equations are V(s1)=1+0.9 V(s1)V(s_1) = 1 + 0.9\,V(s_1) and V(s2)=0+0.9 V(s1)V(s_2) = 0 + 0.9\,V(s_1). The first involves only V(s1)V(s_1) and gives V(s1)=10V(s_1) = 10; then V(s2)=9V(s_2) = 9. State s2s_2 pays nothing under this policy: its whole value is the discounted value of where its action leads.

Solving the equations with linear algebra

DerivationThe value of a policy in closed form
  1. (I−γPπ) Vπ=rπ(I - \gamma P^\pi)\,V^\pi = r^\pi

    Move the unknowns to one side:

  2. Vπ=(I−γPπ)−1rπ=∑t≥0γt (Pπ)t rπV^\pi = (I - \gamma P^\pi)^{-1}r^\pi = \sum_{t\ge0}\gamma^t\,(P^\pi)^t\,r^\pi

    PπP^\pi is a stochastic matrix, with nonnegative entries and rows summing to 1, so ∥Pπx∥∞≤∥x∥∞\norm{P^\pi x}_\infty \le \norm{x}_\infty for every vector xx. If (I−γPπ)x=0(I - \gamma P^\pi)x = 0, then ∥x∥∞=γ∥Pπx∥∞≤γ∥x∥∞\norm{x}_\infty = \gamma\norm{P^\pi x}_\infty \le \gamma\norm{x}_\infty, which forces x=0x = 0. So I−γPπI - \gamma P^\pi is invertible whenever γ<1\gamma < 1, and the Neumann series gives the inverse:

  3. Each term has a meaning: ((Pπ)trπ)(s)\big((P^\pi)^t r^\pi\big)(s) is the expected reward tt steps after starting in ss. The closed form is the definition of value, summed in one go, the same expression Markov Decision Processes reached by summing expected rewards over time.

For (go, stay), both states lead to s2s_2, and the matrices are small enough to invert by hand:

Pπ=(0101),rπ=(02),(I−0.9 Pπ)−1=(1−0.900.1)−1=(19010),Vπ=(1820)P^\pi = \begin{pmatrix}0 & 1\\ 0 & 1\end{pmatrix},\quad r^\pi = \begin{pmatrix}0\\ 2\end{pmatrix},\quad (I - 0.9\,P^\pi)^{-1} = \begin{pmatrix}1 & -0.9\\ 0 & 0.1\end{pmatrix}^{-1} = \begin{pmatrix}1 & 9\\ 0 & 10\end{pmatrix},\quad V^\pi = \begin{pmatrix}18\\ 20\end{pmatrix}
PπP^\pi itself is singular (two equal rows), yet I−γPπI - \gamma P^\pi is not: invertibility comes from γ<1\gamma < 1, not from the chain. The entry 9 is the discounted number of future visits to s2s_2 starting from s1s_1, the successor representation of the Fixed Point lesson Successor Representations & Features.
python
import numpy as np

gamma = 0.9
# P[s, a, s'] and r[s, a] for the two-state MDP; action 0 = stay, 1 = go
P = np.zeros((2, 2, 2))
P[0, 0, 0] = P[0, 1, 1] = P[1, 0, 1] = P[1, 1, 0] = 1.0
r = np.array([[1.0, 0.0],
              [2.0, 0.0]])

def evaluate(pi):
    """Exact policy evaluation; pi[s, a] holds the action probabilities."""
    P_pi = np.einsum("sa,sat->st", pi, P)
    r_pi = (pi * r).sum(axis=1)
    return np.linalg.solve(np.eye(2) - gamma * P_pi, r_pi)

stay, go = [1.0, 0.0], [0.0, 1.0]
for name, pi in [("(stay, stay)", [stay, stay]), ("(go, stay)", [go, stay]),
                 ("(stay, go)", [stay, go]), ("(go, go)", [go, go])]:
    print(name, evaluate(np.array(pi)))

The four deterministic policies are worth (10,20)(10, 20), (18,20)(18, 20), (10,9)(10, 9) and (0,0)(0, 0): the corners of the value-space widget. A linear solve costs O(S3)O(S^3), fine for thousands of states and hopeless for millions; Policy Evaluation in the Planning topic replaces it by repeated backups.

Think first The uniform random policy picks stay or go with probability ½ in both states. What are its values? (Hint: subtract the two Bellman equations.)

The equations are V(s1)=12(1+0.9V(s1))+12(0.9V(s2))V(s_1) = \tfrac12\big(1 + 0.9V(s_1)\big) + \tfrac12\big(0.9V(s_2)\big) and V(s2)=12(2+0.9V(s2))+12(0.9V(s1))V(s_2) = \tfrac12\big(2 + 0.9V(s_2)\big) + \tfrac12\big(0.9V(s_1)\big). Subtracting cancels the discounted terms and leaves V(s2)−V(s1)=0.5V(s_2) - V(s_1) = 0.5. Substituting back gives 0.1 V(s1)=0.7250.1\,V(s_1) = 0.725, so V=(7.25,7.75)V = (7.25, 7.75), less than half of what the best policy earns.

The Bellman optimality equations

The best you can do from each state defines the optimal value functions

V⋆(s)=max⁡πVπ(s),Q⋆(s,a)=max⁡πQπ(s,a).V^\star(s) = \max_\pi V^\pi(s),\qquad Q^\star(s,a) = \max_\pi Q^\pi(s,a).

A priori each state could need its own maximizing policy. It does not: in a finite discounted MDP a single stationary deterministic policy attains both maxima in every state at once (Policies & Why Simple Ones Suffice). Its values satisfy the Bellman equation of a policy that always picks a best action, so the average over actions becomes a maximum:

V⋆(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a) V⋆(s′)],Q⋆(s,a)=r(s,a)+γ∑s′P(s′∣s,a) max⁡a′Q⋆(s′,a′)V^\star(s) = \max_a\Big[r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,V^\star(s')\Big],\qquad Q^\star(s,a) = r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,\max_{a'}Q^\star(s',a')
The Bellman optimality equations. The maximum makes them nonlinear, so there is no matrix inverse: they are solved by value iteration, policy iteration or linear programming, and their solution is unique, as Bellman Operators & Contraction proves.

Given Q⋆Q^\star, acting optimally takes no further thought: any greedy policy π⋆(s)∈arg max⁡aQ⋆(s,a)\pi^\star(s) \in \argmax_a Q^\star(s,a) is optimal. Given only V⋆V^\star, the agent must still look one step ahead, π⋆(s)∈arg max⁡a[r(s,a)+γ∑s′P(s′∣s,a) V⋆(s′)]\pi^\star(s) \in \argmax_a\big[r(s,a) + \gamma\sum_{s'}P(s' \mid s,a)\,V^\star(s')\big], and that needs the rewards and transition probabilities. This one difference is why model-free control methods such as Q-learning learn action values rather than state values.

StateQ⋆(⋅,stay)Q^\star(\cdot, \text{stay})Q⋆(⋅,go)Q^\star(\cdot, \text{go})V⋆=max⁡V^\star = \maxGreedy action
s1s_11+0.9⋅18=17.21 + 0.9 \cdot 18 = 17.20+0.9⋅20=180 + 0.9 \cdot 20 = 181818go
s2s_22+0.9⋅20=202 + 0.9 \cdot 20 = 200+0.9⋅18=16.20 + 0.9 \cdot 18 = 16.22020stay
Checking that V⋆=(18,20)V^\star = (18, 20) solves the optimality equations of the two-state MDP: each row's maximum reproduces its value. Going from s1s_1 gives up 1 now to earn 2 per step later, which pays whenever 2γ>12\gamma > 1.
Check your understanding

A warehouse robot knows the exact optimal state values V⋆V^\star of its MDP but has no model of what its actions do. Can it act optimally?

  1. Yes: it moves toward the neighboring state with the highest V⋆V^\star
  2. Yes: V⋆V^\star and Q⋆Q^\star carry the same information about actions
  3. Not reliably: turning V⋆V^\star into a choice needs r(s,a)r(s,a) and P(s′∣s,a)P(s' \mid s,a) for the one-step lookahead, while Q⋆Q^\star would let it take arg max⁡aQ⋆(s,a)\argmax_a Q^\star(s,a) directly
  4. No: optimal behavior needs the full model even when Q⋆Q^\star is known
Show the answer

C. Not reliably: turning V⋆V^\star into a choice needs r(s,a)r(s,a) and P(s′∣s,a)P(s' \mid s,a) for the one-step lookahead, while Q⋆Q^\star would let it take arg max⁡aQ⋆(s,a)\argmax_a Q^\star(s,a) directly

V⋆V^\star ranks states, not actions. To choose, the agent must predict where each action leads and what it pays on the way, which is exactly the model it lacks. Q⋆Q^\star has that lookahead built in, which is why model-free control learns action values.

Both equations at work: policy iteration

The two kinds of equation do different jobs. The expectation equations evaluate a given policy; the optimality equations characterize the best one. Alternating them is policy iteration: solve the expectation equations of the current policy, then make the policy greedy with respect to the resulting QπQ^\pi. A greedy step only picks actions with nonnegative advantage, so no state gets worse, and since there are finitely many deterministic policies the process stops, at a policy whose values satisfy the optimality equation. The Planning topic develops it in full.

Key takeaways

  • Vπ(s)V^\pi(s) and Qπ(s,a)Q^\pi(s,a) are expected returns; Vπ=∑aπ QπV^\pi = \sum_a \pi\,Q^\pi, Qπ=r+γPVπQ^\pi = r + \gamma P V^\pi, and the advantage Aπ=Qπ−VπA^\pi = Q^\pi - V^\pi averages to zero under π\pi.
  • Taking expectations of Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}, with the Markov property, gives the Bellman expectation equation Vπ=rπ+γPπVπV^\pi = r^\pi + \gamma P^\pi V^\pi, solved exactly by Vπ=(I−γPπ)−1rπV^\pi = (I-\gamma P^\pi)^{-1}r^\pi.
  • The optimality equations replace the policy's average over actions by a maximum; they are nonlinear and are solved by iteration, policy iteration or linear programming.
  • A policy greedy with respect to Q⋆Q^\star is optimal and needs no model; acting from V⋆V^\star needs a one-step lookahead with the model.
  • Policy iteration alternates the two kinds of equation: evaluate the policy exactly, make it greedy, and stop when nothing changes.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In state ss a policy takes a1a_1 with probability 0.25 and a2a_2 with probability 0.75, and Qπ(s,a1)=12Q^\pi(s, a_1) = 12, Qπ(s,a2)=8Q^\pi(s, a_2) = 8. What are Vπ(s)V^\pi(s) and Aπ(s,a1)A^\pi(s, a_1)?

  1. Vπ(s)=10V^\pi(s) = 10 and Aπ(s,a1)=2A^\pi(s,a_1) = 2
  2. Vπ(s)=9V^\pi(s) = 9 and Aπ(s,a1)=3A^\pi(s,a_1) = 3
  3. Vπ(s)=12V^\pi(s) = 12 and Aπ(s,a1)=0A^\pi(s,a_1) = 0
  4. Vπ(s)=9V^\pi(s) = 9 and Aπ(s,a1)=−3A^\pi(s,a_1) = -3
Show the answer

B. Vπ(s)=9V^\pi(s) = 9 and Aπ(s,a1)=3A^\pi(s,a_1) = 3

Vπ(s)=0.25⋅12+0.75⋅8=9V^\pi(s) = 0.25 \cdot 12 + 0.75 \cdot 8 = 9, so Aπ(s,a1)=12−9=3A^\pi(s,a_1) = 12 - 9 = 3 and Aπ(s,a2)=−1A^\pi(s,a_2) = -1. Check: 0.25⋅3+0.75⋅(−1)=00.25 \cdot 3 + 0.75 \cdot (-1) = 0, as the policy average of the advantage must be.

Question 2

Why can VπV^\pi be computed with one linear solve, while V⋆V^\star cannot?

  1. Because V⋆V^\star depends on the start distribution μ\mu
  2. Because PπP^\pi is invertible and PP is not
  3. Because V⋆V^\star is not unique
  4. Because the expectation equations average over actions with fixed weights π(a∣s)\pi(a \mid s), which is linear in VV, while the optimality equations take a maximum, which is not
Show the answer

D. Because the expectation equations average over actions with fixed weights π(a∣s)\pi(a \mid s), which is linear in VV, while the optimality equations take a maximum, which is not

For a fixed π\pi, the map V↦rπ+γPπVV \mapsto r^\pi + \gamma P^\pi V is affine, so its fixed point solves (I−γPπ)V=rπ(I - \gamma P^\pi)V = r^\pi. The maximum in the optimality equation is piecewise linear, with pieces given by the greedy policy, so V⋆V^\star is found by iterating, by policy iteration (one linear solve per policy), or by linear programming.

Question 3

In the backup diagram of the Bellman optimality equation for Q⋆(s,a)Q^\star(s,a), where does the maximum sit?

  1. At the root, over the action aa of the pair (s,a)(s,a)
  2. Over the next actions a′a', after averaging over the next states
  3. Over the next states s′s'
  4. Nowhere: Q⋆Q^\star is an average
Show the answer

B. Over the next actions a′a', after averaging over the next states

Q⋆(s,a)Q^\star(s,a) fixes the first action, averages over where it leads with P(s′∣s,a)P(s' \mid s,a), and then assumes the best next action, max⁡a′Q⋆(s′,a′)\max_{a'}Q^\star(s',a'). What the environment decides is averaged; what the agent decides is maximized.

Question 4

A policy π\pi is not optimal, and π′\pi' is greedy with respect to QπQ^\pi. What does the policy improvement theorem guarantee?

  1. π′\pi' is optimal
  2. π′\pi' is better than π\pi in every state
  3. Vπ′(s)≥Vπ(s)V^{\pi'}(s) \ge V^\pi(s) in every state, with strict inequality in at least one state
  4. Nothing: a greedy step can make some states worse
Show the answer

C. Vπ′(s)≥Vπ(s)V^{\pi'}(s) \ge V^\pi(s) in every state, with strict inequality in at least one state

Greedy actions have Aπ(s,π′(s))=max⁡aAπ(s,a)≥0A^\pi(s,\pi'(s)) = \max_a A^\pi(s,a) \ge 0, so no state gets worse. If π\pi is not optimal, some state has an action with positive advantage, and there π′\pi' is strictly better. That is why policy iteration terminates at an optimal policy: it can only stop when no advantage is positive.

Go deeper

Further reading

  • R. S. Sutton & A. G. Barto, Reinforcement Learning: An Introduction, ch. 3–4 (MIT Press, 2nd ed., 2018).
  • R. Bellman, Dynamic Programming (Princeton University Press, 1957).
  • M. L. Puterman, Markov Decision Processes: Discrete Stochastic Dynamic Programming, ch. 6 (Wiley, 1994).
  • C. Szepesvári, Algorithms for Reinforcement Learning (Morgan & Claypool, 2010).