Home > matlab > trace_plot.m

trace_plot

PURPOSE ^

This function build trace plot for the metropolis hastings draws.

SYNOPSIS ^

function trace_plot(options_,M_,estim_params_,type,blck,name1,name2)

DESCRIPTION ^

 This function build trace plot for the metropolis hastings draws.
 

 INPUTS 

   options_        [structure]    Dynare structure.
   M_              [structure]    Dynare structure (related to model definition).
   estim_params_   [structure]    Dynare structure (related to estimation).
   type            [string]       'DeepParameter', 'MeasurementError' (for measurement equation error) or 'StructuralShock' (for structural shock).
   blck            [integer]      Number of the mh chain.
   name1           [string]       Object name.
   name2           [string]       Object name. 
    
 OUTPUTS 
   None
        
 SPECIAL REQUIREMENTS

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function trace_plot(options_,M_,estim_params_,type,blck,name1,name2)
0002 % This function build trace plot for the metropolis hastings draws.
0003 %
0004 %
0005 % INPUTS
0006 %
0007 %   options_        [structure]    Dynare structure.
0008 %   M_              [structure]    Dynare structure (related to model definition).
0009 %   estim_params_   [structure]    Dynare structure (related to estimation).
0010 %   type            [string]       'DeepParameter', 'MeasurementError' (for measurement equation error) or 'StructuralShock' (for structural shock).
0011 %   blck            [integer]      Number of the mh chain.
0012 %   name1           [string]       Object name.
0013 %   name2           [string]       Object name.
0014 %
0015 % OUTPUTS
0016 %   None
0017 %
0018 % SPECIAL REQUIREMENTS
0019 
0020 % Copyright (C) 2003-2009 Dynare Team
0021 %
0022 % This file is part of Dynare.
0023 %
0024 % Dynare is free software: you can redistribute it and/or modify
0025 % it under the terms of the GNU General Public License as published by
0026 % the Free Software Foundation, either version 3 of the License, or
0027 % (at your option) any later version.
0028 %
0029 % Dynare is distributed in the hope that it will be useful,
0030 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0031 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0032 % GNU General Public License for more details.
0033 %
0034 % You should have received a copy of the GNU General Public License
0035 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0036 
0037 % Cet the column index:
0038 if nargin<7    
0039     column = name2index(options_, M_, estim_params_, type, name1);
0040 else
0041     column = name2index(options_, M_, estim_params_, type, name1, name2);
0042 end
0043 
0044 if isempty(column)
0045     return
0046 end
0047 
0048 % Get informations about the posterior draws:
0049 DirectoryName = CheckPath('metropolis',M_.dname);
0050 try
0051     load([DirectoryName '/' M_.fname '_mh_history.mat']); 
0052 catch
0053     disp(['trace_plot:: I can''t find ' M_.fname '_results.mat !'])
0054     disp(['trace_plot:: Did you run a metropolis?'])
0055     return
0056 end
0057 
0058 FirstMhFile = 1;
0059 FirstLine = 1;
0060 TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
0061 TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
0062 clear record;
0063 
0064 % Get all the posterior draws:
0065 PosteriorDraws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, TotalNumberOfMhDraws, blck);
0066 
0067 
0068 % Plot the posterior draws:
0069 
0070 if strcmpi(type,'DeepParameter')
0071     TYPE = 'parameter ';
0072 elseif strcmpi(type,'StructuralShock')
0073     if nargin<7
0074         TYPE = 'the standard deviation of structural shock ';
0075     else
0076         TYPE = 'the correlation between structural shocks ';
0077     end
0078 elseif strcmpi(type,'MeasurementError')
0079     if nargin<7
0080         TYPE = 'the standard deviation of measurement error ';
0081     else
0082         TYPE = 'the correlation between measurement errors ';
0083     end
0084 end
0085 
0086 if nargin<7
0087     FigureName = ['trace plot for ' TYPE name1];
0088 else
0089     FigureName = ['trace plot for ' TYPE name1 ' and ' name2];
0090 end
0091 
0092 if options_.mh_nblck>1
0093     FigureName = [ FigureName , ' (block number' int2str(blck)  ').']; 
0094 end
0095 
0096 
0097 figure('Name',FigureName)
0098 plot(1:TotalNumberOfMhDraws,PosteriorDraws,'Color',[.8 .8 .8]);
0099 
0100 
0101 % Compute the moving average of the posterior draws:
0102 
0103 N = options_.trace_plot_ma;
0104 MovingAverage = NaN(TotalNumberOfMhDraws,1);
0105 first = N+1;
0106 last = TotalNumberOfMhDraws-N;
0107 
0108 for t=first:last
0109     MovingAverage(t) = mean(PosteriorDraws(t-N:t+N)); 
0110 end
0111 
0112 hold on
0113 plot(1:TotalNumberOfMhDraws,MovingAverage,'-k','linewidth',2)
0114 hold off
0115 axis tight

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005