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} {} nbinrnd (@var{n}, @var{p})
22 ## @deftypefnx {Function File} {} nbinrnd (@var{n}, @var{p}, @var{r})
23 ## @deftypefnx {Function File} {} nbinrnd (@var{n}, @var{p}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} nbinrnd (@var{n}, @var{p}, [@var{sz}])
25 ## Return a matrix of random samples from the negative binomial
26 ## distribution with parameters @var{n} and @var{p}.
28 ## When called with a single size argument, return a square matrix with
29 ## the dimension specified. When called with more than one scalar argument the
30 ## first two arguments are taken as the number of rows and columns and any
31 ## further arguments specify additional matrix dimensions. The size may also
32 ## be specified with a vector of dimensions @var{sz}.
34 ## If no size arguments are given then the result matrix is the common size of
35 ## @var{n} and @var{p}.
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from the Pascal distribution
41 function rnd = nbinrnd (n, p, varargin)
47 if (!isscalar (n) || !isscalar (p))
48 [retval, n, p] = common_size (n, p);
50 error ("nbinrnd: N and P must be of common size or scalars");
54 if (iscomplex (n) || iscomplex (p))
55 error ("nbinrnd: N and P must not be complex");
61 if (isscalar (varargin{1}) && varargin{1} >= 0)
62 sz = [varargin{1}, varargin{1}];
63 elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
66 error ("nbinrnd: dimension vector must be row vector of non-negative integers");
69 if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
70 error ("nbinrnd: dimensions must be non-negative integers");
75 if (!isscalar (n) && !isequal (size (n), sz))
76 error ("nbinrnd: N and P must be scalar or of size SZ");
79 if (isa (n, "single") || isa (p, "single"))
85 if (isscalar (n) && isscalar (p))
86 if ((n > 0) && (n < Inf) && (p > 0) && (p <= 1))
87 rnd = randp ((1 - p) ./ p .* randg (n, sz));
88 if (strcmp (cls, "single"))
91 elseif ((n > 0) && (n < Inf) && (p == 0))
92 rnd = zeros (sz, cls);
99 k = (n > 0) & (n < Inf) & (p == 0);
102 k = (n > 0) & (n < Inf) & (p > 0) & (p <= 1);
103 rnd(k) = randp ((1 - p(k)) ./ p(k) .* randg (n(k)));
109 %!assert(size (nbinrnd (2, 1/2)), [1, 1]);
110 %!assert(size (nbinrnd (2*ones(2,1), 1/2)), [2, 1]);
111 %!assert(size (nbinrnd (2*ones(2,2), 1/2)), [2, 2]);
112 %!assert(size (nbinrnd (2, 1/2*ones(2,1))), [2, 1]);
113 %!assert(size (nbinrnd (2, 1/2*ones(2,2))), [2, 2]);
114 %!assert(size (nbinrnd (2, 1/2, 3)), [3, 3]);
115 %!assert(size (nbinrnd (2, 1/2, [4 1])), [4, 1]);
116 %!assert(size (nbinrnd (2, 1/2, 4, 1)), [4, 1]);
118 %% Test class of input preserved
119 %!assert(class (nbinrnd (2, 1/2)), "double");
120 %!assert(class (nbinrnd (single(2), 1/2)), "single");
121 %!assert(class (nbinrnd (single([2 2]), 1/2)), "single");
122 %!assert(class (nbinrnd (2, single(1/2))), "single");
123 %!assert(class (nbinrnd (2, single([1/2 1/2]))), "single");
125 %% Test input validation
128 %!error nbinrnd (ones(3),ones(2))
129 %!error nbinrnd (ones(2),ones(3))
130 %!error nbinrnd (i, 2)
131 %!error nbinrnd (2, i)
132 %!error nbinrnd (1,2, -1)
133 %!error nbinrnd (1,2, ones(2))
134 %!error nbinrnd (1, 2, [2 -1 2])
135 %!error nbinrnd (1,2, 1, ones(2))
136 %!error nbinrnd (1,2, 1, -1)
137 %!error nbinrnd (ones(2,2), 2, 3)
138 %!error nbinrnd (ones(2,2), 2, [3, 2])
139 %!error nbinrnd (ones(2,2), 2, 2, 3)