def _objective_FW2S_MDL(trial, train, changepoints, tolerance_delay,
                        params):  # FW2S_MDL
    nml_gaussian = partial(sdmdl_nml.nml_gaussian,
                           mu_max=params["mu_max"],
                           div_min=params["div_min"],
                           div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian,
                                  mu_max=params["mu_max"],
                                  div_min=params["div_min"],
                                  div_max=params["div_max"])
    window_size_1 = trial.suggest_int('window_size_1', 10, 500)
    window_size_2 = trial.suggest_int('window_size_2', 10, 500)

    retrospective_first = sdmdl.Retrospective(
        h=window_size_1,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)
    retrospective_second = sdmdl.Retrospective(
        h=window_size_2,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)
    retrospective = fw2s_mdl.Retrospective(retrospective_first,
                                           retrospective_second)
    scores = retrospective.calc_scores(train)

    AUC = calc_AUC(scores, changepoints, tolerance_delay)
    return -AUC
def conduct_FW2S_MDL(n_trials, n_samples, dataset, changepoints,
                     tolerance_delay, params):  # FW2S_MDL
    # hyperparameter tuning
    objective_FW2S_MDL = partial(_objective_FW2S_MDL,
                                 train=dataset[0],
                                 changepoints=changepoints,
                                 tolerance_delay=tolerance_delay,
                                 params=params)
    study = optuna.create_study()
    study.optimize(objective_FW2S_MDL, n_trials=n_trials, n_jobs=-1)
    opt_window_size_1 = study.best_params['window_size_1']
    opt_window_size_2 = study.best_params['window_size_2']

    nml_gaussian = partial(sdmdl_nml.nml_gaussian,
                           mu_max=params["mu_max"],
                           div_min=params["div_min"],
                           div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian,
                                  mu_max=params["mu_max"],
                                  div_min=params["div_min"],
                                  div_max=params["div_max"])

    retrospective_first = sdmdl.Retrospective(
        h=opt_window_size_1,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)
    retrospective_second = sdmdl.Retrospective(
        h=opt_window_size_2,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)
    retrospective = fw2s_mdl.Retrospective(retrospective_first,
                                           retrospective_second)

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

    print("AUC:  ", np.mean(res[:]), "±", np.std(res[:]))

    row = pd.DataFrame({
        "method": ["FW2S_MDL"],
        "AUC_mean": np.mean(res[:]),
        "AUC_std": np.std(res[:])
    })

    return row
Ejemplo n.º 3
0
def _objective_FW2S_MDL(trial, train, changepoints, tolerance_delay, params):
    nml_multgaussian = partial(sdmdl_nml.nml_multgaussian,
                               R=params["R"],
                               lambda_min=params["lambda_min"])
    complexity_multgaussian = partial(sdmdl_nml.complexity_multgaussian,
                                      m=8,
                                      R=params["R"],
                                      lambda_min=params["lambda_min"])

    nml_gaussian = partial(sdmdl_nml.nml_gaussian,
                           mu_max=params["mu_max"],
                           div_min=params["div_min"],
                           div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian,
                                  mu_max=params["mu_max"],
                                  div_min=params["div_min"],
                                  div_max=params["div_max"])
    delta_0 = trial.suggest_uniform('delta_0', 0.001, 0.499)
    window_size_1 = trial.suggest_int('window_size_1', 100, 300)
    window_size_2 = trial.suggest_int('window_size_2', 100, 300)

    retrospective_first = sdmdl.Retrospective(
        h=window_size_1,
        encoding_func=nml_multgaussian,
        complexity_func=complexity_multgaussian,
        delta_0=delta_0,
        order=0)
    retrospective_second = sdmdl.Retrospective(
        h=window_size_2,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)
    retrospective = fw2s_mdl.Retrospective(retrospective_first,
                                           retrospective_second)
    scores = retrospective.calc_scores(train)

    n = len(scores)
    scores[2 * (window_size_1 +
                window_size_2):n] = scores[window_size_1 + window_size_2:n -
                                           (window_size_1 + window_size_2)]
    scores[0:2 * (window_size_1 + window_size_2)] = np.nan

    AUC = calc_AUC(scores, changepoints, tolerance_delay, both=both)
    return -AUC
def conduct_SDMDL(n_trials, n_samples, dataset, changepoints, tolerance_delay,
                  params):  # S-MDL
    # hyperparameter tuning
    objective_SDMDL = partial(_objective_SDMDL,
                              train=dataset[0],
                              changepoints=changepoints,
                              tolerance_delay=tolerance_delay,
                              params=params)
    study = optuna.create_study()
    study.optimize(objective_SDMDL, n_trials=n_trials, n_jobs=-1)
    opt_window_size = study.best_params['window_size']

    nml_gaussian = partial(sdmdl_nml.nml_gaussian,
                           mu_max=params["mu_max"],
                           div_min=params["div_min"],
                           div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian,
                                  mu_max=params["mu_max"],
                                  div_min=params["div_min"],
                                  div_max=params["div_max"])
    retrospective = sdmdl.Retrospective(h=opt_window_size,
                                        encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian,
                                        order=params["order"])

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

    print("AUC:  ", np.mean(res[:]), "±", np.std(res[:]))

    if params["order"] == 0:
        method_name = "SDMDL_0"
    elif params["order"] == 1:
        method_name = "SDMDL_1"
    else:
        method_name = "SDMDL_2"

    row = pd.DataFrame({
        "method": [method_name],
        "AUC_mean": np.mean(res[:]),
        "AUC_std": np.std(res[:])
    })

    return row
Ejemplo n.º 5
0
def _objective_SDMDL(trial, train, changepoints, tolerance_delay, params):
    nml_gaussian = partial(sdmdl_nml.nml_gaussian, mu_max=params[
                           "mu_max"], div_min=params["div_min"], div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian, mu_max=params[
                                  "mu_max"], div_min=params["div_min"], div_max=params["div_max"])
    window_size = trial.suggest_int('window_size', 5, 60)
    retrospective = sdmdl.Retrospective(h=window_size, encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian, order=params["order"])

    scores = retrospective.calc_scores(train)
    n = len(scores)
    scores[2 * window_size - 1: n] = scores[window_size - 1: n - window_size]
    scores[0:2 * window_size] = np.nan

    AUC = calc_AUC(scores, changepoints,  tolerance_delay, both=both)
    return -AUC
Ejemplo n.º 6
0
def _objective_AW2S_MDL(trial, train, changepoints, tolerance_delay):
    window_size = trial.suggest_int('window_size', 10, 500)
    sigma_given = trial.suggest_uniform('sigma_given', 0.1, 2)

    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=window_size,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)

    lnml_gaussian = partial(hsdmdl2_nml.lnml_gaussian, sigma_given=sigma_given)
    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)

    alarms = retrospective.make_alarms(train)
    scores = np.zeros(len(train))
    scores[alarms] = 1

    F1_score, _, _ = calc_F1_score(scores,
                                   changepoints,
                                   tolerance_delay,
                                   tuned_threshold=0.5)

    return -F1_score
def _objective_SDMDL(trial, train, changepoints, tolerance_delay,
                     params):  # S-MDL
    nml_gaussian = partial(sdmdl_nml.nml_gaussian,
                           mu_max=params["mu_max"],
                           div_min=params["div_min"],
                           div_max=params["div_max"])
    complexity_gaussian = partial(sdmdl_nml.complexity_gaussian,
                                  mu_max=params["mu_max"],
                                  div_min=params["div_min"],
                                  div_max=params["div_max"])
    window_size = trial.suggest_int('window_size', 10, 500)
    retrospective = sdmdl.Retrospective(h=window_size,
                                        encoding_func=nml_gaussian,
                                        complexity_func=complexity_gaussian,
                                        order=params["order"])

    scores = retrospective.calc_scores(train)

    AUC = calc_AUC(scores, changepoints, tolerance_delay)
    return -AUC
Ejemplo n.º 8
0
                          tolerance_delay=tolerance_delay,
                          params=params)
study = optuna.create_study()
study.optimize(objective_SDMDL, n_trials=n_trials, n_jobs=-1)
opt_window_size = study.best_params['window_size']

nml_multgaussian = partial(sdmdl_nml.nml_multgaussian,
                           R=params["R"],
                           lambda_min=params["lambda_min"])
complexity_multgaussian = partial(sdmdl_nml.complexity_multgaussian,
                                  m=8,
                                  R=params["R"],
                                  lambda_min=params["lambda_min"])

retrospective = sdmdl.Retrospective(h=opt_window_size,
                                    encoding_func=nml_multgaussian,
                                    complexity_func=complexity_multgaussian,
                                    order=params["order"])

sdmdl_0_scores = retrospective.calc_scores(X.T)
n = len(sdmdl_0_scores)
sdmdl_0_scores[2 * opt_window_size -
               1:n] = sdmdl_0_scores[opt_window_size - 1:n - opt_window_size]
sdmdl_0_scores[0:2 * opt_window_size] = np.nan

# 1st D-MDL
params = {"R": R, "lambda_min": lambda_min, "order": 1}

# hyperparameter tuning
objective_SDMDL = partial(_objective_SDMDL,
                          train=X.T,
                          changepoints=changepoints,
Ejemplo n.º 9
0
def conduct_AW2S_MDL(n_trials, n_samples, dataset, changepoints,
                     tolerance_delay):  # AW2S-MDL
    # hyperparameter tuning
    objective_AW2S_MDL = partial(_objective_AW2S_MDL,
                                 train=dataset[0],
                                 changepoints=changepoints,
                                 tolerance_delay=tolerance_delay)
    study = optuna.create_study()
    study.optimize(objective_AW2S_MDL, n_trials=n_trials, n_jobs=-1)

    opt_window_size = study.best_params['window_size']
    opt_sigma_given = study.best_params['sigma_given']

    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=opt_window_size,
        encoding_func=nml_gaussian,
        complexity_func=complexity_gaussian,
        order=0)

    lnml_gaussian = partial(hsdmdl2_nml.lnml_gaussian,
                            sigma_given=opt_sigma_given)
    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)

    # calculate metrics
    calc_metrics = partial(_calc_AW2S_MDL_metrics,
                           dataset=dataset,
                           changepoints=changepoints,
                           tolerance_delay=tolerance_delay,
                           threshold=0.5,
                           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": ["AW2S_MDL"],
        "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
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)