Law of Large Numbers

The law of large numbers states that the the average of results from a large number of experiment trials:

  • should be close to the expected value 

  • will move closer to the expected value as more trials are performed

In the example below, the dots show individual trial data points and the line shows the average.

There are 200 trials of generating random integer numbers between 1 and 21. The expected value average is 10:

Python Example

The code below generates the chart shown above.

To download the code, click here.

"""
law-of-large-numbers.py
demonstrates the law of large numbers over a range of experiment trials
"""
# Import needed libraries.
from random import randint
import matplotlib.pyplot as plotlib

# Define parameters.
number_of_trials = 200
number_of_trial_values = 21
starting_trial_value = 1
total = 0
results = []
plot_x = []
plot_y = []
plot_z = []

# Calculate, save, and print values over a range of trials.
print("results: number, divisor, value, total, average")
for number in range(0, number_of_trials):
    value = randint(starting_trial_value, number_of_trial_values)
    total += value
    divisor = number + 1
    average = float(total)/float(divisor)
    result = [number, divisor, value, total, average]
    plot_x.append(number)
    plot_y.append(average)
    plot_z.append(value)
    results.append(result)
    print(result)

# Plot the data.
plotlib.plot(plot_x, plot_y, color='black')
plotlib.plot(plot_x, plot_z, '.', color='blue')

# Set axis labels.
plotlib.xlabel("Trial")
plotlib.ylabel("Values")

# Display the plot.
plotlib.show()
The print results are shown below:

results: number, divisor, value, total, average
[0, 1, 15, 15, 15.0]
[1, 2, 11, 26, 13.0]
[2, 3, 20, 46, 15.333333333333334]
[3, 4, 14, 60, 15.0]
[4, 5, 8, 68, 13.6]
[5, 6, 9, 77, 12.833333333333334]
[6, 7, 20, 97, 13.857142857142858]
[7, 8, 9, 106, 13.25]
[8, 9, 6, 112, 12.444444444444445]
[9, 10, 14, 126, 12.6]
[10, 11, 18, 144, 13.090909090909092]
[11, 12, 4, 148, 12.333333333333334]
[12, 13, 6, 154, 11.846153846153847]
[13, 14, 2, 156, 11.142857142857142]
[14, 15, 3, 159, 10.6]
[15, 16, 12, 171, 10.6875]
[16, 17, 6, 177, 10.411764705882353]
[17, 18, 9, 186, 10.333333333333334]
[18, 19, 13, 199, 10.473684210526315]
[19, 20, 2, 201, 10.05]
[20, 21, 16, 217, 10.333333333333334]
[21, 22, 6, 223, 10.136363636363637]
[22, 23, 2, 225, 9.782608695652174]
[23, 24, 6, 231, 9.625]
[24, 25, 7, 238, 9.52]
[25, 26, 2, 240, 9.23076923076923]
[26, 27, 19, 259, 9.592592592592593]
[27, 28, 12, 271, 9.678571428571429]
[28, 29, 17, 288, 9.931034482758621]
[29, 30, 16, 304, 10.133333333333333]
[30, 31, 11, 315, 10.161290322580646]
[31, 32, 20, 335, 10.46875]
[32, 33, 19, 354, 10.727272727272727]
[33, 34, 11, 365, 10.735294117647058]
[34, 35, 3, 368, 10.514285714285714]
[35, 36, 16, 384, 10.666666666666666]
[36, 37, 14, 398, 10.756756756756756]
[37, 38, 4, 402, 10.578947368421053]
[38, 39, 19, 421, 10.794871794871796]
[39, 40, 1, 422, 10.55]
[40, 41, 2, 424, 10.341463414634147]
[41, 42, 2, 426, 10.142857142857142]
[42, 43, 9, 435, 10.116279069767442]
[43, 44, 17, 452, 10.272727272727273]
[44, 45, 14, 466, 10.355555555555556]
[45, 46, 15, 481, 10.456521739130435]
[46, 47, 20, 501, 10.659574468085106]
[47, 48, 14, 515, 10.729166666666666]
[48, 49, 10, 525, 10.714285714285714]
[49, 50, 19, 544, 10.88]
[50, 51, 6, 550, 10.784313725490197]
[51, 52, 3, 553, 10.634615384615385]
[52, 53, 5, 558, 10.528301886792454]
[53, 54, 8, 566, 10.481481481481481]
[54, 55, 14, 580, 10.545454545454545]
[55, 56, 1, 581, 10.375]
[56, 57, 11, 592, 10.385964912280702]
[57, 58, 15, 607, 10.46551724137931]
[58, 59, 7, 614, 10.40677966101695]
[59, 60, 9, 623, 10.383333333333333]
[60, 61, 17, 640, 10.491803278688524]
[61, 62, 4, 644, 10.387096774193548]
[62, 63, 3, 647, 10.26984126984127]
[63, 64, 3, 650, 10.15625]
[64, 65, 1, 651, 10.015384615384615]
[65, 66, 5, 656, 9.93939393939394]
[66, 67, 1, 657, 9.805970149253731]
[67, 68, 7, 664, 9.764705882352942]
[68, 69, 6, 670, 9.710144927536232]
[69, 70, 19, 689, 9.842857142857143]
[70, 71, 17, 706, 9.943661971830986]
[71, 72, 14, 720, 10.0]
[72, 73, 10, 730, 10.0]
[73, 74, 1, 731, 9.878378378378379]
[74, 75, 11, 742, 9.893333333333333]
[75, 76, 15, 757, 9.960526315789474]
[76, 77, 7, 764, 9.922077922077921]
[77, 78, 19, 783, 10.038461538461538]
[78, 79, 3, 786, 9.949367088607595]
[79, 80, 11, 797, 9.9625]
[80, 81, 3, 800, 9.876543209876543]
[81, 82, 15, 815, 9.939024390243903]
[82, 83, 11, 826, 9.951807228915662]
[83, 84, 7, 833, 9.916666666666666]
[84, 85, 18, 851, 10.011764705882353]
[85, 86, 12, 863, 10.034883720930232]
[86, 87, 7, 870, 10.0]
[87, 88, 8, 878, 9.977272727272727]
[88, 89, 13, 891, 10.01123595505618]
[89, 90, 15, 906, 10.066666666666666]
[90, 91, 5, 911, 10.010989010989011]
[91, 92, 20, 931, 10.119565217391305]
[92, 93, 6, 937, 10.075268817204302]
[93, 94, 11, 948, 10.085106382978724]
[94, 95, 20, 968, 10.189473684210526]
[95, 96, 3, 971, 10.114583333333334]
[96, 97, 6, 977, 10.072164948453608]
[97, 98, 16, 993, 10.13265306122449]
[98, 99, 6, 999, 10.090909090909092]
[99, 100, 2, 1001, 10.01]
[100, 101, 20, 1021, 10.108910891089108]
[101, 102, 2, 1023, 10.029411764705882]
[102, 103, 20, 1043, 10.12621359223301]
[103, 104, 14, 1057, 10.163461538461538]
[104, 105, 16, 1073, 10.219047619047618]
[105, 106, 7, 1080, 10.18867924528302]
[106, 107, 3, 1083, 10.121495327102803]
[107, 108, 10, 1093, 10.12037037037037]
[108, 109, 9, 1102, 10.110091743119266]
[109, 110, 1, 1103, 10.027272727272727]
[110, 111, 3, 1106, 9.963963963963964]
[111, 112, 19, 1125, 10.044642857142858]
[112, 113, 11, 1136, 10.053097345132743]
[113, 114, 7, 1143, 10.026315789473685]
[114, 115, 5, 1148, 9.982608695652173]
[115, 116, 1, 1149, 9.905172413793103]
[116, 117, 18, 1167, 9.974358974358974]
[117, 118, 9, 1176, 9.966101694915254]
[118, 119, 8, 1184, 9.949579831932773]
[119, 120, 8, 1192, 9.933333333333334]
[120, 121, 4, 1196, 9.884297520661157]
[121, 122, 9, 1205, 9.87704918032787]
[122, 123, 15, 1220, 9.91869918699187]
[123, 124, 3, 1223, 9.862903225806452]
[124, 125, 1, 1224, 9.792]
[125, 126, 20, 1244, 9.873015873015873]
[126, 127, 21, 1265, 9.960629921259843]
[127, 128, 18, 1283, 10.0234375]
[128, 129, 6, 1289, 9.992248062015504]
[129, 130, 14, 1303, 10.023076923076923]
[130, 131, 11, 1314, 10.030534351145038]
[131, 132, 13, 1327, 10.053030303030303]
[132, 133, 10, 1337, 10.052631578947368]
[133, 134, 16, 1353, 10.097014925373134]
[134, 135, 16, 1369, 10.14074074074074]
[135, 136, 10, 1379, 10.139705882352942]
[136, 137, 15, 1394, 10.175182481751825]
[137, 138, 10, 1404, 10.173913043478262]
[138, 139, 15, 1419, 10.20863309352518]
[139, 140, 21, 1440, 10.285714285714286]
[140, 141, 13, 1453, 10.304964539007091]
[141, 142, 14, 1467, 10.330985915492958]
[142, 143, 6, 1473, 10.3006993006993]
[143, 144, 18, 1491, 10.354166666666666]
[144, 145, 14, 1505, 10.379310344827585]
[145, 146, 13, 1518, 10.397260273972602]
[146, 147, 10, 1528, 10.394557823129253]
[147, 148, 9, 1537, 10.385135135135135]
[148, 149, 14, 1551, 10.409395973154362]
[149, 150, 6, 1557, 10.38]
[150, 151, 20, 1577, 10.443708609271523]
[151, 152, 19, 1596, 10.5]
[152, 153, 11, 1607, 10.50326797385621]
[153, 154, 16, 1623, 10.53896103896104]
[154, 155, 14, 1637, 10.561290322580644]
[155, 156, 2, 1639, 10.506410256410257]
[156, 157, 14, 1653, 10.528662420382165]
[157, 158, 9, 1662, 10.518987341772151]
[158, 159, 9, 1671, 10.50943396226415]
[159, 160, 1, 1672, 10.45]
[160, 161, 10, 1682, 10.4472049689441]
[161, 162, 7, 1689, 10.425925925925926]
[162, 163, 11, 1700, 10.429447852760736]
[163, 164, 10, 1710, 10.426829268292684]
[164, 165, 15, 1725, 10.454545454545455]
[165, 166, 1, 1726, 10.397590361445783]
[166, 167, 2, 1728, 10.347305389221557]
[167, 168, 16, 1744, 10.380952380952381]
[168, 169, 17, 1761, 10.420118343195266]
[169, 170, 1, 1762, 10.364705882352942]
[170, 171, 6, 1768, 10.339181286549708]
[171, 172, 21, 1789, 10.401162790697674]
[172, 173, 1, 1790, 10.346820809248555]
[173, 174, 9, 1799, 10.339080459770114]
[174, 175, 15, 1814, 10.365714285714287]
[175, 176, 15, 1829, 10.392045454545455]
[176, 177, 15, 1844, 10.418079096045197]
[177, 178, 7, 1851, 10.398876404494382]
[178, 179, 10, 1861, 10.396648044692737]
[179, 180, 10, 1871, 10.394444444444444]
[180, 181, 3, 1874, 10.353591160220995]
[181, 182, 3, 1877, 10.313186813186814]
[182, 183, 16, 1893, 10.344262295081966]
[183, 184, 4, 1897, 10.309782608695652]
[184, 185, 5, 1902, 10.281081081081082]
[185, 186, 18, 1920, 10.32258064516129]
[186, 187, 5, 1925, 10.294117647058824]
[187, 188, 21, 1946, 10.351063829787234]
[188, 189, 12, 1958, 10.359788359788359]
[189, 190, 5, 1963, 10.33157894736842]
[190, 191, 10, 1973, 10.329842931937172]
[191, 192, 3, 1976, 10.291666666666666]
[192, 193, 20, 1996, 10.341968911917098]
[193, 194, 1, 1997, 10.293814432989691]
[194, 195, 20, 2017, 10.343589743589744]
[195, 196, 5, 2022, 10.316326530612244]
[196, 197, 13, 2035, 10.32994923857868]
[197, 198, 4, 2039, 10.297979797979798]
[198, 199, 5, 2044, 10.271356783919598]
[199, 200, 11, 2055, 10.275]

References