Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 · Averages and Spread

Variance and Standard Deviation

Understand variance and standard deviation step by step: population versus sample formulas, why we divide by n - 1, and how to compute them in Python.

The standard deviation is the most widely used measure of spread. Informally, it is a typical distance of the values from the mean. A small standard deviation means the data hug the mean; a large one means they are scattered. Behind it is the variance, and in this lesson you will build both from the ground up.

The idea: distances from the mean

Start with the mean x̄. The deviation of a value x is x − x̄. Deviations are positive above the mean and negative below it, and they always add up to zero, so you cannot just average them.

Two fixes are possible: take absolute values, or square them. Squaring is mathematically more convenient and leads to the variance.

Worked example

Data: 2, 4, 4, 4, 5, 5, 7, 9 (n = 8).

  1. Mean: (2+4+4+4+5+5+7+9) / 8 = 40 / 8 = 5.
  2. Deviations: −3, −1, −1, −1, 0, 0, 2, 4.
  3. Squared deviations: 9, 1, 1, 1, 0, 0, 4, 16. Their sum is 32.
x x − 5 (x − 5)²
2 −3 9
4 −1 1
4 −1 1
4 −1 1
5 0 0
5 0 0
7 2 4
9 4 16
Total 0 32

Population formulas

If your data are the whole population:

variance σ² = Σ(x − μ)² / n

standard deviation σ = √variance

Here σ is the Greek letter sigma. For the example: σ² = 32 / 8 = 4 and σ = 2.

The variance is in squared units (cm² if the data are cm), which is awkward. Taking the square root returns to the original units, which is why the standard deviation is the one you quote.

Sample formulas

If your data are only a sample and you want to estimate the population's spread, use:

sample variance s² = Σ(x − x̄)² / (n − 1)

sample standard deviation s = √s²

For the example treated as a sample: s² = 32 / 7 ≈ 4.571 and s ≈ 2.138.

Why divide by n − 1?

Deviations are measured from the sample mean x̄, which is itself computed from the same data and sits right in the middle of them. That makes the deviations slightly smaller than deviations from the true, unknown population mean, so dividing by n would systematically underestimate the spread. Dividing by n − 1 (called Bessel's correction) corrects this on average. With large samples, n and n − 1 are almost the same.

Python Variance and standard deviation by hand and by module

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Your hand calculation and the module should match.

Properties worth knowing

  • Standard deviation is never negative, and it is 0 only if all values are identical.
  • Adding the same number to every value (a shift) leaves the standard deviation unchanged.
  • Multiplying every value by k multiplies the standard deviation by |k|.
  • Like the mean, it is strongly affected by outliers.
Python Shifting and scaling

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Interpreting standard deviation

Consider two classes with the same mean of 70.

Python Same mean, different spread

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Class A is consistent; class B is wildly spread. For many bell-shaped data sets, roughly two thirds of the values lie within one standard deviation of the mean and about 95% within two; you will see why in the distributions chapter. That is a rule of thumb for roughly symmetric data, not a guarantee for every data set.

Variance from a frequency table

With frequencies, variance = Σf(x − mean)² / Σf. A handy alternative is the "mean of squares minus square of the mean" form:

σ² = Σfx² / Σf − (Σfx / Σf)²

It avoids subtracting the mean from every value, but beware of rounding the mean early.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. The sum of squared deviations of 5 values is 40. What is the population variance?

    Population variance = 40 / 5 = 8.
  2. 2. The same sum of squared deviations is 40 for a sample of 5. What is the sample variance?

    Sample variance divides by n − 1 = 4, so 40 / 4 = 10.
  3. 3. Each value in a data set is increased by 7. What happens to the standard deviation?

    A shift moves all values and the mean equally, so the distances between them stay the same.
  4. 4. Why do we take the square root of the variance?

    Variance is in squared units. The square root brings it back to original units.

Summary

  • Deviations from the mean sum to zero, so we square them: variance = average squared deviation.
  • Population: divide by n; sample: divide by n − 1. The standard deviation is the square root of the variance.
  • Shifting data does not change the spread; scaling by k multiplies the standard deviation by |k|.
  • Standard deviation uses every value and is sensitive to outliers.

Next: Box Plots and Outliers