From cea9a851a5f4d8747ca41f835ac1213c0c1ce27c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Argos=29?=
 <stepan@adjemian.eu>
Date: Thu, 13 Mar 2025 14:38:18 +0100
Subject: [PATCH] Bug fix in SEP with sparse and perfect tree.

The initial condition of the state variables was not updated in each iteration.
---
 matlab/ep/ep_problem_2.m                               | 2 +-
 matlab/ep/solve_stochastic_perfect_foresight_model_0.m | 4 ++--
 matlab/ep/solve_stochastic_perfect_foresight_model_1.m | 5 ++---
 3 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/matlab/ep/ep_problem_2.m b/matlab/ep/ep_problem_2.m
index d94f277f8b..2fda1838bc 100644
--- a/matlab/ep/ep_problem_2.m
+++ b/matlab/ep/ep_problem_2.m
@@ -39,7 +39,7 @@ i_hc = i_cols_f - 2*ny;
 
 nzA = cell(periods,world_nbr);
 res = zeros(ny,periods,world_nbr);
-Y = pfm.Y; %zeros(ny*(periods+2),world_nbr);
+Y = zeros(ny*(periods+2),world_nbr);
 Y(1:ny,1) = pfm.y0;
 Y(end-ny+1:end,:) = repmat(steady_state,1,world_nbr);
 Y(pfm.i_upd_y) = y;
diff --git a/matlab/ep/solve_stochastic_perfect_foresight_model_0.m b/matlab/ep/solve_stochastic_perfect_foresight_model_0.m
index 02947bb21b..19e77a8315 100644
--- a/matlab/ep/solve_stochastic_perfect_foresight_model_0.m
+++ b/matlab/ep/solve_stochastic_perfect_foresight_model_0.m
@@ -78,7 +78,6 @@ if update_pfm_struct
     [~, jacobian] = dynamic_model(z, exo_simul, pfm.params, pfm.steady_state, 2);
 
     world_nbr = nnodes^order;
-    Y = repmat(endo_simul(:),1,world_nbr);
 
     % The columns of A map the elements of Y such that
     % each block of Y with ny rows are unfolded column wise
@@ -142,12 +141,13 @@ if update_pfm_struct
     pfm.i_cols_T = i_cols_T;
     pfm.i_upd_r = i_upd_r;
     pfm.i_upd_y = i_upd_y;
-    pfm.Y = Y;
 
     pfm.dimension = dimension;
 
 end
 
+pfm.Y = repmat(endo_simul(:),1,pfm.world_nbr);
+
 y = repmat(pfm.steady_state, pfm.dimension/pfm.ny, 1);
 
 if update_options_struct
diff --git a/matlab/ep/solve_stochastic_perfect_foresight_model_1.m b/matlab/ep/solve_stochastic_perfect_foresight_model_1.m
index d65c464757..ccdc882f61 100644
--- a/matlab/ep/solve_stochastic_perfect_foresight_model_1.m
+++ b/matlab/ep/solve_stochastic_perfect_foresight_model_1.m
@@ -72,9 +72,6 @@ if update_pfm_struct
     % The third row block is ny x nnodes^2
     % and so on until size ny x nnodes^order
     world_nbr = pfm.world_nbr;
-    % Y = endo_simul(:,2:end-1);
-    pfm.Y = repmat(endo_simul(:), 1, world_nbr);
-    pfm.y0 = endo_simul(:,1);
 
     % The columns of A map the elements of Y such that
     % each block of Y with ny rows are unfolded column wise
@@ -133,5 +130,7 @@ if update_options_struct
     end
 end
 
+pfm.y0 = endo_simul(:,1);
+
 [y, errorflag, ~, ~, errorcode] = dynare_solve(@ep_problem_2, y, options_.simul.maxit, options_.dynatol.f, options_.dynatol.x, options_, exo_simul, pfm);
 endo_simul(:,2) = y(1:pfm.ny);
-- 
GitLab