dsge_simulated_theoretical_conditional_variance_decomposition.m 6.4 KB
Newer Older
stepan's avatar
 
stepan committed
1
function [nvar,vartan,NumberOfConditionalDecompFiles] = ...
2
3
        dsge_simulated_theoretical_conditional_variance_decomposition(SampleSize,Steps,M_,options_,oo_,type)
% This function computes the posterior or prior distribution of the conditional variance
stepan's avatar
 
stepan committed
4
5
6
% decomposition of the endogenous variables (or a subset of the endogenous variables).
% 
% INPUTS 
7
8
9
10
11
12
13
14
15
16
17
%   SampleSize   [integer]       scalar, number of simulations.
%   M_           [structure]     Dynare structure describing the model.
%   options_     [structure]     Dynare structure defining global options.
%   oo_          [structure]     Dynare structure where the results are saved.
%   type         [string]        'prior' or 'posterior'
%
%
% OUTPUTS  
%   nvar                             [integer]  nvar is the number of stationary variables.
%   vartan                           [char]     array of characters (with nvar rows).
%   NumberOfConditionalDecompFiles   [integer]  scalar, number of prior or posterior data files (for covariance).
stepan's avatar
 
stepan committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

% Copyright (C) 2009 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.


% Get informations about the _posterior_draws files.
38
39
40
41
42
if strcmpi(type,'posterior')
    DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]);
    posterior = 1;
elseif strcmpi(type,'prior')
    DrawsFiles = dir([M_.dname '/prior/draws/' type '_draws*' ]);
stepan's avatar
   
stepan committed
43
    CheckPath('prior/moments');
44
45
46
47
48
49
    posterior = 0;
else
    disp('dsge_simulated_theoretical_conditional_variance_decomposition:: Unknown type!')
    error()
end

stepan's avatar
   
stepan committed
50
51
52
53
54
55
56
% Set varlist (vartan)
if ~posterior
    if isfield(options_,'varlist')
        temp = options_.varlist;
    end
    options_.varlist = options_.prior_analysis_endo_var_list;
end
stepan's avatar
   
stepan committed
57
[ivar,vartan ] = set_stationary_variables_list(options_, M_);
stepan's avatar
   
stepan committed
58
59
60
61
62
63
64
65
66
67
68
if ~posterior
    if exist('temp','var')
        options_.varlist = temp;
    end
end
nvar = length(ivar);

% Set the size of the auto-correlation function to zero.
nar = options_.ar;
options_.ar = 0;

stepan's avatar
 
stepan committed
69
70
71
72
73
74
75
76
77
78
NumberOfDrawsFiles = rows(DrawsFiles);
NumberOfSavedElementsPerSimulation = nvar*(nvar+1)/2*M_.exo_nbr*length(Steps);
MaXNumberOfConditionalDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8);

if SampleSize<=MaXNumberOfConditionalDecompLines
    Conditional_decomposition_array = zeros(nvar*(nvar+1)/2,length(Steps),M_.exo_nbr,SampleSize);
    NumberOfConditionalDecompFiles = 1;
else
    Conditional_decomposition_array = zeros(nvar*(nvar+1)/2,length(Steps),M_.exo_nbr,MaXNumberOfConditionalDecompLines);
    NumberOfLinesInTheLastConditionalDecompFile = mod(SampleSize,MaXNumberOfConditionalDecompLines);
stepan's avatar
   
stepan committed
79
    NumberOfConditionalDecompFiles = ceil(SampleSize/MaXNumberOfConditionalDecompLines);
stepan's avatar
 
stepan committed
80
81
end

stepan's avatar
   
stepan committed
82
83
NumberOfConditionalDecompLines = size(Conditional_decomposition_array,4);
ConditionalDecompFileNumber = 0;
stepan's avatar
 
stepan committed
84
85
86
87

StateSpaceModel.number_of_state_equations = M_.endo_nbr;
StateSpaceModel.number_of_state_innovations = M_.exo_nbr;

stepan's avatar
   
stepan committed
88
first_call = 1;
stepan's avatar
 
stepan committed
89
90
91

linea = 0;
for file = 1:NumberOfDrawsFiles
92
93
94
95
96
    if posterior
        load([M_.dname '/metropolis/' DrawsFiles(file).name ]);
    else
        load([M_.dname '/prior/draws/' DrawsFiles(file).name ]);
    end
stepan's avatar
 
stepan committed
97
98
99
100
101
102
103
104
105
106
107
    isdrsaved = columns(pdraws)-1;
    NumberOfDraws = rows(pdraws);
    for linee = 1:NumberOfDraws
        linea = linea+1;
        if isdrsaved
            set_parameters(pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
            dr = pdraws{linee,2};
        else
            set_parameters(pdraws{linee,1});
            [dr,info] = resol(oo_.steady_state,0);
        end
stepan's avatar
   
stepan committed
108
109
110
111
112
113
114
115
116
117
118
119
        if first_call
            endo_nbr = M_.endo_nbr;
            nstatic = dr.nstatic;
            npred = dr.npred;
            iv = (1:endo_nbr)';
            ic = [ nstatic+(1:npred) endo_nbr+(1:size(dr.ghx,2)-npred) ]';
            aux = dr.transition_auxiliary_variables;
            k = find(aux(:,2) > npred);
            aux(:,2) = aux(:,2) + nstatic;
            aux(k,2) = aux(k,2) + dr.nfwrd;
            StateSpaceModel.number_of_state_equations = M_.endo_nbr+rows(aux);
            StateSpaceModel.number_of_state_innovations = M_.exo_nbr;
120
            StateSpaceModel.sigma_e_is_diagonal = M_.sigma_e_is_diagonal;
stepan's avatar
   
stepan committed
121
122
123
            first_call = 0;
            clear('endo_nbr','nstatic','npred','k');
        end
stepan's avatar
 
stepan committed
124
125
126
        [StateSpaceModel.transition_matrix,StateSpaceModel.impulse_matrix] = kalman_transition_matrix(dr,iv,ic,aux,M_.exo_nbr);
        StateSpaceModel.state_innovations_covariance_matrix = M_.Sigma_e;
        clear('dr');
stepan's avatar
   
stepan committed
127
        Conditional_decomposition_array(:,:,:,linea) = conditional_variance_decomposition(StateSpaceModel, Steps, ivar);
stepan's avatar
 
stepan committed
128
        if linea == NumberOfConditionalDecompLines
stepan's avatar
   
stepan committed
129
130
            ConditionalDecompFileNumber = ConditionalDecompFileNumber + 1;
            linea = 0;
131
132
133
134
135
136
137
            if posterior
                save([M_.dname '/metropolis/' M_.fname '_PosteriorConditionalVarianceDecomposition' int2str(ConditionalDecompFileNumber) '.mat' ], ...
                     'Conditional_decomposition_array');
            else
                save([M_.dname '/prior/moments/' M_.fname '_PriorConditionalVarianceDecomposition' int2str(ConditionalDecompFileNumber) '.mat' ], ...
                     'Conditional_decomposition_array');
            end
stepan's avatar
   
stepan committed
138
139
            if (ConditionalDecompFileNumber==NumberOfConditionalDecompFiles-1)% Prepare last round.
                Conditional_decomposition_array = zeros(nvar*(nvar+1)/2, length(Steps),M_.exo_nbr,NumberOfLinesInTheLastConditionalDecompFile) ;
stepan's avatar
 
stepan committed
140
                NumberOfConditionalDecompLines = NumberOfLinesInTheLastConditionalDecompFile;
stepan's avatar
   
stepan committed
141
            elseif ConditionalDecompFileNumber<NumberOfConditionalDecompFiles-1
stepan's avatar
 
stepan committed
142
143
144
145
146
147
148
149
150
                Conditional_decomposition_array = zeros(nvar*(nvar+1)/2,length(Steps),M_.exo_nbr,MaXNumberOfConditionalDecompLines);
            else
                clear('Conditional_decomposition_array');
            end
        end
    end
end

options_.ar = nar;