-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgmm_demo.m
177 lines (121 loc) · 4.37 KB
/
gmm_demo.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
% --------------------------------------
% Gausssian Mixture Model demos
% written by Richard Xu
% [email protected], July, 2014
% made it Octave-friendly
% --------------------------------------
clc;
clear;
% ------------------------------------------------------
% Synthetically generate data from nominated parameters
% ------------------------------------------------------
% N is the number of data
N = 1000;
gt_alpha = [0.3 0.3 0.4];
% M is K, means the number of clusters
M = length(gt_alpha);
% Sample Z ~ Mult(N,gt_alpha)
Z = [];
% drawing from a multinomial distribution, parameterised by gt_alpha
% R1 = mnrnd(N,gt_alpha);
R = mnrnd(N, gt_alpha);
for t = 1:M
Z = [Z; ones([R(t) 1])*t];
end
% Simulate data from ground truth N ( theta_{z_1},....,theta_{z_n}
mu = [ -3 4; 0 0; 3 4];
Sigma(:,:,1) = [ 2 0; 0 1];
Sigma(:,:,2) = [ 2 0; 0 1];
Sigma(:,:,3) = [ 2 0; 0 3];
% data is X = {x_1, .... x_n}
data = zeros([N 2]);
for t = 1:M
comp_size = length(find(Z == t));
data(Z == t,:) = mvnrnd(mu(t,:), Sigma(:,:,t), comp_size);
plot(data(:,1),data(:,2),'o');
end
% -------------------------------------------------
% E-M algorithm
% -------------------------------------------------
% initialize, i.e., nominate Theta^(1)
prev_mu = [ -5 -1; -1 -1; 4 -1];
prev_Sigma(:,:,1) = [ 1 0; 0 1];
prev_Sigma(:,:,2) = [ 1 0; 0 1];
prev_Sigma(:,:,3) = [ 1 0; 0 1];
prev_alpha = ones([M 1])/M;
% the iteration number
MaxIter = 20;
response = zeros([N M]);
next_mu = zeros(size(prev_mu));
next_Sigma = zeros(size(prev_Sigma));
d = size(data,2);
% -----------------------------
% The EM loop
% -----------------------------
for t = 1:MaxIter
if t > 1
% -----------------------------
% Compute the responsibilities
% -----------------------------
for l = 1:M
response(:,l) = mvnpdf(data,prev_mu(l,:), prev_Sigma(:,:,l));
end
response_sum = sum(response,2);
response = response ./ repmat(response_sum,[1 3]);
% ---------------------------
% update alpha^(i+1)
% ---------------------------
next_alpha = sum(response)'/N;
% ---------------------------
% update mu^(i+1)
% ---------------------------
for l = 1:M
next_mu(l,:) = sum(data .* repmat(response(:,l),[1 d])) / sum(response(:,l));
end
% ---------------------------
% update Sigma^(i+1)
% ---------------------------
for l = 1:M
zero_mean_data = data - repmat(next_mu(l,:),[N 1]);
%zero_mean_3d = reshape(zero_mean_data,[N 1 d]);
covariances = zeros([d d N]);
for i = 1:N
covariances(:,:,i) = zero_mean_data(i,:)' * zero_mean_data(i,:) * response(i,l);
end
next_Sigma(:,:,l) = sum(covariances,3)/sum(response(:,l));
end
prev_mu = next_mu;
prev_Sigma = next_Sigma;
prev_alpha = next_alpha;
else
% t = 1, just use initial parameters
next_mu = prev_mu;
next_Sigma = prev_Sigma;
next_alpha = prev_alpha;
end
% -----------------------------
% plot GMM
% -----------------------------
figure(1);
plot(data(:,1), data(:,2), 'o');
axis([min(data(:,1)) max(data(:,1)) min(data(:,2)) max(data(:,2))]);
hold on;
gmm_pdf = gmdistribution(next_mu,next_Sigma,next_alpha);
ezcontour(@(u,v)pdf(gmm_pdf,[u v]));
% -----------------------------------------------
% Octave-friendly code
% -----------------------------------------------
% x1 = min(data(:,1)):0.1:max(data(:,1));
% x2 = min(data(:,2)):0.1:max(data(:,2));
% [X1,X2] = meshgrid(x1,x2);
% F = next_alpha(1) * mvnpdf([X1(:) X2(:)],next_mu(1,:),next_Sigma(:,:,1));
% for l = 2:M
% F = F+ next_alpha(l) * mvnpdf([X1(:) X2(:)],next_mu(l,:),next_Sigma(:,:,l));
% end
% F = reshape(F,length(x2),length(x1));
% contour(x1,x2,F);
axis([min(data(:,1)) max(data(:,1)) min(data(:,2)) max(data(:,2))]);
hold off;
% this is to ask user to press a button
waitforbuttonpress;
end