From f14d2ea45cecf58bd6f7e3b9ecde9f75b9a24e24 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Mon, 23 Sep 2024 15:16:58 +0200
Subject: [PATCH] :bug: forecast and smoothing: disallow unsupported options

(cherry picked from commit db286c1a95ff36abbc7f6216d18a924650ad4d8f)
---
 matlab/dyn_forecast.m                   | 10 ++++++++++
 matlab/estimation/dynare_estimation_1.m | 21 +++++++++++++++++----
 matlab/forcst.m                         |  6 +++++-
 matlab/stochastic_solver/simultxdet.m   |  8 ++++++--
 4 files changed, 38 insertions(+), 7 deletions(-)

diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 828fa0e5dd..0ca6c51e7e 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -54,6 +54,16 @@ elseif nargin==6
     mean_varobs=dataset_info.descriptive.mean';
 end
 
+if options_.order>1 && M_.exo_det_nbr == 0 
+    error('forecasting without varexo_det does not support order>1.')
+end
+if options_.order>2 && M_.exo_det_nbr > 0
+    error('forecasting with varexo_det does not support order>2.')
+end
+if options_.order==2 && options_.pruning
+    error('forecasting with varexo_det does not support pruning.')
+end
+
 oo_=make_ex_(M_,options_,oo_);
 
 maximum_lag = M_.maximum_lag;
diff --git a/matlab/estimation/dynare_estimation_1.m b/matlab/estimation/dynare_estimation_1.m
index a80dc85f3b..9c9947a3c3 100644
--- a/matlab/estimation/dynare_estimation_1.m
+++ b/matlab/estimation/dynare_estimation_1.m
@@ -12,7 +12,7 @@ function dynare_estimation_1(var_list_,dname)
 % SPECIAL REQUIREMENTS
 %   none
 
-% Copyright © 2003-2023 Dynare Team
+% Copyright © 2003-2024 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -570,12 +570,25 @@ if options_.particle.status
 end
 
 %Run and store classical smoother if needed
-if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.pshape> 0) && options_.load_mh_file)) ...
-    || ~options_.smoother ) && ~options_.partial_information  % to be fixed
+if options_.smoother && ... %Bayesian smoother requested before
+    (any(bayestopt_.pshape > 0) && options_.mh_replic || ... % Bayesian with MCMC run
+    any(bayestopt_.pshape > 0) && options_.load_mh_file) % Bayesian with loaded MCMC
+    % nothing to do
+elseif options_.partial_information ||...
+    options_.order>1 %no particle smoother
+    % smoothing not yet supported
+else
     %% ML estimation, or posterior mode without Metropolis-Hastings or Metropolis without Bayesian smoothed variables
     oo_=save_display_classical_smoother_results(xparam1,M_,oo_,options_,bayestopt_,dataset_,dataset_info,estim_params_);
 end
-if options_.forecast > 0 && options_.mh_replic == 0 && ~options_.load_mh_file
+
+if options_.forecast == 0 || options_.mh_replic > 0 || options_.load_mh_file
+    % nothing to do
+elseif options_.order>1 && M_.exo_det_nbr == 0 || ...
+        options_.order>2 && M_.exo_det_nbr > 0 || ...
+        options_.order==2 && options_.pruning
+    %forecasting not yet supported
+else
     oo_.forecast = dyn_forecast(var_list_,M_,options_,oo_,'smoother',dataset_info);
 end
 
diff --git a/matlab/forcst.m b/matlab/forcst.m
index 79c1af76e4..c5e9c7afe5 100644
--- a/matlab/forcst.m
+++ b/matlab/forcst.m
@@ -22,7 +22,7 @@ function [yf,int_width,int_width_ME]=forcst(dr,y0,horizon,var_list,M_,oo_,option
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright © 2003-2019 Dynare Team
+% Copyright © 2003-2024 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -39,6 +39,10 @@ function [yf,int_width,int_width_ME]=forcst(dr,y0,horizon,var_list,M_,oo_,option
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 
+if options_.order>1
+    error('forcst.m: Only order=1 is supported. Skipping computuations.')
+end
+
 yf = simult_(M_,options_,y0,dr,zeros(horizon,M_.exo_nbr),1);
 nstatic = M_.nstatic;
 nspred = M_.nspred;
diff --git a/matlab/stochastic_solver/simultxdet.m b/matlab/stochastic_solver/simultxdet.m
index 074cc4b5e6..45512ad55a 100644
--- a/matlab/stochastic_solver/simultxdet.m
+++ b/matlab/stochastic_solver/simultxdet.m
@@ -1,5 +1,5 @@
 function [y_,int_width,int_width_ME]=simultxdet(y0,ex,ex_det, iorder,var_list,M_,oo_,options_)
-%function [y_,int_width]=simultxdet(y0,ex,ex_det, iorder,var_list,M_,oo_,options_)
+%function [y_,int_width,int_width_ME]=simultxdet(y0,ex,ex_det, iorder,var_list,M_,oo_,options_)
 %
 % Simulates a stochastic model in the presence of deterministic exogenous shocks
 %
@@ -39,6 +39,11 @@ function [y_,int_width,int_width_ME]=simultxdet(y0,ex,ex_det, iorder,var_list,M_
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 
+if options_.order>2 
+    error('simultxdet.m: Forecasting with varexo_det does not support order>2.')   
+elseif options_.order==2 && options_.pruning
+    error('simultxdet.m: Forecasting with varexo_det does not support pruning.')
+end
 dr = oo_.dr;
 ykmin = M_.maximum_lag;
 endo_nbr = M_.endo_nbr;
@@ -50,7 +55,6 @@ iter = size(ex,1);
 if size(ex_det, 1) ~= iter+ykmin
     error('Size mismatch: number of forecasting periods for stochastic exogenous and deterministic exogenous don''t match')
 end
-nx = size(dr.ghu,2);
 y_ = zeros(size(y0,1),iter+ykmin);
 y_(:,1:ykmin) = y0;
 k1 = ykmin:-1:1;
-- 
GitLab