Skip to content
advanced

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…

Published 2026-10-02Updated 2026-10-0410 min read
Intense poker game in a lively casino with multiple players. Experience the thrill and strategy of poker.
Intense poker game in a lively casino with multiple players. Experience the thrill and strategy of poker. Photo by Jonathan Borba on Pexels.

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 μ\mu generated the logged trajectories. Every action in your dataset was sampled from μ(⋅∣st)\mu(\cdot \mid s_t). The target policy π\pi is the candidate whose value J(π)J(\pi) 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 HH, a discounted return with factor γ\gamma, and that you know μ\mu's action probabilities (or can recover them from the logging system). Define the per-step ratio

ρt=π(at∣st)μ(at∣st)\rho_t = \frac{\pi(a_t \mid s_t)}{\mu(a_t \mid s_t)}

and the cumulative ratio ρ1:t=∏t′=1tρt′\rho_{1:t} = \prod_{t'=1}^{t} \rho_{t'}.

One precondition does all the heavy lifting later: coverage. Wherever π(a∣s)>0\pi(a \mid s) > 0, we need μ(a∣s)>0\mu(a \mid s) > 0. 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 μ\mu 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 τ=(s1,a1,…,sH,aH)\tau = (s_1, a_1, \dots, s_H, a_H) under a policy. It factors into an initial-state term, a transition term, and an action term:

Pπ(τ)=p(s1)∏t=1HP(st+1∣st,at) π(at∣st).P_\pi(\tau) = p(s_1) \prod_{t=1}^{H} P(s_{t+1} \mid s_t, a_t)\, \pi(a_t \mid s_t).

Now divide the target probability by the behavior probability:

Pπ(τ)Pμ(τ)=p(s1)∏tP(st+1∣st,at) π(at∣st)p(s1)∏tP(st+1∣st,at) μ(at∣st).\frac{P_\pi(\tau)}{P_\mu(\tau)} = \frac{p(s_1) \prod_t P(s_{t+1} \mid s_t, a_t)\, \pi(a_t \mid s_t)}{p(s_1) \prod_t P(s_{t+1} \mid s_t, a_t)\, \mu(a_t \mid s_t)}.

The initial-state density cancels. The transition density cancels — it does not depend on the policy. What survives is the action probabilities:

Pπ(τ)Pμ(τ)=∏t=1Hπ(at∣st)μ(at∣st)=ρ1:H.\frac{P_\pi(\tau)}{P_\mu(\tau)} = \prod_{t=1}^{H} \frac{\pi(a_t \mid s_t)}{\mu(a_t \mid s_t)} = \rho_{1:H}.

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:

V^IS=ρ1:H∑t=1Hγt−1rt.\hat{V}_{\text{IS}} = \rho_{1:H} \sum_{t=1}^{H} \gamma^{t-1} r_t.

The step-wise (per-decision) variant applies the prefix weight to each reward separately:

V^step-IS=∑t=1Hγt−1ρ1:t rt.\hat{V}_{\text{step-IS}} = \sum_{t=1}^{H} \gamma^{t-1} \rho_{1:t}\, r_t.

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.

For a logged two-step trajectory, the per-step ratios are 1.5 and 0.4. What is the trajectory likelihood ratio, assuming the policy-independent initial-state and transition terms cancel?
Output Prediction

Focus: Compute a trajectory likelihood ratio from per-step target-to-behavior action-probability ratios.

Unbiasedness Under Coverage, and Where the Guarantee Stops

The unbiasedness argument is short. Take the expectation of the weighted return under μ\mu:

Eμ ⁣[ρ1:H G]=∫Pμ(τ) Pπ(τ)Pμ(τ) G(τ) dτ=∫Pπ(τ) G(τ) dτ=J(π).\mathbb{E}_{\mu}\!\left[\rho_{1:H}\, G\right] = \int P_\mu(\tau)\, \frac{P_\pi(\tau)}{P_\mu(\tau)}\, G(\tau)\, d\tau = \int P_\pi(\tau)\, G(\tau)\, d\tau = J(\pi).

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 μ\mu — 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 HH ratios. Even if each ratio is modest, the product's variance can grow exponentially in the horizon. A weight of 1.21.2 per step over twenty steps is a multiplier near 3838. 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 target policy takes an action with positive probability in a state, but the behavior policy assigns that action probability zero. What does the article's unbiasedness argument imply?
Misconception Check

Focus: Explain why support coverage is required for the importance-weighted unbiasedness argument.

The Direct Method and the Residual It Leaves Behind

The direct method fits an approximate value function Q^\hat{Q} or V^\hat{V} and reads off the target policy's value with no importance weight at all:

V^DM=Es∼dπ ⁣[V^(s)].\hat{V}_{\text{DM}} = \mathbb{E}_{s \sim d_\pi}\!\left[\hat{V}(s)\right].

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:

δt=rt+γV^(st+1)−Q^(st,at).\delta_t = r_t + \gamma \hat{V}(s_{t+1}) - \hat{Q}(s_t, a_t).

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

A logged episode feeds two paths: its starting-state value estimate forms the baseline, while each step’s residual is multiplied by its cumulative importance ratio. The weighted residuals are summed and added to the baseline to produce the doubly robust estimate.
The doubly robust estimate starts with the model’s prediction, then corrects it using residuals weighted by how likely the target policy was to produce each observed action.

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:

V^DM(τ)=V^(s1).\hat{V}_{\text{DM}}(\tau) = \hat{V}(s_1).

This is the model's prediction of the return the target policy would collect from s1s_1. It is a per-episode baseline, not yet an average over the target-policy state distribution. Averaging V^(s1)\hat{V}(s_1) 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:

V^DR(τ)=V^(s1)+∑t=1Hγt−1ρ1:t δt,δt=rt+γV^(st+1)−Q^(st,at).\hat{V}_{\text{DR}}(\tau) = \hat{V}(s_1) + \sum_{t=1}^{H} \gamma^{t-1} \rho_{1:t}\, \delta_t, \qquad \delta_t = r_t + \gamma \hat{V}(s_{t+1}) - \hat{Q}(s_t, a_t).

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 E[δt∣st,at]=0\mathbb{E}[\delta_t \mid s_t, a_t] = 0 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 Q^=0\hat{Q} = 0 and V^=0\hat{V} = 0. Then δt=rt\delta_t = r_t, 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.

According to the derivation, when can the doubly robust estimator be unbiased?
Single Choice

Focus: Identify the alternative conditions under which the doubly robust estimator is unbiased as described in the article.

A Small Numerical Example, Worked by Hand

Take a two-step episode, one state per step, two actions {a,b}\{a, b\}, and γ=1\gamma = 1. The logged trajectory is aa then bb, with rewards r1=2r_1 = 2 and r2=3r_2 = 3.

Stepπ(at∣st)\pi(a_t \mid s_t)μ(at∣st)\mu(a_t \mid s_t)ρt\rho_tρ1:t\rho_{1:t}
1 (aa)0.80.51.61.6
2 (bb)0.30.60.50.8

The cumulative weight is ρ1:2=1.6×0.5=0.8\rho_{1:2} = 1.6 \times 0.5 = 0.8.

Trajectory-wise IS: 0.8×(2+3)=4.00.8 \times (2 + 3) = 4.0.

Step-wise IS: 1.6×2+0.8×3=3.2+2.4=5.61.6 \times 2 + 0.8 \times 3 = 3.2 + 2.4 = 5.6.

Now suppose an imperfect value model predicts V^(s1)=2.5\hat{V}(s_1) = 2.5, Q^(s1,a)=2.5\hat{Q}(s_1, a) = 2.5, Q^(s2,b)=2.0\hat{Q}(s_2, b) = 2.0, and V^(s3)=0\hat{V}(s_3) = 0. The episode-level direct-method baseline is the model's value at the starting state: V^DM(τ)=V^(s1)=2.5\hat{V}_{\text{DM}}(\tau) = \hat{V}(s_1) = 2.5.

The residuals are δ1=2+V^(s2)−Q^(s1,a)\delta_1 = 2 + \hat{V}(s_2) - \hat{Q}(s_1, a) and δ2=3+V^(s3)−Q^(s2,b)\delta_2 = 3 + \hat{V}(s_3) - \hat{Q}(s_2, b). Using V^(s2)=2.0\hat{V}(s_2) = 2.0 and V^(s3)=0\hat{V}(s_3) = 0: δ1=2+2.0−2.5=1.5\delta_1 = 2 + 2.0 - 2.5 = 1.5 and δ2=3+0−2.0=1.0\delta_2 = 3 + 0 - 2.0 = 1.0.

Doubly robust: V^DR(τ)=2.5+(1.6×1.5+0.8×1.0)=2.5+(2.4+0.8)=2.5+3.2=5.7\hat{V}_{\text{DR}}(\tau) = 2.5 + (1.6 \times 1.5 + 0.8 \times 1.0) = 2.5 + (2.4 + 0.8) = 2.5 + 3.2 = 5.7.

Three numbers — 4.04.0, 5.65.6, 5.75.7 — 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.

In the worked two-step example, the baseline is 2.5, the residuals are 1.5 and 1.0, and the prefix weights are 1.6 and 0.8. What is the episode-level doubly robust estimate when γ = 1?
Output Prediction

Focus: Apply the episode-level doubly robust formula to the article's worked numerical example.

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.

A log contains 100 trajectories, but two have normalized weights of 0.5 each and the other 98 have weight 0. What is the effective sample size using the article's inverse-sum-of-squared-normalized-weights definition?
Question 1 of 2Scenario Interpretation

Focus: Interpret effective sample size when normalized importance weights are concentrated on very few trajectories.

A practitioner has a reasonable value model and moderate overlap between the behavior and target policies. Which choice best matches the article's guidance for estimating the target value?
Question 2 of 2Comparison Reasoning

Focus: Select an estimator consistent with the article's guidance when a reasonable value model and moderate policy overlap are available.

References

  1. Doubly Robust Off-policy Value Evaluation for ...nanjiang.cs.illinois.edu
Practical resource

Build a stronger RL mental model

Use structured resources to connect equations, algorithms, and observable agent behavior.

Browse resources
Related sites

Continue across related AI foundations

Use LearnPyFast for Python foundations and LearnLLMFast for practical language-model and agent application concepts.

Python tutorialstutorial

LearnPyFast

Beginner-friendly Python tutorials, examples, and learning paths for practical programming foundations.

PythonProgrammingBeginners
Visit LearnPyFast
LLM tutorialstutorial

LearnLLMFast

Practical LLM tutorials for builders who want to understand prompting, workflows, agents, and AI applications.

LLMAIBuilders
Visit LearnLLMFast

Keep learning

Related reinforcement learning tutorials

Continue with nearby RL concepts, algorithms, and experiments that build on the same decision process.