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} {} gamrnd (@var{a}, @var{b}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} gamrnd (@var{a}, @var{b}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
|
24 ## random samples from the Gamma distribution with parameters @var{a} |
|
25 ## and @var{b}. Both @var{a} and @var{b} 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{a} and @var{b}. |
|
30 ## @end deftypefn |
5623
|
31 ## |
|
32 ## @seealso{gamma, gammaln, gammainc, gampdf, gamcdf, gaminv} |
5410
|
33 |
5428
|
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
35 ## Description: Random deviates from the Gamma distribution |
|
36 |
5411
|
37 function rnd = gamrnd (a, b, r, c) |
5410
|
38 |
|
39 if (nargin > 1) |
|
40 if (!isscalar(a) || !isscalar(b)) |
|
41 [retval, a, b] = common_size (a, b); |
|
42 if (retval > 0) |
5411
|
43 error ("gamrnd: a and b must be of common size or scalar"); |
5410
|
44 endif |
|
45 endif |
|
46 endif |
|
47 |
|
48 if (nargin == 4) |
|
49 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
50 error ("gamrnd: r must be a positive integer"); |
5410
|
51 endif |
|
52 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
53 error ("gamrnd: c must be a positive integer"); |
5410
|
54 endif |
|
55 sz = [r, c]; |
|
56 |
|
57 if (any (size (a) != 1) |
|
58 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411
|
59 error ("gamrnd: a and b must be scalar or of size [r, c]"); |
5410
|
60 endif |
|
61 elseif (nargin == 3) |
|
62 if (isscalar (r) && (r > 0)) |
|
63 sz = [r, r]; |
|
64 elseif (isvector(r) && all (r > 0)) |
|
65 sz = r(:)'; |
|
66 else |
5411
|
67 error ("gamrnd: r must be a postive integer or vector"); |
5410
|
68 endif |
|
69 |
|
70 if (any (size (a) != 1) |
|
71 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411
|
72 error ("gamrnd: a and b must be scalar or of size sz"); |
5410
|
73 endif |
|
74 elseif (nargin == 2) |
|
75 sz = size(a); |
|
76 else |
5411
|
77 usage ("gamrnd (a, b, r, c)"); |
5410
|
78 endif |
|
79 |
|
80 rnd = zeros (sz); |
|
81 |
|
82 if (isscalar (a) && isscalar(b)) |
|
83 if (find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf))) |
|
84 rnd = NaN * ones (sz); |
|
85 else |
5411
|
86 rnd = gaminv (rand (sz), a, b); |
5410
|
87 endif |
|
88 else |
|
89 k = find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf)); |
|
90 if (any (k)) |
|
91 rnd(k) = NaN; |
|
92 endif |
|
93 k = find ((a > 0) & (a < Inf) & (b > 0) & (b < Inf)); |
|
94 if (any (k)) |
5411
|
95 rnd(k) = gaminv (rand (size (k)), a(k), b(k)); |
5410
|
96 endif |
|
97 endif |
|
98 |
|
99 endfunction |