Commit 341adad2 authored by adjemian's avatar adjemian
Browse files

Added new functions for posterior distribution processing (second order...

Added new functions for posterior distribution processing (second order moments) + Removed global variables.

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1890 ac1d8469-bf42-47a9-8791-bf33cf982152
parent e315e735
function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data) function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)
% function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data) % function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data)
% Evaluates the posterior kernel of a dsge model. % Evaluates the posterior kernel of a dsge model.
% %
...@@ -22,9 +21,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data ...@@ -22,9 +21,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
% %
% part of DYNARE, copyright Dynare Team (2004-2008) % part of DYNARE, copyright Dynare Team (2004-2008)
% Gnu Public License. % Gnu Public License.
global bayestopt_ estim_params_ options_ trend_coeff_ M_ oo_ xparam1_test global bayestopt_ estim_params_ options_ trend_coeff_ M_ oo_ xparam1_test
fval = []; fval = [];
ys = []; ys = [];
trend_coeff = []; trend_coeff = [];
...@@ -155,7 +152,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data ...@@ -155,7 +152,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
% 3. Initial condition of the Kalman filter % 3. Initial condition of the Kalman filter
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
if options_.lik_init == 1 % Kalman filter if options_.lik_init == 1 % Kalman filter
Pstar = lyapunov_symm(T,R*Q*R'); Pstar = lyapunov_symm(T,R*Q*R',options_.qz_criterium);
Pinf = []; Pinf = [];
elseif options_.lik_init == 2 % Old Diffuse Kalman filter elseif options_.lik_init == 2 % Old Diffuse Kalman filter
Pstar = 10*eye(np); Pstar = 10*eye(np);
...@@ -165,7 +162,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data ...@@ -165,7 +162,7 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
Pstar = zeros(np,np); Pstar = zeros(np,np);
ivs = bayestopt_.restrict_var_list_stationary; ivs = bayestopt_.restrict_var_list_stationary;
R1 = R(ivs,:); R1 = R(ivs,:);
Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1'); Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1',options_.qz_criterium);
% Pinf = bayestopt_.Pinf; % Pinf = bayestopt_.Pinf;
% by M. Ratto % by M. Ratto
RR=T(:,bayestopt_.restrict_var_list_nonstationary); RR=T(:,bayestopt_.restrict_var_list_nonstationary);
...@@ -245,15 +242,20 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data ...@@ -245,15 +242,20 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
LIK = DiffuseLikelihoodH3(T,R,Q,H,Pinf,Pstar,data,trend,start); LIK = DiffuseLikelihoodH3(T,R,Q,H,Pinf,Pstar,data,trend,start);
else else
LIK = DiffuseLikelihoodH3corr(T,R,Q,H,Pinf,Pstar,data,trend,start); LIK = DiffuseLikelihoodH3corr(T,R,Q,H,Pinf,Pstar,data,trend,start);
end end
end end
else else
if options_.kalman_algo == 1 if options_.kalman_algo == 1
%nv = size(bayestopt_.Z,1); %nv = size(bayestopt_.Z,1);
%LIK = kalman_filter(bayestopt_.Z,zeros(nv,nv),T,R,Q,data,zeros(size(T,1),1),Pstar,'u'); %LIK = kalman_filter(bayestopt_.Z,zeros(nv,nv),T,R,Q,data,zeros(size(T,1),1),Pstar,'u');
LIK = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,data,trend,start); %tic
% LIK = diffuse_likelihood1(T,R,Q,Pinf,Pstar,data-trend,start); LIK = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,data,trend,start);
%if abs(LIK1-LIK)>0.0000000001 %toc
%tic
%LIK1 = Diffuse_Likelihood1(T,R,Q,Pinf,Pstar,data,trend,start);
%toc
%LIK1-LIK
%if abs(LIK1-LIK)>0.0000000001
% disp(['LIK1 and LIK are not equal! ' num2str(abs(LIK1-LIK))]) % disp(['LIK1 and LIK are not equal! ' num2str(abs(LIK1-LIK))])
%end %end
if isinf(LIK) if isinf(LIK)
......
...@@ -160,7 +160,7 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend ...@@ -160,7 +160,7 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend
% 3. Initial condition of the Kalman filter % 3. Initial condition of the Kalman filter
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
if options_.lik_init == 1 % Kalman filter if options_.lik_init == 1 % Kalman filter
Pstar = lyapunov_symm(T,R*Q*R'); Pstar = lyapunov_symm(T,R*Q*R',options_.qz_criterium);
Pinf = []; Pinf = [];
elseif options_.lik_init == 2 % Old Diffuse Kalman filter elseif options_.lik_init == 2 % Old Diffuse Kalman filter
Pstar = 10*eye(np); Pstar = 10*eye(np);
...@@ -183,7 +183,7 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend ...@@ -183,7 +183,7 @@ function [fval,llik,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend
R1(j,:) = R1(j,:)-RR(j,i).*R(ivd(i),:); R1(j,:) = R1(j,:)-RR(j,i).*R(ivd(i),:);
end end
end end
Pstar = lyapunov_symm(T0,R1*Q*R1'); Pstar = lyapunov_symm(T0,R1*Q*R1',options_.qz_criterium);
end end
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
% 4. Likelihood evaluation % 4. Likelihood evaluation
......
function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,decomp] = DsgeSmoother(xparam1,gend,Y) function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,decomp] = DsgeSmoother(xparam1,gend,Y)
% function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,decomp] = DsgeSmoother(xparam1,gend,Y)
% Estimation of the smoothed variables and innovations. % Estimation of the smoothed variables and innovations.
% %
% INPUTS % INPUTS
...@@ -95,7 +93,7 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d, ...@@ -95,7 +93,7 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,
H = M_.H; H = M_.H;
if options_.lik_init == 1 % Kalman filter if options_.lik_init == 1 % Kalman filter
Pstar = lyapunov_symm(T,R*Q*transpose(R)); Pstar = lyapunov_symm(T,R*Q*transpose(R),options_.qz_criterium);
Pinf = []; Pinf = [];
elseif options_.lik_init == 2 % Old Diffuse Kalman filter elseif options_.lik_init == 2 % Old Diffuse Kalman filter
Pstar = 10*eye(np); Pstar = 10*eye(np);
...@@ -105,7 +103,7 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d, ...@@ -105,7 +103,7 @@ function [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK,T,R,P,PK,d,
Pstar = zeros(np,np); Pstar = zeros(np,np);
ivs = bayestopt_.restrict_var_list_stationary; ivs = bayestopt_.restrict_var_list_stationary;
R1 = R(ivs,:); R1 = R(ivs,:);
Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1'); Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1',options_.qz_criterium);
% Pinf = bayestopt_.Pinf; % Pinf = bayestopt_.Pinf;
% by M. Ratto % by M. Ratto
RR=T(:,bayestopt_.restrict_var_list_nonstationary); RR=T(:,bayestopt_.restrict_var_list_nonstationary);
......
...@@ -122,7 +122,7 @@ end ...@@ -122,7 +122,7 @@ end
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
% 3. theoretical moments (second order) % 3. theoretical moments (second order)
%------------------------------------------------------------------------------ %------------------------------------------------------------------------------
tmp0 = lyapunov_symm(T,R*Q*R');% I compute the variance-covariance matrix tmp0 = lyapunov_symm(T,R*Q*R',options_.qz_criterium);% I compute the variance-covariance matrix
mf = bayestopt_.mf1; % of the restricted state vector. mf = bayestopt_.mf1; % of the restricted state vector.
% Get the non centered second order moments % Get the non centered second order moments
......
...@@ -7,6 +7,7 @@ function [omega,f] = UnivariateSpectralDensity(dr,var_list) ...@@ -7,6 +7,7 @@ function [omega,f] = UnivariateSpectralDensity(dr,var_list)
% Adapted from th_autocovariances.m. % Adapted from th_autocovariances.m.
global options_ oo_ M_ global options_ oo_ M_
omega = []; f = []; omega = []; f = [];
if options_.order > 1 if options_.order > 1
...@@ -17,8 +18,8 @@ if options_.order > 1 ...@@ -17,8 +18,8 @@ if options_.order > 1
end end
pltinfo = 1;%options_.SpectralDensity.Th.plot; pltinfo = 1;%options_.SpectralDensity.Th.plot;
cutoff = 100;%options_.SpectralDensity.Th.cutoff; cutoff = 150;%options_.SpectralDensity.Th.cutoff;
sdl = 0.1;%options_.SepctralDensity.Th.sdl; sdl = 0.01;%options_.SepctralDensity.Th.sdl;
omega = (0:sdl:pi)'; omega = (0:sdl:pi)';
GridSize = length(omega); GridSize = length(omega);
exo_names_orig_ord = M_.exo_names_orig_ord; exo_names_orig_ord = M_.exo_names_orig_ord;
...@@ -76,34 +77,70 @@ for i=M_.maximum_lag:-1:2 ...@@ -76,34 +77,70 @@ for i=M_.maximum_lag:-1:2
AS(:,j1) = AS(:,j1)+ghx(:,i1); AS(:,j1) = AS(:,j1)+ghx(:,i1);
i0 = i1; i0 = i1;
end end
b = ghu1*M_.Sigma_e*ghu1'; Gamma = zeros(nvar,cutoff+1);
[A,B] = kalman_transition_matrix(dr); [A,B] = kalman_transition_matrix(dr,ikx',1:nx,dr.transition_auxiliary_variables,M_.exo_nbr);
% index of predetermined variables in A [vx, u] = lyapunov_symm(A,B*M_.Sigma_e*B',options_.qz_criterium);
i_pred = [nstatic+(1:npred) M_.endo_nbr+1:length(A)]; iky = iv(ivar);
A = A(i_pred,i_pred); if ~isempty(u)
[vx, ns_var] = lyapunov_symm(A,b); iky = iky(find(any(abs(ghx(iky,:)*u) < options_.Schur_vec_tol,2)));
i_ivar = find(~ismember(ivar,dr.order_var(ns_var+nstatic))); ivar = dr.order_var(iky);
ivar = ivar(i_ivar); end
iky = iv(ivar);
aa = ghx(iky,:); aa = ghx(iky,:);
bb = ghu(iky,:); bb = ghu(iky,:);
Gamma = zeros(nvar,cutoff+1);
tmp = aa*vx*aa'+ bb*M_.Sigma_e*bb'; if options_.hp_filter == 0
k = find(abs(tmp) < 1e-12); tmp = aa*vx*aa'+ bb*M_.Sigma_e*bb';
tmp(k) = 0; k = find(abs(tmp) < 1e-12);
Gamma(:,1) = diag(tmp); tmp(k) = 0;
vxy = (A*vx*aa'+ghu1*M_.Sigma_e*bb'); Gamma(:,1) = diag(tmp);
tmp = aa*vxy; vxy = (A*vx*aa'+ghu1*M_.Sigma_e*bb');
k = find(abs(tmp) < 1e-12); tmp = aa*vxy;
tmp(k) = 0; k = find(abs(tmp) < 1e-12);
Gamma(:,2) = diag(tmp); tmp(k) = 0;
for i=2:cutoff Gamma(:,2) = diag(tmp);
vxy = A*vxy; for i=2:cutoff
tmp = aa*vxy; vxy = A*vxy;
k = find(abs(tmp) < 1e-12); tmp = aa*vxy;
tmp(k) = 0; k = find(abs(tmp) < 1e-12);
Gamma(:,i+1) = diag(tmp); tmp(k) = 0;
Gamma(:,i+1) = diag(tmp);
end
else
iky = iv(ivar);
aa = ghx(iky,:);
bb = ghu(iky,:);
lambda = options_.hp_filter;
ngrid = options_.hp_ngrid;
freqs = 0 : ((2*pi)/ngrid) : (2*pi*(1 - .5/ngrid));
tpos = exp( sqrt(-1)*freqs);
tneg = exp(-sqrt(-1)*freqs);
hp1 = 4*lambda*(1 - cos(freqs)).^2 ./ (1 + 4*lambda*(1 - cos(freqs)).^2);
mathp_col = [];
IA = eye(size(A,1));
IE = eye(M_.exo_nbr);
for ig = 1:ngrid
f_omega =(1/(2*pi))*( [inv(IA-A*tneg(ig))*ghu1;IE]...
*M_.Sigma_e*[ghu1'*inv(IA-A'*tpos(ig)) IE]); % state variables
g_omega = [aa*tneg(ig) bb]*f_omega*[aa'*tpos(ig); bb']; % selected variables
f_hp = hp1(ig)^2*g_omega; % spectral density of selected filtered series
mathp_col = [mathp_col ; (f_hp(:))']; % store as matrix row
% for ifft
end;
imathp_col = real(ifft(mathp_col))*(2*pi);
tmp = reshape(imathp_col(1,:),nvar,nvar);
k = find(abs(tmp)<1e-12);
tmp(k) = 0;
Gamma(:,1) = diag(tmp);
sy = sqrt(Gamma(:,1));
sy = sy *sy';
for i=1:cutoff-1
tmp = reshape(imathp_col(i+1,:),nvar,nvar)./sy;
k = find(abs(tmp) < 1e-12);
tmp(k) = 0;
Gamma(:,i+1) = diag(tmp);
end
end end
H = 1:cutoff; H = 1:cutoff;
for i=1:nvar for i=1:nvar
f(i,:) = Gamma(i,1)/(2*pi) + Gamma(i,H+1)*cos(H'*omega')/pi; f(i,:) = Gamma(i,1)/(2*pi) + Gamma(i,H+1)*cos(H'*omega')/pi;
......
% targets and iy order: 1) variances 2) correlations % targets and iy order: 1) variances 2) correlations
% 3) constraints on M_.Sigma_e itself 4) autocorrelations % 3) constraints on M_.Sigma_e itself 4) autocorrelations
function f=calib_obj(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar) function f=calib_obj(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar)
global vx fold global vx fold options_
oo_.gamma_y = cell(nar+1,1); oo_.gamma_y = cell(nar+1,1);
% M_.Sigma_e = M_.Sigma_e'*M_.Sigma_e; % M_.Sigma_e = M_.Sigma_e'*M_.Sigma_e;
...@@ -10,11 +10,11 @@ function f=calib_obj(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar) ...@@ -10,11 +10,11 @@ function f=calib_obj(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar)
b=ghu1*M_.Sigma_e*ghu1'; b=ghu1*M_.Sigma_e*ghu1';
vx = []; vx = [];
if isempty(vx) if isempty(vx)
vx = lyapunov_symm(A,b); vx = lyapunov_symm(A,b,options_.qz_criterium);
else else
[vx,status] = bicgstab_(@f_var,b(:),vx(:),1e-8,50,A,nx); [vx,status] = bicgstab_(@f_var,b(:),vx(:),1e-8,50,A,nx);
if status if status
vx = lyapunov_symm(A,b); vx = lyapunov_symm(A,b,options_.qz_criterium);
else else
vx=reshape(vx,nx,nx); vx=reshape(vx,nx,nx);
end end
......
% targets and iy order: 1) variances 2) correlations % targets and iy order: 1) variances 2) correlations
% 3) constraints on M_.Sigma_e itself 4) autocorrelations % 3) constraints on M_.Sigma_e itself 4) autocorrelations
function objective=calib_obj2(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar) function objective=calib_obj2(M_.Sigma_e,A,ghu1,ghx,ghu,targets,var_weights,iy,nar)
global vx fold global vx fold options_
objective = cell (nar+3); objective = cell (nar+3);
oo_.gamma_y = cell(nar+1,1); oo_.gamma_y = cell(nar+1,1);
M_.Sigma_e=diag(M_.Sigma_e); M_.Sigma_e=diag(M_.Sigma_e);
nx = size(ghx,2); nx = size(ghx,2);
b=ghu1*M_.Sigma_e*ghu1'; b=ghu1*M_.Sigma_e*ghu1';
vx = lyapunov_symm(A,b); vx = lyapunov_symm(A,b,options_.qz_criterium);
oo_.gamma_y{1} = ghx*vx*ghx'+ ghu*M_.Sigma_e*ghu'; oo_.gamma_y{1} = ghx*vx*ghx'+ ghu*M_.Sigma_e*ghu';
if ~isempty(targets{1}) if ~isempty(targets{1})
objective{1} = sqrt(oo_.gamma_y{1}(iy{1})); objective{1} = sqrt(oo_.gamma_y{1}(iy{1}));
......
function [info,description] = check_posterior_analysis_data(type,M_)
% part of DYNARE, copyright Dynare Team (2008)
% Gnu Public License.
info = 0;
if nargout>1
description = '';
end
%% Get informations about mcmc files.
if ~exist([ M_.dname '/metropolis'],'dir')
disp('check_posterior_analysis_data:: Can''t find any mcmc file!')
return
end
mhname = get_name_of_the_last_mh_file(M_);
mhdate = get_date_of_a_file(mhname);
%% Get informations about _posterior_draws files.
if ~exist([ M_.dname '/metropolis/' M_.fname '_posterior_draws.mat'])
info = 1; % select_posterior_draws has to be called first.
if nargout>1
description = 'select_posterior_draws has to be called.';
end
return
else
pddate = get_date_of_a_file([ M_.dname '/metropolis/' M_.fname '_posterior_draws.mat']);
if pddate<mhdate
info = 2; % _posterior_draws files have to be updated.
if nargout>1
description = 'posterior draws files have to be updated.';
end
return
else
info = 3; % Ok!
if nargout>1
description = 'posterior draws files are up to date.';
end
end
end
%% Get informations about posterior data files.
switch type
case 'variance'
generic_post_data_file_name = 'Posterior2ndOrderMoments';
case 'decomposition'
generic_post_data_file_name = 'PosteriorVarianceDecomposition';
case 'dynamic_decomposition'
generic_post_data_file_name = 'PosteriorDynamicVarianceDecomposition';
otherwise
disp('This feature is not yest implemented!')
end
pdfinfo = dir([ M_.dname '/metropolis/' M_.fname '_' generic_post_data_file_name '*'])
if isempty(pdfinfo)
info = 4; % posterior draws have to be processed.
if nargout>1
description = 'posterior draws files have to be processed.';
end
return
else
number_of_the_last_post_data_file = length(pdfinfo);
name_of_the_last_post_data_file = ...
[ M_.dname ...
'/metropolis/' ...
M_.dname ...
generic_post_data_file_name ...
int2str(number_of_the_last_post_data_file) ...
'.mat' ];
pdfdate = get_date_of_a_file(name_of_the_last_post_data_file);
if pdfdate<pddate
info = 5; % posterior data files have to be updated.
if nargout>1
description = 'posterior data files have to be updated.';
end
else
info = 6; % Ok (nothing to do ;-)
if nargout>1
description = 'There is nothing to do';
end
end
end
\ No newline at end of file
function moments=compute_model_moments(dr,options) function moments=compute_model_moments(dr,M_,options,_)
% function compute_model_moments(options)
% Computes posterior filter smoother and forecasts
% %
% INPUTS % INPUTS
% dr: structure describing model solution % dr: structure describing model solution
% options: structure of Dynare options % M_: structure of Dynare options
% options_
% %
% OUTPUTS % OUTPUTS
% moments: a cell array containing requested moments % moments: a cell array containing requested moments
...@@ -16,7 +14,4 @@ function moments=compute_model_moments(dr,options) ...@@ -16,7 +14,4 @@ function moments=compute_model_moments(dr,options)
% part of DYNARE, copyright Dynare Team (2008) % part of DYNARE, copyright Dynare Team (2008)
% Gnu Public License. % Gnu Public License.
% subset of variables moments = th_autocovariances(dr,options_.varlist,M_,options_);
varlist = options.varlist;
moments = th_autocovariances(dr,varlist);
...@@ -19,7 +19,7 @@ function disp_th_moments(dr,var_list) ...@@ -19,7 +19,7 @@ function disp_th_moments(dr,var_list)
end end
end end
[oo_.gamma_y,ivar] = th_autocovariances(dr,ivar); [oo_.gamma_y,ivar] = th_autocovariances(dr,ivar,M_,options_);
m = dr.ys(ivar); m = dr.ys(ivar);
......
function dsge_posterior_theoretical_covariance() function [nvar,vartan,CovarFileNumber] = dsge_posterior_theoretical_covariance(SampleSize,M_,options_,oo_)
% This function estimates the posterior density of the endogenous % This function estimates the posterior density of the endogenous
% variables second order moments. % variables second order moments.
% %
% INPUTS % INPUTS
% None. % SampleSize [integer]
% %
%
%
% OUTPUTS % OUTPUTS
% None. % None.
% %
...@@ -21,52 +23,30 @@ function dsge_posterior_theoretical_covariance() ...@@ -21,52 +23,30 @@ function dsge_posterior_theoretical_covariance()
% part of DYNARE, copyright Dynare Team (2007-2008) % part of DYNARE, copyright Dynare Team (2007-2008)
% Gnu Public License. % Gnu Public License.
global M_ options_ oo_ type = 'posterior';
type = 'posterior';% To be defined as a input argument later...
NumberOfSimulations = 1000;% To be defined in a global structure...
% Set varlist (vartan) % Set varlist (vartan)
[ivar,vartan] = set_stationary_variables_list; [ivar,vartan] = set_stationary_variables_list;
% Set various parameters & Check or create files and directories
if strcmpi(type,'posterior')
MhDirectoryName = CheckPath('metropolis');
else
MhDirectoryName = CheckPath('prior');
end
fname = [ MhDirectoryName '/' M_.fname];
%save([fname '_Posterior2ndOrder'],'varlist');
DrawsFiles = dir([fname '_' type '_draws*' ]);
if ~rows(DrawsFiles)
if strcmpi(type,'posterior')
drsize = size_of_the_reduced_form_model(oo_.dr);
if drsize*NumberOfSimulations>101 %Too big model!
drsize=0;
end
SampleAddress = selec_posterior_draws(NumberOfSimulations,drsize);
else
% (samples from the prior) To be done later...
end
DrawsFiles = dir([fname '_' type '_draws*']);
end
% Get the number of stationary endogenous variables.
nvar = length(ivar); nvar = length(ivar);
nar = options_.ar;% Saves size of the auto-correlation function. % Set the size of the auto-correlation function to zero.
options_.ar = 0;% Set the size of the auto-correlation function to zero. nar = options_.ar;
options_.ar = 0;
NumberOfDrawsFiles = rows(DrawsFiles); % 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.
MaXNumberOfCovarLines = ceil(options_.MaxNumberOfBytes/(nvar*(nvar+1)/2)/8); MaXNumberOfCovarLines = ceil(options_.MaxNumberOfBytes/(nvar*(nvar+1)/2)/8);
if NumberOfSimulations<=MaXNumberOfCovarLines if SampleSize<=MaXNumberOfCovarLines
Covariance_matrix = zeros(NumberOfSimulations,nvar*(nvar+1)/2); Covariance_matrix = zeros(NumberOfSimulations,nvar*(nvar+1)/2);
NumberOfCovarFiles = 1; NumberOfCovarFiles = 1;
else else
Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2); Covariance_matrix = zeros(MaXNumberOfCovarLines,nvar*(nvar+1)/2);
NumberOfLinesInTheLastCovarFile = mod(NumberOfSimulations,MaXNumberOfCovarLines); NumberOfLinesInTheLastCovarFile = mod(SampleSize,MaXNumberOfCovarLines);
NumberOfCovarFiles = ceil(NumberOfSimulations/MaXNumberOfCovarLines); NumberOfCovarFiles = ceil(SampleSize/MaXNumberOfCovarLines);
end end
NumberOfCovarLines = rows(Covariance_matrix); NumberOfCovarLines = rows(Covariance_matrix);
...@@ -86,10 +66,10 @@ for file = 1:NumberOfDrawsFiles ...@@ -86,10 +66,10 @@ for file = 1:NumberOfDrawsFiles
set_parameters(pdraws{linee,1}); set_parameters(pdraws{linee,1});
[dr,info] = resol(oo_.steady_state,0); [dr,info] = resol(oo_.steady_state,0);
end end
tmp = th_autocovariances(dr,ivar); tmp = th_autocovariances(dr,ivar,M_,options_);
for i=1:nvar for i=1:nvar
for j=i:nvar for j=i:nvar
Covariance_matrix(linea,idx(i,j,nvar)) = tmp{1}(i,j); Covariance_matrix(linea,symmetric_matrix_index(i,j,nvar)) = tmp{1}(i,j);
end end