Changed posterior_moments (global variable options_ is not needed anymore, mh_conf_sig is a new input argument) + Cosmetic changes.

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1836 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
adjemian 2008-05-19 13:22:57 +00:00
parent f85752893e
commit de48d0b3ee
8 changed files with 1012 additions and 989 deletions

View File

@ -1,288 +1,290 @@
function get_posterior_parameters_statistics()
% function get_posterior_parameters_statistics()
% This function prints and saves posterior estimates after the mcmc
% (+updates of oo_ & TeX output).
%
% INPUTS
% None.
%
% OUTPUTS
% None.
%
% SPECIAL REQUIREMENTS
% None.
%
% part of DYNARE, copyright Dynare Team (2006-2008)
% Gnu Public License.
global estim_params_ M_ options_ bayestopt_ oo_
if ~options_.mh_replic & options_.load_mh_file
load([M_.fname '_results.mat'],'oo_');
end
TeX = options_.TeX;
nblck = options_.mh_nblck;
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
nx = nvx+nvn+ncx+ncn+np;
DirectoryName = CheckPath('metropolis');
OutputDirectoryName = CheckPath('Output');
load([ DirectoryName '/' M_.fname '_mh_history'])
FirstMhFile = record.KeepedDraws.FirstMhFile;
FirstLine = record.KeepedDraws.FirstLine; ifil = FirstLine;
TotalNumberOfMhFiles = sum(record.MhDraws(:,2))
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
FirstMhFile = record.KeepedDraws.FirstMhFile;
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
clear record;
pnames=[' ';'beta ';'gamm ';'norm ';'invg ';'unif ';'invg2'];
tit2 = sprintf('%10s %7s %10s %14s %4s %6s\n',' ','prior mean','post. mean','conf. interval','prior','pstdev');
pformat = '%12s %7.3f %8.4f %7.4f %7.4f %4s %6.4f';
disp(' ');disp(' ');disp('ESTIMATION RESULTS');disp(' ');
disp(sprintf('Log data density is %f.',oo_.MarginalDensity.ModifiedHarmonicMean))
if np
type = 'parameters';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,1,type);
end
disp(' ')
disp(type)
disp(tit2)
ip = nvx+nvn+ncx+ncn+1;
for i=1:np
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, ...
density] = posterior_moments(Draws,1);
name = bayestopt_.name{ip};
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
else
name = bayestopt_.name{ip};
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),...
post_mean, ...
hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:), ...
bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,1,type);
end
end
if nvx
type = 'shocks_std';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,2,'standard deviation of structural shocks');
end
ip = 1;
disp(' ')
disp('standard deviation of shocks')
disp(tit2)
for i=1:nvx
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws,1);
k = estim_params_.var_exo(i,1);
name = deblank(M_.exo_names(k,:));
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
M_.Sigma_e(k,k) = post_mean*post_mean;
else
k = estim_params_.var_exo(i,1);
name = deblank(M_.exo_names(k,:));
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),post_mean,hpd_interval,...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,2,'standard deviation of structural shocks');
end
end
if nvn
type = 'measurement_errors_std';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,3,'standard deviation of measurement errors')
end
disp(' ')
disp('standard deviation of measurement errors')
disp(tit2)
ip = nvx+1;
for i=1:nvn
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws,1);
name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
else
name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,3,'standard deviation of measurement errors');
end
end
if ncx
type = 'shocks_corr';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,4,'correlation of structural shocks');
end
disp(' ')
disp('correlation of shocks')
disp(tit2)
ip = nvx+nvn+1;
for i=1:ncx
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws,1);
k1 = estim_params_.corrx(i,1);
k2 = estim_params_.corrx(i,2);
name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
M_.Sigma_e(k1,k2) = post_mean*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2));
M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2);
else
k1 = estim_params_.corrx(i,1);
k2 = estim_params_.corrx(i,2);
name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
[post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
end
disp(sprintf(pformat, name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,4,'correlation of structural shocks');
end
end
if ncn
type = 'measurement_errors_corr';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,5,'correlation of measurement errors');
end
disp(' ')
disp('correlation of measurement errors')
disp(tit2)
ip = nvx+nvn+ncx+1;
for i=1:ncn
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(Draws,1);
k1 = estim_params_.corrn(i,1);
k2 = estim_params_.corrn(i,2);
name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,...
post_median,post_var,post_deciles,density);
else
k1 = estim_params_.corrn(i,1);
k2 = estim_params_.corrn(i,2);
name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
[post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
end
disp(sprintf(pformat, name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,5,'correlation of measurement errors');
end
end
%
%% subfunctions:
%
function fid = TeXBegin(directory,fname,fnum,title)
TeXfile = [directory '/' fname '_Posterior_Mean_' int2str(fnum) '.TeX'];
fidTeX = fopen(TeXfile,'w');
fprintf(fidTeX,'%% TeX-table generated by Dynare.\n');
fprintf(fidTeX,['%% RESULTS FROM METROPOLIS HASTINGS (' title ')\n']);
fprintf(fidTeX,['%% ' datestr(now,0)]);
fprintf(fidTeX,' \n');
fprintf(fidTeX,' \n');
fprintf(fidTeX,'\\begin{table}\n');
fprintf(fidTeX,'\\centering\n');
fprintf(fidTeX,'\\begin{tabular}{l|lcccccc} \n');
fprintf(fidTeX,'\\hline\\hline \\\\ \n');
fprintf(fidTeX,[' & Prior distribution & Prior mean & Prior ' ...
's.d. & Posterior mean & Posterior s.d. & HPD inf & HPD sup\\\\ \n']);
fprintf(fidTeX,'\\hline \\\\ \n');
fid = fidTeX;
function TeXCore(fid,name,shape,priormean,priorstd,postmean,poststd,hpd)
fprintf(fid,['$%s$ & %s & %7.3f & %6.4f & %7.3f& %6.4f & %7.4f & %7.4f \\\\ \n'],...
name,...
shape,...
priormean,...
priorstd,...
postmean,...
poststd,...
hpd(1),...
hpd(2));
function TeXEnd(fid,fnum,title)
fprintf(fid,'\\hline\\hline \n');
fprintf(fid,'\\end{tabular}\n ');
fprintf(fid,['\\caption{Results from Metropolis-Hastings (' title ')}\n ']);
fprintf(fid,['\\label{Table:MHPosterior:' int2str(fnum) '}\n']);
fprintf(fid,'\\end{table}\n');
fprintf(fid,'%% End of TeX file.\n');
fclose(fid);
function oo = Filloo(oo,name,type,postmean,hpdinterval,postmedian,postvar,postdecile,density)
eval(['oo.posterior_mean.' type '.' name ' = postmean;']);
eval(['oo.posterior_hpdinf.' type '.' name ' = hpdinterval(1);']);
eval(['oo.posterior_hpdsup.' type '.' name ' = hpdinterval(2);']);
eval(['oo.posterior_median.' type '.' name ' = postmedian;']);
eval(['oo.posterior_variance.' type '.' name ' = postvar;']);
eval(['oo.posterior_deciles.' type '.' name ' = postdecile;']);
eval(['oo.posterior_density.' type '.' name ' = density;']);
function [post_mean,hpd_interval,post_var] = Extractoo(oo,name,type)
hpd_interval = zeros(2,1);
eval(['post_mean = oo.posterior_mean.' type '.' name ';']);
eval(['hpd_interval(1) = oo.posterior_hpdinf.' type '.' name ';']);
eval(['hpd_interval(2) = oo.posterior_hpdsup.' type '.' name ';']);
function get_posterior_parameters_statistics()
% This function prints and saves posterior estimates after the mcmc
% (+updates of oo_ & TeX output).
%
% INPUTS
% None.
%
% OUTPUTS
% None.
%
% SPECIAL REQUIREMENTS
% None.
%
% part of DYNARE, copyright Dynare Team (2006-2008)
% Gnu Public License.
global estim_params_ M_ options_ bayestopt_ oo_
if ~options_.mh_replic & options_.load_mh_file
load([M_.fname '_results.mat'],'oo_');
end
TeX = options_.TeX;
nblck = options_.mh_nblck;
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
nx = nvx+nvn+ncx+ncn+np;
DirectoryName = CheckPath('metropolis');
OutputDirectoryName = CheckPath('Output');
load([ DirectoryName '/' M_.fname '_mh_history'])
FirstMhFile = record.KeepedDraws.FirstMhFile;
FirstLine = record.KeepedDraws.FirstLine; ifil = FirstLine;
TotalNumberOfMhFiles = sum(record.MhDraws(:,2))
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
FirstMhFile = record.KeepedDraws.FirstMhFile;
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
clear record;
pnames=[' ';'beta ';'gamm ';'norm ';'invg ';'unif ';'invg2'];
tit2 = sprintf('%10s %7s %10s %14s %4s %6s\n',' ','prior mean','post. mean','conf. interval','prior','pstdev');
pformat = '%12s %7.3f %8.4f %7.4f %7.4f %4s %6.4f';
disp(' ');disp(' ');disp('ESTIMATION RESULTS');disp(' ');
disp(sprintf('Log data density is %f.',oo_.MarginalDensity.ModifiedHarmonicMean))
if np
type = 'parameters';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,1,type);
end
disp(' ')
disp(type)
disp(tit2)
ip = nvx+nvn+ncx+ncn+1;
for i=1:np
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, ...
density] = posterior_moments(Draws,1,options_.mh_conf_sig);
name = bayestopt_.name{ip};
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
else
name = bayestopt_.name{ip};
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),...
post_mean, ...
hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:), ...
bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,1,type);
end
end
if nvx
type = 'shocks_std';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,2,'standard deviation of structural shocks');
end
ip = 1;
disp(' ')
disp('standard deviation of shocks')
disp(tit2)
for i=1:nvx
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(Draws,1,options_.mh_conf_sig);
k = estim_params_.var_exo(i,1);
name = deblank(M_.exo_names(k,:));
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
M_.Sigma_e(k,k) = post_mean*post_mean;
else
k = estim_params_.var_exo(i,1);
name = deblank(M_.exo_names(k,:));
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),post_mean,hpd_interval,...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,2,'standard deviation of structural shocks');
end
end
if nvn
type = 'measurement_errors_std';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,3,'standard deviation of measurement errors')
end
disp(' ')
disp('standard deviation of measurement errors')
disp(tit2)
ip = nvx+1;
for i=1:nvn
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(Draws,1,options_.mh_conf_sig);
name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
oo_ = Filloo(oo_,name,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
else
name = deblank(options_.varobs(estim_params_.var_endo(i,1),:));
[post_mean,hpd_interval,post_var] = Extractoo(oo_,name,type);
end
disp(sprintf(pformat,name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,3,'standard deviation of measurement errors');
end
end
if ncx
type = 'shocks_corr';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,4,'correlation of structural shocks');
end
disp(' ')
disp('correlation of shocks')
disp(tit2)
ip = nvx+nvn+1;
for i=1:ncx
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(Draws,1,options_.mh_conf_sig);
k1 = estim_params_.corrx(i,1);
k2 = estim_params_.corrx(i,2);
name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,post_median,post_var,post_deciles,density);
M_.Sigma_e(k1,k2) = post_mean*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2));
M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2);
else
k1 = estim_params_.corrx(i,1);
k2 = estim_params_.corrx(i,2);
name = [deblank(M_.exo_names(k1,:)) ',' deblank(M_.exo_names(k2,:))];
NAME = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))];
[post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
end
disp(sprintf(pformat, name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,4,'correlation of structural shocks');
end
end
if ncn
type = 'measurement_errors_corr';
if TeX
fid = TeXBegin(OutputDirectoryName,M_.fname,5,'correlation of measurement errors');
end
disp(' ')
disp('correlation of measurement errors')
disp(tit2)
ip = nvx+nvn+ncx+1;
for i=1:ncn
if options_.mh_replic
Draws = GetAllPosteriorDraws(ip,FirstMhFile,FirstLine,TotalNumberOfMhFiles,NumberOfDraws);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(Draws,1,options_.mh_conf_sig);
k1 = estim_params_.corrn(i,1);
k2 = estim_params_.corrn(i,2);
name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
oo_ = Filloo(oo_,NAME,type,post_mean,hpd_interval,...
post_median,post_var,post_deciles,density);
else
k1 = estim_params_.corrn(i,1);
k2 = estim_params_.corrn(i,2);
name = [deblank(M_.endo_names(k1,:)) ',' deblank(M_.endo_names(k2,:))];
NAME = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))];
[post_mean,hpd_interval,post_var] = Extractoo(oo_,NAME,type);
end
disp(sprintf(pformat, name,bayestopt_.pmean(ip),post_mean,hpd_interval, ...
pnames(bayestopt_.pshape(ip)+1,:),bayestopt_.pstdev(ip)));
if TeX
TeXCore(fid,name,deblank(pnames(bayestopt_.pshape(ip)+1,:)),bayestopt_.pmean(ip),...
bayestopt_.pstdev(ip),post_mean,sqrt(post_var),hpd_interval);
end
ip = ip+1;
end
if TeX
TeXEnd(fid,5,'correlation of measurement errors');
end
end
%
%% subfunctions:
%
function fid = TeXBegin(directory,fname,fnum,title)
TeXfile = [directory '/' fname '_Posterior_Mean_' int2str(fnum) '.TeX'];
fidTeX = fopen(TeXfile,'w');
fprintf(fidTeX,'%% TeX-table generated by Dynare.\n');
fprintf(fidTeX,['%% RESULTS FROM METROPOLIS HASTINGS (' title ')\n']);
fprintf(fidTeX,['%% ' datestr(now,0)]);
fprintf(fidTeX,' \n');
fprintf(fidTeX,' \n');
fprintf(fidTeX,'\\begin{table}\n');
fprintf(fidTeX,'\\centering\n');
fprintf(fidTeX,'\\begin{tabular}{l|lcccccc} \n');
fprintf(fidTeX,'\\hline\\hline \\\\ \n');
fprintf(fidTeX,[' & Prior distribution & Prior mean & Prior ' ...
's.d. & Posterior mean & Posterior s.d. & HPD inf & HPD sup\\\\ \n']);
fprintf(fidTeX,'\\hline \\\\ \n');
fid = fidTeX;
function TeXCore(fid,name,shape,priormean,priorstd,postmean,poststd,hpd)
fprintf(fid,['$%s$ & %s & %7.3f & %6.4f & %7.3f& %6.4f & %7.4f & %7.4f \\\\ \n'],...
name,...
shape,...
priormean,...
priorstd,...
postmean,...
poststd,...
hpd(1),...
hpd(2));
function TeXEnd(fid,fnum,title)
fprintf(fid,'\\hline\\hline \n');
fprintf(fid,'\\end{tabular}\n ');
fprintf(fid,['\\caption{Results from Metropolis-Hastings (' title ')}\n ']);
fprintf(fid,['\\label{Table:MHPosterior:' int2str(fnum) '}\n']);
fprintf(fid,'\\end{table}\n');
fprintf(fid,'%% End of TeX file.\n');
fclose(fid);
function oo = Filloo(oo,name,type,postmean,hpdinterval,postmedian,postvar,postdecile,density)
eval(['oo.posterior_mean.' type '.' name ' = postmean;']);
eval(['oo.posterior_hpdinf.' type '.' name ' = hpdinterval(1);']);
eval(['oo.posterior_hpdsup.' type '.' name ' = hpdinterval(2);']);
eval(['oo.posterior_median.' type '.' name ' = postmedian;']);
eval(['oo.posterior_variance.' type '.' name ' = postvar;']);
eval(['oo.posterior_deciles.' type '.' name ' = postdecile;']);
eval(['oo.posterior_density.' type '.' name ' = density;']);
function [post_mean,hpd_interval,post_var] = Extractoo(oo,name,type)
hpd_interval = zeros(2,1);
eval(['post_mean = oo.posterior_mean.' type '.' name ';']);
eval(['hpd_interval(1) = oo.posterior_hpdinf.' type '.' name ';']);
eval(['hpd_interval(2) = oo.posterior_hpdsup.' type '.' name ';']);
eval(['post_var = oo.posterior_variance.' type '.' name ';']);

View File

@ -1,483 +1,481 @@
function PosteriorIRF(type)
% function PosteriorIRF(type)
% Builds posterior IRFs after the MH algorithm.
%
% INPUTS
% o type [char] string specifying the joint density of the
% deep parameters ('prior','posterior').
%
% OUTPUTS
% None (oo_ and plots).
%
% SPECIAL REQUIREMENTS
% None
%
% part of DYNARE, copyright Dynare Team(2006-2008)
% Gnu Public License.
global options_ estim_params_ oo_ M_ bayestopt_
% Set the number of periods
if isempty(options_.irf) | ~options_.irf
options_.irf = 40;
end
% Set varlist if necessary
varlist = options_.varlist;
if isempty(varlist)
varlist = options_.varobs;
end
options_.varlist = varlist;
nvar = size(varlist,1);
IndxVariables = [];
for i=1:nvar
idx = strmatch(deblank(varlist(i,:)),M_.endo_names,'exact');
if isempty(idx)
disp(['PosteriorIRF :: ' deblank(varlist(i,:)) 'is not a declared endogenous variable!'])
else
IndxVariables = [IndxVariables,idx];
end
end
% Set various parameters & Check or create directories
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
npar = nvx+nvn+ncx+ncn+np;
offset = npar-np;
clear('nvx','nvn','ncx','ncn','np');
nvobs = size(options_.varobs,1);
gend = options_.nobs;
MaxNumberOfPlotPerFigure = 9;
nn = sqrt(MaxNumberOfPlotPerFigure);
MAX_nirfs_dsge = ceil(options_.MaxNumberOfBytes/(options_.irf*nvar*M_.exo_nbr)/8);
MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
MAX_nirfs_dsgevar = ceil(options_.MaxNumberOfBytes/(options_.irf*nvobs*M_.exo_nbr)/8);
else
MAX_nirfs_dsgevar = 0;
end
DirectoryName = CheckPath('Output');
if strcmpi(type,'posterior')
MhDirectoryName = CheckPath('metropolis');
elseif strcmpi(type,'gsa')
MhDirectoryName = CheckPath('GSA');
else
MhDirectoryName = CheckPath('prior');
end
if strcmpi(type,'posterior')
load([ MhDirectoryName '/' M_.fname '_mh_history'])
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
elseif strcmpi(type,'gsa')
load([ MhDirectoryName '/' M_.fname '_prior'],'lpmat0','lpmat','istable')
x=[lpmat0(istable,:) lpmat(istable,:)];
clear lpmat istable
NumberOfDraws=size(x,1);
B=NumberOfDraws; options_.B = B;
else% type = 'prior'
NumberOfDraws = 500;
end
if ~strcmpi(type,'gsa')
B = min([round(.5*NumberOfDraws),500]); options_.B = B;
end
try delete([MhDirectoryName '/' M_.fname '_irf_dsge*.mat'])
catch disp('No _IRFs (dsge) files to be deleted!')
end
try delete([MhDirectoryName '/' M_.fname '_irf_bvardsge*.mat'])
catch disp('No _IRFs (bvar-dsge) files to be deleted!')
end
irun = 0;
IRUN = 0;
irun2 = 0;
NumberOfIRFfiles_dsge = 1;
NumberOfIRFfiles_dsgevar = 1;
ifil2 = 1;
if strcmpi(type,'posterior')
h = waitbar(0,'Bayesian (posterior) IRFs...');
elseif strcmpi(type,'gsa')
h = waitbar(0,'GSA (prior) IRFs...');
else
h = waitbar(0,'Bayesian (prior) IRFs...');
end
% Create arrays
if B <= MAX_nruns
stock_param = zeros(B, npar);
else
stock_param = zeros(MAX_nruns, npar);
end
if B >= MAX_nirfs_dsge
stock_irf_dsge = zeros(options_.irf,nvar,M_.exo_nbr,MAX_nirfs_dsge);
else
stock_irf_dsge = zeros(options_.irf,nvar,M_.exo_nbr,B);
end
if MAX_nirfs_dsgevar
if B >= MAX_nirfs_dsgevar
stock_irf_bvardsge = zeros(options_.irf,nvobs,M_.exo_nbr,MAX_nirfs_dsgevar);
else
stock_irf_bvardsge = zeros(options_.irf,nvobs,M_.exo_nbr,B);
end
[mYY,mXY,mYX,mXX,Ydata,Xdata] = ...
var_sample_moments(options_.first_obs,options_.first_obs+options_.nobs-1,options_.varlag,-1);
NumberOfLags = options_.varlag;
NumberOfLagsTimesNvobs = NumberOfLags*nvobs;
if options_.noconstant
NumberOfParametersPerEquation = NumberOfLagsTimesNvobs;
else
NumberOfParametersPerEquation = NumberOfLagsTimesNvobs+1;
end
Companion_matrix = diag(ones(nvobs*(NumberOfLags-1),1),-nvobs);
end
b = 0;
nosaddle = 0;
while b<=B
b = b + 1;
irun = irun+1;
irun2 = irun2+1;
if ~strcmpi(type,'gsa')
deep = GetOneDraw(type);
else
deep = x(b,:);
end
stock_param(irun2,:) = deep;
set_parameters(deep);
[dr,info] = resol(oo_.steady_state,0);
if info(1)
nosaddle = nosaddle + 1;
b = b - 1;
irun = irun-1;
irun2 = irun2-1;
if info(1) == 1
errordef = 'Static variables are not uniquely defined';
elseif info(1) == 2
errordef = 'Dll problem';
elseif info(1) == 3
errordef = 'No stable trajectory';
elseif info(1) == 4
errordef = 'Indeterminacy';
elseif info(1) == 5
errordef = 'Rank condition is not satisfied';
end
disp(['PosteriorIRF :: Dynare is unable to solve the model (' errordef ')'])
continue
end
SS(M_.exo_names_orig_ord,M_.exo_names_orig_ord) = M_.Sigma_e+1e-14*eye(M_.exo_nbr);
SS = transpose(chol(SS));
for i = 1:M_.exo_nbr
if SS(i,i) > 1e-13
y=irf(dr,SS(M_.exo_names_orig_ord,i), options_.irf, options_.drop,options_.replic,options_.order);
if options_.relative_irf
y = 100*y/cs(i,i);
end
for j = 1:nvar
if max(y(IndxVariables(j),:)) - min(y(IndxVariables(j),:)) > 1e-12
stock_irf_dsge(:,j,i,irun) = transpose(y(IndxVariables(j),:));
end
end
end
end
if MAX_nirfs_dsgevar
IRUN = IRUN+1;
%tmp_dsgevar = zeros(options_.irf,nvobs*M_.exo_nbr);
[fval,cost_flag,info,PHI,SIGMAu,iXX] = DsgeVarLikelihood(deep',gend);
dsge_prior_weight = M_.params(strmatch('dsge_prior_weight',M_.param_names));
DSGE_PRIOR_WEIGHT = floor(gend*(1+dsge_prior_weight));
SIGMA_inv_upper_chol = chol(inv(SIGMAu*gend*(dsge_prior_weight+1)));
explosive_var = 1;
while explosive_var
% draw from the marginal posterior of SIGMA
SIGMAu_draw = rand_inverse_wishart(nvobs, DSGE_PRIOR_WEIGHT-NumberOfParametersPerEquation, ...
SIGMA_inv_upper_chol);
% draw from the conditional posterior of PHI
PHI_draw = rand_matrix_normal(NumberOfParametersPerEquation,nvobs, PHI, ...
chol(SIGMAu_draw)', chol(iXX)');
Companion_matrix(1:nvobs,:) = transpose(PHI_draw(1:NumberOfLagsTimesNvobs,:));
% Check for stationarity
explosive_var = any(abs(eig(Companion_matrix))>1.000000001);
end
% Get the mean
% $$$ if options_.noconstant
mu = zeros(1,nvobs);
% $$$ else
% $$$ AA = eye(nvobs);
% $$$ for lag=1:NumberOfLags
% $$$ AA = AA-PHI_draw((lag-1)*nvobs+1:lag*nvobs,:);
% $$$ end
% $$$ mu = transpose(AA\transpose(PHI_draw(end,:)));
% $$$ end
% Get rotation
if dsge_prior_weight > 0
Atheta(oo_.dr.order_var,M_.exo_names_orig_ord) = oo_.dr.ghu*sqrt(M_.Sigma_e);
A0 = Atheta(bayestopt_.mfys,:);
[OMEGAstar,SIGMAtr] = qr2(A0');
end
SIGMAu_chol = chol(SIGMAu_draw)';
SIGMAtrOMEGA = SIGMAu_chol*OMEGAstar';
PHIpower = eye(NumberOfLagsTimesNvobs);
irfs = zeros (options_.irf,nvobs*M_.exo_nbr);
tmp3 = PHIpower(1:nvobs,1:nvobs)*SIGMAtrOMEGA;
irfs(1,:) = tmp3(:)';
for t = 2:options_.irf
PHIpower = Companion_matrix*PHIpower;
tmp3 = PHIpower(1:nvobs,1:nvobs)*SIGMAtrOMEGA;
irfs(t,:) = tmp3(:)'+kron(ones(1,M_.exo_nbr),mu);
end
tmp_dsgevar = kron(ones(options_.irf,1),mu);
for j = 1:(nvobs*M_.exo_nbr)
if max(irfs(:,j)) - min(irfs(:,j)) > 1e-10
tmp_dsgevar(:,j) = (irfs(:,j));
end
end
if IRUN < MAX_nirfs_dsgevar
stock_irf_bvardsge(:,:,:,IRUN) = reshape(tmp_dsgevar,options_.irf,nvobs,M_.exo_nbr);
else
stock_irf_bvardsge(:,:,:,IRUN) = reshape(tmp_dsgevar,options_.irf,nvobs,M_.exo_nbr);
instr = [MhDirectoryName '/' M_.fname '_irf_bvardsge' ...
int2str(NumberOfIRFfiles_dsgevar) ' stock_irf_bvardsge;'];,
eval(['save ' instr]);
NumberOfIRFfiles_dsgevar = NumberOfIRFfiles_dsgevar+1;
IRUN =0;
stock_irf_dsgevar = zeros(options_.irf,nvobs,M_.exo_nbr,MAX_nirfs_dsgevar);
end
end
if irun == MAX_nirfs_dsge | irun == B | b == B
if b == B
stock_irf_dsge = stock_irf_dsge(:,:,:,1:irun);
if MAX_nirfs_dsgevar & (b == B | IRUN == B)
stock_irf_bvardsge = stock_irf_bvardsge(:,:,:,1:IRUN);
instr = [MhDirectoryName '/' M_.fname '_irf_bvardsge' ...
int2str(NumberOfIRFfiles_dsgevar) ' stock_irf_bvardsge;'];,
eval(['save ' instr]);
NumberOfIRFfiles_dsgevar = NumberOfIRFfiles_dsgevar+1;
irun = 0;
end
end
save([MhDirectoryName '/' M_.fname '_irf_dsge' int2str(NumberOfIRFfiles_dsge)],'stock_irf_dsge');
NumberOfIRFfiles_dsge = NumberOfIRFfiles_dsge+1;
irun = 0;
end
if irun2 == MAX_nruns | b == B
if b == B
stock_param = stock_param(1:irun2,:);
end
stock = stock_param;
save([MhDirectoryName '/' M_.fname '_param_irf' int2str(ifil2)],'stock');
ifil2 = ifil2 + 1;
irun2 = 0;
end
waitbar(b/B,h);
end
if nosaddle
disp(['PosteriorIRF :: Percentage of discarded posterior draws = ' num2str(nosaddle/(B+nosaddle))])
end
close(h);
ReshapeMatFiles('irf_dsge')
if MAX_nirfs_dsgevar
ReshapeMatFiles('irf_bvardsge')
end
if strcmpi(type,'gsa')
return
end
IRF_DSGEs = dir([MhDirectoryName '/' M_.fname '_IRF_DSGEs*.mat']);
NumberOfIRFfiles_dsge = length(IRF_DSGEs);
IRF_BVARDSGEs = dir([MhDirectoryName '/' M_.fname '_IRF_BVARDSGEs*.mat']);
NumberOfIRFfiles_dsgevar = length(IRF_BVARDSGEs);
MeanIRF = zeros(options_.irf,nvar,M_.exo_nbr);
MedianIRF = zeros(options_.irf,nvar,M_.exo_nbr);
VarIRF = zeros(options_.irf,nvar,M_.exo_nbr);
DistribIRF = zeros(options_.irf,9,nvar,M_.exo_nbr);
HPDIRF = zeros(options_.irf,2,nvar,M_.exo_nbr);
if options_.TeX
varlist_TeX = [];
for i=1:nvar
varlist_TeX = strvcat(varlist_TeX,M_.endo_names_tex(IndxVariables(i),:));
end
end
fprintf('MH: Posterior (dsge) IRFs...\n');
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
kdx = 0;
for file = 1:NumberOfIRFfiles_dsge
load([MhDirectoryName '/' M_.fname '_IRF_DSGEs' int2str(file)]);
for i = 1:M_.exo_nbr
for j = 1:nvar
for k = 1:size(STOCK_IRF_DSGE,1)
kk = k+kdx;
[MeanIRF(kk,j,i),MedianIRF(kk,j,i),VarIRF(kk,j,i),HPDIRF(kk,:,j,i),...
DistribIRF(kk,:,j,i)] = posterior_moments(squeeze(STOCK_IRF_DSGE(k,j,i,:)),0);
end
end
end
kdx = kdx + size(STOCK_IRF_DSGE,1);
end
clear STOCK_IRF_DSGE;
for i = 1:M_.exo_nbr
for j = 1:nvar
name = [deblank(M_.endo_names(IndxVariables(j),:)) '_' deblank(tit(i,:))];
eval(['oo_.PosteriorIRF.dsge.Mean.' name ' = MeanIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Median.' name ' = MedianIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Var.' name ' = VarIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Distribution.' name ' = DistribIRF(:,:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.HPDinf.' name ' = HPDIRF(:,1,j,i);']);
eval(['oo_.PosteriorIRF.dsge.HPDsup.' name ' = HPDIRF(:,2,j,i);']);
end
end
if MAX_nirfs_dsgevar
MeanIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
MedianIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
VarIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
DistribIRFdsgevar = zeros(options_.irf,9,nvar,M_.exo_nbr);
HPDIRFdsgevar = zeros(options_.irf,2,nvar,M_.exo_nbr);
fprintf('MH: Posterior (bvar-dsge) IRFs...\n');
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
kdx = 0;
for file = 1:NumberOfIRFfiles_dsgevar
load([MhDirectoryName '/' M_.fname '_IRF_BVARDSGEs' int2str(file)]);
for i = 1:M_.exo_nbr
for j = 1:nvar
for k = 1:size(STOCK_IRF_BVARDSGE,1)
kk = k+kdx;
[MeanIRFdsgevar(kk,j,i),MedianIRFdsgevar(kk,j,i),VarIRFdsgevar(kk,j,i),...
HPDIRFdsgevar(kk,:,j,i),DistribIRFdsgevar(kk,:,j,i)] = ...
posterior_moments(squeeze(STOCK_IRF_BVARDSGE(k,j,i,:)),0);
end
end
end
kdx = kdx + size(STOCK_IRF_BVARDSGE,1);
end
clear STOCK_IRF_BVARDSGE;
for i = 1:M_.exo_nbr
for j = 1:nvar
name = [deblank(M_.endo_names(IndxVariables(j),:)) '_' deblank(tit(i,:))];
eval(['oo_.PosteriorIRF.bvardsge.Mean.' name ' = MeanIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Median.' name ' = MedianIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Var.' name ' = VarIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Distribution.' name ' = DistribIRFdsgevar(:,:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.HPDinf.' name ' = HPDIRFdsgevar(:,1,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.HPDsup.' name ' = HPDIRFdsgevar(:,2,j,i);']);
end
end
end
%%
%% Finally I build the plots.
%%
if options_.TeX
fidTeX = fopen([DirectoryName '/' M_.fname '_BayesianIRF.TeX'],'w');
fprintf(fidTeX,'%% TeX eps-loader file generated by PosteriorIRF.m (Dynare).\n');
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
fprintf(fidTeX,' \n');
titTeX(M_.exo_names_orig_ord,:) = M_.exo_names_tex;
end
%%
subplotnum = 0;
for i=1:M_.exo_nbr
NAMES = [];
if options_.TeX
TEXNAMES = [];
end
figunumber = 0;
for j=1:nvar
if max(abs(MeanIRF(:,j,i))) > 10^(-6)
subplotnum = subplotnum+1;
if options_.nograph
if subplotnum == 1 & options_.relative_irf
hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)],'Visible','off');
elseif subplotnum == 1 & ~options_.relative_irf
hh = figure('Name',['Orthogonalized shock to ' tit(i,:)],'Visible','off');
end
else
if subplotnum == 1 & options_.relative_irf
hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)]);
elseif subplotnum == 1 & ~options_.relative_irf
hh = figure('Name',['Orthogonalized shock to ' tit(i,:)]);
end
end
set(0,'CurrentFigure',hh)
subplot(nn,nn,subplotnum);
if ~MAX_nirfs_dsgevar
h1 = area(1:options_.irf,HPDIRF(:,2,j,i));
set(h1,'FaceColor',[.9 .9 .9]);
set(h1,'BaseValue',min(HPDIRF(:,1,j,i)));
hold on
h2 = area(1:options_.irf,HPDIRF(:,1,j,i),'FaceColor',[1 1 1],'BaseValue',min(HPDIRF(:,1,j,i)));
set(h2,'FaceColor',[1 1 1]);
set(h2,'BaseValue',min(HPDIRF(:,1,j,i)));
plot(1:options_.irf,MeanIRF(:,j,i),'-k','linewidth',3)
% plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
box on
axis tight
xlim([1 options_.irf]);
hold off
else
h1 = area(1:options_.irf,HPDIRF(:,2,j,i));
set(h1,'FaceColor',[.9 .9 .9]);
set(h1,'BaseValue',min([min(HPDIRF(:,1,j,i)),min(HPDIRFdsgevar(:,1,j,i))]));
hold on;
h2 = area(1:options_.irf,HPDIRF(:,1,j,i));
set(h2,'FaceColor',[1 1 1]);
set(h2,'BaseValue',min([min(HPDIRF(:,1,j,i)),min(HPDIRFdsgevar(:,1,j,i))]));
plot(1:options_.irf,MeanIRF(:,j,i),'-k','linewidth',3)
% plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
plot(1:options_.irf,MeanIRFdsgevar(:,j,i),'--k','linewidth',2)
plot(1:options_.irf,HPDIRFdsgevar(:,1,j,i),'--k','linewidth',1)
plot(1:options_.irf,HPDIRFdsgevar(:,2,j,i),'--k','linewidth',1)
box on
axis tight
xlim([1 options_.irf]);
hold off
end
name = deblank(varlist(j,:));
NAMES = strvcat(NAMES,name);
if options_.TeX
texname = deblank(varlist_TeX(j,:));
TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
end
title(name,'Interpreter','none')
end
if subplotnum == MaxNumberOfPlotPerFigure | (j == nvar & subplotnum> 0)
figunumber = figunumber+1;
set(hh,'visible','on')
eval(['print -depsc2 ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
eval(['print -dpdf ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
saveas(hh,[DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber) '.fig']);
set(hh,'visible','off')
if options_.nograph, close(hh), end
if options_.TeX
fprintf(fidTeX,'\\begin{figure}[H]\n');
for jj = 1:size(TEXNAMES,1)
fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
end
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s}\n',M_.fname,deblank(tit(i,:)));
if options_.relative_irf
fprintf(fidTeX,['\\caption{Bayesian relative IRF.}']);
else
fprintf(fidTeX,'\\caption{Bayesian IRF.}');
end
fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s}\n',deblank(tit(i,:)));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
subplotnum = 0;
end
end% loop over selected endo_var
end% loop over exo_var
%%
if options_.TeX
fprintf(fidTeX,'%% End of TeX file.\n');
fclose(fidTeX);
end
function PosteriorIRF(type)
% Builds posterior IRFs after the MH algorithm.
%
% INPUTS
% o type [char] string specifying the joint density of the
% deep parameters ('prior','posterior').
%
% OUTPUTS
% None (oo_ and plots).
%
% SPECIAL REQUIREMENTS
% None
%
% part of DYNARE, copyright Dynare Team(2006-2008)
% Gnu Public License.
global options_ estim_params_ oo_ M_ bayestopt_
% Set the number of periods
if isempty(options_.irf) | ~options_.irf
options_.irf = 40;
end
% Set varlist if necessary
varlist = options_.varlist;
if isempty(varlist)
varlist = options_.varobs;
end
options_.varlist = varlist;
nvar = size(varlist,1);
IndxVariables = [];
for i=1:nvar
idx = strmatch(deblank(varlist(i,:)),M_.endo_names,'exact');
if isempty(idx)
disp(['PosteriorIRF :: ' deblank(varlist(i,:)) 'is not a declared endogenous variable!'])
else
IndxVariables = [IndxVariables,idx];
end
end
% Set various parameters & Check or create directories
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
npar = nvx+nvn+ncx+ncn+np;
offset = npar-np;
clear('nvx','nvn','ncx','ncn','np');
nvobs = size(options_.varobs,1);
gend = options_.nobs;
MaxNumberOfPlotPerFigure = 9;
nn = sqrt(MaxNumberOfPlotPerFigure);
MAX_nirfs_dsge = ceil(options_.MaxNumberOfBytes/(options_.irf*nvar*M_.exo_nbr)/8);
MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
MAX_nirfs_dsgevar = ceil(options_.MaxNumberOfBytes/(options_.irf*nvobs*M_.exo_nbr)/8);
else
MAX_nirfs_dsgevar = 0;
end
DirectoryName = CheckPath('Output');
if strcmpi(type,'posterior')
MhDirectoryName = CheckPath('metropolis');
elseif strcmpi(type,'gsa')
MhDirectoryName = CheckPath('GSA');
else
MhDirectoryName = CheckPath('prior');
end
if strcmpi(type,'posterior')
load([ MhDirectoryName '/' M_.fname '_mh_history'])
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
elseif strcmpi(type,'gsa')
load([ MhDirectoryName '/' M_.fname '_prior'],'lpmat0','lpmat','istable')
x=[lpmat0(istable,:) lpmat(istable,:)];
clear lpmat istable
NumberOfDraws=size(x,1);
B=NumberOfDraws; options_.B = B;
else% type = 'prior'
NumberOfDraws = 500;
end
if ~strcmpi(type,'gsa')
B = min([round(.5*NumberOfDraws),500]); options_.B = B;
end
try delete([MhDirectoryName '/' M_.fname '_irf_dsge*.mat'])
catch disp('No _IRFs (dsge) files to be deleted!')
end
try delete([MhDirectoryName '/' M_.fname '_irf_bvardsge*.mat'])
catch disp('No _IRFs (bvar-dsge) files to be deleted!')
end
irun = 0;
IRUN = 0;
irun2 = 0;
NumberOfIRFfiles_dsge = 1;
NumberOfIRFfiles_dsgevar = 1;
ifil2 = 1;
if strcmpi(type,'posterior')
h = waitbar(0,'Bayesian (posterior) IRFs...');
elseif strcmpi(type,'gsa')
h = waitbar(0,'GSA (prior) IRFs...');
else
h = waitbar(0,'Bayesian (prior) IRFs...');
end
% Create arrays
if B <= MAX_nruns
stock_param = zeros(B, npar);
else
stock_param = zeros(MAX_nruns, npar);
end
if B >= MAX_nirfs_dsge
stock_irf_dsge = zeros(options_.irf,nvar,M_.exo_nbr,MAX_nirfs_dsge);
else
stock_irf_dsge = zeros(options_.irf,nvar,M_.exo_nbr,B);
end
if MAX_nirfs_dsgevar
if B >= MAX_nirfs_dsgevar
stock_irf_bvardsge = zeros(options_.irf,nvobs,M_.exo_nbr,MAX_nirfs_dsgevar);
else
stock_irf_bvardsge = zeros(options_.irf,nvobs,M_.exo_nbr,B);
end
[mYY,mXY,mYX,mXX,Ydata,Xdata] = ...
var_sample_moments(options_.first_obs,options_.first_obs+options_.nobs-1,options_.varlag,-1);
NumberOfLags = options_.varlag;
NumberOfLagsTimesNvobs = NumberOfLags*nvobs;
if options_.noconstant
NumberOfParametersPerEquation = NumberOfLagsTimesNvobs;
else
NumberOfParametersPerEquation = NumberOfLagsTimesNvobs+1;
end
Companion_matrix = diag(ones(nvobs*(NumberOfLags-1),1),-nvobs);
end
b = 0;
nosaddle = 0;
while b<=B
b = b + 1;
irun = irun+1;
irun2 = irun2+1;
if ~strcmpi(type,'gsa')
deep = GetOneDraw(type);
else
deep = x(b,:);
end
stock_param(irun2,:) = deep;
set_parameters(deep);
[dr,info] = resol(oo_.steady_state,0);
if info(1)
nosaddle = nosaddle + 1;
b = b - 1;
irun = irun-1;
irun2 = irun2-1;
if info(1) == 1
errordef = 'Static variables are not uniquely defined';
elseif info(1) == 2
errordef = 'Dll problem';
elseif info(1) == 3
errordef = 'No stable trajectory';
elseif info(1) == 4
errordef = 'Indeterminacy';
elseif info(1) == 5
errordef = 'Rank condition is not satisfied';
end
disp(['PosteriorIRF :: Dynare is unable to solve the model (' errordef ')'])
continue
end
SS(M_.exo_names_orig_ord,M_.exo_names_orig_ord) = M_.Sigma_e+1e-14*eye(M_.exo_nbr);
SS = transpose(chol(SS));
for i = 1:M_.exo_nbr
if SS(i,i) > 1e-13
y=irf(dr,SS(M_.exo_names_orig_ord,i), options_.irf, options_.drop,options_.replic,options_.order);
if options_.relative_irf
y = 100*y/cs(i,i);
end
for j = 1:nvar
if max(y(IndxVariables(j),:)) - min(y(IndxVariables(j),:)) > 1e-12
stock_irf_dsge(:,j,i,irun) = transpose(y(IndxVariables(j),:));
end
end
end
end
if MAX_nirfs_dsgevar
IRUN = IRUN+1;
%tmp_dsgevar = zeros(options_.irf,nvobs*M_.exo_nbr);
[fval,cost_flag,info,PHI,SIGMAu,iXX] = DsgeVarLikelihood(deep',gend);
dsge_prior_weight = M_.params(strmatch('dsge_prior_weight',M_.param_names));
DSGE_PRIOR_WEIGHT = floor(gend*(1+dsge_prior_weight));
SIGMA_inv_upper_chol = chol(inv(SIGMAu*gend*(dsge_prior_weight+1)));
explosive_var = 1;
while explosive_var
% draw from the marginal posterior of SIGMA
SIGMAu_draw = rand_inverse_wishart(nvobs, DSGE_PRIOR_WEIGHT-NumberOfParametersPerEquation, ...
SIGMA_inv_upper_chol);
% draw from the conditional posterior of PHI
PHI_draw = rand_matrix_normal(NumberOfParametersPerEquation,nvobs, PHI, ...
chol(SIGMAu_draw)', chol(iXX)');
Companion_matrix(1:nvobs,:) = transpose(PHI_draw(1:NumberOfLagsTimesNvobs,:));
% Check for stationarity
explosive_var = any(abs(eig(Companion_matrix))>1.000000001);
end
% Get the mean
% $$$ if options_.noconstant
mu = zeros(1,nvobs);
% $$$ else
% $$$ AA = eye(nvobs);
% $$$ for lag=1:NumberOfLags
% $$$ AA = AA-PHI_draw((lag-1)*nvobs+1:lag*nvobs,:);
% $$$ end
% $$$ mu = transpose(AA\transpose(PHI_draw(end,:)));
% $$$ end
% Get rotation
if dsge_prior_weight > 0
Atheta(oo_.dr.order_var,M_.exo_names_orig_ord) = oo_.dr.ghu*sqrt(M_.Sigma_e);
A0 = Atheta(bayestopt_.mfys,:);
[OMEGAstar,SIGMAtr] = qr2(A0');
end
SIGMAu_chol = chol(SIGMAu_draw)';
SIGMAtrOMEGA = SIGMAu_chol*OMEGAstar';
PHIpower = eye(NumberOfLagsTimesNvobs);
irfs = zeros (options_.irf,nvobs*M_.exo_nbr);
tmp3 = PHIpower(1:nvobs,1:nvobs)*SIGMAtrOMEGA;
irfs(1,:) = tmp3(:)';
for t = 2:options_.irf
PHIpower = Companion_matrix*PHIpower;
tmp3 = PHIpower(1:nvobs,1:nvobs)*SIGMAtrOMEGA;
irfs(t,:) = tmp3(:)'+kron(ones(1,M_.exo_nbr),mu);
end
tmp_dsgevar = kron(ones(options_.irf,1),mu);
for j = 1:(nvobs*M_.exo_nbr)
if max(irfs(:,j)) - min(irfs(:,j)) > 1e-10
tmp_dsgevar(:,j) = (irfs(:,j));
end
end
if IRUN < MAX_nirfs_dsgevar
stock_irf_bvardsge(:,:,:,IRUN) = reshape(tmp_dsgevar,options_.irf,nvobs,M_.exo_nbr);
else
stock_irf_bvardsge(:,:,:,IRUN) = reshape(tmp_dsgevar,options_.irf,nvobs,M_.exo_nbr);
instr = [MhDirectoryName '/' M_.fname '_irf_bvardsge' ...
int2str(NumberOfIRFfiles_dsgevar) ' stock_irf_bvardsge;'];,
eval(['save ' instr]);
NumberOfIRFfiles_dsgevar = NumberOfIRFfiles_dsgevar+1;
IRUN =0;
stock_irf_dsgevar = zeros(options_.irf,nvobs,M_.exo_nbr,MAX_nirfs_dsgevar);
end
end
if irun == MAX_nirfs_dsge | irun == B | b == B
if b == B
stock_irf_dsge = stock_irf_dsge(:,:,:,1:irun);
if MAX_nirfs_dsgevar & (b == B | IRUN == B)
stock_irf_bvardsge = stock_irf_bvardsge(:,:,:,1:IRUN);
instr = [MhDirectoryName '/' M_.fname '_irf_bvardsge' ...
int2str(NumberOfIRFfiles_dsgevar) ' stock_irf_bvardsge;'];,
eval(['save ' instr]);
NumberOfIRFfiles_dsgevar = NumberOfIRFfiles_dsgevar+1;
irun = 0;
end
end
save([MhDirectoryName '/' M_.fname '_irf_dsge' int2str(NumberOfIRFfiles_dsge)],'stock_irf_dsge');
NumberOfIRFfiles_dsge = NumberOfIRFfiles_dsge+1;
irun = 0;
end
if irun2 == MAX_nruns | b == B
if b == B
stock_param = stock_param(1:irun2,:);
end
stock = stock_param;
save([MhDirectoryName '/' M_.fname '_param_irf' int2str(ifil2)],'stock');
ifil2 = ifil2 + 1;
irun2 = 0;
end
waitbar(b/B,h);
end
if nosaddle
disp(['PosteriorIRF :: Percentage of discarded posterior draws = ' num2str(nosaddle/(B+nosaddle))])
end
close(h);
ReshapeMatFiles('irf_dsge')
if MAX_nirfs_dsgevar
ReshapeMatFiles('irf_bvardsge')
end
if strcmpi(type,'gsa')
return
end
IRF_DSGEs = dir([MhDirectoryName '/' M_.fname '_IRF_DSGEs*.mat']);
NumberOfIRFfiles_dsge = length(IRF_DSGEs);
IRF_BVARDSGEs = dir([MhDirectoryName '/' M_.fname '_IRF_BVARDSGEs*.mat']);
NumberOfIRFfiles_dsgevar = length(IRF_BVARDSGEs);
MeanIRF = zeros(options_.irf,nvar,M_.exo_nbr);
MedianIRF = zeros(options_.irf,nvar,M_.exo_nbr);
VarIRF = zeros(options_.irf,nvar,M_.exo_nbr);
DistribIRF = zeros(options_.irf,9,nvar,M_.exo_nbr);
HPDIRF = zeros(options_.irf,2,nvar,M_.exo_nbr);
if options_.TeX
varlist_TeX = [];
for i=1:nvar
varlist_TeX = strvcat(varlist_TeX,M_.endo_names_tex(IndxVariables(i),:));
end
end
fprintf('MH: Posterior (dsge) IRFs...\n');
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
kdx = 0;
for file = 1:NumberOfIRFfiles_dsge
load([MhDirectoryName '/' M_.fname '_IRF_DSGEs' int2str(file)]);
for i = 1:M_.exo_nbr
for j = 1:nvar
for k = 1:size(STOCK_IRF_DSGE,1)
kk = k+kdx;
[MeanIRF(kk,j,i),MedianIRF(kk,j,i),VarIRF(kk,j,i),HPDIRF(kk,:,j,i),...
DistribIRF(kk,:,j,i)] = posterior_moments(squeeze(STOCK_IRF_DSGE(k,j,i,:)),0,options_.mh_conf_sig);
end
end
end
kdx = kdx + size(STOCK_IRF_DSGE,1);
end
clear STOCK_IRF_DSGE;
for i = 1:M_.exo_nbr
for j = 1:nvar
name = [deblank(M_.endo_names(IndxVariables(j),:)) '_' deblank(tit(i,:))];
eval(['oo_.PosteriorIRF.dsge.Mean.' name ' = MeanIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Median.' name ' = MedianIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Var.' name ' = VarIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.Distribution.' name ' = DistribIRF(:,:,j,i);']);
eval(['oo_.PosteriorIRF.dsge.HPDinf.' name ' = HPDIRF(:,1,j,i);']);
eval(['oo_.PosteriorIRF.dsge.HPDsup.' name ' = HPDIRF(:,2,j,i);']);
end
end
if MAX_nirfs_dsgevar
MeanIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
MedianIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
VarIRFdsgevar = zeros(options_.irf,nvar,M_.exo_nbr);
DistribIRFdsgevar = zeros(options_.irf,9,nvar,M_.exo_nbr);
HPDIRFdsgevar = zeros(options_.irf,2,nvar,M_.exo_nbr);
fprintf('MH: Posterior (bvar-dsge) IRFs...\n');
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
kdx = 0;
for file = 1:NumberOfIRFfiles_dsgevar
load([MhDirectoryName '/' M_.fname '_IRF_BVARDSGEs' int2str(file)]);
for i = 1:M_.exo_nbr
for j = 1:nvar
for k = 1:size(STOCK_IRF_BVARDSGE,1)
kk = k+kdx;
[MeanIRFdsgevar(kk,j,i),MedianIRFdsgevar(kk,j,i),VarIRFdsgevar(kk,j,i),...
HPDIRFdsgevar(kk,:,j,i),DistribIRFdsgevar(kk,:,j,i)] = ...
posterior_moments(squeeze(STOCK_IRF_BVARDSGE(k,j,i,:)),0,options_.mh_conf_sig);
end
end
end
kdx = kdx + size(STOCK_IRF_BVARDSGE,1);
end
clear STOCK_IRF_BVARDSGE;
for i = 1:M_.exo_nbr
for j = 1:nvar
name = [deblank(M_.endo_names(IndxVariables(j),:)) '_' deblank(tit(i,:))];
eval(['oo_.PosteriorIRF.bvardsge.Mean.' name ' = MeanIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Median.' name ' = MedianIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Var.' name ' = VarIRFdsgevar(:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.Distribution.' name ' = DistribIRFdsgevar(:,:,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.HPDinf.' name ' = HPDIRFdsgevar(:,1,j,i);']);
eval(['oo_.PosteriorIRF.bvardsge.HPDsup.' name ' = HPDIRFdsgevar(:,2,j,i);']);
end
end
end
%%
%% Finally I build the plots.
%%
if options_.TeX
fidTeX = fopen([DirectoryName '/' M_.fname '_BayesianIRF.TeX'],'w');
fprintf(fidTeX,'%% TeX eps-loader file generated by PosteriorIRF.m (Dynare).\n');
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
fprintf(fidTeX,' \n');
titTeX(M_.exo_names_orig_ord,:) = M_.exo_names_tex;
end
%%
subplotnum = 0;
for i=1:M_.exo_nbr
NAMES = [];
if options_.TeX
TEXNAMES = [];
end
figunumber = 0;
for j=1:nvar
if max(abs(MeanIRF(:,j,i))) > 10^(-6)
subplotnum = subplotnum+1;
if options_.nograph
if subplotnum == 1 & options_.relative_irf
hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)],'Visible','off');
elseif subplotnum == 1 & ~options_.relative_irf
hh = figure('Name',['Orthogonalized shock to ' tit(i,:)],'Visible','off');
end
else
if subplotnum == 1 & options_.relative_irf
hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)]);
elseif subplotnum == 1 & ~options_.relative_irf
hh = figure('Name',['Orthogonalized shock to ' tit(i,:)]);
end
end
set(0,'CurrentFigure',hh)
subplot(nn,nn,subplotnum);
if ~MAX_nirfs_dsgevar
h1 = area(1:options_.irf,HPDIRF(:,2,j,i));
set(h1,'FaceColor',[.9 .9 .9]);
set(h1,'BaseValue',min(HPDIRF(:,1,j,i)));
hold on
h2 = area(1:options_.irf,HPDIRF(:,1,j,i),'FaceColor',[1 1 1],'BaseValue',min(HPDIRF(:,1,j,i)));
set(h2,'FaceColor',[1 1 1]);
set(h2,'BaseValue',min(HPDIRF(:,1,j,i)));
plot(1:options_.irf,MeanIRF(:,j,i),'-k','linewidth',3)
% plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
box on
axis tight
xlim([1 options_.irf]);
hold off
else
h1 = area(1:options_.irf,HPDIRF(:,2,j,i));
set(h1,'FaceColor',[.9 .9 .9]);
set(h1,'BaseValue',min([min(HPDIRF(:,1,j,i)),min(HPDIRFdsgevar(:,1,j,i))]));
hold on;
h2 = area(1:options_.irf,HPDIRF(:,1,j,i));
set(h2,'FaceColor',[1 1 1]);
set(h2,'BaseValue',min([min(HPDIRF(:,1,j,i)),min(HPDIRFdsgevar(:,1,j,i))]));
plot(1:options_.irf,MeanIRF(:,j,i),'-k','linewidth',3)
% plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
plot(1:options_.irf,MeanIRFdsgevar(:,j,i),'--k','linewidth',2)
plot(1:options_.irf,HPDIRFdsgevar(:,1,j,i),'--k','linewidth',1)
plot(1:options_.irf,HPDIRFdsgevar(:,2,j,i),'--k','linewidth',1)
box on
axis tight
xlim([1 options_.irf]);
hold off
end
name = deblank(varlist(j,:));
NAMES = strvcat(NAMES,name);
if options_.TeX
texname = deblank(varlist_TeX(j,:));
TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
end
title(name,'Interpreter','none')
end
if subplotnum == MaxNumberOfPlotPerFigure | (j == nvar & subplotnum> 0)
figunumber = figunumber+1;
set(hh,'visible','on')
eval(['print -depsc2 ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
eval(['print -dpdf ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
saveas(hh,[DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber) '.fig']);
set(hh,'visible','off')
if options_.nograph, close(hh), end
if options_.TeX
fprintf(fidTeX,'\\begin{figure}[H]\n');
for jj = 1:size(TEXNAMES,1)
fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
end
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s}\n',M_.fname,deblank(tit(i,:)));
if options_.relative_irf
fprintf(fidTeX,['\\caption{Bayesian relative IRF.}']);
else
fprintf(fidTeX,'\\caption{Bayesian IRF.}');
end
fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s}\n',deblank(tit(i,:)));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
subplotnum = 0;
end
end% loop over selected endo_var
end% loop over exo_var
%%
if options_.TeX
fprintf(fidTeX,'%% End of TeX file.\n');
fclose(fidTeX);
end
fprintf('MH: Posterior IRFs, done!\n');

View File

@ -1,6 +1,4 @@
function dsge_posterior_theoretical_covariance()
% function dsge_posterior_theoretical_covariance()
% This function estimates the posterior density of the endogenous
% variables second order moments.
%
@ -11,7 +9,14 @@ function dsge_posterior_theoretical_covariance()
% None.
%
% SPECIAL REQUIREMENTS
% None.
% 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.
@ -41,6 +46,11 @@ if ~rows(DrawsFiles)
DrawsFiles = dir([fname '_' type '_draws*']);
end
% Get the number of stationary endogenous variables.
nvar = length(ivar);
nar = options_.ar;% Saves size of the auto-correlation function.
options_.ar = 0;% Set the size of the auto-correlation function.
@ -104,7 +114,8 @@ for i=1:nvar
tmp(i1:i2) = Covariance_matrix(:,idx(i,j,nvar));
i1 = i2+1;
end
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(tmp,1);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(tmp,1,options_.mh_conf_sig);
name = fieldname(i,j,vartan);
eval(['oo_.PosteriorTheoreticalMoments.dsge.covariance.mean.' name ' = post_mean;']);
eval(['oo_.PosteriorTheoreticalMoments.dsge.covariance.median.' name ' = post_median;']);

View File

@ -1,6 +1,4 @@
function dsge_posterior_theoretical_variance_decomposition()
% function dsge_posterior_theoretical_variance_decomposition()
% This function estimates the posterior distribution of the variance
% decomposition of the observed endogenous variables.
%
@ -11,7 +9,13 @@ function dsge_posterior_theoretical_variance_decomposition()
% None.
%
% SPECIAL REQUIREMENTS
% None.
% 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.
@ -133,7 +137,8 @@ for i=1:nvar
post_deciles = NaN(9,1);
density = NaN;
else
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(tmp,1);
[post_mean, post_median, post_var, hpd_interval, post_deciles, density] = ...
posterior_moments(tmp,1,options_.mh_conf_sig);
end
eval(['oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.mean.' name ' = post_mean;']);

View File

@ -1,72 +1,73 @@
function [abscissa,f] = kernel_density_estimate(data,number_of_grid_points,bandwidth,kernel_function)
% function [abscissa,f] = kernel_density_estimate(data,number_of_grid_points,bandwidth,kernel_function)
function [abscissa,f] = kernel_density_estimate(data,number_of_grid_points,number_of_draws,bandwidth,kernel_function)
% Estimates a continuous density.
%
% INPUTS
% data: data
% number_of_grid_points: number of grid points
% bandwidth: scalar equals to 0,-1 or -2. For a value different from 0,-1 or -2 the
% function will return optimal_bandwidth = bandwidth.
% kernel_function: 'gaussian','uniform','triangle','epanechnikov',
% 'quartic','triweight','cosinus'.
% data [double] Vector (number_of_draws*1) of draws.
% number_of_grid_points [integer] Scalar, number of points where the density is estimated.
% This (positive) integer must be a power of two.
% number_of_draws [integer] Scalar, number of draws.
% bandwidth [double] Real positive scalar.
% kernel_function [string] Name of the kernel function: 'gaussian', 'triweight',
% 'uniform', 'triangle', 'epanechnikov', 'quartic',
% 'triweight' and 'cosinus'
%
% OUTPUTS
% abscissa: value on the abscissa axis
% f: density
% abscissa [double] Vector (number_of_grid_points*1) of values on the abscissa axis.
% f: [double] Vector (number_of_grid_points*1) of values on the ordinate axis,
% (density estimates).
%
% SPECIAL REQUIREMENTS
% none.
%
% REFERENCES
% A kernel density estimator is used (see Silverman [1986], "Density estimation for statistics and data analysis")
% The code is adapted from Anders Holtsberg's matlab toolbox (stixbox).
%
% part of DYNARE, copyright Dynare Team (2004-2008)
% Gnu Public License.
if size(data,2) > 1 & size(data,1) == 1
data = transpose(data);
elseif size(data,2)>1 & size(data,1)>1
error('kernel_density_estimate :: data must be a one dimensional array.');
if min(size(data))>1
error('kernel_density_estimate:: data must be a one dimensional array.');
else
data = data(:);
end
test = log(number_of_grid_points)/log(2);
if (abs(test-round(test)) > 10^(-12))
error('kernel_density_estimate :: The number of grid points must be a power of 2.');
if (abs(test-round(test)) > 1e-12)
error('kernel_density_estimate:: The number of grid points must be a power of 2.');
end
n = size(data,1);
%% KERNEL SPECIFICATION...
%% Kernel specification.
if strcmpi(kernel_function,'gaussian')
k = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)');
kernel = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)');
elseif strcmpi(kernel_function,'uniform')
k = inline('0.5*(abs(x) <= 1)');
kernel = inline('0.5*(abs(x) <= 1)');
elseif strcmpi(kernel_function,'triangle')
k = inline('(1-abs(x)).*(abs(x) <= 1)');
kernel = inline('(1-abs(x)).*(abs(x) <= 1)');
elseif strcmpi(kernel_function,'epanechnikov')
k = inline('0.75*(1-x.^2).*(abs(x) <= 1)');
kernel = inline('0.75*(1-x.^2).*(abs(x) <= 1)');
elseif strcmpi(kernel_function,'quartic')
k = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)');
kernel = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)');
elseif strcmpi(kernel_function,'triweight')
k = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)');
kernel = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)');
elseif strcmpi(kernel_function,'cosinus')
k = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)');
kernel = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)');
end
%% COMPUTE DENSITY ESTIMATE... Gaussian kernel should be used (FFT).
a = min(data) - (max(data)-min(data))/3;
b = max(data) + (max(data)-min(data))/3;
abscissa = linspace(a,b,number_of_grid_points)';
d = abscissa(2)-abscissa(1);
xi = zeros(number_of_grid_points,1);
xa = (data-a)/(b-a)*number_of_grid_points;
for i = 1:n;
%% Non parametric estimation (Gaussian kernel should be used (FFT)).
lower_bound = min(data) - (max(data)-min(data))/3;
upper_bound = max(data) + (max(data)-min(data))/3;
abscissa = linspace(lower_bound,upper_bound,number_of_grid_points)';
inc = abscissa(2)-abscissa(1);
xi = zeros(number_of_grid_points,1);
xa = (data-lower_bound)/(upper_bound-lower_bound)*number_of_grid_points;
for i = 1:number_of_draws
indx = floor(xa(i));
temp = xa(i)-indx;
xi(indx+[1 2]) = xi(indx+[1 2]) + [1-temp,temp]';
end;
xk = [-number_of_grid_points:number_of_grid_points-1]'*d;
kk = k(xk/bandwidth);
kk = kk / (sum(kk)*d*n);
f = ifft(fft(fftshift(kk)).*fft([xi ;zeros(size(xi))]));
end
xk = [-number_of_grid_points:number_of_grid_points-1]'*inc;
kk = kernel(xk/bandwidth);
kk = kk / (sum(kk)*inc*number_of_draws);
f = ifft(fft(fftshift(kk)).*fft([ xi ; zeros(number_of_grid_points,1) ]));
f = real(f(1:number_of_grid_points));

View File

@ -1,159 +1,165 @@
function optimal_bandwidth = mh_optimal_bandwidth(data,n,bandwidth,kernel_function)
% function optimal_bandwidth = mh_optimal_bandwidth(data,n,bandwidth,kernel_function)
function optimal_bandwidth = mh_optimal_bandwidth(data,number_of_draws,bandwidth,kernel_function)
% This function gives the optimal bandwidth parameter of a kernel estimator
% used to estimate a posterior univariate density from realisations of a
% Metropolis-Hastings algorithm.
%
% INPUTS:
% data: vector with n elements
% n: number of observations
% bandwidth: scalar equal to 0,-1 or -2. For a value different from 0,-1 or -2 the
% function will return optimal_bandwidth = bandwidth
% kernel_function: 'gaussian','uniform','triangle','epanechnikov', 'quartic','triweight','cosinus'
% data [double] Vector (number_of_draws*1) of draws.
% number_of_draws [integer] Scalar, number of draws.
% bandwidth [integer] Scalar equal to 0,-1 or -2.
% bandwidth = 0 => Silverman [1986] rule of thumb.
% bandwidth = -1 => Sheather and Jones [1991].
% bandwidth = -2 => Local bandwith parameters.
% kernel_function [string] Name of the kernel function: 'gaussian', 'triweight',
% 'uniform', 'triangle', 'epanechnikov', 'quartic',
% 'triweight' and 'cosinus'
%
% OUTPUTS:
% optimal_bandwidth: optimal window width (kernel smoothing)
% optimal_bandwidth: [double] Scalar or vector, optimal window width.
%
% SPECIAL REQUIREMENTS
% M. Skold and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm".
% Silverman [1986], "Density estimation for statistics and data analysis".
% SPECIAL REQUIREMENTS:
% none.
%
% REFERENCES:
% [1] M. Skold and G.O. Roberts [2003], "Density estimation for the Metropolis-Hastings algorithm".
% [2] Silverman [1986], "Density estimation for statistics and data analysis".
%
% part of DYNARE, copyright Dynare Team (2004-2007)
% part of DYNARE, copyright Dynare Team (2004-2008)
% Gnu Public License.
%% KERNEL SPECIFICATION...
if strcmpi(kernel_function,'gaussian')
k = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)');
k2 = inline('inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2))'); % second derivate of the gaussian kernel
k4 = inline('inv(sqrt(2*pi))*(3*exp(-0.5*x.^2)-6*(x.^2).*exp(-0.5*x.^2)+(x.^4).*exp(-0.5*x.^2))'); % fourth derivate...
k6 = inline('inv(sqrt(2*pi))*(-15*exp(-0.5*x.^2)+45*(x.^2).*exp(-0.5*x.^2)-15*(x.^4).*exp(-0.5*x.^2)+(x.^6).*exp(-0.5*x.^2))'); % sixth derivate...
mu02 = inv(2*sqrt(pi));
mu21 = 1;
%% Kernel specifications.
if strcmpi(kernel_function,'gaussian')
% Kernel definition
k = inline('inv(sqrt(2*pi))*exp(-0.5*x.^2)');
% Second derivate of the kernel function
k2 = inline('inv(sqrt(2*pi))*(-exp(-0.5*x.^2)+(x.^2).*exp(-0.5*x.^2))');
% Fourth derivate of the kernel function
k4 = inline('inv(sqrt(2*pi))*(3*exp(-0.5*x.^2)-6*(x.^2).*exp(-0.5*x.^2)+(x.^4).*exp(-0.5*x.^2))');
% Sixth derivate of the kernel function
k6 = inline(['inv(sqrt(2*pi))*(-15*exp(-0.5*x.^2)+45*(x.^2).*exp(-' ...
'0.5*x.^2)-15*(x.^4).*exp(-0.5*x.^2)+(x.^6).*exp(-0.5*x.^2))']);
mu02 = inv(2*sqrt(pi));
mu21 = 1;
elseif strcmpi(kernel_function,'uniform')
k = inline('0.5*(abs(x) <= 1)');
mu02 = 0.5;
mu21 = 1/3;
k = inline('0.5*(abs(x) <= 1)');
mu02 = 0.5;
mu21 = 1/3;
elseif strcmpi(kernel_function,'triangle')
k = inline('(1-abs(x)).*(abs(x) <= 1)');
mu02 = 2/3;
mu21 = 1/6;
k = inline('(1-abs(x)).*(abs(x) <= 1)');
mu02 = 2/3;
mu21 = 1/6;
elseif strcmpi(kernel_function,'epanechnikov')
k = inline('0.75*(1-x.^2).*(abs(x) <= 1)');
mu02 = 3/5;
mu21 = 1/5;
k = inline('0.75*(1-x.^2).*(abs(x) <= 1)');
mu02 = 3/5;
mu21 = 1/5;
elseif strcmpi(kernel_function,'quartic')
k = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)');
mu02 = 15/21;
mu21 = 1/7;
k = inline('0.9375*((1-x.^2).^2).*(abs(x) <= 1)');
mu02 = 15/21;
mu21 = 1/7;
elseif strcmpi(kernel_function,'triweight')
k = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)');
k2 = inline('(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1)');
k4 = inline('(-1575/4*x.^2+315/4).*(abs(x) <= 1)');
k6 = inline('(-1575/2).*(abs(x) <= 1)');
mu02 = 350/429;
mu21 = 1/9;
k = inline('1.09375*((1-x.^2).^3).*(abs(x) <= 1)');
k2 = inline('(105/4*(1-x.^2).*x.^2-105/16*(1-x.^2).^2).*(abs(x) <= 1)');
k4 = inline('(-1575/4*x.^2+315/4).*(abs(x) <= 1)');
k6 = inline('(-1575/2).*(abs(x) <= 1)');
mu02 = 350/429;
mu21 = 1/9;
elseif strcmpi(kernel_function,'cosinus')
k = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)');
k2 = inline('(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1)');
k4 = inline('(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1)');
k6 = inline('(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1)');
mu02 = (pi^2)/16;
mu21 = (pi^2-8)/pi^2;
k = inline('(pi/4)*cos((pi/2)*x).*(abs(x) <= 1)');
k2 = inline('(-1/16*cos(pi*x/2)*pi^3).*(abs(x) <= 1)');
k4 = inline('(1/64*cos(pi*x/2)*pi^5).*(abs(x) <= 1)');
k6 = inline('(-1/256*cos(pi*x/2)*pi^7).*(abs(x) <= 1)');
mu02 = (pi^2)/16;
mu21 = (pi^2-8)/pi^2;
else
disp('mh_optimal_bandwidth :: ');
error('This kernel function is not yet implemented in Dynare!');
end
disp('mh_optimal_bandwidth:: ');
error('This kernel function is not yet implemented in Dynare!');
end
%% OPTIMAL BANDWIDTH PARAMETER....
if bandwidth == 0; % Rule of thumb bandwidth parameter (Silverman [1986] corrected by
% Skold and Roberts [2003] for Metropolis-Hastings).
sigma = std(data);
h = 2*sigma*(sqrt(pi)*mu02/(12*(mu21^2)*n))^(1/5); % Silverman's optimal bandwidth parameter.
A = 0;
for i=1:n;
j = i;
while j<= n & data(j,1)==data(i,1);
j = j+1;
end;
A = A + 2*(j-i) - 1;
end;
A = A/n;
h = h*A^(1/5); % correction
elseif bandwidth == -1; % Adaptation of the Sheather and Jones [1991] plug-in estimation of the optimal bandwidth
% parameter for metropolis hastings algorithm.
%% Get the Skold and Roberts' correction.
if bandwidth==0 | bandwidth==-1
correction = correction_for_repeated_draws(data,number_of_draws);
else
correction = 0;
end
%% Compute the standard deviation of the draws.
sigma = std(data);
%% Optimal bandwidth parameter.
if bandwidth == 0; % Rule of thumb bandwidth parameter (Silverman [1986].
h = 2*sigma*(sqrt(pi)*mu02/(12*(mu21^2)*number_of_draws))^(1/5);
h = h*correction^(1/5);
elseif bandwidth == -1; % Sheather and Jones [1991] plug-in estimation of the optimal bandwidth parameter.
if strcmp(kernel_function,'uniform') | ...
strcmp(kernel_function,'triangle') | ...
strcmp(kernel_function,'epanechnikov') | ...
strcmp(kernel_function,'quartic');
error('I can''t compute the optimal bandwidth with this kernel... Try the gaussian, triweight or cosinus kernels.');
end;
sigma = std(data);
A = 0;
for i=1:n;
j = i;
while j<= n & data(j,1)==data(i,1);
j = j+1;
end;
A = A + 2*(j-i) - 1;
end;
A = A/n;
Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
g3 = abs(2*A*k6(0)/(mu21*Itilda4*n))^(1/9);
Ihat3 = 0;
for i=1:n;
Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
end;
Ihat3 = -Ihat3/((n^2)*g3^7);
g2 = abs(2*A*k4(0)/(mu21*Ihat3*n))^(1/7);
Ihat2 = 0;
for i=1:n;
Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
end;
Ihat2 = Ihat2/((n^2)*g2^5);
h = (A*mu02/(n*Ihat2*mu21^2))^(1/5); % equation (22) in Skold and Roberts [2003] --> h_{MH}
elseif bandwidth == -2; % Bump killing... We construct local bandwith parameters in order to remove
strcmp(kernel_function,'quartic')
error(['I can''t compute the optimal bandwidth with this kernel...' ...
'Try the gaussian, triweight or cosinus kernels.']);
end
Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
g3 = abs(2*correction*k6(0)/(mu21*Itilda4*number_of_draws))^(1/9);
Ihat3 = 0;
for i=1:number_of_draws
Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
end
Ihat3 = - Ihat3/((number_of_draws^2)*g3^7);
g2 = abs(2*correction*k4(0)/(mu21*Ihat3*number_of_draws))^(1/7);
Ihat2 = 0;
for i=1:number_of_draws
Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
end
Ihat2 = Ihat2/((number_of_draws^2)*g2^5);
h = (correction*mu02/(number_of_draws*Ihat2*mu21^2))^(1/5); % equation (22) in Skold and Roberts [2003].
elseif bandwidth == -2; % Bump killing... I compute local bandwith parameters in order to remove
% spurious bumps introduced by long rejecting periods.
if strcmp(kernel_function,'uniform') | ...
strcmp(kernel_function,'triangle') | ...
strcmp(kernel_function,'epanechnikov') | ...
strcmp(kernel_function,'quartic');
error('I can''t compute the optimal bandwidth with this kernel... Try the gaussian, triweight or cosinus kernels.');
end;
sigma = std(data);
A = 0;
T = zeros(n,1);
for i=1:n;
j = i;
while j<= n & data(j,1)==data(i,1);
j = j+1;
end;
T(i) = (j-i);
A = A + 2*T(i) - 1;
end;
A = A/n;
Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
g3 = abs(2*A*k6(0)/(mu21*Itilda4*n))^(1/9);
Ihat3 = 0;
for i=1:n;
Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
end;
Ihat3 = -Ihat3/((n^2)*g3^7);
g2 = abs(2*A*k4(0)/(mu21*Ihat3*n))^(1/7);
Ihat2 = 0;
for i=1:n;
Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
end;
Ihat2 = Ihat2/((n^2)*g2^5);
h = ((2*T-1)*mu02/(n*Ihat2*mu21^2)).^(1/5); % Note that h is a column vector (local banwidth parameters).
elseif bandwidth > 0
h = bandwidth;
strcmp(kernel_function,'quartic')
error(['I can''t compute the optimal bandwidth with this kernel...' ...
'Try the gaussian, triweight or cosinus kernels.']);
end
T = zeros(n,1);
for i=1:n
j = i;
while j<= n & (data(j,1)-data(i,1))<2*eps;
j = j+1;
end
T(i) = (j-i);
correction = correction + 2*T(i) - 1;
end
correction = correction/number_of_draws;
Itilda4 = 8*7*6*5/(((2*sigma)^9)*sqrt(pi));
g3 = abs(2*correction*k6(0)/(mu21*Itilda4*correction))^(1/9);
Ihat3 = 0;
for i=1:number_of_draws
Ihat3 = Ihat3 + sum(k6((data(i,1)-data)/g3));
end
Ihat3 = -Ihat3/((n^2)*g3^7);
g2 = abs(2*correction*k4(0)/(mu21*Ihat3*n))^(1/7);
Ihat2 = 0;
for i=1:number_of_draws;
Ihat2 = Ihat2 + sum(k4((data(i)-data)/g2));
end
Ihat2 = Ihat2/((number_of_draws^2)*g2^5);
h = ((2*T-1)*mu02/(number_of_draws*Ihat2*mu21^2)).^(1/5); % h is a column vector (local banwidth parameters).
else
disp('mh_optimal_bandwidth :: ');
error('Parameter bandwidth must be a real parameter value or equal to 0,-1 or -2.');
disp('mh_optimal_bandwidth:: ');
error('Parameter bandwidth must be equal to 0, -1 or -2.');
end
optimal_bandwidth = h;
optimal_bandwidth = h;
function correction = correction_for_repeated_draws(draws,n)
correction = 0;
for i=1:n
j = i;
while j<=n & ( draws(j,1) - draws(i,1) )<2*eps;
j = j+1;
end
correction = correction + 2*(j-i) - 1;
end
correction = correction/n;

View File

@ -40,7 +40,8 @@ function pm3(n1,n2,ifil,B,tit1,tit2,tit3,tit_tex,names1,names2,name3,DirectoryNa
tmp =zeros(B,1);
for i = 1:nvar
for j = 1:n2
[Mean(j,i),Median(j,i),Var(j,i),HPD(:,j,i),Distrib(:,j,i)] = posterior_moments(squeeze(stock1(SelecVariables(i),j,:)),0);
[Mean(j,i),Median(j,i),Var(j,i),HPD(:,j,i),Distrib(:,j,i)] = ...
posterior_moments(squeeze(stock1(SelecVariables(i),j,:)),0,options_.mh_conf_sig);
end
end
clear stock1

View File

@ -1,29 +1,28 @@
function [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(xx,info)
% function [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(xx,info)
function [post_mean, post_median, post_var, hpd_interval, post_deciles, density] = posterior_moments(xx,info,mh_conf_sig)
% Computes posterior mean, median, variance, HPD interval, deciles, and density from posterior draws.
%
% INPUTS
% xx: vector of posterior draws
% info=1: estimates posterior density
% info=0: computes moments
%
% xx [double] Vector of posterior draws (or prior draws ;-)
% info [integer] If equal to one the posterior density is estimated.
% mh_config_sig [double] Scalar between 0 and 1 specifying the size of the HPD interval.
%
%
% OUTPUTS
% post_mean: posterior mean
% post_median: median
% post_var: variance
% hpd_interval: HPD interval
% post_deciles: post deciles
% density: density
% post_mean [double] Scalar, posterior mean.
% post_median [double] Scalar, posterior median.
% post_var [double] Scalar, posterior variance.
% hpd_interval [double] Vector (1*2), Highest Probability Density interval
% post_deciles [double] Vector (9*1), deciles of the posterior distribution.
% density [double] Matrix (n*2), non parametric estimate of the posterior density. First and second
% columns are respectively abscissa and ordinate coordinates.
%
% SPECIAL REQUIREMENTS
% none
%
% Other matlab routines distributed with Dynare: mh_optimal_bandwidth.m
% kernel_density_estimate.m.
%
% part of DYNARE, copyright Dynare Team (2005-2008)
% Gnu Public License.
global options_
xx = xx(:);
xx = sort(xx);
@ -32,33 +31,33 @@ post_mean = mean(xx);
post_median = median(xx);
post_var = var(xx);
n = length(xx);
m = round((1-options_.mh_conf_sig)*n);
k = zeros(m,1);
jj = n-m;
for ii = 1:m
k(ii) = xx(jj)-xx(ii);
number_of_draws = length(xx);
hpd_draws = round((1-mh_conf_sig)*number_of_draws);
kk = zeros(hpd_draws,1);
jj = number_of_draws-hpd_draws;
for ii = 1:hpd_draws
kk(ii) = xx(jj)-xx(ii);
jj = jj + 1;
end
[kmin,idx] = min(k);
[kmin,idx] = min(kk);
hpd_interval = [xx(idx) xx(idx)+kmin];
post_deciles = xx([round(0.1*n) ...
round(0.2*n)...
round(0.3*n)...
round(0.4*n)...
round(0.5*n)...
round(0.6*n)...
round(0.7*n)...
round(0.8*n)...
round(0.9*n)]);
post_deciles = xx([round(0.1*number_of_draws) ...
round(0.2*number_of_draws) ...
round(0.3*number_of_draws) ...
round(0.4*number_of_draws) ...
round(0.5*number_of_draws) ...
round(0.6*number_of_draws) ...
round(0.7*number_of_draws) ...
round(0.8*number_of_draws) ...
round(0.9*number_of_draws)]);
if ~info
density = [];
else
number_of_grid_points = 2^9; % 2^9 = 512 !... Must be a power of two.
bandwidth = 0; % Rule of thumb optimal bandwidth parameter.
kernel_function = 'gaussian'; % Gaussian kernel for Fast Fourrier Transform approximaton.
optimal_bandwidth = mh_optimal_bandwidth(xx,length(xx),bandwidth,kernel_function);
[density(:,1),density(:,2)] = kernel_density_estimate(xx,number_of_grid_points,optimal_bandwidth,kernel_function);
density = [];
if info
number_of_grid_points = 2^9; % 2^9 = 512 !... Must be a power of two.
bandwidth = 0; % Rule of thumb optimal bandwidth parameter.
kernel_function = 'gaussian'; % Gaussian kernel for Fast Fourrier Transform approximaton.
optimal_bandwidth = mh_optimal_bandwidth(xx,number_of_draws,bandwidth,kernel_function);
[density(:,1),density(:,2)] = kernel_density_estimate(xx,number_of_grid_points,...
number_of_draws,optimal_bandwidth,kernel_function);
end