/usr/share/dynare/matlab/th_autocovariances.m is in dynare-common 4.4.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | function [Gamma_y,stationary_vars] = th_autocovariances(dr,ivar,M_,options_,nodecomposition)
% Computes the theoretical auto-covariances, Gamma_y, for an AR(p) process
% with coefficients dr.ghx and dr.ghu and shock variances Sigma_e_
% for a subset of variables ivar (indices in lgy_)
% Theoretical HPfiltering is available as an option
%
% INPUTS
% dr: [structure] Reduced form solution of the DSGE model (decisions rules)
% ivar: [integer] Vector of indices for a subset of variables.
% M_ [structure] Global dynare's structure, description of the DSGE model.
% options_ [structure] Global dynare's structure.
% nodecomposition [integer] Scalar, if different from zero the variance decomposition is not triggered.
%
% OUTPUTS
% Gamma_y [cell] Matlab cell of nar+3 (second order approximation) or nar+2 (first order approximation) 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_.nar).
% Gamma_y{nar+2} [double] Variance decomposition.
% Gamma_y{nar+3} [double] Expectation of the endogenous variables associated with a second
% order approximation.
% stationary_vars [integer] Vector of indices of stationary variables (as a subset of 1:length(ivar))
%
% SPECIAL REQUIREMENTS
%
% Copyright (C) 2001-2012 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 nargin<5
nodecomposition = 0;
end
if options_.order >= 3
error('Theoretical moments not implemented above 2nd order')
end
endo_nbr = M_.endo_nbr;
exo_names_orig_ord = M_.exo_names_orig_ord;
if isoctave
warning('off', 'Octave:divide-by-zero')
else
warning off MATLAB:dividebyzero
end
nar = options_.ar;
Gamma_y = cell(nar+1,1);
if isempty(ivar)
ivar = [1:endo_nbr]';
end
nvar = size(ivar,1);
ghx = dr.ghx;
ghu = dr.ghu;
nspred = M_.nspred;
nstatic = M_.nstatic;
nx = size(ghx,2);
if options_.block == 0
%order_var = dr.order_var;
inv_order_var = dr.inv_order_var;
kstate = dr.kstate;
ikx = [nstatic+1:nstatic+nspred];
k0 = kstate(find(kstate(:,2) <= M_.maximum_lag+1),:);
i0 = find(k0(:,2) == M_.maximum_lag+1);
i00 = i0;
n0 = length(i0);
AS = ghx(:,i0);
ghu1 = zeros(nx,M_.exo_nbr);
ghu1(i0,:) = ghu(ikx,:);
for i=M_.maximum_lag:-1:2
i1 = find(k0(:,2) == i);
n1 = size(i1,1);
j1 = zeros(n1,1);
for k1 = 1:n1
j1(k1) = find(k0(i00,1)==k0(i1(k1),1));
end
AS(:,j1) = AS(:,j1)+ghx(:,i1);
i0 = i1;
end
else
ghu1 = zeros(nx,M_.exo_nbr);
trend = 1:M_.endo_nbr;
inv_order_var = trend(M_.block_structure.variable_reordered);
ghu1(1:length(dr.state_var),:) = ghu(dr.state_var,:);
end;
b = ghu1*M_.Sigma_e*ghu1';
if options_.block == 0
ipred = nstatic+(1:nspred)';
else
ipred = dr.state_var;
end;
% state space representation for state variables only
[A,B] = kalman_transition_matrix(dr,ipred,1:nx,M_.exo_nbr);
% Compute stationary variables (before HP filtering),
% and compute 2nd order mean correction on stationary variables (in case of
% HP filtering, this mean correction is computed *before* filtering)
if options_.order == 2 || options_.hp_filter == 0
[vx, u] = lyapunov_symm(A,B*M_.Sigma_e*B',options_.qz_criterium,options_.lyapunov_complex_threshold);
if options_.block == 0
iky = inv_order_var(ivar);
else
iky = ivar;
end;
stationary_vars = (1:length(ivar))';
if ~isempty(u)
x = abs(ghx*u);
iky = iky(find(all(x(iky,:) < options_.Schur_vec_tol,2)));
stationary_vars = find(all(x(inv_order_var(ivar(stationary_vars)),:) < options_.Schur_vec_tol,2));
end
aa = ghx(iky,:);
bb = ghu(iky,:);
if options_.order == 2 % mean correction for 2nd order
if ~isempty(ikx)
Ex = (dr.ghs2(ikx)+dr.ghxx(ikx,:)*vx(:)+dr.ghuu(ikx,:)*M_.Sigma_e(:))/2;
Ex = (eye(n0)-AS(ikx,:))\Ex;
Gamma_y{nar+3} = NaN*ones(nvar, 1);
Gamma_y{nar+3}(stationary_vars) = AS(iky,:)*Ex+(dr.ghs2(iky)+dr.ghxx(iky,:)*vx(:)+...
dr.ghuu(iky,:)*M_.Sigma_e(:))/2;
else %no static and no predetermined
Gamma_y{nar+3} = NaN*ones(nvar, 1);
Gamma_y{nar+3}(stationary_vars) = (dr.ghs2(iky)+ dr.ghuu(iky,:)*M_.Sigma_e(:))/2;
end
end
end
if options_.hp_filter == 0
v = NaN*ones(nvar,nvar);
v(stationary_vars,stationary_vars) = aa*vx*aa'+ bb*M_.Sigma_e*bb';
k = find(abs(v) < 1e-12);
v(k) = 0;
Gamma_y{1} = v;
% autocorrelations
if nar > 0
vxy = (A*vx*aa'+ghu1*M_.Sigma_e*bb');
sy = sqrt(diag(Gamma_y{1}));
sy = sy(stationary_vars);
sy = sy *sy';
Gamma_y{2} = NaN*ones(nvar,nvar);
Gamma_y{2}(stationary_vars,stationary_vars) = aa*vxy./sy;
for i=2:nar
vxy = A*vxy;
Gamma_y{i+1} = NaN*ones(nvar,nvar);
Gamma_y{i+1}(stationary_vars,stationary_vars) = aa*vxy./sy;
end
end
% variance decomposition
if ~nodecomposition && M_.exo_nbr > 1 && size(stationary_vars, 1) > 0
Gamma_y{nar+2} = zeros(nvar,M_.exo_nbr);
SS(exo_names_orig_ord,exo_names_orig_ord)=M_.Sigma_e+1e-14*eye(M_.exo_nbr);
cs = chol(SS)';
b1(:,exo_names_orig_ord) = ghu1;
b1 = b1*cs;
b2(:,exo_names_orig_ord) = ghu(iky,:);
b2 = b2*cs;
vx = lyapunov_symm(A,b1*b1',options_.qz_criterium,options_.lyapunov_complex_threshold,1);
vv = diag(aa*vx*aa'+b2*b2');
vv2 = 0;
for i=1:M_.exo_nbr
vx1 = lyapunov_symm(A,b1(:,i)*b1(:,i)',options_.qz_criterium,options_.lyapunov_complex_threshold,2);
vx2 = abs(diag(aa*vx1*aa'+b2(:,i)*b2(:,i)'));
Gamma_y{nar+2}(stationary_vars,i) = vx2;
vv2 = vv2 +vx2;
end
if max(abs(vv2-vv)./vv) > 1e-4
warning(['Aggregate variance and sum of variances by shocks ' ...
'differ by more than 0.01 %'])
end
for i=1:M_.exo_nbr
Gamma_y{nar+2}(stationary_vars,i) = Gamma_y{nar+ ...
2}(stationary_vars,i)./vv2;
end
end
else% ==> Theoretical HP filter.
% By construction, all variables are stationary when HP filtered
iky = inv_order_var(ivar);
stationary_vars = (1:length(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;
% Covariance of filtered series
imathp_col = real(ifft(mathp_col))*(2*pi);
Gamma_y{1} = reshape(imathp_col(1,:),nvar,nvar);
% Autocorrelations
if nar > 0
sy = sqrt(diag(Gamma_y{1}));
sy = sy *sy';
for i=1:nar
Gamma_y{i+1} = reshape(imathp_col(i+1,:),nvar,nvar)./sy;
end
end
% Variance decomposition
if ~nodecomposition && M_.exo_nbr > 1
Gamma_y{nar+2} = zeros(nvar,M_.exo_nbr);
SS(exo_names_orig_ord,exo_names_orig_ord) = M_.Sigma_e+1e-14*eye(M_.exo_nbr);
cs = chol(SS)';
SS = cs*cs';
b1(:,exo_names_orig_ord) = ghu1;
b2(:,exo_names_orig_ord) = ghu(iky,:);
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))*b1;IE]...
*SS*[b1'*inv(IA-A'*tpos(ig)) ...
IE]); % state variables
g_omega = [aa*tneg(ig) b2]*f_omega*[aa'*tpos(ig); b2']; % 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);
vv = diag(reshape(imathp_col(1,:),nvar,nvar));
for i=1:M_.exo_nbr
mathp_col = [];
SSi = cs(:,i)*cs(:,i)';
for ig = 1:ngrid
f_omega =(1/(2*pi))*( [inv(IA-A*tneg(ig))*b1;IE]...
*SSi*[b1'*inv(IA-A'*tpos(ig)) ...
IE]); % state variables
g_omega = [aa*tneg(ig) b2]*f_omega*[aa'*tpos(ig); b2']; % 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);
Gamma_y{nar+2}(:,i) = abs(diag(reshape(imathp_col(1,:),nvar,nvar)))./vv;
end
end
end
if isoctave
warning('on', 'Octave:divide-by-zero')
else
warning on MATLAB:dividebyzero
end
|