ai theory
Reinforcement Learning Basics (6) — Monte Carlo Prediction
Junyoung Park · 2024-04-19 · 7 min
Introduction
Dynamic programming in the previous article assumed that we knew the MDP's transition probabilities and reward function.
Given this model, we can perform a Bellman backup by taking a probability-weighted average over every possible next state.
But in a new game or a real robot, it is difficult to know in advance the probability of reaching each next state after an action. We cannot fill in the transition matrix before experiencing the environment.
Monte Carlo methods take a very straightforward approach.
If the model is unknown, run actual episodes to the end and correct the values with the returns received.
It is like rolling a die repeatedly and taking an average when we do not know the probability of each face. Instead of calculating the probabilities of every possible outcome, we collect actual samples to approximate the expectation.
This article introduces Monte Carlo prediction, which estimates the value of a fixed policy from experience.
First, Distinguish Prediction from Control
In reinforcement learning, prediction is the problem of evaluating how good a fixed policy is.
Control is the problem of finding a good policy itself.
Monte Carlo prediction does not change the policy. We run several episodes under policy , observe the return obtained from each state, and estimate .
If we begin evaluation and improvement at the same time, it is easy to lose track of what each equation is changing. We therefore begin by making a report card for one fixed habit of behavior.
Replacing the Expected Value with an Empirical Average
The state value is defined as
Calculating this expectation exactly requires the probabilities of every possible path. Monte Carlo instead collects returns actually observed from state and averages them.
- : the number of times state has been used for evaluation
- : the return after state in sample
- : the value estimated from experience
With enough samples, the empirical average approaches the true expected value .
The Return Is Known Only After the Episode Ends
Consider the following short episode.
Let . After the episode ends, we calculate the return backward from the final state.
The immediate reward at is , but its return is because it also includes the later rewards and .
We now move the value estimate of toward , and the estimate of toward .
An Incremental Update Without Recalculating the Average
We do not need to sum every previous return again whenever a new one arrives. The average can be updated incrementally.
The expression inside the brackets,
is the difference between the newly observed return and the previous expectation.
For example, suppose , the new return is , and the visit count is now . Then
The old estimate of moves one fifth of the distance toward the new observation of .
We can also use a fixed learning rate instead of the visit count.
A fixed does not preserve every old experience with equal weight. It continues to give recent experience a constant amount of influence, which is useful in non-stationary problems where the environment changes over time.
First-Visit and Every-Visit
The same state can appear several times in one episode.
Two methods arise depending on which visit's return we use.
First-Visit Monte Carlo
Use only the return following the first occurrence of state in an episode. In the episode above, only the first produces an update.
Every-Visit Monte Carlo
Use the return after every occurrence of state . Because the three occurrences of have different remaining futures, they produce three updates.
Under sufficient conditions, both methods converge to . When first learning the idea, it is more useful to see the names as answers to one question: “if a state repeats within an episode, which visits should count?”
Monte Carlo Does Not Bootstrap
The Monte Carlo target is the actual return .
The target does not contain the current value estimate . We say that this method does not bootstrap.
Bootstrapping replaces an unobserved part of the future with a current estimate. Monte Carlo waits to observe the entire reward sequence, so it borrows no estimate in its target.
This property has both advantages and disadvantages.
Advantages
- It does not require transition and reward models.
- Even if the current value estimate is wrong, the target does not directly reuse that error.
- Its target—the return from an actual episode—is easy to understand.
Disadvantages
- It must wait for the episode to end.
- It is difficult to apply as-is to a continuing task that never ends.
- The return can have high variance because it is affected by randomness in many actions, transitions, and rewards.
Returns Can Vary Greatly Even from the Same State
Suppose five episodes beginning from state produce the following returns.
Their average is .
Yet individual samples range widely, from to . If both the policy and environment are stochastic, the randomness of the entire episode accumulates in the return.
Monte Carlo finds a value by averaging these fluctuating returns over many trials. It uses an unbiased actual return, but an individual update can be noisy.
TD, introduced next, uses only one reward and the next state's value instead of an entire episode. It observes less of the actual future, but reduces variance and can update immediately.
The Steps of Monte Carlo Prediction
In words, the algorithm proceeds as follows.
- Initialize and the visit counts .
- Follow policy to generate one complete episode.
- Starting from the terminal state, calculate the return at every time step.
- Select visits according to either the first-visit or every-visit rule.
- Update toward .
- Repeat over many episodes.
The important point is that step 2 does not improve the policy. We are performing policy evaluation only.
Action values can be learned in the same way. Count visits to state-action pairs instead of states and average the return from each corresponding time step.
Improving a policy to be -greedy with respect to these Q-values leads to Monte Carlo control. This series, however, covers control in detail through the TD methods SARSA and Q-Learning.
What to Remember
The central idea of this article is
Monte Carlo prediction estimates the value of a policy from actual returns sampled after episodes end.
More specifically:
- A value can be learned from experience generated by a policy even when the model is unknown.
- The expectation in is approximated by the average of observed returns.
- The target is the actual return .
- First-visit uses only the first visit in an episode, while every-visit uses them all.
- Monte Carlo does not bootstrap and must wait for the episode to end.
- It uses actual returns, but those returns can have high variance.
The next article begins with the question, “must we really wait until the episode ends?” Temporal-difference learning borrows the next state's value estimate and updates immediately after every step.