comparison toolboxes/FullBNT-1.0.7/KPMstats/dirichletpdf.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 p = dirichletpdf(x, alpha)
2 %DIRICHLETPDF Dirichlet probability density function.
3 % p = dirichletpdf(x, alpha) returns the probability of vector
4 % x under the Dirichlet distribution with parameter vector
5 % alpha.
6 %
7 % Author: David Ross
8
9 %-------------------------------------------------
10 % Check the input
11 %-------------------------------------------------
12 error(nargchk(2,2,nargin));
13
14 % enusre alpha is a vector
15 if min(size(alpha)) ~= 1 | ndims(alpha) > 2 | length(alpha) == 1
16 error('alpha must be a vector');
17 end
18
19 % ensure x is is a vector of the same size as alpha
20 if any(size(x) ~= size(alpha))
21 error('x and alpha must be the same size');
22 end
23
24
25 %-------------------------------------------------
26 % Main
27 %-------------------------------------------------
28 if any(x < 0)
29 p = 0;
30 elseif sum(x) ~= 1
31 disp(['dirichletpdf warning: sum(x)~=1, but this may be ' ...
32 'due to numerical issues']);
33 p = 0;
34 else
35 z = gammaln(sum(alpha)) - sum(gammaln(alpha));
36 z = exp(z);
37
38 p = z * prod(x.^(alpha-1));
39 end