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
- Find the mean of the dataset.
- Subtract the mean from each value to get its deviation.
- Square each deviation.
- 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.
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.0Example
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