From 6192a6a7765e7bde388f4a6feac4362d1e9fd284 Mon Sep 17 00:00:00 2001 From: Houtan Bastani <houtan@dynare.org> Date: Tue, 17 May 2016 17:17:23 +0200 Subject: [PATCH] preprocessor: add params_derivs_order to identification and sensitivity. #1187 --- preprocessor/ComputingTasks.cc | 30 ++++++++++++++++ preprocessor/DynamicModel.cc | 4 +-- preprocessor/DynamicModel.hh | 2 +- preprocessor/DynareBison.yy | 5 ++- preprocessor/DynareFlex.ll | 1 + preprocessor/ModFile.cc | 34 ++++++++++++------ preprocessor/ModelTree.cc | 63 +++++++++++++++++----------------- preprocessor/ModelTree.hh | 2 +- preprocessor/Statement.hh | 5 +-- preprocessor/StaticModel.cc | 4 +-- preprocessor/StaticModel.hh | 2 +- 11 files changed, 101 insertions(+), 51 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index a47e84979e..19d6f6f694 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -637,6 +637,21 @@ DynareSensitivityStatement::checkPass(ModFileStructure &mod_file_struct, Warning if (it != options_list.num_options.end() && it->second == "1") mod_file_struct.identification_present = true; + + it = options_list.num_options.find("params_derivs_order"); + mod_file_struct.params_deriv_order = -1; + if (it != options_list.num_options.end()) + if (it->second == "0") + mod_file_struct.params_deriv_order = 0; + else if (it->second == "1") + mod_file_struct.params_deriv_order = 1; + else if (it->second == "2") + mod_file_struct.params_deriv_order = 2; + else + { + cerr << "ERROR: Valid values for params_deriv_order option are 0, 1, 2" << endl; + exit(EXIT_FAILURE); + } } void @@ -1553,6 +1568,21 @@ void IdentificationStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { mod_file_struct.identification_present = true; + + OptionsList::num_options_t::const_iterator it = options_list.num_options.find("params_derivs_order"); + mod_file_struct.params_deriv_order = -1; + if (it != options_list.num_options.end()) + if (it->second == "0") + mod_file_struct.params_deriv_order = 0; + else if (it->second == "1") + mod_file_struct.params_deriv_order = 1; + else if (it->second == "2") + mod_file_struct.params_deriv_order = 2; + else + { + cerr << "ERROR: Valid values for params_derivs_order option are 0, 1, 2" << endl; + exit(EXIT_FAILURE); + } } void diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 56dcc0704d..2d1dbc6e7d 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -3113,7 +3113,7 @@ DynamicModel::runTrendTest(const eval_context_t &eval_context) } void -DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, FileOutputType paramsDerivatives, +DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, int paramsDerivatives, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode, bool compute_xrefs) { @@ -3148,7 +3148,7 @@ DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivative computeHessian(vars); } - if (paramsDerivatives != none) + if (paramsDerivatives != 0) { cout << " - derivatives of Jacobian/Hessian w.r. to parameters" << endl; computeParamsDerivatives(paramsDerivatives); diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh index 88f883cbbc..497fe782ed 100644 --- a/preprocessor/DynamicModel.hh +++ b/preprocessor/DynamicModel.hh @@ -212,7 +212,7 @@ public: \param eval_context evaluation context for normalization \param no_tmp_terms if true, no temporary terms will be computed in the dynamic files */ - void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, FileOutputType paramsDerivatives, + void computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, int paramsDerivatives, const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode, bool compute_xrefs); //! Writes model initialization and lead/lag incidence matrix to output void writeOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present, bool compute_xrefs, bool julia) const; diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 194391eba3..b669391cc5 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -86,7 +86,7 @@ class ParsingDriver; %token AIM_SOLVER ANALYTIC_DERIVATION ANALYTIC_DERIVATION_MODE AR AUTOCORR TARB_MODE_COMPUTE %token BAYESIAN_IRF BETA_PDF BLOCK USE_CALIBRATION USE_TARB TARB_NEW_BLOCK_PROBABILITY SILENT_OPTIMIZER %token BVAR_DENSITY BVAR_FORECAST NODECOMPOSITION DR_DISPLAY_TOL HUGE_NUMBER -%token BVAR_PRIOR_DECAY BVAR_PRIOR_FLAT BVAR_PRIOR_LAMBDA TARB_OPTIM +%token BVAR_PRIOR_DECAY BVAR_PRIOR_FLAT BVAR_PRIOR_LAMBDA TARB_OPTIM PARAMS_DERIVS_ORDER %token BVAR_PRIOR_MU BVAR_PRIOR_OMEGA BVAR_PRIOR_TAU BVAR_PRIOR_TRAIN %token BVAR_REPLIC BYTECODE ALL_VALUES_REQUIRED PROPOSAL_DISTRIBUTION %token CALIB_SMOOTHER CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF CYCLE_REDUCTION LOGARITHMIC_REDUCTION @@ -1914,6 +1914,7 @@ identification_option : o_ar | o_prior_trunc | o_analytic_derivation | o_analytic_derivation_mode + | o_params_derivs_order ; model_comparison : MODEL_COMPARISON mc_filename_list ';' @@ -2398,6 +2399,7 @@ dynare_sensitivity_option : o_gsa_identification | o_lik_init | o_analytic_derivation | o_analytic_derivation_mode + | o_params_derivs_order ; shock_decomposition_options_list : shock_decomposition_option COMMA shock_decomposition_options_list @@ -3113,6 +3115,7 @@ o_data_obs_nbr : DATA_OBS_NBR EQUAL INT_NUMBER { driver.option_num("ms.forecast_ o_discretionary_tol: DISCRETIONARY_TOL EQUAL non_negative_number { driver.option_num("discretionary_tol",$3); }; o_analytic_derivation : ANALYTIC_DERIVATION { driver.option_num("analytic_derivation", "1"); } o_analytic_derivation_mode : ANALYTIC_DERIVATION_MODE EQUAL signed_number { driver.option_num("analytic_derivation_mode", $3); } +o_params_derivs_order : PARAMS_DERIVS_ORDER EQUAL INT_NUMBER { driver.option_num("params_derivs_order", $3); } o_endogenous_prior : ENDOGENOUS_PRIOR { driver.option_num("endogenous_prior", "1"); } o_use_univariate_filters_if_singularity_is_detected : USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED EQUAL INT_NUMBER { driver.option_num("use_univariate_filters_if_singularity_is_detected", $3); } o_mcmc_jumping_covariance : MCMC_JUMPING_COVARIANCE EQUAL HESSIAN diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 606744af07..cfaea854e2 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -345,6 +345,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 <DYNARE_STATEMENT>first_deriv_provided {return token::FIRST_DERIV_PROVIDED;} <DYNARE_STATEMENT>second_deriv_provided {return token::SECOND_DERIV_PROVIDED;} <DYNARE_STATEMENT>freq {return token::FREQ;} +<DYNARE_STATEMENT>params_derivs_order {return token::PARAMS_DERIVS_ORDER;} <DYNARE_STATEMENT>monthly {return token::MONTHLY; } <DYNARE_STATEMENT>quarterly {return token::QUARTERLY; } <DYNARE_STATEMENT>initial_year {return token::INITIAL_YEAR;} diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 972a5985bf..575c6499a7 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -489,11 +489,18 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr const bool static_hessian = mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation; - FileOutputType paramsDerivatives = none; - if (mod_file_struct.identification_present) - paramsDerivatives = first; - if (mod_file_struct.estimation_analytic_derivation) - paramsDerivatives = third; + int paramsDerivatives = 0; + if (mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation) + switch (mod_file_struct.params_deriv_order) + { + case 0: + case 1: + case 2: + paramsDerivatives = mod_file_struct.params_deriv_order; + break; + default: + paramsDerivatives = 2; + } static_model.computingPass(global_eval_context, no_tmp_terms, static_hessian, false, paramsDerivatives, block, byte_code); } @@ -526,11 +533,18 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output, bool compute_xr bool thirdDerivatives = mod_file_struct.order_option == 3 || mod_file_struct.estimation_analytic_derivation || output == third; - FileOutputType paramsDerivatives = none; - if (mod_file_struct.identification_present) - paramsDerivatives = first; - if (mod_file_struct.estimation_analytic_derivation) - paramsDerivatives = third; + int paramsDerivatives = 0; + if (mod_file_struct.identification_present || mod_file_struct.estimation_analytic_derivation) + switch (mod_file_struct.params_deriv_order) + { + case 0: + case 1: + case 2: + paramsDerivatives = mod_file_struct.params_deriv_order; + break; + default: + paramsDerivatives = 2; + } dynamic_model.computingPass(true, hessian, thirdDerivatives, paramsDerivatives, global_eval_context, no_tmp_terms, block, use_dll, byte_code, compute_xrefs); } } diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index 518bd3fde0..27f69c2131 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1657,9 +1657,9 @@ ModelTree::sparseHelper(int order, ostream &output, int row_nb, int col_nb, Expr } void -ModelTree::computeParamsDerivatives(FileOutputType paramsDerivatives) +ModelTree::computeParamsDerivatives(int paramsDerivatives) { - if (!(paramsDerivatives == first || paramsDerivatives == second || paramsDerivatives == third)) + if (!(paramsDerivatives == 1 || paramsDerivatives == 2)) return; set<int> deriv_id_set; addAllParamDerivId(deriv_id_set); @@ -1677,7 +1677,7 @@ ModelTree::computeParamsDerivatives(FileOutputType paramsDerivatives) residuals_params_derivatives[make_pair(eq, param)] = d1; } - if (paramsDerivatives == second || paramsDerivatives == third) + if (paramsDerivatives == 2) for (first_derivatives_t::const_iterator it2 = residuals_params_derivatives.begin(); it2 != residuals_params_derivatives.end(); it2++) { @@ -1704,35 +1704,36 @@ ModelTree::computeParamsDerivatives(FileOutputType paramsDerivatives) jacobian_params_derivatives[make_pair(eq, make_pair(var, param))] = d2; } - if (paramsDerivatives == second || paramsDerivatives == third) - for (second_derivatives_t::const_iterator it2 = jacobian_params_derivatives.begin(); - it2 != jacobian_params_derivatives.end(); it2++) - { - int eq = it2->first.first; - int var = it2->first.second.first; - int param1 = it2->first.second.second; - expr_t d1 = it2->second; - - expr_t d2 = d1->getDerivative(param); - if (d2 == Zero) - continue; - jacobian_params_second_derivatives[make_pair(eq, make_pair(var, make_pair(param1, param)))] = d2; - } - - if (paramsDerivatives == third) - for (second_derivatives_t::const_iterator it2 = second_derivatives.begin(); - it2 != second_derivatives.end(); it2++) - { - int eq = it2->first.first; - int var1 = it2->first.second.first; - int var2 = it2->first.second.second; - expr_t d1 = it2->second; + if (paramsDerivatives == 2) + { + for (second_derivatives_t::const_iterator it2 = jacobian_params_derivatives.begin(); + it2 != jacobian_params_derivatives.end(); it2++) + { + int eq = it2->first.first; + int var = it2->first.second.first; + int param1 = it2->first.second.second; + expr_t d1 = it2->second; + + expr_t d2 = d1->getDerivative(param); + if (d2 == Zero) + continue; + jacobian_params_second_derivatives[make_pair(eq, make_pair(var, make_pair(param1, param)))] = d2; + } - expr_t d2 = d1->getDerivative(param); - if (d2 == Zero) - continue; - hessian_params_derivatives[make_pair(eq, make_pair(var1, make_pair(var2, param)))] = d2; - } + for (second_derivatives_t::const_iterator it2 = second_derivatives.begin(); + it2 != second_derivatives.end(); it2++) + { + int eq = it2->first.first; + int var1 = it2->first.second.first; + int var2 = it2->first.second.second; + expr_t d1 = it2->second; + + expr_t d2 = d1->getDerivative(param); + if (d2 == Zero) + continue; + hessian_params_derivatives[make_pair(eq, make_pair(var1, make_pair(var2, param)))] = d2; + } + } } } diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh index 41f30d3b3c..b689c26eb3 100644 --- a/preprocessor/ModelTree.hh +++ b/preprocessor/ModelTree.hh @@ -177,7 +177,7 @@ protected: /*! \param vars the derivation IDs w.r. to which derive the 2nd derivatives */ void computeThirdDerivatives(const set<int> &vars); //! Computes derivatives of the Jacobian and Hessian w.r. to parameters - void computeParamsDerivatives(FileOutputType paramsDerivatives); + void computeParamsDerivatives(int paramsDerivatives); //! Write derivative of an equation w.r. to a variable void writeDerivative(ostream &output, int eq, int symb_id, int lag, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; //! Computes temporary terms (for all equations and derivatives) diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index 050d182a77..366ffc8ce4 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2015 Dynare Team + * Copyright (C) 2006-2016 Dynare Team * * This file is part of Dynare. * @@ -122,7 +122,8 @@ public: int orig_eq_nbr; //! Stores the number of equations added to the Ramsey model int ramsey_eq_nbr; - + //! Stores the order of parameter derivatives to take + int params_deriv_order; }; class Statement diff --git a/preprocessor/StaticModel.cc b/preprocessor/StaticModel.cc index a3b42f5625..b464b31d61 100644 --- a/preprocessor/StaticModel.cc +++ b/preprocessor/StaticModel.cc @@ -1047,7 +1047,7 @@ StaticModel::collect_first_order_derivatives_endogenous() } void -StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatives, FileOutputType paramsDerivatives, bool block, bool bytecode) +StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatives, int paramsDerivatives, bool block, bool bytecode) { initializeVariablesAndEquations(); @@ -1095,7 +1095,7 @@ StaticModel::computingPass(const eval_context_t &eval_context, bool no_tmp_terms computeThirdDerivatives(vars); } - if (paramsDerivatives != none) + if (paramsDerivatives != 0) { cout << " - derivatives of Jacobian/Hessian w.r. to parameters" << endl; computeParamsDerivatives(paramsDerivatives); diff --git a/preprocessor/StaticModel.hh b/preprocessor/StaticModel.hh index d8ec97900b..74f42c97a2 100644 --- a/preprocessor/StaticModel.hh +++ b/preprocessor/StaticModel.hh @@ -164,7 +164,7 @@ public: \param hessian whether 2nd derivatives w.r. to exo, exo_det and endo should be computed \param paramsDerivatives whether 2nd derivatives w.r. to a pair (endo/exo/exo_det, parameter) should be computed */ - void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatices, FileOutputType paramsDerivatives, bool block, bool bytecode); + void computingPass(const eval_context_t &eval_context, bool no_tmp_terms, bool hessian, bool thirdDerivatices, int paramsDerivatives, bool block, bool bytecode); //! Adds informations for simulation in a binary file for a block decomposed model void Write_Inf_To_Bin_File_Block(const string &static_basename, const string &bin_basename, const int &num, -- GitLab