Pearl
Lab

Chapter 02 · Foundations

Value functions & Bellman equations

Abstract. An MDP specifies the rules of interaction. A value function measures how good it is to be in a state — or to take an action in a state — under a given policy. The Bellman equation is the recursive identity that makes values computable: today’s value equals expected immediate reward plus discounted value of tomorrow. This chapter defines \(V^{\pi}\) and \(Q^{\pi}\), derives expectation and optimality equations line by line, states the policy improvement idea, and sketches why the Bellman operator has a unique fixed point. The laboratory runs iterative policy evaluation on a small grid so you can watch the numbers converge.

1. Overview

Chapter 01 gave you the object \(\mathcal{M}=(S,A,P,R,\gamma)\) and the goal \(\max_\pi \mathbb{E}_\pi[G_0]\). To optimize that goal you need a measuring stick: for each situation, how much return should you expect?

That measuring stick is a value function. Once values exist, a remarkable algebraic fact appears: they satisfy a self-consistency equation — Bellman’s equation — relating the value at \(s\) to values at successors \(s'\). Dynamic programming (next chapter) iterates that equation. Temporal-difference learning estimates it from samples. Q-learning replaces expectation over \(\pi\) by a \(\max\). Policy gradients still use values (or advantages) as critics. The entire stack hangs on this chapter.

There is nothing mystical in the recursion. It is simply \(G_t = r_{t+1} + \gamma G_{t+1}\) after taking expectations. Memorize that one-line unrolling; every formula below is bookkeeping around it.

2. Definitions of \(V^{\pi}\) and \(Q^{\pi}\)

Definition 2.1 · State-value function

For a policy \(\pi\), the state-value function is the expected return starting from \(s\) and following \(\pi\) thereafter: \[ V^{\pi}(s) \;=\; \mathbb{E}_{\pi}\!\left[ G_t \,\middle|\, s_t = s \right]. \]

Definition 2.2 · Action-value function

The action-value (Q-function) is the expected return if the agent is forced to take action \(a\) in \(s\) once, and follows \(\pi\) afterward: \[ Q^{\pi}(s,a) \;=\; \mathbb{E}_{\pi}\!\left[ G_t \,\middle|\, s_t = s,\, a_t = a \right]. \]

Both are functions of the policy: change \(\pi\), change the values. We write \(V^*\) and \(Q^*\) for values of an optimal policy \(\pi^*\). Because an optimal deterministic policy exists in finite discounted MDPs (Chapter 01, Theorem 7.1), these are well-defined and attained.

Intuition check: \(V^{\pi}(s)\) answers “how good is it to be here if I behave like \(\pi\)?” while \(Q^{\pi}(s,a)\) answers “how good is it to try \(a\) right now, then behave like \(\pi\)?” The second question is what you need to improve a policy: compare actions without resimulating whole futures by hand.

Remark · Randomness being averaged

The expectation \(\mathbb{E}_{\pi}\) is over: (i) actions drawn from \(\pi\), (ii) successors drawn from \(P\), (iii) any randomness in rewards. A single episode’s return \(G_t\) is a random variable; \(V^{\pi}(s)\) is its conditional mean. Monte Carlo methods estimate that mean by averaging observed \(G_t\); TD methods estimate it by bootstrapping (Chapter 04).

3. Relating \(V\) and \(Q\)

Averaging the Q-function under the policy’s action distribution recovers \(V\):

(1) \[ V^{\pi}(s) = \sum_{a} \pi(a\mid s)\, Q^{\pi}(s,a) = \mathbb{E}_{a\sim\pi(\cdot\mid s)}\!\left[Q^{\pi}(s,a)\right]. \]

Conversely, one-step expansion of \(Q\) yields

(2) \[ Q^{\pi}(s,a) = \mathcal{R}(s,a) + \gamma \sum_{s'} P(s'\mid s,a)\, V^{\pi}(s'), \]

where \(\mathcal{R}(s,a)=\sum_{s'}P(s'\mid s,a)R(s,a,s')\) as in Chapter 01. Equations (1)–(2) together say: values of states and state–actions are two views of the same expected return, linked by one application of \(\pi\) or \(P\).

Definition 3.1 · Advantage

\(A^{\pi}(s,a) = Q^{\pi}(s,a) - V^{\pi}(s)\). By construction \(\mathbb{E}_{a\sim\pi}[A^{\pi}(s,a)]=0\). Advantages measure whether an action is better than average for that state — the scale used by modern actor–critic methods (GAE, PPO).

Visual studio · what \(V^{\pi}\) and \(Q^{\pi}\) mean

The equations above are two views of the same object. This studio makes that literal: same 4×4 grid, same policy \(\pi\), same \(\gamma\). Switch modes to see \(V^{\pi}(s)\) as “how good is it to be here?” and \(Q^{\pi}(s,a)\) as “how good is this move?” Then watch equations (1)–(2) rebuild one from the other with live arithmetic.

\(V\) ↔ \(Q\) visual laboratory

Click a cell · drag policy weights

State value. Each cell shows \(V^{\pi}(s)\): expected discounted return if you start in that cell and follow \(\pi\). Brighter = better position.

Heat map of \(V^{\pi}(s)\). Goal at \((3,3)\), reward \(+1\) on entry.

Live equation

Press “Solve” to compute exact \(V^{\pi}\) and \(Q^{\pi}\).
Selected \(s\)(1,1)
\(V^{\pi}(s)\)
\(Q^{\pi}(s,a)\)
Best action
How to read the modes
  • \(V^{\pi}(s)\) — one number per state; answers “how good is my current position?”
  • \(Q^{\pi}(s,a)\) — one number per arrow; answers “how good is this particular move?”
  • (1) — recover \(V\) by averaging \(Q\) with policy weights (your 0.3·left + 0.7·right example, generalized)
  • (2) — recover \(Q\) from immediate reward plus discounted value of where you land
  • Chain — substitute (2) into (1); you get the Bellman expectation equation (3) from Section 4

4. Bellman expectation equation

Substitute (2) into (1), or expand \(G_t\) directly (Section 5). The Bellman expectation equation for \(V^{\pi}\) is

(3) \[ V^{\pi}(s) = \sum_{a}\pi(a\mid s) \sum_{s'}P(s'\mid s,a) \Big[ R(s,a,s') + \gamma V^{\pi}(s') \Big]. \]

Read left to right:

  1. Average over the action the policy would take.
  2. Average over the environment’s response.
  3. Each branch contributes immediate reward plus discounted value of the landing state.

The companion equation for \(Q^{\pi}\) is

(4) \[ Q^{\pi}(s,a) = \sum_{s'}P(s'\mid s,a) \Big[ R(s,a,s') + \gamma \sum_{a'}\pi(a'\mid s')\, Q^{\pi}(s',a') \Big]. \]

Equation (3) is a linear system in the unknowns \(\{V^{\pi}(s)\}_{s\in S}\): \(|S|\) equations, \(|S|\) unknowns. For finite MDPs it has a unique solution. Policy evaluation algorithms solve that system — by matrix inversion for tiny MDPs, or by iterative backups for larger ones.

Proposition 4.1 · Unique solution for policy evaluation

For fixed \(\pi\) and \(\gamma\in[0,1)\), there is a unique bounded function \(V^{\pi}:S\to\mathbb{R}\) satisfying (3). Iterating the backup \(V_{k+1} = \mathcal{T}^{\pi} V_k\) (defined in Section 8) converges to \(V^{\pi}\) from any initialization.

5. Derivation in detail

Start from the definition and the return recursion \(G_t = r_{t+1} + \gamma G_{t+1}\):

(5) \begin{align*} V^{\pi}(s) &= \mathbb{E}_{\pi}[G_t \mid s_t=s] \\ &= \mathbb{E}_{\pi}[r_{t+1} + \gamma G_{t+1} \mid s_t=s] \\ &= \mathbb{E}_{\pi}[r_{t+1} + \gamma V^{\pi}(s_{t+1}) \mid s_t=s], \end{align*}

where the last step uses the law of total expectation and the fact that, given \(s_{t+1}\), the expected remaining return under \(\pi\) is exactly \(V^{\pi}(s_{t+1})\). Expand the outer expectation by enumerating \(a\) and \(s'\):

(6) \[ V^{\pi}(s) = \sum_a \pi(a\mid s) \sum_{s'} P(s'\mid s,a) \Big( R(s,a,s') + \gamma V^{\pi}(s') \Big), \]

which is (3). The same argument with a fixed first action produces (4).

Analysis · Why this is computationally decisive

Without Bellman, estimating \(V^{\pi}(s)\) seems to require averaging entire futures from \(s\). With Bellman, local consistency is enough: if your table satisfies (3) everywhere, it is \(V^{\pi}\). That reduces global return estimation to local backups — the algorithmic heart of DP and TD.

6. Bellman optimality equations

Optimal values satisfy a different recursion: instead of averaging over \(\pi\), take the best action. The Bellman optimality equation is

(7) \[ V^*(s) = \max_{a} \sum_{s'} P(s'\mid s,a) \Big( R(s,a,s') + \gamma V^*(s') \Big). \]
(8) \[ Q^*(s,a) = \sum_{s'} P(s'\mid s,a) \Big( R(s,a,s') + \gamma \max_{a'} Q^*(s',a') \Big). \]

Once \(Q^*\) is known, an optimal policy is a one-liner — no further planning:

(9) \[ \pi^*(s) \in \arg\max_a Q^*(s,a). \]

Equation (9) is the seed of greedy policy improvement, Q-learning’s target, and the “actor reads argmax of critic” design in discrete-action deep RL.

Expectation vs optimality — do not confuse them

\(\mathcal{T}^{\pi}\) (expectation backup) evaluates a fixed policy. \(\mathcal{T}^*\) (optimality backup) characterizes the best policy’s values. Policy iteration mixes both: evaluate with \(\mathcal{T}^{\pi}\), improve by greedy readout. Value iteration applies \(\mathcal{T}^*\) repeatedly. Q-learning samples a stochastic approximation of \(\mathcal{T}^*\).

7. Greedy policy improvement

Definition 7.1 · Greedy policy

Given any action-value estimate \(Q\), the greedy policy is \(\pi'(s) \in \arg\max_a Q(s,a)\) (ties broken arbitrarily).

Theorem 7.2 · Policy improvement (statement)

Let \(\pi\) be any policy and let \(\pi'\) be greedy with respect to \(Q^{\pi}\). Then \(V^{\pi'}(s) \ge V^{\pi}(s)\) for all \(s\), with equality for all \(s\) iff \(\pi\) is already optimal. (Proof idea: one-step improvement inequality telescopes into an infinite-horizon improvement via repeated application of the Bellman expectation operator.)

This is why “evaluate, then act greedy, repeat” is an escalator that never goes down. Chapter 03 turns the theorem into Policy Iteration. The same greedy step appears inside Value Iteration and in the target of Q-learning — only the source of \(Q\) (exact vs sampled) changes.

8. Contraction view (why uniqueness)

Define the Bellman expectation operator \(\mathcal{T}^{\pi}\) on value vectors by

(10) \[ (\mathcal{T}^{\pi} V)(s) = \sum_a\pi(a\mid s) \sum_{s'}P(s'\mid s,a) \big(R(s,a,s') + \gamma V(s')\big). \]

Then \(V^{\pi}\) is exactly the fixed point \(V = \mathcal{T}^{\pi} V\). In the infinity norm, \(\|\mathcal{T}^{\pi} V - \mathcal{T}^{\pi} U\|_\infty \le \gamma \|V-U\|_\infty\): \(\mathcal{T}^{\pi}\) is a \(\gamma\)-contraction. By the Banach fixed-point theorem there is a unique fixed point, and iterated application converges exponentially fast with rate \(\gamma\).

The optimality operator \(\mathcal{T}^*\) (max instead of \(\pi\)-average) is likewise a \(\gamma\)-contraction; its unique fixed point is \(V^*\). This is the heavy-math guarantee behind Value Iteration. You do not need the full proof to use the algorithms — but it explains why they do not oscillate forever.

9. Why \(Q\) wins in practice

If you know \(P\), knowing \(V^*\) is enough: choose \(a\) maximizing the one-step lookahead in (7). If you do not know \(P\) (robotics, Atari, the usual RL setting), you cannot evaluate that lookahead. But if you know \(Q^*\), equation (9) needs no model — just compare numbers \(Q^*(s,\cdot)\). That is why model-free control learns \(Q\) (or a policy directly), not bare \(V\).

Actor–critic methods reintroduce \(V\) (or \(A\)) as a baseline / critic for training a separate policy network — values assist gradients; they do not replace the policy when actions are continuous.

10. Laboratory · iterative policy evaluation

Fix a simple policy on a 4×4 grid (no walls): always try to move toward the bottom-right goal with a little randomness. Each click applies one synchronous Bellman expectation backup \(V \leftarrow \mathcal{T}^{\pi} V\) for all states. Watch values flow backward from the goal — the contraction in Section 8 made visible.

Policy evaluation sweeps

Operator \(\mathcal{T}^{\pi}\)

Goal at \((3,3)\) with reward \(+1\) on entering; step cost \(0\); \(\gamma\) controllable. Terminal state value held at \(0\) after absorbing.

Cell color intensity ∝ value. Numbers are \(V_k(s)\).

Backup log

Press “One backup sweep” to apply equation (3) everywhere.
Iteration0
‖Δ‖∞
max V0.00
V(start)0.00

Single-state backup inspector

Expand equation (3) for one \(s\)

Choose a cell. We expand \(\sum_a\pi(a\mid s)\sum_{s'}P(\cdot)[R+\gamma V(s')]\) using the current value table — the arithmetic behind one pixel of the heat map.

Select a state.

11. Worked Bellman backup

Tiny two-state illustration. States \(\{s_0,s_G\}\) with \(s_G\) terminal, \(V(s_G)=0\). From \(s_0\), action \(g\) reaches the goal with probability \(1\) and reward \(+1\). Policy takes \(g\) always. Then

(11) \[ V(s_0) = 1 + \gamma V(s_G) = 1. \]

If instead the goal is reached with probability \(0.5\) and otherwise we self-loop with reward \(0\),

(12) \[ V(s_0) = 0.5\big(1 + \gamma\cdot 0\big) + 0.5\big(0 + \gamma V(s_0)\big) = 0.5 + 0.5\gamma V(s_0). \]

Solve: \(V(s_0)(1 - 0.5\gamma) = 0.5\), hence \(V(s_0) = 0.5 / (1 - 0.5\gamma)\). For \(\gamma=0.9\), \(V(s_0)\approx 0.909\). Iterative policy evaluation discovers the same number by repeating the backup without algebraically solving the linear system — that is what the laboratory is doing on a larger grid.

  1. Write Bellman for self-loop MDP: \(V = 0.5(1) + 0.5\gamma V\).
  2. Collect terms: \(V - 0.5\gamma V = 0.5\).
  3. Factor: \(V(1-0.5\gamma)=0.5\).
  4. Closed form: \(V=1/(2-\gamma)\).
  5. At \(\gamma=0.9\): \(V=1/1.1\approx 0.90909\).
  6. Iteration from \(V_0=0\): \(V_1=0.5\), \(V_2=0.5+0.5\cdot0.9\cdot0.5=0.725\), … → same limit.

12. What follows

Dynamic Programming turns Bellman into algorithms you can run when \(P\) and \(R\) are known: Policy Iteration (evaluate → greedy improve → repeat) and Value Iteration (apply \(\mathcal{T}^*\) until convergence). Then TD learning removes the need for \(P\) itself — same backup structure, sampled successors.

See also

  • Sutton & Barto, Ch. 3–4 — values and DP.
  • Chapter 01 — MDP tuple, return, Markov property.
  • Practice notebook — full PI / VI by hand on a \(2\times 3\) grid.
  • course-notes.txt — condensed outline of the full course.

Previous

← Markov Decision Processes

Next

Practice · one gridworld

Work the whole stack by hand before Dynamic Programming.

Practice →