| 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 |
#include "dw_MSStateSpace.h" |
| 19 |
#include "dw_matrix_array.h" |
| 20 |
#include "dw_error.h" |
| 21 |
#include "dw_math.h" |
| 22 |
#include "dw_rand.h" |
| 23 |
#include "dw_std.h" |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
//#define DEBUG_FILTER |
| 29 |
//#define DEBUG_PAUSE |
| 30 |
|
| 31 |
static void AddTranspose(TMatrix X); |
| 32 |
static PRECISION IsSymmetric(TMatrix X); |
| 33 |
|
| 34 |
ThetaRoutines* CreateThetaRoutines_MSStateSpace(void) |
| 35 |
{ |
| 36 |
ThetaRoutines *routines; |
| 37 |
routines=CreateThetaRoutines_empty(); |
| 38 |
|
| 39 |
// sroutines_ps->pLogConditionalLikelihood = logTimetCondLH; |
| 40 |
// sroutines_ps->pLogPrior = logpriordensity; |
| 41 |
// sroutines_ps->pNumberFreeParametersTheta = NumberOfFreeModelSpecificParameters; |
| 42 |
// sroutines_ps->pConvertFreeParametersToTheta = ConvertFreeParameters2ModelSpecificParameters; |
| 43 |
// sroutines_ps->pConvertThetaToFreeParameters = CopyMyFreeParameters2OptimizationParameters; |
| 44 |
// sroutines_ps->pThetaChanged = tz_thetaChanged; |
| 45 |
// sroutines_ps->pTransitionMatrixParametersChanged = tz_TransitionMatrixChanged; |
| 46 |
|
| 47 |
routines->pLogConditionalLikelihood = LogConditionalLikelihood_MSStateSpace; |
| 48 |
routines->pLogPrior = LogPrior_MSStateSpace; |
| 49 |
|
| 50 |
routines->pNumberFreeParametersTheta = NumberFreeParametersTheta_MSStateSpace; |
| 51 |
routines->pConvertFreeParametersToTheta = ConvertFreeParametersToTheta_MSStateSpace; |
| 52 |
routines->pConvertThetaToFreeParameters = ConvertThetaToFreeParameters_MSStateSpace; |
| 53 |
|
| 54 |
routines->pStatesChanged = StatesChanged_MSStateSpace; |
| 55 |
routines->pThetaChanged = ThetaChanged_MSStateSpace; |
| 56 |
routines->pTransitionMatrixParametersChanged = TransitionMatrixParametersChanged_MSStateSpace; |
| 57 |
|
| 58 |
routines->pDestructor = (void (*)(void*))Free_MSStateSpace; |
| 59 |
|
| 60 |
routines->pModelType = ModelType_MSStateSpace; |
| 61 |
|
| 62 |
return routines; |
| 63 |
} |
| 64 |
|
| 65 |
char* ModelType_MSStateSpace(void) |
| 66 |
{ |
| 67 |
return "T_MSStateSpace"; |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
Assumes: |
| 72 |
ny : positive integer |
| 73 |
nz : positive integer |
| 74 |
nu : non-negative integer |
| 75 |
nepsilon : non-negative integer |
| 76 |
nbasestates : positive integer |
| 77 |
nlags_encoded : non-negative integer |
| 78 |
nobs : positive integer |
| 79 |
|
| 80 |
Results: |
| 81 |
Allocates memory for T_MSStateSpace structure. |
| 82 |
*/ |
| 83 |
T_MSStateSpace* Create_MSStateSpace(int ny, int nz, int nu, int nepsilon, int nbasestates, int nlags_encoded, int nobs) |
| 84 |
{ |
| 85 |
T_MSStateSpace *statespace; |
| 86 |
int i, t; |
| 87 |
|
| 88 |
statespace=(T_MSStateSpace*)dw_malloc(sizeof(T_MSStateSpace)); |
| 89 |
|
| 90 |
// Sizes |
| 91 |
statespace->ny=ny; |
| 92 |
statespace->nz=nz; |
| 93 |
statespace->nu=nu; |
| 94 |
statespace->nepsilon=nepsilon; |
| 95 |
statespace->nbasestates=nbasestates; |
| 96 |
statespace->nlags_encoded=nlags_encoded; |
| 97 |
statespace->nobs=nobs; |
| 98 |
for (statespace->zeta_modulus=1, i=nlags_encoded; i > 0; i--) statespace->zeta_modulus*=nbasestates; |
| 99 |
statespace->nstates=statespace->zeta_modulus*nbasestates; |
| 100 |
|
| 101 |
// Flags |
| 102 |
statespace->NonZeroG=0; |
| 103 |
statespace->t0=-1; |
| 104 |
statespace->smoothed=0; |
| 105 |
|
| 106 |
// Create data |
| 107 |
statespace->y=dw_CreateArray_vector(nobs+1); |
| 108 |
for (i=nobs; i >= 0; i--) |
| 109 |
statespace->y[i]=CreateVector(ny); |
| 110 |
|
| 111 |
// Parameters |
| 112 |
statespace->a=dw_CreateArray_vector(nbasestates); |
| 113 |
statespace->H=dw_CreateArray_matrix(nbasestates); |
| 114 |
statespace->b=dw_CreateArray_vector(nbasestates); |
| 115 |
statespace->F=dw_CreateArray_matrix(nbasestates); |
| 116 |
statespace->R=dw_CreateArray_matrix(nbasestates); |
| 117 |
statespace->V=dw_CreateArray_matrix(nbasestates); |
| 118 |
statespace->G=dw_CreateArray_matrix(nbasestates); |
| 119 |
statespace->Phiy=(nu > 0) ? dw_CreateArray_matrix(nbasestates) : (TMatrix*)NULL; |
| 120 |
statespace->Phiz=(nepsilon > 0) ? dw_CreateArray_matrix(nbasestates) : (TMatrix*)NULL; |
| 121 |
for (i=nbasestates-1; i >= 0; i--) |
| 122 |
{ |
| 123 |
statespace->a[i]=CreateVector(ny); |
| 124 |
statespace->H[i]=CreateMatrix(ny,nz); |
| 125 |
statespace->b[i]=CreateVector(nz); |
| 126 |
statespace->F[i]=CreateMatrix(nz,nz); |
| 127 |
statespace->R[i]=CreateMatrix(ny,ny); |
| 128 |
statespace->V[i]=CreateMatrix(nz,nz); |
| 129 |
statespace->G[i]=CreateMatrix(nz,ny); |
| 130 |
if (nu > 0) statespace->Phiy[i]=CreateMatrix(ny,nu); |
| 131 |
if (nepsilon > 0) statespace->Phiz[i]=CreateMatrix(nz,nepsilon); |
| 132 |
} |
| 133 |
|
| 134 |
// Stored Kalman structure |
| 135 |
statespace->Ez=dw_CreateRectangularArray_vector(nobs+1,statespace->nstates); |
| 136 |
statespace->Ezz=dw_CreateRectangularArray_matrix(nobs+1,statespace->nstates); |
| 137 |
statespace->IEz=dw_CreateRectangularArray_vector(nobs+1,statespace->zeta_modulus); |
| 138 |
statespace->IEzz=dw_CreateRectangularArray_matrix(nobs+1,statespace->zeta_modulus); |
| 139 |
statespace->Ez1=dw_CreateRectangularArray_vector(nobs+1,statespace->nstates); |
| 140 |
statespace->Ezz1=dw_CreateRectangularArray_matrix(nobs+1,statespace->nstates); |
| 141 |
statespace->Ey1=dw_CreateRectangularArray_vector(nobs+1,statespace->nstates); |
| 142 |
statespace->Eyy1=dw_CreateRectangularArray_matrix(nobs+1,statespace->nstates); |
| 143 |
statespace->ey1=dw_CreateRectangularArray_vector(nobs+1,statespace->nstates); |
| 144 |
statespace->L=dw_CreateArray_vector(nobs+1); |
| 145 |
statespace->SEz=dw_CreateRectangularArray_vector(nobs+1,statespace->nstates); |
| 146 |
statespace->ISEz=dw_CreateRectangularArray_vector(nobs+1,statespace->zeta_modulus); |
| 147 |
//statespace->SEu=(nu > 0) ? dw_CreateRectangularArray_vector(nobs+1,statespace->zeta_modulus) : (TVector**)NULL; |
| 148 |
//statespace->SEepsilon=(nepsilon > 0) ? dw_CreateRectangularArray_vector(nobs+1,statespace->zeta_modulus) : (TVector**)NULL; |
| 149 |
for (t=nobs; t >= 0; t--) |
| 150 |
{ |
| 151 |
for (i=statespace->nstates-1; i >= 0; i--) |
| 152 |
{ |
| 153 |
statespace->Ez[t][i]=CreateVector(nz); |
| 154 |
statespace->Ezz[t][i]=CreateMatrix(nz,nz); |
| 155 |
statespace->Ez1[t][i]=CreateVector(nz); |
| 156 |
statespace->Ezz1[t][i]=CreateMatrix(nz,nz); |
| 157 |
statespace->Ey1[t][i]=CreateVector(ny); |
| 158 |
statespace->Eyy1[t][i]=CreateMatrix(ny,ny); |
| 159 |
statespace->ey1[t][i]=CreateVector(ny); |
| 160 |
statespace->SEz[t][i]=CreateVector(nz); |
| 161 |
} |
| 162 |
for (i=statespace->zeta_modulus-1; i >= 0; i--) |
| 163 |
{ |
| 164 |
statespace->IEz[t][i]=CreateVector(nz); |
| 165 |
statespace->IEzz[t][i]=CreateMatrix(nz,nz); |
| 166 |
statespace->ISEz[t][i]=CreateVector(nz); |
| 167 |
//if (nu > 0) statespace->SEu[t][i]=CreateVector(nu); |
| 168 |
//if (nepsilon > 0) statespace->SEepsilon[t][i]=CreateVector(nepsilon); |
| 169 |
} |
| 170 |
statespace->L[t]=CreateVector(statespace->nstates); |
| 171 |
} |
| 172 |
|
| 173 |
// Tao's model structure |
| 174 |
statespace->TZstatemodel=(TStateModel*)NULL; |
| 175 |
statespace->MSModel=NULL; |
| 176 |
statespace->gensys_struct=NULL; |
| 177 |
return statespace; |
| 178 |
} |
| 179 |
|
| 180 |
void Free_MSStateSpace(T_MSStateSpace *statespace) |
| 181 |
{ |
| 182 |
if (statespace) |
| 183 |
{ |
| 184 |
dw_FreeArray(statespace->y); |
| 185 |
|
| 186 |
dw_FreeArray(statespace->a); |
| 187 |
dw_FreeArray(statespace->H); |
| 188 |
dw_FreeArray(statespace->b); |
| 189 |
dw_FreeArray(statespace->F); |
| 190 |
dw_FreeArray(statespace->R); |
| 191 |
dw_FreeArray(statespace->V); |
| 192 |
dw_FreeArray(statespace->G); |
| 193 |
dw_FreeArray(statespace->Phiy); |
| 194 |
dw_FreeArray(statespace->Phiz); |
| 195 |
|
| 196 |
dw_FreeArray(statespace->Ez); |
| 197 |
dw_FreeArray(statespace->Ezz); |
| 198 |
dw_FreeArray(statespace->IEz); |
| 199 |
dw_FreeArray(statespace->IEzz); |
| 200 |
dw_FreeArray(statespace->Ezz1); |
| 201 |
dw_FreeArray(statespace->Ey1); |
| 202 |
dw_FreeArray(statespace->Eyy1); |
| 203 |
dw_FreeArray(statespace->ey1); |
| 204 |
dw_FreeArray(statespace->L); |
| 205 |
|
| 206 |
dw_FreeArray(statespace->ISEz); |
| 207 |
dw_FreeArray(statespace->SEz); |
| 208 |
|
| 209 |
dw_free(statespace); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
TStateModel* CreateStateModel_MSStateSpace(TStateModel *basemodel, int nlags_encoded) |
| 214 |
{ |
| 215 |
T_MSStateSpace *statespace=Create_MSStateSpaceFromStateModel(basemodel,nlags_encoded); |
| 216 |
statespace->MSModel = NULL; |
| 217 |
TMarkovStateVariable *sv_new; |
| 218 |
ThetaRoutines *routines; |
| 219 |
TStateModel *model; |
| 220 |
PRECISION *buffer=dw_malloc(NumberFreeParametersQ(basemodel)*sizeof(PRECISION)); |
| 221 |
|
| 222 |
// Create TMarkovStateVariable structure |
| 223 |
sv_new=CreateMarkovStateVariable_lags(nlags_encoded,basemodel->sv); |
| 224 |
|
| 225 |
// Create theta routines |
| 226 |
routines=CreateThetaRoutines_MSStateSpace(); |
| 227 |
|
| 228 |
// Create TStateVariable |
| 229 |
model=CreateStateModel(statespace->nobs,sv_new,routines,(void*)statespace); |
| 230 |
|
| 231 |
// Initialize structure |
| 232 |
model->fobs=basemodel->fobs; |
| 233 |
ConvertQToFreeParameters(basemodel,buffer); |
| 234 |
ConvertFreeParametersToQ(model,buffer); |
| 235 |
dw_free(buffer); |
| 236 |
|
| 237 |
return model; |
| 238 |
} |
| 239 |
|
| 240 |
double LogPrior_MSStateSpace(TStateModel *model) |
| 241 |
{ |
| 242 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 243 |
if (statespace->MSModel == NULL) |
| 244 |
{ |
| 245 |
return (statespace->TZstatemodel->routines->pLogPrior) |
| 246 |
? statespace->TZstatemodel->routines->pLogPrior(statespace->TZstatemodel) : MINUS_INFINITY; |
| 247 |
} |
| 248 |
else |
| 249 |
#ifdef DYNARE_INTERFACE |
| 250 |
return get_log_prior_from_markov_model(statespace->MSModel); |
| 251 |
#else |
| 252 |
return MINUS_INFINITY; |
| 253 |
#endif |
| 254 |
} |
| 255 |
|
| 256 |
void StatesChanged_MSStateSpace(TStateModel *model) |
| 257 |
{ |
| 258 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 259 |
if (statespace->MSModel == NULL) |
| 260 |
{ |
| 261 |
Invalidate_MSStateSpace(statespace); |
| 262 |
if (statespace->TZstatemodel->routines->pStatesChanged) |
| 263 |
statespace->TZstatemodel->routines->pStatesChanged(statespace->TZstatemodel); |
| 264 |
} |
| 265 |
#ifdef DYNARE_INTERFACE |
| 266 |
else |
| 267 |
states_changed_in_markov_model(statespace->MSModel); |
| 268 |
#endif |
| 269 |
} |
| 270 |
|
| 271 |
void ThetaChanged_MSStateSpace(TStateModel *model) |
| 272 |
{ |
| 273 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 274 |
Invalidate_MSStateSpace(statespace); |
| 275 |
if (statespace->MSModel == NULL) |
| 276 |
{ |
| 277 |
if (statespace->TZstatemodel->routines->pThetaChanged) |
| 278 |
statespace->TZstatemodel->routines->pThetaChanged(statespace->TZstatemodel); |
| 279 |
} |
| 280 |
#ifdef DYNARE_INTERFACE |
| 281 |
else |
| 282 |
theta_changed_in_markov_model(statespace->MSModel); |
| 283 |
#endif |
| 284 |
} |
| 285 |
|
| 286 |
void TransitionMatrixParametersChanged_MSStateSpace(TStateModel *model) |
| 287 |
{ |
| 288 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 289 |
if (statespace->MSModel == NULL) |
| 290 |
{ |
| 291 |
Invalidate_MSStateSpace(statespace); |
| 292 |
if (statespace->TZstatemodel->routines->pTransitionMatrixParametersChanged) |
| 293 |
statespace->TZstatemodel->routines->pTransitionMatrixParametersChanged(statespace->TZstatemodel); |
| 294 |
} |
| 295 |
#ifdef DYNARE_INTERFACE |
| 296 |
else |
| 297 |
transition_matrix_parameters_changed_im_markov_model(statespace->MSModel); |
| 298 |
#endif |
| 299 |
} |
| 300 |
|
| 301 |
void Invalidate_MSStateSpace(T_MSStateSpace *statespace) |
| 302 |
{ |
| 303 |
statespace->t0=-1; |
| 304 |
statespace->smoothed=0; |
| 305 |
} |
| 306 |
|
| 307 |
int NumberFreeParametersTheta_MSStateSpace(TStateModel *model) |
| 308 |
{ |
| 309 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 310 |
if (statespace->MSModel == NULL) |
| 311 |
{ |
| 312 |
return (statespace->TZstatemodel->routines->pNumberFreeParametersTheta) ? |
| 313 |
statespace->TZstatemodel->routines->pNumberFreeParametersTheta(statespace->TZstatemodel) : -1; |
| 314 |
} |
| 315 |
else |
| 316 |
#ifdef DYNARE_INTERFACE |
| 317 |
return number_free_parameters_theta_markov_model(statespace->MSModel); |
| 318 |
#else |
| 319 |
return -1; |
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
void ConvertFreeParametersToTheta_MSStateSpace(TStateModel *model, PRECISION *buffer) |
| 324 |
{ |
| 325 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 326 |
if (statespace->MSModel == NULL) |
| 327 |
{ |
| 328 |
if (statespace->TZstatemodel->routines->pConvertFreeParametersToTheta) |
| 329 |
statespace->TZstatemodel->routines->pConvertFreeParametersToTheta(statespace->TZstatemodel,buffer); |
| 330 |
} |
| 331 |
#ifdef DYNARE_INTERFACE |
| 332 |
else |
| 333 |
convert_free_parameter_to_theta_markov_model(statespace->MSModel, buffer); |
| 334 |
#endif |
| 335 |
} |
| 336 |
|
| 337 |
void ConvertThetaToFreeParameters_MSStateSpace(TStateModel *model, PRECISION *buffer) |
| 338 |
{ |
| 339 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 340 |
if (statespace->MSModel == NULL) |
| 341 |
{ |
| 342 |
if (statespace->TZstatemodel->routines->pConvertThetaToFreeParameters) |
| 343 |
statespace->TZstatemodel->routines->pConvertThetaToFreeParameters(statespace->TZstatemodel,buffer); |
| 344 |
} |
| 345 |
#ifdef DYNARE_INTERFACE |
| 346 |
else |
| 347 |
convert_theta_to_free_parameters_markov_model(statespace->MSModel, buffer); |
| 348 |
#endif |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
/* |
| 353 |
Computes |
| 354 |
|
| 355 |
P[y(t) | Y(t-1), s(t)=s, theta, q] |
| 356 |
|
| 357 |
under the assumption that y(t) conditional on Y(t-1), s(t), theta, and q is |
| 358 |
Gaussian with mean statespace->ey1[t][s] and variance statespace->Eyy1[t][s]. The Markov |
| 359 |
process s(t) denotes the grand state in model->sv. |
| 360 |
|
| 361 |
If t > statespace->t0, calls Filter(t,model). |
| 362 |
*/ |
| 363 |
PRECISION LogConditionalLikelihood_MSStateSpace(int s, int t, TStateModel *model) |
| 364 |
{ |
| 365 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 366 |
if ((statespace->t0 < t) && !Filter(t,model)) return MINUS_INFINITY; |
| 367 |
return ElementV(statespace->L[t],s); |
| 368 |
} |
| 369 |
|
| 370 |
//=== Computes E[y[t] | Y[t-1], Z[t], theta, q, s[t] = s], works even for t > T, === |
| 371 |
//=== data[tau] = Y[T+tau+1], assumes exogeneous variable z[t] is deterministic === |
| 372 |
TVector pEyt_MSStateSpace(TVector y, int T, TVector *data, int s, int t, TStateModel *model) |
| 373 |
{ |
| 374 |
if ((t <= T) || (T > model->nobs)) return (TVector)NULL; |
| 375 |
if (t == T+1) |
| 376 |
{ |
| 377 |
} |
| 378 |
else |
| 379 |
{ |
| 380 |
} |
| 381 |
return (TVector)NULL; |
| 382 |
} |
| 383 |
|
| 384 |
//=== Computes E[y[t] | Y[t-1], Z[t], theta, q, s[t] = s, shock(i)], shock(i) is the unit === |
| 385 |
//=== value of the model specific ith shock, works even for t > T, data[tau] = Y[T+tau+1], === |
| 386 |
//=== assumes exogeneous variable z[t] is deterministic === |
| 387 |
TVector pEyt_i_MSStateSpace(TVector y, int T, TVector *data, int i, int s, int t, TStateModel *model); |
| 388 |
|
| 389 |
//=== Makes random draw of y[t] given Y[t-1], Z[t], theta, q, s[t] = s, works for t > T, === |
| 390 |
//=== data[tau] = Y[T+tau+1], assumes exogeneous variable z[t] is deterministic === |
| 391 |
TVector pyt_draw_MSStateSpace(TVector y, int T, TVector *data, int s, int t, TStateModel *model); |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
/* |
| 396 |
Upon entry to a call, the following must be true: |
| 397 |
|
| 398 |
Parameter values |
| 399 |
If statespace->t0 >= 0, then a, H, b, F, R, V, and G are properly set |
| 400 |
|
| 401 |
Kalman filtering data are properly defined for |
| 402 |
IEz[t][j] 1 <= t <= t0-1, 0 <= j < zeta_modulus |
| 403 |
IEzz[t][j] 1 <= t <= t0-1, 0 <= j < zeta_modulus |
| 404 |
Ez1[t][k] 1 <= t <= t0, 0 <= k < nstates - Ez1[1][k] is also defined if t0 = 0 |
| 405 |
Ezz1[t][k] 1 <= t <= t0, 0 <= k < nstates - Ezz1[1][k] is also defined if t0 = 0 |
| 406 |
Ey1[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 407 |
Eyy1[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 408 |
ey1[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 409 |
Ez[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 410 |
Ezz[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 411 |
L[t][k] 1 <= t <= t0, 0 <= k < nstates |
| 412 |
|
| 413 |
Flags |
| 414 |
If statespace->t0 >= 0, then NonZeroG is properly set |
| 415 |
|
| 416 |
----------------------------------------------------------------------------------------------- |
| 417 |
|
| 418 |
After a successful call, with tau >= 0, the following are valid: |
| 419 |
|
| 420 |
Parameter values |
| 421 |
a, H, b, F, R, V, and G are properly set |
| 422 |
|
| 423 |
Kalman filtering data are properly defined for |
| 424 |
IEz[t][j] 1 <= t <= tau-1, 0 <= j < zeta_modulus |
| 425 |
IEzz[t][j] 1 <= t <= tau-1, 0 <= j < zeta_modulus |
| 426 |
Ez1[t][k] 1 <= t <= tau, 0 <= k < nstates - Ez1[1][k] is also defined if tau = 0 |
| 427 |
Ezz1[t][k] 1 <= t <= tau, 0 <= k < nstates - Ezz1[1][k] is also defined if tau = 0 |
| 428 |
Ey1[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 429 |
Eyy1[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 430 |
ey1[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 431 |
Ez[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 432 |
Ezz[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 433 |
L[t][k] 1 <= t <= tau, 0 <= k < nstates |
| 434 |
|
| 435 |
Flags |
| 436 |
NonZeroG is properly set |
| 437 |
statespace->t0 = tau |
| 438 |
|
| 439 |
Returns 1 upon success and 0 otherwise. |
| 440 |
|
| 441 |
If model->t0 < tau-1, calls ForwardRecursion(t,model) for statespace->t0 <= t <= tau-1. |
| 442 |
IEz and IEzz are only updated through tau-1 because ForwardRecursion(t,model) can only |
| 443 |
be called with t < tau. |
| 444 |
|
| 445 |
*/ |
| 446 |
#define LOG2PI 1.83787706640934548356 // log(2*pi) |
| 447 |
int Filter(int tau, TStateModel *model) |
| 448 |
{ |
| 449 |
int j, k, s, t, zeta, terminal_errors, verbose_errors, rtrn=0; |
| 450 |
PRECISION tmp; |
| 451 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 452 |
TMatrix X, Y, K, T; |
| 453 |
TVector v; |
| 454 |
|
| 455 |
if (tau <= statespace->t0) return 1; |
| 456 |
|
| 457 |
if (tau > statespace->nobs) return 0; |
| 458 |
|
| 459 |
// After a successful call to Initialize_MSStateSpace(), the following hold |
| 460 |
// t0 = 0 |
| 461 |
// NonZeroG is properly set |
| 462 |
// a, H, b, F, R, V, and G are properly set |
| 463 |
// Ez1[1][k] and Ezz1[1][k] are properly set for 0 <= k < nstates |
| 464 |
if ((statespace->t0 < 0) && !Initialize_MSStateSpace(model)) return 0; |
| 465 |
|
| 466 |
X=CreateMatrix(statespace->nz,statespace->nz); |
| 467 |
Y=CreateMatrix(statespace->ny,statespace->nz); |
| 468 |
T=CreateMatrix(statespace->ny,statespace->ny); |
| 469 |
K=CreateMatrix(statespace->nz,statespace->ny); |
| 470 |
v=CreateVector(statespace->ny); |
| 471 |
|
| 472 |
terminal_errors=dw_SetTerminalErrors(0); |
| 473 |
verbose_errors=dw_SetVerboseErrors(0); |
| 474 |
|
| 475 |
for (t=statespace->t0+1; t <= tau; t++) |
| 476 |
{ |
| 477 |
if (!ForwardRecursion(t-1,model)) goto CLEAN_UP; |
| 478 |
|
| 479 |
// Sets IEz[t-1] and IEzz[t-1] |
| 480 |
// Uses Ez[t-1], Ezz[t-1], and model->V[t-1](i)=P(s[t-1] = i | Y[t-1], Theta) |
| 481 |
if (t > 1) |
| 482 |
{ |
| 483 |
IntegrateStatesSingleV(statespace->IEz[t-1],model->V[t-1],statespace->Ez[t-1], |
| 484 |
statespace->zeta_modulus,statespace->nbasestates,2); |
| 485 |
IntegrateStatesSingleM(statespace->IEzz[t-1],model->V[t-1],statespace->Ezz[t-1], |
| 486 |
statespace->zeta_modulus,statespace->nbasestates,2); |
| 487 |
} |
| 488 |
|
| 489 |
for (k=statespace->nstates-1; k >= 0; k--) |
| 490 |
{ |
| 491 |
// s = s(t) |
| 492 |
s=model->sv->lag_index[k][0]; |
| 493 |
|
| 494 |
// Sets Ez1[t][k] and Ezz1[t][k] |
| 495 |
// Uses IEz[t-1][k], and IEzz[t-1][k] |
| 496 |
if (t > 1) |
| 497 |
{ |
| 498 |
// zeta = zeta(t-1) |
| 499 |
zeta=k-s*statespace->zeta_modulus; |
| 500 |
|
| 501 |
ProductMV(statespace->Ez1[t][k],statespace->F[s],statespace->IEz[t-1][zeta]); |
| 502 |
AddVV(statespace->Ez1[t][k],statespace->Ez1[t][k],statespace->b[s]); |
| 503 |
|
| 504 |
ProductMM(X,statespace->F[s],statespace->IEzz[t-1][zeta]); |
| 505 |
ProductTransposeMM(statespace->Ezz1[t][k],X,statespace->F[s]); |
| 506 |
AddMM(statespace->Ezz1[t][k],statespace->Ezz1[t][k],statespace->V[s]); |
| 507 |
ForceSymmetric(statespace->Ezz1[t][k]); |
| 508 |
} |
| 509 |
|
| 510 |
// Sets Ey1[t][k] and Eyy1[t][k] |
| 511 |
// Uses Ez1[t][k] and Ezz1[t][k] |
| 512 |
ProductMV(statespace->Ey1[t][k],statespace->H[s],statespace->Ez1[t][k]); |
| 513 |
AddVV(statespace->Ey1[t][k],statespace->Ey1[t][k],statespace->a[s]); |
| 514 |
|
| 515 |
ProductMM(Y,statespace->H[s],statespace->Ezz1[t][k]); |
| 516 |
ProductTransposeMM(statespace->Eyy1[t][k],Y,statespace->H[s]); |
| 517 |
AddMM(statespace->Eyy1[t][k],statespace->Eyy1[t][k],statespace->R[s]); |
| 518 |
if (statespace->NonZeroG) |
| 519 |
{ |
| 520 |
ProductMM(T,statespace->H[s],statespace->G[s]); |
| 521 |
AddTranspose(T); |
| 522 |
AddMM(statespace->Eyy1[t][k],statespace->Eyy1[t][k],T); |
| 523 |
} |
| 524 |
ForceSymmetric(statespace->Eyy1[t][k]); |
| 525 |
|
| 526 |
// Sets ey1[t][k] |
| 527 |
// Uses Ey1[t][k] and y[t] |
| 528 |
SubtractVV(statespace->ey1[t][k],statespace->y[t],statespace->Ey1[t][k]); |
| 529 |
|
| 530 |
// Cholesky decomposition of Eyy1[t][k] (Eyy1[t][k] = T'*T) |
| 531 |
if (!CholeskyUT(T,statespace->Eyy1[t][k])) goto CLEAN_UP; |
| 532 |
|
| 533 |
//dw_PrintMatrix(stdout,statespace->Eyy1[t][k],"%lg "); |
| 534 |
//terminal_errors=dw_SetTerminalErrors(0); |
| 535 |
//if (!CholeskyUT(T,statespace->Eyy1[t][k])) |
| 536 |
// { |
| 537 |
// MatrixSquareRoot(T,statespace->Eyy1[t][k]); |
| 538 |
// if (!QR((TMatrix)NULL,T,T)) |
| 539 |
// { |
| 540 |
// dw_SetTerminalErrors(terminal_errors); |
| 541 |
// return 0; |
| 542 |
// } |
| 543 |
// } |
| 544 |
//dw_SetTerminalErrors(terminal_errors); |
| 545 |
//printf("%d-%d\n",t,k); getchar(); |
| 546 |
|
| 547 |
// K = (Ezz1[t][k]*H[s]' + G[s])*Inverse(T) |
| 548 |
ProductTransposeMM(K,statespace->Ezz1[t][k],statespace->H[s]); |
| 549 |
if (statespace->NonZeroG) AddMM(K,K,statespace->G[s]); |
| 550 |
ProductInverseMU(K,K,T); |
| 551 |
|
| 552 |
// v = (ey1[t][k]'*Inverse(T))' |
| 553 |
ProductInverseVU(v,statespace->ey1[t][k],T); |
| 554 |
|
| 555 |
// Sets Ez[t][k] and Ezz[t][k] |
| 556 |
// Uses K, v, Ez1[t][k], and Ezz1[t][k] |
| 557 |
ProductMV(statespace->Ez[t][k],K,v); |
| 558 |
AddVV(statespace->Ez[t][k],statespace->Ez[t][k],statespace->Ez1[t][k]); |
| 559 |
|
| 560 |
ProductTransposeMM(statespace->Ezz[t][k],K,K); |
| 561 |
SubtractMM(statespace->Ezz[t][k],statespace->Ezz1[t][k],statespace->Ezz[t][k]); |
| 562 |
ForceSymmetric(statespace->Ezz[t][k]); |
| 563 |
|
| 564 |
// Sets L[t](k) |
| 565 |
// Uses T and v. The diagonal elements of T are positive. |
| 566 |
for (tmp=0.0, j=RowM(T)*RowM(T)-1; j >= 0; j-=RowM(T)+1) |
| 567 |
tmp-=log(pElementM(T)[j]); |
| 568 |
ElementV(statespace->L[t],k)=tmp - 0.5*(LOG2PI*statespace->ny + DotProduct(v,v)); |
| 569 |
|
| 570 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 571 |
#ifdef DEBUG_FILTER |
| 572 |
|
| 573 |
TMatrix K1=CreateMatrix(statespace->nz,statespace->ny), K2=CreateMatrix(statespace->nz,statespace->ny); |
| 574 |
TVector v1=CreateVector(statespace->nz); |
| 575 |
|
| 576 |
// Alternative computation of statespace->Ez[t][k] and statespace->Ezz[t][k] |
| 577 |
// Uses statespace->Ezz1[t][k], statespace->Eyy1[t][k], statespace->ey1[t][k] |
| 578 |
ProductTransposeMM(K1,statespace->Ezz1[t][k],statespace->H[s]); |
| 579 |
if (statespace->NonZeroG) AddMM(K1,K1,statespace->G[s]); |
| 580 |
ProductInverseMM(K2,K1,statespace->Eyy1[t][k]); |
| 581 |
ProductMV(v1,K2,statespace->ey1[t][k]); |
| 582 |
AddVV(v1,v1,statespace->Ez1[t][k]); |
| 583 |
if (Norm(SubtractVV(v1,v1,statespace->Ez[t][k])) > 1e-12) |
| 584 |
{ |
| 585 |
printf("Difference E[z(%d) | Y(%d), s(%d)=%d, theta, q] : %le\n",t,t,t,k,Norm(v1)); |
| 586 |
#ifdef DEBUG_PAUSE |
| 587 |
printf("Press <Enter> to continue"); |
| 588 |
getchar(); |
| 589 |
#endif |
| 590 |
} |
| 591 |
|
| 592 |
ProductMM(K1,K2,statespace->Eyy1[t][k]); |
| 593 |
ProductTransposeMM(X,K1,K2); |
| 594 |
SubtractMM(X,statespace->Ezz1[t][k],X); |
| 595 |
ForceSymmetric(X); |
| 596 |
SubtractMM(X,X,statespace->Ezz[t][k]); |
| 597 |
int ii, jj; |
| 598 |
for (sum=0, ii=0; ii < statespace->nz; ii++) |
| 599 |
for (jj=0; jj < statespace->nz; jj++) |
| 600 |
sum+=ElementM(X,ii,jj)*ElementM(X,ii,jj); |
| 601 |
sum=sqrt(sum); |
| 602 |
if (sum > 1e-10) |
| 603 |
{ |
| 604 |
printf("Difference Var[z(%d) | Y(%d), s(%d)=%d, theta, q] : %le\n",t,t,t,k,sum); |
| 605 |
#ifdef DEBUG_PAUSE |
| 606 |
printf("Press <Enter> to continue"); |
| 607 |
getchar(); |
| 608 |
#endif |
| 609 |
} |
| 610 |
|
| 611 |
// An alternative way of computing the loglikelihood |
| 612 |
InverseProductMV(v,statespace->Eyy1[t][k],statespace->ey1[t][k]); |
| 613 |
if (fabs(ElementV(statespace->L[t],k) - (tmp - 0.5*(LOG2PI*statespace->ny + DotProduct(statespace->ey1[t][k],v)))) > 1e-13) |
| 614 |
{ |
| 615 |
printf("Difference conditional log likelihood: t = %d state = %d %le\n",t,k, |
| 616 |
ElementV(statespace->L[t],k) - (tmp - 0.5*(LOG2PI*statespace->ny + DotProduct(statespace->ey1[t][k],v)))); |
| 617 |
#ifdef DEBUG_PAUSE |
| 618 |
printf("Press <Enter> to continue"); |
| 619 |
getchar(); |
| 620 |
#endif |
| 621 |
} |
| 622 |
|
| 623 |
FreeMatrix(K1); |
| 624 |
FreeMatrix(K2); |
| 625 |
FreeVector(v1); |
| 626 |
#endif |
| 627 |
/////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 628 |
} |
| 629 |
|
| 630 |
statespace->t0=t; |
| 631 |
} |
| 632 |
|
| 633 |
rtrn=1; |
| 634 |
|
| 635 |
// Clean up |
| 636 |
CLEAN_UP: |
| 637 |
FreeVector(v); |
| 638 |
FreeMatrix(K); |
| 639 |
FreeMatrix(T); |
| 640 |
FreeMatrix(Y); |
| 641 |
FreeMatrix(X); |
| 642 |
|
| 643 |
dw_SetTerminalErrors(terminal_errors); |
| 644 |
dw_SetVerboseErrors(verbose_errors); |
| 645 |
|
| 646 |
return rtrn; |
| 647 |
} |
| 648 |
#undef LOG2PI |
| 649 |
|
| 650 |
/* |
| 651 |
0 <= t0 <= t1 < T |
| 652 |
|
| 653 |
y - T x ny with y[t][i] null for data that is missing. |
| 654 |
|
| 655 |
Ez_i - Ez_i(k) ~ E[z(t0-1) | Y(t0-1), xi(t0-1)=k, theta, q] |
| 656 |
|
| 657 |
Ezz_i - Ezz_i(k) ~ E[(z(t0-1) - Ez_i(k))(z(t0-1) - Ez_i(k))' | Y(t0-1), xi(t0-1)=k, theta, q] |
| 658 |
|
| 659 |
Pxi_i - Pxi_i(k) ~ P(xi(t0-1)=k | Y(t0-1), theta, q) |
| 660 |
|
| 661 |
Q - transition matrix. Q(i,j) = P(s[t+1]=i | s[t]=j) |
| 662 |
|
| 663 |
Pxi - Pxi[t](k) ~ P(xi(t)=k | Y(t), theta, q) |
| 664 |
|
| 665 |
Pxi1 - Pxi1[t](k) ~ P(xi(t)=k | Y(t-1), theta, q) |
| 666 |
|
| 667 |
IEz - IEz[t](j) ~ E[z(t) | Y(t), zeta(t)=j, theta, q] |
| 668 |
|
| 669 |
IEzz - IEzz[t](j) ~ E[(z(t) - IEz[t](j))(z(t) - IEz[t](j))' | Y(t), zeta(t)=j, theta, q] |
| 670 |
|
| 671 |
Ez1 - Ez1[t](k) ~ E[z(t) | Y(t-1), xi(t)=k, theta, q] |
| 672 |
|
| 673 |
Ezz1 - Ezz1[t](k) ~ E[(z(t) - Ez1[t](k))(z(t) - Ez1[t](k))' | Y(t-1), xi(t)=k, theta, q] |
| 674 |
|
| 675 |
Ez - Ez[t](k) ~ E[z(t) | Y(t), xi(t)=k, theta, q] |
| 676 |
|
| 677 |
Ezz - Ezz[t](k) ~ E[(z(t) - Ez[t](k))(z(t) - Ez[t](t))' | Y(t), xi(t)=k, theta, q] |
| 678 |
|
| 679 |
Pxi, Pxi1, IEz, IEzz, Ez1, Ezz1, Ez, and Ezz are all matrix or vector arrays |
| 680 |
of length T. Upon exit, Pxi[t], Pxi1[t], IEz[t], IEzz[t], Ez1[t], Ezz1[t], |
| 681 |
Ez[t], and Ezz[t] are all set for t0 <= t <= t1. |
| 682 |
|
| 683 |
In general the transition matrix Q could depend on t, however, in this |
| 684 |
application we assume that it is independent of t. |
| 685 |
*/ |
| 686 |
#define LOG2PI 1.83787706640934548356 // log(2*pi) |
| 687 |
int ConditionalFilter(int t0, int t1, PRECISION ***y, TVector *Ez_i, TMatrix *Ezz_i, TVector Pxi_i, TMatrix Q, |
| 688 |
TVector *Pxi, TVector *Pxi1, TVector **IEz, TMatrix **IEzz, TVector **Ez1, TMatrix **Ezz1, |
| 689 |
TVector **Ez, TMatrix **Ezz, T_MSStateSpace *statespace) |
| 690 |
{ |
| 691 |
int i, j, k, m, n, xi, s, t, zeta, terminal_errors, verbose_errors, n_xi=statespace->nstates, |
| 692 |
n_zeta=statespace->zeta_modulus, n_s=statespace->nbasestates, nz=statespace->nz, ny=statespace->ny; |
| 693 |
PRECISION scale, sum; |
| 694 |
TMatrix X, Y, K, T, *H, *R, *G=(TMatrix*)NULL, *IEzz_i, Eyy1; |
| 695 |
TVector v, cy, *a, *IEz_i, Ey1, L; |
| 696 |
|
| 697 |
IEz_i=dw_CreateArray_vector(n_xi); |
| 698 |
IEzz_i=dw_CreateArray_matrix(n_xi); |
| 699 |
a=dw_CreateArray_vector(n_s); |
| 700 |
H=dw_CreateArray_matrix(n_s); |
| 701 |
R=dw_CreateArray_matrix(n_s); |
| 702 |
if (statespace->NonZeroG) G=dw_CreateArray_matrix(n_s); |
| 703 |
L=CreateVector(n_xi); |
| 704 |
X=CreateMatrix(nz,nz); |
| 705 |
|
| 706 |
// Resetting terminal and verbose errors for calls to CholeskyLT() |
| 707 |
terminal_errors=dw_SetTerminalErrors(0); |
| 708 |
verbose_errors=dw_SetVerboseErrors(0); |
| 709 |
|
| 710 |
for (t=t0; t <= t1; t++) |
| 711 |
{ |
| 712 |
if (t > t0) |
| 713 |
{ |
| 714 |
// Sets IEz[t-1] and IEzz[t-1] |
| 715 |
// Uses Ez[t-1], Ezz[t-1], and Pxi[t-1] |
| 716 |
IntegrateStatesSingleV(IEz[t-1],Pxi[t-1],Ez[t-1],n_zeta,n_s,2); |
| 717 |
IntegrateStatesSingleM(IEzz[t-1],Pxi[t-1],Ezz[t-1],n_zeta,n_s,2); |
| 718 |
|
| 719 |
// s=s(t) |
| 720 |
for (s=n_s-1; s >= 0; s--) |
| 721 |
// zeta=zeta(t-1) |
| 722 |
for (zeta=n_zeta-1; zeta >= 0; zeta--) |
| 723 |
{ |
| 724 |
// xi=xi(t) |
| 725 |
xi=s*n_zeta+zeta; |
| 726 |
|
| 727 |
// Sets Ez1[t][xi] and Ezz1[t][xi] |
| 728 |
// Uses IEz[t-1][xi], and IEzz[t-1][xi] |
| 729 |
ProductMV(Ez1[t][xi],statespace->F[s],IEz[t-1][zeta]); |
| 730 |
AddVV(Ez1[t][xi],Ez1[t][xi],statespace->b[s]); |
| 731 |
|
| 732 |
ProductMM(X,statespace->F[s],IEzz[t-1][zeta]); |
| 733 |
ProductTransposeMM(Ezz1[t][xi],X,statespace->F[s]); |
| 734 |
AddMM(Ezz1[t][xi],Ezz1[t][xi],statespace->V[s]); |
| 735 |
ForceSymmetric(Ezz1[t][xi]); |
| 736 |
} |
| 737 |
|
| 738 |
// Sets Pxi1[t] |
| 739 |
// Uses Pxi[t-1] |
| 740 |
ProductMV(Pxi1[t],Q,Pxi[t-1]); |
| 741 |
} |
| 742 |
else |
| 743 |
{ |
| 744 |
// Sets IEz[t-1] and IEzz[t-1] |
| 745 |
// Uses Ez[t-1], Ezz[t-1], and Pxi[t-1] |
| 746 |
IntegrateStatesSingleV(IEz_i,Pxi_i,Ez_i,n_zeta,n_s,2); |
| 747 |
IntegrateStatesSingleM(IEzz_i,Pxi_i,Ezz_i,n_zeta,n_s,2); |
| 748 |
|
| 749 |
// s=s(t) |
| 750 |
for (s=n_s-1; s >= 0; s--) |
| 751 |
// zeta=zeta(t-1) |
| 752 |
for (zeta=n_zeta-1; zeta >= 0; zeta--) |
| 753 |
{ |
| 754 |
// xi=xi(t) |
| 755 |
xi=s*n_zeta+zeta; |
| 756 |
|
| 757 |
// Sets Ez1[t][xi] and Ezz1[t][xi] |
| 758 |
// Uses IEz[t-1][xi], and IEzz[t-1][xi] |
| 759 |
ProductMV(Ez1[t][xi],statespace->F[s],IEz_i[zeta]); |
| 760 |
AddVV(Ez1[t][xi],Ez1[t][xi],statespace->b[s]); |
| 761 |
|
| 762 |
ProductMM(X,statespace->F[s],IEzz_i[zeta]); |
| 763 |
ProductTransposeMM(Ezz1[t][xi],X,statespace->F[s]); |
| 764 |
AddMM(Ezz1[t][xi],Ezz1[t][xi],statespace->V[s]); |
| 765 |
ForceSymmetric(Ezz1[t][xi]); |
| 766 |
} |
| 767 |
|
| 768 |
// Sets Pxi1[t] |
| 769 |
// Uses Pxi[t-1] |
| 770 |
ProductMV(Pxi1[t],Q,Pxi_i); |
| 771 |
} |
| 772 |
|
| 773 |
// Determine the dimension of y[t] |
| 774 |
for (k=0, i=ny-1; i >= 0; i--) if (y[t][i]) k++; |
| 775 |
|
| 776 |
if (k > 0) |
| 777 |
{ |
| 778 |
Ey1=CreateVector(k); |
| 779 |
Eyy1=CreateMatrix(k,k); |
| 780 |
cy=CreateVector(k); |
| 781 |
v=CreateVector(k); |
| 782 |
Y=CreateMatrix(k,nz); |
| 783 |
T=CreateMatrix(k,k); |
| 784 |
K=CreateMatrix(nz,k); |
| 785 |
|
| 786 |
for (i=j=0; i < ny; i++) |
| 787 |
if (y[t][i]) ElementV(cy,j++)=*(y[t][i]); |
| 788 |
|
| 789 |
for (s=n_s-1; s >= 0; s--) |
| 790 |
{ |
| 791 |
a[s]=CreateVector(k); |
| 792 |
H[s]=CreateMatrix(k,nz); |
| 793 |
R[s]=CreateMatrix(k,k); |
| 794 |
|
| 795 |
for (m=k, i=ny-1; i >= 0; i--) |
| 796 |
if (y[t][i]) |
| 797 |
{ |
| 798 |
m--; |
| 799 |
ElementV(a[s],m)=ElementV(statespace->a[s],i); |
| 800 |
for (j=nz-1; j >= 0; j--) |
| 801 |
ElementM(H[s],m,j)=ElementM(statespace->H[s],i,j); |
| 802 |
for (n=k, j=ny-1; j >= 0; j--) |
| 803 |
if (y[t][j]) ElementM(R[s],m,--n)=ElementM(statespace->R[s],i,j); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
if (statespace->NonZeroG) |
| 808 |
for (s=n_s-1; s >= 0; s--) |
| 809 |
{ |
| 810 |
G[s]=CreateMatrix(nz,k); |
| 811 |
for (n=k, j=ny-1; j >= 0; j--) |
| 812 |
if (y[t][j]) |
| 813 |
for (n--, i=nz-1; i >= 0; i--) |
| 814 |
ElementM(G[s],i,n)=ElementM(statespace->G[s],i,j); |
| 815 |
} |
| 816 |
|
| 817 |
for (xi=n_xi-1; xi >= 0; xi--) |
| 818 |
{ |
| 819 |
// s = s(t) |
| 820 |
s=xi/n_zeta; |
| 821 |
|
| 822 |
// Sets Ey1[t][xi] and Eyy1[t][xi] |
| 823 |
// Uses Ez1[t][xi] and Ezz1[t][xi] |
| 824 |
ProductMV(Ey1,H[s],Ez1[t][xi]); |
| 825 |
AddVV(Ey1,Ey1,a[s]); |
| 826 |
|
| 827 |
ProductMM(Y,H[s],Ezz1[t][xi]); |
| 828 |
ProductTransposeMM(Eyy1,Y,H[s]); |
| 829 |
AddMM(Eyy1,Eyy1,R[s]); |
| 830 |
if (statespace->NonZeroG) |
| 831 |
{ |
| 832 |
ProductMM(T,H[s],G[s]); |
| 833 |
AddTranspose(T); |
| 834 |
AddMM(Eyy1,Eyy1,T); |
| 835 |
} |
| 836 |
ForceSymmetric(Eyy1); |
| 837 |
|
| 838 |
// Cholesky decomposition of Eyy1[t][xi] (Eyy1[t][xi] = T'*T) |
| 839 |
if (!CholeskyUT(T,Eyy1)) |
| 840 |
{ |
| 841 |
dw_SetTerminalErrors(terminal_errors); |
| 842 |
dw_SetVerboseErrors(verbose_errors); |
| 843 |
return 0; |
| 844 |
} |
| 845 |
else |
| 846 |
{ |
| 847 |
// K = (Ezz1[t][xi]*H[s]' + G[s])*Inverse(T) |
| 848 |
ProductTransposeMM(K,Ezz1[t][xi],H[s]); |
| 849 |
if (statespace->NonZeroG) AddMM(K,K,G[s]); |
| 850 |
ProductInverseMU(K,K,T); |
| 851 |
|
| 852 |
// v = ((cy - Ey1)'*Inverse(T))' |
| 853 |
SubtractVV(v,cy,Ey1); |
| 854 |
ProductInverseVU(v,v,T); |
| 855 |
|
| 856 |
// Sets Ez[t][xi] and Ezz[t][xi] |
| 857 |
// Uses K, v, Ez1[t][xi], and Ezz1[t][xi] |
| 858 |
ProductMV(Ez[t][xi],K,v); |
| 859 |
AddVV(Ez[t][xi],Ez[t][xi],Ez1[t][xi]); |
| 860 |
|
| 861 |
ProductTransposeMM(Ezz[t][xi],K,K); |
| 862 |
SubtractMM(Ezz[t][xi],Ezz1[t][xi],Ezz[t][xi]); |
| 863 |
ForceSymmetric(Ezz[t][xi]); |
| 864 |
|
| 865 |
// Sets L[t](xi) |
| 866 |
// Uses T and v. The diagonal elements of T are positive. |
| 867 |
for (sum=0.0, j=k*k-1; j >= 0; j-=k+1) sum-=log(pElementM(T)[j]); |
| 868 |
ElementV(L,xi)=sum - 0.5*(LOG2PI*k + DotProduct(v,v)); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
// Free data storage |
| 873 |
if (statespace->NonZeroG) |
| 874 |
for (s=n_s-1; s >= 0; s--) |
| 875 |
{ FreeMatrix(G[s]); G[s]=(TMatrix)NULL; } |
| 876 |
|
| 877 |
for (s=n_s-1; s >= 0; s--) |
| 878 |
{ |
| 879 |
FreeMatrix(R[s]); R[s]=(TMatrix)NULL; |
| 880 |
FreeMatrix(H[s]); H[s]=(TMatrix)NULL; |
| 881 |
FreeVector(a[s]); a[s]=(TVector)NULL; |
| 882 |
} |
| 883 |
|
| 884 |
FreeMatrix(K); |
| 885 |
FreeMatrix(T); |
| 886 |
FreeMatrix(Y); |
| 887 |
FreeVector(v); |
| 888 |
FreeVector(cy); |
| 889 |
FreeMatrix(Eyy1); |
| 890 |
FreeVector(Ey1); |
| 891 |
} |
| 892 |
else |
| 893 |
for (xi=n_xi-1; xi >= 0; xi--) |
| 894 |
{ |
| 895 |
EquateVector(Ez[t][xi],Ez1[t][xi]); |
| 896 |
EquateMatrix(Ezz[t][xi],Ezz1[t][xi]); |
| 897 |
ElementV(L,xi)=0.0; |
| 898 |
} |
| 899 |
|
| 900 |
// Set Pxi[t] |
| 901 |
// Uses Pxi1[t] |
| 902 |
scale=MINUS_INFINITY; |
| 903 |
for (xi=n_xi-1; xi >= 0; xi--) |
| 904 |
{ |
| 905 |
if (ElementV(Pxi1[t],xi) > 0.0) |
| 906 |
{ |
| 907 |
scale=AddScaledLogs(1.0,scale,ElementV(Pxi1[t],xi),ElementV(L,xi)); |
| 908 |
ElementV(Pxi[t],xi)=log(ElementV(Pxi1[t],xi)) + ElementV(L,xi); |
| 909 |
} |
| 910 |
else |
| 911 |
ElementV(Pxi[t],xi)=MINUS_INFINITY; |
| 912 |
} |
| 913 |
|
| 914 |
for (xi=n_xi-1; xi >= 0; xi--) |
| 915 |
if (ElementV(Pxi[t],xi) != MINUS_INFINITY) |
| 916 |
ElementV(Pxi[t],xi)=exp(ElementV(Pxi[t],xi) - scale); |
| 917 |
else |
| 918 |
ElementV(Pxi[t],xi)=0.0; |
| 919 |
} |
| 920 |
|
| 921 |
// Sets IEz[t1] and IEzz[t1] |
| 922 |
// Uses Ez[t1], Ezz[t1], and Pxi[t1] |
| 923 |
IntegrateStatesSingleV(IEz[t1],Pxi[t1],Ez[t1],n_zeta,n_s,2); |
| 924 |
IntegrateStatesSingleM(IEzz[t1],Pxi[t1],Ezz[t1],n_zeta,n_s,2); |
| 925 |
|
| 926 |
// Clean up |
| 927 |
FreeMatrix(X); |
| 928 |
FreeVector(L); |
| 929 |
if (statespace->NonZeroG) dw_FreeArray(G); |
| 930 |
dw_FreeArray(R); |
| 931 |
dw_FreeArray(H); |
| 932 |
dw_FreeArray(a); |
| 933 |
dw_FreeArray(IEzz_i); |
| 934 |
dw_FreeArray(IEz_i); |
| 935 |
|
| 936 |
dw_SetTerminalErrors(terminal_errors); |
| 937 |
dw_SetVerboseErrors(verbose_errors); |
| 938 |
|
| 939 |
return 1; |
| 940 |
} |
| 941 |
#undef LOG2PI |
| 942 |
|
| 943 |
/* |
| 944 |
Assumes: |
| 945 |
IR : horizon x (nvar*nshocks) matrix |
| 946 |
S : integer array of length horizon |
| 947 |
model : valid pointer to TStateModel/T_MSStateSpace structure |
| 948 |
flag : equal to IR_STATES or IR_OBSERVABLES |
| 949 |
|
| 950 |
Returns: |
| 951 |
One upon success and zero otherwise. |
| 952 |
|
| 953 |
Results: |
| 954 |
Fills IR with the impulse responses of the variables with respect to the |
| 955 |
shocks. |
| 956 |
|
| 957 |
Notes: |
| 958 |
The current value of the parameters implicit in model are used to form the |
| 959 |
impulse responses. The element in position (t,i+j*nvar) is the response of |
| 960 |
the ith state variable to the jth shock at horizon t. Horizon 0 is the |
| 961 |
contemporaneous response. |
| 962 |
|
| 963 |
If flag is equal to IR_STATES then the variables are the state variables and |
| 964 |
the shocks are the fundamental shocks. If flag is equal to IR_OBSERVABLES |
| 965 |
then the variables are the observables and the shocks are both the |
| 966 |
fundamental shocks and the measurement error. The fundamental shocks are |
| 967 |
ordered first. |
| 968 |
*/ |
| 969 |
int dw_state_space_impulse_response(TMatrix IR, int *S, TStateModel *model, int flag) |
| 970 |
{ |
| 971 |
TVector *z, y=(TVector)NULL, e; |
| 972 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 973 |
int i, j, t; |
| 974 |
|
| 975 |
// check that there are shocks and variables |
| 976 |
if (flag & IR_OBSERVABLES) |
| 977 |
{ if ((statespace->nepsilon + statespace->nu <= 0) || (statespace->ny <= 0)) return 0; } |
| 978 |
else |
| 979 |
{ if ((statespace->nepsilon <= 0) || (statespace->nz <= 0)) return 0; } |
| 980 |
|
| 981 |
// initialize structures |
| 982 |
if ((statespace->t0 < 0) && !Initialize_MSStateSpace(model)) return 0; |
| 983 |
InitializeMatrix(IR,0.0); |
| 984 |
|
| 985 |
// create observable vector if necessary |
| 986 |
if (flag & IR_OBSERVABLES) y=CreateVector(statespace->ny); |
| 987 |
|
| 988 |
// contemporaneous response to fundamental shocks |
| 989 |
if (statespace->nepsilon > 0) |
| 990 |
{ |
| 991 |
z=dw_CreateArray_vector(statespace->nepsilon); |
| 992 |
InitializeVector(e=CreateVector(statespace->nepsilon),0.0); |
| 993 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 994 |
{ |
| 995 |
ElementV(e,j)=1.0; |
| 996 |
z[j]=ProductMV((TVector)NULL,statespace->Phiz[S[0]],e); |
| 997 |
if (flag & IR_STATES) |
| 998 |
{ |
| 999 |
for (i=statespace->nz-1; i >= 0; i--) |
| 1000 |
ElementM(IR,0,i+j*statespace->nz)=ElementV(z[j],i); |
| 1001 |
} |
| 1002 |
else |
| 1003 |
{ |
| 1004 |
ProductMV(y,statespace->H[S[0]],z[j]); |
| 1005 |
for (i=statespace->ny-1; i >= 0; i--) |
| 1006 |
ElementM(IR,0,i+j*statespace->ny)=ElementV(y,i); |
| 1007 |
} |
| 1008 |
ElementV(e,j)=0.0; |
| 1009 |
} |
| 1010 |
FreeVector(e); |
| 1011 |
} |
| 1012 |
|
| 1013 |
// contemporaneous response to measument error shocks |
| 1014 |
if ((flag & IR_OBSERVABLES) && (statespace->nu > 0)) |
| 1015 |
{ |
| 1016 |
InitializeVector(e=CreateVector(statespace->nu),0.0); |
| 1017 |
for (j=statespace->nu-1; j >= 0; j--) |
| 1018 |
{ |
| 1019 |
ElementV(e,j)=1.0; |
| 1020 |
ProductMV(y,statespace->Phiy[S[0]],e); |
| 1021 |
for (i=statespace->ny-1; i >= 0; i--) |
| 1022 |
ElementM(IR,0,i+(statespace->nepsilon+j)*statespace->ny)=ElementV(y,i); |
| 1023 |
ElementV(e,j)=0.0; |
| 1024 |
} |
| 1025 |
FreeVector(e); |
| 1026 |
} |
| 1027 |
|
| 1028 |
// horizon k response to fundamental shocks |
| 1029 |
if (statespace->nepsilon > 0) |
| 1030 |
{ |
| 1031 |
for (t=1; t < RowM(IR); t++) |
| 1032 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 1033 |
{ |
| 1034 |
ProductMV(z[j],statespace->F[S[t]],z[j]); |
| 1035 |
if (flag & IR_STATES) |
| 1036 |
{ |
| 1037 |
for (i=statespace->nz-1; i >= 0; i--) |
| 1038 |
ElementM(IR,t,i+j*statespace->nz)=ElementV(z[j],i); |
| 1039 |
} |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
ProductMV(y,statespace->H[S[t]],z[j]); |
| 1043 |
for (i=statespace->ny-1; i >= 0; i--) |
| 1044 |
ElementM(IR,t,i+j*statespace->ny)=ElementV(y,i); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
dw_FreeArray(z); |
| 1048 |
} |
| 1049 |
|
| 1050 |
// free memory and return success |
| 1051 |
FreeVector(y); |
| 1052 |
return 1; |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* |
| 1056 |
Assumes |
| 1057 |
VD : horizon x (nvar*nshocks) matrix |
| 1058 |
S : integer array of length horizon |
| 1059 |
model : valid pointer to TStateModel/T_MSStateSpace structure |
| 1060 |
flag : equal to IR_STATES or IR_OBSERVABLES |
| 1061 |
|
| 1062 |
Returns: |
| 1063 |
One upon success and zero otherwise. |
| 1064 |
|
| 1065 |
Results: |
| 1066 |
Fills VD with the variance decompositions of the variables with respect to the |
| 1067 |
shocks. |
| 1068 |
|
| 1069 |
Notes: |
| 1070 |
The element in position (t,j+i*nshocks) of VD is the cummulative contributions |
| 1071 |
to the total variance of ith variable to the jth shock at horizon t. Horizon |
| 1072 |
zero is the contemporaneous contribution. |
| 1073 |
|
| 1074 |
If flag is equal to IR_STATES then the variables are the state variables and |
| 1075 |
the shocks are the fundamental shocks. If flag is equal to IR_OBSERVABLES |
| 1076 |
then the variables are the observables and the shocks are both the |
| 1077 |
fundamental shocks and the measurement error. The fundamental shocks are |
| 1078 |
ordered first. |
| 1079 |
*/ |
| 1080 |
int dw_state_space_variance_decomposition(TMatrix VD, int *S, TStateModel *model, int flag) |
| 1081 |
{ |
| 1082 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1083 |
int t, i, j, rtrn=0, nvars, nshocks; |
| 1084 |
PRECISION *pIR; |
| 1085 |
TMatrix IR=CreateMatrix(RowM(VD),ColM(VD)); |
| 1086 |
if (rtrn=dw_state_space_impulse_response(IR,S,model,flag)) |
| 1087 |
{ |
| 1088 |
// square impulse responses |
| 1089 |
for (pIR=pElementM(IR), j=ColM(IR)*RowM(IR)-1; j >= 0; j--) |
| 1090 |
pIR[j]*=pIR[j]; |
| 1091 |
|
| 1092 |
// accumulate square impulse responses |
| 1093 |
for (t=1; t < RowM(IR); t++) |
| 1094 |
for (j=ColM(IR)-1; j >= 0; j--) |
| 1095 |
ElementM(IR,t,j)+=ElementM(IR,t-1,j); |
| 1096 |
|
| 1097 |
// reorders columns |
| 1098 |
if (flag & IR_STATES) |
| 1099 |
{ |
| 1100 |
nvars=statespace->nz; |
| 1101 |
nshocks=statespace->nepsilon; |
| 1102 |
} |
| 1103 |
else |
| 1104 |
{ |
| 1105 |
nvars=statespace->ny; |
| 1106 |
nshocks=statespace->nepsilon + statespace->nu; |
| 1107 |
} |
| 1108 |
for (t=RowM(VD)-1; t >= 0; t--) |
| 1109 |
for (i=nvars-1; i >= 0; i--) |
| 1110 |
for (j=nshocks-1; j >= 0; j--) |
| 1111 |
ElementM(VD,t,j+i*nshocks)=ElementM(IR,t,i+j*nvars); |
| 1112 |
} |
| 1113 |
FreeMatrix(IR); |
| 1114 |
return rtrn; |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* |
| 1118 |
Assumes: |
| 1119 |
model is a pointer to a properly initialized TStateModel. |
| 1120 |
|
| 1121 |
Returns: |
| 1122 |
1 upon success and 0 otherwise. |
| 1123 |
|
| 1124 |
Results: |
| 1125 |
Draws the discrete Markov state variable from the posterior distribution, |
| 1126 |
conditional on q and theta. The state variable values are stored in |
| 1127 |
model->S. |
| 1128 |
|
| 1129 |
Notes: |
| 1130 |
Calls ForwardRecursion() if necessary. |
| 1131 |
*/ |
| 1132 |
int DrawStates_MSStateSpace(TMatrix Q, TVector *Pxi, TVector *Pxi1, int *S) |
| 1133 |
{ |
| 1134 |
int nstates=RowM(Q), T=dw_DimA(Pxi), t, i, j; |
| 1135 |
PRECISION scale, u, s; |
| 1136 |
|
| 1137 |
//====== Backward recursion ====== |
| 1138 |
if ((u=dw_uniform_rnd()) >= (s=ElementV(Pxi[t=T-1],i=nstates-1))) |
| 1139 |
while (--i > 0) |
| 1140 |
if (u < (s+=ElementV(Pxi[t],i))) break; |
| 1141 |
for (t--; t >= 0; t--) |
| 1142 |
{ |
| 1143 |
scale=1.0/ElementV(Pxi1[t+1],j=i); |
| 1144 |
i=nstates-1; |
| 1145 |
if ((u=dw_uniform_rnd()) >= (s=ElementV(Pxi[t],i)*ElementM(Q,j,i)*scale)) |
| 1146 |
while (--i > 0) |
| 1147 |
if (u < (s+=ElementV(Pxi[t],i)*ElementM(Q,j,i)*scale)) break; |
| 1148 |
} |
| 1149 |
|
| 1150 |
return 1; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* |
| 1154 |
Assumes: |
| 1155 |
f : pointer to valid FILE structure or null pointer |
| 1156 |
filename : pointer to null terminated string or null |
| 1157 |
model : pointer to valid TStateModel structure. |
| 1158 |
|
| 1159 |
Returns: |
| 1160 |
One upon success and zero upon failure. |
| 1161 |
|
| 1162 |
Results: |
| 1163 |
Writes P(s(t) = i | Y(t), theta, q) - which is the element in row t and |
| 1164 |
column i. |
| 1165 |
|
| 1166 |
Notes: |
| 1167 |
One of f or filename should be non-null. |
| 1168 |
*/ |
| 1169 |
int WriteFilteredProbabilities(FILE *f, char *filename, TStateModel *model) |
| 1170 |
{ |
| 1171 |
FILE *f_out; |
| 1172 |
int i, t; |
| 1173 |
if (!Filter(model->nobs,model)) return 0; |
| 1174 |
f_out=f ? f : filename ? fopen(filename,"wt") : (FILE*)NULL; |
| 1175 |
if (f_out) |
| 1176 |
{ |
| 1177 |
for (t=1; t < model->nobs; t++) |
| 1178 |
{ |
| 1179 |
for (i=0; i < model->sv->nbasestates; i++) |
| 1180 |
fprintf(f_out,"%lf ",ProbabilityBaseStateConditionalCurrent(i,t,model)); |
| 1181 |
fprintf(f_out,"\n"); |
| 1182 |
} |
| 1183 |
if (f != f_out) fclose(f_out); |
| 1184 |
return 1; |
| 1185 |
} |
| 1186 |
else |
| 1187 |
return 0; |
| 1188 |
} |
| 1189 |
|
| 1190 |
/* |
| 1191 |
Notes: |
| 1192 |
xi(t+1) = (s(t+1),zeta(t)) = (zeta(t+1),s(t+1-nlags_encoded)) |
| 1193 |
and |
| 1194 |
xi(t+1) = s(t+1)*zeta_modulus + zeta(t) |
| 1195 |
= zeta(t+1)*nbasestates + s(t+1-nlags_encoded) |
| 1196 |
*/ |
| 1197 |
int Smooth_MSStateSpace(TStateModel *model) |
| 1198 |
{ |
| 1199 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1200 |
|
| 1201 |
if (statespace->smoothed) return 1; |
| 1202 |
|
| 1203 |
if (!Filter(statespace->nobs,model) || !ComputeSmoothedProbabilities(model)) return 0; |
| 1204 |
|
| 1205 |
// Compute IEz[nobs][zeta] |
| 1206 |
IntegrateStatesSingleV(statespace->IEz[statespace->nobs],model->V[statespace->nobs],statespace->Ez[statespace->nobs], |
| 1207 |
statespace->zeta_modulus,statespace->nbasestates,2); |
| 1208 |
|
| 1209 |
SmoothMean_MSStateSpace(1,statespace->nobs,statespace->SEz,statespace->ISEz,statespace->Ez1,statespace->Ezz1, |
| 1210 |
statespace->IEz,statespace->IEzz,model->SP,statespace); |
| 1211 |
|
| 1212 |
statespace->smoothed=1; |
| 1213 |
|
| 1214 |
return 1; |
| 1215 |
} |
| 1216 |
|
| 1217 |
/* |
| 1218 |
Assumes: |
| 1219 |
t0 - T > t1 >= t0 >= 0 |
| 1220 |
t1 - T > t1 >= t0 >= 0 |
| 1221 |
SPxi - Vector array of length T |
| 1222 |
Pxi - Vector array of length T, Pxi[t](k) ~ P(xi(t)=k | Y(t), theta, q) |
| 1223 |
Pxi1 - Vector array of length T, Pxi1[t](k) ~ P(xi(t)=k | Y(t-1), theta, q) |
| 1224 |
Q - nstates x nstates transition matrix |
| 1225 |
|
| 1226 |
Returns: |
| 1227 |
Returns one upon success and zero upon failure. |
| 1228 |
|
| 1229 |
Results: |
| 1230 |
Fills SPxi for t0 <= t <= t1 and 0 <= k < nstates. |
| 1231 |
|
| 1232 |
SPxi[t][k]=P(xi(t)=k | Y[t0], theta, q) |
| 1233 |
|
| 1234 |
Notes: |
| 1235 |
Q(i,j) is the probability that xi(t)=i given that xi(t-1)=j. In general, |
| 1236 |
Q could depend on t-1 information. In this function, we do not allow this. |
| 1237 |
*/ |
| 1238 |
int SmoothProbabilities_MSStateSpace(int t0, int t1, TVector *SPxi, TVector *Pxi, TVector *Pxi1, TMatrix Q) |
| 1239 |
{ |
| 1240 |
int nstates=RowM(Q), t=t1, k; |
| 1241 |
|
| 1242 |
//====== Compute Smoothed Probabilities via backward recursion ====== |
| 1243 |
EquateVector(SPxi[t],Pxi[t]); |
| 1244 |
for (t--; t >= t0; t--) |
| 1245 |
{ |
| 1246 |
for (k=nstates-1; k >= 0; k--) |
| 1247 |
ElementV(SPxi[t],k)=(ElementV(Pxi1[t+1],k) > 0.0) ? ElementV(SPxi[t+1],k)/ElementV(Pxi1[t+1],k) : 0.0; |
| 1248 |
|
| 1249 |
ProductVM(SPxi[t],SPxi[t],Q); |
| 1250 |
|
| 1251 |
for (k=nstates-1; k >= 0; k--) |
| 1252 |
ElementV(SPxi[t],k)*=ElementV(Pxi[t],k); |
| 1253 |
} |
| 1254 |
|
| 1255 |
return 1; |
| 1256 |
} |
| 1257 |
|
| 1258 |
/* |
| 1259 |
Computes |
| 1260 |
|
| 1261 |
SEz[t][k] ~ E[z(t) | Y(t1), xi(t+1)=k, theta, q] for t0 <= t < t1 |
| 1262 |
ISEz[t][j] ~ E[z(t) | Y(t1), zeta(t)=j, theta, q] for t0 <= t <= t1 |
| 1263 |
|
| 1264 |
using |
| 1265 |
|
| 1266 |
Ez1(t,xi(t)) ~ E[z(t) | Y(t-1), xi(t), theta, q] t0 < t <= t1 |
| 1267 |
|
| 1268 |
Ezz1(t,xi(t)) ~ E[(z(t) - Ezl(t,xi(t)))(z(t) - Ezl(t,xi(t)))' | Y(t-1), xi(t), theta, q] t0 < t <= t1 |
| 1269 |
|
| 1270 |
IEz(t,zeta(t)) ~ E[z(t) | Y(t), zeta(t), theta, q] t0 <= t <= t1 |
| 1271 |
|
| 1272 |
IEzz(t,zeta(t)) ~ E[(z(t) - Ez(t,zeta(t)))(z(t) - Ez(t,zeta(t)))' | Y(t), zeta(t), theta, q] t0 <= t < t1 |
| 1273 |
*/ |
| 1274 |
void SmoothMean_MSStateSpace(int t0, int t1, TVector **SEz, TVector **ISEz, TVector **Ez1, TMatrix **Ezz1, |
| 1275 |
TVector **IEz, TMatrix **IEzz, TVector *SPxi, T_MSStateSpace *statespace) |
| 1276 |
{ |
| 1277 |
TVector z; |
| 1278 |
TMatrix G; |
| 1279 |
int xi, s, zeta, zeta_t, t=t1; |
| 1280 |
|
| 1281 |
// Computes ISEz[t1][zeta] |
| 1282 |
// zeta = zeta(t1) |
| 1283 |
for (zeta=statespace->zeta_modulus-1; zeta >= 0; zeta--) |
| 1284 |
EquateVector(ISEz[t1][zeta],IEz[t1][zeta]); |
| 1285 |
|
| 1286 |
z=CreateVector(statespace->nz); |
| 1287 |
G=CreateMatrix(statespace->nz,statespace->nz); |
| 1288 |
|
| 1289 |
for (t--; t >= t0; t--) |
| 1290 |
{ |
| 1291 |
// xi = xi(t+1) |
| 1292 |
for (xi=statespace->nstates-1; xi >= 0; xi--) |
| 1293 |
{ |
| 1294 |
// s = s(t+1) |
| 1295 |
s=xi/statespace->zeta_modulus; |
| 1296 |
|
| 1297 |
// zeta = zeta(t+1) |
| 1298 |
zeta=xi/statespace->nbasestates; |
| 1299 |
|
| 1300 |
// zeta_t = zeta(t) |
| 1301 |
zeta_t=xi - s*statespace->zeta_modulus; |
| 1302 |
|
| 1303 |
SubtractVV(z,ISEz[t+1][zeta],Ez1[t+1][xi]); |
| 1304 |
|
| 1305 |
// Use GeneralizedInverse(G,Ezz1[t+1][xi]) instead of |
| 1306 |
// InverseProductMV(z,Ezz1[t+1][xi],z) because |
| 1307 |
// statespace->Ezz1[t+1][xi] can be singular. |
| 1308 |
InverseProductMV(z,Ezz1[t+1][xi],z); |
| 1309 |
//GeneralizedInverse(G,Ezz1[t+1][xi]); |
| 1310 |
//ProductMV(z,G,z); |
| 1311 |
|
| 1312 |
|
| 1313 |
/* Debugging code that prints the condition number of Ezz1[t+1][xi] */ |
| 1314 |
/* GeneralizedInverse(G,Ezz1[t+1][xi]); */ |
| 1315 |
/* ProductMM(G,G,Ezz1[t+1][xi]); */ |
| 1316 |
/* int i; */ |
| 1317 |
/* for (i=0; i < statespace->nz; i++) ElementM(G,i,i)-=1.0; */ |
| 1318 |
/* printf("generalized invers: t=%d zi=%d norm=%le\n",t+1,xi,MatrixNorm(G)); */ |
| 1319 |
/* InverseProductMM(G,Ezz1[t+1][xi],Ezz1[t+1][xi]); */ |
| 1320 |
/* printf("Inverse Product: t=%d zi=%d norm=%le\n",t+1,xi,MatrixNorm(G)); */ |
| 1321 |
/* ProductInverseMM(G,Ezz1[t+1][xi],Ezz1[t+1][xi]); */ |
| 1322 |
/* printf("Product Inverse: t=%d zi=%d norm=%le\n",t+1,xi,MatrixNorm(G)); */ |
| 1323 |
/* TVector d=CreateVector(statespace->nz); */ |
| 1324 |
/* SVD((TMatrix)NULL,d,(TMatrix)NULL,Ezz1[t+1][xi]); */ |
| 1325 |
/* printf("t=%d zi=%d condition=%le/%le\n",t+1,xi,ElementV(d,0),ElementV(d,statespace->nz-1)); */ |
| 1326 |
/* FreeVector(d); */ |
| 1327 |
|
| 1328 |
TransposeProductMV(z,statespace->F[s],z); |
| 1329 |
ProductMV(z,IEzz[t][zeta_t],z); |
| 1330 |
AddVV(SEz[t][xi],IEz[t][zeta_t],z); |
| 1331 |
} |
| 1332 |
|
| 1333 |
IntegrateStatesSingleV(ISEz[t],SPxi[t+1],SEz[t],statespace->nbasestates,statespace->zeta_modulus,1); |
| 1334 |
} |
| 1335 |
|
| 1336 |
FreeVector(z); |
| 1337 |
FreeMatrix(G); |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* |
| 1341 |
Ez[t][v] = E[z(t) | I(t), v(t)=v] for 1 <= t <= T |
| 1342 |
Ez1[t][v] = E[z(t) | I(t+1), v(t+1)=v] for 1 <= t <= T-1 |
| 1343 |
a = nbasestates^(k+1) |
| 1344 |
|
| 1345 |
where |
| 1346 |
|
| 1347 |
v(t) = (s(t), s(t-1), ... , s(t-k)) |
| 1348 |
= s(t)*h^k + s(t-1)*h^(k-1) + ... + s(t-(k-1))*h + s(t-k) |
| 1349 |
|
| 1350 |
for some k >= 0 and h=nbasestates. I(t) is the time t information set. If |
| 1351 |
I(t) = Y[t], then the result will be the filtered shocks. If I(t) = Y[T], |
| 1352 |
then the result will be the smoothed shocks. The information I(t) is |
| 1353 |
determined by the values of the arguments Ez and Ez1. |
| 1354 |
|
| 1355 |
Returns |
| 1356 |
|
| 1357 |
Eepsilon[t][v] = E[epsilon(t) | I(t), v(t)=v] for 2 <= t <= T |
| 1358 |
*/ |
| 1359 |
TVector** ComputeFundamentalShocks(TStateModel *model, TVector **Ez, TVector **Ez1, int a) |
| 1360 |
{ |
| 1361 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1362 |
TMatrix *I; |
| 1363 |
TVector z, **Eepsilon=(TVector**)NULL; |
| 1364 |
int s, v, t, b=a/statespace->nbasestates; |
| 1365 |
if (statespace->nepsilon > 0) |
| 1366 |
{ |
| 1367 |
z=CreateVector(statespace->nz); |
| 1368 |
I=dw_CreateArray_matrix(statespace->nbasestates); |
| 1369 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 1370 |
I[s]=GeneralizedInverse((TMatrix)NULL,statespace->Phiz[s]); |
| 1371 |
|
| 1372 |
Eepsilon=dw_CreateRectangularArray_vector(statespace->nobs+1,a); |
| 1373 |
for (t=statespace->nobs; t > 1; t--) |
| 1374 |
// v = v(t) |
| 1375 |
for (v=a-1; v >= 0; v--) |
| 1376 |
{ |
| 1377 |
// s = s(t) |
| 1378 |
s=v/b; |
| 1379 |
ProductMV(z,statespace->F[s],Ez1[t-1][v]); |
| 1380 |
AddVV(z,z,statespace->b[s]); |
| 1381 |
SubtractVV(z,Ez[t][v],z); |
| 1382 |
Eepsilon[t][v]=ProductMV((TVector)NULL,I[s],z); |
| 1383 |
|
| 1384 |
// test projection |
| 1385 |
/* UpdateProductMV(1.0,z,-1.0,statespace->Phiz[s],Eepsilon[t][v]); */ |
| 1386 |
/* if (Norm(z) > 0.0000001) */ |
| 1387 |
/* { */ |
| 1388 |
/* printf("Projection error: t=%d v=%d norm=%lf\n",t,v,Norm(z)); */ |
| 1389 |
/* if (t < statespace->nobs) printf("norm(Ez[t][v] - Ez1[t][v]) = %lf\n",Norm(SubtractVV(z,Ez[t][v],Ez1[t][v]))); */ |
| 1390 |
/* TVector y=ProductMV((TVector)NULL,statespace->H[0],Ez[t][0]); */ |
| 1391 |
/* AddVV(y,statespace->a[0],y); */ |
| 1392 |
/* SubtractVV(y,statespace->y[t],y); */ |
| 1393 |
/* printf("norm(y(t) - a - H*Ez[t]) = %lf\n",Norm(y)); */ |
| 1394 |
/* FreeVector(y); */ |
| 1395 |
/* ProductMV(z,statespace->F[0],Ez[t][0]); */ |
| 1396 |
/* AddVV(z,statespace->b[0],z); */ |
| 1397 |
/* if (t < statespace->nobs) */ |
| 1398 |
/* { */ |
| 1399 |
/* UpdateProductMV(1.0,z,1.0,statespace->Phiz[0],Eepsilon[t+1][0]); */ |
| 1400 |
/* printf("norm(Ez[t+1] - b - F*Ez[t] - Phyz*Eepsilon[t]) = %lf\n",Norm(SubtractVV(z,Ez[t+1][0],z))); */ |
| 1401 |
/* } */ |
| 1402 |
/* getchar(); */ |
| 1403 |
/* } */ |
| 1404 |
} |
| 1405 |
dw_FreeArray(I); |
| 1406 |
FreeVector(z); |
| 1407 |
} |
| 1408 |
return Eepsilon; |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* |
| 1412 |
ISEz[t][v] = E[z(t) | I(t), v(t)=v] for 1 <= t <= T |
| 1413 |
a = nbasestates^(k+1) |
| 1414 |
|
| 1415 |
where |
| 1416 |
|
| 1417 |
v(t) = (s(t), s(t-1), ... , s(t-k)) |
| 1418 |
= s(t)*h^k + s(t-1)*h^(k-1) + ... + s(t-(k-1))*h + s(t-k) |
| 1419 |
|
| 1420 |
for some k >= 0 and h=nbasestates. I(t) is some information set which could |
| 1421 |
depend on t. |
| 1422 |
|
| 1423 |
Returns |
| 1424 |
|
| 1425 |
Eu[t][v] = E[u(t) | I(t), v(t)=v] |
| 1426 |
*/ |
| 1427 |
TVector** ComputeMeasurmentErrors(TStateModel *model, TVector **Ez, int a) |
| 1428 |
{ |
| 1429 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1430 |
TMatrix *I; |
| 1431 |
TVector y, **Eu=(TVector**)NULL; |
| 1432 |
int s, v, t, b=a/statespace->nbasestates; |
| 1433 |
if (statespace->nu > 0) |
| 1434 |
{ |
| 1435 |
y=CreateVector(statespace->ny); |
| 1436 |
I=dw_CreateArray_matrix(statespace->nbasestates); |
| 1437 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 1438 |
I[s]=GeneralizedInverse((TMatrix)NULL,statespace->Phiy[s]); |
| 1439 |
|
| 1440 |
Eu=dw_CreateRectangularArray_vector(statespace->nobs+1,a); |
| 1441 |
for (t=statespace->nobs; t > 0; t--) |
| 1442 |
// v = v(t) |
| 1443 |
for (v=a-1; v >= 0; v--) |
| 1444 |
{ |
| 1445 |
// s = s(t) |
| 1446 |
s=v/b; |
| 1447 |
ProductMV(y,statespace->H[s],Ez[t][v]); |
| 1448 |
AddVV(y,y,statespace->a[s]); |
| 1449 |
SubtractVV(y,statespace->y[t],y); |
| 1450 |
ProductMV(Eu[t][v],I[s],y); |
| 1451 |
} |
| 1452 |
dw_FreeArray(I); |
| 1453 |
FreeVector(y); |
| 1454 |
} |
| 1455 |
return Eu; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/*******************************************************************************/ |
| 1459 |
/********************************** Utilities **********************************/ |
| 1460 |
/*******************************************************************************/ |
| 1461 |
/* |
| 1462 |
Assumes: |
| 1463 |
X : m x m matrix |
| 1464 |
|
| 1465 |
Results: |
| 1466 |
X = X + X' |
| 1467 |
*/ |
| 1468 |
static void AddTranspose(TMatrix X) |
| 1469 |
{ |
| 1470 |
int j, k, m; |
| 1471 |
PRECISION *pX; |
| 1472 |
if (!X || (RowM(X) != ColM(X))) return; |
| 1473 |
for (m=RowM(X), pX=pElementM(X), j=m*m-2; j > 0; j+=k-1) |
| 1474 |
for (k=j-m+1; k >= 0; j--, k-=m) |
| 1475 |
pX[j]=pX[k]=pX[j]+pX[k]; |
| 1476 |
} |
| 1477 |
|
| 1478 |
/* |
| 1479 |
Assumes: |
| 1480 |
X : n x m matrix |
| 1481 |
|
| 1482 |
Returns: |
| 1483 |
-1 if X is not square and max(fabs(X(i,j) - X(j,i))) otherwise. |
| 1484 |
*/ |
| 1485 |
static PRECISION IsSymmetric(TMatrix X) |
| 1486 |
{ |
| 1487 |
int i, j; |
| 1488 |
PRECISION max; |
| 1489 |
if (!X || (RowM(X) != ColM(X))) return -1.0; |
| 1490 |
|
| 1491 |
for (max=0.0, i=RowM(X)-1; i > 0; i--) |
| 1492 |
for (j=i-1; j >= 0; j--) |
| 1493 |
if (max < fabs(ElementM(X,i,j) - ElementM(X,j,i))) |
| 1494 |
max=fabs(ElementM(X,i,j) - ElementM(X,j,i)); |
| 1495 |
|
| 1496 |
return max; |
| 1497 |
} |
| 1498 |
|
| 1499 |
/*******************************************************************************/ |
| 1500 |
/******************************** Test Routines ********************************/ |
| 1501 |
/*******************************************************************************/ |
| 1502 |
void Check_FilteredInfo(TStateModel *model, PRECISION tol) |
| 1503 |
{ |
| 1504 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1505 |
int s, zeta, xi, t, tmax, smax, zetamax, ximax; |
| 1506 |
TVector y, **Eu; |
| 1507 |
PRECISION max, norm; |
| 1508 |
|
| 1509 |
if (!Filter(statespace->nobs,model)) |
| 1510 |
{ |
| 1511 |
printf("Unable to filter\n"); |
| 1512 |
return; |
| 1513 |
} |
| 1514 |
|
| 1515 |
y=CreateVector(statespace->ny); |
| 1516 |
|
| 1517 |
// Check a[s(t)] + H[s(t)]*Ez[t][xi(t)] + Phiy[s(t)]*Eu[t][xi(t)] = y[t] |
| 1518 |
printf("checking a[s(t)] + H[s(t)]*Ez[t][xi(t)] + Phiy[s(t)]*Eu[t][xi(t)] = y[t]\n"); |
| 1519 |
max=0.0; |
| 1520 |
Eu=ComputeMeasurmentErrors(model,statespace->Ez,statespace->nstates); |
| 1521 |
for (t=1; t <= statespace->nobs; t++) |
| 1522 |
{ |
| 1523 |
for (xi=statespace->nstates-1; xi >= 0; xi--) |
| 1524 |
{ |
| 1525 |
s=model->sv->lag_index[xi][0]; |
| 1526 |
SubtractVV(y,statespace->a[s],statespace->y[t]); |
| 1527 |
UpdateProductMV(1.0,y,1.0,statespace->H[s],statespace->Ez[t][xi]); |
| 1528 |
if (statespace->nu > 0) UpdateProductMV(1.0,y,1.0,statespace->Phiy[s],Eu[t][xi]); |
| 1529 |
if ((norm=Norm(y)) > max) |
| 1530 |
{ |
| 1531 |
max=norm; |
| 1532 |
tmax=t; |
| 1533 |
ximax=xi; |
| 1534 |
smax=s; |
| 1535 |
} |
| 1536 |
if (norm > tol) |
| 1537 |
{ |
| 1538 |
printf("difference = %le (t=%d xi(t)=%d s(t)=%d)\n",norm,t,xi,s); |
| 1539 |
#ifdef DEBUG_PAUSE |
| 1540 |
printf("Press <Enter> to continue"); |
| 1541 |
getchar(); |
| 1542 |
#endif |
| 1543 |
} |
| 1544 |
} |
| 1545 |
} |
| 1546 |
printf("Max difference = %le (t=%d xi(t)=%d s(t)=%d)\n",max,tmax,ximax,smax); |
| 1547 |
dw_FreeArray(Eu); |
| 1548 |
|
| 1549 |
// Check a[s(t)] + H[s(t)]*IEz[t][zeta(t)] = y[t] |
| 1550 |
printf("checking a[s(t)] + H[s(t)]*IEz[t][zeta(t)] + Phiy[s(t)]*Eu[t][zeta(t)] = y[t]\n"); |
| 1551 |
max=0.0; |
| 1552 |
// Compute IEz[nobs][zeta] |
| 1553 |
IntegrateStatesSingleV(statespace->IEz[statespace->nobs],model->V[statespace->nobs],statespace->Ez[statespace->nobs], |
| 1554 |
statespace->zeta_modulus,statespace->nbasestates,2); |
| 1555 |
Eu=ComputeMeasurmentErrors(model,statespace->IEz,statespace->zeta_modulus); |
| 1556 |
for (t=1; t <= statespace->nobs; t++) |
| 1557 |
{ |
| 1558 |
for (xi=statespace->nstates-1; xi >= 0; xi--) |
| 1559 |
{ |
| 1560 |
s=model->sv->lag_index[xi][0]; |
| 1561 |
zeta=xi/statespace->nbasestates; |
| 1562 |
SubtractVV(y,statespace->a[s],statespace->y[t]); |
| 1563 |
UpdateProductMV(1.0,y,1.0,statespace->H[s],statespace->IEz[t][zeta]); |
| 1564 |
if (statespace->nu > 0) UpdateProductMV(1.0,y,1.0,statespace->Phiy[s],Eu[t][zeta]); |
| 1565 |
if ((norm=Norm(y)) > max) |
| 1566 |
{ |
| 1567 |
max=norm; |
| 1568 |
tmax=t; |
| 1569 |
zetamax=xi; |
| 1570 |
smax=s; |
| 1571 |
} |
| 1572 |
if (norm > tol) |
| 1573 |
{ |
| 1574 |
printf("difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",norm,t,zeta,s); |
| 1575 |
#ifdef DEBUG_PAUSE |
| 1576 |
printf("Press <Enter> to continue"); |
| 1577 |
getchar(); |
| 1578 |
#endif |
| 1579 |
} |
| 1580 |
} |
| 1581 |
} |
| 1582 |
printf("Max difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",max,tmax,zetamax,smax); |
| 1583 |
dw_FreeArray(Eu); |
| 1584 |
|
| 1585 |
FreeVector(y); |
| 1586 |
} |
| 1587 |
|
| 1588 |
void Check_SmoothedInfo(TStateModel *model, PRECISION tol) |
| 1589 |
{ |
| 1590 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 1591 |
int i, k, t, tmax, zetamax, smax, ximax, s, zeta, xi; |
| 1592 |
TVector y, z, **ISEz1, **SEepsilon, **SEu, q1, q2; |
| 1593 |
PRECISION max, norm; |
| 1594 |
|
| 1595 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) |
| 1596 |
{ |
| 1597 |
printf("Unable to smooth\n"); |
| 1598 |
return; |
| 1599 |
} |
| 1600 |
|
| 1601 |
// Compute ISEz1[t][zeta] = E[z(t) | Y(T), zeta(t+1)=zeta] 1 <= t < nobs |
| 1602 |
ISEz1=dw_CreateRectangularArray_vector(statespace->nobs+1,statespace->zeta_modulus); |
| 1603 |
for (t=statespace->nobs-1; t > 0; t--) |
| 1604 |
IntegrateStatesSingleV(ISEz1[t],model->SP[t+1],statespace->SEz[t],statespace->zeta_modulus,statespace->nbasestates,2); |
| 1605 |
|
| 1606 |
// Compute smoothed fundamental shocks |
| 1607 |
SEepsilon=ComputeFundamentalShocks(model,statespace->ISEz,ISEz1,statespace->zeta_modulus); |
| 1608 |
|
| 1609 |
// Compute smoothed measurment errors |
| 1610 |
SEu=ComputeMeasurmentErrors(model,statespace->ISEz,statespace->zeta_modulus); |
| 1611 |
|
| 1612 |
// Allocate workspace |
| 1613 |
z=CreateVector(statespace->nz); |
| 1614 |
y=CreateVector(statespace->ny); |
| 1615 |
|
| 1616 |
/////////////////////////////////////////////////////////////////////////////// |
| 1617 |
// Tests xi(t) = s(t-k) + h*s(t-k+1) + ... + (h^k)*s(t) |
| 1618 |
printf("checking lagged encoding\n"); |
| 1619 |
for (xi=statespace->nstates-1; xi >= 0; xi--) |
| 1620 |
{ |
| 1621 |
for (k=0, i=statespace->nlags_encoded; i >= 0; i--) |
| 1622 |
k+=model->sv->lag_index[xi][i]*(int)pow(statespace->nbasestates,statespace->nlags_encoded-i); |
| 1623 |
if (k != xi) |
| 1624 |
printf("Error: %d = xi(t) != s(t-k) + h*s(t-k+1) + ... + (h^k)*s(t) = %d\n",xi,k); |
| 1625 |
} |
| 1626 |
/////////////////////////////////////////////////////////////////////////////// |
| 1627 |
|
| 1628 |
/////////////////////////////////////////////////////////////////////////////// |
| 1629 |
// Test integeration of states |
| 1630 |
q1=CreateVector(statespace->zeta_modulus); |
| 1631 |
q2=CreateVector(statespace->zeta_modulus); |
| 1632 |
printf("testing integration of SP[t+1] against SP[t]\n"); |
| 1633 |
max=0.0; |
| 1634 |
for (t=statespace->nobs-1; t >= 0; t--) |
| 1635 |
{ |
| 1636 |
IntegrateStatesSingle(q1,model->SP[t+1],statespace->nbasestates,statespace->zeta_modulus,1); |
| 1637 |
IntegrateStatesSingle(q2,model->SP[t],statespace->zeta_modulus,statespace->nbasestates,2); |
| 1638 |
SubtractVV(q1,q1,q2); |
| 1639 |
if ((norm=Norm(q1)) > max) |
| 1640 |
{ |
| 1641 |
max=norm; |
| 1642 |
tmax=t; |
| 1643 |
} |
| 1644 |
if (norm > tol) |
| 1645 |
{ |
| 1646 |
printf("difference = %le (t=%d)",norm,t); |
| 1647 |
} |
| 1648 |
} |
| 1649 |
printf("Max difference = %le (t=%d)\n",max,tmax); |
| 1650 |
FreeVector(q2); |
| 1651 |
FreeVector(q1); |
| 1652 |
/////////////////////////////////////////////////////////////////////////////// |
| 1653 |
|
| 1654 |
if (statespace->nu == 0) |
| 1655 |
{ |
| 1656 |
if (statespace->nlags_encoded > 0) |
| 1657 |
{ |
| 1658 |
/////////////////////////////////////////////////////////////////////////////// |
| 1659 |
// Check y[t] = a[s(t)] + H[s(t)]*ISEz[t][zeta(t)] |
| 1660 |
printf("checking y[t] = a[s(t)] + H[s(t)]*ISEz[t][zeta(t)]\n"); |
| 1661 |
max=0.0; |
| 1662 |
for (t=1; t <= statespace->nobs; t++) |
| 1663 |
// zeta = zeta(t) |
| 1664 |
for (zeta=statespace->zeta_modulus-1; zeta >= 0; zeta--) |
| 1665 |
{ |
| 1666 |
// s = s(t) |
| 1667 |
s=model->sv->lag_index[statespace->nbasestates*zeta][0]; |
| 1668 |
SubtractVV(y,statespace->a[s],statespace->y[t]); |
| 1669 |
UpdateProductMV(1.0,y,1.0,statespace->H[s],statespace->ISEz[t][zeta]); |
| 1670 |
if ((norm=Norm(y)) > max) |
| 1671 |
{ |
| 1672 |
max=norm; |
| 1673 |
tmax=t; |
| 1674 |
zetamax=zeta; |
| 1675 |
smax=s; |
| 1676 |
} |
| 1677 |
if (norm > tol) |
| 1678 |
{ |
| 1679 |
printf("difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",norm,t,zeta,s); |
| 1680 |
#ifdef DEBUG_PAUSE |
| 1681 |
printf("Press <Enter> to continue"); |
| 1682 |
getchar(); |
| 1683 |
#endif |
| 1684 |
} |
| 1685 |
} |
| 1686 |
printf("Max difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",max,tmax,zetamax,smax); |
| 1687 |
/////////////////////////////////////////////////////////////////////////////// |
| 1688 |
|
| 1689 |
/////////////////////////////////////////////////////////////////////////////// |
| 1690 |
// Check y[t] = a[s(t)] + H[s(t)]*SEz[t][xi(t+1)] |
| 1691 |
printf("checking y[t] = a[s(t)] + H[s(t)]*SEz[t][xi(t+1)]\n"); |
| 1692 |
max=0.0; |
| 1693 |
for (t=1; t < statespace->nobs; t++) |
| 1694 |
// xi = xi(t+1) |
| 1695 |
for (xi=statespace->nstates-1; xi >= 0; xi--) |
| 1696 |
{ |
| 1697 |
// s = s(t) |
| 1698 |
s=model->sv->lag_index[xi][1]; |
| 1699 |
SubtractVV(y,statespace->a[s],statespace->y[t]); |
| 1700 |
UpdateProductMV(1.0,y,1.0,statespace->H[s],statespace->SEz[t][xi]); |
| 1701 |
if ((norm=Norm(y)) > max) |
| 1702 |
{ |
| 1703 |
max=norm; |
| 1704 |
tmax=t; |
| 1705 |
ximax=xi; |
| 1706 |
smax=s; |
| 1707 |
} |
| 1708 |
if (norm > tol) |
| 1709 |
{ |
| 1710 |
printf("difference = %le (t=%d xi(t+1)=%d s(t)=%d)\n",norm,t,xi,s); |
| 1711 |
#ifdef DEBUG_PAUSE |
| 1712 |
printf("Press <Enter> to continue"); |
| 1713 |
getchar(); |
| 1714 |
#endif |
| 1715 |
} |
| 1716 |
} |
| 1717 |
printf("Max difference = %le (t=%d xi(t+1)=%d s(t)=%d)\n",max,tmax,ximax,smax); |
| 1718 |
/////////////////////////////////////////////////////////////////////////////// |
| 1719 |
} |
| 1720 |
|
| 1721 |
} |
| 1722 |
else |
| 1723 |
{ |
| 1724 |
/////////////////////////////////////////////////////////////////////////////// |
| 1725 |
// Check y[t] = a[s(t)] + H[s(t)]*ISEz[t][zeta(t)] + Phiy[s(t)]*SEu[t][zeta(t)] |
| 1726 |
printf("checking y[t] = a[s(t)] + H[s(t)]*ISEz[t][zeta(t)] + Phiy[s(t)]*SEu[t][zeta(t)]\n"); |
| 1727 |
max=0.0; |
| 1728 |
for (t=1; t <= statespace->nobs; t++) |
| 1729 |
// zeta = zeta(t) |
| 1730 |
for (zeta=statespace->zeta_modulus-1; zeta >= 0; zeta--) |
| 1731 |
{ |
| 1732 |
// s = s(t) |
| 1733 |
s=model->sv->lag_index[statespace->nbasestates*zeta][0]; |
| 1734 |
SubtractVV(y,statespace->a[s],statespace->y[t]); |
| 1735 |
UpdateProductMV(1.0,y,1.0,statespace->H[s],statespace->ISEz[t][zeta]); |
| 1736 |
if (statespace->nu > 0) UpdateProductMV(1.0,y,1.0,statespace->Phiy[s],SEu[t][zeta]); |
| 1737 |
if ((norm=Norm(y)) > max) |
| 1738 |
{ |
| 1739 |
max=norm; |
| 1740 |
tmax=t; |
| 1741 |
zetamax=zeta; |
| 1742 |
smax=s; |
| 1743 |
} |
| 1744 |
if (norm > tol) |
| 1745 |
{ |
| 1746 |
printf("difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",norm,t,zeta,s); |
| 1747 |
#ifdef DEBUG_PAUSE |
| 1748 |
printf("Press <Enter> to continue"); |
| 1749 |
getchar(); |
| 1750 |
#endif |
| 1751 |
} |
| 1752 |
} |
| 1753 |
printf("Max difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",max,tmax,zetamax,smax); |
| 1754 |
/////////////////////////////////////////////////////////////////////////////// |
| 1755 |
} |
| 1756 |
|
| 1757 |
/////////////////////////////////////////////////////////////////////////////// |
| 1758 |
// Check y[t] = a[s(t)] + Phiy[s(t)]*SEu[t][zeta(t)] |
| 1759 |
// + H[s(t)]*(b[s(t)] + F[s(t)]*ISEz[t-1][zeta(t)] + Phiz[s(t)]*SEepsilon[t][zeta(t)]) |
| 1760 |
printf("checking y[t] = a[s(t)] + Phiy[s(t)]*SEu[t][zeta(t)]\n"); |
| 1761 |
printf(" + H[s(t)]*(b[s(t)] + F[s(t)]*ISEz[t-1][zeta(t)] + Phiz[s(t)]*SEepsilon[t][zeta(t)])\n"); |
| 1762 |
max=0.0; |
| 1763 |
for (t=2; t <= statespace->nobs; t++) |
| 1764 |
// zeta = zeta(t) |
| 1765 |
for (zeta=statespace->zeta_modulus-1; zeta >= 0; zeta--) |
| 1766 |
{ |
| 1767 |
// s = s(t) |
| 1768 |
s=model->sv->lag_index[statespace->nbasestates*zeta][0]; |
| 1769 |
ProductMV(z,statespace->F[s],ISEz1[t-1][zeta]); |
| 1770 |
AddVV(z,z,statespace->b[s]); |
| 1771 |
if (statespace->nepsilon > 0) UpdateProductMV(1.0,z,1.0,statespace->Phiz[s],SEepsilon[t][zeta]); |
| 1772 |
ProductMV(y,statespace->H[s],z); |
| 1773 |
AddVV(y,y,statespace->a[s]); |
| 1774 |
if (statespace->nu > 0) UpdateProductMV(1.0,y,1.0,statespace->Phiy[s],SEu[t][zeta]); |
| 1775 |
SubtractVV(y,y,statespace->y[t]); |
| 1776 |
if ((norm=Norm(y)) > max) |
| 1777 |
{ |
| 1778 |
max=norm; |
| 1779 |
tmax=t; |
| 1780 |
zetamax=zeta; |
| 1781 |
smax=s; |
| 1782 |
} |
| 1783 |
if (norm > tol) |
| 1784 |
{ |
| 1785 |
printf("dif = %le %% difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",norm,norm/Norm(statespace->y[t]),t,zeta,s); |
| 1786 |
#ifdef DEBUG_PAUSE |
| 1787 |
printf("Press <Enter> to continue"); |
| 1788 |
getchar(); |
| 1789 |
#endif |
| 1790 |
} |
| 1791 |
} |
| 1792 |
printf("Max difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",max,tmax,zetamax,smax); |
| 1793 |
/////////////////////////////////////////////////////////////////////////////// |
| 1794 |
|
| 1795 |
/////////////////////////////////////////////////////////////////////////////// |
| 1796 |
// Check ISEz[t][zeta(t)] = b[s(t)] + F[s(t)]*ISEz1[t-1][zeta(t)] + Phiz[s(t)]*SEepsilon[t][zeta(t)] |
| 1797 |
printf("checking ISEz[t][zeta(t)] = b[s(t)] + F[s(t)]*ISEz1[t-1][zeta(t)] + Phiz[s(t)]*SEepsilon[t][zeta(t)]\n"); |
| 1798 |
max=0.0; |
| 1799 |
for (t=2; t <= statespace->nobs; t++) |
| 1800 |
// zeta = zeta(t) |
| 1801 |
for (zeta=statespace->zeta_modulus-1; zeta >= 0; zeta--) |
| 1802 |
{ |
| 1803 |
// s = s(t) |
| 1804 |
s=model->sv->lag_index[statespace->nbasestates*zeta][0]; |
| 1805 |
SubtractVV(z,statespace->b[s],statespace->ISEz[t][zeta]); |
| 1806 |
UpdateProductMV(1.0,z,1.0,statespace->F[s],ISEz1[t-1][zeta]); |
| 1807 |
if (statespace->nepsilon > 0) UpdateProductMV(1.0,z,1.0,statespace->Phiz[s],SEepsilon[t][zeta]); |
| 1808 |
if ((norm=Norm(z)) > max) |
| 1809 |
{ |
| 1810 |
max=norm; |
| 1811 |
tmax=t; |
| 1812 |
zetamax=zeta; |
| 1813 |
smax=s; |
| 1814 |
} |
| 1815 |
if (norm > tol) |
| 1816 |
{ |
| 1817 |
printf("dif = %le %% difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",norm, |
| 1818 |
norm/Norm(statespace->ISEz[t][zeta]),t,zeta,s); |
| 1819 |
#ifdef DEBUG_PAUSE |
| 1820 |
printf("Press <Enter> to continue"); |
| 1821 |
getchar(); |
| 1822 |
#endif |
| 1823 |
} |
| 1824 |
} |
| 1825 |
printf("Max difference = %le (t=%d zeta(t)=%d s(t)=%d)\n",max,tmax,zetamax,smax); |
| 1826 |
/////////////////////////////////////////////////////////////////////////////// |
| 1827 |
|
| 1828 |
FreeVector(y); |
| 1829 |
FreeVector(z); |
| 1830 |
dw_FreeArray(ISEz1); |
| 1831 |
dw_FreeArray(SEepsilon); |
| 1832 |
dw_FreeArray(SEu); |
| 1833 |
} |