From 5d47ac2aa9b0ab12c3b3d4bab39a03ead4eda0be Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Mon, 19 Feb 2024 14:19:55 +0100 Subject: [PATCH] SMC: gracefully exit with unsupported options --- doc/manual/source/the-model-file.rst | 3 +- matlab/estimation/dynare_estimation_1.m | 74 +++++++++++++++---------- tests/estimation/hssmc/fs2000.mod | 6 +- 3 files changed, 50 insertions(+), 33 deletions(-) diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst index 43ae152d5..cfbca027d 100644 --- a/doc/manual/source/the-model-file.rst +++ b/doc/manual/source/the-model-file.rst @@ -7490,7 +7490,8 @@ observed variables. Instructs Dynare to use the *Herbst and Schorfheide (2014)* version of the Sequential Monte-Carlo sampler instead of the - standard Random-Walk Metropolis-Hastings. + standard Random-Walk Metropolis-Hastings. Does not yet support + ``moments_varendo``, ``bayesian_irf``, and ``smoother``. ``'dsmh'`` diff --git a/matlab/estimation/dynare_estimation_1.m b/matlab/estimation/dynare_estimation_1.m index 56904f5be..444280a79 100644 --- a/matlab/estimation/dynare_estimation_1.m +++ b/matlab/estimation/dynare_estimation_1.m @@ -493,52 +493,66 @@ if issmc(options_) || (any(bayestopt_.pshape>0) && options_.mh_replic) || (any( end if ~issmc(options_) [error_flag, ~, options_]= metropolis_draw(1, options_, estim_params_, M_); + else + error_flag=false; end if ~(~isempty(options_.sub_draws) && options_.sub_draws==0) if options_.bayesian_irf - if error_flag - error('%s: I cannot compute the posterior IRFs!',dispString) + if ~issmc(options_) + if error_flag + error('%s: I cannot compute the posterior IRFs!',dispString) + end + oo_=PosteriorIRF('posterior',options_,estim_params_,oo_,M_,bayestopt_,dataset_,dataset_info,dispString); + else + fprintf('%s: SMC does not yet support the bayesian_irf option. Skipping computation.\n',dispString); end - oo_=PosteriorIRF('posterior',options_,estim_params_,oo_,M_,bayestopt_,dataset_,dataset_info,dispString); end if options_.moments_varendo - if error_flag - error('%s: I cannot compute the posterior moments for the endogenous variables!',dispString) - end - if options_.load_mh_file && options_.mh_replic==0 %user wants to recompute results - [MetropolisFolder, info] = CheckPath('metropolis',M_.dname); - if ~info - generic_post_data_file_name={'Posterior2ndOrderMoments','decomposition','PosteriorVarianceDecomposition','correlation','PosteriorCorrelations','conditional decomposition','PosteriorConditionalVarianceDecomposition'}; - for ii=1:length(generic_post_data_file_name) - delete_stale_file([MetropolisFolder filesep M_.fname '_' generic_post_data_file_name{1,ii} '*']); - end - % restore compatibility for loading pre-4.6.2 runs where estim_params_ was not saved; see 6e06acc7 and !1944 - NumberOfDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_posterior_draws*' ])); - if NumberOfDrawsFiles>0 - temp=load([M_.dname '/metropolis/' M_.fname '_posterior_draws1']); - if ~isfield(temp,'estim_params_') - for file_iter=1:NumberOfDrawsFiles - save([M_.dname '/metropolis/' M_.fname '_posterior_draws' num2str(file_iter)],'estim_params_','-append') + if ~issmc(options_) + if error_flag + error('%s: I cannot compute the posterior moments for the endogenous variables!',dispString) + end + if options_.load_mh_file && options_.mh_replic==0 %user wants to recompute results + [MetropolisFolder, info] = CheckPath('metropolis',M_.dname); + if ~info + generic_post_data_file_name={'Posterior2ndOrderMoments','decomposition','PosteriorVarianceDecomposition','correlation','PosteriorCorrelations','conditional decomposition','PosteriorConditionalVarianceDecomposition'}; + for ii=1:length(generic_post_data_file_name) + delete_stale_file([MetropolisFolder filesep M_.fname '_' generic_post_data_file_name{1,ii} '*']); + end + % restore compatibility for loading pre-4.6.2 runs where estim_params_ was not saved; see 6e06acc7 and !1944 + NumberOfDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_posterior_draws*' ])); + if NumberOfDrawsFiles>0 + temp=load([M_.dname '/metropolis/' M_.fname '_posterior_draws1']); + if ~isfield(temp,'estim_params_') + for file_iter=1:NumberOfDrawsFiles + save([M_.dname '/metropolis/' M_.fname '_posterior_draws' num2str(file_iter)],'estim_params_','-append') + end end end end end + oo_ = compute_moments_varendo('posterior',options_,M_,oo_,estim_params_,var_list_); + else + fprintf('%s: SMC does not yet support the moments_varendo option. Skipping computation.\n',dispString); end - oo_ = compute_moments_varendo('posterior',options_,M_,oo_,estim_params_,var_list_); end if options_.smoother || ~isempty(options_.filter_step_ahead) || options_.forecast - if error_flag - error('%s: I cannot compute the posterior statistics!',dispString) - end - if options_.order==1 && ~options_.particle.status - oo_=prior_posterior_statistics('posterior',dataset_,dataset_info,M_,oo_,options_,estim_params_,bayestopt_,dispString); %get smoothed and filtered objects and forecasts + if ~ishssmc(options_) + if error_flag + error('%s: I cannot compute the posterior statistics!',dispString) + end + if options_.order==1 && ~options_.particle.status + oo_=prior_posterior_statistics('posterior',dataset_,dataset_info,M_,oo_,options_,estim_params_,bayestopt_,dispString); %get smoothed and filtered objects and forecasts + else + error('%s: Particle Smoothers are not yet implemented.',dispString) + end else - error('%s: Particle Smoothers are not yet implemented.',dispString) + fprintf('%s: SMC does not yet support the smoother and forecast options. Skipping computation.\n',dispString); end end - else - fprintf('%s: sub_draws was set to 0. Skipping posterior computations.',dispString); - end + else + fprintf('%s: sub_draws was set to 0. Skipping posterior computations.\n',dispString); + end xparam1 = get_posterior_parameters('mean',M_,estim_params_,oo_,options_); M_ = set_all_parameters(xparam1,estim_params_,M_); end diff --git a/tests/estimation/hssmc/fs2000.mod b/tests/estimation/hssmc/fs2000.mod index d843c9e98..08a527b48 100644 --- a/tests/estimation/hssmc/fs2000.mod +++ b/tests/estimation/hssmc/fs2000.mod @@ -86,6 +86,8 @@ estimation(order=1, datafile='../fsdat_simul.m', nobs=192, loglinear, posterior_sampling_method='hssmc', posterior_sampler_options=('steps',10, 'lambda',2, - 'particles', 20000, + 'particles', 5000, 'scale',.5, - 'target', .25)); + 'target', .25), +bayesian_irf, smoother, moments_varendo,consider_all_endogenous +);