From 85bea5a68bcee16fe3c707c62e34bef56608e056 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 24 Mar 2025 17:31:47 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20crash=20of=20perfect=20for?=
 =?UTF-8?q?esight=20w/=20expectation=20errors=20+=20marginal=20linearizati?=
 =?UTF-8?q?on=20+=20several=20learnt=5Fin=20blocks?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../perfect_foresight_with_expectation_errors_solver.m    | 8 ++++----
 .../pfwee_homotopy_marginal_linearization.mod             | 6 ++++++
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_solver.m b/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_solver.m
index 105de132fa..22996f1cd1 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_solver.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_with_expectation_errors_solver.m
@@ -75,11 +75,11 @@ while info_period <= periods
     options_.simul.last_simulation_period = dates();
 
     if info_period > 1 && homotopy_completion_share < 1 && options_.simul.homotopy_marginal_linearization_fallback > 0
-        marginal_linearization_previous_raw_sims.sim1.endo_simul = oo_.deterministic_simulation.sim1.endo_simul(:, info_period:end);
-        marginal_linearization_previous_raw_sims.sim1.exo_simul = oo_.deterministic_simulation.sim1.exo_simul(info_period:end, :);
+        marginal_linearization_previous_raw_sims.sim1.endo_simul = oo_.deterministic_simulation.sim1.endo_simul(:, increment+1:end);
+        marginal_linearization_previous_raw_sims.sim1.exo_simul = oo_.deterministic_simulation.sim1.exo_simul(increment+1:end, :);
         marginal_linearization_previous_raw_sims.sim1.homotopy_completion_share = oo_.deterministic_simulation.sim1.homotopy_completion_share;
-        marginal_linearization_previous_raw_sims.sim2.endo_simul = oo_.deterministic_simulation.sim2.endo_simul(:, info_period:end);
-        marginal_linearization_previous_raw_sims.sim2.exo_simul = oo_.deterministic_simulation.sim2.exo_simul(info_period:end, :);
+        marginal_linearization_previous_raw_sims.sim2.endo_simul = oo_.deterministic_simulation.sim2.endo_simul(:, increment+1:end);
+        marginal_linearization_previous_raw_sims.sim2.exo_simul = oo_.deterministic_simulation.sim2.exo_simul(increment+1:end, :);
         marginal_linearization_previous_raw_sims.sim2.homotopy_completion_share = oo_.deterministic_simulation.sim2.homotopy_completion_share;
     else
         marginal_linearization_previous_raw_sims = [];
diff --git a/tests/deterministic_simulations/pfwee_homotopy_marginal_linearization.mod b/tests/deterministic_simulations/pfwee_homotopy_marginal_linearization.mod
index d884a10290..1151349d3f 100644
--- a/tests/deterministic_simulations/pfwee_homotopy_marginal_linearization.mod
+++ b/tests/deterministic_simulations/pfwee_homotopy_marginal_linearization.mod
@@ -32,6 +32,12 @@ endval(learnt_in = 5);
   LoggedProductivityInnovation = 1;
 end;
 
+shocks(learnt_in = 3);
+  var LoggedProductivityInnovation;
+  periods 4;
+  values 0.2;
+end;
+
 perfect_foresight_with_expectation_errors_setup(periods=200);
 perfect_foresight_with_expectation_errors_solver(homotopy_max_completion_share = 0.8, homotopy_marginal_linearization_fallback, steady_solve_algo = 13);
 
-- 
GitLab