Commit a1222a1d authored by Johannes Pfeifer's avatar Johannes Pfeifer Committed by Stéphane Adjemian
Browse files

Implement variance decomposition with measurement error

parent 2472c580
......@@ -4330,7 +4330,17 @@ period(s). The periods must be strictly positive. Conditional variances are give
decomposition provides the decomposition of the effects of shocks upon
impact. The results are stored in
@code{oo_.conditional_variance_decomposition}
(@pxref{oo_.conditional_variance_decomposition}). The variance decomposition is only conducted, if theoretical moments are requested, @i{i.e.} using the @code{periods=0}-option. In case of @code{order=2}, Dynare provides a second-order accurate approximation to the true second moments based on the linear terms of the second-order solution (see @cite{Kim, Kim, Schaumburg and Sims (2008)}). Note that the unconditional variance decomposition (@i{i.e.} at horizon infinity) is automatically conducted if theoretical moments are requested and if @code{nodecomposition} is not set (@pxref{oo_.variance_decomposition})
(@pxref{oo_.conditional_variance_decomposition}).
In the presence of measurement error, the @code{oo_.conditional_variance_decomposition} field will contain
the variance contribution after measurement error has been taken out, i.e. the decomposition will
be conducted of the actual as opposed to the measured variables. The variance decomposition of the
measured variables will be stored in @code{oo_.conditional_variance_decomposition_ME} (@pxref{oo_.conditional_variance_decomposition_ME}).
The variance decomposition is only conducted, if theoretical moments are requested, @i{i.e.} using the @code{periods=0}-option.
In case of @code{order=2}, Dynare provides a second-order accurate approximation to the true
second moments based on the linear terms of the second-order solution (see @cite{Kim, Kim,
Schaumburg and Sims (2008)}). Note that the unconditional variance decomposition (@i{i.e.} at horizon infinity)
is automatically conducted if theoretical moments are requested and if @code{nodecomposition}
is not set (@pxref{oo_.variance_decomposition})
 
@item pruning
Discard higher order terms when iteratively computing simulations of
......@@ -4559,9 +4569,25 @@ accurate approximation of the true second moments, see @code{conditional_varianc
@defvr {MATLAB/Octave variable} oo_.variance_decomposition
After a run of @code{stoch_simul} when requesting theoretical moments (@code{periods=0}),
contains a matrix with the result of the unconditional variance decomposition (@i{i.e.} at horizon infinity).
The first dimension corresponds to the endogenous variables (in the order of declaration) and
The first dimension corresponds to the endogenous variables (in the order of declaration after the command or in
@code{M_.endo_names} if not specified) and
the second dimension corresponds to exogenous variables (in the order of declaration).
Numbers are in percent and sum up to 100 across columns.
Numbers are in percent and sum up to 100 across columns. In the presence of measurement error, the
field will contain the variance contribution after measurement error has been taken out, i.e. the decomposition will
be conducted of the actual as opposed to the measured variables.
@end defvr
@anchor{oo_.variance_decomposition_ME}
@defvr {MATLAB/Octave variable} oo_.variance_decomposition_ME
Field set after a run of @code{stoch_simul} when requesting theoretical moments (@code{periods=0}) if
measurement error is present.
It is similar to @ref{oo_.variance_decomposition}, but the decomposition will
be conducted of the measured variables. The field contains a matrix with the result
of the unconditional variance decomposition (@i{i.e.} at horizon infinity).
The first dimension corresponds to the observed endogenous variables (in the order of declaration after the command)
and the second dimension corresponds to exogenous variables (in the order of declaration), with the last column
corresponding to the contribution of the measurement error.
Numbers are in percent and sum up to 100 across columns.
@end defvr
 
@anchor{oo_.conditional_variance_decomposition}
......@@ -4570,9 +4596,25 @@ After a run of @code{stoch_simul} with the
@code{conditional_variance_decomposition} option, contains a
three-dimensional array with the result of the decomposition. The
first dimension corresponds to forecast horizons (as declared with the
option), the second dimension corresponds to endogenous variables (in
option), the second dimension corresponds to endogenous variables (in the order
of declaration after the command or in @code{M_.endo_names} if not specified)),
the third dimension corresponds to
exogenous variables (in the order of declaration). In the presence of measurement error, the
field will contain the variance contribution after measurement error has been taken out,
i.e. the decomposition will be conducted of the actual as opposed to the measured variables.
@end defvr
@anchor{oo_.conditional_variance_decomposition_ME}
@defvr {MATLAB/Octave variable} oo_.conditional_variance_decomposition_ME
Field set after a run of @code{stoch_simul} with the @code{conditional_variance_decomposition}
option if measurement error is present. It is similar to @ref{oo_.conditional_variance_decomposition}, but
the decomposition will be conducted of the measured variables.
It contains a three-dimensional array with the result of the decomposition. The
first dimension corresponds to forecast horizons (as declared with the
option), the second dimension corresponds to observed endogenous variables (in
the order of declaration), the third dimension corresponds to
exogenous variables (in the order of declaration).
exogenous variables (in the order of declaration), with the last column
corresponding to the contribution of the measurement error.
@end defvr
 
@anchor{oo_.contemporaneous_correlation}
......@@ -6162,9 +6204,13 @@ positive. Conditional variances are given by @math{var(y_{t+k}|t)}. For
period 1, the conditional variance decomposition provides the
decomposition of the effects of shocks upon impact. The results are
stored in
@code{oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition},
but currently there is no displayed output. Note that this option requires the
option @code{moments_varendo} to be specified.
@code{oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition}.
Note that this option requires the
option @code{moments_varendo} to be specified. In the presence of measurement error, the
field will contain the variance contribution after measurement error has been taken out,
i.e. the decomposition will be conducted of the actual as opposed to the measured variables.
The variance decomposition of the measured variables will be stored in
@code{oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME}.
 
@item filtered_vars
@anchor{filtered_vars} Triggers the computation of the posterior
......@@ -6963,8 +7009,17 @@ decomposition according to the order of declaration of shocks
(@pxref{Variable declarations})}
 
@item ConditionalVarianceDecomposition
@anchor{ConditionalVarianceDecomposition}
Only if the @code{conditional_variance_decomposition} option has been
specified. In the presence of measurement error, the field will contain
the variance contribution after measurement error has been taken out, i.e. the decomposition will
be conducted of the actual as opposed to the measured variables.
@item ConditionalVarianceDecompositionME
Only if the @code{conditional_variance_decomposition} option has been
specified
specified. Same as @ref{ConditionalVarianceDecomposition}, but contains the decomposition of the
measured as opposed to the actual variable. The joint contribution of the measurement error
will be saved in a field named @code{ME}.
 
@end table
 
......
......@@ -34,7 +34,7 @@ function oo_ = compute_moments_varendo(type,options_,M_,oo_,var_list_)
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
fprintf('Estimation::compute_moments_varendo: I''m computing endogenous moments (this may take a while)... ');
fprintf('Estimation::compute_moments_varendo: I''m computing endogenous moments (this may take a while)... \n');
if strcmpi(type,'posterior')
posterior = 1;
......@@ -114,6 +114,7 @@ if M_.exo_nbr > 1
end
end
title='Posterior mean variance decomposition (in percent)';
save_name_string='dsge_post_mean_var_decomp_uncond';
else
for i=1:NumberOfEndogenousVariables
for j=1:NumberOfExogenousVariables
......@@ -122,6 +123,7 @@ if M_.exo_nbr > 1
end
end
title='Prior mean variance decomposition (in percent)';
save_name_string='dsge_prior_mean_var_decomp_uncond';
end
title=add_filter_subtitle(title,options_);
headers = M_.exo_names;
......@@ -135,11 +137,57 @@ if M_.exo_nbr > 1
headers = char(' ',headers);
labels = deblank(var_list_tex);
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title,'dsge_post_mean_var_decomp_uncond',headers,labels,100*temp,lh,8,2);
dyn_latex_table(M_,options_,title,save_name_string,headers,labels,100*temp,lh,8,2);
end
skipline();
end
% CONDITIONAL VARIANCE DECOMPOSITION.
skipline();
if ~all(M_.H==0)
[observable_name_requested_vars,varlist_pos]=intersect(var_list_,options_.varobs,'stable');
if ~isempty(observable_name_requested_vars)
NumberOfObservedEndogenousVariables=length(observable_name_requested_vars);
temp=NaN(NumberOfObservedEndogenousVariables,NumberOfExogenousVariables+1);
if posterior
for i=1:NumberOfObservedEndogenousVariables
for j=1:NumberOfExogenousVariables
temp(i,j,:)=oo_.PosteriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).(deblank(M_.exo_names(j,:)));
end
endo_index_varlist=strmatch(deblank(observable_name_requested_vars{i,1}),var_list_,'exact');
oo_ = posterior_analysis('decomposition',var_list_(endo_index_varlist,:),'ME',[],options_,M_,oo_);
temp(i,j+1,:)=oo_.PosteriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).('ME');
end
title='Posterior mean variance decomposition (in percent) with measurement error';
save_name_string='dsge_post_mean_var_decomp_uncond_ME';
else
for i=1:NumberOfObservedEndogenousVariables
for j=1:NumberOfExogenousVariables
temp(i,j,:)=oo_.PriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(deblank(observable_name_requested_vars(i,:))).(deblank(M_.exo_names(j,:)));
end
endo_index_varlist=strmatch(deblank(observable_name_requested_vars{i,1}),var_list_,'exact');
oo_ = prior_analysis('decomposition',var_list_(endo_index_varlist,:),'ME',[],options_,M_,oo_);
temp(i,j+1,:)=oo_.PriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).('ME');
end
title='Prior mean variance decomposition (in percent) with measurement error';
save_name_string='dsge_prior_mean_var_decomp_uncond_ME';
end
title=add_filter_subtitle(title,options_);
headers = M_.exo_names;
headers(M_.exo_names_orig_ord,:) = headers;
headers = char(' ',headers,'ME');
lh = size(deblank(var_list_),2)+2;
dyntable(options_,title,headers,deblank(char(observable_name_requested_vars)),100* ...
temp,lh,8,2);
if options_.TeX
headers=M_.exo_names_tex;
headers = char(' ',headers,'ME');
labels = deblank(var_list_tex(varlist_pos,:));
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title,save_name_string,headers,labels,100*temp,lh,8,2);
end
skipline();
end
end
% CONDITIONAL VARIANCE DECOMPOSITION.
if Steps
temp=NaN(NumberOfEndogenousVariables,NumberOfExogenousVariables,length(Steps));
if posterior
......@@ -150,6 +198,7 @@ if M_.exo_nbr > 1
end
end
title='Posterior mean conditional variance decomposition (in percent)';
save_name_string='dsge_post_mean_var_decomp_cond_h';
else
for i=1:NumberOfEndogenousVariables
for j=1:NumberOfExogenousVariables
......@@ -158,6 +207,7 @@ if M_.exo_nbr > 1
end
end
title='Prior mean conditional variance decomposition (in percent)';
save_name_string='dsge_prior_mean_var_decomp_cond_h';
end
for step_iter=1:length(Steps)
title_print=[title, ' Period ' int2str(Steps(step_iter))];
......@@ -172,10 +222,56 @@ if M_.exo_nbr > 1
headers = char(' ',headers);
labels = deblank(var_list_tex);
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title_print,['dsge_post_mean_var_decomp_cond_h',int2str(Steps(step_iter))],headers,labels,100*temp(:,:,step_iter),lh,8,2);
dyn_latex_table(M_,options_,title_print,[save_name_string,int2str(Steps(step_iter))],headers,labels,100*temp(:,:,step_iter),lh,8,2);
end
end
skipline();
if ~all(M_.H==0)
if ~isempty(observable_name_requested_vars)
NumberOfObservedEndogenousVariables=length(observable_name_requested_vars);
temp=NaN(NumberOfObservedEndogenousVariables,NumberOfExogenousVariables+1,length(Steps));
if posterior
for i=1:NumberOfObservedEndogenousVariables
for j=1:NumberOfExogenousVariables
temp(i,j,:)=oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).(deblank(M_.exo_names(j,:)));
end
endo_index_varlist=strmatch(deblank(observable_name_requested_vars{i,1}),var_list_,'exact');
oo_ = posterior_analysis('conditional decomposition',endo_index_varlist,'ME',Steps,options_,M_,oo_);
temp(i,j+1,:)=oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).('ME');
end
title='Posterior mean conditional variance decomposition (in percent) with measurement error';
save_name_string='dsge_post_mean_var_decomp_ME_cond_h';
else
for i=1:NumberOfObservedEndogenousVariables
for j=1:NumberOfExogenousVariables
temp(i,j,:)=oo_.PriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(deblank(observable_name_requested_vars(i,:))).(deblank(M_.exo_names(j,:)));
end
endo_index_varlist=strmatch(deblank(observable_name_requested_vars{i,1}),var_list_,'exact');
oo_ = prior_analysis('conditional decomposition',endo_index_varlist,'ME',Steps,options_,M_,oo_);
temp(i,j+1,:)=oo_.PriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(deblank(observable_name_requested_vars{i,1})).('ME');
end
title='Prior mean conditional variance decomposition (in percent) with measurement error';
save_name_string='dsge_prior_mean_var_decomp_ME_cond_h';
end
for step_iter=1:length(Steps)
title_print=[title, ' Period ' int2str(Steps(step_iter))];
headers = M_.exo_names;
headers(M_.exo_names_orig_ord,:) = headers;
headers = char(' ',headers,'ME');
lh = size(deblank(var_list_),2)+2;
dyntable(options_,title_print,headers,deblank(char(observable_name_requested_vars)),100* ...
temp(:,:,step_iter),lh,8,2);
if options_.TeX
headers=M_.exo_names_tex;
headers = char(' ',headers,'ME');
labels = deblank(var_list_tex(varlist_pos,:));
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title_print,[save_name_string,int2str(Steps(step_iter))],headers,labels,100*temp(:,:,step_iter),lh,8,2);
end
end
skipline();
end
end
end
end
......
function ConditionalVarianceDecomposition = conditional_variance_decomposition(StateSpaceModel, Steps, SubsetOfVariables,sigma_e_is_diagonal)
function [ConditionalVarianceDecomposition, ConditionalVarianceDecomposition_ME]= conditional_variance_decomposition(StateSpaceModel, Steps, SubsetOfVariables,sigma_e_is_diagonal)
% 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.
% SubsetOfVariables [integer] 1*q vector of indices (declaration order).
%
% OUTPUTS
% ConditionalVarianceDecomposition [double] [n h p] array, where
% n is equal to length(SubsetOfVariables)
% h is the number of Steps
% p is the number of state innovations and
% SPECIAL REQUIREMENTS
%
% [1] In this version, absence of measurement errors is assumed...
% ConditionalVarianceDecomposition_ME [double] [m h p] array, where
% m is equal to length(intersect(SubsetOfVariables,varobs))
% h is the number of Steps
% p is the number of state innovations and
% Copyright (C) 2010-2017 Dynare Team
%
......@@ -82,4 +83,22 @@ for i=1:number_of_state_innovations
for h = 1:length(Steps)
ConditionalVarianceDecomposition(:,h,i) = squeeze(ConditionalVariance(:,h,i))./SumOfVariances(:,h);
end
end
% get intersection of requested variables and observed variables with
% Measurement error
if ~all(StateSpaceModel.measurement_error==0)
[observable_pos,index_subset,index_observables]=intersect(SubsetOfVariables,StateSpaceModel.observable_pos,'stable');
ME_Variance=diag(StateSpaceModel.measurement_error);
ConditionalVarianceDecomposition_ME = zeros(length(observable_pos),length(Steps),number_of_state_innovations+1);
for i=1:number_of_state_innovations
for h = 1:length(Steps)
ConditionalVarianceDecomposition_ME(:,h,i) = squeeze(ConditionalVariance(index_subset,h,i))./(SumOfVariances(index_subset,h)+ME_Variance(index_observables));
end
end
ConditionalVarianceDecomposition_ME(:,:,number_of_state_innovations+1)=1-sum(ConditionalVarianceDecomposition_ME(:,:,1:number_of_state_innovations),3);
else
ConditionalVarianceDecomposition_ME=[];
end
\ No newline at end of file
function oo_ = ...
conditional_variance_decomposition_ME_mc_analysis(NumberOfSimulations, type, dname, fname, Steps, exonames, exo, var_list, endogenous_variable_index, mh_conf_sig, oo_,options_)
% This function analyses the (posterior or prior) distribution of the
% endogenous variables' conditional variance decomposition with measurement error.
%
% INPUTS
% NumberOfSimulations [integer] scalar, number of simulations.
% type [string] 'prior' or 'posterior'
% dname [string] directory name where to save
% fname [string] name of the mod-file
% Steps [integers] horizons at which to conduct decomposition
% exonames [string] (n_exo*char_length) character array with names of exogenous variables
% exo [string] name of current exogenous
% variable
% var_list [string] (n_endo*char_length) character array with name
% of endogenous variables
% endogenous_variable_index [integer] index of the current
% endogenous variable
% mh_conf_sig [double] 2 by 1 vector with upper
% and lower bound of HPD intervals
% oo_ [structure] Dynare structure where the results are saved.
%
% OUTPUTS
% oo_ [structure] Dynare structure where the results are saved.
% Copyright (C) 2017 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/>.
if strcmpi(type,'posterior')
TYPE = 'Posterior';
PATH = [dname '/metropolis/'];
else
TYPE = 'Prior';
PATH = [dname '/prior/moments/'];
end
% $$$ indx = check_name(vartan,var);
% $$$ if isempty(indx)
% $$$ disp([ type '_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)
if isequal(exo,'ME')
exogenous_variable_index=size(exonames,1)+1;
else
disp([ type '_analysis:: ' exo ' is not a declared exogenous variable!'])
return
end
end
[observable_pos_requested_vars,index_subset,index_observables]=intersect(var_list,options_.varobs,'stable');
matrix_pos=strmatch(var_list(endogenous_variable_index,:),var_list(index_subset,:),'exact');
name_1 = deblank(var_list(endogenous_variable_index,:));
name_2 = deblank(exo);
name = [ name_1 '.' name_2 ];
if isfield(oo_, [ TYPE 'TheoreticalMoments' ])
temporary_structure = oo_.([TYPE 'TheoreticalMoments']);
if isfield(temporary_structure,'dsge')
temporary_structure = oo_.([TYPE 'TheoreticalMoments']).dsge;
if isfield(temporary_structure,'ConditionalVarianceDecompositionME')
temporary_structure = oo_.([TYPE 'TheoreticalMoments']).dsge.ConditionalVarianceDecompositionME.Mean;
if isfield(temporary_structure,name)
if sum(Steps-temporary_structure.(name)(1,:)) == 0
% Nothing (new) to do here...
return
end
end
end
end
end
ListOfFiles = dir([ PATH fname '_' TYPE 'ConditionalVarianceDecompME*.mat']);
i1 = 1; tmp = zeros(NumberOfSimulations,length(Steps));
for file = 1:length(ListOfFiles)
load([ PATH ListOfFiles(file).name ]);
% 4D-array (endovar,time,exovar,simul)
i2 = i1 + size(Conditional_decomposition_array_ME,4) - 1;
tmp(i1:i2,:) = transpose(dynare_squeeze(Conditional_decomposition_array_ME(matrix_pos,:,exogenous_variable_index,:)));
i1 = i2+1;
end
p_mean = NaN(1,length(Steps));
p_median = NaN(1,length(Steps));
p_variance = NaN(1,length(Steps));
p_deciles = NaN(9,length(Steps));
if options_.estimation.moments_posterior_density.indicator
p_density = NaN(2^9,2,length(Steps));
end
p_hpdinf = NaN(1,length(Steps));
p_hpdsup = NaN(1,length(Steps));
for i=1:length(Steps)
if options_.estimation.moments_posterior_density.indicator
[pp_mean, pp_median, pp_var, hpd_interval, pp_deciles, pp_density] = ...
posterior_moments(tmp(:,i),1,mh_conf_sig);
p_density(:,:,i) = pp_density;
else
[pp_mean, pp_median, pp_var, hpd_interval, pp_deciles] = ...
posterior_moments(tmp(:,i),0,mh_conf_sig);
end
p_mean(i) = pp_mean;
p_median(i) = pp_median;
p_variance(i) = pp_var;
p_deciles(:,i) = pp_deciles;
p_hpdinf(i) = hpd_interval(1);
p_hpdsup(i) = hpd_interval(2);
end
FirstField = sprintf('%sTheoreticalMoments', TYPE);
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Steps = Steps;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Mean.(name_1).(name_2) = p_mean;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Median.(name_1).(name_2) = p_median;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.Variance.(name_1).(name_2) = p_variance;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.HPDinf.(name_1).(name_2) = p_hpdinf;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.HPDsup.(name_1).(name_2) = p_hpdsup;
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.deciles.(name_1).(name_2) = p_deciles;
if options_.estimation.moments_posterior_density.indicator
oo_.(FirstField).dsge.ConditionalVarianceDecompositionME.density.(name_1).(name_2) = p_density;
end
\ No newline at end of file
......@@ -56,7 +56,9 @@ end
% $$$ endogenous_variable_index = sum(1:indx);
exogenous_variable_index = check_name(exonames,exo);
if isempty(exogenous_variable_index)
disp([ type '_analysis:: ' exo ' is not a declared exogenous variable!'])
if ~isequal(exo,'ME')
disp([ type '_analysis:: ' exo ' is not a declared exogenous variable!'])
end
return
end
......
......@@ -48,6 +48,25 @@ end
y = y(ivar,options_.drop+1:end)';
ME_present=0;
if ~all(M_.H==0)
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');
if ~isempty(observable_pos_requested_vars)
ME_present=1;
i_ME = setdiff([1:size(M_.H,1)],find(diag(M_.H) == 0)); % find ME with 0 variance
chol_S = chol(M_.H(i_ME,i_ME)); %decompose rest
shock_mat=zeros(options_.periods,size(M_.H,1)); %initialize
shock_mat(:,i_ME)=randn(length(i_ME),options_.periods)'*chol_S;
y_ME = y(:,index_subset)+shock_mat(options_.drop+1:end,index_observables);
y_ME_only = shock_mat(options_.drop+1:end,index_observables);
m_ME = mean(y_ME);
y_ME=get_filtered_time_series(y_ME,m_ME,options_);
y_ME_only_filtered=get_filtered_time_series(y_ME_only,mean(y_ME_only),options_);
s2_ME = mean(y_ME.*y_ME);
end
end
m = mean(y);
% filter series
......@@ -151,8 +170,13 @@ if ~options_.nodecomposition
y_sim_one_shock = simult_(y0,oo_.dr,temp_shock_mat,options_.order);
y_sim_one_shock=y_sim_one_shock(ivar,1+options_.drop+1:end)';
y_sim_one_shock=get_filtered_time_series(y_sim_one_shock,mean(y_sim_one_shock),options_);
oo_.variance_decomposition(:,i_exo_var(shock_iter))=var(y_sim_one_shock)./s2*100;
oo_.variance_decomposition(:,i_exo_var(shock_iter))=var(y_sim_one_shock)./s2*100;
end
if ME_present
oo_.variance_decomposition_ME=oo_.variance_decomposition(index_subset,:)...
.*repmat((s2(index_subset)./s2_ME)',1,length(i_exo_var));
oo_.variance_decomposition_ME(:,end+1)=var(y_ME_only_filtered)./s2_ME*100;
end
if ~options_.noprint %options_.nomoments == 0
skipline()
title='VARIANCE DECOMPOSITION SIMULATING ONE SHOCK AT A TIME (in percent)';
......@@ -164,12 +188,21 @@ if ~options_.nodecomposition
headers = char(' ',headers);
lh = size(deblank(M_.endo_names(ivar,:)),2)+2;
dyntable(options_,title,char(headers,'Tot. lin. contr.'),deblank(M_.endo_names(ivar,:)),[oo_.variance_decomposition sum(oo_.variance_decomposition,2)],lh,8,2);
if ME_present
headers_ME=char(headers,'ME');
dyntable(options_,[title,' WITH MEASUREMENT ERROR'],char(headers_ME,'Tot. lin. contr.'),deblank(M_.endo_names(ivar(index_subset), ...
:)),[oo_.variance_decomposition_ME sum(oo_.variance_decomposition_ME,2)],lh,8,2);
end
if options_.TeX
headers=M_.exo_names_tex;
headers = char(' ',headers);
labels = deblank(M_.endo_names_tex(ivar,:));
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title,'sim_var_decomp',char(headers,'Tot. lin. contr.'),labels_TeX,[oo_.variance_decomposition sum(oo_.variance_decomposition,2)],lh,8,2);
if ME_present
headers_ME=char(headers,'ME');
dyn_latex_table(M_,options_,[title,' WITH MEASUREMENT ERROR'],'sim_var_decomp_ME',char(headers_ME,'Tot. lin. contr.'),labels_TeX(ivar(index_subset),:),[oo_.variance_decomposition_ME sum(oo_.variance_decomposition_ME,2)],lh,8,2);
end
end
if options_.order == 1
......
......@@ -52,9 +52,22 @@ z = [ m sd s2 ];
oo_.mean = m;
oo_.var = oo_.gamma_y{1};
ME_present=0;
if ~all(M_.H==0)
[observable_pos_requested_vars,index_subset,index_observables]=intersect(ivar,options_.varobs_id,'stable');
if ~isempty(observable_pos_requested_vars)
ME_present=1;
end
end
if size(stationary_vars, 1) > 0
if ~nodecomposition
oo_.variance_decomposition=100*oo_.gamma_y{options_.ar+2};
if ME_present
ME_Variance=diag(M_.H);
oo_.variance_decomposition_ME=oo_.variance_decomposition(index_subset,:).*repmat(diag(oo_.var(index_subset,index_subset))./(diag(oo_.var(index_subset,index_subset))+ME_Variance(index_observables)),1,M_.exo_nbr);
oo_.variance_decomposition_ME(:,end+1)=100-sum(oo_.variance_decomposition_ME,2);
end
end
if ~options_.noprint %options_.nomoments == 0
if options_.order == 2
......@@ -88,12 +101,22 @@ if size(stationary_vars, 1) > 0
dyntable(options_,title,headers,deblank(M_.endo_names(ivar(stationary_vars), ...
:)),100* ...
oo_.gamma_y{options_.ar+2}(stationary_vars,:),lh,8,2);
if ME_present
[stationary_observables,pos_index_subset]=intersect(index_subset,stationary_vars,'stable');
headers_ME=char(headers,'ME');
dyntable(options_,[title,' WITH MEASUREMENT ERROR'],headers_ME,deblank(M_.endo_names(ivar(stationary_observables), ...
:)),oo_.variance_decomposition_ME(pos_index_subset,:),lh,8,2);
end
if options_.TeX
headers=M_.exo_names_tex;
headers = char(' ',headers);
labels = deblank(M_.endo_names_tex(ivar(stationary_vars),:));
lh = size(labels,2)+2;
dyn_latex_table(M_,options_,title,'th_var_decomp_uncond',headers,labels,100*oo_.gamma_y{options_.ar+2}(stationary_vars,:),lh,8,2);
if ME_present
headers_ME=char(headers,'ME');
dyn_latex_table(M_,options_,[title,' WITH MEASUREMENT ERROR'],'th_var_decomp_uncond_ME',headers_ME,labels,oo_.variance_decomposition_ME(pos_index_subset,:),lh,8,2);
end
end
end
end
......@@ -106,11 +129,17 @@ if size(stationary_vars, 1) > 0
[StateSpaceModel.transition_matrix,StateSpaceModel.impulse_matrix] = kalman_transition_matrix(dr,(1:M_.endo_nbr)',M_.nstatic+(1:M_.nspred)',M_.exo_nbr);
StateSpaceModel.state_innovations_covariance_matrix = M_.Sigma_e;
StateSpaceModel.order_var = dr.order_var;
oo_.conditional_variance_decomposition = conditional_variance_decomposition(StateSpaceModel,conditional_variance_steps,ivar);
StateSpaceModel.measurement_error=M_.H;
StateSpaceModel.observable_pos=options_.varobs_id;
[oo_.conditional_variance_decomposition, oo_.conditional_variance_decomposition_ME]= conditional_variance_decomposition(StateSpaceModel,conditional_variance_steps,ivar);
if options_.noprint == 0
display_conditional_variance_decomposition(oo_.conditional_variance_decomposition,conditional_variance_steps,...
ivar,M_,options_);
if ME_present