Daniel@0: function [mix, options, errlog] = gmmem(mix, x, options) Daniel@0: %GMMEM EM algorithm for Gaussian mixture model. Daniel@0: % Daniel@0: % Description Daniel@0: % [MIX, OPTIONS, ERRLOG] = GMMEM(MIX, X, OPTIONS) uses the Expectation Daniel@0: % Maximization algorithm of Dempster et al. to estimate the parameters Daniel@0: % of a Gaussian mixture model defined by a data structure MIX. The Daniel@0: % matrix X represents the data whose expectation is maximized, with Daniel@0: % each row corresponding to a vector. The optional parameters have Daniel@0: % the following interpretations. Daniel@0: % Daniel@0: % OPTIONS(1) is set to 1 to display error values; also logs error Daniel@0: % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then Daniel@0: % only warning messages are displayed. If OPTIONS(1) is -1, then Daniel@0: % nothing is displayed. Daniel@0: % Daniel@0: % OPTIONS(3) is a measure of the absolute precision required of the Daniel@0: % error function at the solution. If the change in log likelihood Daniel@0: % between two steps of the EM algorithm is less than this value, then Daniel@0: % the function terminates. Daniel@0: % Daniel@0: % OPTIONS(5) is set to 1 if a covariance matrix is reset to its Daniel@0: % original value when any of its singular values are too small (less Daniel@0: % than MIN_COVAR which has the value eps). With the default value of Daniel@0: % 0 no action is taken. Daniel@0: % Daniel@0: % OPTIONS(14) is the maximum number of iterations; default 100. Daniel@0: % Daniel@0: % The optional return value OPTIONS contains the final error value Daniel@0: % (i.e. data log likelihood) in OPTIONS(8). Daniel@0: % Daniel@0: % See also Daniel@0: % GMM, GMMINIT Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Check that inputs are consistent Daniel@0: errstring = consist(mix, 'gmm', x); Daniel@0: if ~isempty(errstring) Daniel@0: error(errstring); Daniel@0: end Daniel@0: Daniel@0: [ndata, xdim] = size(x); Daniel@0: Daniel@0: % Sort out the options Daniel@0: if (options(14)) Daniel@0: niters = options(14); Daniel@0: else Daniel@0: niters = 100; Daniel@0: end Daniel@0: Daniel@0: display = options(1); Daniel@0: store = 0; Daniel@0: if (nargout > 2) Daniel@0: store = 1; % Store the error values to return them Daniel@0: errlog = zeros(1, niters); Daniel@0: end Daniel@0: test = 0; Daniel@0: if options(3) > 0.0 Daniel@0: test = 1; % Test log likelihood for termination Daniel@0: end Daniel@0: Daniel@0: check_covars = 0; Daniel@0: if options(5) >= 1 Daniel@0: if display >= 0 Daniel@0: disp('check_covars is on'); Daniel@0: end Daniel@0: check_covars = 1; % Ensure that covariances don't collapse Daniel@0: MIN_COVAR = eps; % Minimum singular value of covariance matrix Daniel@0: init_covars = mix.covars; Daniel@0: end Daniel@0: Daniel@0: % Main loop of algorithm Daniel@0: for n = 1:niters Daniel@0: Daniel@0: % Calculate posteriors based on old parameters Daniel@0: [post, act] = gmmpost(mix, x); Daniel@0: Daniel@0: % Calculate error value if needed Daniel@0: if (display | store | test) Daniel@0: prob = act*(mix.priors)'; Daniel@0: % Error value is negative log likelihood of data Daniel@0: e = - sum(log(prob)); Daniel@0: if store Daniel@0: errlog(n) = e; Daniel@0: end Daniel@0: if display > 0 Daniel@0: fprintf(1, 'Cycle %4d Error %11.6f\n', n, e); Daniel@0: end Daniel@0: if test Daniel@0: if (n > 1 & abs(e - eold) < options(3)) Daniel@0: options(8) = e; Daniel@0: return; Daniel@0: else Daniel@0: eold = e; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Adjust the new estimates for the parameters Daniel@0: new_pr = sum(post, 1); Daniel@0: new_c = post' * x; Daniel@0: Daniel@0: % Now move new estimates to old parameter vectors Daniel@0: mix.priors = new_pr ./ ndata; Daniel@0: Daniel@0: mix.centres = new_c ./ (new_pr' * ones(1, mix.nin)); Daniel@0: Daniel@0: switch mix.covar_type Daniel@0: case 'spherical' Daniel@0: n2 = dist2(x, mix.centres); Daniel@0: for j = 1:mix.ncentres Daniel@0: v(j) = (post(:,j)'*n2(:,j)); Daniel@0: end Daniel@0: mix.covars = ((v./new_pr))./mix.nin; Daniel@0: if check_covars Daniel@0: % Ensure that no covariance is too small Daniel@0: for j = 1:mix.ncentres Daniel@0: if mix.covars(j) < MIN_COVAR Daniel@0: mix.covars(j) = init_covars(j); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: case 'diag' Daniel@0: for j = 1:mix.ncentres Daniel@0: diffs = x - (ones(ndata, 1) * mix.centres(j,:)); Daniel@0: mix.covars(j,:) = sum((diffs.*diffs).*(post(:,j)*ones(1, ... Daniel@0: mix.nin)), 1)./new_pr(j); Daniel@0: end Daniel@0: if check_covars Daniel@0: % Ensure that no covariance is too small Daniel@0: for j = 1:mix.ncentres Daniel@0: if min(mix.covars(j,:)) < MIN_COVAR Daniel@0: mix.covars(j,:) = init_covars(j,:); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: case 'full' Daniel@0: for j = 1:mix.ncentres Daniel@0: diffs = x - (ones(ndata, 1) * mix.centres(j,:)); Daniel@0: diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin)); Daniel@0: mix.covars(:,:,j) = (diffs'*diffs)/new_pr(j); Daniel@0: end Daniel@0: if check_covars Daniel@0: % Ensure that no covariance is too small Daniel@0: for j = 1:mix.ncentres Daniel@0: if min(svd(mix.covars(:,:,j))) < MIN_COVAR Daniel@0: mix.covars(:,:,j) = init_covars(:,:,j); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: case 'ppca' Daniel@0: for j = 1:mix.ncentres Daniel@0: diffs = x - (ones(ndata, 1) * mix.centres(j,:)); Daniel@0: diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin)); Daniel@0: [tempcovars, tempU, templambda] = ... Daniel@0: ppca((diffs'*diffs)/new_pr(j), mix.ppca_dim); Daniel@0: if length(templambda) ~= mix.ppca_dim Daniel@0: error('Unable to extract enough components'); Daniel@0: else Daniel@0: mix.covars(j) = tempcovars; Daniel@0: mix.U(:, :, j) = tempU; Daniel@0: mix.lambda(j, :) = templambda; Daniel@0: end Daniel@0: end Daniel@0: if check_covars Daniel@0: if mix.covars(j) < MIN_COVAR Daniel@0: mix.covars(j) = init_covars(j); Daniel@0: end Daniel@0: end Daniel@0: otherwise Daniel@0: error(['Unknown covariance type ', mix.covar_type]); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: options(8) = -sum(log(gmmprob(mix, x))); Daniel@0: if (display >= 0) Daniel@0: disp(maxitmess); Daniel@0: end Daniel@0: