From d84bf3e1078d31622418d764306eaec450cf0976 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 17 Mar 2016 17:46:34 +0100 Subject: [PATCH] Add unit tests for perfect foresight to detect issues with histval --- tests/Makefile.am | 7 ++ .../homotopy_histval.mod | 60 +++++++++++++++ .../multiple_lead_lags/AR2.mod | 57 ++++++++++++++ .../multiple_lead_lags/AR2_forward.mod | 59 ++++++++++++++ .../ramst_augmented_histval.mod | 77 +++++++++++++++++++ 5 files changed, 260 insertions(+) create mode 100644 tests/deterministic_simulations/homotopy_histval.mod create mode 100644 tests/deterministic_simulations/multiple_lead_lags/AR2.mod create mode 100644 tests/deterministic_simulations/multiple_lead_lags/AR2_forward.mod create mode 100644 tests/deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.mod diff --git a/tests/Makefile.am b/tests/Makefile.am index 00f587964..7c0c76824 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -203,6 +203,7 @@ MODFILES = \ deterministic_simulations/rbc_det5.mod \ deterministic_simulations/rbc_det6.mod \ deterministic_simulations/homotopy.mod \ + deterministic_simulations/homotopy_histval.mod \ deterministic_simulations/rbc_det_exo_lag_2a.mod \ deterministic_simulations/rbc_det_exo_lag_2b.mod \ deterministic_simulations/rbc_det_exo_lag_2c.mod \ @@ -215,6 +216,9 @@ MODFILES = \ deterministic_simulations/multiple_lead_lags/sim_lead_lag.mod \ deterministic_simulations/lola_solve_one_boundary.mod \ deterministic_simulations/linear_approximation/sw.mod \ + deterministic_simulations/multiple_lead_lags/AR2.mod \ + deterministic_simulations/multiple_lead_lags/AR2_forward.mod \ + deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.mod \ walsh.mod \ measurement_errors/fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod \ trend_var/fs2000_nonstationary.mod \ @@ -364,6 +368,9 @@ deterministic_simulations/multiple_lead_lags/sim_lead_lag_aux_vars.o.trs: determ deterministic_simulations/multiple_lead_lags/sim_lead_lag.m.trs: deterministic_simulations/multiple_lead_lags/sim_base.m.trs deterministic_simulations/multiple_lead_lags/sim_lead_lag_aux_vars.m.trs deterministic_simulations/multiple_lead_lags/sim_lead_lag.o.trs: deterministic_simulations/multiple_lead_lags/sim_base.o.trs deterministic_simulations/multiple_lead_lags/sim_lead_lag_aux_vars.o.trs +deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.m.trs: ramst.m.trs +deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.o.trs: ramst.o.trs + # Matlab TRS Files M_TRS_FILES = $(patsubst %.mod, %.m.trs, $(MODFILES)) M_TRS_FILES += run_block_byte_tests_matlab.m.trs run_reporting_test_matlab.m.trs run_all_unitary_tests.m.trs diff --git a/tests/deterministic_simulations/homotopy_histval.mod b/tests/deterministic_simulations/homotopy_histval.mod new file mode 100644 index 000000000..6fbc5e96f --- /dev/null +++ b/tests/deterministic_simulations/homotopy_histval.mod @@ -0,0 +1,60 @@ +// Example that triggers homotopy in perfect foresight simulation. + +var Consumption, Capital, LoggedProductivity; + +varexo LoggedProductivityInnovation; + +parameters beta, alpha, delta, rho; + +beta = .985; +alpha = 1/3; +delta = alpha/10; +rho = .9; + +model; + +[name='Euler equation'] // This is an equation tag! +1/Consumption = beta/Consumption(1)*(alpha*exp(LoggedProductivity(1))*Capital^(alpha-1)+1-delta); + +[name='Physical capital stock law of motion'] +Capital = exp(LoggedProductivity)*Capital(-1)^alpha+(1-delta)*Capital(-1)-Consumption; + +[name='Logged productivity law of motion'] +LoggedProductivity = rho*LoggedProductivity(-1)+LoggedProductivityInnovation; + +end; + +% steady_state_model; +% LoggedProductivity = LoggedProductivityInnovation/(1-rho); +% Capital = (exp(LoggedProductivity)*alpha/(1/beta-1+delta))^(1/(1-alpha)); +% Consumption = exp(LoggedProductivity)*Capital^alpha-delta*Capital; +% end; + +set_time(1Q1); + +initval; + LoggedProductivityInnovation = 0; + LoggedProductivity = 10; + Capital = 1; + Consumption=2.02049; +end; +steady; + +histval; + Capital(0)=17.9836; + LoggedProductivity(0)=10; +end; + +// endval; +// LoggedProductivityInnovation = 0; +// end; + +// steady; + +simul(periods=200); + +plot(Simulated_time_series.Capital(1Q1:25Q4)); + +if max(abs(oo_.endo_simul(:,1)-M_.endo_histval))>1e-6 + error('Homotopy changed initial conditions') +end diff --git a/tests/deterministic_simulations/multiple_lead_lags/AR2.mod b/tests/deterministic_simulations/multiple_lead_lags/AR2.mod new file mode 100644 index 000000000..0bc871ca1 --- /dev/null +++ b/tests/deterministic_simulations/multiple_lead_lags/AR2.mod @@ -0,0 +1,57 @@ +var y_backward; + +// Exogenous variable: technology level +varexo x; + +parameters rho_1 rho_2; + +rho_1=0.2; +rho_2=0.1; + +// Equilibrium conditions +model; +y_backward=rho_1*y_backward(-1)+rho_2*y_backward(-2); +end; + +// Set starting value for solver +initval; +y_backward=1; +end; +steady; + +// Set initial conditions for state variables +histval; +y_backward(0)=1; +y_backward(-1)=2; +end; + + +// Check the Blanchard-Kahn conditions +check; + +// Declare a positive technological shock in period 1 +shocks; +var x; +periods 1; +values 1.2; +end; + +// Deterministic simulation of the model for 200 periods +options_.solve_tolf=1e-12; +simul(periods=100); + +// Display the path of consumption and capital +rplot y_backward; + +junk=zeros(1,options_.periods+M_.maximum_lag); +junk(1)=2; +junk(2)=1; + +for ii=3:options_.periods+2 + junk(ii)=M_.params(strmatch('rho_1',M_.param_names,'exact'))*junk(ii-1)+M_.params(strmatch('rho_2',M_.param_names,'exact'))*junk(ii-2); +end + +if max(abs(junk(M_.maximum_lag+1:end)-oo_.endo_simul(strmatch('y_backward',M_.endo_names,'exact'),:)))>1e-10 + error('Solution of purely backwards model not correct') +end + diff --git a/tests/deterministic_simulations/multiple_lead_lags/AR2_forward.mod b/tests/deterministic_simulations/multiple_lead_lags/AR2_forward.mod new file mode 100644 index 000000000..21c2adcc4 --- /dev/null +++ b/tests/deterministic_simulations/multiple_lead_lags/AR2_forward.mod @@ -0,0 +1,59 @@ +var y_backward dummy_var; + +// Exogenous variable: technology level +varexo x; + +parameters rho_1 rho_2; + +rho_1=0.2; +rho_2=0.1; + +// Equilibrium conditions +model; +y_backward=rho_1*y_backward(-1)+rho_2*y_backward(-2); +dummy_var=0.9*dummy_var(+1); +end; + +// Set starting value for solver +initval; +y_backward=1; +end; +steady; + +// Set initial conditions for state variables + +histval; +y_backward(0)=1; +y_backward(-1)=2; +end; + + +// Check the Blanchard-Kahn conditions +check; + +// Declare a positive technological shock in period 1 +shocks; +var x; +periods 1; +values 1.2; +end; + +// Deterministic simulation of the model for 200 periods +options_.solve_tolf=1e-12; +simul(periods=100); + +// Display the path of consumption and capital +rplot y_backward; + +junk=zeros(1,options_.periods+M_.maximum_lag); +junk(1)=2; +junk(2)=1; + +for ii=3:options_.periods+M_.maximum_lag+M_.maximum_lead + junk(ii)=M_.params(strmatch('rho_1',M_.param_names,'exact'))*junk(ii-1)+M_.params(strmatch('rho_2',M_.param_names,'exact'))*junk(ii-2); +end + +if max(abs(junk(M_.maximum_lag+1:end)-oo_.endo_simul(strmatch('y_backward',M_.endo_names,'exact'),1:end-M_.maximum_lead)))>1e-10 + error('Solution of purely backwards model not correct') +end + diff --git a/tests/deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.mod b/tests/deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.mod new file mode 100644 index 000000000..9b417917a --- /dev/null +++ b/tests/deterministic_simulations/multiple_lead_lags/ramst_augmented_histval.mod @@ -0,0 +1,77 @@ +//Version of the ramst.mod augmented with purely backward looking AR-process that should not +//alter any dynamics of original + +// Endogenous variables: consumption and capital +var c k y_backward; + +// Exogenous variable: technology level +varexo x; + +// Parameters declaration and calibration +parameters alph gam delt bet aa rho_1 rho_2; +alph=0.5; +gam=0.5; +delt=0.02; +bet=0.05; +aa=0.5; +rho_1=0.2; +rho_2=0.1; + +// Equilibrium conditions +model; +c + k - aa*x*k(-1)^alph - (1-delt)*k(-1); // Resource constraint +c^(-gam) - (1+bet)^(-1)*(aa*alph*x(+1)*k^(alph-1) + 1 - delt)*c(+1)^(-gam); // Euler equation +y_backward=0.2*y_backward(-1)+0.1*y_backward(-2); +end; + +// Set starting value for solver at steady state +initval; +x = 1; +k = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +c = aa*k^alph-delt*k; +y_backward=1; +end; +steady; + +// Set initial conditions for state variables +histval; +k(0) = ((delt+bet)/(1.0*aa*alph))^(1/(alph-1)); +y_backward(0)=1; +y_backward(-1)=2; +end; + + +// Check the Blanchard-Kahn conditions +check; + +// Declare a positive technological shock in period 1 +shocks; +var x; +periods 1; +values 1.2; +end; + +// Deterministic simulation of the model for 200 periods +simul(periods=200); + +junk=zeros(1,options_.periods+M_.maximum_lag); +junk(1)=2; +junk(2)=1; + +for ii=3:options_.periods+M_.maximum_lag+M_.maximum_lead + junk(ii)=M_.params(strmatch('rho_1',M_.param_names,'exact'))*junk(ii-1)+M_.params(strmatch('rho_2',M_.param_names,'exact'))*junk(ii-2); +end + +if max(abs(junk(M_.maximum_lag+1:end)-oo_.endo_simul(strmatch('y_backward',M_.endo_names,'exact'),1:end-M_.maximum_lead)))>1e-10 + error('Solution of purely backwards model not correct') +end + +ramst_results=load('../../ramst_results.mat'); +if max(abs(ramst_results.oo_.endo_simul(strmatch('k',ramst_results.M_.endo_names,'exact'),1:end-M_.maximum_lead)-oo_.endo_simul(strmatch('k',M_.endo_names,'exact'),1:end-M_.maximum_lead)))>1e-10 + error('Solution of forward part of the model not correct') +end + +if max(abs(ramst_results.oo_.endo_simul(strmatch('c',ramst_results.M_.endo_names,'exact'),2:end-M_.maximum_lead)-oo_.endo_simul(strmatch('c',M_.endo_names,'exact'),2:end-M_.maximum_lead)))>1e-10 + error('Solution of forward part of the model not correct') +end +