-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans.m
182 lines (164 loc) · 4.5 KB
/
kmeans.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
function [beta, alpha, theta, subgroup, BIC, timecost] = kmeans(X, Z, Y, train_idx, valid_idx, theta_check, W, real_K)
% fprintf('Initializing..\n');
if nargin >= 5
X_val = X(valid_idx);
Z_val = Z(valid_idx);
Y_val = Y(valid_idx);
X = X(train_idx);
Z = Z(train_idx);
Y = Y(train_idx);
if nargin >= 7
theta_check_val = theta_check(valid_idx,:);
W_val = W(valid_idx);
theta_check = theta_check(train_idx,:);
W = W(train_idx);
end
end
timecost = zeros(1,6);
M = size(X,2);
p = size(X{1},2);
q = size(Z{1},2);
n = zeros(M,1);
for i=1:M
n(i) = size(X{i},1);
end
N = sum(n);
% fprintf('M = %d, p = %d, q = %d, N = %d\n', M, p, q, sum(n(:)));
if nargin<5 % calculating theta_check, W
% fprintf('Calculating W_i..\n');
W = cell(1,M);
big_Z = zeros(sum(n), M*q);
long_Z = zeros(sum(n), q);
long_X = zeros(sum(n), p);
long_Y = zeros(sum(n),1);
G = zeros(sum(n),1);
for i=1:M
big_Z(1+sum(n(1:i-1)):sum(n(1:i)), 1+(i-1)*q:i*q) = Z{i};
long_Z(1+sum(n(1:i-1)):sum(n(1:i)), :) = Z{i};
long_X(1+sum(n(1:i-1)):sum(n(1:i)), :) = X{i};
long_Y(1+sum(n(1:i-1)):sum(n(1:i))) = Y{i};
G(1+sum(n(1:i-1)):sum(n(1:i))) = i;
end
lme = fitlmematrix([long_X, big_Z], long_Y, long_Z, G, 'CovariancePattern', 'Isotropic','FitMethod','REML');
[psi, sigma] = covarianceParameters(lme);
for i=1:M
W{i} = (sigma*eye(n(i))+Z{i}*psi{1}*Z{i}')\eye(n(i));
end
% fprintf('Initialization done.\n');
%% Calculate check parameters
% fprintf('Step 1: Calculate check parameters.\n');
beta_U = zeros(M, p);
theta_check = zeros(M, q);
Var_big = cell(1,M);
tic;
for i=1:M
T = [X{i},Z{i}];
Var_big{i} = T'*W{i}*T;
check = Var_big{i} \ T'*W{i}*Y{i};
beta_U(i,:) = check(1:p);
theta_check(i,:) = check(p+1:end);
end
timecost(1) = toc;
% fprintf('Step 1 done. Timecost: %.6fs\n',timecost(1));
end
%% K-means
% BIC tuning
min_BIC = Inf;
% fprintf('Step 3: K-means\n');
long_Y = zeros(sum(n),1);
for i=1:M
long_Y(1+sum(n(1:i-1)):sum(n(1:i))) = Y{i};
end
theta_K = zeros(M, q);
if nargin >= 8
K_list = real_K;
else
if M > 10
K_list = 1:5;
else
K_list = 1:M;
end
end
tic;
for K=K_list
% initial
fprintf('K = %d\n',K);
centroids = theta_check(randperm(M,K),:);
subgroup = zeros(1,M);
dist = zeros(1,K);
for m=1:M
for k=1:K
diff = centroids-theta_check(m*ones(1,K),:);
dist(k) = norm(diff(k,:));
end
[~, subgroup(m)] = min(dist);
end
old_subgroup = zeros(1,M);
i = 0;
while sum(old_subgroup~=subgroup) && i<1000
i = i+1;
% Maximization
for k=1:K
centroids(k,:) = mean(theta_check(subgroup==k,:),1);
end
% Expectation
old_subgroup = subgroup;
for m=1:M
for k=1:K
diff = centroids-theta_check(m*ones(1,K),:);
dist(k) = norm(diff(k,:));
end
[~, subgroup(m)] = min(dist);
end
end
% translate the subgroups
index = 1:M;
subgroup_K = cell(1,K);
for k=1:K
subgroup_K{k} = index(subgroup==k);
end
subgroup_K(cellfun(@isempty,subgroup_K))=[];
%% Calculate beta and alpha
S = size(subgroup_K, 2);
G = zeros(sum(n), p+S*q);
for s=1:S
for i=subgroup_K{s}
offset = sum(n(1:i-1))+1:sum(n(1:i));
G(offset, 1:p) = X{i};
G(offset, p+(s-1)*q+1:p+s*q) = Z{i};
end
end
W{1} = sparse(W{1});
W_big = blkdiag(W{:});
estimate = (G'*W_big*G) \ G'*W_big*long_Y;
beta_K = estimate(1:p);
alpha_K = reshape(estimate(p+1:end), q,S);
alpha_K = alpha_K';
for s=1:S
for i=subgroup_K{s}
theta_K(i,:) = alpha_K(s,:);
end
end
theta_val = zeros(size(theta_check_val));
subgroup_val = cell(1,S);
for s=1:S
subgroup_val{s} = [];
end
[~, theta_val] = estimate_groups(subgroup_val, alpha_K, theta_check_val);
BIC = bic(X_val, Y_val, Z_val, beta_K', theta_val, K);
% fprintf('BIC: %.4f\n', BIC);
if BIC<min_BIC
subgroup_best = subgroup_K;
beta = beta_K';
theta = theta_K;
alpha = alpha_K;
min_BIC = BIC;
end
end
timecost(2) = toc;
subgroup = subgroup_best;
BIC = min_BIC;
% fprintf('Best K: %d\n', best_K);
timecost = sum(timecost(2:6));
fprintf('Total time cost: %.6fs\n', timecost);
end