2008-06-21 10:33:31 +02:00
|
|
|
function [nvar,vartan,NumberOfDecompFiles] = ...
|
2009-12-16 18:17:34 +01:00
|
|
|
dsge_simulated_theoretical_variance_decomposition(SampleSize,M_,options_,oo_,type)
|
2015-05-09 15:13:59 +02:00
|
|
|
% function [nvar,vartan,NumberOfDecompFiles] = ...
|
|
|
|
% dsge_simulated_theoretical_variance_decomposition(SampleSize,M_,options_,oo_,type)
|
2009-06-11 16:39:52 +02:00
|
|
|
% This function computes the posterior or prior distribution of the variance
|
2007-10-12 19:02:04 +02:00
|
|
|
% decomposition of the observed endogenous variables.
|
2011-09-17 12:33:07 +02:00
|
|
|
%
|
|
|
|
% 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'
|
2007-10-12 19:02:04 +02:00
|
|
|
%
|
2009-06-11 16:39:52 +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 20:53:30 +02:00
|
|
|
|
2021-03-12 14:50:35 +01:00
|
|
|
% Copyright (C) 2007-2021 Dynare Team
|
2008-08-01 20:53:30 +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
|
|
|
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
2008-01-17 16:47:36 +01:00
|
|
|
|
2009-02-10 22:10:11 +01:00
|
|
|
nodecomposition = 0;
|
2007-10-12 19:02:04 +02:00
|
|
|
|
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;
|
2011-09-17 12:33:07 +02:00
|
|
|
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_variance_decomposition:: Unknown type!')
|
|
|
|
error()
|
|
|
|
end
|
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 '_PosteriorVarianceDecomposition*']);
|
2017-09-09 08:42:08 +02:00
|
|
|
delete_stale_file([M_.dname '/metropolis/' M_.fname '_PosteriorVarianceDecompME*']);
|
2015-05-09 15:13:59 +02:00
|
|
|
else
|
2017-09-15 10:07:19 +02:00
|
|
|
delete_stale_file([M_.dname '/prior/moments/' M_.fname '_PriorVarianceDecomposition*']);
|
2017-09-09 08:42:08 +02:00
|
|
|
delete_stale_file([M_.dname '/prior/moments/' M_.fname '_PriorVarianceDecompME*']);end
|
2007-10-12 19:02:04 +02:00
|
|
|
|
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
|
2012-06-06 12:27:46 +02:00
|
|
|
[ivar,vartan,options_] = get_variables_list(options_,M_);
|
2021-01-25 15:56:44 +01:00
|
|
|
endo_names=options_.varlist;
|
2009-06-15 16:36:30 +02:00
|
|
|
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;
|
2011-09-17 12:33:07 +02:00
|
|
|
options_.ar = 0;
|
2009-06-15 16:36:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-10-12 19:02:04 +02:00
|
|
|
nexo = M_.exo_nbr;
|
2008-06-21 10:33:31 +02:00
|
|
|
|
2007-10-12 19:02:04 +02:00
|
|
|
NumberOfSavedElementsPerSimulation = nvar*(nexo+1);
|
|
|
|
MaXNumberOfDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8);
|
|
|
|
|
2017-09-09 08:42:08 +02:00
|
|
|
ME_present=0;
|
2020-06-26 18:24:33 +02:00
|
|
|
if ~all(diag(M_.H)==0)
|
2021-03-12 14:50:35 +01:00
|
|
|
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');
|
2017-09-09 08:42:08 +02:00
|
|
|
if ~isempty(observable_pos_requested_vars)
|
|
|
|
ME_present=1;
|
|
|
|
nobs_ME=length(observable_pos_requested_vars);
|
|
|
|
NumberOfSavedElementsPerSimulation_ME = nobs_ME*(nexo+1);
|
|
|
|
MaXNumberOfDecompLines_ME = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation_ME/8);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-24 19:13:36 +02:00
|
|
|
if SampleSize<=MaXNumberOfDecompLines
|
|
|
|
Decomposition_array = zeros(SampleSize,nvar*nexo);
|
2007-10-12 19:02:04 +02:00
|
|
|
NumberOfDecompFiles = 1;
|
|
|
|
else
|
2008-06-24 19:13:36 +02:00
|
|
|
Decomposition_array = zeros(MaXNumberOfDecompLines,nvar*nexo);
|
|
|
|
NumberOfLinesInTheLastDecompFile = mod(SampleSize,MaXNumberOfDecompLines);
|
|
|
|
NumberOfDecompFiles = ceil(SampleSize/MaXNumberOfDecompLines);
|
2007-10-12 19:02:04 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
NumberOfDecompLines = rows(Decomposition_array);
|
|
|
|
DecompFileNumber = 1;
|
|
|
|
|
2017-09-09 08:42:08 +02:00
|
|
|
if ME_present
|
|
|
|
if SampleSize<=MaXNumberOfDecompLines_ME
|
|
|
|
Decomposition_array_ME = zeros(SampleSize,nobs_ME*(nexo+1));
|
|
|
|
NumberOfDecompFiles_ME = 1;
|
|
|
|
else
|
|
|
|
Decomposition_array_ME = zeros(MaXNumberOfDecompLines_ME,nobs_ME*(nexo+1));
|
|
|
|
NumberOfLinesInTheLastDecompFile_ME = mod(SampleSize,MaXNumberOfDecompLines_ME);
|
|
|
|
NumberOfDecompFiles_ME = ceil(SampleSize/MaXNumberOfDecompLines_ME);
|
|
|
|
end
|
|
|
|
NumberOfDecompLines_ME = rows(Decomposition_array_ME);
|
|
|
|
DecompFileNumber_ME = 1;
|
|
|
|
end
|
2007-10-12 19:02:04 +02:00
|
|
|
% Compute total variances (covariances are not saved) and variances
|
|
|
|
% implied by each structural shock.
|
|
|
|
linea = 0;
|
2017-09-09 08:42:08 +02:00
|
|
|
linea_ME = 0;
|
2014-07-16 16:52:02 +02:00
|
|
|
only_non_stationary_vars=0;
|
2007-10-12 19:02:04 +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
|
|
|
isdrsaved = columns(temp.pdraws)-1;
|
|
|
|
NumberOfDraws = rows(temp.pdraws);
|
2007-10-12 19:02:04 +02:00
|
|
|
for linee = 1:NumberOfDraws
|
|
|
|
linea = linea+1;
|
2017-09-09 08:42:08 +02:00
|
|
|
linea_ME = linea_ME+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});
|
2021-04-01 11:07:28 +02:00
|
|
|
[dr,info,M_,options_,oo_] = compute_decision_rules(M_,options_,oo_);
|
2008-06-17 23:23:02 +02:00
|
|
|
end
|
2014-07-16 16:52:02 +02:00
|
|
|
if file==1 && linee==1
|
|
|
|
[tmp, stationary_vars] = th_autocovariances(dr,ivar,M_,options_,nodecomposition);
|
|
|
|
if isempty(stationary_vars)
|
2017-05-16 15:10:20 +02:00
|
|
|
fprintf('\ndsge_simulated_theoretical_variance_decomposition:: All requested endogenous variables have a unit root and thus infinite variance.\n')
|
|
|
|
fprintf('dsge_simulated_theoretical_variance_decomposition:: No decomposition is performed.\n')
|
|
|
|
only_non_stationary_vars=1;
|
2014-07-16 16:52:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if only_non_stationary_vars
|
2019-12-20 16:28:06 +01:00
|
|
|
Decomposition_array(linea,:) = NaN;
|
2014-07-16 16:52:02 +02:00
|
|
|
else
|
|
|
|
tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition);
|
|
|
|
for i=1:nvar
|
|
|
|
for j=1:nexo
|
|
|
|
Decomposition_array(linea,(i-1)*nexo+j) = tmp{2}(i,j);
|
|
|
|
end
|
2007-10-12 19:02:04 +02:00
|
|
|
end
|
2017-09-09 08:42:08 +02:00
|
|
|
if ME_present
|
2020-06-26 18:24:33 +02:00
|
|
|
M_ = set_measurement_errors(temp.pdraws{linee,1},temp.estim_params_,M_);
|
2017-09-09 08:42:08 +02:00
|
|
|
ME_Variance=diag(M_.H);
|
|
|
|
tmp_ME=NaN(nobs_ME,nexo+1);
|
|
|
|
tmp_ME(:,1:end-1)=tmp{2}(index_subset,:).*repmat(diag(tmp{1}(index_subset,index_subset))./(diag(tmp{1}(index_subset,index_subset))+ME_Variance(index_observables)),1,nexo);
|
|
|
|
tmp_ME(:,end)=1-sum(tmp_ME(:,1:end-1),2);
|
|
|
|
for i=1:nobs_ME
|
|
|
|
for j=1:nexo+1
|
|
|
|
Decomposition_array_ME(linea,(i-1)*(nexo+1)+j) = tmp_ME(i,j);
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-12 19:02:04 +02:00
|
|
|
end
|
|
|
|
if linea == NumberOfDecompLines
|
2009-06-11 16:39:52 +02:00
|
|
|
if posterior
|
2021-01-25 15:56:44 +01:00
|
|
|
save([M_.dname '/metropolis/' M_.fname '_PosteriorVarianceDecomposition' int2str(DecompFileNumber) '.mat' ],'Decomposition_array','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 '_PriorVarianceDecomposition' int2str(DecompFileNumber) '.mat' ],'Decomposition_array','endo_names');
|
2009-06-11 16:39:52 +02:00
|
|
|
end
|
2007-10-12 19:02:04 +02:00
|
|
|
DecompFileNumber = DecompFileNumber + 1;
|
|
|
|
linea = 0;
|
|
|
|
test = DecompFileNumber-NumberOfDecompFiles;
|
2008-06-17 23:23:02 +02:00
|
|
|
if ~test% Prepare the last round...
|
2008-06-24 19:13:36 +02:00
|
|
|
Decomposition_array = zeros(NumberOfLinesInTheLastDecompFile,nvar*nexo);
|
2007-10-12 19:02:04 +02:00
|
|
|
NumberOfDecompLines = NumberOfLinesInTheLastDecompFile;
|
2017-05-16 12:42:01 +02:00
|
|
|
elseif test<0
|
2008-06-24 19:13:36 +02:00
|
|
|
Decomposition_array = zeros(MaXNumberOfDecompLines,nvar*nexo);
|
2007-10-12 19:02:04 +02:00
|
|
|
else
|
|
|
|
clear('Decomposition_array');
|
|
|
|
end
|
|
|
|
end
|
2017-09-09 08:42:08 +02:00
|
|
|
if ME_present
|
|
|
|
if linea_ME == NumberOfDecompLines_ME
|
|
|
|
if posterior
|
2021-01-25 15:56:44 +01:00
|
|
|
save([M_.dname '/metropolis/' M_.fname '_PosteriorVarianceDecompME' int2str(DecompFileNumber_ME) '.mat' ],'Decomposition_array_ME','endo_names');
|
2017-09-09 08:42:08 +02:00
|
|
|
else
|
2021-01-25 15:56:44 +01:00
|
|
|
save([M_.dname '/prior/moments/' M_.fname '_PriorVarianceDecompME' int2str(DecompFileNumber_ME) '.mat' ],'Decomposition_array_ME','endo_names');
|
2017-09-09 08:42:08 +02:00
|
|
|
end
|
|
|
|
DecompFileNumber_ME = DecompFileNumber_ME + 1;
|
|
|
|
linea_ME = 0;
|
|
|
|
test = DecompFileNumber_ME-NumberOfDecompFiles_ME;
|
|
|
|
if ~test% Prepare the last round...
|
|
|
|
Decomposition_array_ME = zeros(NumberOfLinesInTheLastDecompFile_ME,nobs_ME*(nexo+1));
|
|
|
|
NumberOfDecompLines_ME = NumberOfLinesInTheLastDecompFile_ME;
|
|
|
|
elseif test<0
|
|
|
|
Decomposition_array_ME = zeros(MaXNumberOfDecompLines_ME,nobs_ME*(nexo+1));
|
|
|
|
else
|
|
|
|
clear('Decomposition_array_ME');
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-10-12 19:02:04 +02:00
|
|
|
end
|
|
|
|
end
|
2008-06-17 23:23:02 +02:00
|
|
|
|
2017-09-15 10:07:19 +02:00
|
|
|
options_.ar = nar;
|