From f2045e225f010bb2ea78549955b164d83ee4a8d3 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer <jpfeifer@gmx.de> Date: Wed, 16 Oct 2024 17:46:44 +0200 Subject: [PATCH] :bug: smoother: do not attempt plotting if OccBin smoother did not return results Incidentally remove calling store_smoother_results twice --- .../save_display_classical_smoother_results.m | 279 +++++++++--------- 1 file changed, 141 insertions(+), 138 deletions(-) diff --git a/matlab/kalman/save_display_classical_smoother_results.m b/matlab/kalman/save_display_classical_smoother_results.m index f7f2f981e7..e1e5f87cac 100644 --- a/matlab/kalman/save_display_classical_smoother_results.m +++ b/matlab/kalman/save_display_classical_smoother_results.m @@ -34,11 +34,12 @@ function oo_=save_display_classical_smoother_results(xparam1,M_,oo_,options_,bay gend= dataset_.nobs; % Set the number of observed variables. n_varobs = length(options_.varobs); - +smoother_error=false; if options_.occbin.smoother.status && options_.occbin.smoother.inversion_filter [~, info, ~, ~, ~, ~, ~, ~, ~, ~, oo_.dr, atT, innov, oo_.occbin.smoother.regime_history] = occbin.IVF_posterior(xparam1,dataset_,dataset_info,options_,M_,estim_params_,bayestopt_,prior_bounds(bayestopt_,options_.prior_trunc),oo_.dr, oo_.steady_state,oo_.exo_steady_state,oo_.exo_det_steady_state); if ismember(info(1),[303,304,306]) fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n') + smoother_error=true; else updated_variables = atT*nan; measurement_error=[]; @@ -54,95 +55,31 @@ else if options_.occbin.smoother.status [atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,~,~,P,PK,decomp,Trend,state_uncertainty,oo_,bayestopt_] = occbin.DSGE_smoother(xparam1,dataset_.nobs,transpose(dataset_.data),dataset_info.missing.aindex,dataset_info.missing.state,M_,oo_,options_,bayestopt_,estim_params_,dataset_,dataset_info); if oo_.occbin.smoother.error_flag(1)==0 - oo_ = store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty); + [oo_, yf] = store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty); else + smoother_error=true; fprintf('\nOccbin: smoother did not succeed. No results will be written to oo_.\n') end else [atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,~,~,P,PK,decomp,Trend,state_uncertainty,oo_,bayestopt_] = DsgeSmoother(xparam1,dataset_.nobs,transpose(dataset_.data),dataset_info.missing.aindex,dataset_info.missing.state,M_,oo_,options_,bayestopt_,estim_params_); - oo_ = store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty); - end - [oo_,yf]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty); -end -if ~options_.nograph - [nbplt,nr,nc,~,~,nstar] = pltorg(M_.exo_nbr); - if ~exist([M_.dname '/graphs'],'dir') - mkdir(M_.dname,'graphs'); - end - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_SmoothedShocks.tex'],'w'); - fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n'); - fprintf(fidTeX,['%% ' datestr(now,0) '\n']); - fprintf(fidTeX,' \n'); - end - for plt = 1:nbplt - fh = dyn_figure(options_.nodisplay,'Name','Smoothed shocks'); - nstar0=min(nstar,M_.exo_nbr-(plt-1)*nstar); - if gend==1 - marker_string{1,1}='-ro'; - marker_string{2,1}='-ko'; - else - marker_string{1,1}='-r'; - marker_string{2,1}='-k'; - end - for i=1:nstar0 - k = (plt-1)*nstar+i; - subplot(nr,nc,i); - plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5) - hold on - plot(1:gend,innov(k,:),marker_string{2,1},'linewidth',1) - hold off - name = M_.exo_names{k}; - if ~isempty(options_.XTick) - set(gca,'XTick',options_.XTick) - set(gca,'XTickLabel',options_.XTickLabel) - end - if gend>1 - xlim([1 gend]) - end - if options_.TeX - title(['$' M_.exo_names_tex{k} '$'],'Interpreter','latex') - else - title(name,'Interpreter','none') - end - end - dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_SmoothedShocks' int2str(plt)],options_.nodisplay,options_.graph_format); - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fprintf(fidTeX,'\\begin{figure}[H]\n'); - fprintf(fidTeX,'\\centering \n'); - fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedShocks%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); - fprintf(fidTeX,'\\caption{Smoothed shocks.}'); - fprintf(fidTeX,'\\label{Fig:SmoothedShocks:%s}\n',int2str(plt)); - fprintf(fidTeX,'\\end{figure}\n'); - fprintf(fidTeX,'\n'); - end - end - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fprintf(fidTeX,'\n'); - fprintf(fidTeX,'%% End of TeX file.\n'); - fclose(fidTeX); + [oo_,yf]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff,aK,P,PK,decomp,Trend,state_uncertainty); end end -if estim_params_.nvn - number_of_plots_to_draw = 0; - index = []; - for obs_iter=1:n_varobs - if max(abs(measurement_error(obs_iter,:))) > options_.ME_plot_tol - number_of_plots_to_draw = number_of_plots_to_draw + 1; - index = cat(1,index,obs_iter); - end - end +if ~smoother_error if ~options_.nograph - [nbplt,nr,nc,~,~,nstar] = pltorg(number_of_plots_to_draw); + [nbplt,nr,nc,~,~,nstar] = pltorg(M_.exo_nbr); + if ~exist([M_.dname '/graphs'],'dir') + mkdir(M_.dname,'graphs'); + end if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_SmoothedObservationErrors.tex'],'w'); + fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_SmoothedShocks.tex'],'w'); fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n'); fprintf(fidTeX,['%% ' datestr(now,0) '\n']); fprintf(fidTeX,' \n'); end for plt = 1:nbplt - fh = dyn_figure(options_.nodisplay,'Name','Smoothed observation errors'); - nstar0=min(nstar,number_of_plots_to_draw-(plt-1)*nstar); + fh = dyn_figure(options_.nodisplay,'Name','Smoothed shocks'); + nstar0=min(nstar,M_.exo_nbr-(plt-1)*nstar); if gend==1 marker_string{1,1}='-ro'; marker_string{2,1}='-ko'; @@ -155,30 +92,29 @@ if estim_params_.nvn subplot(nr,nc,i); plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5) hold on - plot(1:gend,measurement_error(index(k),:),marker_string{2,1},'linewidth',1) + plot(1:gend,innov(k,:),marker_string{2,1},'linewidth',1) hold off - name = options_.varobs{index(k)}; - if gend>1 - xlim([1 gend]) - end + name = M_.exo_names{k}; if ~isempty(options_.XTick) set(gca,'XTick',options_.XTick) set(gca,'XTickLabel',options_.XTickLabel) end + if gend>1 + xlim([1 gend]) + end if options_.TeX - idx = strmatch(options_.varobs{index(k)}, M_.endo_names, 'exact'); - title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + title(['$' M_.exo_names_tex{k} '$'],'Interpreter','latex') else title(name,'Interpreter','none') end end - dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_SmoothedObservationErrors' int2str(plt)],options_.nodisplay,options_.graph_format); + dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_SmoothedShocks' int2str(plt)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) fprintf(fidTeX,'\\begin{figure}[H]\n'); fprintf(fidTeX,'\\centering \n'); - fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedObservationErrors%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); - fprintf(fidTeX,'\\caption{Smoothed observation errors.}'); - fprintf(fidTeX,'\\label{Fig:SmoothedObservationErrors:%s}\n',int2str(plt)); + fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedShocks%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); + fprintf(fidTeX,'\\caption{Smoothed shocks.}'); + fprintf(fidTeX,'\\label{Fig:SmoothedShocks:%s}\n',int2str(plt)); fprintf(fidTeX,'\\end{figure}\n'); fprintf(fidTeX,'\n'); end @@ -189,64 +125,131 @@ if estim_params_.nvn fclose(fidTeX); end end -end -%% -%% Historical and smoothed variabes -%% -if ~options_.nograph - [nbplt,nr,nc,~,~,nstar] = pltorg(n_varobs); - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_HistoricalAndSmoothedVariables.tex'],'w'); - fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n'); - fprintf(fidTeX,['%% ' datestr(now,0) '\n']); - fprintf(fidTeX,' \n'); - end - for plt = 1:nbplt - fh = dyn_figure(options_.nodisplay,'Name','Historical and smoothed variables'); - nstar0=min(nstar,n_varobs-(plt-1)*nstar); - if gend==1 - marker_string{1,1}='-ro'; - marker_string{2,1}='--ko'; - else - marker_string{1,1}='-r'; - marker_string{2,1}='--k'; + if estim_params_.nvn + number_of_plots_to_draw = 0; + index = []; + for obs_iter=1:n_varobs + if max(abs(measurement_error(obs_iter,:))) > options_.ME_plot_tol + number_of_plots_to_draw = number_of_plots_to_draw + 1; + index = cat(1,index,obs_iter); + end end - for i=1:nstar0 - k = (plt-1)*nstar+i; - subplot(nr,nc,i); - plot(1:gend,yf(k,:),marker_string{1,1},'linewidth',1) - hold on - plot(1:gend,dataset_info.rawdata(:,k),marker_string{2,1},'linewidth',1) - hold off - name = options_.varobs{k}; - if ~isempty(options_.XTick) - set(gca,'XTick',options_.XTick) - set(gca,'XTickLabel',options_.XTickLabel) + if ~options_.nograph + [nbplt,nr,nc,~,~,nstar] = pltorg(number_of_plots_to_draw); + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_SmoothedObservationErrors.tex'],'w'); + fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n'); + fprintf(fidTeX,['%% ' datestr(now,0) '\n']); + fprintf(fidTeX,' \n'); end - if gend>1 - xlim([1 gend]) + for plt = 1:nbplt + fh = dyn_figure(options_.nodisplay,'Name','Smoothed observation errors'); + nstar0=min(nstar,number_of_plots_to_draw-(plt-1)*nstar); + if gend==1 + marker_string{1,1}='-ro'; + marker_string{2,1}='-ko'; + else + marker_string{1,1}='-r'; + marker_string{2,1}='-k'; + end + for i=1:nstar0 + k = (plt-1)*nstar+i; + subplot(nr,nc,i); + plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5) + hold on + plot(1:gend,measurement_error(index(k),:),marker_string{2,1},'linewidth',1) + hold off + name = options_.varobs{index(k)}; + if gend>1 + xlim([1 gend]) + end + if ~isempty(options_.XTick) + set(gca,'XTick',options_.XTick) + set(gca,'XTickLabel',options_.XTickLabel) + end + if options_.TeX + idx = strmatch(options_.varobs{index(k)}, M_.endo_names, 'exact'); + title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + else + title(name,'Interpreter','none') + end + end + dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_SmoothedObservationErrors' int2str(plt)],options_.nodisplay,options_.graph_format); + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fprintf(fidTeX,'\\begin{figure}[H]\n'); + fprintf(fidTeX,'\\centering \n'); + fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_SmoothedObservationErrors%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); + fprintf(fidTeX,'\\caption{Smoothed observation errors.}'); + fprintf(fidTeX,'\\label{Fig:SmoothedObservationErrors:%s}\n',int2str(plt)); + fprintf(fidTeX,'\\end{figure}\n'); + fprintf(fidTeX,'\n'); + end end - if options_.TeX - idx = strmatch(options_.varobs{k}, M_.endo_names,'exact'); - title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fprintf(fidTeX,'\n'); + fprintf(fidTeX,'%% End of TeX file.\n'); + fclose(fidTeX); + end + end + end + %% + %% Historical and smoothed variabes + %% + if ~options_.nograph + [nbplt,nr,nc,~,~,nstar] = pltorg(n_varobs); + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fidTeX = fopen([M_.dname, '/graphs/' M_.fname '_HistoricalAndSmoothedVariables.tex'],'w'); + fprintf(fidTeX,'%% TeX eps-loader file generated by dynare_estimation_1.m (Dynare).\n'); + fprintf(fidTeX,['%% ' datestr(now,0) '\n']); + fprintf(fidTeX,' \n'); + end + for plt = 1:nbplt + fh = dyn_figure(options_.nodisplay,'Name','Historical and smoothed variables'); + nstar0=min(nstar,n_varobs-(plt-1)*nstar); + if gend==1 + marker_string{1,1}='-ro'; + marker_string{2,1}='--ko'; else - title(name,'Interpreter','none') + marker_string{1,1}='-r'; + marker_string{2,1}='--k'; + end + for i=1:nstar0 + k = (plt-1)*nstar+i; + subplot(nr,nc,i); + plot(1:gend,yf(k,:),marker_string{1,1},'linewidth',1) + hold on + plot(1:gend,dataset_info.rawdata(:,k),marker_string{2,1},'linewidth',1) + hold off + name = options_.varobs{k}; + if ~isempty(options_.XTick) + set(gca,'XTick',options_.XTick) + set(gca,'XTickLabel',options_.XTickLabel) + end + if gend>1 + xlim([1 gend]) + end + if options_.TeX + idx = strmatch(options_.varobs{k}, M_.endo_names,'exact'); + title(['$' M_.endo_names_tex{idx} '$'],'Interpreter','latex') + else + title(name,'Interpreter','none') + end + end + dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_HistoricalAndSmoothedVariables' int2str(plt)],options_.nodisplay,options_.graph_format); + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fprintf(fidTeX,'\\begin{figure}[H]\n'); + fprintf(fidTeX,'\\centering \n'); + fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_HistoricalAndSmoothedVariables%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); + fprintf(fidTeX,'\\caption{Historical and smoothed variables.}'); + fprintf(fidTeX,'\\label{Fig:HistoricalAndSmoothedVariables:%s}\n',int2str(plt)); + fprintf(fidTeX,'\\end{figure}\n'); + fprintf(fidTeX,'\n'); end end - dyn_saveas(fh,[M_.dname, '/graphs/' M_.fname '_HistoricalAndSmoothedVariables' int2str(plt)],options_.nodisplay,options_.graph_format); if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fprintf(fidTeX,'\\begin{figure}[H]\n'); - fprintf(fidTeX,'\\centering \n'); - fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_HistoricalAndSmoothedVariables%s}\n',options_.figures.textwidth*min(i/nc,1),[M_.dname, '/graphs/' M_.fname],int2str(plt)); - fprintf(fidTeX,'\\caption{Historical and smoothed variables.}'); - fprintf(fidTeX,'\\label{Fig:HistoricalAndSmoothedVariables:%s}\n',int2str(plt)); - fprintf(fidTeX,'\\end{figure}\n'); fprintf(fidTeX,'\n'); + fprintf(fidTeX,'%% End of TeX file.\n'); + fclose(fidTeX); end end - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fprintf(fidTeX,'\n'); - fprintf(fidTeX,'%% End of TeX file.\n'); - fclose(fidTeX); - end -end +end \ No newline at end of file -- GitLab