Demonstrate the Sampling Distribution of the Mean. Given a population distribution, simulate taking many samples of size n and show that the distribution of sample means has a smaller variance and approaches normality (by CLT).
import numpy as np
def sampling_distribution_of_mean(population: np.ndarray, sample_size: int,
n_samples: int = 10000) -> dict:
sample_means = np.array([
np.mean(np.random.choice(population, size=sample_size, replace=True))
for _ in range(n_samples)
])
pop_mean = np.mean(population)
pop_var = np.var(population)
return {
"population_mean": float(pop_mean),
"population_variance": float(pop_var),
"mean_of_sample_means": float(np.mean(sample_means)),
"variance_of_sample_means": float(np.var(sample_means)),
"theoretical_variance": float(pop_var / sample_size),
"sample_means": sample_means,
}n_samples random samples of size sample_size from the population (with replacement).population_variance / sample_size.