From 14be6bad0018f5f44838729c6d44a5288838bdb5 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 11 Feb 2020 14:12:24 +0100
Subject: [PATCH] occbin: support occbin tags in equation tags, add
 occbin_likelihood and occbin_smoother as options to estimation

---
 src/ComputingTasks.cc | 14 ++++++++++++++
 src/DynamicModel.cc   | 19 ++++++++++++++++++-
 src/DynareBison.yy    | 10 ++++++----
 src/DynareFlex.ll     |  4 +++-
 4 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc
index 3e97a23f..000191b7 100644
--- a/src/ComputingTasks.cc
+++ b/src/ComputingTasks.cc
@@ -1247,6 +1247,20 @@ EstimationStatement::writeOutput(ostream &output, const string &basename, bool m
 {
   options_list.writeOutput(output);
 
+  bool occbin_option_present = false;
+  if (auto it = options_list.num_options.find("occbin_likelihood");
+      it != options_list.num_options.end() && it->second == "true")
+    occbin_option_present = true;
+
+  if (auto it = options_list.num_options.find("occbin_smoother");
+      it != options_list.num_options.end() && it->second == "true")
+    occbin_option_present = true;
+
+  if (occbin_option_present)
+    output << "options_ = set_default_occbin_options(options_, M_);" << endl
+           << "clear mr_runsim_occbin_fn" << endl
+           << "M_ = get_wish_list(M_);" << endl;
+
   // Special treatment for order option and particle filter
   if (auto it = options_list.num_options.find("order");
       it == options_list.num_options.end())
diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index a84bb6f1..e21e2a2c 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2003-2019 Dynare Team
+ * Copyright © 2003-2020 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -3200,6 +3200,23 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
       output << "};" << endl;
     }
 
+  // Write Occbin tags
+  map<int, vector<pair<string, string>>> occbin_options;
+  for (const auto &[eqn, tag] : equation_tags)
+      if (tag.first == "pswitch"
+          || tag.first == "bind"
+          || tag.first == "relax"
+          || tag.first == "pcrit")
+        occbin_options[eqn].push_back(tag);
+
+  int idx = 0;
+  for (const auto &[eqn, tags] : occbin_options)
+    {
+      idx++;
+      for (const auto &[tag_name, tag_value] : tags)
+        output << "M_.occbin.constraint(" << idx << ")." << tag_name << " = '" << tag_value << "';" << endl;
+    }
+
   // Write mapping for variables and equations they are present in
   for (const auto &variable : variableMapping)
     {
diff --git a/src/DynareBison.yy b/src/DynareBison.yy
index dc16976c..efb44bed 100644
--- a/src/DynareBison.yy
+++ b/src/DynareBison.yy
@@ -1,6 +1,6 @@
 // -*- C++ -*-
 /*
- * Copyright © 2003-2019 Dynare Team
+ * Copyright © 2003-2020 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -128,7 +128,7 @@ class ParsingDriver;
 %precedence UNARY
 %nonassoc POWER
 %token EXP LOG LN LOG10 SIN COS TAN ASIN ACOS ATAN ERF DIFF ADL AUXILIARY_MODEL_NAME
-%token SQRT CBRT NORMCDF NORMPDF STEADY_STATE EXPECTATION VAR_ESTIMATION
+%token SQRT CBRT NORMCDF NORMPDF STEADY_STATE EXPECTATION VAR_ESTIMATION OCCBIN_LIKELIHOOD OCCBIN_SMOOTHER
 /* GSA analysis */
 %token DYNARE_SENSITIVITY MORRIS STAB REDFORM PPRIOR PRIOR_RANGE PPOST ILPTAU MORRIS_NLIV
 %token MORRIS_NTRA NSAM LOAD_REDFORM LOAD_RMSE LOAD_STAB ALPHA2_STAB LOGTRANS_REDFORM THRESHOLD_REDFORM
@@ -2100,6 +2100,8 @@ estimation_options : o_datafile
                    | o_emas_max_iter
                    | o_stderr_multiples
                    | o_diagonal_only
+                   | o_occbin_likelihood
+                   | o_occbin_smoother
                    ;
 
 list_optim_option : QUOTED_STRING COMMA QUOTED_STRING
@@ -3484,8 +3486,8 @@ o_proposal_approximation : PROPOSAL_APPROXIMATION EQUAL CUBATURE {driver.option_
 o_distribution_approximation : DISTRIBUTION_APPROXIMATION EQUAL CUBATURE {driver.option_num("particle.distribution_approximation.cubature", "true"); driver.option_num("particle.distribution_approximation.unscented", "false"); driver.option_num("particle.distribution_approximation.montecarlo", "false");}
 		| DISTRIBUTION_APPROXIMATION EQUAL UNSCENTED {driver.option_num("particle.distribution_approximation.cubature", "false"); driver.option_num("particle.distribution_approximation.unscented", "true"); driver.option_num("particle.distribution_approximation.montecarlo", "false");}
 		| DISTRIBUTION_APPROXIMATION EQUAL MONTECARLO {driver.option_num("particle.distribution_approximation.cubature", "false"); driver.option_num("particle.distribution_approximation.unscented", "false"); driver.option_num("particle.distribution_approximation.montecarlo", "true");} ;
-
-
+o_occbin_likelihood : OCCBIN_LIKELIHOOD { driver.option_num("occbin_likelihood", "true"); };
+o_occbin_smoother : OCCBIN_SMOOTHER { driver.option_num("occbin_smoother", "true"); };
 o_gsa_identification : IDENTIFICATION EQUAL INT_NUMBER { driver.option_num("identification", $3); }; /*not in doc */
 o_gsa_morris : MORRIS EQUAL INT_NUMBER { driver.option_num("morris", $3); };
 o_gsa_stab : STAB  EQUAL INT_NUMBER { driver.option_num("stab", $3); };
diff --git a/src/DynareFlex.ll b/src/DynareFlex.ll
index 7cc2b6cd..8b11ccb3 100644
--- a/src/DynareFlex.ll
+++ b/src/DynareFlex.ll
@@ -1,6 +1,6 @@
 /* -*- C++ -*- */
 /*
- * Copyright © 2003-2019 Dynare Team
+ * Copyright © 2003-2020 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -434,6 +434,8 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4]|w([1-9]{1}|[1-4][0-9]|5[0-2]))
 <DYNARE_STATEMENT>rescale_prediction_error_covariance {return token::RESCALE_PREDICTION_ERROR_COVARIANCE;}
 <DYNARE_STATEMENT>use_penalized_objective_for_hessian {return token::USE_PENALIZED_OBJECTIVE_FOR_HESSIAN;}
 <DYNARE_STATEMENT>expression    {return token::EXPRESSION;}
+<DYNARE_STATEMENT>occbin_likelihood {return token::OCCBIN_LIKELIHOOD;}
+<DYNARE_STATEMENT>occbin_smoother {return token::OCCBIN_SMOOTHER;}
 
 <DYNARE_STATEMENT>alpha {
   yylval->build<string>(yytext);
-- 
GitLab