| 1 |
function [loglh,zt_tm1,Pt_tm1, loglh_t_allvalues] = fn_kalfil_tv(Y_T, a, H, R, G, b, F, V, indxIni,z0,P0) |
| 2 |
%[loglh,zt_tm1,Pt_tm1] = fn_kalfil_tv(Y_T, a, H, R, G, b, F, V, indxIni,z0,P0) |
| 3 |
%Time-varying Kalman filter (conditional on all the regimes in a Markov-switching model). It computes |
| 4 |
% a sequence of one-step predictions and their covariance matrices, and the log likelihood. |
| 5 |
% The function uses a forward recursion algorithm. |
| 6 |
% |
| 7 |
% Note: for b, F, V, zt_tm1, and Pt_tm1, there is not need for the T+1 length because the values at T+1 have |
| 8 |
% never been used in the likelihood function. See tz_kalfiltv() in kalman.c for an illustration. |
| 9 |
% |
| 10 |
% State space model is defined as follows: |
| 11 |
% y(t) = a(t) + H(t)*z(t) + eps(t) (observation or measurement equation) |
| 12 |
% z(t) = b(t) + F(t)*z(t) + eta(t) (state or transition equation) |
| 13 |
% where a(t), H(t), b(t), and F(t) depend on s_t that follows a Markov-chain process and are taken as given. |
| 14 |
% |
| 15 |
% Inputs are as follows: |
| 16 |
% Y_T is a n_y-by-T matrix containing data [y(1), ... , y(T)]. |
| 17 |
% a is an n_y-by-T matrix of time-varying input vectors in the measurement equation. |
| 18 |
% H is an n_y-by-n_z-by-T 3-D of time-varying matrices in the measurement equation. |
| 19 |
% R is an n_y-by-n_y-by-T 3-D of time-varying covariance matrices for the error in the measurement equation. |
| 20 |
% G is an n_z-by-n_y-by-T 3-D of time-varying E(eta_t * eps_t'). |
| 21 |
% ------ |
| 22 |
% b is an n_z-by-(T+1) matrix of time-varying input vectors in the state equation with b(:,1) as an initial condition. |
| 23 |
% F is an n_z-by-n_z-by-(T+1) 3-D of time-varying transition matrices in the state equation with F(:,:,1) as an initial condition. |
| 24 |
% V is an n_z-by-n_z-by-(T+1) 3-D of time-varying covariance matrices for the error in the state equation with V(:,:,1) as an initial condition. |
| 25 |
% ------ |
| 26 |
% indxIni: 1: using the initial condition with zt_tm1(:,1)=z0 and Pt_tm1(:,:,1)=P0; |
| 27 |
% 0: using the unconditional mean for any given regime at time 0. |
| 28 |
% z0 is an n_z-by-1 vector of initial condition when indxIni=1. (Do not enter if indxIni=0.) |
| 29 |
% P0 is an n_z-by-n_z matrix of initial condition when indxIni=1. (Do not enter if indxIni=0.) |
| 30 |
% |
| 31 |
% Outputs are as follows: |
| 32 |
% loglh is a value of the log likelihood function of the state-space model |
| 33 |
% under the assumption that errors are multivariate Gaussian. |
| 34 |
% zt_tm1 is an n_z-by-(T+1) matrices of one-step predicted state vectors with z0_0m1 as a initial condition. |
| 35 |
% Pt_tm1 is an n_z-by-n_z-by-(T+1) 3-D of covariance matrices of zt_tm1 with P0_0m1 as a initial condition. |
| 36 |
% loglh_t_allvalues is a T-by-1 vector of loglh at time t for t=1:T. |
| 37 |
% |
| 38 |
% The initial state vector and its covariance matrix are computed under the bounded (stationary) condition: |
| 39 |
% z0_0m1 = (I-F(:,:,1))\b(:,1) |
| 40 |
% vec(P0_0m1) = (I-kron(F(:,:,1),F(:,:,1)))\vec(V(:,:,1)) |
| 41 |
% Note that all eigenvalues of the matrix F(:,:,1) are inside the unit circle when the state-space model is bounded (stationary). |
| 42 |
% |
| 43 |
% March 2007, written by Tao Zha |
| 44 |
% See Hamilton's book ([13.2.13] -- [13.2.22]), Harvey (pp.100-106), and LiuWZ Model I NOTES pp.001-003. |
| 45 |
%========================================================================== |
| 46 |
% Revision history: |
| 47 |
% |
| 48 |
% |
| 49 |
% |
| 50 |
%========================================================================== |
| 51 |
% |
| 52 |
% Copyright (C) 1997-2012 Tao Zha |
| 53 |
% |
| 54 |
% This free software: you can redistribute it and/or modify |
| 55 |
% it under the terms of the GNU General Public License as published by |
| 56 |
% the Free Software Foundation, either version 3 of the License, or |
| 57 |
% (at your option) any later version. |
| 58 |
% |
| 59 |
% It is distributed in the hope that it will be useful, |
| 60 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 61 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 62 |
% GNU General Public License for more details. |
| 63 |
% |
| 64 |
% If you did not received a copy of the GNU General Public License |
| 65 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 66 |
% |
| 67 |
|
| 68 |
|
| 69 |
Tp1 = size(Y_T,2) + 1; %T+1; |
| 70 |
n_y = size(a,1); |
| 71 |
n_z = size(b,1); |
| 72 |
%--- Checking input matrix dimensions |
| 73 |
if (size(Y_T,1)~=n_y) |
| 74 |
error('kalf_tv(): Y_T and a must have the same number of rows') |
| 75 |
end |
| 76 |
%--- Allocating memory. |
| 77 |
zt_tm1 = zeros(n_z,Tp1); |
| 78 |
Pt_tm1 = zeros(n_z,n_z,Tp1); |
| 79 |
loglh_t_allvalues = zeros(Tp1-1,1); |
| 80 |
%--- Initializing. |
| 81 |
loglh = 0.0; |
| 82 |
if (indxIni) |
| 83 |
zt_tm1(:,1) = z0; |
| 84 |
Pt_tm1(:,:,1) = P0; |
| 85 |
else |
| 86 |
eigmax4F = max(abs(eig(F(:,:,1)))); |
| 87 |
format long e |
| 88 |
eigmax4F |
| 89 |
if (eigmax4F < 1.0) |
| 90 |
zt_tm1(:,1) = (eye(n_z)-F(:,:,1))\b(:,1); |
| 91 |
V1 = V(:,:,1); |
| 92 |
Pt_tm1(:,:,1) = reshape((eye(n_z^2)-kron(F(:,:,1),F(:,:,1)))\V1(:) ,n_z,n_z); |
| 93 |
else |
| 94 |
%--- Do NOT use the following option. It turns out that this will often generate explosive conditional liklihood |
| 95 |
%--- at the end of the sample, because Pt_tm1 shrinks to zero overtime due to the sigularity of the initila condition P_{1|0}. |
| 96 |
% zt_tm1(:,1) = zeros(size(zt_tm1(:,1))); |
| 97 |
% Pt_tm1(:,:,1) = V(:,:,1); %+eye(size(V(:,:,1))); |
| 98 |
|
| 99 |
nearinfinity = -1.0e+300 |
| 100 |
loglh = nearinfinity; |
| 101 |
return; %Eearly exit. |
| 102 |
%error('kalf_tv(): For non-stationary solutions, the initial conditions must be supplied by, say, input arguments') |
| 103 |
end |
| 104 |
end |
| 105 |
|
| 106 |
|
| 107 |
%====== See p.002 in LiuWZ. ====== |
| 108 |
indx_badlh = 0; %1: bad likelihood with, say, -infinity of the LH value. |
| 109 |
for t=2:Tp1 |
| 110 |
tdata = t-1; |
| 111 |
|
| 112 |
%--- Setup. |
| 113 |
Htdata = H(:,:,tdata); |
| 114 |
Htdatatran = Htdata'; |
| 115 |
ztdata = zt_tm1(:,tdata); |
| 116 |
Ptdata = Pt_tm1(:,:,tdata); |
| 117 |
PHtran_tdata = Ptdata*Htdatatran; |
| 118 |
Ft = F(:,:,t); |
| 119 |
Fttran = Ft'; |
| 120 |
|
| 121 |
%--- Data. |
| 122 |
etdata = Y_T(:,tdata) - a(:,tdata) - Htdata*ztdata; |
| 123 |
etdatatran = etdata'; |
| 124 |
Dtdata = Htdata*PHtran_tdata + R(:,:,tdata); |
| 125 |
Dtdata = 0.5*(Dtdata + Dtdata'); %Making it symmetric against some rounding errors. |
| 126 |
%This making-symmetric is very IMPORTANT; otherwise, we will get the matrix being singular message |
| 127 |
% and eigenvalues being negative for the SPD matrix, etc. Then the likelihood becomes either |
| 128 |
% a bad number or a complex number. |
| 129 |
|
| 130 |
%--- State (updating). |
| 131 |
Kt_tdata = (Ft*PHtran_tdata+G(:,:,tdata))/Dtdata; |
| 132 |
Kt_tdatatran = Kt_tdata'; |
| 133 |
zt_tm1(:,t) = b(:,t) + Ft*zt_tm1(:,tdata) + Kt_tdata*etdata; |
| 134 |
Pt_tm1(:,:,t) = Ft*Ptdata*Fttran - Kt_tdata*Dtdata*Kt_tdatatran + V(:,:,t); |
| 135 |
|
| 136 |
%--- Forming the log likelihood. |
| 137 |
detDtdata = det(Dtdata); |
| 138 |
%if (~isfinite(detDtdata)) |
| 139 |
if (detDtdata < realmin) |
| 140 |
indx_badlh = 1; |
| 141 |
break; |
| 142 |
else |
| 143 |
loglh_tdata = -(0.5*n_y)*log(2*pi) - 0.5*log(detDtdata) - 0.5*(etdatatran/Dtdata)*etdata; |
| 144 |
loglh = loglh + loglh_tdata; |
| 145 |
loglh_t_allvalues(tdata) = loglh_tdata; |
| 146 |
end |
| 147 |
end |
| 148 |
|
| 149 |
if (indx_badlh) |
| 150 |
nearinfinity = -1.0e+300; |
| 151 |
loglh = nearinfinity; |
| 152 |
loglh_t_allvalues(tdata) = nearinfinity; |
| 153 |
end |
| 154 |
|