From 1755192e639cae39ecfa35645dbe5c02024c219c Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Fri, 31 Mar 2017 16:08:16 +0200
Subject: [PATCH] Make  792924 consistent with cases where Pinf is an empty
 matrix

Fixes the crash in `analytic_derivatives/fs2000_analytic_derivation.mod`
---
 matlab/DsgeSmoother.m                      |  4 +++-
 matlab/missing_DiffuseKalmanSmootherH3_Z.m | 18 +++++++++++++++---
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/matlab/DsgeSmoother.m b/matlab/DsgeSmoother.m
index f9e459e004..08b7011e93 100644
--- a/matlab/DsgeSmoother.m
+++ b/matlab/DsgeSmoother.m
@@ -255,7 +255,9 @@ if kalman_algo == 2 || kalman_algo == 4
                     [Pstar,Pinf] = compute_Pinf_Pstar(mf,ST,R1,Q,options_.qz_criterium);
                 else
                     Pstar = blkdiag(Pstar,H);
-                    Pinf  = blkdiag(Pinf,zeros(vobs));                    
+                    if ~isempty(Pinf)
+                        Pinf  = blkdiag(Pinf,zeros(vobs));                    
+                    end
                 end
                 %now reset H to 0
                 H   = zeros(vobs,vobs);
diff --git a/matlab/missing_DiffuseKalmanSmootherH3_Z.m b/matlab/missing_DiffuseKalmanSmootherH3_Z.m
index 24e6841cf9..b03b13ee0e 100644
--- a/matlab/missing_DiffuseKalmanSmootherH3_Z.m
+++ b/matlab/missing_DiffuseKalmanSmootherH3_Z.m
@@ -122,7 +122,11 @@ end
 
 t = 0;
 icc=0;
-newRank = rank(Z*Pinf(:,:,1)*Z',diffuse_kalman_tol);
+if ~isempty(Pinf(:,:,1))
+    newRank = rank(Z*Pinf(:,:,1)*Z',diffuse_kalman_tol);
+else
+    newRank = rank(Pinf(:,:,1),diffuse_kalman_tol);
+end
 while newRank && t < smpl
     t = t+1;
     a(:,t) = a1(:,t);
@@ -156,7 +160,11 @@ while newRank && t < smpl
         end
     end
     if newRank
-        oldRank = rank(Z*Pinf(:,:,t)*Z',diffuse_kalman_tol);
+        if ~isempty(Pinf(:,:,t))
+            oldRank = rank(Z*Pinf(:,:,t)*Z',diffuse_kalman_tol);
+        else
+            oldRank = rank(Pinf(:,:,t),diffuse_kalman_tol);
+        end        
     else
         oldRank = 0;
     end
@@ -168,7 +176,11 @@ while newRank && t < smpl
     Pstar(:,:,t+1) = T*Pstar(:,:,t)*T'+ QQ;
     Pinf(:,:,t+1) = T*Pinf(:,:,t)*T';
     if newRank
-        newRank       = rank(Z*Pinf(:,:,t+1)*Z',diffuse_kalman_tol);
+        if ~isempty(Pinf(:,:,t+1))
+            newRank = rank(Z*Pinf(:,:,t+1)*Z',diffuse_kalman_tol);
+        else
+            newRank = rank(Pinf(:,:,t+1),diffuse_kalman_tol);
+        end
     end
     if oldRank ~= newRank
         disp('univariate_diffuse_kalman_filter:: T does influence the rank of Pinf!')   
-- 
GitLab