Expectation and Variance

Formal
(Expectation)

The expectation (mean) of a random variable XX is its probability-weighted average value:

E[X]={xxpX(x)discretexfX(x)dxcontinuous(1)\mathbb{E}[X] = \begin{cases} \displaystyle\sum_x x\,p_X(x) & \text{discrete} \\[6pt] \displaystyle\int_{-\infty}^{\infty} x\,f_X(x)\,dx & \text{continuous} \end{cases} \tag{1}

Key property — Linearity of Expectation: for any RVs X,YX, Y and scalars a,ba, b:

E[aX+bY]=aE[X]+bE[Y].(2)\mathbb{E}[aX + bY] = a\,\mathbb{E}[X] + b\,\mathbb{E}[Y]. \tag{2}

This holds even when XX and YY are dependent. It is one of the most powerful and frequently used results in probability.

(Variance)

The variance measures how spread out XX is around its mean μ=E[X]\mu = \mathbb{E}[X]:

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

The standard deviation σX=Var(X)\sigma_X = \sqrt{\text{Var}(X)} is in the same units as XX.

Key properties:

Var(aX+b)=a2Var(X).(4)\text{Var}(aX + b) = a^2\,\text{Var}(X). \tag{4}

For independent X,YX, Y: Var(X+Y)=Var(X)+Var(Y)\text{Var}(X + Y) = \text{Var}(X) + \text{Var}(Y).

(Law of the Unconscious Statistician)

For any function gg:

E[g(X)]=g(x)fX(x)dx.(5)\mathbb{E}[g(X)] = \int g(x)\,f_X(x)\,dx. \tag{5}

This means you don’t need to find the distribution of g(X)g(X) first — just integrate g(x)g(x) against the density of XX.

Intuition

Think of a production service emitting a metric (e.g., request latency in ms):

  • Expectation is the long-run average — the number you’d see on a p50 or mean dashboard panel after many requests.
  • Variance is the jitter — how far individual readings deviate from that average. High variance means unpredictable, bursty behaviour.
  • Standard deviation is variance in interpretable units: “on average, latency deviates from the mean by σ\sigma ms.”

Linearity of expectation is like the additive property of latency in a call chain: E[total latency]=E[service A]+E[service B]\mathbb{E}[\text{total latency}] = \mathbb{E}[\text{service A}] + \mathbb{E}[\text{service B}], regardless of whether the two services are correlated.

ML Applications
  • Loss functions — almost every loss function is an expectation: L(θ)=E(x,y)[(fθ(x),y)]\mathcal{L}(\theta) = \mathbb{E}_{(x,y)}[\ell(f_\theta(x), y)]. Minimising training loss approximates minimising the true expected loss.
  • Bias–variance tradeoff — the mean squared error of an estimator decomposes as MSE=Bias2+Variance\text{MSE} = \text{Bias}^2 + \text{Variance}. Regularisation trades higher bias for lower variance to reduce overall error.
  • Stochastic gradient descent — computes a noisy, unbiased estimate ^L\hat{\nabla}\mathcal{L} of the true gradient E[L]\mathbb{E}[\nabla\mathcal{L}]. The variance of this estimate determines how large the learning rate can be.
  • Moments in distributions — the mean and variance are the first two moments. They fully characterise a Gaussian distribution, and approximating a complex distribution by matching its moments is a common technique.
Connections
  • #type/definition
  • #probability
  • #expectation
  • #variance