5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} binornd (@var{n}, @var{p}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
|
24 ## random samples from the binomial distribution with parameters @var{n} |
|
25 ## and @var{p}. Both @var{n} and @var{p} must be scalar or of size |
|
26 ## @var{r} by @var{c}. |
|
27 ## |
|
28 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
29 ## the common size of @var{n} and @var{p}. |
|
30 ## @end deftypefn |
|
31 |
5428
|
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
33 ## Description: Random deviates from the binomial distribution |
|
34 |
5411
|
35 function rnd = binornd (n, p, r, c) |
5410
|
36 |
|
37 if (nargin > 1) |
|
38 if (!isscalar(n) || !isscalar(p)) |
|
39 [retval, n, p] = common_size (n, p); |
|
40 if (retval > 0) |
5411
|
41 error ("binornd: n and p must be of common size or scalar"); |
5410
|
42 endif |
|
43 endif |
|
44 endif |
|
45 |
|
46 if (nargin == 4) |
|
47 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
48 error ("binornd: r must be a positive integer"); |
5410
|
49 endif |
|
50 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
51 error ("binornd: c must be a positive integer"); |
5410
|
52 endif |
|
53 sz = [r, c]; |
|
54 |
|
55 if (any (size (n) != 1) |
|
56 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
5411
|
57 error ("binornd: n and must be scalar or of size [r, c]"); |
5410
|
58 endif |
|
59 elseif (nargin == 3) |
|
60 if (isscalar (r) && (r > 0)) |
|
61 sz = [r, r]; |
|
62 elseif (isvector(r) && all (r > 0)) |
|
63 sz = r(:)'; |
|
64 else |
5411
|
65 error ("binornd: r must be a postive integer or vector"); |
5410
|
66 endif |
|
67 |
|
68 if (any (size (n) != 1) |
|
69 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
5411
|
70 error ("binornd: n and must be scalar or of size sz"); |
5410
|
71 endif |
|
72 elseif (nargin == 2) |
|
73 sz = size(n); |
|
74 else |
5411
|
75 usage ("binornd (n, p, r, c)"); |
5410
|
76 endif |
|
77 |
|
78 if (isscalar (n) && isscalar (p)) |
|
79 if (find (!(n > 0) | !(n < Inf) | !(n == round (n)) | |
|
80 !(p >= 0) | !(p <= 1))) |
|
81 rnd = NaN * ones (sz); |
|
82 else |
|
83 nel = prod (sz); |
|
84 tmp = rand (n, nel); |
|
85 rnd = sum(tmp < ones (n, nel) * p, 1); |
|
86 rnd = reshape(rnd, sz); |
|
87 endif |
|
88 else |
|
89 rnd = zeros (sz); |
|
90 |
|
91 k = find (!(n > 0) | !(n < Inf) | !(n == round (n)) | |
|
92 !(p >= 0) | !(p <= 1)); |
|
93 if (any (k)) |
|
94 rnd(k) = NaN; |
|
95 endif |
|
96 |
|
97 k = find ((n > 0) & (n < Inf) & (n == round (n)) & (p >= 0) & (p <= 1)); |
|
98 if (any (k)) |
|
99 N = max (n(k)); |
|
100 L = length (k); |
|
101 tmp = rand (N, L); |
|
102 ind = (1 : N)' * ones (1, L); |
|
103 rnd(k) = sum ((tmp < ones (N, 1) * p(k)(:)') & |
|
104 (ind <= ones (N, 1) * n(k)(:)'),1); |
|
105 endif |
|
106 endif |
|
107 |
|
108 endfunction |