From 32bdb82cf814dd8a66b6169362201b40db3a261d Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 20 Jan 2021 10:20:18 +0100 Subject: [PATCH 1/4] Make load_mh_file honor use_mh_covariance option --- matlab/default_option_values.m | 2 +- matlab/posterior_sampler_initialization.m | 41 +++++++++++++---------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/matlab/default_option_values.m b/matlab/default_option_values.m index 66ba29f13..5bffa1e2e 100644 --- a/matlab/default_option_values.m +++ b/matlab/default_option_values.m @@ -425,7 +425,7 @@ options_.smoother = false; options_.posterior_max_subsample_draws = 1200; options_.sub_draws = []; options_.ME_plot_tol=1e-6; -% options_.use_mh_covariance_matrix = 0; +options_.use_mh_covariance_matrix = false; options_.gradient_method = 2; %used by csminwel and newrat options_.gradient_epsilon = 1e-6; %used by csminwel and newrat options_.posterior_sampler_options.sampling_opt = []; %extended set of options for individual posterior samplers diff --git a/matlab/posterior_sampler_initialization.m b/matlab/posterior_sampler_initialization.m index 069985fa4..89b9e96d2 100644 --- a/matlab/posterior_sampler_initialization.m +++ b/matlab/posterior_sampler_initialization.m @@ -490,24 +490,29 @@ elseif options_.mh_recover end function [d,bayestopt_]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d) -if isfield(record,'ProposalCovariance') && isfield(record,'ProposalCovariance') - if isfield(record,'MCMC_sampler') - if ~strcmp(record.MCMC_sampler,options_.posterior_sampler_options.posterior_sampling_method) - error(fprintf('Estimation::mcmc: The posterior_sampling_method differs from the one of the original chain. Please reset it to %s',record.MCMC_sampler)) +if ~options_.use_mh_covariance_matrix + if isfield(record,'ProposalCovariance') && isfield(record,'ProposalScaleVec') + if isfield(record,'MCMC_sampler') + if ~strcmp(record.MCMC_sampler,options_.posterior_sampler_options.posterior_sampling_method) + error(fprintf('Estimation::mcmc: The posterior_sampling_method differs from the one of the original chain. Please reset it to %s',record.MCMC_sampler)) + end + end + fprintf('Estimation::mcmc: Recovering the previous proposal density.\n') + d=record.ProposalCovariance; + bayestopt_.jscale=record.ProposalScaleVec; + else + if ~isequal(options_.posterior_sampler_options.posterior_sampling_method,'slice') + % pass through input d unaltered + if options_.mode_compute~=0 + fprintf('Estimation::mcmc: No stored previous proposal density found, continuing with the one implied by mode_compute\n.'); + elseif ~isempty(options_.mode_file) + fprintf('Estimation::mcmc: No stored previous proposal density found, continuing with the one implied by the mode_file\n.'); + else + error('Estimation::mcmc: No stored previous proposal density found, no mode-finding conducted, and no mode-file provided. I don''t know how to continue') + end end end - fprintf('Estimation::mcmc: Recovering the previous proposal density.\n') - d=record.ProposalCovariance; - bayestopt_.jscale=record.ProposalScaleVec; else - if ~isequal(options_.posterior_sampler_options.posterior_sampling_method,'slice') - % pass through input d unaltered - if options_.mode_compute~=0 - fprintf('Estimation::mcmc: No stored previous proposal density found, continuing with the one implied by mode_compute\n.'); - elseif ~isempty(options_.mode_file) - fprintf('Estimation::mcmc: No stored previous proposal density found, continuing with the one implied by the mode_file\n.'); - else - error('Estimation::mcmc: No stored previous proposal density found, no mode-finding conducted, and no mode-file provided. I don''t know how to continue') - end - end -end + % pass through input d unaltered + fprintf('Estimation::mcmc: use_mh_covariance_matrix specified, continuing with proposal density implied by the previous MCMC run.\n.'); +end \ No newline at end of file From c78890d5ca36f339a81c48ef233f62f550a1ec13 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 20 Jan 2021 17:00:29 +0100 Subject: [PATCH 2/4] posterior_sampler_initialization.m: Allow running different samplers after each other Transform error to warning --- matlab/posterior_sampler_initialization.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlab/posterior_sampler_initialization.m b/matlab/posterior_sampler_initialization.m index 89b9e96d2..aaddd9b70 100644 --- a/matlab/posterior_sampler_initialization.m +++ b/matlab/posterior_sampler_initialization.m @@ -494,7 +494,7 @@ if ~options_.use_mh_covariance_matrix if isfield(record,'ProposalCovariance') && isfield(record,'ProposalScaleVec') if isfield(record,'MCMC_sampler') if ~strcmp(record.MCMC_sampler,options_.posterior_sampler_options.posterior_sampling_method) - error(fprintf('Estimation::mcmc: The posterior_sampling_method differs from the one of the original chain. Please reset it to %s',record.MCMC_sampler)) + warning('Estimation::mcmc: The posterior_sampling_method %s selected differs from the %s of the original chain. This may create problems with the convergence diagnostics.',options_.posterior_sampler_options.posterior_sampling_method,record.MCMC_sampler) end end fprintf('Estimation::mcmc: Recovering the previous proposal density.\n') From fa739c3a634663b8cda0961ce92dd14ee7091f70 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 20 Jan 2021 18:21:37 +0100 Subject: [PATCH 3/4] posterior_sampler_initialization.m: update record if different sampler is used Also removes redundant field --- matlab/posterior_sampler_initialization.m | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/matlab/posterior_sampler_initialization.m b/matlab/posterior_sampler_initialization.m index aaddd9b70..b950d3861 100644 --- a/matlab/posterior_sampler_initialization.m +++ b/matlab/posterior_sampler_initialization.m @@ -225,7 +225,6 @@ if ~options_.load_mh_file && ~options_.mh_recover record.LastFileNumber = AnticipatedNumberOfFiles ; record.LastLineNumber = AnticipatedNumberOfLinesInTheLastFile; record.MCMCConcludedSuccessfully = 0; - record.MCMC_sampler=options_.posterior_sampler_options.posterior_sampling_method; record.ProposalScaleVec=bayestopt_.jscale; fprintf('Ok!\n'); id = write_mh_history_file(MetropolisFolder, ModelName, record); @@ -286,7 +285,7 @@ elseif options_.load_mh_file && ~options_.mh_recover end ilogpo2 = record.LastLogPost; ix2 = record.LastParameters; - [d,bayestopt_]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d); + [d,bayestopt_,record]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d); FirstBlock = 1; NumberOfPreviousSimulations = sum(record.MhDraws(:,1),1); fprintf('Estimation::mcmc: I am writing a new mh-history file... '); @@ -368,7 +367,7 @@ elseif options_.mh_recover LastFileFullIndicator=1; end if ~isequal(options_.posterior_sampler_options.posterior_sampling_method,'slice') - [d,bayestopt_]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d); + [d,bayestopt_,record]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d); end %% Now find out what exactly needs to be redone % 1. Check if really something needs to be done @@ -489,14 +488,9 @@ elseif options_.mh_recover FirstBlock = find(FBlock==1,1); end -function [d,bayestopt_]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d) +function [d,bayestopt_,record]=set_proposal_density_to_previous_value(record,options_,bayestopt_,d) if ~options_.use_mh_covariance_matrix if isfield(record,'ProposalCovariance') && isfield(record,'ProposalScaleVec') - if isfield(record,'MCMC_sampler') - if ~strcmp(record.MCMC_sampler,options_.posterior_sampler_options.posterior_sampling_method) - warning('Estimation::mcmc: The posterior_sampling_method %s selected differs from the %s of the original chain. This may create problems with the convergence diagnostics.',options_.posterior_sampler_options.posterior_sampling_method,record.MCMC_sampler) - end - end fprintf('Estimation::mcmc: Recovering the previous proposal density.\n') d=record.ProposalCovariance; bayestopt_.jscale=record.ProposalScaleVec; @@ -515,4 +509,11 @@ if ~options_.use_mh_covariance_matrix else % pass through input d unaltered fprintf('Estimation::mcmc: use_mh_covariance_matrix specified, continuing with proposal density implied by the previous MCMC run.\n.'); -end \ No newline at end of file +end + +if isfield(record,'Sampler') + if ~strcmp(record.Sampler,options_.posterior_sampler_options.posterior_sampling_method) + warning('Estimation::mcmc: The posterior_sampling_method %s selected differs from the %s of the original chain. This may create problems with the convergence diagnostics.',options_.posterior_sampler_options.posterior_sampling_method,record.Sampler) + record.Sampler=options_.posterior_sampler_options.posterior_sampling_method; %update sampler used + end +end From 731a168f698539f300f699e0d9e0b646923a4092 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 20 Jan 2021 10:33:12 +0100 Subject: [PATCH 4/4] Make mh_tune_jscale work with use_mh_covariance_matrix --- matlab/dynare_estimation_1.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index 8bc62ed9b..063054ca9 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -440,14 +440,19 @@ if (any(bayestopt_.pshape >0 ) && options_.mh_replic) || ... % Tunes the jumping distribution's scale parameter if options_.mh_tune_jscale.status if strcmp(options_.posterior_sampler_options.posterior_sampling_method, 'random_walk_metropolis_hastings') + %get invhess in case of use_mh_covariance_matrix + posterior_sampler_options_temp = options_.posterior_sampler_options.current_options; + posterior_sampler_options_temp.invhess = invhess; + posterior_sampler_options_temp = check_posterior_sampler_options(posterior_sampler_options_temp, options_); + options = options_.mh_tune_jscale; options.rwmh = options_.posterior_sampler_options.rwmh; options_.mh_jscale = calibrate_mh_scale_parameter(objective_function, ... - invhess, xparam1, [bounds.lb,bounds.ub], ... + posterior_sampler_options_temp.invhess, xparam1, [bounds.lb,bounds.ub], ... options, dataset_, dataset_info, options_, M_, estim_params_, bayestopt_, bounds, oo_); + clear('posterior_sampler_options_temp','options') bayestopt_.jscale(:) = options_.mh_jscale; - disp(sprintf('mh_jscale has been set equal to %s', num2str(options_.mh_jscale))) - skipline() + fprintf('mh_jscale has been set equal to %s\n', num2str(options_.mh_jscale)) else warning('mh_tune_jscale is only available with Random Walk Metropolis Hastings!') end