From abbd1118c969622346863d3d84403f56cdd10ba4 Mon Sep 17 00:00:00 2001
From: Marco Ratto <marco.ratto@ec.europa.eu>
Date: Mon, 26 Aug 2024 18:14:55 +0200
Subject: [PATCH] the error in reconstructing the k-step ahead forecasts with
 occbin smoother redux, should only occur in the first occbin smoother
 interation for non-stationary models (with diffuse filter), where tstart>1.

if k>tstart, the same simulation should have been done already in occbin.kalman_update, so it should never give an error

if k<=tstart, the simulation may crash, since we ignore OBC in the first (diffuse) steps and it may happen that, given the linear updated states, the occbin simulation does not converge.

Indeed, I never got a simulation failure in line 474, where this possibility is properly taken into account.
---
 matlab/kalman/DsgeSmoother.m | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/matlab/kalman/DsgeSmoother.m b/matlab/kalman/DsgeSmoother.m
index 7ed75d135a..c84b800286 100644
--- a/matlab/kalman/DsgeSmoother.m
+++ b/matlab/kalman/DsgeSmoother.m
@@ -506,6 +506,13 @@ else
         aaa = zeros(nk,M_.endo_nbr,gend+nk);
         aaa(:,oo_.dr.restrict_var_list,:)=aK;
         
+        if isoccbin
+            tstart = 1;
+        else
+            % we enter here in the first occbin smoother iteration
+            % occbin kalman update is not yet able to accommodate diffuse steps!
+            tstart=d+2; 
+        end
         for k=2:gend+1
             opts_simul.curb_retrench = options_.occbin.smoother.curb_retrench;
             opts_simul.waitbar = options_.occbin.smoother.waitbar;
@@ -530,6 +537,20 @@ else
                 for jnk=1:nk
                     aaa(jnk,oo_.dr.inv_order_var,k+jnk-1) = out.piecewise(jnk,:) - out.ys';
                 end
+            elseif k>tstart
+                % the issue only matters non-stationary models, with
+                % diffuse filter, and for the first occbin smoother iteration,
+                % where tstart>1
+                %
+                % if k>tstart, the same simulation should have been done
+                % already in occbin.kalman_update, so it should never give
+                % an error
+                %
+                % if k<=tstart, the simulation may crash, since we ignore OBC in the first (diffuse) steps
+                % and it may happen that, given the linear updated states,
+                % the occbin simulation does not converge
+                error('this error should not occur, please contact the developers!')
+
             end
         end
         aK=aaa;
-- 
GitLab