# generate random numbers, and take average
X = [] # will contain list of values of X_i, each will be +1,-1 with prob 1/2
n = 100000
for i in range(n):
rand = 2*randint(0,1)-1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n # average of all X_i
print(N(ave))
Sn
import matplotlib.pyplot # python package that has histogram function
matplotlib.pyplot.hist([1,2,2,3,4], bins=4) # run twice to get this
# to work properly
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 1 # number of copies of X_i to sum
results = [] # will store the results for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i, each +/-1 with prob 1/2
for i in range(n):
rand = 2*randint(0,1)-1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=13)
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 5 # number of copies of X_i to sum
results = [] # will store the results for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i, each +/-1 with prob 1/2
for i in range(n):
rand = 2*randint(0,1)-1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=13)
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 99 # number of copies of X_i to sum
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i, each +/-1 with prob 1/2
for i in range(n):
rand = 2*randint(0,1)-1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=15)
# Lesson 1: as n-> infinity, ave = (1/n)(X_1+...+X_n) is very likely to
# be close to 0
# "Law of Large numbers"
# Lesson 2: Histogram looks like a bell curve for n large
# (also need num_iters large)
# "Central limit theorem"
# bell curve corresponds to "normal distribution"
# command to generate random decimal numbers
# uniformly between 0 and 1
# average value 1/2
random()
# uniform decimal number between -1 and 1
# average value 0
2*random()-1
# Change what rand is at each stage
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 1
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = 2*random()-1 # uniform random numbers between -1 and 1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=31)
# Change what rand is at each stage
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 3
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = 2*random()-1 # uniform random number between -1 and 1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=31)
# Change what rand is at each stage
# Use uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 30
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = 2*random()-1 # uniform random number between -1 and 1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=31)
# Change what rand is at each stage
# Use non-uniform random numbers between -1 and 1
import matplotlib.pyplot as plt # python package that has histogram
num_iters = 10000
n = 100
results = [] # will store the result for all the iterations
for j in range(num_iters):
# generate random numbers, and take average
X = [] # list of X_i
for i in range(n):
rand = (random())^2 # this gives non-uniform random variable
# more likely to be close to 0 than 1
X.append(rand)
Sn = sum(X) # sum of all X_i
ave = Sn/n
results.append(N(ave))
# print(results)
plt.hist(results,bins=31)
# Lesson 3: Both Law of Large Numbers and Central Limit Theorem
# hold for any way of choosing each X_i
# (Need: X_i independent from each other,
# also averages must exist, and a condition on
# the variance that is typically satisfied)
# So the Normal Distribution is "universal"