1function trace_plot(options_,M_,estim_params_,type,blck,name1,name2) 2% This function builds trace plot for the Metropolis-Hastings draws. 3% 4% INPUTS 5% 6% options_ [structure] Dynare structure. 7% M_ [structure] Dynare structure (related to model definition). 8% estim_params_ [structure] Dynare structure (related to estimation). 9% type [string] 'DeepParameter', 'MeasurementError' (for measurement equation error), 10% 'StructuralShock' (for structural shock) 11% or 'PosteriorDensity (for posterior density)' 12% blck [integer] Number of the mh chain. 13% name1 [string] Object name. 14% name2 [string] Object name. 15% 16% OUTPUTS 17% None 18% 19% SPECIAL REQUIREMENTS 20 21% Copyright (C) 2003-2018 Dynare Team 22% 23% This file is part of Dynare. 24% 25% Dynare is free software: you can redistribute it and/or modify 26% it under the terms of the GNU General Public License as published by 27% the Free Software Foundation, either version 3 of the License, or 28% (at your option) any later version. 29% 30% Dynare is distributed in the hope that it will be useful, 31% but WITHOUT ANY WARRANTY; without even the implied warranty of 32% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33% GNU General Public License for more details. 34% 35% You should have received a copy of the GNU General Public License 36% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 37 38% Cet the column index: 39if strcmpi(type,'PosteriorDensity') 40 column=0; 41 name1=''; 42else 43 if nargin<7 44 column = name2index(options_, M_, estim_params_, type, name1); 45 else 46 column = name2index(options_, M_, estim_params_, type, name1, name2); 47 end 48end 49 50if isempty(column) 51 return 52end 53 54% Get informations about the posterior draws: 55MetropolisFolder = CheckPath('metropolis',M_.dname); 56load_last_mh_history_file(MetropolisFolder, M_.fname); 57 58FirstMhFile = 1; 59FirstLine = 1; 60TotalNumberOfMhFiles = sum(record.MhDraws(:,2)); 61TotalNumberOfMhDraws = sum(record.MhDraws(:,1)); 62clear record; 63 64% Get all the posterior draws: 65PosteriorDraws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, blck); 66 67 68% Plot the posterior draws: 69 70if strcmpi(type,'DeepParameter') 71 TYPE = 'parameter '; 72elseif strcmpi(type,'StructuralShock') 73 if nargin<7 74 TYPE = 'the standard deviation of structural shock '; 75 else 76 TYPE = 'the correlation between structural shocks '; 77 end 78elseif strcmpi(type,'MeasurementError') 79 if nargin<7 80 TYPE = 'the standard deviation of measurement error '; 81 else 82 TYPE = 'the correlation between measurement errors '; 83 end 84elseif strcmpi(type,'PosteriorDensity') 85 TYPE='the posterior density'; 86end 87 88if nargin<7 89 FigureName = ['Trace plot for ' TYPE name1]; 90else 91 FigureName = ['Trace plot for ' TYPE name1 ' and ' name2]; 92end 93 94if options_.mh_nblck>1 95 FigureName = [ FigureName , ' (block number ' int2str(blck) ').']; 96end 97 98hh=dyn_figure(options_.nodisplay,'Name',FigureName); 99plot(1:TotalNumberOfMhDraws,PosteriorDraws,'Color',[.7 .7 .7]); 100 101 102% Compute the moving average of the posterior draws: 103 104N = options_.trace_plot_ma; 105MovingAverage = NaN(TotalNumberOfMhDraws,1); 106first = N+1; 107last = TotalNumberOfMhDraws-N; 108 109for t=first:last 110 MovingAverage(t) = mean(PosteriorDraws(t-N:t+N)); 111end 112 113hold on 114plot(1:TotalNumberOfMhDraws,MovingAverage,'-k','linewidth',2) 115hold off 116axis tight 117legend({'MCMC draw';[num2str(N) ' period moving average']},'Location','NorthWest') 118% create subdirectory <fname>/graphs if it doesn't exist 119if ~exist(M_.fname, 'dir') 120 mkdir('.',M_.fname); 121end 122if ~exist([M_.fname filesep 'graphs'],'dir') 123 mkdir(M_.fname,'graphs'); 124end 125 126%get name for plot 127if strcmpi(type,'PosteriorDensity') 128 plot_name='Posterior'; 129else 130 plot_name=get_the_name(column,0,M_,estim_params_,options_); 131end 132plot_name=[plot_name,'_blck_',num2str(blck)]; 133 134dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'TracePlot_' plot_name],options_.nodisplay,options_.graph_format) 135 136if options_.TeX 137 fid=fopen([M_.fname,'/graphs/',M_.fname,'_TracePlot_' plot_name,'.tex'],'w+'); 138 139 if strcmpi(type,'DeepParameter') 140 tex_names=M_.param_names_tex; 141 base_names=M_.param_names; 142 elseif strcmpi(type,'StructuralShock') 143 tex_names=M_.exo_names_tex; 144 base_names=M_.exo_names; 145 elseif strcmpi(type,'MeasurementError') 146 tex_names=M_.endo_names_tex; 147 base_names=M_.endo_names; 148 end 149 150 if strcmpi(type,'PosteriorDensity') 151 FigureName = ['Trace plot for ' TYPE name1]; 152 else 153 if nargin<7 154 FigureName = ['Trace plot for ' TYPE '$' tex_names{strmatch(name1,base_names,'exact')} '$']; 155 else 156 FigureName = ['Trace plot for ' TYPE '$' tex_names{strmatch(name1,base_names,'exact')} '$ and $' tex_names{strmatch(name2,base_names,'exact')} '$']; 157 end 158 end 159 if options_.mh_nblck>1 160 FigureName = [ FigureName , ' (block number ' int2str(blck) ').']; 161 end 162 163 fprintf(fid,'%-s\n','\begin{figure}[H]'); 164 fprintf(fid,'%-s\n','\centering'); 165 fprintf(fid,'%-s\n',[' \includegraphics[width=0.8\textwidth]{',[M_.fname, '/graphs/TracePlot_' plot_name],'}\\']); 166 fprintf(fid,'%-s\n',[' \caption{',FigureName,'}']); 167 fprintf(fid,'%-s\n','\end{figure}'); 168 fclose(fid); 169end 170