Lesson 8 of 10 · Probability and Statistics

Variance and standard deviation

The variance measures the spread of values around their mean, and the standard deviation is its square root in the same units as the data; feature normalization and bias–variance analysis start from them.

Mathematics for machine learning Difficulty: Intermediate Study time: 40 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

Two equal means can hide very different data. Variance measures the average squared distance from the mean, and theStandard deviation is its square root.

Learning objectives

  • Computing the population and sample variance and standard deviation
  • Using the shortcut formula \(\mathbb{E}[X^2] - \mathbb{E}[X]^2\)
  • Knowing the effect of linear transformations on the variance
  • Applying standardization

2 Why study this topic?

Feature standardization divides by the standard deviation, normalization layers (batch/layer normalization) compute the mean and variance inside the network, and decomposing a model's error into bias and variance explains overfitting.

3 Theoretical explanation

Definition

\[\operatorname{Var}(X) = \mathbb{E}\big[(X-\mu)^2\big] = \mathbb{E}[X^2] - \mu^2,\quad \sigma = \sqrt{\operatorname{Var}(X)}\]

Sample variance

For a sample of n values we divide by \(n-1\) instead of n (Bessel's correction) to get an unbiased estimate of the population variance: \(s^2 = \frac{1}{n-1}\sum (x_i - \bar x)^2\).

Properties

  • \(\operatorname{Var}(aX + b) = a^2\operatorname{Var}(X)\): shifting has no effect, and scaling multiplies the variance by the square of the factor.
  • For independent variables: \(\operatorname{Var}(X+Y) = \operatorname{Var}(X) + \operatorname{Var}(Y)\).
  • The variance of the mean of n independent, identically distributed variables is \(\sigma^2/n\), which is why gradient noise decreases as the batch grows.

Standardization

\(x' = (x - \mu)/\sigma\) makes the mean 0 and the standard deviation 1, so the features share a scale and gradient descent converges better.

4 Mathematical definitions

Variance
The expected value of the squared deviation of a variable from its mean.
\[\operatorname{Var}(X) = \mathbb{E}[(X-\mu)^2]\]
Standard deviation
The square root of the variance; it has the same unit as the data.
\[\sigma = \sqrt{\operatorname{Var}(X)}\]
Sample variance
An unbiased estimate of the population variance obtained by dividing by n−1.
\[s^2 = \frac{1}{n-1}\sum_i (x_i-\bar x)^2\]
Variance
The expected value of the squared deviation of a variable from its mean; it measures spread.
\[\operatorname{Var}(X)=\mathbb{E}[(X-\mu)^2]\]
Standard deviation
The square root of the variance; it measures spread in the same units as the data.
\[\sigma=\sqrt{\operatorname{Var}(X)}\]

5 The equations

\[\operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2\]
The shortcut formula
\[\operatorname{Var}(aX+b) = a^2\operatorname{Var}(X)\]
Effect of a linear transformation
\[\hat{x} = \frac{x-\mu_B}{\sqrt{\sigma_B^2+\epsilon}}\]
Batch normalization

6 Step-by-step worked example

The variance of a dataset

Problem: Compute the mean, the (population) variance and the standard deviation of the values 2, 4, 4, 4, 5, 5, 7, 9.

  1. The mean: \(40/8 = 5\).
  2. The squared deviations: 9, 1, 1, 1, 0, 0, 4, 16, which sum to 32.
  3. The population variance: \(32/8 = 4\), and the standard deviation is 2.
  4. The sample variance (dividing by 7): \(32/7 \approx 4.5714\).

Result: \(\mu = 5\), \(\sigma^2 = 4\), \(\sigma = 2\)

7 Practical application in AI

  • Feature standardization: Transforming the features to zero mean and unit variance before training.
  • Normalization inside the network: Batch Normalization normalizes a layer's outputs with the batch mean and variance.
  • Batch size: The variance of the gradient estimate is inversely proportional to the batch size.

8 Interactive lab linked to the lesson

A probability calculator, the normal and binomial distributions with plots, descriptive statistics (mean, median, mode, variance and standard deviation), correlation, random sampling, and a Bayes' theorem calculator.

Open the Probability & Statistics Lab

9 Practice exercises

Review questions for reflection

  • Why do we divide by n−1 in the sample variance?
  • How does the variance of the average gradient change if we make the batch four times larger?

Common mistakes

  • Forgetting to square the factor in \(\operatorname{Var}(aX)\).
  • Confusing division by n with division by n−1.
  • Adding the variances of two correlated variables without the covariance term.

Practice 6 questions from the question bank for this lesson

10 Lesson summary

  • The variance is the average squared distance from the mean.
  • \(\operatorname{Var}(aX+b) = a^2\operatorname{Var}(X)\).
  • Standardization divides by the standard deviation.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.