In [1]:
# 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))
-0.00222000000000000
In [19]:
Sn
Out[19]:
-2
In [4]:
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
Out[4]:
(array([1., 2., 1., 1.]),
 array([1.  , 1.75, 2.5 , 3.25, 4.  ]),
 <a list of 4 Patch objects>)
In [59]:
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)
Out[59]:
(array([5028.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
           0.,    0.,    0., 4972.]),
 array([-1.        , -0.84615385, -0.69230769, -0.53846154, -0.38461538,
        -0.23076923, -0.07692308,  0.07692308,  0.23076923,  0.38461538,
         0.53846154,  0.69230769,  0.84615385,  1.        ]),
 <a list of 13 Patch objects>)
In [58]:
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)
Out[58]:
(array([ 345.,    0., 1535.,    0.,    0., 3244.,    0., 3037.,    0.,
           0., 1536.,    0.,  303.]),
 array([-1.        , -0.84615385, -0.69230769, -0.53846154, -0.38461538,
        -0.23076923, -0.07692308,  0.07692308,  0.23076923,  0.38461538,
         0.53846154,  0.69230769,  0.84615385,  1.        ]),
 <a list of 13 Patch objects>)
In [57]:
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)
Out[57]:
(array([1.000e+00, 4.000e+00, 3.800e+01, 1.880e+02, 5.560e+02, 7.390e+02,
        1.928e+03, 2.370e+03, 2.074e+03, 1.285e+03, 4.410e+02, 2.930e+02,
        7.200e+01, 1.000e+01, 1.000e+00]),
 array([-0.43434343, -0.37777778, -0.32121212, -0.26464646, -0.20808081,
        -0.15151515, -0.09494949, -0.03838384,  0.01818182,  0.07474747,
         0.13131313,  0.18787879,  0.24444444,  0.3010101 ,  0.35757576,
         0.41414141]),
 <a list of 15 Patch objects>)
In [17]:
# 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"
In [65]:
# command to generate random decimal numbers
# uniformly between 0 and 1
# average value 1/2 
random()
Out[65]:
0.11017502786578925
In [66]:
# uniform decimal number between -1 and 1
# average value 0
2*random()-1
Out[66]:
0.21701302155444768
In [61]:
# 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)
Out[61]:
(array([312., 309., 355., 315., 328., 316., 340., 332., 319., 302., 314.,
        351., 307., 317., 341., 332., 319., 364., 304., 358., 346., 302.,
        314., 305., 316., 339., 271., 287., 319., 320., 346.]),
 array([-0.99936601, -0.93490528, -0.87044455, -0.80598383, -0.7415231 ,
        -0.67706237, -0.61260165, -0.54814092, -0.48368019, -0.41921947,
        -0.35475874, -0.29029801, -0.22583729, -0.16137656, -0.09691583,
        -0.03245511,  0.03200562,  0.09646635,  0.16092707,  0.2253878 ,
         0.28984853,  0.35430926,  0.41876998,  0.48323071,  0.54769144,
         0.61215216,  0.67661289,  0.74107362,  0.80553434,  0.86999507,
         0.9344558 ,  0.99891652]),
 <a list of 31 Patch objects>)
In [63]:
# 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)
Out[63]:
(array([ 20.,  46.,  67., 103., 130., 190., 250., 284., 412., 474., 564.,
        555., 678., 729., 675., 713., 650., 639., 583., 512., 433., 334.,
        258., 247., 162., 108.,  91.,  52.,  28.,  11.,   2.]),
 array([-0.90266185, -0.84159252, -0.7805232 , -0.71945387, -0.65838455,
        -0.59731522, -0.5362459 , -0.47517657, -0.41410725, -0.35303792,
        -0.2919686 , -0.23089927, -0.16982995, -0.10876062, -0.0476913 ,
         0.01337803,  0.07444736,  0.13551668,  0.19658601,  0.25765533,
         0.31872466,  0.37979398,  0.44086331,  0.50193263,  0.56300196,
         0.62407128,  0.68514061,  0.74620993,  0.80727926,  0.86834859,
         0.92941791,  0.99048724]),
 <a list of 31 Patch objects>)
In [64]:
# 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)
Out[64]:
(array([2.000e+00, 2.000e+00, 5.000e+00, 1.300e+01, 2.400e+01, 5.800e+01,
        1.090e+02, 1.820e+02, 2.520e+02, 4.000e+02, 5.170e+02, 6.710e+02,
        7.640e+02, 8.910e+02, 1.019e+03, 9.710e+02, 9.330e+02, 8.080e+02,
        6.640e+02, 5.390e+02, 4.060e+02, 2.900e+02, 2.080e+02, 1.280e+02,
        6.900e+01, 4.700e+01, 1.600e+01, 6.000e+00, 1.000e+00, 2.000e+00,
        3.000e+00]),
 array([-0.39262327, -0.3665296 , -0.34043593, -0.31434226, -0.28824859,
        -0.26215492, -0.23606125, -0.20996758, -0.18387391, -0.15778024,
        -0.13168656, -0.10559289, -0.07949922, -0.05340555, -0.02731188,
        -0.00121821,  0.02487546,  0.05096913,  0.0770628 ,  0.10315647,
         0.12925014,  0.15534381,  0.18143748,  0.20753115,  0.23362483,
         0.2597185 ,  0.28581217,  0.31190584,  0.33799951,  0.36409318,
         0.39018685,  0.41628052]),
 <a list of 31 Patch objects>)
In [26]:
# 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)
Out[26]:
(array([  1.,   3.,   5.,  11.,  32.,  39.,  72., 142., 236., 346., 526.,
        632., 770., 903., 987., 964., 944., 840., 685., 576., 435., 311.,
        217., 152.,  76.,  53.,  22.,   7.,   4.,   6.,   3.]),
 array([0.22126645, 0.22855612, 0.23584579, 0.24313547, 0.25042514,
        0.25771481, 0.26500448, 0.27229416, 0.27958383, 0.2868735 ,
        0.29416318, 0.30145285, 0.30874252, 0.31603219, 0.32332187,
        0.33061154, 0.33790121, 0.34519088, 0.35248056, 0.35977023,
        0.3670599 , 0.37434957, 0.38163925, 0.38892892, 0.39621859,
        0.40350827, 0.41079794, 0.41808761, 0.42537728, 0.43266696,
        0.43995663, 0.4472463 ]),
 <a list of 31 Patch objects>)
In [ ]:
# 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"