```python
from epipack import SymbolicEpiModel, DeterministicEpiModel, StochasticEpiModel
import sympy as sy 
%matplotlib notebook
```


```python
%matplotlib notebook
```


```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 0x119a67ad0>




```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.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()
```




    {-omega: 1, eta - rho: 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 = DeterministicEpiModel([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.deterministic_epi_models.DeterministicEpiModel at 0x119f62710>




```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("incidence")
pl.ylim([0,1])
pl.legend()
pl.gcf().tight_layout()
```


    <IPython.core.display.Javascript object>







```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 0x11c174e50>




```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("incidence")
pl.ylim([0,1])
pl.legend()
pl.gcf().tight_layout()
pl.show()
```


    <IPython.core.display.Javascript object>






fffb70aa17f69cf9b27d7d90af4427809d9caf39
ipynb
Jupyter Notebook
cookbook/notebooks/epipack_SIRS_example.ipynb
PaPeK/epipack
52fb26ba35672fbce1f2f598eac2ed71e6fcb90f
MIT
cookbook/notebooks/epipack_SIRS_example.ipynb
PaPeK/epipack
52fb26ba35672fbce1f2f598eac2ed71e6fcb90f
MIT
cookbook/notebooks/epipack_SIRS_example.ipynb
PaPeK/epipack
52fb26ba35672fbce1f2f598eac2ed71e6fcb90f
MIT
Qwen/Qwen-72B
1. YES
2. YES