Introduction
Welcome to our next lesson in reinforcement learning foundations. In the previous lesson, we significantly improved the REINFORCE algorithm by introducing a baseline. This helped reduce the high variance of our gradient estimates by judging actions based on whether they produced a return that was better or worse than expected, rather than based on the raw return itself.
However, even with a baseline, our algorithm is still on-policy. This means that after every single policy update, we have to throw away all the trajectory data we collected and gather new data with the newly updated policy. This is incredibly sample-inefficient. Imagine learning to play chess but only being able to learn from the single game you just played, then having to forget it entirely before playing the next one.
Today, we will tackle this inefficiency by exploring off-policy learning. The goal of off-policy methods is to allow our agent (the "target policy" we are training) to learn from data collected by a different, potentially older policy (the "behavior policy"). This dramatically improves sample efficiency.
Our learning outcome for this lesson is to: Describe the importance sampling ratio and its role in off-policy policy gradient methods. This concept is the mathematical key that unlocks off-policy learning and is fundamental to understanding modern algorithms like PPO, which are used to align the very models this course focuses on.
1. From On-Policy to Off-Policy: The Core Problem
The core challenge of off-policy learning is a distributional mismatch. The policy gradient theorem, which we use to update our policy , relies on an expectation calculated over trajectories generated by that same policy .
If we have a dataset collected by a different policy, say , we can't just plug those samples into the equation above. The data comes from a different probability distribution, so the expectation would be incorrect. We need a way to correct for this mismatch.
2. The Solution: Importance Sampling
The statistical technique designed to solve exactly this problem is called Importance Sampling (IS). It provides a way to estimate the expected value of a function under one distribution, using samples drawn from another.
The following video provides an excellent and intuitive explanation of the core mathematical trick behind importance sampling.
This video from the 'Mutual Information' channel breaks down the concept of Importance Sampling, starting from the basic problem of estimating expectations.
Please watch from 00:20 to 08:59. The key sections are: 00:20 - 06:13: Introduction to Monte Carlo estimation for calculating expectations. This sets the stage. 06:13 - 07:35: The core trick of Importance Sampling. Pay close attention to how multiplying by q(x)/q(x) allows you to change the distribution you're sampling from. 07:35 - 08:59: How to use the resulting formula and its properties.
The Importance Sampling Ratio
As the video explained, the central insight of importance sampling is to re-weight the samples. If we want to compute the expectation of a function under a target distribution using samples from a proposal (or behavior) distribution , we can do so with the following identity:
The term is the importance sampling ratio (or importance weight). It "corrects" for the fact that we are sampling from the "wrong" distribution, . It works by:
- Up-weighting samples that were more likely under our target distribution than under the behavior distribution .
- Down-weighting samples that were less likely under .

3. Applying Importance Sampling to Policy Gradients
Now, let's apply this directly to our reinforcement learning problem. In our case:
- The target distribution is our new policy we want to evaluate or improve, .
- The behavior distribution is the old policy that collected the data, .
The following reading provides a clear derivation of the off-policy policy gradient objective using exactly this principle.
This article from Lilian Weng's blog shows the step-by-step derivation of the off-policy policy gradient, explicitly highlighting the importance weight.
Read the section titled 'Off-Policy Policy Gradient'. Focus on the final equation in the derivation, which shows how the standard policy gradient is modified by the importance weight.
As the article shows, the gradient of our off-policy objective becomes:
Here, the importance sampling ratio is . This single ratio allows us to take a batch of data collected by the behavior policy and use it to calculate an unbiased estimate of the gradient for our target policy . This means we can finally reuse old data, making our learning process much more sample-efficient.
4. The Catch: The Problem of High Variance
Importance sampling is powerful, but it comes with a significant risk: high variance. If the target policy becomes very different from the behavior policy , the importance ratio can become extremely large or close to zero.
- If is large but is small, the ratio blows up. A single data point can have an enormous effect on the gradient update, leading to instability.
- If is small but is large, the ratio becomes tiny, and the data point is effectively ignored.
The following resources explain and visualize this critical issue.
This article provides a mathematical derivation showing why the variance of the importance sampling estimator can become very large.
Read the sections 'Importance Sampling' and 'Although importance sampling is unbiased, it can be high variance...'. Pay attention to the derivation in Equation (5), which shows that the variance grows linearly with the number of actions in a simple case. This highlights the danger of diverging policies.

This variance problem is the central challenge that modern policy gradient algorithms like TRPO and PPO were designed to solve.
5. The Solution: Trust Region Methods (A Preview)
If the variance of the importance sampling estimate explodes when the new and old policies are too different, the solution is intuitive: don't let the policies get too different.
This is the core idea behind Trust Region Policy Optimization (TRPO) and its more popular successor, Proximal Policy Optimization (PPO). They use the importance sampling ratio to enable off-policy updates but add a constraint or a penalty to ensure the new policy stays within a "trust region" of the old policy that collected the data.
Let's watch a brief clip that introduces this concept.
L4 TRPO and PPO (Foundations of Deep RL Series)
Professor Pieter Abbeel's lecture on TRPO and PPO explains how the off-policy objective can be formulated as a constrained optimization problem.
Watch from 04:28 to 07:16, and then from 09:09 to 10:20. The first part introduces the 'surrogate loss' function which uses the importance ratio. The second part explains how TRPO turns this into a constrained optimization problem, using KL divergence to measure the distance between the policies.
As the video explains, TRPO frames the problem as:
- Maximize the surrogate objective (which uses the importance sampling ratio).
- Subject to the constraint that the KL divergence between the old and new policies is less than some small value .
This ensures that we get the sample efficiency benefits of off-policy learning while controlling the variance introduced by the importance sampling ratio.
Conclusion
In this lesson, we established the motivation and mechanism for off-policy policy gradient methods, which are far more sample-efficient than their on-policy counterparts.
Key Takeaways:
- On-policy algorithms like REINFORCE are sample-inefficient because they discard data after each update.
- Off-policy algorithms solve this by learning from a dataset collected by a behavior policy () while optimizing a target policy ().
- The importance sampling ratio, , is a correction factor that reweights the contribution of each sample to give an unbiased estimate of the policy gradient.
- The main drawback of importance sampling is high variance when the target and behavior policies diverge significantly.
- Modern algorithms like TRPO and PPO manage this variance by constraining the policy update, ensuring the new policy stays in a "trust region" close to the old one.
Preview of the Next Lesson:
We have now assembled all the foundational pieces of reinforcement learning needed for this course. In our next module, we will move from these general RL concepts to the specific algorithms used to align modern large language models. We'll begin with a deep dive into the full RLHF (Reinforcement Learning from Human Feedback) pipeline, starting with Supervised Fine-Tuning (SFT) and Reward Modeling, before finally tackling Proximal Policy Optimization (PPO) itself. You will see the clipped importance sampling ratio at the very heart of the PPO objective function.