| 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_impulse_response_special.h" |
| 19 |
#include "dw_MSStateSpace.h" |
| 20 |
#include "dw_metropolis_simulation.h" |
| 21 |
#include "dw_matrix_array.h" |
| 22 |
#include "dw_histogram.h" |
| 23 |
#include "dw_parse_cmd.h" |
| 24 |
#include "dw_math.h" |
| 25 |
#include "dw_std.h" |
| 26 |
|
| 27 |
#define size_special 1 |
| 28 |
int ComputeSpecial(TVector special, TVector z, TVector y) |
| 29 |
{ |
| 30 |
if (!z) return 1; |
| 31 |
// (1.0/u_y)*(y_t - c_y*c_t - i_y*i_t - g_y*g_t) |
| 32 |
ElementV(special,0) = 6.113390270546515*(ElementV(z,0) - 0.163575357656760*ElementV(z,1) |
| 33 |
- 0.157481335414194*ElementV(z,2) - 0.18*ElementV(z,16)); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
/* |
| 39 |
Assumes: |
| 40 |
IR : (h+1) x (size_special*(nepsilon+nu)) matrix |
| 41 |
S : integer array of length h+1 |
| 42 |
h : non-negative integer |
| 43 |
model : valid pointer to TStateModel/T_MSStateSpace structure |
| 44 |
|
| 45 |
Returns: |
| 46 |
One upon success and zero otherwise. |
| 47 |
|
| 48 |
Results: |
| 49 |
Fills IR with the impulse responses of the observable variables with respect |
| 50 |
to the fundamental and measurement error shocks for horizons 0 through h |
| 51 |
inclusive. |
| 52 |
|
| 53 |
Notes: |
| 54 |
The current value of the parameters implicit in model are used to form the |
| 55 |
impulse responses. The element in position (k,i+j*size_special) is the |
| 56 |
response of the ith observable variable to the jth shock at horizon k. |
| 57 |
Shocks 0 through nepsilon-1 are fundamental and shocks nepsilon through |
| 58 |
nepsilon+nu-1 are measurement error. Horizon 0 is the contemporaneous |
| 59 |
response. |
| 60 |
*/ |
| 61 |
int dw_state_space_impulse_response_special(TMatrix IR, int *S, int h, TStateModel *model) |
| 62 |
{ |
| 63 |
TMatrix rtrn; |
| 64 |
TVector *z, y, e, special; |
| 65 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 66 |
int i, j, k, err; |
| 67 |
|
| 68 |
// check that there are fundamental or measurement shocks |
| 69 |
if (statespace->nepsilon + statespace->nu == 0) return 0; |
| 70 |
|
| 71 |
// initialize structure |
| 72 |
if ((statespace->t0 < 0) && !Initialize_MSStateSpace(model)) return 0; |
| 73 |
|
| 74 |
// allocate memory and initialize |
| 75 |
special=CreateVector(size_special); |
| 76 |
y=CreateVector(statespace->ny); |
| 77 |
InitializeMatrix(IR,0.0); |
| 78 |
|
| 79 |
// contemporaneous response to fundamental shocks |
| 80 |
if (statespace->nepsilon > 0) |
| 81 |
{ |
| 82 |
z=dw_CreateArray_vector(statespace->nepsilon); |
| 83 |
InitializeVector(e=CreateVector(statespace->nepsilon),0.0); |
| 84 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 85 |
{ |
| 86 |
ElementV(e,j)=1.0; |
| 87 |
z[j]=ProductMV((TVector)NULL,statespace->Phiz[S[0]],e); |
| 88 |
ProductMV(y,statespace->H[S[0]],z[j]); |
| 89 |
|
| 90 |
// begin special computation |
| 91 |
err=ComputeSpecial(special,z[j],y); |
| 92 |
|
| 93 |
if (!err) |
| 94 |
for (i=size_special-1; i >= 0; i--) |
| 95 |
ElementM(IR,0,i+j*size_special)=ElementV(special,i); |
| 96 |
ElementV(e,j)=0.0; |
| 97 |
} |
| 98 |
FreeVector(e); |
| 99 |
} |
| 100 |
|
| 101 |
// contemporaneous response to measument error shocks |
| 102 |
if (statespace->nu > 0) |
| 103 |
{ |
| 104 |
InitializeVector(e=CreateVector(statespace->nu),0.0); |
| 105 |
for (j=statespace->nu-1; j >= 0; j--) |
| 106 |
{ |
| 107 |
ElementV(e,j)=1.0; |
| 108 |
ProductMV(y,statespace->Phiy[S[0]],e); |
| 109 |
|
| 110 |
// begin special computation |
| 111 |
err=ComputeSpecial(special,(TVector)NULL,y); |
| 112 |
|
| 113 |
if (!err) |
| 114 |
for (i=size_special-1; i >= 0; i--) |
| 115 |
ElementM(IR,0,i+(statespace->nepsilon+j)*size_special)=ElementV(special,i); |
| 116 |
ElementV(e,j)=0.0; |
| 117 |
} |
| 118 |
FreeVector(e); |
| 119 |
} |
| 120 |
|
| 121 |
// horizon k response to fundamental shocks |
| 122 |
if (statespace->nepsilon > 0) |
| 123 |
{ |
| 124 |
for (k=1; k <= h; k++) |
| 125 |
for (j=statespace->nepsilon-1; j >= 0; j--) |
| 126 |
{ |
| 127 |
ProductMV(z[j],statespace->F[S[k]],z[j]); |
| 128 |
ProductMV(y,statespace->H[S[k]],z[j]); |
| 129 |
|
| 130 |
// begin special computation |
| 131 |
err=ComputeSpecial(special,z[j],y); |
| 132 |
|
| 133 |
if (!err) |
| 134 |
for (i=size_special-1; i >= 0; i--) |
| 135 |
ElementM(IR,k,i+j*size_special)=ElementV(special,i); |
| 136 |
} |
| 137 |
dw_FreeArray(z); |
| 138 |
} |
| 139 |
|
| 140 |
// free memory and return success |
| 141 |
FreeVector(y); |
| 142 |
FreeVector(special); |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
Assumes |
| 148 |
f_out : valid FILE pointer |
| 149 |
s : constant regime for the impluse response. |
| 150 |
percentiles : vector of numbers between 0 and 1 inclusive |
| 151 |
posterior_draws : each row contains log posterior, log likelihood, and posterior draw |
| 152 |
thin : thinning factor |
| 153 |
h : non-negative integer |
| 154 |
model : point to valid TStateModel/T_MSStateSpace structure |
| 155 |
|
| 156 |
Results: |
| 157 |
Computes and prints to the file f_out the requested percentiles of the |
| 158 |
impulse responses of the observables variables to the fundamental and |
| 159 |
measurement error shocks. |
| 160 |
|
| 161 |
Returns: |
| 162 |
One upon success and zero otherwise. |
| 163 |
|
| 164 |
Notes: |
| 165 |
For each percentile, the element in position (k,i+j*size_special) is the |
| 166 |
response of the ith observable variable to the jth shock at horizon k. |
| 167 |
Shocks 0 through nepsilon-1 are fundamental and shocks nepsilon through |
| 168 |
nepsilon+nu-1 are measurement error. Horizon 0 is the contemporaneous |
| 169 |
response. |
| 170 |
*/ |
| 171 |
int dw_state_space_impulse_response_special_percentile_regime(FILE *f_out, int s, TVector percentiles, TMatrix posterior_draws, |
| 172 |
int thin, int h, TStateModel *model) |
| 173 |
{ |
| 174 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 175 |
int rtrn=0, ntheta=NumberFreeParametersTheta(model), nq=NumberFreeParametersQ(model), *S, i, j, k; |
| 176 |
TVector x; |
| 177 |
TMatrix IR; |
| 178 |
TMatrixHistogram *histogram; |
| 179 |
|
| 180 |
// quick check of passed parameters |
| 181 |
if (!f_out || !percentiles || !posterior_draws || (thin <= 0) || (h < 0) || !model) return 0; |
| 182 |
|
| 183 |
// allocate memory |
| 184 |
x=CreateVector(ColM(posterior_draws)); |
| 185 |
S=(int*)dw_malloc((h+1)*sizeof(int)); |
| 186 |
IR=CreateMatrix(h+1,size_special*(statespace->nepsilon+statespace->nu)); |
| 187 |
histogram=CreateMatrixHistogram(RowM(IR),ColM(IR),40,HISTOGRAM_VARIABLE); |
| 188 |
|
| 189 |
// set S to s |
| 190 |
for (i=0; i <= h; i++) S[i]=s; |
| 191 |
|
| 192 |
for (i=RowM(posterior_draws)-1; i >= 0; i-=thin) |
| 193 |
{ |
| 194 |
// Get free parameters and push them into model |
| 195 |
RowVector(x,posterior_draws,i); |
| 196 |
if (ntheta > 0) ConvertFreeParametersToTheta(model,pElementV(x)+2); |
| 197 |
if (nq > 0) ConvertFreeParametersToQ(model,pElementV(x)+2+ntheta); |
| 198 |
|
| 199 |
// Get transition matrix -- this should be modified if transition matrix is time varying |
| 200 |
if ((model->sv->t1 < model->nobs) && !sv_ComputeTransitionMatrix(model->nobs,model->sv,model)) |
| 201 |
goto ERROR_EXIT; |
| 202 |
|
| 203 |
// Compute impulse response |
| 204 |
if (!dw_state_space_impulse_response_special(IR,S,h,model)) |
| 205 |
goto ERROR_EXIT; |
| 206 |
|
| 207 |
// Accumulate impulse response |
| 208 |
AddMatrixObservation(IR,histogram); |
| 209 |
} |
| 210 |
|
| 211 |
for (i=0; i < DimV(percentiles); i++) |
| 212 |
{ |
| 213 |
MatrixPercentile(IR,ElementV(percentiles,i),histogram); |
| 214 |
dw_PrintMatrix(f_out,IR,"%lg "); |
| 215 |
fprintf(f_out,"\n"); |
| 216 |
} |
| 217 |
|
| 218 |
rtrn=1; |
| 219 |
|
| 220 |
ERROR_EXIT: |
| 221 |
FreeMatrixHistogram(histogram); |
| 222 |
FreeMatrix(IR); |
| 223 |
dw_free(S); |
| 224 |
FreeVector(x); |
| 225 |
|
| 226 |
return rtrn; |
| 227 |
} |
| 228 |
|
| 229 |
/* |
| 230 |
Assumes |
| 231 |
f_out : valid FILE pointer |
| 232 |
percentiles : vector of numbers between 0 and 1 inclusive |
| 233 |
posterior_draws : each row contains log posterior, log likelihood, and posterior draw |
| 234 |
thin : thinning factor |
| 235 |
init_prob : vector or null pointer |
| 236 |
h : non-negative integer |
| 237 |
model : point to valid TStateModel/T_MSStateSpace structure |
| 238 |
|
| 239 |
Results: |
| 240 |
Computes and prints to the file f_out the requested percentiles of the |
| 241 |
impulse responses of the observables variables to the fundamental and |
| 242 |
measurement error shocks. |
| 243 |
|
| 244 |
Returns: |
| 245 |
One upon success and zero otherwise. |
| 246 |
|
| 247 |
Notes: |
| 248 |
For each percentile, the element in position (k,i+j*ny) is the response of |
| 249 |
the ith observable variable to the jth shock at horizon k. Shocks 0 through |
| 250 |
nepsilon-1 are fundamental and shocks nepsilon through nepsilon+nu-1 are |
| 251 |
measurement error. Horizon 0 is the contemporaneous response. |
| 252 |
*/ |
| 253 |
int dw_state_space_impulse_response_special_percentile_ergodic(FILE *f_out, TVector percentiles, TMatrix posterior_draws, |
| 254 |
int thin, TVector init_prob, int h, TStateModel *model) |
| 255 |
{ |
| 256 |
T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); |
| 257 |
int rtrn=0, ntheta=NumberFreeParametersTheta(model), nq=NumberFreeParametersQ(model), *S, use_ergodic, i, j, k; |
| 258 |
TVector x, prob; |
| 259 |
TMatrix IR; |
| 260 |
TMatrixHistogram *histogram; |
| 261 |
|
| 262 |
// quick check of passed parameters |
| 263 |
if (!f_out || !percentiles || !posterior_draws || (thin <= 0) || (h < 0) || !model) return 0; |
| 264 |
|
| 265 |
// allocate memory |
| 266 |
x=CreateVector(ColM(posterior_draws)); |
| 267 |
prob=CreateVector(model->sv->nstates); |
| 268 |
S=(int*)dw_malloc((h+1)*sizeof(int)); |
| 269 |
IR=CreateMatrix(h+1,size_special*(statespace->nepsilon+statespace->nu)); |
| 270 |
histogram=CreateMatrixHistogram(RowM(IR),ColM(IR),40,HISTOGRAM_VARIABLE); |
| 271 |
|
| 272 |
// Use ergodic initial probability |
| 273 |
if (!init_prob) |
| 274 |
{ |
| 275 |
use_ergodic=1; |
| 276 |
init_prob=CreateVector(model->sv->nstates); |
| 277 |
} |
| 278 |
else |
| 279 |
use_ergodic=0; |
| 280 |
|
| 281 |
for (i=RowM(posterior_draws)-1; i >= 0; i-=thin) |
| 282 |
{ |
| 283 |
// Get free parameters and push them into model |
| 284 |
RowVector(x,posterior_draws,i); |
| 285 |
if (ntheta > 0) ConvertFreeParametersToTheta(model,pElementV(x)+2); |
| 286 |
if (nq > 0) ConvertFreeParametersToQ(model,pElementV(x)+2+ntheta); |
| 287 |
|
| 288 |
// Get transition matrix -- this should be modified if transition matrix is time varying |
| 289 |
if ((model->sv->t1 < model->nobs) && !sv_ComputeTransitionMatrix(model->nobs,model->sv,model)) |
| 290 |
goto ERROR_EXIT; |
| 291 |
|
| 292 |
// Get ergodic distribution if necessary |
| 293 |
if (use_ergodic) Ergodic(init_prob,model->sv->Q); |
| 294 |
|
| 295 |
// Draw regimes |
| 296 |
S[0]=DrawDiscrete(init_prob); |
| 297 |
for (j=1; j <= h; j++) |
| 298 |
{ |
| 299 |
ColumnVector(prob,model->sv->Q,S[j-1]); |
| 300 |
S[j]=DrawDiscrete(prob); |
| 301 |
} |
| 302 |
|
| 303 |
// Compute impulse response |
| 304 |
if (!dw_state_space_impulse_response_special(IR,S,h,model)) |
| 305 |
goto ERROR_EXIT; |
| 306 |
|
| 307 |
// Accumulate impulse response |
| 308 |
AddMatrixObservation(IR,histogram); |
| 309 |
} |
| 310 |
|
| 311 |
for (i=0; i < DimV(percentiles); i++) |
| 312 |
{ |
| 313 |
MatrixPercentile(IR,ElementV(percentiles,i),histogram); |
| 314 |
dw_PrintMatrix(f_out,IR,"%lg "); |
| 315 |
fprintf(f_out,"\n"); |
| 316 |
} |
| 317 |
|
| 318 |
rtrn=1; |
| 319 |
|
| 320 |
ERROR_EXIT: |
| 321 |
if (use_ergodic) dw_free(init_prob); |
| 322 |
FreeMatrixHistogram(histogram); |
| 323 |
FreeMatrix(IR); |
| 324 |
dw_free(S); |
| 325 |
FreeVector(prob); |
| 326 |
FreeVector(x); |
| 327 |
|
| 328 |
return rtrn; |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
Command line parameters |
| 333 |
|
| 334 |
-t : filename tag |
| 335 |
-percentiles n p_1 p_2 ... p_n : Percentiles to compute. The first parameter |
| 336 |
after percentiles must be the number of percentiles and the following |
| 337 |
values are the actual percentiles. (default = 3 0.16 0.50 0.84) |
| 338 |
-horizon : The horizon over which to compute the impulse responses. (default = 12) |
| 339 |
-thin : Thinning factor. Only 1/thin of the draws in posterior draws file are used. (default = 1) |
| 340 |
-ergodic : Print impuluse responses using ergodic distribution as initial condition for shocks. |
| 341 |
-regimes : Print impulse responses as if each regime were permanent. |
| 342 |
-regime s : Print impulse responses to regime s as if this regime were permanent. |
| 343 |
|
| 344 |
*/ |
| 345 |
void dw_state_space_impulse_response_special_command_line(int nargs, char **args, TStateModel *model) |
| 346 |
{ |
| 347 |
FILE *f_in, *f_out; |
| 348 |
char *model_id, *in_filename, *out_filename, *fmt, *tag; |
| 349 |
TMatrix posterior_draws; |
| 350 |
TVector percentiles=(TVector)NULL, init_prob=(TVector)NULL; |
| 351 |
int s, i, j, n, nparameters=NumberFreeParametersTheta(model) + NumberFreeParametersQ(model), h, thin, states, observables; |
| 352 |
|
| 353 |
// MSStateSpace model? |
| 354 |
if (!(model_id=ModelType(model)) || strcmp(model_id,"T_MSStateSpace")) |
| 355 |
{ |
| 356 |
printf("dw_state_space_forecast_command_line(): Model type = %s\n",model_id ? model_id : "null"); |
| 357 |
return; |
| 358 |
} |
| 359 |
|
| 360 |
// Get tag from command line |
| 361 |
tag=dw_ParseString(nargs,args,'t',(char*)NULL); |
| 362 |
|
| 363 |
// Read posterior draws |
| 364 |
printf("Reading posterior draws\n"); |
| 365 |
posterior_draws=dw_ReadPosteriorDraws((FILE*)NULL,(char*)NULL,tag,nparameters); |
| 366 |
if (!posterior_draws) |
| 367 |
{ |
| 368 |
printf("dw_state_space_impulse_response_command_line(): Unable to read posterior draws\n"); |
| 369 |
printf(" tag: %s\n",tag ? tag : "null"); |
| 370 |
dw_exit(0); |
| 371 |
} |
| 372 |
|
| 373 |
// Setup percentiles, horizon, thinning factor |
| 374 |
if ((i=dw_FindArgument_String(nargs,args,"percentiles")) == -1) |
| 375 |
{ |
| 376 |
percentiles=CreateVector(3); |
| 377 |
ElementV(percentiles,0)=0.16; ElementV(percentiles,1)=0.5; ElementV(percentiles,2)=0.84; |
| 378 |
} |
| 379 |
else |
| 380 |
if ((i+1 < nargs) && dw_IsInteger(args[i+1]) && ((n=atoi(args[i+1])) > 0) && (i+1+n < nargs)) |
| 381 |
{ |
| 382 |
percentiles=CreateVector(n); |
| 383 |
for (j=0; j < n; j++) |
| 384 |
if (!dw_IsFloat(args[i+2+j])|| ((ElementV(percentiles,j)=atof(args[i+2+j])) <= 0.0) |
| 385 |
|| (ElementV(percentiles,j) >= 1.0)) break; |
| 386 |
if (j < n) |
| 387 |
{ |
| 388 |
FreeVector(percentiles); |
| 389 |
printf("dw_state_space_impulse_response_command_line(): Error parsing percentiles\n"); |
| 390 |
dw_exit(0); |
| 391 |
} |
| 392 |
} |
| 393 |
else |
| 394 |
{ |
| 395 |
printf("dw_state_space_impulse_response_command_line(): Error parsing percentiles\n"); |
| 396 |
dw_exit(0); |
| 397 |
} |
| 398 |
h=dw_ParseInteger_String(nargs,args,"horizon",8); |
| 399 |
thin=dw_ParseInteger_String(nargs,args,"thin",1); |
| 400 |
|
| 401 |
// ergodic impulse responses |
| 402 |
if (dw_FindArgument_String(nargs,args,"ergodic") != -1) |
| 403 |
{ |
| 404 |
// Open output file for impulse responses of observables |
| 405 |
fmt="ir_special_percentiles_ergodic_%s.prn"; |
| 406 |
sprintf(out_filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 407 |
f_out=fopen(out_filename,"wt"); |
| 408 |
dw_free(out_filename); |
| 409 |
|
| 410 |
// Compute percentiles for impulse responses of observables |
| 411 |
printf("Constructing percentiles for impulse responses of special - ergodic\n"); |
| 412 |
dw_state_space_impulse_response_special_percentile_ergodic(f_out,percentiles,posterior_draws,thin,init_prob,h,model); |
| 413 |
fclose(f_out); |
| 414 |
} |
| 415 |
|
| 416 |
if (dw_FindArgument_String(nargs,args,"regimes") != -1) |
| 417 |
for (s=0; s < model->sv->nstates; s++) |
| 418 |
{ |
| 419 |
// Open output file for impulse responses of observables |
| 420 |
fmt="ir_special_percentiles_regime_%d_%s.prn"; |
| 421 |
sprintf(out_filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) + 10),fmt,s,tag); |
| 422 |
f_out=fopen(out_filename,"wt"); |
| 423 |
dw_free(out_filename); |
| 424 |
|
| 425 |
// Compute percentiles for impulse responses of observables |
| 426 |
printf("Constructing percentiles for impulse responses of special - regime %d\n",s); |
| 427 |
dw_state_space_impulse_response_special_percentile_regime(f_out,s,percentiles,posterior_draws,thin,h,model); |
| 428 |
fclose(f_out); |
| 429 |
} |
| 430 |
else |
| 431 |
if (((s=dw_ParseInteger_String(nargs,args,"regime",-1)) >= 0) && (s < model->sv->nstates)) |
| 432 |
{ |
| 433 |
// Open output file for impulse responses of observables |
| 434 |
fmt="ir_special_percentiles_regime_%d_%s.prn"; |
| 435 |
sprintf(out_filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) + 10),fmt,s,tag); |
| 436 |
f_out=fopen(out_filename,"wt"); |
| 437 |
dw_free(out_filename); |
| 438 |
|
| 439 |
// Compute percentiles for impulse responses of observables |
| 440 |
printf("Constructing percentiles for impulse responses of special - regime %d\n",s); |
| 441 |
dw_state_space_impulse_response_special_percentile_regime(f_out,s,percentiles,posterior_draws,thin,h,model); |
| 442 |
fclose(f_out); |
| 443 |
} |
| 444 |
|
| 445 |
FreeMatrix(posterior_draws); |
| 446 |
FreeVector(percentiles); |
| 447 |
} |