# Goal is to simulated averages of independent random samples
# taken from a fixed distribution
randint(0,1) # {0,1} uniform random variables
2*randint(0,1)-1 # {-1,1} uniform
random() # [0,1] uniform
[randint(0,1) for i in range(10)] # produces 5 samples from randint(0,1)
# use histograms for plotting
import matplotlib.pyplot as plt
# plt is now shorthand for matplotlib.pyplot
plt.hist([0,1,1,1,5,2])
plt.hist([randint(0,1) for i in range(1000)])
# look at sums
sum([0,1,2,3])
def ave_rand_01discrete(n):
"""Generate n independent samples from randint(0,1).
Returns average of these"""
list_rand = [randint(0,1) for i in range(n)]
return N(sum(list_rand)/n) # decimal approximation
ave_rand_01discrete(400)
num_trials = 1000
n=2 # num r.v. being added together
plt.hist([ave_rand_01discrete(n) for i in range(num_trials)])
num_trials = 1000
n=10 # num r.v. being added together
plt.hist([ave_rand_01discrete(n) for i in range(num_trials)], bins=11)
# now increase n
num_trials = 1000
n=100 # num r.v. being added together
plt.hist([ave_rand_01discrete(n) for i in range(num_trials)], bins=11)
num_trials = 1000
n=1000 # num r.v. being added together
plt.hist([ave_rand_01discrete(n) for i in range(num_trials)], bins=11)
# NOTE: scale on horizontal axis
# Histograms are getting concentrated around
# average of a single {0,1} discrete r.v.