import starsim as ss
= dict(
pars = 5_000,
n_agents = ss.peryear(20),
birth_rate = 15,
death_rate = dict(
networks type = 'randomnet', # Or 'random'
= 4
n_contacts
),= dict(
diseases type = 'sir',
= 10,
dur_inf = 0.1,
beta
) )
T6 - Interventions
Understanding the impact of interventions is one of the most common reasons to use a disease model. This tutorial shows how to implement standard interventions, as well as how to define your own custom interventions.
An interactive version of this notebook is available on Google Colab or Binder.
Products and interventions
Starsim contains products, which can be thought of as the actual test, diagnostic, treatment, or vaccine product being used, as well as interventions, which are responsible for delivering the products to the population.
Depending on what disease you’re modeling, you might need to define your own custom products and interventions, or you might be able to directly use some of the examples provided in Starsim.
Starsim includes three basic “types” of products: diagnostics, treatment, and vaccination. There isn’t a lot of detail in the templates for each of these, because most of the details about products is specific to a disease. There are also some disease-specific products built in the Starsim’s library of diseases - these can generally be found in the diseases
subfolder (e.g. the cholera interventions are in cholera.py
.
Starsim also includes several basic types of intervention:
routine_vx()
for routine vaccination,campaign_vx()
for one-off campaigns- similarly,
routine_screening()
andcampaign_screening()
for different types of screening program treat_num()
, which treats a certain number of people each timestep (by default, as many people as need treatment, but you can also set a maximum).
These are sometimes general enough that they don’t need to be tailored to a particular disease, and you can just use them directly. That being said, you are always welcome to tailor them as you like to capture particular features of the intervention you’re modeling.
Vaccination
To create an example, let’s first create the parameters that we want to use for the simulation:
Now we’ll create a vaccine product and a vaccination intervention:
# Create the product - a vaccine with 50% efficacy
= ss.simple_vx(efficacy=0.5)
my_vaccine
# Create the intervention
= ss.routine_vx(
my_intervention =2015, # Begin vaccination in 2015
start_year=0.2, # 20% coverage
prob=my_vaccine # Use the MyVaccine product
product
)
# Now create two sims: a baseline sim and one with the intervention
= ss.Sim(pars=pars)
sim_base
sim_base.run()= ss.Sim(pars=pars, interventions=my_intervention)
sim_intv sim_intv.run()
Initializing sim with 5000 agents
Running 2000.01.01 ( 0/51) (0.00 s) ———————————————————— 2%
Running 2010.01.01 (10/51) (0.15 s) ••••———————————————— 22%
Running 2020.01.01 (20/51) (0.18 s) ••••••••———————————— 41%
Running 2030.01.01 (30/51) (0.21 s) ••••••••••••———————— 61%
Running 2040.01.01 (40/51) (0.23 s) ••••••••••••••••———— 80%
Running 2050.01.01 (50/51) (0.26 s) •••••••••••••••••••• 100%
Initializing sim with 5000 agents
Running 2000.01.01 ( 0/51) (0.00 s) ———————————————————— 2%
Running 2010.01.01 (10/51) (0.03 s) ••••———————————————— 22%
Running 2020.01.01 (20/51) (0.06 s) ••••••••———————————— 41%
Running 2030.01.01 (30/51) (0.09 s) ••••••••••••———————— 61%
Running 2040.01.01 (40/51) (0.12 s) ••••••••••••••••———— 80%
Running 2050.01.01 (50/51) (0.15 s) •••••••••••••••••••• 100%
Sim(n=5000; 2000—2050; demographics=births, deaths; networks=randomnet; interventions=routine_vx; diseases=sir)
If we want to see the impact, we can create a plot:
import matplotlib.pyplot as plt
= sim_base.results
res_base = sim_intv.results
res_intv
plt.figure()='Baseline')
plt.plot(res_base.timevec, res_base.sir.prevalence, label='Vax')
plt.plot(res_intv.timevec, res_intv.sir.prevalence, label=ss.date(2015), color='k', ls='--')
plt.axvline(x'Prevalence')
plt.title(
plt.legend() plt.show()
We can see that from the year of introducing the vaccine, prevalence starts to fall.
Custom interventions
The most common way of accessing diseases for modification by interventions is via their names (e.g. sir
, sis
). This means that typically, different interventions need to be defined for different diseases. The example below shows how to define a simple vaccine for SIS which is just a function, rather than a class:
import starsim as ss
def simple_sis_vaccine(sim, start=2030, eff=0.9):
if sim.now == start:
*= 1-eff
sim.diseases.sis.rel_trans[:] return
= dict(
pars =2000,
start=2050,
stop='sis',
diseases='random',
networks=simple_sis_vaccine,
interventions
)
= ss.Sim(pars)
sim
sim.run()
sim.plot() ss.show()
Initializing sim with 10000 agents
Running 2000.01.01 ( 0/51) (0.00 s) ———————————————————— 2%
Running 2010.01.01 (10/51) (0.04 s) ••••———————————————— 22%
Running 2020.01.01 (20/51) (0.07 s) ••••••••———————————— 41%
Running 2030.01.01 (30/51) (0.10 s) ••••••••••••———————— 61%
Running 2040.01.01 (40/51) (0.14 s) ••••••••••••••••———— 80%
Running 2050.01.01 (50/51) (0.17 s) •••••••••••••••••••• 100%
Figure(768x576)
Exercises
- If we change the disease from SIR to SIS and set coverage to 100%, what minimum efficacy of vaccine is required to eradicate the disease by 2050? You will need to implement an
sis_vaccine
class as well (based onss.sir_vaccine
– not the simple vaccine above), but can re-usess.routine_vx
.