Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 56

Importance Sampling for Variance Reduction

Recommended resources

Hello! Welcome back.

In our last lesson, we identified the root cause of noise in our renders: statistical variance. We established that the noise level is not just a function of the number of samples , but also of the variance of our estimator itself:

We concluded that while we can always reduce noise by increasing , a more elegant and efficient approach would be to attack the variance term directly.

Today, we will explore exactly how to do that. This lesson introduces importance sampling, a powerful variance reduction technique that is fundamental to all modern path tracers, including the production renderers like Arnold and Karma that you use in your VFX work. Our goal is to understand the concept of importance sampling and how it allows us to create "smarter" sampling strategies that dramatically reduce noise without just brute-forcing more rays.

1. The Core Idea: Sample Where It Matters

The intuition behind importance sampling is simple: if we are trying to estimate an integral by taking random samples, we should concentrate our efforts where the function's value is highest. It's inefficient to waste computation on samples that will contribute little or nothing to the final result.

In path tracing, this means we want to generate rays that are more likely to travel along paths that carry significant light energy.

To get a solid grasp of this principle, let's start with a reading from the Physically Based Rendering book, a cornerstone text in computer graphics.

13.10 Importance Sampling

The section on 'Importance Sampling' from the PBR book provides a clear, intuitive argument for this technique before diving into the math. It also introduces the crucial paradox of the 'perfect' sampling distribution.

Please read the text from the beginning of the section down to (but not including) the subsection '13.10.1 Multiple Importance Sampling'. Focus on the central argument: why sampling according to a distribution that is similar in shape to the integrand reduces variance.

As the text explains, the ideal (but impossible) scenario would be to sample from a probability density function (PDF) that is directly proportional to the function we're integrating, . If we could do this, the estimator would be a constant, and the variance would be zero! This is a paradox because creating such a PDF would require us to already know the integral's value—the very thing we're trying to estimate.

However, this theoretical ideal gives us our practical goal: find a PDF that is similar to and that we can easily sample from.


2. The Mathematical Mechanism

So, how do we technically use a different distribution without calculating the wrong result? The method relies on a simple but powerful mathematical trick. We can change the distribution we sample from, as long as we compensate for that change by weighting our samples appropriately.

This video gives a clear, step-by-step derivation of how this works.

Importance Sampling

The video 'Importance Sampling' by Mutual Information provides a concise and clear explanation of the core mathematical manipulation that enables importance sampling.

Please watch from 05:54 to 08:59. The key moment is when the integral is multiplied by q(x)/q(x). Pay close attention to how this changes the Monte Carlo estimator and what the new variance depends on.

Let's summarize the key steps. We want to estimate the expectation .

  1. We introduce a new, "smarter" PDF, , that we want to sample from instead.
  2. We rewrite the integral by multiplying and dividing by :
  3. We regroup the terms to form a new expectation, this time with respect to :
  4. This gives us a new Monte Carlo estimator. Instead of averaging with samples from , we now average with samples from .

In our path tracer, we started with uniform hemisphere sampling. In that case, was a constant . Our new estimator becomes:

Where is the rendering equation integrand and samples are now drawn from our new, smarter PDF, .

The whole point is that if we choose to be similar to , the term we are averaging will have much less variation, leading to lower variance and a cleaner image.

Comparison of Sampling Techniques for Noise Reduction
This image provides a powerful visual demonstration. On the left, uniform sampling wastes many samples, resulting in high noise. In the middle, importance sampling directs rays towards the light source, capturing the important energy contributions more efficiently and dramatically reducing noise for the same number of samples.

A word of caution: choosing a bad importance sampling distribution—one that is small where the integrand is large—can actually make the noise worse than uniform sampling. This happens because the few "lucky" samples that hit a high-energy region get weighted by a huge factor (), creating extreme fireflies and high variance.

Test your understanding!

Imagine you are rendering a scene with a single, very bright spotlight illuminating a small patch on a diffuse floor. The rest of the scene is dark. Your rendering equation integrand is therefore large only for directions that come from the spotlight.

You have two importance sampling PDFs to choose from:

  1. PDF A: Samples the hemisphere with a distribution that is highest near the surface normal and falls off to zero at the horizon (a cosine distribution).
  2. PDF B: Samples the hemisphere with a distribution that is highly concentrated in the direction of the spotlight.

Which PDF is the better choice for importance sampling in this specific scenario, and why? Relate your answer to the term .

Show answer

PDF B is the far better choice.

Here's why:

  • The integrand is only significant for rays coming from the spotlight.
  • PDF B is designed to match this. It has a high probability in the exact region where is large. This means the ratio will be relatively stable and low-variance across all samples.
  • PDF A (cosine sampling) might be a good general-purpose strategy, but in this case, it doesn't match the lighting. It will still have a very low probability of sampling the spotlight direction. When a ray does happen to hit the spotlight, you will have a very large divided by a very small , leading to a high-variance estimate and fireflies. PDF A is a poor match for the shape of in this scene.

3. Importance Sampling Strategies in Path Tracing

Now let's make this concrete. What functions can we use to build our "smarter" PDF? The rendering equation integrand gives us the main candidates:

We can create importance sampling strategies based on any of these terms:

  1. Sampling the Cosine Term: For diffuse surfaces, the term dominates the shape of the reflected light. A good strategy is to generate more sample rays near the surface normal and fewer near the horizon. This is called cosine-weighted hemisphere sampling.

  2. Sampling the BRDF: For glossy or reflective materials, the BRDF is highly directional. A good strategy is to generate sample rays that are concentrated around the specular reflection or refraction direction.

  3. Sampling the Light (): In scenes with distinct light sources, the incoming light is zero for most directions, and non-zero only for directions that lead to a light. The most effective strategy here is to explicitly sample directions that point directly at the lights. This is often called Next Event Estimation.

Let's watch a short segment that shows the derivation and effect of the first strategy: cosine-weighted sampling.

Rendering Lecture 06 - Importance Sampling

This lecture from TU Wien is quite advanced, but we will focus on the final part where they apply the theory to develop a key importance sampling technique for diffuse materials.

Please watch from 1:08:40 to 1:11:45. The goal is not to memorize the derivation, but to understand the concept: for diffuse surfaces, the shape of the function we're integrating is heavily influenced by the cosine term, so we create a PDF that mimics the cosine function. Note the visual comparison at the end showing how samples are clustered around the normal.

This cosine-weighting strategy is a significant improvement over uniform sampling for diffuse scenes, but it highlights a new problem.


4. The Challenge: A Product of Functions

The integrand is a product. What happens if one term is "peaky" (like a small light source) and another is also "peaky" (like a mirror reflection)? Which one should we follow?

  • Consider a diffuse floor lit by a small, bright light. Cosine sampling (matching the BRDF and cosine term) is inefficient because it rarely hits the light. A better strategy would be to sample the light directly.
  • Consider a mirror-like floor lit by a large, uniform sky dome. Sampling the light source (the sky) is inefficient because the mirror will only reflect a tiny patch of it. A better strategy would be to sample the BRDF (i.e., cast a single ray in the perfect reflection direction).

This reveals a fundamental challenge: no single importance sampling strategy is optimal for all situations. The best strategy depends on the complex interplay between lighting, geometry, and materials.

Multiple Importance Sampling Illustration
This graph illustrates the problem. The target function f(x) has two peaks. One sampling strategy, p1(x), is good at sampling the left peak but misses the right one. Another strategy, p2(x), is good for the right peak but misses the left. Neither is perfect on its own.

Modern renderers solve this by using Multiple Importance Sampling (MIS). Instead of choosing just one strategy, they take samples from several (e.g., one sample from the BRDF distribution, one from the light distribution) and then combine the results using a clever weighting heuristic that favors the strategy that was most likely to find that particular light path. This makes the renderer robust, allowing it to efficiently handle scenes with both large and small lights, and both diffuse and specular materials. We will delve into MIS in a later lesson.


Conclusion

You now understand the core principle behind one of the most important concepts in modern rendering. Importance sampling is the answer to the inefficiency of brute-force rendering.

Key Takeaways:

  • Importance sampling reduces variance by choosing samples from a Probability Density Function (PDF) that is similar in shape to the function being integrated.
  • This concentrates computational effort where the integrand is large, avoiding wasted samples.
  • Mathematically, this is achieved by changing the sampling distribution and weighting the result by the ratio of the original PDF to the new PDF.
  • In path tracing, common strategies involve creating PDFs based on the BRDF, the cosine term, or the light sources.
  • Because the rendering integrand is a product of functions, no single strategy is always optimal. This motivates the use of more advanced techniques like Multiple Importance Sampling (MIS).

Next Steps

We've covered the "what" and "why" of importance sampling. In our next lesson, we'll move on to the "how." We will implement our very first, and arguably most impactful, importance sampling technique: explicit light source sampling. This will allow us to directly target the "fireflies" caused by small, bright lights and achieve a dramatic improvement in image quality.