2009-07-08 18:38:24 +02:00
|
|
|
|
function [LIK lik] = DiffuseLikelihoodH3corr(T,R,Q,H,Pinf,Pstar,Y,trend,start)
|
2005-02-18 20:54:39 +01:00
|
|
|
|
% Same as DiffuseLikelihoodH3 but allows correlation between the measurement
|
|
|
|
|
% errors (this is not a problem with the multivariate approach).
|
|
|
|
|
|
2008-08-01 20:53:30 +02:00
|
|
|
|
% Copyright (C) 2004 Dynare Team
|
|
|
|
|
%
|
|
|
|
|
% This file is part of Dynare.
|
|
|
|
|
%
|
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
% (at your option) any later version.
|
|
|
|
|
%
|
|
|
|
|
% Dynare is distributed in the hope that it will be useful,
|
|
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
|
%
|
|
|
|
|
% You should have received a copy of the GNU General Public License
|
|
|
|
|
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2005-02-18 20:54:39 +01:00
|
|
|
|
global bayestopt_ options_
|
2009-12-16 18:17:34 +01:00
|
|
|
|
|
2005-02-18 20:54:39 +01:00
|
|
|
|
mf = bayestopt_.mf;
|
|
|
|
|
pp = size(Y,1);
|
|
|
|
|
mm = size(T,1);
|
2010-01-05 11:46:10 +01:00
|
|
|
|
rr = size(Q,1);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
smpl = size(Y,2);
|
|
|
|
|
T = cat(1,cat(2,T,zeros(mm,pp)),zeros(pp,mm+pp));
|
|
|
|
|
R = cat(1,cat(2,R,zeros(mm,pp)),cat(2,zeros(pp,rr),eye(pp)));
|
|
|
|
|
Q = cat(1,cat(2,Q,zeros(rr,pp)),cat(2,zeros(pp,rr),H));
|
|
|
|
|
if size(Pinf,1) % Otherwise Pinf = 0 (no unit root)
|
2009-12-16 18:17:34 +01:00
|
|
|
|
Pinf = cat(1,cat(2,Pinf,zeros(mm,pp)),zeros(pp,mm+pp));
|
2005-02-18 20:54:39 +01:00
|
|
|
|
end
|
|
|
|
|
Pstar = cat(1,cat(2,Pstar,zeros(mm,pp)),cat(2,zeros(pp,mm),H));
|
|
|
|
|
a = zeros(mm+pp,1);
|
|
|
|
|
QQ = R*Q*transpose(R);
|
|
|
|
|
t = 0;
|
2009-07-08 18:38:24 +02:00
|
|
|
|
lik = zeros(smpl,1);
|
2010-01-05 11:46:10 +01:00
|
|
|
|
notsteady = 1;
|
|
|
|
|
crit = options_.kalman_tol;
|
|
|
|
|
newRank = rank(Pinf,crit);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
|
|
|
|
|
while rank(Pinf,crit) & t < smpl %% Matrix Finf is assumed to be zero
|
2009-12-16 18:17:34 +01:00
|
|
|
|
t = t+1;
|
|
|
|
|
for i=1:pp
|
2010-01-05 11:46:10 +01:00
|
|
|
|
v(i) = Y(i,t)-a(mf(i))-a(mm+i)-trend(i,t);
|
|
|
|
|
Fstar = Pstar(mf(i),mf(i))+Pstar(mm+i,mm+i);
|
|
|
|
|
Finf = Pinf(mf(i),mf(i));
|
|
|
|
|
Kstar = Pstar(:,mf(i))+Pstar(:,mm+i);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if Finf > crit
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Kinf = Pinf(:,mf(i));
|
|
|
|
|
a = a + Kinf*v(i)/Finf;
|
|
|
|
|
Pstar = Pstar + Kinf*transpose(Kinf)*Fstar/(Finf*Finf) - ...
|
2009-12-16 18:17:34 +01:00
|
|
|
|
(Kstar*transpose(Kinf)+Kinf*transpose(Kstar))/Finf;
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Pinf = Pinf - Kinf*transpose(Kinf)/Finf;
|
|
|
|
|
lik(t) = lik(t) + log(Finf);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
else %% 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
|
2010-01-05 11:46:10 +01:00
|
|
|
|
%% rank(Pinf)=0. [st<73>phane,11-03-2004].
|
2009-12-16 18:17:34 +01:00
|
|
|
|
if rank(Pinf) == 0
|
2010-01-05 11:46:10 +01:00
|
|
|
|
lik(t) = lik(t) + log(Fstar) + v(i)*v(i)/Fstar;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2010-01-05 11:46:10 +01:00
|
|
|
|
a = a + Kstar*v(i)/Fstar;
|
|
|
|
|
Pstar = Pstar - Kstar*transpose(Kstar)/Fstar;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
|
|
|
|
oldRank = rank(Pinf,crit);
|
2010-01-05 11:46:10 +01:00
|
|
|
|
a = T*a;
|
|
|
|
|
Pstar = T*Pstar*transpose(T)+QQ;
|
|
|
|
|
Pinf = T*Pinf*transpose(T);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
newRank = rank(Pinf,crit);
|
|
|
|
|
if oldRank ~= newRank
|
2010-01-05 11:46:10 +01:00
|
|
|
|
disp('DiffuseLiklihoodH3 :: T does influence the rank of Pinf!')
|
|
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
end
|
|
|
|
|
if t == smpl
|
2009-12-16 18:17:34 +01:00
|
|
|
|
error(['There isn''t enough information to estimate the initial' ...
|
|
|
|
|
' conditions of the nonstationary variables']);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
end
|
|
|
|
|
while notsteady & t < smpl
|
2009-12-16 18:17:34 +01:00
|
|
|
|
t = t+1;
|
|
|
|
|
for i=1:pp
|
|
|
|
|
v(i) = Y(i,t) - a(mf(i)) - trend(i,t) -a(mm+i);
|
|
|
|
|
Fi = Pstar(mf(i),mf(i))+Pstar(mm+i,mm+i);
|
|
|
|
|
if Fi > crit
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Ki = Pstar(:,mf(i))+Pstar(:,mm+i);
|
|
|
|
|
a = a + Ki*v(i)/Fi;
|
|
|
|
|
Pstar = Pstar - Ki*transpose(Ki)/Fi;
|
|
|
|
|
lik(t) = lik(t) + log(Fi) + v(i)*v(i)/Fi;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2010-01-05 11:46:10 +01:00
|
|
|
|
end
|
|
|
|
|
oldP = Pstar;
|
|
|
|
|
a = T*a;
|
|
|
|
|
Pstar = T*Pstar*transpose(T) + QQ;
|
|
|
|
|
notsteady = ~(max(max(abs(Pstar-oldP)))<crit);
|
2005-02-18 20:54:39 +01:00
|
|
|
|
end
|
|
|
|
|
while t < smpl
|
2009-12-16 18:17:34 +01:00
|
|
|
|
t = t+1;
|
|
|
|
|
for i=1:pp
|
|
|
|
|
v(i) = Y(i,t) - a(mf(i)) - trend(i,t) - a(mm+i);
|
|
|
|
|
Fi = Pstar(mf(i),mf(i))+Pstar(mm+i,mm+i);
|
|
|
|
|
if Fi > crit
|
2010-01-05 11:46:10 +01:00
|
|
|
|
Ki = Pstar(:,mf(i))+Pstar(:,mm+i);
|
|
|
|
|
a = a + Ki*v(i)/Fi;
|
|
|
|
|
Pstar = Pstar - Ki*transpose(Ki)/Fi;
|
|
|
|
|
lik(t) = lik(t) + log(Fi) + v(i)*v(i)/Fi;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
end
|
2010-01-05 11:46:10 +01:00
|
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
a = T*a;
|
|
|
|
|
end
|
2009-07-08 18:38:24 +02:00
|
|
|
|
% adding log-likelihhod constants
|
|
|
|
|
lik = (lik + pp*log(2*pi))/2;
|
|
|
|
|
|
|
|
|
|
LIK = sum(lik(start:end)); % Minus the log-likelihood.
|