diff --git a/matlab/kalman/evaluate_smoother.m b/matlab/kalman/evaluate_smoother.m index f093701df8e428693db326b38b37710e69bc5026..599bc5b96de928021f2cb6059390504892241905 100644 --- a/matlab/kalman/evaluate_smoother.m +++ b/matlab/kalman/evaluate_smoother.m @@ -39,7 +39,7 @@ function [oo_,M_,options_,bayestopt_,Smoothed_variables_declaration_order_deviat % [1] This function use persistent variables for the dataset and the description of the missing observations. Consequently, if this function % is called more than once (by changing the value of parameters) the sample *must not* change. -% Copyright © 2010-2023 Dynare Team +% Copyright © 2010-2024 Dynare Team % % This file is part of Dynare. % @@ -127,13 +127,13 @@ else end if ~(options_.occbin.smoother.status && options_.occbin.smoother.inversion_filter) if ~options_.occbin.smoother.status || (options_.occbin.smoother.status && oo_.occbin.smoother.error_flag==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); end else if ~oo_.occbin.smoother.error_flag options_nk=options_.nk; options_.nk=[]; %unset options_.nk and reset it later - [oo_]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); + [oo_,yf]=store_smoother_results(M_,oo_,options_,bayestopt_,dataset_,dataset_info,atT,innov,measurement_error,updated_variables,ys,trend_coeff); options_.nk=options_nk; else fprintf('\nIVF: smoother did not succeed. No results will be written to oo_.\n') @@ -143,6 +143,10 @@ if nargout>4 Smoothed_variables_declaration_order_deviation_form=atT(oo_.dr.inv_order_var(bayestopt_.smoother_var_list),:); end +if ~options_.occbin.smoother.status || (options_.occbin.smoother.status && oo_.occbin.smoother.error_flag==0) + plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf) +end + %reset qz_criterium options_.qz_criterium=qz_criterium_old; oo_.gui.ran_calib_smoother = true; diff --git a/matlab/kalman/plot_classical_smoother_results.m b/matlab/kalman/plot_classical_smoother_results.m new file mode 100644 index 0000000000000000000000000000000000000000..578d3166a146a40cbff6e9b57dde54fbeb61575c --- /dev/null +++ b/matlab/kalman/plot_classical_smoother_results.m @@ -0,0 +1,219 @@ +function plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf) +% function plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf) +% Inputs: +% M_ [structure] storing the model information +% oo_ [structure] storing the results +% options_ [structure] storing the options +% dataset_ [structure] storing the dataset +% estim_params_ [structure] storing information about estimated parameters +% yf [double] (nvarobs*T) matrix storing the smoothed observed variables (order of options_.varobs) +% +% Outputs: +% None + +% Copyright © 2024 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see <https://www.gnu.org/licenses/>. + +gend= dataset_.nobs; +n_varobs = length(options_.varobs); + +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,oo_.SmoothedShocks.(M_.exo_names{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); + end +end +if estim_params_.nvn + number_of_plots_to_draw = 0; + index = []; + for obs_iter=1:n_varobs + if max(abs(oo_.SmoothedMeasurementErrors.(options_.varobs{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 ~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 + 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); + name = options_.varobs{index(k)}; + plot([1 gend],[0 0],marker_string{1,1},'linewidth',.5) + hold on + plot(1:gend,oo_.SmoothedMeasurementErrors.(name),marker_string{2,1},'linewidth',1) + hold off + 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 && 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 + marker_string{1,1}='-r'; + marker_string{2,1}='--k'; + end + for i=1:nstar0 + k = (plt-1)*nstar+i; + subplot(nr,nc,i); + name = options_.varobs{k}; + 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 + 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 + if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) + fprintf(fidTeX,'\n'); + fprintf(fidTeX,'%% End of TeX file.\n'); + fclose(fidTeX); + end +end diff --git a/matlab/kalman/save_display_classical_smoother_results.m b/matlab/kalman/save_display_classical_smoother_results.m index e1e5f87cacaa298770710d1cf04289056592b7cd..46d42c99694af366a4b6df4cc0602eba134ec0ba 100644 --- a/matlab/kalman/save_display_classical_smoother_results.m +++ b/matlab/kalman/save_display_classical_smoother_results.m @@ -30,10 +30,6 @@ function oo_=save_display_classical_smoother_results(xparam1,M_,oo_,options_,bay % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <https://www.gnu.org/licenses/>. - -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); @@ -66,190 +62,5 @@ else end end if ~smoother_error - 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); - 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 ~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 - 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 && 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 - 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 - if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format))) - fprintf(fidTeX,'\n'); - fprintf(fidTeX,'%% End of TeX file.\n'); - fclose(fidTeX); - end - end + plot_classical_smoother_results(M_,oo_,options_,dataset_info,dataset_,estim_params_,yf); end \ No newline at end of file