# 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
%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

1. Load the framework into a ProjectFramework Python instance

2. Use the framework to make a new ProjectData instance

3. Save 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')

Parameter "lu_prog" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lx_prog" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "leu_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lex_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "llu_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "llx_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "l_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "v_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lr_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "ar_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "ar_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pd_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pm_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "px_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nd_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nm_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nx_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pd_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pm_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "px_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nd_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nm_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nx_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"

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

• 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.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')

Parameter "lu_prog" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lx_prog" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "leu_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lex_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "llu_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "llx_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "l_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "v_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "lr_inf" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "ar_act" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "ar_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pd_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pm_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "px_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nd_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nm_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nx_rec" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pd_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "pm_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "px_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nd_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nm_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"
Parameter "nx_term" is in rate units and a maximum value of "1" has been entered. Rates in the framework should generally not be limited to "1"

Elapsed time for running "default": 0.972s


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.806s

[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

1. Make a new ParameterSet (e.g., by copying an existing one)

2. Modify the calibration scale factors in that ParameterSet

3. 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": 0.762s


### 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);

Elapsed time for running "default": 0.797s
Elapsed time for running "default": 0.514s
step 1 (0.5 s) ++ (orig:10.136 | best:10.136 | new:10.126 | diff:-0.0098367)
Elapsed time for running "default": 0.547s
step 2 (1.1 s) -- (orig:10.136 | best:10.126 | new:10.127 | diff:0.00079327)
Elapsed time for running "default": 0.558s
step 3 (1.6 s) ++ (orig:10.136 | best:10.126 | new:10.056 | diff:-0.070741)
Elapsed time for running "default": 0.624s
step 4 (2.3 s) -- (orig:10.136 | best:10.056 | new:10.062 | diff:0.0061375)
Elapsed time for running "default": 0.490s
step 5 (2.8 s) -- (orig:10.136 | best:10.056 | new:10.094 | diff:0.038268)
Elapsed time for running "default": 0.487s
step 6 (3.3 s) -- (orig:10.136 | best:10.056 | new:10.056 | diff:0)
Elapsed time for running "default": 0.608s
step 7 (3.9 s) -- (orig:10.136 | best:10.056 | new:12.454 | diff:2.3984)
Elapsed time for running "default": 0.580s
step 8 (4.5 s) -- (orig:10.136 | best:10.056 | new:10.056 | diff:0)
Elapsed time for running "default": 0.577s
step 9 (5.0 s) ++ (orig:10.136 | best:10.056 | new:10.056 | diff:-0.000027576)
Elapsed time for running "default": 0.530s
step 10 (5.6 s) -- (orig:10.136 | best:10.056 | new:10.153 | diff:0.097926)
Elapsed time for running "default": 0.705s
step 11 (6.3 s) -- (orig:10.136 | best:10.056 | new:10.056 | diff:0)
Elapsed time for running "default": 0.533s
step 12 (6.8 s) -- (orig:10.136 | best:10.056 | new:10.056 | diff:0)
Elapsed time for running "default": 0.577s
step 13 (7.4 s) -- (orig:10.136 | best:10.056 | new:10.644 | diff:0.58797)
Elapsed time for running "default": 0.782s
step 14 (8.2 s) ++ (orig:10.136 | best:10.056 | new:10.051 | diff:-0.0042219)
Elapsed time for running "default": 0.530s
step 15 (8.7 s) ++ (orig:10.136 | best:10.051 | new:10.045 | diff:-0.0066256)
Elapsed time for running "default": 0.529s
step 16 (9.3 s) ++ (orig:10.136 | best:10.045 | new:9.9959 | diff:-0.048780)
Elapsed time for running "default": 0.526s
step 17 (9.8 s) -- (orig:10.136 | best:9.9959 | new:10.049 | diff:0.052907)
Elapsed time for running "default": 0.748s
step 18 (10.5 s) -- (orig:10.136 | best:9.9959 | new:9.9959 | diff:0)
===  Time limit reached (10.548 > 10.000) (18 steps, orig: 10.136 | 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


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 0x7fd100203c90>
————————————————————————————————————————————————————————————
Methods:
_write_spending()   get_outcomes()      to_workbook()
_write_targeting()  get_prop_covera...  validate()
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
Baselin [...]
created: datetime.datetime(2021, 7, 21, 4, 52, 16, 362610,
tzinfo=tzutc())
currency: '\$'
gitinfo: {'branch': 'Git branch N/A', 'hash': 'Git hash N/A',
'date': 'Git dat [...]
modified: datetime.datetime(2021, 7, 21, 4, 52, 17, 772361,
tzinfo=tzutc())
name: 'default'
pars: #0: "v_num":       {'label': 'Number of vaccinations
pops: #0: "0-4":       {'label': 'Children 0-4', 'type':
'default'}
#1: "5- [...]
programs: #0: "BCG":
<atomica.programs.Program at 0x7fd0f9616390>
————————————— [...]
tvec: array([2015., 2016., 2017.])
version: '1.25.5'
————————————————————————————————————————————————————————————
Program set name: default
Programs: ['BCG', 'PCF', 'ACF', 'ACF-p', 'HospDS', 'HospMDR', 'HospXDR', 'AmbDS', 'AmbMDR', 'XDRnew', 'PrisDS', 'PrisDR']
Date created: 2021-Jul-21 04:52:16 UTC
Date modified: 2021-Jul-21 04:52:17 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": 1.05s


## 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]

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.865s
Reconciling in 2018.00, evaluating from 2018.00 up to 2018.25
step 1 (0.0 s) -- (orig:61.584 | best:61.584 | new:61.612 | diff:0.027792)
step 2 (0.0 s) -- (orig:61.584 | best:61.584 | new:61.589 | diff:0.0053849)
step 3 (0.0 s) ++ (orig:61.584 | best:61.584 | new:61.570 | diff:-0.013637)
step 4 (0.0 s) -- (orig:61.584 | best:61.570 | new:61.669 | diff:0.098775)
step 5 (0.0 s) -- (orig:61.584 | best:61.570 | new:61.570 | diff:0)
step 6 (0.0 s) ++ (orig:61.584 | best:61.570 | new:61.489 | diff:-0.081486)
step 7 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.489 | diff:0.000045531)
step 8 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.493 | diff:0.0044314)
step 9 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.494 | diff:0.0053849)
step 10 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.489 | diff:0)
step 11 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.489 | diff:0)
step 12 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.489 | diff:0)
step 13 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.491 | diff:0.0027581)
step 14 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.515 | diff:0.026123)
step 15 (0.0 s) -- (orig:61.584 | best:61.489 | new:61.489 | diff:0.000023633)
step 16 (0.0 s) ++ (orig:61.584 | best:61.489 | new:61.484 | diff:-0.0046735)
step 17 (0.0 s) -- (orig:61.584 | best:61.484 | new:61.484 | diff:0)
step 18 (0.0 s) -- (orig:61.584 | best:61.484 | new:61.488 | diff:0.0041710)
step 19 (0.0 s) -- (orig:61.584 | best:61.484 | new:61.511 | diff:0.027437)
step 20 (0.0 s) ++ (orig:61.584 | best:61.484 | new:61.484 | diff:-0.000027551)
step 21 (0.0 s) -- (orig:61.584 | best:61.484 | new:61.484 | diff:0)
step 22 (0.0 s) ++ (orig:61.584 | best:61.484 | new:61.478 | diff:-0.0059511)
step 23 (0.0 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 24 (0.0 s) -- (orig:61.584 | best:61.478 | new:61.482 | diff:0.0041710)
step 25 (0.0 s) -- (orig:61.584 | best:61.478 | new:61.481 | diff:0.0033592)
step 26 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.560 | diff:0.081486)
step 27 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.483 | diff:0.0046735)
step 28 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.556 | diff:0.078146)
step 29 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.479 | diff:0.0010942)
step 30 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 31 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 32 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 33 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 34 (0.1 s) -- (orig:61.584 | best:61.478 | new:61.478 | diff:0)
step 35 (0.1 s) ++ (orig:61.584 | best:61.478 | new:61.478 | diff:-0.000024873)
step 36 (0.1 s) ++ (orig:61.584 | best:61.478 | new:61.449 | diff:-0.029454)
step 37 (0.1 s) ++ (orig:61.584 | best:61.449 | new:61.379 | diff:-0.069429)
step 38 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.417 | diff:0.038156)
step 39 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 40 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000027551)
step 41 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 42 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000024873)
step 43 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000012755)
step 44 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000011142)
step 45 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.382 | diff:0.0033592)
step 46 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 47 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.382 | diff:0.0023666)
step 48 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 49 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.449 | diff:0.069429)
step 50 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.381 | diff:0.0015457)
step 51 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 52 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000064599)
step 53 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.0012094)
step 54 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 55 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.0011910)
step 56 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.413 | diff:0.033382)
step 57 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00028036)
step 58 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 59 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.392 | diff:0.013245)
step 60 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000032510)
step 61 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.395 | diff:0.016323)
step 62 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.387 | diff:0.0080649)
step 63 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 64 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00029473)
step 65 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000048572)
step 66 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 67 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.386 | diff:0.0067711)
step 68 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.398 | diff:0.018370)
step 69 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.388 | diff:0.0090000)
step 70 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000022428)
step 71 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 72 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000010740)
step 73 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.383 | diff:0.0034239)
step 74 (0.1 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000070963)
step 75 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.381 | diff:0.0017217)
step 76 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 77 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.394 | diff:0.014990)
step 78 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 79 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 80 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.387 | diff:0.0075625)
step 81 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00059739)
step 82 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000017851)
step 83 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 84 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 85 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 86 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00077788)
step 87 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00086330)
step 88 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00029917)
step 89 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.383 | diff:0.0037982)
step 90 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00043227)
step 91 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00039022)
step 92 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 93 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000052504)
step 94 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 95 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 96 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 97 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000016308)
step 98 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00014971)
step 99 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 100 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00019543)
step 101 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.381 | diff:0.0019034)
step 102 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000081674)
step 103 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 104 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 105 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.383 | diff:0.0040077)
step 106 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000072759)
step 107 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000018076)
step 108 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 109 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 110 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 111 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 112 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 113 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.381 | diff:0.0019976)
step 114 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00099720)
step 115 (0.2 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 116 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00049820)
step 117 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 118 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000045049)
step 119 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000044768)
step 120 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000011209)
step 121 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.384 | diff:0.0044527)
step 122 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.381 | diff:0.0022144)
step 123 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 124 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 125 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.0000011245)
step 126 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00095277)
step 127 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 128 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000074883)
step 129 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 130 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 131 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00047665)
step 132 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00024900)
step 133 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00021629)
step 134 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 135 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 136 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000040871)
step 137 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 138 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000025951)
step 139 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00012448)
step 140 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.0011042)
step 141 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 142 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000012900)
step 143 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 144 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00010818)
step 145 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 146 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 147 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 148 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 149 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 150 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 151 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00023839)
step 152 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00011921)
step 153 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.380 | diff:0.00055133)
step 154 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0)
step 155 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000028046)
step 156 (0.3 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.00000020444)
step 157 (0.4 s) -- (orig:61.584 | best:61.379 | new:61.379 | diff:0.000097794)
===  Absolute improvement too small (0 < 0.0000010000) (157 steps, orig: 61.584 | 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 0x7fd10087d590>


### 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.986s
Elapsed time for running "default": 1.17s


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.989s


### 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.971s


## 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 them

• measurables 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
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)

     step 1 (0.9 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 2 (1.9 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 3 (2.8 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 4 (3.9 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 5 (4.8 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 6 (5.9 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 7 (6.8 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 8 (7.9 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 9 (8.8 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 10 (9.7 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
step 11 (10.6 s) -- (orig:-11729 | best:-11729 | new:-11729 | diff:0)
===  Time limit reached (10.640 > 10.000) (11 steps, orig: -11729 | 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": 1.30s


For more details on the optimization system, see the general documentation on optimization.

[ ]: