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