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