]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/hygeinv.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / hygeinv.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1997-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} {} hygeinv (@var{x}, @var{t}, @var{m}, @var{n})
22 ## For each element of @var{x}, compute the quantile (the inverse of
23 ## the CDF) at @var{x} of the hypergeometric distribution with parameters
24 ## @var{t}, @var{m}, and @var{n}.  This is the probability of obtaining @var{x}
25 ## marked items when randomly drawing a sample of size @var{n} without
26 ## replacement from a population of total size @var{t} containing @var{m}
27 ## marked items.
28 ##
29 ## The parameters @var{t}, @var{m}, and @var{n} must be positive integers
30 ## with @var{m} and @var{n} not greater than @var{t}.
31 ## @end deftypefn
32
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
34 ## Description: Random deviates from the hypergeometric distribution
35
36 function inv = hygeinv (x, t, m, n)
37
38   if (nargin != 4)
39     print_usage ();
40   endif
41
42   if (!isscalar (t) || !isscalar (m) || !isscalar (n))
43     [retval, x, t, m, n] = common_size (x, t, m, n);
44     if (retval > 0)
45       error ("hygeinv: X, T, M, and N must be of common size or scalars");
46     endif
47   endif
48
49   if (iscomplex (x) || iscomplex (t) || iscomplex (m) || iscomplex (n))
50     error ("hygeinv: X, T, M, and N must not be complex");
51   endif
52
53   if (isa (x, "single") || isa (t, "single") || isa (m, "single") || isa (n, "single"))
54     inv = NaN (size (x), "single");
55   else
56     inv = NaN (size (x));
57   endif
58
59   ok = ((t >= 0) & (m >= 0) & (n > 0) & (m <= t) & (n <= t) &
60         (t == fix (t)) & (m == fix (m)) & (n == fix (n)));
61
62   if (isscalar (t))
63     if (ok)
64       inv = discrete_inv (x, 0 : n, hygepdf (0 : n, t, m, n));
65       inv(x == 0) = 0;  # Hack to return correct value for start of distribution
66     endif
67   else
68     for i = find (ok(:)')  # Must be row vector arg to for loop
69       v = 0 : n(i);
70       if (x(i) == 0)
71         inv(i) = 0;  # Hack to return correct value for start of distribution
72       else
73         inv(i) = discrete_inv (x(i), v, hygepdf (v, t(i), m(i), n(i)));
74       endif
75     endfor
76   endif
77
78 endfunction
79
80
81 %!shared x
82 %! x = [-1 0 0.5 1 2];
83 %!assert(hygeinv (x, 4*ones(1,5), 2*ones(1,5), 2*ones(1,5)), [NaN 0 1 2 NaN]);
84 %!assert(hygeinv (x, 4*ones(1,5), 2, 2), [NaN 0 1 2 NaN]);
85 %!assert(hygeinv (x, 4, 2*ones(1,5), 2), [NaN 0 1 2 NaN]);
86 %!assert(hygeinv (x, 4, 2, 2*ones(1,5)), [NaN 0 1 2 NaN]);
87 %!assert(hygeinv (x, 4*[1 -1 NaN 1.1 1], 2, 2), [NaN NaN NaN NaN NaN]);
88 %!assert(hygeinv (x, 4, 2*[1 -1 NaN 1.1 1], 2), [NaN NaN NaN NaN NaN]);
89 %!assert(hygeinv (x, 4, 5, 2), [NaN NaN NaN NaN NaN]);
90 %!assert(hygeinv (x, 4, 2, 2*[1 -1 NaN 1.1 1]), [NaN NaN NaN NaN NaN]);
91 %!assert(hygeinv (x, 4, 2, 5), [NaN NaN NaN NaN NaN]);
92 %!assert(hygeinv ([x(1:2) NaN x(4:5)], 4, 2, 2), [NaN 0 NaN 2 NaN]);
93
94 %% Test class of input preserved
95 %!assert(hygeinv ([x, NaN], 4, 2, 2), [NaN 0 1 2 NaN NaN]);
96 %!assert(hygeinv (single([x, NaN]), 4, 2, 2), single([NaN 0 1 2 NaN NaN]));
97 %!assert(hygeinv ([x, NaN], single(4), 2, 2), single([NaN 0 1 2 NaN NaN]));
98 %!assert(hygeinv ([x, NaN], 4, single(2), 2), single([NaN 0 1 2 NaN NaN]));
99 %!assert(hygeinv ([x, NaN], 4, 2, single(2)), single([NaN 0 1 2 NaN NaN]));
100
101 %% Test input validation
102 %!error hygeinv ()
103 %!error hygeinv (1)
104 %!error hygeinv (1,2)
105 %!error hygeinv (1,2,3)
106 %!error hygeinv (1,2,3,4,5)
107 %!error hygeinv (ones(2), ones(3), 1, 1)
108 %!error hygeinv (1, ones(2), ones(3), 1)
109 %!error hygeinv (1, 1, ones(2), ones(3))
110 %!error hygeinv (i, 2, 2, 2)
111 %!error hygeinv (2, i, 2, 2)
112 %!error hygeinv (2, 2, i, 2)
113 %!error hygeinv (2, 2, 2, i)
114