From ee9b19fadc80fdaee3255c05e09a3fb2d106ebad Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtanb@gmail.com>
Date: Mon, 21 Jun 2010 18:40:36 +0200
Subject: [PATCH] DSGE-VAR: support deprecated way of declaring
 dsge_prior_weight

---
 DynareBison.yy             |  1 -
 ModFile.cc                 | 62 +++++++++++++++++++++++++++++++-------
 NumericalInitialization.cc |  9 +++++-
 NumericalInitialization.hh |  3 +-
 ParsingDriver.cc           | 20 ++++--------
 ParsingDriver.hh           |  2 --
 Statement.hh               |  2 ++
 SymbolTable.cc             |  3 ++
 8 files changed, 72 insertions(+), 30 deletions(-)

diff --git a/DynareBison.yy b/DynareBison.yy
index 2967d612..342a59e1 100644
--- a/DynareBison.yy
+++ b/DynareBison.yy
@@ -929,7 +929,6 @@ estimated_elem1 : STDERR symbol
                   }
                 | DSGE_PRIOR_WEIGHT
                   {
-                    driver.declare_dsge_prior_weight();
                     driver.estim_params.type = 2;
                     driver.estim_params.name = "dsge_prior_weight";
                   }
diff --git a/ModFile.cc b/ModFile.cc
index 249c0b07..8044fa1b 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -153,13 +153,51 @@ ModFile::checkPass()
         exit(EXIT_FAILURE);
       }
 
+  if (symbol_table.exists("dsge_prior_weight"))
+    {
+      if (symbol_table.getType("dsge_prior_weight") != eParameter)
+        {
+          cerr << "ERROR: dsge_prior_weight may only be used as a parameter." << endl;
+          exit(EXIT_FAILURE);
+        }
+      else
+        cout << "WARNING: When estimating a DSGE-Var, declaring dsge_prior_weight as a parameter is deprecated. "
+             <<  "The preferred method is to do this via the dsge_var option in the estimation statement." << endl;
+
+      if (mod_file_struct.dsge_var_estimated || !mod_file_struct.dsge_var_calibrated.empty())
+        {
+          cerr << "ERROR: dsge_prior_weight can either be declared as a parameter (deprecated) or via the dsge_var option "
+               << "to the estimation statement (preferred), but not both." << endl;
+          exit(EXIT_FAILURE);
+        }
+
+      if (!mod_file_struct.dsge_prior_weight_initialized && !mod_file_struct.dsge_prior_weight_in_estimated_params)
+        {
+          cerr << "ERROR: If dsge_prior_weight is declared as a parameter, it must either be initialized or placed in the "
+               << "estimated_params block." << endl;
+          exit(EXIT_FAILURE);
+        }
+
+      if (mod_file_struct.dsge_prior_weight_initialized && mod_file_struct.dsge_prior_weight_in_estimated_params)
+        {
+          cerr << "ERROR: dsge_prior_weight cannot be both initalized and estimated." << endl;
+          exit(EXIT_FAILURE);
+        }
+    }
+
   if (mod_file_struct.dsge_prior_weight_in_estimated_params)
-    if (!mod_file_struct.dsge_var_estimated)
+    if (!mod_file_struct.dsge_var_estimated && !mod_file_struct.dsge_var_calibrated.empty())
       {
-        cerr << "ERROR: If dsge_prior_weight is in the estimated_params_block, the prior weight cannot be calibrated "
+        cerr << "ERROR: If dsge_prior_weight is in the estimated_params block, the prior weight cannot be calibrated "
              << "via the dsge_var option in the estimation statement." << endl;
         exit(EXIT_FAILURE);
       }
+    else if (!mod_file_struct.dsge_var_estimated && !symbol_table.exists("dsge_prior_weight"))
+      {
+        cerr << "ERROR: If dsge_prior_weight is in the estimated_params block, it must either be declared as a parameter "
+             << "(deprecated) or the dsge_var option must be passed to the estimation statement (preferred)." << endl;
+        exit(EXIT_FAILURE);
+      }
 }
 
 void
@@ -183,12 +221,14 @@ ModFile::transformPass()
       dynamic_model.substituteExoLag();
     }
 
-  if (!mod_file_struct.dsge_var_calibrated.empty())
+  if (mod_file_struct.dsge_var_estimated || !mod_file_struct.dsge_var_calibrated.empty())
     try
       {
-        addStatement(new InitParamStatement(symbol_table.addSymbol("dsge_prior_weight", eParameter),
-                                            expressions_tree.AddNumConstant(mod_file_struct.dsge_var_calibrated),
-                                            symbol_table));
+        int sid = symbol_table.addSymbol("dsge_prior_weight", eParameter);
+        if (!mod_file_struct.dsge_var_calibrated.empty())
+          addStatement(new InitParamStatement(sid,
+                                              expressions_tree.AddNumConstant(mod_file_struct.dsge_var_calibrated),
+                                              symbol_table));
       }
     catch (SymbolTable::AlreadyDeclaredException &e)
       {
@@ -215,21 +255,21 @@ ModFile::transformPass()
 
   cout << "Found " << dynamic_model.equation_number() << " equation(s)." << endl;
 
-  if (!mod_file_struct.dsge_var_calibrated.empty() || mod_file_struct.dsge_var_estimated)
+  if (symbol_table.exists("dsge_prior_weight"))
     if (mod_file_struct.bayesian_irf_present)
       {
         if (symbol_table.exo_nbr() != symbol_table.observedVariablesNbr())
           {
-            cerr << "ERROR: If bayesian_irf and dsge_var are passed to the estimation statement, "
-                 << "the number of shocks must equal the number of observed variables." << endl;
+            cerr << "ERROR: When estimating a DSGE-Var and the bayesian_irf option is passed to the estimation "
+                 << "statement, the number of shocks must equal the number of observed variables." << endl;
             exit(EXIT_FAILURE);
           }
       }
     else
       if (symbol_table.exo_nbr() < symbol_table.observedVariablesNbr())
         {
-          cerr << "ERROR: If dsge_var is passed to the estimation statement, the number of shocks "
-               << "must be greater than or equal to the number of observed variables." << endl;
+          cerr << "ERROR: When estimating a DSGE-Var, the number of shocks must be "
+               << "greater than or equal to the number of observed variables." << endl;
           exit(EXIT_FAILURE);
         }
 }
diff --git a/NumericalInitialization.cc b/NumericalInitialization.cc
index 357469ea..fdf1a052 100644
--- a/NumericalInitialization.cc
+++ b/NumericalInitialization.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2009 Dynare Team
+ * Copyright (C) 2003-2010 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -32,6 +32,13 @@ InitParamStatement::InitParamStatement(int symb_id_arg,
 {
 }
 
+void
+InitParamStatement::checkPass(ModFileStructure &mod_file_struct)
+{
+  if (symbol_table.getName(symb_id) == "dsge_prior_weight")
+    mod_file_struct.dsge_prior_weight_initialized = true;
+}
+
 void
 InitParamStatement::writeOutput(ostream &output, const string &basename) const
 {
diff --git a/NumericalInitialization.hh b/NumericalInitialization.hh
index f01e6436..e4d343e9 100644
--- a/NumericalInitialization.hh
+++ b/NumericalInitialization.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2003-2009 Dynare Team
+ * Copyright (C) 2003-2010 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -39,6 +39,7 @@ private:
 public:
   InitParamStatement(int symb_id_arg, const NodeID param_value_arg,
                      const SymbolTable &symbol_table_arg);
+  virtual void checkPass(ModFileStructure &mod_file_struct);
   virtual void writeOutput(ostream &output, const string &basename) const;
   //! Fill eval context with parameter value
   void fillEvalContext(eval_context_type &eval_context) const;
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index 96549c33..17b87517 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -164,16 +164,6 @@ ParsingDriver::declare_parameter(string *name, string *tex_name)
     delete tex_name;
 }
 
-void
-ParsingDriver::declare_dsge_prior_weight()
-{
-  if (mod_file->symbol_table.exists("dsge_prior_weight"))
-    error("If dsge_prior_weight appears in the estimated_params block, it cannot have been previosly declared.");
-
-  string *dsge_prior_weight = new string("dsge_prior_weight");
-  declare_parameter(dsge_prior_weight);
-}
-
 void
 ParsingDriver::add_predetermined_variable(string *name)
 {
@@ -893,10 +883,12 @@ ParsingDriver::check()
 void
 ParsingDriver::add_estimated_params_element()
 {
-  check_symbol_existence(estim_params.name);
-  if (estim_params.name2.size() > 0)
-    check_symbol_existence(estim_params.name2);
-
+  if (estim_params.name != "dsge_prior_weight")
+    {
+      check_symbol_existence(estim_params.name);
+      if (estim_params.name2.size() > 0)
+        check_symbol_existence(estim_params.name2);
+    }
   estim_params_list.push_back(estim_params);
   estim_params.init(*data_tree);
 }
diff --git a/ParsingDriver.hh b/ParsingDriver.hh
index 91ef4743..3acb8460 100644
--- a/ParsingDriver.hh
+++ b/ParsingDriver.hh
@@ -212,8 +212,6 @@ public:
   void declare_exogenous_det(string *name, string *tex_name = NULL);
   //! Declares a parameter
   void declare_parameter(string *name, string *tex_name = NULL);
-  //! Declares dsge_prior_weight
-  void declare_dsge_prior_weight();
   //! Adds a predetermined_variable
   void add_predetermined_variable(string *name);
   //! Declares and initializes a local parameter
diff --git a/Statement.hh b/Statement.hh
index 3e17a5de..e2b5e3ef 100644
--- a/Statement.hh
+++ b/Statement.hh
@@ -65,6 +65,8 @@ public:
   bool k_order_solver;
   //! Whether there is a calibrated measurement error
   bool calibrated_measurement_errors;
+  //! Whether dsge_prior_weight was initialized as a parameter
+  bool dsge_prior_weight_initialized;
   //! Whether dsge_prior_weight is in the estimated_params block
   bool dsge_prior_weight_in_estimated_params;
   //! Whether there is a dsge_var, with calibrated prior weight
diff --git a/SymbolTable.cc b/SymbolTable.cc
index b3e4ad12..91e1b6f4 100644
--- a/SymbolTable.cc
+++ b/SymbolTable.cc
@@ -201,6 +201,9 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException)
         {
           output << "M_.param_names = strvcat(M_.param_names, '" << getName(param_ids[id]) << "');" << endl
                  << "M_.param_names_tex = strvcat(M_.param_names_tex, '" << getTeXName(param_ids[id]) << "');" << endl;
+
+          if (getName(param_ids[id]) == "dsge_prior_weight")
+            output << "options_.dsge_var = 1;" << endl;
         }
     }
 
-- 
GitLab