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