3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3191
|
19 |
3456
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} gamma_rnd (@var{a}, @var{b}, @var{r}, @var{c}) |
4854
|
22 ## @deftypefnx {Function File} {} gamma_rnd (@var{a}, @var{b}, @var{sz}) |
|
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}. |
3191
|
27 ## |
3456
|
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 |
3426
|
31 |
3456
|
32 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
33 ## Description: Random deviates from the Gamma distribution |
3426
|
34 |
3191
|
35 function rnd = gamma_rnd (a, b, r, c) |
|
36 |
4854
|
37 if (nargin > 1) |
|
38 if (!isscalar(a) || !isscalar(b)) |
|
39 [retval, a, b] = common_size (a, b); |
|
40 if (retval > 0) |
|
41 error ("gamma_rnd: a and b must be of common size or scalar"); |
|
42 endif |
|
43 endif |
|
44 endif |
|
45 |
3191
|
46 if (nargin == 4) |
4030
|
47 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
3456
|
48 error ("gamma_rnd: r must be a positive integer"); |
3191
|
49 endif |
4030
|
50 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
3456
|
51 error ("gamma_rnd: c must be a positive integer"); |
3191
|
52 endif |
4854
|
53 sz = [r, c]; |
4856
|
54 |
|
55 if (any (size (a) != 1) && ((length (size (a)) != length (sz)) || |
|
56 any (size (a) != sz))) |
|
57 error ("gamma_rnd: a and b must be scalar or of size [r, c]"); |
|
58 endif |
4854
|
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 |
|
65 error ("uniform_rnd: r must be a postive integer or vector"); |
3191
|
66 endif |
4856
|
67 |
|
68 if (any (size (a) != 1) && ((length (size (a)) != length (sz)) || |
|
69 any (size (a) != sz))) |
|
70 error ("gamma_rnd: a and b must be scalar or of size sz"); |
|
71 endif |
3191
|
72 elseif (nargin == 2) |
4854
|
73 sz = size(a); |
3191
|
74 else |
3456
|
75 usage ("gamma_rnd (a, b, r, c)"); |
3191
|
76 endif |
|
77 |
4854
|
78 rnd = zeros (sz); |
3426
|
79 |
4854
|
80 if (isscalar (a) && isscalar(b)) |
|
81 if (find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf))) |
|
82 rnd = NaN * ones (sz); |
|
83 else |
|
84 rnd = gamma_inv (rand (sz), a, b); |
|
85 endif |
|
86 else |
|
87 k = find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf)); |
|
88 if (any (k)) |
|
89 rnd(k) = NaN; |
|
90 endif |
|
91 k = find ((a > 0) & (a < Inf) & (b > 0) & (b < Inf)); |
|
92 if (any (k)) |
|
93 rnd(k) = gamma_inv (rand (size (k)), a(k), b(k)); |
|
94 endif |
3191
|
95 endif |
3426
|
96 |
4854
|
97 endfunction |