annotate toolboxes/FullBNT-1.0.7/netlab3.3/gmmsamp.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 [data, label] = gmmsamp(mix, n)
wolffd@0 2 %GMMSAMP Sample from a Gaussian mixture distribution.
wolffd@0 3 %
wolffd@0 4 % Description
wolffd@0 5 %
wolffd@0 6 % DATA = GSAMP(MIX, N) generates a sample of size N from a Gaussian
wolffd@0 7 % mixture distribution defined by the MIX data structure. The matrix X
wolffd@0 8 % has N rows in which each row represents a MIX.NIN-dimensional sample
wolffd@0 9 % vector.
wolffd@0 10 %
wolffd@0 11 % [DATA, LABEL] = GMMSAMP(MIX, N) also returns a column vector of
wolffd@0 12 % classes (as an index 1..N) LABEL.
wolffd@0 13 %
wolffd@0 14 % See also
wolffd@0 15 % GSAMP, GMM
wolffd@0 16 %
wolffd@0 17
wolffd@0 18 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 19
wolffd@0 20 % Check input arguments
wolffd@0 21 errstring = consist(mix, 'gmm');
wolffd@0 22 if ~isempty(errstring)
wolffd@0 23 error(errstring);
wolffd@0 24 end
wolffd@0 25 if n < 1
wolffd@0 26 error('Number of data points must be positive')
wolffd@0 27 end
wolffd@0 28
wolffd@0 29 % Determine number to sample from each component
wolffd@0 30 priors = rand(1, n);
wolffd@0 31
wolffd@0 32 % Pre-allocate data array
wolffd@0 33 data = zeros(n, mix.nin);
wolffd@0 34 if nargout > 1
wolffd@0 35 label = zeros(n, 1);
wolffd@0 36 end
wolffd@0 37 cum_prior = 0; % Cumulative sum of priors
wolffd@0 38 total_samples = 0; % Cumulative sum of number of sampled points
wolffd@0 39 for j = 1:mix.ncentres
wolffd@0 40 num_samples = sum(priors >= cum_prior & ...
wolffd@0 41 priors < cum_prior + mix.priors(j));
wolffd@0 42 % Form a full covariance matrix
wolffd@0 43 switch mix.covar_type
wolffd@0 44 case 'spherical'
wolffd@0 45 covar = mix.covars(j) * eye(mix.nin);
wolffd@0 46 case 'diag'
wolffd@0 47 covar = diag(mix.covars(j, :));
wolffd@0 48 case 'full'
wolffd@0 49 covar = mix.covars(:, :, j);
wolffd@0 50 case 'ppca'
wolffd@0 51 covar = mix.covars(j) * eye(mix.nin) + ...
wolffd@0 52 mix.U(:, :, j)* ...
wolffd@0 53 (diag(mix.lambda(j, :))-(mix.covars(j)*eye(mix.ppca_dim)))* ...
wolffd@0 54 (mix.U(:, :, j)');
wolffd@0 55 otherwise
wolffd@0 56 error(['Unknown covariance type ', mix.covar_type]);
wolffd@0 57 end
wolffd@0 58 data(total_samples+1:total_samples+num_samples, :) = ...
wolffd@0 59 gsamp(mix.centres(j, :), covar, num_samples);
wolffd@0 60 if nargout > 1
wolffd@0 61 label(total_samples+1:total_samples+num_samples) = j;
wolffd@0 62 end
wolffd@0 63 cum_prior = cum_prior + mix.priors(j);
wolffd@0 64 total_samples = total_samples + num_samples;
wolffd@0 65 end
wolffd@0 66