Skip to content

Latest commit

 

History

94 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Epidemic Modeling for Everyone

Multipopulation model

Repository to accompany the blog series: Epidemic Modeling

These blog posts were recently featured in the Data Exchange Podcast by Ben Lorica in two episodes:

The epidemik package

The compartmental modeling engine that powers the notebooks in this repo, from simple SIR models to networks and metapopulations.

These notebooks build their models with epidemik, a companion Python package for simulating compartmental epidemic models. It lets you define arbitrary compartmental models from interaction (S + I -> I + I) and spontaneous (I -> R) transitions, integrate them deterministically or run seeded stochastic simulations with the same interface, and compute R0 automatically via the next-generation matrix. It also supports vaccination campaigns, birth/death rates, seasonal forcing, age structure, and — through its NetworkEpiModel and MetaEpiModel classes — epidemics on contact networks and across coupled sub-populations.

Under the hood, a model is represented as a directed multigraph (built on networkx), integrated with scipy's ODE solvers, and returned as tidy pandas DataFrames that plug directly into matplotlib for quick trajectory and model-structure plots. Named parameters can reference one another as expressions (e.g. mu="beta/2"), which keeps related rates in sync as you sweep scenarios. Model definitions can be saved to and loaded from YAML, so you can version-control a model independently of the notebook that runs it, or pull a ready-made model straight from the epidemik repository. Starting with Epidemiology304, every notebook in the Advanced Models section builds directly on top of epidemik rather than hand-rolled ODE code, so understanding the package is the fastest way to follow — and extend — those notebooks.

from epidemik import EpiModel

SIR = EpiModel(seed=1337)
SIR.add_interaction('S', 'I', 'I', beta=0.2)
SIR.add_spontaneous('I', 'R', mu=0.1)

Background Information

Context-setting posts on CoVID-19 as a global phenomenon, no modeling required.

An introduction to the CoVID-19 pandemic and why it became the first truly global event of its kind, setting the stage for the modeling posts that follow. The first post walks through the basics anyone needed to understand CoVID-19 as it was unfolding — how it spread, why it was different from prior outbreaks, and the terminology used throughout the rest of the series. The second post zooms out to explain what made this pandemic historically unusual: near-simultaneous, worldwide transmission enabled by modern travel networks, and why that global reach makes CoVID-19 a uniquely rich case study for the modeling techniques covered later in this repository.

  1. CoVID-19: Everything you need to know

  2. CoVID-19: The first truly global event

Visualization

Notebooks that visualize CoVID-19 case, patient, and mortality data without building predictive models.

Covers plotting the geographic and temporal spread of the pandemic, exploring individual patient-level data, and building simple death-toll forecasts from observed trends. Epidemiology001 reconstructs how the outbreak spread across countries and over time directly from public case-count data, giving you an intuitive feel for the data before any model is introduced. Epidemiology002 drills down from aggregate counts to individual patient records, showing how demographics and outcomes vary case by case. Epidemiology003 uses simple trend extrapolation — no compartmental model yet — to forecast near-term deaths, illustrating both the appeal and the pitfalls of naive forecasting that the later Compartmental Models section addresses head-on.

  1. Epidemiology001.ipynb - Visualizing the spread of CoVID-19

  2. Epidemiology002.ipynb - Visualizing individual CoVID-19 patient data

  3. Epidemiology003.ipynb - CoVID-19: Forecasting the death toll

Compartmental Models

The core SIR/SEIR-family models: exponential fits, confidence intervals, seasonality, and competing strains.

Builds up classic compartmental epidemic models step by step, starting from why naive exponential fits mislead, then adding uncertainty quantification, seasonal forcing, and competition between multiple circulating strains. Epidemiology101 explains why fitting a raw exponential to early case counts overestimates growth and leads to bad predictions, motivating the shift to compartmental (SIR-style) models. Epidemiology102 introduces those compartmental models properly, along with an honest discussion of their assumptions and limitations. Epidemiology103 adds confidence intervals and stochastic effects, so a model's output is a distribution of plausible trajectories rather than a single deterministic curve. Epidemiology104 incorporates seasonal forcing, showing how transmission rates that vary over the year reshape the epidemic curve and complicate long-term projections. Epidemiology105 extends the framework to multiple competing strains, modeling how variants interact and compete for the same susceptible population.

  1. Epidemiology101.ipynb - Epidemic Modeling 101: Or why your CoVID19 exponential fits are wrong

  2. Epidemiology102.ipynb - Epidemic Modeling 102: All CoVID-19 models are wrong, but some are useful

  3. Epidemiology103.ipynb - Epidemic Modeling 103: Adding confidence intervals and stochastic effects to your CoVID-19 Models

  4. Epidemiology104.ipynb - Epidemic Modeling 104: Impact of Seasonal effects on CoVID-19

  5. Epidemiology105.ipynb - Epidemic Modeling 105: Competing CoVID-19 Strains

Network models

Moving beyond well-mixed populations to explicit contact networks, super-spreaders, and degree correlations.

Examines how the structure of who-contacts-whom shapes an outbreak, including the role of super-spreaders in contact tracing and how correlations between connected individuals' degrees affect spreading dynamics. Epidemiology201 replaces the homogeneous-mixing assumption of earlier notebooks with an explicit contact network, showing how a small number of highly connected super-spreaders can dominate transmission and how contact tracing exploits that structure to contain outbreaks more efficiently than blanket interventions. Epidemiology202 goes further by varying degree correlations — whether highly connected individuals tend to link to other highly connected individuals or to poorly connected ones — and shows how that single structural property changes epidemic thresholds and final outbreak size, even when the average number of contacts stays fixed.

  1. Epidemiology 201.ipynb - Epidemiology 201: Network Structure, Super-spreaders and Contact Tracing

  2. Epidemiology 202.ipynb - Epidemiology 202: Network Models, the effect of degree correlations

Advanced Models

Extensions that add real-world structure: vaccination, age, geography, demographics, and social contagion.

Covers more realistic model extensions, including the impact of vaccination campaigns, age-structured populations, metapopulation (multi-location) spreading, demographic processes like births and deaths, and an application of epidemic modeling to the spread of ideas as a "cognitive virus."

  1. Epidemiology 301.ipynb - Epidemiology 301: How to model the effects of vaccination

  2. Epidemiology 302.ipynb - Epidemiology 302: The Impact of Age Structure on Epidemic Spreading

  3. Epidemiology 303.ipynb - Epidemiology 303: Metapopulation Models

  4. Epidemiology 304.ipynb - Epidemiology 304: Demographics

  5. Epidemiology 305.ipynb - Epidemiology 305: The Epidemiology of a Cognitive Virus

Setup

GitHub Codespaces

Open in GitHub Codespaces

The repo includes a .devcontainer configuration, so you can run every notebook online without installing anything locally. Click the badge above (or use the "Code" → "Codespaces" button on GitHub) to launch a ready-to-use environment: it installs the Graphviz/GEOS/PROJ system libraries pygraphviz and cartopy need, installs uv, and runs uv sync automatically. Once the Codespace finishes building, open any .ipynb file and select the .venv kernel (or run uv run jupyter lab in the terminal) to start working.

Gitpod

Open in Gitpod

An alternative to Codespaces: the .gitpod.yml config does the same setup — installing the system libraries pygraphviz/cartopy need, installing uv, and running uv sync — on top of Gitpod's own workspace image. Click the badge above to launch it, then run uv run jupyter lab in the terminal (or open a notebook directly with the VS Code Jupyter extension, which is pre-installed).

Local install

This project uses uv to manage its Python environment, including epidemik and the rest of the dependencies listed in pyproject.toml. To install the dependencies and launch Jupyter:

uv sync
uv run jupyter lab

pygraphviz compiles against the system Graphviz library. On macOS with Homebrew:

brew install graphviz
CFLAGS="-I$(brew --prefix graphviz)/include" LDFLAGS="-L$(brew --prefix graphviz)/lib" uv sync

Author

Bruno Gonçalves

Bruno Gonçalves

Data For Science, Inc.

Web: www.data4sci.com
Twitter/X: @bgoncalves
LinkedIn: @bmtgoncalves
Email: info@data4sci.com
Schedule a Call: https://data4sci.com/call

Releases

Packages

Used by

Contributors

Languages