Last active
March 28, 2020 03:32
-
-
Save hughdbrown/eb09b9558810583d2c4afd4932820e7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] | |
) | |
] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
5-day moving geometric average of change in rate of death (as of 2020-03-27):