]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/unidinv.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / unidinv.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 2007-2012 David Bateman
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} {} unidinv (@var{x}, @var{n})
22 ## For each element of @var{x}, compute the quantile (the inverse of
23 ## the CDF) at @var{x} of the discrete uniform distribution which assumes
24 ## the integer values 1--@var{n} with equal probability.
25 ## @end deftypefn
26
27 function inv = unidinv (x, n)
28
29   if (nargin != 2)
30     print_usage ();
31   endif
32
33   if (! isscalar (n))
34     [retval, x, n] = common_size (x, n);
35     if (retval > 0)
36       error ("unidcdf: X and N must be of common size or scalars");
37     endif
38   endif
39
40   if (iscomplex (x) || iscomplex (n))
41     error ("unidinv: X and N must not be complex");
42   endif
43
44   if (isa (x, "single") || isa (n, "single"))
45     inv = NaN (size (x), "single");
46   else
47     inv = NaN (size (x));
48   endif
49
50   ## For Matlab compatibility, unidinv(0) = NaN
51   k = (x > 0) & (x <= 1) & (n > 0 & n == fix (n));
52   if (isscalar (n))
53     inv(k) = floor (x(k) * n);
54   else
55     inv(k) = floor (x(k) .* n(k));
56   endif
57
58 endfunction
59
60
61 %!shared x
62 %! x = [-1 0 0.5 1 2];
63 %!assert(unidinv (x, 10*ones(1,5)), [NaN NaN 5 10 NaN], eps);
64 %!assert(unidinv (x, 10), [NaN NaN 5 10 NaN], eps);
65 %!assert(unidinv (x, 10*[0 1 NaN 1 1]), [NaN NaN NaN 10 NaN], eps);
66 %!assert(unidinv ([x(1:2) NaN x(4:5)], 10), [NaN NaN NaN 10 NaN], eps);
67
68 %% Test class of input preserved
69 %!assert(unidinv ([x, NaN], 10), [NaN NaN 5 10 NaN NaN], eps);
70 %!assert(unidinv (single([x, NaN]), 10), single([NaN NaN 5 10 NaN NaN]), eps);
71 %!assert(unidinv ([x, NaN], single(10)), single([NaN NaN 5 10 NaN NaN]), eps);
72
73 %% Test input validation
74 %!error unidinv ()
75 %!error unidinv (1)
76 %!error unidinv (1,2,3)
77 %!error unidinv (ones(3),ones(2))
78 %!error unidinv (ones(2),ones(3))
79 %!error unidinv (i, 2)
80 %!error unidinv (2, i)
81