| 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_state_space_historical_decomposition.h" |
| 19 |
#include "dw_matrix_array.h" |
| 20 |
#include "dw_std.h" |
| 21 |
|
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
/* |
| 25 |
Assumes: |
| 26 |
var : (nobs - t0 + 1 + h) x (nepsilon + nu + 2)*ny matrix |
| 27 |
t0 : integer between 1 and nobs - 1 |
| 28 |
h : non-negative integer |
| 29 |
model : pointer to valid TStateModel structure |
| 30 |
|
| 31 |
Returns: |
| 32 |
Fills var. The element in column (nepsilon+nu+2)*i is the ith variable, the |
| 33 |
element in column (nepsilon+nu+2)*i + 1 is the exogeneous component, the |
| 34 |
element in column (nepsilon+nu+2)*i + 2 + j is the cummulative response of |
| 35 |
the ith variable to the jth shock. The fundamental shocks epsilon(t) are |
| 36 |
ordered first. The kth row corresponds to time t0 + k. For k > nobs-t0+1, |
| 37 |
the element in column (nepsilon+nu+2)*i is the forecast of the ith variable. |
| 38 |
The initial condition is at time t0 -- E[z(t0) | Y(T)] |
| 39 |
|
| 40 |
Notes: |
| 41 |
This works when the only time varying parameters are Phiy and Phyz, which |
| 42 |
includes the constant parameter case. Also, nlags_encoded must be bigger |
| 43 |
zero. |
| 44 |
*/ |
| 45 |
TMatrix dw_state_space_historical_decomposition(TMatrix var, int t0, int h, TStateModel *model) |
| 46 |
{ |
| 47 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 48 |
TVector *z_decomp, y, z, u, e, pr, |
| 49 |
*P, // P[t][s] = P(s(t)=s | Y(T)) 1 <= t <= T |
| 50 |
**I1, // I1[t][s] = E[z(t) | Y(T), s(t)=s] 1 <= t <= T |
| 51 |
**I2, // I2[t][s] = E[z(t) | Y(T), s(t+1)=s] 1 <= t <= T-1 |
| 52 |
**SEepsilon=(TVector**)NULL, // SEepsilon[t][s] = E[epsilon(t) | Y(T), s(t)=s] 2 <= t <= T |
| 53 |
**SEu=(TVector**)NULL; // SEu[t][s] = E[u(t) | Y(T), s(t)=s] 2 <= t <= T |
| 54 |
int t, s, i, j, nc=statespace->nepsilon+statespace->nu+2; |
| 55 |
|
| 56 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) return (TMatrix)NULL; |
| 57 |
|
| 58 |
y=CreateVector(statespace->ny); |
| 59 |
z=CreateVector(statespace->nz); |
| 60 |
|
| 61 |
if (statespace->nlags_encoded > 1) |
| 62 |
{ |
| 63 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->zeta_modulus,statespace->nbasestates,2); |
| 64 |
I1=IntegrateStatesV((TVector**)NULL,P,statespace->ISEz,statespace->nbasestates, |
| 65 |
statespace->zeta_modulus/statespace->nbasestates,2); |
| 66 |
dw_FreeArray(P); |
| 67 |
} |
| 68 |
else |
| 69 |
if (statespace->nlags_encoded > 0) |
| 70 |
I1=statespace->ISEz; |
| 71 |
else |
| 72 |
{ |
| 73 |
printf("Not yet implemented\n"); |
| 74 |
dw_exit(0); |
| 75 |
} |
| 76 |
|
| 77 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->nbasestates,statespace->zeta_modulus,2); |
| 78 |
|
| 79 |
if (statespace->nlags_encoded > 0) |
| 80 |
{ |
| 81 |
I2=dw_CreateRectangularArray_vector(statespace->nobs+1,statespace->nbasestates); |
| 82 |
for (t=statespace->nobs-1; t > 0; t--) |
| 83 |
IntegrateStatesSingleV(I2[t],model->SP[t+1],statespace->SEz[t],statespace->nbasestates,statespace->zeta_modulus,2); |
| 84 |
} |
| 85 |
else |
| 86 |
I2=statespace->SEz; |
| 87 |
|
| 88 |
// Compute smoothed measurment shocks |
| 89 |
SEu=ComputeMeasurmentErrors(model,I1,statespace->nbasestates); |
| 90 |
|
| 91 |
// Compute smoothed fundamental shocks |
| 92 |
SEepsilon=ComputeFundamentalShocks(model,I1,I2,statespace->nbasestates); |
| 93 |
|
| 94 |
if (!var) var=CreateMatrix(statespace->nobs-t0+1+h,nc*statespace->ny); |
| 95 |
InitializeMatrix(var,0.0); |
| 96 |
|
| 97 |
z_decomp=dw_CreateArray_vector(statespace->nepsilon+1); |
| 98 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 99 |
InitializeVector(z_decomp[j]=CreateVector(statespace->nz),0.0); |
| 100 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 101 |
LinearCombinationV(z_decomp[0],1.0,z_decomp[0],ElementV(P[t0],s),I1[t0][s]); |
| 102 |
|
| 103 |
e=(statespace->nepsilon > 0) ? CreateVector(statespace->nepsilon) : (TVector)NULL; |
| 104 |
u=(statespace->nu > 0) ? CreateVector(statespace->nu) : (TVector)NULL; |
| 105 |
pr=EquateVector((TVector)NULL,P[statespace->nobs]); |
| 106 |
|
| 107 |
// data |
| 108 |
for (t=t0; t <= statespace->nobs; ) |
| 109 |
{ |
| 110 |
// record data |
| 111 |
for (i=statespace->ny-1; i >= 0; i--) |
| 112 |
ElementM(var,t-t0,nc*i)=ElementV(statespace->y[t],i); |
| 113 |
|
| 114 |
// Compute cummulative effects of fundamental shocks on observables and record - (requires H[s]=H and a[s]=a) |
| 115 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 116 |
{ |
| 117 |
ProductMV(y,statespace->H[0],z_decomp[j]); |
| 118 |
for (i=statespace->ny-1; i >= 0; i--) |
| 119 |
ElementM(var,t-t0,nc*i+j+1)=ElementV(y,i); |
| 120 |
} |
| 121 |
for (i=statespace->ny-1; i >= 0; i--) |
| 122 |
ElementM(var,t-t0,nc*i+1)+=ElementV(statespace->a[0],i); |
| 123 |
|
| 124 |
// Compute effects of measurment shocks on observables and record |
| 125 |
for (j=statespace->nu-1; j >= 0; j--) |
| 126 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 127 |
{ |
| 128 |
InitializeVector(u,0.0); |
| 129 |
ElementV(u,j)=ElementV(P[t],s)*ElementV(SEu[t][s],j); |
| 130 |
ProductMV(y,statespace->Phiy[s],e); |
| 131 |
for (i=statespace->ny-1; i >= 0; i--) |
| 132 |
ElementM(var,t-t0,nc*i+statespace->nepsilon+j+2)=ElementV(y,i); |
| 133 |
} |
| 134 |
|
| 135 |
// Compute next period cummulative effects |
| 136 |
t++; |
| 137 |
// Uses F[s]=F |
| 138 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 139 |
ProductMV(z_decomp[j],statespace->F[0],z_decomp[j]); |
| 140 |
if (t <= statespace->nobs) |
| 141 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 142 |
UpdateV(1.0,z_decomp[0],ElementV(P[t],s),statespace->b[s]); |
| 143 |
else |
| 144 |
{ |
| 145 |
ProductMV(pr,model->sv->baseQ,pr); |
| 146 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 147 |
UpdateV(1.0,z_decomp[0],ElementV(pr,s),statespace->b[s]); |
| 148 |
} |
| 149 |
|
| 150 |
// Add shocks |
| 151 |
if (t <= statespace->nobs) |
| 152 |
{ |
| 153 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 154 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 155 |
{ |
| 156 |
InitializeVector(e,0.0); |
| 157 |
ElementV(e,j)=ElementV(P[t],s)*ElementV(SEepsilon[t][s],j); |
| 158 |
ProductMV(z,statespace->Phiz[s],e); |
| 159 |
AddVV(z_decomp[j+1],z_decomp[j+1],z); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// forecasts |
| 165 |
for ( ; t <= statespace->nobs+h; ) |
| 166 |
{ |
| 167 |
// Compute cummulative effects of fundamental shocks on observables and record - (requires H[s]=H and a[s]=a) |
| 168 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 169 |
{ |
| 170 |
ProductMV(y,statespace->H[0],z_decomp[j]); |
| 171 |
for (i=statespace->ny-1; i >= 0; i--) |
| 172 |
{ |
| 173 |
ElementM(var,t-t0,nc*i+j+1)=ElementV(y,i); |
| 174 |
ElementM(var,t-t0,nc*i)+=ElementV(y,i); |
| 175 |
} |
| 176 |
} |
| 177 |
for (i=statespace->ny-1; i >= 0; i--) |
| 178 |
{ |
| 179 |
ElementM(var,t-t0,nc*i+1)+=ElementV(statespace->a[0],i); |
| 180 |
ElementM(var,t-t0,nc*i)+=ElementV(statespace->a[0],i); |
| 181 |
} |
| 182 |
|
| 183 |
// Compute next period cummulative effects |
| 184 |
t++; |
| 185 |
// Uses F[s]=F |
| 186 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 187 |
ProductMV(z_decomp[j],statespace->F[0],z_decomp[j]); |
| 188 |
ProductMV(pr,model->sv->baseQ,pr); |
| 189 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 190 |
UpdateV(1.0,z_decomp[0],ElementV(pr,s),statespace->b[s]); |
| 191 |
} |
| 192 |
|
| 193 |
FreeVector(u); |
| 194 |
FreeVector(y); |
| 195 |
FreeVector(e); |
| 196 |
FreeVector(z); |
| 197 |
FreeVector(pr); |
| 198 |
dw_FreeArray(z_decomp); |
| 199 |
if (I1 != statespace->ISEz) dw_FreeArray(I1); |
| 200 |
if (I2 != statespace->SEz) dw_FreeArray(I2); |
| 201 |
dw_FreeArray(SEepsilon); |
| 202 |
dw_FreeArray(SEu); |
| 203 |
dw_FreeArray(P); |
| 204 |
|
| 205 |
return var; |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
Assumes: |
| 210 |
var : (nobs - t0 + 1 + h) x (nepsilon + 2)*nz matrix |
| 211 |
t0 : integer between 1 and nobs - 1 |
| 212 |
model : pointer to valid TStateModel structure |
| 213 |
|
| 214 |
Returns: |
| 215 |
Fills var. The element in column (nepsilon+2)*i is E[z(t) | Y(t)], the |
| 216 |
element in column (nepsilon+2)*i + 1 is the exogeneous component, the |
| 217 |
element in column (nepsilon+nu+2)*i + 2 + j is the cummulative response of |
| 218 |
the ith state variable to the jth shock. The kth row corresponds to time |
| 219 |
t0 + k. The initial condition is at time t0 -- E[z(t0) | Y(T)] |
| 220 |
|
| 221 |
Notes: |
| 222 |
This works when the only time varying parameters are Phiy and Phyz, which |
| 223 |
includes the constant parameter case. Also, nlags_encoded must be bigger |
| 224 |
zero. |
| 225 |
*/ |
| 226 |
TMatrix dw_state_space_historical_decomposition_states(TMatrix var, int t0, int h, TStateModel *model) |
| 227 |
{ |
| 228 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 229 |
TVector *z_decomp, z, e, pr, |
| 230 |
*P, // P[t][s] = P(s(t)=s | Y(T)) 1 <= t <= T |
| 231 |
**I1, // I1[t][s] = E[z(t) | Y(T), s(t)=s] 1 <= t <= T |
| 232 |
**I2, // I2[t][s] = E[z(t) | Y(T), s(t+1)=s] 1 <= t <= T-1 |
| 233 |
**SEepsilon=(TVector**)NULL; // SEepsilon[t][s] = E[epsilon(t) | Y(T), s(t)=s] 2 <= t <= T |
| 234 |
int t, s, i, j, nc=statespace->nepsilon+2; |
| 235 |
|
| 236 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) return (TMatrix)NULL; |
| 237 |
|
| 238 |
z=CreateVector(statespace->nz); |
| 239 |
|
| 240 |
if (statespace->nlags_encoded > 1) |
| 241 |
{ |
| 242 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->zeta_modulus,statespace->nbasestates,2); |
| 243 |
I1=IntegrateStatesV((TVector**)NULL,P,statespace->ISEz,statespace->nbasestates, |
| 244 |
statespace->zeta_modulus/statespace->nbasestates,2); |
| 245 |
dw_FreeArray(P); |
| 246 |
} |
| 247 |
else |
| 248 |
if (statespace->nlags_encoded > 0) |
| 249 |
I1=statespace->ISEz; |
| 250 |
else |
| 251 |
{ |
| 252 |
printf("Not yet implemented\n"); |
| 253 |
dw_exit(0); |
| 254 |
} |
| 255 |
|
| 256 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->nbasestates,statespace->zeta_modulus,2); |
| 257 |
|
| 258 |
if (statespace->nlags_encoded > 0) |
| 259 |
{ |
| 260 |
I2=dw_CreateRectangularArray_vector(statespace->nobs+1,statespace->nbasestates); |
| 261 |
ComputeTransitionMatrix(model->nobs,model); |
| 262 |
for (t=statespace->nobs-1; t > 0; t--) |
| 263 |
IntegrateStatesSingleV(I2[t],model->SP[t+1],statespace->SEz[t],statespace->nbasestates,statespace->zeta_modulus,2); |
| 264 |
} |
| 265 |
else |
| 266 |
I2=statespace->SEz; |
| 267 |
|
| 268 |
// Compute smoothed fundamental shocks |
| 269 |
SEepsilon=ComputeFundamentalShocks(model,I1,I2,statespace->nbasestates); |
| 270 |
|
| 271 |
if (!var) var=CreateMatrix(statespace->nobs-t0+1+h,nc*statespace->nz); |
| 272 |
InitializeMatrix(var,0.0); |
| 273 |
|
| 274 |
z_decomp=dw_CreateArray_vector(statespace->nepsilon+1); |
| 275 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 276 |
InitializeVector(z_decomp[j]=CreateVector(statespace->nz),0.0); |
| 277 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 278 |
LinearCombinationV(z_decomp[0],1.0,z_decomp[0],ElementV(P[t0],s),I1[t0][s]); |
| 279 |
|
| 280 |
e=(statespace->nepsilon > 0) ? CreateVector(statespace->nepsilon) : (TVector)NULL; |
| 281 |
pr=EquateVector((TVector)NULL,P[statespace->nobs]); |
| 282 |
|
| 283 |
for (t=t0; t <= statespace->nobs; ) |
| 284 |
{ |
| 285 |
// compute and record E[z[t] | Y[t]) |
| 286 |
for (i=statespace->nz-1; i >= 0; i--) |
| 287 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 288 |
ElementM(var,t-t0,nc*i)+=ElementV(P[t],s)*ElementV(I1[t][s],i); |
| 289 |
|
| 290 |
// record cummulative effects of fundamental shocks on states |
| 291 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 292 |
for (i=statespace->nz-1; i >= 0; i--) |
| 293 |
ElementM(var,t-t0,nc*i+j+1)=ElementV(z_decomp[j],i); |
| 294 |
|
| 295 |
// Compute next period cummulative effects |
| 296 |
t++; |
| 297 |
// Uses F[s]=F and b[s]=b |
| 298 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 299 |
ProductMV(z_decomp[j],statespace->F[0],z_decomp[j]); |
| 300 |
if (t <= statespace->nobs) |
| 301 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 302 |
UpdateV(1.0,z_decomp[0],ElementV(P[t],s),statespace->b[s]); |
| 303 |
else |
| 304 |
{ |
| 305 |
ProductMV(pr,model->sv->baseQ,pr); |
| 306 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 307 |
UpdateV(1.0,z_decomp[0],ElementV(pr,s),statespace->b[s]); |
| 308 |
} |
| 309 |
|
| 310 |
if (t <= statespace->nobs) |
| 311 |
{ |
| 312 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 313 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 314 |
{ |
| 315 |
InitializeVector(e,0.0); |
| 316 |
ElementV(e,j)=ElementV(P[t],s)*ElementV(SEepsilon[t][s],j); |
| 317 |
ProductMV(z,statespace->Phiz[s],e); |
| 318 |
AddVV(z_decomp[j+1],z_decomp[j+1],z); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// forecasts |
| 324 |
for ( ; t <= statespace->nobs+h; ) |
| 325 |
{ |
| 326 |
// record cummulative effects of fundamental shocks on states |
| 327 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 328 |
for (i=statespace->nz-1; i >= 0; i--) |
| 329 |
{ |
| 330 |
ElementM(var,t-t0,nc*i+j+1)=ElementV(z_decomp[j],i); |
| 331 |
ElementM(var,t-t0,nc*i)+=ElementV(z_decomp[j],i); |
| 332 |
} |
| 333 |
|
| 334 |
// Compute next period cummulative effects |
| 335 |
t++; |
| 336 |
// Uses F[s]=F and b[s]=b |
| 337 |
for (j=statespace->nepsilon; j >= 0; j--) |
| 338 |
ProductMV(z_decomp[j],statespace->F[0],z_decomp[j]); |
| 339 |
ProductMV(pr,model->sv->baseQ,pr); |
| 340 |
for (s=statespace->nbasestates-1; s >= 0; s--) |
| 341 |
UpdateV(1.0,z_decomp[0],ElementV(pr,s),statespace->b[s]); |
| 342 |
} |
| 343 |
|
| 344 |
FreeVector(e); |
| 345 |
FreeVector(z); |
| 346 |
FreeVector(pr); |
| 347 |
dw_FreeArray(z_decomp); |
| 348 |
if (I1 != statespace->ISEz) dw_FreeArray(I1); |
| 349 |
if (I2 != statespace->SEz) dw_FreeArray(I2); |
| 350 |
dw_FreeArray(SEepsilon); |
| 351 |
dw_FreeArray(P); |
| 352 |
|
| 353 |
return var; |
| 354 |
} |
| 355 |
|
| 356 |
TVector* SmoothedShocks(TStateModel *model) |
| 357 |
{ |
| 358 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 359 |
TVector *P, // P[t][s] = P(s(t)=s | Y(T)) 1 <= t <= T |
| 360 |
**I1, // I1[t][s] = E[z(t) | Y(T), s(t)=s] 1 <= t <= T |
| 361 |
**I2, // I2[t][s] = E[z(t) | Y(T), s(t+1)=s] 1 <= t <= T-1 |
| 362 |
**SEepsilon=(TVector**)NULL, // SEepsilon[t][s] = E[epsilon(t) | Y(T), s(t)=s] 1 <= t <= T |
| 363 |
*rtrn=(TVector*)NULL; // rtrn[t] = E[epsilon(t) | Y(T)] 1 <= t <= T |
| 364 |
int t, k; |
| 365 |
|
| 366 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) return (TVector*)NULL; |
| 367 |
|
| 368 |
if (statespace->nlags_encoded > 1) |
| 369 |
{ |
| 370 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->zeta_modulus,statespace->nbasestates,2); |
| 371 |
I1=IntegrateStatesV((TVector**)NULL,P,statespace->ISEz,statespace->nbasestates, |
| 372 |
statespace->zeta_modulus/statespace->nbasestates,2); |
| 373 |
dw_FreeArray(P); |
| 374 |
} |
| 375 |
else |
| 376 |
if (statespace->nlags_encoded > 0) |
| 377 |
I1=statespace->ISEz; |
| 378 |
else |
| 379 |
{ |
| 380 |
printf("Not yet implemented\n"); |
| 381 |
dw_exit(0); |
| 382 |
} |
| 383 |
|
| 384 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->nbasestates,statespace->zeta_modulus,2); |
| 385 |
|
| 386 |
if (statespace->nlags_encoded > 0) |
| 387 |
{ |
| 388 |
I2=dw_CreateRectangularArray_vector(statespace->nobs+1,statespace->nbasestates); |
| 389 |
ComputeTransitionMatrix(model->nobs,model); |
| 390 |
for (t=statespace->nobs-1; t > 0; t--) |
| 391 |
IntegrateStatesSingleV(I2[t],model->SP[t+1],statespace->SEz[t],statespace->nbasestates,statespace->zeta_modulus,2); |
| 392 |
} |
| 393 |
else |
| 394 |
I2=statespace->SEz; |
| 395 |
|
| 396 |
// Compute smoothed fundamental shocks |
| 397 |
SEepsilon=ComputeFundamentalShocks(model,I1,I2,statespace->nbasestates); |
| 398 |
|
| 399 |
rtrn=dw_CreateArray_vector(statespace->nobs+1); |
| 400 |
for (t=statespace->nobs; t > 1; t--) |
| 401 |
for (rtrn[t]=ProductVS((TVector)NULL,SEepsilon[t][0],ElementV(P[t],0)), k=statespace->nbasestates-1; k > 0; k--) |
| 402 |
UpdateV(1.0,rtrn[t],ElementV(P[t],k),SEepsilon[t][k]); |
| 403 |
|
| 404 |
dw_FreeArray(SEepsilon); |
| 405 |
dw_FreeArray(P); |
| 406 |
if (I1 != statespace->ISEz) dw_FreeArray(I1); |
| 407 |
if (I2 != statespace->SEz) dw_FreeArray(I2); |
| 408 |
|
| 409 |
return rtrn; |
| 410 |
} |
| 411 |
|
| 412 |
TVector* SmoothedStates(TStateModel *model) |
| 413 |
{ |
| 414 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 415 |
TVector *P, // P[t][s] = P(s(t)=s | Y(T)) 1 <= t <= T |
| 416 |
**I1, // I1[t][0] = E[z(t) | Y(T)] 1 <= t <= T |
| 417 |
*rtrn=(TVector*)NULL; // rtrn[t] = E[z(t) | Y(T)] 1 <= t <= T |
| 418 |
int t; |
| 419 |
|
| 420 |
if (!Smooth_MSStateSpace(model) || !ComputeSmoothedProbabilities(model)) return (TVector*)NULL; |
| 421 |
|
| 422 |
if (statespace->nlags_encoded > 0) |
| 423 |
{ |
| 424 |
P=IntegrateStates((TVector*)NULL,model->SP,statespace->zeta_modulus,statespace->nbasestates,2); |
| 425 |
I1=IntegrateStatesV((TVector**)NULL,P,statespace->ISEz,1,statespace->zeta_modulus,2); |
| 426 |
dw_FreeArray(P); |
| 427 |
} |
| 428 |
else |
| 429 |
I1=statespace->ISEz; |
| 430 |
|
| 431 |
rtrn=dw_CreateArray_vector(statespace->nobs+1); |
| 432 |
for (t=statespace->nobs; t >= 1; t--) |
| 433 |
rtrn[t]=EquateVector((TVector)NULL,I1[t][0]); |
| 434 |
|
| 435 |
if (I1 != statespace->ISEz) dw_FreeArray(I1); |
| 436 |
|
| 437 |
return rtrn; |
| 438 |
} |
| 439 |
|
| 440 |
/*******************************************************************************/ |
| 441 |
/******************************** Test Routines ********************************/ |
| 442 |
/*******************************************************************************/ |
| 443 |
void CheckHistoricalDecomposition(TMatrix var, int verbose, TStateModel *model) |
| 444 |
{ |
| 445 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 446 |
PRECISION sum, max=0.0, dif, pmax=0.0, pdif; |
| 447 |
int i, j, t, tmax, imax, ptmax, pimax, nc=ColM(var)/statespace->ny; |
| 448 |
printf("Checking sums in historical decomposition\n"); |
| 449 |
for (t=RowM(var)-1; t >= 0; t--) |
| 450 |
{ |
| 451 |
if (verbose) printf("t=%d ",t); |
| 452 |
for (i=statespace->ny-1; i >= 0; i--) |
| 453 |
{ |
| 454 |
for (sum=0.0, j=nc-1; j > 0; j--) sum+=ElementM(var,t,i*nc+j); |
| 455 |
dif=fabs(sum-ElementM(var,t,i*nc)); |
| 456 |
pdif=(ElementM(var,t,i*nc) != 0) ? 100.0*fabs(dif/ElementM(var,t,i*nc)) : -1.0; |
| 457 |
if (dif > max) { max=dif; tmax=t; imax=i; } |
| 458 |
if (pdif > pmax) { pmax=pdif; ptmax=t; pimax=i; } |
| 459 |
if (verbose) printf("%lf ",dif); |
| 460 |
} |
| 461 |
if (verbose) printf("\n"); |
| 462 |
} |
| 463 |
printf("Max error: %le - t=%d variable=%d\n",max,tmax,imax); |
| 464 |
printf("Max %% error: %lf - t=%d variable=%d\n",pmax,ptmax,pimax); |
| 465 |
} |