Bonus: Animated Sampling Distribution

Bonus: Animated Sampling Distribution#

What happens if we repeatedly take samples from a population? Each sample has a different mean. When we collect many sample means, they form a distribution. This animation shows:

  1. Draw a sample

  2. Compute sample mean

  3. Add it to histogram

  4. Repeat

Watch how randomness becomes structure.

### Install Pillow (GIF support)
!pip install pillow
Requirement already satisfied: pillow in /opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages (12.1.1)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# reproducibility
np.random.seed(42)

# population parameters
population_mean = 50
population_std = 10

# sampling parameters
sample_size = 30
num_frames = 200

sample_means = []

fig, ax = plt.subplots(figsize=(8,5))

def update(frame):
    ax.clear()

    # draw a random sample
    sample = np.random.normal(population_mean, population_std, sample_size)
    sample_mean = sample.mean()

    sample_means.append(sample_mean)

    # histogram of sample means
    ax.hist(sample_means, bins=20)

    # show true population mean
    ax.axvline(population_mean, linestyle="--")

    ax.set_title(f"Sampling Distribution (n={sample_size})")
    ax.set_xlabel("Sample Mean")
    ax.set_ylabel("Frequency")

anim = FuncAnimation(fig, update, frames=num_frames, interval=100)
anim.save("sampling_distribution.gif", writer="pillow", fps=10)
print("GIF saved!")

from IPython.display import Image
Image("sampling_distribution.gif")
GIF saved!
../_images/10b3c39cda386f87d38fec7f609343f156a521d4fbdab98bf88151412a2d6887.gif ../_images/1d0f7fbf8d1f4f04591ccd40c43264697a98b93e4e7aa917adb0f3074b6e19e2.png

Notice:

  • At first → sample means are scattered

  • Over time → they form a bell-shaped curve

This happens even if the population is random.

This is the foundation of hypothesis testing. Remember:

  • Small samples → wide distribution -Large samples → narrow distribution

Task: You can compare Sample Sizes Animation

sample_size = 5   # try 5, 30, 100