Skip to content

Reinforcement Learning: An Introduction, Ch. 3–4 — Value Functions, Bellman Equations and Dynamic Programming

main track #2corefundamentalbookrubric 11/122018 · MIT Press (2nd ed.), Ch. 3–4 · ~75 min

Richard S. Sutton, Andrew G. Barto

Read first: Reinforcement Learning: An Introduction, Ch. 3 — Finite MDPs, Returns and Episodes

Status

TL;DR. A value function answers “how good is it to be here?”. The state value vπ(s)v_\pi(s) is the expected return from state ss if you follow policy π\pi. The action value qπ(s,a)q_\pi(s,a) is the expected return if you take action aa first and then follow π\pi. Because the return is “next reward plus discounted later return”, values satisfy a self-consistency rule called the Bellman equation. Solving it evaluates a policy, and acting greedily on the result improves the policy. Repeating the two steps finds the optimal policy.

Why it matters now. The critic in PPO, the value head that VAPO argues for and GRPO removes, the “pass rate from this prefix” used to judge reasoning steps, and the Q-functions in DQN are all value functions. Knowing exactly what vπv_\pi means (and that it depends on π\pi) prevents a lot of confusion when reading LLM-RL papers.

Definitions (S&B Eqs. 3.12–3.13). For a policy π\pi,

vπ(s)=Eπ[Gt∣St=s],qπ(s,a)=Eπ[Gt∣St=s,At=a].v_\pi(s) = \mathbb{E}_\pi\left[G_t \mid S_t = s\right], \qquad q_\pi(s,a) = \mathbb{E}_\pi\left[G_t \mid S_t = s, A_t = a\right].

They are linked in both directions: the state value is the policy-weighted average of action values, vπ(s)=∑aπ(a∣s) qπ(s,a)v_\pi(s) = \sum_a \pi(a \mid s)\, q_\pi(s,a), and an action value is one step of reward plus the discounted value of where you land.

Deriving the Bellman equation in plain words. Start from the return recursion Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} and take the expectation given St=sS_t = s. The first term is the expected next reward. For the second, once you know the next state s′s', the expected value of Gt+1G_{t+1} is by definition vπ(s′)v_\pi(s'). Averaging over the action the policy picks and the next state the environment picks gives S&B Eq. 3.14:

vπ(s)=∑aπ(a∣s)∑s′,rp(s′,r∣s,a)[r+γ vπ(s′)].v_\pi(s) = \sum_a \pi(a \mid s) \sum_{s', r} p(s', r \mid s, a)\left[r + \gamma\, v_\pi(s')\right].

“The value here equals the average, over what I do and what happens, of the reward I get plus the discounted value of where I end up.” This is a backup: information flows from successor states back to the current state.

Optimal values. The best achievable values v∗v_* and q∗q_* satisfy the Bellman optimality equations (Eqs. 3.19–3.20), where the average over actions is replaced by a max:

v∗(s)=max⁡a∑s′,rp(s′,r∣s,a)[r+γ v∗(s′)],q∗(s,a)=∑s′,rp(s′,r∣s,a)[r+γmax⁡a′q∗(s′,a′)].v_*(s) = \max_a \sum_{s', r} p(s', r \mid s, a)\left[r + \gamma\, v_*(s')\right], \qquad q_*(s,a) = \sum_{s', r} p(s', r \mid s, a)\left[r + \gamma \max_{a'} q_*(s', a')\right].

Once you have q∗q_*, acting greedily on it is optimal. The q∗q_* form is the one Q-learning and DQN use, because it needs no model of pp to pick the action.

Dynamic programming (Ch. 4) assumes you know pp:

  • Policy evaluation (Eq. 4.5): start from any guess v0v_0 and repeatedly apply the right-hand side of Eq. 3.14 to every state. The values converge to vπv_\pi.
  • Policy improvement (Eqs. 4.7–4.9): make the policy greedy with respect to qπq_\pi. The policy improvement theorem says that if qπ(s,π′(s))≥vπ(s)q_\pi(s, \pi'(s)) \ge v_\pi(s) in every state, then vπ′(s)≥vπ(s)v_{\pi'}(s) \ge v_\pi(s) in every state. The greedy policy meets that condition.
  • Policy iteration alternates the two. A finite MDP has finitely many deterministic policies, so this converges to an optimal policy in a finite number of iterations (§4.3). Value iteration (Eq. 4.10) folds a max into each evaluation sweep.
  • Generalized policy iteration (GPI, §4.6) is the umbrella idea: any interleaving of “make values match the policy” and “make the policy greedy on the values”. Actor–critic methods, including PPO with a value head, are GPI with learned approximations.
Bellman backups in the search-or-answer MDP A backup tree. State Q has two actions: answer, with value 0.30, and search, with value minus 0.1 plus 0.9 times v of E, which is 0.62. State E has answer worth 0.80 and search worth 0.62. Under the starting policy v of E is 0.80 and v of Q is the average 0.5 times 0.30 plus 0.5 times 0.62, which is 0.46. Acting greedily picks search in Q and answer in E, giving the optimal value 0.62. Values are backed up from the end of the episode to the start Q v(Q) = 0.46 = mean(0.30, 0.62) answer: q = 0.30 search: q = 0.62 = −0.1 + 0.9·v(E) ⊥ r = 1 w.p. 0.3 E v(E) = 0.80 answer: q = 0.80 search: q = 0.62 ⊥ r = 1 w.p. 0.8 backup greedy: search in Q, answer in E → v*(Q) = 0.62
Original sketch. One Bellman backup per node in the search-or-answer MDP under π₀ (γ = 0.9). Values flow right to left, from terminal rewards back to the start state.

Recall the search-or-answer MDP: from Q, answer pays 1 with probability 0.3 and search costs −0.1 and moves to E; from E, answer pays 1 with probability 0.8 and search costs −0.1 and stays in E. γ = 0.9, and π₀ searches with probability 0.5 in Q and always answers in E.

QuantityBellman backupValue
qπ(E,answer)q_\pi(E, \text{answer})0.8×10.8 \times 10.80
vπ(E)v_\pi(E)π₀ always answers in E0.80
qπ(E,search)q_\pi(E, \text{search})−0.1+0.9×0.8-0.1 + 0.9 \times 0.80.62
qπ(Q,answer)q_\pi(Q, \text{answer})0.3×10.3 \times 10.30
qπ(Q,search)q_\pi(Q, \text{search})−0.1+0.9×vπ(E)-0.1 + 0.9 \times v_\pi(E)0.62
vπ(Q)v_\pi(Q)0.5×0.30+0.5×0.620.5 \times 0.30 + 0.5 \times 0.620.46

The 0.46 matches the trajectory-by-trajectory average on the returns page, as it must.

Policy evaluation by sweeps. Start at v0=0v_0 = 0 everywhere and update both states from the old values. Sweep 1: v1(E)=0.8v_1(E) = 0.8, v1(Q)=0.5×0.3+0.5×(−0.1+0.9×0)=0.10v_1(Q) = 0.5 \times 0.3 + 0.5 \times (-0.1 + 0.9 \times 0) = 0.10. Sweep 2: v2(Q)=0.15+0.5×(−0.1+0.72)=0.46v_2(Q) = 0.15 + 0.5 \times (-0.1 + 0.72) = 0.46. It converges in two sweeps because π₀ never loops. If the policy in E instead searched half the time, v(E)v(E) would appear on both sides, v(E)=0.5×0.8+0.5 (−0.1+0.9 v(E))v(E) = 0.5 \times 0.8 + 0.5\,(-0.1 + 0.9\, v(E)), giving v(E)=0.35/0.55≈0.636v(E) = 0.35/0.55 \approx 0.636. Sweeps would then converge geometrically rather than exactly.

Policy improvement. In Q, q(Q,search)=0.62>q(Q,answer)=0.30q(Q, \text{search}) = 0.62 > q(Q, \text{answer}) = 0.30, so the greedy policy always searches first. In E, answering (0.80) beats searching (0.62). The improved policy “search once, then answer” has v∗(Q)=0.62v_*(Q) = 0.62 and v∗(E)=0.8v_*(E) = 0.8. Evaluating it again gives the same greedy choice, so it is optimal: policy iteration finished in one improvement step.

LLM token generation. With a terminal-only reward and γ = 1, the Bellman backup becomes very simple because appending a token is deterministic:

Q(s,a)=V(s⋅a),V(s)=∑aπ(a∣s) V(s⋅a).Q(s, a) = V(s \cdot a), \qquad V(s) = \sum_a \pi(a \mid s)\, V(s \cdot a).

V(prefix)V(\text{prefix}) is “the probability this policy finishes the prefix with a correct answer” (or its expected reward-model score). You can estimate it by sampling continuations and averaging their rewards, or train a value head to predict it. The optimality equation would need a max over the whole vocabulary at every step and an exact model of future rewards, which is why LLM RL uses policy-gradient methods with a learned or sampled VV rather than value iteration.

A search agent. V(transcript)V(\text{transcript}) is the expected final reward from here: roughly the chance of answering correctly minus the expected cost of future searches. q(s,search)q(s, \text{search}) depends on the retriever. A better retriever raises the value of searching, so improving retrieval changes the agent’s optimal policy, not only its accuracy. In the toy MDP, q(Q,search)=−0.1+0.9×0.8q(Q, \text{search}) = -0.1 + 0.9 \times 0.8, so every point of retrieval quality (the 0.8) is worth 0.9 points of q(Q,search)q(Q, \text{search}).

  • Bellman equation for vπv_\pi (Eq. 3.14), and optimality equations for v∗v_* and q∗q_* (Eqs. 3.19–3.20). For finite MDPs, the optimality equation for v∗v_* has a unique solution (§3.6).
  • Policy improvement theorem (Eqs. 4.7–4.8): greedy improvement gives a policy at least as good in every state, and strictly better somewhere unless the old policy was already optimal (§4.2).
  • Policy iteration terminates in a finite number of iterations for a finite MDP (§4.3). The small gridworld of Figure 4.1 shows a greedy policy that is optimal after only a few evaluation sweeps.

§3.5 policies and value functions, §3.6 optimal policies and optimal value functions, then Chapter 4: §4.1–4.4 (evaluation, improvement, policy iteration, value iteration) and §4.6 (GPI). Skip §4.5 (asynchronous DP) on first read.

  • DP needs the model. Knowing p(s′,r∣s,a)p(s', r \mid s, a) is rare. For a search agent it would mean knowing in advance which documents every query returns. The next page removes this assumption by learning from samples.
  • The state space is too large to tabulate. Every distinct prefix is a state for an LLM. Values must be approximated by a network, which brings bias and, combined with bootstrapping and off-policy data, instability (the “deadly triad”, S&B §11.3).
  • Values are policy-relative. A value model trained on one checkpoint drifts out of date as the policy improves; how fast it goes stale in LLM RL is not well measured.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    In the search-or-answer MDP (γ = 0.9), compute v_π(E), q_π(E, search), q_π(Q, answer), q_π(Q, search) and v_π(Q) for the starting policy π₀ (search with probability 0.5 in Q, always answer in E). Then change the policy in E to "search with probability 0.5" and solve for the new v(E).

    What a strong answer covers
    • v(E) = 0.8 because E always answers and the answer is right with probability 0.8. q(E, search) = −0.1 + 0.9·0.8 = 0.62.
    • q(Q, answer) = 0.3. q(Q, search) = −0.1 + 0.9·v(E) = 0.62. v(Q) = 0.5·0.3 + 0.5·0.62 = 0.46, matching the trajectory average on the returns page.
    • With search probability 0.5 in E, v(E) appears on both sides: v(E) = 0.5·0.8 + 0.5·(−0.1 + 0.9·v(E)). So 0.55·v(E) = 0.35 and v(E) ≈ 0.636.
    • The self-loop is why Bellman equations are a linear system, not a single formula: a state's value depends on its own value through the loop.
  2. Mechanism

    Explain why the Bellman expectation equation for v_π is linear and can be solved exactly, while the Bellman optimality equation is not linear. Then explain, using the policy improvement theorem, why acting greedily with respect to q_π can never make the policy worse.

    What a strong answer covers
    • For a fixed π, v_π(s) is an average over actions and next states of r + γ·v_π(s′). Each equation is linear in the unknown values, so |S| equations in |S| unknowns can be solved directly (or by repeated sweeps).
    • The optimality equation takes a max over actions, which is piecewise linear, so it is solved by iteration (value iteration) or by alternating evaluation and improvement (policy iteration).
    • Policy improvement: if q_π(s, π′(s)) ≥ v_π(s) in every state, then v_π′(s) ≥ v_π(s) in every state. Greedy π′ satisfies this by construction because the max is at least the average.
    • In the toy MDP, q(Q, search) = 0.62 > v(Q) = 0.46, so switching Q to always search raises v(Q) to 0.62, which is also v*.
  3. Connect

    For an LLM with a terminal-only reward from a verifier and γ = 1, show that Q(s, a) = V(s·a), where s·a is the prefix with token a appended, and that V(s) = Σ over a of π(a|s)·V(s·a). What does V(prefix) mean in plain words, and how could you estimate it without training a value network?

    What a strong answer covers
    • Transitions are deterministic (append the token) and the reward before the end is 0, so the one-step Bellman backup q(s, a) = r + γ·v(s′) reduces to V(s·a).
    • Averaging Q over the policy's next-token distribution gives the Bellman expectation equation for V.
    • V(prefix) is the probability that the policy, continuing from this prefix, ends with a correct answer (or the expected reward-model score).
    • Estimate it by Monte Carlo: sample several continuations from the prefix and average their final rewards. This is unbiased for the current policy but costs extra generation.
  4. Critique

    A colleague proposes training a value model on prefixes from today's policy and reusing it unchanged as a "partial-answer reward model" for all later checkpoints. What is the flaw, and when, if ever, would a policy-independent value be the right object?

    What a strong answer covers
    • v_π is defined with respect to a policy: it is the expected return if π keeps acting. As the policy improves, the same prefix gets a different value, so a frozen v_π becomes stale.
    • Using a stale value as a reward can push the new policy toward prefixes the old policy handled well, not prefixes that are actually good.
    • v* is policy-independent, but it is the value under the best policy and is not available in practice.
    • A fixed scorer for intermediate steps is a different object (a process reward of step quality) and should be validated as such, not assumed equal to a value function.
  5. Design

    Design a "value of one more search" estimator for a search agent: an estimate of q(s, search) − q(s, answer) at a given transcript. Specify how you would collect training targets, what the model sees, and how you would check that it improves stopping decisions.

    What a strong answer covers
    • Targets: from the same transcript, roll out "answer now" and "search, then continue with the policy" several times; the difference in mean final reward (minus search cost) is a Monte Carlo estimate of the advantage of searching.
    • Inputs: question, transcript, and features of the candidate query or the retrieval results. In the toy MDP the answer is 0.62 − 0.3 = 0.32 in Q and −0.18 in E.
    • The estimate is policy- and retriever-dependent: a better retriever raises q(s, search), so retrain or recalibrate when either changes.
    • Check: compare accuracy and number of searches when stopping by the estimator against fixed search budgets on held-out questions, and report calibration of the predicted gains.

Done with this page?

Status