135 lines
6.9 KiB
HTML
135 lines
6.9 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||
|
"http://www.w3.org/TR/REC-html40/loose.dtd">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>Description of gamm_rnd</title>
|
||
|
<meta name="keywords" content="gamm_rnd">
|
||
|
<meta name="description" content="PURPOSE: a matrix of random draws from the gamma distribution">
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||
|
<meta name="generator" content="m2html © 2003 Guillaume Flandin">
|
||
|
<meta name="robots" content="index, follow">
|
||
|
<link type="text/css" rel="stylesheet" href="../m2html.css">
|
||
|
</head>
|
||
|
<body>
|
||
|
<a name="_top"></a>
|
||
|
<div><a href="../index.html">Home</a> > <a href="index.html">.</a> > gamm_rnd.m</div>
|
||
|
|
||
|
<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td>
|
||
|
<td align="right"><a href="index.html">Index for . <img alt=">" border="0" src="../right.png"></a></td></tr></table>-->
|
||
|
|
||
|
<h1>gamm_rnd
|
||
|
</h1>
|
||
|
|
||
|
<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="box"><strong>PURPOSE: a matrix of random draws from the gamma distribution</strong></div>
|
||
|
|
||
|
<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="box"><strong>function gb = gamm_rnd(nrow,m,k) </strong></div>
|
||
|
|
||
|
<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="fragment"><pre class="comment"> 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.</pre></div>
|
||
|
|
||
|
<!-- crossreference -->
|
||
|
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
This function calls:
|
||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
||
|
</ul>
|
||
|
This function is called by:
|
||
|
<ul style="list-style-image:url(../matlabicon.gif)">
|
||
|
<li><a href="beta_rnd.html" class="code" title="function rnd = beta_rnd (n, a, b)">beta_rnd</a> PURPOSE: random draws from the beta(a,b) distribution</li><li><a href="rndprior.html" class="code" title="function y = rndprior(bayestopt_)">rndprior</a> </li></ul>
|
||
|
<!-- crossreference -->
|
||
|
|
||
|
|
||
|
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
|
||
|
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function gb = gamm_rnd(nrow,m,k)</a>
|
||
|
0002 <span class="comment">% PURPOSE: a matrix of random draws from the gamma distribution</span>
|
||
|
0003 <span class="comment">%---------------------------------------------------</span>
|
||
|
0004 <span class="comment">% USAGE: r = gamm_rnd(n,m,k)</span>
|
||
|
0005 <span class="comment">% where: n = the size of the vector drawn</span>
|
||
|
0006 <span class="comment">% m = a parameter such that the mean of the gamma = m/k</span>
|
||
|
0007 <span class="comment">% k = a parameter such that the variance of the gamma = m/(k^2)</span>
|
||
|
0008 <span class="comment">% note: m=r/2, k=2 equals chisq r random deviate</span>
|
||
|
0009 <span class="comment">%---------------------------------------------------</span>
|
||
|
0010 <span class="comment">% RETURNS:</span>
|
||
|
0011 <span class="comment">% r = an n x 1 vector of random numbers from the gamma distribution</span>
|
||
|
0012 <span class="comment">% --------------------------------------------------</span>
|
||
|
0013 <span class="comment">% SEE ALSO: gamm_inv, gamm_pdf, gamm_cdf</span>
|
||
|
0014 <span class="comment">%---------------------------------------------------</span>
|
||
|
0015 <span class="comment">% NOTE: written by: Michael Gordy, 15 Sept 1993</span>
|
||
|
0016 <span class="comment">% mbgordy@athena.mit.edu</span>
|
||
|
0017 <span class="comment">%---------------------------------------------------</span>
|
||
|
0018 <span class="comment">% REFERENCES: Luc Devroye, Non-Uniform Random Variate Generation,</span>
|
||
|
0019 <span class="comment">% New York: Springer Verlag, 1986, ch 9.3-6.</span>
|
||
|
0020
|
||
|
0021 <span class="keyword">if</span> nargin ~= 3
|
||
|
0022 error(<span class="string">'Wrong # of arguments to gamm_rnd'</span>);
|
||
|
0023 <span class="keyword">end</span>;
|
||
|
0024
|
||
|
0025 ncol = 1;
|
||
|
0026 gb=zeros(nrow,ncol);
|
||
|
0027 <span class="keyword">if</span> m<=1
|
||
|
0028 <span class="comment">% Use RGS algorithm by Best, p. 426</span>
|
||
|
0029 c=1/m;
|
||
|
0030 t=0.07+0.75*sqrt(1-m);
|
||
|
0031 b=1+exp(-t)*m/t;
|
||
|
0032 <span class="keyword">for</span> i1=1:nrow
|
||
|
0033 <span class="keyword">for</span> i2=1:ncol
|
||
|
0034 accept=0;
|
||
|
0035 <span class="keyword">while</span> accept==0
|
||
|
0036 u=rand; w=rand; v=b*u;
|
||
|
0037 <span class="keyword">if</span> v<=1
|
||
|
0038 x=t*(v^c);
|
||
|
0039 accept=((w<=((2-x)/(2+x))) | (w<=exp(-x)));
|
||
|
0040 <span class="keyword">else</span>
|
||
|
0041 x=-log(c*t*(b-v));
|
||
|
0042 y=x/t;
|
||
|
0043 accept=(((w*(m+y-m*y))<=1) | (w<=(y^(m-1))));
|
||
|
0044 <span class="keyword">end</span>
|
||
|
0045 <span class="keyword">end</span>
|
||
|
0046 gb(i1,i2)=x;
|
||
|
0047 <span class="keyword">end</span>
|
||
|
0048 <span class="keyword">end</span>
|
||
|
0049 <span class="keyword">else</span>
|
||
|
0050 <span class="comment">% Use Best's rejection algorithm XG, p. 410</span>
|
||
|
0051 b=m-1;
|
||
|
0052 c=3*m-0.75;
|
||
|
0053 <span class="keyword">for</span> i1=1:nrow
|
||
|
0054 <span class="keyword">for</span> i2=1:ncol
|
||
|
0055 accept=0;
|
||
|
0056 <span class="keyword">while</span> accept==0
|
||
|
0057 u=rand; v=rand;
|
||
|
0058 w=u*(1-u); y=sqrt(c/w)*(u-0.5);
|
||
|
0059 x=b+y;
|
||
|
0060 <span class="keyword">if</span> x >= 0
|
||
|
0061 z=64*(w^3)*v*v;
|
||
|
0062 accept=(z<=(1-2*y*y/x)) <span class="keyword">...</span>
|
||
|
0063 | (log(z)<=(2*(b*log(x/b)-y)));
|
||
|
0064 <span class="keyword">end</span>
|
||
|
0065 <span class="keyword">end</span>
|
||
|
0066 gb(i1,i2)=x;
|
||
|
0067 <span class="keyword">end</span>
|
||
|
0068 <span class="keyword">end</span>
|
||
|
0069 <span class="keyword">end</span>
|
||
|
0070 gb=gb/k;
|
||
|
0071
|
||
|
0072</pre></div>
|
||
|
<hr><address>Generated on Fri 16-Jun-2006 09:09:06 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address>
|
||
|
</body>
|
||
|
</html>
|