From 0213b140b563d571fb1fbcdcf310dc8be4a34ed6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 18 Mar 2025 17:32:49 +0100
Subject: [PATCH] =?UTF-8?q?Perfect=20foresight:=20move=20test=20about=20?=
 =?UTF-8?q?=E2=80=9Clearnt=5Fin=E2=80=9D=20blocks=20from=20pf=5Fsolver=20t?=
 =?UTF-8?q?o=20pf=5Fsetup?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Leads to a simpler code.
---
 .../perfect_foresight_setup.m                 |  6 +++++-
 .../perfect_foresight_solver.m                | 13 ++----------
 ...foresight_with_expectation_errors_solver.m |  4 ++--
 tests/deterministic_simulations/pfwee.mod     | 21 ++++++++++---------
 .../pfwee_constant_sim_length.mod             | 15 ++++++-------
 .../pfwee_learnt_in.inc                       | 15 ++++++-------
 6 files changed, 36 insertions(+), 38 deletions(-)

diff --git a/matlab/perfect-foresight-models/perfect_foresight_setup.m b/matlab/perfect-foresight-models/perfect_foresight_setup.m
index ff65afb797..287316777f 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_setup.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_setup.m
@@ -14,7 +14,7 @@ function oo_=perfect_foresight_setup(M_, options_, oo_)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright © 1996-2024 Dynare Team
+% Copyright © 1996-2025 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -72,5 +72,9 @@ if options_.simul.endval_steady && M_.maximum_lead == 0
     error('PERFECT_FORESIGHT_SETUP: Option endval_steady cannot be used on a purely backward or static model.')
 end
 
+if ~isempty(M_.learnt_shocks) || ~isempty(M_.learnt_endval)
+    error('A shocks(learnt_in=...) or endval(learnt_in=...) block is present. You want to call perfect_foresight_with_expectations_error_setup and perfect_foresight_with_expectations_error_solver.')
+end
+
 oo_ = make_ex_(M_,options_,oo_);
 oo_ = make_y_(M_,options_,oo_);
diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m
index cc4f2d60a0..d351f2864b 100644
--- a/matlab/perfect-foresight-models/perfect_foresight_solver.m
+++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m
@@ -1,13 +1,10 @@
-function [oo_, ts]=perfect_foresight_solver(M_, options_, oo_, no_error_if_learnt_in_is_present, marginal_linearization_previous_raw_sims)
+function [oo_, ts]=perfect_foresight_solver(M_, options_, oo_, marginal_linearization_previous_raw_sims)
 % Computes deterministic simulations
 %
 % INPUTS
 %   M_                  [structure] describing the model
 %   options_            [structure] describing the options
 %   oo_                 [structure] storing the results
-%   no_error_if_learnt_in_is_present [boolean, optional]
-%       if true, then do not error out if a shocks(learnt_in=…) or endval(learnt_in=…)
-%       block is present
 %   marginal_linearization_previous_raw_sims [struct, optional]
 %       if not empty, contains the two simulations used to compute the extrapolation by marginal
 %       linearization in a previous informational period, in the context of
@@ -23,7 +20,7 @@ function [oo_, ts]=perfect_foresight_solver(M_, options_, oo_, no_error_if_learn
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright © 1996-2024 Dynare Team
+% Copyright © 1996-2025 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -43,14 +40,8 @@ function [oo_, ts]=perfect_foresight_solver(M_, options_, oo_, no_error_if_learn
 check_input_arguments(options_, M_, oo_);
 
 if nargin < 4
-    no_error_if_learnt_in_is_present = false;
-end
-if nargin < 5
     marginal_linearization_previous_raw_sims = [];
 end
-if (~isempty(M_.learnt_shocks) || ~isempty(M_.learnt_endval)) && ~no_error_if_learnt_in_is_present
-    error('A shocks(learnt_in=...) or endval(learnt_in=...) block is present. You want to call perfect_foresight_with_expectations_error_setup and perfect_foresight_with_expectations_error_solver.')
-end
 
 [periods, first_simulation_period, last_simulation_period] = get_simulation_periods(options_);
 
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 22a07c276f..105de132fa 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
@@ -7,7 +7,7 @@ function [oo_, ts] = perfect_foresight_with_expectation_errors_solver(M_, option
 % OUTPUTS
 %   oo_                 [structure] storing the results
 
-% Copyright © 2021-2024 Dynare Team
+% Copyright © 2021-2025 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -85,7 +85,7 @@ while info_period <= periods
         marginal_linearization_previous_raw_sims = [];
     end
 
-    oo_= perfect_foresight_solver(M_, options_, oo_, true, marginal_linearization_previous_raw_sims);
+    oo_= perfect_foresight_solver(M_, options_, oo_, marginal_linearization_previous_raw_sims);
 
     if ~oo_.deterministic_simulation.status
         error('perfect_foresight_with_expectation_errors_solver: failed to compute solution for information available at period %d\n', info_period)
diff --git a/tests/deterministic_simulations/pfwee.mod b/tests/deterministic_simulations/pfwee.mod
index 0280b60537..d5f80859b2 100644
--- a/tests/deterministic_simulations/pfwee.mod
+++ b/tests/deterministic_simulations/pfwee.mod
@@ -37,29 +37,30 @@ perfect_foresight_with_expectation_errors_solver;
 pfwee_simul = oo_.endo_simul;
 
 // Now compute the solution by hand to verify the results
+verbatim;
+
 oo_.steady_state = orig_steady_state;
 oo_.exo_steady_state = orig_exo_steady_state;
 
-perfect_foresight_setup;
-
-verbatim;
+oo_ = make_ex_(M_,options_,oo_);
+oo_ = make_y_(M_,options_,oo_);
 
 % Information arriving in period 1 (temp shock now)
 oo_.exo_simul(2,1) = 1.2;
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 
 % Information arriving in period 2 (temp shock now + permanent shock in future)
 oo_.exo_simul(3,1) = 1.3;
 oo_.exo_steady_state = 1.1;
 oo_.exo_simul(end, 1) = oo_.exo_steady_state;
-oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
+oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_);
 oo_.endo_simul(:, end) = oo_.steady_state;
 options_.periods = 6;
 saved_endo = oo_.endo_simul(:, 1);
 saved_exo = oo_.exo_simul(1, :);
 oo_.endo_simul = oo_.endo_simul(:, 2:end);
 oo_.exo_simul = oo_.exo_simul(2:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -67,14 +68,14 @@ oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 oo_.exo_simul(4,1) = 1.4;
 oo_.exo_steady_state = 1.2;
 oo_.exo_simul(end, 1) = oo_.exo_steady_state;
-oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
+oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_);
 oo_.endo_simul(:, end) = oo_.steady_state;
 options_.periods = 5;
 saved_endo = oo_.endo_simul(:, 1:2);
 saved_exo = oo_.exo_simul(1:2, :);
 oo_.endo_simul = oo_.endo_simul(:, 3:end);
 oo_.exo_simul = oo_.exo_simul(3:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -83,14 +84,14 @@ oo_.exo_simul(7,1) = 1.1;
 oo_.exo_simul(8,1) = 1.1;
 oo_.exo_steady_state = 1.1;
 oo_.exo_simul(end, 1) = oo_.exo_steady_state;
-oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
+oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_);
 oo_.endo_simul(:, end) = oo_.steady_state;
 options_.periods = 2;
 saved_endo = oo_.endo_simul(:, 1:5);
 saved_exo = oo_.exo_simul(1:5, :);
 oo_.endo_simul = oo_.endo_simul(:, 6:end);
 oo_.exo_simul = oo_.exo_simul(6:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_,true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
diff --git a/tests/deterministic_simulations/pfwee_constant_sim_length.mod b/tests/deterministic_simulations/pfwee_constant_sim_length.mod
index a081e19d7c..40c1ebdcbf 100644
--- a/tests/deterministic_simulations/pfwee_constant_sim_length.mod
+++ b/tests/deterministic_simulations/pfwee_constant_sim_length.mod
@@ -36,16 +36,17 @@ perfect_foresight_with_expectation_errors_solver(constant_simulation_length);
 pfwee_simul = oo_.endo_simul;
 
 // Now compute the solution by hand to verify the results
+verbatim;
+
 oo_.steady_state = orig_steady_state;
 oo_.exo_steady_state = orig_exo_steady_state;
 
-perfect_foresight_setup;
-
-verbatim;
+oo_ = make_ex_(M_,options_,oo_);
+oo_ = make_y_(M_,options_,oo_);
 
 % Information arriving in period 1 (temp shock now)
 oo_.exo_simul(2,1) = 1.2;
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 
 % Information arriving in period 2 (temp shock now + permanent shock in future)
 oo_.exo_simul(3,1) = 1.3;
@@ -57,7 +58,7 @@ saved_endo = oo_.endo_simul(:, 1);
 saved_exo = oo_.exo_simul(1, :);
 oo_.endo_simul = oo_.endo_simul(:, 2:end);
 oo_.exo_simul = oo_.exo_simul(2:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -71,7 +72,7 @@ saved_endo = oo_.endo_simul(:, 1:2);
 saved_exo = oo_.exo_simul(1:2, :);
 oo_.endo_simul = oo_.endo_simul(:, 3:end);
 oo_.exo_simul = oo_.exo_simul(3:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -87,7 +88,7 @@ saved_endo = oo_.endo_simul(:, 1:5);
 saved_exo = oo_.exo_simul(1:5, :);
 oo_.endo_simul = oo_.endo_simul(:, 6:end);
 oo_.exo_simul = oo_.exo_simul(6:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
diff --git a/tests/deterministic_simulations/pfwee_learnt_in.inc b/tests/deterministic_simulations/pfwee_learnt_in.inc
index 505968edca..46d0390fc1 100644
--- a/tests/deterministic_simulations/pfwee_learnt_in.inc
+++ b/tests/deterministic_simulations/pfwee_learnt_in.inc
@@ -112,12 +112,13 @@ perfect_foresight_with_expectation_errors_solver;
 pfwee_simul = oo_.endo_simul;
 
 // Now compute the solution by hand to verify the results
+verbatim;
+
 oo_.steady_state = orig_steady_state;
 oo_.exo_steady_state = orig_exo_steady_state;
 
-perfect_foresight_setup;
-
-verbatim;
+oo_ = make_ex_(M_,options_,oo_);
+oo_ = make_y_(M_,options_,oo_);
 
 @#if dates
 % Reset {first,last}_simulation_period since we’re going to manipulate options_.periods
@@ -132,7 +133,7 @@ oo_.exo_simul(4:end,1) = 1.05;
 oo_.exo_steady_state = 1.05;
 oo_.steady_state = evaluate_steady_state(oo_.steady_state, oo_.exo_steady_state, M_, options_, true);
 oo_.endo_simul(:, end) = oo_.steady_state;
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 
 % Information arriving in period 2 (temp shock now + permanent shock in future)
 oo_.exo_simul(3,1) = 1.3;
@@ -145,7 +146,7 @@ saved_endo = oo_.endo_simul(:, 1);
 saved_exo = oo_.exo_simul(1, :);
 oo_.endo_simul = oo_.endo_simul(:, 2:end);
 oo_.exo_simul = oo_.exo_simul(2:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -162,7 +163,7 @@ saved_endo = oo_.endo_simul(:, 1:2);
 saved_exo = oo_.exo_simul(1:2, :);
 oo_.endo_simul = oo_.endo_simul(:, 3:end);
 oo_.exo_simul = oo_.exo_simul(3:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
@@ -178,7 +179,7 @@ saved_endo = oo_.endo_simul(:, 1:5);
 saved_exo = oo_.exo_simul(1:5, :);
 oo_.endo_simul = oo_.endo_simul(:, 6:end);
 oo_.exo_simul = oo_.exo_simul(6:end, :);
-oo_=perfect_foresight_solver(M_, options_, oo_, true);
+oo_=perfect_foresight_solver(M_, options_, oo_);
 oo_.endo_simul = [ saved_endo oo_.endo_simul ];
 oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
 
-- 
GitLab