Post · 3 min read· pp. 28–29

Importance Sampling

Importance sampling is a powerful and pervasive technique in statistics, machine learning, and randomized algorithms.

Basics

Importance sampling is a technique for estimating the expectation μ\mu of a random variable f(x)f(x) under distribution pp from samples of a different distribution q.q.

The key observation is that μ\mu can be expressed as the expectation of a different random variable f∗(x)=p(x)q(x) ⁣⋅ ⁣f(x)f^*(x)=\frac{p(x)}{q(x)}\! \cdot\! f(x) under q.q.

Eq ⁣[f∗(x)]=Eq ⁣[p(x)q(x)f(x)]=∑xq(x)p(x)q(x)f(x)=∑xp(x)f(x)=Ep ⁣[f(x)]=μ\mathbb{E}_{q}\! \left[ f^*(x) \right] = \mathbb{E}_{q}\! \left[ \frac{p(x)}{q(x)} f(x) \right] = \sum_{x} q(x) \frac{p(x)}{q(x)} f(x) = \sum_{x} p(x) f(x) = \mathbb{E}_{p}\! \left[ f(x) \right] = \mu

Technical condition: qq must have support everywhere pp does, f(x)p(x)>0⇒q(x)>0.f(x) p(x) > 0 \Rightarrow q(x) > 0. Without this condition, the equation is biased! Note: qq can support things that pp doesn't.

Terminology: The quantity w(x)=p(x)q(x)w(x) = \frac{p(x)}{q(x)} is often referred to as the importance weight or importance correction. We often refer to pp as the target density and qq as the proposal density.

Now, given samples {x(i)}i=1n\{ x^{(i)} \}_{i=1}^{n} from q,q, we can use the Monte Carlo estimate, μ^≈1n∑i=1nf∗(x(i)),\hat{\mu} \approx \frac{1}{n} \sum_{i=1}^n f^{*}(x^{(i)}), as an unbiased estimator of μ.\mu.

Remarks

There are a few reasons we might want to use importance sampling:

  1. Convenience: It might be trickier to sample directly from p.p.
  2. Bias-correction: Suppose we're developing an algorithm that requires samples to satisfy some safety condition (e.g., a minimum support threshold) and be unbiased. Importance sampling can be used to remove bias while satisfying the condition.
  3. Variance reduction: It might be the case that sampling directly from pp would require more samples to estimate μ.\mu. Check out these great notes for more.
  4. Off-policy evaluation and learning: We might want to collect some exploratory data from qq and evaluate different policies pp (e.g., to pick the best one). Here's a link to a future post on off-policy evaluation and counterfactual reasoning and some cool papers: counterfactual reasoning, reinforcement learning, contextual bandits, domain adaptation.

There are a few common cases for qq worth separate consideration:

  1. Control over qq: This is the case in experimental design, variance reduction, active learning, and reinforcement learning. It's often difficult to design q,q, which results in an estimator with reasonable variance. A very difficult case is in off-policy evaluation because it (essentially) requires a good exploratory distribution for every possible policy. (I have much more to say on this topic.)
  2. Little to no control over qq: For example, you're given some dataset (e.g., new articles), and you want to estimate performance on a different dataset (e.g., Twitter).
  3. Unknown qq: In this case, we want to estimate qq (typically referred to as the propensity score) and use it in the importance sampling estimator. As far as I can tell, this technique is widely used to remove selection bias when estimating the effects of different treatments.

Drawbacks: The main drawback of importance sampling is variance. A few bad samples with large weights can drastically throw off the estimator. Thus, it's often the case that a biased estimator is preferred, e.g., estimating the partition function, clipping weights, indirect importance sampling. A secondary drawback is that both densities must be normalized, which is often intractable.

What's next? I plan to cover variance reduction and off-policy evaluation in more detail in future posts.