Home > matlab > kalman > likelihood > univariate_kalman_filter_ss.m

univariate_kalman_filter_ss

PURPOSE ^

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

SYNOPSIS ^

function [LIK,likk,a] = univariate_kalman_filter_ss(Y,start,last,a,P,kalman_tol,T,H,Z,pp,Zflag,analytic_derivation,Da,DT,DYss,DP,DH,D2a,D2T,D2Yss,D2P)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [LIK,likk,a] = univariate_kalman_filter_ss(Y,start,last,a,P,kalman_tol,T,H,Z,pp,Zflag,analytic_derivation,Da,DT,DYss,DP,DH,D2a,D2T,D2Yss,D2P)
0002 % Computes the likelihood of a stationnary state space model (steady state univariate kalman filter).
0003 
0004 %@info:
0005 %! @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})
0006 %! @anchor{univariate_kalman_filter_ss}
0007 %! @sp 1
0008 %! Computes the likelihood of a stationary state space model, given initial condition for the states (mean and variance).
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 (@var{mm}*1) of doubles, initial mean of the state vector.
0021 %! @item P
0022 %! Matrix (@var{mm}*@var{mm}) of doubles, steady state covariance matrix of the state vector.
0023 %! @item kalman_tol
0024 %! Double scalar, tolerance parameter (rcond, inversibility of the covariance matrix of the prediction errors).
0025 %! @item T
0026 %! Matrix (@var{mm}*@var{mm}) of doubles, transition matrix of the state equation.
0027 %! @item H
0028 %! Vector (@var{pp}) of doubles, diagonal of covariance matrix of the measurement errors (corelation among measurement errors is handled by a model transformation).
0029 %! Matrix (@var{pp}*@var{pp}) of doubles, covariance matrix of the measurement errors (if no measurement errors set H as a zero scalar).
0030 %! @item Z
0031 %! 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}).
0032 %! @item pp
0033 %! Integer scalar, number of observed variables.
0034 %! @item Zflag
0035 %! 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.
0036 %! @end table
0037 %! @sp 2
0038 %! @strong{Outputs}
0039 %! @sp 1
0040 %! @table @ @var
0041 %! @item LIK
0042 %! Double scalar, value of (minus) the likelihood.
0043 %! @item likk
0044 %! Column vector of doubles, values of the density of each observation.
0045 %! @item a
0046 %! Vector (@var{mm}*1) of doubles, mean of the state vector at the end of the (sub)sample.
0047 %! @end table
0048 %! @sp 2
0049 %! @strong{This function is called by:}
0050 %! @sp 1
0051 %! @ref{univariate_kalman_filter}
0052 %! @sp 2
0053 %! @strong{This function calls:}
0054 %! @sp 1
0055 %! @end deftypefn
0056 %@eod:
0057 
0058 % Copyright (C) 2011 Dynare Team
0059 %
0060 % This file is part of Dynare.
0061 %
0062 % Dynare is free software: you can redistribute it and/or modify
0063 % it under the terms of the GNU General Public License as published by
0064 % the Free Software Foundation, either version 3 of the License, or
0065 % (at your option) any later version.
0066 %
0067 % Dynare is distributed in the hope that it will be useful,
0068 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0069 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0070 % GNU General Public License for more details.
0071 %
0072 % You should have received a copy of the GNU General Public License
0073 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0074 
0075 % AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
0076 
0077 % Get sample size.
0078 smpl = last-start+1;
0079 
0080 % Initialize some variables.
0081 t    = start;              % Initialization of the time index.
0082 likk = zeros(smpl,1);      % Initialization of the vector gathering the densities.
0083 LIK  = Inf;                % Default value of the log likelihood.
0084 l2pi = log(2*pi);
0085 
0086 if nargin<12
0087     analytic_derivation = 0;
0088 end
0089 
0090 if  analytic_derivation == 0,
0091     DLIK=[];
0092     Hess=[];
0093 else
0094     k = size(DT,3);                                 % number of structural parameters
0095     DLIK  = zeros(k,1);                             % Initialization of the score.
0096     if analytic_derivation==2,
0097         Hess  = zeros(k,k);                             % Initialization of the Hessian
0098     else
0099         Hess=[];
0100     end
0101 end
0102 
0103 % Steady state kalman filter.
0104 while t<=last
0105     s  = t-start+1;
0106     PP = P;
0107     if analytic_derivation,
0108         DPP = DP;
0109         if analytic_derivation==2,
0110             D2PP = D2P;
0111         end
0112     end
0113     for i=1:pp
0114         if Zflag
0115             prediction_error = Y(i,t) - Z(i,:)*a;
0116             PPZ = PP*Z(i,:)';
0117             Fi = Z(i,:)*PPZ + H(i);
0118         else
0119             prediction_error = Y(i,t) - a(Z(i));
0120             PPZ = PP(:,Z(i));
0121             Fi = PPZ(Z(i)) + H(i);
0122         end
0123         if Fi>kalman_tol
0124             Ki = PPZ/Fi;
0125             a  = a + Ki*prediction_error;
0126             PP = PP - PPZ*Ki';
0127             likk(s) = likk(s) + log(Fi) + prediction_error*prediction_error/Fi + l2pi;
0128             if analytic_derivation,
0129                 if analytic_derivation==2,
0130                     [Da,DPP,DLIKt,D2a,D2PP, Hesst] = univariate_computeDLIK(k,i,Z(i,:),Zflag,prediction_error,Ki,PPZ,Fi,Da,DYss,DPP,DH(i,:),0,D2a,D2Yss,D2PP);
0131                 else
0132                     [Da,DPP,DLIKt] = univariate_computeDLIK(k,i,Z(i,:),Zflag,prediction_error,Ki,PPZ,Fi,Da,DYss,DPP,DH(i,:),0);
0133                 end
0134                 DLIK = DLIK + DLIKt;
0135                 if analytic_derivation==2,
0136                     Hess = Hess + Hesst;
0137                 end
0138             end
0139         end
0140     end
0141     if analytic_derivation,        
0142         if analytic_derivation==2,
0143             [Da,junk,D2a] = univariate_computeDstate(k,a,P,T,Da,DP,DT,[],0,D2a,D2P,D2T);
0144         else
0145             Da = univariate_computeDstate(k,a,P,T,Da,DP,DT,[],0);
0146         end
0147     end
0148     a = T*a;
0149     t = t+1;
0150 end
0151 
0152 likk = .5*likk;
0153 
0154 LIK = sum(likk);
0155 if analytic_derivation==2,
0156     LIK={LIK,DLIK,Hess};
0157 end
0158 if analytic_derivation==1,
0159     LIK={LIK,DLIK};
0160 end

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005