
function [loglik] = mixloglik(X,y,mix)
%
% function [loglik] = mixloglik(X,y,mix)
%
% evaluates the log-likelihood of each data point and the label
% for a Gaussian mixture model stored in 'mix'. 

[n,d] = size(X);

loglik = zeros(size(y));

for i=1:2,
  I = find(y==i-1); % i-1 corresponds to 0/1 label
  loglik(I) = log(mix{i}.p) - 0.5*log(det(mix{i}.cov))-0.5*d*log(2*pi); 

  Xm = X(I,:)-repmat(mix{i}.mu,length(I),1); % subtract the mean
  loglik(I) = loglik(I) -0.5*sum((Xm*mix{i}.icov).*Xm,2); 
end;