-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdrawGmm.m
29 lines (23 loc) · 839 Bytes
/
drawGmm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function [Y,z] = drawGmm(mu, sigSq, p, N)
% Draw N samples from a mixture of N Gaussians. In the multivariate
% case, mu is a matrix where each row is the mean of one Gaussian.
% SigSq is a 3D matrix such that sigSq(:,:,i) is the covariance of the
% ith Gaussian. In the univariate case, mu(i) is the mean and
% sigSq(i) is the variance of the ith Gaussian.
% Copyright (C) 2005 Michael Mandel, mim at ee columbia edu;
% distributable under GPL, see README.txt
% Y ~ sum[ p_i * N(mu_i, sigma_i) ]
[tD,D] = size(mu);
if(tD == 1) D=1; end
z = drawMultinom(repmat(p(:), 1, N));
if(D == 1)
Y = randn(1,N).*sqrt(sigSq(z)) + mu(z);
else
for i=1:length(p)
inClass = find(z == i);
n = numel(inClass);
[u,s,v] = svd(sigSq(:,:,i));
sig = sqrt(s)*v';
Y(inClass,:) = randn(n,D) * sig + repmat(mu(i,:), n, 1);
end
end