# Goal: generate random variables from a fixed distribution
# and take average
randint(0,1) # {0,1} uniform random variable
random() # [0,1] uniform
2*randint(0,1)-1 # {-1,1} uniform
2*random() #[0,2] uniform
# sum of list
sum([0,1,2,3])
def ave_rand_01discrete(n):
"""Generate n independent instances of randint(0,1).
Takes average of these."""
list_rand = [randint(0,1) for i in range(n)]
return sum(list_rand)/n
ave_rand_01discrete(2)
# plot runs of the above as histogram
import matplotlib.pyplot as plt
# plt is now shorthand for matplotlib.pyplot
plt.hist([0,1,2,2,3])
plt.hist([randint(0,1) for i in range(1000)])
# gives a good picture of one random variable
# now take a bunch of instances of ave_rand_01discrete
plt.hist([ave_rand_01discrete(2) for i in range(10000)])
# now take a bunch of instances of ave_rand_01discrete
plt.hist([ave_rand_01discrete(10) for i in range(10000)])
plt.hist([ave_rand_01discrete(100) for i in range(10000)])
# NOTE: scale on horizontal axis
# Histograms are getting concentrated around
# average of a single {0,1} discrete r.v.
plt.hist([ave_rand_01discrete(1000) for i in range(10000)])
# Law of Large Numbers