Commit 4913cfb3 authored by Sébastien Villemot's avatar Sébastien Villemot

Merge branch 'third_order_moments' into 'master'

Add display of theoretical moments at order=3 with pruning

See merge request !1744
parents e5cf2a37 9d2ec5f0
......@@ -3816,8 +3816,9 @@ Computing the stochastic solution
``oo_.conditional_variance_decomposition_ME`` (see
:mvar:`oo_.conditional_variance_decomposition_ME`). The
variance decomposition is only conducted, if theoretical
moments are requested, *i.e.* using the ``periods=0``-option.
In case of ``order=2``, Dynare provides a second-order accurate
moments are requested, *i.e.* using the ``periods=0``-option.
Only available at ``order<3``. In case of ``order=2``,
Dynare provides a second-order accurate
approximation to the true second moments based on the linear
terms of the second-order solution (see *Kim, Kim,
Schaumburg and Sims (2008)*). Note that the unconditional
......@@ -4013,9 +4014,10 @@ Computing the stochastic solution
|br| After a run of ``stoch_simul``, contains the
variance-covariance of the endogenous variables. Contains
theoretical variance if the ``periods`` option is not present (or
an approximation thereof for ``order=2``), and simulated variance
otherwise. The variables are arranged in declaration order.
theoretical variance if the ``periods`` option is not present and simulated variance
otherwise. Only available for ``order<4``. At ``order=2`` it will be be
a second-order accurate approximation. At ``order=3``, theoretical moments
are only available with ``pruning``. The variables are arranged in declaration order.
.. matvar:: oo_.var_list
......@@ -4042,9 +4044,10 @@ Computing the stochastic solution
number of the matrix in the cell array corresponds to the order of
autocorrelation. The option ar specifies the number of
autocorrelation matrices available. Contains theoretical
autocorrelations if the ``periods`` option is not present (or an
approximation thereof for ``order=2``), and simulated
autocorrelations otherwise. The field is only created if
autocorrelations if the ``periods`` option is not present and simulated
autocorrelations otherwise. Only available for ``order<4``. At ``order=2`` it will be be
a second-order accurate approximation. At ``order=3``, theoretical moments
are only available with ``pruning``. The field is only created if
stationary variables are present.
The element ``oo_.autocorr{i}(k,l)`` is equal to the correlation
......@@ -4082,9 +4085,10 @@ Computing the stochastic solution
If a second order approximation has been requested,
contains the vector of the mean correction terms.
In case ``order=2``, the theoretical second moments are a
second order accurate approximation of the true second
moments, see conditional_variance_decomposition.
Only available at ``order<4``. In case ``order=2``, the
theoretical second moments are a second order accurate
approximation of the true second moments. See conditional_variance_decomposition.
At ``order=3``, theoretical moments are only available with ``pruning``.
.. matvar:: oo_.variance_decomposition
......@@ -4152,8 +4156,10 @@ Computing the stochastic solution
|br| After a run of ``stoch_simul`` with the
``contemporaneous_correlation option``, contains theoretical
contemporaneous correlations if the ``periods`` option is not
present (or an approximation thereof for ``order=2``), and
simulated contemporaneous correlations otherwise. The variables
present, and simulated contemporaneous correlations otherwise.
Only available for ``order<4``. At ``order=2`` it will be be
a second-order accurate approximation. At ``order=3``, theoretical moments
are only available with ``pruning``. The variables
are arranged in declaration order.
.. matvar:: oo_.SpectralDensity
......
......@@ -21,7 +21,7 @@ function oo_ = disp_th_moments(dr, var_list, M_, options_, oo_)
nodecomposition = options_.nodecomposition;
if options_.one_sided_hp_filter
error(['disp_th_moments:: theoretical moments incompatible with one-sided HP filter. Use simulated moments instead'])
error('disp_th_moments:: theoretical moments incompatible with one-sided HP filter. Use simulated moments instead')
end
if isempty(var_list)
var_list = M_.endo_names(1:M_.orig_endo_nbr);
......@@ -129,7 +129,7 @@ if size(stationary_vars, 1) > 0
end
end
conditional_variance_steps = options_.conditional_variance_decomposition;
if length(conditional_variance_steps)
if ~isempty(conditional_variance_steps)
StateSpaceModel.number_of_state_equations = M_.endo_nbr;
StateSpaceModel.number_of_state_innovations = M_.exo_nbr;
StateSpaceModel.sigma_e_is_diagonal = M_.sigma_e_is_diagonal;
......@@ -151,7 +151,7 @@ if size(stationary_vars, 1) > 0
end
end
if length(i1) == 0
if isempty(i1)
skipline()
disp('All endogenous are constant or non stationary, not displaying correlations and auto-correlations')
skipline()
......@@ -186,7 +186,7 @@ if ~options_.nocorr && size(stationary_vars, 1)>0
end
if options_.ar > 0 && size(stationary_vars, 1) > 0
z=[];
z=NaN(length(i1),options_.ar);
for i=1:options_.ar
oo_.autocorr{i} = oo_.gamma_y{i+1};
z(:,i) = diag(oo_.gamma_y{i+1}(i1,i1));
......
function oo_=disp_th_moments_order3(dr,M_,options_,i_var,oo_)
% oo_=disp_th_moments_order3(dr,M_,options_,i_var,oo_)
% Display theoretical moments of variables based on (third order) pruned
% state-space
%
% INPUTS:
% dr : Dynare decision rules structure
% M_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}).
% options_ : Matlab's structure describing the options (initialized by dynare, see @ref{options_}).
% i_var : Index of requested variables in declaration order
% oo_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}), containing
%
% OUTPUTS:
% oo_ : Matlab's structure describing the Model (initialized by dynare, see @ref{M_}), containing
% gamma_y [cell] Matlab cell of nar+1 arrays, where nar is the order of the autocorrelation function.
% gamma_y{1} [double] Covariance matrix.
% gamma_y{i+1} [double] Autocorrelation function (for i=1,...,options_.ar).
% mean [vector] Unconditional mean
% var [matrix] Unconditional covariance matrix
% autocorr [cell] Cell storing the theoretical autocorrelation
% contemporaneous_correlation [matrix] matrix of contemporaneous correlations
%
% Copyright (C) 2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if options_.one_sided_hp_filter || options_.hp_filter || options_.bandpass.indicator
error(['disp_th_moments:: theoretical moments incompatible with filtering. Use simulated moments instead'])
end
nvars=length(i_var);
obs_var=NaN(nvars,1);
for i=1:nvars
obs_var(i,1) = find(strcmp(M_.endo_names(i_var(i),:), M_.endo_names(dr.order_var)));
end
pruned_state_space = pruned_state_space_system(M_, options_, dr, obs_var, options_.ar, 1, 0);
m = pruned_state_space.E_y;
oo_.gamma_y{1} = pruned_state_space.Var_y;
i1 = find(abs(diag(oo_.gamma_y{1})) > 1e-12);
s2 = diag(oo_.gamma_y{1});
sd = sqrt(s2);
z = [ m sd s2 ];
oo_.mean = m;
oo_.var = oo_.gamma_y{1};
if ~options_.noprint %options_.nomoments == 0
title='THEORETICAL MOMENTS BASED ON PRUNED STATE SPACE';
headers={'VARIABLE','MEAN','STD. DEV.','VARIANCE'};
labels = M_.endo_names(i_var,:);
lh = cellofchararraymaxlength(labels)+2;
dyntable(options_,title,headers,labels,z,lh,11,4);
if options_.TeX
labels = M_.endo_names_tex(i_var,:);
lh = cellofchararraymaxlength(labels)+2;
dyn_latex_table(M_,options_,title,'th_moments',headers,labels,z,lh,11,4);
end
end
if isempty(i1)
disp_verbose(' ',~options_.noprint)
disp_verbose('All endogenous are constant or non stationary, not displaying correlations and auto-correlations',~options_.noprint)
disp_verbose(' ',~options_.noprint)
return;
end
if options_.nocorr == 0 % && size(stationary_vars, 1) > 0
corr=pruned_state_space.Corr_y;
if options_.contemporaneous_correlation
oo_.contemporaneous_correlation = corr;
end
if ~options_.noprint
skipline()
title='MATRIX OF CORRELATIONS BASED ON PRUNED STATE SPACE';
labels = M_.endo_names(i_var,:);
headers = ['Variables';labels];
lh = cellofchararraymaxlength(labels)+2;
dyntable(options_,title,headers,labels,corr,lh,8,4);
if options_.TeX
labels = M_.endo_names_tex(i_var,:);
headers=['Variables';labels];
lh = cellofchararraymaxlength(labels)+2;
dyn_latex_table(M_,options_,title,'th_corr_matrix',headers,labels,corr,lh,8,4);
end
end
end
if options_.ar > 0 %&& size(stationary_vars, 1) > 0
z=NaN(length(i1),options_.ar);
for i=1:options_.ar
oo_.gamma_y{i+1} = pruned_state_space.Corr_yi(:,:,i);
oo_.autocorr{i} = oo_.gamma_y{i+1};
z(:,i) = diag(oo_.gamma_y{i+1}(i1,i1));
end
if ~options_.noprint
skipline()
title='COEFFICIENTS OF AUTOCORRELATION BASED ON PRUNED STATE SPACE';
labels = M_.endo_names(i_var(i1),:);
headers = ['Order ';cellstr(int2str([1:options_.ar]'))];
lh = cellofchararraymaxlength(labels)+2;
dyntable(options_,title,headers,labels,z,lh,8,4);
if options_.TeX
labels = M_.endo_names_tex(i_var(i1),:);
lh = cellofchararraymaxlength(labels)+2;
dyn_latex_table(M_,options_,title,'th_autocorr_matrix',headers,labels,z,lh,8,4);
end
end
end
\ No newline at end of file
......@@ -189,9 +189,11 @@ if ~options_.nomoments
if PI_PCL_solver
PCL_Part_info_moments(0, PCL_varobs, oo_.dr, i_var);
elseif options_.periods == 0
% There is no code for theoretical moments at 3rd order
if options_.order <= 2
oo_=disp_th_moments(oo_.dr,var_list,M_,options_,oo_);
elseif options_.order == 3 && options_.pruning
% There is no code for theoretical moments at 3rd order without pruning
oo_=disp_th_moments_order3(oo_.dr,M_,options_,i_var,oo_);
end
else
oo_=disp_moments(oo_.endo_simul,var_list,M_,options_,oo_);
......
......@@ -116,6 +116,6 @@ options_.solve_tolf=1E-12;
steady(solve_algo=3);
check;
stoch_simul(order=3,pruning,irf=0,nocorr,nofunctions,nomoments) C I Y H r D K lambda phi;
stoch_simul(order=3,pruning,irf=0,nofunctions,contemporaneous_correlation,TeX) C I Y H r D K lambda phi;
comparison_policy_functions_dynare_mathematica;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment