comparison toolboxes/FullBNT-1.0.7/netlab3.3/gmm.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function mix = gmm(dim, ncentres, covar_type, ppca_dim)
2 %GMM Creates a Gaussian mixture model with specified architecture.
3 %
4 % Description
5 % MIX = GMM(DIM, NCENTRES, COVARTYPE) takes the dimension of the space
6 % DIM, the number of centres in the mixture model and the type of the
7 % mixture model, and returns a data structure MIX. The mixture model
8 % type defines the covariance structure of each component Gaussian:
9 % 'spherical' = single variance parameter for each component: stored as a vector
10 % 'diag' = diagonal matrix for each component: stored as rows of a matrix
11 % 'full' = full matrix for each component: stored as 3d array
12 % 'ppca' = probabilistic PCA: stored as principal components (in a 3d array
13 % and associated variances and off-subspace noise
14 % MIX = GMM(DIM, NCENTRES, COVARTYPE, PPCA_DIM) also sets the
15 % dimension of the PPCA sub-spaces: the default value is one.
16 %
17 % The priors are initialised to equal values summing to one, and the
18 % covariances are all the identity matrix (or equivalent). The centres
19 % are initialised randomly from a zero mean unit variance Gaussian.
20 % This makes use of the MATLAB function RANDN and so the seed for the
21 % random weight initialisation can be set using RANDN('STATE', S) where
22 % S is the state value.
23 %
24 % The fields in MIX are
25 %
26 % type = 'gmm'
27 % nin = the dimension of the space
28 % ncentres = number of mixture components
29 % covartype = string for type of variance model
30 % priors = mixing coefficients
31 % centres = means of Gaussians: stored as rows of a matrix
32 % covars = covariances of Gaussians
33 % The additional fields for mixtures of PPCA are
34 % U = principal component subspaces
35 % lambda = in-space covariances: stored as rows of a matrix
36 % The off-subspace noise is stored in COVARS.
37 %
38 % See also
39 % GMMPAK, GMMUNPAK, GMMSAMP, GMMINIT, GMMEM, GMMACTIV, GMMPOST,
40 % GMMPROB
41 %
42
43 % Copyright (c) Ian T Nabney (1996-2001)
44
45 if ncentres < 1
46 error('Number of centres must be greater than zero')
47 end
48
49 mix.type = 'gmm';
50 mix.nin = dim;
51 mix.ncentres = ncentres;
52
53 vartypes = {'spherical', 'diag', 'full', 'ppca'};
54
55 if sum(strcmp(covar_type, vartypes)) == 0
56 error('Undefined covariance type')
57 else
58 mix.covar_type = covar_type;
59 end
60
61 % Make default dimension of PPCA subspaces one.
62 if strcmp(covar_type, 'ppca')
63 if nargin < 4
64 ppca_dim = 1;
65 end
66 if ppca_dim > dim
67 error('Dimension of PPCA subspaces must be less than data.')
68 end
69 mix.ppca_dim = ppca_dim;
70 end
71
72 % Initialise priors to be equal and summing to one
73 mix.priors = ones(1,mix.ncentres) ./ mix.ncentres;
74
75 % Initialise centres
76 mix.centres = randn(mix.ncentres, mix.nin);
77
78 % Initialise all the variances to unity
79 switch mix.covar_type
80
81 case 'spherical'
82 mix.covars = ones(1, mix.ncentres);
83 mix.nwts = mix.ncentres + mix.ncentres*mix.nin + mix.ncentres;
84 case 'diag'
85 % Store diagonals of covariance matrices as rows in a matrix
86 mix.covars = ones(mix.ncentres, mix.nin);
87 mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ...
88 mix.ncentres*mix.nin;
89 case 'full'
90 % Store covariance matrices in a row vector of matrices
91 mix.covars = repmat(eye(mix.nin), [1 1 mix.ncentres]);
92 mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ...
93 mix.ncentres*mix.nin*mix.nin;
94 case 'ppca'
95 % This is the off-subspace noise: make it smaller than
96 % lambdas
97 mix.covars = 0.1*ones(1, mix.ncentres);
98 % Also set aside storage for principal components and
99 % associated variances
100 init_space = eye(mix.nin);
101 init_space = init_space(:, 1:mix.ppca_dim);
102 init_space(mix.ppca_dim+1:mix.nin, :) = ...
103 ones(mix.nin - mix.ppca_dim, mix.ppca_dim);
104 mix.U = repmat(init_space , [1 1 mix.ncentres]);
105 mix.lambda = ones(mix.ncentres, mix.ppca_dim);
106 % Take account of additional parameters
107 mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ...
108 mix.ncentres + mix.ncentres*mix.ppca_dim + ...
109 mix.ncentres*mix.nin*mix.ppca_dim;
110 otherwise
111 error(['Unknown covariance type ', mix.covar_type]);
112 end
113