M. Ratto added lik in output [October 2005] changes by M. Ratto introduced new global variable id_ for termination of DKF introduced a persistent fmax, in order to keep track the max order of magnitude of the 'zero' values in Pinf at DKF termination new icc counter for Finf steps in DKF new termination for DKF likelihood terms for Fstar must be cumulated in DKF also when Pinf is non zero. this bug is fixed. stephane.adjemian@cepremap.cnrs.fr [07-19-2004] 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). Case where F_{\infty,t} is singular ==> Univariate treatment of multivariate time series. THE PROBLEM: y_t = Z_t * \alpha_t + \varepsilon_t \alpha_{t+1} = T_t * \alpha_t + R_t * \eta_t with: \alpha_1 = a + A*\delta + R_0*\eta_0 m*q matrix A and m*(m-q) matrix R_0 are selection matrices (their columns constitue all the columns of the m*m identity matrix) so that A'*R_0 = 0 and A'*\alpha_1 = \delta We assume that the vector \delta is distributed as a N(0,\kappa*I_q) for a given \kappa > 0. So that the expectation of \alpha_1 is a and its variance is P, with P = \kappa*P_{\infty} + P_{\star} P_{\infty} = A*A' P_{\star} = R_0*Q_0*R_0' P_{\infty} is a m*m diagonal matrix with q ones and m-q zeros. and where: y_t is a pp*1 vector \alpha_t is a mm*1 vector \varepsilon_t is a pp*1 multivariate random variable (iid N(0,H_t)) \eta_t is a rr*1 multivariate random variable (iid N(0,Q_t)) a_1 is a mm*1 vector Z_t is a pp*mm matrix T_t is a mm*mm matrix H_t is a pp*pp matrix R_t is a mm*rr matrix Q_t is a rr*rr matrix P_1 is a mm*mm matrix FILTERING EQUATIONS: v_t = y_t - Z_t* a_t F_t = Z_t * P_t * Z_t' + H_t K_t = T_t * P_t * Z_t' * F_t^{-1} L_t = T_t - K_t * Z_t a_{t+1} = T_t * a_t + K_t * v_t P_{t+1} = T_t * P_t * L_t' + R_t*Q_t*R_t' DIFFUSE FILTERING EQUATIONS: a_{t+1} = T_t*a_t + K_{\ast,t}v_t P_{\infty,t+1} = T_t*P_{\infty,t}*T_t' P_{\ast,t+1} = T_t*P_{\ast,t}*L_{\ast,t}' + R_t*Q_t*R_t' K_{\ast,t} = T_t*P_{\ast,t}*Z_t'*F_{\ast,t}^{-1} v_t = y_t - Z_t*a_t L_{\ast,t} = T_t - K_{\ast,t}*Z_t F_{\ast,t} = Z_t*P_{\ast,t}*Z_t' + H_t
0001 function [LIK, lik] = DiffuseLikelihoodH3(T,R,Q,H,Pinf,Pstar,Y,trend,start) 0002 % M. Ratto added lik in output [October 2005] 0003 % changes by M. Ratto 0004 % introduced new global variable id_ for termination of DKF 0005 % introduced a persistent fmax, in order to keep track the max order of 0006 % magnitude of the 'zero' values in Pinf at DKF termination 0007 % new icc counter for Finf steps in DKF 0008 % new termination for DKF 0009 % likelihood terms for Fstar must be cumulated in DKF also when Pinf is non 0010 % zero. this bug is fixed. 0011 % 0012 % stephane.adjemian@cepremap.cnrs.fr [07-19-2004] 0013 % 0014 % See "Filtering and Smoothing of State Vector for Diffuse State Space 0015 % Models", S.J. Koopman and J. Durbin (2003, in Journal of Time Series 0016 % Analysis, vol. 24(1), pp. 85-98). 0017 % 0018 % Case where F_{\infty,t} is singular ==> Univariate treatment of multivariate 0019 % time series. 0020 % 0021 % THE PROBLEM: 0022 % 0023 % y_t = Z_t * \alpha_t + \varepsilon_t 0024 % \alpha_{t+1} = T_t * \alpha_t + R_t * \eta_t 0025 % 0026 % with: 0027 % 0028 % \alpha_1 = a + A*\delta + R_0*\eta_0 0029 % 0030 % m*q matrix A and m*(m-q) matrix R_0 are selection matrices (their 0031 % columns constitue all the columns of the m*m identity matrix) so that 0032 % 0033 % A'*R_0 = 0 and A'*\alpha_1 = \delta 0034 % 0035 % We assume that the vector \delta is distributed as a N(0,\kappa*I_q) 0036 % for a given \kappa > 0. So that the expectation of \alpha_1 is a and 0037 % its variance is P, with 0038 % 0039 % P = \kappa*P_{\infty} + P_{\star} 0040 % 0041 % P_{\infty} = A*A' 0042 % P_{\star} = R_0*Q_0*R_0' 0043 % 0044 % P_{\infty} is a m*m diagonal matrix with q ones and m-q zeros. 0045 % 0046 % 0047 % and where: 0048 % 0049 % y_t is a pp*1 vector 0050 % \alpha_t is a mm*1 vector 0051 % \varepsilon_t is a pp*1 multivariate random variable (iid N(0,H_t)) 0052 % \eta_t is a rr*1 multivariate random variable (iid N(0,Q_t)) 0053 % a_1 is a mm*1 vector 0054 % 0055 % Z_t is a pp*mm matrix 0056 % T_t is a mm*mm matrix 0057 % H_t is a pp*pp matrix 0058 % R_t is a mm*rr matrix 0059 % Q_t is a rr*rr matrix 0060 % P_1 is a mm*mm matrix 0061 % 0062 % 0063 % FILTERING EQUATIONS: 0064 % 0065 % v_t = y_t - Z_t* a_t 0066 % F_t = Z_t * P_t * Z_t' + H_t 0067 % K_t = T_t * P_t * Z_t' * F_t^{-1} 0068 % L_t = T_t - K_t * Z_t 0069 % a_{t+1} = T_t * a_t + K_t * v_t 0070 % P_{t+1} = T_t * P_t * L_t' + R_t*Q_t*R_t' 0071 % 0072 % 0073 % DIFFUSE FILTERING EQUATIONS: 0074 % 0075 % a_{t+1} = T_t*a_t + K_{\ast,t}v_t 0076 % P_{\infty,t+1} = T_t*P_{\infty,t}*T_t' 0077 % P_{\ast,t+1} = T_t*P_{\ast,t}*L_{\ast,t}' + R_t*Q_t*R_t' 0078 % K_{\ast,t} = T_t*P_{\ast,t}*Z_t'*F_{\ast,t}^{-1} 0079 % v_t = y_t - Z_t*a_t 0080 % L_{\ast,t} = T_t - K_{\ast,t}*Z_t 0081 % F_{\ast,t} = Z_t*P_{\ast,t}*Z_t' + H_t 0082 global bayestopt_ options_ 0083 0084 mf = bayestopt_.mf; 0085 pp = size(Y,1); 0086 mm = size(T,1); 0087 smpl = size(Y,2); 0088 a = zeros(mm,1); 0089 QQ = R*Q*transpose(R); 0090 t = 0; 0091 lik = zeros(smpl+1,1); 0092 lik(smpl+1) = smpl*pp*log(2*pi); %% the constant of minus two times the log-likelihood 0093 notsteady = 1; 0094 crit = options_.kalman_tol; 0095 crit1 = 1.e-6; 0096 newRank = rank(Pinf,crit1); 0097 icc = 0; 0098 while newRank & t < smpl %% Matrix Finf is assumed to be zero 0099 t = t+1; 0100 for i=1:pp 0101 v(i) = Y(i,t)-a(mf(i))-trend(i,t); 0102 Fstar = Pstar(mf(i),mf(i))+H(i,i); 0103 Finf = Pinf(mf(i),mf(i)); 0104 Kstar = Pstar(:,mf(i)); 0105 if Finf > crit & newRank 0106 icc = icc + 1; 0107 Kinf = Pinf(:,mf(i)); 0108 a = a + Kinf*v(i)/Finf; 0109 Pstar = Pstar + Kinf*transpose(Kinf)*Fstar/(Finf*Finf) - ... 0110 (Kstar*transpose(Kinf)+Kinf*transpose(Kstar))/Finf; 0111 Pinf = Pinf - Kinf*transpose(Kinf)/Finf; 0112 lik(t) = lik(t) + log(Finf); 0113 % start new termination criterion for DKF 0114 if ~isempty(options_.diffuse_d), 0115 newRank = (icc<options_.diffuse_d); 0116 %if newRank & any(diag(Pinf(mf,mf))>crit)==0; % M. Ratto this line is BUGGY 0117 if newRank & (any(diag(Pinf(mf,mf))>crit)==0 & rank(Pinf,crit1)==0); 0118 options_.diffuse_d = icc; 0119 newRank=0; 0120 disp('WARNING: Change in OPTIONS_.DIFFUSE_D in univariate DKF') 0121 disp(['new OPTIONS_.DIFFUSE_D = ',int2str(icc)]) 0122 disp('You may have to reset the optimisation') 0123 end 0124 else 0125 %newRank = any(diag(Pinf(mf,mf))>crit); % M. Ratto this line is BUGGY 0126 newRank = (any(diag(Pinf(mf,mf))>crit) | rank(Pinf,crit1)); 0127 if newRank==0, 0128 P0= T*Pinf*transpose(T); 0129 %newRank = any(diag(P0(mf,mf))>crit); % M. Ratto this line is BUGGY 0130 newRank = (any(diag(P0(mf,mf))>crit) | rank(P0,crit1)); % M. Ratto 10 Oct 2005 0131 if newRank==0, 0132 options_.diffuse_d = icc; 0133 end 0134 end 0135 end, 0136 % end new termination and checks for DKF and fmax 0137 elseif Finf > crit 0138 %% Note that : (1) rank(Pinf)=0 implies that Finf = 0, (2) outside this loop (when for some i and t the condition 0139 %% rank(Pinf)=0 is satisfied we have P = Pstar and F = Fstar and (3) Finf = 0 does not imply that 0140 %% rank(Pinf)=0. [stphane,11-03-2004]. 0141 %if rank(Pinf) == 0 0142 % the likelihood terms should alwasy be cumulated, not only 0143 % when Pinf=0, otherwise the lik would depend on the ordering 0144 % of observed variables 0145 lik(t) = lik(t) + log(Fstar) + v(i)*v(i)/Fstar; 0146 %end 0147 a = a + Kstar*v(i)/Fstar; 0148 Pstar = Pstar - Kstar*transpose(Kstar)/Fstar; 0149 else 0150 % disp(['zero F term in DKF for observed ',int2str(i),' ',num2str(Fi)]) 0151 end 0152 end 0153 if newRank 0154 oldRank = rank(Pinf,crit1); 0155 else 0156 oldRank = 0; 0157 end 0158 a = T*a; 0159 Pstar = T*Pstar*transpose(T)+QQ; 0160 Pinf = T*Pinf*transpose(T); 0161 if newRank 0162 newRank = rank(Pinf,crit1); 0163 end 0164 if oldRank ~= newRank 0165 disp('DiffuseLiklihoodH3 :: T does influence the rank of Pinf!') 0166 end 0167 end 0168 if t == smpl 0169 error(['There isn''t enough information to estimate the initial' ... 0170 ' conditions of the nonstationary variables']); 0171 end 0172 while notsteady & t < smpl 0173 t = t+1; 0174 for i=1:pp 0175 v(i) = Y(i,t) - a(mf(i)) - trend(i,t); 0176 Fi = Pstar(mf(i),mf(i))+H(i,i); 0177 if Fi > crit 0178 Ki = Pstar(:,mf(i)); 0179 a = a + Ki*v(i)/Fi; 0180 Pstar = Pstar - Ki*transpose(Ki)/Fi; 0181 lik(t) = lik(t) + log(Fi) + v(i)*v(i)/Fi; 0182 end 0183 end 0184 oldP = Pstar; 0185 a = T*a; 0186 Pstar = T*Pstar*transpose(T) + QQ; 0187 notsteady = ~(max(max(abs(Pstar-oldP)))<crit); 0188 end 0189 while t < smpl 0190 t = t+1; 0191 for i=1:pp 0192 v(i) = Y(i,t) - a(mf(i)) - trend(i,t); 0193 Fi = Pstar(mf(i),mf(i))+H(i,i); 0194 if Fi > crit 0195 Ki = Pstar(:,mf(i)); 0196 a = a + Ki*v(i)/Fi; 0197 Pstar = Pstar - Ki*transpose(Ki)/Fi; 0198 lik(t) = lik(t) + log(Fi) + v(i)*v(i)/Fi; 0199 end 0200 end 0201 a = T*a; 0202 end 0203 LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl); 0204