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