1function oo_ = correlation_mc_analysis(SampleSize,type,dname,fname,vartan,nvar,var1,var2,nar,mh_conf_sig,oo_,M_,options_)
2% This function analyses the (posterior or prior) distribution of the
3% endogenous variables correlation function.
4
5% Copyright (C) 2008-2017 Dynare Team
6%
7% This file is part of Dynare.
8%
9% Dynare is free software: you can redistribute it and/or modify
10% it under the terms of the GNU General Public License as published by
11% the Free Software Foundation, either version 3 of the License, or
12% (at your option) any later version.
13%
14% Dynare is distributed in the hope that it will be useful,
15% but WITHOUT ANY WARRANTY; without even the implied warranty of
16% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17% GNU General Public License for more details.
18%
19% You should have received a copy of the GNU General Public License
20% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
21
22if strcmpi(type,'posterior')
23    TYPE = 'Posterior';
24    PATH = [dname '/metropolis/'];
25else
26    TYPE = 'Prior';
27    PATH = [dname '/prior/moments/'];
28end
29
30indx1 = check_name(vartan,var1);
31if isempty(indx1)
32    disp([ type '_analysis:: ' var1 ' is not a stationary endogenous variable!'])
33    return
34end
35if ~isempty(var2)
36    indx2 = check_name(vartan,var2);
37    if isempty(indx2)
38        disp([ type '_analysis:: ' var2 ' is not a stationary endogenous variable!'])
39        return
40    end
41else
42    indx2 = indx1;
43    var2 = var1;
44end
45
46var1=deblank(var1);
47var2=deblank(var2);
48
49if isfield(oo_,[TYPE 'TheoreticalMoments'])
50    temporary_structure = oo_.([TYPE, 'TheoreticalMoments']);
51    if isfield(temporary_structure,'dsge')
52        temporary_structure = oo_.([TYPE, 'TheoreticalMoments']).dsge;
53        if isfield(temporary_structure,'correlation')
54            temporary_structure = oo_.([TYPE, 'TheoreticalMoments']).dsge.correlation.Mean;
55            if isfield(temporary_structure,deblank(var1))
56                temporary_structure_1 = oo_.([TYPE, 'TheoreticalMoments']).dsge.correlation.Mean.(var1);
57                if isfield(temporary_structure_1,deblank(var2))
58                    temporary_structure_2 = temporary_structure_1.(var2);
59                    l1 = length(temporary_structure_2);
60                    if l1<nar
61                        % INITIALIZATION:
62                        oo_ = initialize_output_structure(var1,var2,nar,type,oo_);
63                        delete([PATH fname '_' TYPE 'Correlations*'])
64                        [nvar,vartan,NumberOfFiles] = ...
65                            dsge_simulated_theoretical_correlation(SampleSize,nar,M_,options_,oo_,type);
66                    else
67                        if ~isnan(temporary_structure_2(nar))
68                            %Nothing to do.
69                            return
70                        end
71                    end
72                else
73                    oo_ = initialize_output_structure(var1,var2,nar,TYPE,oo_,options_);
74                end
75            else
76                oo_ = initialize_output_structure(var1,var2,nar,TYPE,oo_,options_);
77            end
78        else
79            oo_ = initialize_output_structure(var1,var2,nar,TYPE,oo_,options_);
80        end
81    else
82        oo_ = initialize_output_structure(var1,var2,nar,TYPE,oo_,options_);
83    end
84else
85    oo_ = initialize_output_structure(var1,var2,nar,TYPE,oo_,options_);
86end
87ListOfFiles = dir([ PATH  fname '_' TYPE 'Correlations*.mat']);
88i1 = 1; tmp = zeros(SampleSize,1);
89for file = 1:length(ListOfFiles)
90    load([ PATH  ListOfFiles(file).name ]);
91    i2 = i1 + rows(Correlation_array) - 1;
92    tmp(i1:i2) = Correlation_array(:,indx1,indx2,nar);
93    i1 = i2+1;
94end
95name = [ var1 '.' var2 ];
96if options_.estimation.moments_posterior_density.indicator
97    [p_mean, p_median, p_var, hpd_interval, p_deciles, density] = ...
98        posterior_moments(tmp,1,mh_conf_sig);
99else
100    [p_mean, p_median, p_var, hpd_interval, p_deciles] = ...
101        posterior_moments(tmp,0,mh_conf_sig);
102end
103if isfield(oo_,[ TYPE 'TheoreticalMoments'])
104    temporary_structure = oo_.([TYPE, 'TheoreticalMoments']);
105    if isfield(temporary_structure,'dsge')
106        temporary_structure = oo_.([TYPE, 'TheoreticalMoments']).dsge;
107        if isfield(temporary_structure,'correlation')
108            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'Mean',nar,p_mean);
109            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'Median',nar,p_median);
110            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'Variance',nar,p_var);
111            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'HPDinf',nar,hpd_interval(1));
112            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'HPDsup',nar,hpd_interval(2));
113            oo_ = fill_output_structure(var1,var2,TYPE,oo_,'deciles',nar,p_deciles);
114            if options_.estimation.moments_posterior_density.indicator
115                oo_ = fill_output_structure(var1,var2,TYPE,oo_,'density',nar,density);
116            end
117        end
118    end
119end
120
121function oo_ = initialize_output_structure(var1,var2,nar,type,oo_,options_)
122oo_.([type, 'TheoreticalMoments']).dsge.correlation.Mean.(var1).(var2) = NaN(nar,1);
123oo_.([type, 'TheoreticalMoments']).dsge.correlation.Median.(var1).(var2) = NaN(nar,1);
124oo_.([type, 'TheoreticalMoments']).dsge.correlation.Variance.(var1).(var2) = NaN(nar,1);
125oo_.([type, 'TheoreticalMoments']).dsge.correlation.HPDinf.(var1).(var2) = NaN(nar,1);
126oo_.([type, 'TheoreticalMoments']).dsge.correlation.HPDsup.(var1).(var2) = NaN(nar,1);
127oo_.([type, 'TheoreticalMoments']).dsge.correlation.deciles.(var1).(var2) = cell(nar,1);
128if options_.estimation.moments_posterior_density.indicator
129    oo_.([type, 'TheoreticalMoments']).dsge.correlation.density.(var1).(var2) = cell(nar,1);
130end
131for i=1:nar
132    if options_.estimation.moments_posterior_density.indicator
133        oo_.([type, 'TheoreticalMoments']).dsge.correlation.density.(var1).(var2)(i,1) = {NaN};
134    end
135    oo_.([type, 'TheoreticalMoments']).dsge.correlation.deciles.(var1).(var2)(i,1) = {NaN};
136end
137
138function oo_ = fill_output_structure(var1,var2,type,oo_,moment,lag,result)
139switch moment
140  case {'Mean','Median','Variance','HPDinf','HPDsup'}
141    oo_.([type,  'TheoreticalMoments']).dsge.correlation.(moment).(var1).(var2)(lag,1) = result;
142  case {'deciles','density'}
143    oo_.([type, 'TheoreticalMoments']).dsge.correlation.(moment).(var1).(var2)(lag,1) = {result};
144  otherwise
145    disp('fill_output_structure:: Unknown field!')
146end