-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.py
153 lines (118 loc) · 5.62 KB
/
experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 23 00:56:50 2023
Changepoint alghorithms experiment
@author: cleiton
"""
import numpy as np
import pandas as pd
import changepoint_module as cm
df = pd.read_pickle('df_series.pkl')
N = len(df)
series_type = ['d_throughput', 'd_rttmean', 'u_throughput', 'u_rttmean']
# Classical methpods - Basic implementations
sequential_ba = [cm.shewhart_ba, cm.ewma_ba, cm.cusum_2s_ba, cm.cusum_wl_ba]
# Classical methods - Proposed implementations
sequential_ps = [cm.shewhart_ps, cm.ewma_ps, cm.cusum_2s_ps, cm.cusum_wl_ps]
pairs = zip(sequential_ba, sequential_ps)
methods = [m for pair in pairs for m in pair]
methods = methods + [cm.vwcd]
# Proposed frametwork commom hyperparameters
w0 = 20 # phase 1 estimating window size
rl = 4 # consecutive deviations to consider a changepoint
ka = 6 # kappa for anomaly
alpha_norm = 0.01 # normality test significace level
alpha_stat = 0.01 # statinarity test significance level
cs_max = 5 # maximum counter for process not stabilized
filt_per = 0.95 # outlier filtering percentil (first window or not. estab.)
max_var = 1.2 # maximum level of variance increasing to consider
# Shewhart hyperparameters
k = 3
# EWMA hyperparameters
lamb = 0.1
kd = 4
# CUSUM hyperparameters
h = 5 # statistic threshold
delta = 2 # 2S-CUSUM hyperparameter
w1 = 20 # WL-CUSUM
# VWCD
wv = 20 # window-size
alpha = beta = 10 # beta-binomial hyperpeparameters
p_thr = 0.8 # threshold probability to an window decide for a changepoint
vote_p_thr = 0.9 # threshold probabilty to decide for a changepoint after aggregation
vote_n_thr = 10 # min. number of votes to decide for a changepoint
y0 = 0.5 # logistic prior hyperparameter
yw = 0.9 # logistic prior hyperparameter
aggreg = 'posterior' # votes aggregation function
Res = []
for m in methods:
print(f"Processing {m.__name__}")
for n in range(N):
client = df.iloc[n].client
site = df.iloc[n].site
# Prefixo do arquivo
prefixo = client + "_" + site + "_"
if m==cm.vwcd and n%10==0:
print(f'\tprocessing {n+1}/{N} clients-sites')
for s_type in series_type:
# Load the timeseries
file = prefixo + s_type + ".txt"
y = np.loadtxt(f'../dataset/{file}', usecols=1, delimiter=',')
# Remove possible nan values
y = y[~np.isnan(y)]
# Maps the kargs for each method
if m.__name__ == 'shewhart_ba':
kargs = {'y':y, 'w':w0, 'k':k}
elif m.__name__ == 'shewhart_ps' or m.__name__ == 'shewhart_ps2':
kargs = {'y':y, 'w':w0, 'k':k, 'rl':rl, 'ka':ka,
'alpha_norm':alpha_norm, 'alpha_stat':alpha_stat,
'filt_per':filt_per, 'max_var':max_var,
'cs_max':cs_max}
elif m.__name__ == 'ewma_ba':
kargs = {'y':y, 'w':w0, 'kd':kd, 'lamb':lamb}
elif m.__name__ == 'ewma_ps':
kargs = {'y':y, 'w':w0, 'kd':kd, 'lamb':lamb, 'rl':rl, 'ka':ka,
'alpha_norm':alpha_norm, 'alpha_stat':alpha_stat,
'filt_per':filt_per, 'max_var':max_var,
'cs_max':cs_max}
elif m.__name__ == 'cusum_2s_ba':
kargs = {'y':y, 'w':w0, 'delta':delta, 'h':h}
elif m.__name__ == 'cusum_2s_ps':
kargs = {'y':y, 'w':w0, 'delta':delta, 'h':h,
'rl':rl, 'ka':ka,
'alpha_norm':alpha_norm, 'alpha_stat':alpha_stat,
'filt_per':filt_per, 'max_var':max_var,
'cs_max':cs_max}
elif m.__name__ == 'cusum_wl_ba':
kargs = {'y':y, 'w0':w0, 'w1':w1, 'h':h}
elif m.__name__ == 'cusum_wl_ps':
kargs = {'y':y, 'w0':w0, 'w1':w1, 'h':h,
'rl':rl, 'k':k, 'ka':ka,
'alpha_norm':alpha_norm, 'alpha_stat':alpha_stat,
'filt_per':filt_per, 'max_var':max_var,
'cs_max':cs_max}
elif m.__name__ == 'vwcd':
kargs = {'X':y, 'w':wv, 'w0':wv, 'alpha':alpha, 'beta':beta,
'p_thr':p_thr, 'vote_p_thr':vote_p_thr,
'vote_n_thr':vote_n_thr, 'y0':y0, 'yw':yw,
'aggreg':aggreg}
# Call the methods
num_anom_u = num_anom_l = M0 = S0 = None
out = m(**kargs)
if m in sequential_ba:
CP, elapsed_time = out
elif m in sequential_ps:
CP, Anom_u, Anom_l, M0, S0, elapsed_time = out
num_anom_u = len(Anom_u)
num_anom_l = len(Anom_l)
elif m == cm.vwcd:
CP, M0, S0, elapsed_time = out
# Store the results
res = {'client':client, 'site':site, 'serie':s_type,
'method':m.__name__, 'CP':CP, 'num_cp':len(CP),
'num_anom_u':num_anom_u, 'num_anom_l':num_anom_l,
'M0':M0, 'S0':S0, 'elapsed_time':elapsed_time}
Res.append(res)
# Dataframe with results
df_res = pd.DataFrame(Res)
pd.to_pickle(df_res, 'df_results.pkl')