Samples and populations
Last updated
You almost never have all the data you care about. You have some customers, some days, some households: a sample. Statistics is the art of saying something true about everyone from a sample of them. In this lesson you'll draw samples from a population whose true answer you know, watch how much sample results wobble, and see the one problem that no amount of data fixes: a sample chosen the wrong way.
By the end of this lesson you can
- Explain the difference between a population and a sample
- Draw simple random and stratified samples with pandas
- Describe the sampling distribution of a mean, and how sample size changes it
- Recognise sampling bias, and say why a bigger sample doesn't fix it
Population and sample
The population is everyone (or everything) your question is about: all the chain's customers, every day the stores are open, every household in the province. A sample is the part you actually measure. A statistic like the sample mean estimates a parameter, the true value for the whole population, which you usually never see.
To learn how samples behave, it helps to cheat: make up a population, so the true answers are known. The notebooks in this lesson start by making one: all 4,800 loyalty-card customers of the four stores, with their store, age, monthly spend, and whether they follow the chain's Facebook page. Run the cells.
The true mean spend is ₱1,737.35. A random sample of 50 customers says ₱1,853.08. Four more samples of 50 say ₱2,022.72, ₱1,572.02, ₱1,970.50 and ₱1,631.74. None is wrong, and none is exact: every sample has a different mix of customers. That wobble is called sampling variation, and the rest of this stage is about measuring it.
Random sampling
A sample is only useful if it's like the population. The surest way is a random sample: every member has the same chance of being picked, so nothing about them (age, store, spending) decides who's in. Run the cells.
customers.sample(200, random_state=3)picks 200 rows at random;random_statemakes it pick the same 200 each time, so your results can be checked. Its store shares (36.5%, 25.5%, 20.5%, 17.5%) are close to the population's (35.7%, 25.0%, 21.9%, 17.4%), but not equal.- A stratified sample fixes that: split the population into groups (strata) and take the same share from each.
groupby("store").sample(frac=200 / 4800)takes about 4.2% of every store, so the sample's store shares match the population's, and no store can be left out by chance. It's worth doing when the groups differ a lot and you want each one represented.
In real life the hard part is the list to sample from (the sampling frame): you can only pick at random from people you can reach. A loyalty-card list misses customers without a card; a phone survey misses people without phones.
The sampling distribution
If every sample gives a different mean, how far off can one sample be? Find out by taking many samples and looking at all their means. Here the population is the week of 1,203 receipts from 8.1. Draw samples, one at a time and then in hundreds, and try different sizes:
Draw a sample. Its 25 receipts are shown in pink, and its mean drops onto the axis below.
The pile of sample means is the sampling distribution of the mean. Three things to notice:
- It's centred on the true mean: random samples aren't too high or too low on average.
- It's much narrower than the population. A single receipt can be anything from ₱8 to ₱507, but the mean of 25 receipts almost never strays more than about ₱30 from ₱109.
- Bigger samples make it narrower, but slowly: to halve the spread you need four times the sample size. The readout compares the spread with the formula , which lesson 8.4 explains.
Now do it in pandas with the customers: 1,000 samples of 50, then 1,000 of 200. Run the cells.
The 1,000 means of samples of 50 average ₱1,727.95, close to the true ₱1,737.35, with a standard deviation of ₱147.85; 95% of them are between ₱1,444 and ₱2,028. With samples of 200 the standard deviation is ₱71.14: four times the data, half the spread.
This is the key to the whole stage. You only ever get one sample, but if you know how wide the sampling distribution is, you know how far your one sample is likely to be from the truth.
Wrong way: a survey of followers
The owners want to know what their customers spend each month. Running a survey on the Facebook page is free and easy, so they do that. Run the cells.
1,311 of the 4,800 customers follow the page. A survey of 200 of them finds a mean spend of ₱1,356, ₱381 below the truth, and a mean age of 27, when the customers' mean age is 46. Followers are mostly younger customers, and in this population younger customers spend less.
The second cell asks every follower, all 1,311: ₱1,367. Six and a half times the data, the same wrong answer. A random 200 customers, meanwhile, get ₱1,783, within the usual wobble of the truth. Step through why:
This is sampling bias: the way the sample was chosen makes it different from the population in a way that matters. In 1936 the Literary Digest magazine got 2.4 million answers to a poll on the United States election, from lists of its readers, car owners and telephone owners, and predicted the wrong winner by a wide margin; a much smaller but better-chosen poll got it right.
The check: ask who could end up in this sample, and who couldn't? If the answer is linked to what you're measuring (age and spending, here), the sample is biased, and its size can't save it. Either sample at random from the whole population, or say clearly that the result describes only the people you sampled ("our Facebook followers").
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- pandas: DataFrame.sample: random samples of rows, repeatable with random_state
- pandas: DataFrameGroupBy.sample: a sample from each group
- Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed., chapter 1: populations, samples, sampling methods and bias
- Squire, Why the 1936 Literary Digest Poll Failed, Public Opinion Quarterly 52(1), 1988: 2.4 million answers, the wrong winner: a famous biased sample