1function oo_ = ...
2    conditional_variance_decomposition_ME_mc_analysis(NumberOfSimulations, type, dname, fname, Steps, exonames, exo, var_list, endo, mh_conf_sig, oo_,options_)
3% This function analyses the (posterior or prior) distribution of the
4% endogenous variables' conditional variance decomposition with measurement error.
5%
6% INPUTS
7%   NumberOfSimulations     [integer]           scalar, number of simulations.
8%   type                    [string]            'prior' or 'posterior'
9%   dname                   [string]            directory name where to save
10%   fname                   [string]            name of the mod-file
11%   Steps                   [integers]          horizons at which to conduct decomposition
12%   exonames                [string]            (n_exo*char_length) character array with names of exogenous variables
13%   exo                     [string]            name of current exogenous
14%                                               variable
15%   var_list                [string]            (n_endo*char_length) character array with name
16%                                               of endogenous variables
17%   endo                    [integer]           Current endogenous variable
18%   mh_conf_sig             [double]            2 by 1 vector with upper
19%                                               and lower bound of HPD intervals
20%   oo_                     [structure]         Dynare structure where the results are saved.
21%
22% OUTPUTS
23%   oo_          [structure]        Dynare structure where the results are saved.
24
25% Copyright (C) 2017-2018 Dynare Team
26%
27% This file is part of Dynare.
28%
29% Dynare is free software: you can redistribute it and/or modify
30% it under the terms of the GNU General Public License as published by
31% the Free Software Foundation, either version 3 of the License, or
32% (at your option) any later version.
33%
34% Dynare is distributed in the hope that it will be useful,
35% but WITHOUT ANY WARRANTY; without even the implied warranty of
36% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37% GNU General Public License for more details.
38%
39% You should have received a copy of the GNU General Public License
40% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
41
42if strcmpi(type,'posterior')
43    TYPE = 'Posterior';
44    PATH = [dname '/metropolis/'];
45else
46    TYPE = 'Prior';
47    PATH = [dname '/prior/moments/'];
48end
49
50endogenous_variable_index = check_name(var_list, endo);
51if isempty(endogenous_variable_index)
52    disp([ type '_analysis:: Can''t find ' endo '!'])
53    return
54end
55
56exogenous_variable_index = check_name(exonames,exo);
57if isempty(exogenous_variable_index)
58    if isequal(exo,'ME')
59        exogenous_variable_index=length(exonames)+1;
60    else
61        disp([ type '_analysis:: ' exo ' is not a declared exogenous variable!'])
62        return
63    end
64end
65
66if (isoctave && octave_ver_less_than('6')) || (~isoctave && matlab_ver_less_than('8.1'))
67    [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(var_list,options_.varobs);
68else
69    [observable_pos_requested_vars,index_subset,index_observables]=intersect(var_list,options_.varobs,'stable');
70end
71matrix_pos=strmatch(endo, var_list(index_subset),'exact');
72name_1 = endo;
73name_2 = exo;
74name = [ name_1 '.' name_2 ];
75
76if isfield(oo_, [ TYPE 'TheoreticalMoments' ])
77    temporary_structure = oo_.([TYPE 'TheoreticalMoments']);
78    if isfield(temporary_structure,'dsge')
79        temporary_structure = oo_.([TYPE 'TheoreticalMoments']).dsge;
80        if isfield(temporary_structure,'ConditionalVarianceDecompositionME')
81            temporary_structure = oo_.([TYPE 'TheoreticalMoments']).dsge.ConditionalVarianceDecompositionME.Mean;
82            if isfield(temporary_structure,name)
83                if sum(Steps-temporary_structure.(name)(1,:)) == 0
84                    % Nothing (new) to do here...
85                    return
86                end
87            end
88        end
89    end
90end
91
92ListOfFiles = dir([ PATH  fname '_' TYPE 'ConditionalVarianceDecompME*.mat']);
93i1 = 1; tmp = zeros(NumberOfSimulations,length(Steps));
94for file = 1:length(ListOfFiles)
95    load([ PATH ListOfFiles(file).name ]);
96    % 4D-array (endovar,time,exovar,simul)
97    i2 = i1 + size(Conditional_decomposition_array_ME,4) - 1;
98    tmp(i1:i2,:) = transpose(dynare_squeeze(Conditional_decomposition_array_ME(matrix_pos,:,exogenous_variable_index,:)));
99    i1 = i2+1;
100end
101
102p_mean = NaN(1,length(Steps));
103p_median = NaN(1,length(Steps));
104p_variance = NaN(1,length(Steps));
105p_deciles = NaN(9,length(Steps));
106if options_.estimation.moments_posterior_density.indicator
107    p_density = NaN(2^9,2,length(Steps));
108end
109p_hpdinf = NaN(1,length(Steps));
110p_hpdsup = NaN(1,length(Steps));
111for i=1:length(Steps)
112    if options_.estimation.moments_posterior_density.indicator
113        [pp_mean, pp_median, pp_var, hpd_interval, pp_deciles, pp_density] = ...
114            posterior_moments(tmp(:,i),1,mh_conf_sig);
115        p_density(:,:,i) = pp_density;
116    else
117        [pp_mean, pp_median, pp_var, hpd_interval, pp_deciles] = ...
118            posterior_moments(tmp(:,i),0,mh_conf_sig);
119    end
120    p_mean(i) = pp_mean;
121    p_median(i) = pp_median;
122    p_variance(i) = pp_var;
123    p_deciles(:,i) = pp_deciles;
124    p_hpdinf(i) = hpd_interval(1);
125    p_hpdsup(i) = hpd_interval(2);
126end
127
128FirstField = sprintf('%sTheoreticalMoments', TYPE);
129
130oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Steps = Steps;
131oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Mean.(name_1).(name_2) = p_mean;
132oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Median.(name_1).(name_2) = p_median;
133oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Variance.(name_1).(name_2) = p_variance;
134oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.HPDinf.(name_1).(name_2) = p_hpdinf;
135oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.HPDsup.(name_1).(name_2) = p_hpdsup;
136oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.deciles.(name_1).(name_2)  = p_deciles;
137if options_.estimation.moments_posterior_density.indicator
138    oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.density.(name_1).(name_2) = p_density;
139end
140