Search⌘ K
AI Features

Sampling, Confidence Intervals, and the CLT

Explore how to use sampling to estimate population parameters and understand sampling error. Learn to calculate confidence intervals for means and proportions, applying the central limit theorem to interpret sample results, and quantifying uncertainty in data science contexts.

A city needs an estimate of this week’s average delivery time, but measuring every completed order may be too slow or costly. We can sample 30 orders and compute their mean delivery time, yet another random sample of 30 orders will usually produce a different mean even when the delivery process has not changed.

That difference is sampling error, the gap between a sample statistic such as the sample mean and the unknown population value caused by which observations happened to enter the sample. A sample is useful because it gives evidence about all deliveries. A sample is not exact because it leaves most deliveries unobserved.

The wrong intuition is that two samples should agree if they came from the same city and week. Random selection makes differing sample means expected. The practical question is how much those means vary, and how we can report that uncertainty alongside an estimate.

Sample means vary less than individual deliveries

Because individual delivery times can be right-skewed, a few delayed orders may sit far above most orders. The distribution of raw delivery times therefore may not look normal. For an average, however, we do not need every individual delivery to be normal.

Note: As before, Matplotlib (plt) draws the charts here. plt.subplots(ncols=2) creates a figure with two charts side by side, called axes[0] and axes[1], so we can compare the raw delivery times with the sample means. Each .hist() call draws one histogram, and plt.savefig() saves the figure so the output can display both.

A sampling distribution is the distribution created by calculating the same statistic across many possible random samples. Here, each value in the sampling distribution is one sample mean from 30 simulated deliveries. The raw-data distribution answers how long individual deliveries take. The sampling distribution answers how estimates of the citywide mean vary.

Python
import numpy as np
import matplotlib.pyplot as plt
def main() -> None:
rng = np.random.default_rng(42)
mu = 3.2
sigma = 0.55
population_size = 50_000
delivery_minutes = rng.lognormal(mean=mu, sigma=sigma, size=population_size)
n = 30
repetitions = 500
samples = rng.lognormal(mean=mu, sigma=sigma, size=(repetitions, n))
sample_means = samples.mean(axis=1)
fig, axes = plt.subplots(ncols=2, figsize=(12, 4), constrained_layout=True)
axes[0].hist(delivery_minutes, bins=50, color="#4C78A8", edgecolor="white")
axes[0].set_title("Right-skewed delivery times (population simulation)")
axes[0].set_xlabel("Minutes")
axes[0].set_ylabel("Count")
axes[1].hist(sample_means, bins=30, color="#F58518", edgecolor="white")
axes[1].set_title("Sampling distribution: 500 sample means (n=30)")
axes[1].set_xlabel("Sample mean (minutes)")
axes[1].set_ylabel("Count")
plt.savefig("output/sampling_distribution.png")
plt.show()
if __name__ == "__main__":
main()
  • Line 6: default_rng(42) seeds the generator, as in the earlier NumPy lesson, so reruns produce the same histograms.

  • Lines 8–12: A lognormal model produces the intended right-skew (long right tail) for individual delivery times without requiring them to look normal.

  • Lines 17–18: Drawing a 500×30 matrix and taking mean(axis=1) constructs the sampling distribution: each element of sample_means is one sample mean from an independent sample of size 30.

  • Lines 20–30: The side-by-side histograms directly contrast the raw delivery-time distribution with the tighter, more symmetric sampling distribution of means (the CLT effect).

Note: If you are running this code locally, you can use plt.show() to display the plot in a separate window. In this Educative environment, we use plt.savefig() instead because the code runs in a container without a graphical interface. Saving the figure allows Educative to display the generated plot in the output.

This code shows a right-skewed set of simulated delivery times and a much more balanced histogram of 500 sample means. The sample means cluster near the process’s average because a very long delivery has less influence when combined with 29 other deliveries.

The central limit theorem (CLT) states that the sampling distribution of the mean becomes approximately normal as sample size grows, even when the original observations are skewed. The theorem depends on observations being independent enough that one delivery does not determine another, and on the population not being dominated by extreme behavior. Strong dependence, such as repeated measurements from the same order, can make the usual uncertainty calculation too optimistic.

Two distributions: A histogram of delivery times describes individual orders. A histogram of sample means describes repeated estimates of the citywide average.

The CLT does not say that delivery times become normal. It says that averages from similarly sized random samples often have an approximately normal sampling distribution. That distinction lets us quantify uncertainty around the unknown average delivery time. ...