
So, from the above histograms of the sample means, we can see if we take random samples of sufficiently large sample size, then the sampling distribution of the sample means will approach a normal distribution. And this holds true even if the population is not normally distributed.
How to verify the Central Limit Theorem using Python?
We can implement the rolling of a fair die by generating a random number between 1 to 6. Then, we can take 1000 random samples with the sample size sample_size by using the following Python code:
samples = [[random.randint(1, 6) for _ in range(sample_size)] for _ in range(number_of_samples)]
Now, we can calculate the sample means of the random samples with the sample size sample_size using the following Python code:
means = list()
for i in range(0, number_of_samples):
means.append(mean(samples[i]))
Now, we can plot the histogram of the sample means using the following Python code:
pyplot.hist(means)
pyplot.savefig(filename)
pyplot.close()
We can now write the whole code within a function so that we can call the function with different sample size.
def sample_distribution(sample_size, filename, number_of_samples=1000):
samples = [[random.randint(1, 6) for _ in range(sample_size)] for _ in range(number_of_samples)]
means = list()
for i in range(0, number_of_samples):
means.append(mean(samples[i]))
pyplot.hist(means)
pyplot.savefig(filename)
pyplot.close()
return(mean(means))
Now, we can call the function multiple times with different sample size and get the histogram of the sample means saved in a given file name. …








































0 Comments