def _objective_CF(trial, train, changepoints, tolerance_delay):  # CF
    # hyperparameters
    r = trial.suggest_uniform('r', 0.01, 0.99)
    order = trial.suggest_int('order', 1, 20)
    smooth = trial.suggest_int('smooth', 3, 20)

    retrospective = changefinder.Retrospective(r=r, order=order, smooth=smooth)
    scores = retrospective.calc_scores(train)
    AUC = calc_AUC(scores, changepoints, tolerance_delay)
    return -AUC
Example #2
0
def conduct_CF(n_trials, n_samples, dataset, changepoints,
               tolerance_delay):  # ChangeFinder
    # hyperparameter tuning
    objective_CF = partial(_objective_CF,
                           train=dataset[0],
                           changepoints=changepoints,
                           tolerance_delay=tolerance_delay)
    study = optuna.create_study()
    study.optimize(objective_CF, n_trials=n_trials, n_jobs=-1)
    opt_r = study.best_params['r']
    opt_order = study.best_params['order']
    opt_smooth = study.best_params['smooth']

    # optimal threshold
    retrospective = changefinder.Retrospective(r=opt_r,
                                               order=opt_order,
                                               smooth=opt_smooth)
    opt_threshold = calc_opt_threshold(train=dataset[0],
                                       changepoints=changepoints,
                                       tolerance_delay=tolerance_delay,
                                       retrospective=retrospective)

    # calculate metrics
    calc_metrics = partial(_calc_metrics,
                           dataset=dataset,
                           changepoints=changepoints,
                           tolerance_delay=tolerance_delay,
                           threshold=opt_threshold,
                           retrospective=retrospective)
    p = Pool(multi.cpu_count() - 1)
    args = list(range(1, n_samples))
    res = np.array(p.map(calc_metrics, args))
    p.close()

    # result
    print("F1 score:  ", np.mean(res[:, 0]), "±", np.std(res[:, 0]))
    print("precision:  ", np.mean(res[:, 1]), "±", np.std(res[:, 1]))
    print("recall:  ", np.mean(res[:, 2]), "±", np.std(res[:, 2]))

    row = pd.DataFrame({
        "method": ["ChangeFinder"],
        "F1_score_mean": np.mean(res[:, 0]),
        "F1_score_std": np.std(res[:, 0]),
        "precision_mean": np.mean(res[:, 1]),
        "precision_std": np.std(res[:, 1]),
        "recall_mean": np.mean(res[:, 2]),
        "recall_std": np.std(res[:, 2])
    })

    return row
Example #3
0
def _objective_CF(trial, train, changepoints, tolerance_delay):
    # hyperparameters
    r = trial.suggest_uniform('r', 0.01, 0.99)
    order = trial.suggest_int('order', 1, 20)
    smooth = trial.suggest_int('smooth', 3, 20)

    retrospective = changefinder.Retrospective(r=r, order=order, smooth=smooth)

    scores = np.zeros((8, 26450))

    for i in range(8):
        scores[i] = retrospective.calc_scores(train[i])

    scores = np.nanmax(scores, axis=0)

    AUC = calc_AUC(scores, changepoints, tolerance_delay, both=both)
    return -AUC
Example #4
0
    return -AUC


# hyperparameter tuning
objective_CF = partial(_objective_CF,
                       train=X,
                       changepoints=changepoints,
                       tolerance_delay=tolerance_delay)
study = optuna.create_study()
study.optimize(objective_CF, n_trials=n_trials, n_jobs=-1)
opt_r = study.best_params['r']
opt_order = study.best_params['order']
opt_smooth = study.best_params['smooth']

retrospective = changefinder.Retrospective(r=opt_r,
                                           order=opt_order,
                                           smooth=opt_smooth)

cf_scores = np.zeros((8, 26450))

for i in range(8):
    cf_scores[i] = retrospective.calc_scores(X[i])

cf_scores = np.nanmax(cf_scores, axis=0)

# SDMDL 0th


def _objective_SDMDL(trial, train, changepoints, tolerance_delay, params):

    nml_multgaussian = partial(sdmdl_nml.nml_multgaussian,
def test_main():
    np.random.seed(0)

    tolerance_delay = 100
    transition_period = 200
    data, changepoints = one_mean_changing(transition_period=transition_period)

    name = "ChangeFinder"
    retrospective = changefinder.Retrospective(r=0.1, order=5, smooth=5)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "BOCPD"
    h = partial(bocpd.constant_hazard, 1000)
    lik = bocpd.StudentT(0.5, 5, 5, 0)
    retrospective = bocpd.Retrospective(hazard_func=h, likelihood_func=lik)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "SDMDL_0th"
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian, order=0)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "SDMDL_1st"
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian, order=1)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "SDMDL_2nd"
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian, order=2)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW1_0th"
    lnml_gaussian = partial(hsdmdl1_nml.lnml_gaussian)
    retrospective = hsdmdl1.Retrospective(encoding_func=lnml_gaussian, d=2, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=0, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW1_1st"
    lnml_gaussian = partial(hsdmdl1_nml.lnml_gaussian)
    retrospective = hsdmdl1.Retrospective(encoding_func=lnml_gaussian, d=2, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=1, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW1_2nd"
    lnml_gaussian = partial(hsdmdl1_nml.lnml_gaussian)
    retrospective = hsdmdl1.Retrospective(encoding_func=lnml_gaussian, d=2, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=2, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW2_0th"
    nml_gaussian = partial(hsdmdl2_nml.nml_gaussian)
    retrospective = hsdmdl2.Retrospective(encoding_func=nml_gaussian, d=2, M=5, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=0, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW2_1st"
    nml_gaussian = partial(hsdmdl2_nml.nml_gaussian)
    retrospective = hsdmdl2.Retrospective(encoding_func=nml_gaussian, d=2, M=5, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=1, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "Hierarchical_SCAW2_2nd"
    nml_gaussian = partial(hsdmdl2_nml.nml_gaussian)
    retrospective = hsdmdl2.Retrospective(encoding_func=nml_gaussian, d=2, M=5, min_datapoints=5, delta_0=0.05,
                                          delta_1=0.05, delta_2=0.05, how_to_drop='all', order=2, reliability=True)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "FW2S_MDL"
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective_first = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                              complexity_func=complexity_gaussian, order=0)
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective_second = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                               complexity_func=complexity_gaussian, order=0)
    retrospective = fw2s_mdl.Retrospective(
        retrospective_first, retrospective_second)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)

    name = "AW2S_MDL"
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=1e8,
                           div_min=1e-8, div_max=1e8)
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=1e8,
                                  div_min=1e-8, div_max=1e8)
    retrospective_first = sdmdl.Retrospective(h=100, encoding_func=nml_gaussian,
                                              complexity_func=complexity_gaussian, order=0)
    lnml_gaussian = partial(hsdmdl2_nml.lnml_gaussian, sigma_given=0.3)
    retrospective_second = hsdmdl2.Retrospective(encoding_func=lnml_gaussian, d=2, M=5, min_datapoints=5, delta_0=0.05,
                                                 delta_1=0.05, delta_2=0.05, how_to_drop='all', order=0, reliability=True)

    retrospective = aw2s_mdl.Retrospective(
        retrospective_first, retrospective_second)
    _calc_metrics_draw_figure(
        name, data, changepoints, tolerance_delay=tolerance_delay, retrospective=retrospective)