Last active
October 23, 2019 18:40
-
-
Save chaipi-chaya/85090bc06540d1d1f0b78e4d387dd36b to your computer and use it in GitHub Desktop.
Illustration with Python Central Limit Theorem | sample size increases the mean of sample is close to population mean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import random | |
import matplotlib.pyplot as plt | |
# Step 1 | |
## show that as the sample size increases the mean of sample is close to population mean | |
# build gamma distribution as population | |
shape, scale = 2., 2. # mean=4, std=2*sqrt(2) | |
s = np.random.gamma(shape, scale, 1000000) | |
# Step 2 | |
# set expected values of population | |
mu = shape*scale # mean | |
# sample size | |
samplesize = [] | |
# collect difference between sample mean and mu | |
diflist = [] | |
# for each sample size | |
for n in range(10,20000,20): | |
# sample n sample | |
rs = random.choices(s, k=n) | |
# start count | |
c = 0 | |
# calculate mean | |
mean = sum(rs)/len(rs) | |
# collect difference between sample mean and mu | |
diflist.append(mean-mu) | |
samplesize.append(n) | |
# Step 3 | |
# set figure size. | |
plt.figure(figsize=(20,10)) | |
# plot each diference. | |
plt.scatter(samplesize,diflist, marker='o') | |
# show plot. | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment