comparison toolboxes/FullBNT-1.0.7/KPMstats/beta_sample.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function r = betarnd(a,b,m,n);
2 %BETARND Random matrices from beta distribution.
3 % R = BETARND(A,B) returns a matrix of random numbers chosen
4 % from the beta distribution with parameters A and B.
5 % The size of R is the common size of A and B if both are matrices.
6 % If either parameter is a scalar, the size of R is the size of the other
7 % parameter. Alternatively, R = BETARND(A,B,M,N) returns an M by N matrix.
8
9 % Reference:
10 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
11 % Springer-Verlag, 1986
12
13 % Copyright (c) 1993-98 by The MathWorks, Inc.
14 % $Revision: 1.1.1.1 $ $Date: 2005/04/26 02:29:18 $
15
16 if nargin < 2,
17 error('Requires at least two input arguments');
18 end
19
20 if nargin == 2
21 [errorcode rows columns] = rndcheck(2,2,a,b);
22 end
23
24 if nargin == 3
25 [errorcode rows columns] = rndcheck(3,2,a,b,m);
26 end
27
28 if nargin == 4
29 [errorcode rows columns] = rndcheck(4,2,a,b,m,n);
30 end
31
32 if errorcode > 0
33 error('Size information is inconsistent.');
34 end
35
36 r = zeros(rows,columns);
37
38 % Use Theorem 4.1, case A (Devroye, page 430) to derive beta
39 % random numbers as a ratio of gamma random numbers.
40 if prod(size(a)) == 1
41 a1 = a(ones(rows,1),ones(columns,1));
42 g1 = gamrnd(a1,1);
43 else
44 g1 = gamrnd(a,1);
45 end
46 if prod(size(b)) == 1
47 b1 = b(ones(rows,1),ones(columns,1));
48 g2 = gamrnd(b1,1);
49 else
50 g2 = gamrnd(b,1);
51 end
52 r = g1 ./ (g1 + g2);
53
54 % Return NaN if b is not positive.
55 if any(any(b <= 0));
56 if prod(size(b) == 1)
57 tmp = NaN;
58 r = tmp(ones(rows,columns));
59 else
60 k = find(b <= 0);
61 tmp = NaN;
62 r(k) = tmp(ones(size(k)));
63 end
64 end
65
66 % Return NaN if a is not positive.
67 if any(any(a <= 0));
68 if prod(size(a) == 1)
69 tmp = NaN;
70 r = tmp(ones(rows,columns));
71 else
72 k = find(a <= 0);
73 tmp = NaN;
74 r(k) = tmp(ones(size(k)));
75 end
76 end