Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Last active March 28, 2020 03:32
Show Gist options
  • Save hughdbrown/eb09b9558810583d2c4afd4932820e7c to your computer and use it in GitHub Desktop.
Save hughdbrown/eb09b9558810583d2c4afd4932820e7c to your computer and use it in GitHub Desktop.
# Based on this repository of coronavirus data:
# git clone [email protected]:nytimes/covid-19-data.git
from csv import DictReader
from collections import defaultdict
from pprint import pprint
# fields = ['date', 'state', 'cases', 'deaths']
# Selecting on 'cases' gives results that appear influenced by testing.
# Using 'deaths' appears less influenced by noise and human factors, in my opinion.
column_name = 'deaths'
# column_name = 'cases'
with open('us-states.csv') as handle:
dd = DictReader(handle)
d = defaultdict(dict)
for row in dd:
d[row['state']].update({row['date']: int(row[column_name])})
# Cumulative cases sorted by date for each state
cases = {
state: [v for k, v in sorted(cases.items(), key=lambda x: x[0])]
for state, cases in d.items()
}
# 5-day moving average of day-over-day rates by state
n = 5
rate_ma = {
state: [
(state_cases[i + n] / float(state_cases[i])) ** (1. / n)
for i in range(len(state_cases) - n)
if state_cases[i] >= 10
]
for state, state_cases in cases.items()
}
pprint(
[
(k, v and v[-1])
for k, v in sorted(
((k, v) for k, v in rate_ma.items() if v),
key=lambda x: x[1][-1]
)
]
)
@hughdbrown
Copy link
Author

Modified the code to use geometric averages, not arithmetic averages of rates (which would have overestimated the change).

@hughdbrown
Copy link
Author

hughdbrown commented Mar 28, 2020

5-day moving geometric average of change in rate of death (as of 2020-03-27):

[('Washington', 1.0971112499422204),
 ('Florida', 1.213950735423196),
 ('Georgia', 1.2286596790831472),
 ('California', 1.2397415710064692),
 ('New Jersey', 1.3831618672225916),
 ('Louisiana', 1.3899258176361557),
 ('New York', 1.4011310323534027)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment