1function mh_autocorrelation_function(options_,M_,estim_params_,type,blck,name1,name2)
2% This function plots the autocorrelation of the sampled draws in the
3% posterior distribution.
4%
5%
6% INPUTS
7%
8%   options_        [structure]    Dynare structure.
9%   M_              [structure]    Dynare structure (related to model definition).
10%   estim_params_   [structure]    Dynare structure (related to estimation).
11%   type            [string]       'DeepParameter', 'MeasurementError' (for measurement equation error) or 'StructuralShock' (for structural shock).
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-2017 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 nargin<7
40    column = name2index(options_, M_, estim_params_, type, name1);
41else
42    column = name2index(options_, M_, estim_params_, type, name1, name2);
43end
44
45if isempty(column)
46    return
47end
48
49% Get informations about the posterior draws:
50MetropolisFolder = CheckPath('metropolis',M_.dname);
51load_last_mh_history_file(MetropolisFolder, M_.fname);
52
53FirstMhFile = record.KeepedDraws.FirstMhFile;
54FirstLine = record.KeepedDraws.FirstLine; ifil = FirstLine;
55TotalNumberOfMhFiles = sum(record.MhDraws(:,2));
56TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
57NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
58clear record;
59
60% Get all the posterior draws:
61PosteriorDraws = GetAllPosteriorDraws(column, FirstMhFile, FirstLine, TotalNumberOfMhFiles, NumberOfDraws, blck);
62
63% Compute the autocorrelation function:
64[autocov,autocor] = sample_autocovariance(PosteriorDraws,options_.mh_autocorrelation_function_size);
65
66% Plot the posterior draws:
67
68if strcmpi(type,'DeepParameter')
69    TYPE = 'parameter ';
70elseif strcmpi(type,'StructuralShock')
71    if nargin<7
72        TYPE = 'the standard deviation of structural shock ';
73    else
74        TYPE = 'the correlation between structural shocks ';
75    end
76elseif strcmpi(type,'MeasurementError')
77    if nargin<7
78        TYPE = 'the standard deviation of measurement error ';
79    else
80        TYPE = 'the correlation between measurement errors ';
81    end
82end
83
84if nargin<7
85    FigureName = ['Autocorrelogram for ' TYPE name1];
86else
87    FigureName = ['Autocorrelogram for ' TYPE name1 ' and ' name2];
88end
89
90if options_.mh_nblck>1
91    FigureName = [ FigureName , ' (block number' int2str(blck)  ').'];
92end
93
94hh=dyn_figure(options_.nodisplay,'Name',FigureName);
95
96bar(0:options_.mh_autocorrelation_function_size,autocor,'k');
97axis tight
98% create subdirectory <fname>/graphs if it doesn't exist
99if ~exist(M_.fname, 'dir')
100    mkdir('.',M_.fname);
101end
102if ~exist([M_.fname filesep 'graphs'])
103    mkdir(M_.fname,'graphs');
104end
105
106plot_name=get_the_name(column,0,M_,estim_params_,options_);
107dyn_saveas(hh,[M_.fname, filesep, 'graphs', filesep, 'MH_Autocorrelation_' plot_name],options_.nodisplay,options_.graph_format)
108