annotate toolboxes/FullBNT-1.0.7/KPMstats/dirichlet_sample.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 theta = dirichlet_sample(alpha, N)
wolffd@0 2 % SAMPLE_DIRICHLET Sample N vectors from Dir(alpha(1), ..., alpha(k))
wolffd@0 3 % theta = sample_dirichlet(alpha, N)
wolffd@0 4 % theta(i,j) = i'th sample of theta_j, where theta ~ Dir
wolffd@0 5
wolffd@0 6 % We use the method from p. 482 of "Bayesian Data Analysis", Gelman et al.
wolffd@0 7
wolffd@0 8 assert(alpha > 0);
wolffd@0 9 k = length(alpha);
wolffd@0 10 theta = zeros(N, k);
wolffd@0 11 scale = 1; % arbitrary
wolffd@0 12 for i=1:k
wolffd@0 13 %theta(:,i) = gamrnd(alpha(i), scale, N, 1);
wolffd@0 14 theta(:,i) = gamma_sample(alpha(i), scale, N, 1);
wolffd@0 15 end
wolffd@0 16 %theta = mk_stochastic(theta);
wolffd@0 17 S = sum(theta,2);
wolffd@0 18 theta = theta ./ repmat(S, 1, k);