Sampling
Simple random, systematic, stratified and cluster sampling, bias, sample size and allocation, with simulations and Julia code.
https://morrisonkuhlsen.com/en/statistical-inference/sampling
Sampling is choosing part of a population (the sample) in order to draw conclusions about the whole population from it. A well-chosen sample makes it possible to estimate means, proportions and other quantities with known precision, at a fraction of the cost of a census. A badly chosen sample gives wrong answers, and making it bigger does not fix that.
For inference from a sample to be valid, three things matter:
- Representativeness: the sample must reflect the population in the characteristics that matter for the study. A biased sample distorts the estimates, whatever its size (section 4).
- A draw with known probabilities (in probability methods): each element of the population has a known, nonzero probability of entering the sample. That is what allows margins of error to be computed.
- An adequate size: the standard error of the mean is $\sigma/\sqrt{n}$, so precision grows with the square root of $n$. With $n = 100$, the standard error is $\sigma/10$; with $n = 400$, it is $\sigma/20$. Quadrupling the sample halves the standard error.

1. Why Sample?
- Cost and time: a census is usually too expensive and too slow.
- Destructive testing: when measuring destroys the item (material strength, product lifetime), you cannot test everything.
- Large populations: with millions of elements, a sample is the only feasible route.
- Quality control: instead of inspecting all production, standardized sampling plans are used (in Brazil, the ABNT NBR 5426 standard, from 1985, sets out plans for inspection by attributes).
- Data quality: with fewer elements, you can measure more things, more carefully, for each one.
2. Basic Concepts
- Population (size $N$): the complete set of elements of interest.
- Sample (size $n$): the part of the population that is actually observed.
- Parameter: a number describing the population, such as the mean $\mu$, the standard deviation $\sigma$ or the proportion $p$. It is usually unknown.
- Statistic: a number computed from the sample, such as the sample mean $\bar{x}$, the sample standard deviation $s$ or the sample proportion $\hat{p}$. It is used to estimate the parameter.
- Sampling frame: the list of elements from which the sample is drawn. If the frame leaves out part of the population, the sample inherits that coverage bias, even with a perfect random draw.
- Sampling error: the difference between the statistic and the parameter that arises only because part of the population was observed. For the mean of a simple random sample, $\bar{X}$ is right on average ($E[\bar{X}] = \mu$): sampling error has no direction, and it shrinks as $n$ grows.
- Sampling fraction: $f = n/N$, the share of the population that enters the sample. When $f$ is small (the usual rule of thumb is $f < 0.05$), the population can be treated as infinite in the formulas.
- Bias: a systematic error that always pushes the estimates the same way. Unlike sampling error, it does not shrink as $n$ grows: it can only be fixed in the design of the study.
3. Sampling Methods
Probability methods (3.1 to 3.4) draw the elements with known probabilities and allow margins of error to be computed. Non-probability methods (3.5 to 3.8) do not: they are useful for exploratory studies, but they give no basis for a margin of error.
3.1 Simple Random Sampling (SRS)
How it works: every subset of $n$ elements of the population has the same chance of being the sample; in particular, each element has the same probability $n/N$ of being chosen. The draw uses a complete list and random numbers.
Pros: simple to understand and to analyze; it is the reference for the margin-of-error formulas. Cons: it needs the complete list of the population, and it can be costly if the elements are spread out.
Example: a class has 10 students (A to J) and we want to draw 3. We number the students 1 to 10 and draw 3 different numbers; if they come out as 2, 7 and 9, the sample is B, G and I. On the computer, with the fixed seed below, the draw gives F, G and J:
using Random, StatsBase
students = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
Random.seed!(123) # fixed seed: the draw can be repeated
sample_ = sample(students, 3; replace=false) # 3 students, without replacement
println("Selected sample: ", sample_)
Expected output:
3.2 Systematic Sampling
How it works: with the population in a list, compute the interval $k = N/n$, draw a starting point between 1 and $k$, and pick one element every $k$.
Pros: easier to carry out than SRS, especially for long lists or on a production line. Cons: if the list has a pattern that repeats with a period close to $k$ (a shift rota, for example), the sample may always land on the same kind of element.
Example: 20 employees, a sample of 5. The interval is $k = 20/5 = 4$; drawing the start 3, the positions picked are 3, 7, 11, 15 and 19.
using Random
N, n = 20, 5
k = N ÷ n # sampling interval: 20 ÷ 5 = 4
Random.seed!(42)
start = rand(1:k) # random starting point between 1 and k
positions = start .+ k .* (0:n-1)
println("Start = ", start, " positions: ", collect(positions))
Expected output:
3.3 Stratified Sampling
How it works: the population is divided into strata, non-overlapping groups that are, ideally, homogeneous inside (course, region, income bracket). A random sample is drawn within each stratum. The number of elements per stratum may be proportional to the stratum’s size or follow another rule (section 6).
Pros: it guarantees that every stratum shows up in the sample and, when the strata differ from one another, it gives more precise estimates than SRS with the same $n$. Cons: you need to know, beforehand, which stratum each element belongs to.
Example: 100 students, 60 in course A and 40 in course B; a sample of 10. With proportional allocation: $10 \cdot \frac{60}{100} = 6$ students from course A and $10 \cdot \frac{40}{100} = 4$ from course B, drawn within each course.
using Random, StatsBase
course_A = ["A$i" for i in 1:60]
course_B = ["B$i" for i in 1:40]
Random.seed!(7)
sample_ = vcat(sample(course_A, 6; replace=false), # 60% of 10
sample(course_B, 4; replace=false)) # 40% of 10
println("Stratified sample: ", sample_)
Expected output:
3.4 Cluster Sampling
How it works: the population is already divided into natural groups, the clusters (neighborhoods, schools, batches). Some clusters are drawn and every element in them is observed (or a sample within them, in two-stage sampling).
Pros: cheap when the population is spread out, and it does not need a list of all the elements, only of the clusters. Cons: elements in the same cluster tend to be alike, so, for the same $n$, precision is usually lower than with SRS. Ideally, each cluster is varied inside, like a miniature of the population.
Example: 5 neighborhoods with 100 households each; 2 neighborhoods are drawn and all 100 households in each are interviewed.
using Random, StatsBase
districts = ["B1", "B2", "B3", "B4", "B5"]
Random.seed!(21)
drawn = sample(districts, 2; replace=false)
println("Districts drawn: ", drawn, " → interview all 100 households in each")
Expected output:
3.5 Convenience Sampling
Made up of whoever is easiest to reach: the first 10 people walking by in a mall, the students in the researcher’s own class. It is fast and cheap, useful for trying out a questionnaire, but the people who are easy to reach usually differ from the rest of the population, and there is no way to measure that bias.
3.6 Quota Sampling
The researcher is given quotas to fill (for example, so many women and so many men, in given age brackets) and interviews whoever they find until the quotas are met. The quotas make the sample’s makeup resemble the population’s on the chosen variables, but the choice within each quota is not random, and there may be bias in everything that was not controlled.
3.7 Judgment (Purposive) Sampling
An expert picks the elements they consider most suitable, such as the 5 leading companies in a sector for a case study. It is useful when those very cases are of interest, but the results do not generalize to the population.
3.8 Snowball Sampling
The first participants refer others, who refer others, and so on. It helps to reach populations that are hard to list (small or hidden groups), but the sample tends to concentrate in the contact networks of the first participants.
3.9 Comparison
| Method | Type | Needs a list of everyone? | Best use |
|---|---|---|---|
| Simple random | Probability | Yes | The reference; accessible populations with a list |
| Systematic | Probability | Yes (ordered) | Long lists with no periodic patterns |
| Stratified | Probability | Yes, with each one's stratum | Populations with groups that differ |
| Cluster | Probability | Only of the clusters | Geographically spread populations |
| Convenience | Non-probability | No | Questionnaire tests, pilot studies |
| Quota | Non-probability | No | Quick market and opinion surveys |
| Judgment | Non-probability | No | Case studies |
| Snowball | Non-probability | No | Hard-to-reach populations |
4. Errors in Sample Surveys
Every result from a sample survey is subject to two kinds of error, of very different natures.
Sampling error: the variation that exists because each sample is a different part of the population. In probability samples, it is measured by the margin of error and it shrinks as $n$ grows.
Non-sampling errors: they do not shrink as $n$ grows, which makes them the most dangerous:
| Type | What it is | How to reduce it |
|---|---|---|
| Coverage | The frame leaves out part of the population | Update the frame; combine sources |
| Nonresponse | Some of those drawn do not respond, and non-respondents differ from respondents | Repeated contact attempts; compare the respondents' profile with the population's |
| Measurement | A badly worded question, an uncalibrated instrument, an interviewer who leads the answer | Pretest the questionnaire; train interviewers |
| Processing | Data entry, coding or analysis mistakes | Automatic validation; double checking |
Simulation: bias does not shrink with $n$. In an illustrative population of 100,000 people, the proportion who support a proposal is about 0.46. A draw from the whole population lands close to that with any $n$. A draw from a frame that holds all of the group more favorable to the proposal, but only 20% of the other group, is always wrong in the same direction, around 0.61, with $n$ = 100, 1,000 or 10,000:
using Random, Statistics, StatsBase
# Illustrative population of 100,000 people: group 1 (40%) with 70% support for a
# proposal, group 2 (60%) with 30% support. True proportion: 0.4·0.7 + 0.6·0.3 = 0.46.
Random.seed!(123)
group = vcat(fill(1, 40_000), fill(2, 60_000))
supports = [g == 1 ? rand() < 0.7 : rand() < 0.3 for g in group]
println("True proportion: ", round(mean(supports), digits=4))
# Biased frame: it holds all of group 1 and only 20% of group 2
in_frame = [g == 1 || rand() < 0.2 for g in group]
frame = findall(in_frame)
for n in (100, 1_000, 10_000)
good = mean(supports[sample(1:length(supports), n; replace=false)]) # draw from the whole population
bad = mean(supports[sample(frame, n; replace=false)]) # draw from the biased frame
println("n = ", rpad(n, 6), " SRS of the population: ", round(good, digits=3),
" SRS of the biased frame: ", round(bad, digits=3))
end
Expected output:
5. Sample Size
The formulas in this section assume simple random sampling; they are derived in Confidence Intervals. In all of them, $z_{\alpha/2}$ is the standard normal critical value (1.96 for 95% confidence) and $E$ is the largest margin of error wanted. The result is always rounded up.
5.1 Proportion
\[n = \frac{z_{\alpha/2}^2\; p^{\ast}(1-p^{\ast})}{E^2}\]where $p^{\ast}$ is a prior estimate of the proportion (from an earlier survey, for example). With no estimate, use $p^{\ast} = 0.5$, which gives the largest possible $n$.
Example 1 (illustrative numbers): a factory wants to estimate the proportion of defective parts with a margin of 3 percentage points and 95% confidence; from earlier batches, it expects about 10% defects.
\[n = \frac{1.96^2 \cdot 0.10 \cdot 0.90}{0.03^2} = \frac{3.8416 \cdot 0.09}{0.0009} = \frac{0.345744}{0.0009} \approx 384.16\]Rounding up: 385 parts. (With the unrounded critical value, 1.959964, it comes to 384.15, and the answer is the same.)
Example 2 (illustrative numbers): an election poll wants a margin of 2 percentage points and 95% confidence; earlier polls put the candidate’s voting intention near 40%.
\[n = \frac{1.96^2 \cdot 0.40 \cdot 0.60}{0.02^2} = \frac{3.8416 \cdot 0.24}{0.0004} = \frac{0.921984}{0.0004} = 2304.96\]Rounding up: 2,305 voters. Note the price of precision: going from a 3-point to a 2-point margin multiplies $n$ by $(3/2)^2 = 2.25$ (for the same $p^{\ast}$).
using Distributions
z = quantile(Normal(), 0.975) # 95% confidence
sample_size(p, E) = z^2 * p * (1 - p) / E^2
for (name, p, E) in (("Defective parts", 0.10, 0.03), ("Voting intention", 0.40, 0.02))
n = sample_size(p, E)
println(rpad(name, 18), " n = ", round(n, digits=2), " → ", ceil(Int, n))
end
Expected output:
5.2 Mean
With $\sigma$ known (or reliably estimated from an earlier study):
\[n = \left(\frac{z_{\alpha/2}\;\sigma}{E}\right)^2\]When $\sigma$ is only known through an estimate $s$ from a pilot study, the critical value becomes Student’s t, $t_{\alpha/2;\,n-1}$, which depends on $n$ itself. We then look for the smallest $n$ that satisfies
\[\left(\frac{t_{\alpha/2;\,n-1}\cdot s}{E}\right)^2 \leq n\]Simply repeating the calculation “until it converges” does not work: in the example below ($s = 2.4$, $E = 0.8$, 95%), the repetition keeps alternating between 37 and 38 forever. Testing both values: with $n = 37$, $t_{0.025;\,36} = 2.0281$ and $(2.0281 \cdot 2.4/0.8)^2 \approx 37.02$, which exceeds 37, so 37 is not enough; with $n = 38$, $t_{0.025;\,37} = 2.0262$ and $(2.0262 \cdot 3)^2 \approx 36.95 \leq 38$. The answer is $n = 38$.
using Distributions
# n for the mean with σ unknown: the t critical value depends on n itself.
# Find the smallest n such that t(n−1) · s / √n ≤ E, that is, (t · s / E)² ≤ n.
function n_mean_t(s, E; α=0.05)
n = 2
while (quantile(TDist(n - 1), 1 - α/2) * s / E)^2 > n
n += 1
end
return n
end
s, E = 2.4, 0.8
n = n_mean_t(s, E)
for m in (n - 1, n)
t = quantile(TDist(m - 1), 0.975)
println("n = ", m, ": t = ", round(t, digits=4), " (t·s/E)² = ", round((t * s / E)^2, digits=2))
end
println("n required = ", n)
Expected output:
5.3 Finite Population
When the sample is a sizable fraction of the population, the finite population correction lowers the $n$ needed:
\[n = \frac{n_0}{1 + \dfrac{n_0 - 1}{N}}, \qquad n_0 = \frac{z_{\alpha/2}^2\, p^{\ast}(1-p^{\ast})}{E^2}\]Multiplying the top and bottom by $N E^2$, the same formula becomes:
\[n = \frac{N\, z_{\alpha/2}^2\, p^{\ast}(1-p^{\ast})}{E^2 (N-1) + z_{\alpha/2}^2\, p^{\ast}(1-p^{\ast})}\]Example: $N = 5{,}000$, 95% confidence, $E = 0.05$, $p^{\ast} = 0.5$.
- Numerator: $5000 \cdot 1.96^2 \cdot 0.5 \cdot 0.5 = 5000 \cdot 3.8416 \cdot 0.25 = 4802$.
- Denominator: $0.05^2 \cdot 4999 + 3.8416 \cdot 0.25 = 0.0025 \cdot 4999 + 0.9604 = 12.4975 + 0.9604 = 13.4579$.
- Division: $n = \frac{4802}{13.4579} \approx 356.82$.
Rounding up: 357, against 385 without the correction.
using Distributions
N = 5000 # population size
z = quantile(Normal(), 0.975)
p, E = 0.5, 0.05
n0 = z^2 * p * (1 - p) / E^2 # infinite population
n = n0 / (1 + (n0 - 1) / N) # finite population correction
println("n0 = ", round(n0, digits=2), " corrected n = ", round(n, digits=2), " → ", ceil(Int, n))
Expected output:
6. Allocation in a Stratified Sample
Once the total size $n$ is decided, it has to be split among the strata. Let $N_h$ be the size of stratum $h$ in the population and $n_h$ the number of elements drawn from it.
Proportional allocation: each stratum gets a share of $n$ proportional to its size,
\[n_h = n \cdot \frac{N_h}{N}\]Neyman (optimal) allocation: it also takes into account the standard deviation $\sigma_h$ of each stratum, and gives more sample to the more variable strata,
\[n_h = n \cdot \frac{N_h\, \sigma_h}{\sum_i N_i\, \sigma_i}\]For a fixed $n$ and equal cost per element, it is the allocation that minimizes the variance of the stratified mean.
Example (illustrative numbers): a university with 10,000 students, 6,000 in the sciences and 4,000 in the humanities; a sample of $n = 400$. A pilot study found grade standard deviations of 10 in the sciences and 20 in the humanities.
- Proportional: $n_A = 400 \cdot \frac{6000}{10000} = 240$ and $n_B = 400 \cdot \frac{4000}{10000} = 160$.
- Neyman: the denominator is $6000 \cdot 10 + 4000 \cdot 20 = 60{,}000 + 80{,}000 = 140{,}000$; so $n_A = 400 \cdot \frac{60{,}000}{140{,}000} \approx 171.43$ and $n_B = 400 \cdot \frac{80{,}000}{140{,}000} \approx 228.57$, rounded to 171 and 229 (adding up to 400). The humanities, more variable, get more sample even though they are the smaller stratum.
n = 400
N = [6000, 4000] # stratum sizes (Sciences, Humanities)
σ = [10, 20] # standard deviations from a pilot study
proportional = n .* N ./ sum(N)
neyman = n .* (N .* σ) ./ sum(N .* σ)
println("Proportional: ", proportional)
println("Neyman: ", round.(neyman, digits=2), " → ", round.(Int, neyman))
Expected output:
Simulation: how much stratification gains. In an illustrative population with these two strata (means 50 and 80, standard deviations 10 and 20), we draw 5,000 samples of 400 with each method and measure the standard deviation of the 5,000 estimates of the mean:
using Random, Statistics, StatsBase
# Illustrative population: stratum A with 6,000 grades (mean 50, standard deviation 10)
# and stratum B with 4,000 grades (mean 80, standard deviation 20)
Random.seed!(123)
A = 50 .+ 10 .* randn(6000)
B = 80 .+ 20 .* randn(4000)
pop = vcat(A, B)
μ = mean(pop)
# Stratified estimator: the mean of each stratum, weighted by the stratum size
strat(nA, nB) = (6000 * mean(sample(A, nA; replace=false)) +
4000 * mean(sample(B, nB; replace=false))) / 10_000
repetitions = 5_000
srs = [mean(sample(pop, 400; replace=false)) for _ in 1:repetitions]
prop = [strat(240, 160) for _ in 1:repetitions] # proportional allocation
neyman = [strat(171, 229) for _ in 1:repetitions] # Neyman allocation
println("Population mean = ", round(μ, digits=3))
println("Standard deviation of the estimate, n = 400:")
println(" simple random ", round(std(srs), digits=3))
println(" stratified (prop.) ", round(std(prop), digits=3))
println(" stratified (Neyman) ", round(std(neyman), digits=3))
Expected output:
Proportional stratified sampling errs about 30% less than SRS (0.722 against 1.020), and Neyman a little less still (0.684). The gain comes from the strata having very different means (50 and 80): SRS still varies in the share of each course it happens to draw, and stratified sampling does not.
7. How to Choose the Method
| # | Question | If yes | If no |
|---|---|---|---|
| 1 | Is the goal only to explore or to test a questionnaire? | Convenience may be enough (no margin of error) | Use a probability method: go to 2 |
| 2 | Is there a complete, up-to-date list of the population? | Go to 3 | Cluster (a list of the groups is enough) |
| 3 | Is the population spread out, making each visit costly? | Cluster (or two-stage) | Go to 4 |
| 4 | Are there known groups, different from one another, that matter for the study? | Stratified | Go to 5 |
| 5 | Is the list long and free of periodic patterns? | Systematic | Simple random |
🎧 Podcast: Sampling
An audio episode, from this site, on the sampling methods covered on this page (in Portuguese):
References
- Bolfarine, H.; Bussab, W. O. (2005). Elementos de Amostragem. Blucher. (in Portuguese)
- Bussab, W. O.; Morettin, P. A. (2023). Estatística Básica. 10th ed. Saraiva Uni. (in Portuguese)
- Cochran, W. G. (1977). Sampling Techniques. 3rd ed. Wiley.
- Lohr, S. L. (2009). Sampling: Design and Analysis. 2nd ed. Brooks/Cole.
- Squire, P. (1988). Why the 1936 Literary Digest Poll Failed. Public Opinion Quarterly, 52(1), 125–133. DOI: 10.1086/269085.
- Triola, M. F. (2024). Introdução à Estatística. 14th ed. LTC. (in Portuguese; translation of Elementary Statistics)