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.4 (2023-09-19) — © 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.4/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-20 04:02:17,058] 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.30 s) •——————————————————— 7%
Running 1985.0 (20/164) (0.61 s) ••—————————————————— 13%
Running 1987.5 (30/164) (0.93 s) •••————————————————— 19%
Running 1990.0 (40/164) (1.29 s) •••••——————————————— 25%
Running 1992.5 (50/164) (1.66 s) ••••••—————————————— 31%
Running 1995.0 (60/164) (2.06 s) •••••••————————————— 37%
Running 1997.5 (70/164) (2.50 s) ••••••••———————————— 43%
Running 2000.0 (80/164) (2.96 s) •••••••••——————————— 49%
Running 2002.5 (90/164) (3.42 s) •••••••••••————————— 55%
Running 2005.0 (100/164) (3.95 s) ••••••••••••———————— 62%
Running 2007.5 (110/164) (4.49 s) •••••••••••••——————— 68%
Running 2010.0 (120/164) (5.08 s) ••••••••••••••—————— 74%
Running 2012.5 (130/164) (5.67 s) •••••••••••••••————— 80%
Running 2015.0 (140/164) (6.35 s) •••••••••••••••••——— 86%
Running 2017.5 (150/164) (7.01 s) ••••••••••••••••••—— 92%
Running 2020.0 (160/164) (7.75 s) •••••••••••••••••••— 98%
[I 2023-09-20 04:02:25,309] Trial 0 finished with value: 606.1887237253219 and parameters: {'hpv16_sev_fn_k': 0.8175525286537182, 'hpv16_dur_episomal_par1': 7.358425583286783, 'hpv18_sev_fn_k': 0.7650941749588984, 'hpv18_dur_episomal_par1': 8.207859873015764, 'beta': 0.12238060929426899}. Best is trial 0 with value: 606.1887237253219.
Simulation summary:
46,027,299 infections
0 dysplasias
0 pre-cins
16,749,539 cin1s
6,653,415 cin2s
5,962,431 cin3s
73,726,274 cins
1,010,618 cancers
0 cancer detections
701,758 cancer deaths
0 detected cancer deaths
38,298,619 reinfections
0 reactivations
861,690,368 number susceptible
78,435,312 number infectious
6,970,894 number with inactive infection
231,116,224 number with no cellular changes
94,215,888 number with episomal infection
718 number with transformation
6,970,894 number with cancer
85,406,216 number infected
101,186,792 number with abnormal cells
0 number with latent infection
8,685,425 number with precin
11,682,803 number with cin1
14,362,699 number with cin2
45,120,108 number with cin3
62,857,284 number with detectable dysplasia
0 number with detected cancer
0 number screened
0 number treated for precancerous lesions
0 number treated for cancer
0 number vaccinated
0 number given therapeutic vaccine
1.78 hpv incidence (/100)
0 cin1 incidence (/100,000)
0 cin2 incidence (/100,000)
0 cin3 incidence (/100,000)
0 dysplasia incidence (/100,000)
832 cancer incidence (/100,000)
7,520,377 births
2,608,070 other deaths
-344,774 migration
1,000 age-adjusted cervical cancer incidence (/100,000)
0 age-adjusted cervical cancer mortality
0 newly vaccinated
0 cumulative number vaccinated
0 new doses
0 cumulative doses
0 new therapeutic vaccine doses
0 newly received therapeutic vaccine
0 cumulative therapeutic vaccine doses
0 total received therapeutic vaccine
0 new screens
0 newly screened
0 new cin treatments
0 newly treated for cins
0 new cancer treatments
0 newly treated for cancer
0 cumulative screens
0 cumulative number screened
0 cumulative cin treatments
0 cumulative number treated for cins
0 cumulative cancer treatments
0 cumulative number treated for cancer
0 detected cancer incidence (/100,000)
546 cancer mortality
231,116,224 number alive
0 crude death rate
0 crude birth rate
11.31 hpv prevalence (/100)
0 pre-cin prevalence (/100,000)
0 cin1 prevalence (/100,000)
0 cin2 prevalence (/100,000)
0 cin3 prevalence (/100,000)
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.62 s) ••—————————————————— 13%
Running 1987.5 (30/164) (0.94 s) •••————————————————— 19%
Running 1990.0 (40/164) (1.30 s) •••••——————————————— 25%
Running 1992.5 (50/164) (1.68 s) ••••••—————————————— 31%
Running 1995.0 (60/164) (2.07 s) •••••••————————————— 37%
Running 1997.5 (70/164) (2.50 s) ••••••••———————————— 43%
Running 2000.0 (80/164) (2.95 s) •••••••••——————————— 49%
Running 2002.5 (90/164) (3.41 s) •••••••••••————————— 55%
Running 2005.0 (100/164) (3.93 s) ••••••••••••———————— 62%
Running 2007.5 (110/164) (4.43 s) •••••••••••••——————— 68%
Running 2010.0 (120/164) (4.99 s) ••••••••••••••—————— 74%
Running 2012.5 (130/164) (5.57 s) •••••••••••••••————— 80%
Running 2015.0 (140/164) (6.19 s) •••••••••••••••••——— 86%
Running 2017.5 (150/164) (6.85 s) ••••••••••••••••••—— 92%
Running 2020.0 (160/164) (7.54 s) •••••••••••••••••••— 98%
[I 2023-09-20 04:02:33,282] Trial 1 finished with value: 517.4016407118734 and parameters: {'hpv16_sev_fn_k': 0.9481367071693338, 'hpv16_dur_episomal_par1': 6.123413311645033, 'hpv18_sev_fn_k': 0.7734618149622636, 'hpv18_dur_episomal_par1': 6.743106530710248, 'beta': 0.1990380438602805}. Best is trial 1 with value: 517.4016407118734.
Simulation summary:
48,261,145 infections
0 dysplasias
0 pre-cins
17,324,163 cin1s
7,132,507 cin2s
5,267,855 cin3s
74,012,148 cins
864,089 cancers
0 cancer detections
649,324 cancer deaths
0 detected cancer deaths
39,993,757 reinfections
0 reactivations
805,298,304 number susceptible
71,711,512 number infectious
5,828,113 number with inactive infection
226,589,632 number with no cellular changes
89,072,304 number with episomal infection
0 number with transformation
5,828,113 number with cancer
77,539,624 number infected
94,900,416 number with abnormal cells
0 number with latent infection
9,687,424 number with precin
11,227,414 number with cin1
12,632,367 number with cin2
38,812,908 number with cin3
54,742,888 number with detectable dysplasia
0 number with detected cancer
0 number screened
0 number treated for precancerous lesions
0 number treated for cancer
0 number vaccinated
0 number given therapeutic vaccine
2.00 hpv incidence (/100)
0 cin1 incidence (/100,000)
0 cin2 incidence (/100,000)
0 cin3 incidence (/100,000)
0 dysplasia incidence (/100,000)
730 cancer incidence (/100,000)
7,506,012 births
2,716,530 other deaths
-179,570 migration
863 age-adjusted cervical cancer incidence (/100,000)
0 age-adjusted cervical cancer mortality
0 newly vaccinated
0 cumulative number vaccinated
0 new doses
0 cumulative doses
0 new therapeutic vaccine doses
0 newly received therapeutic vaccine
0 cumulative therapeutic vaccine doses
0 total received therapeutic vaccine
0 new screens
0 newly screened
0 new cin treatments
0 newly treated for cins
0 new cancer treatments
0 newly treated for cancer
0 cumulative screens
0 cumulative number screened
0 cumulative cin treatments
0 cumulative number treated for cins
0 cumulative cancer treatments
0 cumulative number treated for cancer
0 detected cancer incidence (/100,000)
523 cancer mortality
226,589,632 number alive
0 crude death rate
0 crude birth rate
10.55 hpv prevalence (/100)
0 pre-cin prevalence (/100,000)
0 cin1 prevalence (/100,000)
0 cin2 prevalence (/100,000)
0 cin3 prevalence (/100,000)
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.27 s) •——————————————————— 7%
Running 1985.0 (20/164) (0.54 s) ••—————————————————— 13%
Running 1987.5 (30/164) (0.81 s) •••————————————————— 19%
Running 1990.0 (40/164) (1.08 s) •••••——————————————— 25%
Running 1992.5 (50/164) (1.34 s) ••••••—————————————— 31%
Running 1995.0 (60/164) (1.61 s) •••••••————————————— 37%
Running 1997.5 (70/164) (1.90 s) ••••••••———————————— 43%
Running 2000.0 (80/164) (2.19 s) •••••••••——————————— 49%
Running 2002.5 (90/164) (2.47 s) •••••••••••————————— 55%
Running 2005.0 (100/164) (2.79 s) ••••••••••••———————— 62%
Running 2007.5 (110/164) (3.10 s) •••••••••••••——————— 68%
Running 2010.0 (120/164) (3.41 s) ••••••••••••••—————— 74%
Running 2012.5 (130/164) (3.74 s) •••••••••••••••————— 80%
Running 2015.0 (140/164) (4.06 s) •••••••••••••••••——— 86%
Running 2017.5 (150/164) (4.40 s) ••••••••••••••••••—— 92%
[I 2023-09-20 04:02:38,321] Trial 2 finished with value: 15.300730529293745 and parameters: {'hpv16_sev_fn_k': 0.949865493907329, 'hpv16_dur_episomal_par1': 8.583790153088373, 'hpv18_sev_fn_k': 0.9398701718589766, 'hpv18_dur_episomal_par1': 6.920445501319353, 'beta': 0.014907468897859308}. Best is trial 2 with value: 15.300730529293745.
Running 2020.0 (160/164) (4.75 s) •••••••••••••••••••— 98%
Simulation summary:
689,547 infections
0 dysplasias
0 pre-cins
248,524 cin1s
173,105 cin2s
183,161 cin3s
1,728,897 cins
31,604 cancers
0 cancer detections
32,323 cancer deaths
0 detected cancer deaths
509,978 reinfections
0 reactivations
624,866,688 number susceptible
2,162,737 number infectious
243,496 number with inactive infection
206,497,952 number with no cellular changes
6,199,462 number with episomal infection
0 number with transformation
243,496 number with cancer
2,406,233 number infected
6,442,960 number with abnormal cells
0 number with latent infection
211,174 number with precin
255,707 number with cin1
489,866 number with cin2
2,469,442 number with cin3
3,189,157 number with detectable dysplasia
0 number with detected cancer
0 number screened
0 number treated for precancerous lesions
0 number treated for cancer
0 number vaccinated
0 number given therapeutic vaccine
0.04 hpv incidence (/100)
0 cin1 incidence (/100,000)
0 cin2 incidence (/100,000)
0 cin3 incidence (/100,000)
0 dysplasia incidence (/100,000)
31 cancer incidence (/100,000)
7,520,377 births
2,154,118 other deaths
-481,247 migration
48 age-adjusted cervical cancer incidence (/100,000)
0 age-adjusted cervical cancer mortality
0 newly vaccinated
0 cumulative number vaccinated
0 new doses
0 cumulative doses
0 new therapeutic vaccine doses
0 newly received therapeutic vaccine
0 cumulative therapeutic vaccine doses
0 total received therapeutic vaccine
0 new screens
0 newly screened
0 new cin treatments
0 newly treated for cins
0 new cancer treatments
0 newly treated for cancer
0 cumulative screens
0 cumulative number screened
0 cumulative cin treatments
0 cumulative number treated for cins
0 cumulative cancer treatments
0 cumulative number treated for cancer
0 detected cancer incidence (/100,000)
31 cancer mortality
206,497,952 number alive
0 crude death rate
0 crude birth rate
0.35 hpv prevalence (/100)
0 pre-cin prevalence (/100,000)
0 cin1 prevalence (/100,000)
0 cin2 prevalence (/100,000)
0 cin3 prevalence (/100,000)
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.4/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.4/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.4/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.4/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.4/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.4/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):
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.
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]:
[ ]: