Biostatistics · Introductory

Central limit theorem

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

The central limit theorem says that the average of many independent observations tends to follow a normal distribution, whatever the shape of the original data, provided the variance is finite. As the sample size grows, the distribution of sample means becomes more bell-shaped and narrower. This is why many statistical methods based on the normal distribution work for means even with skewed biological data.

Intuition

Imagine measuring hospital length of stay, which is strongly right-skewed: most patients stay a few days, a few stay for months. Take one patient and the value is unpredictable. Now average 30 patients. Unusually long stays happen, but they are diluted by many typical stays, and highs and lows tend to cancel. Repeat this many times and plot the averages: they pile up symmetrically around the true population mean, forming a bell shape. Bigger samples cancel more, so the bell gets narrower, with spread equal to the population standard deviation divided by the square root of n. In the simulation, you draw repeated samples from a skewed population, record each mean, and watch the histogram of means turn into a normal curve as n increases.

Key equations

X‾n=1n∑i=1nXi
(1)

The sample mean of n independent observations.

E(X‾n)=μ,SD(X‾n)=σn
(2)

The mean of sample means equals the population mean; their spread (the standard error) shrinks with the square root of n.

Zn=X‾n−μσ/n→dN(0,1)
(3)

The standardized sample mean approaches the standard normal distribution as n becomes large.

Derivation

  1. Let X1,...,Xn be independent with the same mean mu and variance sigma squared.
  2. By linearity of expectation, E of the mean = (1/n)(nμ)=μ.
  3. By independence, variances add: Var of the sum = n sigma squared.
  4. Dividing the sum by n divides the variance by n squared, so Var of the mean = sigma squared / n, and the standard deviation is sigma / sqrt(n).
  5. Standardizing by subtracting μ and dividing by σ/n gives a variable with mean 0 and variance 1 for every n.
  6. The central limit theorem (proved with moment generating or characteristic functions) states that this standardized variable converges in distribution to the standard normal. Higher-order features such as skewness shrink like 1/n.

Worked example

Problem

Serum triglyceride in a population is right-skewed with mean 150 mg/dL and standard deviation 60 mg/dL. For a random sample of 36 people, what is the approximate probability that the sample mean exceeds 165 mg/dL?

  1. Standard error = 60 / 36 = 60 / 6 = 10 mg/dL.
  2. By the CLT, the sample mean is approximately normal with mean 150 and SD 10.
  3. Z = (165 - 150) / 10 = 1.5.
  4. P(Z > 1.5) = 1 - 0.9332 = 0.0668.

Answer About 6.7%

Common mistakes

  • Thinking the raw data become normal; only the distribution of the sample mean does.
  • Confusing standard deviation (spread of individuals) with standard error (spread of means).
  • Assuming n = 30 always suffices; heavily skewed data or outliers need larger n.
  • Applying it to non-independent data, such as repeated measures on the same patient, without adjustment.
  • Forgetting that the theorem requires finite variance; heavy-tailed distributions can fail.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

scene.py(178 lines)
import sys
import numpy as np
from manim import *
from blocks import *


def generate_clt_data(pop_func, n_samples=2000, n_per_sample=30):
    """Generate sample means and population data for a given population function."""
    np.random.seed(42)
    # Generate population data for visualization
    pop_data = pop_func(n_samples * 10)
    
    # Generate sample means
    means = []
    for _ in range(n_samples):
        sample = pop_func(n_per_sample)
        means.append(np.mean(sample))
    means = np.array(means)
    
    return pop_data, means


def uniform_pop(size):
    return np.random.uniform(0, 10, size)

def exponential_pop(size):
    return np.random.exponential(2, size)

def bimodal_pop(size):
    part1 = np.random.normal(2, 0.5, size // 2)
    part2 = np.random.normal(8, 0.5, size // 2)
    return np.concatenate([part1, part2])


class Main(Scene):
    def construct(self):
        self.camera.background_color = "#111111"
        
        # Define scenarios
        scenarios = [
            {"name": "Uniform", "func": uniform_pop, "color": C_MAIN},
            {"name": "Exponential", "func": exponential_pop, "color": C_BAD},
            {"name": "Bimodal", "func": bimodal_pop, "color": C_ACCENT}
        ]
        
        TitleCard(self, "Central Limit Theorem", "Sampling distributions converge to Normal")
        
        # Prepare plots with fixed ranges to prevent clipping
        # Left: Population Distribution
        # Right: Sampling Distribution of the Mean
        
        pop_plot = CurvePlot(lambda x: 0, x_range=(0, 10, 2), y_range=(0, 0.3, 0.1),
                             x_label="Value", y_label="Density",
                             x_length=5.0, y_length=3.0, color=C_DIM)
        pop_plot.move_to(LEFT * 3.5 + UP * 0.5)
        
        samp_plot = CurvePlot(lambda x: 0, x_range=(0, 10, 2), y_range=(0, 0.3, 0.1),
                              x_label="Mean", y_label="Density",
                              x_length=5.0, y_length=3.0, color=C_GOOD)
        samp_plot.move_to(RIGHT * 3.5 + UP * 0.5)
        
        self.play(Create(pop_plot.axes), FadeIn(pop_plot.labels))
        self.play(Create(samp_plot.axes), FadeIn(samp_plot.labels))
        
        n_samples = 2000
        n_per_sample = 30
        
        for i, sc in enumerate(scenarios):
            StepCaption(self, f"Scenario {i+1}: {sc['name']} Population")
            
            # 1. Show Population Shape
            pop_data, means = generate_clt_data(sc["func"], n_samples, n_per_sample)
            
            # Plot population histogram
            pop_counts, pop_bins = np.histogram(pop_data, bins=30, range=(0, 10))
            pop_density = pop_counts / (np.sum(pop_counts) * (pop_bins[1]-pop_bins[0]))
            
            pop_pts = [pop_plot.axes.c2p(a, b) for a, b in zip(pop_bins[:-1], pop_density)]
            pop_line = VMobject(color=sc["color"]).set_points_as_corners(pop_pts)
            self.play(Create(pop_line), run_time=1.5)
            
            # 2. Plot Sampling Distribution
            samp_counts, samp_bins = np.histogram(means, bins=40, range=(np.min(means)-0.1, np.max(means)+0.1))
            samp_density = samp_counts / (np.sum(samp_counts) * (samp_bins[1]-samp_bins[0]))
            
            # Ensure y-range is sufficient
            max_density = np.max(samp_density)
            if max_density > 0.3:
                samp_plot.axes.y_range = (0, max_density * 1.2, 0.1)
                # Rebuild axes to apply new range
                samp_plot.axes = Axes(x_range=(0, 10, 2), y_range=(0, max_density * 1.2, 0.1),
                                      x_length=5.0, y_length=3.0,
                                      axis_config={"color": C_DIM, "include_tip": False})
                samp_plot.axes.move_to(samp_plot.axes.get_center())
                self.play(FadeOut(samp_plot.labels), FadeOut(samp_plot.curve))
                self.play(Create(samp_plot.axes))
                samp_plot.labels = VGroup(
                    Text("Mean", font_size=24, color=WHITE).next_to(samp_plot.axes.x_axis.get_end(), DOWN, buff=0.15),
                    Text("Density", font_size=24, color=WHITE).next_to(samp_plot.axes.y_axis.get_end(), LEFT, buff=0.15)
                )
                self.play(FadeIn(samp_plot.labels))
            
            samp_pts = [samp_plot.axes.c2p(a, b) for a, b in zip(samp_bins[:-1], samp_density)]
            samp_line = VMobject(color=C_GOOD).set_points_as_corners(samp_pts)
            self.play(Create(samp_line), run_time=1.5)
            
            # 3. Normal Approximation
            mu = np.mean(means)
            sigma = np.std(means)
            x_vals = np.linspace(samp_bins[0], samp_bins[-1], 100)
            normal_pdf = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_vals - mu) / sigma)**2)
            
            # Ensure normal curve fits in y-range
            max_normal = np.max(normal_pdf)
            if max_normal > max_density * 1.2:
                samp_plot.axes.y_range = (0, max_normal * 1.2, 0.1)
                samp_plot.axes = Axes(x_range=(0, 10, 2), y_range=(0, max_normal * 1.2, 0.1),
                                      x_length=5.0, y_length=3.0,
                                      axis_config={"color": C_DIM, "include_tip": False})
                samp_plot.axes.move_to(samp_plot.axes.get_center())
                self.play(FadeOut(samp_plot.labels))
                self.play(Create(samp_plot.axes))
                samp_plot.labels = VGroup(
                    Text("Mean", font_size=24, color=WHITE).next_to(samp_plot.axes.x_axis.get_end(), DOWN, buff=0.15),
                    Text("Density", font_size=24, color=WHITE).next_to(samp_plot.axes.y_axis.get_end(), LEFT, buff=0.15)
                )
                self.play(FadeIn(samp_plot.labels))
            
            norm_pts = [samp_plot.axes.c2p(x, y) for x, y in zip(x_vals, normal_pdf)]
            norm_line = VMobject(color=C_ACCENT).set_points_as_corners(norm_pts)
            self.play(Create(norm_line), run_time=1.0)
            
            # 4. Highlight Convergence
            eq = MathTex(r"\bar{X} \sim \mathcal{N}(\mu, \sigma/\sqrt{n})", font_size=28)
            eq.to_edge(DOWN, buff=0.2)
            self.play(Write(eq), run_time=1.0)
            self.wait(0.5)
            self.play(FadeOut(eq))
            
            # Cleanup for next scenario
            self.play(FadeOut(pop_line), FadeOut(samp_line), FadeOut(norm_line))
            self.wait(0.2)

        StepCaption(self, "Regardless of shape, sample means become Normal.")
        self.wait(2)


if __name__ == "__main__" and "--selftest" in sys.argv:
    # Test that sample means from uniform converge to normal-like stats
    # Uniform(0,10) mean=5, var=100/12=8.33
    # Sample mean of 30: expected mean 5, expected std sqrt(8.33/30) ~ 0.527
    
    np.random.seed(42)
    means = []
    for _ in range(1000):
        s = np.random.uniform(0, 10, 30)
        means.append(np.mean(s))
    means = np.array(means)
    
    assert abs(np.mean(means) - 5.0) < 0.2, f"Mean should be near 5, got {np.mean(means)}"
    expected_std = np.sqrt(100/12 / 30)
    assert abs(np.std(means) - expected_std) < 0.1, f"Std should be near {expected_std}, got {np.std(means)}"
    
    # Test exponential
    np.random.seed(42)
    means2 = []
    for _ in range(1000):
        s = np.random.exponential(2, 30)
        means2.append(np.mean(s))
    means2 = np.array(means2)
    
    # Exp(2) mean=2, var=4
    # Sample mean std: sqrt(4/30) ~ 0.365
    assert abs(np.mean(means2) - 2.0) < 0.2, f"Mean should be near 2, got {np.mean(means2)}"
    expected_std2 = np.sqrt(4 / 30)
    assert abs(np.std(means2) - expected_std2) < 0.1, f"Std should be near {expected_std2}, got {np.std(means2)}"
    
    print("ok")

The Manim scene code that draws this lesson's animation in the Signals AI app (Python, Manim Community).

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.