Daniel@0: function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type, method) Daniel@0: % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians Daniel@0: % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method) Daniel@0: % Daniel@0: % INPUTS: Daniel@0: % data(:,t) is the t'th example Daniel@0: % M = num. mixture components Daniel@0: % cov_type = 'full', 'diag' or 'spherical' Daniel@0: % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab) Daniel@0: % Daniel@0: % OUTPUTS: Daniel@0: % mu(:,k) Daniel@0: % Sigma(:,:,k) Daniel@0: % weights(k) Daniel@0: Daniel@0: if nargin < 4, method = 'kmeans'; end Daniel@0: Daniel@0: [d T] = size(data); Daniel@0: data = reshape(data, d, T); % in case it is data(:, t, sequence_num) Daniel@0: Daniel@0: switch method Daniel@0: case 'rnd', Daniel@0: C = cov(data'); Daniel@0: Sigma = repmat(diag(diag(C))*0.5, [1 1 M]); Daniel@0: % Initialize each mean to a random data point Daniel@0: indices = randperm(T); Daniel@0: mu = data(:,indices(1:M)); Daniel@0: weights = normalise(ones(M,1)); Daniel@0: case 'kmeans', Daniel@0: mix = gmm(d, M, cov_type); Daniel@0: options = foptions; Daniel@0: max_iter = 5; Daniel@0: options(1) = -1; % be quiet! Daniel@0: options(14) = max_iter; Daniel@0: mix = gmminit(mix, data', options); Daniel@0: mu = reshape(mix.centres', [d M]); Daniel@0: weights = mix.priors(:); Daniel@0: for m=1:M Daniel@0: switch cov_type Daniel@0: case 'diag', Daniel@0: Sigma(:,:,m) = diag(mix.covars(m,:)); Daniel@0: case 'full', Daniel@0: Sigma(:,:,m) = mix.covars(:,:,m); Daniel@0: case 'spherical', Daniel@0: Sigma(:,:,m) = mix.covars(m) * eye(d); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: