Mercurial > hg > octave-lyh
annotate scripts/statistics/distributions/unidcdf.m @ 13803:a2e158c3451f
provide the waitbar function
* waitbar.m: New file.
* plot/module.mk (plot_FCN_FILES): Add it to the list.
* NEWS: Add waitbar to the list of new functions.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 03 Nov 2011 05:30:45 -0400 |
parents | 19b9f17d22af |
children | 72c96de7a403 |
rev | line source |
---|---|
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
1 ## Copyright (C) 2011 Rik Wehbring |
11523 | 2 ## Copyright (C) 2007-2011 David Bateman |
6356 | 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 | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
6356 | 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 | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
6356 | 19 |
20 ## -*- texinfo -*- | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
21 ## @deftypefn {Function File} {} unidcdf (@var{x}, @var{n}) |
6356 | 22 ## For each element of @var{x}, compute the cumulative distribution |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
23 ## function (CDF) at @var{x} of a discrete uniform distribution which assumes |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
24 ## the integer values 1--@var{n} with equal probability. |
6356 | 25 ## @end deftypefn |
26 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
27 function cdf = unidcdf (x, n) |
6356 | 28 |
29 if (nargin != 2) | |
30 print_usage (); | |
31 endif | |
32 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
33 if (! isscalar (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
34 [retval, x, n] = common_size (x, n); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
35 if (retval > 0) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
36 error ("unidcdf: X and N must be of common size or scalars"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
37 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
38 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
39 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
40 if (iscomplex (x) || iscomplex (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
41 error ("unidcdf: X and N must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
42 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
43 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
44 if (isa (x, "single") || isa (n, "single")) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
45 cdf = zeros (size (x), "single"); |
6356 | 46 else |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
47 cdf = zeros (size (x)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
48 endif |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
49 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
50 knan = isnan (x) | ! (n > 0 & n == fix (n)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
51 if (any (knan(:))) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
52 cdf(knan) = NaN; |
6356 | 53 endif |
54 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
55 k = (x >= n) & !knan; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
56 cdf(k) = 1; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
57 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
58 k = (x >= 1) & (x < n) & !knan; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
59 if (isscalar (n)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
60 cdf(k) = floor (x(k)) / n; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
61 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
62 cdf(k) = floor (x(k)) ./ n(k); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
63 endif |
12495
4675ce154a55
Correctly refer to "discrete uniform" distribution in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
64 |
6356 | 65 endfunction |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
66 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
67 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
68 %!shared x,y |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
69 %! x = [0 1 2.5 10 11]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
70 %! y = [0, 0.1 0.2 1.0 1.0]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
71 %!assert(unidcdf (x, 10*ones(1,5)), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
72 %!assert(unidcdf (x, 10), y); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
73 %!assert(unidcdf (x, 10*[0 1 NaN 1 1]), [NaN 0.1 NaN y(4:5)]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
74 %!assert(unidcdf ([x(1:2) NaN Inf x(5)], 10), [y(1:2) NaN 1 y(5)]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
75 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
76 %% Test class of input preserved |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
77 %!assert(unidcdf ([x, NaN], 10), [y, NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
78 %!assert(unidcdf (single([x, NaN]), 10), single([y, NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
79 %!assert(unidcdf ([x, NaN], single(10)), single([y, NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
80 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
81 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
82 %!error unidcdf () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
83 %!error unidcdf (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
84 %!error unidcdf (1,2,3) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
85 %!error unidcdf (ones(3),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
86 %!error unidcdf (ones(2),ones(3)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
87 %!error unidcdf (i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
88 %!error unidcdf (2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
89 |