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