Daniel@0: function x = dirichletrnd(alpha) Daniel@0: %DIRICHLETRND Random vector from a dirichlet distribution. Daniel@0: % x = dirichletrnd(alpha) returns a vector randomly selected Daniel@0: % from the Dirichlet distribution with parameter vector alpha. Daniel@0: % Daniel@0: % The algorithm used is the following: Daniel@0: % For each alpha(i), generate a value s(i) with distribution Daniel@0: % Gamma(alpha(i),1). Now x(i) = s(i) / sum_j s(j). Daniel@0: % Daniel@0: % The above algorithm was recounted to me by Radford Neal, but Daniel@0: % a reference would be appreciated... Daniel@0: % Do the gamma parameters always have to be 1? Daniel@0: % Daniel@0: % Author: David Ross Daniel@0: % $Id: dirichletrnd.m,v 1.1.1.1 2005/05/22 23:32:12 yozhik Exp $ Daniel@0: Daniel@0: %------------------------------------------------- Daniel@0: % Check the input Daniel@0: %------------------------------------------------- Daniel@0: error(nargchk(1,1,nargin)); Daniel@0: Daniel@0: if min(size(alpha)) ~= 1 | length(alpha) < 2 Daniel@0: error('alpha must be a vector of length at least 2'); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %------------------------------------------------- Daniel@0: % Main Daniel@0: %------------------------------------------------- Daniel@0: gamma_vals = gamrnd(alpha, ones(size(alpha)), size(alpha)); Daniel@0: denom = sum(gamma_vals); Daniel@0: x = gamma_vals / denom;