# Variance

In probability theory and statistics, variance is the expected value of the squared deviation from the mean of a random variable. The standard deviation (SD) is obtained as the square root of the variance. Variance is a measure of dispersion, meaning it is a measure of how far a set of numbers is spread out from their average value. It is the second central moment of a distribution, and the covariance of the random variable with itself, and it is often represented by $\sigma ^{2}$, $s^{2}$, $\operatorname {Var} (X)$, $V(X)$, or $\mathbb {V} (X)$.
An advantage of variance as a measure of dispersion is that it is more amenable to algebraic manipulation than other measures of dispersion such as the expected absolute deviation; for example, the variance of a sum of uncorrelated random variables is equal to the sum of their variances. A disadvantage of the variance for practical applications is that, unlike the standard deviation, its units differ from the random variable, which is why the standard deviation is more commonly reported as a measure of dispersion once the calculation is finished. Another disadvantage is that the variance is not finite for many distributions.
There are two distinct concepts that are both called "variance". One, as discussed above, is part of a theoretical probability distribution and is defined by an equation. The other variance is a characteristic of a set of observations. When variance is calculated from observations, those observations are typically measured from a real-world system. If all possible observations of the system are present, then the calculated variance is called the population variance. Normally, however, only a subset is available, and the variance calculated from this is called the sample variance. The variance calculated from a sample is considered an estimate of the full population variance. There are multiple ways to calculate an estimate of the population variance, as discussed in the section below.
The two kinds of variance are closely related. To see how, consider that a theoretical probability distribution can be used as a generator of hypothetical observations. If an infinite number of observations are generated using a distribution, then the sample variance calculated from that infinite set will match the value calculated using the distribution's equation for variance. Variance has a central role in statistics, where some ideas that use it include descriptive statistics, statistical inference, hypothesis testing, goodness of fit, and Monte Carlo sampling.

## Definition

The variance of a random variable $X$ is the expected value of the squared deviation from the mean of $X$, $\mu =\operatorname {E} [X]$: $\operatorname {Var} (X)=\operatorname {E} \left[(X-\mu )^{2}\right].$ This definition encompasses random variables that are generated by processes that are discrete, continuous, neither, or mixed. The variance can also be thought of as the covariance of a random variable with itself:
$\operatorname {Var} (X)=\operatorname {Cov} (X,X).$ The variance is also equivalent to the second cumulant of a probability distribution that generates $X$. The variance is typically designated as $\operatorname {Var} (X)$, or sometimes as $V(X)$ or $\mathbb {V} (X)$, or symbolically as $\sigma _{X}^{2}$ or simply $\sigma ^{2}$ (pronounced "sigma squared"). The expression for the variance can be expanded as follows: ${\begin{aligned}\operatorname {Var} (X)&=\operatorname {E} \left[{\left(X-\operatorname {E} [X]\right)}^{2}\right]\\[4pt]&=\operatorname {E} \left[X^{2}-2X\operatorname {E} [X]+\operatorname {E} [X]^{2}\right]\\[4pt]&=\operatorname {E} \left[X^{2}\right]-2\operatorname {E} [X]\operatorname {E} [X]+\operatorname {E} [X]^{2}\\[4pt]&=\operatorname {E} \left[X^{2}\right]-2\operatorname {E} [X]^{2}+\operatorname {E} [X]^{2}\\[4pt]&=\operatorname {E} \left[X^{2}\right]-\operatorname {E} [X]^{2}\end{aligned}}$
In other words, the variance of X is equal to the mean of the square of X minus the square of the mean of X. This equation should not be used for computations using floating-point arithmetic, because it suffers from catastrophic cancellation if the two components of the equation are similar in magnitude. For other numerically stable alternatives, see algorithms for calculating variance.

### Discrete random variable

If the generator of random variable $X$ is discrete with probability mass function $x_{1}\mapsto p_{1},x_{2}\mapsto p_{2},\ldots ,x_{n}\mapsto p_{n}$, then
$\operatorname {Var} (X)=\sum _{i=1}^{n}p_{i}\cdot {\left(x_{i}-\mu \right)}^{2},$
where $\mu$ is the expected value. That is,
$\mu =\sum _{i=1}^{n}p_{i}x_{i}.$
(When such a discrete weighted variance is specified by weights whose sum is not 1, then one divides by the sum of the weights.)
The variance of a collection of $n$ equally likely values can be written as
$\operatorname {Var} (X)={\frac {1}{n}}\sum _{i=1}^{n}(x_{i}-\mu )^{2}$
where $\mu$ is the average value. That is,
$\mu ={\frac {1}{n}}\sum _{i=1}^{n}x_{i}.$
The variance of a set of $n$ equally likely values can be equivalently expressed, without directly referring to the mean, in terms of squared deviations of all pairwise squared distances of points from each other:
$\operatorname {Var} (X)={\frac {1}{n^{2}}}\sum _{i=1}^{n}\sum _{j=1}^{n}{\frac {1}{2}}{\left(x_{i}-x_{j}\right)}^{2}={\frac {1}{n^{2}}}\sum _{i}\sum _{j>i}{\left(x_{i}-x_{j}\right)}^{2}.$

### Absolutely continuous random variable

If the random variable $X$ has a probability density function $f(x)$, and $F(x)$ is the corresponding cumulative distribution function, then
${\begin{aligned}\operatorname {Var} (X)=\sigma ^{2}&=\int _{\mathbb {R} }{\left(x-\mu \right)}^{2}f(x)\,dx\\[4pt]&=\int _{\mathbb {R} }x^{2}f(x)\,dx-2\mu \int _{\mathbb {R} }xf(x)\,dx+\mu ^{2}\int _{\mathbb {R} }f(x)\,dx\\[4pt]&=\int _{\mathbb {R} }x^{2}\,dF(x)-2\mu \int _{\mathbb {R} }x\,dF(x)+\mu ^{2}\int _{\mathbb {R} }\,dF(x)\\[4pt]&=\int _{\mathbb {R} }x^{2}\,dF(x)-2\mu \cdot \mu +\mu ^{2}\cdot 1\\[4pt]&=\int _{\mathbb {R} }x^{2}\,dF(x)-\mu ^{2},\end{aligned}}$
or equivalently,
$\operatorname {Var} (X)=\int _{\mathbb {R} }x^{2}f(x)\,dx-\mu ^{2},$
where $\mu$ is the expected value of $X$ given by
$\mu =\int _{\mathbb {R} }xf(x)\,dx=\int _{\mathbb {R} }x\,dF(x).$
In these formulas, the integrals with respect to $dx$ and $dF(x)$ are Lebesgue and Lebesgue–Stieltjes integrals, respectively.
If the function $x^{2}f(x)$ is Riemann-integrable on every finite interval $[a,b]\subset \mathbb {R} ,$ then
$\operatorname {Var} (X)=\int _{-\infty }^{+\infty }x^{2}f(x)\,dx-\mu ^{2},$
where the integral is an improper Riemann integral.

## Examples
### Exponential distribution

The exponential distribution with parameter λ > 0 is a continuous distribution whose probability density function is given by $f(x)=\lambda e^{-\lambda x}$ on the interval [0, ∞). Its mean can be shown to be $\operatorname {E} [X]=\int _{0}^{\infty }x\lambda e^{-\lambda x}\,dx={\frac {1}{\lambda }}.$
Using integration by parts and making use of the expected value already calculated, we have: ${\begin{aligned}\operatorname {E} \left[X^{2}\right]&=\int _{0}^{\infty }x^{2}\lambda e^{-\lambda x}\,dx\\&={\left[-x^{2}e^{-\lambda x}\right]}_{0}^{\infty }+\int _{0}^{\infty }2xe^{-\lambda x}\,dx\\&=0+{\frac {2}{\lambda }}\operatorname {E} [X]\\&={\frac {2}{\lambda ^{2}}}.\end{aligned}}$
Thus, the variance of X is given by $\operatorname {Var} (X)=\operatorname {E} \left[X^{2}\right]-\operatorname {E} [X]^{2}={\frac {2}{\lambda ^{2}}}-\left({\frac {1}{\lambda }}\right)^{2}={\frac {1}{\lambda ^{2}}}.$

### Fair die

A fair six-sided die can be modeled as a discrete random variable, X, with outcomes 1 through 6, each with equal probability 1/6. The expected value of X is $(1+2+3+4+5+6)/6=7/2.$ Therefore, the variance of X is ${\begin{aligned}\operatorname {Var} (X)&=\sum _{i=1}^{6}{\frac {1}{6}}\left(i-{\frac {7}{2}}\right)^{2}\\[5pt]&={\frac {1}{6}}\left((-5/2)^{2}+(-3/2)^{2}+(-1/2)^{2}+(1/2)^{2}+(3/2)^{2}+(5/2)^{2}\right)\\[5pt]&={\frac {35}{12}}\approx 2.92.\end{aligned}}$
The general formula for the variance of the outcome, X, of an n-sided die is ${\begin{aligned}\operatorname {Var} (X)&=\operatorname {E} \left(X^{2}\right)-(\operatorname {E} (X))^{2}\\[5pt]&={\frac {1}{n}}\sum _{i=1}^{n}i^{2}-\left({\frac {1}{n}}\sum _{i=1}^{n}i\right)^{2}\\[5pt]&={\frac {(n+1)(2n+1)}{6}}-\left({\frac {n+1}{2}}\right)^{2}\\[4pt]&={\frac {n^{2}-1}{12}}.\end{aligned}}$

### Commonly used probability distributions

The following table lists the variance for some commonly used probability distributions.
| Name of the probability distribution | Probability distribution function | Mean | Variance |
| Binomial distribution | $\Pr \,(X=k)={\binom {n}{k}}p^{k}(1-p)^{n-k}$ | $np$ | $np(1-p)$ |
| Geometric distribution | $\Pr \,(X=k)=(1-p)^{k-1}p$ | ${\frac {1}{p}}$ | ${\frac {(1-p)}{p^{2}}}$ |
| Normal distribution | $f\left(x\mid \mu ,\sigma ^{2}\right)={\frac {1}{\sqrt {2\pi \sigma ^{2}}}}e^{-{\frac {1}{2}}{\left({\frac {x-\mu }{\sigma }}\right)}^{2}}$ | $\mu$ | $\sigma ^{2}$ |
| Uniform distribution (continuous) | $f(x\mid a,b)={\begin{cases}{\frac {1}{b-a}}&{\text{for }}a\leq x\leq b,\\[3pt]0&{\text{for }}x<a{\text{ or }}x>b\end{cases}}$ | ${\frac {a+b}{2}}$ | ${\frac {(b-a)^{2}}{12}}$ |
| Exponential distribution | $f(x\mid \lambda )=\lambda e^{-\lambda x}$ | ${\frac {1}{\lambda }}$ | ${\frac {1}{\lambda ^{2}}}$ |
| Poisson distribution | $f(k\mid \lambda )={\frac {e^{-\lambda }\lambda ^{k}}{k!}}$ | $\lambda$ | $\lambda$ |

## Properties
### Basic properties

Variance is non-negative because the squares are positive or zero: $\operatorname {Var} (X)\geq 0.$
The variance of a constant is zero. $\operatorname {Var} (a)=0.$
Conversely, if the variance of a random variable is 0, then it is almost surely a constant. That is, it always has the same value: $\operatorname {Var} (X)=0\iff \exists a:P(X=a)=1.$

### Issues of finiteness

If a distribution does not have a finite expected value, as is the case for the Cauchy distribution, then the variance cannot be finite either. However, some distributions may not have a finite variance, despite their expected value being finite. An example is a Pareto distribution whose index $k$ satisfies $1<k\leq 2.$

### Decomposition

The general formula for variance decomposition or the law of total variance is: If $X$ and $Y$ are two random variables, and the variance of $X$ exists, then
$\operatorname {Var} [X]=\operatorname {E} (\operatorname {Var} [X\mid Y])+\operatorname {Var} (\operatorname {E} [X\mid Y]).$
The conditional expectation $\operatorname {E} (X\mid Y)$ of $X$ given $Y$, and the conditional variance $\operatorname {Var} (X\mid Y)$ may be understood as follows. Given any particular value y of the random variable Y, there is a conditional expectation $\operatorname {E} (X\mid Y=y)$ given the event Y = y. This quantity depends on the particular value y; it is a function $g(y)=\operatorname {E} (X\mid Y=y)$. That same function evaluated at the random variable Y is the conditional expectation $\operatorname {E} (X\mid Y)=g(Y).$
In particular, if $Y$ is a discrete random variable assuming possible values $y_{1},y_{2},y_{3}\ldots$ with corresponding probabilities $p_{1},p_{2},p_{3}\ldots ,$, then in the formula for total variance, the first term on the right-hand side becomes
$\operatorname {E} (\operatorname {Var} [X\mid Y])=\sum _{i}p_{i}\sigma _{i}^{2},$
where $\sigma _{i}^{2}=\operatorname {Var} [X\mid Y=y_{i}]$. Similarly, the second term on the right-hand side becomes
$\operatorname {Var} (\operatorname {E} [X\mid Y])=\sum _{i}p_{i}\mu _{i}^{2}-\left(\sum _{i}p_{i}\mu _{i}\right)^{2}=\sum _{i}p_{i}\mu _{i}^{2}-\mu ^{2},$
where $\mu _{i}=\operatorname {E} [X\mid Y=y_{i}]$ and $\mu =\sum _{i}p_{i}\mu _{i}$. Thus the total variance is given by
$\operatorname {Var} [X]=\sum _{i}p_{i}\sigma _{i}^{2}+\left(\sum _{i}p_{i}\mu _{i}^{2}-\mu ^{2}\right).$
A similar formula is applied in analysis of variance, where the corresponding formula is
${\mathit {MS}}_{\text{total}}={\mathit {MS}}_{\text{between}}+{\mathit {MS}}_{\text{within}};$
here ${\mathit {MS}}$ refers to the Mean of the Squares. In linear regression analysis the corresponding formula is
${\mathit {MS}}_{\text{total}}={\mathit {MS}}_{\text{regression}}+{\mathit {MS}}_{\text{residual}}.$
This can also be derived from the additivity of variances, since the total (observed) score is the sum of the predicted score and the error score, where the latter two are uncorrelated.
Similar decompositions are possible for the sum of squared deviations (sum of squares, ${\mathit {SS}}$): ${\mathit {SS}}_{\text{total}}={\mathit {SS}}_{\text{between}}+{\mathit {SS}}_{\text{within}},$ ${\mathit {SS}}_{\text{total}}={\mathit {SS}}_{\text{regression}}+{\mathit {SS}}_{\text{residual}}.$

### Calculation from the CDF

The population variance for a non-negative random variable can be expressed in terms of the cumulative distribution function F using
$2\int _{0}^{\infty }u(1-F(u))\,du-{\left[\int _{0}^{\infty }(1-F(u))\,du\right]}^{2}.$
This expression can be used to calculate the variance in situations where the CDF, but not the density, can be conveniently expressed.

### Characteristic property

The second moment of a random variable attains the minimum value when taken around the first moment (i.e., mean) of the random variable, i.e. $\mathrm {argmin} _{m}\,\mathrm {E} \left(\left(X-m\right)^{2}\right)=\mathrm {E} (X)$. Conversely, if a continuous function $\varphi$ satisfies $\mathrm {argmin} _{m}\,\mathrm {E} (\varphi (X-m))=\mathrm {E} (X)$ for all random variables X, then it is necessarily of the form $\varphi (x)=ax^{2}+b$, where a > 0. This also holds in the multidimensional case.

### Units of measurement

Unlike the expected absolute deviation, the variance of a variable has units that are the square of the units of the variable itself. For example, a variable measured in meters will have a variance measured in meters squared. For this reason, describing data sets via their standard deviation or root mean square deviation is often preferred over using the variance. In the dice example the standard deviation is √2.9 ≈ 1.7, slightly larger than the expected absolute deviation of 1.5.
The standard deviation and the expected absolute deviation can both be used as an indicator of the "spread" of a distribution. The standard deviation is more amenable to algebraic manipulation than the expected absolute deviation, and, together with variance and its generalization covariance, is used frequently in theoretical statistics; however the expected absolute deviation tends to be more robust as it is less sensitive to outliers arising from measurement anomalies or an unduly heavy-tailed distribution.

## Propagation
### Addition and multiplication by a constant

Variance is invariant with respect to changes in a location parameter. That is, if a constant is added to all values of the variable, the variance is unchanged: $\operatorname {Var} (X+a)=\operatorname {Var} (X).$
If all values are scaled by a constant, the variance is scaled by the square of that constant: $\operatorname {Var} (aX)=a^{2}\operatorname {Var} (X).$
The variance of a sum of two random variables is given by ${\begin{aligned}\operatorname {Var} (aX+bY)&=a^{2}\operatorname {Var} (X)+b^{2}\operatorname {Var} (Y)+2ab\,\operatorname {Cov} (X,Y)\\[1ex]\operatorname {Var} (aX-bY)&=a^{2}\operatorname {Var} (X)+b^{2}\operatorname {Var} (Y)-2ab\,\operatorname {Cov} (X,Y)\end{aligned}}$
where $\operatorname {Cov} (X,Y)$ is the covariance.

### Linear combinations

In general, for the sum of $N$ random variables $\{X_{1},\dots ,X_{N}\}$, the variance becomes: $\operatorname {Var} \left(\sum _{i=1}^{N}X_{i}\right)=\sum _{i,j=1}^{N}\operatorname {Cov} (X_{i},X_{j})=\sum _{i=1}^{N}\operatorname {Var} (X_{i})+\sum _{i\neq j}\operatorname {Cov} (X_{i},X_{j}),$ see also general Bienaymé's identity.
These results lead to the variance of a linear combination as:
${\begin{aligned}\operatorname {Var} \left(\sum _{i=1}^{N}a_{i}X_{i}\right)&=\sum _{i,j=1}^{N}a_{i}a_{j}\operatorname {Cov} (X_{i},X_{j})\\&=\sum _{i=1}^{N}a_{i}^{2}\operatorname {Var} (X_{i})+\sum _{i\neq j}a_{i}a_{j}\operatorname {Cov} (X_{i},X_{j})\\&=\sum _{i=1}^{N}a_{i}^{2}\operatorname {Var} (X_{i})+2\sum _{1\leq i<j\leq N}a_{i}a_{j}\operatorname {Cov} (X_{i},X_{j}).\end{aligned}}$
If the random variables $X_{1},\dots ,X_{N}$ are such that $\operatorname {Cov} (X_{i},X_{j})=0\ ,\ \forall \ (i\neq j),$ then they are said to be uncorrelated. It follows immediately from the expression given earlier that if the random variables $X_{1},\dots ,X_{N}$ are uncorrelated, then the variance of their sum is equal to the sum of their variances, or, expressed symbolically:
$\operatorname {Var} \left(\sum _{i=1}^{N}X_{i}\right)=\sum _{i=1}^{N}\operatorname {Var} (X_{i}).$
Since independent random variables are always uncorrelated (see Covariance § Uncorrelatedness and independence), the equation above holds in particular when the random variables $X_{1},\dots ,X_{n}$ are independent. Thus, independence is sufficient but not necessary for the variance of the sum to equal the sum of the variances.

#### Matrix notation for the variance of a linear combination

Define $X$ as a column vector of $n$ random variables $X_{1},\ldots ,X_{n}$, and $c$ as a column vector of $n$ scalars $c_{1},\ldots ,c_{n}$. Therefore, $c^{\mathsf {T}}X$ is a linear combination of these random variables, where $c^{\mathsf {T}}$ denotes the transpose of $c$. Also let $\Sigma$ be the covariance matrix of $X$. The variance of $c^{\mathsf {T}}X$ is then given by:
$\operatorname {Var} \left(c^{\mathsf {T}}X\right)=c^{\mathsf {T}}\Sigma c.$
This implies that the variance of the mean can be written as (with a column vector of ones)
$\operatorname {Var} \left({\bar {x}}\right)=\operatorname {Var} \left({\frac {1}{n}}1'X\right)={\frac {1}{n^{2}}}1'\Sigma 1.$

### Sum of variables
#### Sum of uncorrelated variables

One reason for the use of the variance in preference to other measures of dispersion is that the variance of the sum (or the difference) of uncorrelated random variables is the sum of their variances:
$\operatorname {Var} \left(\sum _{i=1}^{n}X_{i}\right)=\sum _{i=1}^{n}\operatorname {Var} (X_{i}).$
This statement is called the Bienaymé formula and was discovered in 1853. It is often made with the stronger condition that the variables are independent, but being uncorrelated suffices. So if all the variables have the same variance σ$^2$, then, since division by n is a linear transformation, this formula immediately implies that the variance of their mean is
$\operatorname {Var} \left({\overline {X}}\right)=\operatorname {Var} \left({\frac {1}{n}}\sum _{i=1}^{n}X_{i}\right)={\frac {1}{n^{2}}}\sum _{i=1}^{n}\operatorname {Var} \left(X_{i}\right)={\frac {1}{n^{2}}}n\sigma ^{2}={\frac {\sigma ^{2}}{n}}.$
That is, the variance of the mean decreases when n increases. This formula for the variance of the mean is used in the definition of the standard error of the sample mean, which is used in the central limit theorem.
To prove the initial statement, it suffices to show that
$\operatorname {Var} (X+Y)=\operatorname {Var} (X)+\operatorname {Var} (Y).$
The general result then follows by induction. Starting with the definition,
${\begin{aligned}\operatorname {Var} (X+Y)&=\operatorname {E} \left[(X+Y)^{2}\right]-(\operatorname {E} [X+Y])^{2}\\[5pt]&=\operatorname {E} \left[X^{2}+2XY+Y^{2}\right]-(\operatorname {E} [X]+\operatorname {E} [Y])^{2}.\end{aligned}}$
Using the linearity of the expectation operator and the assumption of independence (or uncorrelatedness) of X and Y, this further simplifies as follows:
${\begin{aligned}\operatorname {Var} (X+Y)&=\operatorname {E} {\left[X^{2}\right]}+2\operatorname {E} [XY]+\operatorname {E} {\left[Y^{2}\right]}-\left(\operatorname {E} [X]^{2}+2\operatorname {E} [X]\operatorname {E} [Y]+\operatorname {E} [Y]^{2}\right)\\[5pt]&=\operatorname {E} \left[X^{2}\right]+\operatorname {E} \left[Y^{2}\right]-\operatorname {E} [X]^{2}-\operatorname {E} [Y]^{2}\\[5pt]&=\operatorname {Var} (X)+\operatorname {Var} (Y).\end{aligned}}$

#### Sum of correlated variables
##### Sum of correlated variables with fixed sample size

In general, the variance of the sum of n variables is the sum of their covariances:
$\operatorname {Var} \left(\sum _{i=1}^{n}X_{i}\right)=\sum _{i=1}^{n}\sum _{j=1}^{n}\operatorname {Cov} \left(X_{i},X_{j}\right)=\sum _{i=1}^{n}\operatorname {Var} \left(X_{i}\right)+2\sum _{1\leq i<j\leq n}\operatorname {Cov} \left(X_{i},X_{j}\right).$
(Note: The second equality comes from the fact that Cov(X$_i$,X$_i$) = Var(X$_i$).)
Here, $\operatorname {Cov} (\cdot ,\cdot )$ is the covariance, which is zero for independent random variables (if it exists). The formula states that the variance of a sum is equal to the sum of all elements in the covariance matrix of the components. The next expression states equivalently that the variance of the sum is the sum of the diagonal of covariance matrix plus two times the sum of its upper triangular elements (or its lower triangular elements); this emphasizes that the covariance matrix is symmetric. This formula is used in the theory of Cronbach's alpha in classical test theory.
So, if the variables have equal variance σ$^2$ and the average correlation of distinct variables is ρ, then the variance of their mean is
$\operatorname {Var} \left({\overline {X}}\right)={\frac {\sigma ^{2}}{n}}+{\frac {n-1}{n}}\rho \sigma ^{2}.$
This implies that the variance of the mean increases with the average of the correlations. In other words, additional correlated observations are not as effective as additional independent observations at reducing the uncertainty of the mean. Moreover, if the variables have unit variance, for example if they are standardized, then this simplifies to
$\operatorname {Var} \left({\overline {X}}\right)={\frac {1}{n}}+{\frac {n-1}{n}}\rho .$
This formula is used in the Spearman–Brown prediction formula of classical test theory. This converges to ρ if n goes to infinity, provided that the average correlation remains constant or converges too. So for the variance of the mean of standardized variables with equal correlations or converging average correlation we have
$\lim _{n\to \infty }\operatorname {Var} \left({\overline {X}}\right)=\rho .$
Therefore, the variance of the mean of a large number of standardized variables is approximately equal to their average correlation. This makes clear that the sample mean of correlated variables does not generally converge to the population mean, even though the law of large numbers states that the sample mean will converge for independent variables.

##### Sum of uncorrelated variables with random sample size

There are cases when a sample is taken without knowing, in advance, how many observations will be acceptable according to some criterion. In such cases, the sample size N is a random variable whose variation adds to the variation of X, such that, $\operatorname {Var} \left(\sum _{i=1}^{N}X_{i}\right)=\operatorname {E} \left[N\right]\operatorname {Var} (X)+\operatorname {Var} (N)(\operatorname {E} \left[X\right])^{2}$ which follows from the law of total variance.
If N has a Poisson distribution, then $\operatorname {E} [N]=\operatorname {Var} (N)$ with estimator n = N. So, the estimator of $\operatorname {Var} \left(\sum _{i=1}^{n}X_{i}\right)$ becomes $n{S_{x}}^{2}+n{\bar {X}}^{2}$, giving $\operatorname {SE} ({\bar {X}})={\sqrt {\frac {{S_{x}}^{2}+{\bar {X}}^{2}}{n}}}$ (see standard error of the sample mean).

#### Weighted sum of variables

The scaling property and the Bienaymé formula, along with the property of the covariance Cov(aX, bY) = ab Cov(X, Y) jointly imply that
$\operatorname {Var} (aX\pm bY)=a^{2}\operatorname {Var} (X)+b^{2}\operatorname {Var} (Y)\pm 2ab\,\operatorname {Cov} (X,Y).$
This implies that in a weighted sum of variables, the variable with the largest weight will have a disproportionally large weight in the variance of the total. For example, if X and Y are uncorrelated and the weight of X is two times the weight of Y, then the weight of the variance of X will be four times the weight of the variance of Y.
The expression above can be extended to a weighted sum of multiple variables:
$\operatorname {Var} \left(\sum _{i}^{n}a_{i}X_{i}\right)=\sum _{i=1}^{n}a_{i}^{2}\operatorname {Var} (X_{i})+2\sum _{1\leq i}\sum _{<j\leq n}a_{i}a_{j}\operatorname {Cov} (X_{i},X_{j})$

### Product of variables
#### Product of independent variables

If two variables X and Y are independent, the variance of their product is given by $\operatorname {Var} (XY)=[\operatorname {E} (X)]^{2}\operatorname {Var} (Y)+[\operatorname {E} (Y)]^{2}\operatorname {Var} (X)+\operatorname {Var} (X)\operatorname {Var} (Y).$
Equivalently, using the basic properties of expectation, it is given by
$\operatorname {Var} (XY)=\operatorname {E} \left(X^{2}\right)\operatorname {E} \left(Y^{2}\right)-[\operatorname {E} (X)]^{2}[\operatorname {E} (Y)]^{2}.$

#### Product of statistically dependent variables

In general, if two variables are statistically dependent, then the variance of their product is given by: ${\begin{aligned}\operatorname {Var} (XY)=&\operatorname {E} \left[X^{2}Y^{2}\right]-[\operatorname {E} (XY)]^{2}\\[5pt]=&\operatorname {Cov} \left(X^{2},Y^{2}\right)+\operatorname {E} (X^{2})\operatorname {E} \left(Y^{2}\right)-[\operatorname {E} (XY)]^{2}\\[5pt]=&\operatorname {Cov} \left(X^{2},Y^{2}\right)+\left(\operatorname {Var} (X)+[\operatorname {E} (X)]^{2}\right)\left(\operatorname {Var} (Y)+[\operatorname {E} (Y)]^{2}\right)\\[5pt]&-[\operatorname {Cov} (X,Y)+\operatorname {E} (X)\operatorname {E} (Y)]^{2}\end{aligned}}$

### Arbitrary functions

The delta method uses second-order Taylor expansions to approximate the variance of a function of one or more random variables: see Taylor expansions for the moments of functions of random variables. For example, the approximate variance of a function of one variable is given by
$\operatorname {Var} \left[f(X)\right]\approx \left(f'(\operatorname {E} \left[X\right])\right)^{2}\operatorname {Var} \left[X\right]$
provided that f is twice differentiable and that the mean and variance of X are finite.

## Population variance and sample variance

Real-world observations such as the measurements of yesterday's rain throughout the day typically cannot be complete sets of all possible observations that could be made. As such, the variance calculated from the finite set will in general not match the variance that would have been calculated from the full population of possible observations. This means that one estimates the mean and variance from a limited set of observations by using an estimator equation. The estimator is a function of the sample of n observations drawn without observational bias from the whole population of potential observations. In this example, the sample would be the set of actual measurements of yesterday's rainfall from available rain gauges within the geography of interest.
The simplest estimators for population mean and population variance are simply the mean and variance of the sample, the sample mean and (uncorrected) sample variance – these are consistent estimators (they converge to the value of the whole population as the number of samples increases) but can be improved. Most simply, the sample variance is computed as the sum of squared deviations about the (sample) mean, divided by n as the number of samples. However, using values other than n improves the estimator in various ways. Four common values for the denominator are n, n − 1, n + 1, and n − 1.5: n is the simplest (the variance of the sample), n − 1 eliminates bias, n + 1 minimizes mean squared error for the normal distribution, and n − 1.5 mostly eliminates bias in unbiased estimation of standard deviation for the normal distribution.
Firstly, if the true population mean is unknown, then the sample variance (which uses the sample mean in place of the true mean) is a biased estimator: it underestimates the variance by a factor of (n − 1) / n; correcting this factor, resulting in the sum of squared deviations about the sample mean divided by n -1 instead of n, is called Bessel's correction. The resulting estimator is unbiased and is called the (corrected) sample variance or unbiased sample variance. If the mean is determined in some other way than from the same samples used to estimate the variance, then this bias does not arise, and the variance can safely be estimated as that of the samples about the (independently known) mean.
Secondly, the sample variance does not generally minimize mean squared error between sample variance and population variance. Correcting for bias often makes this worse: one can always choose a scale factor that performs better than the corrected sample variance, though the optimal scale factor depends on the excess kurtosis of the population (see mean squared error: variance) and introduces bias. This always consists of scaling down the unbiased estimator (dividing by a number larger than n − 1) and is a simple example of a shrinkage estimator: one "shrinks" the unbiased estimator towards zero. For the normal distribution, dividing by n + 1 (instead of n − 1 or n) minimizes mean squared error. The resulting estimator is biased, however, and is known as the biased sample variation.

### Population variance

In general, the population variance of a finite population of size N with values x$_i$ is given by ${\begin{aligned}\sigma ^{2}&={\frac {1}{N}}\sum _{i=1}^{N}{\left(x_{i}-\mu \right)}^{2}={\frac {1}{N}}\sum _{i=1}^{N}\left(x_{i}^{2}-2\mu x_{i}+\mu ^{2}\right)\\[5pt]&=\left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}^{2}\right)-2\mu \left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}\right)+\mu ^{2}\\[5pt]&=\operatorname {E} [x_{i}^{2}]-\mu ^{2}\end{aligned}}$where the population mean is μ=E⁡[x$_i$]=1/N ∑$_{i=1}$$^N$x$_i${\textstyle \mu =\operatorname {E} [x_{i}]={\frac {1}{N}}\sum _{i=1}^{N}x_{i}} and E⁡[x$_i$$^2$]=( 1/N ∑$_{i=1}$$^N$x$_i$$^2$){\textstyle \operatorname {E} [x_{i}^{2}]=\left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}^{2}\right)}, where E{\textstyle \operatorname {E} } is the expectation value operator.
The population variance can also be computed using
$\sigma ^{2}={\frac {1}{N^{2}}}\sum _{i<j}\left(x_{i}-x_{j}\right)^{2}={\frac {1}{2N^{2}}}\sum _{i,j=1}^{N}\left(x_{i}-x_{j}\right)^{2}.$
(The right side has duplicate terms in the sum while the middle side has only unique terms to sum.) This is true because ${\begin{aligned}&{\frac {1}{2N^{2}}}\sum _{i,j=1}^{N}{\left(x_{i}-x_{j}\right)}^{2}\\[5pt]=&{\frac {1}{2N^{2}}}\sum _{i,j=1}^{N}\left(x_{i}^{2}-2x_{i}x_{j}+x_{j}^{2}\right)\\[5pt]=&{\frac {1}{2N}}\sum _{j=1}^{N}\left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}^{2}\right)-\left({\frac {1}{N}}\sum _{i=1}^{N}x_{i}\right)\left({\frac {1}{N}}\sum _{j=1}^{N}x_{j}\right)+{\frac {1}{2N}}\sum _{i=1}^{N}\left({\frac {1}{N}}\sum _{j=1}^{N}x_{j}^{2}\right)\\[5pt]=&{\frac {1}{2}}\left(\sigma ^{2}+\mu ^{2}\right)-\mu ^{2}+{\frac {1}{2}}\left(\sigma ^{2}+\mu ^{2}\right)\\[5pt]=&\sigma ^{2}.\end{aligned}}$ The population variance matches the variance of the generating probability distribution. In this sense, the concept of population can be extended to continuous random variables with infinite populations.

### Sample variance
#### Biased sample variance

In many practical situations, the true variance of a population is not known a priori and must be computed somehow. When dealing with extremely large populations, it is not possible to count every object in the population, so the computation must be performed on a sample of the population. This is generally referred to as sample variance or empirical variance. Sample variance can also be applied to the estimation of the variance of a continuous distribution from a sample of that distribution.
We take a sample with replacement of n values Y$_1$, ..., Y$_n$ from the population of size N, where n < N, and estimate the variance on the basis of this sample. Directly taking the variance of the sample data gives the average of the squared deviations:
${\tilde {S}}_{Y}^{2}={\frac {1}{n}}\sum _{i=1}^{n}\left(Y_{i}-{\overline {Y}}\right)^{2}=\left({\frac {1}{n}}\sum _{i=1}^{n}Y_{i}^{2}\right)-{\overline {Y}}^{2}={\frac {1}{n^{2}}}\sum _{i,j\,:\,i<j}\left(Y_{i}-Y_{j}\right)^{2}.$
(See the section Population variance for the derivation of this formula.) Here, ${\overline {Y}}$ denotes the sample mean: ${\overline {Y}}={\frac {1}{n}}\sum _{i=1}^{n}Y_{i}.$
Since the Y$_i$ are selected randomly, both ${\overline {Y}}$ and ${\tilde {S}}_{Y}^{2}$ are random variables. Their expected values can be evaluated by averaging over the ensemble of all possible samples {Y$_i$} of size n from the population. For ${\tilde {S}}_{Y}^{2}$ this gives: ${\begin{aligned}\operatorname {E} [{\tilde {S}}_{Y}^{2}]&=\operatorname {E} \left[{\frac {1}{n}}\sum _{i=1}^{n}{\left(Y_{i}-{\frac {1}{n}}\sum _{j=1}^{n}Y_{j}\right)}^{2}\right]\\[5pt]&={\frac {1}{n}}\sum _{i=1}^{n}\operatorname {E} \left[Y_{i}^{2}-{\frac {2}{n}}Y_{i}\sum _{j=1}^{n}Y_{j}+{\frac {1}{n^{2}}}\sum _{j=1}^{n}Y_{j}\sum _{k=1}^{n}Y_{k}\right]\\[5pt]&={\frac {1}{n}}\sum _{i=1}^{n}\left(\operatorname {E} \left[Y_{i}^{2}\right]-{\frac {2}{n}}\left(\sum _{j\neq i}\operatorname {E} \left[Y_{i}Y_{j}\right]+\operatorname {E} \left[Y_{i}^{2}\right]\right)+{\frac {1}{n^{2}}}\sum _{j=1}^{n}\sum _{k\neq j}^{n}\operatorname {E} \left[Y_{j}Y_{k}\right]+{\frac {1}{n^{2}}}\sum _{j=1}^{n}\operatorname {E} \left[Y_{j}^{2}\right]\right)\\[5pt]&={\frac {1}{n}}\sum _{i=1}^{n}\left({\frac {n-2}{n}}\operatorname {E} \left[Y_{i}^{2}\right]-{\frac {2}{n}}\sum _{j\neq i}\operatorname {E} \left[Y_{i}Y_{j}\right]+{\frac {1}{n^{2}}}\sum _{j=1}^{n}\sum _{k\neq j}^{n}\operatorname {E} \left[Y_{j}Y_{k}\right]+{\frac {1}{n^{2}}}\sum _{j=1}^{n}\operatorname {E} \left[Y_{j}^{2}\right]\right)\\[5pt]&={\frac {1}{n}}\sum _{i=1}^{n}\left[{\frac {n-2}{n}}\left(\sigma ^{2}+\mu ^{2}\right)-{\frac {2}{n}}(n-1)\mu ^{2}+{\frac {1}{n^{2}}}n(n-1)\mu ^{2}+{\frac {1}{n}}\left(\sigma ^{2}+\mu ^{2}\right)\right]\\[5pt]&={\frac {n-1}{n}}\sigma ^{2}.\end{aligned}}$
Here σ$^2$=E⁡[Y$_i$$^2$]−μ$^2${\textstyle \sigma ^{2}=\operatorname {E} [Y_{i}^{2}]-\mu ^{2}} derived in the section is population variance and E⁡[Y$_i$Y$_j$]=E⁡[Y$_i$]E⁡[Y$_j$]=μ$^2${\textstyle \operatorname {E} [Y_{i}Y_{j}]=\operatorname {E} [Y_{i}]\operatorname {E} [Y_{j}]=\mu ^{2}} due to independency of Y$_i${\textstyle Y_{i}} and Y$_j${\textstyle Y_{j}}.
Hence S$^~$$_Y$$^2${\textstyle {\tilde {S}}_{Y}^{2}} gives an estimate of the population variance σ$^2${\textstyle \sigma ^{2}} that is biased by a factor of n−1/n {\textstyle {\frac {n-1}{n}}} because the expectation value of S$^~$$_Y$$^2${\textstyle {\tilde {S}}_{Y}^{2}} is smaller than the population variance (true variance) by that factor. For this reason, S$^~$$_Y$$^2${\textstyle {\tilde {S}}_{Y}^{2}} is referred to as the biased sample variance.

#### Unbiased sample variance

Correcting for this bias yields the unbiased sample variance, denoted $S^{2}$:
$S^{2}={\frac {n}{n-1}}{\tilde {S}}_{Y}^{2}={\frac {n}{n-1}}\left[{\frac {1}{n}}\sum _{i=1}^{n}\left(Y_{i}-{\overline {Y}}\right)^{2}\right]={\frac {1}{n-1}}\sum _{i=1}^{n}\left(Y_{i}-{\overline {Y}}\right)^{2}$
Either estimator may be simply referred to as the sample variance when the version can be determined by context. The same proof is also applicable for samples taken from a continuous probability distribution.
The use of the term n − 1 is called Bessel's correction, and it is also used in sample covariance and the sample standard deviation (the square root of variance). The square root is a concave function and thus introduces negative bias (by Jensen's inequality), which depends on the distribution, and thus the corrected sample standard deviation (using Bessel's correction) is biased. The unbiased estimation of standard deviation is a technically involved problem, though for the normal distribution using the term n − 1.5 yields an almost unbiased estimator.
The unbiased sample variance is a U-statistic for the function f(y$_1$, y$_2$) = (y$_1$ − y$_2$)$^2$/2, meaning that it is obtained by averaging a 2-sample statistic over 2-element subsets of the population.

##### Example

For a set of numbers {10, 15, 30, 45, 57, 52, 63, 72, 81, 93, 102, 105}, if this set is the whole data population for some measurement, then variance is the population variance 932.743 as the sum of the squared deviations about the mean of this set, divided by 12 as the number of the set members. If the set is a sample from the whole population, then the unbiased sample variance can be calculated as 1017.538 that is the sum of the squared deviations about the mean of the sample, divided by 11 instead of 12. A function VAR.S in Microsoft Excel gives the unbiased sample variance while VAR.P is for population variance.

#### Distribution of the sample variance

Being a function of random variables, the sample variance is itself a random variable, and it is natural to study its distribution. In the case that Y$_i$ are independent observations from a normal distribution, Cochran's theorem shows that the unbiased sample variance S$^2$ follows a scaled chi-squared distribution (see also: asymptotic properties and an elementary proof): $(n-1){\frac {S^{2}}{\sigma ^{2}}}\sim \chi _{n-1}^{2}$
where σ$^2$ is the population variance. As a direct consequence, it follows that $\operatorname {E} \left(S^{2}\right)=\operatorname {E} \left({\frac {\sigma ^{2}}{n-1}}\chi _{n-1}^{2}\right)=\sigma ^{2},$
and
$\operatorname {Var} \left[S^{2}\right]=\operatorname {Var} \left({\frac {\sigma ^{2}}{n-1}}\chi _{n-1}^{2}\right)={\frac {\sigma ^{4}}{{\left(n-1\right)}^{2}}}\operatorname {Var} \left(\chi _{n-1}^{2}\right)={\frac {2\sigma ^{4}}{n-1}}.$
If Y$_i$ are independent and identically distributed, but not necessarily normally distributed, then
$\operatorname {E} \left[S^{2}\right]=\sigma ^{2},\quad \operatorname {Var} \left[S^{2}\right]={\frac {\sigma ^{4}}{n}}\left(\kappa -1+{\frac {2}{n-1}}\right)={\frac {1}{n}}\left(\mu _{4}-{\frac {n-3}{n-1}}\sigma ^{4}\right),$
where κ is the kurtosis of the distribution and μ$_4$ is the fourth central moment.
If the conditions of the law of large numbers hold for the squared observations, S$^2$ is a consistent estimator of σ$^2$. One can see indeed that the variance of the estimator tends asymptotically to zero. An asymptotically equivalent formula was given in Kenney and Keeping (1951:164), Rose and Smith (2002:264), and Weisstein (n.d.).

#### Samuelson's inequality

Samuelson's inequality is a result that states bounds on the values that individual observations in a sample can take, given that the sample mean and (biased) variance have been calculated. Values must lie within the limits ${\bar {y}}\pm \sigma _{Y}(n-1)^{1/2}.$

### Relations with the harmonic and arithmetic means

It has been shown that for a sample {y$_i$} of positive real numbers,
$\sigma _{y}^{2}\leq 2y_{\max }(A-H),$
where y$_{max}$ is the maximum of the sample, A is the arithmetic mean, H is the harmonic mean of the sample and $\sigma _{y}^{2}$ is the (biased) variance of the sample.
This bound has been improved, and it is known that variance is bounded by
${\begin{aligned}\sigma _{y}^{2}&\leq {\frac {y_{\max }(A-H)(y_{\max }-A)}{y_{\max }-H}},\\[1ex]\sigma _{y}^{2}&\geq {\frac {y_{\min }(A-H)(A-y_{\min })}{H-y_{\min }}},\end{aligned}}$
where y$_{min}$ is the minimum of the sample.

## Tests of equality of variances

The F-test of equality of variances and the chi square tests are adequate when the sample is normally distributed. Non-normality makes testing for the equality of two or more variances more difficult.
Several non parametric tests have been proposed: these include the Barton–David–Ansari–Freund–Siegel–Tukey test, the Capon test, Mood test, the Klotz test and the Sukhatme test. The Sukhatme test applies to two variances and requires that both medians be known and equal to zero. The Mood, Klotz, Capon and Barton–David–Ansari–Freund–Siegel–Tukey tests also apply to two variances. They allow the median to be unknown but do require that the two medians are equal.
The Lehmann test is a parametric test of two variances. Of this test there are several variants known. Other tests of the equality of variances include the Box test, the Box–Anderson test and the Moses test.
Resampling methods, which include the bootstrap and the jackknife, may be used to test the equality of variances.

## Moment of inertia

The variance of a probability distribution is analogous to the moment of inertia in classical mechanics of a corresponding mass distribution along a line, with respect to rotation about its center of mass. It is because of this analogy that such things as the variance are called moments of probability distributions. The covariance matrix is related to the moment of inertia tensor for multivariate distributions. The moment of inertia of a cloud of n points with a covariance matrix of $\Sigma$ is given by $I=n\left(\mathbf {1} _{3\times 3}\operatorname {tr} (\Sigma )-\Sigma \right).$
This difference between moment of inertia in physics and in statistics is clear for points that are gathered along a line. Suppose many points are close to the x axis and distributed along it. The covariance matrix might look like $\Sigma ={\begin{bmatrix}10&0&0\\0&0.1&0\\0&0&0.1\end{bmatrix}}.$
That is, there is the most variance in the x direction. Physicists would consider this to have a low moment about the x axis so the moment-of-inertia tensor is $I=n{\begin{bmatrix}0.2&0&0\\0&10.1&0\\0&0&10.1\end{bmatrix}}.$

## Semivariance

The semivariance is calculated in the same manner as the variance but only those observations that fall below the mean are included in the calculation: ${\text{Semivariance}}={\frac {1}{n}}\sum _{i:x_{i}<\mu }{\left(x_{i}-\mu \right)}^{2}$ It is also described as a specific measure in different fields of application. For skewed distributions, the semivariance can provide additional information that a variance does not.
For inequalities associated with the semivariance, see Chebyshev's inequality § Semivariances.

## Etymology

The term variance was first introduced by Ronald Fisher in his 1918 paper The Correlation Between Relatives on the Supposition of Mendelian Inheritance:
   The great body of available statistics show us that the deviations of a human measurement from its mean follow very closely the Normal Law of Errors, and, therefore, that the variability may be uniformly measured by the standard deviation corresponding to the square root of the mean square error. When there are two independent causes of variability capable of producing in an otherwise uniform population distributions with standard deviations $\sigma _{1}$ and $\sigma _{2}$, it is found that the distribution, when both causes act together, has a standard deviation ${\sqrt {\sigma _{1}^{2}+\sigma _{2}^{2}}}$. It is therefore desirable in analysing the causes of variability to deal with the square of the standard deviation as the measure of variability. We shall term this quantity the Variance...

## Generalizations
### For complex variables

If $x$ is a scalar complex-valued random variable, with values in $\mathbb {C} ,$ then its variance is $\operatorname {E} \left[(x-\mu )(x-\mu )^{*}\right],$ where $x^{*}$ is the complex conjugate of $x.$ This variance is a real scalar.

### For vector-valued random variables
#### As a matrix

If $X$ is a vector-valued random variable, with values in $\mathbb {R} ^{n},$ and thought of as a column vector, then a natural generalization of variance is $\operatorname {E} \left[(X-\mu ){(X-\mu )}^{\mathsf {T}}\right],$ where $\mu =\operatorname {E} (X)$ and $X^{\mathsf {T}}$ is the transpose of X, and so is a row vector. The result is a positive semi-definite square matrix, commonly referred to as the variance-covariance matrix (or simply as the covariance matrix).
If $X$ is a vector- and complex-valued random variable, with values in $\mathbb {C} ^{n},$ then the covariance matrix is $\operatorname {E} \left[(X-\mu ){(X-\mu )}^{\dagger }\right],$ where $X^{\dagger }$ is the conjugate transpose of $X.$ This matrix is also positive semi-definite and square.

#### As a scalar

Another generalization of variance for vector-valued random variables $X$, which results in a scalar value rather than in a matrix, is the generalized variance $\det(C)$, the determinant of the covariance matrix. The generalized variance can be shown to be related to the multidimensional scatter of points around their mean.
A different generalization is obtained by considering the equation for the scalar variance, $\operatorname {Var} (X)=\operatorname {E} \left[(X-\mu )^{2}\right]$, and reinterpreting $(X-\mu )^{2}$ as the squared Euclidean distance between the random variable and its mean, or, simply as the scalar product of the vector $X-\mu$ with itself. This results in $\operatorname {E} \left[(X-\mu )^{\mathsf {T}}(X-\mu )\right]=\operatorname {tr} (C),$ which is the trace of the covariance matrix.