Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/KPMstats/sample_gaussian.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 M = sample_gaussian(mu, Sigma, N) |
wolffd@0 | 2 % SAMPLE_GAUSSIAN Draw N random row vectors from a Gaussian distribution |
wolffd@0 | 3 % samples = sample_gaussian(mean, cov, N) |
wolffd@0 | 4 |
wolffd@0 | 5 if nargin==2 |
wolffd@0 | 6 N = 1; |
wolffd@0 | 7 end |
wolffd@0 | 8 |
wolffd@0 | 9 % If Y = CX, Var(Y) = C Var(X) C'. |
wolffd@0 | 10 % So if Var(X)=I, and we want Var(Y)=Sigma, we need to find C. s.t. Sigma = C C'. |
wolffd@0 | 11 % Since Sigma is psd, we have Sigma = U D U' = (U D^0.5) (D'^0.5 U'). |
wolffd@0 | 12 |
wolffd@0 | 13 mu = mu(:); |
wolffd@0 | 14 n=length(mu); |
wolffd@0 | 15 [U,D,V] = svd(Sigma); |
wolffd@0 | 16 M = randn(n,N); |
wolffd@0 | 17 M = (U*sqrt(D))*M + mu*ones(1,N); % transform each column |
wolffd@0 | 18 M = M'; |
wolffd@0 | 19 |