# SEIR model example


\begin{align}
\dot{S} & = - \alpha\beta\frac{SI}{N}\\
\dot{E} & =  \alpha\beta\frac{SI}{N} - E\frac{1}{\tau_{EI}}\\
\dot{I} & =  E\frac{1}{\tau_{EI}} - I\frac{1}{\tau_{IR}}\\
\dot{R} & =  I\frac{1}{\tau_{IR}} \\
\end{align}

# SEIR model example
\begin{align}
\dot{S} & = S_f - \alpha\beta\frac{SI}{N} + \rho_{RS} R + \phi_S\\
\dot{E} & = E_f + \alpha\beta\frac{SI}{N} - E\frac{1}{\tau_{EI}} + \phi_E\\
\dot{I} & = I_f + E\frac{1}{\tau_{EI}} - I\frac{1}{\tau_{IR}} + \phi_I\\
\dot{R} & = R_f + I\frac{1}{\tau_{IR}} - \rho_{IR} R + \phi_R\\
\dot{\Phi} & = \sum_{i} (\phi_{i}) \\
N & = N_0 + \Phi \\
N_0 & = S_0+E_0+I_0+R_0  \\
\end{align}

Where:  
* $S:$ Susceptible Individuals
* $E:$ Exposed Individuals
* $I:$ Infectious Individuals
* $R:$ Removed Individuals
* $\alpha:$ Average number of contacts per person per time
* $\beta:$ Probability of disease transmission in a contact between a susceptible and an infectious subject
* $N:$ Total population
* $\tau_{EI}:$ Transition time between exposed and infectious
* $\tau_{IR}:$ Transition time between infectious and recovered
* $\rho_{RS}:$ Immunity loss rate. Inverse of Immunity loss rate medium time ($\frac{1}{t_{RS}}$)  
* $\phi_S,\phi_E,\phi_I,\phi_R:$ External flux per compartment
* $\Phi$: Total external population flux 


```python
# Util libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime

# cv19 libraries
from cv19gm.models.seir import SEIR
import cv19gm.utils.cv19functions as cv19functions
```


```python
# For pop-up plots execute this code (optional)
import platform
OS = platform.system()

if OS == 'Linux':    
    %matplotlib tk
    print('Linux')
elif OS == 'Windows':
    %matplotlib qt
    print('Windows')
elif OS == 'Darwin':
    %matplotlib tk
    print('Mac (Funciona?)')
```

    Linux


# Build a basic SEIR model using a configuration file

## Constant parameters


```python
# Input configuration file
config = 'cfg/SEIR.toml'
```


```python
# Build simulation object
model = SEIR(config = config,beta=0.3)
```


```python
%%capture
# Simulate (solve ODE)
model.solve()
```


```python
# Show results
model.results
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>t</th>
      <th>dates</th>
      <th>S</th>
      <th>E</th>
      <th>E_d</th>
      <th>I</th>
      <th>I_d</th>
      <th>R</th>
      <th>R_d</th>
      <th>Flux</th>
      <th>E_ac</th>
      <th>I_ac</th>
      <th>R_ac</th>
      <th>I_det</th>
      <th>I_d_det</th>
      <th>I_ac_det</th>
      <th>prevalence_total</th>
      <th>prevalence_susc</th>
      <th>prevalence_det</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>None</td>
      <td>986000</td>
      <td>3000</td>
      <td>1500</td>
      <td>6000</td>
      <td>3000</td>
      <td>5000</td>
      <td>0</td>
      <td>0.0</td>
      <td>1500</td>
      <td>18000</td>
      <td>0</td>
      <td>6000</td>
      <td>3000</td>
      <td>18000</td>
      <td>0.018000</td>
      <td>0.018000</td>
      <td>0.018000</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>None</td>
      <td>984216</td>
      <td>4073</td>
      <td>1681</td>
      <td>6106</td>
      <td>1563</td>
      <td>5603</td>
      <td>382</td>
      <td>0.0</td>
      <td>3181</td>
      <td>19563</td>
      <td>382</td>
      <td>6106</td>
      <td>1563</td>
      <td>19563</td>
      <td>0.019564</td>
      <td>0.019564</td>
      <td>0.019564</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2</td>
      <td>None</td>
      <td>982376</td>
      <td>5003</td>
      <td>1785</td>
      <td>6392</td>
      <td>1159</td>
      <td>6227</td>
      <td>536</td>
      <td>0.0</td>
      <td>4966</td>
      <td>20723</td>
      <td>918</td>
      <td>6392</td>
      <td>1159</td>
      <td>20723</td>
      <td>0.020724</td>
      <td>0.020724</td>
      <td>0.020724</td>
    </tr>
    <tr>
      <th>3</th>
      <td>3</td>
      <td>None</td>
      <td>980434</td>
      <td>5858</td>
      <td>1890</td>
      <td>6820</td>
      <td>1122</td>
      <td>6886</td>
      <td>616</td>
      <td>0.0</td>
      <td>6857</td>
      <td>21846</td>
      <td>1534</td>
      <td>6820</td>
      <td>1122</td>
      <td>21846</td>
      <td>0.021846</td>
      <td>0.021846</td>
      <td>0.021846</td>
    </tr>
    <tr>
      <th>4</th>
      <td>4</td>
      <td>None</td>
      <td>978352</td>
      <td>6685</td>
      <td>2019</td>
      <td>7366</td>
      <td>1214</td>
      <td>7595</td>
      <td>677</td>
      <td>0.0</td>
      <td>8876</td>
      <td>23060</td>
      <td>2212</td>
      <td>7366</td>
      <td>1214</td>
      <td>23060</td>
      <td>0.023061</td>
      <td>0.023061</td>
      <td>0.023061</td>
    </tr>
    <tr>
      <th>...</th>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <th>495</th>
      <td>495</td>
      <td>None</td>
      <td>59570</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>940429</td>
      <td>0</td>
      <td>0.0</td>
      <td>928717</td>
      <td>949156</td>
      <td>935385</td>
      <td>0</td>
      <td>0</td>
      <td>949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
    </tr>
    <tr>
      <th>496</th>
      <td>496</td>
      <td>None</td>
      <td>59570</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>940429</td>
      <td>0</td>
      <td>0.0</td>
      <td>928717</td>
      <td>949156</td>
      <td>935385</td>
      <td>0</td>
      <td>0</td>
      <td>949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
    </tr>
    <tr>
      <th>497</th>
      <td>497</td>
      <td>None</td>
      <td>59570</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>940429</td>
      <td>0</td>
      <td>0.0</td>
      <td>928717</td>
      <td>949156</td>
      <td>935385</td>
      <td>0</td>
      <td>0</td>
      <td>949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
    </tr>
    <tr>
      <th>498</th>
      <td>498</td>
      <td>None</td>
      <td>59570</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>940429</td>
      <td>0</td>
      <td>0.0</td>
      <td>928717</td>
      <td>949156</td>
      <td>935385</td>
      <td>0</td>
      <td>0</td>
      <td>949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
    </tr>
    <tr>
      <th>499</th>
      <td>499</td>
      <td>None</td>
      <td>59570</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>940429</td>
      <td>0</td>
      <td>0.0</td>
      <td>928717</td>
      <td>949156</td>
      <td>935385</td>
      <td>0</td>
      <td>0</td>
      <td>949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
      <td>0.949156</td>
    </tr>
  </tbody>
</table>
<p>500 rows × 19 columns</p>
</div>




```python
plt.plot(model.t,model.S+model.E+model.I+model.R)
```




    [<matplotlib.lines.Line2D at 0x7fdf4ccb6b50>]




```python
# Plot matplotlib
plt.plot(model.t,model.S,label='S')
plt.plot(model.t,model.E,label='E')
plt.plot(model.t,model.I,label='I')
plt.plot(model.t,model.R,label='R')
plt.xlim(0,300)
plt.legend(loc=0)
plt.show()
```


```python
# Plot with pandas
ax = model.results.plot(x='t',y=['S','E','I','R'])
ax.set_xlim(0,300)
ax.set_title('SEIR simulation plot')
```

## Variable parameters
Create a function with time dependent parameters. Check covid19functions notebook for more information on this.


```python
config2 = 'cfg/SEIR_v2.toml'
```


```python
model2 = SEIR(config = config2)
```

    input dict
    input dict



```python
%%capture
model2.solve()
```


```python
plt.plot(model2.t,model2.S,label='S')
plt.plot(model2.t,model2.E,label='E')
plt.plot(model2.t,model2.I,label='I')
plt.plot(model2.t,model2.R,label='R')
plt.xlim(0,300)
plt.legend(loc=0)
plt.show()
```

# SEIR desde cmd


```python
beta = 0.2
population = 10000
I = 100
```


```python
model = SEIR(config = config)
```


```python
model.solve()
```

### Variable parameters


```python
t = np.linspace(0,6,1000)
```


```python
beta = lambda t: np.sin(t)
```


```python
plt.plot(t,beta(t))
```




    [<matplotlib.lines.Line2D at 0x7f667035d730>]




```python
model = SEIR(config = config,beta=beta)
```


```python
model.solve()
```


```python
plt.plot(model.t,model.I)
plt.title('Active Infected')
plt.xlim(0,50)
plt.show()
```


```python

```


```python
beta2 = cv19functions.events(days=[[0,10],[10,60],[60,100]],values=[0.12,0.2,0.3],default = 0.1)
```


```python
t = np.linspace(0,200,10000)
```


```python
plt.plot(t,beta2(t))
```




    [<matplotlib.lines.Line2D at 0x7f667029c580>]




```python
model2 = SEIR(config = config,beta=beta2)
```


```python
model2.solve()
```


```python
plt.plot(model2.t,model2.I)
```




    [<matplotlib.lines.Line2D at 0x7f667029cd60>]



# Study


```python
# Input configuration file
config = 'cfg/SEIR.toml'
```


```python
# Build simulation object
model1 = SEIR(config = config, I_d_det = 600, population = 1000000)
model2 = SEIR(config = config, I_d_det = 600, population = 10000000)
```


```python
%%capture
# Simulate (solve ODE)
model1.solve()
model2.solve()
```


```python
model1.I_d[0]
```


```python
# Plot matplotlib
#plt.plot(model.t,model1.S,label='S')
#plt.plot(model.t,model1.E,label='E')
plt.plot(model.t,model1.I_d,label='I_1')
plt.plot(model.t,model2.I_d,label='I_2')
plt.xlim(0,300)
plt.yscale('log')
plt.legend(loc=0)
plt.show()
```


```python

```

b3f7c24786d2cd95a29ee2ad1e162314ca2b6a6d
ipynb
Jupyter Notebook
Examples/SEIR.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Examples/SEIR.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Examples/SEIR.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Qwen/Qwen-72B
1. YES
2. YES