function pdraw = prior_draw(bayestopt_, prior_trunc, uniform)
% pdraw = prior_draw(bayestopt_, prior_trunc, uniform)
% This function generate one draw from the joint prior distribution and
% allows sampling uniformly from the prior support (uniform==1 when called with init==1)
%
% INPUTS
% o init [integer] scalar equal to:
% 1: first call to set up persistent variables
% describing the prior
% 0: subsequent call to get prior
% draw
% o uniform [integer] scalar used in initialization (init=1), equal to:
% 1: sample uniformly from prior
% support (overwrites prior shape used for sampling within this function)
% 0: sample from joint prior distribution
%
% OUTPUTS
% o pdraw [double] 1*npar vector, draws from the joint prior density.
%
%
% SPECIAL REQUIREMENTS
% none
%
% NOTE 1. Input arguments 1 and 2 are only needed for initialization.
% NOTE 2. A given draw from the joint prior distribution does not satisfy BK conditions a priori.
%
% Copyright © 2006-2023 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 .
persistent p6 p7 p3 p4 lb ub
persistent uniform_index gaussian_index gamma_index beta_index inverse_gamma_1_index inverse_gamma_2_index weibull_index
persistent uniform_draws gaussian_draws gamma_draws beta_draws inverse_gamma_1_draws inverse_gamma_2_draws weibull_draws
if nargin>0
p6 = bayestopt_.p6;
p7 = bayestopt_.p7;
p3 = bayestopt_.p3;
p4 = bayestopt_.p4;
bounds = prior_bounds(bayestopt_, prior_trunc);
lb = bounds.lb;
ub = bounds.ub;
number_of_estimated_parameters = length(p6);
if nargin>2 && uniform
prior_shape = repmat(5,number_of_estimated_parameters,1);
else
prior_shape = bayestopt_.pshape;
end
beta_index = find(prior_shape==1);
if isempty(beta_index)
beta_draws = false;
else
beta_draws = true;
end
gamma_index = find(prior_shape==2);
if isempty(gamma_index)
gamma_draws = false;
else
gamma_draws = true;
end
gaussian_index = find(prior_shape==3);
if isempty(gaussian_index)
gaussian_draws = false;
else
gaussian_draws = true;
end
inverse_gamma_1_index = find(prior_shape==4);
if isempty(inverse_gamma_1_index)
inverse_gamma_1_draws = false;
else
inverse_gamma_1_draws = true;
end
uniform_index = find(prior_shape==5);
if isempty(uniform_index)
uniform_draws = false;
else
uniform_draws = true;
end
inverse_gamma_2_index = find(prior_shape==6);
if isempty(inverse_gamma_2_index)
inverse_gamma_2_draws = false;
else
inverse_gamma_2_draws = true;
end
weibull_index = find(prior_shape==8);
if isempty(weibull_index)
weibull_draws = false;
else
weibull_draws = true;
end
pdraw = NaN(number_of_estimated_parameters,1);
return
end
if uniform_draws
pdraw(uniform_index) = rand(length(uniform_index),1).*(p4(uniform_index)-p3(uniform_index)) + p3(uniform_index);
out_of_bound = find( (pdraw(uniform_index)'>ub(uniform_index)) | (pdraw(uniform_index)'ub(uniform_index)) | (pdraw(uniform_index)'ub(gaussian_index)) | (pdraw(gaussian_index)'ub(gaussian_index)) | (pdraw(gaussian_index)'ub(gamma_index)) | (pdraw(gamma_index)'ub(gamma_index)) | (pdraw(gamma_index)'ub(beta_index)) | (pdraw(beta_index)'ub(beta_index)) | (pdraw(beta_index)'ub(inverse_gamma_1_index)) | (pdraw(inverse_gamma_1_index)'ub(inverse_gamma_1_index)) | (pdraw(inverse_gamma_1_index)'ub(inverse_gamma_2_index)) | (pdraw(inverse_gamma_2_index)'ub(inverse_gamma_2_index)) | (pdraw(inverse_gamma_2_index)'ub(weibull_index)) | (pdraw(weibull_index)'ub(weibull_index)) | (pdraw(weibull_index)'