comparison toolboxes/FullBNT-1.0.7/KPMstats/gamma_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 = gamrnd(a,b,m,n);
2 %GAMRND Random matrices from gamma distribution.
3 % R = GAMRND(A,B) returns a matrix of random numbers chosen
4 % from the gamma 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 = GAMRND(A,B,M,N) returns an M by N matrix.
8 %
9 % Some references refer to the gamma distribution
10 % with a single parameter. This corresponds to GAMRND
11 % with B = 1. (See Devroye, pages 401-402.)
12
13 % GAMRND uses a rejection or an inversion method depending on the
14 % value of A.
15
16 % References:
17 % [1] L. Devroye, "Non-Uniform Random Variate Generation",
18 % Springer-Verlag, 1986
19
20 % B.A. Jones 2-1-93
21 % Copyright (c) 1993-98 by The MathWorks, Inc.
22 % $Revision: 1.1.1.1 $ $Date: 2005/04/26 02:29:18 $
23
24 if nargin < 2,
25 error('Requires at least two input arguments.');
26 end
27
28
29 if nargin == 2
30 [errorcode rows columns] = rndcheck(2,2,a,b);
31 end
32
33 if nargin == 3
34 [errorcode rows columns] = rndcheck(3,2,a,b,m);
35 end
36
37 if nargin == 4
38 [errorcode rows columns] = rndcheck(4,2,a,b,m,n);
39 end
40
41 if errorcode > 0
42 error('Size information is inconsistent.');
43 end
44
45 % Initialize r to zero.
46 lth = rows*columns;
47 r = zeros(lth,1);
48 a = a(:); b = b(:);
49
50 scalara = (length(a) == 1);
51 if scalara
52 a = a*ones(lth,1);
53 end
54
55 scalarb = (length(b) == 1);
56 if scalarb
57 b = b*ones(lth,1);
58 end
59
60 % If a == 1, then gamma is exponential. (Devroye, page 405).
61 k = find(a == 1);
62 if any(k)
63 r(k) = -b(k) .* log(rand(size(k)));
64 end
65
66
67 k = find(a < 1 & a > 0);
68 % (Devroye, page 418 Johnk's generator)
69 if any(k)
70 c = zeros(lth,1);
71 d = zeros(lth,1);
72 c(k) = 1 ./ a(k);
73 d(k) = 1 ./ (1 - a(k));
74 accept = k;
75 while ~isempty(accept)
76 u = rand(size(accept));
77 v = rand(size(accept));
78 x = u .^ c(accept);
79 y = v .^ d(accept);
80 k1 = find((x + y) <= 1);
81 if ~isempty(k1)
82 e = -log(rand(size(k1)));
83 r(accept(k1)) = e .* x(k1) ./ (x(k1) + y(k1));
84 accept(k1) = [];
85 end
86 end
87 r(k) = r(k) .* b(k);
88 end
89
90 % Use a rejection method for a > 1.
91 k = find(a > 1);
92 % (Devroye, page 410 Best's algorithm)
93 bb = zeros(size(a));
94 c = bb;
95 if any(k)
96 bb(k) = a(k) - 1;
97 c(k) = 3 * a(k) - 3/4;
98 accept = k;
99 count = 1;
100 while ~isempty(accept)
101 m = length(accept);
102 u = rand(m,1);
103 v = rand(m,1);
104 w = u .* (1 - u);
105 y = sqrt(c(accept) ./ w) .* (u - 0.5);
106 x = bb(accept) + y;
107 k1 = find(x >= 0);
108 if ~isempty(k1)
109 z = 64 * (w .^ 3) .* (v .^ 2);
110 k2 = (z(k1) <= (1 - 2 * (y(k1) .^2) ./ x(k1)));
111 k3 = k1(find(k2));
112 r(accept(k3)) = x(k3);
113 k4 = k1(find(~k2));
114 k5 = k4(find(log(z(k4)) <= (2*(bb(accept(k4)).*log(x(k4)./bb(accept(k4)))-y(k4)))));
115 r(accept(k5)) = x(k5);
116 omit = [k3; k5];
117 accept(omit) = [];
118 end
119 end
120 r(k) = r(k) .* b(k);
121 end
122
123 % Return NaN if a or b is not positive.
124 r(b <= 0 | a <= 0) = NaN;
125
126 r = reshape(r,rows,columns);