Section 3 · Lesson 1
Samples, sampling distributions and the central limit theorem
Why sample statistics vary, what a sampling distribution is, and how the central limit theorem makes averages predictable.
You will almost never see the whole population: every user who will ever land on the page, every order a store will ever take. You see a sample and use it to make a claim about the population. This lesson is about the most important fact in statistics: how much a sample statistic wobbles from sample to sample, and why that wobble is predictable.
Vocabulary
- Population: everything you want to know about (all future visitors).
- Parameter: a number describing the population (the true conversion rate). Usually unknown.
- Sample: the part you actually observe (the 4,000 users in this test).
- Statistic: a number computed from the sample (the observed conversion rate). We use statistics to estimate parameters.
A sample only tells you about the population if it was chosen fairly. Taking “the first 30 rows”, only weekday traffic, or only people who answered a survey can give a biased picture no matter how big the sample is. Random sampling, and random assignment in experiments, is what makes the math in this section valid.
Sampling variability and sampling distributions
Draw a random sample of 30 orders and compute the mean. Draw another 30: you get a different mean. The sampling distribution of the mean is the distribution of all those means across many hypothetical samples of the same size. Its standard deviation has a special name, the standard error (SE). It measures how far a sample mean typically lands from the truth.
In real projects you get one sample, so you can’t see the sampling distribution directly. In this lesson we cheat: we treat the 768 completed orders as the population and simulate thousands of samples from it.
The central limit theorem (CLT)
For random samples of size n from a population with mean μ and standard deviation σ:
mean of the sample means = μ
SE of the mean = σ / sqrt(n)
shape → approximately normal as n grows
The surprising part is the last line: it holds even when the population is skewed. Order amounts have skewness around 5, yet the means of samples of 40 orders already have skewness below 1, and the means of larger samples look like a bell curve. How big n must be depends on the skew: about 30 is plenty for mildly skewed data, while very heavy tails need more.
In Python
NumPy’s random Generator makes simulations reproducible: the same seed gives the same draws.
import numpy as np
rng = np.random.default_rng(42) # seeded generator
rng.choice(amounts, size=30, replace=False) # one sample of 30 distinct values
draws = rng.choice(amounts, size=(2000, 40)) # 2,000 samples of 40, as a 2-D array
means = draws.mean(axis=1) # axis=1: average across each row
np.std(means, ddof=1) # simulated standard error
Sampling with replacement (the default) mimics drawing from a much larger population.
In the real world
- The CLT is why you can run a t-test or build a confidence interval on revenue per user even though that metric is mostly zeros with a long tail: with thousands of users, the average is close to normal.
- It is also a warning: with small samples of heavy-tailed metrics, one big spender can swing the mean. Small campaigns look wildly better or worse day to day partly because n is small.
- Because SE shrinks with sqrt(n), getting twice the precision costs four times the traffic. That trade-off drives how long experiments must run.
Practice
Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.
One random sample
DoneTreat the amount of all completed orders in orders.csv as the population.
pop_mean: the population meansample: a random sample of 30 amounts drawn without replacement, using
rng = np.random.default_rng(42) and rng.choice(amounts, size=30, replace=False)
sample_mean: the mean ofsamplesampling_error:sample_mean - pop_mean
No rounding needed.
Need a hint?
rng.choice(amounts, size=30, replace=False)picks 30 different orders at random..to_numpy()turns a pandas column into a NumPy array, whichrng.choiceworks with directly.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
orders = pd.read_csv("orders.csv")
amounts = orders.loc[orders["status"] == "completed", "amount"].to_numpy()
rng = np.random.default_rng(42)
pop_mean = amounts.mean()
sample = None
sample_mean = None
sampling_error = None
print(pop_mean, sample_mean, sampling_error)
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)Simulate a sampling distribution
DoneStill treating completed order amounts as the population, use rng = np.random.default_rng(42) to draw 2,000 samples of size 40 (with replacement) and record each sample's mean.
sample_means: a NumPy array (or list) of the 2,000 sample meansse_simulated: the standard deviation ofsample_means(ddof=1)se_formula: the CLT prediction, population SD (ddof=0) divided bysqrt(40)
The two standard errors should be close. No rounding needed.
Need a hint?
- Loop version:
for _ in range(2000): sample_means.append(rng.choice(amounts, size=40).mean()). - Vectorized version:
rng.choice(amounts, size=(2000, 40)).mean(axis=1)draws all samples at once and averages each row. np.std(x)uses ddof=0 by default; passddof=1for the sample version.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
orders = pd.read_csv("orders.csv")
amounts = orders.loc[orders["status"] == "completed", "amount"].to_numpy()
rng = np.random.default_rng(42)
n, n_samples = 40, 2000
sample_means = []
# TODO: draw n_samples samples of size n and append each mean
se_simulated = None
se_formula = None
print(se_simulated, se_formula)
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)Four times the data, half the noise
DoneUse rng = np.random.default_rng(42) and the completed order amounts again. Simulate 2,000 sample means with samples of size 10, then 2,000 sample means with samples of size 160 (with replacement).
sd_10: the standard deviation (ddof=1) of the n = 10 sample meanssd_160: the standard deviation (ddof=1) of the n = 160 sample meansratio:sd_10 / sd_160, rounded to 2 decimals
The CLT predicts sqrt(160 / 10) = 4. Your simulated ratio will be close, not exact.
Need a hint?
rng.choice(amounts, size=(2000, 10)).mean(axis=1)gives 2,000 means of samples of size 10.- Standard error scales with 1 / sqrt(n): 16 times the sample size gives 1/4 the spread.
Practice tasks, quizzes and the certificate are part of the course. The first lesson is free to try.
import numpy as np
import pandas as pd
orders = pd.read_csv("orders.csv")
amounts = orders.loc[orders["status"] == "completed", "amount"].to_numpy()
rng = np.random.default_rng(42)
sd_10 = None
sd_160 = None
ratio = None
print(sd_10, sd_160, ratio)
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)