annotate scripts/statistics/distributions/unidinv.m @ 20595:c1a6c31ac29a

eliminate more simple uses of error_state * ov-classdef.cc: Eliminate simple uses of error_state.
author John W. Eaton <jwe@octave.org>
date Tue, 06 Oct 2015 00:20:02 -0400
parents d9341b422488
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13171
diff changeset
1 ## Copyright (C) 2012 Rik Wehbring
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
2 ## Copyright (C) 2007-2015 David Bateman
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
3 ##
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
4 ## This file is part of Octave.
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
5 ##
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
9 ## your option) any later version.
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
10 ##
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
14 ## General Public License for more details.
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
15 ##
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
18 ## <http://www.gnu.org/licenses/>.
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
19
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
20 ## -*- texinfo -*-
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
21 ## @deftypefn {Function File} {} unidinv (@var{x}, @var{n})
20209
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
22 ## For each element of @var{x}, compute the quantile (the inverse of the CDF)
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19867
diff changeset
23 ## at @var{x} of the discrete uniform distribution which assumes
13171
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
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
25 ## @end deftypefn
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
26
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
27 function inv = unidinv (x, n)
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
28
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
29 if (nargin != 2)
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
30 print_usage ();
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
31 endif
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
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 ("unidinv: 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 inv = NaN (size (x), "single");
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
46 else
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
47 inv = NaN (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 ## For Matlab compatibility, unidinv(0) = NaN
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
51 k = (x > 0) & (x <= 1) & (n > 0 & n == fix (n));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
52 if (isscalar (n))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
53 inv(k) = floor (x(k) * n);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
54 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
55 inv(k) = floor (x(k) .* n(k));
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
56 endif
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
57
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
58 endfunction
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
59
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
60
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
61 %!shared x
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
62 %! x = [-1 0 0.5 1 2];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
63 %!assert (unidinv (x, 10*ones (1,5)), [NaN NaN 5 10 NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
64 %!assert (unidinv (x, 10), [NaN NaN 5 10 NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
65 %!assert (unidinv (x, 10*[0 1 NaN 1 1]), [NaN NaN NaN 10 NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
66 %!assert (unidinv ([x(1:2) NaN x(4:5)], 10), [NaN NaN NaN 10 NaN], eps)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
67
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
68 ## Test class of input preserved
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
69 %!assert (unidinv ([x, NaN], 10), [NaN NaN 5 10 NaN NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
70 %!assert (unidinv (single ([x, NaN]), 10), single ([NaN NaN 5 10 NaN NaN]), eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
71 %!assert (unidinv ([x, NaN], single (10)), single ([NaN NaN 5 10 NaN NaN]), eps)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
72
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
73 ## Test input validation
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
74 %!error unidinv ()
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
75 %!error unidinv (1)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
76 %!error unidinv (1,2,3)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
77 %!error unidinv (ones (3), ones (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
78 %!error unidinv (ones (2), ones (3))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
79 %!error unidinv (i, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
80 %!error unidinv (2, i)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12642
diff changeset
81