From 86534a5f9f172d519aa479e2635139530ca66878 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Sun, 30 Oct 2016 10:40:04 +0100 Subject: [PATCH] missing_DiffuseKalmanSmootherH1_Z.m: introduce proper case distinction in diffuse backward pass if Finf is singular --- matlab/missing_DiffuseKalmanSmootherH1_Z.m | 50 ++++++++++++++-------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/matlab/missing_DiffuseKalmanSmootherH1_Z.m b/matlab/missing_DiffuseKalmanSmootherH1_Z.m index 00b74d6ab..77b0de22f 100644 --- a/matlab/missing_DiffuseKalmanSmootherH1_Z.m +++ b/matlab/missing_DiffuseKalmanSmootherH1_Z.m @@ -80,15 +80,19 @@ aK = zeros(nk,mm,smpl+nk); PK = zeros(nk,mm,mm,smpl+nk); iF = zeros(pp,pp,smpl); Fstar = zeros(pp,pp,smpl); +iFstar = zeros(pp,pp,smpl); iFinf = zeros(pp,pp,smpl); K = zeros(mm,pp,smpl); L = zeros(mm,mm,smpl); Linf = zeros(mm,mm,smpl); +Lstar = zeros(mm,mm,smpl); Kstar = zeros(mm,pp,smpl); Kinf = zeros(mm,pp,smpl); P = zeros(mm,mm,smpl+1); -Pstar = zeros(spstar(1),spstar(2),smpl+1); Pstar(:,:,1) = Pstar1; -Pinf = zeros(spinf(1),spinf(2),smpl+1); Pinf(:,:,1) = Pinf1; +Pstar = zeros(spstar(1),spstar(2),smpl+1); +Pstar(:,:,1) = Pstar1; +Pinf = zeros(spinf(1),spinf(2),smpl+1); +Pinf(:,:,1) = Pinf1; rr = size(Q,1); QQ = R*Q*transpose(R); QRt = Q*transpose(R); @@ -96,6 +100,7 @@ alphahat = zeros(mm,smpl); etahat = zeros(rr,smpl); epsilonhat = zeros(rr,smpl); r = zeros(mm,smpl+1); +Finf_singular = zeros(1,smpl); t = 0; while rank(Pinf(:,:,t+1),diffuse_kalman_tol) && td+1 t = t-1; di = data_index{t}; if isempty(di) % in this case, L is simply T due to Z=0, so that DK (2012), eq. 4.93 obtains - r(:,t) = L(:,:,t)'*r(:,t+1); + r(:,t) = L(:,:,t)'*r(:,t+1); %compute r_{t-1}, DK (2012), eq. 4.38 with Z=0 else ZZ = Z(di,:); - r(:,t) = ZZ'*iF(di,di,t)*v(di,t) + L(:,:,t)'*r(:,t+1); %DK (2012), eq. 4.38 + r(:,t) = ZZ'*iF(di,di,t)*v(di,t) + L(:,:,t)'*r(:,t+1); %compute r_{t-1}, DK (2012), eq. 4.38 end alphahat(:,t) = a(:,t) + P(:,:,t)*r(:,t); %DK (2012), eq. 4.35 etahat(:,t) = QRt*r(:,t); %DK (2012), eq. 4.63 @@ -242,19 +251,24 @@ end if d %diffuse periods % initialize r_d^(0) and r_d^(1) as below DK (2012), eq. 5.23 r0 = zeros(mm,d+1); - r0(:,d+1) = r(:,d+1); - r1 = zeros(mm,d+1); + r0(:,d+1) = r(:,d+1); %set r0_{d}, i.e. shifted by one period + r1 = zeros(mm,d+1); %set r1_{d}, i.e. shifted by one period for t = d:-1:1 - r0(:,t) = Linf(:,:,t)'*r0(:,t+1); % DK (2012), eq. 5.21 where L^(0) is named Linf di = data_index{t}; if isempty(di) r1(:,t) = Linf(:,:,t)'*r1(:,t+1); else - r1(:,t) = Z(di,:)'*(iFinf(di,di,t)*v(di,t)-Kstar(:,di,t)'*T'*r0(:,t+1)) ... - + Linf(:,:,t)'*r1(:,t+1); % DK (2012), eq. 5.21, noting that i) F^(1)=(F^Inf)^(-1)(see 5.10), ii) where L^(0) is named Linf, and iii) Kstar=T^{-1}*K^(1) + if ~Finf_singular(1,t) + r0(:,t) = Linf(:,:,t)'*r0(:,t+1); % DK (2012), eq. 5.21 where L^(0) is named Linf + r1(:,t) = Z(di,:)'*(iFinf(di,di,t)*v(di,t)-Kstar(:,di,t)'*T'*r0(:,t+1)) ... + + Linf(:,:,t)'*r1(:,t+1); % DK (2012), eq. 5.21, noting that i) F^(1)=(F^Inf)^(-1)(see 5.10), ii) where L^(0) is named Linf, and iii) Kstar=T^{-1}*K^(1) + else + r0(:,t) = Z(di,:)'*iFstar(di,di,t)*v(di,t)-Lstar(:,di,t)'*r0(:,t+1); % DK (2003), eq. (14) + r1(:,t) = T'*r1(:,t+1); % DK (2003), eq. (14) + end end - alphahat(:,t) = a(:,t) + Pstar(:,:,t)*r0(:,t) + Pinf(:,:,t)*r1(:,t); % DK (2012), eq. 5.23 - etahat(:,t) = QRt*r0(:,t); % DK (2012), p. 135 + alphahat(:,t) = a(:,t) + Pstar(:,:,t)*r0(:,t) + Pinf(:,:,t)*r1(:,t); % DK (2012), eq. 5.23 + etahat(:,t) = QRt*r0(:,t); % DK (2012), p. 135 end end