forked from reynard-hu/mbbna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjfun_slide_LDA5
247 lines (194 loc) · 7.2 KB
/
Objfun_slide_LDA5
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
function [Sm] = Objfun_slide_LDA5(Amv,label,K, lambda)
%% objective function:
% min_{Sm, Sm*1=1, Sm_ii>0}
% \sum_{m}^{M}(\sum_{v}^{V}||Sm - Am^v||_F^2 + lambda*\sum_{p}^{K}||Sm - Sp||_F^2 + [1+\sum_{p}^{K}||Sm - Sp||_F^2 - \sum_{q}^{K}||Sm - Sq||_F^2]_+)
% Amv: m * v cell, each cell is n * n matrix
% label: m * 1 matrix
% Sm: n * n matrix
% Sp: n * n matrix
% Sq: n * n matrix
% K: the number of neighbors
% m: the number of samples
% v: the number of slide-windows
% n: the number of ROI
%% solutions:
% for each m, each row
% if the hinge loss > 0: \sum_{i}^{n} ||s_i - \frac{1}{V+(lambda+1)*K - K} \sum_{v}^{V}a_i^v + (lambda+1)*\sum_{p}^{K}s_i^p - \sum_{q}^{K}s_i^q||_2^2
% else: \sum_{i}^{n} ||s_i - \frac{1}{V+lambda*K} \sum_{v}^{V}a_i^v + lambda*\sum_{p}^{K}s_i^p ||_2^2
%% example: [Sm] = Objfun_slide_LDA5(data,label)
if (~exist('K','var'))
K = 30;
end
if (~exist('lambda','var'))
lambda = 0.5;
end
eps =10^-4;
[Sam_num,Time_num] = size(Amv); % Sam_num: the number of sample; Time_num: the time of slide-window
ROI_num = size(Amv{1,1},1); % ROI_num: the selected brain region
%% Initialization
%% to calculate the sum of the Amv
for i = 1:Sam_num
sum_Am_i =zeros(ROI_num,ROI_num);
for j =1:Time_num
sum_Am_i = sum_Am_i + Amv{i,j};
end
sum_Amv{i} = sum_Am_i;
end
%% Initialize the Sm
% for i = 1:Sam_num
% for j = 1:ROI_num
% numerator = (sum_Amv{i});
% denominator = Time_num;
% v1 = numerator(j,:)/(denominator+eps);
% Sm_i{i}(j,:) = ProjectOntoSimplex(v1, 1);
% end
% Sm{i} = (Sm_i{i} + Sm_i{i}')/2;
% end
for i=1:Sam_num
Sm_i{i} = rand(ROI_num,ROI_num)*2;
Sm{i} = (Sm_i{i} + Sm_i{i}')/2;
end
flagP = 1;
iter = 1;
obji =1;
while 1
[Sm_update, Sp_value, Sq_value] = generate_Sm_update(Amv, label, Sm, sum_Amv, K, lambda);
%% calculate obj
sum_obj_term1 = 0;
sum_obj_term2 = 0;
sumsp = 0;
sumsq = 0;
for ii = 1:Sam_num
for jj = 1:Time_num
sum_obj_term1 = sum_obj_term1 + norm((Sm{ii}-Amv{ii,jj}),'fro')^2;
% sum(diag(EuDist2(Sm{ii},Amv{ii,jj})).^2);
end
sum_obj_term2 = sum_obj_term2 + lambda * sum(Sp_value{ii}) + max(1 + sum(Sp_value{ii}) - sum(Sq_value{ii}),0);
sumsp = sumsp + sum(Sp_value{ii});
sumsq = sumsq + sum(Sq_value{ii});
end
obj(iter) = sum_obj_term1 + sum_obj_term2;
obj1(iter) = sum_obj_term1;
obj2(iter) = sum_obj_term2;
obj3(iter) = sumsp;
obj4(iter) = sumsq;
cver = abs((obj(iter)-obji)/obji);
obji = obj(iter);
iter = iter + 1;
if ( cver < eps && iter > 2) || iter == 20, break, end
%% to update Sm
Sm = Sm_update;
end
if flagP == 1, plot(obj), end
end
%% to update Si for each row of each sample
function [Sm_update, Sp_value, Sq_value] = generate_Sm_update(Amv, label, Sm, sum_Amv, K, lambda)
[Sam_num,Time_num] = size(Amv);
ROI_num = size(Amv{1,1},1);
num_cluster = unique(label); % the cluster of label (-1,0,1)
num_class = length(num_cluster); % the number of class
% to divide the data by the label
for i = 1:num_class
temp1{i} = find(label == num_cluster(i));
mk(i) = length(temp1{i});
Smc{i} = Sm(temp1{i});
% Amvc{i} = Amv(temp1{i},:);
end
clear i
for i = 1:Sam_num
Sm_each_sam = Sm{i};
switch label(i)
case 1
[res_same, sum_sample_same, res_diff, sum_sample_diff] = nonzero_update(Sm_each_sam, label(i), num_cluster, mk, Smc, K);
case -1
[res_same, sum_sample_same, res_diff, sum_sample_diff] = nonzero_update(Sm_each_sam, label(i), num_cluster, mk, Smc, K);
case 0
[res_same, sum_sample_same, res_diff, sum_sample_diff] = zero_update(Sm_each_sam, Sm, K);
end
sum_same = sum(res_same,2);
sum_diff = sum(res_diff,2);
for j = 1:ROI_num
judge_hinge_term = 1 + sum_same(j) - sum_diff(j);
if judge_hinge_term > 0 %% judge > 0
v =(sum_Amv{i}(j,:) + (lambda+1) * sum_sample_same(j,:) - sum_sample_diff(j,:))./(Time_num + (lambda+1) * K - K);
Sm_each_update(j,:) = ProjectOntoSimplex(v, 1);
else %% judge <= 0
v =(sum_Amv{i}(j,:) + sum_sample_same(j,:))/(Time_num + K);
Sm_each_update(j,:) = ProjectOntoSimplex(v, 1);
end
clear v judge_hinge_term
end
Sp_value{i} = sum_same;
Sq_value{i} = sum_diff;
Sm_update{i} = (Sm_each_update + Sm_each_update')/2;
clear Sm_each_update sum_same sum_diff
% i
end
end
function [res_same, sum_sample_same, res_diff, sum_sample_diff] = nonzero_update(Sm_each_sam, labeli, num_cluster, mk, Smc, K)
ind_ini = find(num_cluster == labeli); % ind_ini--->ind_other: 1--->-1 or -1--->1
ind_other = find(num_cluster == labeli*-1);
ROI_num = size(Smc{1}{1},1);
Smc_ini = Smc{ind_ini};
Smc_other = Smc{ind_other};
% to calculate the distance of each row of each sample for the same class
for j1 = 1:ROI_num
for i1 = 1:mk(ind_ini)
dist_same(j1,i1) = EuDist2(Sm_each_sam(j1,:), Smc_ini{i1}(j1,:))^2;
end
[res_sam(j1,:),ind_sam(j1,:)] = sort(dist_same(j1,:));
res_same(j1,:) = res_sam(j1,2:K+1);
ind_same(j1,:) = ind_sam(j1,2:K+1);
sum_vector_same = zeros(1, ROI_num);
for i2 = 1:K
ind1 = ind_same(j1,i2);
sum_vector_same = sum_vector_same + Smc_ini{ind1}(j1,:);
end
sum_sample_same(j1,:) = sum_vector_same;
end
% to calculate the distance of each row of each sample for the different class
for j2 = 1:ROI_num
for i3 = 1:mk(ind_other)
dist_diff(j2,i3) = EuDist2(Sm_each_sam(j2,:), Smc_other{i3}(j2,:))^2;
end
[res_dif(j2,:),ind_dif(j2,:)] = sort(dist_diff(j2,:));
res_diff(j2,:) = res_dif(j2,2:K+1);
ind_diff(j2,:) = ind_dif(j2,2:K+1);
sum_vector_diff = zeros(1, ROI_num);
for i4 = 1:K
ind2 = ind_diff(j2,i4);
sum_vector_diff = sum_vector_diff + Smc_other{ind2}(j2,:);
end
sum_sample_diff(j2,:) = sum_vector_diff;
end
end
function [res_same, sum_sample_same, res_diff, sum_sample_diff] = zero_update(Sm_each_sam, Sm, K)
Sam_num = size(Sm,2);
ROI_num = size(Sm{1},1);
for i = 1:ROI_num
for j = 1:Sam_num
dist(i,j) = EuDist2(Sm_each_sam(i,:), Sm{j}(i,:))^2;
end
[res_all(i,:),ind_all(i,:)] = sort(dist(i,:));
% to select the K-nearest neighbor vector of each row of s_i among all samples
res_same(i,:) = res_all(i,2:K+1);
ind_same(i,:) = ind_all(i,2:K+1);
% to select the K-farthest neighbor vector of each row of s_i among all samples
res_diff(i,:) = res_all(i,Sam_num-K+1:Sam_num);
ind_diff(i,:) = ind_all(i,Sam_num-K+1:Sam_num);
% to calculate the sum of K-nearest neighobor vector of each row of s_i
sum_vector_same = zeros(1, ROI_num);
for z1 = 1:K
ind1 = ind_same(i,z1);
sum_vector_same = sum_vector_same + Sm{ind1}(i,:);
end
sum_sample_same(i,:) = sum_vector_same;
% to calculate the sum of K-farthest neighobor vector of each row of s_i
sum_vector_diff = zeros(1, ROI_num);
for z2 = 1:K
ind2 = ind_same(i,z2);
sum_vector_diff = sum_vector_diff + Sm{ind2}(i,:);
end
sum_sample_diff(i,:) = sum_vector_diff;
end
end