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 , chooses an action and receives a reward . A policy is a conditional distribution over actions. The value of a policy is with , where is the potential reward of action . Write for the mean reward.
We have a log of i.i.d. triples produced by a behaviour policy , so that . We want for a different target policy . This is the treatment-effect problem with treatments, where 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 used (the action depends on the potential rewards only through ). Positivity becomes support: whenever . Here is known, because we logged it, which is a major advantage over observational causal inference.
Four estimators are standard. With the importance weight and a reward model :
- Direct method (DM): . It is outcome regression, and it is as good as .
- Inverse-propensity scoring (IPS): .
- Self-normalized IPS (SNIPS; Swaminathan and Joachims, 2015): .
- Doubly robust (DR; Dudik, Langford and Li, 2011): .
Theorem 3.5 (IPS is unbiased). If whenever , then . Moreover , and where .
Proof. Condition on and sum over the logged action: where the sum runs over actions with ; by the support condition this loses no action with . Taking the expectation over gives . The same computation with replaced by 1 gives . For the second moment, , and subtracting gives the variance.
The variance contains : where the target puts probability on actions that the logging policy rarely chooses, the weights are large and the variance grows. The condition also explains SNIPS: the normalizer 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 to every reward adds exactly to SNIPS, whereas IPS adds ), 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 .
Theorem 3.6 (doubly robust OPE: unbiasedness and variance). Let be fixed or fitted on data independent of the log, and let . Under the support condition, the DR term has for every , and where . IPS is the case , for which . Hence DR has smaller variance than IPS exactly when . If the middle term vanishes and the variance is the smallest value of this form.
Proof. Write with and . Then . Given , the first term is fixed. For the second, as in Theorem 3.5, , so , and . For the variance use the law of total variance. The conditional mean is , giving . The conditional variance is , because has conditional mean zero given , hence is uncorrelated with any function of , including . Taking expectations gives the formula. Setting gives IPS. The comparison and the case follow from the formula and .
Three consequences. First, DR is unbiased even if 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 , 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 in the weights, the same calculation as in Theorem 3.3 gives a bias of , a product of the propensity error and the reward-model error. That is double robustness for OPE.
The experiment uses a fixed world with actions and three context features. The mean reward is and the noise is . The logging policy is a softmax of a random linear score, unrelated to the rewards. The target is , a good policy that concentrates on the best actions. The true value of the target is computed by Monte Carlo with 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 , and a correctly specified model 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 , 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 is within noise) but has standard deviation 0.1782, three times that of the correct DM.
- SNIPS has standard deviation 0.0828 and bias , 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 ) but its standard deviation (0.1965) is larger than that of IPS (0.1782). This is the case Theorem 3.6 warns about: varies more than 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.
