diff --git a/matlab/missing_DiffuseKalmanSmootherH1_Z.m b/matlab/missing_DiffuseKalmanSmootherH1_Z.m index 80b5c6efa..d5f5cde9f 100644 --- a/matlab/missing_DiffuseKalmanSmootherH1_Z.m +++ b/matlab/missing_DiffuseKalmanSmootherH1_Z.m @@ -43,6 +43,8 @@ function [alphahat,epsilonhat,etahat,atilde,P,aK,PK,decomp] = missing_DiffuseKal % See "Filtering and Smoothing of State Vector for Diffuse State Space % Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series % Analysis, vol. 24(1), pp. 85-98). +% Durbin/Koopman (2012): "Time Series Analysis by State Space Methods", Oxford University Press, +% Second Edition, Ch. 5 % Copyright (C) 2004-2016 Dynare Team % @@ -83,16 +85,16 @@ K = zeros(mm,pp,smpl); L = zeros(mm,mm,smpl); Linf = 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; -steady = smpl; rr = size(Q,1); QQ = R*Q*transpose(R); QRt = Q*transpose(R); alphahat = zeros(mm,smpl); etahat = zeros(rr,smpl); -epsilonhat = zeros(rr,smpl); +epsilonhat = zeros(rr,smpl); r = zeros(mm,smpl+1); t = 0; @@ -100,54 +102,57 @@ 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); else ZZ = Z(di,:); - r(:,t) = ZZ'*iF(di,di,t)*v(di,t) + L(:,:,t)'*r(:,t+1); + r(:,t) = ZZ'*iF(di,di,t)*v(di,t) + L(:,:,t)'*r(:,t+1); %DK (2012), eq. 4.38 end - alphahat(:,t) = a(:,t) + P(:,:,t)*r(:,t); - etahat(:,t) = QRt*r(:,t); + alphahat(:,t) = a(:,t) + P(:,:,t)*r(:,t); %DK (2012), eq. 4.35 + etahat(:,t) = QRt*r(:,t); %DK (2012), eq. 4.63 end -if d +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); for t = d:-1:1 - r0(:,t) = Linf(:,:,t)'*r0(:,t+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)'*r0(:,t+1)) ... - + Linf(:,:,t)'*r1(:,t+1); + 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) end - alphahat(:,t) = a(:,t) + Pstar(:,:,t)*r0(:,t) + Pinf(:,:,t)*r1(:,t); - etahat(:,t) = QRt*r0(:,t); + 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 @@ -260,7 +268,6 @@ if decomp_flag eta_tm1t = QRt*Z(di,:)'*iF(di,di,t)*v(di,t); AAA = P(:,:,t)*Z(di,:)'*ZRQinv(di,di)*bsxfun(@times,Z(di,:)*R,eta_tm1t'); % calculate decomposition - Ttok = eye(mm,mm); decomp(1,:,:,t+1) = AAA; for h = 2:nk AAA = T*AAA;