Skip to main content
🧠15% 🤖85%

Tutorials

The tutorials teach Starsim through one example: a simplified measles outbreak in a town, which gains a latent period, age groups, vaccination, data to fit it to, and more. Each tutorial starts with the model so far, so you can start at any one, and ends with exercises. The user guide covers each topic in full.

If you are new to Starsim, start with T1–T3, which cover what every model needs. T4–T6 add what most real analyses need: who meets whom, interventions, and data. T7 and T8 are for when you need them. Sections marked “Advanced” can be skipped on a first read; many of them show the same model as agents, one simulated person at a time.

T1 – Getting started

A measles outbreak as an SIR model: run it, plot it, check what Starsim built, and run it as agents.

T2 – Building a model

The first model in more depth: states, transitions, parameters, time, and results.

T3 – Building a disease

A latent period, how long people stay in each state, waning immunity, and deaths; and, as agents, chance and superspreading.

T4 – Mixing

Who meets whom: age groups and contact matrices, movement between towns, and networks of contacts between people.

T5 – Interventions and scenarios

It’s not all doom and gloom: vaccination, measures that cut transmission, and comparing scenarios across many runs; and, as agents, treatment, screening, and partner notification.

T6 – Data and calibration

Fitting the model to reported cases, projecting a campaign with its uncertainty, and a real population with births and deaths.

T7 – Stratification and co-transmission

Splitting a model by more than age, and two diseases that affect each other.

T8 – Custom modules

Writing your own behavior as code: an intervention for compartments, and a disease, a quantity for each person, and a network for agents.

The answers to the exercises are on the solutions pages, one per tutorial.

Running the tutorials yourself

Install Starsim with pip install "starsim[jax]" (see installation). JAX is used for gradients and for fitting by NUTS in the user guide; the tutorials run without it (pip install starsim).

Download a tutorial as a Jupyter notebook from the “Jupyter notebook” link beside the page’s contents. The notebook has the Python code as cells; the R code is included as text.

Data files: T6 reads the reported cases from data/measles_cases.csv and data/measles_cases_after.csv. Save them in a folder called data beside the notebook. The other tutorials need no files.

To run all the tutorials at once, clone the Starsim repository, install it (pip install -e .), and open the files in docs/tutorials in Jupyter (jupyter lab): with jupytext installed, Jupyter opens the .qmd files directly, or run ./convert_notebooks there first to convert them.