Beispiel #1
0
def _calc_metrics(idx_data, dataset, changepoints, tolerance_delay, threshold,
                  retrospective):  # calculate the metrics
    _retrospective = deepcopy(retrospective)
    scores = _retrospective.calc_scores(dataset[idx_data])
    F1_score, precision, recall = calc_F1_score(scores, changepoints,
                                                tolerance_delay, threshold)
    return F1_score, precision, recall
Beispiel #2
0
def _calc_AW2S_MDL_metrics(idx_data, dataset, changepoints, tolerance_delay,
                           threshold, retrospective):
    _retrospective = deepcopy(retrospective)
    alarms = _retrospective.make_alarms(dataset[idx_data])
    scores = np.zeros(len(dataset[idx_data]))
    scores[alarms] = 1

    F1_score, precision, recall = calc_F1_score(scores, changepoints,
                                                tolerance_delay, threshold)
    return F1_score, precision, recall
Beispiel #3
0
def _objective_CF(trial, train, changepoints, tolerance_delay):  # ChangeFinder
    # 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)
    F1_score, _, _, _ = calc_F1_score(scores, changepoints, tolerance_delay)

    return -F1_score
Beispiel #4
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
Beispiel #5
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
Beispiel #6
0
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
Beispiel #7
0
def calc_opt_threshold(train, changepoints, tolerance_delay, retrospective):
    _retrospective = deepcopy(retrospective)
    scores = _retrospective.calc_scores(train)
    _, _, _, opt_threshold = calc_F1_score(scores, changepoints,
                                           tolerance_delay)
    return opt_threshold