From 2e3fbfc0408a360dd169c927a5ea38c89f6ff95c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 6 Oct 2023 17:45:14 -0400
Subject: [PATCH] =?UTF-8?q?New=20option=20=E2=80=9Cfrom=5Finitval=5Fto=5Fe?=
 =?UTF-8?q?ndval=E2=80=9D=20to=20=E2=80=9Chomotopy=5Fsetup=E2=80=9D=20bloc?=
 =?UTF-8?q?k?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 doc/manual/source/the-model-file.rst          | 19 +++++++++++++++++--
 matlab/steady.m                               | 14 +++++++++++++-
 meson.build                                   |  2 ++
 preprocessor                                  |  2 +-
 tests/steady_state/homotopy/common.mod        | 15 +++++++++++++++
 .../homotopy_from_initval_to_endval.mod       |  3 +++
 6 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100644 tests/steady_state/homotopy/homotopy_from_initval_to_endval.mod

diff --git a/doc/manual/source/the-model-file.rst b/doc/manual/source/the-model-file.rst
index e0405236e1..dcf20277e0 100644
--- a/doc/manual/source/the-model-file.rst
+++ b/doc/manual/source/the-model-file.rst
@@ -3101,6 +3101,7 @@ After computation, the steady state is available in the following variable:
     been computed with ``steady``, it will first try to compute it.
 
 .. block:: homotopy_setup ;
+           homotopy_setup(from_initval_to_endval) ;
 
     This block is used to declare initial and final values when using
     a homotopy method. It is used in conjunction with the option
@@ -3129,12 +3130,26 @@ After computation, the steady state is available in the following variable:
 
     Here only the final value is specified for a given
     parameter/exogenous; the initial value is taken from the
-    preceeding ``initval`` block.
+    preceeding ``initval`` block (or from the preceeding ``endval`` block if
+    there is one before the ``homotopy_setup`` block).
 
     A necessary condition for a successful homotopy is that Dynare
     must be able to solve the steady state for the initial
     parameters/exogenous without additional help (using the guess
-    values given in the ``initval`` block).
+    values given in the ``initval`` or ``endval`` block).
+
+    The ``from_initval_to_endval`` option can be used in the context of a
+    permanent shock, when the initial steady state has already been computed.
+    This option can be used following the ``endval`` block that describes the
+    terminal steady state. In that case, in the subsequent ``steady`` command,
+    Dynare will perform a homotopy from the initial to the terminal steady
+    state (technically, using this option is equivalent to writing a
+    ``homotopy_setup`` block where all exogenous variables are asked to
+    transition from their values in the ``initval`` to their values in the
+    ``endval`` block). When this option is used, the ``homotopy_setup`` block
+    is typically empty (but it’s nevertheless possible to add explicit
+    directives for moving exogenous or parameters; these will be added on top
+    of those implicitly generated by the ``from_initval_to_endval`` option).
 
     If the homotopy fails, a possible solution is to increase the
     number of steps (given in ``homotopy_steps`` option of
diff --git a/matlab/steady.m b/matlab/steady.m
index 9866224b32..106f4c201f 100644
--- a/matlab/steady.m
+++ b/matlab/steady.m
@@ -28,7 +28,7 @@ function steady()
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 
-global M_ oo_ options_
+global M_ oo_ options_ ex0_
 
 test_for_deep_parameters_calibration(M_);
 
@@ -62,6 +62,18 @@ if options_.homotopy_mode ~= 0
         error('HOMOTOPY_SETUP: incorrect variable types specified')
     end
 
+    % If the “from_initval_to_endval” option was passed to the “homotopy_setup” block, add the relevant homotopy information
+    if options_.homotopy_from_initval_to_endval
+        if isempty(ex0_)
+            error('HOMOTOPY_SETUP: the from_initval_to_endval option cannot be used without an endval block')
+        end
+        for i = 1:M_.exo_nbr
+            if ~any(hv(:,1)==1 & hv(:,2)==i) % Do not overwrite information manually specified by the user
+                hv = vertcat(hv, [ 1 i ex0_(i) oo_.exo_steady_state(i)]);
+            end
+        end
+    end
+
     homotopy_func = str2func(['homotopy' num2str(options_.homotopy_mode)]);
     [M_,oo_,errorcode] = homotopy_func(hv, options_.homotopy_steps, M_, options_, oo_);
 
diff --git a/meson.build b/meson.build
index 136beca405..647b8d6b15 100644
--- a/meson.build
+++ b/meson.build
@@ -1062,6 +1062,8 @@ mod_and_m_tests = [
     'extra' : [ 'steady_state/homotopy/common.mod' ] },
   { 'test' : [ 'steady_state/homotopy/homotopy3_test.mod' ],
     'extra' : [ 'steady_state/homotopy/common.mod' ] },
+  { 'test' : [ 'steady_state/homotopy/homotopy_from_initval_to_endval.mod' ],
+    'extra' : [ 'steady_state/homotopy/common.mod' ] },
   { 'test' : [ 'bvar_a_la_sims/bvar_standalone.mod' ],
     'extra' : [ 'bvar_a_la_sims/bvar_sample.m' ] },
   { 'test' : [ 'bvar_a_la_sims/bvar_and_dsge.mod' ],
diff --git a/preprocessor b/preprocessor
index 86b24dc9bf..084372a314 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit 86b24dc9bf1c8fa5738f084e3cfac54d5523ca56
+Subproject commit 084372a314a5f3081dc055ba83dd879947809576
diff --git a/tests/steady_state/homotopy/common.mod b/tests/steady_state/homotopy/common.mod
index 347dd464ab..eea56c1b7a 100644
--- a/tests/steady_state/homotopy/common.mod
+++ b/tests/steady_state/homotopy/common.mod
@@ -19,11 +19,26 @@ k = ((delt+bet)/(aa*x*alph))^(1/(alph-1));
 c = aa*x*k^alph-delt*k;
 end;
 
+@#ifdef homotopy_from_initval_to_endval
+
+steady;
+
+endval;
+x = 2;
+end;
+
+homotopy_setup(from_initval_to_endval);
+end;
+
+@#else
+
 homotopy_setup;
 bet, 0.05, 0.1;
 x, 2;
 end;
 
+@#endif
+
 steady(homotopy_mode = @{homotopy_mode}, homotopy_steps = 50);
 
 if abs(oo_.steady_state(1)/(aa*oo_.exo_steady_state(1)*oo_.steady_state(2)^alph-delt*oo_.steady_state(2)) - 1) > 1e-4
diff --git a/tests/steady_state/homotopy/homotopy_from_initval_to_endval.mod b/tests/steady_state/homotopy/homotopy_from_initval_to_endval.mod
new file mode 100644
index 0000000000..40064187c1
--- /dev/null
+++ b/tests/steady_state/homotopy/homotopy_from_initval_to_endval.mod
@@ -0,0 +1,3 @@
+@#define homotopy_from_initval_to_endval
+@#define homotopy_mode = 3
+@#include "common.mod"
-- 
GitLab