| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#ifndef __MARKOV_SWITCHING_STATE_SPACE_MODEL__ |
| 19 |
#define __MARKOV_SWITCHING_STATE_SPACE_MODEL__ |
| 20 |
|
| 21 |
#ifdef __cplusplus |
| 22 |
#include "MarkovSwitchingModel.hh" |
| 23 |
extern "C" { |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "dw_switch.h" |
| 27 |
#include "dw_switchio.h" |
| 28 |
#include "dw_matrix.h" |
| 29 |
|
| 30 |
/* Impulse response flags */ |
| 31 |
#define IR_STATES 0x0001 |
| 32 |
#define IR_OBSERVABLES 0x0002 |
| 33 |
|
| 34 |
#ifndef __cplusplus |
| 35 |
typedef struct MakovSwitchingModel MarkovSwitchingModel; |
| 36 |
typedef struct GensysForm_tag GensysForm; |
| 37 |
#endif |
| 38 |
|
| 39 |
typedef struct T_MSStateSpace_tag |
| 40 |
{ |
| 41 |
int ny, |
| 42 |
nz, |
| 43 |
nu, |
| 44 |
nepsilon, |
| 45 |
nobs, |
| 46 |
nstates, // nstates = nbasestates^(nlags_encoded+1) |
| 47 |
nbasestates, // h is equal to nbasestates in the comments below |
| 48 |
nlags_encoded, // rho is equal to nlag_encoded in the comments below |
| 49 |
zeta_modulus; // zeta_modulus = nbasestates^nlags_encoded |
| 50 |
|
| 51 |
// Data |
| 52 |
TVector *y; // y[t] must be defined for 1 <= t <= nobs |
| 53 |
|
| 54 |
// Reduced form parameters |
| 55 |
TVector *a; |
| 56 |
TMatrix *H; |
| 57 |
TVector *b; |
| 58 |
TMatrix *F; |
| 59 |
TMatrix *R; |
| 60 |
TMatrix *V; |
| 61 |
TMatrix *G; |
| 62 |
TMatrix *Phiy; |
| 63 |
TMatrix *Phiz; |
| 64 |
|
| 65 |
int NonZeroG; |
| 66 |
|
| 67 |
// Filtering |
| 68 |
int t0; // Kalman forward recursion updated through t0 |
| 69 |
|
| 70 |
TVector **Ez; // Ez[t][k] ~ E[z(t) | Y(t), xi(t)=k, theta, q] |
| 71 |
|
| 72 |
TMatrix **Ezz; // Ezz[t][k] ~ E[(z(t) - Ez(t,xi(t)))(z(t) - Ez(t,xi(t)))' | Y(t), xi(t)=k, theta, q] |
| 73 |
|
| 74 |
TVector **IEz; // IEz[t][j] ~ E[z(t) | Y(t), zeta(t)=j, theta, q] |
| 75 |
|
| 76 |
TMatrix **IEzz; // IEzz[t][j] ~ E[(z(t) - Ez(t,zeta(t)))(z(t) - Ez(t,zeta(t)))' | Y(t), zeta(t)=j, theta, q] |
| 77 |
|
| 78 |
TVector **Ez1; // Ez1[t][k] ~ E[z(t) | Y(t-1), xi(t)=k, theta, q] |
| 79 |
|
| 80 |
TMatrix **Ezz1; // Ezz1[t][k] ~ E[(z(t) - Ez1(t,xi(t)))(z(t) - Ez1(t,xi(t)))' | Y(t-1), xi(t)=k, theta, q] |
| 81 |
|
| 82 |
TVector **Ey1; // Ey1[t][k] ~ E[y(t) | Y(t-1), xi(t)=k, theta, q] |
| 83 |
|
| 84 |
TMatrix **Eyy1; // Eyy1[t][k] ~ E[(y(t) - Ey1(t,xi(t)))(y(t) - Ey1(t,xi(t)))' | Y(t-1), xi(t)=k, theta, q] |
| 85 |
|
| 86 |
TVector **ey1; // ey1[t][k] ~ y(t) - E[y(t) | Y(t-1), xi(t)=k, theta, q] |
| 87 |
|
| 88 |
TVector *L; // L[t](k) ~ P[y(t) | Y(t-1), xi(t)=k, theta, q] |
| 89 |
|
| 90 |
// Smoothing |
| 91 |
int smoothed; // non-zero if Kalman smoothing completed |
| 92 |
|
| 93 |
TVector **SEz; // SEz[t][k] ~ E[z(t) | Y(T), xi(t+1)=k, theta, q] for 1 <= t < nobs |
| 94 |
TVector **ISEz; // ISEz[t][j] ~ E[z(t) | Y(T), zeta(t)=j, theta, q] for 1 <= t <= nobs |
| 95 |
|
| 96 |
// Tao's data structure |
| 97 |
TStateModel *TZstatemodel; |
| 98 |
|
| 99 |
// Jake's Markov Model |
| 100 |
void *MSModel; |
| 101 |
void *gensys_struct; |
| 102 |
|
| 103 |
} T_MSStateSpace; |
| 104 |
|
| 105 |
T_MSStateSpace* Create_MSStateSpace(int ny, int nz, int nu, int nepsilon, int nbasestates, int nlags_encoded, int nobs); |
| 106 |
T_MSStateSpace* Create_MSStateSpaceFromStateModel(TStateModel *TZstatemodel, int nlags_encoded); |
| 107 |
void Free_MSStateSpace(T_MSStateSpace *statespace); |
| 108 |
ThetaRoutines* CreateThetaRoutines_MSStateSpace(void); |
| 109 |
|
| 110 |
TStateModel* CreateStateModel_MSStateSpace(TStateModel *basemodel, int nlags_encoded); |
| 111 |
|
| 112 |
char* ModelType_MSStateSpace(void); |
| 113 |
double LogPrior_MSStateSpace(TStateModel *model); |
| 114 |
void StatesChanged_MSStateSpace(TStateModel *model); |
| 115 |
void ThetaChanged_MSStateSpace(TStateModel *model); |
| 116 |
void TransitionMatrixParametersChanged_MSStateSpace(TStateModel *model); |
| 117 |
void Invalidate_MSStateSpace(T_MSStateSpace *statespace); |
| 118 |
int Initialize_MSStateSpace(TStateModel *model); |
| 119 |
void Invalidate_MSStateSpace(T_MSStateSpace *statespace); |
| 120 |
int NumberFreeParametersTheta_MSStateSpace(TStateModel *model); |
| 121 |
void ConvertFreeParametersToTheta_MSStateSpace(TStateModel *model, PRECISION *buffer); |
| 122 |
void ConvertThetaToFreeParameters_MSStateSpace(TStateModel *model, PRECISION *buffer); |
| 123 |
|
| 124 |
PRECISION LogConditionalLikelihood_MSStateSpace(int s, int t, TStateModel *model); |
| 125 |
|
| 126 |
TVector pEyt_MSStateSpace(TVector y, int T, TVector *data, int s, int t, struct TStateModel_tag *model); |
| 127 |
|
| 128 |
// Kalman Filtering |
| 129 |
int Filter(int T, TStateModel *model); |
| 130 |
int ConditionalFilter(int t0, int t1, PRECISION ***y, TVector *Ez_i, TMatrix *Ezz_i, TVector Pxi_i, TMatrix Q, |
| 131 |
TVector *Pxi, TVector *Pxi1, TVector **IEz, TMatrix **IEzz, TVector **Ez1, TMatrix **Ezz1, |
| 132 |
TVector **Ez, TMatrix **Ezz, T_MSStateSpace *statespace); |
| 133 |
|
| 134 |
int Smooth_MSStateSpace(TStateModel *model); |
| 135 |
int SmoothProbabilities_MSStateSpace(int t0, int t1, TVector *SPxi, TVector *Pxi, TVector *Pxi1, TMatrix Q); |
| 136 |
void SmoothMean_MSStateSpace(int t0, int t1, TVector **SEz, TVector **ISEz, TVector **Ez1, TMatrix **Ezz1, |
| 137 |
TVector **IEz, TMatrix **IEzz, TVector *SPxi, T_MSStateSpace *statespace); |
| 138 |
TVector** ComputeFundamentalShocks(TStateModel *model, TVector **Ez, TVector **Ez1, int a); |
| 139 |
TVector** ComputeMeasurmentErrors(TStateModel *model, TVector **Ez, int a); |
| 140 |
|
| 141 |
int WriteFilteredProbabilities(FILE *f, char *filename, TStateModel *model); |
| 142 |
|
| 143 |
TMatrix Forecast_MSStateSpace(TMatrix forecast, int t0, int data_periods, int forecast_periods, TVector *shocks, TStateModel *model); |
| 144 |
TVector* CummulativeHistoricalDecomposition(int t0, TStateModel *model); |
| 145 |
TVector* CummulativeHistoricalDecompositionStates(int t0, TStateModel *model); |
| 146 |
TVector* Check_HistoricalDecompositon_Shocks(TVector *dev, TStateModel *model); |
| 147 |
|
| 148 |
int dw_state_space_variance_decomposition(TMatrix VD, int *S, TStateModel *model, int flag); |
| 149 |
int dw_state_space_impulse_response(TMatrix IR, int *S, TStateModel *model, int flag); |
| 150 |
|
| 151 |
// Checks |
| 152 |
void Check_FilteredInfo(TStateModel *model, PRECISION tol); |
| 153 |
void Check_SmoothedInfo(TStateModel *model, PRECISION tol); |
| 154 |
|
| 155 |
// Routines to be written |
| 156 |
int Filter_ConditionalRegimes(int T, int *S, TStateModel *model); |
| 157 |
|
| 158 |
// Jakes Routines |
| 159 |
void draw_free_parameters_from_prior_markov_model(void* msm, double **buffer); |
| 160 |
double get_log_prior_from_markov_model(void* msm); |
| 161 |
void states_changed_in_markov_model(void* msm); |
| 162 |
void theta_changed_in_markov_model(void* msm); |
| 163 |
void transition_matrix_parameters_changed_im_markov_model(void *msm); |
| 164 |
void invalidate_markov_model(void *msm); |
| 165 |
int number_free_parameters_theta_markov_model(void *msm); |
| 166 |
void convert_free_parameter_to_theta_markov_model(void *msm, double *buffer); |
| 167 |
void convert_theta_to_free_parameters_markov_model(void *msm, double *buffer); |
| 168 |
|
| 169 |
#ifdef __cplusplus |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
#endif |
| 174 |
|
| 175 |
/******************************************************************************** |
| 176 |
State space form: |
| 177 |
|
| 178 |
y(t) = a(s(t)) + H(s(t))*z(t) + Phiy(s(t))*u(t) (measurement equation) |
| 179 |
z(t) = b(s(t)) + F(s(t))*z(t-1) + Phiz(s(t))*epsilon(t) (state equation) |
| 180 |
|
| 181 |
|
| 182 |
uhat(t) = Phiy(s(t))*u(t) |
| 183 |
epsilonhat(t) = Phiz(s(t))*epsilon(t) |
| 184 |
E[u(t)u(t)'] = I ==> E[uhat(t)uhat(t)'] = R(s(t)) = Phiy(s(t))*Phiy'(s(t)) |
| 185 |
E[epsilon(t)epsilon(t)'] = I ==> E[epsilonhat(t)epsilonhat(t)'] = V(s(t)) = Phiz(s(t))*Phiz'(s(t)) |
| 186 |
E[epsilonhat(t)uhat(t)'] = Phiz(s(t))*E[epsilon(t)uhat(t)']*Phiy(s(t))' = G(s(t)) |
| 187 |
u(t) and epsilon(t) are independent of s(t) |
| 188 |
|
| 189 |
y(t) : ny vector |
| 190 |
u(t) : ny vector |
| 191 |
z(t) : nz vector |
| 192 |
epsilon(t) : nz vector |
| 193 |
a(i) : ny vector |
| 194 |
H(i) : ny x nz matrix |
| 195 |
b(i) : nz vector |
| 196 |
F(i) : nz x nz matrix |
| 197 |
R(i) : ny x ny matrix |
| 198 |
V(i) : nz x nz matrix |
| 199 |
G(i) : nz x ny matrix |
| 200 |
Phiy(i) : ny x nu matrix |
| 201 |
Phiz(i) : nz x nepsilon matrix |
| 202 |
|
| 203 |
0 <= i < h |
| 204 |
|
| 205 |
|
| 206 |
Fundemental relation: |
| 207 |
|
| 208 |
x1 mu1 sigma11 sigma12 |
| 209 |
if ~ N , |
| 210 |
x2 mu2 sigma21 sigma22 |
| 211 |
|
| 212 |
then x1 | x2 ~ N(m1 + sigma12 inverse(sigma22) (x2 - mu2), sigma11 - sigma12 inverse(sigma22) sigma21) |
| 213 |
|
| 214 |
|
| 215 |
Recursive filtering relations: |
| 216 |
|
| 217 |
zeta(t) = (s(t), ... ,s(t-rho+1)) |
| 218 |
xi(t) = (s(t), ... ,s(t-rho+1),s(t-rho)) = (s(t),zeta(t-1)) = (zeta(t),s(t-rho)) |
| 219 |
|
| 220 |
The index rho >= 0 is fixed. If rho == 0, then zeta(t) is empty. Both xi and zeta |
| 221 |
are encoded as integers. Under this encodeding, |
| 222 |
|
| 223 |
xi(t) = zeta(t)*h + s(t-rho) = s(t)*(h^rho) + zeta(t-1) |
| 224 |
|
| 225 |
Pxi(t,xi(t)) ~= P[xi(t) | Y(t), theta, q] |
| 226 |
|
| 227 |
Ps(t,zeta(t),s(t-rho)) ~= P[s(t-rho) | Y(t), zeta(t), theta, q] |
| 228 |
|
| 229 |
Ez(t,xi(t)) ~= E[z(t) | Y(t), xi(t), theta, q] |
| 230 |
|
| 231 |
Ezz(t,xi(t)) ~= E[(z(t) - Ez(t,xi(t)))(z(t) - Ez(t,xi(t)))' | Y(t), xi(t), theta, q] |
| 232 |
|
| 233 |
IEz(t,zeta(t)) ~= E[z(t) | Y(t), zeta(t), theta, q] |
| 234 |
|
| 235 |
IEzz(t,zeta(t)) ~= E[(z(t) - Ez(t,zeta(t)))(z(t) - Ez(t,zeta(t)))' | Y(t), zeta(t), theta, q] |
| 236 |
|
| 237 |
Ez1(t,xi(t)) ~= E[z(t) | Y(t-1), xi(t), theta, q] |
| 238 |
|
| 239 |
Ezz1(t,xi(t)) ~= E[(z(t) - Ezl(t,xi(t)))(z(t) - Ezl(t,xi(t)))' | Y(t-1), xi(t), theta, q] |
| 240 |
|
| 241 |
Eyl(t,xi(t)) ~= E[y(t) | Y(t-1), xi(t), theta, q] |
| 242 |
|
| 243 |
Eyyl(t,xi(t))) ~= E[(y(t) - Ey(t,xi(t)))(y(t) - Ey(t,xi(t)))' | Y(t-1), xi(t), theta, q] |
| 244 |
|
| 245 |
Filtering approximations: |
| 246 |
|
| 247 |
Ez(t,xi(t)) ~ given |
| 248 |
|
| 249 |
Ezz(t,xi(t)) ~ given |
| 250 |
|
| 251 |
Pxi(t,xi(t)) = ProbabilityStateConditionalCurrent(xi(t),t,model) |
| 252 |
|
| 253 |
Ps(t,(zeta(t),s(t-rho))) = Pxi(t,(zeta(t),s(t-rho)))/Sum{Pxi(t,(zeta(t),i)); 0 <= i < h} |
| 254 |
|
| 255 |
IEz(t,zeta(t)) = Sum{Ps(t,zeta(t),i)*Ez(t,(zeta(t),i)); 0 <= i < h} |
| 256 |
|
| 257 |
IEzz(t,zeta(t)) = Sum{Ps(t,zeta(t),i)*Ezz(t,(zeta(t),i)); 0 <= i < h} |
| 258 |
|
| 259 |
Ezl(t+1,(s(t+1),zeta(t))) = b(s(t+1)) + F(s(t+1))*IEz(t,zeta(t)) |
| 260 |
|
| 261 |
Ezzl(t+1,(s(t+1),zeta(t))) = V(s(t+1)) + F(s(t+1))*IEzz(t,zeta(t))*F(s(t+1))' |
| 262 |
|
| 263 |
Eyl(t+1,(s(t+1),zeta(t))) = a(s(t+1)) + H(s(t+1))*Ez1(t+1,(s(t+1),zeta(t))) |
| 264 |
|
| 265 |
Eyyl(t+1,(s(t+1),zeta(t))) = R(s(t+1)) + H(s(t+1))*Ezz1(t+1,(s(t+1),zeta(t)))*H'(s(t+1)) |
| 266 |
+ H(s(t+1))*G(s(t+1)) + G'(s(t+1))*H'(s(t+1)) |
| 267 |
|
| 268 |
Ez(t+1,(s(t+1),zeta(t))) = Ez1(t+1),(s(t+1),zeta(t))) + (Ezz1(t+1,(s(t+1),zeta(t)))*H'(s(t+1)) + G(s(t+1))) |
| 269 |
*Inv(Eyy1(t+1,(s(t+1),zeta(t))))*(y(t+1) - Ey1(t+1,(s(t+1),zeta(t)))) |
| 270 |
|
| 271 |
Ezz(t+1,(s(t+1),zeta(t))) = Ezz1(t+1,(s(t+1),zeta(t))) - (Ezz1(t+1,(s(t+1),zeta(t)))*H'(s(t+1)) + G(s(t+1))) |
| 272 |
*Inv(Eyy1(t+1,(s(t+1),zeta(t))))*(Ezz1(t+1,(s(t+1),zeta(t)))*H'(s(t+1)) + G(s(t+1)))' |
| 273 |
|
| 274 |
If the values for Ez(t,xi(t)), Ezz(t,xi(t)), and Pxi(t,xi(t)) were exact, then the values for |
| 275 |
Ps(t,zeta(t),s(t-rho)), IEz(t,zeta(t)), and IEzz(t,zeta(t)) could be shown to be exact using |
| 276 |
properties of conditional and marginal probablities. The values of Ezl(t+1,xi(t+1)), |
| 277 |
Ezzl(t+1,xi(t+1)), Eyl(t+1,xi(t+1)), and Eyyl(t+1,xi(t+1)) could be shown to be exact using the |
| 278 |
state and measurement equations. However, the values of Ez(t+1,xi(t+1)) and Ezz(t+1,xi(t+1)) |
| 279 |
would be exact only if the joint distribution of z(t+1) and y(t+1), conditional on Y(t) and |
| 280 |
xi(t+1), were normal. This would follow from the fundemental relation. Furthermore, the |
| 281 |
distribution of z(t+1), conditional on Y(t+1) and xi(t+1) would also be normal. Unfortunately, |
| 282 |
even if the distribution of z(t), conditional on Y(t) and xi(t) were normal, the distribution of |
| 283 |
z(t) conditional on Y(t) and zeta(t) would not be normal, and hence the joint distribution of |
| 284 |
z(t+1) and y(t+1), conditional on Y(t) and xi(t+1), would not be normal. Thus the above |
| 285 |
filtering recursion is only approximate. |
| 286 |
|
| 287 |
Under the approximation that y(t) conditional on Y(t-1) and xi(t) is normal with mean |
| 288 |
Ey1(t,xi(t)) and variance Eyy1(t,xi(t), we can recursively compute the function |
| 289 |
LogConditionalLikelihood(xi(t),t,model). This recursion will call |
| 290 |
ProbabilityStateConditionalCurrent(xi(tau),tau,model), and hence ForwardRecursion(tau,model), |
| 291 |
for tau <= t-1. Since ForwardRecursion(tau,model) only makes calls to |
| 292 |
LogConditionalLikelihood(xi(tau),tau,model) with tau <= t-1, this recursion will be well |
| 293 |
defined. |
| 294 |
|
| 295 |
|
| 296 |
Likelihood Function: |
| 297 |
|
| 298 |
The likelihood function P(y(t) | Y(t-1), xi(t), theta, q) is approximately normal with mean Ey1(t,xi(t) |
| 299 |
and variance Eyy1(t,xi(t)). Thus the log likelihood is approximately |
| 300 |
|
| 301 |
-0.5*(log(2*pi)*ny + LogAbsDeterminate(Eyy1[t][k]) + ey1[t][k]'*Inverse(Eyy1[t][k])*ey1[t][k]) |
| 302 |
|
| 303 |
where k = xi(t) and ey1[t][k] = y(t) - Ey1[t][k]. |
| 304 |
|
| 305 |
|
| 306 |
Recursive smoothing relations: |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
********************************************************************************/ |