From 567c5bcb1faa2c12dd8cd0fea298a658ed131788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Karaba=29?= Date: Mon, 18 Oct 2010 14:39:48 +0200 Subject: [PATCH] Fixed bug in the initialization of the metropolis when mode_compute=6 was previously used (so that we have an optimal mh scale factor) and option load_mh_file is used. After the (stochastic) optimization, the optimal value of the scale parameter is saved in a mat file. --- matlab/dynare_estimation_1.m | 7 +++++++ matlab/independent_metropolis_hastings.m | 3 +++ matlab/independent_metropolis_hastings_core.m | 11 ++++++++++- matlab/random_walk_metropolis_hastings.m | 3 +++ matlab/random_walk_metropolis_hastings_core.m | 13 ++++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index e4830a659..6acce4d83 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -389,6 +389,12 @@ if options_.mode_compute == 0 && length(options_.mode_file) == 0 && options_.mh_ return; end +if options_.mode_compute>0 || options_.mode_compute==6 + % Erase previously computed optimal mh scale parameter. + delete([M_.fname '_optimal_mh_scale_parameter.mat']) +end + + %% Estimation of the posterior mode or likelihood mode if options_.mode_compute > 0 && ~options_.mh_posterior_mode_estimation if ~options_.dsge_var @@ -550,6 +556,7 @@ if options_.mode_compute > 0 && ~options_.mh_posterior_mode_estimation end hh = inv(PostVar); save([M_.fname '_mode.mat'],'xparam1','hh'); + save([M_.fname '_optimal_mh_scale_parameter.mat'],'Scale'); bayestopt_.jscale = ones(length(xparam1),1)*Scale;%??! end case 7 diff --git a/matlab/independent_metropolis_hastings.m b/matlab/independent_metropolis_hastings.m index 815337ba7..4627591c4 100644 --- a/matlab/independent_metropolis_hastings.m +++ b/matlab/independent_metropolis_hastings.m @@ -94,6 +94,9 @@ else if (options_.load_mh_file~=0) & any(fline>1) , NamFileInput(length(NamFileInput)+1,:)={[M_.dname '/metropolis/'],[ModelName '_mh' int2str(NewFile(1)) '_blck*.mat']}; end + if exist([ModelName '_optimal_mh_scale_parameter.mat']) + NamFileInput(length(NamFileInput)+1,:)={'',[ModelName '_optimal_mh_scale_parameter.mat']}; + end % from where to get back results % NamFileOutput(1,:) = {[M_.dname,'/metropolis/'],'*.*'}; diff --git a/matlab/independent_metropolis_hastings_core.m b/matlab/independent_metropolis_hastings_core.m index f99e71e95..6b96edb8c 100644 --- a/matlab/independent_metropolis_hastings_core.m +++ b/matlab/independent_metropolis_hastings_core.m @@ -88,7 +88,16 @@ end %%%% NOW i run the (nblck-fblck+1) metropolis-hastings chains %%%% -proposal_covariance = d * diag(bayestopt_.jscale); +if any(isnan(bayestopt_.jscale)) + if exist([ModelName '_optimal_mh_scale_parameter.mat'])% This file is created by mode_compute=6. + load([ModelName '_optimal_mh_scale_parameter']) + proposal_covariance = d*Scale; + else + error('mh:: Something is wrong. I can''t figure out the value of the scale parameter.') + end +else + proposal_covariance = d*diag(bayestopt_.jscale); +end jloop=0; diff --git a/matlab/random_walk_metropolis_hastings.m b/matlab/random_walk_metropolis_hastings.m index 1c6804235..69d14710a 100644 --- a/matlab/random_walk_metropolis_hastings.m +++ b/matlab/random_walk_metropolis_hastings.m @@ -113,6 +113,9 @@ else if (options_.load_mh_file~=0) & any(fline>1) , NamFileInput(length(NamFileInput)+1,:)={[M_.dname '/metropolis/'],[ModelName '_mh' int2str(NewFile(1)) '_blck*.mat']}; end + if exist([ModelName '_optimal_mh_scale_parameter.mat']) + NamFileInput(length(NamFileInput)+1,:)={'',[ModelName '_optimal_mh_scale_parameter.mat']}; + end % from where to get back results % NamFileOutput(1,:) = {[M_.dname,'/metropolis/'],'*.*'}; diff --git a/matlab/random_walk_metropolis_hastings_core.m b/matlab/random_walk_metropolis_hastings_core.m index 9198601b7..2ce118d86 100644 --- a/matlab/random_walk_metropolis_hastings_core.m +++ b/matlab/random_walk_metropolis_hastings_core.m @@ -119,7 +119,18 @@ end %%%% NOW i run the (nblck-fblck+1) metropolis-hastings chains %%%% -proposal_covariance = d*diag(bayestopt_.jscale); + +if any(isnan(bayestopt_.jscale)) + if exist([ModelName '_optimal_mh_scale_parameter.mat'])% This file is created by mode_compute=6. + load([ModelName '_optimal_mh_scale_parameter']) + proposal_covariance = d*Scale; + else + error('mh:: Something is wrong. I can''t figure out the value of the scale parameter.') + end +else + proposal_covariance = d*diag(bayestopt_.jscale); +end + jloop=0;