Skip to content

Instantly share code, notes, and snippets.

@chaipi-chaya
Last active October 23, 2019 18:31
Show Gist options
  • Save chaipi-chaya/5b7826755f6d464ebc8083e876411fad to your computer and use it in GitHub Desktop.
Save chaipi-chaya/5b7826755f6d464ebc8083e876411fad to your computer and use it in GitHub Desktop.
Illustration with Python Central Limit Theorem | sample with different sample size
import numpy as np
import random
import matplotlib.pyplot as plt
# build gamma distribution as population
shape, scale = 2., 2. # mean=4, std=2*sqrt(2)
s = np.random.gamma(shape, scale, 1000000)
# Step 1
## sample with different sample size
# list of sample mean
meansample = []
# number of sampling
numofsample = 25000
# sample size
samplesize = [1,5,10,30,100,1000]
# for each sample size (1 to 1000)
for i in samplesize:
# collect mean of each sample
eachmeansample = []
# for each sampling
for j in range(0,numofsample):
# sampling i sample from population
rc = random.choices(s, k=i)
# collect mean of each sample
eachmeansample.append(sum(rc)/len(rc))
# add mean of each sampling to the list
meansample.append(eachmeansample)
# Step 2
# plot
cols = 2
rows = 3
fig, ax = plt.subplots(rows, cols, figsize=(20,15))
n = 0
for i in range(0, rows):
for j in range(0, cols):
ax[i, j].hist(meansample[n], 200, density=True)
ax[i, j].set_title(label="sample size :" + str(samplesize[n]))
n += 1
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment