diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index a47e84979e234b3f232df8e6bf265dc3bc6090be..19d6f6f6946292d6135ce2d3db5b8130afdc8b47 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 56dcc0704dde7a49e8b5200e5456c20d539f962f..2d1dbc6e7d5ba82b16c9f4f3d3a96818a8887f85 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 88f883cbbc94a3c0992605e9fb1149978bb0778f..497fe782edf9d26880e9f9036a147b5ff2a0f4f1 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 194391eba38733b2d38656035ca2270739ffea17..b669391cc587a50f8664d14574e7552b74943784 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 606744af07148d7757827f3bff9ec36b9b94c28b..cfaea854e259fb65b1db2094b359634bd9292915 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 972a5985bf24c9af7db50e8062aaf5b719350b36..575c6499a72ecdf88b788a11b3b442bdcdfa3ac8 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 518bd3fde01bfdcbf83fb4acaf6214ed3faa527e..27f69c213104175cbcea3000c1556b72c365c134 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 41f30d3b3c1700908d8c7756f87b4f90613bedf4..b689c26eb364b9bbbfe6404340b6cb4e168b8f09 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 050d182a77c9146c5ccd02bcd081f91645bd7e37..366ffc8ce46786bee22d679b263e98b95bd07dc0 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 a3b42f56250cd563b3cabab26eca4292b323b149..b464b31d61bb5449e82b2428f592fd7527989bbc 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 d8ec97900bd11037b6d8bfc0ff3f923ee2ea6eef..74f42c97a2302b861f63a7b94ef7c648846e3f2b 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,