Prediction, control, importance sampling, blackjack, and racetrack experiments.
Chapter 5 estimates value functions and improves policies from complete sampled episodes, without requiring an environment model.
Exercise 5.1
Why the big jump in the last two rows at the rear (player sums 20–21)?
At sums of 20 and 21, the policy is to stick. The bust risk is gone, and we are already very close to 21, so the probability of winning is very high.
Before 20, the policy is to hit. Therefore, the bust risk is high, and the probability of winning is low.
Why does the far-left column of that back row drop (dealer showing Ace)?
When the dealer shows an Ace, the probability of the dealer's sum reaching 21 is a bit higher than if the dealer shows some other card. Therefore, our probability of winning is a bit lower.
Why are the frontmost values higher in the upper plots than the lower?
With a usable ace, we have more flexibility in moving to a strong hand since it can be used as a 1 or an 11. The expected value is therefore uniformly higher in the frontmost rows of the upper plots than the lower plots.
Exercise 5.2
In blackjack, a given state (defined by player sum, dealer's card, usable ace flag) usually appears at most once per episode. This is because upon each action, the player's sum either increases or the episode ends.
Because there are almost no repeated state visits within the same episode, first-visit and every-visit MC give the same results.
Exercise 5.3
For MC prediction of qπ, the backup is a full-episode backup: from the state-action pair (st,at), you follow the policy π all the way to termination, then backup the total return Gt to (st,at) (no bootstrapping).
Exercise 5.4
Instead of keeping all returns, we can keep
N(s,a): a count of how many times action a was taken in state s
Q(s,a): the running average of returns
Then, when we see a new return Gt, we update incrementally:
Q(s,a)←Q(s,a)+N(s,a)1[Gt−Q(s,a)]
Exercise 5.5
The first-visit MC return is 10, whereas the every-visit MC return is the average of the returns from each visit:
vEV(s)=101+⋯+10=5.5
Exercise 5.6
Firstly, we only include the time steps where action a was taken in state s. Secondly, since the first action is restricted, we start our ratios from t+1 instead of t.
Weighted importance sampling is biased but the bias vanishes asymptotically as the number of episodes grows.
Early on, the denominator (sum of weights) is small and unstable, which can cause the ratio to fluctuate and push the estimate away from the true value. This explains the initial increase in mean square error (MSE). As more data accumulates, the denominator stabilizes, the bias shrinks, and the error decreases.
Ordinary importance sampling, on the other hand, is unbiased at every sample size but has high variance. Its MSE starts high due to this variance but decreases steadily with more episodes.
Exercise 5.8
For every-visit MC, the second moment of the importance sampling ratio is:
This is because in off-policy control, the target policy π is kept deterministic and greedy.
If At=π(St) then π(At∣St)=0 and we stop since the importance-sampling ratio beyond this point will be 0.
Otherwise, π(At∣St)=1 and so the importance-sampling ratio will be
b(At∣St)π(At∣St)=b(At∣St)1
Exercise 5.12
To solve this problem, I used an Off-policy Monte Carlo agent with an epsilon-greedy behavior policy and an initial value of -500 for each state. Additionally, I used exploring starts for the first half of the episodes to ensure that the agent explores the entire state space.
The value function for the first track is:
Learned value function for the first track.
Sample trajectories for the first track:
The value function for the second track is:
Learned value function for the second track.
Sample trajectories for the second track:
Exercise 5.13
The expectation in ρt:T−1Rt+1 can be written as:
We know that Rt+1 is the reward for taking action At in state St and following policy π from that point onwards. This means that the reward Rt+1 and the actions taken after t+1 are independent.