-
Notifications
You must be signed in to change notification settings - Fork 8
/
vblapsmm.m
342 lines (313 loc) · 10.3 KB
/
vblapsmm.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
%% Laplacian regularized Student's t mixture model using variational Inference
%
% @author Junhao HUA
% Create Time: 2013-1-12
%
% references:
% Svensen,M.Bishop,Bohust Bayesian Mixture Modelling,2004
%
%%
function [label,model,logLRange] = vblapsmm(Data,K,option)
%% parameters Description:
% K the number of mixing components
% Data all observed data (dim*N)
% N the number of data
% dim the Dimension of data
% Alpha the weight vector of each Gaussian (1 x K)
% Mu the mean vector of each Gaussian (dim x K)
% Sigma the Covariance matrix of each Gaussian (dim x dim x K)
%
% the initialization of prior superparameters
% They can be set to small positive numbers to give
% broad prior distrbutions indicating ignorance about the prior
% Dirichlet Distribution Parameters:
% alpha0 1
% Wishart Distribution Parameters:
% invW0 dim x dim
% v0 1
% Gaussian Distribution Parameters:
% m0 dim x 1
% beta0 1
% the latent variable
% the probability of each point in each component
% R N x K
% the parameters of latent variable u of Gamma distribution
% v 1 x K
[dim,N] = size(Data);
prior = struct('alpha0',1e-3,'m0',zeros(dim,1),'beta0',1e-3,'invW0',eye(dim,dim),'v0',dim+1);
p =option.p;
lambda = option.lambda;
gamma = 0.9;
W = CalcWeightbyknn(Data,p);
DCol = full(sum(W,2));
D = spdiags(DCol,0,N,N);
L = D-W;
maxtimes = 1000;
logLRange = -inf(1,maxtimes);
logL = -realmax;
model = Initvb(Data,K);
model = MaxStep(Data,model,prior);
% label = cell(N,1);
esp = 1e-4;
t = 0;
while t < maxtimes
t = t+1;
logL0 = logL;
while 1
logL0 = logL;
model.R = SmoothPosterior2(W,model.R,gamma); % laplacian process
model.R(model.R<realmin) = realmin;
model.logR = log(model.R);
[model,flag] = MaxStep(Data,model,prior);
if ~flag
break;
end
model = updateU(model,Data);
llnew = vbound(Data,model,prior);
laploss = sum(sum(model.R'*L.*model.R'));
logL = (llnew - lambda*laploss)/N;
%fprintf('%e %e %e %f\n',llnew,lambda*laploss,logL,gamma);
if(logL < logL0)
gamma = 0.9*gamma;
else
break;
end
end
logLRange(t) = logL;
% label{t} = label0;
%fprintf('%e %e \n',abs(logL-logL0),esp*abs(logL));
if abs(logL-logL0) < esp*abs(logL) || t >80 ||~flag
break;
end
model = ExpectStep(Data,model);
end
[~,label] = max(model.R,[],2);
logLRange = logLRange(1:t);
disp(['Total iteratons:',num2str(t)]);
function [model,flag] = MaxStep(data,model,prior)
%%
% update the statistics
% avgN 1 x K
% avgX dim x K
% avgS dim x dim x K
% update the superparameters
% the parameter of weight(pi) (1 x K):
% alpha 1 x K
% the parameters of preision (dim x dim x K):
% invW dim x dim x K inv(W)
% V 1 x K
% the parameters of mean:
% M dim x K
% beta 1 x K
% the Gamma parameters of the latent variable U
% Uv 1 x K
[~,K] = size(model.R);
ReU = model.R.*model.eU;
sumR = sum(model.R);
aXn = data*ReU;
avgN = sum(ReU);
avgX = bsxfun(@times,aXn,1./avgN);
% wishart
ws = (prior.beta0*avgN)./(prior.beta0+avgN);
sqrtRU = sqrt(ReU);
for i = 1:K
avgNS = bsxfun(@times,bsxfun(@minus,data,avgX(:,i)),sqrtRU(:,i)');
Xkm0 = avgX(:,i)-prior.m0;
model.invW(:,:,i) = prior.invW0 +avgNS*avgNS'+ ws(i).*(Xkm0*Xkm0');
end
model.V = prior.v0 + sumR;
% dirichlet
model.alpha = prior.alpha0 + sumR;
% gaussian
model.beta = prior.beta0 + avgN;
model.M = bsxfun(@times,bsxfun(@plus,prior.beta0.*prior.m0,aXn),1./model.beta);
% non-linear equations: newton method
sumR(sumR < realmin) = realmin;
tmp = dot(model.elogU-model.eU,model.R)./sumR;
flag = true;
if(max(tmp)>=-(1+1e-3))
flag = false;
return;
end
model.Uv = zeros(1,K);
for i=1:K
model.Uv(i) = fzero(@(x)1+tmp(i)+log(x/2)-psi(0,x/2),[1e-5 1e5]); % init parameter ?
end
function model = ExpectStep(data,model)
%%
% update the moments of parameters
% EQ the expectation of Covariance matrix N x K
% E_logLambda the log expectation of precision 1 x K
% E_logPi the log expectation of the mixing proportion of the mixture components 1 x K
%
% latent variable
% R N x K
% <U>,<logU>
[dim,N] = size(data);
[~,K] = size(model.M);
EQ = zeros(N,K);
logW = zeros(1,K);
for i=1:K
U = chol(model.invW(:,:,i));
logW(i) = -2*sum(log(diag(U)));
Q = U'\bsxfun(@minus,data,model.M(:,i));
EQ(:,i) = dim/model.beta(i) + model.V(i)*dot(Q,Q,1); % N x 1
end
E_logLambda = sum(psi(0,bsxfun(@minus,model.V+1,(1:dim)')/2),1) + dim*log(2)+logW; % - + ?
E_logPi = psi(0,model.alpha) - psi(0,sum(model.alpha)); % 1 x K
% update latent parameter: R
logRho = bsxfun(@plus,dim*model.elogU-model.eU.*EQ,2*E_logPi + E_logLambda -dim*log(2*pi))/2;
model.logR = bsxfun(@minus,logRho,logsumexp(logRho,2));
model.R = exp(model.logR);
%disp(sum(model.R));
a = 1/2*bsxfun(@plus,model.Uv,dim*model.R);
b = 1/2*bsxfun(@plus,model.Uv,model.R.*EQ); %N x K
model.a = a;
model.b = b;
% update <U>,<logU>
model.eU = a./b;
model.elogU = psi(0,a)-log(b);
function model = updateU(model,data)
%%
% update hyperparameters of latent parmeter U:
[dim,N] = size(data);
[~,K] = size(model.M);
EQ = zeros(N,K);
for i=1:K
U = chol(model.invW(:,:,i));
Q = U'\bsxfun(@minus,data,model.M(:,i));
EQ(:,i) = dim/model.beta(i) + model.V(i)*dot(Q,Q,1); % N x 1
end
a = 1/2*bsxfun(@plus,model.Uv,dim*model.R);
b = 1/2*bsxfun(@plus,model.Uv,model.R.*EQ); %N x K
model.a = a;
model.b = b;
% update <U>,<logU>
model.eU = a./b;
model.elogU = psi(0,a)-log(b);
function model =Initvb(data,K)
%%
[~,N] = size(data);
[IDX,~] = kmeans(data',K,'emptyaction','drop','start','uniform');
R0 = zeros(N,K);
for i = 1:K
R0(:,i) = IDX == i;
end
model.R = R0;
tmp = sum(R0);
model.eU = repmat(tmp./sum(tmp),N,1);
model.elogU = log(model.eU);
function L = vbound(X, model, prior)
%% stopping criterion
alpha0 = prior.alpha0;
beta0 = prior.beta0;
m0 = prior.m0;
v0 = prior.v0;
invW0 = prior.invW0;
% Dirichlet
alpha = model.alpha;
% Gaussian
beta = model.beta;
m = model.M;
% Whishart
v = model.V;
invW = model.invW; %inv(W) = V'*V
% gamma
Uv =model.Uv;
a = model.a; % N x k
%
R = model.R;
logR = model.logR;
eU = model.eU;
elogU = model.elogU;
[N,~] =size(R);
[dim,k] = size(m);
sumR = sum(R,1);
ReU = model.R.*model.eU;
avgN = sum(ReU);
Elogpi = psi(0,alpha)-psi(0,sum(alpha));
E_pz = dot(sumR,Elogpi); %10.72 / 6
E_qz = dot(R(:),logR(:)); %10.75 / 11
logCoefDir0 = gammaln(k*alpha0)-k*gammaln(alpha0); % the coefficient of Dirichlet Distribution
E_ppi = logCoefDir0+(alpha0-1)*sum(Elogpi); %10.73 / 5
logCoefDir = gammaln(sum(alpha))-sum(gammaln(alpha));
E_qpi = dot(alpha-1,Elogpi)+logCoefDir; %10.76 / 10
U0 = chol(invW0);
xbar = bsxfun(@times,X*ReU,1./avgN); % 10.52
logW = zeros(1,k);
trM0W = zeros(1,k);
xbaruLambadxbaru = zeros(1,k);
mm0Wmm0 = zeros(1,k);
for i = 1:k
U = chol(invW(:,:,i));
logW(i) = -2*sum(log(diag(U)));
Q = U0/U;
trM0W(i) = dot(Q(:),Q(:));
q = U'\(xbar(:,i)-m(:,i));
xbaruLambadxbaru(i) = dim/beta(i)+v(i).*dot(q,q);
q = U'\(m(:,i)-m0);
mm0Wmm0(i) = dot(q,q);
end
ElogLambda = sum(psi(0,bsxfun(@minus,v+1,(1:dim)')/2),1)+dim*log(2)+logW; % 10.65 -+ ?
E_pX = 0.5*sum(dot(R,bsxfun(@plus,ElogLambda-dim*log(2*pi),dim*elogU-bsxfun(@times,eU,xbaruLambadxbaru)))); %10.71 / 1
Epmu = sum(dim*log(beta0/(2*pi))-beta0.*xbaruLambadxbaru)/2;
EpLambda = 0.5*sum((v0-dim+1).*ElogLambda-dot(v,trM0W),2);
E_logpMu_Lambda = Epmu + EpLambda; % 10.74 /2 3
logB = -v.*(logW+dim*log(2))/2-logmvgamma(0.5*v,dim);
E_logqMu_Lambda = sum(dim*log(v./(2*pi))+logB+dim+(v-dim).*ElogLambda+v.*dim,2)/2;
Uv2 = Uv/2;
E_logpu = sum(N*(Uv2.*log(Uv2)-gammaln(Uv2))+(Uv2-1).*sum(elogU)-Uv2.*sum(eU),2); % / 4
E_logqu = sum(sum(a.*(psi(a)-1)-gammaln(a)-elogU),2); % / 9
L = E_pX+E_pz+E_ppi+E_logpMu_Lambda+E_logpu-E_qz-E_qpi-E_logqMu_Lambda-E_logqu;
function pkx = SmoothPosterior2(W,pkx,gamma)
%%
if_iterator =1;
[nSmp,k] = size(pkx);
DCol = full(sum(W,2));
S = spdiags(DCol.^-1,0,nSmp,nSmp)*W;
if if_iterator
F = pkx;
relaF = 1;
esp = 1e-3;
t =0;
while max(max(relaF)) > esp
t = t+1;
for j=1:199
F = (1-gamma)*F + gamma*W*F./repmat(DCol,1,k);
end
Fold = F;
F = (1-gamma)*F + gamma*W*F./repmat(DCol,1,k);
if t > 15
break;
end
F(F<realmin) = realmin;
relaF = abs( Fold - F)./F;
%fprintf('%f \n',max(max(relaF)));
end
else
T = speye(size(W,1)) - gamma*S;
T = T/(1-gamma);
F = T\pkx;
if min(min(F)) < 0
F = max(0,F);
%error('negative!');
end
end
pkx = F;
function W = CalcWeightbyknn(x,p)
%%
%@input:
% x(data) dim x N
% p the number of nearest neighbors
%@output:
% sparse matrix (N*N)
% W
% IDX N x p
[~,nSmp] = size(x);
[IDX,~] = knnsearch(x',x','K',p+1,'NSMethod','kdtree','distance','euclidean');
% Weight functoin: euclidean distance
D = ones(nSmp,p+1);
a = reshape(repmat((1:1:nSmp),1,p),nSmp*p,1);
b = double(reshape(IDX(:,2:end),nSmp*p,1));
s = reshape(D(:,2:end),nSmp*p,1);
W = sparse(a,b,s,nSmp,nSmp);