1function [info,description] = check_prior_analysis_data(type,M_)
2% function [info,description] = check_prior_analysis_data(type,M_)
3% Checks the status of prior analysis and in particular if files need to be
4% created or updated; called by prior_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; % prior_sampler has to be called first.
13%                                   info = 2; % _prior_draws files have to be updated.
14%                                   info = 3; % Ok! prior draws files are up to date ;
15%                                   info = 4; % prior draws have to be processed.
16%                                   info = 5; % prior data files have to be updated.
17%                                   info = 6; % Ok (nothing to do ;-)
18%   description [string]        Message corresponding to info
19
20
21% Copyright (C) 2009-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
38info = 0;
39if nargout>1
40    description = '';
41end
42
43%% Get informations about prior draws files.
44if ~exist([ M_.dname '/prior/draws'],'dir')
45    disp('check_prior_analysis_data:: Can''t find any prior draws file!')
46    return
47end
48
49prior_draws_info = dir([ M_.dname '/prior/draws/prior_draws*.mat']);
50date_of_the_last_prior_draw_file = prior_draws_info(end).datenum;
51
52%% Get informations about _posterior_draws files.
53if isempty(prior_draws_info)
54    info = 1;
55    if nargout>1
56        description = 'prior_sampler has to be called.';
57    end
58    return
59else
60    date_of_the_prior_definition = get_date_of_a_file([ M_.dname '/prior/definition.mat']);
61    if date_of_the_prior_definition>date_of_the_last_prior_draw_file
62        info = 2;
63        if nargout>1
64            description = 'prior draws files have to be updated.';
65        end
66        return
67    else
68        info = 3; % Nothing to do!
69        if nargout>1
70            description = 'prior draws files are up to date.';
71        end
72    end
73end
74
75%% Get informations about prior data files.
76switch type
77  case 'variance'
78    generic_prior_data_file_name = 'Prior2ndOrderMoments';
79  case 'decomposition'
80    generic_prior_data_file_name = 'PriorVarianceDecomposition';
81  case 'correlation'
82    generic_prior_data_file_name = 'PriorCorrelations';
83  case 'conditional decomposition'
84    generic_prior_data_file_name = 'PriorConditionalVarianceDecomposition';
85  otherwise
86    disp(['This feature is not yet implemented!'])
87end
88CheckPath('prior/moments',M_.dname);
89pdfinfo = dir([ M_.dname '/prior/' generic_prior_data_file_name '*']);
90if isempty(pdfinfo)
91    info = 4;
92    if nargout>1
93        description = 'prior draws files have to be processed.';
94    end
95    return
96else
97    number_of_the_last_prior_data_file = length(pdfinfo);
98    name_of_the_last_prior_data_file = pdinfo(end).name;
99    pdfdate = pdinfo(end).datenum;
100    % /!\ REMARK /!\
101    % The user can change the model or the value of a calibrated
102    % parameter without changing the prior. In this case the (prior)
103    % moments should be computed. But this case cannot be detected!!!
104    if pdfdate<date_of_the_last_prior_draw_file
105        info = 5; % prior data files have to be updated.
106        if nargout>1
107            description = 'prior data files have to be updated.';
108        end
109    else
110        info = 6; % Ok (nothing to do ;-)
111        if nargout>1
112            description = 'prior data files are up to date.';
113        end
114    end
115end