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