T1 - Getting started

Installing and getting started with Covasim is quite simple.

To install, just type pip install covasim. If it worked, you should be able to import Covasim with import covasim as cv.

The basic design philosophy of Covasim is: common tasks should be simple. For example:

This tutorial walks you through how to do these things.

Click here to open an interactive version of this notebook.

Hello world

To create, run, and plot a sim with default options is just:

import covasim as cv

sim = cv.Sim()
sim.run()
fig = sim.plot()
Covasim 4.0.1 (2026-10-16) — © 2020-2026 by IDM
Initializing sim with 20000 agents
  Running 2020.03.01 ( 0/61) (0.00 s)  ———————————————————— 2%
  Running 2020.03.11 (10/61) (0.10 s)  •••————————————————— 18%
  Running 2020.03.21 (20/61) (0.19 s)  ••••••—————————————— 34%
  Running 2020.03.31 (30/61) (0.29 s)  ••••••••••—————————— 51%
  Running 2020.04.10 (40/61) (0.40 s)  •••••••••••••——————— 67%
  Running 2020.04.20 (50/61) (0.52 s)  ••••••••••••••••———— 84%
  Running 2020.04.30 (60/61) (0.65 s)  •••••••••••••••••••• 100%

Simulation summary:
       11,873 cumulative infections
        9,271 cumulative infectious
        5,913 cumulative symptomatic
          322 cumulative severe
           89 cumulative critical
        4,478 cumulative recoveries
           21 cumulative deaths
            0 cumulative known deaths
          533 cumulative reinfections
            0 cumulative tests
            0 cumulative diagnoses
            0 cumulative vaccine doses
            0 cumulative vaccinated people
            0 cumulative quarantined people
            0 cumulative isolated people

Defining parameters and running simulations

Parameters are defined as a dictionary. The most common parameters to modify are the population size, the initial number of people infected, and the start and end dates of the simulation. We can define those as:

pars = dict(
    pop_size = 50e3,
    pop_infected = 100,
    start_day = '2020-04-01',
    end_day = '2020-06-01',
)

Running a simulation is pretty easy. In fact, running a sim with the parameters we defined above is just:

sim = cv.Sim(pars)
sim.run()
Initializing sim with 50000 agents
  Running 2020.04.01 ( 0/62) (0.00 s)  ———————————————————— 2%
  Running 2020.04.11 (10/62) (0.13 s)  •••————————————————— 18%
  Running 2020.04.21 (20/62) (0.27 s)  ••••••—————————————— 34%
  Running 2020.05.01 (30/62) (0.42 s)  ••••••••••—————————— 50%
  Running 2020.05.11 (40/62) (0.60 s)  •••••••••••••——————— 66%
  Running 2020.05.21 (50/62) (0.81 s)  ••••••••••••••••———— 82%
  Running 2020.05.31 (60/62) (1.04 s)  •••••••••••••••••••— 98%
Simulation summary:
       36,255 cumulative infections
       30,772 cumulative infectious
       19,761 cumulative symptomatic
        1,234 cumulative severe
          325 cumulative critical
       17,839 cumulative recoveries
           50 cumulative deaths
            0 cumulative known deaths
        2,353 cumulative reinfections
            0 cumulative tests
            0 cumulative diagnoses
            0 cumulative vaccine doses
            0 cumulative vaccinated people
            0 cumulative quarantined people
            0 cumulative isolated people
Sim(n=50000; 2020.04.01—2020.06.01; networks=a; diseases=covid; connectors=crossimmunity)

This will generate a results dictionary sim.results. For example, the number of new infections per day is sim.results['new_infections'].

Rather than creating a parameter dictionary, any valid parameter can also be passed to the sim directly. For example, exactly equivalent to the above is:

sim = cv.Sim(pop_size=50e3, pop_infected=100, start_day='2020-04-01', end_day='2020-06-01')
sim.run()
Initializing sim with 50000 agents
  Running 2020.04.01 ( 0/62) (0.00 s)  ———————————————————— 2%
  Running 2020.04.11 (10/62) (0.12 s)  •••————————————————— 18%
  Running 2020.04.21 (20/62) (0.25 s)  ••••••—————————————— 34%
  Running 2020.05.01 (30/62) (0.38 s)  ••••••••••—————————— 50%
  Running 2020.05.11 (40/62) (0.56 s)  •••••••••••••——————— 66%
  Running 2020.05.21 (50/62) (0.76 s)  ••••••••••••••••———— 82%
  Running 2020.05.31 (60/62) (0.99 s)  •••••••••••••••••••— 98%
Simulation summary:
       36,255 cumulative infections
       30,772 cumulative infectious
       19,761 cumulative symptomatic
        1,234 cumulative severe
          325 cumulative critical
       17,839 cumulative recoveries
           50 cumulative deaths
            0 cumulative known deaths
        2,353 cumulative reinfections
            0 cumulative tests
            0 cumulative diagnoses
            0 cumulative vaccine doses
            0 cumulative vaccinated people
            0 cumulative quarantined people
            0 cumulative isolated people
Sim(n=50000; 2020.04.01—2020.06.01; networks=a; diseases=covid; connectors=crossimmunity)

You can mix and match too – pass in a parameter dictionary with default options, and then include other parameters as keywords (including overrides; keyword arguments take precedence). For example:

sim = cv.Sim(pars, pop_infected=10) # Use parameters defined above, except start with 10 infected people
sim.run()
Initializing sim with 50000 agents
  Running 2020.04.01 ( 0/62) (0.00 s)  ———————————————————— 2%
  Running 2020.04.11 (10/62) (0.11 s)  •••————————————————— 18%
  Running 2020.04.21 (20/62) (0.23 s)  ••••••—————————————— 34%
  Running 2020.05.01 (30/62) (0.35 s)  ••••••••••—————————— 50%
  Running 2020.05.11 (40/62) (0.48 s)  •••••••••••••——————— 66%
  Running 2020.05.21 (50/62) (0.63 s)  ••••••••••••••••———— 82%
  Running 2020.05.31 (60/62) (0.82 s)  •••••••••••••••••••— 98%
Simulation summary:
       12,354 cumulative infections
        8,222 cumulative infectious
        5,053 cumulative symptomatic
          280 cumulative severe
           75 cumulative critical
        3,287 cumulative recoveries
           10 cumulative deaths
            0 cumulative known deaths
          214 cumulative reinfections
            0 cumulative tests
            0 cumulative diagnoses
            0 cumulative vaccine doses
            0 cumulative vaccinated people
            0 cumulative quarantined people
            0 cumulative isolated people
Sim(n=50000; 2020.04.01—2020.06.01; networks=a; diseases=covid; connectors=crossimmunity)

Plotting results

As you saw above, plotting the results of a simulation is rather easy too:

fig = sim.plot()

Full usage example

Many of the details of this example will be explained in later tutorials, but to give you a taste, here’s an example of how you would run two simulations to determine the impact of a custom intervention aimed at protecting the elderly.

import covasim as cv

# Custom intervention -- see Tutorial 5
def protect_elderly(sim):
    if sim.ti == sim.day('2020-04-01'):
        elderly = sim.people.age>70
        sim.people.rel_sus[elderly] = 0.0

pars = dict(
    pop_type = 'hybrid', # Use a more realistic population model
    location = 'japan', # Use population characteristics for Japan
    pop_size = 50e3, # Have 50,000 people total in the population
    pop_infected = 100, # Start with 100 infected people
    n_days = 90, # Run the simulation for 90 days
    verbose = 0, # Do not print any output
)

# Running with multisims -- see Tutorial 3
s1 = cv.Sim(pars, label='Default')
s2 = cv.Sim(pars, interventions=protect_elderly, label='Protect the elderly')
msim = cv.MultiSim([s1, s2])
msim.run()
fig = msim.plot(['cum_deaths', 'cum_infections'])
Exception ignored while finalizing file <_io.BytesIO object at 0x7f52204d89f0>:
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.14.8/x64/lib/python3.14/site-packages/starsim/utils.py", line 671, in __getattr__
    raise AttributeError(attr)
BufferError: Existing exports of data: object cannot be re-sized