```python
from epipack import SymbolicEpiModel, EpiModel, StochasticEpiModel
import sympy as sy 
import numpy as np
%matplotlib widget
```


```python
%matplotlib widget
```


```python
S, I, R, eta, rho, omega = sy.symbols("S I R eta rho omega")
```


```python
SIRS = SymbolicEpiModel([S,I,R])

SIRS.set_processes([
    #### transmission process ####
    # S + I (eta)-> I + I
    (S, I, eta, I, I),
    
    #### transition processes ####
    # I (rho)-> R
    # R (omega)-> S
    (I, rho, R),
    (R, omega, S),

])
```




    <epipack.symbolic_epi_models.SymbolicEpiModel at 0x15c684880>




```python
SIRS.ODEs()
```




    [Eq(Derivative(S, t), -I*S*eta + R*omega),
     Eq(Derivative(I, t), I*(S*eta - rho)),
     Eq(Derivative(R, t), I*rho - R*omega)]




```python
SIRS.ODEs_jupyter()
```


$\displaystyle \frac{d}{d t} S = - I S \eta + R \omega$



$\displaystyle \frac{d}{d t} I = I \left(S \eta - \rho\right)$



$\displaystyle \frac{d}{d t} R = I \rho - R \omega$



```python
SIRS.find_fixed_points()
```




$\displaystyle \left\{\left( S, \  0, \  0\right), \left( \frac{\rho}{\eta}, \  \frac{R \omega}{\rho}, \  R\right)\right\}$




```python
SIRS.jacobian()
```




$\displaystyle \left[\begin{matrix}- I \eta & - S \eta & \omega\\I \eta & S \eta - \rho & 0\\0 & \rho & - \omega\end{matrix}\right]$




```python
SIRS.get_eigenvalues_at_disease_free_state()
```




    {eta - rho: 1, -omega: 1, 0: 1}




```python
S, I, R = list("SIR")
R0 = 2.5
rho = recovery_rate = 1 # let's say 1/days
eta = infection_rate = R0 * recovery_rate
omega = 1/14 # in units of 1/days

SIRS = EpiModel([S,I,R])

SIRS.set_processes([
    #### transmission process ####
    # S + I (eta)-> I + I
    (S, I, eta, I, I),
    
    #### transition processes ####
    # I (rho)-> R
    # R (omega)-> S
    (I, rho, R),
    (R, omega, S),

])

SIRS.set_initial_conditions({S:1-0.01, I:0.01})
```




    <epipack.numeric_epi_models.EpiModel at 0x106990f40>




```python
t = np.linspace(0,40,1000) 
result = SIRS.integrate(t)
```


```python
import matplotlib.pyplot as pl
pl.figure()
for compartment, timeseries in result.items():
    pl.plot(t, timeseries, label=compartment)
pl.xlabel(r"time $t\times\rho$")
pl.ylabel("frequency")
pl.ylim([0,1])
pl.legend()
pl.gcf().tight_layout()
```


    Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …



```python
import networkx as nx

k0 = 50
eta = R0 * rho / k0
N = int(1e4)
edges = [ (e[0], e[1], 1.0) for e in nx.fast_gnp_random_graph(N,k0/(N-1)).edges() ]

SIRS = StochasticEpiModel([S,I,R],N,edge_weight_tuples=edges)

SIRS.set_link_transmission_processes([
    #### transmission process ####
    # I + S (eta)-> I + I
    (I, S, eta, I, I),
])
    
SIRS.set_node_transition_processes([
    #### transition processes ####
    # I (rho)-> R
    # R (omega)-> S
    (I, rho, R),
    (R, omega, S),

])

SIRS.set_random_initial_conditions({S:N-int(1e-2*N), I:int(1e-2*N)})
```




    <epipack.stochastic_epi_models.StochasticEpiModel at 0x15d253be0>




```python
t_s, result_s = SIRS.simulate(40)
```


```python
pl.figure()

for compartment, timeseries in result.items():
    #pl.plot(t, timeseries, label=compartment)
    pl.plot(t_s, result_s[compartment]/N, label=compartment+" (sim.)")
pl.xlabel(r"time $t\times\rho$")
pl.ylabel("frequency")
pl.ylim([0,1])
pl.legend()
pl.gcf().tight_layout()
pl.show()
```


    Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …



```python

```

54582865c851ccb9aea493edd372ae6c58fab058
ipynb
Jupyter Notebook
cookbook/notebooks/epipack_SIRS_example.ipynb
benmaier/epipack
3a2ffb3e673e3499a553ed47c3621cd88877e6d2
MIT
2020-10-22T10:59:10.000Z
2022-01-05T02:24:21.000Z
cookbook/notebooks/epipack_SIRS_example.ipynb
benmaier/epipack
3a2ffb3e673e3499a553ed47c3621cd88877e6d2
MIT
2021-03-06T10:18:14.000Z
2021-09-06T21:34:46.000Z
cookbook/notebooks/epipack_SIRS_example.ipynb
benmaier/epipack
3a2ffb3e673e3499a553ed47c3621cd88877e6d2
MIT
2021-06-17T14:26:03.000Z
2021-08-13T02:11:00.000Z
Qwen/Qwen-72B
1. YES
2. YES