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