Statistics Variance

Variance is the average of the squared deviations from the mean, and it is the core calculation that standard deviation is built on.

What Is Variance?

Variance measures spread the same way standard deviation does, but it stops one step earlier: it is the average of the squared distances between each value and the mean. Squaring the deviations serves two purposes - it prevents negative deviations from canceling out positive ones, and it penalizes values that are far from the mean more heavily than values that are close to it.

Calculating Variance Step by Step

  1. Find the mean of the dataset.
  2. Subtract the mean from each value to get its deviation.
  3. Square each deviation.
  4. Average the squared deviations - that average IS the variance.

Suppose five students report studying 10, 12, 14, 16, and 18 hours in a week. The mean is (10 + 12 + 14 + 16 + 18) / 5 = 70 / 5 = 14 hours.

Hours StudiedDeviation from MeanSquared Deviation
10-416
12-24
1400
1624
18416

The squared deviations sum to 16 + 4 + 0 + 4 + 16 = 40. If these five students represent the entire population you care about, the population variance is 40 / 5 = 8 (hours squared). If instead they are a sample meant to estimate a larger group of students, the sample variance divides by (n - 1) instead: 40 / 4 = 10 (hours squared).

Example

study_hours = [10, 12, 14, 16, 18]

mean = sum(study_hours) / len(study_hours)

squared_diffs = [(hours - mean) ** 2 for hours in study_hours]

population_variance = sum(squared_diffs) / len(study_hours)
print("Mean:", mean)
print("Population variance:", population_variance)
# Mean: 14.0
# Population variance: 8.0

Example

import statistics

study_hours = [10, 12, 14, 16, 18]

print("Population variance:", statistics.pvariance(study_hours))
print("Sample variance:", statistics.variance(study_hours))
# Population variance: 8
# Sample variance: 10

Variance vs Standard Deviation

Note: Variance is measured in squared units - 'hours squared' in this example - which is awkward to interpret on its own. That is exactly why standard deviation (the square root of variance) is reported more often in practice: taking the square root brings the measure of spread back into the original, interpretable units (hours, minutes, dollars, and so on).
Note: A common exercise pitfall is forgetting whether a question wants the population variance (divide by n) or the sample variance (divide by n - 1). Always check first whether your dataset represents the whole population or just a sample drawn from a larger group, since the two formulas give different answers.