Commit 12436db7 authored by michel's avatar michel
Browse files

v4 added new triangular algorithm for diffuse kalman filter (options_.kalman_algo=4,5)

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1648 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 1a8db86c
function [alphahat,etahat,a, aK] = DiffuseKalmanSmoother1_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% 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
% 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_
nk = options_.nk;
spinf = size(Pinf1);
spstar = size(Pstar1);
v = zeros(pp,smpl);
a = zeros(mm,smpl+1);
aK = zeros(nk,mm,smpl+1);
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);
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);
Pstar(:,:,t+1) = T*Pstar(:,:,t)*T'-T*Pstar(:,:,t)*Z'*Kinf(:,:,t)'-Kinf(:,:,t)*F*Kstar(:,:,t) + QQ;
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));
F = Z*P(:,:,t)*Z'
if rcond(F) < crit
return
end
iF(:,:,t) = inv(F);
K(:,:,t) = T*P(:,:,t)*Z'*iF(:,:,t);
L(:,:,t) = T-K(:,:,t)*Z;
a(:,t+1) = T*a(:,t) + K(:,:,t)*v(:,t);
aK(1,:,t+1) = a(:,t+1);
for jnk=2:nk,
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
end
P(:,:,t+1) = T*P(:,:,t)*transpose(T)-T*P(:,:,t)*Z'*K(:,:,t)' + QQ;
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);
aK(1,:,t+1) = a(:,t+1);
for jnk=2:nk,
aK(jnk,:,t+jnk) = T^(jnk-1)*a(:,t+1);
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);
r1_0 = Z'*(iFinf(:,:,1)*v(:,1)-Kstar(:,:,1)*r0(:,1)) + Linf(:,:,1)'*r1(:,1);
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
function [alphahat,etahat,a1, aK] = DiffuseKalmanSmoother3_Z(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% function [alphahat,etahat,a1, aK] = DiffuseKalmanSmoother3(T,Z,R,Q,Pinf1,Pstar1,Y,pp,mm,smpl)
% Computes the diffuse kalman smoother without measurement error, in the case of a singular var-cov matrix.
% Univariate treatment of multivariate time series.
%
% 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
% a1: matrix of one step ahead filtered state variables
% aK: 3D array of k step ahead filtered state variables
% 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 nk-stpe ahed predictions)
% New input argument nk: max order of predictions in aK
% New option options_.diffuse_d where the DKF stops (common with
% diffuselikelihood3)
% New icc variable to count number of iterations for Finf steps
% Pstar % Pinf simmetric
% New termination of DKF iterations based on options_.diffuse_d
% Li also stored during DKF iterations !!
% some bugs corrected in the DKF part of the smoother (Z matrix and
% alphahat)
global options_
nk = options_.nk;
spinf = size(Pinf1);
spstar = size(Pstar1);
v = zeros(pp,smpl);
a = zeros(mm,smpl+1);
a1 = a;
aK = zeros(nk,mm,smpl+nk);
if isempty(options_.diffuse_d),
smpl_diff = 1;
else
smpl_diff=rank(Pinf1);
end
Fstar = zeros(pp,smpl_diff);
Finf = zeros(pp,smpl_diff);
Ki = zeros(mm,pp,smpl);
Li = zeros(mm,mm,pp,smpl);
Linf = zeros(mm,mm,pp,smpl_diff);
L0 = zeros(mm,mm,pp,smpl_diff);
Kstar = zeros(mm,pp,smpl_diff);
P = zeros(mm,mm,smpl+1);
P1 = P;
Pstar = zeros(spstar(1),spstar(2),smpl_diff+1); Pstar(:,:,1) = Pstar1;
Pinf = zeros(spinf(1),spinf(2),smpl_diff+1); Pinf(:,:,1) = Pinf1;
Pstar1 = Pstar;
Pinf1 = Pinf;
crit = options_.kalman_tol;
crit1 = 1.e-6;
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;
icc=0;
newRank = rank(Pinf(:,:,1),crit1);
while newRank & t < smpl
t = t+1;
a1(:,t) = a(:,t);
Pstar(:,:,t)=tril(Pstar(:,:,t))+tril(Pstar(:,:,t),-1)';
Pinf(:,:,t)=tril(Pinf(:,:,t))+tril(Pinf(:,:,t),-1)';
Pstar1(:,:,t) = Pstar(:,:,t);
Pinf1(:,:,t) = Pinf(:,:,t);
for i=1:pp
Zi = Z(i,:);
v(i,t) = Y(i,t)-Zi*a(:,t);
Fstar(i,t) = Zi*Pstar(:,:,t)*Zi';
Finf(i,t) = Zi*Pinf(:,:,t)*Zi';
Kstar(:,i,t) = Pstar(:,:,t)*Zi;
if Finf(i,t) > crit & newRank
icc=icc+1;
Kinf(:,i,t) = Pinf(:,:,t)*Zi';
Linf(:,:,i,t) = eye(mm) - Kinf(:,i,t)*Z(i,:)/Finf(i,t);
L0(:,:,i,t) = (Kinf(:,i,t)*Fstar(i,t)/Finf(i,t) - Kstar(:,i,t))*Zi/Finf(i,t);
a(:,t) = a(:,t) + Kinf(:,i,t)*v(i,t)/Finf(i,t);
Pstar(:,:,t) = Pstar(:,:,t) + ...
Kinf(:,i,t)*Kinf(:,i,t)'*Fstar(i,t)/(Finf(i,t)*Finf(i,t)) - ...
(Kstar(:,i,t)*Kinf(:,i,t)' +...
Kinf(:,i,t)*Kstar(:,i,t)')/Finf(i,t);
Pinf(:,:,t) = Pinf(:,:,t) - Kinf(:,i,t)*Kinf(:,i,t)'/Finf(i,t);
Pstar(:,:,t)=tril(Pstar(:,:,t))+tril(Pstar(:,:,t),-1)';
Pinf(:,:,t)=tril(Pinf(:,:,t))+tril(Pinf(:,:,t),-1)';
% new terminiation criteria by M. Ratto
P0=Pinf(:,:,t);
if ~isempty(options_.diffuse_d),
newRank = (icc<options_.diffuse_d);
if newRank & (any(diag(Z*P0*Z')>crit)==0 & rank(P0,crit1)==0);
disp('WARNING!! Change in OPTIONS_.DIFFUSE_D in univariate DKF')
options_.diffuse_d = icc;
newRank=0;
end
else
newRank = (any(diag(Z*P0*Z')>crit) | rank(P0,crit1));
if newRank==0,
options_.diffuse_d = icc;
end
end,
% end new terminiation criteria by M. Ratto
elseif Fstar(i,t) > crit
%% Note that : (1) rank(Pinf)=0 implies that Finf = 0, (2) outside this loop (when for some i and t the condition
%% rank(Pinf)=0 is satisfied we have P = Pstar and F = Fstar and (3) Finf = 0 does not imply that
%% rank(Pinf)=0. [stphane,11-03-2004].
Li(:,:,i,t) = eye(mm)-Kstar(:,i,t)*Z(i,:)/Fstar(i,t); % we need to store Li for DKF smoother
a(:,t) = a(:,t) + Kstar(:,i,t)*v(i,t)/Fstar(i,t);
Pstar(:,:,t) = Pstar(:,:,t) - Kstar(:,i,t)*Kstar(:,i,t)'/Fstar(i,t);
Pstar(:,:,t)=tril(Pstar(:,:,t))+tril(Pstar(:,:,t),-1)';
end
end
a(:,t+1) = T*a(:,t);
for jnk=1:nk,
aK(jnk,:,t+jnk) = T^jnk*a(:,t);
end
Pstar(:,:,t+1) = T*Pstar(:,:,t)*T'+ QQ;
Pinf(:,:,t+1) = T*Pinf(:,:,t)*T';
P0=Pinf(:,:,t+1);
if newRank,
newRank = rank(P0,crit1);
end
end
d = t;
P(:,:,d+1) = Pstar(:,:,d+1);
Linf = Linf(:,:,:,1:d);
L0 = L0(:,:,:,1:d);
Fstar = Fstar(:,1:d);
Finf = Finf(:,1:d);
Kstar = Kstar(:,:,1:d);
Pstar = Pstar(:,:,1:d);
Pinf = Pinf(:,:,1:d);
Pstar1 = Pstar1(:,:,1:d);
Pinf1 = Pinf1(:,:,1:d);
notsteady = 1;
while notsteady & t<smpl
t = t+1;
a1(:,t) = a(:,t);
P(:,:,t)=tril(P(:,:,t))+tril(P(:,:,t),-1)';
P1(:,:,t) = P(:,:,t);
for i=1:pp
Zi = Z(i,:)'
v(i,t) = Y(i,t) - Zi*a(:,t);
Fi(i,t) = Zi*P(:,:,t)*Zi';
Ki(:,i,t) = P(:,:,t)*Zi';
if Fi(i,t) > crit
Li(:,:,i,t) = eye(mm)-Ki(:,i,t)*Z(i,:)/Fi(i,t);
a(:,t) = a(:,t) + Ki(:,i,t)*v(i,t)/Fi(i,t);
P(:,:,t) = P(:,:,t) - Ki(:,i,t)*Ki(:,i,t)'/Fi(i,t);
P(:,:,t)=tril(P(:,:,t))+tril(P(:,:,t),-1)';
end
end
a(:,t+1) = T*a(:,t);
for jnk=1:nk,
aK(jnk,:,t+jnk) = T^jnk*a(:,t);
end
P(:,:,t+1) = T*P(:,:,t)*T' + QQ;
notsteady = ~(max(max(abs(P(:,:,t+1)-P(:,:,t))))<crit);
end
P_s=tril(P(:,:,t))+tril(P(:,:,t),-1)';
Fi_s = Fi(:,t);
Ki_s = Ki(:,:,t);
L_s =Li(:,:,:,t);
if t<smpl
P = cat(3,P(:,:,1:t),repmat(P_s,[1 1 smpl-t]));
Fi = cat(2,Fi(:,1:t),repmat(Fi_s,[1 1 smpl-t]));
Li = cat(4,Li(:,:,:,1:t),repmat(L_s,[1 1 smpl-t]));
Ki = cat(3,Ki(:,:,1:t),repmat(Ki_s,[1 1 smpl-t]));
end
while t<smpl
t=t+1;
a1(:,t) = a(:,t);
for i=1:pp
Zi = Z(i,:)';
v(i,t) = Y(i,t) - Zi*a(:,t);
if Fi_s(i) > crit
a(:,t) = a(:,t) + Ki_s(:,i)*v(i,t)/Fi_s(i);
end
end
a(:,t+1) = T*a(:,t);
for jnk=1:nk,
aK(jnk,:,t+jnk) = T^jnk*a(:,t);
end
end
a1(:,t+1) = a(:,t+1);
ri=r;
t = smpl+1;
while t>d+1 & t>2,
t = t-1;
for i=pp:-1:1
if Fi(i,t) > crit
ri(:,t) = Z(i,:)'/Fi(i,t)*v(i,t)+Li(:,:,i,t)'*ri(:,t);
end
end
r(:,t-1) = ri(:,t);
alphahat(:,t) = a1(:,t) + P1(:,:,t)*r(:,t-1);
etahat(:,t) = QRt*r(:,t);
ri(:,t-1) = T'*ri(:,t);
end
if d
r0 = zeros(mm,d); r0(:,d) = ri(:,d);
r1 = zeros(mm,d);
for t = d:-1:2
for i=pp:-1:1
if Finf(i,t) > crit & ~(t==d & i>options_.diffuse_d), % use of options_.diffuse_d to be sure of DKF termination
r1(:,t) = Z(i,:)'*v(i,t)/Finf(i,t) + ...
L0(:,:,i,t)'*r0(:,t) + Linf(:,:,i,t)'*r1(:,t);
r0(:,t) = Linf(:,:,i,t)'*r0(:,t);
elseif Fstar(i,t) > crit % step needed whe Finf == 0
r0(:,t) = Z(i,:)'/Fstar(i,t)*v(i,t)+Li(:,:,i,t)'*r0(:,t);
end
end
alphahat(:,t) = a1(:,t) + Pstar1(:,:,t)*r0(:,t) + Pinf1(:,:,t)*r1(:,t);
r(:,t-1) = r0(:,t);
etahat(:,t) = QRt*r(:,t);
r0(:,t-1) = T'*r0(:,t);
r1(:,t-1) = T'*r1(:,t);
end
r0_0 = r0(:,1);
r1_0 = r1(:,1);
for i=pp:-1:1
if Finf(i,1) > crit,
r1_0 = Z(i,:)'*v(i,1)/Finf(i,1) + ...
L0(:,:,i,1)'*r0_0 + Linf(:,:,i,1)'*r1_0;
r0_0 = Linf(:,:,i,1)'*r0_0;
elseif Fstar(i,1) > crit, % step needed when Finf=0
r0_0=transpose(Z(i,:))/Fstar(i,1)*v(i,1)+Li(:,:,i,1)'*r0_0;
end
end
alphahat(:,1) = a1(:,1) + Pstar1(:,:,1)*r0_0 + Pinf1(:,:,1)*r1_0;
etahat(:,1) = QRt*r(:,1);
else
r0 = ri(:,1);
for i=pp:-1:1
if Fi(i,1) > crit
r0 = Z(i,:)'/Fi(i,1)*v(i,1)+Li(:,:,i,1)'*r0;
end
end
alphahat(:,1) = a1(:,1) + P1(:,:,1)*r0;
etahat(:,1) = QRt*r(:,1);
end
a=a(:,1:end-1);
function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,start)
% function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
% function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,start)
% Computes the diffuse likelihood without measurement error, in the case of a non-singular var-cov matrix
%
% INPUTS
......@@ -11,7 +11,6 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
% Pinf: mm*mm diagonal matrix with with q ones and m-q zeros
% Pstar: mm*mm variance-covariance matrix with stationary variables
% Y: pp*1 vector
% trend
% start: likelihood evaluation at 'start'
%
% OUTPUTS
......@@ -47,7 +46,7 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
reste = 0;
while rank(Pinf,crit) & t < smpl
t = t+1;
v = Y(:,t)-Z*a-trend(:,t);
v = Y(:,t)-Z*a;
Finf = Z*Pinf*Z';
if rcond(Finf) < crit
if ~all(abs(Finf(:)) < crit)
......@@ -68,7 +67,7 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
Kinf = Pinf*Z'*iFinf;
Fstar = Z*Pstar*Z';
Kstar = (Pstar*Z'-Kinf*Fstar)*iFinf;
Pstar = T*(Pstar-Pstar*Z'*Kinf'-Pinf*Z'*Kstar)*T'+QQ;
Pstar = T*(Pstar-Pstar*Z'*Kinf'-Pinf*Z'*Kstar')*T'+QQ;
Pinf = T*(Pinf-Pinf*Z'*Kinf')*T';
a = T*(a+Kinf*v);
end
......@@ -80,7 +79,7 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
F_singular = 1;
while notsteady & t < smpl
t = t+1;
v = Y(:,t)-Z*a-trend(:,t);
v = Y(:,t)-Z*a;
F = Z*Pstar*Z';
oldPstar = Pstar;
dF = det(F);
......@@ -97,7 +96,7 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
lik(t) = log(dF)+v'*iF*v;
K = Pstar*Z'*iF;
a = T*(a+K*v);
Pstar = T*(Pstar-K*Pstar*Z')*T'+QQ;
Pstar = T*(Pstar-K*Z*Pstar)*T'+QQ;
end
notsteady = ~(max(max(abs(Pstar-oldPstar)))<crit);
end
......@@ -108,7 +107,7 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)
reste = smpl-t;
while t < smpl
t = t+1;
v = Y(:,t)-Z*a-trend(:,t);
v = Y(:,t)-Z*a;
a = T*(a+K*v);
lik(t) = v*iF*v;
end
......
function [LIK, lik] = DiffuseLikelihood3_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)%//Z,T,R,Q,Pinf,Pstar,Y)
function [LIK, lik] = DiffuseLikelihood3_Z(T,Z,R,Q,Pinf,Pstar,Y,start)
% function [LIK, lik] = DiffuseLikelihood3(T,R,Q,Pinf,Pstar,Y,trend,start)
% function [LIK, lik] = DiffuseLikelihood3(T,R,Q,Pinf,Pstar,Y,start)
% Computes the diffuse likelihood without measurement error, in the case of
% a singular var-cov matrix.
% Univariate treatment of multivariate time series.
......@@ -13,7 +13,6 @@ function [LIK, lik] = DiffuseLikelihood3_Z(T,Z,R,Q,Pinf,Pstar,Y,trend,start)%//Z
% Pinf: mm*mm diagonal matrix with with q ones and m-q zeros
% Pstar: mm*mm variance-covariance matrix with stationary variables
% Y: pp*1 vector
% trend
% start: likelihood evaluation at 'start'
%
% OUTPUTS
......@@ -60,7 +59,7 @@ while newRank & t < smpl
t = t+1;
for i=1:pp
Zi = Z(i,:);
v(i) = Y(i,t)-Zi*a-trend(i,t);
v(i) = Y(i,t)-Zi*a;
Fstar = Zi*Pstar*Zi';
Finf = Zi*Pinf*Zi';
Kstar = Pstar*Zi';
......@@ -131,7 +130,7 @@ while notsteady & t < smpl
oldP = Pstar;
for i=1:pp
Zi = Z(i,:);
v(i) = Y(i,t) - Zi*a - trend(i,t);
v(i) = Y(i,t) - Zi*a;
Fi = Zi*Pstar*Zi';
if Fi > crit
Ki = Pstar*Zi';
......@@ -151,7 +150,7 @@ while t < smpl
Pstar = oldP;
for i=1:pp
Zi = Z(i,i);
v(i) = Y(i,t) - Zi*a - trend(i,t);
v(i) = Y(i,t) - Zi*a;
Fi = Zi*Pstar*Zi';
if Fi > crit
Ki = Pstar*Zi';
......
......@@ -138,18 +138,71 @@ function [fval,cost_flag,ys,trend_coeff,info] = DsgeLikelihood(xparam1,gend,data
Pstar = 10*eye(np);
Pinf = [];
elseif options_.lik_init == 3 % Diffuse Kalman filter
Pstar = zeros(np,np);
ivs = bayestopt_.restrict_var_list_stationary;
R1 = R(ivs,:);
Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1');
% Pinf = bayestopt_.Pinf;
% by M. Ratto
RR=T(:,bayestopt_.restrict_var_list_nonstationary);
i=find(abs(RR)>1.e-10);
R0=zeros(size(RR));
R0(i)=sign(RR(i));
Pinf=R0*R0';
% by M. Ratto
if options_.kalman_algo < 4
Pstar = zeros(np,np);
ivs = bayestopt_.restrict_var_list_stationary;
R1 = R(ivs,:);
Pstar(ivs,ivs) = lyapunov_symm(T(ivs,ivs),R1*Q*R1');
% Pinf = bayestopt_.Pinf;
% by M. Ratto
RR=T(:,bayestopt_.restrict_var_list_nonstationary);
i=find(abs(RR)>1.e-10);
R0=zeros(size(RR));
R0(i)=sign(RR(i));
Pinf=R0*R0';
% by M. Ratto
else
[QT,ST] = schur(T);
e1 = abs(ordeig(ST)) > 2-options_.qz_criterium;
[QT,ST] = ordschur(QT,ST,e1);
k = find(abs(ordeig(ST)) > 2-options_.qz_criterium);
nk = length(k);
nk1 = nk+1;
Pinf = zeros(np,np);
Pinf(1:nk,1:nk) = eye(nk);
Pstar = zeros(np,