Created
March 12, 2020 15:21
-
-
Save tommycarstensen/4fead8e1dfb47a4e053e957f88d61a43 to your computer and use it in GitHub Desktop.
Script to fit COVID19 cases to logistic sigmoid functions.
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
from datetime import datetime | |
from scipy.optimize import curve_fit | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# Data acquired from: | |
# https://covid19info.live/ | |
d = { | |
'Europe': ''' | |
2020-01-23 0 | |
2020-01-24 2 | |
2020-01-25 3 | |
2020-01-26 3 | |
2020-01-27 4 | |
2020-01-28 8 | |
2020-01-29 10 | |
2020-01-30 10 | |
2020-01-31 20 | |
2020-02-01 25 | |
2020-02-02 27 | |
2020-02-03 29 | |
2020-02-04 30 | |
2020-02-05 30 | |
2020-02-06 30 | |
2020-02-07 34 | |
2020-02-08 39 | |
2020-02-09 41 | |
2020-02-10 51 | |
2020-02-11 53 | |
2020-02-12 55 | |
2020-02-13 55 | |
2020-02-14 55 | |
2020-02-15 56 | |
2020-02-16 56 | |
2020-02-17 56 | |
2020-02-18 56 | |
2020-02-19 56 | |
2020-02-20 56 | |
2020-02-21 73 | |
2020-02-22 115 | |
2020-02-23 208 | |
2020-02-24 290 | |
2020-02-25 394 | |
2020-02-26 554 | |
2020-02-27 820 | |
2020-02-28 1114 | |
2020-02-29 1489 | |
2020-03-01 2242 | |
2020-03-02 2773 | |
2020-03-03 3415 | |
2020-03-04 4437 | |
2020-03-05 5872 | |
2020-03-06 7700 | |
2020-03-07 9881 | |
2020-03-08 12505 | |
2020-03-09 15335 | |
2020-03-10 18812 | |
2020-03-11 23993 | |
''', | |
'Korea': ''' | |
2020-01-23 1 | |
2020-01-24 2 | |
2020-01-25 2 | |
2020-01-26 3 | |
2020-01-27 4 | |
2020-01-28 4 | |
2020-01-29 4 | |
2020-01-30 4 | |
2020-01-31 11 | |
2020-02-01 12 | |
2020-02-02 15 | |
2020-02-03 15 | |
2020-02-04 16 | |
2020-02-05 19 | |
2020-02-06 23 | |
2020-02-07 24 | |
2020-02-08 24 | |
2020-02-09 25 | |
2020-02-10 27 | |
2020-02-11 28 | |
2020-02-12 28 | |
2020-02-13 28 | |
2020-02-14 28 | |
2020-02-15 28 | |
2020-02-16 29 | |
2020-02-17 30 | |
2020-02-18 31 | |
2020-02-19 31 | |
2020-02-20 104 | |
2020-02-21 204 | |
2020-02-22 433 | |
2020-02-23 602 | |
2020-02-24 833 | |
2020-02-25 977 | |
2020-02-26 1261 | |
2020-02-27 1766 | |
2020-02-28 2337 | |
2020-02-29 3150 | |
2020-03-01 3736 | |
2020-03-02 4335 | |
2020-03-03 5186 | |
2020-03-04 5621 | |
2020-03-05 6088 | |
2020-03-06 6593 | |
2020-03-07 7041 | |
2020-03-08 7314 | |
2020-03-09 7478 | |
2020-03-10 7513 | |
2020-03-11 7755 | |
''', | |
} | |
region = 'Europe' | |
s = d[region] | |
x, y = zip(*(l.rstrip().split(' ') for l in s.split('\n')[1:-1])) | |
dates = x | |
# x = [datetime.strptime(s, '%Y-%m-%d').date() for s in x] | |
x = list(range(len(x))) | |
y = [int(_) for _ in y] | |
for i in range(len(x)): | |
print(x[i], y[i]) | |
print(x) | |
print(y) | |
def sigmoid(x, a, b): | |
y = 1 / (1 + np.exp(-b*(x-a))) | |
return y | |
def logistic(x, a, b, c): | |
y = a / (1 + np.exp(-b * (x - c))) | |
return y | |
# Seed values for regression. | |
p0=[200000, 0.5, 70] | |
popt, pcov = curve_fit(logistic, x, y, p0=p0) | |
print(popt) | |
plt.scatter(x, y, c='r', label='Cases') | |
plt.xlabel('Days') | |
plt.ylabel('Present and past confirmed cases') | |
x2 = list(range(150)) | |
plt.plot(x2, logistic(x2, *popt), 'b', label='Fitted sigmoid logistic function') | |
title = '{} {}'.format(region, dates[-1]) | |
title += '\nMaximum={:d}, Midpoint={:d}, Steepness={:.2f}'.format( | |
int(popt[0]), int(popt[2]), popt[1]) | |
title += '\nCurrent day={}'.format(len(x)) | |
title += ', Current present and past cases={}'.format(y[-1]) | |
plt.title(title, fontsize='small') | |
plt.legend() | |
# plt.yscale('log') | |
plt.savefig('COVID19_sigmoid_{}_{}.png'.format(region, dates[-1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment