2011-10-24 17:16:20 +02:00
function [LIK,likk,a] = univariate_kalman_filter_ss ( Y,start,last,a,P,kalman_tol,T,H,Z,pp,Zflag)
2011-09-19 17:01:24 +02:00
% Computes the likelihood of a stationnary state space model (steady state univariate kalman filter).
2011-10-24 17:16:20 +02:00
%@info:
%! @deftypefn {Function File} {[@var{LIK},@var{likk},@var{a} ] =} univariate_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})
%! @anchor{univariate_kalman_filter_ss}
%! @sp 1
%! Computes the likelihood of a stationary state space model, given initial condition for the states (mean and variance).
%! @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
%! Vector (@var{mm}*1) of doubles, initial mean of the state vector.
%! @item P
%! Matrix (@var{mm}*@var{mm}) of doubles, steady state covariance matrix of the state vector.
%! @item kalman_tol
%! Double scalar, tolerance parameter (rcond, inversibility of the covariance matrix of the prediction errors).
%! @item T
%! Matrix (@var{mm}*@var{mm}) of doubles, transition matrix of the state equation.
%! @item H
2011-11-02 11:10:58 +01:00
%! Vector (@var{pp}) of doubles, diagonal of covariance matrix of the measurement errors (corelation among measurement errors is handled by a model transformation).
2011-10-24 17:16:20 +02:00
%! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
%! @item Z
%! Matrix (@var{pp}*@var{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
%! Vector (@var{mm}*1) of doubles, mean of the state vector at the end of the (sub)sample.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 1
%! @ref{univariate_kalman_filter}
%! @sp 2
%! @strong{This function calls:}
%! @sp 1
%! @end deftypefn
%@eod:
2011-09-19 17:01:24 +02:00
% Copyright (C) 2011 Dynare Team
2011-10-24 17:16:20 +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
2011-10-24 17:16:20 +02:00
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
2011-09-19 17:01:24 +02:00
2011-10-24 17:16:20 +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 17:16: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.
l2pi = log ( 2 * pi ) ;
% Steady state kalman filter.
while t < = last
s = t - start + 1 ;
PP = P ;
for i = 1 : pp
if Zflag
prediction_error = Y ( i , t ) - Z ( i , : ) * a ;
2011-11-02 11:10:58 +01:00
Fi = Z ( i , : ) * PP * Z ( i , : ) ' + H ( i ) ;
2011-09-19 17:01:24 +02:00
else
prediction_error = Y ( i , t ) - a ( Z ( i ) ) ;
2011-11-02 11:10:58 +01:00
Fi = PP ( Z ( i ) , Z ( i ) ) + H ( i ) ;
2011-09-19 17:01:24 +02:00
end
if Fi > kalman_tol
if Zflag
2011-10-24 17:16:20 +02:00
Ki = ( PP * Z ( i , : ) ) ' / Fi ;
2011-09-19 17:01:24 +02:00
else
Ki = PP ( : , Z ( i ) ) / Fi ;
end
a = a + Ki * prediction_error ;
PP = PP - ( Fi * Ki ) * transpose ( Ki ) ;
2011-10-24 17:16:20 +02:00
likk ( s ) = likk ( s ) + log ( Fi ) + prediction_error * prediction_error / Fi + l2pi ;
2011-09-19 17:01:24 +02:00
end
end
a = T * a ;
t = t + 1 ;
end
2011-10-24 17:16:20 +02:00
likk = . 5 * likk ;
2011-09-19 17:01:24 +02:00
2011-10-24 17:16:20 +02:00
LIK = sum ( likk ) ;