Importance sampling in reinforcement learning with an estimated behavior policy

In reinforcement learning, importance sampling is a widely used method for evaluating an expectation under the distribution of data of one policy when the data has in fact been generated by a different policy. Importance sampling requires computing the likelihood ratio between the action probabilities of a target policy and those of the data-producing behavior policy. In this article, we study importance sampling where the behavior policy action probabilities are replaced by their maximum likelihood estimate of these probabilities under the observed data. We show this general technique reduces variance due to sampling error in Monte Carlo style estimators. We introduce two novel estimators that use this technique to estimate expected values that arise in the RL literature. We find that these general estimators reduce the variance of Monte Carlo sampling methods, leading to faster learning for policy gradient algorithms and more accurate off-policy policy evaluation. We also provide theoretical analysis showing that our new estimators are consistent and have asymptotically lower variance than Monte Carlo estimators.


Introduction
The field of reinforcement learning (RL) seeks to model an autonomous agent interacting with a task while learning through trial-and-error interaction. RL algorithms result in policies that tell the agent how to act in all possible world states in order to complete a particular task. Despite much recent empirical success (Mnih et al. 2015;Silver et al. 2016), many RL algorithms remain prohibitively sample inefficient-the amount of task interactions they require before a high-performing policy is found may be beyond what is possible on many real world problems found in fields such as medicine or robotics. If these RL algorithms are to be broadly applied, it is imperative to address this data inefficiency.
A fundamental problem in the reinforcement learning literature is estimating the expected value of a function under the distribution of data induced by a policy. For example, in policy gradient RL, algorithms must estimate the expected value of the policy gradient under the distribution of states and actions that the current policy induces (Sutton and Barto 1998). In batch policy evaluation (Li et al. 2015; Thomas and Brunskill 2016a), algorithms must estimate the expected return of a policy under the distribution of stateaction trajectories that induces. We call this problem the expectation evaluation problem. Data efficient solutions to this problem are an important step towards data efficient RL. In this work, we introduce methods that increase the data efficiency of expectation evaluation methods in reinforcement learning.
One widely used approach for the expectation evaluation problem is to use a sampleaverage or Monte Carlo estimate of the desired expectation. This approach is straightforward: the policy is run to sample data and then the function values under the resulting data are averaged. In the limit, as the amount of sampled data increases, the estimate probabilistically converges to the true expected value. However, for a finite amount of data, it may exhibit high variance that causes error in the estimate. Variance in a Monte Carlo estimate arises when the observed samples occur at different frequencies than they would in expectation. For example, if a policy selects between two actions with equal probability in a given state, the resulting data may show that one action occurred 60% of the time while the other action occurred only 40% of the time. With this observed data, the Monte Carlo estimate will place too much emphasis on the first action and not enough emphasis on the second. We term this source of variance sampling error and provide an illustration in Fig. 1; reducing sampling error is the main benefit of the methods we introduce.
In this work, we frame the sampling error problem as an off-policy policy evaluation problem. In the off-policy policy evaluation problem, we are interested in observing data under one policy, , but instead observe data from a different, behavior policy. We observe that though we are interested in observing data under a policy , sampling error may result in our data appearing to have been generated by a different, empirical policy, ̂ . This observation motivates correcting sampling error with the well-known off-policy technique of importance sampling (Precup et al. 2000). In this article, we propose first estimating the empirical policy from observed state-action pairs and then using this policy as the behavior policy in an importance sampling estimate. Figure 1 illustrates how this approach corrects sampling error in Monte Carlo sampling. The combination of importance sampling with an estimated behavior policy to correct sampling error is the central contribution of this work.
It may be natural to assume that importance sampling with an estimated behavior policy will perform worse than with the true behavior policy probabilities because it is using an estimate in place of the "correct" behavior policy probability. Furthermore, it may appear that importance sampling is unnecessary in the on-policy case. However, in this work, we 1 3 show that importance sampling with an estimated behavior policy lowers the variance of expectation evaluation in both on-and off-policy settings. Our work complements existing approaches in the causal inference (Rosenbaum 1987;Hirano et al. 2003) and bandit (Li et al. 2015;Narita et al. 2019) literatures that has used importance sampling with an estimated behavior policy as a variance reduction strategy. We extend this general approach to sequential decision making tasks.
We first consider expectation evaluation for expectations of the form: where is a vector or scalar-valued function of state-action pairs and d is the distribution of states that policy will encounter. This form of expected value arises in policy gradient reinforcement learning (Peters and Schaal 2008;Schulman et al. 2015) as well as average reward reinforcement learning (Puterman 2014;Schwartz 1993;Mahadevan 1996). We introduce a novel expectation evaluation estimator called the sampling error corrected (SEC) estimator that reduces sampling error in Monte Carlo estimates by importance sampling with an estimated behavior policy. We prove (under a limiting set of assumptions) that the SEC estimator has variance at most that of the Monte Carlo estimator and (under lighter assumptions) that this approach has asymptotic variance at most that of the Monte Carlo estimator. We then instantiate the SEC estimator for the problem of estimating the policy gradient when running a batch policy gradient algorithm. We introduce the sampling error corrected policy gradient estimator and present an empirical study in which our new estimator leads to faster convergence of batch policy gradient algorithms for the REINFORCE algorithm (Williams 1992) and trust-region policy optimization (Schulman et al. 2015) compared to the these algorithms using the Monte Carlo estimator. Each action a is sampled with probability (a|s) and is observed in the proportion given by ̂(a|s) . Monte Carlo weighting gives each action the weight ̂(a|s) while our novel sampling error corrected (SEC) weighting gives each action the weight ̂(a|s) (a|s) (a|s) = (a|s) . In other words, the SEC estimator weights each action by the expected frequency for each a in s while the Monte Carlo estimator will have error unless the empirical frequency of sampled actions, ̂ , is equal to the expected frequency, for all actions We next consider expectation evaluation when the target expectation takes the form: where is a vector or scalar-valued function of trajectories, H, generated by following . This form of expected value arises in the problem of policy evaluation where we wish to estimate the expected return when running a particular policy (Jiang and Li 2016;Thomas and Brunskill 2016a). When expectations take this form, it is not always straightforward to recast the expectation as an expectation under state-action pairs, e.g., in finitehorizon off-policy evaluation. Thus our new SEC estimator is inapplicable. We show that sampling error can be viewed as an off-policy expectation evaluation problem where the behavior policy is a non-Markovian policy that conditions its action selection on the entire history of past states and actions. We introduce a family of regression importance sampling (RIS) estimators that estimate a possibly non-Markovian policy as the behavior policy for importance sampling. Under similar assumptions to those made for the SEC estimator, we prove that all RIS estimators are consistent and have asymptotic variance at most that of the Monte Carlo estimator. Finally, we instantiate RIS methods for the problem of off-policy batch policy evaluation and present an empirical study showing that regression importance sampling leads to lower mean squared error off-policy policy evaluation than standard importance sampling baselines.
This article proceeds as follows. In Sect. 2, we introduce necessary background: reinforcement learning notation, two common forms of expectation evaluation in RL, the on-and off-policy Monte Carlo estimator, and the concept of sampling error in the Monte Carlo estimator. In Sect. 3 we introduce the SEC estimator that uses importance sampling with an estimated behavior policy to correct sampling error in stateaction expectations and establish theoretical properties of this novel estimator. Then, in Sect. 4, we apply the SEC estimator to estimating the policy gradient in a batch policy gradient algorithm and empirically show faster convergence rates on several RL tasks. In Sect. 5 we turn to trajectory expectations and introduce a family of regression importance sampling estimators that use importance sampling with an estimated behavior policy to reduce sampling error. We provide theoretical analysis of this family of estimators, establishing consistency and asymptotic variance analysis. Then, in Sect. 6, we apply RIS estimators to the problem of off-policy policy evaluation and show our new estimators yield lower mean squared error estimates than off-policy Monte Carlo methods. In Sect. 7, we discuss prior literature on importance sampling with an estimated behavior policy, addressing sampling error, and reducing variance in reinforcement learning. Finally, we discuss the strengths and limitations of our new methods and results, discuss avenues for future research, and conclude.

Background
In this section we first introduce the notation used throughout this work. We then discuss the expectation evaluation problem in the reinforcement learning literature. Finally, we discuss Monte Carlo sampling as a solution method for expectation evaluation problems.

Notation
We assume the environment is an episodic Markov decision process with state set S , action set A , transition function, P ∶ S × A × S → [0, 1] , reward function r ∶ S × A → ℝ , discount factor , and initial state distribution d 0 (Puterman 2014). For simplicity, we assume that S and A are finite, though all methods and theoretical results discussed in this paper are applicable to both finite and infinite S and A , unless otherwise noted. We assume that the transition and reward functions are unknown. A policy, ∶ S × A → [0, 1] , is a function mapping states and actions to probabilities. We use (a|s) ∶= (s, a) to denote the conditional probability of action a given state s and P(s � |s, a) ∶= P(s, a, s � ) to denote the conditional probability of state s ′ given state s and action a. The agent interacts with the environment MDP as follows: The agent begins in initial state S 0 ∼ d 0 . At discrete time-step t the agents takes action A t ∼ (⋅|S t ) . The environment responds with R t ∶= r(S t , A t ) and S t+1 ∼ P(⋅|S t , A t ) according to the reward function and transition function. After interacting with the environment for at most l steps the agent returns to a new initial state and the process repeats. For notational convenience, we assume that all interactions last for at most l steps. In the MDP definition, we also include a terminal state, s ∞ , that allows the possibility of episodes ending before time-step l . If at any time-step, t, S t = s ∞ , then for all t ′ ≥ t , S t � = s ∞ and R t � = 0.
Let h ∶= (s 0 , a 0 , r 0 , s 1 , … , s l−1 , a l−1 , r l−1 ) be a trajectory and g(h) ∶= ∑ l−1 t=0 t r t be the discounted return of h. For trajectory h, we will use h t∶t � to denote the partial trajectory, s t , a t , r t , ..., s t ′ , a t ′ , r t ′ . If t < 0 , h t∶t � denotes the beginning of the trajectory until step t ′ . Any policy induces a distribution over trajectories, Pr(H = h| ) , where H is a random variable representing a trajectory. The distribution over trajectories induces a distribution over sets of m trajectories, Pr(D = {h 1 , ...h m }| ) , where D is a random variable representing a set of trajectories. We will write H ∼ to denote sampling a trajectory by following and D ∼ to denote sampling a set of trajectories by following . We use B for the random variable representing all k state-action pairs observed in D. 1 A policy also induces a distribution over state visitation frequencies, d ∶ S → [0, 1].
We define the value of a policy, v( ) ∶= [g(H)|H ∼ ] , as the expected discounted return when sampling a trajectory with policy .

Expectation evaluation in reinforcement learning
An important problem that arises across the reinforcement learning literature is the problem of evaluating expectations of functions under the distribution of data induced by a policy. In this section we introduce this problem as the expectation evaluation problem. We describe two general forms of expected value that occur in the reinforcement learning literature and give examples of their occurrence. In the following subsection we will discuss how both forms of expected values can be approximated with Monte Carlo sampling.

State-action expectations
The first form of expected value we consider is the expectation of a function of state-action pairs under the distribution of states and actions that a policy induces.
Definition 1 (state-action expectation) Let ∶ S × A → ℝ d be any function mapping trajectories to d-dimensional vectors and let be a policy. The state-action expectation takes the form:

Example 1 Policy Gradient Learning
An example state-action expectation from the reinforcement learning literature is the policy gradient. Let be a policy parameterized by the vector . Policy gradient algorithms attempt to find that maximize v( ) with gradient ascent on v( ) with respect to .
where q (s, a) is an estimate of the sum of rewards following action a in state s. Taking (s, a) ∶= q (s, a) log (a|s) , we obtain a state-action expectation form.

Trajectory expectations
The second form of expectation we consider is an expectation of a function under the distribution of trajectories the policy will generate.
Definition 2 (trajectory expectation) Let H be the set of all possible trajectories, let ∶ H → ℝ d be any function mapping trajectories to d dimensional vectors and let be a policy. The trajectory expectation takes the form: Example 2 Policy Evaluation An example from the reinforcement learning literature where evaluating a trajectory expectation is necessary is the problem of batch policy evaluation (Thomas and Brunskill 2016a;Jiang and Li 2016). In this problem, we are given a fixed, evaluation policy, e , and tasked with estimating v( e ) . Taking (h) ∶= g(h) , we obtain a trajectory expectation.
in dynamic programming (Bellman 1966), intractable. As an alternative to analytic computation, one of the most straightforward and widely used methods for evaluating expectations in reinforcement learning is the sample average or Monte Carlo approach.
Given a set, B, of k state-action pairs, collected by repeatedly sampling S ∼ d and A ∼ (⋅|S) , the Monte Carlo estimate for a state-action expectation is: Similarly, given a set, D, of m trajectories collected by repeatedly sampling H ∼ , the Monte Carlo approximation for a trajectory expectation is: These Monte Carlo estimators are on-policy approaches to expectation evaluation; they must use data collected from to evaluate an expected value under distributions induced by . We can generalize the Monte Carlo estimator to use data collected from a different behavior policy, b , by importance sampling. We call the off-policy Monte Carlo estimator the ordinary importance sampling (OIS) estimator. The OIS estimate for a state-action expectation is: The OIS estimate for a trajectory expectation is: Note that d is typically unknown and so (6) is not directly computable while the OIS estimate for a trajectory expectation is computable. Thus, when we consider state-action expectation evaluation, we will only consider the on-policy case. A recent line of work has explored estimation of the ratio Gelada and Bellemare 2019;Hallak and Mannor 2017); this work offers one path towards extending our consideration of stateaction expectations to the off-policy setting. When we consider trajectory expectation evaluation, we will also consider the more general off-policy case.
We make the following standard assumptions on the behavior policy.
Assumption 2 (Strong Ignorability) There are no hidden confounders that influence the choice of actions other than the current observed state.
Assumption 1 is only an assumption on the data generating policy and not an assumption on the observed data. For a particular finite sample, there may be actions where (a|s) > 0 but (s, a) was never seen. .
To address a point of potential confusion, the Monte Carlo return in RL has become synonymous with using the sum of discounted rewards to approximate the return. This approach is typically contrasted with bootstrapping methods that truncate the sum of discounted rewards after a number of steps and then add an estimate of the expected reward after truncation to estimate the full return. These bootstrapping methods remain, at least partially, Monte Carlo methods. Thus, the methods we introduce later are of potential value for improving bootstrapping methods, though, we do not study this combination in this work.

Sampling error in the Monte Carlo estimator
In this section we describe how Monte Carlo estimators can have error for finite sample sizes. We present this discussion in a unified setting that captures both state-action and trajectory expectations.
Let X be a finite set, p ∶ X → [0, 1] be a probability distribution over elements of X , and define f ∶ X → ℝ . We assume p is known and f can be evaluated at any x ∈ X . Suppose that we sample a set of m samples X = {X 1 , ..., X m } . The expectation, f , of f(X) with X ∼ p is defined as: and its Monte Carlo approximation is defined as: The Monte Carlo approximation weights each f(x) by the frequency at which x occurs in the data. However, this weighting is sub-optimal in that the weights are inaccurate unless we happen to observe each x according to its true probability, p(x).
When the frequency of any element of X in X is unequal to its expected frequency under p, the Monte Carlo estimator puts either too much or too little weight on that element. We 1 3 refer to error due to some elements being either over-or under-represented in the observed data as sampling error. Figure 2 illustrates sampling error for |X| = 3. Sampling error in the Monte Carlo estimator can be viewed as a distribution shift problem; we want to observe samples weighted by p but instead they are weighted by the empirical distribution at which they occur. Let p X ∶ X → [0, 1] be the proportion of times that x occurs in X. Formally, we define p X (x) ∶= c(x) m where c(s) is the number of times that we observe x in X. We call p X the empirical distribution of X. Given these definitions, the Monte Carlo estimator can be re-written as: Notably, the sample average in (9) has been replaced with an exact expectation as in (8). However, the expectation is taken under the empirical distribution p X and not p.
The Monte Carlo estimator is an unbiased estimator of the true value of the expectation (Hammersley and Handscomb 1964, Chapter 2). That is, if we were to repeatedly sample batches of data and compute the estimate, the estimates would be correct in expectation. However, once a single batch of data has been collected, we might ask, "can we correct for the sampling error observed in this fixed sample?" In fact, (10) suggests a simple solution to correcting sampling error. If the Monte Carlo weights samples according to the empirical distribution, we need only apply importance sampling to correct from the empirical distribution, p X , to the distribution of interest, p. Previous work in the causal inference (Rosenbaum 1987;Hirano et al. 2003) and Monte Carlo integration literature (Henmi et al. 2007) has shown such an approach to be effective at improving Monte Carlo estimators. However in RL, p is unknown for both state-action expectations and trajectory expectations and thus we cannot compute the numerator of the importance weight. Thus a direct application of previous research is impossible. In the following sections we show that, as long as we know the policy, we can still use importance sampling to partially correct sampling error.

Correcting sampling error in state-action expectations
We now introduce the first contribution of this work: a new estimator for on-policy, stateaction expectations that corrects sampling error by importance sampling with an estimated behavior policy. The inspiration for this method comes from the view, presented in the previous section, that sampling error in a Monte Carlo estimate can be viewed as distribution shift-we are interested in an expectation weighting samples by their true distribution but instead have an expectation weighting samples by their empirical distribution. We call this new estimator the sampling error corrected (SEC) estimator. In this section and the following section, we only consider state-action expectations and the on-policy case; in Sect. 5 we will again consider trajectory expectations and discuss the off-policy case. (10) We assume that, in addition to the observed data B, we are given a set of policies, where each � ∈ is a Markovian policy, � ∶ S × A → [0, 1] . The SEC estimator first estimates ̂ so that ̂ is the maximum likelihood policy under the observed data: For many RL problems, (11) can be formulated as a supervised learning problem.
After estimating ̂ , the SEC estimator computes the estimate: This estimate is similar to the Monte Carlo estimate (4) except each (S i , A i ) is re-weighted by the ratio of the true likelihood (A i |S i ) to the estimated empirical likelihood ̂(A i |S i ) . Intuitively, when an action is sampled more often than its expected frequency, SEC decreases the weight on that action. When an action is sampled less often than its expected frequency, SEC increases the weight on that action. Importantly, SEC estimates ̂ with the same k samples that will be used to compute the estimate. If ̂ is estimated with a different set of samples then ̂ will contain no information for correcting sampling error in B.  (14) shows that the SEC estimator can also be written as an exact expectation but the action weighting is now under instead of B . The state weighting is still that of d B ; since d is unknown we are only able to correct sampling error due to sampling from the policy. Equation 14 demonstrates an equivalence between SEC and analytic expectation methods [e.g., all-action policy gradients ] in discrete action spaces. In the following subsection we discuss a different intuition for SEC in continuous action spaces where analytic expectation methods are more challenging to apply.
Despite the use of importance sampling, we introduce SEC as an on-policy only estimator. In the off-policy setting, importance sampling corrects from the distribution that actions were sampled from to the distribution of actions under the policy of interest. SEC uses importance sampling to correct from the empirical distribution of actions to the distribution of actions under the policy of interest. SEC could possibly be extended to the 1 3 off-policy setting by combining it with a method that estimates the state density ratio ). However, this combination is outside the scope of this article.

Correcting sampling error with continuous actions
In the previous subsection, we discussed how SEC corrects for sampling error in finite MDPs. Here, we discuss how SEC corrects for sampling error in MDPs with continuousvalued action sets. The primary purpose of this discussion is to build intuition and we limit discussion to a setting that can be easily visualized. Specifically, we consider a multiarmed bandit problem with scalar, real-valued actions. We wish to estimate the expectation of function ∶ A → ℝ under policy which we assume to have bounded support in [0, 1]: The Monte Carlo estimate of this expectation with k samples from is: Even though the Monte Carlo estimate is a sum over a finite number of samples, we show it is exactly equal to an integral over a particular piece-wise function. We assume (w.l.o.g) that the A i 's are in non-decreasing order ( A 0 <= A i <= A m ). Imagine that we divide the range [0, 1] into k equal bins. We now define piece-wise constant function ̃M    . b, c ̃⋆ and the piece-wise ̃M C and ̃S EC approximations to ̃⋆ after 10 and 200 samples respectively. SEC counts the frequency that action fall into the bins a ≤ 0.5 or a > 0.5 to form its empirical estimate of Figure 3a gives a visualization of an example ̃⋆ using on-policy Monte Carlo sampling from an example and linear . In contrast to the true ̃⋆ , the Monte Carlo approximation to ̃ , ̃M C stretches ranges of according to the number of samples in that range: ranges with many samples are stretched and ranges without many samples are contracted. As the sample size grows, any range of will be stretched in proportion to the probability of getting a sample in that range. For example, if the probability of drawing a sample from [a, b] is 0.5 then ̃⋆ stretches on [a, b] to cover half the range [0, 1]. Figure 3 visualizes ̃M C the Monte Carlo approximation to ̃⋆ for sample sizes of 10 and 200.
In this analysis, sampling error corresponds to over-stretching or under-stretching in any given range. The limitation of Monte Carlo sampling can then be expressed as follows: given , we know the correct amount of stretching for any range and yet the Monte Carlo estimator ignores this information and stretches based on the empirical proportion of samples in a particular range. On the other hand, SEC first divides by the empirical probability density function (pdf) (approximately undoing the stretching from sampling) and then multiplies by the true pdf to more correctly stretch . Figure 3 also visualizes the ̃S EC approximation to ̃⋆ for sample sizes of 10 and 200. In this figure, we can see that ̃S EC is a closer approximation to ̃⋆ than ̃M C for both sample sizes. In both instances, the squared error of the SEC estimate is less than that of the Monte Carlo estimate.
Since may be unknown until sampled, we will still have non-zero error. However the Monte Carlo estimate has error due to both sampling error and unknown values. SEC has error only due to the unknown values for actions that remain unsampled.

Theoretical analysis
In this section we establish theoretical properties of the SEC estimator. Since SEC is a biased estimator of ̄ , the most important properties to establish are consistency and lower variance compared to the Monte Carlo estimator. In the following subsections we establish consistency and asymptotically lower variance under a set of general assumptions. Under a set of stronger assumptions, we show that the variance of the SEC estimator will always be at most that of the Monte Carlo estimator. To the best of our knowledge, the only prior theoretical work on importance sampling with an estimated behavior policy for state-action expectations is the variance and bias results of Dudík et al. (2011) for contextual bandits. However, this prior work made the assumption that ̂ is estimated independently of the data used to compute the estimate of ̄ and is thus inapplicable to SEC.

Consistency
We prove that the SEC estimator is a consistent estimator of ̄ under the following assumption: This assumption is fairly easy to satisfy assuming that the true policy, , is included in and the log likelihood and estimated log likelihood satisfy smoothness assumptions with respect to . We discuss these mild assumptions further in "Appendix 1" when we provide a full proof of Proposition 1.

Proposition 1 Under Assumption 3, the SEC estimator is a consistent estimator of ̄:
Proof See "Appendix 1".

Asymptotic variance
Consistency is an important property as it establishes the asymptotic correctness of an estimator. We next establish an ordering between the variances of the SEC and Monte Carlo estimators. In this section, we show that the asymptotic variance of the SEC estimator is at most that of the Monte Carlo estimator when and ̂ both belong to the same parametric family. This result is a corollary to an existing result in the Monte Carlo integration literature (Henmi et al. 2007) and is shown under the following assumptions: Assumption 4 The policy set, is a set of policies parameterized by a vector and all policies ∈ are twice differentiable with respect to .
Assumption 5 Policy is in the parameterized set of policies considered by SEC. ∃̃ such that ̃ ∈ and ̃ = b .
These assumptions cover widely used choices of policy approximation such as neural networks and linear functions. Under these assumptions, we prove Corollary 1:

Variance
Corollary 1 is derived under a set of mild assumptions. With more restrictive assumptions we can compare the variance of the two estimators in the non-asymptotic case. This analysis is done under the following assumptions:

Assumption 6
The action space is discrete and if a state is observed then all actions have also been observed in that state.
Assumption 7 For all observed states, the estimated policy ̂ is equal to B , i.e., if action a occurs k times in state s and s occurs n times in B then ̂(a|s) = k n .
These more restrictive assumptions are only made for the proof of Proposition 2.

Proposition 2 Let Var(EST) denote the variance of estimator EST . Under Assumptions 6
and 7, for the Monte Carlo estimator, MC , and the SEC estimator, SEC: Proof The full proof is provided in "Appendix 4".

Empirical study: state-action expectations
We have introduced the SEC estimator as a general estimator for state-action expectations in reinforcement learning. In order to empirically evaluate the SEC estimator, we apply the general estimator to the problem of estimating the policy gradient for use in a policy gradient algorithm. Specifically, we focus on batch policy gradient algorithms that repeatedly collect a batch of on-policy trajectories, estimate the policy gradient, update the policy, and then discard previously collected data to collect more trajectories for the next update. We show that variants of trust-region policy optimization (TRPO) (Schulman et al. 2015) and REINFORCE (Williams 1992) that use the SEC estimator converge faster than their counterparts that use the Monte Carlo estimator. Recall from Sect. 2.2.1 that in policy gradient reinforcement learning, a parameterized policy is updated with stochastic gradient ascent, using the gradient of its expected return: The SEC estimator for the right-hand side of (17) is given as: where q is an estimate of q . In Algorithm 1 we provide pseudocode for a generic batch policy gradient algorithm using the SEC estimator. Having instantiated the SEC estimator for batch policy gradient learning, we now conduct an empirical study comparing the SEC policy gradient estimator to the Monte Carlo policy gradient estimator. Our experiments are designed to answer the questions: 1. Does the SEC policy gradient estimator lead to faster convergence for batch policy gradient algorithms compared to the Monte Carlo estimator? 2. Does the SEC estimator reduce variance by correcting sampling error?

Empirical set-up: state-action expectations
In each RL task that we consider we choose a policy gradient algorithm (either REIN-FORCE or TRPO) and evaluate the number of policy update steps until convergence for a variant that uses the SEC estimator as compared to a variant that used the Monte Carlo estimator. For each task and each algorithm variant we run a series of trials where a single trial consists of a fixed number of policy updates. The policy gradient algorithms considered require an estimate of q (s, a) for any s, a that are observed in B. We use the sum of discounted rewards following action a in state s as an estimate of q (s, a) . We also use a state-dependent baseline, v (s) , as is common in the policy gradient literature (Greensmith et al. 2004;Schulman et al. 2016;Williams 1992). We next describe four reinforcement learning tasks, the empirical set-up for each task, and the motivation for evaluating SEC in these domains. Figure 4 displays images of these domains.

Grid World
Our first domain is a 4 × 4 Grid World and we use REINFORCE (Williams 1992) as the underlying batch policy gradient algorithm. The agent begins in grid cell (0, 0) and trajectories terminate when it reaches (3, 3). The agent receives a reward of 100 at termination, − 10 at (1, 1) and − 1 otherwise. The agent's policy is a state-dependent softmax distribution over actions: With this representation, the policy does not generalize across states or actions.
The SEC estimator estimates the policy by counting how many times each action is taken in each state. This domain closely matches the assumptions made in our theoretical analysis. Specifically, the action set is finite and ̂ is exactly equal to B . While we do not explicitly enforce the assumption that all actions are observed in all states, the small size of the state and action space ( |S| = 16 and |A| = 4 ) makes it likely that this assumption holds.
In our implementation of REINFORCE, we normalize the gradient estimates by dividing by their magnitudes and use a step-size of 1. At each iteration, each method collects a batch of 10 trajectories with the current policy.

Tabular Mountain Car
Our second domain is a discretized version of the classic Mountain Car domain (Moore 1990;Singh and Sutton 1996), where an agent attempts to move an under-powered car up a steep hill by accelerating to the left or right or not accelerating. The original task has a state of the car's position (a continuous scalar in the range [− 1.2, 0.6] ) and velocity (a continuous scalar in the range [− 0.07, 0.07] ). Following Jiang and Li (2016), we discretize position into 6 bins and velocity into 8 bins for a total of 4292 states. We use the discretized version of the task because the large number of discrete states makes it unlikely that all actions are observed in all visited states (in violation of Assumption 6). The domain does still match the assumptions in Sect. 3.2.3 in that the action set is finite and the estimated behavior policy is exactly equal to B .
We again use REINFORCE as the batch policy gradient algorithm. The agent's policy is a state-dependent softmax distribution over the three discrete actions as is used in the Grid World domain. The SEC estimator estimates the policy using the empirical proportion of times that each action is taken in each state.
As in Grid World we normalize the gradient estimates by dividing by their magnitudes and use a step-size of 1. We run each method with batch sizes of 100, 200, 600, and 800 trajectories.

Linear dynamical system
Our third domain is a two-dimensional linear dynamical system in which we evaluate SEC when actions are real-valued vectors. The reward is the agent's distance to the origin and trajectories last for 20 time-steps. In this domain the learning agent observes horizontal and vertical position and velocity and uses a linear Gaussian policy to select continuous valued accelerations in the horizontal and vertical direction: where , w , and b are the policy parameters, . We use the OpenAI Baselines  implementation of TRPO as the underlying batch policy gradient algorithm. We set the generalized advantage estimation ) parameters ( , ) both to 1. We estimate ̂ with ordinary least squares and estimate a state-independent variance parameter. In this domain, none of our theoretical assumptions hold: the action and state sets are infinite and ̂≠ B . We include it to evaluate SEC with simple function approximation. At each iteration, we use a batch size of 1000 time-steps and set the TRPO KLdivergence constraint, = 0.01.

Cart Pole
Our final domain is the Cart Pole domain from OpenAI Gym (Brockman et al. 2016) and we again use TRPO as the underlying batch policy gradient algorithm. At each iteration, we run the current policy for 200 steps and set the KL-divergence constraint, = 0.001 . The policy representation is a two layer neural network with 32 hidden units in each layer where consists of the weights and biases of the network. The input to the policy is the position and velocity of the cart and the angle and angular velocity of the pole. The output of the network is the parameters of a softmax distribution over the two actions. Estimating ̂ is equivalent to learning a soft classifier that attempts to classify what action would take in a given state. We consider two parameterizations of : 1. Each ∈ is a neural network with the same architecture as . We learn ̂ with gradient descent, using all data in B to estimate the gradient. We refer to this method as SEC Neural Network. 2. Each ∈ is a linear function that receives the activations of the last hidden layer of as input. The dual ̂ and architecture is shown in Fig. 5. We estimate the weights of ̂ with gradient descent, using all data in B to estimate the gradient. This method is labeled SEC Linear.
Again, this domain violates all assumptions made in our theoretical analysis. We include this domain to study SEC with more complex function approximation. This setting allows us to study SEC with neural network policies but is simple enough to avoid extensive tuning of hyper-parameters.

Empirical results: state-action expectations
We now present our empirical results for estimating state-action expectations with the SEC estimator.

Main results
Results for the Linear Dynamical System (lds), and Cart Pole environment are given in Fig. 6. In both domains, we see that the SEC methods lead to a learning speed-up compared to the Monte Carlo based approaches. In the LDS domain, SEC outperforms Monte Carlo in time to convergence to optimal. In Cart Pole, both variants of SEC learn faster initially, however, Monte Carlo catches up to the neural network version of SEC. This result demonstrates that we can leverage intermediate representations of (in this case, the activations of the final hidden layer) to learn ̂ with a simpler model class. In fact, results suggest that fitting a simpler model improves performance.

Tabular Mountain Car
We also compare SEC to Monte Carlo in the Mountain Car domain. We run our experiments four times with a different batch size in each experiment. Each experiment consists of 25 trials for each algorithm. Figure 7 shows results for each of the different tested batch sizes. For each batch size, we can see that SEC improves upon the Monte Carlo approach. The relative improvement does change across batches. With the largest batch size, improvement is marginal as the large batch size means that the Monte Carlo estimate will have low variance. For the smallest batch size, improvement is again marginal-though the small batch size means Monte Carlo has higher variance, it also means that SEC may have higher bias as some actions will be unobserved in visited states. Intermediate batch sizes have the widest gap between Learning results for the Linear Dynamical System (LDS) and Cart Pole domains. The horizontal is the number of timesteps and the vertical axis is the average return of a policy. We run 25 trials of each method using different random seeds. The shaded region represents a 95% confidence interval. In both domains we see that all variants of sampling error corrected policy gradient outperform the batch Monte Carlo policy gradient in either time to optimal convergence or final performance the two methods-the batch size is small enough that Monte Carlo has high variance but that SEC has less bias.  Fig. 8a shows that SEC leads to faster convergence compared to Monte Carlo. This domain most closely matches our theoretical assumptions where we showed SEC has lower variance than Monte Carlo estimates. The lower variance translates into faster learning.

Grid World experiments
We also use the Grid World domain to perform a quantitative evaluation of sampling error. As a measure of sampling error we use the total variation distance between the current policy and the empirical frequency of actions, B . For any state s, the total variation distance between the two policies is given by: Fig. 7 Learning results for the Mountain Car domain with different batch sizes. The horizontal axis is the number of iterations (i.e., the number of times the policy has been updated). The vertical axis is average return. We run 25 trials of each method using different random seeds. The shaded region represents a 95% confidence interval. For all batch sizes we see that the sampling error corrected policy gradient outperforms the batch Monte Carlo policy gradient in either time to optimal convergence or final performance after 1000 iterations 1 3 We report the mean D value over states in B as a measure of sampling error. We choose the total variation distance as opposed to the more commonly used KL-divergence since B and may not share support. That is, there may be an action, a, where B (a|s) is 0 and (a|s) > 0. Figure 8b shows that sampling error increases and then decreases during learning. Peak sampling error correlates with where the learning curve gap between the two methods is greatest. Note that sampling error naturally decreases as learning converges because the policy becomes more deterministic. Figure 8c shows that the entropy of the current policy goes to zero, i.e., becomes more deterministic. A more deterministic policy will have less sampling error and so we expect to see less advantage from SEC as learning progresses.
We also perform a sensitivity analysis of SEC to the batch-size at each iteration. We run 10 trials each of the SEC and Monte Carlo policy gradient algorithms with batchsizes from 1 to 1000 trajectories. For each method and batch-size we compute the mean area-under-the-curve (AUC) for the average return up to iteration 20 (close to where learning converges). We then compute the relative improvement of SEC compared to Monte Carlo for each batch-size as: Figure 9a shows that the performance improvement is greatest when the batch-size is small and decreases as the batch-size grows. When the batch-size is small, the Monte Carlo policy gradient will have the highest sampling error and thus SEC has the most room for improvement. As the batch-size grows, sampling error decreases and the SEC improvement is more marginal.
Finally, we verify the importance of using the same data to both estimate ̂ and estimate the policy gradient. Figure 9b introduces two alternatives to SEC: • Independent: Estimates ̂ with a separate set of k samples and then compute the SEC estimate using this ̂.  • Random: Instead of computing importance weights, we randomly sample weights from a normal distribution and use them in place of the learned SEC weights. The normal distribution has mean one and standard deviation chosen to approximately match the range of weights seen when using the SEC estimator. Figure 8a shows that independent hurts performance compared to Monte Carlo. random performs marginally worse than Monte Carlo. This result demonstrates the need to use the same set of data to estimate ̂ and compute the SEC estimate.
To conclude our empirical study of the SEC estimator for state-action expectations, we have shown that correcting sampling error with the SEC estimator can decrease the number of policy updates needed for a batch policy gradient algorithm to converge. This empirical study focused on using SEC to lower the variance of policy gradient estimates compared to a Monte Carlo estimator. However, SEC is a general estimator for any reinforcement learning problem that requires estimating a state-action expectation and is thus potentially applicable to other problems, for example, policy evaluation in average reward reinforcement learning. Unfortunately, not all expectations in reinforcement learning can be easily written as state-action expectations. In the next section, we describe how to correct sampling error when estimating trajectory expectations.

Correcting sampling error in trajectory expectations
In this section we introduce the second contribution of this article: a family of estimators called regression importance sampling (RIS) estimators that correct for sampling error in the set of observed trajectories, D, by importance sampling with an estimated behavior policy. In contrast to SEC that corrects sampling error when estimating state-action expectations with on-policy data, RIS estimators correct sampling error for estimating trajectory Fig. 9 Sampling error corrected policy gradient ablations in the Grid World Domain. a The percent improvement of SEC compared to Monte Carlo for varying batch sizes. For each batch size, we compute area under the average return curve (AUC) for each method during the first 20 learning iterations. We compute the mean AUC over 10 trials and report the percent improvement of the SEC mean over Monte Carlo. b Average return for two alternative weight corrections. Results are averaged over 25 trials and confidence bars are for a 95% confidence interval expectations with either on-policy or off-policy data. Since we consider both the on-and off-policy cases, we will discuss the RIS estimator relative to the ordinary importance sampling (OIS) estimator that generalizes the Monte Carlo estimator to the off-policy setting (see Sect. 2).
As with SEC, we assume that, in addition to D, we are given a set of policies. Unlike SEC, we assume this set, n , (possibly) contains non-Markovian policies: each ∈ n is a distribution over actions conditioned on the immediate preceding state and the last n states and actions preceding that state: ∶ S n+1 × A n → [0, 1] . The RIS(n) estimator first estimates the maximum likelihood behavior policy in n under D: When n = 0 , RIS and SEC return the same ̂ . The RIS(n) estimate is then an OIS estimate with ̂( n) replacing b .
We refer to as the RIS(n) weight for action A t , state S t , and trajectory segment H t−n∶t . Though RIS(0) and SEC would return the same ̂ , RIS(0) corrects sampling error along the entire trajectory since it uses the product of importance weights.
We have introduced RIS as a family of estimators where different RIS methods estimate the empirical behavior policy conditioned on different history lengths. Among these estimators, our primary method of study is RIS(0) . For larger n, RIS(n) may be less reliable for small sample sizes as the ̂( n) estimate will be highly peaked (it will be 1 for most observed actions.) We verify this claim empirically below. However, as we discuss in Sect. 6.2.2, larger n may produce asymptotically more accurate sampling error corrections and thus asymptotically more accurate estimates.

Correcting sampling error in discrete action spaces
We now present an example illustrating how RIS corrects for sampling error when used to estimate trajectory expectations. Our goal in this section is to build intuition and we make several limiting assumptions to facilitate presentation. These assumptions are removed for our more formal theoretical and empirical analysis and should not be understood as limitations of RIS methods. We make the following assumptions: 1. S and A are finite sets. 2. The distributions d 0 and P are deterministic, that is, d 0 (s) = 1 for only one s ∈ S and for all s, a, P(s � |s, a) = 1 for only one s � ∈ S. 2. Let H be the (finite) set of possible trajectories under behavior policy, b . We assume that our observed data, D, contains at least one of each h ∈ H.
We define c(h i∶j ) as the number of times that trajectory segment h i∶j appears during any trajectory in D. Similarly, we define c(h i∶j , a) as the number of times that action a is observed following trajectory segment h i∶j during any trajectory in D. RIS(n) estimates the empirical behavior policy as: 1 3 Observe that both OIS and all variants of RIS can be written in one of two forms: where w � (h) = ∏ l−1 t=0 � (a t �s t ) and for OIS, � ∶= b and for RIS(n) , � ∶=̂( n) as defined in Eq. (19).
If we had sampled trajectories using ̂( l−1) instead of b , in a deterministic environment, the probability of each trajectory, h, would be Pr(H = h|H ∼̂( l−1) ) = c(h) m . Thus Form (ii) can be written as: To emphasize what we have shown so far: OIS and RIS are both sample-average estimators whose estimates can be written as exact expectations. However, this exact expectation is under the distribution that trajectories were observed and not the distribution of trajectories under b . Furthermore, the distribution that trajectories were observed is the trajectory distribution of a non-Markovian behavior policy.
Consider choosing w � ∶= w (l−1) Choosing w ′ to be ŵ(n) for n < l − 1 also does not result in [ (H)|H ∼ ] being returned in this example. This observation is surprising because even though we know that the true Pr(H = h� b ) = ∏ l−1 t=0 b (a t �s t ) , it does not follow that the estimated probability of a trajectory is equal to the product of the estimated Markovian action probabilities, i.e., that With a finite number of samples, the data may have higher likelihood under a non-Markovian behavior policy-possibly even a policy that conditions on all past states and actions. Thus, to fully correct for sampling error, we must importance sample with an estimated non-Markovian behavior policy. However, ŵ(n) with n < l − 1 still provides a better sampling error correction than w b since any ̂( n) will reflect the realized statistics of D while b only reflects the expected statistics. This statement is supported by our empirical results comparing RIS(0) to OIS and a theoretical result we present in the following section that states that, for all n, RIS(n) has lower asymptotic variance than the Monte Carlo estimator.
Before concluding this section, we discuss two limitations of the presented examplethese limitations are not present in our theoretical or empirical results. First, the example lacks stochasticity in the rewards and transitions. In stochastic environments, sampling error arises from sampling states, actions, and rewards while in deterministic environments, sampling error only arises from sampling actions. Like SEC, RIS is only able to correct for stochasticity in the action selection since d 0 and P are unknown. Second, we assumed that D contains at least one of each trajectory possible under b . If a trajectory is absent from D then RIS(l − 1) has non-zero bias. Theoretical analysis of this bias for both RIS(l − 1) and other RIS variants is an open question for future analysis.

Theoretical analysis
In this section we present theoretical properties of RIS estimators. Like SEC, we prove consistency and asymptotically lower variance than the Monte Carlo estimator. To the best of our knowledge, the only prior theoretical work on importance sampling with an estimated behavior policy for estimating trajectory expectations is the work of Farajtabar et al. (2018). This prior work makes the assumption that ̂ is estimated with different data than the data used for the estimate and thus the analysis is inapplicable to RIS estimators.

Consistency
Following a similar proof to that of Proposition 1, we show that all RIS estimators are consistent estimators of ̄ . Like Proposition 1, we require the assumption of consistent estimation of the behavior policy.

Proposition 3 Under Assumption 3, ∀n , RIS(n) is a consistent estimator of
Proof See "Appendix 1" for a full proof.

Asymptotic variance
We also show that all RIS estimators have lower asymptotic variance compared to the OIS estimator or Monte Carlo estimator. The proof also requires Assumptions 4 and 5 to hold for the set of policies, n , and behavior policy, b .

Corollary 2 Under Assumptions 4 and 5, ∀n,
where Var denotes the asymptotic variance.
Proof See "Appendix 3" for a full proof.

Empirical study: trajectory expectations
In the previous section, we introduced the RIS estimator as a general estimator for trajectory expectations in reinforcement learning. In order to empirically evaluate RIS, we apply the general estimator to the problem of batch policy evaluation. We show that using RIS and specifically the RIS(0) method leads to lower mean squared error policy evaluation than OIS in both the on-and off-policy case. We also show that RIS weights can be used in conjunction with other variants of importance sampling to obtain even lower mean squared error policy evaluation. Recall from Sect. 2.2.2 that in the batch policy evaluation problem, we seek to estimate v( e ) for some evaluation policy, e . We will assume we are given a batch of trajectories, D, that was collected by running some behavior policy, b . Our objective is to use a policy evaluation method, PE , that estimates v( e ) with low mean squared error: Our primary baseline is the OIS estimator, though, we also consider extensions of OIS such as weighted importance sampling (Precup et al. 2000) and doubly robust estimators (Jiang and Li 2016;Thomas and Brunskill 2016a). Our experiments are designed to answer the following questions: 1. What is the empirical effect of replacing OIS weights, e (a|s) b (a|s) , with RIS weights, e (a|s) (a|s) , in policy evaluation for sequential decision making tasks? 2. How important is using D to both estimate the behavior policy and compute the importance sampling estimate? 3. How does the choice of n affect the MSE of RIS(n)?
With non-linear function approximation, our results suggest that the common supervised learning approach of model selection using hold-out validation loss may be suboptimal for the RIS estimator. Thus, we also investigate the question: 4. Does minimizing hold-out validation loss set yield the minimal MSE regression importance sampling estimator when estimating ̂ with gradient descent and neural network function approximation?

Empirical set-up: trajectory expectations
We run policy evaluation experiments in several domains. We provide a short description of each domain here and the motivation for evaluating RIS methods in these domains.

Grid World
This domain is the same 4 × 4 Grid World used in Sect. 4 and has been used in prior off-policy policy evaluation work (Thomas 2015;Thomas and Brunskill 2016a). This domain allows us to study RIS separately from questions of function approximation as the small number of states and actions permits RIS to use count-based estimation of b . Our first set of experiments uses a behavior policy, b , that can reach the high reward terminal state and an evaluation policy, e , that is the same policy with lower entropy action selection. The second set of experiments uses the same behavior policy as both behavior and evaluation policy.

Single Path
See Fig. 10 for a description. This domain is small enough to make implementations of RIS(l − 1) and the REG method from Li et al. (2015) tractable. We include the REG baseline since it can be shown to be equivalent to any RIS estimator in the contextless bandit setting; see "Appendix 5" for more discussion. All RIS methods use count-based estimation of b . In each state, b selects action, a 0 , with probability p = 0.6 and e selects action, a 0 , with probability 1 − p = 0.4 . Action a 0 causes a deterministic transition to the next state. Action a 1 causes a transition to the next state with probability 0.5, otherwise, the agent remains in its current state. The agent receives a reward of 1 for action a 0 and 0 otherwise. The REG baseline is given access to the environment's state transition function, P, which it needs to compute its estimate.

Linear dynamical system
This domain is the same LDS domain used in Sect. 4. We make one change which is that policies are linear in a second order polynomial transform of the state features instead of being linear in the state features. The intention of this change is to make the true behavior policy be a non-linear function of state features but still allow us to estimate ̂ with ordinary least squares. We obtain a basic policy by optimizing the parameters of a policy for 10 iterations of the Cross-Entropy optimization method (Rubinstein and Kroese 2013). The basic policy maps the state to the mean of a Gaussian distribution over actions. The evaluation policy and true behavior policy both use the same basic policy to provide the mean but the evaluation policy uses a standard deviation of 0.5 and b uses a standard deviation of 0.6.

Simulated robotics
We also use two continuous control tasks from the OpenAI gym: Hopper and HalfCheetah. 3 In each task, we use neural network policies with 2 layers of 64 tanh hidden units each for e and b . Each policy maps the state to the mean of a Gaussian distribution with state-independent standard deviation. We obtain e and b by running the OpenAI Baselines ) implementation of proximal policy optimization (PPO) (Schulman et al. 2017) and then selecting two policies along the learning curve. For both environments, we use the policy after 30 updates for e and after 20 updates for b . These policies Fig. 10 The Single Path MDP. This environment has 5 states, 2 actions, and l = 5 . The agent begins in state 0 and both actions either take the agent from state n to state n + 1 or cause the agent to remain in state n. Not shown: If the agent takes action a 1 it remains in its current state with probability 0.5 use tanh activations on their hidden units since these are the default in the OpenAI Baselines PPO implementation. RIS represents the behavior policy as a Gaussian distribution over possible actions with the mean given by a neural network function of the state and a state-independent standard deviation. RIS estimates the behavior policy with gradient descent on the negative log-likelihood of the actions with respect to the policy parameters.
In all our experiments we use the Adam optimizer (Kingma and Ba 2015) with a learning rate of 1 × 10 −3 . The neural network behavior policies learned by RIS have either 0, 1, 2, or 3 hidden layers with 64 hidden units with relu activations. In all domains we run repeated trials of each experiment. Except for the simulated robotics domains, a trial consists of evaluating the squared error of different estimators over an increasing data set. The average squared error over multiple trials is an unbiased estimate of the mean squared error of each method. In the simulated robotics domain, a trial consists of collecting a single batch of 400 trajectories and evaluating the squared error of different estimators on this batch.

Empirical results: trajectory expectations
We now present our empirical results. Except where specified otherwise, RIS refers to RIS(0).

Grid World Policy evaluation
Our first experiment compares several importance sampling variants implemented with both RIS weights and OIS weights in the Grid World domain. Specifically, we use the basic IS estimator, the weighted IS estimator (Precup et al. 2000), per-decision IS, the doubly robust (Jiang and Li 2016), and the weighted doubly robust estimator (Thomas and Brunskill 2016a). Figure 11a shows the MSE of the evaluated methods averaged over 100 trials. The results show that, for this domain, using RIS weights lowers MSE for all tested IS variants relative to OIS weights.
We also evaluate alternative data sources for estimating ̂ in order to establish the importance of using D to both estimate ̂ and compute the estimate. Specifically, we consider: 1. Independent estimate In addition to D, this method has access to an additional set, D . The behavior policy is estimated with D and the policy value estimate is computed with D. Since state-action pairs in D may be absent from D we use Laplace smoothing (i.e., we add 1 to the count for each (s, a) pair (Manning et al. 2008)) to ensure that the importance weights never have a zero in the denominator. 2. Extra-data estimate This baseline is the same as Independent Estimate except it uses both D and D to estimate b . Only D is used to compute the policy value estimate. Figure 11b shows that these alternative data sources for estimating b decrease accuracy compared to RIS and OIS. Independent Estimate has high MSE when the sample size is small but its MSE approaches that of OIS as the sample size grows. We understand this result as showing that this baseline cannot correct for sampling error in the offpolicy data since the behavior policy estimate is unrelated to the data used in computing the value estimate. Extra-data Estimate initially has high MSE but its MSE decreases faster than that of OIS. Since this baseline estimates b with data that includes D, it can partially correct for sampling error-though the extra data harms its ability to do so. Only estimating ̂ with D and D alone lowers MSE over OIS for all sample sizes. We also repeat these experiments for the on-policy setting and present results in Fig. 11c, d. We observe similar trends as in the off-policy experiments suggesting that RIS can lower variance in Monte Carlo sampling methods even when OIS weights are otherwise unnecessary.
In both the on-and off-policy setting, we measure the empirical decomposition of the MSE for RIS into its bias and variance components. In both settings we see that variance is the primary contributor to the MSE. In the on-policy setting, we find that RIS initially has a higher bias but this bias decreases to a negligible amount with a small number of trajectories.

RIS(n)
In the Grid World domain it is difficult to observe the performance of RIS(n) for various n because of the long horizon: smaller n perform similarly and larger n scale poorly with l . To see the effects of different n more clearly, we use the Single Path domain. Figure 12 gives the mean squared error for OIS, RIS, and the REG estimator of Li et al.

3
(2015) that has full access to the environment's transition probabilities. For RIS, we use n = 0, 3, 4 and each method is run for 200 trials. Figure 12 shows that higher values of n and REG tend to give inaccurate estimates when the sample size is small. However, as data increases, these methods give increasingly accurate value estimates. In particular, REG and RIS(4) produce estimates with MSE more than 20 orders of magnitude below that of RIS(3) (Fig. 12 is cut off at the bottom for clarity of the rest of the results). REG eventually passes the performance of RIS(4) since its knowledge of the transition probabilities allows it to eliminate sampling error in both the actions and the environment. In the low-to-medium data regime, only RIS(0) outperforms OIS. However, as data increases, the MSE of all RIS methods and REG decreases faster than that of OIS. We provide an additional, informal analysis of the observed similarities between RIS and REG in "Appendix 5".

RIS with linear function approximation
Our next set of experiments consider continuous state and action spaces in the Linear Dynamical System domain. RIS represents ̂ as a Gaussian policy with mean given as a linear function of the state features. Similar to in Grid World, we compare three variants of IS, each implemented with RIS and OIS weights: the ordinary IS estimator, weighted IS (WIS), and per-decison IS (PDIS). Each method is averaged over 200 trials and results are shown in Fig. 13a.
We see that RIS weights lower the MSE of both IS and PDIS, while both WIS variants have similar MSE. This result suggests that the MSE reduction from using RIS weights depends, at least partially, on the variant of IS being used.
Similar to Grid World, we also consider estimating ̂ with either an independent dataset or with extra data and see a similar ordering of methods. Independent Estimate gives high variance estimates for small sample sizes but then approaches OIS as the sample size grows. Extra-Data Estimate corrects for some sampling error and has lower MSE than OIS. RIS lowers MSE compared to all baselines.

Fig. 12
Off-policy policy evaluation in the Single Path MDP for various n. The horizontal axis is the number of trajectories in D and the vertical axis is MSE. Both axes are log-scaled. The curves for REG and RIS(4) have been cut-off to more clearly show all methods. These methods converge to an MSE value of approximately 1 × 10 −31

RIS with neural network function approximation
Our remaining experiments use the Hopper and HalfCheetah domains with neural network function approximation. A practical concern for RIS estimators (and also SEC) is how to avoid over-fitting when using powerful function approximation to estimate the empirical policy. RIS uses all of the available data to both estimate ̂ and compute the off-policy estimate of [ (H)|H ∼ e ] . Unfortunately, the RIS estimate may suffer from high variance if the function approximator is too expressive and ̂ is overfit to our data. Additionally, if the functional form of the true behavior policy, b , is unknown, it may be unclear what is the right function approximation representation for ̂ . A practical solution is to use a validation set-distinct from D-to select an appropriate policy class and appropriate regularization criteria for RIS. This solution is a small departure from the previous definition of RIS as selecting ̂ to maximize the log likelihood on D and only D. Rather, we select ̂ to maximize the log likelihood on D while avoiding over-fitting. This approach represents a trade-off between robust empirical performance and a potentially stronger sample correction by further maximizing log likelihood on the data used for computing the RIS estimate. Figure 14 compares the MSE of RIS for different neural network architectures. Our main point of comparison is RIS using the architecture that achieves the lowest validation error during training (the darker bars in Fig. 14). Under this comparison, the MSE of RIS with a two-hidden-layer network is lower than that of OIS in both Hopper and HalfCheetah, though, in HalfCheetah, the difference is statistically insignificant. We also observe that the policy class with the best validation error does not always give the lowest MSE (e.g., in Hopper, the two hidden layer network gives the lowest validation loss but the network with a single layer of hidden units has ≈ 25 % less MSE than the two hidden layer network). This last observation motivates our final experiment.

RIS model selection
Our final experiment aims to better understand how hold-out validation error relates to the MSE of the RIS estimator when using gradient descent to estimate neural network approximations of ̂ . This experiment duplicates our previous experiment, except every 25 steps of gradient descent we stop optimizing ̂ and compute the RIS estimate with the current ̂ and its MSE. We also compute the training and hold-out validation negative log-likelihood. Plotting these values gives a picture of how the MSE of RIS changes as our estimate of ̂ changes. Figure 15 shows these plots for the Hopper and HalfCheetah domains.
We see that the policy with minimal MSE and the policy that minimizes validation loss are misaligned. If training is stopped when the validation loss is minimized, the MSE of RIS is lower than that of OIS (the intersection of the RIS curve and the vertical dashed line in Fig. 15. However, the ̂ that minimizes the validation loss curve is not identical to the ̂ that minimizes MSE. To understand this result, we also plot the mean RIS estimate throughout behavior policy learning (bottom of Fig. 15). We can see that at the beginning of training, RIS tends to over-estimate v( e ) because the probabilities given by ̂ to the observed data will be small (and thus the RIS weights are large). As the likelihood of D under ̂ increases (negative log likelihood decreases), the RIS weights become smaller and the estimates tend to underestimate v( e ) . The implication of these observations, for RIS, is that during behavior policy estimation the RIS estimate will likely have zero MSE at some point. Thus, there may be an early stopping criterion-besides minimal validation loss-that would lead to lower mse with RIS, however, to date we have not found one. Note that OIS also tends to underestimate policy value in MDPs as has been previously analyzed by Doroudi et al. (2017).

Related work
In this section we survey literature related to importance sampling with an estimated behavior policy, alternatives to Monte Carlo sampling in reinforcement learning, and variance reduction for Monte Carlo sampling.

Importance sampling with an estimated behavior policy
A number of research works have shown that estimating the denominator of importance weights (instead of using the true probabilities) lowers the variance of importance sampling. To the best of our knowledge, all such prior work has been done in the multi-armed bandit, contextual bandit, or causal inference communities. One can directly extend these methods to state-action expectations by estimating d (s) (s) or to trajectory expectations by estimating Pr(h| ) . Unfortunately, such methods are often impractical as they require knowing d (s) or Pr(h| ) for the numerator of the importance weights. Concurrent to this work, Pavse et al. (2020) built upon our prior work  and showed that a SEC-like method could lower error in batch value function approximation.
Our work takes inspiration from Li et al. (2015) who prove, for contextless bandits, that importance sampling with an estimated behavior policy has lower minimax mean squared error than using the true behavior policy. They corroborate these theoretical findings with experiments showing that the mean squared error of the so-called REG estimator decreases faster than that of importance sampling with the true behavior policy. The main distinction between this work and the work of Li et al. (2015) is that we consider MDPs where actions affect both reward and the next state. Our theoretical results only address the asymptotic sample size while Li et al. (2015) provide variance and bias results for finite samples of any size.
For contextual bandits, Narita et al. (2019) prove that importance sampling with an estimated behavior policy minimizes asymptotic variance among all asymptotically normal estimators (including ordinary importance sampling). They also provide a large-scale study of policy evaluation with the empirical behavior policy on an adplacement task. Xie et al. (2018) provide similar results and prove a reduction in finitesample mean squared error when using an estimated behavior policy. Again, our work differs from these two works in that we are concerned with full MDPs.
It has long been known in the causal inference literature that the empirical behavior policy produces lower variance estimates than using the true behavior policy for importance sampling. In this literature, the behavior policy action probabilities are known as propensities and importance sampling is known as inverse propensity scoring (Austin 2011). Rosenbaum (1987) first showed that using parametric propensity estimates lowered the variance of importance sampling. In later work, Hirano et al. (2003) studied this approach using non-parametric propensity score estimates. The causal inference problems studied can be viewed as a class of contextual bandit problems. Under that view, our work differs from these earlier studies in that we are concerned with MDPs.
Importance sampling is commonly defined as a way to use samples from a proposal distribution to estimate an expectation under a target distribution. Henmi et al. (2007) proved that importance sampling with a maximum likelihood parametric estimate of the proposal distribution has lower asymptotic variance than using the true proposal distribution. This result forms the basis of our own proofs that show SEC and all RIS methods have lower asymptotic variance than Monte Carlo estimates. Delyon and Portier (2016) proved asymptotic lower variance for using a non-parametric estimate of the proposal distribution.
Other works have explored directly estimating the importance weights instead of first estimating the proposal distribution (i.e., behavior policy) to compute the importance weights (Oates et al. 2017;Liu and Lee 2017). These "blackbox" importance sampling approaches show superior convergence rates compared to ordinary importance sampling. In recent years a number of methods have been proposed that attempt to weight (s, a) pairs with blackbox weights when estimating state-action expectations for policy evaluation Mousavi et al. 2020;Yang et al. 2020). The stated focus of most of these works tends to be on reducing variance due to long horizons; an interesting question is whether some of the success of these methods is due to correcting sampling error.
In contextual bandit problems, Dudík et al. (2011) present theoretical results showing that an estimated behavior policy may increase the variance of importance sampling while also introducing bias. Farajtabar et al. (2018) prove similar results for full MDPs. However, in these works the behavior policy is estimated with a separate set of data than the set used for computing the off-policy value estimate. Because the behavior policy is estimated with a separate set of data it has no power to correct sampling error in the data used for the off-policy value estimate. In fact, these theoretical findings are in line with our experiments showing that it is important to use the same set of data both to estimate the behavior policy and to compute the regression importance sampling estimate (see Figs. 11e, f, 13b in Sect. 6). Raghu et al. (2018) report that larger differences between the true behavior policy and estimated behavior policy lead to more error in the off-policy value estimate. However, they measure off-policy policy evaluation error with respect to the true behavior policy weighted importance sampling estimate and so it is unsurprising that as the policies become more different the error increases.

Analytic expectations
In this work we use importance sampling with an estimated behavior policy to correct sampling error in reinforcement learning. Here, we discuss alternative approaches in the reinforcement learning literature that avoid sampling error altogether.
The SARSA algorithm (Rummery and Niranjan 1994) uses (S, A, R, S � , A � ) tuples to learn an estimate of the action-value function, q , for a policy . The algorithm requires two sampled actions for each update and the second of these is used to form a Monte Carlo estimate of the expected value of q in state S ′ . The expected SARSA update (Van Seijen et al. 2009) replaces the Monte Carlo estimate with an analytic evaluation of the expected value of q in S ′ . By replacing the Monte Carlo estimate, sampling error is eliminated and expected SARSA may converge much faster than SARSA. Expected SARSA requires either a small discrete action-set or for and q to have forms that allow analytic integration. In this work, we place no limitations on the action-set or policy and do not explicitly learn an action-value function.
Expected SARSA can be extended to a multi-step algorithm with the tree-backup algorithm (Precup et al. 2000;Sutton and Barto 1998). More recent work has shown that the amount of sampling as opposed to exact expectations can be done on a per-state basis using the Q( ) algorithm (Asis et al. 2018). Other tree-backup-like algorithms have been proposed and hold the promise to eliminate sampling error in off-policy data (Yang et al. 2018;Shi et al. 2019). Like expected SARSA, these algorithms require the ability to compute the sum of (a|s)q (s, a) over all a ∈ A.
In policy gradient reinforcement learning, Sutton et al. (2000) introduced the all-actions policy gradient algorithm that avoids sampling in the action-space by first learning the function q and then analytically computing the expectation of q (s, a) log (a|s) . This approach has been further developed as the expected policy gradient algorithm Fellows et al. 2018), the mean actor-critic algorithm (Asadi et al. 2017), and the MC-256 algorithm (Petit et al. 2019). With a good approximation of q , these algorithms learn faster than a Monte Carlo policy gradient estimator. However, requiring a good approximation of q undercuts one of the primary reasons for using policy gradient RL: it may be easier to represent a good policy than to represent the correct action-value function (Sutton and Barto 1998). The sampling error corrected policy gradient estimator provides an alternative method for reducing sampling error when q is difficult to learn. We also note that estimating (as the sampling error corrected policy gradient estimator does) may be easier than estimating q since the right function approximator class for is known while, in general, it is unknown for q .

Variance reduction in reinforcement learning
Aside from reducing sampling error, other approaches exists for lowering the variance of Monte Carlo expectation evaluations in reinforcement. Control variates use the known expected value of a second random variable to lower the variance of estimating the expected value of or . The most commonly considered type of control variate in the RL literature is the additive control variate which includes constant baselines , state dependent baselines (Greensmith et al. 2004;Schulman et al. 2016) and state-action dependent baselines (Jiang and Li 2016;Thomas and Brunskill 2016a) A second type of control variate is the multiplicative control variate of which the weighted importance sampling estimator (Precup et al. 2000) may be the best known in the RL literature. As we have shown in our empirical study, control variate techniques are complementary to the sampling error correction methods we introduce.
Adaptive importance sampling methods change the data distribution to lower the variance of the Monte Carlo estimator. The data distribution of a Monte Carlo estimator can be adapted by either changing the behavior policy or the MDP transition probabilities. Hanna et al. (2017) show that the OIS estimator can have lower variance than on-policy Monte Carlo sampling and introduce a method that adapts the behavior policy to obtain low variance estimates for the problem of off-policy batch policy evaluation. Whiteson (2017) andFrank et al. (2008) consider adaptive importance sampling through changing P. This approach is possible when learning is done in a simulator and we can both know and control P. Regardless of how the data distribution is adapted, adaptive importance sampling methods still have variance due to sampling error.
Finally, bootstrapping from a learned value function is a widely used variance reduction strategy in RL (Sutton 1984;Mnih et al. 2016;Greensmith et al. 2004). In some cases, this technique would provide complementary variance reduction to that of SEC or RIS estimators. For example, in Sect. 4, we use a learned value function as a baseline (Greensmith et al. 2004;Schulman et al. 2016) for both the SEC policy gradient estimator and the Monte Carlo policy gradient estimator. In other cases, such as online value function learning, further work may be needed to apply SEC and RIS.

Discussion of limitations
In this section we discuss the results we have presented and limitations of the SEC and RIS estimator.
Our theoretical and empirical studies have focused on the statistical properties of the SEC and RIS estimators. The gain in statistical efficiency comes at a cost of increased computational complexity. Both SEC and all RIS estimators have an additional step of estimating the empirical behavior policy compared to the Monte Carlo estimator. Furthermore, in the on-policy setting, the Monte Carlo estimator avoids computing importance ratios while SEC and RIS estimators must always compute the ratios. The trade-off between computational and statistical efficiency is a trade-off that must be made by practitioners.
Our theoretical analysis compared the asymptotic properties of our new estimators to that of the Monte Carlo estimator. This analysis proves the statistical benefit of using our new estimators when the sample size is very large. However, our empirical results show a statistical benefit to using the new estimators even for smaller sample sizes. Currently, we lack a theoretical explanation for small sample size variance reduction. We also know that SEC and RIS estimators are introducing bias but we lack theoretical analysis as to how much bias is introduced and how fast this bias goes to zero.
The SEC and RIS estimators are related to the use of importance sampling for off-policy reinforcement learning where the behavior policy is unknown and thus must be estimated before it can be used to form the importance weights. In practice, behavior policy estimation can be challenging when the distribution class of the true behavior policy is unknown (Raghu et al. 2018). However, in the settings we studied, we have complete access to the behavior policy and can specify the policy set to include (thus ensuring consistency of the SEC and RIS estimators). We can even simplify the policy set by estimating a policy that conditions on intermediate representations of the behavior policy. For example if the behavior policy, b , is a convolutional neural network mapping states to a softmax distribution over actions, we can use all but the last layer of b as a feature extractor and then model as all linear functions mapping these features to a softmax distribution over actions. Such a technique can significantly simplify estimating ̂ while maintaining consistency guarantees when the behavior policy is a complex function. Our CartPole experiment in Sect. 4 shows evidence of the benefit of this approach.

Future work
In this section, we outline directions for future work to further develop the SEC and RIS estimators for correcting sampling error in reinforcement learning. As an overarching direction, we note that this work assumed an episodic and fully observable environment. Future work should consider how to best correct sampling error in continuing or partially observable environments.

Behavior policy search for regression importance sampling
The methods introduced in this article are methods that lower variance post data collection. That is, data is collected in the same way that a Monte Carlo estimator would collect data, and only then do our new methods re-weight data to lower variance. One direction for future work would be to answer the question, "how should we collect data for the most accurate SEC or RIS estimate?" Hanna et al. (2017) introduce the idea of adapting the behavior policy to lower the variance of Monte Carlo policy evaluation. However, after collecting data, their policy value estimate remains a Monte Carlo estimate. A straightforward additional study would be to use their behavior policy gradient algorithm to learn how collect data but then use regression importance sampling to lower sampling error in the observed data.
Though straightforward, this proposed approach may be sub-optimal and we illustrate this fact by considering the bandit setting. Consider a k-armed bandit with deterministic rewards on each arm. After all k arms have been observed, the RIS estimate will have both zero bias and zero variance. 4 Thus the optimal behavior policy for RIS should increase the probability of unobserved actions; it is a non-stationary policy that depends on all of the past actions. In contrast, an optimal behavior policy for the Monte Carlo estimator would take actions in proportion to (a)r(a) (Hanna et al. 2017). Thus behavior policy search, as introduced in prior work, may yield a behavior policy that is sub-optimal for the RIS estimator.

Finite-sample analysis
In Sects. 3.2 and 5.2 we proved SEC and RIS have asymptotically variance at most that of the Monte Carlo estimator. Further theoretical analysis should examine the finite-sample bias and variance of SEC and RIS compared to the Monte Carlo estimator. A starting point for this work could be the results of Li et al. (2015) who provide bounds on these finitesample quantities in the bandit setting. Extending these results to MDPs would give us a deeper understanding of when RIS and SEC are lower error estimators than Monte Carlo. The empirical results in Sect. 6 provide strong evidence that RIS is always preferable to ois. However, theoretical analysis would strengthen this claim.
The theoretical analysis in Sect. 5.2 did not distinguish different RIS methods according to how much history they conditioned on (the estimator parameter n). Theoretical analysis of the finite-sample bias-variance trade-off and asymptotic variance for different RIS methods would deepen our understanding of how to choose n. Empirical results on the Singlepath domain (Fig. 12) suggest that small n have lower small-sample MSE while large n have asymptotically lower MSE. Verifying this finding formally is an interesting direction for future work.

Value function learning
Finally, we have only considered estimating scalar or vector-valued expectations that arise in the RL literature. Another important problem that arises in the RL literature is how to efficiently learn the value function that gives the expected return of a policy from any state. Many value function learning algorithms rely on leveraging intermediate value estimates to avoid variance due to sampling many consecutive actions (Sutton 1984). However, these methods still tend to require some amount of action sampling and thus have some amount of sampling error to be corrected. Pavse et al. (2020) have shown that correcting sampling error with a method like SEC or the RIS estimators leads to lower value function error compared to standard temporal difference learning when learning from a fixed batch of data. Future work should consider whether a similar advantage can be shown in online value function learning where the learning agent processes a single transition tuple ( s, a, r, s ′ ) at a time.

Regression importance sampling for high confidence off-policy evaluation
Empirical results in Sect. 6 showed that regression importance sampling leads to lower mean squared error off-policy evaluation. It remains to be seen if RIS also leads to tighter confidence intervals for high confidence off-policy evaluation. One way to tackle this problem would be to simply use RIS with a bootstrap confidence interval as done by Thomas et al. (2015) and Hanna et al. (2017). Given that RIS has been empirically shown to have lower variance than ordinary importance sampling, we could expect such a method to produce tighter confidence intervals.
A more challenging direction for future work would be to obtain true confidence intervals with an estimated behavior policy. While the data efficiency of bootstrapping is desirable, it only provides approximate confidence bounds. In order to determine exact confidence intervals for RIS, we would need to develop concentration inequalities for RIS in the same way that one can use Hoeffding's inequality to establish confidence intervals for ois. One possible direction is to explore use of the Dvoretzky-Kiefer-Wolfowitz inequality which bounds how far the empirical distribution of samples is from the true distribution (Dvoretzky et al. 1956). Regardless of the exact approach, exact confidence bounds for importance sampling with an estimated behavior policy would be of great value to providing provable guarantees of safety in real world settings where the true behavior policy is unknown.

3 10 Conclusion
This article introduces and describes a general method for reducing the variance of Monte Carlo estimation in reinforcement learning: estimate the empirical action probabilities, ̂(a|s) , from observed data and then use importance sampling with the ratio (a|s) (a|s) . This general approach lowers variance by correcting sampling error-error due to stochasticity in the agent's action selection. Following this general approach, we first introduce the sampling error corrected (SEC) estimator and present theoretical analysis showing that the SEC estimator has asymptotic variance at most that of the Monte Carlo estimator. We use the SEC estimator to lower the variance of policy gradient estimates in two batch policy gradient algorithms and demonstrate this approach leads to more data efficient RL compared to a Monte Carlo approach.
We next introduce a family of regression importance sampling (RIS) estimators for settings where the desired expectation to estimate is written as a distribution over trajectories. Like the SEC estimator, RIS estimators first estimate the behavior policy before importance sampling. Unlike the SEC estimator, the family of RIS estimators contains methods that estimate non-Markovian behavior policies before importance sampling and corrects for sampling error due to action selection along the entire trajectory. We show that all RIS estimators have asymptotic variance at most that of the Monte Carlo estimator. We further apply RIS to the problem of off-policy policy evaluation and show that RIS estimators lead to lower mean squared error policy value estimates than Monte Carlo importance sampling variants.

Appendix 1: Consistency proof
In this appendix we show that, assuming we use a consistent estimator of the behavior policy, the SEC estimator and RIS estimators are consistent estimators of ̄ and ̄ respectively.

Proposition 1 Under Assumption 3, the SEC estimator is a consistent estimator of ̄:
Proof We have assumed that as the amount of data increases, the behavior policy estimated by SEC will almost surely converge to the true behavior policy: We will denote elements of H n as h n and random variables that take values from H n as H n . Let d b ,H n ∶ H n → [0, 1] be the distribution over elements of H n induced by running b . Previously, we defined the behavior policy, b , to be a function mapping state-action pairs to probabilities. We re-define b ∶ H n × A → [0, 1] , i.e., a policy that conditions the distribution over actions on the preceding length n trajectory segment. These definitions are equivalent provided for any h n,i = (s i , a i , ...s i+n−1 ) and h n,j = (s j , a j , ...s j+n−1 ) , if s i+n−1 = s j+n−1 then ∀a b (a|h n,i ) = b (a|h n,j ).
Let (Ω, F, ) be a probability space and D m ∶ Ω → D be a random variable. D m ( ) is a sample of m trajectories with ∈ Ω . Let d b be the distribution of length n trajectory segments under b . Define the expected log-likelihood: and its sample estimate from samples in D m ( ): Note that: and Define the KL-divergence ( D ) between b and D after segment h n as: Assuming for all h n and a the variance of log (a|h n ) is bounded, L ( |D m ( )) is a consistent estimator of L( ) . We make this assumption explicit:

Assumption 8 (Consistent Estimation of Log likelihood). For all
This assumption will hold when the support of b is a subset of the support of for all ∈ , i.e., no ∈ places zero probability measure on an action that b might take. We can ensure this assumption is satisfied by only considering ∈ that place non-zero probability on any action that b has taken.
We also make an additional assumption about the piece-wise continuity of the log-likelihood, L , and the estimate of the log-likelihood, L . First we present two necessary definitions as given by Thomas and Brunskill (2016b) Definition 4 ( -covering As pointed out by Thomas and Brunskill, this assumption holds for the most commonly considered policy classes but is also general enough to hold for other settings (see Thomas and Brunskill 2016b for further discussion of Assumption 9 and the related definitions).

Appendix 3: Asymptotic variance of RIS and SEC
In this section we prove that the SEC estimator and, ∀n , RIS(n) has asymptotic variance at most that of the Monte Carlo estimator. These results are corollaries of Theorem 1 in Henmi et al. (2007) that holds for general Monte Carlo integration. Consider estimating v = [f (X)|X ∼ p] for probability mass function p and real-valued function f with domain X . Note that while we define distributions as probability mass functions, this result can be applied to continuous-valued state and action spaces by replacing probability mass functions with density functions. Given parameterized and twice differentiable probability mass function q(⋅|̃ ) , the Monte Carlo estimator of where ̂ is the maximum likelihood estimate of ̃ given samples from q(⋅|̃ ) . The following theorem relates the asymptotic variance of v to that of ṽ.

Theorem 1
where Var denotes the asymptotic variance.
Theorem 1 shows that an importance sampling estimate using the maximum likelihood estimate of the sampling distribution parameters yields an asymptotically lower variance estimate than using the true parameters, ̃ . To specialize this theorem to our setting, we show that the maximum likelihood behavior policy parameters are also the maximum likelihood parameters for the state-action distribution (for SEC) and the trajectory distribution (for RIS methods). We first need to specify the parameterized class of the sampling distribution. For SEC, the sampling distribution is Pr(S = s, A = a; ) = d (s) (a|s) . Note that the state distribution d is not parameterized by -only the policy, . This parameterization means that changing leaves the distribution of states unchanged and is justified because we are only concerned with weighting already sampled data and not with collecting additional data. For RIS(n), the sampling distribution is Pr(H = h; ) = p(h)w (h) where p(h) ∶= d 0 (s 0 ) ∏ l−1 t=1 P(s t �s t−1 , a t−1 ) and w (h) = ∏ l−1 t=0 (a t �s t−n , a t−n , … , s t ). We next present two lemmas that show that maximum likelihood estimation of the behavior policy is equivalent to maximum likelihood estimation of the specified sampling distributions. For SEC, we give the following lemma: log Pr(S k , A k ; )

Proof
And for all RIS(n):

Proof
Combining each of these lemmas in turn with Theorem 1 allows us to prove Corollaries 1 and 2 respectively. Proof Define X ∶= S × A , f (x) ∶= (s, a) , p(x) ∶= Pr(s, a| ) and q(s, a| ) ∶= Pr(s, a| ) . Lemma 2 implies that: is the maximum likelihood estimate of ̃ (where ̃ = and Pr(s, a|̃ ) is the probability of (s, a) under ) and then Corollary 1 follows directly from Theorem 1.

Corollary 2 Under Assumptions 4 and 5,∀n,
where Var denotes the asymptotic variance.
Proof Define f (x) = g(h) , p(h) = Pr(h| e ) and q(h| ) = Pr(h| ) . Lemma 3 implies that: Var (RIS(n)( , D)) ≤ Var (OIS( , D, b )) Proof Claim 2 also follows from the same logic as Claim 1. The cancellation of the B (a|s) factors converts the inner summation over actions into an exact expectation under . Since the Monte Carlo estimator is an unbiased estimator, the inner summation over actions must be equal to the exact expectation under in expectation. Thus the expectation of both estimators conditioned on X is: This proves Claim 2.
We can now prove Proposition 2. Proof Using the law of total variance, the variance of the general estimator given by (31) can be decomposed as: The first term, Σ U , is the variance due to stochasticity in the action selection. From Claim 1, we know that for SEC this term is zero while in general it is not zero for MC. 5 The second term, Σ X , is the variance due to only visiting a finite number of states before computing the estimate. Claim 2 shows that this term is equal for both SEC and MC . Thus the variance of SEC is at most that of MC.
In multi-armed bandit problems (MDPs with a single state and length one horizon), REG is identical to SEC and RIS(0) with f being either the function or respectively.
To apply REG to state-action expectations, one first estimates the mean value over (s, a) pairs as D and then computes the estimate: This estimate requires knowledge of d and is thus inapplicable to general RL tasks. To apply REG to trajectory expectations, one first estimates the mean value for each observed trajectory as D (H) and then computes the estimate: This estimate requires knowledge of d 0 and P and is thus also inapplicable to general RL tasks.
We now elucidate a relationship between RIS(l − 1) and REG even though they are different estimators. Let c(h) denote the number of times that trajectory h appears in D. We can rewrite REG as an importance sampling method: The denominator in (35) can be re-written as a telescoping product to obtain an estimator that is similar to RIS(l − 1): This expression differs from RIS(l − 1) in two ways: Pr ( 1. The numerator includes the initial state distribution and transition probabilities of the environment. 2. The denominator includes count-based estimates of the initial state distribution and transition probabilities of the environment where the transition probabilities are conditioned on all past states and actions. If we assume that the empirical estimates of the environment probabilities in the denominator are equal to the true environment probabilities then these factors cancel and we obtain the RIS(l − 1) estimate. This assumption will almost always be false except in deterministic environments. However, showing that RIS(l − 1) is approximating REG suggests that RIS(l − 1) may have similar theoretical properties to those derived for REG by Li et al. (2015). Our SinglePath experiment (See Fig. 10 in Sect. 6) supports this conjecture: RIS(l − 1) has high bias in the low to medium sample size but have asymptotically lower MSE compared to other methods. REG has even higher bias in the low to medium sample size range but has asymptotically lower MSE compared to RIS(l − 1) . RIS with smaller n appear to decrease the initial bias but have larger MSE as the sample size grows. The asymptotic benefit of RIS for all n is also corroborated by Corollary 2 in "Appendix 3" though Corollary 2 does not tell us anything about how different RIS methods compare. The asymptotic benefit of REG compared to RIS methods can be understood as REG correcting for sampling error in both the action selection and state transitions. Similar conclusions can be drawn for a comparison between SEC and REG.