7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2007 Kurt Hornik |
6349
|
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. |
6349
|
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/>. |
6349
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} nbinrnd (@var{n}, @var{p}, @var{r}, @var{c}) |
|
21 ## @deftypefnx {Function File} {} nbinrnd (@var{n}, @var{p}, @var{sz}) |
|
22 ## Return an @var{r} by @var{c} matrix of random samples from the Pascal |
|
23 ## (negative binomial) distribution with parameters @var{n} and @var{p}. |
|
24 ## Both @var{n} and @var{p} must be scalar or of size @var{r} by @var{c}. |
|
25 ## |
|
26 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
27 ## the common size of @var{n} and @var{p}. Or if @var{sz} is a vector, |
|
28 ## create a matrix of size @var{sz}. |
|
29 ## @end deftypefn |
|
30 |
|
31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
|
32 ## Description: Random deviates from the Pascal distribution |
|
33 |
|
34 function rnd = nbinrnd (n, p, r, c) |
|
35 |
|
36 if (nargin > 1) |
|
37 if (!isscalar(n) || !isscalar(p)) |
|
38 [retval, n, p] = common_size (n, p); |
|
39 if (retval > 0) |
|
40 error ("nbinrnd: n and p must be of common size or scalar"); |
|
41 endif |
|
42 endif |
|
43 endif |
|
44 |
|
45 if (nargin == 4) |
|
46 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
|
47 error ("nbinrnd: r must be a positive integer"); |
|
48 endif |
|
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
|
50 error ("nbinrnd: c must be a positive integer"); |
|
51 endif |
|
52 sz = [r, c]; |
|
53 |
|
54 if (any (size (n) != 1) && |
|
55 ((length (size (n)) != length (sz)) || any (size (n) != sz))) |
|
56 error ("nbinrnd: n and p must be scalar or of size [r, c]"); |
|
57 endif |
|
58 |
|
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 |
7007
|
65 error ("nbinrnd: r must be a positive integer or vector"); |
6349
|
66 endif |
|
67 |
|
68 if (any (size (n) != 1) && |
|
69 ((length (size (n)) != length (sz)) || any (size (n) != sz))) |
|
70 error ("nbinrnd: n and p must be scalar or of size sz"); |
|
71 endif |
|
72 elseif (nargin == 2) |
|
73 sz = size(n); |
|
74 else |
|
75 print_usage (); |
|
76 endif |
|
77 |
|
78 if (isscalar (n) && isscalar (p)) |
|
79 if ((n < 1) || (n == Inf) || (n != round (n)) || (p <= 0) || (p > 1)); |
|
80 rnd = NaN * ones (sz); |
|
81 elseif ((n > 0) && (n < Inf) && (n == round (n)) && |
|
82 (p > 0) && (p <= 1)) |
|
83 rnd = randp ((1 - p) ./ p .* randg (n, sz)); |
|
84 else |
|
85 rnd = zeros (sz); |
|
86 endif |
|
87 else |
|
88 rnd = zeros (sz); |
|
89 |
|
90 k = find ((n < 1) || (n == Inf) || (n != round (n)) || |
|
91 (p <= 0) || (p > 1)); |
|
92 if (any (k)) |
|
93 rnd(k) = NaN; |
|
94 endif |
|
95 |
|
96 k = find ((n > 0) & (n < Inf) & (n == round (n)) & (p > 0) & (p <= 1)); |
|
97 if (any (k)) |
|
98 rnd(k) = randp ((1 - p(k)) ./ p(k) .* randg (n(k), size(k))); |
|
99 endif |
|
100 endif |
|
101 |
|
102 endfunction |