]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/hygepdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / hygepdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1996-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {} hygepdf (@var{x}, @var{t}, @var{m}, @var{n})
22 ## Compute the probability density function (PDF) at @var{x} of the
23 ## hypergeometric distribution with parameters @var{t}, @var{m}, 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 parameters @var{t}, @var{m}, and @var{n} must be positive integers
29 ## with @var{m} and @var{n} not greater than @var{t}.
30 ## @end deftypefn
31
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
33 ## Description: PDF of the hypergeometric distribution
34
35 function pdf = hygepdf (x, t, m, n)
36
37   if (nargin != 4)
38     print_usage ();
39   endif
40
41   if (!isscalar (t) || !isscalar (m) || !isscalar (n))
42     [retval, x, t, m, n] = common_size (x, t, m, n);
43     if (retval > 0)
44       error ("hygepdf: X, T, M, and N must be of common size or scalars");
45     endif
46   endif
47
48   if (iscomplex (x) || iscomplex (t) || iscomplex (m) || iscomplex (n))
49     error ("hygepdf: X, T, M, and N must not be complex");
50   endif
51
52   if (isa (x, "single") || isa (t, "single") || isa (m, "single") || isa (n, "single"))
53     pdf = zeros (size (x), "single");
54   else
55     pdf = zeros (size (x));
56   endif
57
58   ## everything in nel gives NaN
59   nel = (isnan (x) | (t < 0) | (m < 0) | (n <= 0) | (m > t) | (n > t) |
60         (t != fix (t)) | (m != fix (m)) | (n != fix (n)));
61   ## everything in zel gives 0 unless in nel
62   zel = ((x != fix (x)) | (x < 0) | (x > m) | (n < x) | (n-x > t-m));
63
64   pdf(nel) = NaN;
65
66   k = !nel & !zel;
67   if (any (k(:)))
68     if (isscalar (t) && isscalar (m) && isscalar (n))
69       pdf(k) = (bincoeff (m, x(k)) .* bincoeff (t-m, n-x(k))
70                 / bincoeff (t, n));
71     else
72       pdf(k) = (bincoeff (m(k), x(k)) .* bincoeff (t(k)-m(k), n(k)-x(k))
73                 ./ bincoeff (t(k), n(k)));
74     endif
75   endif
76
77 endfunction
78
79
80 %!shared x,y
81 %! x = [-1 0 1 2 3];
82 %! y = [0 1/6 4/6 1/6 0];
83 %!assert(hygepdf (x, 4*ones(1,5), 2, 2), y);
84 %!assert(hygepdf (x, 4, 2*ones(1,5), 2), y);
85 %!assert(hygepdf (x, 4, 2, 2*ones(1,5)), y);
86 %!assert(hygepdf (x, 4*[1 -1 NaN 1.1 1], 2, 2), [0 NaN NaN NaN 0]);
87 %!assert(hygepdf (x, 4, 2*[1 -1 NaN 1.1 1], 2), [0 NaN NaN NaN 0]);
88 %!assert(hygepdf (x, 4, 5, 2), [NaN NaN NaN NaN NaN]);
89 %!assert(hygepdf (x, 4, 2, 2*[1 -1 NaN 1.1 1]), [0 NaN NaN NaN 0]);
90 %!assert(hygepdf (x, 4, 2, 5), [NaN NaN NaN NaN NaN]);
91 %!assert(hygepdf ([x, NaN], 4, 2, 2), [y, NaN], eps);
92
93 %% Test class of input preserved
94 %!assert(hygepdf (single([x, NaN]), 4, 2, 2), single([y, NaN]));
95 %!assert(hygepdf ([x, NaN], single(4), 2, 2), single([y, NaN]));
96 %!assert(hygepdf ([x, NaN], 4, single(2), 2), single([y, NaN]));
97 %!assert(hygepdf ([x, NaN], 4, 2, single(2)), single([y, NaN]));
98
99 %% Test input validation
100 %!error hygepdf ()
101 %!error hygepdf (1)
102 %!error hygepdf (1,2)
103 %!error hygepdf (1,2,3)
104 %!error hygepdf (1,2,3,4,5)
105 %!error hygepdf (1, ones(3),ones(2),ones(2))
106 %!error hygepdf (1, ones(2),ones(3),ones(2))
107 %!error hygepdf (1, ones(2),ones(2),ones(3))
108 %!error hygepdf (i, 2, 2, 2)
109 %!error hygepdf (2, i, 2, 2)
110 %!error hygepdf (2, 2, i, 2)
111 %!error hygepdf (2, 2, 2, i)
112