Skip to content

Commit

Permalink
docs(model): add sir tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
emptymalei committed Jan 4, 2025
1 parent 83926f4 commit 83e6333
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
55 changes: 55 additions & 0 deletions docs/tutorials/sir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.4
# kernelspec:
# display_name: .venv
# language: python
# name: python3
# ---

# %% [markdown]
# # SIR Model
#
# In this tutorial, we will learn how to use the SIR model.

# %%
import plotly.express as px

from hamilflow.models.sir import SIR

# %% [markdown]
# ## Model

# %%
sir_1 = SIR(
system={
"beta": 0.03,
"alpha": 0.1,
"delta_t": 0.1,
},
initial_condition={
"susceptible_0": 999,
"infected_0": 1,
"recovered_0": 0,
},
)

# %%
n_steps = 100

sir_1_results = sir_1.generate_from(n_steps=n_steps)
sir_1_results.head()

# %%
px.line(
sir_1_results,
x="t",
y=["S", "I", "R"],
)

# %%
8 changes: 4 additions & 4 deletions hamilflow/models/sir.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def generate_from(self, n_steps: int) -> pd.DataFrame:
:param n_steps: Number of steps to simulate
:return: DataFrame with time, S, I, R columns
"""
time_steps = np.arange(0, n_steps) * self.system.delta_t
time_steps = np.arange(1, n_steps) * self.system.delta_t

return self(time_steps)

Expand Down Expand Up @@ -155,8 +155,8 @@ def __call__(self, t: TypeTime) -> pd.DataFrame:

delta_s, delta_i, delta_r = self._step(susceptible, infected)

susceptible += delta_s
infected += delta_i
recovered += delta_r
susceptible = max(susceptible + delta_s, 0)
infected = max(infected + delta_i, 0)
recovered = max(recovered + delta_r, 0)

return pd.DataFrame(results)

0 comments on commit 83e6333

Please sign in to comment.