Mercurial > octave-nkf
annotate scripts/statistics/distributions/betarnd.m @ 10549:95c3e38098bf
Untabify .m scripts
author | Rik <code@nomad.inbox5.com> |
---|---|
date | Fri, 23 Apr 2010 11:28:50 -0700 |
parents | 3306cfcb856e |
children | be55736a0783 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410 | 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. | |
5410 | 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/>. | |
5410 | 18 |
19 ## -*- texinfo -*- | |
5411 | 20 ## @deftypefn {Function File} {} betarnd (@var{a}, @var{b}, @var{r}, @var{c}) |
21 ## @deftypefnx {Function File} {} betarnd (@var{a}, @var{b}, @var{sz}) | |
5410 | 22 ## Return an @var{r} by @var{c} or @code{size (@var{sz})} matrix of |
23 ## random samples from the Beta distribution with parameters @var{a} and | |
24 ## @var{b}. Both @var{a} and @var{b} must be scalar or of size @var{r} | |
25 ## by @var{c}. | |
26 ## | |
27 ## If @var{r} and @var{c} are omitted, the size of the result matrix is | |
28 ## the common size of @var{a} and @var{b}. | |
29 ## @end deftypefn | |
30 | |
5428 | 31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 32 ## Description: Random deviates from the Beta distribution |
33 | |
5411 | 34 function rnd = betarnd (a, b, r, c) |
5410 | 35 |
36 if (nargin > 1) | |
37 if (!isscalar(a) || !isscalar(b)) | |
38 [retval, a, b] = common_size (a, b); | |
39 if (retval > 0) | |
10549 | 40 error ("betarnd: a and b must be of common size or scalar"); |
5410 | 41 endif |
42 endif | |
43 endif | |
44 | |
45 if (nargin == 4) | |
46 if (! (isscalar (r) && (r > 0) && (r == round (r)))) | |
5411 | 47 error ("betarnd: r must be a positive integer"); |
5410 | 48 endif |
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
5411 | 50 error ("betarnd: c must be a positive integer"); |
5410 | 51 endif |
52 sz = [r, c]; | |
53 | |
54 if (any (size (a) != 1) | |
10549 | 55 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411 | 56 error ("betarnd: a and b must be scalar or of size [r,c]"); |
5410 | 57 endif |
58 elseif (nargin == 3) | |
59 if (isscalar (r) && (r > 0)) | |
60 sz = [r, r]; | |
61 elseif (isvector(r) && all (r > 0)) | |
62 sz = r(:)'; | |
63 else | |
7007 | 64 error ("betarnd: r must be a positive integer or vector"); |
5410 | 65 endif |
66 | |
67 if (any (size (a) != 1) | |
10549 | 68 && (length (size (a)) != length (sz) || any (size (a) != sz))) |
5411 | 69 error ("betarnd: a and b must be scalar or of size sz"); |
5410 | 70 endif |
71 elseif (nargin == 2) | |
72 sz = size(a); | |
73 else | |
6046 | 74 print_usage (); |
5410 | 75 endif |
76 | |
77 if (isscalar(a) && isscalar(b)) | |
78 if (find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf))) | |
10525
3306cfcb856e
Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
79 rnd = NaN (sz); |
5410 | 80 else |
6339 | 81 r1 = randg(a,sz); |
82 rnd = r1 ./ (r1 + randg(b,sz)); | |
5410 | 83 endif |
84 else | |
85 rnd = zeros (sz); | |
86 | |
87 k = find (!(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf)); | |
88 if (any (k)) | |
10525
3306cfcb856e
Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
89 rnd(k) = NaN (size (k)); |
5410 | 90 endif |
91 | |
92 k = find ((a > 0) & (a < Inf) & (b > 0) & (b < Inf)); | |
93 if (any (k)) | |
6339 | 94 r1 = randg(a(k),size(k)); |
95 rnd(k) = r1 ./ (r1 + randg(b(k),size(k))); | |
5410 | 96 endif |
97 endif | |
98 | |
99 endfunction |