<jupyter_start><jupyter_text>Covid 19 Doubling Period Outside of ChinaAuthor: <NAME> The following describes a Python program for calculating the Covid-19 doubling period using data from outside of China. Data for this project comes from the Johns Hopkins University Coronavirus Map (https://coronavirus.jhu.edu/map.html). In the early stages of a pandemic, the number of infected increases exponentially and it is the exponential growth that we'll model. We'll create and exponential model containing two unknown parameters and use the exising number of infected over time to calculate the model parameters that minimize the square error between the model and the data.  Math Model$$\hat{y}=A2^{\frac{t}{\tau}}$$$\tau$ is the doubling period and $\hat{y}$ is an estimate of the number of infected at time $t$, the number of days after January 19, 2020. $A$ is a constant representing the initial number of infected.  Linearizing$$\ln{\hat{y}}=\ln{A}+\frac{t}{\tau}\ln{2}$$ System of equations from modelWe can create a system of linear equations from the model output at each day. $$\begin{matrix}\ln{\hat{y}_{1}}=\ln{A}+\frac{t_1}{\tau}\ln{2} \\ \vdots \\ \ln{\hat{y}_{n}}=\ln{A}+\frac{t_n}{\tau}\ln{2} \end{matrix}$$ Matrix RepresentationNow we can represent the system of linear equations in matrix form. $$\begin{pmatrix}\hat{y}_1 \\ \vdots \\ \hat{y}_n \end{pmatrix}=\begin{bmatrix}t_1\ln{2} & 1 \\ \vdots & \vdots \\ t_n\ln{2} & 1\end{bmatrix}\begin{bmatrix}\frac{1}{\tau} \\ \ln{A} \\ \end{bmatrix}$$Define additional matrices as\begin{equation}    \begin{aligned}   \hat{Y} &= \begin{pmatrix}\hat{y}_1 \\ \vdots \\ \hat{y}_n \end{pmatrix} \\[5pt]    H&=\begin{bmatrix}t_1\ln{2} & 1 \\ \vdots & \vdots \\ t_n\ln{2} & 1\end{bmatrix} \\[5pt]  \Theta&=\begin{bmatrix}\frac{1}{\tau} \\ \ln{A} \end{bmatrix}=\begin{bmatrix}\theta_1 \\ \theta_2 \end{bmatrix} \\[5pt]    \end{aligned}\end{equation} Then $$\hat{Y}=H\Theta$$ Least Squares SolutionLet the vector of actual measurements of the infected for each day be $$Y = \begin{pmatrix}y_1 \\ \vdots \\ y_n \end{pmatrix}$$It is known that the parameters $\theta_1$ and $\theta_2$ associated with the least squares fit of the data to the model are given by $$\Theta=[H^TH]^{-1}H^TY$$So, $$\theta_1 = \frac{1}{\tau}$$and $$\theta_2 = \ln{A}$$Which gives the doubling period $\tau$ by$$\tau=\frac{1}{\theta_1}$$and A, the number of initial infected, as $$A=e^{\theta_2}$$ Further ReadingFor more on the matematics of the Coronavirus, see Mathematics of the Corona outbreak – with professor <NAME> (https://www.youtube.com/watch?v=gSqIwXl6IjQ). CodeThe code for this project follows, along with a plot of the model and exising data.<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import math

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})

%matplotlib inline

# Source of data from outside of China: https://coronavirus.jhu.edu/map.html 
Y = np.array([4, 6, 8 ,14, 25, 40, 57, 64, 87, 105, 118, 
              153, 173, 183, 188, 212, 227, 265, 317, 343, 
              361,457, 476, 523, 538, 595, 685, 780, 896, 
              1000, 1100, 1200, 1400, 1700, 2000, 2400, 
              2600, 3300, 4300, 5300, 6500, 8500, 10300, 
              12700, 14900, 17500, 21200, 25200, 29100, 
              32800, 37800, 44900, 47400, 63600, 75100, 
              81700, 100500, 116100, 133800, 161600, 190900, 232200])

T = np.arange(1,Y.size + 1)
Tln2 = T*(np.log(2))
Ones = np.ones(Y.size)
H = np.concatenate([Tln2, Ones])
Ht = np.reshape(H, (2, Y.size))
H = np.transpose(Ht)
lnY = np.log(Y)
# m, c = np.linalg.lstsq(H, lnY, rcond=None)[0] # Alternate calculation 
Hinv = np.linalg.pinv(H)
Theta = np.matmul(Hinv, lnY)
m = Theta[0]
c = Theta[1]
A = np.exp(c)
print("\nA")
print(f"{A:{4}.{3}}")
tau = 1/m
print("\ntau")
print(f"{tau:{4}.{3}}")
Ym = A*np.power(2, T/tau)
plt.plot(T, Y, label='actual')
plt.plot(T, Ym, label='model')
plt.title("Covid-19 Doubling Period Outside of China")
plt.xlabel("Time t (days after January 1\9, 2020)")
plt.ylabel("Number Infected I")
plt.annotate(f'Doubling period = {tau:{4}.{3}} days', xy=(0.25, 0.5), xycoords='figure fraction')
plt.annotate(f'Model: I = {A:{4}.{3}} x 2^(t/{tau:{4}.{3}})', xy=(0.25, 0.4), xycoords='figure fraction')
plt.legend()
plt.savefig('covid19.png')

time_to_million = (tau/math.log(2))*(math.log(1000000/A))

print(f"\nTime to 1,000,000 infected: {time_to_million:{4}.{3}} days after January 19")<jupyter_output>A
11.9

tau
4.41

Time to 1,000,000 infected: 72.2 days after January 19
{'fraction_non_alphanumeric': 0.1690885072655218, 'fraction_numerical': 0.07881990312637605, 'mean_word_length': 4.1217587373167985, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '628418', 'license': 'MIT', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1585, 'n_words': 500, 'path': 'Covid-19 Doubling Period Outside of China.ipynb', 'repo_name': 'stmorgan/covid19', 'size': '4553'}