Home > . > gamm_rnd.m

gamm_rnd

PURPOSE ^

PURPOSE: a matrix of random draws from the gamma distribution

SYNOPSIS ^

function gb = gamm_rnd(nrow,m,k)

DESCRIPTION ^

 PURPOSE: a matrix of random draws from the gamma distribution
---------------------------------------------------
 USAGE: r = gamm_rnd(n,m,k)
 where: n = the size of the vector 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 n x 1 vector of random numbers from the gamma distribution      
 --------------------------------------------------
 SEE ALSO: gamm_inv, gamm_pdf, gamm_cdf
---------------------------------------------------
 NOTE: written by: Michael Gordy, 15 Sept 1993
                   mbgordy@athena.mit.edu
---------------------------------------------------
 REFERENCES: Luc Devroye, Non-Uniform Random Variate Generation,
            New York: Springer Verlag, 1986, ch 9.3-6.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function gb = gamm_rnd(nrow,m,k)
0002 % PURPOSE: a matrix of random draws from the gamma distribution
0003 %---------------------------------------------------
0004 % USAGE: r = gamm_rnd(n,m,k)
0005 % where: n = the size of the vector drawn
0006 %        m = a parameter such that the mean of the gamma = m/k
0007 %        k = a parameter such that the variance of the gamma = m/(k^2)
0008 %        note: m=r/2, k=2 equals chisq r random deviate
0009 %---------------------------------------------------
0010 % RETURNS:
0011 %        r = an n x 1 vector of random numbers from the gamma distribution
0012 % --------------------------------------------------
0013 % SEE ALSO: gamm_inv, gamm_pdf, gamm_cdf
0014 %---------------------------------------------------
0015 % NOTE: written by: Michael Gordy, 15 Sept 1993
0016 %                   mbgordy@athena.mit.edu
0017 %---------------------------------------------------
0018 % REFERENCES: Luc Devroye, Non-Uniform Random Variate Generation,
0019 %            New York: Springer Verlag, 1986, ch 9.3-6.
0020 
0021 if nargin ~= 3
0022 error('Wrong # of arguments to gamm_rnd');
0023 end;
0024 
0025 ncol = 1;
0026 gb=zeros(nrow,ncol);
0027 if m<=1
0028   % Use RGS algorithm by Best, p. 426
0029   c=1/m; 
0030   t=0.07+0.75*sqrt(1-m);
0031   b=1+exp(-t)*m/t;
0032   for i1=1:nrow
0033     for i2=1:ncol
0034        accept=0;
0035        while accept==0
0036           u=rand; w=rand; v=b*u;
0037           if v<=1
0038              x=t*(v^c);
0039              accept=((w<=((2-x)/(2+x))) | (w<=exp(-x)));
0040           else
0041              x=-log(c*t*(b-v));
0042              y=x/t;
0043              accept=(((w*(m+y-m*y))<=1) | (w<=(y^(m-1))));
0044           end
0045        end
0046        gb(i1,i2)=x;
0047     end
0048   end
0049 else
0050   % Use Best's rejection algorithm XG, p. 410
0051   b=m-1;
0052   c=3*m-0.75;
0053   for i1=1:nrow
0054     for i2=1:ncol
0055        accept=0;
0056        while accept==0
0057           u=rand;  v=rand;
0058           w=u*(1-u);  y=sqrt(c/w)*(u-0.5);
0059           x=b+y;
0060           if x >= 0
0061              z=64*(w^3)*v*v;
0062              accept=(z<=(1-2*y*y/x)) ...
0063                     | (log(z)<=(2*(b*log(x/b)-y)));
0064           end
0065        end
0066        gb(i1,i2)=x;
0067     end
0068   end
0069 end
0070 gb=gb/k;    
0071     
0072

Generated on Fri 16-Jun-2006 09:09:06 by m2html © 2003