7017
|
1 ## Copyright (C) 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
7002
|
20 ## @deftypefn {Function File} {} hygepdf (@var{x}, @var{t}, @var{m}, @var{n}) |
5410
|
21 ## Compute the probability density function (PDF) at @var{x} of the |
7002
|
22 ## hypergeometric distribution with parameters @var{t}, @var{m}, and |
5410
|
23 ## @var{n}. This is the probability of obtaining @var{x} marked items |
|
24 ## when randomly drawing a sample of size @var{n} without replacement |
|
25 ## from a population of total size @var{t} containing @var{m} marked items. |
|
26 ## |
|
27 ## The arguments must be of common size or scalar. |
|
28 ## @end deftypefn |
|
29 |
5428
|
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
31 ## Description: PDF of the hypergeometric distribution |
|
32 |
7002
|
33 function pdf = hygepdf (x, t, m, n) |
5410
|
34 |
|
35 if (nargin != 4) |
6046
|
36 print_usage (); |
5410
|
37 endif |
|
38 |
7002
|
39 if (!isscalar (t) || !isscalar (m) || !isscalar (n)) |
|
40 [retval, x, t, m, n] = common_size (x, t, m, n); |
5410
|
41 if (retval > 0) |
7002
|
42 error ("hygepdf: x, t, m, and n must be of common size or scalar"); |
5410
|
43 endif |
|
44 endif |
|
45 |
|
46 pdf = zeros (size (x)); |
|
47 |
|
48 ## everything in i1 gives NaN |
7002
|
49 i1 = ((t < 0) | (m < 0) | (n <= 0) | (t != round (t)) | |
|
50 (m != round (m)) | (n != round (n)) | (m > t) | (n > t)); |
5410
|
51 ## everything in i2 gives 0 unless in i1 |
|
52 i2 = ((x != round (x)) | (x < 0) | (x > m) | (n < x) | (n-x > t-m)); |
|
53 k = find (i1); |
|
54 if (any (k)) |
7002
|
55 if (isscalar (t) && isscalar (m) && isscalar (n)) |
5410
|
56 pdf = NaN * ones ( size (x)); |
|
57 else |
|
58 pdf (k) = NaN; |
|
59 endif |
|
60 endif |
|
61 k = find (!i1 & !i2); |
|
62 if (any (k)) |
7002
|
63 if (isscalar (t) && isscalar (m) && isscalar (n)) |
5410
|
64 pdf (k) = (bincoeff (m, x(k)) .* bincoeff (t-m, n-x(k)) |
|
65 / bincoeff (t, n)); |
|
66 else |
|
67 pdf (k) = (bincoeff (m(k), x(k)) .* bincoeff (t(k)-m(k), n(k)-x(k)) |
|
68 ./ bincoeff (t(k), n(k))); |
|
69 endif |
|
70 endif |
|
71 |
|
72 endfunction |