2021-07-22 17:03:27 +02:00
|
|
|
function [nvar,vartan,CovarFileNumber] = dsge_simulated_theoretical_covariance(SampleSize,nar,M_,options_,oo_,type)
|
|
|
|
% function [nvar,vartan,CovarFileNumber] = dsge_simulated_theoretical_covariance(SampleSize,nar,M_,options_,oo_,type)
|
2009-06-11 16:39:52 +02:00
|
|
|
% This function computes the posterior or prior distribution of the endogenous
|
2011-09-17 12:33:07 +02:00
|
|
|
% variables second order moments.
|
|
|
|
%
|
|
|
|
% INPUTS
|
2009-06-11 16:39:52 +02:00
|
|
|
% 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'
|
2008-06-21 10:33:31 +02:00
|
|
|
%
|
|
|
|
%
|
2011-09-17 12:33:07 +02:00
|
|
|
% OUTPUTS
|
2009-06-11 16:39:52 +02:00
|
|
|
% nvar [integer] nvar is the number of stationary variables.
|
|
|
|
% vartan [char] array of characters (with nvar rows).
|
|
|
|
% CovarFileNumber [integer] scalar, number of prior or posterior data files (for covariance).
|
2008-08-01 14:40:33 +02:00
|
|
|
|
2022-04-13 13:15:19 +02:00
|
|
|
% Copyright © 2007-2021 Dynare Team
|
2008-08-01 14:40:33 +02:00
|
|
|
%
|
|
|
|
% 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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2008-01-17 16:45:44 +01:00
|
|
|
|
2009-02-10 22:10:11 +01:00
|
|
|
nodecomposition = 1;
|
|
|
|
|
2008-06-21 10:33:31 +02:00
|
|
|
% Get informations about the _posterior_draws files.
|
2009-06-11 16:39:52 +02:00
|
|
|
if strcmpi(type,'posterior')
|
2020-03-12 15:56:55 +01:00
|
|
|
NumberOfDrawsFiles = length(dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]));
|
2009-06-11 16:39:52 +02:00
|
|
|
posterior = 1;
|
|
|
|
elseif strcmpi(type,'prior')
|
2020-03-12 15:56:55 +01:00
|
|
|
NumberOfDrawsFiles = length(dir([M_.dname '/prior/draws/' type '_draws*' ]));
|
2011-12-15 11:56:23 +01:00
|
|
|
CheckPath('prior/moments',M_.dname);
|
2009-06-11 16:39:52 +02:00
|
|
|
posterior = 0;
|
|
|
|
else
|
|
|
|
disp('dsge_simulated_theoretical_covariance:: Unknown type!')
|
|
|
|
error();
|
|
|
|
end
|
2008-06-21 10:33:31 +02:00
|
|
|
|
2015-05-09 15:13:59 +02:00
|
|
|
%delete old stale files before creating new ones
|
|
|
|
if posterior
|
|
|
|
delete_stale_file([M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments*'])
|
2021-07-22 17:03:27 +02:00
|
|
|
delete_stale_file([M_.dname '/metropolis/' M_.fname '_PosteriorCorrelations*']);
|
2015-05-09 15:13:59 +02:00
|
|
|
else
|
|
|
|
delete_stale_file([M_.dname '/prior/moments/' M_.fname '_Prior2ndOrderMoments*'])
|
2021-07-22 17:03:27 +02:00
|
|
|
delete_stale_file([M_.dname '/prior/moments/' M_.fname '_PriorCorrelations*']);
|
2015-05-09 15:13:59 +02:00
|
|
|
end
|
|
|
|
|
2009-06-15 16:36:30 +02:00
|
|
|
% Set varlist (vartan)
|
|
|
|
if ~posterior
|
|
|
|
if isfield(options_,'varlist')
|
|
|
|
temp = options_.varlist;
|
|
|
|
end
|
|
|
|
options_.varlist = options_.prior_analysis_endo_var_list;
|
|
|
|
end
|
2021-01-25 15:56:44 +01:00
|
|
|
endo_names=options_.varlist;
|
2012-06-06 12:27:46 +02:00
|
|
|
[ivar,vartan] = get_variables_list(options_,M_);
|
2009-06-15 16:36:30 +02:00
|
|
|
if ~posterior
|
|
|
|
if exist('temp','var')
|
|
|
|
options_.varlist = temp;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nvar = length(ivar);
|
|
|
|
|
2021-01-17 18:21:29 +01:00
|
|
|
if options_.pruning
|
|
|
|
obs_var=NaN(nvar,1);
|
|
|
|
for i=1:nvar
|
|
|
|
obs_var(i,1) = find(strcmp(M_.endo_names(ivar(i),:), M_.endo_names(oo_.dr.order_var)));
|
|
|
|
end
|
|
|
|
end
|
2021-07-22 17:03:27 +02:00
|
|
|
|
|
|
|
options_.ar = nar;
|
2009-06-15 16:36:30 +02:00
|
|
|
|
2008-06-21 10:33:31 +02:00
|
|
|
% Number of lines in posterior data files.
|
2007-09-22 00:54:23 +02:00
|
|
|
MaXNumberOfCovarLines = ceil(options_.MaxNumberOfBytes/(nvar*(nvar+1)/2)/8);
|
2021-07-22 17:03:27 +02:00
|
|
|
MaXNumberOfCorrLines = ceil(options_.MaxNumberOfBytes/(nvar*nvar*nar)/8);
|
2007-09-22 00:54:23 +02:00
|
|
|
|
2008-06-21 10:33:31 +02:00
|
|
|
if SampleSize<=MaXNumberOfCovarLines
|
2008-06-24 19:13:36 +02:00
|
|
|
Covariance_matrix = zeros(SampleSize,nvar*(nvar+1)/2);
|
2007-09-22 00:54:23 +02:00
|
|
|
NumberOfCovarFiles = 1;
|
|
|
|
else
|
|
|
|
Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
|
2008-06-21 10:33:31 +02:00
|
|
|
NumberOfLinesInTheLastCovarFile = mod(SampleSize,MaXNumberOfCovarLines);
|
|
|
|
NumberOfCovarFiles = ceil(SampleSize/MaXNumberOfCovarLines);
|
2007-09-22 00:54:23 +02:00
|
|
|
end
|
2021-07-22 17:03:27 +02:00
|
|
|
if SampleSize<=MaXNumberOfCorrLines
|
|
|
|
Correlation_array = zeros(SampleSize,nvar,nvar,nar);
|
|
|
|
NumberOfCorrFiles = 1;
|
|
|
|
else
|
|
|
|
Correlation_array = zeros(MaXNumberOfCorrLines,nvar,nvar,nar);
|
|
|
|
NumberOfLinesInTheLastCorrFile = mod(SampleSize,MaXNumberOfCorrLines);
|
|
|
|
NumberOfCorrFiles = ceil(SampleSize/MaXNumberOfCorrLines);
|
|
|
|
end
|
2007-09-22 00:54:23 +02:00
|
|
|
|
|
|
|
NumberOfCovarLines = rows(Covariance_matrix);
|
|
|
|
CovarFileNumber = 1;
|
2021-07-22 17:03:27 +02:00
|
|
|
NumberOfCorrLines = rows(Correlation_array);
|
|
|
|
CorrFileNumber = 1;
|
2007-09-22 00:54:23 +02:00
|
|
|
|
2009-06-11 16:39:52 +02:00
|
|
|
% Compute 2nd order moments and save them in *_[Posterior, Prior]2ndOrderMoments* files
|
2021-07-22 17:03:27 +02:00
|
|
|
linea_cov = 0;
|
|
|
|
linea_corr = 0;
|
2007-09-22 00:54:23 +02:00
|
|
|
for file = 1:NumberOfDrawsFiles
|
2009-06-11 16:39:52 +02:00
|
|
|
if posterior
|
2020-06-26 18:24:33 +02:00
|
|
|
temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
|
2009-06-11 16:39:52 +02:00
|
|
|
else
|
2020-06-26 18:24:33 +02:00
|
|
|
temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
|
2009-06-11 16:39:52 +02:00
|
|
|
end
|
2020-06-26 18:24:33 +02:00
|
|
|
NumberOfDraws = rows(temp.pdraws);
|
|
|
|
isdrsaved = columns(temp.pdraws)-1;
|
2007-09-22 00:54:23 +02:00
|
|
|
for linee = 1:NumberOfDraws
|
2021-07-22 17:03:27 +02:00
|
|
|
linea_cov = linea_cov+1;
|
|
|
|
linea_corr = linea_corr+1;
|
2008-06-17 23:23:02 +02:00
|
|
|
if isdrsaved
|
2020-06-26 18:24:33 +02:00
|
|
|
M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
|
|
|
|
dr = temp.pdraws{linee,2};
|
2008-06-17 23:23:02 +02:00
|
|
|
else
|
2020-06-26 18:24:33 +02:00
|
|
|
M_=set_parameters_locally(M_,temp.pdraws{linee,1});
|
2023-09-15 13:40:10 +02:00
|
|
|
[dr,info,M_.params] = compute_decision_rules(M_,options_,oo_.dr, oo_.steady_state, oo_.exo_steady_state, oo_.exo_det_steady_state);
|
2008-06-17 23:23:02 +02:00
|
|
|
end
|
2021-01-17 18:21:29 +01:00
|
|
|
if ~options_.pruning
|
|
|
|
tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition);
|
|
|
|
else
|
|
|
|
pruned_state_space = pruned_state_space_system(M_, options_, dr, obs_var, options_.ar, 1, 0);
|
|
|
|
tmp{1} = pruned_state_space.Var_y;
|
2021-07-22 17:03:27 +02:00
|
|
|
for i=1:nar
|
|
|
|
tmp{i+1} = pruned_state_space.Corr_yi(:,:,i);
|
|
|
|
end
|
2021-01-17 18:21:29 +01:00
|
|
|
end
|
2007-09-22 00:54:23 +02:00
|
|
|
for i=1:nvar
|
|
|
|
for j=i:nvar
|
2021-07-22 17:03:27 +02:00
|
|
|
Covariance_matrix(linea_cov,symmetric_matrix_index(i,j,nvar)) = tmp{1}(i,j);
|
2007-09-22 00:54:23 +02:00
|
|
|
end
|
|
|
|
end
|
2021-07-22 17:03:27 +02:00
|
|
|
for i=1:nar
|
|
|
|
Correlation_array(linea_corr,:,:,i) = tmp{i+1};
|
|
|
|
end
|
|
|
|
|
|
|
|
if linea_cov == NumberOfCovarLines
|
2009-06-11 16:39:52 +02:00
|
|
|
if posterior
|
2021-01-25 15:56:44 +01:00
|
|
|
save([ M_.dname '/metropolis/' M_.fname '_Posterior2ndOrderMoments' int2str(CovarFileNumber) '.mat' ],'Covariance_matrix','endo_names');
|
2009-06-11 16:39:52 +02:00
|
|
|
else
|
2021-01-25 15:56:44 +01:00
|
|
|
save([ M_.dname '/prior/moments/' M_.fname '_Prior2ndOrderMoments' int2str(CovarFileNumber) '.mat' ],'Covariance_matrix','endo_names');
|
2009-06-11 16:39:52 +02:00
|
|
|
end
|
2007-09-22 00:54:23 +02:00
|
|
|
CovarFileNumber = CovarFileNumber + 1;
|
2021-07-22 17:03:27 +02:00
|
|
|
linea_cov = 0;
|
2007-09-22 00:54:23 +02:00
|
|
|
test = CovarFileNumber-NumberOfCovarFiles;
|
2008-06-17 23:23:02 +02:00
|
|
|
if ~test% Prepare the last round...
|
2007-09-22 00:54:23 +02:00
|
|
|
Covariance_matrix = zeros(NumberOfLinesInTheLastCovarFile,nvar*(nvar+1)/2);
|
|
|
|
NumberOfCovarLines = NumberOfLinesInTheLastCovarFile;
|
2009-02-10 22:10:11 +01:00
|
|
|
elseif test<0
|
2007-09-22 00:54:23 +02:00
|
|
|
Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
|
|
|
|
else
|
2008-06-17 23:23:02 +02:00
|
|
|
clear('Covariance_matrix');
|
2007-09-22 00:54:23 +02:00
|
|
|
end
|
|
|
|
end
|
2021-07-22 17:03:27 +02:00
|
|
|
if linea_corr == NumberOfCorrLines
|
|
|
|
if posterior
|
|
|
|
save([ M_.dname '/metropolis/' M_.fname '_PosteriorCorrelations' int2str(CorrFileNumber) '.mat' ],'Correlation_array','endo_names');
|
|
|
|
else
|
|
|
|
save([ M_.dname '/prior/moments/' M_.fname '_PriorCorrelations' int2str(CorrFileNumber) '.mat' ],'Correlation_array','endo_names');
|
|
|
|
end
|
|
|
|
CorrFileNumber = CorrFileNumber + 1;
|
|
|
|
linea_corr = 0;
|
|
|
|
test = CorrFileNumber-NumberOfCorrFiles;
|
|
|
|
if ~test% Prepare the last round...
|
|
|
|
Correlation_array = zeros(NumberOfLinesInTheLastCorrFile,nvar,nvar,nar);
|
|
|
|
NumberOfCorrLines = NumberOfLinesInTheLastCorrFile;
|
|
|
|
CorrFileNumber = CorrFileNumber - 1;
|
|
|
|
elseif test<0
|
|
|
|
Correlation_array = zeros(MaXNumberOfCorrLines,nvar,nvar,nar);
|
|
|
|
else
|
|
|
|
clear('Correlation_array');
|
|
|
|
end
|
|
|
|
end
|
2007-09-22 00:54:23 +02:00
|
|
|
end
|
|
|
|
end
|