1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} binornd (@var{n}, @var{p})
22 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{r})
23 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, [@var{sz}])
25 ## Return a matrix of random samples from the binomial distribution with
26 ## parameters @var{n} and @var{p}, where @var{n} is the number of trials
27 ## and @var{p} is the probability of success.
29 ## When called with a single size argument, return a square matrix with
30 ## the dimension specified. When called with more than one scalar argument the
31 ## first two arguments are taken as the number of rows and columns and any
32 ## further arguments specify additional matrix dimensions. The size may also
33 ## be specified with a vector of dimensions @var{sz}.
35 ## If no size arguments are given then the result matrix is the common size of
36 ## @var{n} and @var{p}.
39 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
40 ## Description: Random deviates from the binomial distribution
42 function rnd = binornd (n, p, varargin)
48 if (!isscalar (n) || !isscalar (p))
49 [retval, n, p] = common_size (n, p);
51 error ("binornd: N and P must be of common size or scalars");
55 if (iscomplex (n) || iscomplex (p))
56 error ("binornd: N and P must not be complex");
62 if (isscalar (varargin{1}) && varargin{1} >= 0)
63 sz = [varargin{1}, varargin{1}];
64 elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
67 error ("binornd: dimension vector must be row vector of non-negative integers");
70 if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
71 error ("binornd: dimensions must be non-negative integers");
76 if (!isscalar (n) && !isequal (size (n), sz))
77 error ("binornd: N and P must be scalar or of size SZ");
80 if (isa (n, "single") || isa (p, "single"))
86 if (isscalar (n) && isscalar (p))
87 if ((n > 0) && (n < Inf) && (n == fix (n)) && (p >= 0) && (p <= 1))
90 rnd = sum (tmp < p, 1);
91 rnd = reshape (rnd, sz);
92 if (strcmp (cls, "single"))
95 elseif ((n == 0) && (p >= 0) && (p <= 1))
96 rnd = zeros (sz, cls);
101 rnd = zeros (sz, cls);
103 k = !(n >= 0) | !(n < Inf) | !(n == fix (n)) | !(p >= 0) | !(p <= 1);
106 k = (n > 0) & (n < Inf) & (n == fix (n)) & (p >= 0) & (p <= 1);
111 ind = repmat ((1 : N)', 1, L);
112 rnd(k) = sum ((tmp < repmat (p(k)(:)', N, 1)) &
113 (ind <= repmat (n(k)(:)', N, 1)), 1);
120 %!assert (binornd (0, 0, 1), 0)
121 %!assert (binornd ([0, 0], [0, 0], 1, 2), [0, 0])
123 %!assert(size (binornd (2, 1/2)), [1, 1]);
124 %!assert(size (binornd (2*ones(2,1), 1/2)), [2, 1]);
125 %!assert(size (binornd (2*ones(2,2), 1/2)), [2, 2]);
126 %!assert(size (binornd (2, 1/2*ones(2,1))), [2, 1]);
127 %!assert(size (binornd (2, 1/2*ones(2,2))), [2, 2]);
128 %!assert(size (binornd (2, 1/2, 3)), [3, 3]);
129 %!assert(size (binornd (2, 1/2, [4 1])), [4, 1]);
130 %!assert(size (binornd (2, 1/2, 4, 1)), [4, 1]);
132 %% Test class of input preserved
133 %!assert(class (binornd (2, 0.5)), "double");
134 %!assert(class (binornd (single(2), 0.5)), "single");
135 %!assert(class (binornd (single([2 2]), 0.5)), "single");
136 %!assert(class (binornd (2, single(0.5))), "single");
137 %!assert(class (binornd (2, single([0.5 0.5]))), "single");
139 %% Test input validation
142 %!error binornd (ones(3),ones(2))
143 %!error binornd (ones(2),ones(3))
144 %!error binornd (i, 2)
145 %!error binornd (2, i)
146 %!error binornd (1,2, -1)
147 %!error binornd (1,2, ones(2))
148 %!error binornd (1,2, [2 -1 2])
149 %!error binornd (1,2, 1, ones(2))
150 %!error binornd (1,2, 1, -1)
151 %!error binornd (ones(2,2), 2, 3)
152 %!error binornd (ones(2,2), 2, [3, 2])
153 %!error binornd (ones(2,2), 2, 2, 3)