1function [info,description] = check_posterior_analysis_data(type,M_)
2% function [info,description] = check_posterior_analysis_data(type,M_)
3% Checks the status of posterior analysis and in particular if files need to be
4% created or updated; called by posterior_analysis.m
5%
6% Inputs:
7%   type        [string]        name of the posterior moment considered
8%   M_          [structure]     Dynare model structure
9%
10% Outputs:
11%   info        [scalar]        return code
12%                                   info = 1; % select_posterior_draws has to be called first.
13%                                   info = 2; % _posterior_draws files have to be updated.
14%                                   info = 3; % Ok! posterior draws files are up to date ;
15%                                   info = 4; % posterior draws have to be processed.
16%                                   info = 5; % posterior data files have to be updated.
17%                                   info = 6; % Ok (nothing to do ;-)
18%   description [string]        Message corresponding to info
19
20% Copyright (C) 2008-2017 Dynare Team
21%
22% This file is part of Dynare.
23%
24% Dynare is free software: you can redistribute it and/or modify
25% it under the terms of the GNU General Public License as published by
26% the Free Software Foundation, either version 3 of the License, or
27% (at your option) any later version.
28%
29% Dynare is distributed in the hope that it will be useful,
30% but WITHOUT ANY WARRANTY; without even the implied warranty of
31% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32% GNU General Public License for more details.
33%
34% You should have received a copy of the GNU General Public License
35% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
36
37info = 0;
38if nargout>1
39    description = '';
40end
41
42[MetropolisFolder, info] = CheckPath('metropolis',M_.dname);
43
44% Get informations about mcmc files.
45if info
46    disp('check_posterior_analysis_data:: Can''t find any mcmc file!')
47    return
48end
49
50mhname = get_name_of_the_last_mh_file(M_);
51mhdate = get_date_of_a_file([MetropolisFolder filesep mhname]);
52
53% Get informations about _posterior_draws files.
54drawsinfo = dir([ MetropolisFolder filesep M_.fname '_posterior_draws*.mat']);
55if isempty(drawsinfo)
56    info = 1; % select_posterior_draws has to be called first.
57    if nargout>1
58        description = 'select_posterior_draws has to be called.';
59    end
60    return
61else
62    number_of_last_posterior_draws_file = length(drawsinfo);
63    pddate = get_date_of_a_file([ MetropolisFolder filesep M_.fname '_posterior_draws' int2str(number_of_last_posterior_draws_file) '.mat']);
64    if pddate<mhdate
65        info = 2; % _posterior_draws files have to be updated.
66        if nargout>1
67            description = 'posterior draws files have to be updated.';
68        end
69        return
70    else
71        info = 3; % Ok!
72        if nargout>1
73            description = 'posterior draws files are up to date.';
74        end
75    end
76end
77
78% Get informations about posterior data files.
79switch type
80  case 'variance'
81    generic_post_data_file_name = 'Posterior2ndOrderMoments';
82  case 'decomposition'
83    generic_post_data_file_name = 'PosteriorVarianceDecomposition';
84  case 'correlation'
85    generic_post_data_file_name = 'PosteriorCorrelations';
86  case 'conditional decomposition'
87    generic_post_data_file_name = 'PosteriorConditionalVarianceDecomposition';
88  otherwise
89    disp('This feature is not yet implemented!')
90end
91pdfinfo = dir([ MetropolisFolder filesep M_.fname '_' generic_post_data_file_name '*']);
92if isempty(pdfinfo)
93    info = 4; % posterior draws have to be processed.
94    if nargout>1
95        description = 'posterior draws files have to be processed.';
96    end
97    return
98else
99    number_of_the_last_post_data_file = length(pdfinfo);
100    name_of_the_last_post_data_file = ...
101        [ pwd filesep MetropolisFolder filesep ...
102          M_.fname '_' ...
103          generic_post_data_file_name ...
104          int2str(number_of_the_last_post_data_file) ...
105          '.mat' ];
106    pdfdate = get_date_of_a_file(name_of_the_last_post_data_file);
107    if pdfdate<pddate
108        info = 5; % posterior data files have to be updated.
109        if nargout>1
110            description = 'posterior data files have to be updated.';
111        end
112    else
113        info = 6; % Ok (nothing to do ;-)
114        if nargout>1
115            description = 'There is nothing to do';
116        end
117    end
118end