Skip to main content

Welcome to a Grasp lesson

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

Lesson 35

Monte Carlo Estimation and Variance Reduction

Recommended resources

Welcome to the next lesson in our "Estimation and Inference" module. In our previous session, we saw how Maximum a Posteriori (MAP) estimation provides a probabilistic interpretation for L1 and L2 regularization, treating them as consequences of placing Gaussian or Laplace priors on model parameters. This was a significant step beyond the purely frequentist view of MLE. However, both MLE and MAP provide only a single point estimate for our parameters. A fully Bayesian approach requires us to reason about the entire posterior distribution, which often involves computing expectations by integrating over this distribution. As we concluded last time, these integrals are almost always analytically intractable, especially in the high-dimensional spaces typical of deep learning.

Today, we will tackle this problem head-on by introducing Monte Carlo methods. This lesson will equip you with a powerful and general framework for approximating these intractable expectations. You will learn how to formulate the fundamental Monte Carlo estimator, understand its statistical properties, and, crucially, quantify its convergence and error using variance. This technique is not just a theoretical curiosity; it's a practical workhorse that underpins the training of many of the advanced generative models you're aiming to implement, including VAEs and Diffusion Models.

1. The Challenge: Intractable Expectations and the Curse of Dimensionality

In many areas of machine learning, from Bayesian inference to reinforcement learning, we encounter the need to compute an expectation of a function with respect to a probability distribution :

A prime example, directly following our last lesson, is computing the Bayesian evidence (or marginal likelihood), , which is an expectation of the likelihood with respect to the prior.

For simple, one-dimensional problems, we might use classical numerical integration methods like the trapezoidal rule or Simpson's rule. These methods work by discretizing the integration domain into a grid. However, this approach collapses completely in high dimensions—a phenomenon often called the curse of dimensionality. If our data lives in a -dimensional space and we want to place just 10 grid points along each dimension, we would need to evaluate the function at locations. For even a modest , this is evaluations, and for a high-resolution image, can be in the millions. This makes any form of grid-based approximation computationally impossible.

The following video provides a concise overview of why direct computation and grid approximations fail, setting the stage for a sampling-based approach.

Estimating Expectations is Difficult: Why do we need MCMC?

This video from the channel Drawing Distributions excellently motivates the need for sampling-based methods.

Watch the first part, which frames the problem in the context of intractable expectations in Bayesian statistics. Then, pay close attention to the explanation of why grid approximation fails catastrophically in high dimensions. This highlights the inefficiency of exhaustively evaluating the space.


2. The Monte Carlo Estimator: An Elegant Solution

The Monte Carlo method sidesteps the curse of dimensionality with a simple yet profound idea: instead of trying to evaluate the integrand everywhere, we can approximate the integral by taking a random sample of points and averaging the function's value at those points.

Formally, to estimate the expectation , we draw independent and identically distributed (i.i.d.) samples from the distribution . The Monte Carlo estimator of the expectation is then simply the sample mean:

The power of this method comes from the law of large numbers, which guarantees that as , this sample mean converges to the true expectation. More importantly for our purposes, this estimator is unbiased. This means that for any finite , the expected value of our estimator is equal to the true value we are trying to estimate.

Let's prove this. The samples are i.i.d. from , so is the same for all and is equal to the true expectation, which we'll call . Using the linearity of expectation:

The following resource from the excellent "Physically Based Rendering" textbook formalizes this core estimator.

Monte Carlo: Basics - Physically Based Rendering

This text provides a formal, yet clear, definition of the Monte Carlo estimator and its unbiased nature.

In the section titled "2.1.3 The Monte Carlo Estimator", focus on the general form of the estimator (Equation 2.7 in the text) and the accompanying derivation that shows its expectation equals the desired integral. This confirms the unbiasedness property we just discussed.


3. Quantifying the Error: The Variance of the Estimator

Knowing our estimator is unbiased is crucial, but it doesn't tell us how accurate a specific estimate from a finite number of samples is likely to be. For that, we need to analyze its variance. The variance tells us how much the estimator "bounces around" its mean value (the true integral ) over different sets of random samples.

Let's denote the variance of the function itself as . Since the samples are independent, the variance of the sum is the sum of the variances. We can derive the variance of our estimator as follows:

This is a fundamental result. The variance of the Monte Carlo estimator decreases linearly with the number of samples .

The standard error of the estimator, which is the standard deviation of its sampling distribution, is the square root of the variance:

This tells us that to halve the error, we must quadruple the number of samples. This convergence rate might seem slow, but its most important feature is that it is completely independent of the dimension of the integration space. This is why Monte Carlo methods are the tool of choice for high-dimensional integration.

This plot demonstrates the core property of Monte Carlo estimation. The variance of the estimator, shown on the y-axis, decreases as a function of \(1/n\), where \(n\) is the number of samples. The empirical variance (blue), calculated from repeated experiments, closely tracks the theoretical variance (red), which is \(\sigma^2/n\).

In practice, we don't know the true variance , but we can estimate it from our samples using the sample variance:

This allows us to estimate the error of our Monte Carlo result without knowing the true answer.

The following reading elaborates on this error analysis.

Monte Carlo: Basics - Physically Based Rendering

Let's now formally analyze the error and convergence rate of the Monte Carlo estimator.

Please read the section "Error in Monte Carlo Estimators". Focus on the derivation that shows the variance of the estimator is \sigma^2/N. This is the theoretical justification for the 1/\sqrt{N} convergence rate. Also, note the practical definition of sample variance, which allows us to estimate our error from the data itself.


4. A Glimpse of Variance Reduction: Importance Sampling

While the convergence rate is a great asset, the constant term can be a major problem. If the variance of is very large, we might need an astronomical number of samples to achieve acceptable accuracy. This often happens when the function has large values in regions where the probability is small. Plain Monte Carlo will rarely sample these "important" but rare regions, leading to a high-variance estimate.

Importance Sampling is a powerful variance reduction technique that addresses this. The core idea is to stop sampling from and instead use a different, "proposal" distribution that is chosen to over-sample the important regions. To correct for this change of distribution, we re-weight the samples.

The derivation is simple:

The new estimator is:

The terms are called the importance weights. By choosing to be large where is large, we can dramatically reduce the variance of the estimator. This is a vast topic, but the following video gives a great conceptual introduction.

Importance Sampling

This video provides an excellent visual and conceptual walkthrough of Importance Sampling, a key variance reduction technique.

First, watch the explanation of the core trick, which shows how to rewrite the expectation by introducing a new sampling distribution, q(x), and correcting with importance weights. Then, watch the segment starting at the example to see a concrete demonstration of how choosing a good proposal distribution q(x) can drastically reduce the variance of the estimate.


Conclusion

In this lesson, you've added a cornerstone of modern computational statistics and machine learning to your toolkit. You now have a principled method for tackling the intractable integrals that stymie purely analytical approaches.

Key Takeaways:

  • Monte Carlo (MC) estimation approximates intractable expectations by averaging function evaluations over random samples drawn from the relevant probability distribution.
  • The standard MC estimator, , is unbiased, meaning its expected value is the true expectation.
  • The estimator's error, quantified by the standard error, decreases at a rate of . Crucially, this convergence rate is independent of the problem's dimensionality, which is the method's primary advantage over grid-based techniques.
  • The variance of the estimator is , where is the variance of the function being evaluated. This variance can be estimated from the samples themselves, providing a measure of confidence in our result.
  • Techniques like Importance Sampling can significantly reduce the variance of the estimator by intelligently choosing where to draw samples, leading to faster convergence.

We have established how to estimate expectations. But in deep learning, we don't just want to estimate values; we want to optimize them. What happens if an expectation is part of a loss function that we need to minimize via gradient descent? This requires differentiating through the Monte Carlo estimation process.

In our next lesson, we will derive the pathwise gradient estimator, more famously known as the reparameterization trick. This is the key technique that allows us to compute low-variance gradients for stochastic models, unlocking our ability to train a Variational Autoencoder.