]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/betarnd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / betarnd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} betarnd (@var{a}, @var{b})
22 ## @deftypefnx {Function File} {} betarnd (@var{a}, @var{b}, @var{r})
23 ## @deftypefnx {Function File} {} betarnd (@var{a}, @var{b}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} betarnd (@var{a}, @var{b}, [@var{sz}])
25 ## Return a matrix of random samples from the Beta distribution with parameters
26 ## @var{a} and @var{b}.
27 ##
28 ## When called with a single size argument, return a square matrix with
29 ## the dimension specified.  When called with more than one scalar argument the
30 ## first two arguments are taken as the number of rows and columns and any
31 ## further arguments specify additional matrix dimensions.  The size may also
32 ## be specified with a vector of dimensions @var{sz}.
33 ## 
34 ## If no size arguments are given then the result matrix is the common size of
35 ## @var{a} and @var{b}.
36 ## @end deftypefn
37
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from the Beta distribution
40
41 function rnd = betarnd (a, b, varargin)
42
43   if (nargin < 2)
44     print_usage ();
45   endif
46
47   if (!isscalar (a) || !isscalar (b))
48     [retval, a, b] = common_size (a, b);
49     if (retval > 0)
50       error ("betarnd: A and B must be of common size or scalars");
51     endif
52   endif
53
54   if (iscomplex (a) || iscomplex (b))
55     error ("betarnd: A and B must not be complex");
56   endif
57
58   if (nargin == 2)
59     sz = size (a);
60   elseif (nargin == 3)
61     if (isscalar (varargin{1}) && varargin{1} >= 0)
62       sz = [varargin{1}, varargin{1}];
63     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
64       sz = varargin{1};
65     else
66       error ("betarnd: dimension vector must be row vector of non-negative integers");
67     endif
68   elseif (nargin > 3)
69     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
70       error ("betarnd: dimensions must be non-negative integers");
71     endif
72     sz = [varargin{:}];
73   endif
74
75   if (!isscalar (a) && !isequal (size (a), sz))
76     error ("betarnd: A and B must be scalar or of size SZ");
77   endif
78
79   if (isa (a, "single") || isa (b, "single"))
80     cls = "single";
81   else
82     cls = "double";
83   endif
84
85   if (isscalar (a) && isscalar (b))
86     if ((a > 0) && (a < Inf) && (b > 0) && (b < Inf))
87       r = randg (a, sz);
88       rnd = r ./ (r + randg (b, sz));
89       if (strcmp (cls, "single"))
90         rnd = single (rnd);
91       endif
92     else
93       rnd = NaN (sz, cls);
94     endif
95   else
96     rnd = NaN (sz, cls);
97
98     k = (a > 0) & (a < Inf) & (b > 0) & (b < Inf);
99     r = randg (a(k));
100     rnd(k) = r ./ (r + randg (b(k)));
101   endif
102
103 endfunction
104
105
106 %!assert(size (betarnd (1,2)), [1, 1]);
107 %!assert(size (betarnd (ones(2,1), 2)), [2, 1]);
108 %!assert(size (betarnd (ones(2,2), 2)), [2, 2]);
109 %!assert(size (betarnd (1, 2*ones(2,1))), [2, 1]);
110 %!assert(size (betarnd (1, 2*ones(2,2))), [2, 2]);
111 %!assert(size (betarnd (1, 2, 3)), [3, 3]);
112 %!assert(size (betarnd (1, 2, [4 1])), [4, 1]);
113 %!assert(size (betarnd (1, 2, 4, 1)), [4, 1]);
114
115 %% Test class of input preserved
116 %!assert(class (betarnd (1, 2)), "double");
117 %!assert(class (betarnd (single(1), 2)), "single");
118 %!assert(class (betarnd (single([1 1]), 2)), "single");
119 %!assert(class (betarnd (1, single(2))), "single");
120 %!assert(class (betarnd (1, single([2 2]))), "single");
121
122 %% Test input validation
123 %!error betarnd ()
124 %!error betarnd (1)
125 %!error betarnd (ones(3),ones(2))
126 %!error betarnd (ones(2),ones(3))
127 %!error betarnd (i, 2)
128 %!error betarnd (2, i)
129 %!error betarnd (1,2, -1)
130 %!error betarnd (1,2, ones(2))
131 %!error binornd (1,2, [2 -1 2])
132 %!error betarnd (1,2, 1, ones(2))
133 %!error betarnd (1,2, 1, -1)
134 %!error betarnd (ones(2,2), 2, 3)
135 %!error betarnd (ones(2,2), 2, [3, 2])
136 %!error betarnd (ones(2,2), 2, 2, 3)
137