AnyLearn
All lessons
Mathintermediate

Expectation, Variance, and the CLT

Master the three numbers that summarize any distribution: mean, variance, and standard deviation. Derive linearity of expectation, understand covariance and correlation, then see why the Central Limit Theorem makes the Gaussian unavoidable — with a worked numeric example from scratch.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 10

Expected value: the distribution's center of mass

The expected value (mean) of XX is its probability-weighted average:

E[X]={xxp(x)discretexf(x)dxcontinuous\mathbb{E}[X] = \begin{cases} \displaystyle\sum_x x\, p(x) & \text{discrete} \\ \displaystyle\int_{-\infty}^{\infty} x\, f(x)\, dx & \text{continuous} \end{cases}

Think of it as the center of mass of the PMF/PDF. It need not be a likely value — for a fair die, E[X]=3.5\mathbb{E}[X] = 3.5, which never appears on a roll.

Expected value of a function: E[g(X)]=xg(x)p(x)\mathbb{E}[g(X)] = \sum_x g(x)\, p(x). This is the law of the unconscious statistician (LOTUS) — you integrate g(x)g(x) against the original distribution of XX, no change of variables needed.

Full lesson text

All 10 steps on one page, for reading, reference, and search.

Show

1. Expected value: the distribution's center of mass

The expected value (mean) of XX is its probability-weighted average:

E[X]={xxp(x)discretexf(x)dxcontinuous\mathbb{E}[X] = \begin{cases} \displaystyle\sum_x x\, p(x) & \text{discrete} \\ \displaystyle\int_{-\infty}^{\infty} x\, f(x)\, dx & \text{continuous} \end{cases}

Think of it as the center of mass of the PMF/PDF. It need not be a likely value — for a fair die, E[X]=3.5\mathbb{E}[X] = 3.5, which never appears on a roll.

Expected value of a function: E[g(X)]=xg(x)p(x)\mathbb{E}[g(X)] = \sum_x g(x)\, p(x). This is the law of the unconscious statistician (LOTUS) — you integrate g(x)g(x) against the original distribution of XX, no change of variables needed.

2. Linearity of expectation

The single most useful fact in probability:

E[aX+bY+c]=aE[X]+bE[Y]+c\mathbb{E}[aX + bY + c] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y] + c

This holds regardless of whether XX and YY are independent. No joint distribution needed.

Example: what is the expected number of fixed points (cards in their natural position) in a random shuffle of nn cards? Define Xi=1[card i is fixed]X_i = \mathbf{1}[\text{card } i \text{ is fixed}]. Then E[Xi]=1/n\mathbb{E}[X_i] = 1/n and E ⁣[i=1nXi]=i=1nE[Xi]=n1n=1.\mathbb{E}\!\left[\sum_{i=1}^n X_i\right] = \sum_{i=1}^n \mathbb{E}[X_i] = n \cdot \frac{1}{n} = 1. Always exactly 1, for any nn — a clean result that would be a combinatorial nightmare without linearity.

3. Variance and standard deviation

Variance measures spread around the mean:

Var(X)=E ⁣[(XE[X])2]=E[X2](E[X])2.\text{Var}(X) = \mathbb{E}\!\left[(X - \mathbb{E}[X])^2\right] = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.

The second form — raw second moment minus squared mean — is almost always faster to compute. The standard deviation is σ=Var(X)\sigma = \sqrt{\text{Var}(X)}, which lives in the same units as XX.

Key scaling rules:

  • Var(aX+b)=a2Var(X)\text{Var}(aX + b) = a^2\,\text{Var}(X) — shift has no effect, scale is squared.
  • If X,YX, Y independent: Var(X+Y)=Var(X)+Var(Y)\text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y).
  • If dependent: Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y) + 2\,\text{Cov}(X, Y).

Variance is not linear — a common source of errors in error-propagation and mini-batch gradient analysis.

4. Worked example: rolling two dice

Let XX and YY be independent fair dice (1166). Compute E[X+Y]\mathbb{E}[X + Y] and Var(X+Y)\text{Var}(X + Y).

Step 1. For one fair die: E[X]=16(1+2+3+4+5+6)=3.5\mathbb{E}[X] = \frac{1}{6}(1+2+3+4+5+6) = 3.5.

Step 2. E[X2]=16(1+4+9+16+25+36)=91615.17\mathbb{E}[X^2] = \frac{1}{6}(1+4+9+16+25+36) = \frac{91}{6} \approx 15.17.

Step 3. Var(X)=15.173.52=15.1712.25=2.92\text{Var}(X) = 15.17 - 3.5^2 = 15.17 - 12.25 = 2.92.

Step 4. By linearity: E[X+Y]=3.5+3.5=7\mathbb{E}[X + Y] = 3.5 + 3.5 = 7.

Step 5. By independence: Var(X+Y)=2.92+2.92=5.83\text{Var}(X + Y) = 2.92 + 2.92 = 5.83, so σX+Y=5.832.42\sigma_{X+Y} = \sqrt{5.83} \approx 2.42.

import numpy as np
rolls = np.random.randint(1, 7, size=(10_000_000, 2))
sums = rolls.sum(axis=1)
print(f"E[X+Y] = {sums.mean():.4f}")  # ~7.0000
print(f"Var    = {sums.var():.4f}")   # ~5.8333

5. Covariance and correlation

Covariance quantifies how two RVs move together:

Cov(X,Y)=E[(XE[X])(YE[Y])]=E[XY]E[X]E[Y].\text{Cov}(X, Y) = \mathbb{E}[(X - \mathbb{E}[X])(Y - \mathbb{E}[Y])] = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y].

Cov(X,X)=Var(X)\text{Cov}(X, X) = \text{Var}(X). Covariance is scale-dependent, making it hard to interpret directly.

Pearson correlation normalizes it: ρ(X,Y)=Cov(X,Y)Var(X)Var(Y)[1,1].\rho(X, Y) = \frac{\text{Cov}(X, Y)}{\sqrt{\text{Var}(X)\,\text{Var}(Y)}} \in [-1, 1].

ρ=1\rho = 1: perfect positive linear relationship; ρ=0\rho = 0: uncorrelated (but not necessarily independent); ρ=1\rho = -1: perfect negative.

For a random vector XRd\mathbf{X} \in \mathbb{R}^d, the covariance matrix Σij=Cov(Xi,Xj)\Sigma_{ij} = \text{Cov}(X_i, X_j) is symmetric and positive semi-definite — it appears in Gaussian distributions, PCA, and Kalman filters.

6. The Law of Large Numbers

Let X1,X2,X_1, X_2, \ldots be i.i.d. with mean μ\mu and finite variance σ2\sigma^2. The sample mean is Xˉn=1ni=1nXi\bar{X}_n = \frac{1}{n}\sum_{i=1}^n X_i.

Weak LLN: For any ε>0\varepsilon > 0, P(Xˉnμ>ε)0P(|\bar{X}_n - \mu| > \varepsilon) \to 0 as nn \to \infty.

Proof sketch via Chebyshev: P(Xˉnμ>ε)Var(Xˉn)ε2=σ2nε20P(|\bar{X}_n - \mu| > \varepsilon) \le \frac{\text{Var}(\bar{X}_n)}{\varepsilon^2} = \frac{\sigma^2}{n\varepsilon^2} \to 0.

In ML: Monte Carlo estimators, stochastic gradient descent, empirical risk minimization — all implicitly rely on the LLN. When you average enough noisy gradient estimates, you get close to the true gradient. The LLN tells you convergence happens; the CLT tells you how fast and what the fluctuations look like.

7. The Central Limit Theorem

The most important theorem in statistics:

CLT. If X1,,XnX_1, \ldots, X_n are i.i.d. with mean μ\mu and variance σ2<\sigma^2 < \infty, then Xˉnμσ/ndN(0,1)as n.\frac{\bar{X}_n - \mu}{\sigma / \sqrt{n}} \xrightarrow{d} \mathcal{N}(0, 1) \quad \text{as } n \to \infty.

Three takeaways:

  1. The shape of the original distribution is irrelevant — the sum converges to Gaussian.
  2. The fluctuations shrink as 1/n1/\sqrt{n}, not 1/n1/n. Halving the error requires the data.
  3. The approximation is often usable by n30n \approx 30 for unimodal distributions, but may need n100n \gg 100 for heavy-tailed ones.

This is why Gaussian noise appears in so many models: real noise is typically a sum of many small independent perturbations.

8. Standard error and confidence in practice

The standard error of the mean (SEM) is the standard deviation of Xˉn\bar{X}_n: SE=σn.\text{SE} = \frac{\sigma}{\sqrt{n}}.

In practice σ\sigma is unknown, so you substitute the sample standard deviation s=1n1i(XiXˉ)2s = \sqrt{\frac{1}{n-1}\sum_i (X_i - \bar{X})^2}.

The SEM quantifies estimation uncertainty. A rough 95% interval for μ\mu is Xˉn±1.96SE\bar{X}_n \pm 1.96 \cdot \text{SE} (via the CLT). Comparing two models? Compute the SEM of the accuracy difference — if the interval excludes 0, the difference is statistically detectable at the 5% level.

import numpy as np
from scipy import stats

np.random.seed(42)
data = np.random.exponential(scale=5, size=200)  # non-Gaussian!
xbar = data.mean()
se   = stats.sem(data)
ci   = stats.t.interval(0.95, df=len(data)-1, loc=xbar, scale=se)
print(f"mean={xbar:.2f}, SE={se:.2f}, 95% CI={ci}")

Notice we used a tt-distribution (not normal) because n=200n=200 with unknown σ\sigma — conservative and correct.

9. From i.i.d. samples to the CLT

How repeated averaging turns any distribution into a Gaussian:

flowchart LR
  A["i.i.d. samples X1...Xn from any distribution"]
  B["Sample mean Xbar_n = (1/n) sum Xi"]
  C["E[Xbar_n] = mu, Var(Xbar_n) = sigma^2 / n"]
  D["Standardize: Z_n = (Xbar_n - mu) / (sigma / sqrt(n))"]
  E["CLT: Z_n -> N(0,1) as n -> infinity"]
  F["Confidence intervals and hypothesis tests"]
  A --> B
  B --> C
  C --> D
  D --> E
  E --> F

10. Why Gaussians are everywhere in ML

Beyond the CLT, Gaussians dominate ML for three deeper reasons:

  1. Maximum entropy. Among all distributions with fixed mean μ\mu and variance σ2\sigma^2, N(μ,σ2)\mathcal{N}(\mu, \sigma^2) has the highest differential entropy. It's the "most uncertain" distribution given those two constraints — a natural prior when you know only the first two moments.

  2. Analytic tractability. Gaussians are closed under linear transformations, conditioning, and marginalization. If (X,Y)(X, Y) is jointly Gaussian, XY=yX \mid Y=y is Gaussian with a closed-form mean and variance. This is the engine of Gaussian processes and Kalman filters.

  3. Exponential family. N(μ,σ2)\mathcal{N}(\mu, \sigma^2) is in the exponential family, making maximum-likelihood estimation and Bayesian conjugacy computationally clean.

Every time you assume "Gaussian noise," you're implicitly invoking the CLT, maximum entropy, and analytic convenience simultaneously.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. Let $X$ be a discrete RV with $P(X=-1) = 0.5$ and $P(X=3) = 0.5$. What is $\text{Var}(X)$?
    • 4
    • 2
    • 8
    • 1
  2. You have i.i.d. samples $X_1, \ldots, X_{100}$ with $\mathbb{E}[X]=2$ and $\text{Var}(X)=9$. What is the standard error of $\bar{X}_{100}$?
    • 0.09
    • 0.3
    • 0.9
    • 3
  3. Which statement about linearity of expectation is correct?
    • $\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y]$ always holds.
    • $\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y]$ holds only when $X$ and $Y$ are independent.
    • $\mathbb{E}[X + Y] = \mathbb{E}[X] + \mathbb{E}[Y]$ holds for all $X$ and $Y$, dependent or not.
    • $\mathbb{E}[X^2] = (\mathbb{E}[X])^2$ always holds.
  4. The CLT says that $\bar{X}_n$ converges to a Gaussian as $n \to \infty$. Which is the correct rate of convergence of the standard deviation of $\bar{X}_n$?
    • It shrinks at rate $1/n$.
    • It shrinks at rate $1/\sqrt{n}$.
    • It shrinks at rate $1/\log n$.
    • It shrinks at rate $1/n^2$.
  5. Two RVs $X$ and $Y$ have $\text{Cov}(X, Y) = 0$. What can you conclude?
    • $X$ and $Y$ are independent.
    • $X$ and $Y$ are uncorrelated, but may still be dependent.
    • $\text{Var}(X+Y) = \text{Var}(X)\,\text{Var}(Y)$.
    • $\mathbb{E}[X] = \mathbb{E}[Y]$.

Related lessons

Math
intermediate

Counting Without Listing

Combinatorics answers how many arrangements exist without producing any of them, which is what makes password strength, hash collisions and search-space size computable at all. This lesson builds the product rule, permutations, combinations, inclusion-exclusion and the pigeonhole principle, then applies them to problems where intuition is reliably wrong.

10 steps·~15 min
Math
intermediate

Gradients, Jacobians, and Hessians: Calculus in Many Dimensions

One derivative becomes three objects once a function has many inputs and many outputs. This lesson builds the gradient, the Jacobian and the Hessian, shows what each one actually tells you, and explains why curvature decides how many steps an optimiser needs and why nobody ever writes the Hessian down.

10 steps·~15 min
Math
intermediate

The Derivative Is a Local Linear Model

Machine learning uses the derivative as a search strategy, not a symbolic exercise. This lesson builds it as the best local linear approximation, derives the gradient descent update from it, and shows why estimating derivatives numerically loses half your digits and costs one function evaluation per parameter.

10 steps·~15 min
Business
intermediate

Peeking: How Watching Your Experiment Ruins It

The most natural behaviour in experimentation, checking results daily and stopping when they look significant, quietly destroys the statistical guarantee everyone thinks they have. This lesson shows the peeking mechanism with honest arithmetic, then the fixes: fixed-horizon discipline, group sequential designs, and always-valid inference.

7 steps·~11 min