Пример #1
0
def run_calibration_chain(max_seconds: int, run_id: int, num_chains: int):
    targets = load_targets("sir_example", Region.VICTORIA)
    params = load_params("sir_example", Region.VICTORIA)
    prevalence_infectious = targets["prevalence_infectious"]
    target_outputs = [
        {
            "output_key": prevalence_infectious["output_key"],
            "years": prevalence_infectious["times"],
            "values": prevalence_infectious["values"],
            "loglikelihood_distri": "normal",
            "time_weights": list(range(1, len(prevalence_infectious["times"]) + 1)),
        },
    ]
    calib = Calibration(
        "sir_example",
        build_model,
        params,
        PRIORS,
        target_outputs,
        run_id,
        num_chains,
        region_name=Region.AUSTRALIA,
    )
    calib.run_fitting_algorithm(
        run_mode="autumn_mcmc",
        n_chains=1,
        available_time=max_seconds,
    )
Пример #2
0
def run_calibration_chain(max_seconds: int, run_id: int):
    """
    Run a calibration chain for the Marshall Islands TB model

    num_iters: Maximum number of iterations to run.
    available_time: Maximum time, in seconds, to run the calibration.
    """
    print(
        f"Preparing to run Marshall Islands TB model calibration for run {run_id}"
    )
    calib = Calibration(
        "marshall_islands",
        build_model,
        params,
        PAR_PRIORS,
        TARGET_OUTPUTS,
        MULTIPLIERS,
        run_id,
        total_nb_chains=1,
    )
    print("Starting calibration.")
    calib.run_fitting_algorithm(
        run_mode="autumn_mcmc",
        n_iterations=N_ITERS,
        n_burned=N_BURNED,
        n_chains=N_CHAINS,
        available_time=max_seconds,
    )
    print(f"Finished calibration for run {run_id}.")
Пример #3
0
def test_calibrate_autumn_mcmc(temp_data_dir):
    # Import autumn stuff inside function so we can mock out the database.
    priors = [{
        "param_name": "ice_cream_sales",
        "distribution": "uniform",
        "distri_params": [1, 5],
    }]
    target_outputs = [{
        "output_key": "shark_attacks",
        "years": [2000, 2001, 2002, 2003, 2004],
        "values": [3, 6, 9, 12, 15],
        "loglikelihood_distri": "poisson",
    }]
    multipliers = {}
    params = {
        "default": {
            "start_time": 2000
        },
        "scenario_start_time": 2000,
        "scenarios": {},
    }
    calib = Calibration(
        "sharks",
        _build_mock_model,
        params,
        priors,
        target_outputs,
        multipliers,
        1,
        1,
    )
    calib.run_fitting_algorithm(
        run_mode=CalibrationMode.AUTUMN_MCMC,
        n_iterations=50,
        n_burned=10,
        n_chains=1,
        available_time=1e6,
    )
    app_dir = os.path.join(temp_data_dir, "outputs", "calibrate", "sharks",
                           "main")
    run_dir = os.path.join(app_dir, os.listdir(app_dir)[0])
    db_fname = [
        fname for fname in os.listdir(run_dir) if fname.endswith(".db")
    ][0]
    out_db_path = os.path.join(run_dir, db_fname)
    assert os.path.exists(out_db_path)

    out_db = Database(out_db_path)
    assert set(out_db.engine.table_names()) == {
        "outputs",
        "derived_outputs",
        "mcmc_run",
    }
    mcmc_runs = out_db.query("mcmc_run")
    max_idx = mcmc_runs.loglikelihood.idxmax()
    best_run = mcmc_runs.iloc[max_idx]
    ice_cream_sales_mle = best_run.ice_cream_sales
    # This value is non-deterministic due to fixed seed.
    assert 2.9 < ice_cream_sales_mle < 3.1
Пример #4
0
def test_calibrate_autumn_mcmc(mock_load_targets, temp_data_dir):
    # Import autumn stuff inside function so we can mock out the database.
    priors = [{
        "param_name": "ice_cream_sales",
        "distribution": "uniform",
        "distri_params": [1, 5],
    }]
    mock_load_targets.return_value = {
        "shark_attacks": {
            "output_key": "shark_attacks"
        }
    }
    target_outputs = [{
        "output_key": "shark_attacks",
        "years": [2000, 2001, 2002, 2003, 2004],
        "values": [3, 6, 9, 12, 15],
        "loglikelihood_distri": "poisson",
    }]

    params = {
        "default": {
            "time": {
                "start": 2000
            }
        },
        "scenarios": {},
    }
    calib = Calibration("sharks",
                        _build_mock_model,
                        params,
                        priors,
                        target_outputs,
                        1,
                        1,
                        region_name="hawaii")
    calib.run_fitting_algorithm(
        run_mode=CalibrationMode.AUTUMN_MCMC,
        n_chains=1,
        available_time=1,
    )
    app_dir = os.path.join(temp_data_dir, "outputs", "calibrate", "sharks",
                           "hawaii")
    run_dir = os.path.join(app_dir, os.listdir(app_dir)[0])
    db_paths = db.load.find_db_paths(run_dir)
    assert len(db_paths) == 1
    out_db = db.get_database(db_paths[0])
    assert set(out_db.table_names()) == {
        "outputs",
        "derived_outputs",
        "mcmc_run",
        "mcmc_params",
    }
    mcmc_runs = out_db.query("mcmc_run")
    mcmc_params = out_db.query("mcmc_params")
    mle_params = db.process.find_mle_params(mcmc_runs, mcmc_params)
    ice_cream_sales_mle = mle_params["ice_cream_sales"]
    # This value is non-deterministic due to fixed seed.
    assert 2.9 < ice_cream_sales_mle < 3.1
Пример #5
0
def run_calibration_chain(max_seconds: int, run_id: int, num_chains: int):
    params = load_params("sir_example", Region.PHILIPPINES)
    calib = Calibration(
        "sir_example",
        build_model,
        params,
        PRIORS,
        TARGET_OUTPUTS,
        run_id,
        num_chains,
        region_name=Region.PHILIPPINES,
    )
    calib.run_fitting_algorithm(
        run_mode="autumn_mcmc",
        n_chains=1,
        available_time=max_seconds,
    )
Пример #6
0
def run_calibration_chain(
    max_seconds: int,
    run_id: int,
    country: str,
    par_priors,
    target_outputs,
    mode="lsm",
    _grid_info=None,
    _start_time_range=None,
    _run_extra_scenarios=True,
    _multipliers={}
):
    """
    Run a calibration chain for the covid model

    num_iters: Maximum number of iterations to run.
    available_time: Maximum time, in seconds, to run the calibration.
    mode is either 'lsm' or 'autumn_mcmc'
    """
    print(f"Preparing to run covid model calibration for country {country}")

    country_model = CountryModel(country)
    build_model = country_model.build_model
    params = country_model.params
    calib = Calibration(
        f"covid_{country}",
        build_model,
        par_priors,
        target_outputs,
        _multipliers,
        run_id,
        model_parameters=params,
        start_time_range=_start_time_range,
        run_extra_scenarios=_run_extra_scenarios
    )
    print("Starting calibration.")
    calib.run_fitting_algorithm(
        run_mode=mode,
        n_iterations=N_ITERS,
        n_burned=N_BURNED,
        n_chains=N_CHAINS,
        available_time=max_seconds,
        grid_info=_grid_info
    )
    print(f"Finished calibration for run {run_id}.")
Пример #7
0
def run_calibration_chain(
    max_seconds: int,
    run_id: int,
    num_chains: int,
    region: str,
    par_priors,
    target_outputs,
    mode="autumn_mcmc",
    _grid_info=None,
    _multipliers={},
):
    """
    Run a calibration chain for the covid model

    num_iters: Maximum number of iterations to run.
    available_time: Maximum time, in seconds, to run the calibration.
    mode is either 'lsm' or 'autumn_mcmc'
    """
    logger.info(
        f"Preparing to run covid model calibration for region {region}")

    region_model = RegionApp(region)
    build_model = region_model.build_model
    params = region_model.params
    calib = Calibration(
        "covid_19",
        build_model,
        params,
        par_priors,
        target_outputs,
        _multipliers,
        run_id,
        num_chains,
        param_set_name=region,
    )
    logger.info("Starting calibration.")
    calib.run_fitting_algorithm(
        run_mode=mode,
        n_iterations=N_ITERS,
        n_burned=N_BURNED,
        n_chains=N_CHAINS,
        available_time=max_seconds,
        grid_info=_grid_info,
    )
    logger.info(f"Finished calibration for run {run_id}.")
Пример #8
0
def run_calibration_chain(max_seconds: int, run_id: int, num_chains: int):
    params = load_params("tuberculosis_strains", Region.PHILIPPINES)
    calib = Calibration(
        "tuberculosis_strains",
        build_model,
        params,
        PRIORS,
        TARGET_OUTPUTS,
        run_id,
        num_chains,
        region_name=Region.PHILIPPINES,
    )
    calib.run_fitting_algorithm(
        run_mode="autumn_mcmc",
        n_chains=1,
        available_time=max_seconds,
        haario_scaling_factor=params["default"]["haario_scaling_factor"],
    )
Пример #9
0
def run_calibration_chain(max_seconds: int, run_id: int, num_chains: int):
    params = load_params("tuberculosis", Region.MARSHALL_ISLANDS)
    calib = Calibration(
        "tuberculosis",
        build_model,
        params,
        PRIORS,
        TARGET_OUTPUTS,
        run_id,
        num_chains,
        region_name=Region.MARSHALL_ISLANDS,
        initialisation_type=params["default"]["metropolis_initialisation"],
        metropolis_init_rel_step_size=0.10,
    )
    calib.run_fitting_algorithm(
        run_mode="autumn_mcmc",
        n_chains=1,
        available_time=max_seconds,
        haario_scaling_factor=params["default"]["haario_scaling_factor"],
    )