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