Daniel@0: function p = multipdf(x,theta) Daniel@0: %MULTIPDF Multinomial probability density function. Daniel@0: % p = multipdf(x,theta) returns the probabilities of Daniel@0: % vector x, under the multinomial distribution Daniel@0: % with parameter vector theta. Daniel@0: % Daniel@0: % Author: David Ross Daniel@0: Daniel@0: %-------------------------------------------------------- Daniel@0: % Check the arguments. Daniel@0: %-------------------------------------------------------- Daniel@0: error(nargchk(2,2,nargin)); Daniel@0: Daniel@0: % make sure theta is a vector Daniel@0: if ndims(theta) > 2 | all(size(theta) > 1) Daniel@0: error('theta must be a vector'); Daniel@0: end Daniel@0: Daniel@0: % make sure x is of the appropriate size Daniel@0: if ndims(x) > 2 | any(size(x) ~= size(theta)) Daniel@0: error('columns of X must have same length as theta'); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %-------------------------------------------------------- Daniel@0: % Main... Daniel@0: %-------------------------------------------------------- Daniel@0: p = prod(theta .^ x); Daniel@0: p = p .* factorial(sum(x)) ./ prod(factorial_v(x)); Daniel@0: Daniel@0: Daniel@0: %-------------------------------------------------------- Daniel@0: % Function factorial_v(x): computes the factorial function Daniel@0: % on each element of x Daniel@0: %-------------------------------------------------------- Daniel@0: function r = factorial_v(x) Daniel@0: Daniel@0: if size(x,2) == 1 Daniel@0: x = x'; Daniel@0: end Daniel@0: Daniel@0: r = []; Daniel@0: for y = x Daniel@0: r = [r factorial(y)]; Daniel@0: end