Wilson's Blog

Rise, the great work must be done!

0%

Verify the Central Limit Theorem by Simulation

Assignment for ECEN 646 Homework 5 Problem 4. Coded in Python.

Problem:

Let $ \{ Xi \}{i>=1} $ be i.i.d. uniform (0, 1) random variables. Compute the standard deviation $\sigma$ of $X_i$ and define

Plot the empirical distribution functions (based on $10^4$ random variables) for $Z{10}$, $Z{100}$ and $Z_{1000}$. On the same figure plot the distribution function of the standard normal random variable.

Code:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import matplotlib.pyplot as plt
import random
from random import seed
from statistics import *
from scipy.stats import norm

def z_n_calc(n):
root_n = pow(n, 0.5)
seed()
num_array = [random.uniform(0,1) for _ in range(n)]
expectation = sum(num_array)/n
stdeviation = stdev(num_array)
res = 0

for i in range(0, n):
res = res + (num_array[i]-expectation)/(stdeviation*root_n)

return res

def sample_generator(n):
return [z_n_calc(n) for _ in range(10000)]

def norm_cdf_generator(array_in):
return [norm.cdf(x) for x in array_in]


y_array = [x/10000.0 for x in range(10000)]

# Z10
x_array_10 = sorted(sample_generator(10))
plt.figure()
# plt.axis([-2*(10**(-13)),2*(10**(-13)),0,1])
plt.plot(x_array_10, y_array, label='Z_10')
plt.plot(x_array_10, norm_cdf_generator(x_array_10))
plt.legend()

# Z100
x_array_100 = sorted(sample_generator(100))
plt.figure()
# plt.axis([-2*(10**(-13)),2*(10**(-13)),0,1])
plt.plot(x_array_100, y_array, label='Z_100')
plt.plot(x_array_100, norm_cdf_generator(x_array_100))
plt.legend()

# Z1000
x_array_1000 = sorted(sample_generator(1000))
plt.figure()
plt.plot(x_array_1000, y_array, label='Z_1000')
plt.plot(x_array_1000, norm_cdf_generator(x_array_1000))
plt.legend()

# All together
plt.figure()
plt.plot(x_array_10, y_array, label='Z_10')
plt.plot(x_array_100, y_array, label='Z_100')
plt.plot(x_array_1000, y_array, label='Z_1000')
plt.plot(x_array_1000, norm_cdf_generator(x_array_10), label='Standard normal')
plt.legend()

plt.ylabel('CDF')
plt.show()

Plot:

Z_10 plot

$Z_{10}$ plot

Z_100 plot

$Z_{100}$ plot

Z_1000 plot

$Z_{1000}$ plot

All together plot

All plot

GitHub repository

Welcome to my other publishing channels