-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdate_coordinates.m
More file actions
348 lines (255 loc) · 12.6 KB
/
Update_coordinates.m
File metadata and controls
348 lines (255 loc) · 12.6 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
function [coor_xi, coor_eta, currentParticle] = Update_coordinates(TESTFLAG, currentParticle, coor_xi, coor_eta, datas, dataNum, bPara)
% Update the coordinates
% TESTFLAG: indicate if we need to calculate the AUC and test log-likelihood
% currentParticle: the current structure of particle
% coor_xi, coor_eta: row and column coordinates for all the nodes
% datas, dataNum, bPara: defined as previously
% use Metropolis-Hasting Algorithm to update the nodes' coordinates
points = currentParticle.points;
kdtree = currentParticle.kdtree;
nodeNum = currentParticle.nodeNum;
%
%
% model paramters
z_label = currentParticle.z_label{(nodeNum-1)/2};
%
%
%
numClass = (nodeNum+1)/2;
tau1_kl = zeros(1, numClass);
tau0_kl = zeros(1, numClass);
for ii = 1:numClass
ii_data = datas(z_label == ii);
tau1_kl(ii) = sum(sum(ii_data==1));
tau0_kl(ii) = sum(sum(ii_data==0));
end
%% First, we need to check out the available points
if nodeNum > 1
addIndex = kdtree(2:nodeNum, 1)+3; % addIndex denote the index of the TRUE added points
addPoints = points(addIndex, :); % addedPoints denote the coordiantes of the added points
fore_x_coor = addPoints((1:2:end), 1);
back_x_coor = addPoints((2:2:end), 1);
fore_y_coor = addPoints((1:2:end), 2);
back_y_coor = addPoints((2:2:end), 2);
y_coor = addPoints(:, 2);
x_coor = addPoints(:, 1);
y_coor(y_coor == 0) = [];
y_coor(y_coor == 1) = [];
x_coor(x_coor == 0) = [];
x_coor(x_coor == 1) = [];
y_coor = sort(y_coor);
x_coor = sort(x_coor);
% identify the blocks' class
[cate_x, cate_y] = block_identify( currentParticle, y_coor, x_coor, fore_x_coor, back_x_coor, fore_y_coor, back_y_coor );
for ii = randperm(dataNum)
%& First sampling \coor_xi_ii
propo_coor_eta = rand;
origin_ii1 = hist((z_label(ii, datas(ii, :)==1)), 1:numClass);
origin_ii0 = hist((z_label(ii, datas(ii, :)==0)), 1:numClass);
tau1_kl_ori = tau1_kl - origin_ii1;
tau0_kl_ori = tau0_kl - origin_ii0;
% calculate the proposal_coor_xi's value
forward_sel = (((propo_coor_eta-fore_y_coor).*(propo_coor_eta-back_y_coor))<0); %%% this is for the x coordinate's calculation
if sum(forward_sel)>0
% the cut point on the vertical direction
propo_x_coor = back_x_coor(forward_sel) + ((propo_coor_eta-back_y_coor(forward_sel)).*(fore_x_coor(forward_sel)-...
back_x_coor(forward_sel)))./(fore_y_coor(forward_sel)-back_y_coor(forward_sel));
propo_x_coor = sort(propo_x_coor);
else
propo_x_coor = [];
end
coor_etaLoc = zeros(1, numel(coor_xi));
for jj = 1:numel(coor_xi)
coor_etaLoc(jj) = sum(coor_xi(jj)>(propo_x_coor))+1;
end
x_spec = sum(propo_coor_eta>y_coor)+1;
tt_cate_x = cate_x(x_spec, coor_etaLoc);
% start to define the original categories
% calculate the original coor_xi's value
forward_sel = (((coor_eta(ii)-fore_y_coor).*(coor_eta(ii)-back_y_coor))<0); %%% this is for the x coordinate's calculation
if sum(forward_sel)>0
% the cut point on the vertical direction
propo_x_coor = back_x_coor(forward_sel) + ((coor_eta(ii)-back_y_coor(forward_sel)).*(fore_x_coor(forward_sel)-...
back_x_coor(forward_sel)))./(fore_y_coor(forward_sel)-back_y_coor(forward_sel));
propo_x_coor = sort(propo_x_coor);
else
propo_x_coor = [];
end
coor_etaLoc = zeros(1, numel(coor_xi));
for jj = 1:numel(coor_xi)
coor_etaLoc(jj) = sum(coor_xi(jj)>(propo_x_coor))+1;
end
x_spec = sum(coor_eta(ii)>y_coor)+1;
origin_tt_cate_x = cate_x(x_spec, coor_etaLoc);
%
propo_ii1 = hist((tt_cate_x.*(datas(ii, :)==1)), 0:numClass);
propo_ii1(1) = [];
propo_ii0 = hist((tt_cate_x.*(datas(ii, :)==0)), 0:numClass);
propo_ii0(1) = [];
tau1_propo = tau1_kl_ori + propo_ii1;
tau0_propo = tau0_kl_ori + propo_ii0;
if (sum(tau1_propo<0)>0)||(sum(tau0_propo<0)>0)
fprintf('xi_sampling wrong, propro\n');
end
if log(rand)<(sum(gammaln(bPara(1)+tau1_propo)+gammaln(bPara(2)+...
tau0_propo)-gammaln(bPara(1)+tau1_propo+bPara(2)+tau0_propo)-...
gammaln(bPara(1)+tau1_kl)-gammaln(bPara(2)+tau0_kl)+...
gammaln(bPara(1)+tau1_kl+bPara(2)+tau0_kl)))
tau1_kl = tau1_propo;
tau0_kl = tau0_propo;
coor_eta(ii) = propo_coor_eta;
z_label(ii, :) = tt_cate_x;
else
z_label(ii, :) = origin_tt_cate_x;
propo_ii1 = hist((origin_tt_cate_x.*(datas(ii, :)==1)), 0:numClass);
propo_ii1(1) = [];
propo_ii0 = hist((origin_tt_cate_x.*(datas(ii, :)==0)), 0:numClass);
propo_ii0(1) = [];
tau1_kl = tau1_kl_ori + propo_ii1;
tau0_kl = tau0_kl_ori + propo_ii0;
end
%% Then sampling \coor_eta_ii
propo_coor_xi = rand;
% delete the current statistics from Bk1, Bk0
origin_ii1 = hist((z_label(datas(:, ii)==1, ii)), 1:numClass);
origin_ii0 = hist((z_label(datas(:, ii)==0, ii)), 1:numClass);
tau1_kl_ori = tau1_kl - origin_ii1;
tau0_kl_ori = tau0_kl - origin_ii0;
% calculate the proposal_coor_eta's value
backward_sel = ((propo_coor_xi-fore_x_coor).*((propo_coor_xi-back_x_coor))<0); %%% this is for the y coordinate's calculation
if sum(backward_sel)>0
% the cut point on the horizonal direction
propo_y_coor = back_y_coor(backward_sel) + ((propo_coor_xi-back_x_coor(backward_sel)).*(fore_y_coor(backward_sel)...
-back_y_coor(backward_sel)))./(fore_x_coor(backward_sel)-back_x_coor(backward_sel));
propo_y_coor = sort(propo_y_coor);
else
propo_y_coor = [];
end
coor_xiLoc = zeros(1, numel(coor_eta));
for jj = 1:numel(coor_eta)
coor_xiLoc(jj) = sum(coor_eta(jj)>(propo_y_coor))+1;
end
x_spec = sum(propo_coor_xi>x_coor)+1;
tt_cate_y = cate_y(x_spec, coor_xiLoc);
% start to define the original column
backward_sel = ((coor_xi(ii)-fore_x_coor).*((coor_xi(ii)-back_x_coor))<0); %%% this is for the y coordinate's calculation
if sum(backward_sel)>0
% the cut point on the horizonal direction
propo_y_coor = back_y_coor(backward_sel) + ((coor_xi(ii)-back_x_coor(backward_sel)).*(fore_y_coor(backward_sel)...
-back_y_coor(backward_sel)))./(fore_x_coor(backward_sel)-back_x_coor(backward_sel));
propo_y_coor = sort(propo_y_coor);
else
propo_y_coor = [];
end
coor_xiLoc = zeros(1, numel(coor_eta));
for jj = 1:numel(coor_eta)
coor_xiLoc(jj) = sum(coor_eta(jj)>(propo_y_coor))+1;
end
x_spec = sum(coor_xi(ii)>x_coor)+1;
origin_tt_cate_y = cate_y(x_spec, coor_xiLoc);
%
propo_ii1 = hist((tt_cate_y'.*(datas(:, ii)==1)), 0:numClass);
propo_ii0 = hist((tt_cate_y'.*(datas(:, ii)==0)), 0:numClass);
propo_ii1(1) = [];
propo_ii0(1) = [];
tau1_propo = tau1_kl_ori + propo_ii1;
tau0_propo = tau0_kl_ori + propo_ii0;
if (sum(tau1_propo<0)>0)||(sum(tau0_propo<0)>0)
fprintf('eta_sampling wrong, propo\n');
end
if log(rand)<(sum(gammaln(bPara(1)+tau1_propo)+gammaln(bPara(2)+...
tau0_propo)-gammaln(bPara(1)+tau1_propo+bPara(2)+tau0_propo)-...
gammaln(bPara(1)+tau1_kl)-gammaln(bPara(2)+tau0_kl)+...
gammaln(bPara(1)+tau1_kl+bPara(2)+tau0_kl)))
tau1_kl = tau1_propo;
tau0_kl = tau0_propo;
coor_xi(ii) = propo_coor_xi;
z_label(:, ii) = tt_cate_y';
else
z_label(:, ii) = origin_tt_cate_y';
propo_ii1 = hist((origin_tt_cate_y'.*(datas(:, ii)==1)), 0:numClass);
propo_ii0 = hist((origin_tt_cate_y'.*(datas(:, ii)==0)), 0:numClass);
propo_ii1(1) = [];
propo_ii0(1) = [];
tau1_kl = tau1_kl_ori + propo_ii1;
tau0_kl = tau0_kl_ori + propo_ii0;
end
end
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%% Start to calculate the AUC value, which is critical
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
if TESTFLAG==1
tau_kl = tau1_kl + tau0_kl;
linkProb = (tau1_kl+bPara(1))./(tau_kl+sum(bPara));
testIndex = currentParticle.testIndex;
testData = currentParticle.testData;
predictProb = zeros(1, numel(testIndex));
for aucii = 1:numel(testIndex)
iiRow = mod(testIndex(aucii), dataNum);
if (iiRow==0)
iiRow = dataNum;
end
iiColumn = ceil(testIndex(aucii)/dataNum);
ii_coor_eta = coor_eta(iiRow);
ii_coor_xi = coor_xi(iiColumn);
%%% From the tt_cate_y perspective
% calculate the proposal_coor_eta's value
backward_sel = ((ii_coor_xi-fore_x_coor).*((ii_coor_xi-back_x_coor))<0); %%% this is for the y coordinate's calculation
if sum(backward_sel)>0
% the cut point on the horizonal direction
propo_y_coor = back_y_coor(backward_sel) + ((ii_coor_xi-back_x_coor(backward_sel)).*(fore_y_coor(backward_sel)...
-back_y_coor(backward_sel)))./(fore_x_coor(backward_sel)-back_x_coor(backward_sel));
propo_y_coor = sort(propo_y_coor);
else
propo_y_coor = [];
end
coor_xiLoc = sum(ii_coor_eta>(propo_y_coor))+1;
x_spec = sum(ii_coor_xi>x_coor)+1;
tt_cate_y = cate_y(x_spec, coor_xiLoc);
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
predictProb(aucii) = linkProb(tt_cate_y);
end
n1 = sum(testData(:));
no = numel(testData)-n1;
[~, rank_indcs] = sort(predictProb);
R_sorted = testData(rank_indcs);
So = sum(find(R_sorted > 0));
if ((So - (n1*(n1+1))/2)<0)||(n1<0)||(no<0)
a = 1;
fprintf('error. \n');
end
aucValue = (So - (n1*(n1+1))/2)/(n1*no);
else
aucValue = 0.5;
end
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%% End the auc value calculation here
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
%%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%% %%%%%%%%
else
z_label = ones(dataNum, dataNum);
aucValue = 0.5;
end
tau_kl = tau1_kl + tau0_kl;
probs = (tau1_kl+bPara(1))./(tau_kl+sum(bPara));
trainll = 0;
for ii = 1:numClass
ii_data = datas(z_label == ii);
trainll = trainll + sum(ii_data==1)*log(probs(ii))+sum(ii_data==0)*log(1-probs(ii));
end
%
currentParticle.tau1_kl = tau1_kl;
currentParticle.tau0_kl = tau0_kl;
currentParticle.trainll = trainll;
currentParticle.aucValue = aucValue;
currentParticle.z_label = z_label_determine(currentParticle, coor_eta, coor_xi, points, kdtree, nodeNum, dataNum);
end