Add unit test for order=3

The unit test uses the policy rules of Fernandez-Villaverde et al.
(2011)'s "Risk matter" derived from Mathematica to compare them to the
Dynare matrices. If the policy functions differ by more than 1e-9, an
error is produced.
time-shift
Johannes Pfeifer 2013-04-24 23:12:52 +02:00
parent cdb7b01879
commit 7e63b562d9
3 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,121 @@
/*
* This file replicates the IRFs of the small open economy model described in
* Jesús Fernández-Villaverde, Pablo Guerrón-Quintana,
* Juan F. Rubio-Ramírez, and Martin Uribe (2011): "Risk Matters",
* American Economic Review 101 (October 2011): 25302561.
*
* This implementation was written by Benjamin Born and Johannes Pfeifer. Please
* note that the following copyright notice only applies to this Dynare
* implementation of the
* model.
*/
/*
* Copyright (C) 2013 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/>.
*/
var sigma_r sigma_tb eps_r eps_tb X D K lambda C H Y I phi r;
varexo u_sigma_r u_sigma_tb u_r u_tb u_x ;
predetermined_variables K D;
parameters r_bar rho_eps_r sigma_r_bar rho_sigma_r eta_r
rho_eps_tb sigma_tb_bar rho_sigma_tb eta_tb
delta alppha nu rho_x betta
Phi phipar sigma_x D_bar omega eta;
// Calibration from Table 3
rho_eps_r=0.97;
sigma_r_bar=-5.71;
rho_sigma_r=0.94;
eta_r=0.46;
// Calibration from Table 4
rho_eps_tb=0.95;
sigma_tb_bar=-8.06; %8.05 in paper, but 8.06 in code
rho_sigma_tb=0.94;
eta_tb=0.13;
nu=5; %inverse of the elasticity of intertemporal substitution
eta=1000; %elasticity of labor to wages
delta=0.014; %depreciation
alppha = 0.32; % capital income share
rho_x=0.95; %autocorrelation TFP
// Calibration from Table 6 //
r_bar=log(0.02);
betta = 1/(1+exp(r_bar)); %discount factor
Phi=0.001; %debt elasticity
D_bar= 4; % steady state debt
phipar=95; % capital adjustment costs
sigma_x=log(0.015);
omega=1;
model;
(exp(C))^-nu=exp(lambda);
exp(lambda)/(1+exp(r))=exp(lambda)*Phi*((D(+1))-D_bar)+betta*exp(lambda(+1));
-exp(phi)+betta*((1-delta)*exp(phi(+1))+alppha*exp(Y(+1))/exp(K(+1))*exp(lambda(+1)))=0;
omega*exp(H)^eta=(1-alppha)*exp(Y)/exp(H)*exp(lambda);
exp(phi)*(1-phipar/2*((exp(I)-exp(I(-1)))/exp(I(-1)))^2-phipar*exp(I)/exp(I(-1))*((exp(I)-exp(I(-1)))/exp(I(-1))))+betta*exp(phi(+1))*phipar*(exp(I(+1))/exp(I))^2*((exp(I(+1))-exp(I))/exp(I))=exp(lambda);
exp(Y)=exp(K)^alppha*(exp(X)*exp(H))^(1-alppha);
X=rho_x*X(-1)+exp(sigma_x)*u_x;
exp(K(+1))=(1-delta)*exp(K)+(1-phipar/2*(exp(I)/exp(I(-1))-1)^2)*exp(I);
exp(Y)-exp(C)-exp(I)=(D)-(D(+1))/(1+exp(r))+Phi/2*((D(+1))-D_bar)^2;
exp(r)=exp(r_bar)+eps_tb+eps_r;
eps_tb=rho_eps_tb*eps_tb(-1)+exp(sigma_tb)*u_tb;
sigma_tb=(1-rho_sigma_tb)*sigma_tb_bar+rho_sigma_tb*sigma_tb(-1)+eta_tb*u_sigma_tb;
eps_r=rho_eps_r*eps_r(-1)+exp(sigma_r)*u_r;
sigma_r=(1-rho_sigma_r)*sigma_r_bar+rho_sigma_r*sigma_r(-1)+eta_r*u_sigma_r;
end;
initval;
sigma_tb=sigma_tb_bar;
sigma_r=sigma_r_bar;
eps_r=0;
eps_tb=0;
D=D_bar;
% steady states taken from Mathematica code
C=0.8779486025329908;
K=3.293280327636415;
lambda=-4.389743012664954;
H=-0.0037203652717462993;
phi=-4.389743012664954;
I=-0.9754176217303792;
Y=1.0513198564588924;
r=r_bar;
end;
shocks;
var u_x; stderr 1;
var u_r; stderr 1;
var u_tb; stderr 1;
var u_sigma_tb; stderr 1;
var u_sigma_r; stderr 1;
end;
resid(1);
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;
comparison_policy_functions_dynare_mathematica;

View File

@ -0,0 +1,125 @@
%read in the FV et al. policy functions derived from Mathematica
load policyfunctions
order=options_.order;
dr=oo_.dr;
nx =size(dr.ghx,2);
nu =size(dr.ghu,2);
k = find(dr.kstate(:,2) <= M_.maximum_lag+1);
klag = dr.kstate(k,[1 2]);
state_vars=dr.order_var(klag(:,1));
%order of endogenous variables in FV et al. paper: c, invest, y, h, r, dp, kp, lambda, pi
varlist_FV={'C';'I';'Y';'H';'r';'D';'K';'lambda';'phi'};
for ii=1: length(varlist_FV)
FV_endo_order(ii,1)=strmatch(varlist_FV(ii),M_.endo_names(dr.order_var,:),'exact');
end
% order in states of FV et al. paper:
% exogenous: usigmar usigmatb ur utb ug
varlist_FV_exo_states={'u_sigma_r';'u_sigma_tb';'u_r';'u_tb';'u_x'};
for ii=1: length(varlist_FV_exo_states)
FV_exo_order(ii)=strmatch(varlist_FV_exo_states(ii),M_.exo_names,'exact');
end
%followed by endogenous: sigmarlag sigmatblag erlag etblag glag investlag debt capital Lambda
varlist_FV_endo_states={'sigma_r';'sigma_tb';'eps_r';'eps_tb';'X';'I';'D';'K'};
for ii=1: length(varlist_FV_endo_states)
FV_endo_state_order(ii)=strmatch(varlist_FV_endo_states(ii),M_.endo_names(state_vars,:),'exact');
end
%% First order
gx_dyn(:,1:nu)=dr.ghu(FV_endo_order,FV_exo_order);
gx_dyn(:,nu+1:nu+nx)=dr.ghx(FV_endo_order,FV_endo_state_order);
if max(max(abs(gx(:,1:end-1)-gx_dyn))) > 1e-9
max(max(abs(gx(:,1:end-1)-gx_dyn)))
error('First order wrong')
else max(max(abs(gx(:,1:end-1)-gx_dyn)));
end
%% Second order
gxx_dyn=zeros(size(gxx));
for endo_iter_1=1:nx
for endo_iter_2=1:nx
gxx_dyn(nu+endo_iter_1,nu+endo_iter_2,:)=dr.ghxx(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nx+FV_endo_state_order(endo_iter_2));
end
end
for exo_iter_1=1:nu
for exo_iter_2=1:nu
gxx_dyn(exo_iter_1,exo_iter_2,:)=dr.ghuu(FV_endo_order,(FV_exo_order(exo_iter_1)-1)*nu+FV_exo_order(exo_iter_2));
end
end
for endo_iter_1=1:nx
for exo_iter_1=1:nu
gxx_dyn(nu+endo_iter_1,exo_iter_1,:)=dr.ghxu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nu+FV_exo_order(exo_iter_1));
gxx_dyn(exo_iter_1,nu+endo_iter_1,:)=dr.ghxu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nu+FV_exo_order(exo_iter_1));
end
end
% deal with Lambda, the perturbation parameter
for ii=1:length(FV_endo_order)
gxx_dyn(14,14,:)=dr.ghs2(FV_endo_order);
end
if max(max(max(abs(gxx-gxx_dyn)))) > 1e-9
max(max(max(abs(gxx-gxx_dyn))))
error('Second order wrong')
else max(max(max(abs(gxx-gxx_dyn))));
end
%% third order
gxxx_dyn=zeros(size(gxxx));
for endo_iter_1=1:nx
for endo_iter_2=1:nx
for endo_iter_3=1:nx
gxxx_dyn(nu+endo_iter_1,nu+endo_iter_2,nu+endo_iter_3,:)=dr.ghxxx(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nx*nx+(FV_endo_state_order(endo_iter_2)-1)*nx+FV_endo_state_order(endo_iter_3));
end
end
end
for exo_iter_1=1:nu
for exo_iter_2=1:nu
for exo_iter_3=1:nu
gxxx_dyn(exo_iter_1,exo_iter_2,exo_iter_3,:)=dr.ghuuu(FV_endo_order,(FV_exo_order(exo_iter_1)-1)*nu*nu+(FV_exo_order(exo_iter_2)-1)*nu+FV_exo_order(exo_iter_3));
end
end
end
for endo_iter_1=1:nx
for endo_iter_2=1:nx
for exo_iter=1:nu
gxxx_dyn(nu+endo_iter_1,nu+endo_iter_2,exo_iter,:)=dr.ghxxu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nx*nu+(FV_endo_state_order(endo_iter_2)-1)*nu+FV_exo_order(exo_iter));
gxxx_dyn(exo_iter,nu+endo_iter_2,nu+endo_iter_1,:)=dr.ghxxu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nx*nu+(FV_endo_state_order(endo_iter_2)-1)*nu+FV_exo_order(exo_iter));
gxxx_dyn(nu+endo_iter_1,exo_iter,nu+endo_iter_2,:)=dr.ghxxu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nx*nu+(FV_endo_state_order(endo_iter_2)-1)*nu+FV_exo_order(exo_iter));
end
end
end
for endo_iter_1=1:nx
for exo_iter_1=1:nu
for exo_iter_2=1:nu
gxxx_dyn(nu+endo_iter_1,exo_iter_1,exo_iter_2,:)=dr.ghxuu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nu*nu+(FV_exo_order(exo_iter_1)-1)*nu+FV_exo_order(exo_iter_2));
gxxx_dyn(exo_iter_1,nu+endo_iter_1,exo_iter_2,:)=dr.ghxuu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nu*nu+(FV_exo_order(exo_iter_1)-1)*nu+FV_exo_order(exo_iter_2));
gxxx_dyn(exo_iter_1,exo_iter_2,nu+endo_iter_1,:)=dr.ghxuu(FV_endo_order,(FV_endo_state_order(endo_iter_1)-1)*nu*nu+(FV_exo_order(exo_iter_1)-1)*nu+FV_exo_order(exo_iter_2));
end
end
end
% deal with Lambda, the perturbation parameter
gxxx_dyn(14,14,1:5,:)=oo_.dr.ghuss(FV_endo_order,FV_exo_order)';
gxxx_dyn(14,1:5,14,:)=oo_.dr.ghuss(FV_endo_order,FV_exo_order)';
gxxx_dyn(1:5,14,14,:)=oo_.dr.ghuss(FV_endo_order,FV_exo_order)';
gxxx_dyn(14,14,nu+1:13,:)=oo_.dr.ghxss(FV_endo_order,FV_endo_state_order)';
gxxx_dyn(14,14,14,:)=0;
gxxx_dyn(14,nu+1:13,14,:)=oo_.dr.ghxss(FV_endo_order,FV_endo_state_order)';
gxxx_dyn(nu+1:13,14,14,:)=oo_.dr.ghxss(FV_endo_order,FV_endo_state_order)';
if max(max(max(max(abs(gxxx-gxxx_dyn))))) > 1e-9
max(max(max(max(abs(gxxx-gxxx_dyn)))))
error('Third order wrong')
else max(max(max(max(abs(gxxx-gxxx_dyn)))))
end

Binary file not shown.