From 03c6130e231a88a2e993864700cf138f270d229d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Thu, 29 Apr 2021 18:15:25 +0200
Subject: [PATCH] time-varying information set solver: restore
 oo_.{,exo_}steady_state at the end of the simulation

---
 matlab/time_varying_information_set_solver.m | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/matlab/time_varying_information_set_solver.m b/matlab/time_varying_information_set_solver.m
index 1a44bf7725..8b4cf9d44d 100644
--- a/matlab/time_varying_information_set_solver.m
+++ b/matlab/time_varying_information_set_solver.m
@@ -9,6 +9,10 @@ if ~islogical(terminal_steady_state_as_guess_value)
     error('A boolean must be given as second argument')
 end
 
+if ~isempty(ys0_)
+    error('Cannot be used in conjunction with endval')
+end
+
 periods = options_.periods;
 
 %% Read CSV file
@@ -50,6 +54,10 @@ end
 endo_simul = [repmat(oo_.steady_state, 1, M_.maximum_lag) NaN(M_.endo_nbr, periods+M_.maximum_lead)];
 exo_simul = [repmat(oo_.exo_steady_state',M_.maximum_lag,1); NaN(periods+M_.maximum_lead,M_.exo_nbr)];
 
+% Save initial steady state, for restoring it at the end
+initial_steady_state = oo_.steady_state;
+initial_exo_steady_state = oo_.exo_steady_state;
+
 % Start main loop around informational periods
 info_period = 1;
 while info_period <= periods
@@ -65,7 +73,7 @@ while info_period <= periods
         oo_.endo_simul(:, M_.maximum_lag+(1:periods-info_period+1)) = repmat(oo_.steady_state, 1, periods-info_period+1);
     elseif info_period == 1
         % Use initial steady state as guess value for first simulation if not using terminal steady state
-        oo_.endo_simul(:, M_.maximum_lag+(1:periods)) = repmat(steady_state_prev, 1, periods);
+        oo_.endo_simul(:, M_.maximum_lag+(1:periods)) = repmat(initial_steady_state, 1, periods);
     end
     oo_.endo_simul(:, end-M_.maximum_lead+1:end) = repmat(oo_.steady_state, 1, M_.maximum_lead);
     oo_.exo_simul = exo_simul(info_period:end, :);
@@ -91,4 +99,7 @@ options_.periods = periods;
 oo_.endo_simul = endo_simul;
 oo_.exo_simul = exo_simul;
 
+oo_.steady_state = initial_steady_state;
+oo_.exo_steady_state = initial_exo_steady_state;
+
 end
-- 
GitLab