Skip to content

Instantly share code, notes, and snippets.

@alkhe
Last active May 19, 2020 05:17
Show Gist options
  • Select an option

  • Save alkhe/e929ce163d895e572ea2218b44283250 to your computer and use it in GitHub Desktop.

Select an option

Save alkhe/e929ce163d895e572ea2218b44283250 to your computer and use it in GitHub Desktop.
Demonstration of Benford's Law in National Coronavirus Cases and Deaths
// scraped from https://www.worldometers.info/coronavirus/ on May 19, 2020
const deaths = [91981, 2722, 27709, 16853, 34796, 32007, 28239, 8123, 4171, 7057, 3164, 2789, 4634, 5842, 320, 9080, 5332, 478, 5694, 939, 15, 2799, 1886, 171, 3698, 1231, 22, 1547, 224, 349, 936, 535, 1191, 1120, 276, 286, 749, 592, 629, 118, 645, 434, 831, 263, 548, 231, 279, 297, 382, 233, 555, 12, 173, 99, 192, 113, 35, 300, 191, 217, 29, 25, 61, 174, 107, 127, 462, 140, 40, 56, 165, 146, 16, 13, 105, 26, 133, 110, 95, 28, 38, 79, 104, 10, 64, 41, 59, 61, 7, 21, 28, 104, 57, 11, 30, 18, 14, 4, 4, 46, 4, 19, 9, 31, 26, 17, 50, 55, 52, 10, 51, 11, 51, 7, 20, 7, 13, 12, 41, 9, 10, 6, 43, 21, 9, 53, 33, 21, NaN, 7, 15, 2, 2, 5, 2, 24, 10, 12, 3, NaN, 9, NaN, 1, NaN, 4, NaN, 7, 22, 1, 2, 14, 6, NaN, 13, NaN, NaN, 1, NaN, 20, 9, 10, NaN, 8, 3, 4, 11, 1, 7, 1, 4, 15, 3, 3, NaN, 3, 3, 4, NaN, 1, 3, NaN, 8, 3, 1, 1, NaN, NaN, NaN, NaN, 2, NaN, NaN, NaN, NaN, 1, NaN, NaN, NaN, NaN, 1, NaN, 1, 1, 1, NaN, NaN, 2, 1, NaN, NaN, NaN, NaN, NaN, NaN, NaN].filter(x => !isNaN(x))
const cases = [1550294, 290678, 278188, 255368, 246406, 225886, 179927, 177289, 150593, 122492, 101261, 94933, 82960, 78072, 57345, 55559, 51633, 46059, 44141, 43966, 33969, 33582, 30597, 30572, 30377, 29209, 28343, 24200, 24190, 23870, 18885, 18616, 18010, 17036, 16643, 16433, 16305, 16295, 16269, 15691, 12764, 12725, 12718, 11078, 10968, 10699, 9726, 8586, 8371, 8257, 7201, 7184, 7072, 7068, 6952, 6941, 6751, 6380, 6175, 6138, 5735, 5379, 4823, 4263, 3947, 3554, 3535, 3529, 3387, 3031, 2836, 2798, 2796, 2791, 2591, 2544, 2304, 2235, 2228, 2119, 2001, 1881, 1817, 1802, 1784, 1729, 1547, 1538, 1518, 1503, 1495, 1466, 1455, 1432, 1413, 1370, 1243, 1106, 1056, 1043, 1032, 1009, 992, 948, 931, 917, 912, 909, 874, 866, 796, 788, 761, 761, 737, 719, 712, 701, 654, 629, 618, 558, 555, 533, 520, 519, 519, 509, 446, 440, 412, 388, 375, 352, 339, 335, 332, 330, 328, 327, 324, 324, 322, 297, 290, 248, 246, 229, 210, 205, 192, 191, 187, 155, 147, 145, 141, 140, 130, 125, 124, 122, 116, 101, 97, 96, 94, 88, 82, 81, 77, 70, 65, 60, 58, 50, 46, 45, 42, 39, 39, 25, 25, 25, 24, 24, 22, 21, 19, 18, 18, 18, 18, 17, 16, 16, 16, 15, 13, 12, 12, 11, 11, 11, 11, 11, 9, 8, 8, 6, 6, 6, 3, 1, 1].filter(x => !isNaN(x))
function sum(xs) {
return xs.reduce((a, b) => a + b, 0)
}
function get_first_digit(n) {
while (n >= 10) {
n = n / 10 | 0
}
return n
}
function first_digit_stats(xs) {
const digits = Array(9).fill(0)
xs.forEach(x => {
digits[get_first_digit(x) - 1]++
})
return digits
}
function digits_histogram(digits) {
const total = sum(digits)
const weights_out_of_20 = digits.map(d => Math.round(d / total * 20))
let graph = ''
for (let i = 10; i > 0; i--) {
const asterisks = weights_out_of_20.map(w => w >= i ? '*' : ' ')
graph += `${ String(5 * i).padStart(2, ' ') }% ${ asterisks.join(' ')} \n`
}
graph += ` ${ digits.map((_, i) => i + 1).join(' ') }`
return graph
}
const death_stats = first_digit_stats(deaths)
console.log(' First Digit Representation in')
console.log(' National Coronavirus Deaths')
console.log(digits_histogram(death_stats))
console.log()
const case_stats = first_digit_stats(cases)
console.log(' First Digit Representation in')
console.log(' National Coronavirus Cases')
console.log(digits_histogram(case_stats))
First Digit Representation in
National Coronavirus Deaths
50%
45%
40%
35% *
30% *
25% *
20% * *
15% * *
10% * * * * *
5% * * * * * * * *
1 2 3 4 5 6 7 8 9
First Digit Representation in
National Coronavirus Cases
50%
45%
40%
35% *
30% *
25% *
20% *
15% * *
10% * * *
5% * * * * * * * * *
1 2 3 4 5 6 7 8 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment