## Exercise:

This example is sourced from the [Scipy website](https://scipython.com/book/chapter-8-scipy/additional-examples/the-sir-epidemic-model/) where you can find more details. For more on these models and their variations, see [Compartmental models in epidimiology, Wikipedia](https://en.wikipedia.org/wiki/Compartmental_models_in_epidemiology).

Numerically solve the SIR epidemic model:

1. In pure Python using for loops to integrate in time.
1. Using Scipy's odeint package. 

The differential equations that describe the model are:

\begin{equation}
\frac{dS}{dt} = \frac{-\beta I S}{N}
\end{equation}

\begin{equation}
\frac{dI}{dt} = \frac{\beta I S}{N} - \gamma I
\end{equation}

\begin{equation}
\frac{dR}{dt} = \gamma I
\end{equation}

Where S are the susceptible numbers in the population, I is the number of infected, R is the number of recovered persons in the population. $\beta$ is the *effective contact rate*, that is, an infected individual comes into contact with $\beta N$ individuals. $\gamma$ is the mean recovery rate: $1/\gamma$ is the mean period of time that an individual can pass on the infection.

A vitally important number to keep track of is the ratio: $R_0 = \beta/\gamma$; when $R_0 \gt 1$, the disease spreads through the population, when $R_0 \lt 1$, the disease quickly dies out.


```python
import numpy as np
import matplotlib.pyplot as plt
```


```python
N = 1000
Rec0 = 0
Inf0 = 1
Sus0 = N - Inf0
```


```python
beta = 0.2
gamma = 0.1
R0 = beta/gamma
```


```python
R0
```




    2.0




```python
Sus = [Sus0]
Rec = [Rec0]
Inf = [Inf0]
for t in range(150):
    delta_S = -beta * Inf[-1] * Sus[-1] / N
    Sus.append(Sus[-1]+delta_S)
    
    delta_I = beta * Inf[-1] * Sus[-1] / N - gamma * Inf[-1]
    Inf.append(Inf[-1] + delta_I)
    
    delta_R = gamma * Inf[-1]
    Rec.append(Rec[-1] + delta_R)
    #print(delta_S, delta_I, delta_R)
```


```python
Sus = np.array(Sus)
Rec = np.array(Rec)
Inf = np.array(Inf)
```


```python
plt.plot(np.arange(151), Sus, label="Susceptible")
plt.plot(np.arange(151), Rec, label="Recovered")
plt.plot(np.arange(151), Inf, label="Infected")
plt.legend()
```

### Exercise 07: SIR model in scipy

Use scipy.integrate.odeint to solve the SIR model. For more information, refer to the [documentation page](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html).


```python
# %load ./solutions/sol_scipy_SIR.py
```

35c5fd73b1bf49625a466a96ad1d9a4fe549b7c3
ipynb
Jupyter Notebook
04_Numpy_SIR.ipynb
adityarn/MAV110_PythonModule
c3ee6457ba0e4d2cae04f3f6a138d0b473bb4f8e
MIT
2021-11-25T13:08:27.000Z
2021-11-25T13:08:30.000Z
04_Numpy_SIR.ipynb
adityarn/MAV110_PythonModule
c3ee6457ba0e4d2cae04f3f6a138d0b473bb4f8e
MIT
04_Numpy_SIR.ipynb
adityarn/MAV110_PythonModule
c3ee6457ba0e4d2cae04f3f6a138d0b473bb4f8e
MIT
Qwen/Qwen-72B
1. YES
2. YES