annotate toolboxes/FullBNT-1.0.7/netlab3.3/gmmem.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [mix, options, errlog] = gmmem(mix, x, options)
Daniel@0 2 %GMMEM EM algorithm for Gaussian mixture model.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % [MIX, OPTIONS, ERRLOG] = GMMEM(MIX, X, OPTIONS) uses the Expectation
Daniel@0 6 % Maximization algorithm of Dempster et al. to estimate the parameters
Daniel@0 7 % of a Gaussian mixture model defined by a data structure MIX. The
Daniel@0 8 % matrix X represents the data whose expectation is maximized, with
Daniel@0 9 % each row corresponding to a vector. The optional parameters have
Daniel@0 10 % the following interpretations.
Daniel@0 11 %
Daniel@0 12 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 13 % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then
Daniel@0 14 % only warning messages are displayed. If OPTIONS(1) is -1, then
Daniel@0 15 % nothing is displayed.
Daniel@0 16 %
Daniel@0 17 % OPTIONS(3) is a measure of the absolute precision required of the
Daniel@0 18 % error function at the solution. If the change in log likelihood
Daniel@0 19 % between two steps of the EM algorithm is less than this value, then
Daniel@0 20 % the function terminates.
Daniel@0 21 %
Daniel@0 22 % OPTIONS(5) is set to 1 if a covariance matrix is reset to its
Daniel@0 23 % original value when any of its singular values are too small (less
Daniel@0 24 % than MIN_COVAR which has the value eps). With the default value of
Daniel@0 25 % 0 no action is taken.
Daniel@0 26 %
Daniel@0 27 % OPTIONS(14) is the maximum number of iterations; default 100.
Daniel@0 28 %
Daniel@0 29 % The optional return value OPTIONS contains the final error value
Daniel@0 30 % (i.e. data log likelihood) in OPTIONS(8).
Daniel@0 31 %
Daniel@0 32 % See also
Daniel@0 33 % GMM, GMMINIT
Daniel@0 34 %
Daniel@0 35
Daniel@0 36 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 37
Daniel@0 38 % Check that inputs are consistent
Daniel@0 39 errstring = consist(mix, 'gmm', x);
Daniel@0 40 if ~isempty(errstring)
Daniel@0 41 error(errstring);
Daniel@0 42 end
Daniel@0 43
Daniel@0 44 [ndata, xdim] = size(x);
Daniel@0 45
Daniel@0 46 % Sort out the options
Daniel@0 47 if (options(14))
Daniel@0 48 niters = options(14);
Daniel@0 49 else
Daniel@0 50 niters = 100;
Daniel@0 51 end
Daniel@0 52
Daniel@0 53 display = options(1);
Daniel@0 54 store = 0;
Daniel@0 55 if (nargout > 2)
Daniel@0 56 store = 1; % Store the error values to return them
Daniel@0 57 errlog = zeros(1, niters);
Daniel@0 58 end
Daniel@0 59 test = 0;
Daniel@0 60 if options(3) > 0.0
Daniel@0 61 test = 1; % Test log likelihood for termination
Daniel@0 62 end
Daniel@0 63
Daniel@0 64 check_covars = 0;
Daniel@0 65 if options(5) >= 1
Daniel@0 66 if display >= 0
Daniel@0 67 disp('check_covars is on');
Daniel@0 68 end
Daniel@0 69 check_covars = 1; % Ensure that covariances don't collapse
Daniel@0 70 MIN_COVAR = eps; % Minimum singular value of covariance matrix
Daniel@0 71 init_covars = mix.covars;
Daniel@0 72 end
Daniel@0 73
Daniel@0 74 % Main loop of algorithm
Daniel@0 75 for n = 1:niters
Daniel@0 76
Daniel@0 77 % Calculate posteriors based on old parameters
Daniel@0 78 [post, act] = gmmpost(mix, x);
Daniel@0 79
Daniel@0 80 % Calculate error value if needed
Daniel@0 81 if (display | store | test)
Daniel@0 82 prob = act*(mix.priors)';
Daniel@0 83 % Error value is negative log likelihood of data
Daniel@0 84 e = - sum(log(prob));
Daniel@0 85 if store
Daniel@0 86 errlog(n) = e;
Daniel@0 87 end
Daniel@0 88 if display > 0
Daniel@0 89 fprintf(1, 'Cycle %4d Error %11.6f\n', n, e);
Daniel@0 90 end
Daniel@0 91 if test
Daniel@0 92 if (n > 1 & abs(e - eold) < options(3))
Daniel@0 93 options(8) = e;
Daniel@0 94 return;
Daniel@0 95 else
Daniel@0 96 eold = e;
Daniel@0 97 end
Daniel@0 98 end
Daniel@0 99 end
Daniel@0 100
Daniel@0 101 % Adjust the new estimates for the parameters
Daniel@0 102 new_pr = sum(post, 1);
Daniel@0 103 new_c = post' * x;
Daniel@0 104
Daniel@0 105 % Now move new estimates to old parameter vectors
Daniel@0 106 mix.priors = new_pr ./ ndata;
Daniel@0 107
Daniel@0 108 mix.centres = new_c ./ (new_pr' * ones(1, mix.nin));
Daniel@0 109
Daniel@0 110 switch mix.covar_type
Daniel@0 111 case 'spherical'
Daniel@0 112 n2 = dist2(x, mix.centres);
Daniel@0 113 for j = 1:mix.ncentres
Daniel@0 114 v(j) = (post(:,j)'*n2(:,j));
Daniel@0 115 end
Daniel@0 116 mix.covars = ((v./new_pr))./mix.nin;
Daniel@0 117 if check_covars
Daniel@0 118 % Ensure that no covariance is too small
Daniel@0 119 for j = 1:mix.ncentres
Daniel@0 120 if mix.covars(j) < MIN_COVAR
Daniel@0 121 mix.covars(j) = init_covars(j);
Daniel@0 122 end
Daniel@0 123 end
Daniel@0 124 end
Daniel@0 125 case 'diag'
Daniel@0 126 for j = 1:mix.ncentres
Daniel@0 127 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 128 mix.covars(j,:) = sum((diffs.*diffs).*(post(:,j)*ones(1, ...
Daniel@0 129 mix.nin)), 1)./new_pr(j);
Daniel@0 130 end
Daniel@0 131 if check_covars
Daniel@0 132 % Ensure that no covariance is too small
Daniel@0 133 for j = 1:mix.ncentres
Daniel@0 134 if min(mix.covars(j,:)) < MIN_COVAR
Daniel@0 135 mix.covars(j,:) = init_covars(j,:);
Daniel@0 136 end
Daniel@0 137 end
Daniel@0 138 end
Daniel@0 139 case 'full'
Daniel@0 140 for j = 1:mix.ncentres
Daniel@0 141 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 142 diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin));
Daniel@0 143 mix.covars(:,:,j) = (diffs'*diffs)/new_pr(j);
Daniel@0 144 end
Daniel@0 145 if check_covars
Daniel@0 146 % Ensure that no covariance is too small
Daniel@0 147 for j = 1:mix.ncentres
Daniel@0 148 if min(svd(mix.covars(:,:,j))) < MIN_COVAR
Daniel@0 149 mix.covars(:,:,j) = init_covars(:,:,j);
Daniel@0 150 end
Daniel@0 151 end
Daniel@0 152 end
Daniel@0 153 case 'ppca'
Daniel@0 154 for j = 1:mix.ncentres
Daniel@0 155 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 156 diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin));
Daniel@0 157 [tempcovars, tempU, templambda] = ...
Daniel@0 158 ppca((diffs'*diffs)/new_pr(j), mix.ppca_dim);
Daniel@0 159 if length(templambda) ~= mix.ppca_dim
Daniel@0 160 error('Unable to extract enough components');
Daniel@0 161 else
Daniel@0 162 mix.covars(j) = tempcovars;
Daniel@0 163 mix.U(:, :, j) = tempU;
Daniel@0 164 mix.lambda(j, :) = templambda;
Daniel@0 165 end
Daniel@0 166 end
Daniel@0 167 if check_covars
Daniel@0 168 if mix.covars(j) < MIN_COVAR
Daniel@0 169 mix.covars(j) = init_covars(j);
Daniel@0 170 end
Daniel@0 171 end
Daniel@0 172 otherwise
Daniel@0 173 error(['Unknown covariance type ', mix.covar_type]);
Daniel@0 174 end
Daniel@0 175 end
Daniel@0 176
Daniel@0 177 options(8) = -sum(log(gmmprob(mix, x)));
Daniel@0 178 if (display >= 0)
Daniel@0 179 disp(maxitmess);
Daniel@0 180 end
Daniel@0 181