]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/discrete_cdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / discrete_cdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 2010-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} {} discrete_cdf (@var{x}, @var{v}, @var{p})
22 ## For each element of @var{x}, compute the cumulative distribution
23 ## function (CDF) at @var{x} of a univariate discrete distribution which
24 ## assumes the values in @var{v} with probabilities @var{p}.
25 ## @end deftypefn
26
27 function cdf = discrete_cdf (x, v, p)
28
29   if (nargin != 3)
30     print_usage ();
31   endif
32
33   if (! isvector (v))
34     error ("discrete_cdf: V must be a vector");
35   elseif (any (isnan (v)))
36     error ("discrete_cdf: V must not have any NaN elements");
37   elseif (! isvector (p) || (length (p) != length (v)))
38     error ("discrete_cdf: P must be a vector with length (V) elements");
39   elseif (! (all (p >= 0) && any (p)))
40     error ("discrete_cdf: P must be a nonzero, non-negative vector");
41   endif
42
43   p = p(:) / sum (p);   # Reshape and normalize probability vector
44
45   if (isa (x, "single") || isa (v, "single") || isa (p, "single"));
46     cdf = NaN (size (x), "single");
47   else
48     cdf = NaN (size (x));
49   endif
50
51   k = !isnan (x);
52   [vs, vi] = sort (v);
53   cdf(k) = [0 ; cumsum(p(vi))](lookup (vs, x(k)) + 1);
54
55 endfunction
56
57
58 %!shared x,v,p,y
59 %! x = [-1 0.1 1.1 1.9 3];
60 %! v = 0.1:0.2:1.9;
61 %! p = 1/length(v) * ones(1, length(v));
62 %! y = [0 0.1 0.6 1 1];
63 %!assert(discrete_cdf ([x, NaN], v, p), [y, NaN], eps);
64
65 %% Test class of input preserved
66 %!assert(discrete_cdf (single([x, NaN]), v, p), single([y, NaN]), 2*eps("single"));
67 %!assert(discrete_cdf ([x, NaN], single(v), p), single([y, NaN]), 2*eps("single"));
68 %!assert(discrete_cdf ([x, NaN], v, single(p)), single([y, NaN]), 2*eps("single"));
69
70 %% Test input validation
71 %!error discrete_cdf ()
72 %!error discrete_cdf (1)
73 %!error discrete_cdf (1,2)
74 %!error discrete_cdf (1,2,3,4)
75 %!error discrete_cdf (1, ones(2), ones(2,1))
76 %!error discrete_cdf (1, [1 ; NaN], ones(2,1))
77 %!error discrete_cdf (1, ones(2,1), ones(1,1))
78 %!error discrete_cdf (1, ones(2,1), [1 -1])
79 %!error discrete_cdf (1, ones(2,1), [1 NaN])
80 %!error discrete_cdf (1, ones(2,1), [0  0])
81