annotate toolboxes/FullBNT-1.0.7/KPMstats/dirichletrnd.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 x = dirichletrnd(alpha)
wolffd@0 2 %DIRICHLETRND Random vector from a dirichlet distribution.
wolffd@0 3 % x = dirichletrnd(alpha) returns a vector randomly selected
wolffd@0 4 % from the Dirichlet distribution with parameter vector alpha.
wolffd@0 5 %
wolffd@0 6 % The algorithm used is the following:
wolffd@0 7 % For each alpha(i), generate a value s(i) with distribution
wolffd@0 8 % Gamma(alpha(i),1). Now x(i) = s(i) / sum_j s(j).
wolffd@0 9 %
wolffd@0 10 % The above algorithm was recounted to me by Radford Neal, but
wolffd@0 11 % a reference would be appreciated...
wolffd@0 12 % Do the gamma parameters always have to be 1?
wolffd@0 13 %
wolffd@0 14 % Author: David Ross
wolffd@0 15 % $Id: dirichletrnd.m,v 1.1.1.1 2005/05/22 23:32:12 yozhik Exp $
wolffd@0 16
wolffd@0 17 %-------------------------------------------------
wolffd@0 18 % Check the input
wolffd@0 19 %-------------------------------------------------
wolffd@0 20 error(nargchk(1,1,nargin));
wolffd@0 21
wolffd@0 22 if min(size(alpha)) ~= 1 | length(alpha) < 2
wolffd@0 23 error('alpha must be a vector of length at least 2');
wolffd@0 24 end
wolffd@0 25
wolffd@0 26
wolffd@0 27 %-------------------------------------------------
wolffd@0 28 % Main
wolffd@0 29 %-------------------------------------------------
wolffd@0 30 gamma_vals = gamrnd(alpha, ones(size(alpha)), size(alpha));
wolffd@0 31 denom = sum(gamma_vals);
wolffd@0 32 x = gamma_vals / denom;