Textbook
A random variable $X$ has finite second moment, $\mathbb E[X^2]<\infty$. Its variance is the expected squared deviation from its expectation. This is a nonnegative measure of distributional spread.
$$\operatorname{Var}(X):=\mathbb E[(X-\mathbb E[X])^2]=\mathbb E[X^2]-(\mathbb E[X])^2.$$
Other notation includes $D(X)$ and $\sigma_X^2$. Standard deviation is $\sigma_X=\sqrt{\operatorname{Var}(X)}$. If $X$ is measured in seconds, variance uses square seconds while standard deviation uses seconds.
For equally likely values 2, 4, and 6, the expectation is 4 and squared deviations are 4, 0, and 4. Variance is therefore $8/3$ and standard deviation is $\sqrt{8/3}\approx1.633$. A constant variable has only zero deviations.
Changing scale to $Y=aX+b$ gives variance $a^2\operatorname{Var}(X)$: a shift leaves deviations unchanged and scaling is squared. Estimating an unknown variance from a sample is a separate task; its denominator cannot be selected by visual resemblance.
The two variance formulas agree by expanding the square: $\mathbb E[X^2-2\mu X+\mu^2]=\mathbb E[X^2]-2\mu\mathbb E[X]+\mu^2$, where $\mu=\mathbb E[X]$. Since $\mu$ is constant, the result is $\mathbb E[X^2]-\mu^2$. Nonnegativity is clearer in the first form: every squared deviation is nonnegative. In a finite model, zero variance means that every positive-probability value equals the expectation; zero-weight outcomes do not affect the result.
Definitions and notation: Mathematics for Machine Learning.
Related concepts: Expected value: the mean of a distribution, Sample variance and the estimated mean.
In plain language
The deviations of 2 and 6 from center 4 sum to zero even though the spread remains. Squaring prevents opposite signs from arranging a fictitious truce: variance averages those squares with probability weights. The center is the distribution’s own expectation; shifting every value ten meters leaves the spread unchanged, while doubling distances multiplies variance by four. The result has squared units. Taking its square root restores the original units, but standard deviation remains different from mean absolute error.
Example
The constructed distribution gives 2, 4, and 6 equally often in its probability model. The example develops the calculation of variance.
Variance: the mean squared deviation
Distribution center
The expectation is 4.
Step 1 of 4: Distribution center
Read all steps
Distribution center
The expectation is 4.
- Values
- 246
- Center
- 4current
Deviations from the center
Deviations are −2, 0, and 2; their mean is zero.
- Deviations
- −2current02current
Squared deviations
Squared deviations cannot cancel.
- Squares
- 4included04included
Mean squared deviation
Each square has weight 1/3, giving variance 8/3.
- Variance; standard deviation
- 8/3included√(8/3)current
A weighted cross-check gives $\mathbb E[X^2]=(4+16+36)/3=56/3$, while the squared expectation is 16. Subtracting gives $56/3-16=8/3$, matching the stepwise calculation. These are two ways to calculate one quantity, not two optional variances. Moving the square outside the first expectation changes the result: $\mathbb E[X^2]$ and $(\mathbb E[X])^2$ differ by precisely the variance, so treating them as interchangeable erases the spread.
A spread formula requires a specified center, weights, and units of the result.