annotate toolboxes/FullBNT-1.0.7/KPMstats/mixgauss_init.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 [mu, Sigma, weights] = mixgauss_init(M, data, cov_type, method)
Daniel@0 2 % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians
Daniel@0 3 % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method)
Daniel@0 4 %
Daniel@0 5 % INPUTS:
Daniel@0 6 % data(:,t) is the t'th example
Daniel@0 7 % M = num. mixture components
Daniel@0 8 % cov_type = 'full', 'diag' or 'spherical'
Daniel@0 9 % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab)
Daniel@0 10 %
Daniel@0 11 % OUTPUTS:
Daniel@0 12 % mu(:,k)
Daniel@0 13 % Sigma(:,:,k)
Daniel@0 14 % weights(k)
Daniel@0 15
Daniel@0 16 if nargin < 4, method = 'kmeans'; end
Daniel@0 17
Daniel@0 18 [d T] = size(data);
Daniel@0 19 data = reshape(data, d, T); % in case it is data(:, t, sequence_num)
Daniel@0 20
Daniel@0 21 switch method
Daniel@0 22 case 'rnd',
Daniel@0 23 C = cov(data');
Daniel@0 24 Sigma = repmat(diag(diag(C))*0.5, [1 1 M]);
Daniel@0 25 % Initialize each mean to a random data point
Daniel@0 26 indices = randperm(T);
Daniel@0 27 mu = data(:,indices(1:M));
Daniel@0 28 weights = normalise(ones(M,1));
Daniel@0 29 case 'kmeans',
Daniel@0 30 mix = gmm(d, M, cov_type);
Daniel@0 31 options = foptions;
Daniel@0 32 max_iter = 5;
Daniel@0 33 options(1) = -1; % be quiet!
Daniel@0 34 options(14) = max_iter;
Daniel@0 35 mix = gmminit(mix, data', options);
Daniel@0 36 mu = reshape(mix.centres', [d M]);
Daniel@0 37 weights = mix.priors(:);
Daniel@0 38 for m=1:M
Daniel@0 39 switch cov_type
Daniel@0 40 case 'diag',
Daniel@0 41 Sigma(:,:,m) = diag(mix.covars(m,:));
Daniel@0 42 case 'full',
Daniel@0 43 Sigma(:,:,m) = mix.covars(:,:,m);
Daniel@0 44 case 'spherical',
Daniel@0 45 Sigma(:,:,m) = mix.covars(m) * eye(d);
Daniel@0 46 end
Daniel@0 47 end
Daniel@0 48 end
Daniel@0 49