Tutorials › VEE Mathematical Statistics › The Computational Formula for Sample Variance

VEE Mathematical Statistics · Random Samples and Statistics · Tutorial 356 of 880

The Computational Formula for Sample Variance

Learn to derive and apply the computational formula for sample variance, check it against direct calculation, and avoid denominator and rounding errors.

Intermediate 13 min read

What You'll Learn

  • Derive the identity for the sum of squared deviations from the sample mean
  • Express sample variance using the sum of observations and the sum of their squares
  • Calculate sample variance from raw observations and frequency data
  • Check computational results by calculating deviations directly
  • Recognize denominator, centering, and numerical-precision traps

Why Use a Computational Formula?

Sample variance measures how far observations lie from their sample mean. Its definition asks us to find that mean, subtract it from every observation, square the differences, and add them. A useful algebraic identity gives the same sum using only the sum of the observations and the sum of their squares. This can simplify hand calculations, calculations from frequency tables, and checks of intermediate work.

The identity does not change what variance measures: it is a rearrangement of the squared-deviation definition. In particular, the mean must still be accounted for. Merely adding the squared observations does not give the sum of squared deviations.

Definition: For observations \(X_1,\ldots,X_n\), where \(n\geq 2\), the sample mean is \(\bar X=\frac{1}{n}\sum_{i=1}^n X_i\). The sum of squared deviations from the sample mean is \(Q=\sum_{i=1}^n(X_i-\bar X)^2\). Under the usual convention, the sample variance is \(S^2=\frac{Q}{n-1}\).

When the observations are treated as a random sample, the usual model is that \(X_1,\ldots,X_n\) are independent and identically distributed, with finite variance. The algebraic identity below does not require independence, identical distributions, or any particular probability distribution: it holds for any list of \(n\) real observations. The sampling assumptions matter for statistical interpretations of \(S^2\), not for the rearrangement itself.

The Computational Identity

Write \(T=\sum_{i=1}^n X_i\) and \(R=\sum_{i=1}^n X_i^2\). Since \(\bar X=T/n\), the centered sum of squares can be calculated either from the deviations or from the raw totals:

Theorem: For any \(n\) real observations, $$ Q=\sum_{i=1}^n(X_i-\bar X)^2 =\sum_{i=1}^n X_i^2-n\bar X^2 =\sum_{i=1}^n X_i^2-\frac{\left(\sum_{i=1}^n X_i\right)^2}{n}. $$ Consequently, for \(n\geq 2\), the usual sample variance is $$ S^2=\frac{1}{n-1}\left[\sum_{i=1}^n X_i^2-\frac{\left(\sum_{i=1}^n X_i\right)^2}{n}\right]. $$

The expression in brackets is \(Q\), not \(S^2\) itself. Dividing that expression by \(n-1\) gives sample variance under the usual convention. The alternative variance with divisor \(n\), sometimes used as a descriptive measure of the observed list, would instead be \(Q/n\). Keep the two quantities distinct when a question specifies which convention it uses.

Why the Identity Holds

Expand each squared deviation and then use the fact that the sum of the observations is \(n\bar X\). This shows exactly where the correction term \(n\bar X^2\) comes from.

$$ \begin{aligned} Q &=\sum_{i=1}^n(X_i-\bar X)^2\\ &=\sum_{i=1}^n\left(X_i^2-2X_i\bar X+\bar X^2\right)\\ &=\sum_{i=1}^n X_i^2-2\bar X\sum_{i=1}^n X_i+n\bar X^2\\ &=\sum_{i=1}^n X_i^2-2\bar X(n\bar X)+n\bar X^2\\ &=\sum_{i=1}^n X_i^2-n\bar X^2. \end{aligned} $$

Because \(\bar X=(\sum X_i)/n\), we also have \(n\bar X^2=(\sum X_i)^2/n\), giving the second form. The correction subtracts the squared contribution of the sample's location. If all observations are shifted by the same constant, their deviations from their shifted mean do not change; accordingly, \(Q\) does not change either.

1
Find the raw totals.
Calculate \(T=\sum X_i\) and \(R=\sum X_i^2\). Square each observation before adding to obtain \(R\).
2
Center the total.
Calculate \(Q=R-T^2/n\). This is the sum of squared deviations, not yet the sample variance.
3
Apply the requested convention.
For the usual sample variance, calculate \(S^2=Q/(n-1)\). If a problem explicitly defines a different divisor, follow that definition.
4
Check the result.
When practical, calculate the deviations directly. The two methods must produce the same \(Q\), apart from rounding.

Worked Calculations

The examples below use observed numerical data; no probability distribution is needed to apply the identity. In each case, the usual sample variance uses divisor \(n-1\).

Worked Example: Four Laboratory Readings

A technician records four readings, in the same measurement units: \(2,4,7,9\). Find the sample mean, the sum of squared deviations, and the usual sample variance. Check the result by direct calculation.

Method. Use the computational identity, then verify the centered sum by the definition. This method avoids repeatedly centering the data before finding the raw totals.

First, \(n=4\) and \(T=2+4+7+9=22\), so \(\bar X=22/4=5.5\). The sum of squares is \(R=2^2+4^2+7^2+9^2=4+16+49+81=150\). Therefore,

$$ Q=R-\frac{T^2}{n} =150-\frac{22^2}{4} =150-\frac{484}{4} =150-121 =29. $$

Since \(n-1=3\), the sample variance is \(S^2=29/3=9.6667\), rounded to four decimal places. For the direct check, the deviations from \(5.5\) are \(-3.5,-1.5,1.5,3.5\). Their squares are \(12.25,2.25,2.25,12.25\), which sum to \(29\). The centered sum agrees with the computational result.

Common mistake avoided: \(R=150\) is the sum of the squared observations, not the sum of squared deviations. Subtracting \(n\bar X^2=121\) is essential to center the values.

Worked Example: A Frequency Table With Repeated Values

A small calibration set contains value \(1\) twice, value \(3\) three times, and value \(5\) once. Find the sample variance of the six observations without writing the repeated observations as separate entries.

Method. Use frequencies to calculate the total and the sum of squares, counting each value according to its frequency. This avoids the error of treating a frequency as if it were an observation.

The sample size is \(n=2+3+1=6\). The total is \(T=2(1)+3(3)+1(5)=2+9+5=16\), giving \(\bar X=16/6=8/3\). The sum of squared observations is \(R=2(1^2)+3(3^2)+1(5^2)=2+27+25=54\). Thus,

$$ Q=54-\frac{16^2}{6} =54-\frac{256}{6} =54-\frac{128}{3} =\frac{34}{3}. $$

The usual sample variance is $$ S^2=\frac{Q}{n-1} =\frac{34/3}{5} =\frac{34}{15} =2.2667, $$ rounded to four decimal places. As a check, the mean is \(8/3\). The two observations equal to \(1\) contribute \(2(1-8/3)^2=50/9\); the three equal to \(3\) contribute \(3(3-8/3)^2=3/9\); and the one equal to \(5\) contributes \((5-8/3)^2=49/9\). The total is \(102/9=34/3\), as above.

Common mistake avoided: The count \(3\) attached to value \(3\) means that \(3\) appears three times. Its contribution to \(R\) is \(3(3^2)=27\), not \(3^2\) just once.

Worked Example: Readings With a Large Common Offset

A sensor reports five readings: \(101,103,106,110,115\). Find their sample variance and verify it from deviations. The readings are in the same units and are treated simply as five observed values.

Method. Apply the raw-total identity and then check by centering. This example also illustrates why the correction term must be computed accurately when readings share a large offset.

The total is \(T=101+103+106+110+115=535\), so \(\bar X=535/5=107\). The sum of squares is

$$ R=101^2+103^2+106^2+110^2+115^2 =10201+10609+11236+12100+13225 =57371. $$

The centering correction is \(T^2/n=535^2/5=286225/5=57245\), so \(Q=57371-57245=126\). Therefore, \(S^2=126/(5-1)=31.5\). Directly, the deviations from \(107\) are \(-6,-4,-1,3,8\); their squares sum to \(36+16+1+9+64=126\). Both methods agree.

Common mistake avoided: Squaring the mean and subtracting only \(\bar X^2\) would subtract \(11449\), not \(57245\). The formula requires \(n\bar X^2\), which here equals \(5(11449)=57245\).

Common Traps and Practical Checks

The computational formula is compact, but its terms have specific meanings. A reliable calculation labels the total, the sum of squares, the centered sum, and the variance separately. That simple separation makes it easier to catch an arithmetic or convention error before reporting a result.

  • Using the wrong divisor. The identity gives \(Q\). Under the usual sample-variance convention, divide \(Q\) by \(n-1\), not by \(n\). If the requested quantity is explicitly defined with divisor \(n\), use that instead.
  • Omitting the sample-size factor. Subtract \(n\bar X^2\), equivalently \(T^2/n\), from \(R\). Do not subtract only \(\bar X^2\) or \(T^2\).
  • Squaring after summing individual squares incorrectly. The correction uses the square of the total, \(T^2\), divided by \(n\). In general, \((\sum X_i)^2\) is not \(\sum X_i^2\).
  • Rounding too early. Keep \(T^2/n\) in exact or sufficiently precise form until the subtraction and division are complete. Early rounding can affect the final digits.
  • Ignoring numerical cancellation. If all observations are very large but close together, \(R\) and \(T^2/n\) may be large, nearly equal numbers. Their subtraction can lose precision in a computer calculation. When possible, use a numerically stable centered or online algorithm, or recenter observations by subtracting a convenient constant before calculating.

For an algebraic check, \(Q\) cannot be negative because it is a sum of squares. A negative result from the computational formula signals an arithmetic, rounding, or numerical-precision problem. Also, if all observations are identical, then \(R=T^2/n\), so \(Q=0\) and \(S^2=0\); this is a useful boundary check.

Key takeaway: Calculate \(Q=\sum X_i^2-(\sum X_i)^2/n\) to obtain the sum of squared deviations. For the usual sample variance, divide that result by \(n-1\). The correction term centers the squared observations at the sample mean.

Check Your Understanding

For each problem, use the stated usual sample-variance convention unless indicated otherwise. Answers are given so you can check your calculations.

  1. A sample consists of \(2,5,8\). Calculate its mean, sum of squared deviations, and sample variance using the computational identity. (Answer: \(\bar X=5\), \(Q=18\), and \(S^2=9\).)
  2. A frequency table records \(0\) once, \(2\) twice, and \(4\) once. Find the sample size, sum of squared deviations, and sample variance. (Answer: \(n=4\), \(Q=4\), and \(S^2=4/3\).)
  3. For a sample of size \(5\), the sum of observations is \(30\) and the sum of squared observations is \(190\). Find the sample mean, \(Q\), and \(S^2\). (Answer: \(\bar X=6\), \(Q=10\), and \(S^2=2.5\).)
  4. Four readings are \(1004,1006,1008,1012\). Use the computational identity to find the usual sample variance, and state the value of \(Q\) used. (Answer: \(Q=35\) and \(S^2=35/3\approx11.6667\).)
  5. A sample has \(n=4\), \(\bar X=3.5\), and \(\sum X_i^2=58\). Calculate the centered sum of squares and the usual sample variance. (Answer: \(Q=9\) and \(S^2=3\).)
Clinical Biostats