csci5521/assignments/hwk03/E_step.m

48 lines
1.4 KiB
Mathematica
Raw Normal View History

2023-11-10 03:29:17 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name: E_step.m
% Input: x - a nxd matrix (nx3 if using RGB)
% Q - vector of values from the complete data log-likelihood function
% h - a nxk matrix, the expectation of the hidden variable z given the data set and distribution params
2023-11-15 15:53:18 +00:00
% pi - vector of mixing coefficients
2023-11-10 03:29:17 +00:00
% m - cluster means
% S - cluster covariance matrices
% k - the number of clusters
2023-11-15 15:53:18 +00:00
% Output: h - a nxk matrix, the expectation of the hidden variable z given the data set and distribution params
2023-11-10 03:29:17 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023-11-15 15:53:18 +00:00
function [h] = E_step(x, h, pi, m, S, k)
2023-11-10 03:29:17 +00:00
2023-11-12 17:42:19 +00:00
[num_data, ~] = size(x);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023-11-16 08:46:02 +00:00
% perform E-step of EM algorithm
2023-11-12 17:42:19 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2023-11-15 15:53:18 +00:00
parts = zeros(num_data, k);
2023-11-16 08:46:02 +00:00
for i = 1:k
parts(:, i) = pi(i) * mvnpdf(x, m(i, :), S(:, :, i));
2023-11-15 15:53:18 +00:00
end
s = sum(parts);
2023-11-16 08:46:02 +00:00
for j = 1:num_data
h(j, :) = parts(j, :) ./ s;
2023-11-15 15:53:18 +00:00
end
2023-11-10 03:29:17 +00:00
2023-11-18 08:40:46 +00:00
% parts = zeros(k);
%
% denom = 0;
% for i = 1:k
% N = mvnpdf(x, m(i, :), S(:, :, i));
% for j = 1:num_data
% parts(i) = parts(i) + pi(i) * N(j);
% end
% denom = denom + parts(i);
% end
%
% for i = 1:k
% h(:, i) = parts(i) ./ denom;
% end
2023-11-10 03:29:17 +00:00
end