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