forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixDpGbOl.m
34 lines (34 loc) · 991 Bytes
/
mixDpGbOl.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
function [label, Theta, w, llh] = mixDpGbOl(X, alpha, theta)
% Online collapsed Gibbs sampling for Dirichlet process (infinite) mixture model.
% Input:
% X: d x n data matrix
% alpha: parameter for Dirichlet process prior
% theta: class object for prior of component distribution (such as Gauss)
% Output:
% label: 1 x n cluster label
% Theta: 1 x k structure of trained components
% w: 1 x k component weight vector
% llh: loglikelihood
% Written by Mo Chen ([email protected]).
n = size(X,2);
Theta = {};
nk = [];
label = zeros(1,n);
llh = 0;
for i = randperm(n)
x = X(:,i);
Pk = log(nk)+cellfun(@(t) t.logPredPdf(x), Theta);
P0 = log(alpha)+theta.logPredPdf(x);
p = [Pk,P0];
llh = llh+sum(p-log(n));
k = discreteRnd(exp(p-logsumexp(p)));
if k == numel(Theta)+1
Theta{k} = theta.clone().addSample(x);
nk = [nk,1];
else
Theta{k} = Theta{k}.addSample(x);
nk(k) = nk(k)+1;
end
label(i) = k;
end
w = nk/n;