예제 #1
0
def conduct_BOCPD(n_trials, n_samples, dataset, changepoints,
                  tolerance_delay):  # BOCPD
    # hyperparameter tuning
    objective_BOCPD = partial(_objective_BOCPD,
                              train=dataset[0],
                              changepoints=changepoints,
                              tolerance_delay=tolerance_delay)
    study = optuna.create_study()
    study.optimize(objective_BOCPD, n_trials=n_trials, n_jobs=-1)
    opt_lam = study.best_params['lam']
    opt_alpha = study.best_params['alpha']
    opt_beta = study.best_params['beta']
    opt_kappa = study.best_params['kappa']
    opt_mu = study.best_params['mu']

    # optimal threshold
    h = partial(bocpd.constant_hazard, opt_lam)
    lik = bocpd.StudentT(opt_alpha, opt_beta, opt_kappa, opt_mu)
    retrospective = bocpd.Retrospective(hazard_func=h, likelihood_func=lik)
    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": ["BOCPD"],
        "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
예제 #2
0
def _objective_BOCPD(trial, train, changepoints, tolerance_delay):  # BOCPD
    lam = trial.suggest_int('lam', 2, 1000)
    alpha = trial.suggest_uniform('alpha', 0.01, 10)
    beta = trial.suggest_uniform('beta', 0.01, 10)
    kappa = trial.suggest_uniform('kappa', 0.01, 10)
    mu = trial.suggest_uniform('mu', 0.01, 10)

    h = partial(bocpd.constant_hazard, lam)
    lik = bocpd.StudentT(alpha, beta, kappa, mu)
    retrospective = bocpd.Retrospective(hazard_func=h, likelihood_func=lik)

    scores = retrospective.calc_scores(train)
    F1_score, _, _, _ = calc_F1_score(scores, changepoints, tolerance_delay)

    return -F1_score
예제 #3
0
def _objective_BOCPD(trial, train, changepoints, tolerance_delay):
    lam = trial.suggest_int('lam', 2, 100)
    alpha = trial.suggest_uniform('alpha', 1e-8, 10)
    beta = trial.suggest_uniform('beta', 1e-8, 0.001)
    kappa = trial.suggest_uniform('kappa', 1e-8, 0.001)
    mu = 0
    #mu = trial.suggest_uniform('mu', -1, 1)

    h = partial(bocpd.constant_hazard, lam)
    lik = bocpd.StudentT(alpha, beta, kappa, mu)
    retrospective = bocpd.Retrospective(hazard_func=h, likelihood_func=lik)

    scores = retrospective.calc_scores(train)
    AUC = calc_AUC(scores, changepoints, tolerance_delay, both=both)

    return -AUC
예제 #4
0
                          changepoints=changepoints,
                          tolerance_delay=tolerance_delay)
study = optuna.create_study()
study.optimize(objective_BOCPD, n_trials=n_trials, n_jobs=-1)

# calculate scores
opt_lam = study.best_params['lam']
opt_alpha = study.best_params['alpha']
opt_beta = study.best_params['beta']
opt_kappa = study.best_params['kappa']
#opt_mu = study.best_params['mu']
opt_mu = 0

h = partial(bocpd.constant_hazard, opt_lam)
lik = bocpd.StudentT(opt_alpha, opt_beta, opt_kappa, opt_mu)
retrospective = bocpd.Retrospective(hazard_func=h, likelihood_func=lik)

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

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

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

# CF


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)
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)