From b444304a89c6c55727aeaa7f6794829f894caea4 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 15 Nov 2023 22:35:25 -0600 Subject: [PATCH] wtf --- assignments/hwk03/EMG.m | 16 ++++++++-------- assignments/hwk03/M_step.m | 11 +++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/assignments/hwk03/EMG.m b/assignments/hwk03/EMG.m index c4fe80c..93a1749 100644 --- a/assignments/hwk03/EMG.m +++ b/assignments/hwk03/EMG.m @@ -22,12 +22,12 @@ function [h, m, Q] = EMG(x, k, epochs, flag) Q = zeros(epochs*2,1); % vector that can hold complete data log-likelihood after each E and M step %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: Initialise cluster means using k-means + % Initialise cluster means using k-means %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% [~, ~, ~, D] = kmeans(x, k); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: Determine the b values for all data points + % Determine the b values for all data points %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for i = 1:num_data row = D(i,:); @@ -36,7 +36,7 @@ function [h, m, Q] = EMG(x, k, epochs, flag) end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: Initialize pi's (mixing coefficients) + % Initialize pi's (mixing coefficients) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pi = zeros(k, 1); for i = 1:k @@ -44,8 +44,8 @@ function [h, m, Q] = EMG(x, k, epochs, flag) end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: Initialize the covariance matrix estimate - % further modifications will need to be made when doing 2(d) + % Initialize the covariance matrix estimate + % further modifications will need to be made when doing 2(d) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% m = zeros(k, dim); for i = 1:k @@ -63,13 +63,13 @@ function [h, m, Q] = EMG(x, k, epochs, flag) [h] = E_step(x, h, pi, m, S, k); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: Store the value of the complete log-likelihood function + % Store the value of the complete log-likelihood function %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% L = 0; 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))); + L = L + h(i, j) * (log(pi(j)) + log(prior(j))); end end @@ -77,7 +77,7 @@ function [h, m, Q] = EMG(x, k, epochs, flag) % M-step %%%%%%%%%%%%%%%% fprintf('M-step, epoch #%d\n', n); - [Q, S, m] = M_step(x, Q, h, S, k); + [Q, S, m] = M_step(x, h, S, k, flag); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % TODO: Store the value of the complete log-likelihood function diff --git a/assignments/hwk03/M_step.m b/assignments/hwk03/M_step.m index 97105e8..e205eec 100644 --- a/assignments/hwk03/M_step.m +++ b/assignments/hwk03/M_step.m @@ -18,9 +18,16 @@ function [S, m, pi] = M_step(x, h, S, k, flag) lambda = 1e-3; % value for improved version of EM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - % TODO: update mixing coefficients + % update mixing coefficients %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - + pi = zeros(k, 1); + for i = 1:num_data + row = h(i, :); + maxValue = max(row); + maxIdx = find(row == maxValue); + pi(maxIdx) = pi(maxIdx) + 1; + end + pi = pi ./ num_data; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%