Model-Free Prediction
Organisation
- Group assignments are done and fixed.
- Presentations: what we expect
- Guest lecture in the week of 17 December: what is your availability?
- This course will be given again next semester.
Today
- Recap: Bellman equations, value iteration and policy iteration
- From planning to learning: what if we don't know $P$ and $r$?
- Monte Carlo prediction
- Temporal-difference prediction and bootstrapping
- Bias vs. variance, and TD($\lambda$) in between
Recap: planning with a model
Same maze, same notation as last week.
RecapThe two-exit maze and its optimal values
Deterministic moves. Exits: $+1$ (close), $+5$ (far). All other rewards $0$.
At $\gamma = 0.9$ the far exit is worth it from everywhere: $V^\star(s_1) = 5\gamma^5 \approx 2.95$.
RecapValue iteration and policy iteration
- $V_0 = 0$
- Repeat, for all $s$: $$V_{k+1}(s) = \max_a \Big( \textcolor{#b5541c}{r(s,a)} + \gamma \sum_{s'} \textcolor{#b5541c}{P(s'|s,a)}\, V_k(s') \Big)$$
- Act greedily w.r.t. the last $V_k$
- Start from any $\pi_0$
- Evaluate: $$V^{\pi_k} = \textcolor{#b5541c}{r^{\pi_k}} + \gamma\, \textcolor{#b5541c}{P^{\pi_k}}\, V^{\pi_k}$$
- Improve, for all $s$: $$\pi_{k+1}(s) = \argmax_a \big( \textcolor{#b5541c}{r(s,a)} + \gamma \textstyle\sum_{s'} \textcolor{#b5541c}{P(s'|s,a)}\, V^{\pi_k}(s') \big)$$
RecapVI and PI side by side
From planning to learning
Someone gives you a policy $\pi$: a direction to take in every cell (right).
But you know nothing about the world: not $P$, not $r$. You don't know where the exits are or what they pay.
All you can do is follow $\pi$ and observe: $$S_0, A_0, R_1, S_1, A_1, R_2, \dots, S_{T-1}, A_{T-1}, R_T$$
Reinforcement learning
= generalised policy iteration from data
- Policy evaluation: estimate the current value function using data
- Policy improvement: update the policy gradually towards the greedy one
- Today: evaluation only (prediction). The policy is fixed.
- Next week (Chris): improvement, i.e. control. SARSA and Q-learning.
Today's setting: a fixed policy
$\pi$: with probability $0.8$ follow $\pistar$, with probability $0.2$ pick a random action.
- Not optimal, not random: an arbitrary fixed policy
- Episodes start in $s_1$, $\gamma = 0.9$
- We know $\Vpi$ because we know the model, so we can check the agent's estimates
Monte Carlo prediction
A value is an expectation, so estimate it by an average.
Monte Carlo: the idea
- Play an episode with $\pi$ until it terminates
- For each state visited, compute the return that followed, from this state on: $\;G_t = R_{t+1} + \gamma R_{t+2} + \dots + \gamma^{T-t-1} R_T$
- Average these returns over episodes. By the law of large numbers this converges to $\Vpi(s)$.
Let's practice: returns of two episodes
$\gamma = 0.9$. Backwards: $\;G_t = R_{t+1} + \gamma\, G_{t+1}$, $\;G_T = 0$.
| Episode 1 (follows π) | |||
|---|---|---|---|
| $t$ | $S_t$ | $R_{t+1}$ | $G_t$ |
| 0 | $s_1$ | 0 | 2.95 |
| 1 | $s_2$ | 0 | 3.28 |
| 2 | $s_3$ | 0 | 3.65 |
| 3 | $s_6$ | 0 | 4.05 |
| 4 | $s_7$ | 0 | 4.50 |
| 5 | $s_{10}$ | 5 | 5 |
| Episode 2 | |||
|---|---|---|---|
| $t$ | $S_t$ | $R_{t+1}$ | $G_t$ |
| 0 | $s_1$ | 0 | 0.90 |
| 1 | $s_2$ | 1 | 1 |
In $s_2$ the agent took one of its random actions ($\uparrow$) and fell into the small exit.
States never visited keep their initial value $V = 0$: no data, no estimate.
Monte Carlo prediction: the algorithm
- $V(s) = 0$, $N(s) = 0$ for all $s$
- For each episode $S_0, A_0, R_1, \dots, R_T$ generated by $\pi$:
$G \leftarrow 0$; for $t = T-1, \dots, 0$:- $G \leftarrow R_{t+1} + \gamma G$
- $N(S_t) \mathrel{+}= 1, \quad V(S_t) \leftarrow V(S_t) + \frac{1}{N(S_t)}\big(G - V(S_t)\big)$
Why $\tfrac{1}{N}$? From the average to a step size $\alpha$
After $N$ visits of $s$ with returns $G_1, \dots, G_N$, the Monte Carlo estimate is their average:
- The update with $\tfrac1N$ gives exactly the same number as recomputing the average
- But it only stores $V$ and $N$, not all past returns: cheaper, and it updates one return at a time
Recent returns count more; old ones are forgotten geometrically. Useful when the policy keeps changing (next week).
Let's practice: MC by hand, then many episodes
First the three episodes below. Step plays one transition; at the end of an episode, each Step performs one update, going backwards.
- Ep. 1: $s_1 \to s_2 \to s_3 \to s_6 \to s_7 \to s_{10} \to$ +5
- Ep. 2: $s_1 \to s_2 \to$ +1 (random action in $s_2$)
- Ep. 3: $s_1 \to s_5 \to s_1 \to s_2 \to \cdots \to$ +5 (revisits $s_1$)
Then +10, +100: new random episodes from $\pi$. Compare with the true $\Vpi$. ($n$ = number of updates of each cell.)
Monte Carlo: properties
Good
- Unbiased: $\E[G_t \mid S_t = s] = \Vpi(s)$
- Simple; no model needed
- Does not rely on the Markov property
Less good
- High variance: $G_t$ sums many random rewards and transitions
- Must wait until the end of the episode to update
- Only for episodic tasks
Questions?
Temporal-difference learning
Learn a guess from a guess.
TD(0): the idea
By definition of the return: $\quad G_t = R_{t+1} + \gamma\, \cfut{G_{t+1}}$
MC uses $G_t$ as the target, so it must wait until the episode ends to know $\cfut{G_{t+1}}$.
TD: after one step we don't know $\cfut{G_{t+1}}$ yet, but $\cfut{V(S_{t+1})}$ is our current estimate of it, since $\Vpi(s') = \E_\pi[G_{t+1} \mid S_{t+1} = s']$:
with $V(\text{exit}) = 0$
- Update after every step, online
- Works for continuing tasks (no end needed)
- A sample of the Bellman expectation equation
- We say the estimator bootstraps: it uses its own stored values $V(S_{t+1})$
Backup diagrams: what each target looks at
DP
$V(s) \leftarrow \sum_a \cpol{\pi(a|s)} \big( \crew{r(s,a)}$
$\qquad\qquad + \gamma \sum_{s'} \cfut{P(s'|s,a)\, V(s')} \big)$
Needs the model. One step, all branches.
MC
$V(S_t) \leftarrow V(S_t) + \alpha\big(\crew{G_t} - V(S_t)\big)$, $\;\crew{G_t = R_{t+1} + \gamma R_{t+2} + \cdots}$
One sampled trajectory, all the way to the end.
TD(0)
$V(S_t) \leftarrow V(S_t) + \alpha\big(\crew{R_{t+1}} + \gamma\, \cfut{V(S_{t+1})} - V(S_t)\big)$
One sampled step, then bootstrap.
Let's practice: TD(0) by hand, same episodes
$\alpha = 0.5$, $V_0 = 0$. Each Step = one transition and its update.
- After episode 1, which states have changed? Only $s_{10}$: $V(s_{10}) = 2.5$
- After episode 3: $V(s_7) = 1.125$, $V(s_{10}) = 3.75$. Information travels one step back per visit.
MazeMC and TD on the same episodes
The $+5$ moves back one state per visit, scaled by $\alpha$. MC moves it all the way back in one episode.
Here the conditions are ideal for MC: short episodes, a single reward at the end, deterministic moves. See the appendix for the bigger maze.
Bias, variance, and TD($\lambda$)
MazeBias and variance of the estimate of $V(s_2)$
Exactly the setting of the previous slide: same maze and policy, $\gamma = 0.9$, start in $s_1$, $V_0 = 0$, both methods with $\alpha = 0.2$. We repeat the 100-episode run 1000 times and record $V(s_2)$ after every episode.
Bias–variance trade-off
| Target | Bias | Variance | |
|---|---|---|---|
| MC | $G_t = R_{t+1} + \gamma R_{t+2} + \dots$ | none: $\E[G_t | S_t = s] = \Vpi(s)$ | high: many random steps |
| TD | $R_{t+1} + \gamma V(S_{t+1})$ | biased while $V \neq \Vpi$ | low: one random step |
- The "ideal" TD target $R_{t+1} + \gamma \Vpi(S_{t+1})$ is unbiased, but we don't have $\Vpi$
- Bias of the TD target: $\;\gamma\,\E\big[V(S_{t+1}) - \Vpi(S_{t+1})\big]$, which shrinks as $V$ improves
- Both converge to $\Vpi$ (tabular, suitable step sizes $\sum \alpha_t = \infty$, $\sum \alpha_t^2 < \infty$)
A detailed bias–variance study in the bigger maze is in the appendix.
In between: $n$-step returns
Look $n$ steps ahead, then bootstrap:
| $n$ | $1$ | $2$ | $\cdots$ | $\infty$ |
|---|---|---|---|---|
| target | $R_{t+1} + \gamma V(S_{t+1})$ (TD(0)) | $R_{t+1} + \gamma R_{t+2} + \gamma^2 V(S_{t+2})$ | $G_t$ (MC) |
$n$-step TD: $\;V(S_t) \leftarrow V(S_t) + \alpha\big(G_t^{(n)} - V(S_t)\big)$. Small $n$: more bias. Large $n$: more variance.
TD($\lambda$): average over all $n$
- Geometric weights, summing to 1
- $\lambda = 0$: TD(0). $\;\lambda = 1$: MC.
- "Forward view": still needs the future, like MC
TD($\lambda$), backward view: eligibility traces
- Each episode: $e(s) = 0$ for all $s$
- At each step $t$:
- $\delta_t = R_{t+1} + \gamma V(S_{t+1}) - V(S_t)$
- $e(s) \leftarrow \gamma \lambda\, e(s)$ for all $s$; $\quad e(S_t) \leftarrow e(S_t) + 1$
- $V(s) \leftarrow V(s) + \alpha\, \delta_t\, e(s)$ for all $s$
- $e(s)$ is a discounted counter of visits: $+1$ at each visit of $s$, then shrinks by $\gamma\lambda$ at every step: $\;e_t(s) = \sum_{k \le t} (\gamma\lambda)^{t-k}\, \mathbb{1}[S_k = s]$
- Online, one step at a time, like TD(0)
- Sends each TD error back to all recently visited states at once
- Offline, it matches the forward view (Sutton & Barto §12.4)
Example: episode $s_1 \to s_5 \to s_1 \to s_2 \to s_3$, with $\gamma\lambda = 0.5$. At $t = 4$, the TD error $\delta_4$ updates every visited state, weighted by its trace (box on the right).
MazeTD($\lambda$) on the same episodes
TD($\lambda$) sends the final TD error back along the whole path, with weight $(\gamma\lambda)^k$.
MazeChoosing $\lambda$
Average error over the first 30 episodes, for four step sizes $\alpha$.
- Small $\alpha$: $\lambda$ close to 1 is best. The bias from $V_0 = 0$ dominates, so it pays to look far ahead.
- Larger $\alpha$: the best $\lambda$ is in between (about 0.6–0.8). Variance matters too.
- Same picture as Sutton & Barto, Fig. 12.3
Is TD($\lambda$) still used?
- Eligibility traces as such: less common in deep RL. They don't combine easily with experience replay.
- But the idea is everywhere:
- $n$-step returns: A3C, Rainbow DQN
- $\lambda$-returns of advantages, i.e. GAE in PPO (policy gradients, December)
- Off-policy multi-step corrections: Retrace, V-trace
- Takeaway: how far to look before bootstrapping is a central design choice
From $V$ to $Q$: $n$-step TD for action values
Same ideas, with state–action pairs instead of states: estimate $\Qpi(s,a) = \E_\pi[G_t \mid S_t = s, A_t = a]$, a table of $|\S| \times |\A|$ numbers (48 in the maze).
Along the episode $\;S_t, A_t, \crew{R_{t+1}}, S_{t+1}, A_{t+1}, \crew{R_{t+2}}, \dots, S_{t+n}, A_{t+n}$, with all actions drawn from $\pi$:
Uses the quintuple $(S_t, A_t, R_{t+1}, S_{t+1}, A_{t+1})$.
ConclusionsTwo ways to estimate a value function from data
- Unbiased, but high variance
- Waits for the end of the episode
- Needs no Markov property
- Biased while $V$ is wrong, but low variance
- Learns after every step
- Uses the Markov structure
In between: $n$-step returns and TD($\lambda$) choose how far to look before bootstrapping. The same ideas estimate $Q$ as well as $V$.
The TD error is the gap between the current estimate and the (sampled) Bellman target:
It works like a loss: $V(S_t) \leftarrow V(S_t) + \alpha\, \delta_t$ is a gradient step on $\tfrac12 \delta_t^2$ (target held fixed). Learning a value function becomes "make the Bellman error small", for any model of $V$ or $Q$.
Next time: Model-free control (Chris)
- We can now estimate $\Qpi$ from data, for a fixed $\pi$
- Improve the policy as we go, while still exploring
- TD for $Q$ + improvement = SARSA, Q-learning
Bias–variance study
MC and TD compared in the bigger maze: spread of the targets, online learning, and what each method makes of the same data.
AppendixA bigger maze, with rewards along the way
The two-exit maze is small, with a single reward at the end. What about bigger problems with rewards along the way? A 10×10 maze:
- Coins $+1$ and traps $-1$, each time you step on them
- Exits: $+5$ (far, behind a trap), $+1$ (near the start)
- Slippery: 20% chance to slip
- Given policy: the shortest route to the big exit, with 20% random actions
- $\gamma = 0.95$; episodes last about 30 steps
Every return $G_t$ now adds up many random rewards: coins and traps hit or missed, slips.
Bigger mazeMonte Carlo in the bigger maze
- Press Episode a few times, then +10, +100
- Each episode updates every cell along its path, with a very noisy return
- Cells off the route are rarely visited, so they stay wrong for a long time
Bigger mazeWhat do the targets for $V(\text{start})$ look like?
300 episodes from the start cell. Each row is a histogram: bar height = how many of the 300 targets fall in that range. Black line = their mean.
- MC: centred on the true value, but spread out: every coin, trap and slip along the way adds noise
- TD with a good $V$: same centre, about half the spread: only one random step
- TD at the start ($V = 0$): almost no spread, but wrong. That's the bias.
Bigger mazeMC and TD online, on the same episodes
Near the exits TD is quickly accurate. Far from them, the $+5$ still has to travel back one cell per visit.
Bigger mazeLearning curves, online
Average over 20 runs; error weighted by how often $\pi$ visits each state.
- Online, from $V_0 = 0$, MC is still ahead early on: TD pays for its bias while values propagate back
- A larger $\alpha$ speeds TD up but leaves more noise at the end
- So is TD just worse? Look at what happens when both get the same data and can use it fully.
Bigger mazeSame data, better answer: batch TD
Batch setting: collect $K$ episodes, then replay them again and again until the estimates stop changing.
- Batch MC = average of the observed returns
- Batch TD(0) = exact value of the MDP estimated from the data (empirical transitions and rewards)
- TD is about 25% more accurate for every amount of data: it uses the Markov structure, MC does not
Bigger mazeWhy: TD reuses what it knows about the future
50 ordinary episodes from the start, then one episode from the new cell. How good is each estimate of $V(\text{new cell})$?
- MC: the only data is that one noisy return
- TD: one step, then it joins states it already knows well. It bootstraps from them.
References
- R. Sutton, A. Barto. Reinforcement Learning: An Introduction, 2nd ed., 2018. Chapters 5, 6, 7, 12.
- Cs. Szepesvári. Algorithms for Reinforcement Learning, 2010. Chapter 2.
- D. Silver. UCL RL course, Lecture 4.
- G. Martius, P. Kolev. RL Lecture 4 (Model-free prediction), Universität Tübingen, 2026.
- J. Schulman et al. High-dimensional continuous control using generalized advantage estimation. ICLR 2016.