


Computes the likelihood of a stationnary state space model (steady state kalman filter).


0001 function [LIK, likk, a] = kalman_filter_ss(Y,start,last,a,T,K,iF,dF,Z,pp,Zflag,analytic_derivation,Da,DT,DYss,D2a,D2T,D2Yss) 0002 % Computes the likelihood of a stationnary state space model (steady state kalman filter). 0003 0004 %@info: 0005 %! @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}) 0006 %! @anchor{kalman_filter} 0007 %! @sp 1 0008 %! 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. 0009 %! @sp 2 0010 %! @strong{Inputs} 0011 %! @sp 1 0012 %! @table @ @var 0013 %! @item Y 0014 %! Matrix (@var{pp}*T) of doubles, data. 0015 %! @item start 0016 %! Integer scalar, first period. 0017 %! @item last 0018 %! Integer scalar, last period (@var{last}-@var{first} has to be inferior to T). 0019 %! @item a 0020 %! Vector (mm*1) of doubles, initial mean of the state vector. 0021 %! @item T 0022 %! Matrix (mm*mm) of doubles, transition matrix of the state equation. 0023 %! @item K 0024 %! Matrix (mm*@var{pp}) of doubles, steady state kalman gain. 0025 %! @item iF 0026 %! Matrix (@var{pp}*@var{pp}) of doubles, inverse of the steady state covariance matrix of the prediction errors. 0027 %! @item dF 0028 %! Double scalar, determinant of the steady state covariance matrix of teh prediction errors. 0029 %! @item Z 0030 %! 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}). 0031 %! @item pp 0032 %! Integer scalar, number of observed variables. 0033 %! @item Zflag 0034 %! 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. 0035 %! @end table 0036 %! @sp 2 0037 %! @strong{Outputs} 0038 %! @sp 1 0039 %! @table @ @var 0040 %! @item LIK 0041 %! Double scalar, value of (minus) the likelihood. 0042 %! @item likk 0043 %! Column vector of doubles, values of the density of each observation. 0044 %! @item a 0045 %! Vector (mm*1) of doubles, mean of the state vector at the end of the (sub)sample. 0046 %! @end table 0047 %! @sp 2 0048 %! @strong{This function is called by:} 0049 %! @sp 1 0050 %! @ref{kalman_filter} 0051 %! @sp 2 0052 %! @strong{This function calls:} 0053 %! @sp 1 0054 %! @end deftypefn 0055 %@eod: 0056 0057 % Copyright (C) 2011 Dynare Team 0058 % 0059 % This file is part of Dynare. 0060 % 0061 % Dynare is free software: you can redistribute it and/or modify 0062 % it under the terms of the GNU General Public License as published by 0063 % the Free Software Foundation, either version 3 of the License, or 0064 % (at your option) any later version. 0065 % 0066 % Dynare is distributed in the hope that it will be useful, 0067 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0068 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0069 % GNU General Public License for more details. 0070 % 0071 % You should have received a copy of the GNU General Public License 0072 % along with Dynare. If not, see <http://www.gnu.org/licenses/>. 0073 0074 % AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr 0075 0076 % Get sample size. 0077 smpl = last-start+1; 0078 0079 % Initialize some variables. 0080 t = start; % Initialization of the time index. 0081 likk = zeros(smpl,1); % Initialization of the vector gathering the densities. 0082 LIK = Inf; % Default value of the log likelihood. 0083 notsteady = 0; 0084 if nargin<12 0085 analytic_derivation = 0; 0086 end 0087 0088 if analytic_derivation == 0, 0089 DLIK=[]; 0090 Hess=[]; 0091 else 0092 k = size(DT,3); % number of structural parameters 0093 DLIK = zeros(k,1); % Initialization of the score. 0094 if analytic_derivation==2, 0095 Hess = zeros(k,k); % Initialization of the Hessian 0096 else 0097 Hess=[]; 0098 end 0099 end 0100 0101 while t <= last 0102 if Zflag 0103 v = Y(:,t)-Z*a; 0104 else 0105 v = Y(:,t)-a(Z); 0106 end 0107 tmp = (a+K*v); 0108 if analytic_derivation, 0109 if analytic_derivation==2, 0110 [Da,junk,DLIKt,D2a,junk2, Hesst] = computeDLIK(k,tmp,Z,Zflag,v,T,K,[],iF,Da,DYss,DT,[],[],[],notsteady,D2a,D2Yss,D2T,[],[]); 0111 else 0112 [Da,junk,DLIKt] = computeDLIK(k,tmp,Z,Zflag,v,T,K,[],iF,Da,DYss,DT,[],[],[],notsteady); 0113 end 0114 DLIK = DLIK + DLIKt; 0115 if analytic_derivation==2, 0116 Hess = Hess + Hesst; 0117 end 0118 end 0119 a = T*tmp; 0120 likk(t-start+1) = transpose(v)*iF*v; 0121 t = t+1; 0122 end 0123 0124 % Adding constant determinant of F (prediction error covariance matrix) 0125 likk = likk + log(dF); 0126 0127 % Add log-likelihhod constants and divide by two 0128 likk = .5*(likk + pp*log(2*pi)); 0129 0130 % Sum the observation's densities (minus the likelihood) 0131 LIK = sum(likk); 0132 if analytic_derivation==2, 0133 LIK={LIK,DLIK,Hess}; 0134 end 0135 if analytic_derivation==1, 0136 LIK={LIK,DLIK}; 0137 end