Menu
CoddyTech

The Sampling Distribution

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 21 of 64.

Video sessions are right-skewed: most last a few minutes, a few run two hours. Yet the means of 1000 samples of 40 sessions form a bell:

Two histograms on one chart. Single sessions in gray pile up near 0 minutes with a long tail to 120. The means of 40 sessions form a narrow blue bell around the population mean of 30 minutes

The histogram of many sample means is the sampling distribution. With enough values in each sample, it is a bell centered on the population mean, even for skewed data:

means = sample_means(sessions, 40, 1000)  # your function from Sample Means Vary
ax.hist(means, bins=12)

That is the central limit theorem. It makes averages predictable: a model's mean error over a test set is roughly normal too.

challenge icon

Challenge

Medium

A cloud drive's 6000 file sizes in files (MB) are right-skewed: many documents, a few videos. The program reads a sample size; the starter has sample_means and two charts.

  1. Get 500 sample means of that size.
  2. Print mean minus median for the files, then the means (2 decimals).
  3. Histogram the files on axs[0] and the means on axs[1], bins=12, and show.

For another drive and 60:

Files: mean - median = 25.28 MB
Sample means: mean - median = 0.55 MB

Two histograms side by side. All Files: a tall first bar and a long thin tail to the right. Sample Means: a symmetric bell

Then Coddy reports each chart. A gap near 0 means the skew is gone.

Try it yourself

import numpy as np
import matplotlib.pyplot as plt

size = int(input())

# Don't change the lines below: the size in MB of every file in the drive
rng = np.random.default_rng(21)
files = np.round(rng.random(6000) ** 4 * 300 + 5, 2)


# Your function from Sample Means Vary: the means of count samples
def sample_means(data, size, count):
    means = []
    for i in range(count):
        sample = rng.choice(data, size=size, replace=False)
        means.append(sample.mean())
    return np.array(means)


fig, axs = plt.subplots(1, 2)
axs[0].set_title("All Files")
axs[0].set_xlabel("File size (MB)")
axs[1].set_title("Sample Means")
axs[1].set_xlabel("Mean file size (MB)")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler