From 456dc86ad16669556917a95a852e681bc01c924d Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer <jpfeifer@gmx.de> Date: Fri, 14 May 2021 13:38:42 +0200 Subject: [PATCH] identification: support optimal policy Related to https://git.dynare.org/Dynare/dynare/-/merge_requests/1837 --- matlab/dynare_identification.m | 8 ++++++++ matlab/get_perturbation_params_derivs.m | 12 ++++++++++-- ..._perturbation_params_derivs_numerical_objective.m | 2 +- matlab/identification_analysis.m | 2 +- matlab/identification_numerical_objective.m | 2 +- tests/discretionary_policy/dennis_1_estim.mod | 4 +++- .../Ramsey/Ramsey_Example_estimation.mod | 3 ++- 7 files changed, 26 insertions(+), 7 deletions(-) diff --git a/matlab/dynare_identification.m b/matlab/dynare_identification.m index 196ae7d5e3..8a4515441b 100644 --- a/matlab/dynare_identification.m +++ b/matlab/dynare_identification.m @@ -321,6 +321,14 @@ options_ident = set_default_option(options_ident,'analytic_derivation_mode', opt % 1: kronecker products method to compute analytical derivatives as in Iskrev (2010) (only for order=1) % -1: numerical two-sided finite difference method to compute numerical derivatives of all identification Jacobians using function identification_numerical_objective.m (previously thet2tau.m) % -2: numerical two-sided finite difference method to compute numerically dYss, dg1, dg2, dg3, d2Yss and d2g1, the identification Jacobians are then computed analytically as with 0 + +if options_.discretionary_policy || options_.ramsey_policy + if options_ident.analytic_derivation_mode~=-1 + fprintf('dynare_identification: discretionary_policy and ramsey_policy require analytic_derivation_mode=-1. Resetting the option.') + options_ident.analytic_derivation_mode=-1; + end +end + options_.analytic_derivation_mode = options_ident.analytic_derivation_mode; %overwrite setting in options_ % initialize persistent variables in prior_draw diff --git a/matlab/get_perturbation_params_derivs.m b/matlab/get_perturbation_params_derivs.m index 2deb29da74..f23a58d17c 100644 --- a/matlab/get_perturbation_params_derivs.m +++ b/matlab/get_perturbation_params_derivs.m @@ -344,9 +344,17 @@ if analytic_derivation_mode == -1 %Parameter Jacobian of dynamic model derivatives (wrt selected model parameters only) dYss_g = fjaco(numerical_objective_fname, modparam1, 'dynamic_model', estim_params_model, M, oo, options); ind_Yss = 1:endo_nbr; - ind_g1 = ind_Yss(end) + (1:endo_nbr*yy0ex0_nbr); + if options.discretionary_policy || options.ramsey_policy + ind_g1 = ind_Yss(end) + (1:M.eq_nbr*yy0ex0_nbr); + else + ind_g1 = ind_Yss(end) + (1:endo_nbr*yy0ex0_nbr); + end DERIVS.dYss = dYss_g(ind_Yss, :); %in tensor notation, wrt selected model parameters only - DERIVS.dg1 = reshape(dYss_g(ind_g1,:),[endo_nbr, yy0ex0_nbr, modparam_nbr]); %in tensor notation, wrt selected model parameters only + if options.discretionary_policy || options.ramsey_policy + DERIVS.dg1 = reshape(dYss_g(ind_g1,:),[M.eq_nbr, yy0ex0_nbr, modparam_nbr]); %in tensor notation, wrt selected model parameters only + else + DERIVS.dg1 = reshape(dYss_g(ind_g1,:),[endo_nbr, yy0ex0_nbr, modparam_nbr]); %in tensor notation, wrt selected model parameters only + end if order > 1 ind_g2 = ind_g1(end) + (1:endo_nbr*yy0ex0_nbr^2); DERIVS.dg2 = reshape(sparse(dYss_g(ind_g2,:)),[endo_nbr, yy0ex0_nbr^2*modparam_nbr]); %blockwise in matrix notation, i.e. [dg2_dp1 dg2_dp2 ...], where dg2_dpj has dimension endo_nbr by yy0ex0_nbr^2 diff --git a/matlab/get_perturbation_params_derivs_numerical_objective.m b/matlab/get_perturbation_params_derivs_numerical_objective.m index 5193bc323a..c888eef48b 100644 --- a/matlab/get_perturbation_params_derivs_numerical_objective.m +++ b/matlab/get_perturbation_params_derivs_numerical_objective.m @@ -51,7 +51,7 @@ function [out,info] = get_perturbation_params_derivs_numerical_objective(params, %% Update stderr, corr and model parameters and compute perturbation approximation and steady state with updated parameters M = set_all_parameters(params,estim_params,M); -[~,info,M,options,oo] = resol(0,M,options,oo); +[~,info,M,options,oo] = compute_decision_rules(M,options,oo); Sigma_e = M.Sigma_e; if info(1) > 0 diff --git a/matlab/identification_analysis.m b/matlab/identification_analysis.m index ff773195cf..5f2b47660e 100644 --- a/matlab/identification_analysis.m +++ b/matlab/identification_analysis.m @@ -133,7 +133,7 @@ no_identification_minimal = options_ident.no_identification_minimal; no_identification_spectrum = options_ident.no_identification_spectrum; %Compute linear approximation and fill dr structure -[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); +[oo_.dr,info,M_,options_,oo_] = compute_decision_rules(M_,options_,oo_); if info(1) == 0 %no errors in solution % Compute parameter Jacobians for identification analysis diff --git a/matlab/identification_numerical_objective.m b/matlab/identification_numerical_objective.m index 66112ca6b3..f38575af9d 100644 --- a/matlab/identification_numerical_objective.m +++ b/matlab/identification_numerical_objective.m @@ -76,7 +76,7 @@ else end %% compute Kalman transition matrices and steady state with updated parameters -[~,info,M,options,oo] = resol(0,M,options,oo); +[~,info,M,options,oo] = compute_decision_rules(M,options,oo); options = rmfield(options,'options_ident'); pruned = pruned_state_space_system(M, options, oo.dr, indvar, nlags, useautocorr, 0); diff --git a/tests/discretionary_policy/dennis_1_estim.mod b/tests/discretionary_policy/dennis_1_estim.mod index f869a69e66..ea05d899e8 100644 --- a/tests/discretionary_policy/dennis_1_estim.mod +++ b/tests/discretionary_policy/dennis_1_estim.mod @@ -36,8 +36,10 @@ estimated_params; end; options_.plot_priors=0; -estimation(order = 1, datafile = dennis_simul, mh_replic = 2000, mh_nblocks=1,smoother,bayesian_irf,moments_varendo) y i pi pi_c q; +estimation(order = 1, datafile = dennis_simul, mh_replic = 2000, mh_nblocks=1,smoother,bayesian_irf,moments_varendo, conditional_variance_decomposition=[1,2]) y i pi pi_c q; if max(abs(oo_.posterior.optimization.mode - [1; 0.3433])) > 0.025 error('Posterior mode too far from true parameter values'); end + +identification; \ No newline at end of file diff --git a/tests/optimal_policy/Ramsey/Ramsey_Example_estimation.mod b/tests/optimal_policy/Ramsey/Ramsey_Example_estimation.mod index b51233fc1b..4b7845c17a 100644 --- a/tests/optimal_policy/Ramsey/Ramsey_Example_estimation.mod +++ b/tests/optimal_policy/Ramsey/Ramsey_Example_estimation.mod @@ -222,7 +222,7 @@ end; ramsey_model(instruments=(R),planner_discount=beta,planner_discount_latex_name=$\beta$); //conduct stochastic simulations of the Ramsey problem - stoch_simul(order=1,irf=20,periods=500) pi_ann log_h R_ann log_C Z r_real; + stoch_simul(TeX,order=1,irf=20,periods=500) pi_ann log_h R_ann log_C Z r_real; evaluate_planner_objective; @# if Estimation_under_Ramsey==1 @@ -234,6 +234,7 @@ end; varobs log_C; estimation(datafile=ramsey_simulation,mode_compute=5,mh_nblocks=1,mh_replic=0); + identification(parameter_set=posterior_mode); @# endif @# endif @# endif -- GitLab