working kinda?
This commit is contained in:
parent
bc30320eef
commit
97dc43c792
6 changed files with 54 additions and 21 deletions
BIN
assignments/hwk03/2a.png
Normal file
BIN
assignments/hwk03/2a.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 86 KiB |
|
@ -65,13 +65,8 @@ function [h, m, Q] = EMG(x, k, epochs, flag)
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% TODO: Store the value of the complete log-likelihood function
|
% TODO: Store the value of the complete log-likelihood function
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
L = 0;
|
Q(2*n - 1) = Q_step(x, m, S, k, pi, h);
|
||||||
% for i = 1:num_data
|
|
||||||
% for j = 1:k
|
|
||||||
% prior = mvnpdf(x, m(j, :), S(:, :, j));
|
|
||||||
% L = L + h(i, j) * (log(pi(i)) + log(prior(i)));
|
|
||||||
% end
|
|
||||||
% end
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%
|
||||||
% M-step
|
% M-step
|
||||||
|
@ -82,6 +77,7 @@ function [h, m, Q] = EMG(x, k, epochs, flag)
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% TODO: Store the value of the complete log-likelihood function
|
% TODO: Store the value of the complete log-likelihood function
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
Q(2*n) = Q_step(x, m, S, k, pi, h);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -20,18 +20,25 @@ function [S, m, pi] = M_step(x, h, S, k, flag)
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% update mixing coefficients
|
% update mixing coefficients
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
pi = zeros(k, 1);
|
|
||||||
N_i = zeros(k, 1);
|
N_i = zeros(k, 1);
|
||||||
|
m = zeros(k, dim);
|
||||||
for i = 1:k
|
for i = 1:k
|
||||||
N_i(i) = sum(h(:, i));
|
N_i(i) = sum(h(:, i));
|
||||||
|
for j = 1:num_data
|
||||||
|
m(i, :) = m(i, :) + h(j, i) * x(j, :);
|
||||||
|
end
|
||||||
end
|
end
|
||||||
pi = N_i / num_data;
|
pi = N_i / num_data;
|
||||||
|
|
||||||
|
for i = 1:k
|
||||||
|
m(i, :) = m(i, :) ./ N_i(i);
|
||||||
|
end
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% update cluster means
|
% update cluster means
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
m = zeros(k, dim);
|
% m = zeros(k, dim);
|
||||||
m = h' * x ./ N_i;
|
% m = h' * x ./ N_i;
|
||||||
% for i = 1:k
|
% for i = 1:k
|
||||||
% m(i, :) = sum(h(:, i) .* x(i, :)) / N_i(i);
|
% m(i, :) = sum(h(:, i) .* x(i, :)) / N_i(i);
|
||||||
% end
|
% end
|
||||||
|
@ -40,19 +47,24 @@ function [S, m, pi] = M_step(x, h, S, k, flag)
|
||||||
% Calculate the covariance matrix estimate
|
% Calculate the covariance matrix estimate
|
||||||
% further modifications will need to be made when doing 2(d)
|
% further modifications will need to be made when doing 2(d)
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
S = zeros(dim, dim, k);
|
S = zeros(dim, dim, k) + eps;
|
||||||
for i = 1:k
|
for i = 1:k
|
||||||
% for j = 1:num_data
|
s = zeros(dim, dim);
|
||||||
% S(:, :, i) = S(:, :, i) + h(j, i) * (x(j, :) - m(i, :)) * (x(j, :) - m(i, :))';
|
for j = 1:num_data
|
||||||
% end
|
s = s + h(j, i) * (x(j, :) - m(i, :))' * (x(j, :) - m(i, :));
|
||||||
s = (x - m(i, :))' * ((x - m(i, :)) .* h(:, i)) / N_i(i);
|
end
|
||||||
|
s = s / N_i(i);
|
||||||
|
% s = (x - m(i, :))' * ((x - m(i, :)) .* h(:, i)) / N_i(i);
|
||||||
|
|
||||||
% % MAKE IT SYMMETRIC https://stackoverflow.com/a/38730499
|
% % MAKE IT SYMMETRIC https://stackoverflow.com/a/38730499
|
||||||
% S(:, :, i) = (s + s') / 2;
|
% S(:, :, i) = (s + s') / 2;
|
||||||
% https://www.mathworks.com/matlabcentral/answers/366140-eig-gives-a-negative-eigenvalue-for-a-positive-semi-definite-matrix#answer_290270
|
% https://www.mathworks.com/matlabcentral/answers/366140-eig-gives-a-negative-eigenvalue-for-a-positive-semi-definite-matrix#answer_290270
|
||||||
s = (s + s') / 2;
|
s = (s + s') / 2;
|
||||||
|
% https://www.mathworks.com/matlabcentral/answers/57411-matlab-sometimes-produce-a-covariance-matrix-error-with-non-postive-semidefinite#answer_69524
|
||||||
[V, D] = eig(s);
|
[V, D] = eig(s);
|
||||||
S(:, :, i) = V * max(D,eps) / V;
|
s = V * max(D, eps) / V;
|
||||||
|
|
||||||
|
S(:, :, i) = s;
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
|
@ -45,6 +45,7 @@ function [] = Problem2()
|
||||||
c = repmat([1 0 0; 0 1 0], length(x)/2, 1);
|
c = repmat([1 0 0; 0 1 0], length(x)/2, 1);
|
||||||
scatter(x,Q,20,c);
|
scatter(x,Q,20,c);
|
||||||
index = index + 1;
|
index = index + 1;
|
||||||
|
pause;
|
||||||
end
|
end
|
||||||
shg
|
shg
|
||||||
|
|
||||||
|
@ -69,6 +70,7 @@ function [] = Problem2()
|
||||||
% TODO: plot goldy image after using clusters from k-means
|
% TODO: plot goldy image after using clusters from k-means
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
% begin code here
|
% begin code here
|
||||||
|
[~, ~, ~, D] = kmeans(goldy_x, k);
|
||||||
|
|
||||||
% end code here
|
% end code here
|
||||||
shg
|
shg
|
||||||
|
|
11
assignments/hwk03/Q_step.m
Normal file
11
assignments/hwk03/Q_step.m
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
function [LL] = Q_step(x, m, S, k, pi, h)
|
||||||
|
[num_data, ~] = size(x);
|
||||||
|
LL = 0;
|
||||||
|
for i = 1:k
|
||||||
|
N = mvnpdf(x, m(i, :), S(:, :, i));
|
||||||
|
for j = 1:num_data
|
||||||
|
LL = LL + h(j, i) * (log(pi(i) + eps) + log(N(j) + eps));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -67,3 +67,15 @@ Updates:
|
||||||
&= - sum_t (r^t-y^t) (x^t_j v_1 cases(0 "if" ww dot xx < 0, 1 "otherwise") + x^t_j v_2 (1 - tanh^2 (ww dot xx))) \
|
&= - sum_t (r^t-y^t) (x^t_j v_1 cases(0 "if" ww dot xx < 0, 1 "otherwise") + x^t_j v_2 (1 - tanh^2 (ww dot xx))) \
|
||||||
&= - sum_t (r^t-y^t) x^t_j (v_1 cases(0 "if" ww dot xx < 0, 1 "otherwise") + v_2 (1 - tanh^2 (ww dot xx))) \
|
&= - sum_t (r^t-y^t) x^t_j (v_1 cases(0 "if" ww dot xx < 0, 1 "otherwise") + v_2 (1 - tanh^2 (ww dot xx))) \
|
||||||
$
|
$
|
||||||
|
|
||||||
|
#pagebreak()
|
||||||
|
|
||||||
|
= Problem 2a + 2b
|
||||||
|
|
||||||
|
#image("2a.png")
|
||||||
|
|
||||||
|
= Problem 2c
|
||||||
|
|
||||||
|
= Problem 2d
|
||||||
|
|
||||||
|
MLE of $Sigma_i$
|
Loading…
Reference in a new issue