3191
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
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 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3456
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} discrete_pdf (@var{x}, @var{v}, @var{p}) |
|
19 ## For each element of @var{x}, compute the probability density function |
|
20 ## (pDF) at @var{x} of a univariate discrete distribution which assumes |
|
21 ## the values in @var{v} with probabilities @var{p}. |
|
22 ## @end deftypefn |
3191
|
23 |
3456
|
24 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
25 ## Description: pDF of a discrete distribution |
3191
|
26 |
3456
|
27 function pdf = discrete_pdf (x, v, p) |
3426
|
28 |
3191
|
29 if (nargin != 3) |
3456
|
30 usage ("discrete_pdf (x, v, p)"); |
3191
|
31 endif |
|
32 |
3456
|
33 [r, c] = size (x); |
3191
|
34 |
3456
|
35 if (! is_vector (v)) |
|
36 error ("discrete_pdf: v must be a vector"); |
|
37 elseif (! is_vector (p) || (length (p) != length (v))) |
|
38 error ("discrete_pdf: p must be a vector with length (v) elements"); |
|
39 elseif (! (all (p >= 0) && any (p))) |
|
40 error ("discrete_pdf: p must be a nonzero, nonnegative vector"); |
3191
|
41 endif |
|
42 |
|
43 n = r * c; |
3456
|
44 m = length (v); |
|
45 x = reshape (x, n, 1); |
|
46 v = reshape (v, 1, m); |
|
47 p = reshape (p / sum (p), m, 1); |
3191
|
48 |
|
49 pdf = zeros (n, 1); |
3456
|
50 k = find (isnan (x)); |
|
51 if (any (k)) |
3191
|
52 pdf (k) = NaN * ones (length (k), 1); |
|
53 endif |
3456
|
54 k = find (!isnan (x)); |
|
55 if (any (k)) |
3191
|
56 n = length (k); |
3456
|
57 pdf (k) = ((x(k) * ones (1, m)) == (ones (n, 1) * v)) * P; |
3191
|
58 endif |
|
59 |
|
60 pdf = reshape (pdf, r, c); |
|
61 |
|
62 endfunction |