Skip to content

Descriptive Statistics (mean, median, variance)

  • Sensitive to outliers
  • Good for symmetric distributions
Mean
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.mean(x))
  • Robust to outliers
Median
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
print(np.median(x))

Useful for categorical data.

Mode (SciPy)
import numpy as np
from scipy import stats
 
x = np.array([1, 1, 2, 2, 2, 3])
print(stats.mode(x, keepdims=True))
  • Range: max - min (very sensitive)
  • Variance: average squared distance from mean
  • Standard deviation (std): sqrt(variance)
Variance / Std
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
print("var:", np.var(x, ddof=1))
print("std:", np.std(x, ddof=1))

Robust measure of spread.

IQR
import numpy as np
 
x = np.array([10, 12, 12, 13, 12, 11, 100])
q1 = np.percentile(x, 25)
q3 = np.percentile(x, 75)
print("IQR:", q3 - q1)
  • Use median/IQR when outliers exist
  • Use mean/std when distribution is roughly symmetric
  • Always visualize (histogram/boxplot) before trusting summary stats
diagram Choosing a central tendency measure mermaid
A quick decision path for picking mean, median, or mode.

The mean is pulled toward extreme values because it uses every number in the calculation. The median only cares about the middle position, so it barely moves when one point is far away. Watch the outlier drag the mean marker to the right while the median stays put:

sketch Mean vs. median on a number line p5.js
The mean shifts toward the outlier; the median stays anchored near the middle of the data.

Exercise 1 – Mean vs. median with an outlier

Section titled “Exercise 1 – Mean vs. median with an outlier”

Exercise 2 – Variance and standard deviation

Section titled “Exercise 2 – Variance and standard deviation”

Continue to Probability Basics to build the foundation for confidence intervals and hypothesis tests.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading