I3 · Mathematics for AI

Variance and Standard Deviation

Variance is the average squared distance of values from their mean. Standard deviation is its square root, so it's in the same units as the data. Both measure how spread out the data is.

10 min Intermediate · Sublevel Beginner

What you need: The earlier topics of this module.

Definition

Variance is the average squared distance of values from their mean. Standard deviation is its square root, so it's in the same units as the data. Both measure how spread out the data is.

In simple words

Two classes both average 60 marks. In one, everybody scored between 58 and 62. In the other, marks run from 10 to 100. The averages match, but the classes are very different. Standard deviation is the number that captures this.

Overview

  • Measures of spread describe how far values lie from the centre.
  • Variance is the average squared distance from the mean.
  • Standard deviation is the square root of variance, so it has the same units as the data.
  • Sample variance divides by n − 1 instead of n (Bessel's correction) to avoid underestimating.
  • For roughly normal data, about 68% of values lie within one standard deviation of the mean.
  • Feature scaling and z-scores use the standard deviation directly.

Why it matters

A column with zero variance tells a model nothing, so you can drop it. Feature scaling divides by standard deviation. PCA looks for directions of maximum variance. It's also how you judge whether a result is noise or a real effect.

Key terms

Deviation
How far one value is from the mean: x minus the mean.
Variance
The average of the squared deviations; its units are the data's units squared.
Standard deviation
The square root of the variance, in the data's own units.
Population vs sample
Dividing by n for a whole population, by n - 1 for a sample (Bessel's correction).
z-score
How many standard deviations a value is from the mean.
Standardisation
Rescaling a feature to mean 0 and standard deviation 1.

Types

Spread can be measured in several ways. These are the main types and what each tells you.

1. Range

The difference between the largest and smallest values.

  • How it works: Subtract the minimum from the maximum; it uses only two values, so one outlier changes it completely.
  • Example: Temperatures from 24°C to 39°C have a range of 15°C.
  • When to use it: Use it for a quick sense of spread or limits, not for careful comparison between data sets.

2. Variance

The mean of the squared deviations from the mean.

  • How it works: Subtract the mean from each value, square the results, add them and divide by n (population) or n − 1 (sample).
  • Example: Values 2, 4, 6 have mean 4 and squared deviations 4, 0, 4, so the population variance is 8 ÷ 3 ≈ 2.67.
  • When to use it: Use it in mathematics and model building, because variances add up neatly; report the standard deviation to people instead.

3. Standard deviation

The square root of the variance.

  • How it works: Taking the root brings the spread back to the original units; np.std(x) gives the population version and ddof=1 the sample version.
  • Example: Two shops both sell an average of 100 samosas a day; one has a standard deviation of 5 and the other of 40.
  • When to use it: Use it as the standard everyday measure of spread, for z-scores, error bars and standardising features before modelling.

4. Interquartile range (IQR)

The spread of the middle half of the data: Q3 − Q1.

  • How it works: Find the 25th and 75th percentiles and subtract; because it ignores the top and bottom quarters, outliers barely affect it.
  • Example: Delivery times with Q1 = 22 and Q3 = 34 minutes have an IQR of 12 minutes.
  • When to use it: Use it for skewed data and for detecting outliers with the 1.5 × IQR rule.

5. Coefficient of variation (CV)

The standard deviation divided by the mean, often as a percentage.

  • How it works: Dividing by the mean removes the units, so spreads of very different quantities can be compared.
  • Example: Rice yields with mean 3 t/ha and SD 0.3 have CV 10%; mango yields with mean 10 t/ha and SD 2 have CV 20%, so mango is more variable.
  • When to use it: Use it to compare consistency across quantities with different scales, for positive data only.

Explained in detail

Why square the deviations?

To measure spread, a natural first idea is to average how far each value is from the mean. But deviations above and below the mean cancel exactly: their sum is always zero. Squaring makes every deviation positive and gives large deviations extra weight. Taking the average of those squares gives the variance. Because squaring changes the units, marks become marks squared, we take the square root to return to the original units, giving the standard deviation. A standard deviation of 8 marks is easy to interpret; a variance of 64 squared marks is not.

A worked example

Mark xDeviation (x − 70)Squared
60−10100
65−525
7000
75525
8010100

The mean is 70 and the squared deviations add to 250. Treated as a whole population, the variance is 250 ÷ 5 = 50 and the standard deviation is about 7.07. Treated as a sample from a larger class, we divide by n − 1 = 4, giving a variance of 62.5 and a standard deviation of about 7.91. The n − 1, called Bessel's correction, compensates for the fact that a sample's own mean is always closer to its values than the true population mean would be, which would otherwise underestimate the spread.

Reading a standard deviation

For data that is roughly bell-shaped, the empirical rule gives a quick picture: about 68 per cent of values lie within one standard deviation of the mean, about 95 per cent within two and about 99.7 per cent within three. A z-score turns this into a single number: z = (x − mean) / standard deviation. A mark of 86 in the example above has a z-score of about 2, so it is unusually high. Values with |z| above 3 are often flagged as possible outliers, although for skewed data this rule is unreliable.

Variance in machine learning

  • Standardisation: many models, such as k-nearest neighbours, SVMs and neural networks, train better when every feature is rescaled to z-scores, so no feature dominates just because of its units.
  • Bias and variance: in model evaluation, "variance" also describes how much a model's predictions change with different training data; high variance is a sign of overfitting.
  • Uncertainty: reporting results as mean ± standard deviation over several runs shows how reliable an accuracy figure is.

How it works

  1. Find the mean.
  2. Subtract the mean from each value. These are the deviations.
  3. Square each one, so negatives don't cancel positives.
  4. Average the squares. That's the variance.
  5. Take the square root. That's the standard deviation.

Why divide by n−1?

For a whole population, divide by n. For a sample, divide by n−1. A sample's mean sits closer to its own values than the true mean does, so the plain average underestimates the spread. Using n−1, called Bessel's correction, fixes that.

The maths

Population variance
Sample variance
The sample standard deviation
The sample mean

Sample: 2, 4, 6, 8. The mean is 5.

Deviations:  −3, −1, 1, 3
Squares:      9, 1, 1, 9     sum = 20
s² = 20 / (4−1) = 6.67
s  = √6.67 ≈ 2.58

In code

spread.py
import numpy as np
import statistics as st

data = [2, 4, 6, 8]
print("statistics.stdev (n-1):", round(st.stdev(data), 3))
print("numpy std (n):", round(np.std(data), 3))
print("numpy std with ddof=1 (n-1):", round(np.std(data, ddof=1), 3))

calm = [58, 59, 60, 61, 62]
wild = [10, 30, 60, 90, 110]
print("Same mean?", st.mean(calm), st.mean(wild))
print("Spread:", round(st.stdev(calm), 1), round(st.stdev(wild), 1))

Output

statistics.stdev (n-1): 2.582
numpy std (n): 2.236
numpy std with ddof=1 (n-1): 2.582
Same mean? 60 60
Spread: 1.6 41.2

Common mistakes

  • Using population and sample formulas interchangeably on small data, where the difference is noticeable.
  • Comparing the standard deviations of variables with different units or scales without standardising first.

Check yourself

Question 1

1. Why do we square the deviations?

Question 2

2. A column has the same value in every row. What is its standard deviation?

Summary

  • Variance: the average squared distance from the mean.
  • Standard deviation: its square root, in the original units.
  • For samples, divide by n−1.

Finished reading?

Sources: NIST handbook, measures of scale