annotate toolboxes/FullBNT-1.0.7/netlabKPM/gmmem2.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, num_iter, ll] = gmmem_kpm(mix, x, varargin)
Daniel@0 2 %GMMEM_KPM Like GMMEM, but with additional optional arguments
Daniel@0 3 % function [mix, num_iter, ll] = gmmem_kpm(mix, x, varargin)
Daniel@0 4 %
Daniel@0 5 % Input:
Daniel@0 6 % mix - structure created by gmminit or gmmem_multi_restart
Daniel@0 7 % data - each row is an example
Daniel@0 8 %
Daniel@0 9 % Output:
Daniel@0 10 % mix - modified structure
Daniel@0 11 % num_iter - number of iterations needed to reach convergence
Daniel@0 12 % ll - final log likelihood
Daniel@0 13 %
Daniel@0 14 % [ ... ] = gmmem_kpm(..., 'param1',val1, 'param2',val2, ...) allows you to
Daniel@0 15 % specify optional parameter name/value pairs.
Daniel@0 16 % Parameters are below [default value in brackets]
Daniel@0 17 %
Daniel@0 18 % 'max_iter' - maximum number of EM iterations [10]
Daniel@0 19 % 'll_thresh' - change in log-likelihood threshold for convergence [1e-2]
Daniel@0 20 % 'verbose' - 1 means display output while running [0]
Daniel@0 21 % 'prior_cov' - this will be added to each estimated covariance
Daniel@0 22 % to prevent singularities [1e-3*eye(d)]
Daniel@0 23 % 'fn' - this function, if non-empty, will be called at every iteration
Daniel@0 24 % (e.g., to display the parameters as they evolve) [ [] ]
Daniel@0 25 % The fn is called as fn(mix, x, iter_num, fnargs).
Daniel@0 26 % It is also called before the iteration starts as
Daniel@0 27 % fn(mix, x, -1, fnargs), which can be used to initialize things.
Daniel@0 28 % 'fnargs' - additional arguments to be passed to fn [ {} ]
Daniel@0 29 %
Daniel@0 30 % Modified by Kevin P Murphy, 29 Dec 2002
Daniel@0 31
Daniel@0 32
Daniel@0 33 % Check that inputs are consistent
Daniel@0 34 errstring = consist(mix, 'gmm', x);
Daniel@0 35 if ~isempty(errstring)
Daniel@0 36 error(errstring);
Daniel@0 37 end
Daniel@0 38
Daniel@0 39 [ndata, xdim] = size(x);
Daniel@0 40
Daniel@0 41 [max_iter, ll_thresh, verbose, prior_cov, fn, fnargs] = ...
Daniel@0 42 process_options(varargin, ...
Daniel@0 43 'max_iter', 10, 'll_thresh', 1e-2, 'verbose', 1, ...
Daniel@0 44 'prior_cov', 1e-3*eye(xdim), 'fn', [], 'fnargs', {});
Daniel@0 45
Daniel@0 46 options = foptions;
Daniel@0 47 if verbose, options(1)=1; else options(1)=-1; end
Daniel@0 48 options(14) = max_iter;
Daniel@0 49 options(3) = ll_thresh;
Daniel@0 50
Daniel@0 51
Daniel@0 52 % Sort out the options
Daniel@0 53 if (options(14))
Daniel@0 54 niters = options(14);
Daniel@0 55 else
Daniel@0 56 niters = 100;
Daniel@0 57 end
Daniel@0 58
Daniel@0 59 display = options(1);
Daniel@0 60 test = 0;
Daniel@0 61 if options(3) > 0.0
Daniel@0 62 test = 1; % Test log likelihood for termination
Daniel@0 63 end
Daniel@0 64
Daniel@0 65 check_covars = 0;
Daniel@0 66 if options(5) >= 1
Daniel@0 67 if display >= 0
Daniel@0 68 disp('check_covars is on');
Daniel@0 69 end
Daniel@0 70 check_covars = 1; % Ensure that covariances don't collapse
Daniel@0 71 MIN_COVAR = eps; % Minimum singular value of covariance matrix
Daniel@0 72 init_covars = mix.covars;
Daniel@0 73 end
Daniel@0 74
Daniel@0 75 mix0 = mix; % save init values for debugging
Daniel@0 76
Daniel@0 77 if ~isempty(fn)
Daniel@0 78 feval(fn, mix, x, -1, fnargs{:});
Daniel@0 79 end
Daniel@0 80
Daniel@0 81 % Main loop of algorithm
Daniel@0 82 for n = 1:niters
Daniel@0 83
Daniel@0 84 % Calculate posteriors based on old parameters
Daniel@0 85 [post, act] = gmmpost(mix, x);
Daniel@0 86
Daniel@0 87 % Calculate error value if needed
Daniel@0 88 if (display | test)
Daniel@0 89 prob = act*(mix.priors)';
Daniel@0 90 % Error value is negative log likelihood of data
Daniel@0 91 e = - sum(log(prob + eps));
Daniel@0 92 if display > 0
Daniel@0 93 fprintf(1, 'Cycle %4d Error %11.6f\n', n, e);
Daniel@0 94 end
Daniel@0 95 if test
Daniel@0 96 if (n > 1 & abs(e - eold) < options(3))
Daniel@0 97 options(8) = e;
Daniel@0 98 ll = -e;
Daniel@0 99 num_iter = n;
Daniel@0 100 return; %%%%%%%%%%%%%%%% Exit here if converged
Daniel@0 101 else
Daniel@0 102 eold = e;
Daniel@0 103 end
Daniel@0 104 end
Daniel@0 105 end
Daniel@0 106
Daniel@0 107 if ~isempty(fn)
Daniel@0 108 feval(fn, mix, x, n, fnargs{:});
Daniel@0 109 end
Daniel@0 110
Daniel@0 111 % Adjust the new estimates for the parameters
Daniel@0 112 new_pr = sum(post, 1);
Daniel@0 113 new_c = post' * x;
Daniel@0 114
Daniel@0 115 % Now move new estimates to old parameter vectors
Daniel@0 116 mix.priors = new_pr ./ ndata;
Daniel@0 117
Daniel@0 118 mix.centres = new_c ./ (new_pr' * ones(1, mix.nin));
Daniel@0 119
Daniel@0 120 switch mix.covar_type
Daniel@0 121 case 'spherical'
Daniel@0 122 n2 = dist2(x, mix.centres);
Daniel@0 123 for j = 1:mix.ncentres
Daniel@0 124 v(j) = (post(:,j)'*n2(:,j));
Daniel@0 125 end
Daniel@0 126 mix.covars = ((v./new_pr) + sum(diag(prior_cov)))./mix.nin;
Daniel@0 127 if check_covars
Daniel@0 128 % Ensure that no covariance is too small
Daniel@0 129 for j = 1:mix.ncentres
Daniel@0 130 if mix.covars(j) < MIN_COVAR
Daniel@0 131 mix.covars(j) = init_covars(j);
Daniel@0 132 end
Daniel@0 133 end
Daniel@0 134 end
Daniel@0 135 case 'diag'
Daniel@0 136 for j = 1:mix.ncentres
Daniel@0 137 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 138 wts = (post(:,j)*ones(1, mix.nin));
Daniel@0 139 mix.covars(j,:) = sum((diffs.*diffs).*wts + prior_cov, 1)./new_pr(j);
Daniel@0 140 end
Daniel@0 141 if check_covars
Daniel@0 142 % Ensure that no covariance is too small
Daniel@0 143 for j = 1:mix.ncentres
Daniel@0 144 if min(mix.covars(j,:)) < MIN_COVAR
Daniel@0 145 mix.covars(j,:) = init_covars(j,:);
Daniel@0 146 end
Daniel@0 147 end
Daniel@0 148 end
Daniel@0 149 case 'full'
Daniel@0 150 for j = 1:mix.ncentres
Daniel@0 151 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 152 diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin));
Daniel@0 153 mix.covars(:,:,j) = (diffs'*diffs + prior_cov)/new_pr(j);
Daniel@0 154 end
Daniel@0 155 if check_covars
Daniel@0 156 % Ensure that no covariance is too small
Daniel@0 157 for j = 1:mix.ncentres
Daniel@0 158 if min(svd(mix.covars(:,:,j))) < MIN_COVAR
Daniel@0 159 mix.covars(:,:,j) = init_covars(:,:,j);
Daniel@0 160 end
Daniel@0 161 end
Daniel@0 162 end
Daniel@0 163 case 'ppca'
Daniel@0 164 for j = 1:mix.ncentres
Daniel@0 165 diffs = x - (ones(ndata, 1) * mix.centres(j,:));
Daniel@0 166 diffs = diffs.*(sqrt(post(:,j))*ones(1, mix.nin));
Daniel@0 167 [mix.covars(j), mix.U(:,:,j), mix.lambda(j,:)] = ...
Daniel@0 168 ppca((diffs'*diffs)/new_pr(j), mix.ppca_dim);
Daniel@0 169 end
Daniel@0 170 if check_covars
Daniel@0 171 if mix.covars(j) < MIN_COVAR
Daniel@0 172 mix.covars(j) = init_covars(j);
Daniel@0 173 end
Daniel@0 174 end
Daniel@0 175 otherwise
Daniel@0 176 error(['Unknown covariance type ', mix.covar_type]);
Daniel@0 177 end
Daniel@0 178 end
Daniel@0 179
Daniel@0 180 ll = sum(log(gmmprob(mix, x)));
Daniel@0 181 num_iter = n;
Daniel@0 182
Daniel@0 183 %if (display >= 0)
Daniel@0 184 % disp('Warning: Maximum number of iterations has been exceeded');
Daniel@0 185 %end
Daniel@0 186