Added posterior multivariate auto-correlation analysis

+ Changed load instruction in selec_posterior_draws for octave compatibility.

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1911 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
adjemian 2008-07-01 11:03:23 +00:00
parent adb5f730f5
commit c86ba80007
5 changed files with 234 additions and 26 deletions

View File

@ -44,6 +44,8 @@ function [info,description] = check_posterior_analysis_data(type,M_)
generic_post_data_file_name = 'Posterior2ndOrderMoments';
case 'decomposition'
generic_post_data_file_name = 'PosteriorVarianceDecomposition';
case 'correlation'
generic_post_data_file_name = 'PosteriorCorrelations';
case 'dynamic_decomposition'
generic_post_data_file_name = 'PosteriorDynamicVarianceDecomposition';
otherwise

View File

@ -0,0 +1,102 @@
function oo_ = correlation_posterior_analysis(SampleSize,dname,fname,vartan,nvar,var1,var2,nar,mh_conf_sig,oo_,M_,options_)
indx1 = check_name(vartan,var1);
if isempty(indx1)
disp(['posterior_analysis:: ' var1 ' is not a stationary endogenous variable!'])
return
end
if ~isempty(var2)
indx2 = check_name(vartan,var2);
if isempty(indx2)
disp(['posterior_analysis:: ' var2 ' is not a stationary endogenous variable!'])
return
end
else
indx2 = indx1;
var2 = var1;
end
if isfield(oo_,'PosteriorTheoreticalMoments')
if isfield(oo_.PosteriorTheoreticalMoments,'dsge')
if isfield(oo_.PosteriorTheoreticalMoments.dsge,'correlation')
if isfield(oo_.PosteriorTheoreticalMoments.dsge.correlation.mean,var1)
eval(['s1 = oo_.PosteriorTheoreticalMoments.dsge.correlation.mean' '.' var1 ';'])
if isfield(s1,var2)
eval(['s2 = s1' '.' var2 ';'])
l1 = length(s2);
if l1<nar
% INITIALIZATION:
oo_ = initialize_output_structure(var1,var2,nar,oo_);
system(['rm ' M_.dname '/metropolis/' M_.fname '_PosteriorCorrelations*']);
[nvar,vartan,NumberOfFiles] = ...
dsge_posterior_theoretical_correlation(SampleSize,nar,M_,options_,oo_);
else
if ~isnan(s2(nar))
%Nothing to do.
return
end
end
else
oo_ = initialize_output_structure(var1,var2,nar,oo_);
end
else
oo_ = initialize_output_structure(var1,var2,nar,oo_);
end
else
oo_ = initialize_output_structure(var1,var2,nar,oo_);
end
else
oo_ = initialize_output_structure(var1,var2,nar,oo_);
end
else
oo_ = initialize_output_structure(var1,var2,nar,oo_);
end
tmp = dir([ dname '/metropolis/' fname '_PosteriorCorrelations*.mat']);
NumberOfFiles = length(tmp);
i1 = 1; tmp = zeros(SampleSize,1);
for file = 1:NumberOfFiles
load([ dname '/metropolis/' fname '_PosteriorCorrelations' int2str(file) '.mat']);
i2 = i1 + rows(Correlation_array) - 1;
tmp(i1:i2) = Correlation_array(:,indx1,indx2,nar);
i1 = i2+1;
end
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(tmp,1,mh_conf_sig);
name = [ var1 '.' var2 ];
if isfield(oo_,'PosteriorTheoreticalMoments')
if isfield(oo_.PosteriorTheoreticalMoments,'dsge')
if isfield(oo_.PosteriorTheoreticalMoments.dsge,'correlation')
oo_ = fill_output_structure(var1,var2,oo_,'mean',nar,post_mean);
oo_ = fill_output_structure(var1,var2,oo_,'median',nar,post_median);
oo_ = fill_output_structure(var1,var2,oo_,'variance',nar,post_var);
oo_ = fill_output_structure(var1,var2,oo_,'hpdinf',nar,hpd_interval(1));
oo_ = fill_output_structure(var1,var2,oo_,'hpdsup',nar,hpd_interval(2));
oo_ = fill_output_structure(var1,var2,oo_,'deciles',nar,post_deciles);
oo_ = fill_output_structure(var1,var2,oo_,'density',nar,density);
end
end
end
function oo_ = initialize_output_structure(var1,var2,nar,oo_)
name = [ var1 '.' var2 ];
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.mean.' name ' = NaN(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.median.' name ' = NaN(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.variance.' name ' = NaN(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.hpdinf.' name ' = NaN(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.hpdsup.' name ' = NaN(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.deciles.' name ' = cell(' int2str(nar) ',1);']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.density.' name ' = cell(' int2str(nar) ',1);']);
for i=1:nar
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.density.' name '(' int2str(i) ',1) = {NaN};']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.deciles.' name '(' int2str(i) ',1) = {NaN};']);
end
function oo_ = fill_output_structure(var1,var2,oo_,type,lag,result)
name = [ var1 '.' var2 ];
switch type
case {'mean','median','variance','hpdinf','hpdsup'}
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.' type '.' name '(' int2str(lag) ',1) = result;']);
case {'deciles','density'}
eval(['oo_.PosteriorTheoreticalMoments.dsge.correlation.' type '.' name '(' int2str(lag) ',1) = {result};']);
otherwise
disp('fill_output_structure:: Unknown field!')
end

View File

@ -0,0 +1,91 @@
function [nvar,vartan,CorrFileNumber] = dsge_posterior_theoretical_correlation(SampleSize,nar,M_,options_,oo_)
% This function estimates the posterior density of the endogenous
% variables second order moments.
%
% INPUTS
% SampleSize [integer]
%
%
%
% OUTPUTS
% None.
%
% SPECIAL REQUIREMENTS
% Other matlab routines distributed with Dynare: set_stationary_variables_list.m
% CheckPath.m
% selec_posterior_draws.m
% set_parameters.m
% resol.m
% th_autocovariances.m
% posterior_moments.m
%
%
% part of DYNARE, copyright Dynare Team (2007-2008)
% Gnu Public License.
type = 'posterior';
% Set varlist (vartan)
[ivar,vartan] = set_stationary_variables_list;
nvar = length(ivar);
% Set the size of the auto-correlation function to nar.
oldnar = options_.ar;
options_.ar = nar;
% Get informations about the _posterior_draws files.
DrawsFiles = dir([M_.dname '/metropolis/' M_.fname '_' type '_draws*' ]);
NumberOfDrawsFiles = length(DrawsFiles);
% Number of lines in posterior data files.
MaXNumberOfCorrLines = ceil(options_.MaxNumberOfBytes/(nvar*nvar*nar)/8);
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
NumberOfCorrLines = rows(Correlation_array);
CorrFileNumber = 1;
% Compute 2nd order moments and save them in *_PosteriorCorrelations* files
linea = 0;
for file = 1:NumberOfDrawsFiles
load([M_.dname '/metropolis/' DrawsFiles(file).name ]);
NumberOfDraws = rows(pdraws);
isdrsaved = cols(pdraws)-1;
for linee = 1:NumberOfDraws
linea = linea+1;
if isdrsaved
dr = pdraws{linee,2};
else
set_parameters(pdraws{linee,1});
[dr,info] = resol(oo_.steady_state,0);
end
tmp = th_autocovariances(dr,ivar,M_,options_);
for i=1:nar
Correlation_array(linea,:,:,i) = tmp{i+1};
end
if linea == NumberOfCorrLines
save([ M_.dname '/metropolis/' M_.fname '_PosteriorCorrelations' int2str(CorrFileNumber) '.mat' ],'Correlation_array');
CorrFileNumber = CorrFileNumber + 1;
linea = 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
end
end
options_.ar = oldnar;

View File

@ -1,4 +1,4 @@
function oo_ = posterior_analysis(type,arg1,arg2,options_,M_,oo_)
function oo_ = posterior_analysis(type,arg1,arg2,arg3,options_,M_,oo_)
% part of DYNARE, copyright Dynare Team (2008)
% Gnu Public License.
@ -15,34 +15,47 @@ function oo_ = posterior_analysis(type,arg1,arg2,options_,M_,oo_)
drsize=0;
end
SampleAddress = selec_posterior_draws(SampleSize,drsize);
oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_);
case {4,5}
switch type
case 'variance'
[nvar,vartan,NumberOfFiles] = ...
dsge_posterior_theoretical_covariance(SampleSize,M_,options_,oo_);
oo_ = covariance_posterior_analysis(SampleSize,M_.dname,M_.fname,...
vartan,nvar,arg1,arg2,options_.mh_conf_sig,oo_);
case 'decomposition'
[nvar,vartan,NumberOfFiles] = ...
dsge_posterior_theoretical_variance_decomposition(SampleSize,M_,options_,oo_);
oo_ = variance_decomposition_posterior_analysis(SampleSize,M_.dname,M_.fname,...
M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_);
otherwise
disp('Not yet implemented')
end
oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_);
case 6
[ivar,vartan] = set_stationary_variables_list;
nvar = length(ivar);
switch type
case 'variance'
oo_ = covariance_posterior_analysis(SampleSize,M_.dname,M_.fname,...
vartan,nvar,arg1,arg2,options_.mh_conf_sig,oo_);
case 'decomposition'
oo_ = variance_decomposition_posterior_analysis(SampleSize,M_.dname,M_.fname,...
M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_);
otherwise
disp('Not yet implemented')
end
oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_,nvar,vartan);
otherwise
error(['posterior_analysis:: Check_posterior_analysis_data gave a meaningless output!'])
end
function oo_ = job(type,SampleSize,arg1,arg2,arg3,options_,M_,oo_,nvar,vartan)
narg1 = 8;
narg2 = 10;
if ~(nargin==narg1 | nargin==narg2)
error('posterior_analysis:: Call to function job is buggy!')
end
switch type
case 'variance'
if nargin==narg1
[nvar,vartan,NumberOfFiles] = ...
dsge_posterior_theoretical_covariance(SampleSize,M_,options_,oo_);
end
oo_ = covariance_posterior_analysis(SampleSize,M_.dname,M_.fname,...
vartan,nvar,arg1,arg2,options_.mh_conf_sig,oo_);
case 'decomposition'
if nargin==narg1
[nvar,vartan,NumberOfFiles] = ...
dsge_posterior_theoretical_variance_decomposition(SampleSize,M_,options_,oo_);
end
oo_ = variance_decomposition_posterior_analysis(SampleSize,M_.dname,M_.fname,...
M_.exo_names,arg2,vartan,arg1,options_.mh_conf_sig,oo_);
case 'correlation'
if nargin==narg1
[nvar,vartan,NumberOfFiles] = ...
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_);
otherwise
disp('Not yet implemented')
end

View File

@ -50,7 +50,7 @@ function SampleAddress = selec_posterior_draws(SampleSize,drsize)
% Get informations about the mcmc:
MhDirectoryName = CheckPath('metropolis');
fname = [ MhDirectoryName '/' M_.fname];
load([ fname '_mh_history']);
load([ fname '_mh_history.mat']);
FirstMhFile = record.KeepedDraws.FirstMhFile;
FirstLine = record.KeepedDraws.FirstLine;
TotalNumberOfMhFiles = sum(record.MhDraws(:,2));