forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
centralLimitDemo.py
46 lines (36 loc) · 1.23 KB
/
centralLimitDemo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Central Limit theorem
# Author: Animesh Gupta
import numpy as np
import matplotlib.pyplot as plt
#number of samples
samples = 100000
bins = 20
N = [1, 5]
def convolutionHist(N,sampleSize,bins):
# Generating 1, 5 random numbers from 1 to 5
# taking their mean and appending it to list means.
means = []
for j in N:
# Generating seed so that we can get same result
# every time the loop is run...
np.random.seed(1)
x = np.mean(
np.random.beta(
1, 5,[sampleSize,j]),axis=1)
means.append(x)
return means
def plot_convolutionHist(means,N,sampleSize,bins):
for i, mean_ in zip(N, means):
counts, nbinslocation = np.histogram(mean_, bins=20)
counts = counts / (sampleSize/bins)
plt.figure(figsize=(4,4))
plt.title(f'N = {i}')
plt.bar(nbinslocation[:-1], counts, width=0.02, color='tab:blue', align='edge')
plt.xticks(np.linspace(0,1,3))
plt.yticks(np.linspace(0,3,4))
plt.xlim(0, 1)
plt.ylim(0, 3)
plt.savefig(f"../figures/clt_N_{i}.pdf", dpi=300)
plt.show()
means = convolutionHist(N,samples,bins)
plot_convolutionHist(means,N,samples,bins)