Computing Library › Probability Statistics
Probability Statistics

Variance and Standard Deviation

Variance measures how far a random variable spreads around its mean; the standard deviation restores the original units.

Definition

The variance is Var(X) = E[(X − E[X])²], the expected squared deviation from the mean. The standard deviation is its square root, σ = √Var(X), which has the same units as X and is therefore easier to interpret.

Computational form

A convenient identity is Var(X) = E[X²] − (E[X])². It lets you accumulate the mean and the mean of squares in one pass over the data, though for large values it can lose precision — a numerically stable streaming algorithm (Welford's) is preferred.

Scaling and shifting

Adding a constant does not change spread: Var(X + c) = Var(X). Scaling squares the factor: Var(aX) = a² Var(X). For the sum of variables, Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y); if they are independent the covariance term vanishes.

Variance of a sample mean

For n independent draws with variance σ², the sample mean has variance σ²/n, so its standard deviation shrinks like 1/√n. This is why halving Monte Carlo error requires four times as many samples — a fundamental and often frustrating scaling.

Why square the deviations

Squaring makes deviations positive and penalizes large excursions more heavily, and it gives variance clean additivity for independent sums. The cost is sensitivity to outliers, which is why robust alternatives like the median absolute deviation exist for heavy-tailed data.

The sample variance estimates the true variance from data; dividing the summed squared deviations by n − 1 rather than n corrects a downward bias that arises because deviations are taken from the sample mean rather than the unknown true mean. This is Bessel's correction, and it is the reason most software reports the n − 1 version by default.