Ordinary off-policy Monte Carlo scales a whole return by the full importance-sampling ratio , even though, with , most of the return is decided early. Discounting-aware importance sampling treats discounting as partial termination and scales each flat partial return only by the ratio of the actions that produced it.
With flat partial returns and ratios , each visit to state (in an episode ending at ) contributes
and the same expression with every replaced by 1 to a denominator term . The ordinary estimator (5.9) is and the weighted estimator (5.10) is .
Write discounting_aware_is(episodes, pi, b, gamma, state, first_visit=True):
episodes is a list of episodes, each a list of (S_k, A_k, R_{k+1}) tuples for .pi and b are arrays of shape (n_states, n_actions) with the target and behaviour policies' probabilities.state is visited: only the first visit in each episode when first_visit is true, every visit otherwise.Return (ordinary, weighted) as floats. Return 0.0 for an estimator whose denominator is zero (no visits, or ).