From 2d80043a70805cf1885920d227e839204eb43ddf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 27 Nov 2024 17:33:22 +0100
Subject: [PATCH] =?UTF-8?q?Add=20dseries=20output=20to=20=E2=80=9Cperfect?=
 =?UTF-8?q?=5Fforesight=5Fwith=5Fexpectation=5Ferrors=5Fsolver=E2=80=9D=20?=
 =?UTF-8?q?command?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

By the way, only create the dseries output of “perfect_foresight_solver” when
requested.
---
 .../construct_simulation_dseries.m            | 37 +++++++++++++++++++
 .../perfect_foresight_solver.m                | 15 +-------
 ...foresight_with_expectation_errors_solver.m |  8 +++-
 preprocessor                                  |  2 +-
 4 files changed, 46 insertions(+), 16 deletions(-)
 create mode 100644 matlab/perfect-foresight-models/construct_simulation_dseries.m

diff --git a/matlab/perfect-foresight-models/construct_simulation_dseries.m b/matlab/perfect-foresight-models/construct_simulation_dseries.m
new file mode 100644
index 0000000000..17a62b7ad5
--- /dev/null
+++ b/matlab/perfect-foresight-models/construct_simulation_dseries.m
@@ -0,0 +1,37 @@
+function ts = construct_simulation_dseries(oo_, M_, first_simulation_period)
+% Returns a dseries object for the perfect foresight simulation.
+% Merges it with the contents of the datafile option on the initval_file command if provided.
+% first_simulation_period may be empty, in which case some default value will be used.
+
+% Copyright © 2021-2024 Dynare Team
+%
+% This file is part of Dynare.
+%
+% Dynare is free software: you can redistribute it and/or modify
+% it under the terms of the GNU General Public License as published by
+% the Free Software Foundation, either version 3 of the License, or
+% (at your option) any later version.
+%
+% Dynare is distributed in the hope that it will be useful,
+% but WITHOUT ANY WARRANTY; without even the implied warranty of
+% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+% GNU General Public License for more details.
+%
+% You should have received a copy of the GNU General Public License
+% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
+
+if isempty(first_simulation_period)
+    if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
+        first_simulation_period = oo_.initval_series.dates(1)+(M_.orig_maximum_lag-1);
+    else
+        first_simulation_period = dates(1,1);
+    end
+end
+
+ts = dseries([transpose(oo_.endo_simul(1:M_.orig_endo_nbr,:)), oo_.exo_simul], ...
+             first_simulation_period - M_.maximum_lag, [M_.endo_names(1:M_.orig_endo_nbr); M_.exo_names]);
+
+if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
+    names = ts.name;
+    ts = merge(oo_.initval_series{names{:}}, ts);
+end
diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m
index 5b5c71d2d1..cc4f2d60a0 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_solver.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m
@@ -274,19 +274,8 @@ if ~isempty(per_block_status)
     oo_.deterministic_simulation.block = per_block_status;
 end
 
-if isempty(first_simulation_period)
-    if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
-        first_simulation_period = oo_.initval_series.dates(1)+(M_.orig_maximum_lag-1);
-    else
-        first_simulation_period = dates(1,1);
-    end
-end
-
-ts = dseries([transpose(oo_.endo_simul(1:M_.orig_endo_nbr,:)), oo_.exo_simul], first_simulation_period - M_.maximum_lag, [M_.endo_names(1:M_.orig_endo_nbr); M_.exo_names]);
-
-if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
-    names = ts.name;
-    ts = merge(oo_.initval_series{names{:}}, ts);
+if nargout > 1
+    ts = construct_simulation_dseries(oo_, M_, first_simulation_period);
 end
 
 oo_.gui.ran_perfect_foresight = oo_.deterministic_simulation.status;
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 c5d9a10e39..0ea7575fcc 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
@@ -1,4 +1,4 @@
-function oo_=perfect_foresight_with_expectation_errors_solver(M_, options_, oo_)
+function [oo_, ts] = perfect_foresight_with_expectation_errors_solver(M_, options_, oo_)
 % INPUTS
 %   M_                  [structure] describing the model
 %   options_            [structure] describing the options
@@ -28,7 +28,7 @@ if options_.pfwee.constant_simulation_length && ~isempty(options_.simul.last_sim
     error('Options constant_simulation_length and last_simulation_period cannot be used together')
 end
 
-periods = get_simulation_periods(options_);
+[periods, first_simulation_period] = get_simulation_periods(options_);
 
 % Retrieve initial paths built by pfwee_setup
 % (the versions in oo_ will be truncated before calling perfect_foresight_solver)
@@ -114,3 +114,7 @@ end
 % Set final paths
 oo_.endo_simul = endo_simul;
 oo_.exo_simul = exo_simul;
+
+if nargout > 1
+    ts = construct_simulation_dseries(oo_, M_, first_simulation_period);
+end
diff --git a/preprocessor b/preprocessor
index a88ac75488..3d0989774b 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit a88ac75488d86a51f8febbead4692769722881c1
+Subproject commit 3d0989774b282fb2d875e9880be15e626cc69c40
-- 
GitLab