-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndt_client4_gig03_drtt_vwcd.py
213 lines (183 loc) · 7.02 KB
/
ndt_client4_gig03_drtt_vwcd.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
"""
Voting Windows Changepoint Detection example
@author: Cleiton Moya de Almeida
"""
import numpy as np
from scipy.stats import betabinom
import matplotlib.pyplot as plt
import time
plt.rcParams.update({'font.size': 8, 'axes.titlesize': 8})
# Load the timeseries
file = 'dca6326b9ca8_gig03_d_rttmean.txt'
X = np.loadtxt(f'../Dataset/ndt/{file}', usecols=1, delimiter=',')
verbose = True
# Hyperparameters
w = 20 # window size
w0 = 20 # window used to estimate the post-change parameters
alpha = 1 # Beta-binomial hyperp - prior dist. window
beta = 1 # Beta-binomial hyperp - prior dist. window
p_thr = 0.99 # threshold probability to an window decide for a changepoint
pa_thr = 0.9 # threshold probabilty to decide for a changepoint
vote_n_thr = 0.5 # min. number of votes to decide for a changepoint
y0 = 0.5 # logistic prior hyperparameter
yw = 0.9 # logistic prior hyperparameter
aggreg = 'mean'
# Auxiliary functions
# Compute the window posterior probability given the log-likelihood and prior
# using the log-sum-exp trick
def pos_fun(ll, prior, tau):
c = np.nanmax(ll)
lse = c + np.log(np.nansum(prior*np.exp(ll - c)))
p = ll[tau] + np.log(prior[tau]) - lse
return np.exp(p)
# Aggregate a list of votes - compute the posterior probability
def votes_pos(vote_list, prior_v):
vote_list = np.array(vote_list)
prod1 = vote_list.prod()*prior_v
prod2 = (1-vote_list).prod()*(1-prior_v)
p = prod1/(prod1+prod2)
return p
# Prior probabily for votes aggregation
def logistic_prior(x, w, y0, yw):
a = np.log((1-y0)/y0)
b = np.log((1-yw)/yw)
k = (a-b)/w
x0 = a/k
y = 1./(1+np.exp(-k*(x-x0)))
return y
# Compute the log-likelihood value for the normal distribution
# Obs.: the scipy built-in function logpdf does not use numpy and so is inneficient
def loglik(x,loc,scale):
n = len(x)
c = 1/np.sqrt(2*np.pi)
y = n*np.log(c/scale) -(1/(2*scale**2))*((x-loc)**2).sum()
return y
# Auxiliary variables
N = len(X)
vote_n_thr = np.floor(w*vote_n_thr)
#vote_n_thr = 16
# Prior probatilty for a changepoint in a window - Beta-Binomial
i_ = np.arange(0,w-3)
prior_w = betabinom(n=w-4,a=alpha,b=beta).pmf(i_)
# prior for votes aggregation
x_votes = np.arange(1,w+1)
prior_v = logistic_prior(x_votes, w, y0, yw)
votes = {i:[] for i in range(N)} # dictionary of votes
votes_agg = {} # aggregated voteylims
lcp = 0 # last changepoint
CP = [] # changepoint list
M0 = [] # list of post-change mean
S0 = [] # list of post-change standard deviation
N_votes_tot = np.zeros(N)
N_votes_ele = np.zeros(N)
startTime = time.time()
for n in range(N):
if n>=w-1:
# estimate the paramaters (w0 window)
if n == lcp+w0:
# estimate the post-change mean and variace
m_w0 = X[n-w0+1:n+1].mean()
s_w0 = X[n-w0+1:n+1].std(ddof=1)
M0.append(m_w0)
S0.append(s_w0)
# current window
Xw = X[n-w+1:n+1]
LLR_h = []
for nu in range(1,w-3+1):
# MLE and log-likelihood for H1
x1 = Xw[:nu+1] #Xw até nu
m1 = x1.mean()
s1 = x1.std(ddof=1)
if np.round(s1,3) == 0:
s1 = 0.001
logL1 = loglik(x1, loc=m1, scale=s1)
# MLE and log-likelihood for H2
x2 = Xw[nu+1:]
m2 = x2.mean()
s2 = x2.std(ddof=1)
if np.round(s2,3) == 0:
s2 = 0.001
logL2 = loglik(x2, loc=m2, scale=s2)
# log-likelihood ratio
llr = logL1+logL2
LLR_h.append(llr)
# Compute the posterior probability
LLR_h = np.array(LLR_h)
pos = [pos_fun(LLR_h, prior_w, nu) for nu in range(w-3)]
pos = [np.nan] + pos + [np.nan]*2
pos = np.array(pos)
# Compute the MAP (vote)
p_vote_h = np.nanmax(pos)
nu_map_h = np.nanargmax(pos)
# Store the vote
j = n-w+1+nu_map_h # Adjusted index
votes[j].append(p_vote_h)
# Aggregate the votes for X[n-w+1]
votes_list = votes[n-w+1]
elegible_votes = [v for v in votes_list if v > p_thr]
num_votes_tot = len(votes_list) # number of total votes
num_votes_ele = len(elegible_votes) # number of elegible votes
N_votes_tot[n-w+1] = num_votes_tot
N_votes_ele[n-w+1] = num_votes_ele
# Decide for a changepoit
if num_votes_ele >= vote_n_thr:
if aggreg == 'posterior':
agg_vote = votes_pos(elegible_votes, prior_v[num_votes_ele-1])
elif aggreg == 'mean':
agg_vote = np.mean(elegible_votes)
votes_agg[n-w+1] = agg_vote
if agg_vote >= pa_thr:
if verbose: print(f'n={n}: Changepoint at n={n-w+1}, p={agg_vote:.2f}, num. votes={num_votes_ele}/{num_votes_tot}')
lcp = n-w+1 # last changepoint
CP.append(lcp)
#%%
fig,ax=plt.subplots(figsize=(4.5,4), nrows=4, sharex=True, layout='constrained')
for ax_ in ax:
ax_.tick_params(axis='both', labelsize=6)
ax_.grid(linestyle=':')
ax[0].set_title('Voting window change-point detection')
ax[0].plot(X, linewidth=0.5)
if len(CP)>0:
for j,cp in enumerate(CP):
if j==0:
ax[0].axvline(cp, color='r', linewidth=0.5, label='change-point')
else:
ax[0].axvline(cp, color='r', linewidth=0.5)
ax[0].legend(loc='lower right')
ax[0].set_ylabel('ms', fontsize=6)
ax[0].set_yticks([0,100, 200,300])
ax[0].set_ylim([0,300])
ax[1].set_title('Total number of votes')
N_votes_tot_idx = np.where(N_votes_tot)[0]
N_votes_tot_nonzero = N_votes_tot[N_votes_tot_idx]
markerline, stemline, baseline = ax[1].stem(N_votes_tot_idx, N_votes_tot_nonzero)
plt.setp(markerline, markersize = 2)
plt.setp(baseline, linewidth=0)
plt.setp(stemline, linewidth=0.5)
ax[1].set_yticks([0,5,10,15,20])
ax[1].set_ylim([0,20])
ax[2].set_title('Number of votes above the threshold prob.')
N_votes_ele_idx = np.where(N_votes_ele)[0]
N_votes_ele_nonzero = N_votes_ele[N_votes_ele_idx]
markerline, stemline, baseline = ax[2].stem(N_votes_ele_idx, N_votes_ele_nonzero)
plt.setp(markerline, markersize = 2)
plt.setp(baseline, linewidth=0)
plt.setp(stemline, linewidth=0.5)
ax[2].axhline(vote_n_thr, color='red', linewidth=0.5, label='threshold')
ax[2].legend(loc='upper right')
ax[2].set_yticks([0,5,10,15,20])
ax[2].set_ylim([0,20])
ax[3].set_title('Votes aggregation (change-point probability)')
if len(CP)>0:
markerline, stemline, baseline = ax[3].stem(list(votes_agg), votes_agg.values())
plt.setp(markerline, markersize = 2)
plt.setp(baseline, linewidth=0)
plt.setp(stemline, linewidth=0.5)
ax[3].axhline(pa_thr, color='red', linewidth=0.5, label='threshold')
ax[3].legend(loc='lower right')
ax[3].set_yticks([0,0.25,0.5,0.75,1])
ax[3].set_ylim(bottom=0)
ax[3].set_xlabel('sample (t)', fontsize=6)
ax[3].set_xticks(np.arange(0,1200,100))
ax[3].set_xlim([0,1100])