liveBook · Manning

Variance

As explained in Grokking Statistics

A measure of dispersion equal to the average squared deviation from a distribution's mean.

Variance measures how widely data points scatter around their mean. Small variance means values cluster together; large variance means they are more spread out. This turns a vague impression—“the data is all over the place”—into a number useful for judging predictability. section 2.17 section 2.18

Calculate variance by following the same basic recipe:

  • Find the dataset’s mean. section 2.18
  • Subtract the mean from each data point and square each difference. section 2.17
  • Average the squared differences. For a population, divide by N; for a sample, divide by n − 1 to account for sampling uncertainty through Bessel’s correction. section 2.18

Same average, different spread

A group whose heights cluster near 6 feet has low variance. A group containing heights such as 4.5, 5, and 7 feet can have the same mean but higher variance. Higher variance generally means predictions may have larger errors and be less reliable. section 2.18

import numpy as np

temps = [77,75,68,71,60,65,81,71,56,73,
      79,80,85,84,72,69,82,81,71,63,
      75,83,80,80,88,76,79,73,80,81,82]

# calculate mean
mean = sum(temps) / len(temps)

# sum all the squared differences
squared_diffs = 0
for t in temps:
  squared_diffs += (t - mean) ** 2

# take the average of the squared differences
variance = squared_diffs / (len(temps) - 1)

# Variance:  57.65806451612904
print("Variance: ", variance)

# From NumPy:  57.65806451612903
print("From NumPy: ", np.var(temps, ddof=1))

Inspect a temperature dataset, calculate squared differences by hand in Python, and verify the sample variance with NumPy. section 2.18

How Grokking Statistics covers it

How deep it goes
Defines variance, explains population and sample denominators, and demonstrates manual and NumPy calculations without deriving the formulas formally.
Examples
Uses collie weights, contrasting height groups, and daily temperatures to illustrate low and high variance.
What it assumes
Builds from the mean and distinguishes samples from populations while assuming no prior statistics background.
What you can do after
Apply it via NumPy or a manual Python calculation and interpret spread as a clue about prediction reliability.
How it connects
Connects variance to the mean, standard deviation, interquartile range, samples, populations, and prediction error.

Also explained in

Compare how 19 books explain variance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Statistics. The section links point to the parts of the book it draws on.