v4.1:: Changes related to the gamma variate generator.
+ Changed headers. + Allows vector of coefficients when calling the Beta random generator. git-svn-id: https://www.dynare.org/svn/dynare/trunk@2422 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
a05b5e9481
commit
01ba142329
|
@ -1,11 +1,19 @@
|
||||||
function rnd = betarnd(a, b)
|
function rnd = betarnd(a, b)
|
||||||
% BETARND Random samples from the Beta distribution
|
% This function produces independent random variates 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
|
% INPUTS
|
||||||
% the distribution is A/(A+B) and variance is
|
% a [double] n*1 vector of positive parameters.
|
||||||
% A*B/(A+B)^2/(A+B+1) ).
|
% b [double] n*1 vector of positive parameters.
|
||||||
|
%
|
||||||
|
% OUTPUT
|
||||||
|
% rnd [double] n*1 vector of independent variates from the beta(a,b) distribution.
|
||||||
|
% rnd(i) is beta distributed with variance a(i)/(a(i)+b(i)) and
|
||||||
|
% variance a(i)b(i)/(a(i)+b(i))^2/(a(i)+b(i)+1).
|
||||||
|
%
|
||||||
|
% ALGORITHMS
|
||||||
|
% Described and Devroye (1986, chapter 9).
|
||||||
|
|
||||||
% Copyright (C) 2008 Dynare Team
|
% Copyright (C) 2008-2009 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
%
|
%
|
||||||
|
@ -26,13 +34,20 @@ if (nargin ~= 2)
|
||||||
error('betarnd: you must give two arguments');
|
error('betarnd: you must give two arguments');
|
||||||
end
|
end
|
||||||
|
|
||||||
if (~isscalar(a) || ~isscalar(b))
|
if (any(a<0)) || (any(b<0)) || (any(a==Inf)) || (any(b==Inf))
|
||||||
error('betarnd: A and B should be scalar parameters');
|
error('betarnd:: Input arguments must be finite and positive!');
|
||||||
end
|
end
|
||||||
|
|
||||||
if (a <= 0 || a == Inf || b <= 0 || b == Inf)
|
[ma,na] = size(a);
|
||||||
rnd = NaN;
|
[mb,nb] = size(b);
|
||||||
else
|
|
||||||
x = gamrnd(a, 1);
|
if ma~=mb || na~=nb
|
||||||
rnd = x/(x+gamrnd(b, 1));
|
error('betarnd:: Input arguments must have the same size!');
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if na~=1
|
||||||
|
error('betarnd:: Input arguments must be column vectors');
|
||||||
|
end
|
||||||
|
|
||||||
|
x = gamrnd(a,ones(ma,1));
|
||||||
|
rnd = x./(x+gamrnd(b, ones(mb,1)));
|
|
@ -1,12 +1,19 @@
|
||||||
function rnd = gamrnd(a,b,method)
|
function rnd = gamrnd(a,b,method)
|
||||||
% GAMRND Random samples from the Gamma distribution
|
% This function produces independent random variates 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)
|
% INPUTS
|
||||||
|
% a [double] n*1 vector of positive parameters.
|
||||||
|
% b [double] n*1 vector of positive parameters.
|
||||||
|
% method [string] 'BawensLubranoRichard' or anything else (see below).
|
||||||
|
%
|
||||||
|
% OUTPUT
|
||||||
|
% rnd [double] n*1 vector of independent variates from the gamma(a,b) distribution.
|
||||||
|
% rnd(i) is gamma distributed with variance a(i)b(i) and variance a(i)b(i)^2.
|
||||||
|
%
|
||||||
|
% ALGORITHMS
|
||||||
|
% Described in Bauwens, Lubrano and Richard (1999, page 316) and Devroye (1986, chapter 9).
|
||||||
|
|
||||||
% Copyright (C) 2006-2008 Dynare Team
|
% Copyright (C) 2006-2009 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
%
|
%
|
||||||
|
@ -36,7 +43,10 @@ if nargin==2
|
||||||
end
|
end
|
||||||
if ~strcmpi(method,'BauwensLubranoRichard')
|
if ~strcmpi(method,'BauwensLubranoRichard')
|
||||||
Devroye.big = 'Best'; % 'Cheng' , 'Best'
|
Devroye.big = 'Best'; % 'Cheng' , 'Best'
|
||||||
% REMARK: The first algorithm (Cheng) is still producing obviously wrong simulations.
|
% REMARK 1: The first algorithm (Cheng) is still producing obviously wrong simulations.
|
||||||
|
% REMARK 2: The second algorithm seems slightly slower than the algorithm advocated by Bauwens,
|
||||||
|
% Lubrano and Richard, but the comparison depends on the value of a (this should be
|
||||||
|
% investigated further).
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue