# Covid 19 Doubling Period Outside of China
Author: Scott Morgan

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 model
We 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 Representation
Now 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 Solution
Let 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 Reading
For more on the matematics of the Coronavirus, see Mathematics of the Corona outbreak – with professor Tom Britton (https://www.youtube.com/watch?v=gSqIwXl6IjQ).
## Code
The code for this project follows, along with a plot of the model and exising data. 


```python
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")
```


```python

```

ab4e81da6c5dfcb5e5cf90680428458c82faf801
ipynb
Jupyter Notebook
Covid-19 Doubling Period Outside of China.ipynb
stmorgan/covid19
cee98c2ae67c743c9ff02c80ee782e7058dc55ee
MIT
Covid-19 Doubling Period Outside of China.ipynb
stmorgan/covid19
cee98c2ae67c743c9ff02c80ee782e7058dc55ee
MIT
Covid-19 Doubling Period Outside of China.ipynb
stmorgan/covid19
cee98c2ae67c743c9ff02c80ee782e7058dc55ee
MIT
Qwen/Qwen-72B
1. YES
2. YES