From 373346b01ac7b55488edb4d42bcc128b4a1a700b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 3 Oct 2024 13:00:12 -0400
Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Perfect=20foresight=20with=20lin?=
 =?UTF-8?q?ear=20approximation:=20path=20for=20endogenous=20after=20failur?=
 =?UTF-8?q?e=20to=20converge=20was=20not=20centered?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Also add a test for that codepath (linear_approximation + stack_solve_algo=0).

(partial and manually adapted cherry-pick from d2065889643ae044623a4ac14ebaaab927be9de1)
---
 matlab/perfect-foresight-models/sim1_linear.m        |  2 +-
 .../linear_approximation/sw.mod                      | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/matlab/perfect-foresight-models/sim1_linear.m b/matlab/perfect-foresight-models/sim1_linear.m
index 0cf316034b..5136f94015 100644
--- a/matlab/perfect-foresight-models/sim1_linear.m
+++ b/matlab/perfect-foresight-models/sim1_linear.m
@@ -239,7 +239,7 @@ end
 
 if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) || ~isreal(res) || ~isreal(Y)
     success = false; % NaN or Inf occurred
-    endogenousvariables = reshape(Y, ny, periods+maximum_lag+M_.maximum_lead);
+    endogenousvariables = bsxfun(@plus, reshape(Y, ny, periods+maximum_lag+M_.maximum_lead), steadystate_y);
     if verbose
         skipline()
         if ~isreal(res) || ~isreal(Y)
diff --git a/tests/deterministic_simulations/linear_approximation/sw.mod b/tests/deterministic_simulations/linear_approximation/sw.mod
index 949d427267..a4767a8b00 100644
--- a/tests/deterministic_simulations/linear_approximation/sw.mod
+++ b/tests/deterministic_simulations/linear_approximation/sw.mod
@@ -133,3 +133,15 @@ end
 if max(abs(endo_simul_0(:)-endo_simul_1(:)))>.01*options_.dynatol.f
     error('Something is wrong!')
 end
+
+// Test stack_solve_algo=0+linear_approximation, which uses a different codepath than stack_solve_algo=7
+perfect_foresight_setup(periods=300);
+perfect_foresight_solver(linear_approximation, stack_solve_algo=0);
+endo_simul_2 = oo_.endo_simul;
+if ~oo_.deterministic_simulation.status
+   error('Perfect foresight simulation failed')
+end
+
+if max(abs(endo_simul_0(:)-endo_simul_2(:)))>.01*options_.dynatol.f
+    error('Something is wrong!')
+end
-- 
GitLab