In [ ]:
# Goal is to simulated averages of independent random samples 
# taken from a fixed distribution
In [52]:
randint(0,1) # {0,1} uniform random variables
Out[52]:
1
In [12]:
2*randint(0,1)-1 # {-1,1} uniform
Out[12]:
1
In [18]:
random() # [0,1] uniform
Out[18]:
0.5213604294730817
In [24]:
[randint(0,1) for i in range(10)] # produces 5 samples from randint(0,1)
Out[24]:
[0, 1, 0, 0, 1, 0, 0, 0, 0, 0]
In [25]:
# use histograms for plotting
import matplotlib.pyplot as plt
# plt is now shorthand for matplotlib.pyplot
In [26]:
plt.hist([0,1,1,1,5,2])
Out[26]:
(array([1., 0., 3., 0., 1., 0., 0., 0., 0., 1.]),
 array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ]),
 <a list of 10 Patch objects>)
In [53]:
plt.hist([randint(0,1) for i in range(1000)])
Out[53]:
(array([475.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0., 525.]),
 array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
 <a list of 10 Patch objects>)
In [32]:
# look at sums
sum([0,1,2,3])
Out[32]:
6
In [40]:
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
In [49]:
ave_rand_01discrete(400)
Out[49]:
0.492500000000000
In [54]:
num_trials = 1000
n=2 # num r.v. being added together
plt.hist([ave_rand_01discrete(n) for i in range(num_trials)])
Out[54]:
(array([237.,   0.,   0.,   0.,   0., 488.,   0.,   0.,   0., 275.]),
 array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ]),
 <a list of 10 Patch objects>)
In [60]:
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)
Out[60]:
(array([  1.,  16.,  34., 113., 205., 272., 195., 110.,  39.,  13.,   2.]),
 array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,
        0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,
        0.90909091, 1.        ]),
 <a list of 11 Patch objects>)
In [61]:
# 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)
Out[61]:
(array([  5.,  25.,  77., 144., 221., 245., 160.,  96.,  24.,   2.,   1.]),
 array([0.35, 0.38, 0.41, 0.44, 0.47, 0.5 , 0.53, 0.56, 0.59, 0.62, 0.65,
        0.68]),
 <a list of 11 Patch objects>)
In [62]:
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.
Out[62]:
(array([  3.,   7.,  32.,  99., 173., 234., 239., 131.,  56.,  21.,   5.]),
 array([0.443     , 0.45281818, 0.46263636, 0.47245455, 0.48227273,
        0.49209091, 0.50190909, 0.51172727, 0.52154545, 0.53136364,
        0.54118182, 0.551     ]),
 <a list of 11 Patch objects>)
In [ ]: