Estimating a new policy from logged actions

← Back to Blog & Articles

Listing 3.6a: the bandit world, the two policies, the Monte Carlo ground truth and the logger (library code).

Now change the question. A contextual bandit observes a context XX, chooses an action A∈{1,…,K}A\in\{1,\dots,K\} and receives a reward RR. A policy π(a∣x)\pi(a\mid x) is a conditional distribution over actions. The value of a policy is V(π)=𝔼[R(A)]V(\pi)=\mathbb{E}\big[R(A)\big] with A∼π(⋅∣X)A\sim\pi(\cdot\mid X), where R(a)R(a) is the potential reward of action aa. Write q(x,a)=𝔼[R∣X=x,A=a]q(x,a)=\mathbb{E}[R\mid X=x,A=a] for the mean reward.

We have a log of nn i.i.d. triples (Xi,Ai,Ri)(X_i,A_i,R_i) produced by a behaviour policy μ\mu, so that Ai∼μ(⋅∣Xi)A_i\sim\mu(\cdot\mid X_i). We want V(π)V(\pi) for a different target policy π\pi. This is the treatment-effect problem with KK treatments, where μ\mu plays the role of the propensity score, and it comes with the same three assumptions. Consistency and ignorability hold by design when the log contains the full context that μ\mu used (the action depends on the potential rewards only through XX). Positivity becomes support: μ(a∣x)>0\mu(a\mid x)>0 whenever π(a∣x)>0\pi(a\mid x)>0. Here μ\mu is known, because we logged it, which is a major advantage over observational causal inference.

Four estimators are standard. With the importance weight wi=π(Ai∣Xi)/μ(Ai∣Xi)w_i=\pi(A_i\mid X_i)/\mu(A_i\mid X_i) and a reward model q̂\hat q:

  • Direct method (DM): V̂DM=1n∑i∑aπ(a∣Xi)q̂(Xi,a)\hat V_{\mathrm{DM}}=\frac1n\sum_i\sum_a\pi(a\mid X_i)\,\hat q(X_i,a). It is outcome regression, and it is as good as q̂\hat q.
  • Inverse-propensity scoring (IPS): V̂IPS=1n∑iwiRi\hat V_{\mathrm{IPS}}=\frac1n\sum_i w_iR_i.
  • Self-normalized IPS (SNIPS; Swaminathan and Joachims, 2015): V̂SN=∑iwiRi/∑iwi\hat V_{\mathrm{SN}}=\sum_i w_iR_i\big/\sum_i w_i.
  • Doubly robust (DR; Dudik, Langford and Li, 2011): V̂DR=1n∑i(∑aπ(a∣Xi)q̂(Xi,a)+wi(Ri−q̂(Xi,Ai)))\hat V_{\mathrm{DR}}=\frac1n\sum_i\Big(\sum_a\pi(a\mid X_i)\hat q(X_i,a)+w_i\big(R_i-\hat q(X_i,A_i)\big)\Big).

Theorem 3.5 (IPS is unbiased). If μ(a∣x)>0\mu(a\mid x)>0 whenever π(a∣x)>0\pi(a\mid x)>0, then 𝔼[wR]=V(π)\mathbb{E}[w\,R]=V(\pi). Moreover 𝔼[w]=1\mathbb{E}[w]=1, and Var(wR)=𝔼[∑aπ(a∣X)2μ(a∣X)(q(X,a)2+σ2(X,a))]−V(π)2,\mathrm{Var}(w\,R)=\mathbb{E}\Big[\sum_a\frac{\pi(a\mid X)^2}{\mu(a\mid X)}\big(q(X,a)^2+\sigma^2(X,a)\big)\Big]-V(\pi)^2, where σ2(x,a)=Var(R∣X=x,A=a)\sigma^2(x,a)=\mathrm{Var}(R\mid X=x,A=a).

Proof. Condition on X=xX=x and sum over the logged action: 𝔼[wR∣X=x]=∑aμ(a∣x)π(a∣x)μ(a∣x)q(x,a)=∑aπ(a∣x)q(x,a),\mathbb{E}[wR\mid X=x]=\sum_a\mu(a\mid x)\,\frac{\pi(a\mid x)}{\mu(a\mid x)}\,q(x,a)=\sum_a\pi(a\mid x)\,q(x,a), where the sum runs over actions with μ(a∣x)>0\mu(a\mid x)>0; by the support condition this loses no action with π(a∣x)>0\pi(a\mid x)>0. Taking the expectation over XX gives V(π)V(\pi). The same computation with RR replaced by 1 gives 𝔼[w]=1\mathbb{E}[w]=1. For the second moment, 𝔼[w2R2∣X=x]=∑aμ(π/μ)2(q2+σ2)=∑a(π2/μ)(q2+σ2)\mathbb{E}[w^2R^2\mid X=x]=\sum_a\mu\,(\pi/\mu)^2\,(q^2+\sigma^2)=\sum_a(\pi^2/\mu)(q^2+\sigma^2), and subtracting V(π)2V(\pi)^2 gives the variance. ◼\blacksquare

The variance contains π2/μ\pi^2/\mu: where the target puts probability on actions that the logging policy rarely chooses, the weights are large and the variance grows. The condition 𝔼[w]=1\mathbb{E}[w]=1 also explains SNIPS: the normalizer 1n∑iwi\frac1n\sum_iw_i is a random variable with mean 1, and dividing by it removes the fluctuation of the weight sum from the estimate. It also makes the estimate equivariant to reward shifts (adding cc to every reward adds exactly cc to SNIPS, whereas IPS adds c⋅1n∑iwic\cdot\frac1n\sum_iw_i), and it keeps the estimate inside the range of observed rewards. SNIPS is a ratio of two sample means, hence consistent by the law of large numbers, with a bias of order 1/n1/n.

Theorem 3.6 (doubly robust OPE: unbiasedness and variance). Let q̂\hat q be fixed or fitted on data independent of the log, and let Δ(x,a)=q(x,a)−q̂(x,a)\Delta(x,a)=q(x,a)-\hat q(x,a). Under the support condition, the DR term ψ=∑aπ(a∣X)q̂(X,a)+w(R−q̂(X,A))\psi=\sum_a\pi(a\mid X)\hat q(X,a)+w\,\big(R-\hat q(X,A)\big) has 𝔼[ψ]=V(π)\mathbb{E}[\psi]=V(\pi) for every q̂\hat q, and Var(ψ)=Var(v(X))+𝔼[Var(wΔ(X,A)∣X)]+𝔼[w2σ2(X,A)],\mathrm{Var}(\psi)=\mathrm{Var}\big(v(X)\big)+\mathbb{E}\big[\mathrm{Var}(w\,\Delta(X,A)\mid X)\big]+\mathbb{E}\big[w^2\sigma^2(X,A)\big], where v(x)=∑aπ(a∣x)q(x,a)v(x)=\sum_a\pi(a\mid x)q(x,a). IPS is the case q̂=0\hat q=0, for which Δ=q\Delta=q. Hence DR has smaller variance than IPS exactly when 𝔼[Var(wΔ∣X)]≤𝔼[Var(wq∣X)]\mathbb{E}[\mathrm{Var}(w\Delta\mid X)]\le\mathbb{E}[\mathrm{Var}(wq\mid X)]. If q̂=q\hat q=q the middle term vanishes and the variance is the smallest value of this form.

Proof. Write R=q(X,A)+εR=q(X,A)+\varepsilon with 𝔼[ε∣X,A]=0\mathbb{E}[\varepsilon\mid X,A]=0 and Var(ε∣X,A)=σ2(X,A)\mathrm{Var}(\varepsilon\mid X,A)=\sigma^2(X,A). Then ψ=∑aπq̂+wΔ+wε\psi=\sum_a\pi\hat q+w\Delta+w\varepsilon. Given XX, the first term is fixed. For the second, as in Theorem 3.5, 𝔼[wΔ∣X]=∑aπ(a∣X)Δ(X,a)\mathbb{E}[w\Delta\mid X]=\sum_a\pi(a\mid X)\Delta(X,a), so 𝔼[ψ∣X]=∑aπq̂+∑aπΔ=∑aπq=v(X)\mathbb{E}[\psi\mid X]=\sum_a\pi\hat q+\sum_a\pi\Delta=\sum_a\pi q=v(X), and 𝔼[ψ]=V(π)\mathbb{E}[\psi]=V(\pi). For the variance use the law of total variance. The conditional mean is v(X)v(X), giving Var(v(X))\mathrm{Var}(v(X)). The conditional variance is Var(wΔ∣X)+𝔼[w2σ2∣X]\mathrm{Var}(w\Delta\mid X)+\mathbb{E}[w^2\sigma^2\mid X], because ε\varepsilon has conditional mean zero given (X,A)(X,A), hence is uncorrelated with any function of (X,A)(X,A), including wΔw\Delta. Taking expectations gives the formula. Setting q̂=0\hat q=0 gives IPS. The comparison and the case q̂=q\hat q=q follow from the formula and Var≥0\mathrm{Var}\ge0. ◼\blacksquare

Three consequences. First, DR is unbiased even if q̂\hat q is arbitrarily wrong, so long as the known logging propensity is used. Second, its variance is not automatically lower than IPS: it is lower exactly when the reward model’s errors, weighted by ww, vary less across actions than the rewards themselves. A good model helps a great deal, a bad one can hurt. Third, if one uses an estimated μ̂\hat\mu in the weights, the same calculation as in Theorem 3.3 gives a bias of 𝔼[∑aπ(a∣X)(μ(a∣X)/μ̂(a∣X)−1)Δ(X,a)]\mathbb{E}\big[\sum_a\pi(a\mid X)\big(\mu(a\mid X)/\hat\mu(a\mid X)-1\big)\Delta(X,a)\big], a product of the propensity error and the reward-model error. That is double robustness for OPE.

The experiment uses a fixed world with K=6K=6 actions and three context features. The mean reward is q(x,a)=sin⁡(x⊤wa)+baq(x,a)=\sin(x^\top w_a)+b_a and the noise is N(0,1)N(0,1). The logging policy is a softmax of a random linear score, unrelated to the rewards. The target is π(a∣x)∝exp⁡(3q(x,a))\pi(a\mid x)\propto\exp\big(3\,q(x,a)\big), a good policy that concentrates on the best actions. The true value of the target is computed by Monte Carlo with 10610^6 contexts from the exact mean rewards, so its error is negligible for our purposes. We use two reward models fitted by ridge regression per action on the log: a misspecified linear model [1,x][1,x], and a correctly specified model [1,sin⁡(x⊤wa)][1,\sin(x^\top w_a)] whose form matches the truth. The second is a best case that no real analysis can guarantee.

ch03_listings.py builds one world at module level (WORLD = make_world(0)) and a replication helper:

The target policy is worth 1.1099 and the logging policy 0.2539, so the evaluation is not a small perturbation: the target policy is a very different, much better policy than the one that produced the data. The weights have mean 1.000 as Theorem 3.5 requires, but a maximum of 487.7 in a log of 100000 samples and an effective-sample-size fraction of only 0.131.

The rows show each estimator’s character.

  • DM with the misspecified model is stable (standard deviation 0.0623) and wrong (bias −0.2164-0.2164, RMSE 0.2251). Its error does not shrink with more data. DM with the correct form is the best estimator in the table (RMSE 0.0550), because here the parametric form is right and the model has few parameters.
  • IPS is unbiased (bias −0.0061-0.0061 is within noise) but has standard deviation 0.1782, three times that of the correct DM.
  • SNIPS has standard deviation 0.0828 and bias −0.0045-0.0045, and an RMSE of 0.0828, less than half that of IPS. In this experiment the normalization costs almost no bias and removes most of the variance.
  • DR with the correct form has bias 0.0007 and standard deviation 0.0888, half that of IPS, and it would be unbiased with any other model, since the weights are exact. DR with the misspecified linear model is also unbiased (bias −0.0021-0.0021) but its standard deviation (0.1965) is larger than that of IPS (0.1782). This is the case Theorem 3.6 warns about: wΔw\,\Delta varies more than wqw\,q does. A likely cause is that the linear model is worst on the rarely logged actions that the target favours, which are the samples with the largest weights, but we did not isolate this.

In summary, the four estimators trade bias and variance, and none dominates. DM is best with a good model and unsafe otherwise. IPS and DR are protected against model error but pay for it in variance. DR is the right default when you can fit a reasonable reward model, and an unbiased estimate with a large standard deviation is not a good estimate. Notice that the DR with a bad model does not underperform IPS by much (0.1965 against 0.1782 RMSE), whereas DM with a bad model is off by 0.2164, which no amount of data repairs.

Continue with the book

Explore the full treatment, worked examples, and exercises in Applied AI Under Uncertainty.


Companion article for Applied AI Under Uncertainty, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top