v4 matlab: rreplaced beta_rnd.m and gamm_rnd. by GPL'ed custom ones
git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1980 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
8bef1f0399
commit
f76badf151
|
@ -1,34 +0,0 @@
|
|||
function rnd = beta_rnd (n, a, b)
|
||||
% PURPOSE: random draws from the beta(a,b) distribution
|
||||
%--------------------------------------------------------------
|
||||
% USAGE: rnd = beta_rnd(n,a,b)
|
||||
% where: n = size of the vector of draws
|
||||
% a = beta distribution parameter, a = scalar
|
||||
% b = beta distribution parameter b = scalar
|
||||
% NOTE: mean = a/(a+b), variance = ab/((a+b)*(a+b)*(a+b+1))
|
||||
%--------------------------------------------------------------
|
||||
% RETURNS: n-vector of random draws from the beta(a,b) distribution
|
||||
%--------------------------------------------------------------
|
||||
% SEE ALSO: beta_d, beta_pdf, beta_inv, beta_rnd
|
||||
%--------------------------------------------------------------
|
||||
|
||||
% written by:
|
||||
% James P. LeSage, Dept of Economics
|
||||
% University of Toledo
|
||||
% 2801 W. Bancroft St,
|
||||
% Toledo, OH 43606
|
||||
% jlesage@spatial-econometrics.com
|
||||
|
||||
|
||||
if (nargin ~= 3)
|
||||
error('Wrong # of arguments to beta_rnd');
|
||||
end;
|
||||
|
||||
if any(any((a<=0)|(b<=0)))
|
||||
error('Parameter a or b is nonpositive')
|
||||
end
|
||||
|
||||
a1n = gamm_rnd(n,a,1);
|
||||
a1d = gamm_rnd(n,b,1);
|
||||
rnd = a1n./(a1n+a1d);
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
function rnd = betarnd(a, b)
|
||||
% BETARND Random samples from the Beta distribution
|
||||
% RND = betarnd(A,B) returns a random sample from the
|
||||
% Beta distribution with parameters A and B (i.e. mean of
|
||||
% the distribution is A/(A+B) and variance is
|
||||
% A*B/(A+B)^2/(A+B+1).
|
||||
|
||||
% Copyright (C) 2008 Dynare Team
|
||||
%
|
||||
% This file is part of Dynare.
|
||||
%
|
||||
% Dynare is free software: you can redistribute it and/or modify
|
||||
% it under the terms of the GNU General Public License as published by
|
||||
% the Free Software Foundation, either version 3 of the License, or
|
||||
% (at your option) any later version.
|
||||
%
|
||||
% Dynare is distributed in the hope that it will be useful,
|
||||
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
% GNU General Public License for more details.
|
||||
%
|
||||
% You should have received a copy of the GNU General Public License
|
||||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
if (nargin ~= 2)
|
||||
error('betarnd: you must give two arguments');
|
||||
end
|
||||
|
||||
if (~isscalar(a) || ~isscalar(b))
|
||||
error('betarnd: A and B should be scalar parameters');
|
||||
end
|
||||
|
||||
if (a <= 0 || a == Inf || b <= 0 || b == Inf)
|
||||
rnd = NaN;
|
||||
else
|
||||
x = gamrnd(a, 1);
|
||||
rnd = x/(x+gamrnd(b, 1));
|
||||
end
|
|
@ -0,0 +1,57 @@
|
|||
function rnd = gamrnd(a,b)
|
||||
% GAMRND Random samples from the Gamma distribution
|
||||
% RND = gamrnd(A,B) returns a random sample from the
|
||||
% Gamma distribution with parameters A and B (i.e. mean of
|
||||
% the distribution is A*B and variance is A*B^2).
|
||||
%
|
||||
% Algorithm of Bauwens, Lubrano & Richard (page 316)
|
||||
|
||||
% Copyright (C) 2006-2008 Dynare Team
|
||||
%
|
||||
% This file is part of Dynare.
|
||||
%
|
||||
% Dynare is free software: you can redistribute it and/or modify
|
||||
% it under the terms of the GNU General Public License as published by
|
||||
% the Free Software Foundation, either version 3 of the License, or
|
||||
% (at your option) any later version.
|
||||
%
|
||||
% Dynare is distributed in the hope that it will be useful,
|
||||
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
% GNU General Public License for more details.
|
||||
%
|
||||
% You should have received a copy of the GNU General Public License
|
||||
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
if (nargin ~= 2)
|
||||
error('gamrnd: you must give two arguments');
|
||||
end
|
||||
|
||||
if (~isscalar(a) || ~isscalar(b))
|
||||
error('gamrnd: A and B should be scalar parameters');
|
||||
end
|
||||
|
||||
if (a <= 0 || a == Inf || b <= 0 || b == Inf)
|
||||
rnd = NaN;
|
||||
return
|
||||
end
|
||||
|
||||
if a >30
|
||||
z = randn;
|
||||
rnd = b*(z+sqrt(4*a-1))^2/4;
|
||||
else
|
||||
condi = 1;
|
||||
while condi
|
||||
x = -1;
|
||||
while x<0
|
||||
u1 = rand;
|
||||
y = tan(pi*u1);
|
||||
x = y*sqrt(2*a-1)+a-1;
|
||||
end
|
||||
u2 = rand;
|
||||
if log(u2) <= log(1+y^2)+(a-1)*log(x/(a-1))-y*sqrt(2*a-1);
|
||||
break
|
||||
end
|
||||
end
|
||||
rnd = x*b;
|
||||
end
|
|
@ -1,72 +0,0 @@
|
|||
function gb = gamm_rnd(nrow,m,k)
|
||||
% 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.
|
||||
|
||||
if nargin ~= 3
|
||||
error('Wrong # of arguments to gamm_rnd');
|
||||
end;
|
||||
|
||||
ncol = 1;
|
||||
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;
|
||||
|
||||
|
|
@ -108,26 +108,23 @@ for i = 1:npar
|
|||
end
|
||||
case 2% Gamma prior.
|
||||
while condition
|
||||
g = gamma_draw(a(i),b(i),p3(i));
|
||||
g = gamrnd(a(i),b(i)) + p3(i);
|
||||
if g >= bounds(i,1) && g <= bounds(i,2)
|
||||
pdraw(i) = g;
|
||||
break
|
||||
end
|
||||
end
|
||||
case 1% Beta distribution (TODO: generalized beta distribution)
|
||||
case 1% Beta distribution
|
||||
while condition
|
||||
y1 = gamma_draw(a(i),1,0);
|
||||
y2 = gamma_draw(b(i),1,0);
|
||||
tmp = y1/(y1+y2);
|
||||
tmp = betarnd(a(i), b(i));
|
||||
if tmp >= bounds(i,1) && tmp <= bounds(i,2)
|
||||
%pdraw(i) = pmean(i)+tmp*pstd(i);
|
||||
pdraw(i) = p3(i)+tmp*(p4(i)-p3(i));
|
||||
break
|
||||
end
|
||||
end
|
||||
case 4% INV-GAMMA1 distribution
|
||||
while condition
|
||||
tmp = sqrt(1/gamma_draw(p2(i)/2,2/p1(i),0));
|
||||
tmp = sqrt(1/gamrnd(p2(i)/2,2/p1(i)));
|
||||
if tmp >= bounds(i,1) && tmp <= bounds(i,2)
|
||||
pdraw(i) = tmp;
|
||||
break
|
||||
|
@ -135,7 +132,7 @@ for i = 1:npar
|
|||
end
|
||||
case 6% INV-GAMMA2 distribution
|
||||
while condition
|
||||
tmp = 1/gamma_draw(p2(i)/2,2/p1(i),0);
|
||||
tmp = 1/gamrnd(p2(i)/2,2/p1(i));
|
||||
if tmp >= bounds(i,1) && tmp <= bounds(i,2)
|
||||
pdraw(i) = tmp;
|
||||
break
|
||||
|
@ -145,29 +142,3 @@ for i = 1:npar
|
|||
% Nothing to do here.
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function g = gamma_draw(a,b,c)
|
||||
% Bauwens, Lubrano & Richard (page 316)
|
||||
if a >30
|
||||
z = randn;
|
||||
g = b*(z+sqrt(4*a-1))^2/4 + c;
|
||||
else
|
||||
condi = 1;
|
||||
while condi
|
||||
x = -1;
|
||||
while x<0
|
||||
u1 = rand;
|
||||
y = tan(pi*u1);
|
||||
x = y*sqrt(2*a-1)+a-1;
|
||||
end
|
||||
u2 = rand;
|
||||
if log(u2) <= log(1+y^2)+(a-1)*log(x/(a-1))-y*sqrt(2*a-1);
|
||||
break
|
||||
end
|
||||
end
|
||||
g = x*b+c;
|
||||
end
|
|
@ -11,7 +11,7 @@ function y = rndprior(bayestopt_)
|
|||
% SPECIAL REQUIREMENTS
|
||||
% none
|
||||
|
||||
% Copyright (C) 2003-2007 Dynare Team
|
||||
% Copyright (C) 2003-2008 Dynare Team
|
||||
%
|
||||
% This file is part of Dynare.
|
||||
%
|
||||
|
@ -44,14 +44,14 @@ for i=1:length(pmean),
|
|||
stdd = p2(i)/(p4(i)-p3(i));
|
||||
A = (1-mu)*mu^2/stdd^2 - mu;
|
||||
B = A*(1/mu - 1);
|
||||
y(1,i) = beta_rnd(1, A, B);
|
||||
y(1,i) = betarnd(A, B);
|
||||
y(1,i) = y(1,i) * (p4(i)-p3(i)) + p3(i);
|
||||
|
||||
case 2 %'gamma'
|
||||
mu = pmean(i)-p3(i);
|
||||
B = mu/p2(i)^2; %gamm_rnd uses 1/B instead of B as param.
|
||||
A = mu*B;
|
||||
y(1,i) = gamm_rnd(1, A, B) + p3(i);
|
||||
B = p2(i)^2/mu;
|
||||
A = mu/B;
|
||||
y(1,i) = gamrnd(A, B) + p3(i);
|
||||
|
||||
case 3 %'normal'
|
||||
MU = pmean(i);
|
||||
|
@ -61,8 +61,7 @@ for i=1:length(pmean),
|
|||
case 4 %'invgamma'
|
||||
nu = p2(i);
|
||||
s = p1(i);
|
||||
y(1,i) = 1/sqrt(gamm_rnd(1, nu/2, s/2)); %gamm_rnd uses 1/B
|
||||
%instead of B as param.
|
||||
y(1,i) = 1/sqrt(gamrnd(nu/2, 2/s));
|
||||
|
||||
case 5 %'uniform'
|
||||
y(1,i) = rand*(p2(i)-p1(i)) + p1(i);
|
||||
|
|
Loading…
Reference in New Issue