Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/fn_kalfil_tv2.m

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