From f4b6199746088517901b44b8902eccc8c3d20666 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Mon, 25 Mar 2024 11:48:01 +0100
Subject: [PATCH] :bug: make sure user-defined warning states are not
 overridden

warning_config.m should only be called once in dynare.m
---
 matlab/+identification/run.m                     |  4 +++-
 matlab/+mom/run.m                                |  6 ++++--
 matlab/+occbin/kalman_update_algo_1.m            | 16 ++++++++++++----
 .../shock_decomposition/WriteShockDecomp2Excel.m |  6 ++++--
 4 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/matlab/+identification/run.m b/matlab/+identification/run.m
index 49be5e4821..38bfe21eed 100644
--- a/matlab/+identification/run.m
+++ b/matlab/+identification/run.m
@@ -74,6 +74,7 @@ fname = M_.fname; %model name
 dname = M_.dname; %model name
 
 %turn warnings off, either globally or only relevant ids
+orig_warning_state = warning;
 if isoctave
     %warning('off'),
     warning('off','Octave:singular-matrix');
@@ -498,6 +499,7 @@ if iload <=0
                 fprintf('The model did not solve for any of 50 attempts of random samples from the prior\n');
             end
             fprintf('-----------\n');
+            warning(orig_warning_state);
             return
         else
             % found a (random) point that solves the model
@@ -976,6 +978,6 @@ if SampleSize > 1
 end
 
 %reset warning state
-warning_config;
+warning(orig_warning_state);
 
 fprintf('\n==== Identification analysis completed ====\n\n')
diff --git a/matlab/+mom/run.m b/matlab/+mom/run.m
index 19b1a6206b..bbcd1cc9bf 100644
--- a/matlab/+mom/run.m
+++ b/matlab/+mom/run.m
@@ -95,7 +95,6 @@ function [oo_, options_mom_, M_] = run(bayestopt_, options_, oo_, estim_params_,
 %  o skipline
 %  o test_for_deep_parameters_calibration
 %  o transform_prior_to_laplace_prior
-%  o warning_config
 
 % Copyright © 2020-2023 Dynare Team
 %
@@ -182,6 +181,8 @@ end
 % -------------------------------------------------------------------------
 % initializations
 % -------------------------------------------------------------------------
+%save warning state for restoring later on
+orig_warning_state = warning;
 % create output directories to store results
 M_.dname = options_mom_.dirname;
 CheckPath(M_.dname,'.');
@@ -689,6 +690,7 @@ if do_bayesian_estimation_mcmc
         % skip optimizer-based mode-finding and instead compute the mode based on a run of a MCMC
         [~,~,posterior_mode,~] = compute_mh_covariance_matrix(bayestopt_,M_.fname,M_.dname,'method_of_moments');
         oo_.mom = fill_mh_mode(posterior_mode',NaN(length(posterior_mode),1),M_,options_mom_,estim_params_,bayestopt_,oo_.mom,'posterior');
+        warning(orig_warning_state);
         return
     else
         % get stored results if required
@@ -812,7 +814,7 @@ fprintf('\n==== Method of Moments Estimation (%s) Completed ====\n\n',options_mo
 % -------------------------------------------------------------------------
 % clean up
 % -------------------------------------------------------------------------
-warning_config; %reset warning state
+warning(orig_warning_state); %reset warning state
 if isoctave && isfield(options_mom_, 'prior_restrictions') && ...
    isfield(options_mom_.prior_restrictions, 'routine')
     % Octave crashes if it tries to save function handles (to the _results.mat file)
diff --git a/matlab/+occbin/kalman_update_algo_1.m b/matlab/+occbin/kalman_update_algo_1.m
index 42705e12d5..11205e63e0 100644
--- a/matlab/+occbin/kalman_update_algo_1.m
+++ b/matlab/+occbin/kalman_update_algo_1.m
@@ -62,7 +62,7 @@ function [a, a1, P, P1, v, T, R, C, regimes_, error_flag, M_, lik, etahat, alpha
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
 
-warning off
+orig_warning_state = warning;
 
 options_.noprint = true;
 R=NaN(size(RR));
@@ -148,6 +148,7 @@ else
 end
 if error_flag
     etahat=NaN(size(QQQ,1),1);
+    warning(orig_warning_state);
     return;
 end
 
@@ -183,6 +184,7 @@ if out.error_flag
     error_flag = out.error_flag;
     etahat=etahat(:,2);
     lik=inf;
+    warning(orig_warning_state);
     return;
 end
 
@@ -239,6 +241,7 @@ if any(myregime) || ~isequal(regimes_(1),regimes0(1))
             error_flag = out.error_flag;
             etahat=etahat(:,2);
             lik=inf;
+            warning(orig_warning_state);
             return;
         end
         regimes0=regimes_;
@@ -268,6 +271,7 @@ if any(myregime) || ~isequal(regimes_(1),regimes0(1))
                         error_flag = out.error_flag;
                         etahat=etahat(:,2);
                         lik=inf;
+                        warning(orig_warning_state);
                         return;
                     else
                         regimes_ = out.regime_history;
@@ -280,6 +284,7 @@ if any(myregime) || ~isequal(regimes_(1),regimes0(1))
                     error_flag = 330;
                     etahat=etahat(:,2);
                     lik=inf;
+                    warning(orig_warning_state);
                     return;
                 end
             end
@@ -306,7 +311,8 @@ if nargout>=13
     etahat=etahat(:,2);
 end
 
-warning_config;
+%reset warning state
+warning(orig_warning_state);
 end
 
 function [a, a1, P, P1, v, alphahat, etahat, lik, V, error_flag] = occbin_kalman_update0(a,a1,P,P1,data_index,Z,v,Y,H,QQQ,TT,RR,CC,iF,L,mm, rescale_prediction_error_covariance, IF_likelihood, state_uncertainty_flag)
@@ -322,7 +328,7 @@ else
     V=[];
 end
 
-warning off
+orig_warning_state = warning;
 if nargin<18
     IF_likelihood=0;
 end
@@ -352,6 +358,7 @@ else
     sig=sqrt(diag(F));
     if any(any(isnan(F)))
         error_flag=1;
+        warning(orig_warning_state);
         return;
     end
     if rank(F)<size(F,1) 
@@ -434,5 +441,6 @@ while t > 1
     end
 end
 
-warning_config;
+%reset warning state
+warning(orig_warning_state);
 end
diff --git a/matlab/shock_decomposition/WriteShockDecomp2Excel.m b/matlab/shock_decomposition/WriteShockDecomp2Excel.m
index f8571661e4..adb5847f59 100644
--- a/matlab/shock_decomposition/WriteShockDecomp2Excel.m
+++ b/matlab/shock_decomposition/WriteShockDecomp2Excel.m
@@ -116,6 +116,8 @@ for j=1:nvar
         end
     end
 
+    %save warning state for restoring later on
+    orig_warning_state = warning;
     warning off
     fig_name1 = strrep(fig_name1,' ','_');
     fig_name1 = strrep(fig_name1,'.','');
@@ -125,8 +127,8 @@ for j=1:nvar
     else
         writetable(cell2table(d0), [OutputDirectoryName,filesep,M_.fname,'_shock_decomposition',fig_mode,fig_name1 '.xls'], 'Sheet', endo_names{i_var(j)},'WriteVariableNames',false);
     end
-    warning_config;
+    warning(orig_warning_state);
 
     clear d0
 
-end
+end
\ No newline at end of file
-- 
GitLab