Mercurial > hg > octave-nkf
annotate scripts/statistics/distributions/binornd.m @ 8086:83646120b54c
Handle zero values of n in binornd correctly
author | Mark van Rossum <mvanross@inf.ed.ac.uk> |
---|---|
date | Mon, 08 Sep 2008 10:49:46 -0400 |
parents | a1dbe9d80eee |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5410 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5410 | 18 |
19 ## -*- texinfo -*- | |
5411 | 20 ## @deftypefn {Function File} {} binornd (@var{n}, @var{p}, @var{r}, @var{c}) |
21 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{sz}) | |
5410 | 22 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
23 ## random samples from the binomial distribution with parameters @var{n} | |
24 ## and @var{p}. Both @var{n} and @var{p} must be scalar or of size | |
25 ## @var{r} by @var{c}. | |
26 ## | |
27 ## If @var{r} and @var{c} are omitted, the size of the result matrix is | |
28 ## the common size of @var{n} and @var{p}. | |
29 ## @end deftypefn | |
30 | |
5428 | 31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 32 ## Description: Random deviates from the binomial distribution |
33 | |
5411 | 34 function rnd = binornd (n, p, r, c) |
5410 | 35 |
36 if (nargin > 1) | |
37 if (!isscalar(n) || !isscalar(p)) | |
38 [retval, n, p] = common_size (n, p); | |
39 if (retval > 0) | |
5411 | 40 error ("binornd: n and p must be of common size or scalar"); |
5410 | 41 endif |
42 endif | |
43 endif | |
44 | |
45 if (nargin == 4) | |
46 if (! (isscalar (r) && (r > 0) && (r == round (r)))) | |
5411 | 47 error ("binornd: r must be a positive integer"); |
5410 | 48 endif |
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
5411 | 50 error ("binornd: c must be a positive integer"); |
5410 | 51 endif |
52 sz = [r, c]; | |
53 | |
54 if (any (size (n) != 1) | |
55 && (length (size (n)) != length (sz) || any (size (n) != sz))) | |
5411 | 56 error ("binornd: n and must be scalar or of size [r, c]"); |
5410 | 57 endif |
58 elseif (nargin == 3) | |
59 if (isscalar (r) && (r > 0)) | |
60 sz = [r, r]; | |
61 elseif (isvector(r) && all (r > 0)) | |
62 sz = r(:)'; | |
63 else | |
7007 | 64 error ("binornd: r must be a positive integer or vector"); |
5410 | 65 endif |
66 | |
67 if (any (size (n) != 1) | |
68 && (length (size (n)) != length (sz) || any (size (n) != sz))) | |
5411 | 69 error ("binornd: n and must be scalar or of size sz"); |
5410 | 70 endif |
71 elseif (nargin == 2) | |
72 sz = size(n); | |
73 else | |
6046 | 74 print_usage (); |
5410 | 75 endif |
76 | |
77 if (isscalar (n) && isscalar (p)) | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
78 if (find (!(n >= 0) | !(n < Inf) | !(n == round (n)) | |
5410 | 79 !(p >= 0) | !(p <= 1))) |
80 rnd = NaN * ones (sz); | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
81 elseif (n == 0) |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
82 rnd = zeros (sz); |
5410 | 83 else |
84 nel = prod (sz); | |
85 tmp = rand (n, nel); | |
86 rnd = sum(tmp < ones (n, nel) * p, 1); | |
87 rnd = reshape(rnd, sz); | |
88 endif | |
89 else | |
90 rnd = zeros (sz); | |
91 | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
92 k = find (!(n >= 0) | !(n < Inf) | !(n == round (n)) | |
5410 | 93 !(p >= 0) | !(p <= 1)); |
94 if (any (k)) | |
95 rnd(k) = NaN; | |
96 endif | |
97 | |
98 k = find ((n > 0) & (n < Inf) & (n == round (n)) & (p >= 0) & (p <= 1)); | |
99 if (any (k)) | |
100 N = max (n(k)); | |
101 L = length (k); | |
102 tmp = rand (N, L); | |
103 ind = (1 : N)' * ones (1, L); | |
104 rnd(k) = sum ((tmp < ones (N, 1) * p(k)(:)') & | |
105 (ind <= ones (N, 1) * n(k)(:)'),1); | |
106 endif | |
107 endif | |
108 | |
109 endfunction | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
110 |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
111 %!assert (binornd(0, 0, 1), 0) |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
112 %!assert (binornd([0, 0], [0, 0], 1, 2), [0, 0]) |