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