T7 - Calibration

Tutorial 2 demonstrated how to run the model and plot the outputs. But it’s entirely possible that the model outputs won’t look like the data for the country that you wish to model. The default parameter values included in HPVsim are intended as points of departure to be iteratively refined via calibration. The process of model calibration involves finding the model parameters that are the most likely explanation for the observed data. This tutorial gives an introduction to the Fit object and some recipes for optimization approaches.

Click here to open an interactive version of this notebook.

Data types supported by HPVsim

Data on HPV and cervical disease comes in many different formats. When using HPVsim, the goal is typically to produce population-level estimates of epidemic outputs like: - age-specific incidence of cancer or high-grade lesions in one or more years; - number of cases of cancer or high-grade lesions reported in one or more years; - HPV prevalence over time; - lifetime incidence of HPV; - the distribution of genotypes in detected cases of cancer/high-grade lesions; - sexual behavior metrics like the average age at first marriage, duration of relationships, or number of lifetime partners.

After running HPVsim, estimates all of these variables are included within the results dictionary. To plot them alongside data, the easiest method is to use the Calibration object.

The Calibration object

Calibration objects contain the following ingredients: - an hpv.Sim() instance with details of the model configuration; - two lists of parameters to vary, one for parameters that vary by genotype and one for those that don’t; - dataframes that hold the calibration targets, which are typically added as csv files; - a list of any additional results to plot; - settings that are passed to the Optuna package[LINK], an open source hyperparameter optimization framework that automates calibration for HPVsim.

We have included Optuna as a built-in calibration option as we have found that it works reasonably well, but it is also possible to use other methods; we will discuss this a little further down.

The example below illustrates the general idea of calibration, and can be adapted for different use cases:

[1]:
# Import HPVsim
import hpvsim as hpv

# Configure a simulation with some parameters
pars = dict(n_agents=10e3, start=1980, end=2020, dt=0.25, location='nigeria')
sim = hpv.Sim(pars)

# Specify some parameters to adjust during calibration.
# The parameters in the calib_pars dictionary don't vary by genotype,
# whereas those in the genotype_pars dictionary do. Both kinds are
# given in the order [best, lower_bound, upper_bound].
calib_pars = dict(
        beta=[0.05, 0.010, 0.20],
    )

genotype_pars = dict(
    hpv16=dict(
        cin_fn=dict(k=[0.5, 0.2, 1.0]),
        dur_cin=dict(par1=[6, 4, 12])
    ),
    hpv18=dict(
        cin_fn=dict(k=[0.5, 0.2, 1.0]),
        dur_cin=dict(par1=[6, 4, 12])
    )
)

# List the datafiles that contain data that we wish to compare the model to:
datafiles=['nigeria_cancer_cases.csv',
           'nigeria_cancer_types.csv']

# List extra results that we don't have data on, but wish to include in the
# calibration object so we can plot them.
results_to_plot = ['cancer_incidence', 'asr_cancer_incidence']

# Create the calibration object, run it, and plot the results
calib = hpv.Calibration(
    sim,
    calib_pars=calib_pars,
    genotype_pars=genotype_pars,
    extra_sim_result_keys=results_to_plot,
    datafiles=datafiles,
    total_trials=3, n_workers=1
)
calib.calibrate(die=True)
calib.plot(res_to_plot=4);
HPVsim 2.0.0 (2023-11-29) — © 2023 by IDM
Loading location-specific demographic data for "nigeria"
Initializing sim with 10000 agents
Loading location-specific data for "nigeria"
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/latest/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
[I 2023-11-29 14:27:02,591] A new study created in RDB with name: hpvsim_calibration
Could not delete study, skipping...
'Record does not exist.'
Removed existing calibration hpvsim_calibration.db
Initializing sim (resetting people) with 10000 agents
Loading location-specific data for "nigeria"
  Running 1980.0 ( 0/164) (0.00 s)  ———————————————————— 1%
  Running 1982.5 (10/164) (0.24 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.47 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.71 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.94 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.17 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.41 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.66 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (1.93 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.19 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.46 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.74 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (3.03 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (3.34 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (3.65 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (3.96 s)  ••••••••••••••••••—— 92%
[I 2023-11-29 14:27:07,216] Trial 0 finished with value: 19.576687012228533 and parameters: {'hpv16_cin_fn_k': 0.9627873472768935, 'hpv16_dur_cin_par1': 4.340579221244315, 'hpv18_cin_fn_k': 0.24783071447590005, 'hpv18_dur_cin_par1': 8.164671454998311, 'beta': 0.16617801301844115}. Best is trial 0 with value: 19.576687012228533.
  Running 2020.0 (160/164) (4.29 s)  •••••••••••••••••••— 98%
Simulation summary:
     662,108,513 total HPV infections
       1,050,842 total cancers
         599,763 total cancer deaths
            4.92 mean HPV prevalence (%)
           37.46 mean cancer incidence (per 100k)
           33.95 mean age of infection (years)
           41.82 mean age of cancer (years)

Initializing sim (resetting people) with 10000 agents
Loading location-specific data for "nigeria"
  Running 1980.0 ( 0/164) (0.00 s)  ———————————————————— 1%
  Running 1982.5 (10/164) (0.23 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.46 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.68 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.90 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.12 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.35 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.57 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (1.82 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.06 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.30 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.55 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (2.81 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (3.08 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (3.35 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (3.63 s)  ••••••••••••••••••—— 92%
[I 2023-11-29 14:27:11,386] Trial 1 finished with value: 8.025478139905836 and parameters: {'hpv16_cin_fn_k': 0.6319631092301246, 'hpv16_dur_cin_par1': 5.335347461248497, 'hpv18_cin_fn_k': 0.7027941105501854, 'hpv18_dur_cin_par1': 6.892958598280027, 'beta': 0.04236832253527888}. Best is trial 1 with value: 8.025478139905836.
  Running 2020.0 (160/164) (3.91 s)  •••••••••••••••••••— 98%
Simulation summary:
     174,845,530 total HPV infections
         487,711 total cancers
         334,000 total cancer deaths
            1.87 mean HPV prevalence (%)
           18.48 mean cancer incidence (per 100k)
           37.58 mean age of infection (years)
           52.90 mean age of cancer (years)

Initializing sim (resetting people) with 10000 agents
Loading location-specific data for "nigeria"
  Running 1980.0 ( 0/164) (0.00 s)  ———————————————————— 1%
  Running 1982.5 (10/164) (0.24 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.48 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.72 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.97 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.22 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.48 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.75 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (2.03 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.31 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.59 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.89 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (3.20 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (3.51 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (3.83 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (4.15 s)  ••••••••••••••••••—— 92%
[I 2023-11-29 14:27:16,156] Trial 2 finished with value: 46.661023720471924 and parameters: {'hpv16_cin_fn_k': 0.698611315624649, 'hpv16_dur_cin_par1': 8.831780070386813, 'hpv18_cin_fn_k': 0.9964638765920693, 'hpv18_dur_cin_par1': 8.916457056327964, 'beta': 0.14038855640723372}. Best is trial 1 with value: 8.025478139905836.
  Running 2020.0 (160/164) (4.49 s)  •••••••••••••••••••— 98%
Simulation summary:
     644,871,990 total HPV infections
       2,172,793 total cancers
       1,196,652 total cancer deaths
            4.97 mean HPV prevalence (%)
           80.19 mean cancer incidence (per 100k)
           35.34 mean age of infection (years)
           43.12 mean age of cancer (years)

Loading saved results...
    Removed temporary file tmp_calibration_00000.obj
  Loaded trial 0
    Removed temporary file tmp_calibration_00001.obj
  Loaded trial 1
    Removed temporary file tmp_calibration_00002.obj
  Loaded trial 2
Making results structure...
Processed 3 trials; 0 failed
Deleted study hpvsim_calibration in sqlite:///hpvsim_calibration.db
Removed existing calibration hpvsim_calibration.db
../_images/tutorials_tut_calibration_3_9.svg

This isn’t a great fit yet! In general, it will probably be necessary to run many more trials that the 3 we ran here. Moreover, careful consideration should be given to the parameters that you want to adjust during calibration. In HPVsim we have taken the approach that any parameter can be adjusted. As we learn more about which parameters make most sense to calibrate, we will add details here. We would also enourage users to share their experiences with calibration and parameter searches.

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]: