wolffd@0: function mix = gmm(dim, ncentres, covar_type, ppca_dim) wolffd@0: %GMM Creates a Gaussian mixture model with specified architecture. wolffd@0: % wolffd@0: % Description wolffd@0: % MIX = GMM(DIM, NCENTRES, COVARTYPE) takes the dimension of the space wolffd@0: % DIM, the number of centres in the mixture model and the type of the wolffd@0: % mixture model, and returns a data structure MIX. The mixture model wolffd@0: % type defines the covariance structure of each component Gaussian: wolffd@0: % 'spherical' = single variance parameter for each component: stored as a vector wolffd@0: % 'diag' = diagonal matrix for each component: stored as rows of a matrix wolffd@0: % 'full' = full matrix for each component: stored as 3d array wolffd@0: % 'ppca' = probabilistic PCA: stored as principal components (in a 3d array wolffd@0: % and associated variances and off-subspace noise wolffd@0: % MIX = GMM(DIM, NCENTRES, COVARTYPE, PPCA_DIM) also sets the wolffd@0: % dimension of the PPCA sub-spaces: the default value is one. wolffd@0: % wolffd@0: % The priors are initialised to equal values summing to one, and the wolffd@0: % covariances are all the identity matrix (or equivalent). The centres wolffd@0: % are initialised randomly from a zero mean unit variance Gaussian. wolffd@0: % This makes use of the MATLAB function RANDN and so the seed for the wolffd@0: % random weight initialisation can be set using RANDN('STATE', S) where wolffd@0: % S is the state value. wolffd@0: % wolffd@0: % The fields in MIX are wolffd@0: % wolffd@0: % type = 'gmm' wolffd@0: % nin = the dimension of the space wolffd@0: % ncentres = number of mixture components wolffd@0: % covartype = string for type of variance model wolffd@0: % priors = mixing coefficients wolffd@0: % centres = means of Gaussians: stored as rows of a matrix wolffd@0: % covars = covariances of Gaussians wolffd@0: % The additional fields for mixtures of PPCA are wolffd@0: % U = principal component subspaces wolffd@0: % lambda = in-space covariances: stored as rows of a matrix wolffd@0: % The off-subspace noise is stored in COVARS. wolffd@0: % wolffd@0: % See also wolffd@0: % GMMPAK, GMMUNPAK, GMMSAMP, GMMINIT, GMMEM, GMMACTIV, GMMPOST, wolffd@0: % GMMPROB wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: if ncentres < 1 wolffd@0: error('Number of centres must be greater than zero') wolffd@0: end wolffd@0: wolffd@0: mix.type = 'gmm'; wolffd@0: mix.nin = dim; wolffd@0: mix.ncentres = ncentres; wolffd@0: wolffd@0: vartypes = {'spherical', 'diag', 'full', 'ppca'}; wolffd@0: wolffd@0: if sum(strcmp(covar_type, vartypes)) == 0 wolffd@0: error('Undefined covariance type') wolffd@0: else wolffd@0: mix.covar_type = covar_type; wolffd@0: end wolffd@0: wolffd@0: % Make default dimension of PPCA subspaces one. wolffd@0: if strcmp(covar_type, 'ppca') wolffd@0: if nargin < 4 wolffd@0: ppca_dim = 1; wolffd@0: end wolffd@0: if ppca_dim > dim wolffd@0: error('Dimension of PPCA subspaces must be less than data.') wolffd@0: end wolffd@0: mix.ppca_dim = ppca_dim; wolffd@0: end wolffd@0: wolffd@0: % Initialise priors to be equal and summing to one wolffd@0: mix.priors = ones(1,mix.ncentres) ./ mix.ncentres; wolffd@0: wolffd@0: % Initialise centres wolffd@0: mix.centres = randn(mix.ncentres, mix.nin); wolffd@0: wolffd@0: % Initialise all the variances to unity wolffd@0: switch mix.covar_type wolffd@0: wolffd@0: case 'spherical' wolffd@0: mix.covars = ones(1, mix.ncentres); wolffd@0: mix.nwts = mix.ncentres + mix.ncentres*mix.nin + mix.ncentres; wolffd@0: case 'diag' wolffd@0: % Store diagonals of covariance matrices as rows in a matrix wolffd@0: mix.covars = ones(mix.ncentres, mix.nin); wolffd@0: mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ... wolffd@0: mix.ncentres*mix.nin; wolffd@0: case 'full' wolffd@0: % Store covariance matrices in a row vector of matrices wolffd@0: mix.covars = repmat(eye(mix.nin), [1 1 mix.ncentres]); wolffd@0: mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ... wolffd@0: mix.ncentres*mix.nin*mix.nin; wolffd@0: case 'ppca' wolffd@0: % This is the off-subspace noise: make it smaller than wolffd@0: % lambdas wolffd@0: mix.covars = 0.1*ones(1, mix.ncentres); wolffd@0: % Also set aside storage for principal components and wolffd@0: % associated variances wolffd@0: init_space = eye(mix.nin); wolffd@0: init_space = init_space(:, 1:mix.ppca_dim); wolffd@0: init_space(mix.ppca_dim+1:mix.nin, :) = ... wolffd@0: ones(mix.nin - mix.ppca_dim, mix.ppca_dim); wolffd@0: mix.U = repmat(init_space , [1 1 mix.ncentres]); wolffd@0: mix.lambda = ones(mix.ncentres, mix.ppca_dim); wolffd@0: % Take account of additional parameters wolffd@0: mix.nwts = mix.ncentres + mix.ncentres*mix.nin + ... wolffd@0: mix.ncentres + mix.ncentres*mix.ppca_dim + ... wolffd@0: mix.ncentres*mix.nin*mix.ppca_dim; wolffd@0: otherwise wolffd@0: error(['Unknown covariance type ', mix.covar_type]); wolffd@0: end wolffd@0: