From 7959102d43684a123fcadcff997c134327ee112a Mon Sep 17 00:00:00 2001 From: Michel Juillard <michel.juillard@mjui.fr> Date: Sun, 5 Aug 2012 15:10:21 +0200 Subject: [PATCH] removed global and varargin in Metropolis routines --- matlab/metropolis_hastings_initialization.m | 19 +++++++---- matlab/random_walk_metropolis_hastings.m | 34 +++++++++++-------- matlab/random_walk_metropolis_hastings_core.m | 11 +++--- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/matlab/metropolis_hastings_initialization.m b/matlab/metropolis_hastings_initialization.m index 2d6e7f07d1..b248ad2542 100644 --- a/matlab/metropolis_hastings_initialization.m +++ b/matlab/metropolis_hastings_initialization.m @@ -1,5 +1,7 @@ function [ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = ... - metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds, varargin) + metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,options_,M_,estim_params_,bayestopt_,oo_) +%function [ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = +% metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds, dataset_,options_,M_,estim_params_,bayestopt_,oo_) % Metropolis-Hastings initialization. % % INPUTS @@ -8,7 +10,12 @@ function [ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, % o xparam1 [double] (p*1) vector of parameters to be estimated (initial values). % o vv [double] (p*p) matrix, posterior covariance matrix (at the mode). % o mh_bounds [double] (p*2) matrix defining lower and upper bounds for the parameters. -% o varargin list of argument following mh_bounds +% o dataset_ data structure +% o options_ options structure +% o M_ model structure +% o estim_params_ estimated parameters structure +% o bayestopt_ estimation options structure +% o oo_ outputs structure % % OUTPUTS % None @@ -33,8 +40,6 @@ function [ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -global M_ options_ bayestopt_ - ix2 = []; ilogpo2 = []; ModelName = []; @@ -54,7 +59,7 @@ if ~isempty(M_.bvar) ModelName = [M_.fname '_bvar']; end -bayestopt_.penalty = 1e8; +bayestopt_.penalty = Inf; MhDirectoryName = CheckPath('metropolis',M_.dname); @@ -107,7 +112,7 @@ if ~options_.load_mh_file && ~options_.mh_recover candidate = rand_multivariate_normal( transpose(xparam1), d * options_.mh_init_scale, npar); if all(candidate(:) > mh_bounds(:,1)) && all(candidate(:) < mh_bounds(:,2)) ix2(j,:) = candidate; - ilogpo2(j) = - feval(TargetFun,ix2(j,:)',varargin{:}); + ilogpo2(j) = - feval(TargetFun,ix2(j,:)',dataset_,options_,M_,estim_params_,bayestopt_,oo_); if ilogpo2(j) <= - bayestopt_.penalty+1e-6 validate = 0; else @@ -142,7 +147,7 @@ if ~options_.load_mh_file && ~options_.mh_recover candidate = transpose(xparam1); if all(candidate' > mh_bounds(:,1)) && all(candidate' < mh_bounds(:,2)) ix2 = candidate; - ilogpo2 = - feval(TargetFun,ix2',varargin{:}); + ilogpo2 = - feval(TargetFun,ix2',dataset_,options_,M_,estim_params_,bayestopt_,oo_); disp('MH: Initialization at the posterior mode.') disp(' ') fprintf(fidlog,[' Blck ' int2str(1) 'params:\n']); diff --git a/matlab/random_walk_metropolis_hastings.m b/matlab/random_walk_metropolis_hastings.m index 48c20fa947..c5e982921c 100644 --- a/matlab/random_walk_metropolis_hastings.m +++ b/matlab/random_walk_metropolis_hastings.m @@ -1,5 +1,5 @@ -function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,varargin) -%function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,varargin) +function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,dataset_,options_,M_,estim_params_,bayestopt_,oo_) +%function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv,mh_bounds,dataset_,options_,M_,estim_params_,bayestopt_,oo_) % Random walk Metropolis-Hastings algorithm. % % INPUTS @@ -8,7 +8,12 @@ function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv % o xparam1 [double] (p*1) vector of parameters to be estimated (initial values). % o vv [double] (p*p) matrix, posterior covariance matrix (at the mode). % o mh_bounds [double] (p*2) matrix defining lower and upper bounds for the parameters. -% o varargin list of argument following mh_bounds +% o dataset_ data structure +% o options_ options structure +% o M_ model structure +% o estim_params_ estimated parameters structure +% o bayestopt_ estimation options structure +% o oo_ outputs structure % % OUTPUTS % o record [struct] structure describing the iterations @@ -51,12 +56,11 @@ function record=random_walk_metropolis_hastings(TargetFun,ProposalFun,xparam1,vv % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -global M_ options_ bayestopt_ estim_params_ oo_ %%%% %%%% Initialization of the random walk metropolis-hastings chains. %%%% [ ix2, ilogpo2, ModelName, MhDirectoryName, fblck, fline, npar, nblck, nruns, NewFile, MAX_nruns, d ] = ... - metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds, varargin{:}); + metropolis_hastings_initialization(TargetFun, xparam1, vv, mh_bounds,dataset_,options_,M_,estim_params_,bayestopt_,oo_); InitSizeArray = min([repmat(MAX_nruns,nblck,1) fline+nruns-1],[],2); @@ -93,10 +97,16 @@ localVars = struct('TargetFun', TargetFun, ... 'nruns', nruns, ... 'NewFile', NewFile, ... 'MAX_nruns', MAX_nruns, ... - 'd', d); -localVars.InitSizeArray=InitSizeArray; -localVars.record=record; -localVars.varargin=varargin; + 'd', d, ... + 'InitSizeArray',InitSizeArray, ... + 'record', record, ... + 'dataset_', dataset_, ... + 'options_', options_, ... + 'M_',M_, ... + 'bayestopt_', bayestopt_, ... + 'estim_params_', estim_params_, ... + 'oo_', oo_,... + 'varargin',[]); % The user don't want to use parallel computing, or want to compute a @@ -110,11 +120,7 @@ if isnumeric(options_.parallel) || (nblck-fblck)==0, % Parallel in Local or remote machine. else % Global variables for parallel routines. - globalVars = struct('M_',M_, ... - 'options_', options_, ... - 'bayestopt_', bayestopt_, ... - 'estim_params_', estim_params_, ... - 'oo_', oo_); + globalVars = struct(); % which files have to be copied to run remotely NamFileInput(1,:) = {'',[ModelName '_static.m']}; diff --git a/matlab/random_walk_metropolis_hastings_core.m b/matlab/random_walk_metropolis_hastings_core.m index 1898d478ab..8bb4d44b1b 100644 --- a/matlab/random_walk_metropolis_hastings_core.m +++ b/matlab/random_walk_metropolis_hastings_core.m @@ -68,9 +68,6 @@ if nargin<4, whoiam=0; end - -global bayestopt_ estim_params_ options_ M_ oo_ - % reshape 'myinputs' for local computation. % In order to avoid confusion in the name space, the instruction struct2local(myinputs) is replaced by: @@ -90,6 +87,12 @@ MAX_nruns=myinputs.MAX_nruns; d=myinputs.d; InitSizeArray=myinputs.InitSizeArray; record=myinputs.record; +dataset_ = myinputs.dataset_; +bayestopt_ = myinputs.bayestopt_; +estim_params_ = myinputs.estim_params_; +options_ = myinputs.options_; +M_ = myinputs.M_; +oo_ = myinputs.oo_; varargin=myinputs.varargin; % Necessary only for remote computing! @@ -168,7 +171,7 @@ for b = fblck:nblck, par = feval(ProposalFun, ix2(b,:), proposal_covariance_Cholesky_decomposition, n); if all( par(:) > mh_bounds(:,1) ) && all( par(:) < mh_bounds(:,2) ) try - logpost = - feval(TargetFun, par(:),varargin{:}); + logpost = - feval(TargetFun, par(:),dataset_,options_,M_,estim_params_,bayestopt_,oo_); catch logpost = -inf; end -- GitLab