Skip to main content
This section estimates the value function vπv_\pi of a given policy π\pi when the MDP is unknown. You can interact with the environment and observe what it does, but you do not have its dynamics p(s′,r∣s,a)p(s', r \mid s, a). This is the model-free prediction problem. The methods for it learn from sampled experience, and they also help when the model is known but too large for the dynamic programming of the MDP chapter. This section covers Monte Carlo (MC) methods. Temporal Difference (TD) Prediction covers the other family, and MC vs TD(0) compares the two on one example. From here on, the capital letters VV and QQ denote estimates of vπv_\pi and qπq_\pi.

Averaging sampled returns

The value of a state is the expected return from it, vπ(s)=Eπ[Gt∣St=s]v_\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s]. An episode that follows π\pi and visits ss at time tt gives one sample of that return: Gt=∑k=0T−t−1γkRt+1+k,G_t = \sum_{k=0}^{T-t-1}\gamma^k R_{t+1+k}, where TT is the final time step of the episode. MC prediction estimates vπ(s)v_\pi(s) by the average of the returns observed after visits to ss. By the law of large numbers, the average converges to vπ(s)v_\pi(s) as the number of visits grows (Sutton & Barto, 2018). The name comes from this averaging of random samples. Backup tree of MC prediction. The estimate for the root state is updated from a whole sampled trajectory, down to the terminal state T shown in red. The return is known only when the episode ends. MC prediction therefore works only for episodic tasks, and it updates its estimates only after an episode is complete.

First-visit and every-visit MC

A state can be visited more than once in the same episode. First-visit MC averages, in each episode, only the return that follows the first visit to ss. Every-visit MC averages the returns that follow every visit. Both converge to vπ(s)v_\pi(s). In first-visit MC the returns from different episodes are independent samples of vπ(s)v_\pi(s), so the estimate is unbiased and its standard error falls as 1/n1/\sqrt{n} with the number nn of returns.
The loop runs backward through the episode, so each return comes from the next one through the recursion Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}.

Incremental updates

You do not need to store every return. The mean of kk numbers follows from the mean of the first k−1k-1: μk=1k∑j=1kxj=1k(xk+∑j=1k−1xj)=1k(xk+(k−1)μk−1)=μk−1+1k(xk−μk−1).\begin{aligned} \mu_k &= \frac{1}{k} \sum_{j=1}^k x_j = \frac{1}{k} \bigl( x_k + \sum_{j=1}^{k-1} x_j \bigr) \\ &= \frac{1}{k} ( x_k + (k-1) \mu_{k-1} ) = \mu_{k-1} + \frac{1}{k} ( x_k - \mu_{k-1} ). \end{aligned} With the return GtG_t that follows a visit to ss as the new sample, the estimate is updated after each episode: N(s)←N(s)+1,V(s)←V(s)+α(Gt−V(s)),\begin{aligned} N(s) &\leftarrow N(s) + 1, \\ V(s) &\leftarrow V(s) + \alpha ( G_t - V(s) ), \end{aligned} where N(s)N(s) counts the returns seen from ss. With α=1/N(s)\alpha = 1/N(s) this is the sample average, which weights every return equally. A constant step size α∈(0,1]\alpha \in (0, 1] gives constant-α\alpha MC instead. It is an exponentially weighted average that favors recent returns, which helps when the environment changes over time (Sutton & Barto, 2018).

Properties of MC prediction

The MC target is unbiased, but its variance can be large. GtG_t is an unbiased sample of vπ(St)v_\pi(S_t). It is also a sum of every reward until the end of the episode, and each of those rewards is random, so long episodes give noisy targets and need many samples. MC does not bootstrap. Each state’s estimate uses only the returns that followed that state, never the estimate of another state as dynamic programming does. The cost of estimating one state’s value therefore does not grow with the number of states, which makes MC a good fit when you need the values of only a few states. The navigation MDP section uses this property. It estimates the value of a go-to-goal controller on a simulated TurtleBot3 from the returns of rollouts, at a grid of start positions facing the goal, without a table of transition probabilities. MC does not rely on the Markov property. The average of the sampled returns is the right answer whenever the episodes follow π\pi. Because MC never uses the estimate at the next state, it is not misled when the state does not summarize the past, for example when the agent’s observation hides part of the true state.

Action values

Estimating action values qπ(s,a)q_\pi(s,a), the expected return after taking aa in ss and following π\pi afterwards, works the same way, with one average per state-action pair. Action values are what model-free control needs. Improving a policy from VV requires the dynamics p(s′∣s,a)p(s' \mid s, a) to look one step ahead, while improving it from QQ only requires arg⁡max⁡aQ(s,a)\arg\max_a Q(s,a). 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.