annotate scripts/statistics/distributions/unidpdf.m @ 19833:9fc020886ae9

maint: Clean up m-files to follow Octave coding conventions. Try to trim long lines to < 80 chars. Use '##' for single line comments. Use '(...)' around tests for if/elseif/switch/while. Abut cell indexing operator '{' next to variable. Abut array indexing operator '(' next to variable. Use space between negation operator '!' and following expression. Use two newlines between endfunction and start of %!test or %!demo code. Remove unnecessary parens grouping between short-circuit operators. Remove stray extra spaces (typos) between variables and assignment operators. Remove stray extra spaces from ends of lines.
author Rik <rik@octave.org>
date Mon, 23 Feb 2015 14:54:39 -0800
parents 4197fc428c7d
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
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19593
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: 12495
diff changeset
21 ## @deftypefn {Function File} {} unidpdf (@var{x}, @var{n})
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
22 ## For each element of @var{x}, compute the probability density function
12495
4675ce154a55 Correctly refer to "discrete uniform" distribution in documentation.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
23 ## (PDF) at @var{x} of a discrete uniform distribution which assumes
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
24 ## the integer values 1--@var{n} with equal probability.
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
25 ##
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
26 ## Warning: The underlying implementation uses the double class and
19593
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17744
diff changeset
27 ## will only be accurate for @var{n} @leq{} @code{bitmax}
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
28 ## (@w{@math{2^{53} - 1}} on IEEE-754 compatible systems).
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
29 ## @end deftypefn
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
30
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
31 function pdf = unidpdf (x, n)
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
32
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
33 if (nargin != 2)
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
34 print_usage ();
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
35 endif
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
36
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
37 if (! isscalar (n))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
38 [retval, x, n] = common_size (x, n);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
39 if (retval > 0)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
40 error ("unidpdf: X and N must be of common size or scalars");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
41 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
42 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
43
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
44 if (iscomplex (x) || iscomplex (n))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
45 error ("unidpdf: X and N must not be complex");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
46 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
47
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
48 if (isa (x, "single") || isa (n, "single"))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
49 pdf = zeros (size (x), "single");
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
50 else
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
51 pdf = zeros (size (x));
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
52 endif
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
53
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
54 k = isnan (x) | ! (n > 0 & n == fix (n));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
55 pdf(k) = NaN;
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
56
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
57 k = !k & (x >= 1) & (x <= n) & (x == fix (x));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
58 if (isscalar (n))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
59 pdf(k) = 1 / n;
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
60 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
61 pdf(k) = 1 ./ n(k);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
62 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
63
6356
8bb0d11c7a97 [project @ 2007-02-26 14:45:29 by dbateman]
dbateman
parents:
diff changeset
64 endfunction
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
65
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
66
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
67 %!shared x,y
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
68 %! x = [-1 0 1 2 10 11];
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
69 %! y = [0 0 0.1 0.1 0.1 0];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
70 %!assert (unidpdf (x, 10*ones (1,6)), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
71 %!assert (unidpdf (x, 10), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
72 %!assert (unidpdf (x, 10*[0 NaN 1 1 1 1]), [NaN NaN y(3:6)])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
73 %!assert (unidpdf ([x, NaN], 10), [y, NaN])
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
74
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
75 ## 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
76 %!assert (unidpdf (single ([x, NaN]), 10), single ([y, NaN]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
77 %!assert (unidpdf ([x, NaN], single (10)), single ([y, NaN]))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
78
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
79 ## Test input validation
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
80 %!error unidpdf ()
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
81 %!error unidpdf (1)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
82 %!error unidpdf (1,2,3)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
83 %!error unidpdf (ones (3), ones (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
84 %!error unidpdf (ones (2), ones (3))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
85 %!error unidpdf (i, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
86 %!error unidpdf (2, i)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 12495
diff changeset
87