1function forecast_graphs(var_list,M_, oo_,options_)
2% function forecast_graphs(var_list,M_, oo_,options_)
3% Plots the classical forecasts created by dyn_forecast.m
4%
5% Inputs:
6%   o var_list              character array with variable names
7%   o M_                    model structure
8%   o oo_                   outputs structure
9%   o options_              options structure
10
11% Copyright (C) 2008-2018 Dynare Team
12%
13% This file is part of Dynare.
14%
15% Dynare is free software: you can redistribute it and/or modify
16% it under the terms of the GNU General Public License as published by
17% the Free Software Foundation, either version 3 of the License, or
18% (at your option) any later version.
19%
20% Dynare is distributed in the hope that it will be useful,
21% but WITHOUT ANY WARRANTY; without even the implied warranty of
22% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23% GNU General Public License for more details.
24%
25% You should have received a copy of the GNU General Public License
26% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
27
28nc = 4;
29nr = 3;
30endo_names = M_.endo_names;
31fname = M_.fname;
32dname = M_.dname;
33
34yf = oo_.forecast.Mean;
35hpdinf = oo_.forecast.HPDinf;
36hpdsup = oo_.forecast.HPDsup;
37if isempty(var_list)
38    var_list = endo_names(1:M_.orig_endo_nbr);
39end
40i_var = [];
41for i = 1:length(var_list)
42    tmp = strmatch(var_list{i}, endo_names, 'exact');
43    if isempty(tmp)
44        error([var_list{i} ' isn''t an endogenous variable'])
45    end
46    i_var = [i_var; tmp];
47end
48nvar = length(i_var);
49
50% create subdirectory <dname>/graphs if id doesn't exist
51if ~exist(dname, 'dir')
52    mkdir('.',dname);
53end
54if ~exist([dname '/graphs'],'dir')
55    mkdir(dname,'graphs');
56end
57
58if options_.TeX && any(strcmp('eps', cellstr(options_.graph_format)))
59    fidTeX = write_LaTeX_header([M_.dname '/graphs/', fname, '_forcst.tex']);
60end
61
62m = 1;
63n_fig = 1;
64hh = dyn_figure(options_.nodisplay, 'Name', 'Forecasts (I)');
65
66for j= 1:nvar
67    if m > nc*nr
68        dyn_saveas(hh,[ dname '/graphs/forcst' int2str(n_fig)], options_.nodisplay, options_.graph_format);
69        if options_.TeX && any(strcmp('eps', cellstr(options_.graph_format)))
70            fprintf(fidTeX,'\\begin{figure}[H]\n');
71            fprintf(fidTeX,'\\centering \n');
72            fprintf(fidTeX,'\\includegraphics[width=0.8\\textwidth]{%s/graphs/forcst%d}\n',dname,n_fig);
73            fprintf(fidTeX,'\\label{Fig:forcst:%d}\n',n_fig);
74            fprintf(fidTeX,'\\caption{Mean forecasts and %2.0f\\%% confidence intervals}\n',options_.forecasts.conf_sig*100);
75            fprintf(fidTeX,'\\end{figure}\n');
76            fprintf(fidTeX,' \n');
77        end
78        n_fig =n_fig+1;
79        eval(['hh=dyn_figure(options_.nodisplay,''Name'',''Forecasts (' int2str(n_fig) ')'');']);
80        m = 1;
81    end
82    subplot(nr, nc, m);
83    vn = endo_names{i_var(j)};
84    obs = 0;
85    plot([NaN(obs,1); yf.(vn)],'b-');
86    hold on
87    plot([NaN(obs,1); hpdinf.(vn)],'b-');
88    hold on
89    plot([NaN(obs,1); hpdsup.(vn)],'b-');
90    title(vn,'Interpreter','none');
91    xlim([1 obs+length(hpdsup.(vn))])
92    hold off
93    m = m + 1;
94end
95
96if m > 1
97    dyn_saveas(hh,[dname '/graphs/forcst' int2str(n_fig)],options_.nodisplay,options_.graph_format);
98    if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
99        fprintf(fidTeX,'\\begin{figure}[H]\n');
100        fprintf(fidTeX,'\\centering \n');
101        fprintf(fidTeX,'\\includegraphics[width=0.8\\textwidth]{%s/graphs/forcst%d}\n',dname,n_fig);
102        fprintf(fidTeX,'\\label{Fig:forcst:%d}\n',n_fig);
103        fprintf(fidTeX,'\\caption{Mean forecasts and %2.0f\\%% confidence intervals}\n',options_.forecasts.conf_sig*100);
104        fprintf(fidTeX,'\\end{figure}\n');
105        fprintf(fidTeX,' \n');
106    end
107end
108
109if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
110    write_LaTeX_footer(fidTeX);
111end
112
113if isfield(oo_.forecast,'HPDinf_ME')
114    var_names=fieldnames(oo_.forecast.HPDinf_ME);
115
116    if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
117        fidTeX=write_LaTeX_header([M_.dname '/graphs/',fname,'_forcst_ME.tex']);
118    end
119
120    m = 1;
121    n_fig = 1;
122    hh=dyn_figure(options_.nodisplay,'Name','Forecasts including ME (I)');
123    for j= 1:length(var_names)
124        if m > nc*nr
125            dyn_saveas(hh,[ dname '/graphs/forcst_ME' int2str(n_fig)],options_.nodisplay,options_.graph_format);
126            if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
127                fprintf(fidTeX,'\\begin{figure}[H]\n');
128                fprintf(fidTeX,'\\centering \n');
129                fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s/graphs/forcst_ME%d}\n',options_.figures.textwidth*min((m-1)/nc,1),dname,n_fig);
130                fprintf(fidTeX,'\\label{Fig:forcst_ME:%d}\n',n_fig);
131                fprintf(fidTeX,'\\caption{Mean forecasts and %2.0f\\%% confidence intervals accounting for measurement error}\n',options_.forecasts.conf_sig*100);
132                fprintf(fidTeX,'\\end{figure}\n');
133                fprintf(fidTeX,' \n');
134            end
135            n_fig =n_fig+1;
136            eval(['hh=dyn_figure(options_.nodisplay,''Name'',''Forecasts (' int2str(n_fig) ')'');']);
137            m = 1;
138        end
139        subplot(nr,nc,m);
140        vn = var_names{j};
141        obs = 0;
142
143        plot([NaN(obs,1); yf.(vn)],'b-');
144        hold on
145        plot([NaN(obs,1); oo_.forecast.HPDinf_ME.(vn)],'b-');
146        hold on
147        plot([NaN(obs,1); oo_.forecast.HPDsup_ME.(vn)],'b-');
148        title(vn,'Interpreter','none');
149        xlim([1 obs+length(oo_.forecast.HPDsup_ME.(vn))])
150        hold off
151        m = m + 1;
152    end
153
154    if m > 1
155        dyn_saveas(hh,[dname '/graphs/forcst_ME' int2str(n_fig)],options_.nodisplay,options_.graph_format);
156        if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
157            fprintf(fidTeX,'\\begin{figure}[H]\n');
158            fprintf(fidTeX,'\\centering \n');
159            fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s/graphs/forcst_ME%d}\n',options_.figures.textwidth*min((m-1)/nc,1),dname,n_fig);
160            fprintf(fidTeX,'\\label{Fig:forcst_ME:%d}\n',n_fig);
161            fprintf(fidTeX,'\\caption{Mean forecasts and %2.0f\\%% confidence intervals accounting for measurement error}\n',options_.forecasts.conf_sig*100);
162            fprintf(fidTeX,'\\end{figure}\n');
163            fprintf(fidTeX,' \n');
164        end
165    end
166
167    if options_.TeX && any(strcmp('eps',cellstr(options_.graph_format)))
168        write_LaTeX_footer(fidTeX);
169    end
170end
171
172
173function fidTeX=write_LaTeX_header(fname)
174%% write LaTeX-Header
175fidTeX = fopen(fname,'w');
176fprintf(fidTeX,'%% TeX eps-loader file generated by Dynare''s forecast_graphs.m.\n');
177fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
178fprintf(fidTeX,' \n');
179
180
181function write_LaTeX_footer(fidTeX)
182%% write LaTeX-Footer
183fprintf(fidTeX,' \n');
184fprintf(fidTeX,'%% End of TeX file.\n');
185fclose(fidTeX);
186