1function oo_ = variance_decomposition_ME_mc_analysis(NumberOfSimulations,type,dname,fname,exonames,exo,vartan,var,mh_conf_sig,oo_,options_)
2% function oo_ = variance_decomposition_ME_mc_analysis(NumberOfSimulations,type,dname,fname,exonames,exo,vartan,var,mh_conf_sig,oo_)
3% This function analyses the (posterior or prior) distribution of the
4% endogenous variables' variance decomposition.
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%   exonames                [string]            (n_exo*char_length) character array with names of exogenous variables
12%   exo                     [string]            name of current exogenous
13%                                               variable
14%   vartan                  [string]            (n_endo*char_length) character array with name
15%                                               of endogenous variables
16%   var                     [integer]         index of the current
17%                                               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%   options_                [structure]         Dynare options structure
22%
23% OUTPUTS
24%   oo_          [structure]        Dynare structure where the results are saved.
25
26
27
28% Copyright (C) 2017 Dynare Team
29%
30% This file is part of Dynare.
31%
32% Dynare is free software: you can redistribute it and/or modify
33% it under the terms of the GNU General Public License as published by
34% the Free Software Foundation, either version 3 of the License, or
35% (at your option) any later version.
36%
37% Dynare is distributed in the hope that it will be useful,
38% but WITHOUT ANY WARRANTY; without even the implied warranty of
39% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40% GNU General Public License for more details.
41%
42% You should have received a copy of the GNU General Public License
43% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
44
45if strcmpi(type,'posterior')
46    TYPE = 'Posterior';
47    PATH = [dname '/metropolis/'];
48else
49    TYPE = 'Prior';
50    PATH = [dname '/prior/moments/'];
51end
52
53indx = check_name(vartan,var);
54if isempty(indx)
55    disp([ type '_analysis:: ' var ' is not a stationary endogenous variable!'])
56    return
57end
58jndx = check_name(exonames,exo);
59if isempty(jndx)
60    if isequal(exo,'ME')
61        jndx=size(exonames,1)+1;
62    else
63        disp([ type '_analysis:: ' exo ' is not a declared exogenous variable!'])
64        return
65    end
66end
67
68var=deblank(var);
69exo=deblank(exo);
70
71name = [ var '.' exo ];
72if isfield(oo_, [ TYPE 'TheoreticalMoments'])
73    temporary_structure = oo_.([TYPE, 'TheoreticalMoments']);
74    if isfield(temporary_structure,'dsge')
75        temporary_structure = oo_.([TYPE, 'TheoreticalMoments']).dsge;
76        if isfield(temporary_structure,'VarianceDecompositionME')
77            temporary_structure = oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.Mean;
78            if isfield(temporary_structure,name)
79                % Nothing to do.
80                return
81            end
82        end
83    end
84end
85
86ListOfFiles = dir([ PATH  fname '_' TYPE 'VarianceDecompME*.mat']);
87i1 = 1; tmp = zeros(NumberOfSimulations,1);
88indice = (indx-1)*rows(exonames)+jndx;
89for file = 1:length(ListOfFiles)
90    load([ PATH ListOfFiles(file).name ]);
91    i2 = i1 + rows(Decomposition_array_ME) - 1;
92    tmp(i1:i2) = Decomposition_array_ME(:,indice);
93    i1 = i2+1;
94end
95
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
103
104oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.Mean.(var).(exo) = p_mean;
105oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.Median.(var).(exo) = p_median;
106oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.Variance.(var).(exo) = p_var;
107oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.HPDinf.(var).(exo) = hpd_interval(1);
108oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.HPDsup.(var).(exo) = hpd_interval(2);
109oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.deciles.(var).(exo) = p_deciles;
110if options_.estimation.moments_posterior_density.indicator
111    oo_.([TYPE, 'TheoreticalMoments']).dsge.VarianceDecompositionME.density.(var).(exo) = density;
112end