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