forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamm_rnd.m
71 lines (67 loc) · 1.99 KB
/
gamm_rnd.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function gb = gamm_rnd(nrow,ncol,m,k)
% PURPOSE: a matrix of random draws from the gamma distribution
%---------------------------------------------------
% USAGE: r = gamm_rnd(nrow,ncol,m,k)
% where: nrow,ncol = the size of the matrix drawn
% m = a parameter such that the mean of the gamma = m/k
% k = a parameter such that the variance of the gamma = m/(k^2)
% note: m=r/2, k=2 equals chisq r random deviate
%---------------------------------------------------
% RETURNS:
% r = an nrow x ncol matrix of random numbers from the gamma distribution
% --------------------------------------------------
% SEE ALSO: gamm_inv, gamm_pdf, gamm_cdf
%---------------------------------------------------
% NOTE: written by: Michael Gordy, 15 Sept 1993
%---------------------------------------------------
% REFERENCES: Luc Devroye, Non-Uniform Random Variate Generation,
% New York: Springer Verlag, 1986, ch 9.3-6.
if nargin ~= 4
error('Wrong # of arguments to gamm_rnd');
end;
gb=zeros(nrow,ncol);
if m<=1
% Use RGS algorithm by Best, p. 426
c=1/m;
t=0.07+0.75*sqrt(1-m);
b=1+exp(-t)*m/t;
for i1=1:nrow
for i2=1:ncol
accept=0;
while accept==0
u=rand; w=rand; v=b*u;
if v<=1
x=t*(v^c);
accept=((w<=((2-x)/(2+x))) | (w<=exp(-x)));
else
x=-log(c*t*(b-v));
y=x/t;
accept=(((w*(m+y-m*y))<=1) | (w<=(y^(m-1))));
end
end
gb(i1,i2)=x;
end
end
else
% Use Best's rejection algorithm XG, p. 410
b=m-1;
c=3*m-0.75;
for i1=1:nrow
for i2=1:ncol
accept=0;
while accept==0
u=rand; v=rand;
w=u*(1-u); y=sqrt(c/w)*(u-0.5);
x=b+y;
if x >= 0
z=64*(w^3)*v*v;
accept=(z<=(1-2*y*y/x)) ...
| (log(z)<=(2*(b*log(x/b)-y)));
end
end
gb(i1,i2)=x;
end
end
end
gb=gb/k;