1 ## Copyright (C) 2012 Arno Onken
3 ## This program is free software: you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation, either version 3 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{y} =} mnpdf (@var{x}, @var{p})
18 ## Compute the probability density function of the multinomial distribution.
20 ## @subheading Arguments
24 ## @var{x} is vector with a single sample of a multinomial distribution with
25 ## parameter @var{p} or a matrix of random samples from multinomial
26 ## distributions. In the latter case, each row of @var{x} is a sample from a
27 ## multinomial distribution with the corresponding row of @var{p} being its
31 ## @var{p} is a vector with the probabilities of the categories or a matrix
32 ## with each row containing the probabilities of a multinomial sample.
35 ## @subheading Return values
39 ## @var{y} is a vector of probabilites of the random samples @var{x} from the
40 ## multinomial distribution with corresponding parameter @var{p}. The parameter
41 ## @var{n} of the multinomial distribution is the sum of the elements of each
42 ## row of @var{x}. The length of @var{y} is the number of columns of @var{x}.
43 ## If a row of @var{p} does not sum to @code{1}, then the corresponding element
44 ## of @var{y} will be @code{NaN}.
47 ## @subheading Examples
52 ## p = [0.2, 0.5, 0.3];
57 ## x = [1, 4, 2; 1, 0, 9];
58 ## p = [0.2, 0.5, 0.3; 0.1, 0.1, 0.8];
63 ## @subheading References
67 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
68 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC, 2001.
71 ## Merran Evans, Nicholas Hastings and Brian Peacock. @cite{Statistical
72 ## Distributions}. pages 134-136, Wiley, New York, third edition, 2000.
76 ## Author: Arno Onken <asnelt@asnelt.org>
77 ## Description: PDF of the multinomial distribution
79 function y = mnpdf (x, p)
86 if (! ismatrix (x) || any (x(:) < 0 | round (x(:) != x(:))))
87 error ("mnpdf: x must be a matrix of non-negative integer values");
89 if (! ismatrix (p) || any (p(:) < 0))
90 error ("mnpdf: p must be a non-empty matrix with rows of probabilities");
94 if (! isvector (x) || ! isvector (p))
101 if (size (x, 1) == 1 && size (p, 1) > 1)
102 x = repmat (x, size (p, 1), 1);
103 elseif (size (x, 1) > 1 && size (p, 1) == 1)
104 p = repmat (p, size (x, 1), 1);
107 # Continue argument check
108 if (any (size (x) != size (p)))
109 error ("mnpdf: x and p must have compatible sizes");
112 # Count total number of elements of each multinomial sample
114 # Compute probability density function of the multinomial distribution
117 y = exp (gammaln (n+1) - sum (gammaln (x+1), 2) + sum (t, 2));
118 # Set invalid rows to NaN
119 k = (abs (sum (p, 2) - 1) > 1e-6);
126 %! p = [0.2, 0.5, 0.3];
128 %! assert (y, 0.11812, 0.001);
131 %! x = [1, 4, 2; 1, 0, 9];
132 %! p = [0.2, 0.5, 0.3; 0.1, 0.1, 0.8];
134 %! assert (y, [0.11812; 0.13422], 0.001);