Skip to content

04-07: Sampling & CLT Simulator

Statistics & Probability

View the live site — ijk37.com

Project 07

Home  |  All Projects  |  Notes  |  Exercises  |  Quiz Hub

Draw every sampling design from a known population, compare their precision, and then watch the Central Limit Theorem turn a badly skewed population into a normal sampling distribution.

Chapters applied: 08-01 · 08-02

Difficulty: ⭐⭐⭐ Intermediate–Advanced


The Data

data/population.csv — a complete population of 1,000 units, so every parameter is known exactly and every sample can be graded against the truth.

Column Notes
unit_id 1–1000
region North 400, South 300, East 200, West 100 — deliberately unequal
value Right-skewed within each region, with clearly different regional means

Because the strata genuinely differ, stratified sampling has something to gain here — that is the point.


What You Produce

  1. The true population parameters: μ, σ, and the regional means
  2. Four samples of the same size n = 40: simple random, systematic, stratified (proportional), cluster
  3. A precision comparison: repeat each design 2,000 times and compare the standard errors
  4. A CLT demonstration: the sampling distribution of at n = 1, 5, 30, 100
  5. A standard-error table confirming the σ/√n rule numerically
  6. A bias demonstration: what a convenience sample of the "easiest" units produces

Excel Route

Step 1 — Population parameters

=COUNT(C2:C1001)                       ' N     -> 1000
=AVERAGE(C2:C1001)                     ' mu    -- the TRUE mean
=STDEV.P(C2:C1001)                     ' sigma -- POPULATION sd, so .P not .S
=SKEW(C2:C1001)                        ' confirms the right skew

Add a PivotTable of value by region (Count and Average) to see the four regional means.

Step 2 — Simple random sample of 40

' Helper column D:  =RAND()
' Select A:D, Data ▸ Sort ▸ by column D, then take the first 40 rows.

' Excel 365 one-liner:
=INDEX(SORTBY($A$2:$C$1001, RANDARRAY(1000)), SEQUENCE(40), {1,2,3})

Step 3 — Systematic sample

=ROUNDDOWN(1000/40, 0)                 ' k = 25
=RANDBETWEEN(1, 25)                    ' random start r
=INDEX($C$2:$C$1001, $G$2 + (ROW()-2)*$G$1)      ' r, r+k, r+2k, … fill 40 rows

Step 4 — Stratified sample (proportional allocation)

=COUNTIF($B$2:$B$1001, "North")                   ' stratum size N_i
=ROUND(40*COUNTIF($B$2:$B$1001,"North")/1000, 0)  ' n_i -> 16
' repeat for South (12), East (8), West (4);  16+12+8+4 = 40  ✓
' then draw an SRS of n_i from each stratum's rows using the Step 2 method

Step 5 — Cluster sample

' Treat the 4 regions as clusters and randomly pick 1 (or use 50 blocks of 20)
=INDEX({"North";"South";"East";"West"}, RANDBETWEEN(1,4))
' then take EVERY unit in the chosen cluster(s)

Step 6 — Compare all four

Build this table and refresh it with F9 twenty times, recording each result:

Design Sample mean Error vs. μ
SRS =AVERAGE(srs_range) =ABS(mean - mu)
Systematic
Stratified
Cluster

Stratified errors will cluster tightly around zero; cluster-sample errors will swing widely, because each draw commits to one region.

Step 7 — The CLT

' 1. Column A: the 1,000 population values (already there)
' 2. In C2:AF2  (30 columns), draw one sample of 30:
=INDEX($C$2:$C$1001, RANDBETWEEN(1,1000))
' 3. AH2:  =AVERAGE(C2:AF2)      the mean of that sample
' 4. Fill rows 2:1001  -> 1,000 sample means
' 5. Histogram column C (the population)  -> strongly RIGHT-SKEWED
'    Histogram column AH (the means)      -> BELL-SHAPED and much narrower

=STDEV.S(AH2:AH1001)                   ' simulated standard error
=STDEV.P(C2:C1001)/SQRT(30)            ' theoretical sigma/sqrt(n) -> should match

Step 8 — Standard-error table

' For n = 1, 4, 9, 25, 100:
=$sigma/SQRT(n)                        ' theory
' compare with the simulated sd of the sample means at each n

Note that quadrupling n halves the standard error — the √n law, visible in your own numbers.


R Route

Rscript r/analysis.R

Runs all four designs, the 2,000-replication precision comparison, and the CLT demo, and writes sampling_plots.png. See r/analysis.R.

Python Route

python python/analysis.py

Same via NumPy + pandas. See python/analysis.py.


Checkpoints

  • σ is computed with the population formula (STDEV.P / ddof=0)
  • Proportional allocation sums to exactly n
  • Every design is shown to be unbiased (means centre on μ) but with different precision
  • Stratified has a smaller standard error than SRS; cluster has a larger one
  • The simulated standard error matches σ/√n
  • The CLT panel shows the shape changing while the centre stays put

Extend It

  • Add the finite population correction √((N−n)/(N−1)) and show it matters at n = 200 but not at n = 40
  • Build a convenience sample of the 40 largest values and quantify the bias — then show that taking 400 of them makes it worse
  • Repeat the CLT demo starting from a uniform and a bimodal population; how quickly does each become normal?
  • Add a sampling distribution of the median and compare its spread with that of the mean