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