From 4db28998687b8845568e8750d99a1b45f0d6a94f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 16 Dec 2021 15:36:23 +0100
Subject: [PATCH] estimated_params: fixes for new semantics
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The change implemented in 21a8a5794a83b5098cad0fe7beab93dcbe5ef6bf broke the
testsuite and was incomplete.

– new option “overwrite” to provide the old behaviour (used to fix the testsuite);
– when concatenating several “estimated_params” block, the preprocessor now
  checks that paramateres are not declared twice across blocks.
---
 doc/manual/source/the-model-file.rst                  | 11 +++++++++--
 preprocessor                                          |  2 +-
 tests/TeX/fs2000_corr_ME.mod                          |  2 +-
 .../fs2000_analytic_derivation.mod                    |  2 +-
 .../RBC/RBC_MoM_GMM_gradient_optim.mod                | 10 ++++------
 .../method_of_moments/RBC/RBC_MoM_optimizer.mod       |  7 +++----
 .../fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod         |  2 +-
 tests/moments/fs2000_post_moments.mod                 |  2 +-
 8 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst
index 8d09b4f7c1..401d2ce59e 100644
--- a/doc/manual/source/the-model-file.rst
+++ b/doc/manual/source/the-model-file.rst
@@ -5276,6 +5276,7 @@ block decomposition of the model (see :opt:`block`).
             end;
 
 .. block:: estimated_params ;
+           estimated_params (overwrite) ;
 
     |br| This block lists all parameters to be estimated and specifies
     bounds and priors as necessary.
@@ -5465,8 +5466,14 @@ block decomposition of the model (see :opt:`block`).
             end;
 
 
-    It is possible to have several ``estimated_params`` blocks, in which case
-    they will be concatenated.
+    It is possible to have several ``estimated_params`` blocks. By default,
+    subsequent blocks are concatenated with the previous ones; this can be
+    useful when building models in a modular fashion (see also
+    :bck:`estimated_params_remove` for that use case). However, if an
+    ``estimated_params`` block has the ``overwrite`` option, its contents
+    becomes the new list of estimated parameters, cancelling previous blocks;
+    this can be useful when doing several estimations in a single ``.mod``
+    file.
 
 .. block:: estimated_params_init ;
            estimated_params_init (OPTIONS...);
diff --git a/preprocessor b/preprocessor
index 5ffbc5bad3..7dde09169e 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit 5ffbc5bad31c306f561d673cde36d460cb5906c1
+Subproject commit 7dde09169ecd4b5e32b3cd35bef88958a262ec11
diff --git a/tests/TeX/fs2000_corr_ME.mod b/tests/TeX/fs2000_corr_ME.mod
index efb79485f1..31aa48351d 100644
--- a/tests/TeX/fs2000_corr_ME.mod
+++ b/tests/TeX/fs2000_corr_ME.mod
@@ -158,7 +158,7 @@ estimation(order=1,datafile='../fs2000/fsdat_simul',mode_check,smoother,filter_c
 
 
 
-estimated_params;
+estimated_params(overwrite);
 //alp, beta_pdf, 0.356, 0.02;
 gam, normal_pdf, 0.0085, 0.003;
 //del, beta_pdf, 0.01, 0.005;
diff --git a/tests/analytic_derivatives/fs2000_analytic_derivation.mod b/tests/analytic_derivatives/fs2000_analytic_derivation.mod
index 9cf36ae9f3..05a35aaec2 100644
--- a/tests/analytic_derivatives/fs2000_analytic_derivation.mod
+++ b/tests/analytic_derivatives/fs2000_analytic_derivation.mod
@@ -101,7 +101,7 @@ if abs(fval_ML_1-fval_ML_2)>1e-5 || abs(fval_ML_1-fval_ML_3)>1e-5
 end
 options_.debug=0;
 
-estimated_params;
+estimated_params(overwrite);
 alp, beta_pdf, 0.356, 0.02;
 rho, beta_pdf, 0.129, 0.100;
 psi, beta_pdf, 0.65, 0.05;
diff --git a/tests/estimation/method_of_moments/RBC/RBC_MoM_GMM_gradient_optim.mod b/tests/estimation/method_of_moments/RBC/RBC_MoM_GMM_gradient_optim.mod
index 6b68071490..85427c0426 100644
--- a/tests/estimation/method_of_moments/RBC/RBC_MoM_GMM_gradient_optim.mod
+++ b/tests/estimation/method_of_moments/RBC/RBC_MoM_GMM_gradient_optim.mod
@@ -46,10 +46,8 @@ end;
 
 
 @#for estimParams in [0, 1, 2]
-  clear estim_params_;
-
   @#if estimParams == 0
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         0.025;
         %BETTA,         0.984;
         %B,             0.5;
@@ -61,7 +59,7 @@ end;
   @#endif
 
   @#if estimParams == 1
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         ,        0,           1;
         %BETTA,         ,        0,           1;
         %B,             ,        0,           1;
@@ -73,7 +71,7 @@ end;
   @#endif
 
   @#if estimParams == 2
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         0.025,         0,           1,  normal_pdf, 0.02, 0.5;
         %BETTA,         0.98,         0,           1,  beta_pdf, 0.90, 0.25;
         %B,             0.45,         0,           1,  normal_pdf, 0.40, 0.5;
@@ -122,4 +120,4 @@ end;
     @#endif
   @#endfor  
   
-@#endfor
\ No newline at end of file
+@#endfor
diff --git a/tests/estimation/method_of_moments/RBC/RBC_MoM_optimizer.mod b/tests/estimation/method_of_moments/RBC/RBC_MoM_optimizer.mod
index aaaaeec594..a59fdce8b9 100644
--- a/tests/estimation/method_of_moments/RBC/RBC_MoM_optimizer.mod
+++ b/tests/estimation/method_of_moments/RBC/RBC_MoM_optimizer.mod
@@ -72,9 +72,8 @@ options_.solveopt.TolXConstraint=1e-3;
 
 
 @#for estimParams in [0, 1, 2]
-  clear estim_params_;
   @#if estimParams == 0
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         0.025;
         %BETTA,         0.984;
         %B,             0.5;
@@ -87,7 +86,7 @@ options_.solveopt.TolXConstraint=1e-3;
   @#endif
 
   @#if estimParams == 1
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         ,        0,           1;
         %BETTA,         ,        0,           1;
         %B,             ,        0,           1;
@@ -100,7 +99,7 @@ options_.solveopt.TolXConstraint=1e-3;
   @#endif
 
   @#if estimParams == 2
-    estimated_params;
+    estimated_params(overwrite);
         %DELTA,         0.025,         0,           1,  normal_pdf, 0.02, 0.5;
         %BETTA,         0.98,         0,           1,  beta_pdf, 0.90, 0.25;
         %B,             0.45,         0,           1,  normal_pdf, 0.40, 0.5;
diff --git a/tests/measurement_errors/fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod b/tests/measurement_errors/fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod
index 4adf427213..16ebcedd2f 100644
--- a/tests/measurement_errors/fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod
+++ b/tests/measurement_errors/fs2000_corr_me_ml_mcmc/fs2000_corr_ME.mod
@@ -116,7 +116,7 @@ estimation(order=1,datafile=fsdat_simul,mode_check,smoother,filter_decomposition
 
 
 
-estimated_params;
+estimated_params(overwrite);
 //alp, beta_pdf, 0.356, 0.02;
 gam, normal_pdf, 0.0085, 0.003;
 //del, beta_pdf, 0.01, 0.005;
diff --git a/tests/moments/fs2000_post_moments.mod b/tests/moments/fs2000_post_moments.mod
index a28deb64e7..ea70e28b1a 100644
--- a/tests/moments/fs2000_post_moments.mod
+++ b/tests/moments/fs2000_post_moments.mod
@@ -181,7 +181,7 @@ for var_iter_1=1:nvars
 end
 
 // case with measurement error
-estimated_params;
+estimated_params(overwrite);
 alp, beta_pdf, 0.356, 0.02;
 bet, beta_pdf, 0.993, 0.002;
 gam, normal_pdf, 0.0085, 0.003;
-- 
GitLab