From c62d9db0b9e40501715bbd11bf7aead169ee67a0 Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Wed, 27 Nov 2024 10:33:37 +0100
Subject: [PATCH] :bug: OccBin: clear State_uncertainty field before trying to
 fill it

Prevents crashes with smoothed_state_uncertainty option if MCMC smoother is run after classical one
---
 matlab/estimation/prior_posterior_statistics.m |  7 +++++--
 tests/occbin/filter/NKM.mod                    | 13 +++++++++++--
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/matlab/estimation/prior_posterior_statistics.m b/matlab/estimation/prior_posterior_statistics.m
index 36349dc6b8..a0fb6128cc 100644
--- a/matlab/estimation/prior_posterior_statistics.m
+++ b/matlab/estimation/prior_posterior_statistics.m
@@ -141,9 +141,9 @@ if options_.filter_covariance
     filter_covariance=1;
 end
 
-smoothed_state_uncertainty=0;
+smoothed_state_uncertainty=false;
 if options_.smoothed_state_uncertainty
-    smoothed_state_uncertainty=1;
+    smoothed_state_uncertainty=true;
 end
 
 % Store the variable mandatory for local/remote parallel computing.
@@ -327,6 +327,9 @@ if options_.smoother
         varlist,'UpdatedVariables',DirectoryName, ...
         '_update',dispString);
     if smoothed_state_uncertainty
+        if isfield(oo_,'Smoother') && isfield(oo_.Smoother,'State_uncertainty')
+            oo_.Smoother=rmfield(oo_.Smoother,'State_uncertainty'); %needs to be removed as classical smoother field has a different format
+        end
         oo_=pm3(M_,options_,oo_,endo_nbr,endo_nbr,ifil(13),B,'State Uncertainty',...
             varlist,M_.endo_names_tex,M_.endo_names,...
             varlist,'StateUncertainty',DirectoryName,'_state_uncert',dispString);
diff --git a/tests/occbin/filter/NKM.mod b/tests/occbin/filter/NKM.mod
index 07166af5db..96f0250fe4 100644
--- a/tests/occbin/filter/NKM.mod
+++ b/tests/occbin/filter/NKM.mod
@@ -389,13 +389,22 @@ varobs yg inom pi;
     else
     disp('smoother redux successfully recovers full k-step ahead variables results!')
     end
+
+    //run PKF with MCMC
+    options_.smoother_redux=false;
+    estimation(
+        datafile=dataobsfile2, mode_file=NKM_mh_mode_saved,
+        mode_compute=0, nobs=120, first_obs=1,
+        mh_replic=50, plot_priors=0, smoother,
+        consider_all_endogenous,heteroskedastic_filter,filter_step_ahead=[1:8],smoothed_state_uncertainty);
+
     // use inversion filter (note that IF provides smoother together with likelihood)
     occbin_setup(likelihood_inversion_filter,smoother_inversion_filter);
             
     estimation(
             datafile=dataobsfile2, mode_file=NKM_mh_mode_saved,
             mode_compute=0, nobs=120, first_obs=1,
-            mh_replic=0, plot_priors=0, smoother,
+            mh_replic=50, plot_priors=0, smoother,
             consider_all_endogenous,heteroskedastic_filter,filter_step_ahead=[1:8],smoothed_state_uncertainty);
             
     // show initial condition effect of IF
@@ -420,7 +429,7 @@ varobs yg inom pi;
     estimation(
             datafile=dataobsfile2, mode_file=NKM_mh_mode_saved,
             mode_compute=0, nobs=120, first_obs=1,
-            mh_replic=10, plot_priors=0, smoother,
+            mh_replic=50, plot_priors=0, smoother,
             consider_all_endogenous,heteroskedastic_filter,filter_step_ahead=[1:8],smoothed_state_uncertainty);
 
 write_latex_dynamic_model;
-- 
GitLab