Modeling the World

Reinforcement learning, part 1: Markov decision processes and the Bellman equations

Author

Published

Oct. 2026

Introduction

What does Machine Learning (ML) learn? It took me a while to answer this question but the answer is actually straightforward. Machine learning always learns a function: y=f(x)y=f(x).

But Reinforcement learning (RL) learns something else: how to act. An agent looks at its situation, takes an action, sees what happens, and does it again, step after step. Nobody tells it which action was right. It only receives a number, a reward, after each step, which can be: points in a game, a penalty for falling, a bonus for reaching the goal. Its job is to find a way of acting, a policy, that collects as much reward as possible in the long run.

This setting covers a lot of ground: an agent learning to play Atari games from the pixels and the score, a simulated body learning to walk without falling, AlphaGo beating Lee Sedol at Go, AlphaZero mastering chess and shogi by self-play, language models fine-tuned from human preferences. Each time, a sequence of decisions, each one changing what the next one will face, and a score that says how well it went.

Two questions run through the whole subject:

This page is about the first case: planning with a model. This is the beginning of the story of Reinforcement Learning. It builds the framework that all of RL rests on, we’ll see:

RL framework:

  1. Sequential decisions and the return, the score of a whole trajectory.
  2. Markov decision processes, the model of the world.

Evaluating a decision:

  1. Value functions and the Bellman equations: how good a state is, and how we can link them by a one-step recursion.

Taking better decisions:

  1. Optimal values and policies: the best one can do, and the Bellman optimality equation that characterizes it.

Then Part 2, Planning with a Model turns these equations into algorithms that solve them exactly: policy evaluation, policy iteration and value iteration. These are required steps before switching to Part 3, Learning from experience, which removes the model and uses Monte Carlo, temporal-difference learning, SARSA and Q-learning to learn the same values from sampled interactions only. Everything they do is an approximation of what Parts 1 and 2 compute exactly.

A word on notation, which follows Sutton and Barto’s book.

1. Sequential decisions

Taking good decisions starts with being able to evaluate the impact of a decision in a given situation. This section describes the framework that is used to describe: 1) the impact of a decision and 2) the quality of this decision.

1.1 Decisions in stages

In sequential decision making, a decision maker takes successive decisions, each one based on what it observes of the effect of the previous ones. Picture a decision tree: from the current situation, each possible action leads to a new situation, from which new actions branch out, and so on. A good decision maker finds a good path through this tree.

Decisions in stages
Each action leads to a new situation, from which new actions branch out. A trajectory is one path through the tree.

The framework is called sequential because time is discrete: there is a first decision, a second one, a third one. But the framework says nothing about how long a step lasts: a millisecond for a robot’s motor, a day for a stock portfolio. And only time is discrete: the actions themselves can be a choice among a few options (left, right) or a real number (the torque of a joint).

Think about it for a second, this can cover a large portion of real-world problems.

In RL, a decision maker, the agent, interacts with its environment, everything distinct from itself. They talk in a loop. At each step tt, the agent observes the state StS_t and picks an action AtA_t; the environment answers with a reward Rt+1R_{t+1} and a new state St+1S_{t+1}. One interaction produces a trajectory:

S0, A0, R1, S1, A1, R2, S2, A2, R3, …S_0,\ A_0,\ R_1,\ S_1,\ A_1,\ R_2,\ S_2,\ A_2,\ R_3,\ \dots

Note the index of the reward: Rt+1R_{t+1} is the reward that follows action AtA_t, received together with St+1S_{t+1}.

The agent–environment loop
Press Step to run the loop: the agent picks an action At, the environment answers with a reward Rt+1 and a new state St+1, which the agent observes at the next step. Each event adds its symbols to the trajectory.

This is a sequential decision making framework, with a reward describing how good an action, from a given state, is.

1.2 Optimal means optimal for a score

What does being optimal mean?

Being optimal is always being optimal with regard to a cost or a score. To illustrate this concept, here is the running example of this page, a tiny map with four places at different altitudes:

S (start) · 0 mA · 10 m
B · 40 mG (goal) · 5 m

A hiker starts at SS and wants to reach GG with as little effort as possible, the effort being the total altitude change along the way. Two routes:

The altitude map
The two routes from S to G, on the map and as a graph of states. Each move is an arrow labelled with its action and its effort, the altitude change; G is terminal. The effort of a route is the sum of the efforts of its moves: the route through B climbs 40 m only to come down again.

Both reach GG, but the first route is five times cheaper. “Best” only means something with respect to a score computed along the whole trajectory. Control theory usually minimizes a cost ∑tc(St,At)\sum_t c(S_t, A_t); RL maximizes a sum of rewards ∑tRt\sum_t R_t. They are the same thing with R=−cR = -c: on our map, the reward of a move will be minus the absolute altitude change.

In RL, the reward is a step signal: a single number handed to the agent after each step, not a grade computed once at the end. The agent does not see the reward structure in advance; it discovers it one step at a time. Sutton and Barto state the bet behind the whole field as the reward hypothesis: all goals can be described as the maximization of the expected cumulative reward.

Ideally, the reward would be:

In practice, such a sparse signal is often too hard to learn from, and designers add intermediate rewards to guide the agent, at the risk of rewarding something other than what they meant.

1.3 The return and the discount factor

The quantity the agent actually maximizes is not the reward directly but rather the return, the sum of the discounted rewards to come, from step tt on:

Gt≐Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1G_t \doteq R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \cdots = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}

The number γ∈(0,1]\gamma \in (0, 1] is the discount factor: a reward received kk steps from now counts γk\gamma^k times as much as the same reward received now. Like money, cash now is worth more than potential money later. With γ\gamma close to 00, the agent is said to be myopic: only the next rewards matter. With γ\gamma close to 11, it is said to be far-sighted.

The discount also keeps the sum finite. When the task never ends (a continuing task) and the rewards are bounded, ∣Rt∣≤Rmax⁡|R_t| \le R_{\max}, the geometric series gives

∣Gt∣≤∑k=0∞γkRmax⁡=Rmax⁡1−γfor γ<1|G_t| \le \sum_{k=0}^{\infty} \gamma^k R_{\max} = \frac{R_{\max}}{1 - \gamma} \qquad \text{for } \gamma < 1

When the task stops after a finite number of steps (an episodic task, like a game or our hike), the sum is finite anyway, and γ=1\gamma = 1 is allowed.

On our map, the route right-then-down gives R1=−10R_1 = -10 and R2=−5R_2 = -5, then the episode ends. With γ=1\gamma = 1, G0=−15G_0 = -15; with γ=0.9\gamma = 0.9, G0=−10+0.9×(−5)=−14.5G_0 = -10 + 0.9 \times (-5) = -14.5: the second step is discounted, the first never is.

For a task that never ends, with a reward of +1+1 at every step, the return is a geometric series: G0=1+γ+γ2+⋯=11−γG_0 = 1 + \gamma + \gamma^2 + \cdots = \frac{1}{1 - \gamma}. That is 1010 with γ=0.9\gamma = 0.9, 100100 with γ=0.99\gamma = 0.99, and no finite value with γ=1\gamma = 1: roughly, the agent looks 11−γ\frac{1}{1 - \gamma} steps ahead.

Play with γ\gamma on a few reward sequences:

The discount factor weighs the future
Light bars: the rewards R; solid bars: their contribution γkR to the return G0. Lower γ and the future fades. The route S → A → G ends after two steps; the task with +1 forever never ends, and its return stays finite only because γ < 1.

One identity will matter more than any other on this page. Take the first reward out of the sum, factor γ\gamma out of the rest, and shift the index with j=k−1j = k - 1:

Gt=∑k=0∞γkRt+k+1=Rt+1+∑k=1∞γkRt+k+1=Rt+1+γ∑j=0∞γjR(t+1)+j+1.\begin{aligned} G_t &= \sum_{k=0}^{\infty} \gamma^k R_{t+k+1} = R_{t+1} + \sum_{k=1}^{\infty} \gamma^k R_{t+k+1} \\ &= R_{t+1} + \gamma \sum_{j=0}^{\infty} \gamma^j R_{(t+1)+j+1}. \end{aligned}

What remains is the return of the next step:

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

Stated differently, the return is the next reward plus the discounted return from the next step on.

Exercise 1.3 Returns from a reward sequence
  1. An episode. With γ=0.5\gamma = 0.5, an episode gives the rewards R1=−1R_1 = -1, R2=2R_2 = 2, R3=6R_3 = 6, R4=3R_4 = 3, R5=2R_5 = 2, then ends at T=5T = 5 (so G5=0G_5 = 0). Compute G5,G4,…,G0G_5, G_4, \dots, G_0.
  2. A task that never ends. With γ=0.9\gamma = 0.9, the rewards are R1=2R_1 = 2, followed by an infinite sequence of 77s: R2=R3=⋯=7R_2 = R_3 = \cdots = 7. Compute G1G_1, then G0G_0.
  3. The sequence of question 2 never ends. Why is G1G_1 still a finite number?
Hint

Work backwards, from G5=0G_5 = 0, with Gt=Rt+1+γ Gt+1G_t = R_{t+1} + \gamma\, G_{t+1}: each return is one reward plus the discounted return you just computed.

Hint

For question 2, G1=7+7γ+7γ2+⋯G_1 = 7 + 7\gamma + 7\gamma^2 + \cdots is a geometric series: ∑k≥0γk=11−γ\sum_{k \ge 0} \gamma^k = \frac{1}{1 - \gamma} for γ<1\gamma < 1.

Checkpoint what you should get

In question 1, G0=2G_0 = 2; in question 2, G1=70G_1 = 70 and G0=65G_0 = 65.

Solution
  1. Backwards, with Gt=Rt+1+0.5 Gt+1G_t = R_{t+1} + 0.5\, G_{t+1}:

    • G5=0G_5 = 0
    • G4=2+0.5×0=2G_4 = 2 + 0.5 \times 0 = 2
    • G3=3+0.5×2=4G_3 = 3 + 0.5 \times 2 = 4
    • G2=6+0.5×4=8G_2 = 6 + 0.5 \times 4 = 8
    • G1=2+0.5×8=6G_1 = 2 + 0.5 \times 8 = 6
    • G0=−1+0.5×6=2G_0 = -1 + 0.5 \times 6 = 2

    Forwards gives the same result, G0=−1+0.5×2+0.25×6+0.125×3+0.0625×2=2G_0 = -1 + 0.5 \times 2 + 0.25 \times 6 + 0.125 \times 3 + 0.0625 \times 2 = 2, with more work.

  2. From t=1t = 1 on, the reward is always 77: G1=71−0.9=70G_1 = \frac{7}{1 - 0.9} = 70, and G0=R1+γ G1=2+0.9×70=65G_0 = R_1 + \gamma\, G_1 = 2 + 0.9 \times 70 = 65.

  3. Because γ<1\gamma < 1: the weights γk\gamma^k form a convergent geometric series, and with bounded rewards ∣Gt∣≤Rmax⁡1−γ|G_t| \le \frac{R_{\max}}{1 - \gamma}. Here the bound 70.1=70\frac{7}{0.1} = 70 is reached exactly. With γ=1\gamma = 1, the sum would diverge.

What to notice

The backward recursion computes all the returns of an episode in one pass, each from the next one. This is how Monte Carlo methods compute returns in Part 3, and the same split, one reward plus the rest, is the seed of the Bellman equations of section 3.

2. Markov decision processes

The previous section showed what taking decisions looks like. But before applying this to real problems, we need another brick: how can we model the world as a decision tree?

2.1 The Markov property

Do we need to know all the past to take a good decision?

The past is the history, everything the agent has seen so far: observations, actions, rewards. The state is what we keep of it to decide what comes next. Keeping everything works, but it grows without end; the goal is to use the smallest summary that loses nothing useful for predicting the future.

A state StS_t is said to be Markov when it has the following property: knowing StS_t is enough to predict St+1S_{t+1}, and the rest of the history adds nothing,

P[St+1∣St]=P[St+1∣S1,…,St]\mathbb{P}[S_{t+1} \mid S_t] = \mathbb{P}[S_{t+1} \mid S_1, \dots, S_t]

In words: the future is independent of the past, given the present. For a point mass pushed by a force, position and velocity form a Markov state: the next position and velocity follow from them and from the force applied, whatever path led there. Position alone would not be Markov.

Is the state Markov?
A point mass in the plane. Two different pasts (dashed) reach the same position xt, and the same force gives the same acceleration at. Left: they also share the velocity vt, and one next state follows. Right: different velocities, two different next positions. Faint dotted: where the mass would go without the action; the thin curve is the path the action bends.

We often get to choose the state, and a common trick is to put a bit of history in it to make it Markov. In the Atari game, DeepMind used the last four frames of images as the state.

2.2 The model of the world

A Markov decision process (MDP) is the mathematical model of a sequential decision problem. It is made of:

The dynamics is a single function describing the joint probability of the next state and of the reward, given the current state and action:

p(s′,r∣s,a)≐P[St=s′, Rt=r∣St−1=s, At−1=a]p(s', r \mid s, a) \doteq \mathbb{P}[S_t = s',\ R_t = r \mid S_{t-1} = s,\ A_{t-1} = a]

For each pair (s,a)(s, a), it is a probability distribution over the pairs (s′,r)(s', r): after taking aa in ss, the agent lands somewhere and collects some reward, so ∑s′,rp(s′,r∣s,a)=1\sum_{s', r} p(s', r \mid s, a) = 1.

Other functions follow from pp, for instance the probability of landing in s′s' and the expected reward:

p(s′∣s,a)=∑rp(s′,r∣s,a),r(s,a)≐E[Rt∣St−1=s,At−1=a]=∑s′,rr p(s′,r∣s,a)\begin{aligned} p(s' \mid s, a) &= \sum_{r} p(s', r \mid s, a), \\ r(s, a) &\doteq \mathbb{E}[R_t \mid S_{t-1} = s, A_{t-1} = a] = \sum_{s', r} r\ p(s', r \mid s, a) \end{aligned}
Why do we need a probability?

A function s′=f(s,a)s' = f(s, a) would only describe a world where the same action always has the same effect. Real environments are often random: a wheel slips, a die is rolled, another player moves. A probability covers all these cases at once, and a deterministic world is just the case where one outcome has probability 11. The reward is in the same distribution because it can be random too, and it often depends on where the agent lands.

The dynamics of one action
The distribution p(s′, r | A, down) on our map. Deterministic: the map of this page. Stochastic: the same move on a slippery path, which can also leave the hiker at A or send her back to S. The thickness of an arrow is its probability; in both cases the probabilities sum to 1. The hiker takes the action again and again: on the slippery path, the outcome changes from one try to the next, and the counts below approach the probabilities.

On our 2×2 map example:

Here is our map framed as an MDP with some design choices:

Each cell gives the next state s′s' and the reward rr of an action aa taken in a state ss; a dash means the action is not available there.

ssrightdownleftupstay
SSAA, −10-10BB, −40-40–––
AA–GG, −5-5SS, −10-10–AA, −1-1
BBGG, −35-35––SS, −40-40BB, −1-1

This environment is deterministic: for each pair (s,a)(s, a), a single pair (s′,r)(s', r) has probability 11, for instance p(A,−10∣S,right)=1p(A, -10 \mid S, \text{right}) = 1, and every other pair has probability 00. The sum over (s′,r)(s', r) is still 11, not because the world is deterministic, but because p(⋅,⋅∣s,a)p(\cdot, \cdot \mid s, a) is a probability distribution.

Vocabulary:

The process is Markov because pp conditions on the last state and action only. It is stationary if pp does not change over time, which we assume. And it is finite when there are finitely many states, actions and rewards: pp is then a table of numbers, the case of this whole page.

2.3 Policies

Last, how do we describe a behaviour compared to another?

A policy is the agent’s decision rule: it describes which action to take in which state. In general it is a probability law, a probability distribution over the actions for each state:

π(a∣s)≐P[At=a∣St=s],∑a∈A(s)π(a∣s)=1  for every s\pi(a \mid s) \doteq \mathbb{P}[A_t = a \mid S_t = s], \qquad \sum_{a \in \mathcal{A}(s)} \pi(a \mid s) = 1 \ \text{ for every } s

A deterministic policy, a=π(s)a = \pi(s), is the special case where all the probability sits on one action.

Two policies on our map will come back in the exercises:

Two policies on the map
A hiker walks one episode under πA, then one under πB, in a loop. Arrows: the actions of the current policy, their thickness proportional to π(a | s). Beside the map, the two policies as decision tables. πB draws its action at S at random: run it a few times and both routes appear.

Both sum to 11 at SS, which is what makes them policies. The normalization holds state by state.

Once a problem is written as an MDP, there is a well-defined notion of best behaviour: an optimal policy π∗\pi_* is the decision rule that maximizes the expected return.

The MDP is the model of the problem; the policy is its solution. The next section makes “maximizes the expected return” precise.

3. Value functions and the Bellman equation

Now that we know how to define our environment and what taking decisions means, we will study how to compare policies and find the best actions.

3.1 How good is a state, how good is an action

To compare policies, and to improve them, we need to know how good each situation is. The state-value function of a policy π\pi is the expected return when starting in ss and following π\pi:

vπ(s)≐Eπ[Gt∣St=s]v_\pi(s) \doteq \mathbb{E}_\pi[G_t \mid S_t = s]

The action-value function is the expected return when starting in ss, taking action aa first, then following π\pi:

qπ(s,a)≐Eπ[Gt∣St=s, At=a]q_\pi(s, a) \doteq \mathbb{E}_\pi[G_t \mid S_t = s,\ A_t = a]
Why do we need an expectation?

Eπ[⋅]\mathbb{E}_\pi[\cdot] is the expectation of the return, under a certain policy. Since our environment and our policies might be stochastic, the return has to be evaluated on average, to compare between policies.

The expectation is over everything random: the actions drawn from π\pi and the outcomes drawn from pp. Both functions score the same return; qπq_\pi just conditions on one more piece of information, the first action. vπv_\pi ranks states; qπq_\pi ranks the moves available in a state. In discrete cases, these expectations are expressed as sums directly.

On our 2×2 example, take the policy that flips a coin at SS, right or down with probability 12\frac12 each, then heads straight for GG, with γ=1\gamma = 1.

From AA the return is −5-5, from BB it is −35-35, and from SS it is −15-15 or −75-75 depending on the coin (−45-45 on average), leading to:

ssSSAABBGG
vπ(s)v_\pi(s)−45-45−5-5−35-3500

At SS, the first two moves give qπ(S,right)=−15q_\pi(S, \text{right}) = -15 and qπ(S,down)=−75q_\pi(S, \text{down}) = -75: already, qπq_\pi says that right is the better move. The whole table of qπq_\pi, one row per state, one column per action: each entry is the reward of the first move plus the value vπv_\pi of where it lands.

qπ(s,a)q_\pi(s, a)rightdownleftupstay
SS−15-15−75-75–––
AA–−5-5−55-55–−6-6
BB−35-35––−85-85−36-36

And averaging the action values over the policy’s own choices gives back the state value (weighted by the probability of each action):

vπ(s)=∑aπ(a∣s) qπ(s,a)v_\pi(s) = \sum_{a} \pi(a \mid s)\, q_\pi(s, a)

For instance on SS:

vπ(S)=12(−15)+12(−75)=−45 \qquad v_\pi(S) = \tfrac12 (-15) + \tfrac12 (-75) = -45

3.2 The Bellman expectation equation

Computing vπ(s)v_\pi(s) from its definition means averaging the return over every trajectory that starts in ss. Their number explodes with the length of the episode, and it is infinite as soon as the process can run forever, as on our map, where the hiker can walk back and forth.

Every trajectory from S
All the ways the hiker can walk from S on our map, over three steps. A branch ends at G; from S, A or B, two moves keep walking, so the unfinished trajectories double at every step and never run out.

Bellman’s idea is to never enumerate trajectories. Any trajectory from ss is one step, followed by a trajectory from the next state, and the value of “the rest” is vπv_\pi again, one step later.

Think of the hike. You don’t need to walk every route from SS to know what SS is worth: if you already know that AA is worth −5-5, the move S→AS \to A is worth −10+(−5)-10 + (-5), and nothing beyond AA matters. The value of a state is written with the values of its neighbours, and theirs with their neighbours’, and this is a consistency that the table should verify everywhere:

Using Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} and the expectation taken given St=sS_t = s, we obtain:

vπ(s)=Eπ[Rt+1+γ vπ(St+1)∣St=s]v_\pi(s) = \mathbb{E}_\pi[R_{t+1} + \gamma\, v_\pi(S_{t+1}) \mid S_t = s]

Written with the policy and the dynamics, first the choice of the action, then the outcome:

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)\, \big[ r + \gamma\, v_\pi(s') \big]

From the inside out: r+γ vπ(s′)r + \gamma\, v_\pi(s') is what one outcome is worth; the inner sum averages it over what the environment may do; the outer sum averages over what the policy may choose.

This is the Bellman expectation equation. Read it as a picture, a backup diagram: from the state ss, the policy branches over the actions; from each action, the environment branches over the outcomes (s′,r)(s', r); each leaf is worth its reward plus the discounted value of where it lands. The same reasoning for qπq_\pi puts the outcome first and the next action second:

qπ(s,a)=∑s′,rp(s′,r∣s,a) [r+γ∑a′π(a′∣s′) qπ(s′,a′)]q_\pi(s, a) = \sum_{s', r} p(s', r \mid s, a)\, \Big[ r + \gamma \sum_{a'} \pi(a' \mid s')\, q_\pi(s', a') \Big]
Backup diagrams
Open circles are states, filled dots are actions. Left, vπ(s): the policy chooses the action, then the environment chooses the outcome. Right, qπ(s, a): the action is given, the environment answers first, then the policy chooses the next action. The values of the leaves are carried up to the root, one step back: a backup.

The equation turns an infinite sum over trajectories into a one-step, self-referential relation. In a finite MDP, it is one linear equation per state, so ∣S∣|\mathcal{S}| equations for the ∣S∣|\mathcal{S}| unknowns vπ(s)v_\pi(s): a linear system, which can be solved directly. For πB\pi_B on our map, with γ=1\gamma = 1, one equation per state:

{vπB(S)=0.3 [−10+vπB(A)]+0.7 [−40+vπB(B)]vπB(A)=−5+vπB(G)vπB(B)=−35+vπB(G)vπB(G)=0\left\{ \begin{aligned} v_{\pi_B}(S) &= 0.3\, \big[ -10 + v_{\pi_B}(A) \big] + 0.7\, \big[ -40 + v_{\pi_B}(B) \big] \\ v_{\pi_B}(A) &= -5 + v_{\pi_B}(G) \\ v_{\pi_B}(B) &= -35 + v_{\pi_B}(G) \\ v_{\pi_B}(G) &= 0 \end{aligned} \right.

Four equations, four unknowns. Here the system is even triangular: GG gives AA and BB, which give SS.

Another way to see it: vπv_\pi is a fixed point. Replace every value by the one-step look-ahead of its neighbours, the right-hand side of the equation, and the table does not change.

Optional Where the Markov property is used: deriving the Bellman equation

The equation is not as straightforward as it might look: it takes two lines of conditioning, and one assumption.

From vπv_\pi to qπq_\pi. Split the expectation along the first action, whose probability given St=sS_t = s is π(a∣s)\pi(a \mid s) (law of total expectation): vπ(s)=∑aπ(a∣s) qπ(s,a)v_\pi(s) = \sum_a \pi(a \mid s)\, q_\pi(s, a).

From qπq_\pi to vπv_\pi. Write Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1} and split along the outcome (St+1,Rt+1)=(s′,r)(S_{t+1}, R_{t+1}) = (s', r), whose probability is p(s′,r∣s,a)p(s', r \mid s, a):

qπ(s,a)=∑s′,rp(s′,r∣s,a) [r+γ Eπ[Gt+1∣St=s,At=a,Eπ[St+1=s′,Rt+1=r]]\begin{aligned} q_\pi(s, a) = \sum_{s', r} p(s', r \mid s, a)\, \Big[ r + \gamma\, &\mathbb{E}_\pi\big[G_{t+1} \mid S_t = s, A_t = a, \\ &\phantom{\mathbb{E}_\pi\big[} S_{t+1} = s', R_{t+1} = r\big] \Big] \end{aligned}

The last expectation is where the Markov property enters: given the present state St+1=s′S_{t+1} = s', the future does not depend on how the agent got there, so this expectation is just vπ(s′)v_\pi(s'). Substituting one relation into the other gives both Bellman equations.

Without the Markov property, the value of s′s' would depend on the path that led to it, “the value of a state” would not be defined, and there would be no Bellman equation: no dynamic programming, and none of the learning methods of Part 3.

Exercise 3.2 Value functions on the altitude map

Take the altitude map of section 2.2, with γ=1\gamma = 1, and the two policies of section 2.3.

  1. By backward substitution from GG, where vπA(G)=0v_{\pi_A}(G) = 0, compute vπA(A)v_{\pi_A}(A), then vπA(S)v_{\pi_A}(S), then vπA(B)v_{\pi_A}(B).
  2. Compute qπA(S,right)q_{\pi_A}(S, \text{right}), qπA(S,down)q_{\pi_A}(S, \text{down}), qπA(A,down)q_{\pi_A}(A, \text{down}) and qπA(A,left)q_{\pi_A}(A, \text{left}).
  3. Compare qπA(S,⋅)q_{\pi_A}(S, \cdot) with the values −15-15 and −75-75 of the coin-flipping policy of section 3.1. Are they equal? Why, since the two policies differ?
  4. Compute vπB(S)v_{\pi_B}(S) as an 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). Is it closer to q(S,right)q(S, \text{right}) or to q(S,down)q(S, \text{down})?
  5. Check your result with the Bellman expectation equation of πB\pi_B at SS.
Hint

The map is deterministic and γ=1\gamma = 1, so each value is one reward plus the value of where you land: vπA(A)=−5+vπA(G)v_{\pi_A}(A) = -5 + v_{\pi_A}(G). For qq, the first action is the one you are given, whatever the policy says.

Hint

qπ(S,a)q_\pi(S, a) fixes the action at SS, then follows π\pi from the next state on. Which states does it look at, and what do the policies do there?

Checkpoint what you should get

vπA(S)=−15v_{\pi_A}(S) = -15, qπA(A,left)=−25q_{\pi_A}(A, \text{left}) = -25, and vπB(S)=−57v_{\pi_B}(S) = -57.

Solution
  1. vπA(A)=−5+0=−5v_{\pi_A}(A) = -5 + 0 = -5, then vπA(S)=−10+vπA(A)=−15v_{\pi_A}(S) = -10 + v_{\pi_A}(A) = -15. πA\pi_A goes right at BB, so vπA(B)=−35+0=−35v_{\pi_A}(B) = -35 + 0 = -35.
  2. qπA(S,right)=−10+vπA(A)=−15q_{\pi_A}(S, \text{right}) = -10 + v_{\pi_A}(A) = -15 and qπA(S,down)=−40+vπA(B)=−75q_{\pi_A}(S, \text{down}) = -40 + v_{\pi_A}(B) = -75. Then qπA(A,down)=−5+0=−5q_{\pi_A}(A, \text{down}) = -5 + 0 = -5 and qπA(A,left)=−10+vπA(S)=−25q_{\pi_A}(A, \text{left}) = -10 + v_{\pi_A}(S) = -25: going back pays the climb from SS to AA a second time.
  3. They are equal. By definition, qπ(S,a)q_\pi(S, a) fixes the action at SS: it ignores π(⋅∣S)\pi(\cdot \mid S) and depends on the policy only through what it does at the next states, AA and BB. There, the two policies agree (down at AA, right at BB), so they have the same q(S,⋅)q(S, \cdot).
  4. For the same reason, qπB(S,⋅)=qπA(S,⋅)q_{\pi_B}(S, \cdot) = q_{\pi_A}(S, \cdot), since πB\pi_B differs from πA\pi_A only at SS. So vπB(S)=0.3×(−15)+0.7×(−75)=−4.5−52.5=−57v_{\pi_B}(S) = 0.3 \times (-15) + 0.7 \times (-75) = -4.5 - 52.5 = -57, much closer to −75-75: πB\pi_B puts 70% of its probability on the expensive move.
  5. vπB(S)=0.3 [−10+vπB(A)]+0.7 [−40+vπB(B)]=0.3 [−10−5]+0.7 [−40−35]=−57v_{\pi_B}(S) = 0.3\, [-10 + v_{\pi_B}(A)] + 0.7\, [-40 + v_{\pi_B}(B)] = 0.3\, [-10 - 5] + 0.7\, [-40 - 35] = -57. The same computation as in question 4, expanded one step further, from qq into r+γv(s′)r + \gamma v(s').
What to notice

vπ(s)v_\pi(s) is an average of the qπ(s,a)q_\pi(s, a), so it always lies between the smallest and the largest of them, here between −75-75 and −15-15. And qq compares actions without caring what the policy does in the current state: this is what makes the improvement step of Part 2 (section 2) possible.

4. Optimality: the best policy

Wrapping everything up, how do we define the best strategy? What properties does this strategy satisfy?

4.1 Optimal values and optimal policies

The best achievable values, over all policies, are the optimal value functions:

v∗(s)≐max⁡πvπ(s),q∗(s,a)≐max⁡πqπ(s,a)v_*(s) \doteq \max_\pi v_\pi(s), \qquad q_*(s, a) \doteq \max_\pi q_\pi(s, a)

A policy π∗\pi_* is optimal if vπ∗(s)=v∗(s)v_{\pi_*}(s) = v_*(s) in every state.

That a single policy can be best everywhere at once is not obvious; it is a theorem. For a finite MDP with bounded rewards (and γ<1\gamma < 1, or episodes that end), the theorem states that:

So once q∗q_* is known, acting optimally is a table lookup, and there is no need to search among stochastic policies.

Acting from v∗v_* is also possible, π∗(s)=arg⁡max⁡a∑s′,rp(s′,r∣s,a) [r+γv∗(s′)]\pi_*(s) = \arg\max_a \sum_{s', r} p(s', r \mid s, a)\,[r + \gamma v_*(s')], but it needs the model pp; acting from q∗q_* does not. This is why the model-free methods of Part 3 will learn qq rather than vv.

The optimal values satisfy their own Bellman equations, the Bellman optimality equations. An optimal agent puts all its probability on a best action, so the average over the actions becomes a max:

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

No policy is left in these equations: the max is the optimal policy.

Note what changed and what did not: max over what the agent controls (the action), expectation over what it does not (the outcome).

On our 2×2 map, with γ=1\gamma = 1, one equation per state again, each value tied to those of its neighbours:

{v∗(S)=max⁡(−10+v∗(A), −40+v∗(B))v∗(A)=max⁡(−5+v∗(G), −10+v∗(S), −1+v∗(A))v∗(B)=max⁡(−35+v∗(G), −40+v∗(S), −1+v∗(B))v∗(G)=0\left\{ \begin{aligned} v_*(S) &= \max\big( -10 + v_*(A),\ -40 + v_*(B) \big) \\ v_*(A) &= \max\big( -5 + v_*(G),\ -10 + v_*(S),\ -1 + v_*(A) \big) \\ v_*(B) &= \max\big( -35 + v_*(G),\ -40 + v_*(S),\ -1 + v_*(B) \big) \\ v_*(G) &= 0 \end{aligned} \right.

Because of the max, the system is no longer linear, and there is no closed-form solution in general. Part 2 (sections 1 to 3) solves it by iteration; on this small map, Exercise 4.1a solves it by hand.

The optimal policy also depends on γ\gamma. Take a task that never ends, where the agent only decides in one state, s0s_0: left pays +1+1 now and 00 on the way back, right pays 00 now and +2+2 on the way back. Which loop is better?

The discount factor decides the optimal policy
Left: the two loops from s0, the optimal one highlighted. Right: the value of s0 under each policy, as a function of γ. Move γ: a short-sighted agent takes the +1 now, a patient one waits for the +2.
Exercise 4.1a The optimal policy of the altitude map

On the altitude map, with γ=1\gamma = 1:

  1. At AA, three actions are available: down, left and stay. Explain why stay and left are never optimal, and deduce v∗(A)v_*(A). Do the same at BB and deduce v∗(B)v_*(B).
  2. Deduce v∗(S)v_*(S), by comparing right and down from SS.
  3. Plug the three values back into the Bellman optimality equations at SS, AA and BB, and check that they hold.
  4. Deduce π∗\pi_*. Compare it with πA\pi_A and πB\pi_B.
Hint

Start from the equation of AA in the system above, v∗(A)=max⁡(−5+v∗(G), −10+v∗(S), −1+v∗(A))v_*(A) = \max\big( -5 + v_*(G),\ -10 + v_*(S),\ -1 + v_*(A) \big). Can the last term ever be the max, for a finite v∗(A)v_*(A)?

Hint

For left: from SS, going down costs −40-40 and every later reward is ≤0\le 0, so it is worth at most −40-40; going right is worth −10+v∗(A)-10 + v_*(A), which is at least −15-15 (go down to GG from AA). So v∗(S)=−10+v∗(A)v_*(S) = -10 + v_*(A). What does that give for −10+v∗(S)-10 + v_*(S)?

Checkpoint what you should get

v∗(S)=−15v_*(S) = -15, v∗(A)=−5v_*(A) = -5, v∗(B)=−35v_*(B) = -35.

Solution
  1. Stay gives −1+v∗(A)-1 + v_*(A), which is strictly less than v∗(A)v_*(A): it can never be the max. (Staying forever would return −1−1−⋯=−∞-1 - 1 - \cdots = -\infty.) Left gives −10+v∗(S)≤−10+(−10+v∗(A))=−20+v∗(A)<v∗(A)-10 + v_*(S) \le -10 + (-10 + v_*(A)) = -20 + v_*(A) < v_*(A): going back pays the climb between SS and AA twice, for nothing. Only down is left: v∗(A)=−5+0=−5v_*(A) = -5 + 0 = -5. At BB, stay loses for the same reason, and up gives −40+v∗(S)≤−55-40 + v_*(S) \le -55; right gives −35-35, so v∗(B)=−35v_*(B) = -35.

  2. v∗(S)=max⁡(−10+v∗(A), −40+v∗(B))=max⁡(−15,−75)=−15v_*(S) = \max(-10 + v_*(A),\ -40 + v_*(B)) = \max(-15, -75) = -15, reached by right.

  3. With v∗(S)=−15v_*(S) = -15:

    • v∗(A)=max⁡(−5+0, −10−15, −1−5)=max⁡(−5,−25,−6)=−5v_*(A) = \max(-5 + 0,\ -10 - 15,\ -1 - 5) = \max(-5, -25, -6) = -5;
    • v∗(B)=max⁡(−35+0, −40−15, −1−35)=max⁡(−35,−55,−36)=−35v_*(B) = \max(-35 + 0,\ -40 - 15,\ -1 - 35) = \max(-35, -55, -36) = -35;
    • v∗(S)=max⁡(−10−5, −40−35)=−15v_*(S) = \max(-10 - 5,\ -40 - 35) = -15.

    v∗v_* is a fixed point of the optimality equations: one more update changes nothing.

  4. π∗(S)=right\pi_*(S) = \text{right}, π∗(A)=down\pi_*(A) = \text{down}, π∗(B)=right\pi_*(B) = \text{right}: this is πA\pi_A, and indeed vπA(S)=−15=v∗(S)v_{\pi_A}(S) = -15 = v_*(S) (Exercise 3.2). It is deterministic, as the theorem promises, and unique here because no two actions tie. The stochastic πB\pi_B only gets −57-57.

What to notice

Mixing in a bad action never helps once you know q∗q_*: randomness is useless for acting in a known MDP. It becomes useful for learning, when the agent must try actions to find out what they are worth (Part 3).

Exercise 4.1b The discount factor decides the optimal policy

A continuing task: from the state s0s_0, the agent chooses left, which pays +1+1 and leads to slefts_\text{left}, or right, which pays 00 and leads to srights_\text{right}. From slefts_\text{left}, the only action returns to s0s_0 with reward 00; from srights_\text{right}, the only action returns to s0s_0 with reward +2+2. The agent decides only in s0s_0: there are two deterministic policies, πleft\pi_\text{left} and πright\pi_\text{right}.

  1. Write the infinite reward sequence seen from s0s_0 under each policy.
  2. Show that vπleft(s0)=11−γ2v_{\pi_\text{left}}(s_0) = \frac{1}{1 - \gamma^2} and vπright(s0)=2γ1−γ2v_{\pi_\text{right}}(s_0) = \frac{2\gamma}{1 - \gamma^2}.
  3. Which policy is optimal for γ=0\gamma = 0? For γ=0.9\gamma = 0.9? For γ=0.5\gamma = 0.5?
  4. For which γ\gamma are the two policies equally good?
Hint

Each policy cycles through s0s_0 every two steps. Group the rewards by pairs: the sum is a geometric series of ratio γ2\gamma^2.

Hint

Or, with the Bellman equation over one cycle: under πleft\pi_\text{left}, v(s0)=1+γ (0+γ v(s0))v(s_0) = 1 + \gamma\,(0 + \gamma\, v(s_0)).

Checkpoint what you should get

Left is optimal at γ=0\gamma = 0, right at γ=0.9\gamma = 0.9 (18019\frac{180}{19} against 10019\frac{100}{19}), and they tie at γ=12\gamma = \frac12, both worth 43\frac43.

Solution
  1. πleft\pi_\text{left}: 1,0,1,0,…1, 0, 1, 0, \dots and πright\pi_\text{right}: 0,2,0,2,…0, 2, 0, 2, \dots
  2. Under πleft\pi_\text{left}, the reward is 11 at the even steps and 00 at the odd ones: vπleft(s0)=∑k≥0γ2k=11−γ2v_{\pi_\text{left}}(s_0) = \sum_{k \ge 0} \gamma^{2k} = \frac{1}{1 - \gamma^2}. Under πright\pi_\text{right}, 22 at the odd steps: vπright(s0)=∑k≥02γ2k+1=2γ1−γ2v_{\pi_\text{right}}(s_0) = \sum_{k \ge 0} 2\gamma^{2k + 1} = \frac{2\gamma}{1 - \gamma^2}. With the Bellman equation: v=1+γ2vv = 1 + \gamma^2 v and v=2γ+γ2vv = 2\gamma + \gamma^2 v give the same results.
  3. Since 1−γ2>01 - \gamma^2 > 0, compare the numerators, 11 and 2γ2\gamma:
    • γ=0\gamma = 0: 11 against 00, left is optimal (only the next reward counts);
    • γ=0.9\gamma = 0.9: 10019≈5.26\frac{100}{19} \approx 5.26 against 18019≈9.47\frac{180}{19} \approx 9.47, right is optimal;
    • γ=0.5\gamma = 0.5: both are worth 43\frac43, an exact tie.
  4. 11−γ2=2γ1−γ2  ⟺  1=2γ  ⟺  γ=12\frac{1}{1 - \gamma^2} = \frac{2\gamma}{1 - \gamma^2} \iff 1 = 2\gamma \iff \gamma = \frac12.
What to notice

In section 1.3, γ\gamma only reweighted the rewards of a fixed trajectory. Here it changes the argmax: which policy is optimal. γ\gamma is not a tuning knob, it is part of the problem statement, like the rewards. Check your answers on the figure above.

4.2 How to choose a reward? The rewards are part of the problem

All the rewards of the altitude map are negative. Is that an arbitrary choice? Why not add a constant to make them positive, and maximize a “score” instead of minimizing an effort? The answer differs between continuing and episodic tasks.

Exercise 4.2 Do the signs of the rewards matter?

Fix a constant cc and shift every reward of an MDP: Rt′=Rt+cR'_t = R_t + c. Write vπ′v'_\pi for the values in the shifted MDP.

  1. Continuing task, γ<1\gamma < 1. Show that vπ′(s)=vπ(s)+vcv'_\pi(s) = v_\pi(s) + v_c, with a constant vcv_c that depends neither on ss nor on π\pi. Give vcv_c. Does the shift change which policy is optimal?
  2. Episodic task, γ=1\gamma = 1. Let TT be the (random) time at which the episode ends. Show that vπ′(s)=vπ(s)+c Eπ[T∣S0=s]v'_\pi(s) = v_\pi(s) + c\, \mathbb{E}_\pi[T \mid S_0 = s]. Why is this different from question 1?
  3. On the altitude map (γ=1\gamma = 1), shift every reward by c=+11c = +11. Compare the return from SS of πA\pi_A (two steps to GG) with the return of the policy that goes to AA, plays stay kk times, then goes to GG. What is v∗′(S)v'_*(S)?
  4. For which values of cc does the shifted map keep the same optimal behaviour, reaching GG along the cheapest route?
Hint

Question 1: Gt′=∑kγk(Rt+k+1+c)G'_t = \sum_k \gamma^k (R_{t+k+1} + c); split the sum in two.

Hint

Question 3: with the shift, a step to AA pays −10+11=+1-10 + 11 = +1, the last step −5+11=+6-5 + 11 = +6, and each stay −1+11=+10-1 + 11 = +10.

Hint

Question 4: look at what one more stay adds to the return, −1+c-1 + c, and what the last step costs, −5+c-5 + c. Be careful with the policy that stays at AA forever.

Checkpoint what you should get

vc=c1−γv_c = \frac{c}{1 - \gamma}; with c=11c = 11, πA\pi_A returns 77 and dawdling kk times returns 7+10k7 + 10k, so v∗′(S)=+∞v'_*(S) = +\infty; the optimal behaviour survives only for c<1c < 1.

Solution
  1. Gt′=∑k≥0γkRt+k+1+c∑k≥0γk=Gt+c1−γG'_t = \sum_{k \ge 0} \gamma^k R_{t+k+1} + c \sum_{k \ge 0} \gamma^k = G_t + \frac{c}{1 - \gamma}, on every trajectory. Taking the expectation, vπ′(s)=vπ(s)+vcv'_\pi(s) = v_\pi(s) + v_c with vc=c1−γv_c = \frac{c}{1 - \gamma}, the same for every state and every policy, because every trajectory is infinitely long. All policies move by the same amount: their ranking, and π∗\pi_*, do not change. In a continuing task, only the differences between rewards matter, not their sign.

  2. Now G0′=∑t=1T(Rt+c)=G0+c TG'_0 = \sum_{t=1}^{T} (R_t + c) = G_0 + c\,T, so vπ′(s)=vπ(s)+c Eπ[T∣S0=s]v'_\pi(s) = v_\pi(s) + c\, \mathbb{E}_\pi[T \mid S_0 = s] (for policies that end the episode). The shift depends on the policy, through the expected length of the episodes it produces: adding cc to every reward puts a price on time, cc per step, and policies differ in how much time they take.

  3. πA\pi_A: G0′=1+6=7G'_0 = 1 + 6 = 7 (check: −15+2×11=7-15 + 2 \times 11 = 7). Staying kk times at AA: G0′=1+10k+6=7+10kG'_0 = 1 + 10k + 6 = 7 + 10k (check: −15−k+11(k+2)=7+10k-15 - k + 11(k + 2) = 7 + 10k). Each extra stay now pays +10+10: the longer the agent dawdles, the better, without limit, so v∗′(S)=+∞v'_*(S) = +\infty and no policy is optimal. The least-effort problem has become “stay out as long as possible”.

  4. Each extra stay adds −1+c-1 + c:

    • c<1c < 1: staying still costs something, the reasoning of Exercise 4.1a still holds, and π∗\pi_* is unchanged, with v∗′(S)=−15+2cv'_*(S) = -15 + 2c;
    • c=1c = 1: staying is free. Dawdling a finite number of times ties with πA\pi_A, at −13-13, but parking at AA forever returns −9-9 and never pays the −5+1=−4-5 + 1 = -4 of the last step: the optimal agent never reaches GG;
    • c>1c > 1: staying pays, and v∗′(S)=+∞v'_*(S) = +\infty.

    The map stays a least-effort problem only for c<1c < 1. This is why stay costs −1-1: a free stay is exactly the case c=1c = 1.

What to notice

In an episodic task, the level of the rewards is part of the specification: a reward of 00 per step says “time is free”, a negative one says “hurry”, a positive one says “take your time”. A robot rewarded +1+1 at the goal and 00 elsewhere, with γ=1\gamma = 1, scores the same whether it arrives fast or slowly.

4.3 Some useful vocabulary

Solving the Bellman optimality equation is the whole problem: from v∗v_* or q∗q_*, the optimal policy is found from it. We might solve it from different approaches in various cases.

Each one answers a question with one of two opposite terms:

The vocabulary to understand an algorithm
What does it compute?
Prediction: evaluate a given policy, find vπ
or
Control: find the best policy, v* and π*
Does it use the model p?
Model-based: has p, or learns it
or
Model-free: does without p
What does it learn?
Value-based: v or q, then acts greedily
or
Policy-based: π directly. Actor-critic methods learn both.
Whose data does it learn from?
On-policy: the policy it evaluates and improves
or
Off-policy: another policy, the one that behaves
When does it update?
Online: at every step, while interacting
or
Offline: afterwards, at the end of an episode or from stored data
How does a learning agent pick its action?
Exploit: take the best action it knows, and collect reward now
or
Explore: try another one, and learn something for later

Every algorithm of the course can be placed on each of the two axes.

Let’s focus a bit on the last line. Every agent faces a trade-off that a planner does not: explore to find out more about the environment, or exploit what it already knows to collect reward. It is a trade-off, not a choice made once: an agent that only exploits can settle on a mediocre action it never questions, and one that only explores never uses what it has learned. A good learner explores a lot at first, and less as it learns.

Explore or exploit
The second miner leaves his own hole to exploit the first one, and never finds the diamond just below where he stopped. Reward now weighs against knowledge for later: too much of either loses.

4.4 Wrap-up: the 2×2 map, solved

Everything on this page fits on our four-state map. Pick a policy, or build your own one state at a time, and read its values: each one is the Bellman expectation equation of its state, numbers included.

Click on a state to see the values or change its policy:

The map, solved
The value of each state under the chosen policy, computed from the Bellman expectation equations, and the optimal value v* below it. Select a state to see its backup with numbers and to change its action. The line below the map says whether the policy is optimal, and if not, which single change would improve it.
What to look at in the figure above
  • πA reaches v∗v_* everywhere: it is optimal, as in Exercise 4.1a. πB loses 4242 at SS by going down 70% of the time.
  • Stay when you can, with γ=1\gamma = 1: AA and BB are worth −∞-\infty, the hiker never reaches GG. Lower γ\gamma and the values become finite: discounting makes an endless task finite, as in section 1.3.
  • The optimal policy depends on γ\gamma. Staying forever at AA is worth −11−γ-\frac{1}{1-\gamma}, against −5-5 for going down to GG: below γ=0.8\gamma = 0.8, the optimal hiker parks at AA instead of finishing. At BB, where finishing costs 3535, this happens below γ≈0.97\gamma \approx 0.97. A short-sighted agent finds the end of the hike too expensive.

An eye to what’s next 👀

On four states, everything can be solved by hand. But in real problems, solving the Bellman equations exactly is rarely possible. It needs the dynamics, which robots, cars or robots do not hand out; the computing power to handle every state, while Go has about 1017010^{170} legal positions; and the Markov property.

The rest of the course is about approximate solutions, learned from interaction. We’ll first study problems with a model (model-based) before switching to problems with no access to a model (model-free).

Part 2, Planning with a Model, does find approximate solutions on any finite MDP, with iterative algorithms: evaluate a policy, improve it toward the optimal value function, and repeat, until the policy is optimal.

Extra exercises

Exercise E.1 The v*, q*, π* dictionary

Four one-line relations, used constantly from Part 2 on. Give each, and justify it in a sentence.

  1. v∗v_* in terms of q∗q_*.
  2. q∗q_* in terms of v∗v_* and the dynamics p(s′,r∣s,a)p(s', r \mid s, a).
  3. π∗\pi_* in terms of q∗q_*.
  4. π∗\pi_* in terms of v∗v_* and pp.
  5. Compare 3 and 4: to act optimally from q∗q_*, what must you know about the environment? And from v∗v_*?
Hint

An optimal agent puts all its probability on a best action: the average ∑aπ(a∣s) q∗(s,a)\sum_a \pi(a \mid s)\, q_*(s, a) of section 3.1 collapses to a max.

Hint

For 2, the agent chooses aa, but not the outcome: the sum over (s′,r)(s', r) stays an expectation.

Checkpoint what you should get

v∗(s)=max⁡aq∗(s,a)v_*(s) = \max_a q_*(s, a), and π∗(s)∈arg⁡max⁡aq∗(s,a)\pi_*(s) \in \arg\max_a q_*(s, a): acting from q∗q_* needs no model.

Solution
  1. v∗(s)=max⁡aq∗(s,a)v_*(s) = \max_{a} q_*(s, a): an optimal agent puts all its probability on a maximizing action, so the average over the actions becomes a max.
  2. q∗(s,a)=∑s′,rp(s′,r∣s,a) [r+γ v∗(s′)]q_*(s, a) = \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_*(s') \big]: take aa, then act optimally. The sum over (s′,r)(s', r) stays an expectation, since the agent does not choose the outcome.
  3. π∗(s)∈arg⁡max⁡aq∗(s,a)\pi_*(s) \in \arg\max_{a} q_*(s, a). Membership, not equality: when several actions tie, any choice among them, or any mixture of them, is optimal.
  4. Substituting 2 into 3: π∗(s)∈arg⁡max⁡a∑s′,rp(s′,r∣s,a) [r+γ v∗(s′)]\pi_*(s) \in \arg\max_{a} \sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v_*(s') \big].
  5. From q∗q_*, nothing: read the row q∗(s,⋅)q_*(s, \cdot), take the largest entry, act. From v∗v_*, the model pp, to look one step ahead for each candidate action before comparing them.
What to notice

qq is what you can act on without a model. Dynamic programming (Part 2) may work with vv, because the model is given; Monte Carlo, SARSA and Q-learning (Part 3) never know pp, so they estimate qq.

Exercise E.2 The same equations in three-argument notation

Much of the literature does not use p(s′,r∣s,a)p(s', r \mid s, a), but the two functions of section 2.2, p(s′∣s,a)p(s' \mid s, a) and r(s,a)r(s, a).

  1. Show that, for any function vv of the states, ∑s′,rp(s′,r∣s,a) [r+γ v(s′)]=r(s,a)+γ∑s′p(s′∣s,a) v(s′)\sum_{s', r} p(s', r \mid s, a)\, \big[ r + \gamma\, v(s') \big] = r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v(s') Which property of the bracket makes this work?
  2. Deduce the four Bellman equations, for vπv_\pi, qπq_\pi, v∗v_* and q∗q_*, written with p(s′∣s,a)p(s' \mid s, a) and r(s,a)r(s, a) only.
  3. In one sentence: what is lost, and what is gained, in this notation?
Hint

Split the sum in two. In the second term, v(s′)v(s') does not depend on rr: it comes out of the sum over rr.

Checkpoint what you should get

The bracket is affine in rr and in v(s′)v(s'). Only the mean of the reward survives: its distribution is lost.

Solution
  1. Split the sum: ∑s′,rp(s′,r∣s,a) r+γ∑s′(∑rp(s′,r∣s,a))v(s′)=r(s,a)+γ∑s′p(s′∣s,a) v(s′)\sum_{s', r} p(s', r \mid s, a)\, r + \gamma \sum_{s'} \Big( \sum_{r} p(s', r \mid s, a) \Big) v(s') = r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v(s') The first sum is r(s,a)r(s, a) by definition; in the second, v(s′)v(s') does not depend on rr, and summing rr out of p(s′,r∣s,a)p(s', r \mid s, a) gives p(s′∣s,a)p(s' \mid s, a). It works because the bracket is affine in rr and in v(s′)v(s').
  2. Applying 1 to each equation: vπ(s)=∑aπ(a∣s) [r(s,a)+γ∑s′p(s′∣s,a) vπ(s′)]qπ(s,a)=r(s,a)+γ∑s′p(s′∣s,a)∑a′π(a′∣s′) qπ(s′,a′)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′)\begin{aligned} v_\pi(s) &= \sum_{a} \pi(a \mid s)\, \Big[ r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_\pi(s') \Big] \\ 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') \\ v_*(s) &= \max_{a} \Big[ r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, v_*(s') \Big] \\ q_*(s, a) &= r(s, a) + \gamma \sum_{s'} p(s' \mid s, a)\, \max_{a'} q_*(s', a') \end{aligned}
  3. Gained: compactness, sums over the states only, and r(s,a)r(s, a) is often what a simulator hands out. Lost: everything about the distribution of the reward, of which only the mean survives.
What to notice

Losing the distribution is harmless here: the Bellman equations depend on the reward only through its expectation. It is fatal as soon as you care about risk or about the spread of the return, which is why risk-sensitive and distributional RL keep the four-argument form.

Exercise E.3 Episodic or continuing?
  1. The dynamics of section 2.2 satisfy ∑s′,rp(s′,r∣s,a)=1\sum_{s', r} p(s', r \mid s, a) = 1. In an episodic task, which set must s′s' range over for this to hold? Where does the agent land on the last step of an episode?
  2. Pole-balancing, as an episodic task with discounting: every reward is 00, except −1-1 when the pole falls, which ends the episode. Let KK be the number of steps from tt until the fall. Give GtG_t.
  3. The same task as a continuing one: the pole is put back up after each fall, and the task never ends. The falls happen K1<K2<⋯K_1 < K_2 < \cdots steps after tt. Give GtG_t, and say how it differs from question 2.
  4. Check in both cases that maximizing the return means “balance the pole as long as possible”.
Hint

Question 2: a single reward is nonzero, Rt+K=−1R_{t+K} = -1, and it carries the weight γK−1\gamma^{K-1}.

Checkpoint what you should get

Episodic: Gt=−γK−1G_t = -\gamma^{K-1}. Continuing: Gt=−∑i≥1γKi−1G_t = -\sum_{i \ge 1} \gamma^{K_i - 1}.

Solution
  1. Over the states and the terminal state. On the last step, the agent lands in the terminal state; leaving it out of the sum would lose that probability, and the total would fall short of 11. The condition still only concerns the non-terminal ss, since no action is taken from a terminal state: the agent acts from the non-terminal states and lands anywhere, terminal included.
  2. Only the fall contributes: Gt=−γK−1G_t = -\gamma^{K-1} (a fall at the very next step, K=1K = 1, gives −1-1).
  3. Every future fall contributes, each discounted by how far away it is: Gt=−∑i≥1γKi−1G_t = -\sum_{i \ge 1} \gamma^{K_i - 1}. The episodic return only sees the next fall, what comes after belongs to another episode; the continuing return adds up the whole infinite future.
  4. In both cases the return is a sum of terms −γK−1-\gamma^{K-1}, which increase with KK when γ<1\gamma < 1: pushing every fall further away increases the return, and an agent that never lets the pole fall reaches the best possible return, 00.
What to notice

Here the rewards are 00 or negative and γ<1\gamma < 1, so “maximize the return” and “last as long as possible” coincide. Exercise 4.2 showed how easily this breaks when the level of the rewards changes.

Exercise E.4 Limits of the MDP framework

No computation, and no single answer.

  1. Is the MDP framework enough to represent every goal-directed learning task? Propose at least two counterexamples, and say in each case what exactly breaks.
  2. Consider driving a car. The actions could be the accelerator, the steering wheel and the brakes; or one level below, the driver’s muscles; or one level above, the choice of a route. Where should the boundary between agent and environment be drawn? Is there a single right answer? What rule of thumb would you propose?
Solution

Some directions, not a complete answer.

  1. The state may not be observable: a poker player does not see the other hands, and the observation is not Markov (section 2.1). The goal may not fit in one number: several objectives that cannot be traded against each other, or constraints that must never be violated. Other agents may learn at the same time, so the dynamics are no longer stationary. And the reward may simply not be available, or be too sparse to learn from (section 1.2).
  2. There is no single answer; Sutton and Barto’s rule is that the boundary separates what the agent can change arbitrarily from what it cannot. Anything it cannot control directly belongs to the environment, even its own body. In practice, the boundary is chosen at the level where the decisions of interest are taken: the route for navigation, the pedals and the wheel for driving, the muscles for motor control.