Basic workflow¶
This page demonstrates how to perform basic operations in Atomica. First, we will set up the notebook environment - the commands below are typically not required in user scripts:
[1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import sys
sys.path.append('..')
To start with, import Atomica itself. It is often also useful to import numpy
and matplotlib
[2]:
import atomica as at
import numpy as np
import matplotlib.pyplot as plt
Starting an application¶
The first step in starting a new application is to write a Framework file. This can be done by copying one of the templates in the atomica/library
folder (either framework_template.xlsx
or framework_template_advanced.xlsx
) and implementing your model. Further guidance on this is provided separately in the framework documentation.
After writing the Framework, the next step is to generate a databook. This is performed in three steps
Load the framework into a
ProjectFramework
Python instanceUse the framework to make a new
ProjectData
instanceSave the
ProjectData
instance to a spreadsheet
In this example, we will load an existing framework from the library. You can use at.LIBRARY_PATH
to refer to the folder containing the library Excel files shipped with Atomica:
[3]:
F = at.ProjectFramework(at.LIBRARY_PATH / 'tb_framework.xlsx') # Load the Framework
D = at.ProjectData.new(F,pops=2, tvec=np.arange(2000,2018), transfers=0)
D.save('new_databook.xlsx')
Object saved to /home/vsts/work/1/s/docs/examples/new_databook.xlsx.
The ProjectData
class in Python can be thought of as an equivalent representation of the databook - you can edit the databook in Excel, which will result in changes to the ProjectData
variable when the spreadsheet is loaded, and you can modify the ProjectData
in Python and then write a modified spreadsheet. ProjectData
has a number of methods that you can use to modify the databook, to do things like
Add or remove populations
Change the time span of the databook
To perform these operations, you can load in a databook using ProjectData.from_spreadsheet()
. This lets you load in a databook given a particular framework. It is not required that the databook be completed prior to loading - you only need to complete the databook in its entirity if you want to use the databook in a project. So for example, to add an additional population and a transfer to this newly created databook, we could use:
[4]:
D = at.ProjectData.from_spreadsheet('new_databook.xlsx', framework=F)
D.add_pop('pris','Prisoners')
D.add_transfer('aging','Aging')
D.save('new_databook_2.xlsx')
Object saved to /home/vsts/work/1/s/docs/examples/new_databook_2.xlsx.
Creating a project¶
Once you have completed the framework file and databook, you can create a project that can be used to run simulations and analyses. To do this, simply create a Project
instance, passing in the file names for the framework and databook. Here we will use a pre-filled databook from the library:
[5]:
P = at.Project(framework=at.LIBRARY_PATH / 'tb_framework.xlsx', databook=at.LIBRARY_PATH / 'tb_databook.xlsx')
Elapsed time for running "default": 0.939s
When you create a project, a default simulation is automatically run. You can subsequently run simulations using P.run_sim()
[6]:
res = P.run_sim(parset='default', result_name='Default parset')
P.results.keys()
Elapsed time for running "default": 0.988s
[6]:
['parset_default']
When you run a simulation, by default it is automatically copied into the project, as well as being returned. Specifying the result name is optional, but recommended because it helps to keep track of the simulations when comparing and plotting them. We can now plot the result to show the compartment sizes:
[7]:
d = at.PlotData(res,pops='0-4',project=P)
at.plot_series(d,plot_type='stacked', data=P.data, legend_mode='separate');
For full details on plotting, please refer to the full plotting documentation here.
Calibrating the model¶
Model calibration can be performed in one of two ways - either manually, or automatically
Manual calibration¶
Manual calibration of the model proceeds in three steps
Make a new ParameterSet (e.g., by copying an existing one)
Modify the calibration scale factors in that ParameterSet
Run a simulation using the new parameter set
The commands to do this are shown below, for an example where the force of infection has been decreased:
[8]:
new_parset = P.parsets.copy('default','manually_calibrated')
new_parset.pars['foi_out'].meta_y_factor = 0.8 # Decrease infectiousness of all populations
new_parset.pars['foi_in'].y_factor['0-4'] = 2.0 # Increase susceptibility of young children
res_manually_calibrated = P.run_sim(parset='manually_calibrated', result_name='Manually calibrated')
d = at.PlotData([res,res_manually_calibrated],outputs='ac_inf',project=P)
at.plot_series(d, axis='results');
Elapsed time for running "default": 1.04s
Automatic calibration¶
To perform an automatic calibration, simply use P.calibrate()
specifying the amount of time to run the calibration for, and the name of the new calibrated parset to create.
[9]:
P.calibrate(max_time=10, parset='default', new_name="auto_calibrated", save_to_project=True);
ASD: Launching with random seed None
Elapsed time for running "default": 0.652s
Elapsed time for running "default": 0.506s
step=1 choice=192, par=62, pm=1.0, origval=1.0, newval=0.9
step 1 (0.5 s) ++ (orig:10.14 | best:10.14 | new:10.14 | diff:-0.000005557)
Elapsed time for running "default": 0.519s
step=2 choice=187, par=57, pm=1.0, origval=1.0, newval=0.9
step 2 (1.0 s) ++ (orig:10.14 | best:10.14 | new:10.12 | diff:-0.01620)
Elapsed time for running "default": 0.502s
step=3 choice=192, par=62, pm=1.0, origval=0.9, newval=0.75
step 3 (1.5 s) ++ (orig:10.14 | best:10.12 | new:10.12 | diff:-0.000007145)
Elapsed time for running "default": 0.490s
step=4 choice=46, par=46, pm=0.0, origval=1.0, newval=1.1
step 4 (2.0 s) ++ (orig:10.14 | best:10.12 | new:10.12 | diff:-0.002866)
Elapsed time for running "default": 0.680s
step=5 choice=12, par=12, pm=0.0, origval=1.0, newval=1.1
step 5 (2.7 s) -- (orig:10.14 | best:10.12 | new:10.39 | diff:0.2698)
Elapsed time for running "default": 0.495s
step=6 choice=212, par=82, pm=1.0, origval=1.0, newval=0.9
step 6 (3.2 s) ++ (orig:10.14 | best:10.12 | new:9.863 | diff:-0.2536)
Elapsed time for running "default": 0.502s
step=7 choice=36, par=36, pm=0.0, origval=1.0, newval=1.1
step 7 (3.7 s) -- (orig:10.14 | best:9.863 | new:9.863 | diff:0)
Elapsed time for running "default": 0.488s
step=8 choice=192, par=62, pm=1.0, origval=0.75, newval=0.5249999999999999
step 8 (4.2 s) -- (orig:10.14 | best:9.863 | new:9.863 | diff:0.000004321)
Elapsed time for running "default": 0.510s
step=9 choice=259, par=129, pm=1.0, origval=1.0, newval=0.9
step 9 (4.7 s) -- (orig:10.14 | best:9.863 | new:9.974 | diff:0.1111)
Elapsed time for running "default": 0.688s
step=10 choice=8, par=8, pm=0.0, origval=1.0, newval=1.1
step 10 (5.4 s) -- (orig:10.14 | best:9.863 | new:9.989 | diff:0.1255)
Elapsed time for running "default": 0.509s
step=11 choice=240, par=110, pm=1.0, origval=1.0, newval=0.9
step 11 (5.9 s) ++ (orig:10.14 | best:9.863 | new:9.640 | diff:-0.2231)
Elapsed time for running "default": 0.496s
step=12 choice=107, par=107, pm=0.0, origval=1.0, newval=1.1
step 12 (6.4 s) -- (orig:10.14 | best:9.640 | new:9.640 | diff:0)
Elapsed time for running "default": 0.490s
step=13 choice=85, par=85, pm=0.0, origval=1.0, newval=1.1
step 13 (6.9 s) -- (orig:10.14 | best:9.640 | new:9.688 | diff:0.04796)
Elapsed time for running "default": 0.487s
step=14 choice=198, par=68, pm=1.0, origval=1.0, newval=0.9
step 14 (7.4 s) ++ (orig:10.14 | best:9.640 | new:9.619 | diff:-0.02157)
Elapsed time for running "default": 0.672s
step=15 choice=233, par=103, pm=1.0, origval=1.0, newval=0.9
step 15 (8.1 s) -- (orig:10.14 | best:9.619 | new:9.619 | diff:0)
Elapsed time for running "default": 0.496s
step=16 choice=222, par=92, pm=1.0, origval=1.0, newval=0.9
step 16 (8.6 s) -- (orig:10.14 | best:9.619 | new:9.619 | diff:0)
Elapsed time for running "default": 0.493s
step=17 choice=120, par=120, pm=0.0, origval=1.0, newval=1.1
step 17 (9.1 s) -- (orig:10.14 | best:9.619 | new:10.04 | diff:0.4256)
Elapsed time for running "default": 0.490s
step=18 choice=172, par=42, pm=1.0, origval=1.0, newval=0.9
step 18 (9.6 s) ++ (orig:10.14 | best:9.619 | new:9.024 | diff:-0.5947)
Elapsed time for running "default": 0.488s
step=19 choice=43, par=43, pm=0.0, origval=1.0, newval=1.1
step 19 (10.1 s) -- (orig:10.14 | best:9.024 | new:9.078 | diff:0.05397)
=== Time limit reached (10.10 > 10.00) (19 steps, orig: 10.14 | best: 0 | ratio: 0) ===
You can then run a simulation with the calibrated parset by passing the name of the new parset to run_sim
[10]:
res_auto_calibrated = P.run_sim(parset='auto_calibrated',result_name='Automatically calibrated')
Elapsed time for running "default": 0.862s
Adding programs¶
The programs system allows parameter values to be overwritten based on spending on a set of programs. To get started, you will first need a program spreadsheet (progbook). The progbook is specific to a framework and a databook, because it refers to both the compartments and parameters of the model (from the framework) as well as the populations (from the databook).
You can make a new progbook using the .make_progbook()
method of the project:
[11]:
P.make_progbook(progbook_path='example_progbook.xlsx', progs=4, data_start=2014, data_end=2018)
Object saved to /home/vsts/work/1/s/docs/examples/example_progbook.xlsx.
[11]:
'/home/vsts/work/1/s/docs/examples/example_progbook.xlsx'
After filling out the progbook, you can load it into the project using the .load_progbook()
method. Here, we will load in a pre-filled progbook from the library:
[12]:
P.load_progbook(at.LIBRARY_PATH / 'tb_progbook.xlsx')
[12]:
<atomica.programs.ProgramSet at 0x7fd79fb98bd0>
[<class 'atomica.programs.ProgramSet'>, <class 'atomica.utils.NamedItem'>, <class 'object'>]
————————————————————————————————————————————————————————————
Methods:
_get_code_name() add_pop() remove_par()
_normalize_inpu... add_program() remove_pop()
_read_effects() copy() remove_program()
_read_spending() from_spreadsheet() sample()
_read_targeting() get_alloc() save()
_write_effects() get_capacities() to_spreadsheet()
_write_spending() get_outcomes() to_workbook()
_write_targeting() get_prop_covera... validate()
add_comp() new() add_par()
remove_comp()
————————————————————————————————————————————————————————————
_book: None
_formats: None
_pop_types: ['default']
_references: None
comps: #0: 'initj': {'label': 'Initialization population size',
'type': 'de [...]
covouts: #0: ('v_num', '0-4'):
Parameter: v_num
Population: 0-4
Baseline [...]
created: datetime.datetime(2024, 2, 5, 19, 33, 17, 198494,
tzinfo=tzutc())
currency: '$'
gitinfo: {'branch': 'Detached head (no branch)', 'hash':
'17efbee', 'date': '2 [...]
modified: datetime.datetime(2024, 2, 5, 19, 33, 18, 355036,
tzinfo=tzutc())
name: 'default'
pars: #0: 'v_num': {'label': 'Number of vaccinations
administered', ' [...]
pops: #0: '0-4': {'label': 'Children 0-4', 'type':
'default'}
#1: '5- [...]
programs: #0: 'BCG':
<atomica.programs.Program at 0x7fd79e3e2d50>
[<class 'atom [...]
tvec: array([2015., 2016., 2017.])
version: '1.28.1'
————————————————————————————————————————————————————————————
Program set name: default
Programs: ['BCG', 'PCF', 'ACF', 'ACF-p', 'HospDS', 'HospMDR', 'HospXDR', 'AmbDS', 'AmbMDR', 'XDRnew', 'PrisDS', 'PrisDR']
Date created: 2024-Feb-05 19:33:17 UTC
Date modified: 2024-Feb-05 19:33:18 UTC
============================================================
This progbook has been added to the list of available progsets:
[13]:
P.progsets.keys()
[13]:
['default']
Running a simulation with programs requires one additional piece of information - a ProgramInstructions
instance that specifies
What years the programs are active
Any overwrites to spending or coverage
In our case, we might just want to run a simulation with programs starting in 2018, so we can create a ProgramInstructions
instance accordingly, and then use it to run the simulation:
[14]:
instructions = at.ProgramInstructions(start_year=2018)
res_progs = P.run_sim(parset='default',progset='default',progset_instructions=instructions)
Elapsed time for running "default": 0.965s
Reconciliation¶
Reconciliation is an operation that aims to change the properties of programs (such as their unit costs) such that the program-calculated parameter values optimally match the databook parameter values in the year the programs become active (or some other specified year). The reconciliation operation can therefore be treated as a mapping from one progset to another. To perform reconciliation, use the reconcile
function directly, passing in:
the parameter set you want to match
the program set to modify
the reconciliation year
a specification of which aspects of the program set to modify (e.g. unit cost, program outcomes)
The reconcile function returns a new progset, which you can store in the project if desired, or otherwise work with independently:
[15]:
P.progsets['reconciled'] = at.reconcile(project=P, parset='default', progset='default', reconciliation_year=2018, unit_cost_bounds=0.05)[0]
WARNING {reconciliation.py:243} - Reconcilation when parameter is in number units not fully tested
Program set 'default' will be ignored while running project 'default' due to the absence of program set instructions
Elapsed time for running "default": 0.873s
Reconciling in 2018.00, evaluating from 2018.00 up to 2018.25
ASD: Launching with random seed None
step=1 choice=13, par=1, pm=1.0, origval=1000.0, newval=950.0
step 1 (0.0 s) ++ (orig:61.58 | best:61.58 | new:61.58 | diff:-0.004674)
step=2 choice=20, par=8, pm=1.0, origval=4500.0, newval=4275.0
step 2 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0)
step=3 choice=5, par=5, pm=0.0, origval=7500.0, newval=7875.0
step 3 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.68 | diff:0.09877)
step=4 choice=2, par=2, pm=0.0, origval=4000.0, newval=4200.0
step 4 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0.00002251)
step=5 choice=3, par=3, pm=0.0, origval=2500.0, newval=2625.0
step 5 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0.005385)
step=6 choice=9, par=9, pm=0.0, origval=18000.0, newval=18900.0
step 6 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.59 | diff:0.01249)
step=7 choice=0, par=0, pm=0.0, origval=2.5, newval=2.625
step 7 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0.00004553)
step=8 choice=0, par=0, pm=0.0, origval=2.5, newval=2.625
step 8 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0.00004553)
step=9 choice=11, par=11, pm=0.0, origval=8000.0, newval=8400.0
step 9 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0)
step=10 choice=14, par=2, pm=1.0, origval=4000.0, newval=3800.0
step 10 (0.0 s) ++ (orig:61.58 | best:61.58 | new:61.58 | diff:-0.00002487)
step=11 choice=2, par=2, pm=0.0, origval=3800.0, newval=4000.0
step 11 (0.0 s) -- (orig:61.58 | best:61.58 | new:61.58 | diff:0.00002487)
step=12 choice=16, par=4, pm=1.0, origval=4900.0, newval=4655.0
step 12 (0.0 s) ++ (orig:61.58 | best:61.58 | new:61.51 | diff:-0.06943)
step=13 choice=18, par=6, pm=1.0, origval=10000.0, newval=9500.0
step 13 (0.0 s) ++ (orig:61.58 | best:61.51 | new:61.48 | diff:-0.02985)
step=14 choice=21, par=9, pm=1.0, origval=18000.0, newval=17100.0
step 14 (0.0 s) ++ (orig:61.58 | best:61.48 | new:61.47 | diff:-0.01325)
step=15 choice=22, par=10, pm=1.0, origval=5500.0, newval=5225.0
step 15 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.47 | diff:0.003359)
step=16 choice=1, par=1, pm=0.0, origval=950.0, newval=1050.0
step 16 (0.0 s) -- (orig:61.58 | best:61.47 | new:61.48 | diff:0.009103)
step=17 choice=17, par=5, pm=1.0, origval=7500.0, newval=7125.0
step 17 (0.0 s) ++ (orig:61.58 | best:61.47 | new:61.39 | diff:-0.08149)
step=18 choice=4, par=4, pm=0.0, origval=4655.0, newval=5145.0
step 18 (0.0 s) -- (orig:61.58 | best:61.39 | new:61.53 | diff:0.1476)
step=19 choice=3, par=3, pm=0.0, origval=2500.0, newval=2625.0
step 19 (0.0 s) -- (orig:61.58 | best:61.39 | new:61.39 | diff:0.005385)
step=20 choice=8, par=8, pm=0.0, origval=4500.0, newval=4725.0
step 20 (0.0 s) -- (orig:61.58 | best:61.39 | new:61.39 | diff:0)
step=21 choice=15, par=3, pm=1.0, origval=2500.0, newval=2375.0
step 21 (0.0 s) ++ (orig:61.58 | best:61.39 | new:61.38 | diff:-0.005951)
step=22 choice=6, par=6, pm=0.0, origval=9500.0, newval=10500.0
step 22 (0.0 s) -- (orig:61.58 | best:61.38 | new:61.44 | diff:0.05689)
step=23 choice=20, par=8, pm=1.0, origval=4500.0, newval=4275.0
step 23 (0.0 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=24 choice=7, par=7, pm=0.0, origval=2700.0, newval=2835.0
step 24 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=25 choice=3, par=3, pm=0.0, origval=2375.0, newval=2437.5
step 25 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003052)
step=26 choice=22, par=10, pm=1.0, origval=5500.0, newval=5225.0
step 26 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003359)
step=27 choice=12, par=0, pm=1.0, origval=2.5, newval=2.375
step 27 (0.1 s) ++ (orig:61.58 | best:61.38 | new:61.38 | diff:-0.00002755)
step=28 choice=23, par=11, pm=1.0, origval=8000.0, newval=7600.0
step 28 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=29 choice=10, par=10, pm=0.0, origval=5500.0, newval=5775.0
step 29 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004171)
step=30 choice=3, par=3, pm=0.0, origval=2375.0, newval=2406.25
step 30 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001546)
step=31 choice=1, par=1, pm=0.0, origval=950.0, newval=1000.0
step 31 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004672)
step=32 choice=4, par=4, pm=0.0, origval=4655.0, newval=4900.0
step 32 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.45 | diff:0.06943)
step=33 choice=19, par=7, pm=1.0, origval=2700.0, newval=2565.0
step 33 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=34 choice=5, par=5, pm=0.0, origval=7125.0, newval=7500.0
step 34 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.46 | diff:0.08149)
step=35 choice=19, par=7, pm=1.0, origval=2700.0, newval=2565.0
step 35 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=36 choice=1, par=1, pm=0.0, origval=950.0, newval=975.0
step 36 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.002367)
step=37 choice=2, par=2, pm=0.0, origval=3800.0, newval=3900.0
step 37 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001275)
step=38 choice=7, par=7, pm=0.0, origval=2700.0, newval=2835.0
step 38 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=39 choice=5, par=5, pm=0.0, origval=7125.0, newval=7312.5
step 39 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.42 | diff:0.03816)
step=40 choice=22, par=10, pm=1.0, origval=5500.0, newval=5362.5
step 40 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001209)
step=41 choice=11, par=11, pm=0.0, origval=8000.0, newval=8400.0
step 41 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=42 choice=19, par=7, pm=1.0, origval=2700.0, newval=2632.5
step 42 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=43 choice=10, par=10, pm=0.0, origval=5500.0, newval=5775.0
step 43 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004171)
step=44 choice=6, par=6, pm=0.0, origval=9500.0, newval=10000.0
step 44 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.41 | diff:0.02945)
step=45 choice=20, par=8, pm=1.0, origval=4500.0, newval=4387.5
step 45 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=46 choice=19, par=7, pm=1.0, origval=2700.0, newval=2666.25
step 46 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=47 choice=8, par=8, pm=0.0, origval=4500.0, newval=4725.0
step 47 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=48 choice=9, par=9, pm=0.0, origval=17100.0, newval=18000.0
step 48 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.01325)
step=49 choice=7, par=7, pm=0.0, origval=2700.0, newval=2767.5
step 49 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=50 choice=8, par=8, pm=0.0, origval=4500.0, newval=4612.5
step 50 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=51 choice=1, par=1, pm=0.0, origval=950.0, newval=962.5
step 51 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001191)
step=52 choice=11, par=11, pm=0.0, origval=8000.0, newval=8200.0
step 52 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=53 choice=9, par=9, pm=0.0, origval=17100.0, newval=17550.0
step 53 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.006771)
step=54 choice=4, par=4, pm=0.0, origval=4655.0, newval=4777.5
step 54 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.41 | diff:0.03338)
step=55 choice=22, par=10, pm=1.0, origval=5500.0, newval=5431.25
step 55 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002947)
step=56 choice=2, par=2, pm=0.0, origval=3800.0, newval=3850.0
step 56 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000006460)
step=57 choice=23, par=11, pm=1.0, origval=8000.0, newval=7600.0
step 57 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=58 choice=23, par=11, pm=1.0, origval=8000.0, newval=7800.0
step 58 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=59 choice=4, par=4, pm=0.0, origval=4655.0, newval=4716.25
step 59 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.40 | diff:0.01632)
step=60 choice=10, par=10, pm=0.0, origval=5500.0, newval=5637.5
step 60 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001094)
step=61 choice=7, par=7, pm=0.0, origval=2700.0, newval=2733.75
step 61 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=62 choice=20, par=8, pm=1.0, origval=4500.0, newval=4443.75
step 62 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=63 choice=6, par=6, pm=0.0, origval=9500.0, newval=9750.0
step 63 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.01499)
step=64 choice=9, par=9, pm=0.0, origval=17100.0, newval=17325.0
step 64 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003424)
step=65 choice=11, par=11, pm=0.0, origval=8000.0, newval=8100.0
step 65 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=66 choice=6, par=6, pm=0.0, origval=9500.0, newval=9625.0
step 66 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.007562)
step=67 choice=20, par=8, pm=1.0, origval=4500.0, newval=4471.875
step 67 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=68 choice=23, par=11, pm=1.0, origval=8000.0, newval=7900.0
step 68 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=69 choice=4, par=4, pm=0.0, origval=4655.0, newval=4685.625
step 69 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.008065)
step=70 choice=10, par=10, pm=0.0, origval=5500.0, newval=5568.75
step 70 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002804)
step=71 choice=22, par=10, pm=1.0, origval=5500.0, newval=5465.625
step 71 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00007276)
step=72 choice=0, par=0, pm=0.0, origval=2.375, newval=2.4375
step 72 (0.1 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001114)
step=73 choice=6, par=6, pm=0.0, origval=9500.0, newval=9562.5
step 73 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.003798)
step=74 choice=0, par=0, pm=0.0, origval=2.375, newval=2.40625
step 74 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004857)
step=75 choice=5, par=5, pm=0.0, origval=7125.0, newval=7218.75
step 75 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.40 | diff:0.01837)
step=76 choice=11, par=11, pm=0.0, origval=8000.0, newval=8050.0
step 76 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=77 choice=23, par=11, pm=1.0, origval=8000.0, newval=7950.0
step 77 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=78 choice=8, par=8, pm=0.0, origval=4500.0, newval=4556.25
step 78 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=79 choice=1, par=1, pm=0.0, origval=950.0, newval=956.25
step 79 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0005974)
step=80 choice=5, par=5, pm=0.0, origval=7125.0, newval=7171.875
step 80 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.39 | diff:0.009000)
step=81 choice=22, par=10, pm=1.0, origval=5500.0, newval=5482.8125
step 81 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001808)
step=82 choice=19, par=7, pm=1.0, origval=2700.0, newval=2683.125
step 82 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=83 choice=9, par=9, pm=0.0, origval=17100.0, newval=17212.5
step 83 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001722)
step=84 choice=10, par=10, pm=0.0, origval=5500.0, newval=5534.375
step 84 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00007096)
step=85 choice=4, par=4, pm=0.0, origval=4655.0, newval=4670.3125
step 85 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004008)
step=86 choice=6, par=6, pm=0.0, origval=9500.0, newval=9531.25
step 86 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001903)
step=87 choice=3, par=3, pm=0.0, origval=2375.0, newval=2390.625
step 87 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0007779)
step=88 choice=0, par=0, pm=0.0, origval=2.375, newval=2.390625
step 88 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000002243)
step=89 choice=20, par=8, pm=1.0, origval=4500.0, newval=4485.9375
step 89 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=90 choice=11, par=11, pm=0.0, origval=8000.0, newval=8025.0
step 90 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=91 choice=8, par=8, pm=0.0, origval=4500.0, newval=4528.125
step 91 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=92 choice=23, par=11, pm=1.0, origval=8000.0, newval=7975.0
step 92 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=93 choice=7, par=7, pm=0.0, origval=2700.0, newval=2716.875
step 93 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=94 choice=8, par=8, pm=0.0, origval=4500.0, newval=4514.0625
step 94 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=95 choice=1, par=1, pm=0.0, origval=950.0, newval=953.125
step 95 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002992)
step=96 choice=8, par=8, pm=0.0, origval=4500.0, newval=4507.03125
step 96 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=97 choice=16, par=4, pm=1.0, origval=4655.0, newval=4655.0
step 97 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=98 choice=3, par=3, pm=0.0, origval=2375.0, newval=2382.8125
step 98 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0003902)
step=99 choice=7, par=7, pm=0.0, origval=2700.0, newval=2708.4375
step 99 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=100 choice=2, par=2, pm=0.0, origval=3800.0, newval=3825.0
step 100 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000003251)
step=101 choice=9, par=9, pm=0.0, origval=17100.0, newval=17156.25
step 101 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0008633)
step=102 choice=1, par=1, pm=0.0, origval=950.0, newval=951.5625
step 102 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0001497)
step=103 choice=7, par=7, pm=0.0, origval=2700.0, newval=2704.21875
step 103 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=104 choice=7, par=7, pm=0.0, origval=2700.0, newval=2702.109375
step 104 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=105 choice=5, par=5, pm=0.0, origval=7125.0, newval=7148.4375
step 105 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.004453)
step=106 choice=18, par=6, pm=1.0, origval=9500.0, newval=9500.0
step 106 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=107 choice=19, par=7, pm=1.0, origval=2700.0, newval=2691.5625
step 107 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=108 choice=17, par=5, pm=1.0, origval=7125.0, newval=7125.0
step 108 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=109 choice=22, par=10, pm=1.0, origval=5500.0, newval=5491.40625
step 109 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004505)
step=110 choice=3, par=3, pm=0.0, origval=2375.0, newval=2378.90625
step 110 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0001954)
step=111 choice=10, par=10, pm=0.0, origval=5500.0, newval=5517.1875
step 111 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00001785)
step=112 choice=21, par=9, pm=1.0, origval=17100.0, newval=17100.0
step 112 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=113 choice=7, par=7, pm=0.0, origval=2700.0, newval=2701.0546875
step 113 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=114 choice=23, par=11, pm=1.0, origval=8000.0, newval=7987.5
step 114 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=115 choice=13, par=1, pm=1.0, origval=950.0, newval=950.0
step 115 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=116 choice=4, par=4, pm=0.0, origval=4655.0, newval=4662.65625
step 116 (0.2 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.001998)
step=117 choice=2, par=2, pm=0.0, origval=3800.0, newval=3812.5
step 117 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000001631)
step=118 choice=0, par=0, pm=0.0, origval=2.375, newval=2.3828125
step 118 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000001074)
step=119 choice=23, par=11, pm=1.0, origval=8000.0, newval=7993.75
step 119 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=120 choice=6, par=6, pm=0.0, origval=9500.0, newval=9515.625
step 120 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0009528)
step=121 choice=15, par=3, pm=1.0, origval=2375.0, newval=2375.0
step 121 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=122 choice=5, par=5, pm=0.0, origval=7125.0, newval=7136.71875
step 122 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.002214)
step=123 choice=9, par=9, pm=0.0, origval=17100.0, newval=17128.125
step 123 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0004323)
step=124 choice=19, par=7, pm=1.0, origval=2700.0, newval=2695.78125
step 124 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=125 choice=20, par=8, pm=1.0, origval=4500.0, newval=4492.96875
step 125 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=126 choice=20, par=8, pm=1.0, origval=4500.0, newval=4496.484375
step 126 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=127 choice=0, par=0, pm=0.0, origval=2.375, newval=2.37890625
step 127 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0000005250)
step=128 choice=10, par=10, pm=0.0, origval=5500.0, newval=5508.59375
step 128 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.000004477)
step=129 choice=19, par=7, pm=1.0, origval=2700.0, newval=2697.890625
step 129 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=130 choice=11, par=11, pm=0.0, origval=8000.0, newval=8012.5
step 130 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=131 choice=12, par=0, pm=1.0, origval=2.375, newval=2.375
step 131 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=132 choice=9, par=9, pm=0.0, origval=17100.0, newval=17114.0625
step 132 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0002163)
step=133 choice=11, par=11, pm=0.0, origval=8000.0, newval=8006.25
step 133 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=134 choice=3, par=3, pm=0.0, origval=2375.0, newval=2376.953125
step 134 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.00009779)
step=135 choice=0, par=0, pm=0.0, origval=2.375, newval=2.376953125
step 135 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0.0000002595)
step=136 choice=15, par=3, pm=1.0, origval=2375.0, newval=2375.0
step 136 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
step=137 choice=14, par=2, pm=1.0, origval=3800.0, newval=3800.0
step 137 (0.3 s) -- (orig:61.58 | best:61.38 | new:61.38 | diff:0)
=== Relative improvement too small (0.0000008117 < 0.001000) (137 steps, orig: 61.58 | best: 0 | ratio: 0) ===
You can then run simulations with the modified program set. You can also save the new programset to a progbook if you wish to edit it further in Excel:
[16]:
P.progsets['reconciled'].save('reconciled_progset.xlsx');
Object saved to /home/vsts/work/1/s/docs/examples/reconciled_progset.xlsx.
Scenarios¶
A scenario involves overriding some aspect of the simulation that would otherwise be specified in the databook or progbook. There are three kinds of scenarios
Parameter scenarios, when you want to test the effect of a specific parameter value
Budget scenarios, when you want to examine the outcomes of specific spending values
Coverage scenarios, when you want to examine the effect of specific program coverages irrespective of spending
Examples of these scenarios are shown below:
Parameter scenarios¶
[17]:
scvalues = dict()
scvalues['b_rate'] = dict()
scvalues['b_rate']['0-4'] = dict()
scvalues['b_rate']['0-4']["t"] = [2015, 2020, 2035]
scvalues['b_rate']['0-4']["y"] = [270000, 220000, 220000]
scen = P.make_scenario(which='parameter', name="Reduced births", scenario_values=scvalues)
res_par_scen = scen.run(P, P.parsets["default"]);
# Plot the parameter and compare to scenario input values
d = at.PlotData(res_par_scen,outputs='b_rate',pops='0-4')
at.plot_series(d)
plt.scatter(scvalues['b_rate']['0-4']["t"],scvalues['b_rate']['0-4']["y"],color='r')
Elapsed time for running "default": 0.696s
[17]:
<matplotlib.collections.PathCollection at 0x7fd79f383e10>
Budget scenarios¶
To run a program-related scenario, such as a budget or coverage scenario, it is not necessary to construct a Scenario
object. Instead, you can directly create and use the program instructions that define the scenario:
[18]:
alloc = P.progsets[0].get_alloc(2018)
doubled_budget = {x:v*2 for x,v in alloc.items()}
instructions = at.ProgramInstructions(start_year=2018,alloc=doubled_budget)
res_baseline = P.run_sim(parset='default',progset='default',progset_instructions=at.ProgramInstructions(start_year=2018),result_name='Baseline')
res_budget_scen = P.run_sim(parset='default',progset='default',progset_instructions=instructions,result_name='Doubled');
d = at.PlotData.programs([res_baseline,res_budget_scen]).interpolate(2018)
at.plot_bars(d,stack_outputs='all');
Elapsed time for running "default": 0.955s
Elapsed time for running "default": 1.20s
Alternatively, you can create a full scenario object by storing the instructions
in a CombinedScenario
. The CombinedScenario
optionally allows you to mix parameter and program scenarios.
[19]:
scen = P.make_scenario(which='combined', name="Doubled (scen)", instructions=instructions)
res_combined_scen = scen.run(P, parset='default',progset='default')
d = at.PlotData.programs([res_baseline,res_budget_scen, res_combined_scen]).interpolate(2018)
at.plot_bars(d,stack_outputs='all');
Elapsed time for running "default": 0.970s
Coverage scenarios¶
With coverage scenarios, the program instructions override a program’s coverage. Therefore, the spending values and coverage values may not match up with what is entered in the program book. If running coverage scenarios, take care not to use the spending values for such results - typically this is not a problem, because if you did have a particular spending amount in mind, then it would be better to use a budget scenario.
[20]:
half_coverage = {x:0.5 for x in P.progsets[0].programs.keys()}
instructions = at.ProgramInstructions(start_year=2018,coverage=half_coverage)
scen = at.CombinedScenario(name='Reduced coverage',instructions=instructions)
res_cov_scen = scen.run(P,parset='default',progset='default');
Elapsed time for running "default": 0.930s
Optimization¶
The role of optimization is to produce a set of program spending overwrites that improves the model output in some way. It is thus an operation that maps one set of program instructions to another, where the optimized program instructions contain the optimized allocation. An optimization consists of three parts
adjustments
that specify what parts of the program instructions to change, and how to change themmeasurables
that define optimality (e.g. reducing new infections, maximizing people alive)constraints
that must be satisfied, such as fixed total spending
An Optimization
object contains these three items, as well any additional parameters specific to the optimization algorithm (e.g. the optimization method, the maximum run time).
The optimize
function uses the Optimization
to modify a particular set of program instructions. It therefore takes in
A parset and progset to use
A program instructions instance to optimize
An optimization object, that specifies how to perform the optimization
[21]:
instructions = at.ProgramInstructions(alloc=P.progsets[0],start_year=2020) # Instructions for default spending
adjustments = [at.SpendingAdjustment(x,2020,'rel',0.,2.) for x in instructions.alloc.keys()]
measurables = at.MaximizeCascadeStage(None,2020)
constraints = at.TotalSpendConstraint() # Cap total spending in all years
optimization = at.Optimization(name='default', adjustments=adjustments, measurables=measurables,constraints=constraints,maxtime=10) # Evaluate from 2020 to end of simulation
optimized_instructions = at.optimize(P, optimization, parset=P.parsets["default"], progset=P.progsets['default'], instructions=instructions)
ASD: Launching with random seed None
step=1 choice=21, par=9, pm=1.0, origval=961200.0, newval=865080.0
step 1 (0.9 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=2 choice=0, par=0, pm=0.0, origval=345000.0, newval=379500.0
step 2 (2.0 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=3 choice=3, par=3, pm=0.0, origval=893333.3333333334, newval=982666.6666666667
step 3 (2.9 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=4 choice=4, par=4, pm=0.0, origval=109461100.0, newval=120407210.0
step 4 (3.8 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=5 choice=2, par=2, pm=0.0, origval=25282133.33333333, newval=27810346.66666666
step 5 (4.7 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=6 choice=5, par=5, pm=0.0, origval=8205000.0, newval=9025500.0
step 6 (5.6 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=7 choice=15, par=3, pm=1.0, origval=893333.3333333334, newval=804000.0
step 7 (6.8 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=8 choice=11, par=11, pm=0.0, origval=240000.0, newval=264000.0
step 8 (7.7 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=9 choice=11, par=11, pm=0.0, origval=240000.0, newval=252000.0
step 9 (8.6 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=10 choice=17, par=5, pm=1.0, origval=8205000.0, newval=7384500.0
step 10 (9.4 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
step=11 choice=15, par=3, pm=1.0, origval=893333.3333333334, newval=848666.6666666667
step 11 (10.6 s) -- (orig:-11730 | best:-11730 | new:-11730 | diff:0)
=== Time limit reached (10.61 > 10.00) (11 steps, orig: -11730 | best: 0 | ratio: 0) ===
The function returns a set of optimized instructions, that can then be used to run a simulation
[22]:
res_optimized = P.run_sim(parset='default',progset='default',progset_instructions=optimized_instructions)
Elapsed time for running "default": 0.972s
For more details on the optimization system, see the general documentation on optimization.