Daniel@0: function theta = dirichlet_sample(alpha, N) Daniel@0: % SAMPLE_DIRICHLET Sample N vectors from Dir(alpha(1), ..., alpha(k)) Daniel@0: % theta = sample_dirichlet(alpha, N) Daniel@0: % theta(i,j) = i'th sample of theta_j, where theta ~ Dir Daniel@0: Daniel@0: % We use the method from p. 482 of "Bayesian Data Analysis", Gelman et al. Daniel@0: Daniel@0: assert(alpha > 0); Daniel@0: k = length(alpha); Daniel@0: theta = zeros(N, k); Daniel@0: scale = 1; % arbitrary Daniel@0: for i=1:k Daniel@0: %theta(:,i) = gamrnd(alpha(i), scale, N, 1); Daniel@0: theta(:,i) = gamma_sample(alpha(i), scale, N, 1); Daniel@0: end Daniel@0: %theta = mk_stochastic(theta); Daniel@0: S = sum(theta,2); Daniel@0: theta = theta ./ repmat(S, 1, k);