benford.html
001| '''
001| Benford. Concerning the distribution of leading digits in lists of random numbers.
001|
001| 1: ########################################## 126
001| 2: ###########################* 82
001| 3: #################* 52
001| 4: ################### 57
001| 5: ####################* 61
001| 6: #########* 28
001| 7: ####### 21
001| 8: ######### 27
001| 9: ####### 21
001|
001| Bin[1-3] = 260 54.7%
001| Bin[4-6] = 146 30.7%
001| Bin[7-9] = 69 14.5%
001|
001| Bin[1] = 126 26.5%
001| Bin[2-9] = 349 73.5%
001|
001| Bin[1-2] = 208 43.8%
001| Bin[3-9] = 267 56.2%
001|
001| Bin[1-3] = 260 54.7%
001| Bin[4-9] = 215 45.2%
001|
001| Bin[1-4] = 317 66.7%
001| Bin[5-9] = 158 33.3%
001|
001| Bin[1-5] = 378 79.6%
001| Bin[6-9] = 97 20.4%
001|
001| Bin[1-6] = 406 85.5%
001| Bin[7-9] = 69 14.5%
001|
001| '''
002|
003| from sys import argv
004|
005|
006|
007|
008|
009| url = 'http://www.census.gov/population/www/documentation/twps0027/tab13.txt'
010|
011|
012| data0 = [
013| 3437202, 1698575, 1293697, 575238, 560892, 508957, 381768, 352387, 342782, 325902,
014| 321616, 287104, 285704, 285315, 278718, 246070, 206433, 204731, 202718, 175597,
015| 169164, 163752, 163065, 162608, 133859, 131822, 129896, 125560, 118421, 108374,
016| 108027, 105171, 104863, 102979, 102555, 102479, 102320, 102026, 94969, 94151,
017| 91886, 90426, 89872, 87565, 85333, 85050, 80865, 80671, 79850, 78961,
018| 76508, 75935, 73307, 70996, 68513, 66960, 62559, 62442, 62139, 62059,
019| 61643, 60651, 59364, 59007, 56987, 56383, 56100, 55807, 54244, 53531,
020| 53321, 52969, 52733, 52130, 51721, 51418, 50167, 50145, 47931, 46624,
021| 45859, 45712, 45115, 44885, 44633, 42938, 42728, 42638, 42345, 41459,
022| 40169, 40063, 39647, 39441, 39231, 38973, 38878, 38469, 38415, 38307
023| ]
024|
025|
026| data1 = [
027| 33200000, 17800000, 17700000, 17500000, 17400000, 16425000, 14750000, 14350000, 14300000, 14250000,
028| 13400000, 12700000, 12200000, 11789000, 11200000, 10800000, 10500000, 10000000, 9800000, 9645000,
029| 9000000, 9000000, 8614000, 8308000, 8278000, 8000000, 7350000, 7250000, 7000000, 7000000,
030| 6500000, 6000000, 5950000, 5700000, 5500000, 5425000, 5400000, 5300000, 5300000, 5149000,
031| 5100000, 5000000, 4919000, 4900000, 4900000, 4750000, 4400000, 4367000, 4250000, 4200000,
032| 4146000, 4032000, 4000000, 4000000, 4000000, 4000000, 3934000, 3903000, 3900000, 3823000,
033| 3685000, 3675000, 3502000, 3500000, 3500000, 3229000, 3216000, 3200000, 3162000, 3100000,
034| 3025000, 2907000, 2900000, 2800000, 2750000, 2750000, 2712000, 2700000, 2674000, 2650000,
035| 2500000, 2500000, 2400000, 2389000, 2300000, 2284000, 2260000, 2250000, 2245000, 2217000,
036| 2200000, 2200000, 2150000, 2100000, 2078000, 2076000, 2075000, 2062000, 2000000, 2000000,
037| 1985000, 1960000, 1925000, 1900000, 1850000, 1830000, 1800000, 1800000, 1787000, 1753000,
038| 1750000, 1750000, 1625000, 1600000, 1600000, 1583000, 1570000, 1550000, 1538000, 1525000,
039| 1525000, 1508000, 1507000, 1503000, 1500000
040| ]
041|
042|
043| data2 = [
044| 6993, 8683, 1968, 1049, 2072, 2564, 1399, 484, 1295, 1360,
045| 738, 531, 1295, 4320, 2266, 1580, 2150, 746, 518, 2723,
046| 1166, 2875, 748, 5498, 1623, 466, 2642, 686, 518, 596,
047| 1010, 2396, 414, 376, 596, 648, 534, 583, 622, 4661,
048| 622, 469, 2891, 518, 945, 453, 1606, 1655, 1554, 453,
049| 3644, 4497, 868, 583, 1101, 479, 2996, 3267, 803, 3355,
050| 684, 984, 1687, 5083, 596, 1365, 1740, 479, 2080, 583,
051| 376, 2069, 829, 583, 389, 777, 2470, 686, 2026, 583,
052| 648, 842, 583, 2316, 453, 600, 984, 881, 558, 2309,
053| 544, 531, 544, 544, 2147, 1768, 414, 2078, 510, 466,
054| 1292, 816, 829, 712, 673, 1120, 648, 702, 1676, 2208,
055| 492, 712, 583, 544, 518, 1228, 712, 453, 674, 673,
056| 816, 1603, 1136, 1740, 453
057| ]
058|
059|
060| data3 = [
061| 4750, 2050, 9000, 16700, 8400, 6400, 10550, 29650, 11050, 10500,
062| 18150, 23900, 9400, 2750, 4950, 6850, 4900, 13400, 18900, 3550,
063| 7700, 3150, 11500, 1500, 5100, 17150, 2800, 10550, 13500, 11750,
064| 6450, 2500, 14350, 15200, 9250, 8400, 10100, 9100, 8550, 1100,
065| 8200, 10650, 1700, 9450, 5200, 10500, 2750, 2650, 2750, 9250,
066| 1150, 900, 4600, 6850, 3650, 8350, 1300, 1200, 4850, 1150,
067| 5400, 3750, 2100, 700, 5900, 2350, 1850, 6700, 1500, 5300,
068| 8050, 1400, 3500, 4800, 7100, 3550, 1100, 3950, 1300, 4550,
069| 3850, 2950, 4100, 1050, 5050, 3800, 2300, 2550, 4000, 950,
070| 4050, 4150, 3950, 3850, 950, 1150, 5000, 1000, 3900, 4300,
071| 1550, 2400, 2300, 2650, 2750, 1650, 2800, 2550, 1050, 800,
072| 3550, 2450, 2800, 2950, 3100, 1300, 2200, 3400, 2300, 2250,
073| 1850, 950, 1350, 850, 3300
074| ]
075|
076|
077|
078| def Benford (numbers):
079| a = [0] * 9
080|
081| for n in numbers:
082| s = str(n)
083| c = int(s[0])
084| a[c-1] += 1
085|
086| for ix,n in enumerate(a):
087| print('%d: %s %d' % (ix+1, '*'*n, n))
088| print()
089|
090| print('Bin[1-3] = %d' % sum(a[0:3]))
091| print('Bin[4-6] = %d' % sum(a[3:6]))
092| print('Bin[7-9] = %d' % sum(a[6:9]))
093| print()
094|
095| print('Bin[1] = %d' % sum(a[0:1]))
096| print('Bin[2-9] = %d' % sum(a[1:9]))
097| print()
098|
099| print('Bin[1-2] = %d' % sum(a[0:2]))
100| print('Bin[3-9] = %d' % sum(a[2:9]))
101| print()
102|
103| print('Bin[1-3] = %d' % sum(a[0:3]))
104| print('Bin[4-9] = %d' % sum(a[3:9]))
105| print()
106|
107| print('Bin[1-4] = %d' % sum(a[0:4]))
108| print('Bin[5-9] = %d' % sum(a[4:9]))
109| print()
110|
111| print('Bin[1-5] = %d' % sum(a[0:5]))
112| print('Bin[6-9] = %d' % sum(a[5:9]))
113| print()
114|
115| print('Bin[1-6] = %d' % sum(a[0:6]))
116| print('Bin[7-9] = %d' % sum(a[6:9]))
117| print()
118|
119|
120|
121| def do_main (*args):
122| print('main/parameters: %s' % str(args))
123| Benford(data0+data1+data2+data3)
124| return 'Done'
125|
126|
127| def dispatch (cmd, *args):
128| print('command: %s' % cmd)
129| print('arguments: %d' % len(args))
130| if cmd == 'main': return do_main(*args)
131| return 'Nothing to do!'
132|
133| if __name__ == '__main__':
134| print('autorun: %s' % argv[0])
135| cmd = argv[1] if 1 < len(argv) else ''
136| etc = argv[2:] if 2 < len(argv) else []
137| obj = dispatch(cmd, *etc)
138| print(obj)
139|
140| '''eof'''
141|