Off-policy learning and importance sampling
Off-policy learning lets you evaluate or learn a target policy using data generated by a different behaviour policy; importance sampling reweights returns to correct that mismatch.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Off-policy learning is used when you must learn or evaluate a policy (the target policy) from data collected by another policy (the behaviour policy). Engineers use it when running the target policy online is expensive or unsafe (for example, robotics, recommender systems, or medical policies) and when using historical logs (offline RL). Importance sampling is the basic statistical tool that enables this reweighting.
Key ideas
Start with definitions.
- Reinforcement learning (RL): learning by interaction where an agent takes actions in an environment to maximise cumulative reward. (See other topics for MDPs and policies.)
- Policy: a probability distribution over actions given states. If the policy we want to evaluate is π, call it the target policy. The policy that generated data is b, called the behaviour policy.
- Off-policy learning: any method that uses data generated under b to evaluate or improve π.
- Importance sampling (IS): a method from statistics that reweights expectations under one distribution to compute expectations under another. In RL IS multiplies episode returns by the ratio of probabilities of the episode under π and under b.
- Coverage (support) assumption: for IS to be valid you must have b(a|s) > 0 whenever π(a|s) > 0 for all states s and actions a encountered. In words: b must sometimes take all actions that π might take.
Exact statement (Monte Carlo, episodic) — unbiasedness of ordinary IS:
Assume episodic tasks with returns G, episodes τ = (S0,A0,R1,S1,A1,R2,...,ST), and that episodes start from the same initial-state distribution under both policies or that you condition on the start state s. Let P_b(τ) be the probability of τ under behaviour policy b and P_π(τ) under π. Define the importance weight for an episode as
ρ(τ) = Π_{t=0}^{T-1} π(A_t|S_t) / b(A_t|S_t).
If b has coverage (for all actions with π>0, b>0), then
E_b[ ρ(τ) · G(τ) | S0 = s ] = E_π[ G(τ) | S0 = s ] = v_π(s).
Proof (common exam derivation):
- Write the expectation under b as sum over episodes: E_b[ρG] = Σ_τ P_b(τ) ρ(τ) G(τ).
- Replace P_b(τ)·ρ(τ) by P_π(τ) because the product of b-probabilities cancels with ρ's denominator.
- This becomes Σ_τ P_π(τ) G(τ) = E_π[G]. The cancellation requires the coverage assumption.
Variants and practical points:
- Ordinary importance sampling (also called unnormalized IS) uses the mean of ρ·G across N episodes. It is unbiased (finite N expectation equals v_π) assuming coverage and correct start-state distribution.
- Weighted importance sampling (also called self-normalized IS) uses weights normalized by their sum: (Σ ρ_i G_i) / (Σ ρ_i). This estimator is biased for finite N but often has far lower variance; it is consistent (converges to true value as N → ∞) under mild conditions.
- Per-decision importance sampling (PDIS): apply IS ratios per reward step to reduce variance. It can also be combined with bootstrapping in off-policy TD methods. For episodic returns G = Σ γ^t R_{t+1}, the per-decision estimator multiplies each reward by the product of importance ratios up to the time the reward depends on.
Where diagrams help (draw this in an exam):
- Draw a timeline of one episode: S0 —(A0)→ S1 —(A1)→ S2 ... label each arrow with both π(A_t|S_t) and b(A_t|S_t). Underneath, write the reward R_{t+1} at each step. Show that ρ is the product of π/b along the arrows. This visual helps show why P_b·ρ = P_π.
Assumptions to state in answers: episodic MDP, same start-state distribution (or conditioning on S0), coverage assumption, finite returns (so expectations exist). Note variance issues: IS weights can blow up, making estimates high-variance or numerically unstable.
Formulas
v_π(s) = E_π[ G | S0 = s ] — v_π(s) is the value of state s under policy π; units: expected cumulative reward (same units as reward); applies in MDP episodic or infinite-horizon with discount.
ρ(τ) = Π_{t=0}^{T-1} π(A_t|S_t) / b(A_t|S_t) — importance weight of an entire episode τ; dimensionless; valid when b(a|s) > 0 wherever π(a|s) > 0.
ordinary IS estimator: V̂ = (1/N) Σ_{i=1}^N ρ(τ_i) · G(τ_i) — estimate of v_π(s) from N episodes starting at s; units: expected reward; unbiased under coverage and same start distribution.
weighted IS estimator: V̂ = (Σ_{i=1}^N ρ(τ_i) · G(τ_i)) / (Σ_{i=1}^N ρ(τ_i)) — normalized weights; units: expected reward; biased for finite N but consistent as N→∞.
per-decision IS (episodic): V̂ = (1/N) Σ_{i=1}^N Σ_{t=0}^{T_i-1} γ^t · ρ_{i,t} · R_{i,t+1} where ρ_{i,t} = Π_{k=0}^{t} π(A_{i,k}|S_{i,k}) / b(A_{i,k}|S_{i,k}) and γ∈[0,1] is discount.
coverage condition: ∀s,∀a: π(a|s) > 0 ⇒ b(a|s) > 0 — dimensionless; required for unbiased ordinary IS.
Worked examples
Example 1 — numeric, episodic single-step MDP (clear steps, units are reward units):
Problem: One start state S. Two actions a1 and a2. Rewards: R=1 if a1 chosen (episode ends), R=0 if a2 chosen (episode ends). Target π(a1|S)=0.8, π(a2|S)=0.2. Behaviour b(a1|S)=0.5, b(a2|S)=0.5. We collect N=3 episodes with observed (action, reward): (a2,0), (a1,1), (a1,1).
Step 1: Compute episode importance weights ρ = π(a|s)/b(a|s).
ρ1 = 0.2 / 0.5 = 0.4 for episode 1 (G1=0).
ρ2 = 0.8 / 0.5 = 1.6 for episode 2 (G2=1).
ρ3 = 0.8 / 0.5 = 1.6 for episode 3 (G3=1).
Step 2: Ordinary IS estimate:
V̂_ord = (1/3) * (0.4*0 + 1.6*1 + 1.6*1) = (1/3) * 3.2 = 1.0666667 reward units. Bold: 1.0666667.
Step 3: Weighted IS estimate:
Numerator = 0.40 + 1.61 + 1.61 = 3.2. Denominator = 0.4 + 1.6 + 1.6 = 3.6.
V̂_wt = 3.2 / 3.6 = 0.8888889. Bold: 0.8888889.
True v_π(S) = 0.81 + 0.2*0 = 0.8. Compare: ordinary IS (1.0667) is unbiased in expectation but here high; weighted IS (0.8889) is biased for finite N (bias can be up or down) but often has lower variance and is numerically more stable.
Example 2 — exam-style derivation (model answer): Question: Prove that ordinary importance sampling gives an unbiased estimator of v_π(s) for episodic Monte Carlo policy evaluation, stating assumptions. Model answer (steps):
- State assumptions: episodic MDP, episodes τ begin in S0 = s (or same start distribution), behaviour b has coverage for π, returns G(τ) finite.
- Write estimator expectation under b: E_b[ρ(τ) G(τ) | S0 = s] = Σ_τ P_b(τ | S0=s) · ρ(τ) · G(τ).
- Expand P_b(τ|S0=s) = Π_{t=0}^{T-1} P(S_{t+1}|S_t,A_t)·b(A_t|S_t). The product b in denominator of ρ cancels, leaving P_π(τ|S0=s).
- So E_b[ρG|S0=s] = Σ_τ P_π(τ|S0=s) G(τ) = E_π[G|S0=s] = v_π(s). QED.
Common mistakes
- Normalising ordinary IS when asked for unbiased estimator (normalising gives weighted IS which is biased for finite N).
- Dropping discount γ in per-decision IS or mixing step indices incorrectly when multiplying ratios.
- Treating weighted IS as unbiased for finite samples (it is biased but lower variance).
- Not conditioning on the same start-state distribution; the unbiasedness proof needs same start distribution or conditioning.
For your exam
Types of questions you will get: definitions (define off-policy, importance sampling, coverage), short notes (compare ordinary vs weighted IS), derivations (prove unbiasedness of ordinary IS; derive per-decision IS), numericals (compute IS estimates from sample episodes), and diagram questions (draw episode timeline showing π and b probabilities and the ρ product).
What full-marks answers include:
- State all definitions clearly and in words (target/behaviour, importance ratio).
- Write assumptions (episodic or infinite-horizon, same start distribution or conditioning, coverage, finite returns).
- For derivations: show step-by-step multiplication/cancellation of probabilities and cite law of total expectation where needed.
- For numericals: show each weight ρ, each return G, and the final arithmetic (carry units), and comment on variance or bias if asked.
Quick check
- What is the coverage assumption in off-policy importance sampling?
- Is the ordinary importance sampling estimator biased or unbiased for finite N?
- Why might one prefer weighted IS over ordinary IS in practice? Answers: 1) For all s,a: if π(a|s)>0 then b(a|s)>0 (behaviour must sample all target actions). 2) Unbiased (if coverage and same start distribution). 3) Weighted IS reduces variance (at the cost of finite-sample bias) and is numerically more stable.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?