2015-10-13 17:15:01 +02:00
function [LIK, likk, a] = kalman_filter_ss ( Y,start,last,a,T,K,iF,log_dF,Z,pp,Zflag,analytic_derivation,Da,DT,DYss,D2a,D2T,D2Yss)
2011-09-19 17:01:24 +02:00
% Computes the likelihood of a stationnary state space model (steady state kalman filter).
2011-10-24 13:02:41 +02:00
%@info:
2011-12-26 15:39:27 +01:00
%! @deftypefn {Function File} {[@var{LIK},@var{likk},@var{a},@var{P} ] =} kalman_filter_ss (@var{Y}, @var{start}, @var{last}, @var{a}, @var{P}, @var{kalman_tol}, @var{riccati_tol},@var{presample},@var{T},@var{Q},@var{R},@var{H},@var{Z},@var{mm},@var{pp},@var{rr},@var{Zflag},@var{diffuse_periods})
2011-10-24 13:02:41 +02:00
%! @anchor{kalman_filter}
%! @sp 1
%! Computes the likelihood of a stationary state space model, given initial condition for the states (mean), the steady state kalman gain and the steady state inveverted covariance matrix of the prediction errors.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item Y
%! Matrix (@var{pp}*T) of doubles, data.
%! @item start
%! Integer scalar, first period.
%! @item last
%! Integer scalar, last period (@var{last}-@var{first} has to be inferior to T).
%! @item a
2021-01-18 17:33:21 +01:00
%! Vector (mm*1) of doubles, levels of the predicted initial state variables (E_{0}(alpha_1)).
2011-10-24 13:02:41 +02:00
%! @item T
%! Matrix (mm*mm) of doubles, transition matrix of the state equation.
%! @item K
%! Matrix (mm*@var{pp}) of doubles, steady state kalman gain.
%! @item iF
%! Matrix (@var{pp}*@var{pp}) of doubles, inverse of the steady state covariance matrix of the prediction errors.
%! @item dF
%! Double scalar, determinant of the steady state covariance matrix of teh prediction errors.
%! @item Z
%! Matrix (@var{pp}*mm) of doubles or vector of integers, matrix relating the states to the observed variables or vector of indices (depending on the value of @var{Zflag}).
%! @item pp
%! Integer scalar, number of observed variables.
%! @item Zflag
%! Integer scalar, equal to 0 if Z is a vector of indices targeting the obseved variables in the state vector, equal to 1 if Z is a @var{pp}*@var{mm} matrix.
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item LIK
%! Double scalar, value of (minus) the likelihood.
%! @item likk
%! Column vector of doubles, values of the density of each observation.
%! @item a
2021-01-18 17:33:21 +01:00
%! Vector (mm*1) of doubles, current estimate of the state vector tomorrow (E_{T}(alpha_{T+1})).
2011-10-24 13:02:41 +02:00
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 1
%! @ref{kalman_filter}
%! @sp 2
%! @strong{This function calls:}
%! @sp 1
%! @end deftypefn
%@eod:
2011-09-19 17:01:24 +02:00
2022-04-13 13:15:19 +02:00
% Copyright © 2011-2017 Dynare Team
2011-10-24 13:02:41 +02:00
%
2011-09-19 17:01:24 +02: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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2011-09-19 17:01:24 +02:00
2011-10-24 13:02:41 +02:00
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
2011-09-19 17:01:24 +02:00
% Get sample size.
smpl = last - start + 1 ;
% Initialize some variables.
t = start ; % Initialization of the time index.
2011-10-24 13:14:20 +02:00
likk = zeros ( smpl , 1 ) ; % Initialization of the vector gathering the densities.
2011-09-19 17:01:24 +02:00
LIK = Inf ; % Default value of the log likelihood.
2012-04-27 15:57:58 +02:00
notsteady = 0 ;
2012-06-08 14:23:18 +02:00
asy_hess = 0 ;
2012-04-27 15:57:58 +02:00
if nargin < 12
analytic_derivation = 0 ;
end
2017-05-16 12:42:01 +02:00
if analytic_derivation == 0
2012-04-27 15:57:58 +02:00
DLIK = [ ] ;
Hess = [ ] ;
else
k = size ( DT , 3 ) ; % number of structural parameters
DLIK = zeros ( k , 1 ) ; % Initialization of the score.
2012-06-08 14:23:18 +02:00
dlikk = zeros ( smpl , k ) ;
2017-05-16 12:42:01 +02:00
if analytic_derivation == 2
2012-04-27 15:57:58 +02:00
Hess = zeros ( k , k ) ; % Initialization of the Hessian
else
2012-06-08 14:23:18 +02:00
asy_hess = D2a ;
2017-05-16 12:42:01 +02:00
if asy_hess
2012-06-08 14:23:18 +02:00
Hess = zeros ( k , k ) ; % Initialization of the Hessian
else
Hess = [ ] ;
end
2012-04-27 15:57:58 +02:00
end
end
2011-09-19 17:01:24 +02:00
while t < = last
if Zflag
v = Y ( : , t ) - Z * a ;
else
v = Y ( : , t ) - a ( Z ) ;
end
2012-04-27 15:57:58 +02:00
tmp = ( a + K * v ) ;
2017-05-16 12:42:01 +02:00
if analytic_derivation
if analytic_derivation == 2
2018-11-13 17:58:42 +01:00
[ Da , ~ , DLIKt , D2a , ~ , Hesst ] = computeDLIK ( k , tmp , Z , Zflag , v , T , K , [ ] , iF , Da , DYss , DT , [ ] , [ ] , [ ] , notsteady , D2a , D2Yss , D2T , [ ] , [ ] ) ;
2012-04-27 15:57:58 +02:00
else
2018-11-13 17:58:42 +01:00
[ Da , ~ , DLIKt , Hesst ] = computeDLIK ( k , tmp , Z , Zflag , v , T , K , [ ] , iF , Da , DYss , DT , [ ] , [ ] , [ ] , notsteady ) ;
2012-04-27 15:57:58 +02:00
end
DLIK = DLIK + DLIKt ;
2017-05-16 12:42:01 +02:00
if analytic_derivation == 2 || asy_hess
2012-04-27 15:57:58 +02:00
Hess = Hess + Hesst ;
end
2012-06-08 14:23:18 +02:00
dlikk ( t - start + 1 , : ) = DLIKt ;
2012-04-27 15:57:58 +02:00
end
a = T * tmp ;
2011-10-24 13:14:20 +02:00
likk ( t - start + 1 ) = transpose ( v ) * iF * v ;
2011-09-19 17:01:24 +02:00
t = t + 1 ;
end
% Adding constant determinant of F (prediction error covariance matrix)
2015-10-13 17:15:01 +02:00
likk = likk + log_dF ;
2011-09-19 17:01:24 +02:00
% Add log-likelihhod constants and divide by two
2011-10-24 13:14:20 +02:00
likk = . 5 * ( likk + pp * log ( 2 * pi ) ) ;
2011-09-19 17:01:24 +02:00
2011-10-24 13:02:41 +02:00
% Sum the observation's densities (minus the likelihood)
2012-04-27 15:57:58 +02:00
LIK = sum ( likk ) ;
2017-05-16 12:42:01 +02:00
if analytic_derivation
2012-06-08 14:23:18 +02:00
dlikk = dlikk / 2 ;
DLIK = DLIK / 2 ;
likk = { likk , dlikk } ;
2012-04-27 15:57:58 +02:00
end
2017-05-16 12:42:01 +02:00
if analytic_derivation == 2 || asy_hess
if asy_hess == 0
2012-06-08 14:23:18 +02:00
Hess = Hess + tril ( Hess , - 1 ) ' ;
end
Hess = - Hess / 2 ;
LIK = { LIK , DLIK , Hess } ;
2017-05-16 12:42:01 +02:00
elseif analytic_derivation == 1
2012-04-27 15:57:58 +02:00
LIK = { LIK , DLIK } ;
end