Sampling, Standard Error & the Central Limit Theorem
After this lesson, you will be able to:
- Tell a population from a sample, and a parameter (a fixed truth you want) from a statistic (a number you computed that changes with every sample)
- Explain why the sample mean wobbles from sample to sample, state the Central Limit Theorem in plain words with its conditions, and compute how much it wobbles with the standard error, s divided by the square root of n
- Never again confuse standard deviation (spread of the data) with standard error (wobble of the average), and know why 4 times the data buys only 2 times the precision
- Recognize a biased sampling method and understand why a huge biased sample is confidently wrong, while a small random one is honestly uncertain
- Put an error bar on a model's accuracy with the standard error of a proportion, the square root of p(1 - p) over n, and see how it shrinks as the test set grows
Before you start
#The Core Question
A number computed from a sample is a guess about the population. How good a guess, and how can you tell? Everything in this lesson is a way of putting a size on that doubt, so that a result becomes "about 91%, give or take 1.3 points" (for a test set of 2,000 examples) instead of a falsely exact "91%".
#Population and Sample
- A parameter is a number describing the population, such as the true average order value, written with Greek letters: the mean is mu and the standard deviation is sigma. It is fixed, and almost always unknown.
- A statistic is a number you compute from the sample, such as the sample mean, written x-bar. It is known, and it changes every time you draw a new sample.
#The Sampling Distribution of the Mean
Here is the idea that unlocks everything. Suppose you could repeat your whole study many times, each time drawing a fresh sample of size n and computing its mean. Those means would not all be equal. If you collected them, they would form a distribution of their own, called the sampling distribution of the mean.
You cannot actually redo a study 2,000 times in real life, but a computer can, from a population we build ourselves. The cell below makes a deliberately ugly population (time on a page, strongly skewed to the right, most people leave quickly and a few stay forever), draws 2,000 samples of 30 people each, and plots the 2,000 means.
Try it! Run the cell, then changeN_SAMPLEto 5 and to 200 and watch the right-hand histogram.
Three things appear in that output, and each one matters.
- The means pile up around the true mean. The average of the 2,000 sample means is 59.80 against a true mean of 60.21. A sample mean has no tendency to miss in one direction, so it is called an unbiased estimator. The 0.41 gap is not a lean, it is simulation noise: the average of 2,000 means itself wobbles by about 11.42 divided by the square root of 2,000, which is 0.26, so a gap of 1.6 of those units is unremarkable. A different seed gives a different gap.
- The means are much less spread out than the individuals. The raw data has a standard deviation of 60.32 seconds, but the 2,000 sample means have a standard deviation of only 11.42. Averaging cancels noise.
- The histogram of means is a bell, even though the population is a lopsided ramp. That is the Central Limit Theorem, which the next section states properly.
The first point, that the sample means have no tendency to miss in one direction, is a short calculation. Open the derivation to follow it line by line.
The experiment above ran on a population we invented. The next demo runs on data you will use all through Track 1. StreamBox is a synthetic table of 200 app users (plan, device, minutes per session, sessions per week, whether they churned), and the demo draws samples from a 10,000-user population built to match it. Open the "Sampling distribution" tab, draw 100 samples at n = 30, then raise n to 300 and watch the bell narrow. The sampling-bias switch is there too, leave it off for now, because a few sections from here you will turn it on and see what it does. The Bootstrap tab of this demo is for the next lesson.
#The Central Limit Theorem
The simulation showed a bell appearing from a lopsided population. That is not luck, it is a theorem, and this lesson is where it is stated.
It comes with conditions, and the conditions are where it fails.
- The draws must be independent: one value must not tell you anything about the next.
- The source must have a finite variance: a spread that is a real, finite number. A source with such extreme outliers that its variance is infinite breaks the theorem, and a heavy-tailed one that merely comes close needs far more than 30 draws.
- n must not be tiny. A common rule of thumb is that n around 30 is enough for mildly skewed data, and strongly skewed data needs more. The theorem says what happens as n grows, not that any particular n is already there.
The statement above is a theorem, so it has a proof. The full proof is advanced, but its engine fits in seven short steps, and the derivation below shows it. Its assumptions are written into the first step.
Here is where "about 95%" comes from. A Normal curve puts about 68% of its values within 1 standard deviation of its center, 95% within 2 and 99.7% within 3 (the 68-95-99.7 rule from Descriptive Statistics). The CLT says the sample mean is approximately Normal, and its standard deviation is sigma over the square root of n, our wobble unit. So about 95% of sample means land within 2 of those units of mu. In the simulation, 67.1% of the 2,000 means landed within 1 unit of 60.21, 94.9% within 2 and 99.5% within 3, with a Normal curve predicting 68, 95 and 99.7.
Now see it with your own hands: pick a source, set n, and draw batches of means.
#Standard Error and the Square Root Law
Before the formula, commit to a guess.
Your estimate from a sample of 100 has some typical error. You want to cut that typical error in half. How much data do you need in total?
This formula is a result, not a definition. It follows from two facts about variance, and the derivation proves both as it goes.
Check it against the simulation above. Sigma was 60.32 and n was 30, so sigma divided by the square root of 30 is 11.01, and the simulated spread of the 2,000 means was 11.42. The formula and the experiment agree, within the noise you would expect from only 2,000 repeats.
#Sigma is unknown in practice, so use s
Why n - 1 and not n? The distances are measured from x-bar, and x-bar is the point that sits closest to your own values by construction, closer than the true mean mu would. So the squared distances come out a little too small on average, and dividing by n - 1 instead of n corrects for it. With n = 1000 the difference is invisible. With ten values it is a real 5%, and the Try It Yourself at the end of this lesson shows exactly that gap.
That "a little too small" can be made exact, and the exact answer shows why n - 1 is the right divisor.
Once sigma is replaced by s, the 1.96 of a 95% interval stops being exactly right for a small sample, and the interval needs a Student t multiplier instead. Where that distribution comes from can be derived for samples from a Normal source.
#The square root law, with numbers
Take a population with sigma = 15 and watch what each extra block of data buys.
| Sample size n | SE = 15 / sqrt(n) | Change from previous row |
|---|---|---|
| 25 | 3.00 | |
| 100 | 1.50 | 4 times the data, half the error |
| 400 | 0.75 | 4 times the data, half the error |
| 1,600 | 0.375 | 4 times the data, half the error |
| 10,000 | 0.15 |
The cell below measures this tax directly: for four sample sizes it draws 2,000 samples and compares the measured SE to the formula.
#The standard error of an accuracy
A test set is a sample of the situations your model will meet. Each example is right or wrong, so score each one 1 for right and 0 for wrong. Accuracy is then the mean of these 0/1 values, and for a 0/1 column in which a fraction p are ones, the variance is p(1 - p). Check with 100 examples, 91 right: the 91 ones each sit 0.09 from the mean 0.91, the 9 zeros each sit 0.91 away, and the variance is 0.91 x 0.09 x 0.09 + 0.09 x 0.91 x 0.91 = 0.0819, which is 0.91 x 0.09.
So sigma is the square root of p(1 - p), and the standard error of an accuracy is sigma over the square root of n: SE = sqrt(p(1 - p) / n), the whole fraction under one square root. For 91% accuracy on 2,000 test examples, SE = sqrt(0.0819 / 2,000) = 0.0064, and the 95% half-width is 1.96 x 0.0064 = 0.0125, about 1.25 points (the 1.96 is the factor that makes a Normal curve 95%, close to the 2 of the 68-95-99.7 rule). On 100 examples the same 91% has SE 0.0286 and a half-width of 5.6 points. At n = 1,000 it is 1.8 points and at n = 20,000 it is 0.4 points. Same model, same true quality; only the amount of test data changed.
The variance p(1 - p) and the final formula follow from the definition of variance in five short steps.
#Standard Deviation vs Standard Error
These two are confused more often than any other pair in statistics, and the confusion quietly ruins reports. They answer different questions.
The easiest way to keep them straight is to ask what happens as you collect more data.
- Standard deviation does not shrink. It is a property of the population. Measure a million people and the heights still vary by about 7 cm, because that is how people are.
- Standard error shrinks without limit. It measures your uncertainty about the average, and more data removes that uncertainty. With a million people the SE of the average height is 0.007 cm.
You measure the response time of 400 requests. The standard deviation is 80 ms. What does the standard error of the mean tell you?
#How You Sample Decides What You Learn
Estimate the average monthly spend of a company's customers. Option A is a simple random sample of 100 customers. Option B is an optional survey email that 1,000,000 customers could answer, and big spenders are much more likely to bother replying. Which average do you trust more?
#Three honest ways to sample
- Simple random sampling. Every member of the population has the same chance of being picked. It is the baseline that the standard error formula assumes.
- Stratified sampling. Split the population into groups that differ in ways that matter (age bands, countries, fraud versus not fraud), then sample randomly inside each group, in proportion to its size. You cannot accidentally miss a small group, and the estimate is usually more precise. This is what a
stratify=yargument does when you split a dataset for training. - Cluster sampling. Pick whole groups at random (a few schools, a few data centers), then measure everyone inside the chosen groups. It is cheap when visiting individuals is costly, but people inside a cluster resemble each other, so you get less information per person than a simple random sample would give.
#Three ways samples go quietly wrong
- Self-selection bias. People choose whether to be in your data. Online reviews come from the delighted and the furious; survey replies come from people with time and opinions.
- Survivorship bias. You only see what survived to be observed. Studying the habits of successful companies ignores the ones that failed doing the same things. A model evaluated only on users who stayed learns nothing about those who left.
- Non-response bias. Some chosen people never answer, and they differ from those who do. A random sample that only 10% reply to is no longer random.
The cell below builds a population of customers' monthly spend, then estimates the mean two ways at four sample sizes: honestly at random, and with a self-selection tilt where the chance of responding grows with how much you spend.
#Try It Yourself
Put an error bar on a 91% test accuracy two ways, with the formula and with the 0/1 variance, and check that the standard errors agree. The stretch step reruns the n versus n - 1 question on the ten job times.
Tests: Verify the formula table gives SE 0.0064 and a 1.25 point half-width at n = 2000 and SE 0.0286 and 5.61 points at n = 100, that the 0/1 variance equals p(1-p) = 0.0819 and s over root n matches the formula at 0.0064, and that the ten job times give s = 6.15 versus 5.83 and standard errors 1.94 versus 1.84.
Running the solution prints a table where n = 100 gives SE 0.0286 and a half-width of 5.61 points, n = 500 gives 0.0128 and 2.51 points, n = 1000 gives 0.0090 and 1.77 points, n = 2000 gives 0.0064 and 1.25 points, and n = 20000 gives 0.0020 and 0.40 points. The 0/1 array has variance 0.0819, exactly p(1 - p), and s over the square root of n equals the formula at 0.0064. On the ten job times, s is 6.15 with the n - 1 divisor and 5.83 with n, so the standard errors are 1.94 and 1.84. The bootstrap in the next lesson reports 1.82, which sits beside the second.
⚡ Playground: Probability Explorer → — explore sampling distributions and watch the Central Limit Theorem emerge.
#Key Takeaways
- A statistic is a guess, and it wobbles. The sample mean differs from sample to sample, and the sampling distribution of the mean is centered on the truth; averages are less spread out than the data they come from
- The Central Limit Theorem makes means bell-shaped. Averages of many independent draws are approximately Normal with mean mu and standard deviation sigma over the square root of n, whatever the source shape, so about 95% of sample means land within 2 standard errors of mu
- Standard error is sigma over the square root of n. In practice use s, which divides by n - 1. With sigma = 15 it goes 3.0, 1.5, 0.75 as n goes 25, 100, 400: 4 times the data for 2 times the precision, 100 times the data for 10 times
- Standard deviation is about the data; standard error is about the average. Standard deviation never shrinks with more data, standard error always does, and a report that says "plus or minus" without saying which, and n, is not usable
- Size fixes noise, not bias. A self-selected sample of 100,000 gave 84.03 with a standard error of 0.24 when the truth was 45.23; always ask how data got into the sample before trusting its error bar
- An accuracy is a mean of 0/1 values. Its standard error is sqrt(p(1 - p) / n): 91% on 2,000 test examples is give or take 1.25 points, and on 100 examples give or take 5.6 points
#Quick Check
Scores have a standard deviation of 15 points. You average n = 25 scores, then later n = 100 scores. What happens to the standard error of the average?