Added Posterior distribution of the conditional variance
decomposition (more tests are needed). The results are saved in oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition. Contrary to the asymptotic variance decomposition, we do not report contribution shares but contribution levels of each structural shock. LIMITATIONS: * Won't work in a model with measurement errors. * Won't work in a model with correlated shocks. * The routines do not compute the covariance decompositions. git-svn-id: https://www.dynare.org/svn/dynare/trunk@2719 ac1d8469-bf42-47a9-8791-bf33cf982152time-shift
parent
19b704d54b
commit
dedca98dba
|
@ -64,8 +64,8 @@ function [info,description] = check_posterior_analysis_data(type,M_)
|
|||
generic_post_data_file_name = 'PosteriorVarianceDecomposition';
|
||||
case 'correlation'
|
||||
generic_post_data_file_name = 'PosteriorCorrelations';
|
||||
case 'dynamic_decomposition'
|
||||
generic_post_data_file_name = 'PosteriorDynamicVarianceDecomposition';
|
||||
case 'conditional decomposition'
|
||||
generic_post_data_file_name = 'PosteriorConditionalVarianceDecomposition';
|
||||
otherwise
|
||||
disp('This feature is not yest implemented!')
|
||||
end
|
||||
|
|
|
@ -35,6 +35,7 @@ function oo_ = compute_moments_varendo(options_,M_,oo_,var_list_)
|
|||
NumberOfExogenousVariables = M_.exo_nbr;
|
||||
list_of_exogenous_variables = M_.exo_names;
|
||||
NumberOfLags = options_.ar;
|
||||
Steps = options_.conditional_variance_decomposition_dates;
|
||||
% COVARIANCE MATRIX.
|
||||
for i=1:NumberOfEndogenousVariables
|
||||
for j=i:NumberOfEndogenousVariables
|
||||
|
@ -54,4 +55,10 @@ function oo_ = compute_moments_varendo(options_,M_,oo_,var_list_)
|
|||
for j=1:NumberOfExogenousVariables
|
||||
oo_ = posterior_analysis('decomposition',var_list_(i,:),M_.exo_names(j,:),[],options_,M_,oo_);
|
||||
end
|
||||
end
|
||||
% CONDITIONAL VARIANCE DECOMPOSITION.
|
||||
for i=1:NumberOfEndogenousVariables
|
||||
for j=1:NumberOfExogenousVariables
|
||||
oo_ = posterior_analysis('conditional decomposition',var_list_(i,:),M_.exo_names(j,:),Steps,options_,M_,oo_);
|
||||
end
|
||||
end
|
|
@ -0,0 +1,54 @@
|
|||
function PackedConditionalVarianceDecomposition = conditional_variance_decomposition(StateSpaceModel, Steps, SubsetOfVariables)
|
||||
% This function computes the conditional variance decomposition of a given state space model
|
||||
% for a subset of endogenous variables.
|
||||
%
|
||||
% INPUTS
|
||||
% StateSpaceModel [structure] Specification of the state space model.
|
||||
% Steps [integer] 1*h vector of dates.
|
||||
% SubsetOfVariables [integer] 1*q vector of indices.
|
||||
%
|
||||
% OUTPUTS
|
||||
% PackedConditionalVarianceDecomposition [double] n(n+1)/2*p matrix, where p is the number of state innovations and
|
||||
% n is equal to length(SubsetOfVariables).
|
||||
%
|
||||
% SPECIAL REQUIREMENTS
|
||||
%
|
||||
% [1] The covariance matrix of the state innovations needs to be diagonal.
|
||||
% [2] In this version, absence of measurement errors is assumed...
|
||||
|
||||
% 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/>.
|
||||
ConditionalVariance = zeros(StateSpaceModel.number_of_state_equations,StateSpaceModel.number_of_state_equations);
|
||||
ConditionalVariance = repmat(ConditionalVariance,[1 1 length(Steps) StateSpaceModel.number_of_state_innovations]);
|
||||
BB = StateSpaceModel.impulse_matrix*transpose(StateSpaceModel.impulse_matrix);
|
||||
for h = 1:length(Steps)
|
||||
for t = 0:Steps(h)
|
||||
for i=1:StateSpaceModel.number_of_state_innovations
|
||||
ConditionalVariance(:,:,h,i) = ...
|
||||
StateSpaceModel.transition_matrix*ConditionalVariance(:,:,h,i)*transpose(StateSpaceModel.transition_matrix) ...
|
||||
+BB*StateSpaceModel.state_innovations_covariance_matrix(i,i);
|
||||
end
|
||||
end
|
||||
end
|
||||
ConditionalVariance = ConditionalVariance(SubsetOfVariables,SubsetOfVariables,:,:);
|
||||
NumberOfVariables = length(SubsetOfVariables);
|
||||
PackedConditionalVarianceDecomposition = zeros(NumberOfVariables*(NumberOfVariables+1)/2,length(Steps),StateSpaceModel.number_of_state_innovations);
|
||||
for i=1:StateSpaceModel.number_of_state_innovations
|
||||
for h = 1:length(Steps)
|
||||
PackedConditionalVarianceDecomposition(:,h,i) = vech(ConditionalVariance(:,:,h,i));
|
||||
end
|
||||
end
|
|
@ -0,0 +1,83 @@
|
|||
function oo_ = conditional_variance_decomposition_posterior_analysis(NumberOfSimulations, dname, fname, ...
|
||||
Steps, exonames, exo, vartan, var, mh_conf_sig, oo_)
|
||||
% 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/>.
|
||||
|
||||
indx = check_name(vartan,var);
|
||||
if isempty(indx)
|
||||
disp(['posterior_analysis:: ' var ' is not a stationary endogenous variable!'])
|
||||
return
|
||||
end
|
||||
endogenous_variable_index = sum(1:indx);
|
||||
exogenous_variable_index = check_name(exonames,exo);
|
||||
if isempty(exogenous_variable_index)
|
||||
disp(['posterior_analysis:: ' exo ' is not a declared exogenous variable!'])
|
||||
return
|
||||
end
|
||||
|
||||
tmp = dir([ dname '/metropolis/' fname '_PosteriorConditionalVarianceDecomposition*.mat']);
|
||||
NumberOfFiles = length(tmp);
|
||||
i1 = 1; tmp = zeros(NumberOfSimulations,length(Steps));
|
||||
|
||||
for file = 1:NumberOfFiles
|
||||
load([dname '/metropolis/' fname '_PosteriorConditionalVarianceDecomposition' int2str(file) '.mat']);
|
||||
% (endovar,time,exovar,simul)
|
||||
i2 = i1 + size(Conditional_decomposition_array,4) - 1;
|
||||
tmp(i1:i2,:) = transpose(dynare_squeeze(Conditional_decomposition_array(endogenous_variable_index,:,exogenous_variable_index,:)));
|
||||
i1 = i2+1;
|
||||
end
|
||||
name = [ var '.' exo ];
|
||||
if isfield(oo_,'PosteriorTheoreticalMoments')
|
||||
if isfield(oo_.PosteriorTheoreticalMoments,'dsge')
|
||||
if isfield(oo_.PosteriorTheoreticalMoments.dsge,'ConditionalVarianceDecomposition')
|
||||
if isfield(oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.mean,name)
|
||||
if sum(Steps-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.mean.(name)(1,:)) == 0
|
||||
% Nothing (new) to do here...
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
posterior_mean = NaN(2,length(Steps));
|
||||
posterior_mean(1,:) = Steps;
|
||||
posterior_median = NaN(1,length(Steps));
|
||||
posterior_variance = NaN(1,length(Steps));
|
||||
posterior_deciles = NaN(9,length(Steps));
|
||||
posterior_density = NaN(2^9,2,length(Steps));
|
||||
posterior_hpdinf = NaN(1,length(Steps));
|
||||
posterior_hpdsup = NaN(1,length(Steps));
|
||||
for i=1:length(Steps)
|
||||
if ~isconst(tmp(:,i))
|
||||
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
|
||||
posterior_moments(tmp(:,i),1,mh_conf_sig);
|
||||
posterior_mean(2,i) = post_mean;
|
||||
posterior_median(i) = post_median;
|
||||
posterior_variance(i) = post_var;
|
||||
posterior_deciles(:,i) = post_deciles;
|
||||
posterior_hpdinf(i) = hpd_interval(1);
|
||||
posterior_hpdinf(i) = hpd_interval(2);
|
||||
posterior_density(:,:,i) = density;
|
||||
end
|
||||
end
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.mean.' name ' = posterior_mean;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.median.' name ' = posterior_median;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.variance.' name ' = posterior_variance;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.hpdinf.' name ' = posterior_hpdinf;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.hpdsup.' name ' = posterior_hpdsup;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.deciles.' name ' = posterior_deciles;']);
|
||||
eval(['oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.density.' name ' = posterior_density;']);
|
|
@ -0,0 +1,112 @@
|
|||
function [nvar,vartan,NumberOfConditionalDecompFiles] = ...
|
||||
dsge_posterior_theoretical_conditional_variance_decomposition(SampleSize,Steps,M_,options_,oo_)
|
||||
% This function estimates the posterior distribution of the conditional variance
|
||||
% decomposition of the endogenous variables (or a subset of the endogenous variables).
|
||||
%
|
||||
% INPUTS
|
||||
% SampleSize [integer] scalar, number of draws in the posterior distribution.
|
||||
% Steps [integer] 1*h vector of dates.
|
||||
%
|
||||
% OUTPUTS
|
||||
% nvar [integer] scalar, number of endogenous variables.
|
||||
% vartan [string] array, list of endogenous variables.
|
||||
% NumberOfConditionalDecompFiles [integer] scalar.
|
||||
|
||||
% 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/>.
|
||||
|
||||
type = 'posterior';% To be defined as a input argument later...
|
||||
|
||||
% Set varlist (vartan)
|
||||
[ivar,vartan] = set_stationary_variables_list;
|
||||
nvar = length(ivar);
|
||||
|
||||
% Set the size of the auto-correlation function to zero.
|
||||
nar = options_.ar;
|
||||
options_.ar = 0;
|
||||
|
||||
% Get informations about the _posterior_draws files.
|
||||
DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]);
|
||||
NumberOfDrawsFiles = length(DrawsFiles);
|
||||
|
||||
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);
|
||||
NumberOfConditionalDecompFiles = ceil(SampleSize/MaXNumberOfCOnditionalDecompLines);
|
||||
end
|
||||
|
||||
NumberOfConditionalDecompLines = rows(Conditional_decomposition_array);
|
||||
ConditionalDecompFileNumber = 1;
|
||||
|
||||
StateSpaceModel.number_of_state_equations = M_.endo_nbr;
|
||||
StateSpaceModel.number_of_state_innovations = M_.exo_nbr;
|
||||
|
||||
endo_nbr = M_.endo_nbr;
|
||||
nstatic = oo_.dr.nstatic;
|
||||
npred = oo_.dr.npred;
|
||||
iv = (1:endo_nbr)';
|
||||
ic = [ nstatic+(1:npred) endo_nbr+(1:size(oo_.dr.ghx,2)-npred) ]';
|
||||
aux = oo_.dr.transition_auxiliary_variables;
|
||||
k = find(aux(:,2) > npred);
|
||||
aux(:,2) = aux(:,2) + nstatic;
|
||||
aux(k,2) = aux(k,2) + oo_.dr.nfwrd;
|
||||
|
||||
linea = 0;
|
||||
for file = 1:NumberOfDrawsFiles
|
||||
load([M_.dname '/metropolis/' DrawsFiles(file).name ]);
|
||||
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
|
||||
[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');
|
||||
Conditional_decomposition_array(:,:,:,linea) = conditional_variance_decomposition(StateSpaceModel, Steps, ivar);
|
||||
if linea == NumberOfConditionalDecompLines
|
||||
save([M_.dname '/metropolis/' M_.fname '_PosteriorConditionalVarianceDecomposition' int2str(ConditionalDecompFileNumber) '.mat' ], ...
|
||||
'Conditional_decomposition_array');
|
||||
ConditionalDecompFileNumber = ConditionalDecompFileNumber + 1;
|
||||
linea = 0;
|
||||
test = ConditionalDecompFileNumber-NumberOfConditionalDecompFiles;
|
||||
if ~test% Prepare the last round...
|
||||
Conditional_decomposition_array = zeros(nvar*(nvar+1)/2,length(Steps),M_.exo_nbr,NumberOfLinesInTheLastConditionalDecompFile);
|
||||
NumberOfConditionalDecompLines = NumberOfLinesInTheLastConditionalDecompFile;
|
||||
ConditionalDecompFileNumber = ConditionalDecompFileNumber - 1;
|
||||
elseif test<0;
|
||||
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;
|
|
@ -183,10 +183,17 @@ function global_initialization()
|
|||
options_.student_degrees_of_freedom = 3;
|
||||
options_.trace_plot_ma = 200;
|
||||
options_.mh_autocorrelation_function_size = 30;
|
||||
options_.plot_priors = 0;
|
||||
options_.plot_priors = 1;
|
||||
options_.cova_compute = 1;
|
||||
options_.parallel = 0;
|
||||
|
||||
options_.number_of_grid_points_for_kde = 2^9;
|
||||
quarter = 1;
|
||||
years = [1 2 3 4 5 10 20 30 40 50];
|
||||
options_.conditional_variance_decomposition_dates = zeros(1,length(years));
|
||||
for i=1:length(years)
|
||||
options_.conditional_variance_decomposition_dates(i) = ...
|
||||
(years(i)-1)*4+quarter;
|
||||
end
|
||||
% Misc
|
||||
options_.conf_sig = 0.6;
|
||||
oo_.exo_simul = [];
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
function oo_ = posterior_analysis(type,arg1,arg2,arg3,options_,M_,oo_)
|
||||
|
||||
% Copyright (C) 2008 Dynare Team
|
||||
%
|
||||
% This file is part of Dynare.
|
||||
|
@ -46,7 +45,7 @@ function oo_ = posterior_analysis(type,arg1,arg2,arg3,options_,M_,oo_)
|
|||
function oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_,nvar,vartan)
|
||||
narg1 = 8;
|
||||
narg2 = 10;
|
||||
if ~(nargin==narg1 | nargin==narg2)
|
||||
if ~(nargin==narg1 || nargin==narg2)
|
||||
error('posterior_analysis:: Call to function job is buggy!')
|
||||
end
|
||||
switch type
|
||||
|
@ -70,7 +69,14 @@ function oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_,nvar,vartan)
|
|||
dsge_posterior_theoretical_correlation(SampleSize,arg3,M_,options_,oo_);
|
||||
end
|
||||
oo_ = correlation_posterior_analysis(SampleSize,M_.dname,M_.fname,...
|
||||
vartan,nvar,arg1,arg2,arg3,options_.mh_conf_sig,oo_,M_,options_);
|
||||
vartan,nvar,arg1,arg2,arg3,options_.mh_conf_sig,oo_,M_,options_);
|
||||
case 'conditional decomposition'
|
||||
if nargin==narg1
|
||||
[nvar,vartan,NumberOfFiles] = ...
|
||||
dsge_posterior_theoretical_conditional_variance_decomposition(SampleSize,arg3,M_,options_,oo_);
|
||||
end
|
||||
oo_ = conditional_variance_decomposition_posterior_analysis(SampleSize,M_.dname,M_.fname,...
|
||||
arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_);
|
||||
otherwise
|
||||
disp('Not yet implemented')
|
||||
end
|
Loading…
Reference in New Issue