Skip to main content

Welcome to a Grasp lesson

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

Lesson 27

Understanding Expectations, Variances, and Covariances in Stochastic Systems

Recommended resources

Welcome back. In our previous lesson, we established a formal language for randomness using probability spaces and random variables. We saw a concrete application of expectation in resolving the training-inference mismatch in dropout. That was our first glimpse into how probabilistic thinking directly informs deep learning engineering.

Today, we will deepen this understanding by exploring the crucial concepts of variance and covariance. Our objective is to interpret expectations, variances, and covariances in the contexts of Monte Carlo estimators and optimizer noise. You'll see how these statistical "moments" are not just theoretical constructs, but the very tools we use to approximate intractable quantities in generative models and to control the chaotic dynamics of training deep neural networks.

1. Moments of a Distribution: Beyond the Average

While the expectation, or first moment, tells us the center of mass of a distribution, it doesn't capture the full picture. To describe a random variable's behavior more completely, we need to consider higher moments, primarily the second.

Variance, the second central moment, quantifies the spread or dispersion of a random variable around its mean. For a random variable with mean , the variance is defined as:

A useful identity, which you have likely seen before, is . This shows that the variance is the difference between the second raw moment () and the square of the first moment. In essence, it measures the expected squared distance from the mean. A low variance implies the variable's outcomes are tightly clustered around the mean, while a high variance indicates they are spread out.

Covariance extends this idea to two random variables, and , with means and . It measures their joint variability:

The sign of the covariance tells us the direction of the linear relationship:

  • Positive covariance: and tend to be above their respective means simultaneously.
  • Negative covariance: When is above its mean, tends to be below its mean, and vice-versa.
  • Zero covariance: There is no linear relationship. Note that statistical independence implies zero covariance, but the reverse is not generally true.

These definitions are the bedrock for the applied concepts we will explore next.


2. Monte Carlo Estimators: Expectation in Practice

In deep learning, especially in generative modeling, we frequently encounter expectations that are impossible to compute analytically. A classic example is finding the marginal likelihood of a data point in a Variational Autoencoder (VAE), which involves integrating over all possible latent codes : . If is high-dimensional, this integral is intractable.

This is where Monte Carlo methods come in. The core idea is to approximate an expectation with a sample average. The "Mutual Information" channel provides an excellent introduction to this idea.

Importance Sampling

This video introduces the core concept of Monte Carlo estimation: approximating an intractable integral (an expectation) with an average of samples. It also introduces the critical properties of the resulting estimator, namely its bias and variance.

Watch the first part of the video, from the introduction to Monte Carlo methods. Pay close attention to: The setup: We want to compute \mathbb{E}_{p(x)}[f(x)]. The approximation: We draw n samples x_i \sim p(x) and compute the sample mean \frac{1}{n} \sum_{i=1}^n f(x_i). The properties of the estimator s: It's a random variable itself, it's unbiased (\mathbb{E}[s] is the true expectation), and its variance is Var(f(x))/n.

Let's formalize this using the definitions from the "Bounded Rationality" blog post.

Importance Sampling and Estimating Marginal Likelihood in Variational Autoencoders | Bounded Rationality

This article section provides the mathematical formulation for the Monte Carlo approximation of an expectation, grounded in the law of large numbers.

Please read the first subsection, "A Brief Review of Monte Carlo Simulation". This recaps the core idea you saw in the video, presenting the approximation in its formal mathematical notation (Equation 2).

The key takeaways from these resources are fundamental to all of generative modeling:

  1. The Estimator: The sample mean is called a Monte Carlo estimator of the true expectation .
  2. Unbiasedness: The estimator is unbiased, meaning . On average, our estimate is correct.
  3. Variance: The quality of our estimate is determined by its variance: . This is the crucial part: the variance of our estimate is inversely proportional to the number of samples . To get a more reliable estimate (lower variance), we need more samples. The Central Limit Theorem, which you're familiar with, tells us that for large , the distribution of our estimate will be approximately Gaussian, centered at the true mean with the variance we just defined.

This simple yet powerful technique is the workhorse for approximating intractable loss functions, gradients, and evaluation metrics across modern deep learning.


3. Optimizer Noise as Gradient Variance

We can now re-examine Stochastic Gradient Descent (SGD) through this lens. When we compute the gradient of the loss on a mini-batch, we are performing Monte Carlo estimation.

The full-dataset gradient is the average of the gradients for each individual data point:

Calculating this is computationally expensive for large . Instead, SGD computes a gradient on a small, random mini-batch of size :

This is a Monte Carlo estimator for . As we've established, it's an unbiased estimator (), but it has non-zero variance. This variance is the "optimizer noise" we've spoken of.

Advanced optimizers like Adam are designed explicitly to handle this noise by estimating the moments of the gradient's distribution. The following video provides a fantastic overview.

Introduction to Deep Learning - 5.Scaling Optimization and Stochastic Gradient Descent (Summer 2020)

This lecture from the Introduction to Deep Learning series at TUM explains how optimizers beyond vanilla SGD are designed to manage the noisy gradient estimates that arise from mini-batch training. It explicitly frames them as estimators of the gradient's moments.

Please watch the following segments: SGD as an approximation: First, watch the segment on Stochastic Gradient Descent. This frames the mini-batch gradient as a random subset used to approximate the true gradient. The trade-off between batch size, computational cost, and the "noisiness" (variance) of the gradient estimate is key here. Momentum (First Moment): Next, watch the explanation of momentum. The core idea is to accumulate an exponentially weighted average of past gradients. This is an estimate of the first moment (mean) of the gradient distribution. It helps accelerate in directions of consistent gradients and dampens oscillations in high-variance directions. RMSProp (Second Moment): Now, see how RMSProp takes this further by watching the segment from its introduction. RMSProp divides the learning rate by an exponentially decaying average of the squared gradients. The lecturer correctly points out that this is effectively computing the uncentered second moment, which is directly related to the variance of the gradients. It dampens updates for parameters whose gradients show high variance. Adam (Adaptive Moment Estimation): Finally, watch the segment on the Adam optimizer. As its name implies, Adam combines the two ideas: it maintains estimates of both the first moment (the mean, like momentum) and the second moment (the uncentered variance, like RMSProp) to adapt the learning rate for each parameter individually.

To summarize the brilliant logic of these optimizers:

  • Vanilla SGD uses a noisy, but unbiased, estimate of the gradient at each step.
  • Momentum tracks the mean of the gradient's recent history (the first moment) to smooth out the noise and build velocity in consistent directions.
  • RMSProp tracks the uncentered variance of the gradient's recent history (the second moment) to scale down updates in noisy, oscillating directions.
  • Adam does both, creating an adaptive learning rate for each parameter based on its own estimated gradient distribution statistics. It is the quintessential example of interpreting and leveraging the expectation and variance of a Monte Carlo estimator (the mini-batch gradient) to improve performance.

Conclusion

In this lesson, we moved from basic definitions to powerful applications. We've seen how the fundamental statistical concepts of expectation and variance are not mere academic exercises, but are actively employed at the heart of deep learning.

Key Takeaways:

  • Variance and covariance are essential for describing the spread of a distribution and the relationship between random variables.
  • Monte Carlo estimators are our primary tool for approximating intractable expectations. Their accuracy is governed by their variance, which we can reduce by increasing the number of samples.
  • The "noise" in stochastic optimizers is precisely the variance of the mini-batch gradient estimator.
  • Adaptive optimizers like Adam explicitly estimate the first (mean) and second (variance) moments of the gradient distribution to navigate the loss landscape more intelligently, dampening updates in high-variance directions and accelerating in low-variance ones.

In our next lesson, we will continue building our probabilistic toolkit by exploring the Laws of Total Expectation and Variance. These laws are indispensable for reasoning about the hierarchical and latent-variable models that are foundational to many advanced generative techniques.