-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_mat4.m
319 lines (283 loc) · 12.2 KB
/
eval_mat4.m
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function eval_mat4()
% --------------
clc;
clear all;
%close all;
warning('off');
per=1:20;
nperiods=2:20;
npperiod=1:20;
noise=60;
str={'Auto','Enright','DFT'};
marker={'d','s','o'};
linestyle={':','--','-'};
colors={'r','g','b'};
avg_res5=zeros(6,3);
std_res5=zeros(6,3);
res3=zeros(6,5);
for i=1:3
mat=[];
for j=1:2
f=sprintf('simulations_ver3/mat_%s_per_%d_%d_noise_%d.mat',str{i},10*j-9,10*j,noise);
s=load(f);
mat=[mat;s.mat];
end
mat(find((mat(:,3)==1)),:)=[]; % removes mat positions with one point per period
mat(find((mat(:,3)==2)),:)=[]; % removes mat positions with two points per period
% Plot results obtained from simulations
plot_results(2,i);
%calc_period_variance(i);
%plot_hist(i);
end
%plot_hist_aux;
% Plot average error as a function of the number of
% periods or the number of points per period
function plot_results(xaxis,i)
if (xaxis==1)
init=1;
final=19;
array=nperiods;
axis_label='Number of cycles (NC)';
col=[3 2];
else if (xaxis==2)
init=3;
final=20;
array=npperiod;
axis_label='Number of points per cycle (NPC)';
col=[2 3];
end
end
aux1=mat(find((mat(:,col(1))>=1)&(mat(:,col(1))<=20)),:);
count3=1;
for k=init:final
aux2=aux1(find(aux1(:,col(2))==array(k)),:);
aux3=abs(aux2(:,4:3:16)); % the period
aux4=aux2(:,5:3:17); % the power
aux5=aux2(:,6:3:18); % the pvalue
% Obtains the average relative difference of periods
aux3(find(aux3==0))=Inf;
%aux6=abs((repmat(aux2(:,1),1,5))-aux3);
%aux7=min(aux6,[],2);
[x y]=size(aux3);
for l=1:x
xxx=min(aux5(l,aux3(l,:)~=Inf));
xxx(find(xxx>=0.01))=[];
if ~isempty(xxx)
yyy=find(aux5(l,:)==xxx);
aux7(l)=aux3(l,yyy(1));
else
aux7(l)=Inf;
end
end
aux7=abs(aux7(:,1)-aux2(:,1));
aux8=aux7./(aux2(:,1));
aux9=aux8(find(aux8~=Inf));
% Stores results
res_mean_avgper(count3)=mean(aux9);
res_std_avgper(count3)=std(aux9)/sqrt(length(aux9));
count3=count3+1;
end
% Produces the plots
figure(1);
subplot(3,2,6);
hold on;
h=errorbar(array(init:final),res_mean_avgper,res_std_avgper,'k');
set(h(1),'Marker',marker{i},'LineStyle',linestyle{i},'DisplayName',str{i},'Color',colors{i});
set(gca,'fontweight','b','fontsize',16);
xlabel(axis_label,'fontweight','b','fontsize',16);
ylabel('Average RD','fontweight','b','fontsize',16);
axis([1 20 0 0.08]);
set(gca,'fontweight','b','fontsize',16);
aux=['Noise - ',num2str(noise),'%'];
title(aux,'fontweight','b','fontsize',16);
legend(str);
grid on;
end
% Calculates period variance
function calc_period_variance(i)
count1=1;
for j=1:length(nperiods)
for k=3:length(npperiod)
aux1=mat(find((mat(:,2)==nperiods(j))&(mat(:,3)==npperiod(k))),:);
count2=1;
for l=1:length(per)
aux2=aux1(find(aux1(:,1)==per(l)),:);
aux3=abs(aux2(:,4:3:16)); % the period
aux4=aux2(:,5:3:17); % the power
aux5=aux2(:,6:3:18); % the pvalue
% Obtains the average relative difference of periods
aux3(find(aux3==0))=Inf;
aux6=abs((repmat(aux2(:,1),1,5))-aux3);
[aux7 ind1]=min(aux6,[],2);
aux8=aux7./(aux2(:,1));
aux9=aux8(find(aux8~=Inf));
% Only consider relevant points
if ~isempty(aux9)
res1(count2)=aux9;
count2=count2+1;
end
end
final_res1(count1)=var(res1);
count1=count1+1;
end
end
% Write final results
mean(final_res1)
end
% Plot histogram of positions
function plot_hist(i)
res1=zeros(6,6);
res2=zeros(6,5);
res4=zeros(6,2);
res5=zeros(6,1);
count=1;
for j=1:length(nperiods)
for k=3:length(npperiod)
aux1=mat(find((mat(:,2)==nperiods(j))&(mat(:,3)==npperiod(k))),:);
for l=1:length(per)
aux2=aux1(find(aux1(:,1)==per(l)),:);
aux3=abs(aux2(:,4:3:16)); % the period
aux4=aux2(:,5:3:17); % the power
aux5=aux2(:,6:3:18); % the pvalue
% Obtains the average relative difference of periods
aux3(find(aux3==0))=Inf;
if ~isempty(find(aux3~=Inf))
aux6=abs((repmat(aux2(:,1),1,5))-aux3);
[aux7 ind1]=min(aux6,[],2); % closest period
[aux8 ind2]=max(aux4(aux3~=Inf),[],2); % maximum power
[aux9 ind3]=min(aux5(aux3~=Inf),[],2); % minimum pvalue
aux10=aux7./(aux2(:,1));
aux11=(abs(aux3(ind2)-(aux2(:,1))))./(aux2(:,1));
aux12=(abs(aux3(ind3)-(aux2(:,1))))./(aux2(:,1));
% Take the closest period with pvalue < 0.01
aux13=sort(aux6(find(aux5<0.01)),'ascend');
if ~isempty(aux13)
ind4=find(aux6==aux13(1));
aux14=(abs(aux3(ind4)-(aux2(:,1))))./(aux2(:,1));
else
aux14=Inf;
end
% Take the maximum power with pvalue < 0.01
aux15=sort(aux4(find(aux5<0.01)),'descend');
if ~isempty(aux15)
ind5=find(aux4==aux15(1));
aux16=(abs(aux3(ind5)-(aux2(:,1))))./(aux2(:,1));
else
aux16=Inf;
end
% Take the minimum pvalues with pvalue < 0.01
aux17=sort(aux5(find(aux5<0.01)),'ascend');
if ~isempty(aux17)
ind6=find(aux5==aux17(1));
aux18=(abs(aux3(ind6)-(aux2(:,1))))./(aux2(:,1));
else
aux18=Inf;
end
else
aux10=Inf;
aux11=Inf;
aux12=Inf;
end
% Sort final results
aux19=[ind1 ind4 ind2 ind5 ind3 ind6];
aux20=[aux10 aux14 aux11 aux16 aux12 aux18];
for opt=1:6
ind=aux19(opt);
aux=aux20(opt);
if aux~=Inf
res1(opt,ind+1)=res1(opt,ind+1)+1;
res2(opt,ind,count)=aux;
res4(opt,2)=res4(opt,2)+1;
res5(opt,i,count)=aux;
else
res1(opt,1)=res1(opt,1)+1;
res4(opt,1)=res4(opt,1)+1;
end
end
count=count+1;
if (aux7~=Inf)
aux21=find(sort(aux4,'descend')==aux4(ind1)); %gets power position of the closest period
aux22=find(sort(aux5,'ascend')==aux5(ind1)); %gets p-value position of the closest period
res3(2*i-1,aux21(1))=res3(2*i-1,aux21(1))+1;
res3(2*i,aux22(1))=res3(2*i,aux22(1))+1;
end
end
end
end
for opt=1:6
avg_res2(opt,:)=mean(res2(opt,:,:),3);
avg_res5(opt,i)=mean(res5(opt,i,:),3);
res1(opt,:)=res1(opt,:)./sum(res1(opt,:)); % normalize results
res4(opt,:)=res4(opt,:)./sum(res4(opt,:)); % normalize results
for l=1:5
std_res2(opt,l)=std(res2(opt,l,:));
end
std_res5(opt,i)=std(res5(opt,i,:))/sqrt(length(res5(opt,i,:)));
end
res3(2*i-1,:)=res3(2*i-1,:)./sum(res3(2*i-1,:)); % normalize results
res3(2*i,:)=res3(2*i,:)./sum(res3(2*i,:)); % normalize results
% figure(1);
% subplot(3,2,2*i-1);
% title(str(i),'fontweight','b','fontsize',13);
% hold on;
% barweb(res1',zeros(6,6));
% xlim([0 7]);
% ylim([0 1]);
% set(gca,'fontweight','b','fontsize',13,'XTickLabel',['0';'1';'2';'3';'4';'5']);
% xlabel('Position in the results vector','fontweight','b','fontsize',13);
% ylabel('Normalized Count','fontweight','b','fontsize',13);
% legend('Closest','Closest Elm NS','Max Power','Max Power Elm NS','Min P-Value','Min P-Value Elm NS');
% grid on;
% subplot(3,2,2*i);
% title(str(i),'fontweight','b','fontsize',13);
% hold on;
% barweb(-log(avg_res2)',-log(std_res2)');
% hold on;
% xlim([0 6]);
% ylim([0 15]);
% set(gca,'fontweight','b','fontsize',13,'XTickLabel',['1';'2';'3';'4';'5']);
% xlabel('Position in results (increasing period)','fontweight','b','fontsize',13);
% ylabel('-log(Relative difference)','fontweight','b','fontsize',13);
% %legend('Closest','Closest Elm NS','Maximum Power','Maximum Power Elm NS','Minimum P-Value','Minimum P-Value Elm NS');
% grid on;
%
% figure(3);
% subplot(3,2,2*i-1);
% title(str(i),'fontweight','b','fontsize',13);
% hold on;
% barweb(res4',zeros(2,6));
% xlim([0 3]);
% ylim([0 1]);
% set(gca,'fontweight','b','fontsize',13,'XTickLabel',['0';'1']);
% ylabel('Normalized Count','fontweight','b','fontsize',13);
% legend('Closest','Closest (p<0.01)','Max Power','Max Power (p<0.01)','Min P-Value','Min P-Value (p<0.01)');
% grid on;
end
function plot_hist_aux()
% figure(1)
% subplot(2,2,1);
% title('Noise - 0%','fontweight','b','fontsize',16);
% hold on;
% barweb(res3',zeros(5,6));
% xlim([0 6]);
% ylim([0 1]);
% set(gca,'fontweight','b','fontsize',13,'XTickLabel',['1';'2';'3';'4';'5']);
% xlabel('Highest to Lowest Power / Lowest to Highest P-Value','fontweight','b','fontsize',13);
% ylabel('Normalized Count','fontweight','b','fontsize',13);
% legend('Power of closest period-Auto','P-value of closest period-Auto','Power of closest period-Enright','P-value of closest period-Enright','Power of closest period-DFT','P-value of closest period-DFT');
% grid on;
subplot(3,1,3);
title('Noise - 60%','fontweight','b','fontsize',16);
hold on;
avg_res5(1:2,:)=[];
avg_res5(2:3,:)=[];
barweb(-log(avg_res5)',zeros(3,2));
hold on;
ylim([0 10]);
set(gca,'fontweight','b','fontsize',13,'XTickLabel',['1';'2';'3']);
ylabel('-log(RD)','fontweight','b','fontsize',13);
%legend('Closest','Closest (p<0.01)','Max Power','Max Power (p<0.01)','Min P-Value','Min P-Value (p<0.01)');
legend('Max Peak','Min p-value (p<0.01)');
grid on;
end
end