# "Epidemic dynamics: The SIR model"
> "The Susceptible-Infected-Recovered model with python"

- toc: true 
- badges: true
- comments: true
- categories: [jupyter, Covid19]

## Modeling 

The Susceptible-Infected-Recovered (SIR) model is useful for understanding the spread of infections and epidemics and pretty easy to explain and understand (Wikipedia explanation [here](https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology#The_SIR_model) isn't bad. 

Guided by this didactic ['Coronavirus Curve - Numberphile](https://www.youtube.com/watch?v=k6nLfCbAzgo) video I wrote simple python code (using the python Scipy ordinary differential equatio solver) to run and visualize some simulations. I've also set it up to run it as an interactive [web app](https://ricardian.herokuapp.com/). 

## The Model
The proportion of the population are infected $I_t$, susceptible $S$, and recovered $R$ evolve over time according to these equation swhich depend on the transmission rate $\beta$ and the recovery rate $\gamma$:

$$
\begin{align}
\frac{dI}{dt} &= \beta \cdot S \cdot I - \gamma \cdot I  \\
\frac{dS}{dt} &=-\beta \cdot S \cdot I  \\
\frac{dR}{dt} &= \gamma \cdot I
\end{align}
$$



```python
#hide
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from ipywidgets import interact
```

We'll need some initial conditions and sample parameters:


```python
#collapse-show
N = 1          # Size of the population (so everything in proportions)
I0 = 0.01      # Initial proportion of the population infected
S0 = N - I0    # Initial proportion of the population susceptible
R0 = 0.0       # Initial proportion of the population recovered
maxT = 25      # max number of periods in simulation

beta = 0.5      # transmission rate
gamma = 0.1     # recovery rate
```

A `SIR()` function that for any time $t$ and current value of stock variables $y=(S,I,R)$ updates $\frac{dS}{dt}, \frac{dI}{dt}$ and $\frac{dR}{dt}$


```python
#collapse-hide
def SIR(y, t, beta, gamma):
    '''the SIR model'''
    S, I, R = y
    dSdt = -beta*S*I
    dIdt = beta*S*I - gamma*I
    dRdt = gamma*I
    return([dSdt, dIdt, dRdt])
```

and some code to solve and plot this system of ordinary differential equations for any given value of $\beta$ and $\gamma$


```python
#collapse-hide
def plotSIR(beta = beta, gamma = gamma, maxT = maxT):
    '''Solve differential equations in SIR and plot'''
    t = np.linspace(0, maxT, 1000)
    soln = odeint(SIR,[S0,I0,R0], t, args=(beta, gamma))
    soln = np.array(soln)
    plt.figure(figsize=[8,6])

    plt.plot(t, soln[:,0], linewidth=3, label = 'S(t)')
    plt.plot(t, soln[:,1], linewidth=3, label = 'I(t)')
    plt.plot(t, soln[:,2], linewidth=3, label = 'R(t)')
    plt.grid()
    plt.legend()
    plt.xlabel("Time")
    plt.ylabel("proportions")
    plt.title("SIR model")
    plt.show()
```

Below is a plot with the default parameters ($\beta=0.5$, $\gamma=0.1$) to have a graphic in case widget above does not display.


```python
plotSIR(beta, gamma, maxT)
```

Now if you have a python kernel running in the background (e.g. you opened this with binder or google colab) you can change the model parameters with sliders and see how that changes the predicted course of the infection over time. You can also do this with the interactive [web app](https://ricardian.herokuapp.com/) version of this notebook.


```python
#collapse-hide
interact(plotSIR, beta=(0,1,0.05), gamma=(0,1,0.05), maxT=(5,100,5));
```


    interactive(children=(FloatSlider(value=0.5, description='beta', max=1.0, step=0.05), FloatSlider(value=0.1, d…


4914d2f0c8d31c9428e509255410ef070a1b0566
ipynb
Jupyter Notebook
_notebooks/2020-04-10-SIRmodel.ipynb
jhconning/econblog
1a22d81fb869705e9a76d1c3c86e05e27642ae75
Apache-2.0
_notebooks/2020-04-10-SIRmodel.ipynb
jhconning/econblog
1a22d81fb869705e9a76d1c3c86e05e27642ae75
Apache-2.0
2021-03-30T08:53:57.000Z
2021-09-28T01:37:03.000Z
_notebooks/2020-04-10-SIRmodel.ipynb
jhconning/econblog
1a22d81fb869705e9a76d1c3c86e05e27642ae75
Apache-2.0
2020-04-11T07:41:23.000Z
2020-04-11T07:41:23.000Z
Qwen/Qwen-72B
1. YES
2. YES