annotate toolboxes/FullBNT-1.0.7/netlab3.3/gmminit.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 = gmminit(mix, x, options)
Daniel@0 2 %GMMINIT Initialises Gaussian mixture model from data
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % MIX = GMMINIT(MIX, X, OPTIONS) uses a dataset X to initialise the
Daniel@0 6 % parameters of a Gaussian mixture model defined by the data structure
Daniel@0 7 % MIX. The k-means algorithm is used to determine the centres. The
Daniel@0 8 % priors are computed from the proportion of examples belonging to each
Daniel@0 9 % cluster. The covariance matrices are calculated as the sample
Daniel@0 10 % covariance of the points associated with (i.e. closest to) the
Daniel@0 11 % corresponding centres. For a mixture of PPCA model, the PPCA
Daniel@0 12 % decomposition is calculated for the points closest to a given centre.
Daniel@0 13 % This initialisation can be used as the starting point for training
Daniel@0 14 % the model using the EM algorithm.
Daniel@0 15 %
Daniel@0 16 % See also
Daniel@0 17 % GMM
Daniel@0 18 %
Daniel@0 19
Daniel@0 20 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 21
Daniel@0 22 [ndata, xdim] = size(x);
Daniel@0 23
Daniel@0 24 % Check that inputs are consistent
Daniel@0 25 errstring = consist(mix, 'gmm', x);
Daniel@0 26 if ~isempty(errstring)
Daniel@0 27 error(errstring);
Daniel@0 28 end
Daniel@0 29
Daniel@0 30 % Arbitrary width used if variance collapses to zero: make it 'large' so
Daniel@0 31 % that centre is responsible for a reasonable number of points.
Daniel@0 32 GMM_WIDTH = 1.0;
Daniel@0 33
Daniel@0 34 % Use kmeans algorithm to set centres
Daniel@0 35 options(5) = 1;
Daniel@0 36 [mix.centres, options, post] = kmeansNetlab(mix.centres, x, options);
Daniel@0 37
Daniel@0 38 % Set priors depending on number of points in each cluster
Daniel@0 39 cluster_sizes = max(sum(post, 1), 1); % Make sure that no prior is zero
Daniel@0 40 mix.priors = cluster_sizes/sum(cluster_sizes); % Normalise priors
Daniel@0 41
Daniel@0 42 switch mix.covar_type
Daniel@0 43 case 'spherical'
Daniel@0 44 if mix.ncentres > 1
Daniel@0 45 % Determine widths as distance to nearest centre
Daniel@0 46 % (or a constant if this is zero)
Daniel@0 47 cdist = dist2(mix.centres, mix.centres);
Daniel@0 48 cdist = cdist + diag(ones(mix.ncentres, 1)*realmax);
Daniel@0 49 mix.covars = min(cdist);
Daniel@0 50 mix.covars = mix.covars + GMM_WIDTH*(mix.covars < eps);
Daniel@0 51 else
Daniel@0 52 % Just use variance of all data points averaged over all
Daniel@0 53 % dimensions
Daniel@0 54 mix.covars = mean(diag(cov(x)));
Daniel@0 55 end
Daniel@0 56 case 'diag'
Daniel@0 57 for j = 1:mix.ncentres
Daniel@0 58 % Pick out data points belonging to this centre
Daniel@0 59 c = x(find(post(:, j)),:);
Daniel@0 60 diffs = c - (ones(size(c, 1), 1) * mix.centres(j, :));
Daniel@0 61 mix.covars(j, :) = sum((diffs.*diffs), 1)/size(c, 1);
Daniel@0 62 % Replace small entries by GMM_WIDTH value
Daniel@0 63 mix.covars(j, :) = mix.covars(j, :) + GMM_WIDTH.*(mix.covars(j, :)<eps);
Daniel@0 64 end
Daniel@0 65 case 'full'
Daniel@0 66 for j = 1:mix.ncentres
Daniel@0 67 % Pick out data points belonging to this centre
Daniel@0 68 c = x(find(post(:, j)),:);
Daniel@0 69 diffs = c - (ones(size(c, 1), 1) * mix.centres(j, :));
Daniel@0 70 mix.covars(:,:,j) = (diffs'*diffs)/(size(c, 1));
Daniel@0 71 % Add GMM_WIDTH*Identity to rank-deficient covariance matrices
Daniel@0 72 if rank(mix.covars(:,:,j)) < mix.nin
Daniel@0 73 mix.covars(:,:,j) = mix.covars(:,:,j) + GMM_WIDTH.*eye(mix.nin);
Daniel@0 74 end
Daniel@0 75 end
Daniel@0 76 case 'ppca'
Daniel@0 77 for j = 1:mix.ncentres
Daniel@0 78 % Pick out data points belonging to this centre
Daniel@0 79 c = x(find(post(:,j)),:);
Daniel@0 80 diffs = c - (ones(size(c, 1), 1) * mix.centres(j, :));
Daniel@0 81 [tempcovars, tempU, templambda] = ...
Daniel@0 82 ppca((diffs'*diffs)/size(c, 1), mix.ppca_dim);
Daniel@0 83 if length(templambda) ~= mix.ppca_dim
Daniel@0 84 error('Unable to extract enough components');
Daniel@0 85 else
Daniel@0 86 mix.covars(j) = tempcovars;
Daniel@0 87 mix.U(:, :, j) = tempU;
Daniel@0 88 mix.lambda(j, :) = templambda;
Daniel@0 89 end
Daniel@0 90 end
Daniel@0 91 otherwise
Daniel@0 92 error(['Unknown covariance type ', mix.covar_type]);
Daniel@0 93 end
Daniel@0 94