Tutorials › Introduction to Statistics › The Computational Formula for Variance

Random Variables and Expected Value · Tutorial 360 of 996

The Computational Formula for Variance

Learn how to derive and apply the variance formula using the first and second moments of a random variable.

Intermediate 10 min read

What You'll Learn

  • Derive the computational formula from the definition of variance.
  • Distinguish the second moment from the square of the mean.
  • Apply the formula to random variables with specified outcome probabilities.
  • Check a computational result against the expected squared deviation.
  • Recognize when the formula is valid and how units affect interpretation.

Why Use a Computational Formula?

Variance measures how far a random variable’s values tend to lie from its mean. Its definition captures this idea directly: take each possible deviation from the mean, square it, and find the expected squared deviation. That definition is conceptually useful, but calculating a separate deviation for every possible value can involve many steps.

An algebraic identity gives another route. Instead of finding each deviation first, calculate the expected value of the squared variable and subtract the square of its expected value. This is the computational formula for variance. It can make arithmetic shorter, especially when the first two moments of a random variable are already available.

Definition: If a random variable \(X\) has a finite second moment, its variance is \(\operatorname{Var}(X)=E[(X-\mu)^2]\), where \(\mu=E(X)\). The computational formula is \(\operatorname{Var}(X)=E(X^2)-[E(X)]^2\). Here \(E(X^2)\) is the expected value of the squared random variable, while \([E(X)]^2\) is the square of its expected value.

The formula applies whenever \(E(X^2)\) exists and is finite. For a discrete random variable with finitely many possible values, this condition is satisfied. The expectation \(E(X^2)\) is also called the second moment about zero; it is not itself the variance.

Deriving the Identity

Start with the definition, and write \(\mu=E(X)\). Expand the square inside the expectation. Linearity of expectation allows each term to be handled separately, and the expectation of a constant is that constant.

$$ \begin{aligned} \operatorname{Var}(X) &=E[(X-\mu)^2]\\ &=E[X^2-2\mu X+\mu^2]\\ &=E(X^2)-2\mu E(X)+\mu^2\\ &=E(X^2)-2\mu^2+\mu^2\\ &=E(X^2)-\mu^2\\ &=E(X^2)-[E(X)]^2. \end{aligned} $$

The key substitution in the fourth line is \(E(X)=\mu\). The coefficient \(2\mu\) is constant with respect to the random outcome, so \(E(2\mu X)=2\mu E(X)\). The last two terms combine because \(-2\mu^2+\mu^2=-\mu^2\). This establishes the identity; it is not an approximation or a special rule for a particular distribution.

The derivation also explains why the formula is useful. The definition centers every possible value by subtracting \(\mu\). The computational formula avoids that centering step: it uses the second moment \(E(X^2)\) and the first moment \(E(X)\). Either route gives the same variance when the required expectations exist.

Applying the Formula

For a discrete random variable, an expected value is a probability-weighted average. Thus, if \(X\) can take values \(x_i\) with probabilities \(p_i\), then \(E(X)=\sum_i x_i p_i\) and \(E(X^2)=\sum_i x_i^2p_i\). In the examples below, these sums are used to obtain the two moments before applying the identity. The main point is how those moments combine to give variance.

Worked Example: A Variable With Three Possible Values

Suppose a hypothetical game produces a score \(X\), measured in points. The score is 0 with probability 0.20, 2 with probability 0.50, and 5 with probability 0.30. Find the mean and variance using the computational formula.

First calculate the mean by weighting each score by its probability:

$$ E(X)=0(0.20)+2(0.50)+5(0.30)=0+1+1.5=2.5\text{ points}. $$

For the second moment, square each score before weighting it:

$$ E(X^2)=0^2(0.20)+2^2(0.50)+5^2(0.30) =0+2+7.5=9.5\text{ points}^2. $$

Now subtract the square of the mean from the second moment:

$$ \operatorname{Var}(X)=9.5-(2.5)^2 =9.5-6.25=3.25\text{ points}^2. $$

The mean score is 2.5 points, and the variance is 3.25 points squared. As a check, the direct definition gives \(0.20(0-2.5)^2+0.50(2-2.5)^2+0.30(5-2.5)^2 =1.25+0.125+1.875=3.25\text{ points}^2\). The direct calculation agrees, but the computational route avoids separately organizing all the centered squared deviations.

Worked Example: A Variable With Negative and Positive Outcomes

Consider a hypothetical net change \(X\), measured in dollars, with possible outcomes \(-4\), 0, and 6. Suppose their probabilities are 0.25, 0.50, and 0.25, respectively. Find the mean and variance.

The expected net change is

$$ E(X)=(-4)(0.25)+0(0.50)+6(0.25) =-1+0+1.5=0.5\text{ dollars}. $$

For the second moment, the negative outcome is squared too, so its contribution is positive:

$$ E(X^2)=(-4)^2(0.25)+0^2(0.50)+6^2(0.25) =4+0+9=13\text{ dollars}^2. $$

Therefore,

$$ \operatorname{Var}(X)=13-(0.5)^2 =13-0.25=12.75\text{ dollars}^2. $$

The variance is 12.75 dollars squared. The mean of 0.5 dollars does not mean that outcomes are all close to half a dollar: the possible outcomes include a loss of 4 dollars and a gain of 6 dollars. Variance summarizes squared spread around the mean, not the direction or size of the mean itself.

Worked Example: Using Moments Already Calculated

Suppose a hypothetical count variable \(Y\), measured in events per fixed interval, has already been summarized by \(E(Y)=4\) events and \(E(Y^2)=22\) events squared. Find its variance. This situation occurs when the moments have been obtained in an earlier stage of an analysis and only the variance is still needed.

Use the computational formula directly:

$$ \operatorname{Var}(Y)=E(Y^2)-[E(Y)]^2 =22-4^2=22-16=6\text{ events}^2. $$

The variance is 6 events squared. No probabilities or individual deviations need to be recalculated because the two required moments are already known. The given values are consistent with a possible random variable: variance cannot be negative, and here \(22-16=6\) is positive. That check alone does not establish that any arbitrary pair of moments describes a valid distribution, but it catches one important impossibility.

Checks, Units, and Interpretation

The computational formula involves subtracting two quantities that are both measured in squared units. If \(X\) is measured in points, then both \(E(X^2)\) and \([E(X)]^2\) are in points squared, so their difference is in points squared as well. This is why variance does not have the same units as the original variable. The standard deviation, covered separately, returns spread to the original measurement units.

There are two useful checks when applying the identity:

  • Check the order of operations. Find \(E(X^2)\) by squaring each possible value before taking its expectation. Separately find \(E(X)\), then square that result.
  • Check nonnegativity. Variance is an expected squared deviation, so it cannot be negative. If \(E(X^2)-[E(X)]^2\) is negative, there is an arithmetic error or inconsistent input.
  • Check units. Both terms being subtracted must have squared units. Mixing a first moment with a second moment is a sign that the calculation is not set up correctly.

The formula can also be less convenient in some numerical settings. If \(E(X^2)\) and \([E(X)]^2\) are both very large and close together, rounding can cause their subtraction to lose precision. With exact arithmetic, the identity remains valid; with rounded calculations, keep additional digits until the final result where possible.

Common Mistakes

Common pitfall: Confusing the expected square with the square of the expectation. In general, \(E(X^2)\ne [E(X)]^2\). The first squares each possible value before averaging; the second averages the values first and then squares the mean. Variance is the difference between these two quantities.

For example, in the first worked example, \(E(X^2)=9.5\), whereas \([E(X)]^2=(2.5)^2=6.25\). Reversing these quantities would give a negative answer, \(6.25-9.5=-3.25\), which cannot be a variance. That negative result is a warning, not an alternative measure of spread.

Another error is to omit the square on the mean. The correct expression is \(E(X^2)-[E(X)]^2\), not \(E(X^2)-E(X)\). The two terms must be on the same squared scale. A third error is to square the probabilities along with the values; each probability is used as a weight, not squared.

When the Identity Matters

The computational formula is valuable because expected values of functions of a random variable can often be found directly. Since \(X^2\) is a function of \(X\), \(E(X^2)\) is a well-defined expectation when it is finite. Once \(E(X)\) and \(E(X^2)\) are available, the variance follows without first constructing every deviation from the mean. This is especially efficient when moments have already been calculated or when the algebraic form of \(E(X^2)\) is simpler than a centered calculation.

At the same time, the definition remains the clearest guide to interpretation: variance is the expected squared distance from the mean. The computational formula is an equivalent way to calculate that quantity, not a different concept. Keeping both forms in view helps diagnose errors and clarifies why the result measures spread.

Key takeaway: For a random variable with finite second moment, calculate variance as \(E(X^2)-[E(X)]^2\). Square possible values before averaging to obtain \(E(X^2)\), and square the mean only after calculating \(E(X)\).

Check Your Understanding

Answer each question, showing the distinction between the two moments where relevant.

  1. In one sentence, derive the computational formula from the definition of variance by expanding the squared deviation.
  2. A random variable \(W\) has \(E(W)=3\) and \(E(W^2)=14\). Find its variance, with units if \(W\) has units of seconds.
  3. Explain why \(E(X^2)\) and \([E(X)]^2\) are generally different quantities.
  4. If a calculation gives \(E(X^2)-[E(X)]^2=-2\), what should you conclude about the calculation?
  5. If \(X\) is measured in kilograms, what are the units of \(E(X^2)\) and \(\operatorname{Var}(X)\)?