From d7ae0d003908152f4c7901b6bdf46b0bc47cc5e0 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Mon, 18 Dec 2023 14:03:52 +0100 Subject: [PATCH] OccBin: improve consistency - Make name of opts_simul subfield homogeneous for likelihood computation and smoother - fill the new field opts_regime with init_binding_indicator init_regime_history, to avoid possible conflics with simulations done in realtime filtering steps - smoother: use init_binding_indicator and init_regime_history options set in options_.occbin.smoother --- matlab/+occbin/DSGE_smoother.m | 34 +++++++++---------- matlab/+occbin/kalman_update_algo_3.m | 4 +-- matlab/estimation/dsge_likelihood.m | 6 ++-- .../missing_observations_kalman_filter.m | 4 +-- .../missing_DiffuseKalmanSmootherH3_Z.m | 6 ++-- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/matlab/+occbin/DSGE_smoother.m b/matlab/+occbin/DSGE_smoother.m index a92cfd58d..aef63f253 100644 --- a/matlab/+occbin/DSGE_smoother.m +++ b/matlab/+occbin/DSGE_smoother.m @@ -119,9 +119,9 @@ opts_simul.piecewise_only = options_.occbin.smoother.piecewise_only; occbin_options = struct(); occbin_options.first_period_occbin_update = options_.occbin.smoother.first_period_occbin_update; -occbin_options.opts_regime = opts_simul; % this builds the opts_simul options field needed by occbin.solver -occbin_options.opts_regime.binding_indicator = options_.occbin.likelihood.init_binding_indicator; -occbin_options.opts_regime.regime_history=options_.occbin.likelihood.init_regime_history; +occbin_options.opts_simul = opts_simul; % this builds the opts_simul options field needed by occbin.solver +occbin_options.opts_regime.binding_indicator = options_.occbin.smoother.init_binding_indicator; +occbin_options.opts_regime.regime_history=options_.occbin.smoother.init_regime_history; error_indicator=false; try @@ -133,7 +133,7 @@ catch ME for iter = 1:numel(ME.stack) ME.stack(iter) end -end + end if error_indicator || isempty(alphahat0) etahat= oo_.occbin.linear_smoother.etahat; alphahat0= oo_.occbin.linear_smoother.alphahat0; @@ -299,13 +299,13 @@ while is_changed && maxiter>iter && ~is_periodic eee(:,k) = eig(TT(:,:,k)); end if options_.debug - err_eig(iter-1) = max(max(abs(sort(eee)-sort(sto_eee)))); - err_alphahat(iter-1) = max(max(max(abs(alphahat-sto_alphahat)))); - err_etahat(iter-1) = max(max(max(abs(etahat-sto_etahat{iter-1})))); - err_CC(iter-1) = max(max(max(abs(CC-sto_CC)))); - err_RR(iter-1) = max(max(max(abs(RR-sto_RR)))); - err_TT(iter-1) = max(max(max(abs(TT-sto_TT)))); - end + err_eig(iter-1) = max(max(abs(sort(eee)-sort(sto_eee)))); + err_alphahat(iter-1) = max(max(max(abs(alphahat-sto_alphahat)))); + err_etahat(iter-1) = max(max(max(abs(etahat-sto_etahat{iter-1})))); + err_CC(iter-1) = max(max(max(abs(CC-sto_CC)))); + err_RR(iter-1) = max(max(max(abs(RR-sto_RR)))); + err_TT(iter-1) = max(max(max(abs(TT-sto_TT)))); + end end if occbin_smoother_debug || is_periodic @@ -483,15 +483,15 @@ if (~is_changed || occbin_smoother_debug) && nargin==12 fprintf(fidTeX,'\\label{Fig:smoothedshocks_occbin:%s}\n',int2str(ifig)); fprintf(fidTeX,'\\end{figure}\n'); fprintf(fidTeX,' \n'); - end end end + end end - if mod(j1,9)~=0 && j==M_.exo_nbr - annotation('textbox', [0.1,0,0.35,0.05],'String', 'Linear','Color','Blue','horizontalalignment','center','interpreter','none'); - annotation('textbox', [0.55,0,0.35,0.05],'String', 'Piecewise','Color','Red','horizontalalignment','center','interpreter','none'); - dyn_saveas(hh_fig,[GraphDirectoryName filesep M_.fname,'_smoothedshocks_occbin',int2str(ifig)],options_.nodisplay,options_.graph_format); + if mod(j1,9)~=0 && j==M_.exo_nbr + annotation('textbox', [0.1,0,0.35,0.05],'String', 'Linear','Color','Blue','horizontalalignment','center','interpreter','none'); + annotation('textbox', [0.55,0,0.35,0.05],'String', 'Piecewise','Color','Red','horizontalalignment','center','interpreter','none'); + dyn_saveas(hh_fig,[GraphDirectoryName filesep M_.fname,'_smoothedshocks_occbin',int2str(ifig)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) % TeX eps loader file fprintf(fidTeX,'\\begin{figure}[H]\n'); @@ -505,6 +505,6 @@ if (~is_changed || occbin_smoother_debug) && nargin==12 end if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) fclose(fidTeX); - end end end +end diff --git a/matlab/+occbin/kalman_update_algo_3.m b/matlab/+occbin/kalman_update_algo_3.m index 77c4a8cfc..7a4101f04 100644 --- a/matlab/+occbin/kalman_update_algo_3.m +++ b/matlab/+occbin/kalman_update_algo_3.m @@ -85,7 +85,7 @@ if isempty(nk) end nk=max(nk,1); -opts_simul = occbin_options.opts_regime; +opts_simul = occbin_options.opts_simul; options_.occbin.simul=opts_simul; base_regime = struct(); if M_.occbin.constraint_nbr==1 @@ -383,4 +383,4 @@ while t > 1 ri = T'*ri; % KD (2003), eq. (23), equation for r_{t-1,p_{t-1}} end -end +end \ No newline at end of file diff --git a/matlab/estimation/dsge_likelihood.m b/matlab/estimation/dsge_likelihood.m index 563357f48..7d5d2ce27 100644 --- a/matlab/estimation/dsge_likelihood.m +++ b/matlab/estimation/dsge_likelihood.m @@ -906,7 +906,5 @@ occbin_options.opts_simul.restrict_state_space = options_.occbin.likelihood.rest occbin_options.opts_simul.full_output = options_.occbin.likelihood.full_output; occbin_options.opts_simul.piecewise_only = options_.occbin.likelihood.piecewise_only; -if ~isempty(options_.occbin.smoother.init_binding_indicator) - occbin_options.opts_simul.init_binding_indicator = options_.occbin.likelihood.init_binding_indicator; - occbin_options.opts_simul.init_regime_history=options_.occbin.likelihood.init_regime_history; -end +occbin_options.opts_regime.init_binding_indicator = options_.occbin.likelihood.init_binding_indicator; +occbin_options.opts_regime.init_regime_history=options_.occbin.likelihood.init_regime_history; diff --git a/matlab/kalman/likelihood/missing_observations_kalman_filter.m b/matlab/kalman/likelihood/missing_observations_kalman_filter.m index 201cb9762..4979a5e2f 100644 --- a/matlab/kalman/likelihood/missing_observations_kalman_filter.m +++ b/matlab/kalman/likelihood/missing_observations_kalman_filter.m @@ -109,8 +109,8 @@ if occbin_.status M_=occbin_.info{6}; occbin_options=occbin_.info{7}; occbin_options.opts_simul.SHOCKS = []; - opts_regime.regime_history = occbin_options.opts_simul.init_regime; - opts_regime.binding_indicator = occbin_options.opts_simul.init_binding_indicator; + opts_regime.regime_history = occbin_options.opts_regime.init_regime_history; + opts_regime.binding_indicator = occbin_options.opts_regime.init_binding_indicator; if t>1 first_period_occbin_update = max(t+1,options_.occbin.likelihood.first_period_occbin_update); else diff --git a/matlab/kalman/missing_DiffuseKalmanSmootherH3_Z.m b/matlab/kalman/missing_DiffuseKalmanSmootherH3_Z.m index 5562a0a99..eb19f41eb 100644 --- a/matlab/kalman/missing_DiffuseKalmanSmootherH3_Z.m +++ b/matlab/kalman/missing_DiffuseKalmanSmootherH3_Z.m @@ -348,7 +348,7 @@ if isoccbin first_period_occbin_update = max(t+2,occbin_options.first_period_occbin_update); % kalman update is not yet robust to accommodate diffuse steps end - if occbin_options.opts_regime.waitbar && first_period_occbin_update1 && isoccbin && (t>=first_period_occbin_update || isinf(first_period_occbin_update)) - opts_simul = occbin_options.opts_regime; + opts_simul = occbin_options.opts_simul; opts_simul.SHOCKS = zeros(nk,M_.exo_nbr); if smoother_redux tmp=zeros(M_.endo_nbr,1);