annotate scripts/statistics/distributions/hygecdf.m @ 19867: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 d9341b422488
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) 1997-2015 Kurt Hornik
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
3 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
5 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
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: 7002
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: 7002
diff changeset
9 ## your option) any later version.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
10 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
15 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
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: 7002
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: 7002
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
19
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
7002
12ab7f5fc663 [project @ 2007-10-10 18:27:06 by jwe]
jwe
parents: 6046
diff changeset
21 ## @deftypefn {Function File} {} hygecdf (@var{x}, @var{t}, @var{m}, @var{n})
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
22 ## Compute the cumulative distribution function (CDF) at @var{x} of the
7002
12ab7f5fc663 [project @ 2007-10-10 18:27:06 by jwe]
jwe
parents: 6046
diff changeset
23 ## hypergeometric distribution with parameters @var{t}, @var{m}, and
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
24 ## @var{n}. This is the probability of obtaining not more than @var{x}
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
25 ## marked items when randomly drawing a sample of size @var{n} without
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
26 ## replacement from a population of total size @var{t} containing
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
27 ## @var{m} marked items.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
28 ##
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
29 ## The parameters @var{t}, @var{m}, and @var{n} must be positive integers
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
30 ## with @var{m} and @var{n} not greater than @var{t}.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
31 ## @end deftypefn
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
32
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5411
diff changeset
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
34 ## Description: CDF of the hypergeometric distribution
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
35
7002
12ab7f5fc663 [project @ 2007-10-10 18:27:06 by jwe]
jwe
parents: 6046
diff changeset
36 function cdf = hygecdf (x, t, m, n)
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
37
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
38 if (nargin != 4)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
39 print_usage ();
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
40 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
41
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
42 if (! isscalar (t) || ! isscalar (m) || ! isscalar (n))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
43 [retval, x, t, m, n] = common_size (x, t, m, n);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
44 if (retval > 0)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
45 error ("hygecdf: X, T, M, and N must be of common size or scalars");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
46 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
47 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
48
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
49 if (iscomplex (x) || iscomplex (t) || iscomplex (m) || iscomplex (n))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
50 error ("hygecdf: X, T, M, and N must not be complex");
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
51 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
52
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
53 if (isa (x, "single") || isa (t, "single")
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
54 || isa (m, "single") || isa (n, "single"))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
55 cdf = NaN (size (x), "single");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
56 else
11589
b0084095098e missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
57 cdf = NaN (size (x));
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
58 endif
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
59
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
60 ok = ((t >= 0) & (m >= 0) & (n > 0) & (m <= t) & (n <= t) &
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
61 (t == fix (t)) & (m == fix (m)) & (n == fix (n)));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
62
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
63 if (isscalar (t))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
64 if (ok)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
65 cdf = discrete_cdf (x, 0 : n, hygepdf (0 : n, t, m, n));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
66 endif
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
67 else
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
68 for i = find (ok(:)') # Must be row vector arg to for loop
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
69 v = 0 : n(i);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
70 cdf(i) = discrete_cdf (x(i), v, hygepdf (v, t(i), m(i), n(i)));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
71 endfor
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
72 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
73
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
74 endfunction
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
75
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
76
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
77 %!shared x,y
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
78 %! x = [-1 0 1 2 3];
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
79 %! y = [0 1/6 5/6 1 1];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
80 %!assert (hygecdf (x, 4*ones (1,5), 2, 2), y, eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
81 %!assert (hygecdf (x, 4, 2*ones (1,5), 2), y, eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
82 %!assert (hygecdf (x, 4, 2, 2*ones (1,5)), y, eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
83 %!assert (hygecdf (x, 4*[1 -1 NaN 1.1 1], 2, 2), [y(1) NaN NaN NaN y(5)], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
84 %!assert (hygecdf (x, 4, 2*[1 -1 NaN 1.1 1], 2), [y(1) NaN NaN NaN y(5)], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
85 %!assert (hygecdf (x, 4, 5, 2), [NaN NaN NaN NaN NaN])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
86 %!assert (hygecdf (x, 4, 2, 2*[1 -1 NaN 1.1 1]), [y(1) NaN NaN NaN y(5)], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
87 %!assert (hygecdf (x, 4, 2, 5), [NaN NaN NaN NaN NaN])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
88 %!assert (hygecdf ([x(1:2) NaN x(4:5)], 4, 2, 2), [y(1:2) NaN y(4:5)], eps)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
89
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
90 ## 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
91 %!assert (hygecdf ([x, NaN], 4, 2, 2), [y, NaN], eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
92 %!assert (hygecdf (single ([x, NaN]), 4, 2, 2), single ([y, NaN]), eps ("single"))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
93 %!assert (hygecdf ([x, NaN], single (4), 2, 2), single ([y, NaN]), eps ("single"))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
94 %!assert (hygecdf ([x, NaN], 4, single (2), 2), single ([y, NaN]), eps ("single"))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
95 %!assert (hygecdf ([x, NaN], 4, 2, single (2)), single ([y, NaN]), eps ("single"))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
96
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
97 ## Test input validation
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
98 %!error hygecdf ()
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
99 %!error hygecdf (1)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
100 %!error hygecdf (1,2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
101 %!error hygecdf (1,2,3)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
102 %!error hygecdf (1,2,3,4,5)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
103 %!error hygecdf (ones (2), ones (3), 1, 1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
104 %!error hygecdf (1, ones (2), ones (3), 1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
105 %!error hygecdf (1, 1, ones (2), ones (3))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
106 %!error hygecdf (i, 2, 2, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
107 %!error hygecdf (2, i, 2, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
108 %!error hygecdf (2, 2, i, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
109 %!error hygecdf (2, 2, 2, i)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11589
diff changeset
110