InstituteforDiseaseModeling / covasim

COVID-19 Agent-based Simulator (Covasim): a model for exploring coronavirus dynamics and interventions
https://covasim.org
MIT License
254 stars 223 forks source link

Start Sim Midway Through Pandemic #145

Open wpettine opened 4 years ago

wpettine commented 4 years ago

I would like to simulate forward from midway through a pandemic by specifying the starting number symptomatic, severe, etc. That way, we can simulate forward from an arbitrary time point, rather than starting from the very first day of the pandemic.

If this is low on the priority list, I'm happy to take a crack.

cliffckerr commented 4 years ago

@wpettine The easiest way to do it is to run the simulation, then save the results, then pick up where you left off. Here's some example code:

import covasim as cv

fn = 'halfrun.sim'

s1 = cv.Sim()

cb = cv.change_beta(days=[30, 40], changes=[0.1, 1.0])

s1['interventions'] = [cb]

s2 = s1.copy()

s1.run()
s1.plot()

s2.initialize()
for i in range(35):
    s2.step()
s2.save(fn, keep_people=True)

s3 = cv.Sim.load(fn)

s3.run()
s3.plot()

Does that do what you need?

wpettine commented 4 years ago

For a scenario then, would that be used as a basesim? Also, would it be an issue manipulating parameters within the "halfrun," such as runtime, or others that are not accessible as interventions? I've tried that, and it didn't work too well. If it should work in theory though, I can spend more time playing with the code.

To expand a bit more on why this is important, I am trying to model a suite of policy choices ("scenarios") to be made on May 1st in Colorado. As it stands, I am simulating them from the start of the pandemic on February 1st. After building in the historical policy interventions, the margins of error become huge (see attached file). Thus, by the time we get to the actual policy choice, the margins are drowning out the difference between the interventions.

It would be incredibly useful though if I could just start the different policy scenarios on May 1st with known numbers, and then explore the whole range of them. For policy-makers who want to use these results to project orders of medical supplies, that feature is a game-changer.

Daily number infected

cliffckerr commented 4 years ago

Hi @wpettine yes, that should be possible by adapting the code above.

cliffckerr commented 4 years ago

Closing for now since seems possible with current version of code

wpettine commented 4 years ago

Sorry, for the delayed response! Only now getting back to the code to help calculate a PPE order.

The code you provided simply runs the same simulation again. Thus, the results of s3 are exactly the same as s1. What I'm hoping to do is manipulate the parameters midway.

For example, let's say I try to manipulate the 'n_days' param to extend it further.

s4 = s2.copy()
s4.pars['n_days'] = 120
s4.run()

This throws an error.

Or, let's say I just try to add an intervention.

s3 = cv.Sim.load(fn)
s3.pars['interventions'] = [cv.InterventionDict('change_beta', pars={'days': [38, 55], 'changes': [0.02, 0], 'layers': None, 'do_plot': None})]
s3.run()

This also throws an error.

Am I misunderstanding something?

yycccyk commented 4 months ago

Can some simulation parameters be changed midway now?

cliffckerr commented 4 months ago

Hi, and sorry for missing your (4 years) earlier comment! Yes, parameters can be changed -- see the cv.dynamic_pars() intervention. Docs are here and the tutorial is here. Does that answer your question?

yycccyk commented 4 months ago

The dynamic change of parameters is also predefined before the simulation; it is only possible to define in advance on which day to change the parameters during the simulation. This is not the effect I want to achieve. However, I found that each intervention measure in the intervention file has a built-in initializable function. By applying this function, I can achieve the desired effect.

sim = cv.Sim()
sim.initialize()
change_beta = cv.change_beta(days=[0], changes=[1])
for step in range(sim.npts):
    sim.step()
    if sim.t == 20:
        change_beta.days = 20
        change_beta.changes = 1
        change_beta.initialize(sim)
        sim['interventions'] = [change_beta]
while sim.t < sim.npts - 1:
    sim.step()
sim.finalize()
sim.plot()