2008-07-21 15:51:43 +02:00
|
|
|
function [alphahat,etahat,a,P,aK,PK,d,decomp] = DiffuseKalmanSmoother1_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
|
2008-01-27 10:34:20 +01:00
|
|
|
|
|
|
|
% function [alphahat,etahat,a, aK] = DiffuseKalmanSmoother1(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
|
|
|
|
% Computes the diffuse kalman smoother without measurement error, in the case of a non-singular var-cov matrix
|
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% T: mm*mm matrix
|
|
|
|
% Z: pp*mm matrix
|
|
|
|
% R: mm*rr matrix
|
|
|
|
% Q: rr*rr matrix
|
|
|
|
% Pinf1: mm*mm diagonal matrix with with q ones and m-q zeros
|
|
|
|
% Pstar1: mm*mm variance-covariance matrix with stationary variables
|
|
|
|
% Y: pp*1 vector
|
|
|
|
% pp: number of observed variables
|
|
|
|
% mm: number of state variables
|
|
|
|
% smpl: sample size
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% alphahat: smoothed state variables
|
|
|
|
% etahat: smoothed shocks
|
|
|
|
% a: matrix of one step ahead filtered state variables
|
|
|
|
% aK: 3D array of k step ahead filtered state variables
|
2008-02-04 18:52:16 +01:00
|
|
|
% (meaningless for periods 1:d)
|
|
|
|
% P: 3D array of one-step ahead forecast error variance
|
|
|
|
% matrices
|
|
|
|
% PK: 4D array of k-step ahead forecast error variance
|
|
|
|
% matrices (meaningless for periods 1:d)
|
|
|
|
% d: number of periods where filter remains in diffuse part
|
|
|
|
% (should be equal to the order of integration of the model)
|
2008-05-01 17:41:15 +02:00
|
|
|
% decomp: decomposition of the effect of shocks on filtered values
|
2008-02-04 18:52:16 +01:00
|
|
|
%
|
2008-01-27 10:34:20 +01:00
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% 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).
|
|
|
|
%
|
|
|
|
% part of DYNARE, copyright Dynare Team (2004-2008)
|
|
|
|
% Gnu Public License.
|
|
|
|
|
|
|
|
|
|
|
|
% modified by M. Ratto:
|
|
|
|
% new output argument aK (1-step to k-step predictions)
|
|
|
|
% new options_.nk: the max step ahed prediction in aK (default is 4)
|
|
|
|
% new crit1 value for rank of Pinf
|
|
|
|
% it is assured that P is symmetric
|
|
|
|
|
|
|
|
|
|
|
|
global options_
|
|
|
|
|
2008-04-16 16:27:50 +02:00
|
|
|
d = 0;
|
2008-05-01 17:41:15 +02:00
|
|
|
decomp = [];
|
2008-01-27 10:34:20 +01:00
|
|
|
nk = options_.nk;
|
|
|
|
spinf = size(Pinf1);
|
|
|
|
spstar = size(Pstar1);
|
|
|
|
v = zeros(pp,smpl);
|
|
|
|
a = zeros(mm,smpl+1);
|
2008-02-04 18:52:16 +01:00
|
|
|
aK = zeros(nk,mm,smpl+nk);
|
|
|
|
PK = zeros(nk,mm,mm,smpl+nk);
|
2008-01-27 10:34:20 +01:00
|
|
|
iF = zeros(pp,pp,smpl);
|
|
|
|
Fstar = zeros(pp,pp,smpl);
|
|
|
|
iFinf = zeros(pp,pp,smpl);
|
|
|
|
K = zeros(mm,pp,smpl);
|
|
|
|
L = zeros(mm,mm,smpl);
|
|
|
|
Linf = zeros(mm,mm,smpl);
|
|
|
|
Kstar = 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;
|
|
|
|
crit = options_.kalman_tol;
|
|
|
|
crit1 = 1.e-8;
|
|
|
|
steady = smpl;
|
|
|
|
rr = size(Q,1);
|
|
|
|
QQ = R*Q*transpose(R);
|
|
|
|
QRt = Q*transpose(R);
|
|
|
|
alphahat = zeros(mm,smpl);
|
|
|
|
etahat = zeros(rr,smpl);
|
|
|
|
r = zeros(mm,smpl);
|
|
|
|
|
|
|
|
t = 0;
|
|
|
|
while rank(Pinf(:,:,t+1),crit1) & t<smpl
|
|
|
|
t = t+1;
|
|
|
|
v(:,t)= Y(:,t) - Z*a(:,t);
|
|
|
|
F = Z*Pinf(:,:,t)*Z';
|
|
|
|
if rcond(F) < crit
|
|
|
|
return
|
|
|
|
end
|
|
|
|
iFinf(:,:,t) = inv(F);
|
|
|
|
Kinf(:,:,t) = T*Pinf(:,:,t)*Z'*iFinf(:,:,t);
|
|
|
|
a(:,t+1) = T*a(:,t) + Kinf(:,:,t)*v(:,t);
|
|
|
|
aK(1,:,t+1) = a(:,t+1);
|
2008-02-04 18:52:16 +01:00
|
|
|
% isn't a meaningless as long as we are in the diffuse part? MJ
|
2008-01-27 10:34:20 +01:00
|
|
|
for jnk=2:nk,
|
|
|
|
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
|
|
|
|
end
|
|
|
|
Linf(:,:,t) = T - Kinf(:,:,t)*Z;
|
|
|
|
Fstar(:,:,t) = Z*Pstar(:,:,t)*Z';
|
|
|
|
Kstar(:,:,t) = (T*Pstar(:,:,t)*Z'-Kinf(:,:,t)*Fstar(:,:,t))*iFinf(:,:,t);
|
2008-01-31 11:58:58 +01:00
|
|
|
Pstar(:,:,t+1) = T*Pstar(:,:,t)*T'-T*Pstar(:,:,t)*Z'*Kinf(:,:,t)'-Kinf(:,:,t)*F*Kstar(:,:,t)' + QQ;
|
2008-01-27 10:34:20 +01:00
|
|
|
Pinf(:,:,t+1) = T*Pinf(:,:,t)*T'-T*Pinf(:,:,t)*Z'*Kinf(:,:,t)';
|
|
|
|
end
|
|
|
|
d = t;
|
|
|
|
P(:,:,d+1) = Pstar(:,:,d+1);
|
|
|
|
iFinf = iFinf(:,:,1:d);
|
|
|
|
Linf = Linf(:,:,1:d);
|
|
|
|
Fstar = Fstar(:,:,1:d);
|
|
|
|
Kstar = Kstar(:,:,1:d);
|
|
|
|
Pstar = Pstar(:,:,1:d);
|
|
|
|
Pinf = Pinf(:,:,1:d);
|
|
|
|
notsteady = 1;
|
|
|
|
while notsteady & t<smpl
|
|
|
|
t = t+1;
|
|
|
|
v(:,t) = Y(:,t) - Z*a(:,t);
|
|
|
|
P(:,:,t)=tril(P(:,:,t))+transpose(tril(P(:,:,t),-1));
|
2008-01-31 11:58:58 +01:00
|
|
|
F = Z*P(:,:,t)*Z';
|
2008-01-27 10:34:20 +01:00
|
|
|
if rcond(F) < crit
|
|
|
|
return
|
|
|
|
end
|
|
|
|
iF(:,:,t) = inv(F);
|
|
|
|
K(:,:,t) = T*P(:,:,t)*Z'*iF(:,:,t);
|
|
|
|
L(:,:,t) = T-K(:,:,t)*Z;
|
2008-02-04 18:52:16 +01:00
|
|
|
a(:,t+1) = T*a(:,t) + K(:,:,t)*v(:,t);
|
|
|
|
af = a(:,t);
|
|
|
|
Pf = P(:,:,t);
|
|
|
|
for jnk=1:nk,
|
|
|
|
af = T*af;
|
|
|
|
Pf = T*Pf*T' + QQ;
|
|
|
|
aK(jnk,:,t+jnk) = af;
|
|
|
|
PK(jnk,:,:,t+jnk) = Pf;
|
2008-01-27 10:34:20 +01:00
|
|
|
end
|
2008-02-04 18:52:16 +01:00
|
|
|
P(:,:,t+1) = T*P(:,:,t)*T'-T*P(:,:,t)*Z'*K(:,:,t)' + QQ;
|
2008-01-27 10:34:20 +01:00
|
|
|
notsteady = ~(max(max(abs(P(:,:,t+1)-P(:,:,t))))<crit);
|
|
|
|
end
|
|
|
|
K_s = K(:,:,t);
|
|
|
|
iF_s = iF(:,:,t);
|
|
|
|
P_s = P(:,:,t+1);
|
|
|
|
if t<smpl
|
|
|
|
P = cat(3,P(:,:,1:t),repmat(P_s,[1 1 smpl-t]));
|
|
|
|
iF = cat(3,iF(:,:,1:t),repmat(iF_s,[1 1 smpl-t]));
|
|
|
|
L = cat(3,L(:,:,1:t),repmat(T-K_s*Z,[1 1 smpl-t]));
|
|
|
|
K = cat(3,K(:,:,1:t),repmat(T*P_s*Z'*iF_s,[1 1 smpl-t]));
|
|
|
|
end
|
|
|
|
while t<smpl
|
|
|
|
t=t+1;
|
|
|
|
v(:,t) = Y(:,t) - Z*a(:,t);
|
|
|
|
a(:,t+1) = T*a(:,t) + K_s*v(:,t);
|
2008-02-04 18:52:16 +01:00
|
|
|
af = a(:,t);
|
|
|
|
Pf = P(:,:,t);
|
|
|
|
for jnk=1:nk,
|
|
|
|
af = T*af;
|
|
|
|
Pf = T*Pf*T' + QQ;
|
|
|
|
aK(jnk,:,t+jnk) = af;
|
|
|
|
PK(jnk,:,:,t+jnk) = Pf;
|
2008-01-27 10:34:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
t = smpl+1;
|
|
|
|
while t>d+1 & t>2
|
|
|
|
t = t-1;
|
|
|
|
r(:,t-1) = Z'*iF(:,:,t)*v(:,t) + L(:,:,t)'*r(:,t);
|
|
|
|
alphahat(:,t) = a(:,t) + P(:,:,t)*r(:,t-1);
|
|
|
|
etahat(:,t) = QRt*r(:,t);
|
|
|
|
end
|
|
|
|
if d
|
|
|
|
r0 = zeros(mm,d);
|
|
|
|
r0(:,d) = r(:,d);
|
|
|
|
r1 = zeros(mm,d);
|
|
|
|
for t = d:-1:2
|
|
|
|
r0(:,t-1) = Linf(:,:,t)'*r0(:,t);
|
|
|
|
r1(:,t-1) = Z'*(iFinf(:,:,t)*v(:,t)-Kstar(:,:,t)'*r0(:,t)) + Linf(:,:,t)'*r1(:,t);
|
|
|
|
alphahat(:,t) = a(:,t) + Pstar(:,:,t)*r0(:,t-1) + Pinf(:,:,t)*r1(:,t-1);
|
|
|
|
etahat(:,t) = QRt*r0(:,t);
|
|
|
|
end
|
|
|
|
r0_0 = Linf(:,:,1)'*r0(:,1);
|
2008-01-31 11:58:58 +01:00
|
|
|
r1_0 = Z'*(iFinf(:,:,1)*v(:,1)-Kstar(:,:,1)'*r0(:,1)) + Linf(:,:,1)'*r1(:,1);
|
2008-01-27 10:34:20 +01:00
|
|
|
alphahat(:,1) = a(:,1) + Pstar(:,:,1)*r0_0 + Pinf(:,:,1)*r1_0;
|
|
|
|
etahat(:,1) = QRt*r0(:,1);
|
|
|
|
else
|
|
|
|
r0 = Z'*iF(:,:,1)*v(:,1) + L(:,:,1)'*r(:,1);
|
|
|
|
alphahat(:,1) = a(:,1) + P(:,:,1)*r0;
|
|
|
|
etahat(:,1) = QRt*r(:,1);
|
|
|
|
end
|
2008-05-01 17:41:15 +02:00
|
|
|
|
|
|
|
if nargout > 7
|
|
|
|
decomp = zeros(nk,mm,rr,smpl+nk);
|
|
|
|
ZRQinv = inv(Z*QQ*Z');
|
|
|
|
for t = d:smpl
|
2008-06-21 18:17:57 +02:00
|
|
|
ri_d = Z'*iF(:,:,t)*v(:,t);
|
2008-05-01 17:41:15 +02:00
|
|
|
|
|
|
|
% calculate eta_tm1t
|
|
|
|
eta_tm1t = QRt*ri_d;
|
|
|
|
% calculate decomposition
|
|
|
|
Ttok = eye(mm,mm);
|
|
|
|
for h = 1:nk
|
|
|
|
for j=1:rr
|
|
|
|
eta=zeros(rr,1);
|
|
|
|
eta(j) = eta_tm1t(j);
|
2008-06-21 18:17:57 +02:00
|
|
|
decomp(h,:,j,t+h) = T^(h-1)*P(:,:,t)*Z'*ZRQinv*Z*R*eta;
|
2008-05-01 17:41:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|