Fix moment computation with Measurement errors

- check logic for M_.H was faulty
- M_.H was not updated in posterior sampling
time-shift
Johannes Pfeifer 2020-06-26 18:24:33 +02:00
parent 69fc7acb9c
commit 6e06acc7f4
12 changed files with 46 additions and 44 deletions

View File

@ -144,7 +144,7 @@ if M_.exo_nbr > 1
skipline(); skipline();
end end
skipline(); skipline();
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if isoctave if isoctave
[observable_name_requested_vars, varlist_pos] = intersect_stable(var_list_, options_.varobs); [observable_name_requested_vars, varlist_pos] = intersect_stable(var_list_, options_.varobs);
else else
@ -231,7 +231,7 @@ if M_.exo_nbr > 1
end end
end end
skipline(); skipline();
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if ~isempty(observable_name_requested_vars) if ~isempty(observable_name_requested_vars)
NumberOfObservedEndogenousVariables = length(observable_name_requested_vars); NumberOfObservedEndogenousVariables = length(observable_name_requested_vars);
temp=NaN(NumberOfObservedEndogenousVariables,NumberOfExogenousVariables+1,length(Steps)); temp=NaN(NumberOfObservedEndogenousVariables,NumberOfExogenousVariables+1,length(Steps));

View File

@ -88,7 +88,7 @@ end
% get intersection of requested variables and observed variables with % get intersection of requested variables and observed variables with
% Measurement error % Measurement error
if ~all(StateSpaceModel.measurement_error==0) if ~all(diag(StateSpaceModel.measurement_error)==0)
if isoctave if isoctave
[observable_pos,index_subset,index_observables]=intersect_stable(SubsetOfVariables,StateSpaceModel.observable_pos); [observable_pos,index_subset,index_observables]=intersect_stable(SubsetOfVariables,StateSpaceModel.observable_pos);
else else

View File

@ -54,7 +54,7 @@ oo_.mean = m;
oo_.var = oo_.gamma_y{1}; oo_.var = oo_.gamma_y{1};
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if isoctave if isoctave
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else

View File

@ -82,7 +82,7 @@ NumberOfSavedElementsPerSimulation = nvar*M_.exo_nbr*length(Steps);
MaXNumberOfConditionalDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8); MaXNumberOfConditionalDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8);
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if isoctave if isoctave
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else
@ -131,20 +131,20 @@ linea = 0;
linea_ME = 0; linea_ME = 0;
for file = 1:NumberOfDrawsFiles for file = 1:NumberOfDrawsFiles
if posterior if posterior
load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
else else
load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
end end
isdrsaved = columns(pdraws)-1; isdrsaved = columns(temp.pdraws)-1;
NumberOfDraws = rows(pdraws); NumberOfDraws = rows(temp.pdraws);
for linee = 1:NumberOfDraws for linee = 1:NumberOfDraws
linea = linea+1; linea = linea+1;
linea_ME = linea_ME+1; linea_ME = linea_ME+1;
if isdrsaved if isdrsaved
M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
dr = pdraws{linee,2}; dr = temp.pdraws{linee,2};
else else
M_=set_parameters_locally(M_,pdraws{linee,1}); M_=set_parameters_locally(M_,temp.pdraws{linee,1});
[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
end end
if first_call if first_call
@ -163,6 +163,7 @@ for file = 1:NumberOfDrawsFiles
end end
[StateSpaceModel.transition_matrix,StateSpaceModel.impulse_matrix] = kalman_transition_matrix(dr,iv,ic,M_.exo_nbr); [StateSpaceModel.transition_matrix,StateSpaceModel.impulse_matrix] = kalman_transition_matrix(dr,iv,ic,M_.exo_nbr);
StateSpaceModel.state_innovations_covariance_matrix = M_.Sigma_e; StateSpaceModel.state_innovations_covariance_matrix = M_.Sigma_e;
M_ = set_measurement_errors(temp.pdraws{linee,1},temp.estim_params_,M_);
StateSpaceModel.measurement_error=M_.H; StateSpaceModel.measurement_error=M_.H;
clear('dr'); clear('dr');
[ConditionalVarianceDecomposition, ConditionalVarianceDecomposition_ME]=conditional_variance_decomposition(StateSpaceModel, Steps, ivar); [ConditionalVarianceDecomposition, ConditionalVarianceDecomposition_ME]=conditional_variance_decomposition(StateSpaceModel, Steps, ivar);

View File

@ -94,19 +94,19 @@ CorrFileNumber = 1;
linea = 0; linea = 0;
for file = 1:NumberOfDrawsFiles for file = 1:NumberOfDrawsFiles
if posterior if posterior
load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
else else
load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
end end
NumberOfDraws = rows(pdraws); NumberOfDraws = rows(temp.pdraws);
isdrsaved = columns(pdraws)-1; isdrsaved = columns(temp.pdraws)-1;
for linee = 1:NumberOfDraws for linee = 1:NumberOfDraws
linea = linea+1; linea = linea+1;
if isdrsaved if isdrsaved
M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
dr = pdraws{linee,2}; dr = temp.pdraws{linee,2};
else else
M_=set_parameters_locally(M_,pdraws{linee,1}); M_=set_parameters_locally(M_,temp.pdraws{linee,1});
[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
end end
tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition); tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition);

View File

@ -93,19 +93,19 @@ CovarFileNumber = 1;
linea = 0; linea = 0;
for file = 1:NumberOfDrawsFiles for file = 1:NumberOfDrawsFiles
if posterior if posterior
load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
else else
load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
end end
NumberOfDraws = rows(pdraws); NumberOfDraws = rows(temp.pdraws);
isdrsaved = columns(pdraws)-1; isdrsaved = columns(temp.pdraws)-1;
for linee = 1:NumberOfDraws for linee = 1:NumberOfDraws
linea = linea+1; linea = linea+1;
if isdrsaved if isdrsaved
M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
dr = pdraws{linee,2}; dr = temp.pdraws{linee,2};
else else
M_=set_parameters_locally(M_,pdraws{linee,1}); M_=set_parameters_locally(M_,temp.pdraws{linee,1});
[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
end end
tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition); tmp = th_autocovariances(dr,ivar,M_,options_,nodecomposition);

View File

@ -85,7 +85,7 @@ NumberOfSavedElementsPerSimulation = nvar*(nexo+1);
MaXNumberOfDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8); MaXNumberOfDecompLines = ceil(options_.MaxNumberOfBytes/NumberOfSavedElementsPerSimulation/8);
ME_present=0; ME_present=0;
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if isoctave if isoctave
[observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id); [observable_pos_requested_vars,index_subset,index_observables]=intersect_stable(ivar,options_.varobs_id);
else else
@ -130,20 +130,20 @@ linea_ME = 0;
only_non_stationary_vars=0; only_non_stationary_vars=0;
for file = 1:NumberOfDrawsFiles for file = 1:NumberOfDrawsFiles
if posterior if posterior
load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]); temp=load([M_.dname '/metropolis/' M_.fname '_' type '_draws' num2str(file) ]);
else else
load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]); temp=load([M_.dname '/prior/draws/' type '_draws' num2str(file) ]);
end end
isdrsaved = columns(pdraws)-1; isdrsaved = columns(temp.pdraws)-1;
NumberOfDraws = rows(pdraws); NumberOfDraws = rows(temp.pdraws);
for linee = 1:NumberOfDraws for linee = 1:NumberOfDraws
linea = linea+1; linea = linea+1;
linea_ME = linea_ME+1; linea_ME = linea_ME+1;
if isdrsaved if isdrsaved
M_=set_parameters_locally(M_,pdraws{linee,1});% Needed to update the covariance matrix of the state innovations. M_=set_parameters_locally(M_,temp.pdraws{linee,1});% Needed to update the covariance matrix of the state innovations.
dr = pdraws{linee,2}; dr = temp.pdraws{linee,2};
else else
M_=set_parameters_locally(M_,pdraws{linee,1}); M_=set_parameters_locally(M_,temp.pdraws{linee,1});
[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); [dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
end end
if file==1 && linee==1 if file==1 && linee==1
@ -164,6 +164,7 @@ for file = 1:NumberOfDrawsFiles
end end
end end
if ME_present if ME_present
M_ = set_measurement_errors(temp.pdraws{linee,1},temp.estim_params_,M_);
ME_Variance=diag(M_.H); ME_Variance=diag(M_.H);
tmp_ME=NaN(nobs_ME,nexo+1); 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(:,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);

View File

@ -64,7 +64,7 @@ switch type
end end
oo_ = variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... oo_ = variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,...
M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if strmatch(arg1,options_.varobs,'exact') if strmatch(arg1,options_.varobs,'exact')
if isoctave if isoctave
[observable_name_requested_vars,index_subset,index_observables]=intersect_stable(vartan,options_.varobs); [observable_name_requested_vars,index_subset,index_observables]=intersect_stable(vartan,options_.varobs);
@ -72,7 +72,7 @@ switch type
[observable_name_requested_vars,index_subset,index_observables]=intersect(vartan,options_.varobs,'stable'); [observable_name_requested_vars,index_subset,index_observables]=intersect(vartan,options_.varobs,'stable');
end end
oo_ = variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... oo_ = variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,...
M_.exo_names,arg2,observable_name_requested_vars,arg1,options_.mh_conf_sig,oo_,options_); [M_.exo_names;'ME'],arg2,observable_name_requested_vars,arg1,options_.mh_conf_sig,oo_,options_);
end end
end end
case 'correlation' case 'correlation'
@ -89,10 +89,10 @@ switch type
end end
oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,...
arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if strmatch(arg1,options_.varobs,'exact') if strmatch(arg1,options_.varobs,'exact')
oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,... oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'posterior',M_.dname,M_.fname,...
arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); arg3,[M_.exo_names;'ME'],arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);
end end
end end
otherwise otherwise

View File

@ -78,7 +78,7 @@ switch type
end end
oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,... oo_ = conditional_variance_decomposition_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,...
arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
if strmatch(vartan(arg1,:),options_.varobs,'exact') if strmatch(vartan(arg1,:),options_.varobs,'exact')
oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,... oo_ = conditional_variance_decomposition_ME_mc_analysis(SampleSize,'prior',M_.dname,M_.fname,...
arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_); arg3,M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_,options_);

View File

@ -150,7 +150,7 @@ while iteration < NumberOfSimulations
end end
if ( file_line_number==TableOfInformations(file_indx_number+1,2) ) if ( file_line_number==TableOfInformations(file_indx_number+1,2) )
file_indx_number = file_indx_number + 1; file_indx_number = file_indx_number + 1;
save([ PriorDirectoryName '/prior_draws' int2str(file_indx_number) '.mat' ],'pdraws'); save([ PriorDirectoryName '/prior_draws' int2str(file_indx_number) '.mat' ],'pdraws','estim_params_');
if file_indx_number<NumberOfFiles if file_indx_number<NumberOfFiles
if drsave if drsave
pdraws = cell(TableOfInformations(file_indx_number+1,2),drsave+2); pdraws = cell(TableOfInformations(file_indx_number+1,2),drsave+2);

View File

@ -122,7 +122,7 @@ if info
old_mhblck = mhblck; old_mhblck = mhblck;
end end
clear('x2') clear('x2')
save([BaseName '_posterior_draws1.mat'],'pdraws') save([BaseName '_posterior_draws1.mat'],'pdraws','estim_params_')
else% The posterior draws are saved in xx files. else% The posterior draws are saved in xx files.
NumberOfDrawsPerFile = fix(MAX_mega_bytes/drawsize); NumberOfDrawsPerFile = fix(MAX_mega_bytes/drawsize);
NumberOfFiles = ceil(SampleSize*drawsize/MAX_mega_bytes); NumberOfFiles = ceil(SampleSize*drawsize/MAX_mega_bytes);
@ -149,7 +149,7 @@ if info
old_mhblck = mhblck; old_mhblck = mhblck;
if fnum < NumberOfFiles && linee == NumberOfDrawsPerFile if fnum < NumberOfFiles && linee == NumberOfDrawsPerFile
linee = 0; linee = 0;
save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws') save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws','estim_params_')
fnum = fnum+1; fnum = fnum+1;
if fnum < NumberOfFiles if fnum < NumberOfFiles
pdraws = cell(NumberOfDrawsPerFile,info); pdraws = cell(NumberOfDrawsPerFile,info);
@ -158,6 +158,6 @@ if info
end end
end end
end end
save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws') save([BaseName '_posterior_draws' num2str(fnum) '.mat'],'pdraws','estim_params_')
end end
end end

View File

@ -124,7 +124,7 @@ if ~options_.noprint
lh = cellofchararraymaxlength(labels)+2; lh = cellofchararraymaxlength(labels)+2;
dyn_latex_table(M_, options_, my_title, 'covar_ex_shocks', headers, labels, M_.Sigma_e, lh, 10, 6); dyn_latex_table(M_, options_, my_title, 'covar_ex_shocks', headers, labels, M_.Sigma_e, lh, 10, 6);
end end
if ~all(M_.H==0) if ~all(diag(M_.H)==0)
my_title='MATRIX OF COVARIANCE OF MEASUREMENT ERRORS'; my_title='MATRIX OF COVARIANCE OF MEASUREMENT ERRORS';
labels = cellfun(@(x) horzcat('SE_', x), options_.varobs, 'UniformOutput', false); labels = cellfun(@(x) horzcat('SE_', x), options_.varobs, 'UniformOutput', false);
headers = vertcat('Variables', labels); headers = vertcat('Variables', labels);