5410
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} hygepdf (@var{x}, @var{m}, @var{t}, @var{n}) |
5410
|
22 ## Compute the probability density function (PDF) at @var{x} of the |
|
23 ## hypergeometric distribution with parameters @var{m}, @var{t}, and |
|
24 ## @var{n}. This is the probability of obtaining @var{x} marked items |
|
25 ## when randomly drawing a sample of size @var{n} without replacement |
|
26 ## from a population of total size @var{t} containing @var{m} marked items. |
|
27 ## |
|
28 ## The arguments must be of common size or scalar. |
|
29 ## @end deftypefn |
|
30 |
5428
|
31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
32 ## Description: PDF of the hypergeometric distribution |
|
33 |
5411
|
34 function pdf = hygepdf (x, m, t, n) |
5410
|
35 |
|
36 if (nargin != 4) |
6046
|
37 print_usage (); |
5410
|
38 endif |
|
39 |
|
40 if (!isscalar (m) || !isscalar (t) || !isscalar (n)) |
|
41 [retval, x, m, t, n] = common_size (x, m, t, n); |
|
42 if (retval > 0) |
5411
|
43 error ("hygepdf: x, m, t, and n must be of common size or scalar"); |
5410
|
44 endif |
|
45 endif |
|
46 |
|
47 pdf = zeros (size (x)); |
|
48 |
|
49 ## everything in i1 gives NaN |
|
50 i1 = ((m < 0) | (t < 0) | (n <= 0) | (m != round (m)) | |
|
51 (t != round (t)) | (n != round (n)) | (m > t) | (n > t)); |
|
52 ## everything in i2 gives 0 unless in i1 |
|
53 i2 = ((x != round (x)) | (x < 0) | (x > m) | (n < x) | (n-x > t-m)); |
|
54 k = find (i1); |
|
55 if (any (k)) |
|
56 if (isscalar (m) && isscalar (t) && isscalar (n)) |
|
57 pdf = NaN * ones ( size (x)); |
|
58 else |
|
59 pdf (k) = NaN; |
|
60 endif |
|
61 endif |
|
62 k = find (!i1 & !i2); |
|
63 if (any (k)) |
|
64 if (isscalar (m) && isscalar (t) && isscalar (n)) |
|
65 pdf (k) = (bincoeff (m, x(k)) .* bincoeff (t-m, n-x(k)) |
|
66 / bincoeff (t, n)); |
|
67 else |
|
68 pdf (k) = (bincoeff (m(k), x(k)) .* bincoeff (t(k)-m(k), n(k)-x(k)) |
|
69 ./ bincoeff (t(k), n(k))); |
|
70 endif |
|
71 endif |
|
72 |
|
73 endfunction |