From 7ead968240b0f3e9ae7e03b0200460fbff4c5973 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 17 Jul 2014 20:28:16 +0200 Subject: [PATCH 1/8] Add info to dynamic and static file headers that auxiliary equations may be present --- preprocessor/DynamicModel.cc | 3 ++- preprocessor/StaticModel.cc | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index ca2051329..0104c3682 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -1523,7 +1523,8 @@ DynamicModel::writeDynamicMFile(const string &dynamic_basename) const << "%" << endl << "% Outputs:" << endl << "% residual [M_.endo_nbr by 1] double vector of residuals of the dynamic model equations in order of " << endl - << "% declaration of the equations" << endl + << "% declaration of the equations." << endl + << "% Dynare may prepend auxiliary equations, see M_.aux_vars" << endl << "% g1 [M_.endo_nbr by #dynamic variables] double Jacobian matrix of the dynamic model equations;" << endl << "% rows: equations in order of declaration" << endl << "% columns: variables in order stored in M_.lead_lag_incidence" << endl diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index 18be48f11..09fb9261b 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1159,7 +1159,8 @@ StaticModel::writeStaticMFile(const string &func_name) const << "%" << endl << "% Outputs:" << endl << "% residual [M_.endo_nbr by 1] double vector of residuals of the static model equations " << endl - << "% in order of declaration of the equations" << endl + << "% in order of declaration of the equations." << endl + << "% Dynare may prepend auxiliary equations, see M_.aux_vars" << endl << "% g1 [M_.endo_nbr by M_.endo_nbr] double Jacobian matrix of the static model equations;" << endl << "% columns: variables in declaration order" << endl << "% rows: equations in order of declaration" << endl From 3f72f394aa9210d6dc882775425426480b8c3ec6 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 18 Jul 2014 08:53:51 +0200 Subject: [PATCH 2/8] Cosmetic fixes to dyn_ramsey_static.m Also makes sure that the check flag is returned and the steady state file always called if it exists. Otherwise, parameters might not be updated and NaN not detected. --- matlab/dyn_ramsey_static.m | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m index 32eaa745b..d0ac9455f 100644 --- a/matlab/dyn_ramsey_static.m +++ b/matlab/dyn_ramsey_static.m @@ -14,7 +14,7 @@ function [steady_state,params,check] = dyn_ramsey_static(x,M,options_,oo) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2003-2012 Dynare Team +% Copyright (C) 2003-2014 Dynare Team % % This file is part of Dynare. % @@ -32,7 +32,6 @@ function [steady_state,params,check] = dyn_ramsey_static(x,M,options_,oo) % along with Dynare. If not, see . -steady_state = []; params = M.params; check = 0; options_.steadystate.nocheck = 1; @@ -40,12 +39,11 @@ options_.steadystate.nocheck = 1; nl_func = @(x) dyn_ramsey_static_1(x,M,options_,oo); % check_static_model is a subfunction -if check_static_model(oo.steady_state,M,options_,oo) +if check_static_model(oo.steady_state,M,options_,oo) && ~options_.steadystate_flag steady_state = oo.steady_state; return elseif options_.steadystate_flag k_inst = []; - instruments = options_.instruments; inst_nbr = size(options_.instruments,1); for i = 1:inst_nbr k_inst = [k_inst; strmatch(options_.instruments(i,:), ... @@ -54,16 +52,16 @@ elseif options_.steadystate_flag if inst_nbr == 1 inst_val = csolve(nl_func,oo.steady_state(k_inst),'',options_.solve_tolf,100); else - [inst_val,info1] = dynare_solve(nl_func,ys(k_inst),0); + [inst_val,check] = dynare_solve(nl_func,ys(k_inst),0); end ys(k_inst) = inst_val; exo_ss = [oo.exo_steady_state oo.exo_det_steady_state]; [xx,params,check] = evaluate_steady_state_file(ys,exo_ss,M,options_); - [junk,jun,steady_state] = nl_func(inst_val); + [junk,junk,steady_state] = nl_func(inst_val); else n_var = M.orig_endo_nbr; xx = oo.steady_state(1:n_var); - [xx,info1] = dynare_solve(nl_func,xx,0); + [xx,check] = dynare_solve(nl_func,xx,0); [junk,junk,steady_state] = nl_func(xx); end @@ -78,24 +76,14 @@ mult = []; params = M.params; endo_nbr = M.endo_nbr; endo_names = M.endo_names; -exo_nbr = M.exo_nbr; orig_endo_nbr = M.orig_endo_nbr; aux_vars_type = [M.aux_vars.type]; orig_endo_aux_nbr = orig_endo_nbr + min(find(aux_vars_type == 6)) - 1; orig_eq_nbr = M.orig_eq_nbr; inst_nbr = orig_endo_aux_nbr - orig_eq_nbr; % indices of Lagrange multipliers -i_mult = [orig_endo_aux_nbr+(1:orig_eq_nbr)]'; fname = M.fname; -max_lead = M.maximum_lead; -max_lag = M.maximum_lag; -% indices of all endogenous variables -i_endo = [1:endo_nbr]'; -% indices of endogenous variable except instruments -% i_inst = M.instruments; -% lead_lag incidence matrix -i_lag = M.lead_lag_incidence; if options_.steadystate_flag k_inst = []; @@ -130,7 +118,6 @@ end % value and Jacobian of objective function ex = zeros(1,M.exo_nbr); [U,Uy,Uyy] = feval([fname '_objective_static'],x,ex, params); -Uy = Uy'; Uyy = reshape(Uyy,endo_nbr,endo_nbr); % set multipliers and auxiliary variables that From 7c09435e5e3a49c5631520c6a549b629a12d4a97 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 18 Jul 2014 12:35:52 +0200 Subject: [PATCH 3/8] Fix bug in dyn_ramsey_static.m with steady state file that resulted in incorrect steady state when running twice The x returned by a steady state file includes Lagrange multipliers. When starting with 0 multipliers, the steady state of the multipliers is computed correctly. But when running it a second time like in a subsequent call to stoch_simul, the algorithm thinks that elements of xx are 0 which they aren't. As a result, it finds minus the multiplier as the solution, resulting in an output of 0 for all multipliers. --- matlab/dyn_ramsey_static.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m index d0ac9455f..96463aeb5 100644 --- a/matlab/dyn_ramsey_static.m +++ b/matlab/dyn_ramsey_static.m @@ -34,7 +34,7 @@ function [steady_state,params,check] = dyn_ramsey_static(x,M,options_,oo) params = M.params; check = 0; -options_.steadystate.nocheck = 1; +options_.steadystate.nocheck = 1; %disable checking because Lagrange multipliers are not accounted for in evaluate_steady_state_file % dyn_ramsey_static_1 is a subfunction nl_func = @(x) dyn_ramsey_static_1(x,M,options_,oo); @@ -100,7 +100,7 @@ if options_.steadystate_flag end xx = zeros(endo_nbr,1); -xx(1:length(x)) = x; +xx(1:M.orig_endo_nbr) = x(1:M.orig_endo_nbr); %take care of steady state file that will also return multipliers % setting steady state of auxiliary variables % that depends on original endogenous variables if any([M.aux_vars.type] ~= 6) From 27572277d00d55dd7ab995fffe6193cb508b6339 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 18 Jul 2014 12:41:28 +0200 Subject: [PATCH 4/8] Test the result of steady state computation for correctness - Checks correctness of steady state file - Tests whether result of computation really is a steady state - Also eliminates case that can never be reached --- matlab/evaluate_steady_state.m | 125 ++++++++++++++++++++++++++++++--- matlab/print_info.m | 12 ++++ 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/matlab/evaluate_steady_state.m b/matlab/evaluate_steady_state.m index dcaa56133..53d842de7 100644 --- a/matlab/evaluate_steady_state.m +++ b/matlab/evaluate_steady_state.m @@ -52,7 +52,119 @@ function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadysta end if options.ramsey_policy - [ys,params] = dyn_ramsey_static(ys_init,M,options,oo); + if steadystate_flag + % explicit steady state file + [ys,params,info] = evaluate_steady_state_file(ys_init,exo_ss,M, ... + options); + %test whether it solves model conditional on the instruments + [resids] = evaluate_static_model(ys,exo_ss,params,M,options); + n_multipliers=M.endo_nbr-M.orig_endo_nbr; + nan_indices=find(isnan(resids(n_multipliers+1:end))); + + if ~isempty(nan_indices) + fprintf('\nevaluate_steady_state: The steady state file computation for the Ramsey problem resulted in NaNs.\n') + fprintf('evaluate_steady_state: The steady state was computed conditional on the following initial instrument values: \n') + for ii = 1:size(options.instruments,1); + fprintf('\t %s \t %f \n',options.instruments(ii,:),ys_init(strmatch(options.instruments(ii,:),M.endo_names,'exact'))) + end + fprintf('evaluate_steady_state: The problem occured in the following equations: \n') + fprintf('\t Equation(s): ') + for ii=1:length(nan_indices) + fprintf('%d, ',nan_indices(ii)); + end + skipline(); + fprintf('evaluate_steady_state: If those initial values are not admissable, change them using an initval-block.\n') + skipline(2); + check=1; + info(1) = 84; + info(2) = resids'*resids; + return; + end + if max(abs(resids(n_multipliers+1:end))) > options.dynatol.f %does it solve for all variables except for the Lagrange multipliers + fprintf('\nevaluate_steady_state: The steady state file does not solve the steady state for the Ramsey problem.\n') + fprintf('evaluate_steady_state: Conditional on the following instrument values: \n') + for ii = 1:size(options.instruments,1); + fprintf('\t %s \t %f \n',options.instruments(ii,:),ys_init(strmatch(options.instruments(ii,:),M.endo_names,'exact'))) + end + fprintf('evaluate_steady_state: the following equations have non-zero residuals: \n') + for ii=n_multipliers+1:M.endo_nbr + if abs(resids(ii)) > options.dynatol.f/100 + fprintf('\t Equation number %d: %f\n',ii-n_multipliers, resids(ii)) + end + end + skipline(2); + info(1) = 85; + info(2) = resids'*resids; + return; + end + end + %either if no steady state file or steady state file without problems + [ys,params,info] = dyn_ramsey_static(ys_init,M,options,oo); + if info + info=81;%case should not happen + return; + end + %check whether steady state really solves the model + [resids] = evaluate_static_model(ys,exo_ss,params,M,options); + + n_multipliers=M.endo_nbr-M.orig_endo_nbr; + nan_indices_multiplier=find(isnan(resids(1:n_multipliers))); + nan_indices=find(isnan(resids(n_multipliers+1:end))); + + if ~isempty(nan_indices) + fprintf('\nevaluate_steady_state: The steady state computation for the Ramsey problem resulted in NaNs.\n') + fprintf('evaluate_steady_state: The steady state computation resulted in the following instrument values: \n') + for i = 1:size(options.instruments,1); + fprintf('\t %s \t %f \n',options.instruments(i,:),ys(strmatch(options.instruments(i,:),M.endo_names,'exact'))) + end + fprintf('evaluate_steady_state: The problem occured in the following equations: \n') + fprintf('\t Equation(s): ') + for ii=1:length(nan_indices) + fprintf('%d, ',nan_indices(ii)); + end + skipline(); + info(1) = 82; + return; + end + + if ~isempty(nan_indices_multiplier) + fprintf('\nevaluate_steady_state: The steady state computation for the Ramsey problem resulted in NaNs in the auxiliary equations.\n') + fprintf('evaluate_steady_state: The steady state computation resulted in the following instrument values: \n') + for i = 1:size(options.instruments,1); + fprintf('\t %s \t %f \n',options.instruments(i,:),ys(strmatch(options.instruments(i,:),M.endo_names,'exact'))) + end + fprintf('evaluate_steady_state: The problem occured in the following equations: \n') + fprintf('\t Auxiliary equation(s): ') + for ii=1:length(nan_indices_multiplier) + fprintf('%d, ',nan_indices_multiplier(ii)); + end + skipline(); + info(1) = 83; + return; + end + + if max(abs(resids)) > options.dynatol.f %does it solve for all variables including the auxiliary ones + fprintf('\nevaluate_steady_state: The steady state for the Ramsey problem could not be computed.\n') + fprintf('evaluate_steady_state: The steady state computation stopped with the following instrument values:: \n') + for i = 1:size(options.instruments,1); + fprintf('\t %s \t %f \n',options.instruments(i,:),ys_init(strmatch(options.instruments(i,:),M.endo_names,'exact'))) + end + fprintf('evaluate_steady_state: The following equations have non-zero residuals: \n') + for ii=1:n_multipliers + if abs(resids(ii)) > options.dynatol.f/100 + fprintf('\t Auxiliary Ramsey equation number %d: %f\n',ii, resids(ii)) + end + end + for ii=n_multipliers+1:M.endo_nbr + if abs(resids(ii)) > options.dynatol.f/100 + fprintf('\t Equation number %d: %f\n',ii-n_multipliers, resids(ii)) + end + end + skipline(2); + info(1) = 81; + info(2) = resids'*resids; + return; + end elseif steadystate_flag % explicit steady state file [ys,params,info] = evaluate_steady_state_file(ys_init,exo_ss,M, ... @@ -109,13 +221,8 @@ function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadysta end if check - if options.steadystate_flag - info(1)= 19; - resid = check1 ; - else - info(1)= 20; - resid = evaluate_static_model(ys_init,exo_ss,params,M,options); - end + info(1)= 20; + resid = evaluate_static_model(ys_init,exo_ss,params,M,options); info(2) = resid'*resid ; if isnan(info(2)) info(1)=22; @@ -161,3 +268,5 @@ function [ys,params,info] = evaluate_steady_state(ys_init,M,options,oo,steadysta return end + + diff --git a/matlab/print_info.m b/matlab/print_info.m index 34017e513..49bdc7e79 100644 --- a/matlab/print_info.m +++ b/matlab/print_info.m @@ -119,6 +119,18 @@ if ~noprint case 72 error(['Calibrated covariance of the measurement errors implies correlation larger than +-1.']); % Aim Code Conversions by convertAimCodeToInfo.m + case 81 + error(['Ramsey: The solution to the static first order conditions for optimal policy could not be found. Either the model' ... + ' doesn''t have a steady state, there are an infinity of steady states, ' ... + ' or the guess values are too far from the solution']); + case 82 + error(['Ramsey: The steady state computation resulted in NaN in the static first order conditions for optimal policy']); + case 83 + error(['Ramsey: The steady state computation resulted in NaN in the auxiliary equations for optimal policy']); + case 84 + error(['Ramsey: The steady state file computation for the Ramsey problem resulted in NaNs at the initial values of the instruments']); + case 85 + error(['Ramsey: The steady state file does not solve the static first order conditions conditional on the instruments.']); case 102 error('Aim: roots not correctly computed by real_schur'); case 103 From 6fb787711394b95b6755c9942e30b5078f4e8c6c Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 18 Jul 2014 13:31:01 +0200 Subject: [PATCH 5/8] Add bunch of unit test checking the correctness of Ramsey computation with initval and steady state files --- tests/Makefile.am | 10 ++- tests/optimal_policy/Ramsey/ramsey_ex.mod | 63 +++++++++++++++++++ .../Ramsey/ramsey_ex_initval.mod | 52 +++++++++++++++ .../Ramsey/ramsey_ex_initval_AR2.mod | 50 +++++++++++++++ .../Ramsey/ramsey_ex_wrong_ss_file.mod | 41 ++++++++++++ 5 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 tests/optimal_policy/Ramsey/ramsey_ex.mod create mode 100644 tests/optimal_policy/Ramsey/ramsey_ex_initval.mod create mode 100644 tests/optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod create mode 100644 tests/optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod diff --git a/tests/Makefile.am b/tests/Makefile.am index 60ac51c55..ee6a32439 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -26,6 +26,9 @@ MODFILES = \ optimal_policy/nk_ramsey_expectation.mod \ optimal_policy/nk_ramsey_expectation_a.mod \ optimal_policy/mult_elimination_test.mod \ + optimal_policy/Ramsey/ramsey_ex_initval.mod \ + optimal_policy/Ramsey/ramsey_ex.mod \ + optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod \ discretionary_policy/dennis_1.mod \ ramst_initval_file.mod \ ramst_normcdf_and_friends.mod \ @@ -182,7 +185,8 @@ MODFILES = \ reporting/example1.mod XFAIL_MODFILES = ramst_xfail.mod \ - estim_param_in_shock_value.mod + estim_param_in_shock_value.mod \ + optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod \ # Dependencies example1_use_dll.m.trs: example1.m.trs @@ -344,7 +348,9 @@ EXTRA_DIST = \ filter_step_ahead/fs2000_filter_step_ahead_bayesian_steadystate.m \ filter_step_ahead/fs2000_filter_step_ahead_ML_steadystate.m \ loglinear/results_exp.mat \ - smoother2histval/fsdat_simul.m + smoother2histval/fsdat_simul.m \ + optimal_policy/Ramsey/find_c.m \ + optimal_policy/Ramsey/oo_ramsey_policy_initval.mat \ TARGETS = diff --git a/tests/optimal_policy/Ramsey/ramsey_ex.mod b/tests/optimal_policy/Ramsey/ramsey_ex.mod new file mode 100644 index 000000000..d28882f97 --- /dev/null +++ b/tests/optimal_policy/Ramsey/ramsey_ex.mod @@ -0,0 +1,63 @@ +/* Mod file tests the correctness of the Ramsey command when used together with a steady state file by + * - checking whether the results coincide with the ones when used with an initval block + * - checking whether between stoch_simul and ramsey_planner are consistent + * The example is taken from Juillard, Michel (2011): User manual for optimal policy package, + * MONFISPOL FP7 project SSH-225149, Deliverable 1.1.2 +*/ + +var pai, c, n, r, a; +varexo u; +parameters beta, rho, epsilon, omega, phi, gamma; + +beta=0.99; +gamma=3; +omega=17; +epsilon=8; +phi=1; +rho=0.95; + +model; +a = rho*a(-1)+u; +1/c = beta*r/(c(+1)*pai(+1)); +pai*(pai-1)/c = beta*pai(+1)*(pai(+1)-1)/c(+1)+epsilon*phi*n^(gamma+1)/omega -exp(a)*n*(epsilon-1)/(omega*c); +exp(a)*n = c+(omega/2)*(pai-1)^2; +end; + +initval; +r=1; +end; + +steady_state_model; +a = 0; +pai = beta*r; +c = find_c(0.96,pai,beta,epsilon,phi,gamma,omega); +n = c+(omega/2)*(pai-1)^2; +end; + +shocks; +var u; stderr 0.008; +end; + +planner_objective(ln(c)-phi*((n^(1+gamma))/(1+gamma))); +ramsey_policy(planner_discount=0.99,order=1,instruments=(r)); +oo_ramsey_policy_steady_state_file=oo_; +stoch_simul(periods=0, order=1, irf=25, nograph); +if max(abs((oo_ramsey_policy_steady_state_file.steady_state-oo_.steady_state)))>1e-5 ... + || max(abs(oo_ramsey_policy_steady_state_file.dr.ys-oo_.dr.ys))>1e-5 ... + || max(max(abs(oo_ramsey_policy_steady_state_file.dr.ghx-oo_.dr.ghx)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_steady_state_file.dr.ghu-oo_.dr.ghu)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_steady_state_file.dr.Gy-oo_.dr.Gy)))>1e-5 ... + || max(abs((oo_ramsey_policy_steady_state_file.planner_objective_value-oo_.planner_objective_value)))>1e-5 + error('Running stoch_simul after ramsey_policy leads to inconsistent results') +end + +load oo_ramsey_policy_initval; + +if max(abs((oo_ramsey_policy_initval.steady_state-oo_.steady_state)))>1e-5 ... + || max(abs(oo_ramsey_policy_initval.dr.ys-oo_.dr.ys))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.ghx-oo_.dr.ghx)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.ghu-oo_.dr.ghu)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.Gy-oo_.dr.Gy)))>1e-5 ... + || max(abs((oo_ramsey_policy_initval.planner_objective_value-oo_.planner_objective_value)))>1e-5 + error('Initval and steady state file yield different results') +end diff --git a/tests/optimal_policy/Ramsey/ramsey_ex_initval.mod b/tests/optimal_policy/Ramsey/ramsey_ex_initval.mod new file mode 100644 index 000000000..170b956f9 --- /dev/null +++ b/tests/optimal_policy/Ramsey/ramsey_ex_initval.mod @@ -0,0 +1,52 @@ +/* Mod file tests the functioning of the Ramsey command when used together with an initval-block + * - Tests whether subsequent calls to ramsey_policy and stoch_simul are possible + * The example is taken from Juillard, Michel (2011): User manual for optimal policy package, + * MONFISPOL FP7 project SSH-225149, Deliverable 1.1.2 +*/ + +var pai, c, n, r, a; +varexo u; +parameters beta, rho, epsilon, omega, phi, gamma; + +beta=0.99; +gamma=3; +omega=17; +epsilon=8; +phi=1; +rho=0.95; + +model; +a = rho*a(-1)+u; +1/c = beta*r/(c(+1)*pai(+1)); +pai*(pai-1)/c = beta*pai(+1)*(pai(+1)-1)/c(+1)+epsilon*phi*n^(gamma+1)/omega -exp(a)*n*(epsilon-1)/(omega*c); +exp(a)*n = c+(omega/2)*(pai-1)^2; +end; + +initval; +r=1; +end; + +initval; +a = 0; +pai = beta; +c = 0.9665; +n = 0.9673; +end; + +shocks; +var u; stderr 0.008; +end; + +planner_objective(ln(c)-phi*((n^(1+gamma))/(1+gamma))); +ramsey_policy(planner_discount=0.99,order=1,instruments=(r)); +oo_ramsey_policy_initval=oo_; +%save oo_ramsey_policy_initval oo_ramsey_policy_initval +stoch_simul(periods=0, order=1, irf=25, nograph); +if max(abs((oo_ramsey_policy_initval.steady_state-oo_.steady_state)))>1e-5 ... + || max(abs(oo_ramsey_policy_initval.dr.ys-oo_.dr.ys))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.ghx-oo_.dr.ghx)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.ghu-oo_.dr.ghu)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval.dr.Gy-oo_.dr.Gy)))>1e-5 ... + || max(abs((oo_ramsey_policy_initval.planner_objective_value-oo_.planner_objective_value)))>1e-5 + error('Running stoch_simul after ramsey_policy leads to inconsistent results') +end diff --git a/tests/optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod b/tests/optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod new file mode 100644 index 000000000..c10204922 --- /dev/null +++ b/tests/optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod @@ -0,0 +1,50 @@ +/* Mod file tests the correctness of the Ramsey command when Auxiliary variables for AR2 are used + * The example is adapted from Juillard, Michel (2011): User manual for optimal policy package, + * MONFISPOL FP7 project SSH-225149, Deliverable 1.1.2 +*/ + +var pai, c, n, r, a; +varexo u; +parameters beta, rho, epsilon, omega, phi, gamma; + +beta=0.99; +gamma=3; +omega=17; +epsilon=8; +phi=1; +rho=0.95; + +model; +log(a) = rho*log(a(-2))+u; +1/c = beta*r/(c(+1)*pai(+1)); +pai*(pai-1)/c = beta*pai(+1)*(pai(+1)-1)/c(+1)+epsilon*phi*n^(gamma+1)/omega -a*n*(epsilon-1)/(omega*c); +a*n = c+(omega/2)*(pai-1)^2; +end; + +initval; +r=1; +end; + +initval; +a = 1; +pai = beta; +c = 0.9665; +n = 0.9673; +end; + +shocks; +var u; stderr 0.008; +end; + +planner_objective(ln(c)-phi*((n^(1+gamma))/(1+gamma))); +ramsey_policy(planner_discount=0.99,order=1,instruments=(r)); +oo_ramsey_policy_initval_AR2=oo_; +stoch_simul(periods=0, order=1, irf=25, nograph); +if max(abs((oo_ramsey_policy_initval_AR2.steady_state-oo_.steady_state)))>1e-5 ... + || max(abs(oo_ramsey_policy_initval_AR2.dr.ys-oo_.dr.ys))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval_AR2.dr.ghx-oo_.dr.ghx)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval_AR2.dr.ghu-oo_.dr.ghu)))>1e-5 ... + || max(max(abs(oo_ramsey_policy_initval_AR2.dr.Gy-oo_.dr.Gy)))>1e-5 ... + || max(abs((oo_ramsey_policy_initval_AR2.planner_objective_value-oo_.planner_objective_value)))>1e-5 + error('Running stoch_simul after ramsey_policy leads to inconsistent results') +end \ No newline at end of file diff --git a/tests/optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod b/tests/optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod new file mode 100644 index 000000000..3b44bad17 --- /dev/null +++ b/tests/optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod @@ -0,0 +1,41 @@ +/* Mod file tests whether the Ramsey command correctly filters out an incorrect steady state + * The example is taken from Juillard, Michel (2011): User manual for optimal policy package, + * MONFISPOL FP7 project SSH-225149, Deliverable 1.1.2 +*/ + + +var pai, c, n, r, a; +varexo u; +parameters beta, rho, epsilon, omega, phi, gamma; + +beta=0.99; +gamma=3; +omega=17; +epsilon=8; +phi=1; +rho=0.95; + +model; +a = rho*a(-1)+u; +1/c = beta*r/(c(+1)*pai(+1)); +pai*(pai-1)/c = beta*pai(+1)*(pai(+1)-1)/c(+1)+epsilon*phi*n^(gamma+1)/omega -exp(a)*n*(epsilon-1)/(omega*c); +exp(a)*n = c+(omega/2)*(pai-1)^2; +end; + +initval; +r=1; +end; + +steady_state_model; +a = 1; +pai = beta*r; +c = find_c(0.96,pai,beta,epsilon,phi,gamma,omega); +n = c+(omega/2)*(pai-1)^2; +end; + +shocks; +var u; stderr 0.008; +end; + +planner_objective(ln(c)-phi*((n^(1+gamma))/(1+gamma))); +ramsey_policy(planner_discount=0.99,order=1,instruments=(r)); \ No newline at end of file From 36222cb40d34dd36d629d3ca14ea17d6ca143b48 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 18 Jul 2014 14:05:12 +0200 Subject: [PATCH 6/8] Account for presence of Lagrange multipliers and auxiliary equations in display_problematic_vars_Jacobian.m --- matlab/display_problematic_vars_Jacobian.m | 63 +++++++++++++++++++--- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/matlab/display_problematic_vars_Jacobian.m b/matlab/display_problematic_vars_Jacobian.m index 0cb42b581..b77c60804 100644 --- a/matlab/display_problematic_vars_Jacobian.m +++ b/matlab/display_problematic_vars_Jacobian.m @@ -36,6 +36,11 @@ function []=display_problematic_vars_Jacobian(problemrow,problemcol,M_,x,type,ca if nargin<6 caller_string=''; end +aux_vars_type = [M_.aux_vars.type]; +aux_eq_nbr = max(find(aux_vars_type == 6)); +if isempty(aux_eq_nbr) + aux_eq_nbr=0; +end if strcmp(type,'dynamic') for ii=1:length(problemrow) [var_row,var_index]=find(M_.lead_lag_incidence==problemcol(ii)); @@ -47,10 +52,33 @@ if strcmp(type,'dynamic') type_string='lead of'; end if var_index<=M_.orig_endo_nbr - fprintf('Derivative of Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',problemrow(ii),type_string,deblank(M_.endo_names(var_index,:)),deblank(M_.endo_names(var_index,:)),x(var_index)) + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + fprintf('Derivative of Auxiliary Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',eq_nbr,type_string,deblank(M_.endo_names(var_index,:)),deblank(M_.endo_names(var_index,:)),x(var_index)) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + fprintf('Derivative of Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',eq_nbr,type_string,deblank(M_.endo_names(var_index,:)),deblank(M_.endo_names(var_index,:)),x(var_index)) + end else %auxiliary vars - orig_var_index=M_.aux_vars(1,var_index-M_.orig_endo_nbr).orig_index; - fprintf('Derivative of Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',problemrow(ii),type_string,deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(orig_var_index)) + if M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).type ==6 %Ramsey Lagrange Multiplier + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + fprintf('Derivative of Auxiliary Equation %d with respect to %s of Langrange multiplier of equation %s (initial value: %g) \n',eq_nbr,type_string,M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).eq_nbr,x(problemcol(ii))) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + fprintf('Derivative of Equation %d with respect to %s of Langrange multiplier of equation %s (initial value: %g) \n',eq_nbr,type_string,M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).eq_nbr,x(problemcol(ii))) + end + else + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + orig_var_index=M_.aux_vars(1,var_index-M_.orig_endo_nbr).orig_index; + fprintf('Derivative of Auxiliary Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',eq_nbr,type_string,deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(orig_var_index)) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + orig_var_index=M_.aux_vars(1,var_index-M_.orig_endo_nbr).orig_index; + fprintf('Derivative of Equation %d with respect to %s Variable %s (initial value of %s: %g) \n',eq_nbr,type_string,deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(orig_var_index)) + end + end end end fprintf('\n%s The problem most often occurs, because a variable with\n',caller_string) @@ -59,10 +87,33 @@ if strcmp(type,'dynamic') elseif strcmp(type,'static') for ii=1:length(problemrow) if problemcol(ii)<=M_.orig_endo_nbr - fprintf('Derivative of Equation %d with respect to Variable %s (initial value of %s: %g) \n',problemrow(ii),deblank(M_.endo_names(problemcol(ii),:)),deblank(M_.endo_names(problemcol(ii),:)),x(problemcol(ii))) + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + fprintf('Derivative of Auxiliary Equation %d with respect to Variable %s (initial value of %s: %g) \n',eq_nbr,deblank(M_.endo_names(problemcol(ii),:)),deblank(M_.endo_names(problemcol(ii),:)),x(problemcol(ii))) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + fprintf('Derivative of Equation %d with respect to Variable %s (initial value of %s: %g) \n',eq_nbr,deblank(M_.endo_names(problemcol(ii),:)),deblank(M_.endo_names(problemcol(ii),:)),x(problemcol(ii))) + end else %auxiliary vars - orig_var_index=M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).orig_index; - fprintf('Derivative of Equation %d with respect to Variable %s (initial value of %s: %g) \n',problemrow(ii),deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(problemcol(ii))) + if M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).type ==6 %Ramsey Lagrange Multiplier + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + fprintf('Derivative of Auxiliary Equation %s with respect to Lagrange multiplier of equation %d (initial value: %g) \n',eq_nbr,M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).eq_nbr,x(problemcol(ii))) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + fprintf('Derivative of Equation %s with respect to Lagrange multiplier of equation %d (initial value: %g) \n',eq_nbr,M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).eq_nbr,x(problemcol(ii))) + end + else + if problemrow(ii)<=aux_eq_nbr + eq_nbr=problemrow(ii); + orig_var_index=M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).orig_index; + fprintf('Derivative of Auxiliary Equation %d with respect to Variable %s (initial value of %s: %g) \n',eq_nbr,deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(problemcol(ii))) + else + eq_nbr=problemrow(ii)-aux_eq_nbr; + orig_var_index=M_.aux_vars(1,problemcol(ii)-M_.orig_endo_nbr).orig_index; + fprintf('Derivative of Equation %d with respect to Variable %s (initial value of %s: %g) \n',eq_nbr,deblank(M_.endo_names(orig_var_index,:)),deblank(M_.endo_names(orig_var_index,:)),x(problemcol(ii))) + end + end end end fprintf('\n%s The problem most often occurs, because a variable with\n',caller_string) From 59da9953bbc9c93bda95131a80217ecb41b942eb Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 30 Jul 2014 13:07:52 +0200 Subject: [PATCH 7/8] Fix formating for Makefile.am Replaced spaces by tabs --- tests/Makefile.am | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index ee6a32439..dc9e0324c 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -26,9 +26,9 @@ MODFILES = \ optimal_policy/nk_ramsey_expectation.mod \ optimal_policy/nk_ramsey_expectation_a.mod \ optimal_policy/mult_elimination_test.mod \ - optimal_policy/Ramsey/ramsey_ex_initval.mod \ - optimal_policy/Ramsey/ramsey_ex.mod \ - optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod \ + optimal_policy/Ramsey/ramsey_ex_initval.mod \ + optimal_policy/Ramsey/ramsey_ex.mod \ + optimal_policy/Ramsey/ramsey_ex_initval_AR2.mod \ discretionary_policy/dennis_1.mod \ ramst_initval_file.mod \ ramst_normcdf_and_friends.mod \ @@ -186,7 +186,7 @@ MODFILES = \ XFAIL_MODFILES = ramst_xfail.mod \ estim_param_in_shock_value.mod \ - optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod \ + optimal_policy/Ramsey/ramsey_ex_wrong_ss_file.mod \ # Dependencies example1_use_dll.m.trs: example1.m.trs @@ -349,8 +349,8 @@ EXTRA_DIST = \ filter_step_ahead/fs2000_filter_step_ahead_ML_steadystate.m \ loglinear/results_exp.mat \ smoother2histval/fsdat_simul.m \ - optimal_policy/Ramsey/find_c.m \ - optimal_policy/Ramsey/oo_ramsey_policy_initval.mat \ + optimal_policy/Ramsey/find_c.m \ + optimal_policy/Ramsey/oo_ramsey_policy_initval.mat \ TARGETS = From 4b49abd7247e1f02df1aa551a942f6d5e8580df5 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Sun, 7 Sep 2014 21:15:56 +0200 Subject: [PATCH 8/8] Add missing files for Ramsey unit test with steady state. --- tests/optimal_policy/Ramsey/find_c.m | 8 ++++++++ .../Ramsey/oo_ramsey_policy_initval.mat | Bin 0 -> 2896 bytes 2 files changed, 8 insertions(+) create mode 100644 tests/optimal_policy/Ramsey/find_c.m create mode 100644 tests/optimal_policy/Ramsey/oo_ramsey_policy_initval.mat diff --git a/tests/optimal_policy/Ramsey/find_c.m b/tests/optimal_policy/Ramsey/find_c.m new file mode 100644 index 000000000..badafbf98 --- /dev/null +++ b/tests/optimal_policy/Ramsey/find_c.m @@ -0,0 +1,8 @@ +function c = find_c(c0,pai,beta,epsilon,phi,gamma,omega) +c = csolve(@nk_ss,c0,[],1e-8,100,pai,beta,epsilon,phi,gamma,omega); +end + +function r = nk_ss(c,pai,beta,epsilon,phi,gamma,omega) +r = pai*(pai-1)/c - beta*pai*(pai-1)/c-epsilon*phi*(c+(omega/2)*(pai-1)^2)^(gamma+1)/omega +(c+(omega/2)*(pai-1)^2)*(epsilon-1)/(omega*c); + +end \ No newline at end of file diff --git a/tests/optimal_policy/Ramsey/oo_ramsey_policy_initval.mat b/tests/optimal_policy/Ramsey/oo_ramsey_policy_initval.mat new file mode 100644 index 0000000000000000000000000000000000000000..ff829dd9dc5fbf2a4d9b0d649c50e1537c7a672f GIT binary patch literal 2896 zcmV-W3$OG|K~zjZLLfCRFd$7qR4ry{Y-KDUP;6mzW^ZzBIv`L(S4mDbG%O%Pa%Ew3 zWn>_4ZaN@Fa%mt+b!;FpI3O`IIx#XjF*qPHFflYBARr(hARr(hARr(hARr(hARr(h zARr(hARr(hARr(hARr(hARr(B00000000000ZB~{0002U3IG6job8wiRFlUR$0vkE zRHzCnitLMkK(V5NLWWw=R)wMhDxe`ifIvtvAt4}OP{FoZw^vXBt5sz2i2^FEEcsYn zS{DRu5tZj&S!AiU1+?-elMpeiwdZ)wJI^_1@;mp=%>T}vneQ9;)BymUx&SC6jzU!d zNQ#owCiSUWsIu8q4lRsJkD`XNnGF9ZDucy{;M15&KoPf#``Mu%x`0jPGQxOF@MfX2 z0tRdd;D7-O)`_6g0-_LT5%j+P0D45fHkn^8#6TM25=09Nqfw*!#SNp=SOd+6PaAlm zG+qSTpUvsl$lwHW2kIZrq_J3Z4wdZ}Lidkg@aa^vh&(z_M{PFX^?oQz`}gh#8NA*P zaZ$h?Fzm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