2015-04-08 15:49:12 +02:00
function [dLIK,dlik,a,Pstar] = kalman_filter_d ( Y, start, last, a, Pinf, Pstar, kalman_tol, diffuse_kalman_tol, riccati_tol, presample, T, R, Q, H, Z, mm, pp, rr)
2008-10-27 18:12:44 +01:00
% Computes the diffuse likelihood of a state space model.
%
2017-05-16 15:10:20 +02:00
% INPUTS
2011-09-19 17:01:24 +02:00
% Y [double] pp*smpl matrix of (detrended) data, where pp is the number of observed variables.
% start [integer] scalar, first observation.
% last [integer] scalar, last observation.
2021-01-18 17:33:21 +01:00
% a [double] mm*1 vector, levels of the predicted initial state variables (E_{0}(alpha_1)).
2008-10-27 18:12:44 +01:00
% Pinf [double] mm*mm matrix used to initialize the covariance matrix of the state vector.
% Pstar [double] mm*mm matrix used to initialize the covariance matrix of the state vector.
2016-06-14 17:29:31 +02:00
% kalman_tol [double] scalar, tolerance parameter (rcond) of F_star.
% diffuse_kalman_tol [double] scalar, tolerance parameter (rcond) of Pinf to signify end of diffuse filtering and Finf.
2017-05-16 15:10:20 +02:00
% riccati_tol [double] scalar, tolerance parameter (riccati iteration);
2016-06-14 17:29:31 +02:00
% not used in this filter as usually diffuse phase will be left before convergence of filter to steady state.
2017-05-16 15:10:20 +02:00
% presample [integer] scalar, presampling if strictly positive.
2011-09-19 17:01:24 +02:00
% T [double] mm*mm matrix, transition matrix in the state equations.
% R [double] mm*rr matrix relating the structural innovations to the state vector.
% Q [double] rr*rr covariance matrix of the structural innovations.
2017-05-16 15:10:20 +02:00
% H [double] pp*pp covariance matrix of the measurement errors (if H is equal to zero (scalar) there is no measurement error).
2013-03-18 10:59:32 +01:00
% Z [double] pp*mm matrix, selection matrix or pp linear independent combinations of the state vector.
2011-09-19 17:01:24 +02:00
% mm [integer] scalar, number of state variables.
% pp [integer] scalar, number of observed variables.
2017-05-16 15:10:20 +02:00
% rr [integer] scalar, number of structural innovations.
%
% OUTPUTS
2011-09-19 17:01:24 +02:00
% LIK [double] scalar, minus loglikelihood
% lik [double] smpl*1 vector, log density of each vector of observations.
2021-01-18 17:33:21 +01:00
% a [double] mm*1 vector, current estimate of the state vector tomorrow
% (E_{T}(alpha_{T+1})).
2017-05-16 15:10:20 +02:00
% Pstar [double] mm*mm matrix, covariance matrix of the state vector.
%
% REFERENCES
2008-10-27 18:12:44 +01:00
% See "Filtering and Smoothing of State Vector for Diffuse State Space
2017-05-16 15:10:20 +02:00
% Models", S.J. Koopman and J. Durbin (2003), in Journal of Time Series
% Analysis, vol. 24(1), pp. 85-98.
2016-06-14 17:29:31 +02:00
% and
% Durbin/Koopman (2012): "Time Series Analysis by State Space Methods", Oxford University Press,
% Second Edition, Ch. 5 and 7.2
2008-10-27 18:12:44 +01:00
2021-05-02 17:07:12 +02:00
% Copyright (C) 2004-2021 Dynare Team
2008-10-27 18:12:44 +01:00
%
% 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/>.
2009-12-16 18:17:34 +01:00
2011-09-19 17:01:24 +02:00
% Get sample size.
smpl = last - start + 1 ;
% Initialize some variables.
2009-12-16 18:17:34 +01:00
dF = 1 ;
2021-05-02 17:07:12 +02:00
isqvec = false ;
if ndim ( Q ) > 2
Qvec = Q ;
Q = Q ( : , : , 1 ) ;
isqvec = true ;
end
2011-09-19 17:01:24 +02:00
QQ = R * Q * transpose ( R ) ; % Variance of R times the vector of structural innovations.
t = start ; % Initialization of the time index.
dlik = zeros ( smpl , 1 ) ; % Initialization of the vector gathering the densities.
dLIK = Inf ; % Default value of the log likelihood.
2010-04-16 12:19:09 +02:00
oldK = Inf ;
2011-11-21 12:39:02 +01:00
s = 0 ;
2009-12-16 18:17:34 +01:00
2017-11-16 17:19:39 +01:00
while rank ( Z * Pinf * Z ' , diffuse_kalman_tol ) && ( t < = last )
2011-09-19 17:01:24 +02:00
s = t - start + 1 ;
2016-06-14 17:29:31 +02:00
v = Y ( : , t ) - Z * a ; %get prediction error v^(0) in (5.13) DK (2012)
Finf = Z * Pinf * Z ' ; % (5.7) in DK (2012)
2021-05-02 17:07:12 +02:00
if isqvec
QQ = R * Qvec ( : , : , t + 1 ) * transpose ( R ) ;
end
2017-05-16 15:10:20 +02:00
%do case distinction based on whether F_{\infty,t} has full rank or 0 rank
if rcond ( Finf ) < diffuse_kalman_tol %F_{\infty,t} = 0
2016-06-14 17:29:31 +02:00
if ~ all ( abs ( Finf ( : ) ) < diffuse_kalman_tol ) %rank-deficient but not rank 0
2017-05-16 15:10:20 +02:00
% The univariate diffuse kalman filter should be used instead.
2009-12-16 18:17:34 +01:00
return
2016-06-14 17:29:31 +02:00
else %rank of F_{\infty,t} is 0
Fstar = Z * Pstar * Z ' + H ; % (5.7) in DK (2012)
if rcond ( Fstar ) < kalman_tol %F_{*} is singular
2009-12-16 18:17:34 +01:00
if ~ all ( abs ( Fstar ( : ) ) < kalman_tol )
% The univariate diffuse kalman filter should be used.
return
2016-06-14 17:29:31 +02:00
else %rank 0
2017-05-16 15:10:20 +02:00
%pathological case, discard draw
return
2009-12-16 18:17:34 +01:00
end
else
iFstar = inv ( Fstar ) ;
dFstar = det ( Fstar ) ;
2016-10-28 11:40:35 +02:00
Kstar = Pstar * Z ' * iFstar ; %(5.15) of DK (2012) with Kstar=T^{-1}*K^(0)
2016-06-14 17:29:31 +02:00
dlik ( s ) = log ( dFstar ) + v ' * iFstar * v ; %set w_t to bottom case in bottom equation page 172, DK (2012)
Pinf = T * Pinf * transpose ( T ) ; % (5.16) DK (2012)
Pstar = T * ( Pstar - Pstar * Z ' * Kstar ' ) * T ' + QQ ; % (5.17) DK (2012)
a = T * ( a + Kstar * v ) ; % (5.13) DK (2012)
2009-12-16 18:17:34 +01:00
end
end
2016-06-14 17:29:31 +02:00
else %F_{\infty,t} positive definite
2017-05-16 15:10:20 +02:00
%To compare to DK (2012), this block makes use of the following transformation
%Kstar=T^{-1}*K^{(1)}=M_{*}*F^{(1)}+M_{\infty}*F^{(2)}
% =P_{*}*Z'*F^{(1)}+P_{\infty}*Z'*((-1)*(-F_{\infty}^{-1})*F_{*}*(F_{\infty}^{-1}))
% =[P_{*}*Z'-Kinf*F_{*})]*F^{(1)}
%Make use of L^{0}'=(T-K^{(0)}*Z)'=(T-T*M_{\infty}*F^{(1)}*Z)'
% =(T-T*P_{\infty*Z'*F^{(1)}*Z)'=(T-T*Kinf*Z)'
% = (T*(I-*Kinf*Z))'=(I-Z'*Kinf')*T'
%P_{*}=T*P_{\infty}*L^{(1)}+T*P_{*}*L^{(0)}+RQR
% =T*[(P_{\infty}*(-K^{(1)*Z}))+P_{*}*(I-Z'*Kinf')*T'+RQR]
2016-06-14 17:29:31 +02:00
dlik ( s ) = log ( det ( Finf ) ) ; %set w_t to top case in bottom equation page 172, DK (2012)
2011-09-19 17:01:24 +02:00
iFinf = inv ( Finf ) ;
2016-10-28 11:40:35 +02:00
Kinf = Pinf * Z ' * iFinf ; %define Kinf=T^{-1}*K_0 with M_{\infty}=Pinf*Z'
2016-06-14 17:29:31 +02:00
Fstar = Z * Pstar * Z ' + H ; %(5.7) DK(2012)
2016-10-29 15:00:37 +02:00
Kstar = ( Pstar * Z ' - Kinf * Fstar ) * iFinf ; %(5.12) DK(2012); note that there is a typo in DK (2003) with "+ Kinf" instead of "- Kinf", but it is correct in their appendix
2016-06-14 17:29:31 +02:00
Pstar = T * ( Pstar - Pstar * Z ' * Kinf ' - Pinf * Z ' * Kstar ' ) * T ' + QQ ; %(5.14) DK(2012)
Pinf = T * ( Pinf - Pinf * Z ' * Kinf ' ) * T ' ; %(5.14) DK(2012)
a = T * ( a + Kinf * v ) ; %(5.13) DK(2012)
2009-12-16 18:17:34 +01:00
end
2011-09-19 17:01:24 +02:00
t = t + 1 ;
2009-12-16 18:17:34 +01:00
end
2011-09-19 17:01:24 +02:00
if t > last
2017-05-16 15:10:20 +02:00
warning ( [ ' kalman_filter_d: There isn' ' t enough information to estimate the initial conditions of the nonstationary variables. The diffuse Kalman filter never left the diffuse stage.' ] ) ;
2011-09-19 17:01:24 +02:00
dLIK = NaN ;
2010-02-05 23:18:08 +01:00
return
2009-12-16 18:17:34 +01:00
end
2011-09-19 17:01:24 +02:00
dlik = dlik ( 1 : s ) ;
dlik = . 5 * ( dlik + pp * log ( 2 * pi ) ) ;
2009-12-16 18:17:34 +01:00
2012-01-23 16:23:47 +01:00
dLIK = sum ( dlik ( 1 + presample : end ) ) ;