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.2 (2024-03-05) — © 2022-2024 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
Could not delete study, skipping...
'Record does not exist.'
Removed existing calibration hpvsim_calibration.db
[I 2024-03-15 03:53:13,027] A new study created in RDB with name: hpvsim_calibration
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.44 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.65 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.88 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.10 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.32 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.55 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (1.79 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.03 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.27 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.52 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (2.78 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (3.04 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (3.31 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (3.58 s)  ••••••••••••••••••—— 92%
[I 2024-03-15 03:53:17,211] Trial 0 finished with value: 8.042036335565294 and parameters: {'hpv16_cin_fn_k': 0.4024756372995709, 'hpv16_dur_cin_par1': 9.99503616068686, 'hpv18_cin_fn_k': 0.2383719497308744, 'hpv18_dur_cin_par1': 6.565175858240141, 'beta': 0.04995933816796226}. Best is trial 0 with value: 8.042036335565294.
  Running 2020.0 (160/164) (3.86 s)  •••••••••••••••••••— 98%
Simulation summary:
     204,098,146 total HPV infections
         412,292 total cancers
         244,933 total cancer deaths
            2.01 mean HPV prevalence (%)
           14.59 mean cancer incidence (per 100k)
           38.06 mean age of infection (years)
           50.23 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.22 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.42 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.61 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.82 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.01 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.21 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.41 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (1.61 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (1.81 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.02 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.23 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (2.43 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (2.63 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (2.85 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (3.06 s)  ••••••••••••••••••—— 92%
[I 2024-03-15 03:53:20,724] Trial 1 finished with value: 6.741988517179703 and parameters: {'hpv16_cin_fn_k': 0.20741138480495902, 'hpv16_dur_cin_par1': 10.022886672851614, 'hpv18_cin_fn_k': 0.5966393374034245, 'hpv18_dur_cin_par1': 4.987922727733171, 'beta': 0.027740684811100765}. Best is trial 1 with value: 6.741988517179703.
  Running 2020.0 (160/164) (3.28 s)  •••••••••••••••••••— 98%
Simulation summary:
      68,737,826 total HPV infections
         147,965 total cancers
         104,869 total cancer deaths
            0.93 mean HPV prevalence (%)
            5.52 mean cancer incidence (per 100k)
           43.38 mean age of infection (years)
           59.38 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.47 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.70 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (0.94 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.18 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.42 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (1.68 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (1.94 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.21 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (2.48 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (2.76 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (3.06 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (3.35 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (3.66 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (3.98 s)  ••••••••••••••••••—— 92%
[I 2024-03-15 03:53:25,313] Trial 2 finished with value: 43.8997330658975 and parameters: {'hpv16_cin_fn_k': 0.6393809840591638, 'hpv16_dur_cin_par1': 11.190142829930004, 'hpv18_cin_fn_k': 0.30606790669684436, 'hpv18_dur_cin_par1': 5.8520892296011215, 'beta': 0.15892915680400588}. Best is trial 1 with value: 6.741988517179703.
  Running 2020.0 (160/164) (4.31 s)  •••••••••••••••••••— 98%
Simulation summary:
     678,970,820 total HPV infections
       1,590,269 total cancers
         900,003 total cancer deaths
            5.08 mean HPV prevalence (%)
           56.68 mean cancer incidence (per 100k)
           34.35 mean age of infection (years)
           43.86 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_11.png

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.

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]: