0001 function [LIK, lik] = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,Y,trend,start)
0002
0003
0004
0005
0006 global bayestopt_ options_
0007
0008 mf = bayestopt_.mf;
0009 smpl = size(Y,2);
0010 mm = size(T,2);
0011 pp = size(Y,1);
0012 a = zeros(mm,1);
0013 dF = 1;
0014 QQ = R*Q*transpose(R);
0015 t = 0;
0016 lik = zeros(smpl+1,1);
0017 LIK = Inf;
0018 lik(smpl+1) = smpl*pp*log(2*pi);
0019 notsteady = 1;
0020 crit = options_.kalman_tol;
0021 reste = 0;
0022 while rank(Pinf,crit) & t < smpl
0023 t = t+1;
0024 v = Y(:,t)-a(mf)-trend(:,t);
0025 Finf = Pinf(mf,mf);
0026 if rcond(Finf) < crit
0027 if ~all(abs(Finf(:)) < crit)
0028 return
0029 else
0030 iFstar = inv(Pstar(mf,mf));
0031 dFstar = det(Pstar(mf,mf));
0032 Kstar = Pstar(:,mf)*iFstar;
0033 lik(t) = log(dFstar) + transpose(v)*iFstar*v;
0034 Pinf = T*Pinf*transpose(T);
0035 Pstar = T*(Pstar-Pstar(:,mf)*transpose(Kstar))*transpose(T)+QQ;
0036 a = T*(a+Kstar*v);
0037 end
0038 else
0039 lik(t) = log(det(Finf));
0040 iFinf = inv(Finf);
0041 Kinf = Pinf(:,mf)*iFinf;
0042 Fstar = Pstar(mf,mf);
0043 Kstar = (Pstar(:,mf)-Kinf*Fstar)*iFinf;
0044 Pstar = T*(Pstar-Pstar(:,mf)*transpose(Kinf)-Pinf(:,mf)*transpose(Kstar))*transpose(T)+QQ;
0045 Pinf = T*(Pinf-Pinf(:,mf)*transpose(Kinf))*transpose(T);
0046 a = T*(a+Kinf*v);
0047 end
0048 end
0049 if t == smpl
0050 error(['There isn''t enough information to estimate the initial' ...
0051 ' conditions of the nonstationary variables']);
0052 end
0053 F_singular = 1;
0054 while notsteady & t < smpl
0055 t = t+1;
0056 v = Y(:,t)-a(mf)-trend(:,t);
0057 F = Pstar(mf,mf);
0058 oldPstar = Pstar;
0059 dF = det(F);
0060 if rcond(F) < crit
0061 if ~all(abs(F(:))<crit)
0062 return
0063 else
0064 a = T*a;
0065 Pstar = T*Pstar*transpose(T)+QQ;
0066 end
0067 else
0068 F_singular = 0;
0069 iF = inv(F);
0070 lik(t) = log(dF)+transpose(v)*iF*v;
0071 K = Pstar(:,mf)*iF;
0072 a = T*(a+K*v);
0073 Pstar = T*(Pstar-K*Pstar(mf,:))*transpose(T)+QQ;
0074 end
0075 notsteady = ~(max(max(abs(Pstar-oldPstar)))<crit);
0076 end
0077 if F_singular == 1
0078 error(['The variance of the forecast error remains singular until the' ...
0079 'end of the sample'])
0080 end
0081 reste = smpl-t;
0082 while t < smpl
0083 t = t+1;
0084 v = Y(:,t)-a(mf)-trend(:,t);
0085 a = T*(a+K*v);
0086 lik(t) = transpose(v)*iF*v;
0087 end
0088 lik(t) = lik(t) + reste*log(dF);
0089
0090
0091 LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl);