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(
        sev_fn=dict(k=[0.5, 0.2, 1.0]),
        dur_episomal=dict(par1=[6, 4, 12])
    ),
    hpv18=dict(
        sev_fn=dict(k=[0.5, 0.2, 1.0]),
        dur_episomal=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 1.2.7 (2023-09-22) — © 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/v1.2.7/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-09-27 12:19:44,820] 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.29 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.60 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.92 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (1.25 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.60 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.98 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (2.38 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (2.80 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (3.23 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (3.70 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (4.21 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (4.75 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (5.31 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (5.95 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (6.56 s)  ••••••••••••••••••—— 92%
  Running 2020.0 (160/164) (7.24 s)  •••••••••••••••••••— 98%
[I 2023-09-27 12:19:52,535] Trial 0 finished with value: 565.2624528804706 and parameters: {'hpv16_sev_fn_k': 0.9643529188213189, 'hpv16_dur_episomal_par1': 9.597857246755657, 'hpv18_sev_fn_k': 0.7176134346533387, 'hpv18_dur_episomal_par1': 7.965573344973663, 'beta': 0.058903086361880255}. Best is trial 0 with value: 565.2624528804706.
Simulation summary:
     797,705,909 total HPV infections
      17,037,571 total cancers
       9,077,606 total cancer deaths
            9.30 mean HPV prevalence (%)
          474.98 mean cancer incidence (per 100k)
           39.48 mean age of infection (years)
           44.53 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.29 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.58 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.88 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (1.20 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.52 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.87 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (2.22 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (2.59 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.99 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (3.40 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (3.82 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (4.28 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (4.76 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (5.25 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (5.76 s)  ••••••••••••••••••—— 92%
  Running 2020.0 (160/164) (6.32 s)  •••••••••••••••••••— 98%
[I 2023-09-27 12:19:59,222] Trial 1 finished with value: 337.29392687361417 and parameters: {'hpv16_sev_fn_k': 0.8449878867518281, 'hpv16_dur_episomal_par1': 8.61407419168688, 'hpv18_sev_fn_k': 0.8532920731078624, 'hpv18_dur_episomal_par1': 9.535638446810541, 'beta': 0.039209038605526374}. Best is trial 1 with value: 337.29392687361417.
Simulation summary:
     454,749,391 total HPV infections
      10,368,353 total cancers
       5,656,445 total cancer deaths
            6.34 mean HPV prevalence (%)
          312.53 mean cancer incidence (per 100k)
           42.20 mean age of infection (years)
           47.47 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.28 s)  •——————————————————— 7%
  Running 1985.0 (20/164) (0.57 s)  ••—————————————————— 13%
  Running 1987.5 (30/164) (0.86 s)  •••————————————————— 19%
  Running 1990.0 (40/164) (1.17 s)  •••••——————————————— 25%
  Running 1992.5 (50/164) (1.48 s)  ••••••—————————————— 31%
  Running 1995.0 (60/164) (1.81 s)  •••••••————————————— 37%
  Running 1997.5 (70/164) (2.14 s)  ••••••••———————————— 43%
  Running 2000.0 (80/164) (2.50 s)  •••••••••——————————— 49%
  Running 2002.5 (90/164) (2.86 s)  •••••••••••————————— 55%
  Running 2005.0 (100/164) (3.25 s)  ••••••••••••———————— 62%
  Running 2007.5 (110/164) (3.63 s)  •••••••••••••——————— 68%
  Running 2010.0 (120/164) (4.04 s)  ••••••••••••••—————— 74%
  Running 2012.5 (130/164) (4.47 s)  •••••••••••••••————— 80%
  Running 2015.0 (140/164) (4.93 s)  •••••••••••••••••——— 86%
  Running 2017.5 (150/164) (5.38 s)  ••••••••••••••••••—— 92%
  Running 2020.0 (160/164) (5.87 s)  •••••••••••••••••••— 98%
[I 2023-09-27 12:20:05,428] Trial 2 finished with value: 181.57301961654107 and parameters: {'hpv16_sev_fn_k': 0.9408099417632243, 'hpv16_dur_episomal_par1': 8.145092484845424, 'hpv18_sev_fn_k': 0.482437201418555, 'hpv18_dur_episomal_par1': 10.131454544636284, 'beta': 0.030657110366586873}. Best is trial 2 with value: 181.57301961654107.
Simulation summary:
     286,392,062 total HPV infections
       6,453,015 total cancers
       3,658,193 total cancer deaths
            4.53 mean HPV prevalence (%)
          205.19 mean cancer incidence (per 100k)
           43.67 mean age of infection (years)
           47.96 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
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
/home/docs/checkouts/readthedocs.org/user_builds/institute-for-disease-modeling-hpvsim/envs/v1.2.7/lib/python3.9/site-packages/seaborn/_oldcore.py:1498: FutureWarning: is_categorical_dtype is deprecated and will be removed in a future version. Use isinstance(dtype, CategoricalDtype) instead
  if pd.api.types.is_categorical_dtype(vector):
../_images/tutorials_tut_calibration_3_10.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.

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]:

[ ]: