]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/unifcdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / unifcdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-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} {} unifcdf (@var{x})
22 ## @deftypefnx {Function File} {} unifcdf (@var{x}, @var{a}, @var{b})
23 ## For each element of @var{x}, compute the cumulative distribution
24 ## function (CDF) at @var{x} of the uniform distribution on the interval
25 ## [@var{a}, @var{b}].
26 ##
27 ## Default values are @var{a} = 0, @var{b} = 1.
28 ## @end deftypefn
29
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
31 ## Description: CDF of the uniform distribution
32
33 function cdf = unifcdf (x, a = 0, b = 1)
34
35   if (nargin != 1 && nargin != 3)
36     print_usage ();
37   endif
38
39   if (!isscalar (a) || !isscalar (b))
40     [retval, x, a, b] = common_size (x, a, b);
41     if (retval > 0)
42       error ("unifcdf: X, A, and B must be of common size or scalars");
43     endif
44   endif
45
46   if (iscomplex (x) || iscomplex (a) || iscomplex (b))
47     error ("unifcdf: X, A, and B must not be complex");
48   endif
49
50   if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
51     cdf = zeros (size (x), "single");
52   else
53     cdf = zeros (size (x));
54   endif
55
56   k = isnan (x) | !(a < b);
57   cdf(k) = NaN;
58
59   k = (x >= b) & (a < b);
60   cdf(k) = 1;
61
62   k = (x > a) & (x < b);
63   if (isscalar (a) && isscalar (b))
64     cdf(k) = (x(k) < b) .* (x(k) - a) / (b - a);
65   else
66     cdf(k) = (x(k) < b(k)) .* (x(k) - a(k)) ./ (b(k) - a(k));
67   endif
68
69 endfunction
70
71
72 %!shared x,y
73 %! x = [-1 0 0.5 1 2] + 1;
74 %! y = [0 0 0.5 1 1];
75 %!assert(unifcdf (x, ones(1,5), 2*ones(1,5)), y);
76 %!assert(unifcdf (x, 1, 2*ones(1,5)), y);
77 %!assert(unifcdf (x, ones(1,5), 2), y);
78 %!assert(unifcdf (x, [2 1 NaN 1 1], 2), [NaN 0 NaN 1 1]);
79 %!assert(unifcdf (x, 1, 2*[0 1 NaN 1 1]), [NaN 0 NaN 1 1]);
80 %!assert(unifcdf ([x(1:2) NaN x(4:5)], 1, 2), [y(1:2) NaN y(4:5)]);
81
82 %% Test class of input preserved
83 %!assert(unifcdf ([x, NaN], 1, 2), [y, NaN]);
84 %!assert(unifcdf (single([x, NaN]), 1, 2), single([y, NaN]));
85 %!assert(unifcdf ([x, NaN], single(1), 2), single([y, NaN]));
86 %!assert(unifcdf ([x, NaN], 1, single(2)), single([y, NaN]));
87
88 %% Test input validation
89 %!error unifcdf ()
90 %!error unifcdf (1,2)
91 %!error unifcdf (1,2,3,4)
92 %!error unifcdf (ones(3),ones(2),ones(2))
93 %!error unifcdf (ones(2),ones(3),ones(2))
94 %!error unifcdf (ones(2),ones(2),ones(3))
95 %!error unifcdf (i, 2, 2)
96 %!error unifcdf (2, i, 2)
97 %!error unifcdf (2, 2, i)
98