AI State of the Art
Math/Probability and Statistics/12 min
Section 327

Expectation, Variance, and Moments

Listen as a podcast β†’

Summarizing a distribution in a few numbers

A probability distribution contains an enormous amount of information. But to reason efficiently, we need compact summaries: where is the "center"? What is the "spread"? Are the variables related to each other? These are the moments of a distribution, and they are at the heart of analyzing ML models.

Expectation: the expected average value

Definition

The expectation E[X]E[X] is the weighted average of all possible values, each weighted by its probability:

  • Discrete case: E[X]=βˆ‘xxβ‹…P(X=x)E[X] = \sum_x x \cdot P(X = x)
  • Continuous case: E[X]=βˆ«βˆ’βˆž+∞xβ‹…f(x) dxE[X] = \int_{-\infty}^{+\infty} x \cdot f(x) \, dx

Analogy

Imagine a loaded die. If the 6 comes up twice as often as the other faces, the expectation is no longer 3.5 but shifts upward. The expectation is the "center of gravity" of the distribution: if you placed weights on a ruler at each possible value, proportional to their probability, the balance point would be exactly the expectation.

Essential properties

  • Linearity: E[aX+bY]=aE[X]+bE[Y]E[aX + bY] = aE[X] + bE[Y], always, even if XX and YY are not independent
  • Expectation of a constant: E[c]=cE[c] = c
  • Expectation of a function: E[g(X)]=βˆ‘xg(x)P(X=x)E[g(X)] = \sum_x g(x) P(X = x) (law of the unconscious statistician)

In ML

Expectation appears everywhere:

  • The average loss over a batch is an empirical expectation: L^=1nβˆ‘i=1nβ„“(yi,y^i)\hat{L} = \frac{1}{n} \sum_{i=1}^n \ell(y_i, \hat{y}_i)
  • Training with SGD estimates the expected gradient using a random sample
  • The ELBO of VAEs is an expectation under the variational distribution

Variance: measuring the spread

Definition

Variance measures how far values stray from the mean:

Var(X)=E[(Xβˆ’E[X])2]=E[X2]βˆ’(E[X])2\text{Var}(X) = E[(X - E[X])^2] = E[X^2] - (E[X])^2

The second formula is often more practical for calculations.

Analogy

Two archers each shoot 100 arrows. Both hit the center on average (same expectation). But the first archer's arrows are clustered near the center, while the second's are scattered across the entire target. The second has a much larger variance.

Properties

  • Non-negative: Var(X)β‰₯0\text{Var}(X) \geq 0, and Var(X)=0\text{Var}(X) = 0 only if XX is constant
  • Scaling: Var(aX)=a2Var(X)\text{Var}(aX) = a^2 \text{Var}(X) (the square matters!)
  • Sum of independent variables: Var(X+Y)=Var(X)+Var(Y)\text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y) if XX and YY are independent

Standard deviation: the square root of variance

Οƒ=Var(X)\sigma = \sqrt{\text{Var}(X)}

The standard deviation shares the usefulness of variance for measuring spread, but with a practical advantage: it's expressed in the same units as XX. If XX is in meters, Οƒ\sigma is in meters (variance would be in square meters).

In ML, when we say "the weights are initialized with a standard deviation of 0.02," we mean Οƒ=0.02\sigma = 0.02 in W∼N(0,0.022)W \sim \mathcal{N}(0, 0.02^2).

Covariance: relationships between variables

Definition

Covariance measures how two variables vary together:

Cov(X,Y)=E[(Xβˆ’E[X])(Yβˆ’E[Y])]=E[XY]βˆ’E[X]E[Y]\text{Cov}(X, Y) = E[(X - E[X])(Y - E[Y])] = E[XY] - E[X]E[Y]

Interpretation

  • Cov(X,Y)>0\text{Cov}(X, Y) > 0: when XX increases, YY tends to increase too
  • Cov(X,Y)<0\text{Cov}(X, Y) < 0: when XX increases, YY tends to decrease
  • Cov(X,Y)=0\text{Cov}(X, Y) = 0: no linear relationship (but there can be a nonlinear relationship!)

Correlation: normalized covariance

ρ(X,Y)=Cov(X,Y)ΟƒXΟƒY\rho(X, Y) = \frac{\text{Cov}(X, Y)}{\sigma_X \sigma_Y}

Correlation is always between βˆ’1-1 and 11, making it easier to interpret than raw covariance.

Covariance matrix

For a random vector

When you have a vector X=(X1,X2,…,Xd)\mathbf{X} = (X_1, X_2, \ldots, X_d), the covariance matrix Ξ£\boldsymbol{\Sigma} gathers all pairwise covariances:

Ξ£ij=Cov(Xi,Xj)\boldsymbol{\Sigma}_{ij} = \text{Cov}(X_i, X_j)

It's a dΓ—dd \times d matrix, symmetric and positive semi-definite.

Geometric properties

The covariance matrix describes the "shape" of the distribution:

  • Its eigenvalues give the variance in each principal direction
  • Its eigenvectors give the axes of that shape

In ML: PCA

Principal Component Analysis (PCA) consists exactly of diagonalizing the covariance matrix. The eigenvectors associated with the largest eigenvalues are the directions of greatest variance in the data. Projecting onto these directions lets you reduce dimensionality while keeping the maximum amount of information.

In ML: multivariate Gaussian

The multivariate Gaussian is entirely defined by its mean vector and covariance matrix. That's why these two quantities are enough to describe the latent space of a VAE.

The bias-variance tradeoff

The central problem of ML

When you train a model, the prediction error decomposes into three terms:

E[(f^(x)βˆ’y)2]=Bias2+Variance+IrreducibleΒ noiseE[(\hat{f}(x) - y)^2] = \text{Bias}^2 + \text{Variance} + \text{Irreducible noise}

Four shooting targets in a two-by-two grid. Columns: low variance and high variance. Rows: low bias and high bias. The hits are tight on the centre, scattered around the centre, tight but off-centre, or scattered and off-centre.
Each dot is the model obtained from a different training set, and the red centre is the true function. Bias is how far the cloud of dots sits from the centre, variance is how spread out the cloud is. The two errors are independent: you can be tight but off-target, or centred but scattered.Source : CS4780 course, Cornell University

Bias: systematic error

Bias measures how much the model is wrong on average. A model that's too simple (a straight line for curved data) has high bias. It systematically misses the target, regardless of the training data.

Bias=E[f^(x)]βˆ’f(x)\text{Bias} = E[\hat{f}(x)] - f(x)

Variance: sensitivity to data

Variance measures how much predictions change when you change the training data. A model that's too complex (a degree-100 polynomial for 10 data points) has high variance. It fits the training data perfectly but changes drastically with a slightly different dataset.

The tradeoff

  • Simple model: high bias, low variance (underfitting)
  • Complex model: low bias, high variance (overfitting)
  • Optimal model: the right balance between the two
Plot of model risk against model complexity. Squared bias decreases, variance increases, and their sum, the test error, forms a U-shaped curve whose minimum marks the optimal complexity.
The two terms of the decomposition move in opposite directions as the model gets more complex: squared bias goes down, variance goes up. Their sum, the test error, therefore passes through a minimum, which is the balance point described above.Source : Data 100, UC Berkeley, Model Bias, Variance and Inference

Practical translation

TechniqueEffect on biasEffect on variance
Increase model sizeDecreasesIncreases
Regularization (L2, dropout)Slightly increasesDecreases
More training dataUnchangedDecreases
Ensemble methods (bagging)UnchangedDecreases
BoostingDecreasesSlightly increases

The deep learning paradox

Classical theory predicts that very large models should have enormous variance and overfit severely. Yet, overparameterized neural networks often generalize very well. This phenomenon, sometimes called "double descent," is an active research topic. The current intuition is that gradient descent acts as an implicit regularizer, favoring "simple" solutions even in an immense parameter space.

Test error curve as a function of ResNet18 width. The curve falls, rises to a peak around width 10, then falls steadily again. Two dashed arrows show the prediction of classical statistics (rising) and of modern ML (monotone decrease).
The blue curve is what is actually measured as a ResNet18 is widened. The classical bias-variance U shows up first, but past the peak the test error starts falling again even though the model only keeps growing. That second descent is what double descent refers to.Source : Deep Double Descent (Nakkiran et al., OpenAI), via Anish Acharya

Key takeaways

  • Expectation is the expected average value, fundamental for defining loss functions
  • Variance measures spread, essential for understanding the reliability of estimates
  • Covariance captures linear relationships between variables
  • The covariance matrix is at the heart of PCA and Gaussian modeling
  • The bias-variance tradeoff explains why a model that's neither too simple nor too complex is necessary
  • Deep learning challenges the classical view of this tradeoff with the double descent phenomenon

Where this is used. The standard deviation of a group of scores sits at the heart of GRPO, and the bias-variance tradeoff is the subject of overfitting.