Section 1 · Lesson 2
Spread: variance, standard deviation, percentiles and outliers
Measure how spread out data is, read percentiles and boxplots, and flag outliers with the 1.5 × IQR rule.
Two campaigns can both average $1.20 per click while one is rock steady and the other swings from $0.30 to $4.00. The average hides that difference. Measures of spread tell you how consistent, or how risky, a metric is.
Range
range = max - min. Easy to explain, but it only uses the two most extreme values, so one odd
row can double it.
Variance and standard deviation
Measure how far each value sits from the mean, square those distances (so negatives don’t cancel positives), and average them:
population variance = sum((x - mean) ** 2) / N
sample variance = sum((x - mean) ** 2) / (n - 1)
standard deviation = sqrt(variance)
The standard deviation (SD) is back in the original units (dollars, seconds), so it is the one you usually report. Roughly: “a typical value is about one SD away from the mean.”
Why n - 1? When you only have a sample, its values sit closer to their own sample mean
than to the true population mean, so dividing by n slightly underestimates the spread.
Dividing by n - 1 (Bessel’s correction) fixes that on average.
| Library | Default | Meaning |
|---|---|---|
pandas .var(), .std() |
ddof=1 |
sample (divide by n - 1) |
NumPy np.var(), np.std() |
ddof=0 |
population (divide by n) |
ddof (“delta degrees of freedom”) is what gets subtracted from n. Both libraries accept it,
so be explicit when it matters: np.std(x, ddof=1) or s.std(ddof=0).
Percentiles and quartiles
The p-th percentile is the value below which p% of the data falls. The quartiles are
Q1 = 25th, Q2 = 50th (the median) and Q3 = 75th percentile. When the requested position falls
between two sorted values, pandas .quantile() and NumPy np.percentile() use linear
interpolation by default (Excel’s PERCENTILE.INC does the same). Other tools may use a
different method and give slightly different answers on small data.
The interquartile range, IQR = Q3 - Q1, is the spread of the middle 50% of the data.
Like the median, it ignores the extremes, so it is the robust partner of the SD.
Boxplots and the 1.5 × IQR rule
A boxplot draws a box from Q1 to Q3 with a line at the median. Whiskers reach out to the most extreme values that are still inside the fences, and anything beyond is drawn as a dot:
lower fence = Q1 - 1.5 * IQR
upper fence = Q3 + 1.5 * IQR
In Python
import pandas as pd
ab = pd.read_csv("ab_test.csv")
t = ab["time_on_page_sec"]
t.max() - t.min() # range
t.var(), t.std() # sample variance and SD (ddof=1)
t.std(ddof=0) # population SD
t.quantile([0.25, 0.5, 0.75]) # several percentiles at once
q1, q3 = t.quantile(0.25), t.quantile(0.75)
outside = (t < q1 - 1.5 * (q3 - q1)) | (t > q3 + 1.5 * (q3 - q1))
outside.sum() # number of points beyond the fences
In the real world
- An outlier is a flag for investigation, not an automatic delete. In right-skewed data such as order amounts, the rule flags about 1 in 10 orders, and those are your biggest customers.
- Teams report percentiles for skewed metrics: p95 page-load time, p90 cost per click. “95% of sessions load in under 2 seconds” is clearer than an average.
- In A/B tests, decide how you’ll treat extreme values (for example, capping revenue at the 99th percentile) before you look at results; trimming after the fact can manufacture a win.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
Spread of campaign budgets
DoneLoad campaigns.csv. For the daily_budget column, create these variables, each rounded to 2 decimals:
budget_range: max minus minbudget_var: the sample variance (divide by n - 1)budget_std: the sample standard deviationbudget_pop_std: the population standard deviation (divide by n,ddof=0)
Remember: pandas .var() / .std() default to the sample version (ddof=1).
Need a hint?
budget.var()andbudget.std()use ddof=1 by default;budget.std(ddof=0)divides by n.- With only 40 campaigns, the two standard deviations differ by about 5 dollars. With thousands of rows the gap almost disappears.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
campaigns = pd.read_csv("campaigns.csv")
budget = campaigns["daily_budget"]
budget_range = None
budget_var = None
budget_std = None
budget_pop_std = None
print(budget_range, budget_var, budget_std, budget_pop_std)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Percentiles of time on page
DoneLoad ab_test.csv. For time_on_page_sec, use .quantile() with its default linear interpolation to create:
p25,p75andp95: the 25th, 75th and 95th percentiles (no rounding needed)iqr: the interquartile range,p75 - p25
Linear interpolation means a percentile can land between two data values, so p95 does not have to be a whole number even though the data are whole seconds.
Need a hint?
.quantile()takes a fraction between 0 and 1: the 95th percentile ist.quantile(0.95).np.percentile(t, 95)gives the same answer; it takes a percent from 0 to 100.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
ab = pd.read_csv("ab_test.csv")
t = ab["time_on_page_sec"]
p25 = t.min()
p75 = t.max()
p95 = t.max()
iqr = p75 - p25
print(p25, p75, p95, iqr)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)Flag outlier orders with the 1.5 × IQR rule
DoneUsing the amount of completed orders in orders.csv (default linear quantiles):
lower_fence: Q1 - 1.5 × IQRupper_fence: Q3 + 1.5 × IQRn_outliers: how many completed orders fall outside the fences (below the lower
fence or above the upper fence), as an integer
No rounding needed.
Need a hint?
- Combine conditions with
|(or) and put each comparison in parentheses:(amount < lower_fence) | (amount > upper_fence). .sum()on a True/False Series counts the True values. Wrap it inint().
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import pandas as pd
orders = pd.read_csv("orders.csv")
amount = orders.loc[orders["status"] == "completed", "amount"]
q1 = amount.quantile(0.25)
q3 = amount.quantile(0.75)
iqr = q3 - q1
lower_fence = None
upper_fence = None
n_outliers = 0
print(lower_fence, upper_fence, n_outliers)
Files in the working directory
ab_test.csv (4,000 rows)daily_stats.csv (1,561 rows)orders.csv (900 rows)campaigns.csv (40 rows)