diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 2684494d973439875ec4f422f031e1239ab6fa7d..e29f91d917173fe5623df33172547da077e91c49 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -3242,18 +3242,16 @@ DynamicModel::substitutePacTargetNonstationary(const string &pac_model_name, exp } void -DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsOrder, - const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll) +DynamicModel::computingPass(int derivsOrder, int paramsDerivsOrder, const eval_context_t &eval_context, + bool no_tmp_terms, bool block, bool use_dll) { - assert(jacobianExo || (derivsOrder < 2 && paramsDerivsOrder == 0)); - initializeVariablesAndEquations(); // Prepare for derivation computeDerivIDs(); // Computes dynamic jacobian columns, must be done after computeDerivIDs() - computeDynJacobianCols(jacobianExo); + computeDynJacobianCols(); /* In both MATLAB and Julia, tensors for higher-order derivatives are stored in matrices whose columns correspond to variable multi-indices. Since we @@ -3268,12 +3266,13 @@ DynamicModel::computingPass(bool jacobianExo, int derivsOrder, int paramsDerivsO exit(EXIT_FAILURE); } - // Compute derivatives w.r. to all endogenous, and possibly exogenous and exogenous deterministic + // Compute derivatives w.r. to all endogenous, exogenous and exogenous deterministic set<int> vars; for (auto &it : deriv_id_table) { SymbolType type = symbol_table.getType(it.first.first); - if (type == SymbolType::endogenous || (jacobianExo && (type == SymbolType::exogenous || type == SymbolType::exogenousDet))) + if (type == SymbolType::endogenous || type == SymbolType::exogenous + || type == SymbolType::exogenousDet) vars.insert(it.second); } @@ -4024,7 +4023,7 @@ DynamicModel::addAllParamDerivId(set<int> &deriv_id_set) } void -DynamicModel::computeDynJacobianCols(bool jacobianExo) +DynamicModel::computeDynJacobianCols() { // Sort the dynamic endogenous variables by lexicographic order over (lag, type_specific_symbol_id) map<pair<int, int>, int> ordered_dyn_endo; @@ -4039,17 +4038,16 @@ DynamicModel::computeDynJacobianCols(bool jacobianExo) dyn_jacobian_cols_table[deriv_id] = sorted_id++; // Fill the dynamic columns for exogenous and exogenous deterministic - if (jacobianExo) - for (const auto &[symb_lag, deriv_id] : deriv_id_table) - { - int symb_id{symb_lag.first}; - int tsid{symbol_table.getTypeSpecificID(symb_id)}; - if (SymbolType type{symbol_table.getType(symb_id)}; - type == SymbolType::exogenous) - dyn_jacobian_cols_table[deriv_id] = ordered_dyn_endo.size() + tsid; - else if (type == SymbolType::exogenousDet) - dyn_jacobian_cols_table[deriv_id] = ordered_dyn_endo.size() + symbol_table.exo_nbr() + tsid; - } + for (const auto &[symb_lag, deriv_id] : deriv_id_table) + { + int symb_id{symb_lag.first}; + int tsid{symbol_table.getTypeSpecificID(symb_id)}; + if (SymbolType type{symbol_table.getType(symb_id)}; + type == SymbolType::exogenous) + dyn_jacobian_cols_table[deriv_id] = ordered_dyn_endo.size() + tsid; + else if (type == SymbolType::exogenousDet) + dyn_jacobian_cols_table[deriv_id] = ordered_dyn_endo.size() + symbol_table.exo_nbr() + tsid; + } } void diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh index 9e0c1780e0ce4315fa975c79a69361de41136895..c64713aa5a1d3f89aff36804141ffdfbbeafa962 100644 --- a/src/DynamicModel.hh +++ b/src/DynamicModel.hh @@ -183,7 +183,7 @@ private: int getTypeSpecificIDByDerivID(int deriv_id) const override; //! Compute the column indices of the dynamic Jacobian - void computeDynJacobianCols(bool jacobianExo); + void computeDynJacobianCols(); //! Computes derivatives of the Jacobian w.r. to trend vars and tests that they are equal to zero void testTrendDerivativesEqualToZero(const eval_context_t &eval_context); @@ -323,14 +323,13 @@ public: //! Execute computations (variable sorting + derivation + block decomposition) /*! - \param jacobianExo whether derivatives w.r. to exo and exo_det should be in the Jacobian (derivatives w.r. to endo are always computed) \param derivsOrder order of derivatives w.r. to exo, exo_det and endo should be computed (implies jacobianExo = true when order >= 2) \param paramsDerivsOrder order of derivatives w.r. to a pair (endo/exo/exo_det, parameter) to be computed (>0 implies jacobianExo = true) \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, int derivsOrder, int paramsDerivsOrder, - const eval_context_t &eval_context, bool no_tmp_terms, bool block, bool use_dll); + void computingPass(int derivsOrder, int paramsDerivsOrder, const eval_context_t &eval_context, + bool no_tmp_terms, bool block, bool use_dll); //! Writes information about the dynamic model to the driver file void writeDriverOutput(ostream &output, const string &basename, bool block, bool estimation_present, bool compute_xrefs) const; diff --git a/src/ModFile.cc b/src/ModFile.cc index bb0b8ca8c82de104a167dd93f762c0982fea3dee..64c76deb20922056062dad6c7c27bc3cedf9be28 100644 --- a/src/ModFile.cc +++ b/src/ModFile.cc @@ -700,7 +700,7 @@ ModFile::computingPass(bool no_tmp_terms, OutputType output, int params_derivs_o derivsOrder = 2; else if (output == OutputType::third) derivsOrder = 3; - dynamic_model.computingPass(true, derivsOrder, 0, global_eval_context, no_tmp_terms, block, use_dll); + dynamic_model.computingPass(derivsOrder, 0, global_eval_context, no_tmp_terms, block, use_dll); } else { @@ -729,13 +729,13 @@ ModFile::computingPass(bool no_tmp_terms, OutputType output, int params_derivs_o || mod_file_struct.estimation_analytic_derivation || (mod_file_struct.GMM_present && (mod_file_struct.analytic_standard_errors_present || mod_file_struct.analytic_jacobian_present))) paramsDerivsOrder = params_derivs_order; - dynamic_model.computingPass(true, derivsOrder, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll); + dynamic_model.computingPass(derivsOrder, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll); if (linear && mod_file_struct.ramsey_model_present) - orig_ramsey_dynamic_model.computingPass(true, 2, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll); + orig_ramsey_dynamic_model.computingPass(2, paramsDerivsOrder, global_eval_context, no_tmp_terms, block, use_dll); } } else // No computing task requested, compute derivatives up to 2nd order by default - dynamic_model.computingPass(true, 2, 0, global_eval_context, no_tmp_terms, block, use_dll); + dynamic_model.computingPass(2, 0, global_eval_context, no_tmp_terms, block, use_dll); /* Check that the model is linear. FIXME: this check always passes if derivsOrder = 1, i.e. for a perfect @@ -772,7 +772,7 @@ ModFile::computingPass(bool no_tmp_terms, OutputType output, int params_derivs_o // Compute epilogue derivatives (but silence standard output) streambuf *oldcout = cout.rdbuf(); cout.rdbuf(nullptr); - epilogue.computingPass(true, 2, 0, global_eval_context, true, false, false); + epilogue.computingPass(2, 0, global_eval_context, true, false, false); cout.rdbuf(oldcout); }