Skip to main content
Open In Colab This section runs Monte-Carlo Prediction and Temporal Difference (TD) Prediction on the random walk of Sutton and Barto, Example 6.2, and compares how fast and how accurately each one learns the values of a fixed policy (Sutton & Barto, 2018).

The random walk as a Markov reward process

When the policy is fixed, the agent has no decision left to make, and the MDP reduces to a Markov reward process (MRP), the tuple ⟨S,P,R,γ⟩\langle \mathcal S, \mathcal P, \mathcal R, \gamma \rangle. Its transition probabilities and expected rewards are those of the MDP, averaged over the actions of the policy: Pss′π=∑aπ(a∣s) p(s′∣s,a),rπ(s)=∑aπ(a∣s) r(s,a).\mathcal P^\pi_{ss'} = \sum_a \pi(a \mid s)\, p(s' \mid s, a), \qquad r_\pi(s) = \sum_a \pi(a \mid s)\, r(s, a). Prediction is a question about this MRP: the value function of the MRP is vπv_\pi. Random walk MRP from Sutton and Barto, Example 6.2. The random walk: five nonterminal states A to E between two terminal states. Every episode starts in C. At every step the walk moves left or right with probability 1/2. The reward is +1+1 when the walk steps off the right end and 0 on every other transition, and γ=1\gamma = 1. The value of a state is therefore the probability that a walk from it ends on the right. The Bellman equation of the MRP, v=r+Pvv = r + \mathcal P v, is a linear system with five unknowns. The code builds P\mathcal P and rr and solves it.
The values are 1/6,2/6,3/6,4/61/6, 2/6, 3/6, 4/6 and 5/65/6 for A to E. Each one is the average of its two neighbors, so they lie on a straight line from 0 at the left exit to 1 at the right exit.

TD(0) estimates during learning

The two update rules below act on one episode, stored as a list of transitions (St,Rt+1,St+1)(S_t, R_{t+1}, S_{t+1}). TD(0) updates after every transition. MC computes the returns backward from the end of the episode and then moves each visited state toward its return.
The next cell runs TD(0) with α=0.1\alpha = 0.1, starting from V(s)=0.5V(s) = 0.5 in every state, and records the estimates after 0, 1, 10 and 100 episodes.
Output from cell 5 TD(0) estimates after 0, 1, 10 and 100 episodes, with α=0.1\alpha = 0.1. The lines for 0 and 1 episodes coincide except at E. The first episode wandered for 25 steps before it stepped off the right end, yet it changed only one estimate: V(E)V(E) rose from 0.5 to 0.55. Every other transition had reward 0 and led from a state valued 0.5 to another state valued 0.5, so its TD error was 0. Only the last transition, from E to the exit, had a nonzero TD error, 1+0−0.5=0.51 + 0 - 0.5 = 0.5, and α=0.1\alpha = 0.1 moved V(E)V(E) by 0.05. After 10 episodes the estimates already rise from A to E, and after 100 episodes they are close to the true line, with an RMS error of 0.088. With a constant step size the estimates keep moving around the true values instead of settling on them.

RMS error over episodes

A single run is noisy. The next figure measures the error of the estimates after each episode as the root-mean-square difference from the true values over the five states, RMS(V)=15∑s(V(s)−vπ(s))2,\text{RMS}(V) = \sqrt{\frac{1}{5} \sum_{s} \bigl(V(s) - v_\pi(s)\bigr)^2}, averaged over 100 independent runs. It compares constant-α\alpha MC with α\alpha from 0.01 to 0.04 and TD(0) with α\alpha from 0.05 to 0.15, the settings of Sutton and Barto’s figure.
Output from cell 7 RMS error of the estimates after each episode, averaged over 100 runs. Dashed: constant-α\alpha MC. Solid: TD(0). Darker lines have larger step sizes. Every TD(0) curve ends below every MC curve. After 100 episodes the best TD(0) setting, α=0.05\alpha = 0.05, has an RMS error of 0.035, and the best MC setting, α=0.02\alpha = 0.02, has 0.082. A larger step size learns faster at first and then levels off at a higher error, because each estimate follows its latest targets more closely and averages fewer of them. Some curves reach their lowest error before episode 100 and rise slightly afterwards. TD(0) with α=0.15\alpha = 0.15, for example, reaches 0.052 and ends at 0.071.

Batch updating

The two methods also differ when they see the same data. In batch updating the agent keeps a fixed set of episodes and presents them again and again. Each pass computes the TD(0) or MC increment for every transition, adds the increments up, and changes VV only at the end of the pass. With a small enough α\alpha, both procedures converge, each to an answer that does not depend on α\alpha:
  • Batch MC converges to the average of the returns observed from each state.
  • Batch TD(0) converges to the exact values of the maximum-likelihood model of the data: the MRP whose transition probabilities and expected rewards are the frequencies and averages observed in the episodes. This is the certainty-equivalence estimate, because it treats the fitted model as if it were known to be right.
The code checks the second statement on 20 episodes. It then computes both fixed points directly after each of 100 episodes, averaged over 100 runs.
Output from cell 9 RMS error of the batch fixed points after each episode, averaged over 100 runs. Iterating the batch TD(0) updates gives the values of the maximum-likelihood model to six decimals. After one episode both methods have the same large error, about 0.40, because each one fits a single walk. From the second episode on, batch TD(0) has the lower error: 0.125 against 0.184 after 10 episodes, and 0.038 against 0.056 after 100. Both methods see the same episodes, so the difference comes only from how each one uses them.

You are the predictor

Suppose you observe eight episodes of an unknown MRP with two states, A and B, and γ=1\gamma = 1. One episode starts in A, moves to B with reward 0, and ends from B with reward 0. Six episodes start in B and end with reward 1. The last one starts in B and ends with reward 0. What are the values of A and B?
Both methods give V(B)=0.75V(B) = 0.75, the fraction of the eight episodes through B that ended with reward 1. They disagree on A. Batch MC gives V(A)=0V(A) = 0, because the only episode through A returned 0. Batch TD(0) gives V(A)=0.75V(A) = 0.75: in the data, A always moved to B with reward 0, so in the fitted model the value of A equals the value of B. The MC answer fits the observed returns exactly. If the process is Markov, the TD answer predicts future returns from A better, because it uses all eight episodes through B and not only the one through A. The same effect explains why TD(0) learned faster on the random walk.

The MC vs TD(0) trade-off

The two methods sit at opposite ends of a trade-off between bias, variance and use of the Markov structure. Bias. The MC target is the actual return GtG_t, an unbiased sample of vπ(St)v_\pi(S_t). The TD(0) target Rt+1+γV(St+1)R_{t+1} + \gamma V(S_{t+1}) is biased whenever V(St+1)V(S_{t+1}) is wrong. At the start every state has V=0.5V = 0.5, which is wrong everywhere except at C. Variance. GtG_t depends on the whole random trajectory from tt to the end of the episode, so its variance grows with the length of the episode. The TD(0) target depends on one random transition. On the random walk, where an episode can take dozens of steps, the MC target is either 0 or 1, while the TD(0) target moves only by the difference between neighboring estimates. The lower variance lets TD(0) use larger step sizes without becoming unstable. Use of the Markov property. TD(0) treats V(St+1)V(S_{t+1}) as a summary of everything that happens after St+1S_{t+1}, which is the Markov assumption. MC averages the observed returns and does not use the Markov structure. When the process is Markov, as the random walk is, TD(0) reuses what it has learned about the successor states, and MC has to learn the value of each state from that state’s own returns. When to use each.
  • Episodic or continuing tasks. MC needs GtG_t, so the episode has to end. TD(0) updates after every step and also works on continuing tasks.
  • Long episodes. The variance of the MC target grows with the length of the episode, and the TD(0) target’s does not.
  • Learning early. TD(0) learns from the first transition of the first episode, and MC waits until the episode ends.
  • A state that is not Markov. MC does not rely on the Markov property, so it can be the safer choice when the state hides part of the past.
n-step TD and the λ-return covers targets between these two ends. Key references: (Sutton & Barto, 2018)

References

  • Sutton, R., Barto, A. (2018). Reinforcement Learning: An Introduction. Link

Found an error on this page? Report an issue.