Sampling refers to the generation of random variables following a certain probability distribution; for example if is a density on , we want to generate random variables which are independent and follow the distribution given by , or we want to compute expectations like
for some function . In many cases, one does not fully knows , but only that it is proportional to some function , that is
and computing the normalizing constant is intractable.
There are many techniques that still allow to sample from in this case; the whole field of Monte-Carlo research consists in crafting stochastic systems (Markov Chains, diffusions) that converge toward samples from . In this note I'm focusing on a simpler method, importance sampling (IS), also called reweighting, which allows to compute integrals like above, without sampling from .
Plan of the note.
First, I define IS and give examples.
Second, I introduce a usefull metric called the Effective Sample Size. Statisticians know this stuff very well.
Then I explain see how much compute you need to use IS efficiently: first with a result by Chatterjee and Diaconis for estimating a specific integral, then a result on Wasserstein distances for estimating simultaneously every integral.
There's a small section of "useful" diagnostics for IS estimators.
And then there's the proof of the Chatterjee-Diaconis result.
Let be another probability density, supposed easy to sample from. Throughout, denote the normalized densities and their unnormalized versions, so that and . We will never use the exact values of or .
In the sequel, we will adopt the following notations.
Samples from are noted or . The density is called the target.
Samples from are noted or . The density is called the proposal.
The ratio of unnormalized densities will be denoted by .
The ratio of the densities will be denoted by .
Then, for any function ,
Applying this formula to yields . Consequently,
This suggests the following method for approximating without computing or .
Proof. By the Law of Large Numbers, the LHS of (5) converges towards .
Also by the LLN, converges towards .
Take the ratio of the two to get (6).
This technique explains the word importance sampling: the samples are iid but their density is , not , and the weights correct the difference between the two.
The estimator in (6) is sometimes called Self-Normalized Importance Sampling (SNIS), because of the normalization term .
If some is very likely under but not so much under (meaning that is close to zero but is high), then this sample will be assigned a very small weight.
It is very common that has an exponential form, typically for some functions and . In this case you better estimate with with the log-sum-exp trick to avoid numerical instabilities.
Unless and are already very close to each other, this estimator can be very bad. Let's look at the variance of the estimator given in (5): clearly , hence
The first term, especially , could be prohibitively big. Indeed, let us consider the following situation: our prior density is a Standard Gaussian , and our target density is the same, but shifted by 10: . Here both densities are normalized, so and , . But,
That is too big. Having a low variance for would require having more than samples, which is impossible. And indeed, one can even find simple examples (exponential distributions) for which the variance is infinite.
The ESS. Let us note the normalized weights,
The SNIS estimation of is
Suppose for a moment that you are in the ideal case where your proposal distribution is equal to , and you have samples . Then, all the weights are equal to 1 and thus . The variance of in this case is simply
where . Now go back to the case where you have samples (I'll call them "fake samples") from a proposal distribution . Forget an instant that the are random, and treat them like fixed weights. Yes, I know they're not, but imagine. Then the variance of should be
This is actually a good approximation of the variance of , as explained in the litterature (e.g. in Kong's seminal paper or in Liu's paper).
Given these two approximations, what is the number of "real" samples from that would give the same variance as "fake" samples? Just solve (11) = (12) and you get the Effective Sample Size,
Other equivalent forms are sometimes seen in the litterature, e.g. in Kong's seminal paper[1], with more rigorous derivations (they consist in using the "delta-method" to estimate and are not especially enlightening).
An ESS of with a sample size of 1000 says that your 1000 "fake samples" will get you the same precision as 100 "real samples". Ultimately, if one real sample from costs 1€, then the real price of your fake samples would be €. And we always have , of course.
Generalized ESS. In general, if you have a decent "precision metric" to evaluate an estimator, you can compute a generalized notion of sample size: just compute the metric on your samples, and then compute the number of real samples which would give the same metric.
In practice, the ESS is not really used to assess the quality of an estimator, but rather to take decisions on when to resample or not. It is often used in sequential settings, where one can monitor the ESS along time and decide to resample the proposals when the ESS collapses.
However, Empirical Sample Sizes often do not tell the whole story, and are not very informative on the precision of an estimator, but rather on the relative precision compared to other estimators. Concretely, they do not tell you if your samples are really enough to estimate .
For any function , we will denote by
the IS estimator of the target integral , which will be noted .
Note that is unchanged if the weights are replaced by the normalized likelihood ratio ; in the sequel of this section we work with , for which .
The Kullback-Leibler divergence from towards is
It is supposed to be . It can also be defined as . In general, concentration inequalities allow to bound how much is concentrated around its mean : depending on , the deviation probability
is a decreasing function of with . The error terms in the sequel will be expressed using this function . For any positive we note
and finally we also note the L2-norm relatively to the target .
Theorem (Chatterjee and Diaconis, 2015).
Positive part. Suppose that .
Then, for any which is in , Negative part. Conversely, suppose that .
We note the IS estimator of the Kullback-Leibler divergence . Then this estimator is bad, in the sense that with a fixed positive probability, at least greater than .
To summarize: if one has more than samples, then for every fixed the probability of having close to is high. But if is smaller than , then there might be functions such that with high probability, the estimator is far away from . The original proof in the Chatterjee-Diaconis paper shows that the function is itself hard to approximate, for some . That the KL itself is hard to estimate is due to El Houssain Chahboun, a PhD student under the supervision of Pierre Jacob. The proof is entirely due to him!
Uniform bounds. Note that for a specific , it might very well be true that less than samples are needed. But is always enough. This asks for a natural question: how many samples do you need so that every estimator of every is simultaneously good? That is, could we control
over a certain class ?
If is the set of all 1-Lipschitz functions, it turns out that this supremum is nothing more than the Wasserstein-1 distance between the target density and the « empirical self-normalized weighted measure », namely
where are the self-normalized weights. In a recent work with Michael Goldmann, we computed the asymptotics of as is large[2].
Wasserstein asymptotics. For compactly supported and bounded away from zero and , we have
Here, means that the LHS is between and , for some unknosn absolute constants . However, in the case, it is known that , so that the result is actually a true limit.
You can safely remove the . We didn't include it in the paper we all you have to check is a concentration bound. It's safe.
Consequence on the sample size. Using this asymptotic, a good proxy for Chatterjee and Diaconis’ study would be that, to have a good approximation, we would need a number of samples at least comparable to , where . If is large, we can work further our approximation:
The entropy of is ; therefore, the term above is also equal to , which is not the of Chatterjee-Diaconis. What happens here is not 100% clear for me, but here is my take:
Low entropy means less samples are needed. If is very low, then is very concentrated; ultimately one can think of as almost a Dirac located at a point, say . Then, all the integrals are either (if the support of avoids zero) or . If there is a proposal point close to 0, then ALL the integrals will be well approximated. Hence the need for less points.
High entropy means more samples are needed. If is high then is more uniform on its support. But then, will high probability there will be a (random) zone in the domain where there is a lack of points, and the functions supported on this zone will be badly approximated. Hence the need for more points.
As explained in the ESS section, a prominent question in the theory of IS is: how can we craft diagnostic metrics of an estimator, that indicate whether the estimator is good or bad?
A priori, the two results given above could give an answer: we have to check if is greater than, for example, . We do not know but we could still estimate it using IS, with
Then declare your IS estimator to be good is .
This is dommed to fail. As Chatterjee and Diaconis explain, "any diagnostic criterion that is itself dependent on the accuracy of an estimate obtained by importance sampling, is unlikely to be effective as a measure of the efficacy of importance sampling". Id est, the second point of the theorem tells you that estimating with (23) is already a bad idea, so don't use it to estimate if is large enough.
That's the point of the ESS: it is self-contained, in that you only need the weights to compute the diagnostic criterion. But the ESS itself is a weak criterion, in that there is no connection between the variance of and . This is why Chatterjee and Diaconis favor another criterion, which is simply the ratio,
They have convincing but incomplete heuristics on this diagnostic quantity. I will, at some point, expose them here.
The proof relies on the following lemma, in which we have set
As noted earlier we have , hence it is natural to choose at the same scale as . We take . The ratio becomes and . Overall, the RHS of becomes equal to .
We will prove this lemma later. From now on, let us prove the theorem.
Proof of the positive part. If and then we see that Now choose and . Moreover, since by the Cauchy-Schwarz inequality, we get that
In particular, if , then
We use the union bound and bound the probability of each event separately; to do that we apply Markov's inequality and the Lemma to the constant function and to the function . We get Hence by the union bound the probability is at most , which is the claim.
Proof of the negative part. A small remark to begin: we note
for the normalized importance weights. They are positive and sum to 1. Consequently, for any numbers , if is greater than then at least one of the is greater than .
When trying to estimate the KL with importance sampling, we set
where . Then, by the remark above, Now we bound this probability using Markov’s inequality. We have To summarize, if we showed that
hence with probability at least .
Set . Then,
– Term is equal to . By the Cauchy-Schwarz inequality it is smaller than
– Term is smaller than hence its expectation is smaller than and we use the same trick as (37).
– Term is bounded as follows:
We gather the three bounds and get (26).
The paper by Chatterjee and Diaconis, with a very nice application to an old remark by Knuth on self-avoiding walks.
A nice paper on the Effective Sample Size by Jun S Liu, with a theoretical justification on why the ESS is significant and a comparison with other sampling techniques.
Two papers on the sample size of IS, here and there by Daniel Sanz-Alonso, especially on the -divergence.
Cool blog posts by Statisticians on the ESS: Sebastian Nowozin, Alex Smola
| [2] | actually we computed the asymptotics for every . |
| [1] | Typically the equivalent expressions are where is the variance of under . Since , we see that , and thus Kong's ESS is . But we can estimate
and thus Hence the equivalence between the two expressions. |