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_Hierarchical(trial, train, changepoints, tolerance_delay, order): nml_gaussian = partial(hsdmdl2_nml.nml_gaussian) min_datapoints = 5 # delta_0だけはいかなる場合でもチューニングしなければならない delta_0 = trial.suggest_uniform('delta_0', 0.000001, 10.00) if order == 1: delta_1 = trial.suggest_uniform('delta_1', 0.000001, 1.00) else: delta_1 = 0.05 if order == 2: delta_2 = trial.suggest_uniform('delta_2', 0.000001, 1.00) else: delta_2 = 0.05 retrospective = hsdmdl2.Retrospective(encoding_func=nml_gaussian, d=2, M=5, min_datapoints=min_datapoints, delta_0=delta_0, delta_1=delta_1, delta_2=delta_2, how_to_drop='all', order=order, reliability=True) 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 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 conduct_Hierarchical(n_trials, n_samples, dataset, changepoints, tolerance_delay, order): # Hierarchical # hyperparameter tuning objective_Hierarchical = partial(_objective_Hierarchical, train=dataset[0], changepoints=changepoints, tolerance_delay=tolerance_delay, order=order) study = optuna.create_study() study.optimize(objective_Hierarchical, n_trials=n_trials, n_jobs=-1) opt_delta_0 = study.best_params['delta_0'] if order == 1: opt_delta_1 = study.best_params['delta_1'] else: opt_delta_1 = 0.05 if order == 2: opt_delta_2 = study.best_params['delta_2'] else: opt_delta_2 = 0.05 min_datapoints = 5 nml_gaussian = partial(hsdmdl2_nml.nml_gaussian) retrospective = hsdmdl2.Retrospective(encoding_func=nml_gaussian, d=2, M=5, min_datapoints=min_datapoints, delta_0=opt_delta_0, delta_1=opt_delta_1, delta_2=opt_delta_2, how_to_drop='all', order=True, reliability=True) # calculate metrics calc_metrics = partial(_calc_Hierarchical_metrics, dataset=dataset, changepoints=changepoints, tolerance_delay=tolerance_delay, threshold=0.5, retrospective=retrospective, order=order) 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])) method_name = "Hierarchical" if order == 0: method_name += "_0th" elif order == 1: method_name += "_1st" else: method_name += "_2nd" row = pd.DataFrame({ "method": [method_name], "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)