Derive Importance Sampling and Doubly Robust Off-Policy Evaluation
You have a log of episodes from one policy, a candidate policy you have never deployed, and a single number that claims to be the candidate's value. The…

Key topics
You have a log of episodes from one policy, a candidate policy you have never deployed, and a single number that claims to be the candidate's value. The number came from an estimator. The estimator did not run the candidate policy. It reweighted what it saw.
That reweighting is the whole game, and it is only as trustworthy as the overlap between the two policies. This article derives the trajectory importance weight, builds the doubly robust correction on top of it, and then marks the boundary where the derivation stops protecting you.
Setup: Behavior Policy, Target Policy, and What Is Actually Observed
Fix the data-generating process before touching any formula.
The behavior policy generated the logged trajectories. Every action in your dataset was sampled from . The target policy is the candidate whose value you want. It never touched the environment. That asymmetry is the entire problem: you have samples from one distribution and a question about another.
Assume a finite horizon , a discounted return with factor , and that you know 's action probabilities (or can recover them from the logging system). Define the per-step ratio
and the cumulative ratio .
One precondition does all the heavy lifting later: coverage. Wherever , we need . If the target policy would take an action the behavior policy never takes, the ratio is undefined — you are dividing by zero, and no estimator repairs that. If merely makes that action rare, the ratio is finite but enormous. Zero overlap breaks the math; near-zero overlap breaks the estimate.
If expected values and density rewriting feel shaky, the prerequisite math path covers that ground. Here we assume you accept that an expectation over one distribution can be rewritten by multiplying and dividing by another density.
Why the Trajectory Likelihood Ratio Is a Product of Per-Step Ratios
Write the probability of a trajectory under a policy. It factors into an initial-state term, a transition term, and an action term:
Now divide the target probability by the behavior probability:
The initial-state density cancels. The transition density cancels — it does not depend on the policy. What survives is the action probabilities:
The product form is not a rule to memorize. It is what remains after the policy-independent terms cancel. Each step where the target policy was more likely to take the observed action multiplies the weight up. Each step where it was less likely multiplies it down. Over a long horizon, those multiplications compound.
The trajectory-wise estimator applies the full weight to the whole return:
The step-wise (per-decision) variant applies the prefix weight to each reward separately:
Both have the same expectation. The step-wise form has lower variance because a reward collected early is not multiplied by ratios from steps that had not happened yet. That is the version worth defaulting to.
Knowledge check
Check your understanding
Answer this question before you continue.
Unbiasedness Under Coverage, and Where the Guarantee Stops
The unbiasedness argument is short. Take the expectation of the weighted return under :
The weight converts one measure into the other. That is the entire proof, and it holds only when the ratio is finite almost surely under — which is exactly the coverage condition. The proof does not merely prefer coverage. It requires it.
Now the part the proof does not promise. The weight is a product of ratios. Even if each ratio is modest, the product's variance can grow exponentially in the horizon. A weight of per step over twenty steps is a multiplier near . One trajectory can dominate the average.
This is where effective sample size earns its place. If a handful of trajectories carry almost all the weight, your estimate is effectively an average over a handful of samples, no matter how many episodes you logged. The number of episodes is not the number of samples that mattered.
Decision boundary: When the target and behavior policies are close and the horizon is short, plain importance sampling is defensible. When either condition fails, the estimate is dominated by noise, and you should reach for a lower-variance estimator.
Knowledge check
Check your understanding
Answer this question before you continue.
The Direct Method and the Residual It Leaves Behind
The direct method fits an approximate value function or and reads off the target policy's value with no importance weight at all:
Its bias comes from model error, and that error is invisible from the data alone. Collecting more episodes does not shrink it — you are converging confidently to the wrong answer.
Define the per-step residual as the observed return minus the model's prediction:
A good model makes residuals small and roughly centered. A bad model leaves a structured gap. That gap is the raw material the correction will reweight.
The tradeoff is now on the table: the direct method is low variance and biased; importance sampling is unbiased and high variance. The doubly robust estimator is the attempt to keep the good half of each.
Deriving the Doubly Robust Correction
The doubly robust estimator is easiest to read as an episode-level quantity. For a single logged episode, define the direct-method estimate as the model's value at the episode's starting state:
This is the model's prediction of the return the target policy would collect from . It is a per-episode baseline, not yet an average over the target-policy state distribution. Averaging over many episodes drawn from the target policy's initial-state distribution recovers the population-level direct method.
Now add a weighted residual term to that baseline:
The baseline is the model's guess. The sum is the model's correction, weighted by how much the target policy cared about each step. Why does this stay unbiased? Two separate arguments, and you need only one of them.
If the model is correct, then for every step. The weighted residual term has expectation zero, so the estimator reduces to the direct method — which is already correct. The correction adds nothing but noise, and that noise is centered.
If the model is wrong but the weights are valid, the weighted residual term absorbs the model's error. The direct-method bias is exactly the quantity the residual term estimates and subtracts, because the importance weight makes the residual average over the target policy's distribution rather than the behavior policy's.
That is the doubly robust property stated precisely: unbiased if either the model or the weights are correct — not both. It is a weaker promise than the name suggests. It does not repair zero overlap, and it does not remove variance from the weight itself. If the weights are heavy-tailed, the correction inherits that tail.
Notice the special case: set and . Then , and the estimator collapses back to step-wise importance sampling. The doubly robust form is a generalization, not a replacement.
Knowledge check
Check your understanding
Answer this question before you continue.
A Small Numerical Example, Worked by Hand
Take a two-step episode, one state per step, two actions , and . The logged trajectory is then , with rewards and .
| Step | ||||
|---|---|---|---|---|
| 1 () | 0.8 | 0.5 | 1.6 | 1.6 |
| 2 () | 0.3 | 0.6 | 0.5 | 0.8 |
The cumulative weight is .
Trajectory-wise IS: .
Step-wise IS: .
Now suppose an imperfect value model predicts , , , and . The episode-level direct-method baseline is the model's value at the starting state: .
The residuals are and . Using and : and .
Doubly robust: .
Three numbers — , , — from one trajectory. They disagree because they weight the same data differently, and the model's error is large enough that the correction moves the estimate a lot. One trajectory proves nothing statistically. The example shows the mechanics, not a ranking. Do not read a winner out of a single sample.
Knowledge check
Check your understanding
Answer this question before you continue.
Support, Variance, and When Not to Trust the Number
Two conditions must hold for any of this to mean anything. First, coverage: the behavior policy must assign positive probability to every action the target policy might take. Second, weights must not be dominated by a few trajectories.
The diagnostics worth computing before you report a value:
- The maximum weight and its distribution. A single weight orders of magnitude above the rest is a warning, not a win.
- Effective sample size, the inverse of the sum of squared normalized weights. It tells you how many trajectories actually carried the estimate.
- A side-by-side comparison against the direct method. If the two disagree wildly, the weights are doing something you should inspect before trusting either.
A high estimated value with heavy-tailed weights is a red flag. It usually means a few trajectories are standing in for the whole target policy, and the estimate is closer to a lottery ticket than a measurement. This is the same coverage boundary that makes offline learning hard — unsupported actions produce unreliable targets whether you are learning or evaluating.
Decision boundary: Use plain importance sampling for short horizons and similar policies. Use a doubly robust form when you have a reasonable model and moderate overlap. Treat any estimate as unusable when overlap is near zero, regardless of which estimator you pick.
These are estimators with sampling error, not guarantees. A point estimate without a variance or interval is an incomplete result.
The Habit That Follows From the Derivation
Before you report any off-policy value, do three things: check overlap, inspect the weight distribution, and report the estimate with its uncertainty rather than as a single number. The derivation tells you why the number is unbiased under coverage. It does not tell you whether your data satisfies the condition.
Your next step is concrete: take your own logged episodes, compute the per-step ratios, and plot the weight distribution and effective sample size. Then run the importance sampling and doubly robust estimates side by side and look at the gap. If the gap is small and the weights are tame, you have a defensible estimate. If the gap is large or the weights are heavy-tailed, you have found the boundary of what your data can support — and that is more valuable than a confident number.
Knowledge check
Final check
Finish the article by checking the ideas you just learned.
References
Build a stronger RL mental model
Use structured resources to connect equations, algorithms, and observable agent behavior.


