| 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_switch.h" |
| 19 |
#include "dw_switchio.h" |
| 20 |
#include "dw_histogram.h" |
| 21 |
#include "dw_matrix_sort.h" |
| 22 |
#include "VARio.h" |
| 23 |
#include "VARbase.h" |
| 24 |
#include "dw_sbvar_command_line.h" |
| 25 |
#include "dw_parse_cmd.h" |
| 26 |
#include "dw_ascii.h" |
| 27 |
#include "dw_error.h" |
| 28 |
#include "dw_math.h" |
| 29 |
#include "dw_std.h" |
| 30 |
|
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <math.h> |
| 34 |
|
| 35 |
// define to use ECB normalization versions |
| 36 |
//#define ECB_VERSION |
| 37 |
|
| 38 |
static int GetNumberDraws(FILE *f) |
| 39 |
{ |
| 40 |
int count=0, n; |
| 41 |
char *buffer=(char*)NULL; |
| 42 |
rewind(f); |
| 43 |
while ((buffer=dw_ReadLine(f,buffer,&n))) count++; |
| 44 |
rewind(f); |
| 45 |
return count; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
|
| 50 |
*/ |
| 51 |
static TVector* RandomSamplePosterior(FILE *posterior_file, __attribute__ ((unused)) int nposterior_draws, __attribute__ ((unused)) int size) |
| 52 |
{ |
| 53 |
TVector *sample=(TVector*)NULL; |
| 54 |
rewind(posterior_file); |
| 55 |
|
| 56 |
|
| 57 |
rewind(posterior_file); |
| 58 |
return sample; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
Assumes |
| 63 |
ir : horizon x nvar*nvar matrix |
| 64 |
S : integer array of length horizon |
| 65 |
A0_Xi : nvar x nvar matrix equal to A0[S[0]]*Xi[S[0]] |
| 66 |
B : h dimensional array of nvar x nvar*nlags matrices or null pointer. |
| 67 |
See GetIRMatrices() for a description of B. A null pointer implies |
| 68 |
that nlags is zero. |
| 69 |
|
| 70 |
Notes: |
| 71 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 72 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 73 |
response. |
| 74 |
*/ |
| 75 |
void impulse_response(TMatrix ir, int *S, TMatrix A0_Xi, TMatrix* B) |
| 76 |
{ |
| 77 |
int nvars=RowM(A0_Xi), horizon=RowM(ir), t, i, j; |
| 78 |
TMatrix Y_t, X_t, epsilon_t; |
| 79 |
|
| 80 |
Y_t=CreateMatrix(nvars,nvars); |
| 81 |
epsilon_t=IdentityMatrix((TMatrix)NULL,nvars); |
| 82 |
|
| 83 |
ProductInverseMM(Y_t,epsilon_t,A0_Xi); |
| 84 |
for (i=nvars-1; i >= 0; i--) |
| 85 |
for (j=nvars-1; j >= 0; j--) |
| 86 |
ElementM(ir,0,nvars*i+j)=ElementM(Y_t,i,j); |
| 87 |
|
| 88 |
if (B) |
| 89 |
{ |
| 90 |
InitializeMatrix(X_t=CreateMatrix(nvars,RowM(B[0])),0.0); |
| 91 |
for (t=1; t < horizon; t++) |
| 92 |
{ |
| 93 |
for (i=nvars-1; i >= 0; i--) |
| 94 |
{ |
| 95 |
for (j=RowM(B[0])-1; j >= nvars; j--) ElementM(X_t,i,j)=ElementM(X_t,i,j-nvars); |
| 96 |
for (j=nvars-1; j >= 0; j--) ElementM(X_t,i,j)=ElementM(Y_t,i,j); |
| 97 |
} |
| 98 |
ProductMM(Y_t,X_t,B[S[t]]); |
| 99 |
for (i=nvars-1; i >= 0; i--) |
| 100 |
for (j=nvars-1; j >= 0; j--) |
| 101 |
ElementM(ir,t,nvars*i+j)=ElementM(Y_t,i,j); |
| 102 |
} |
| 103 |
FreeMatrix(X_t); |
| 104 |
} |
| 105 |
else |
| 106 |
for (t=horizon-1; t > 0; t--) |
| 107 |
for (j=ColM(ir)-1; j >= 0; j--) |
| 108 |
ElementM(ir,t,j)=0.0; |
| 109 |
|
| 110 |
FreeMatrix(Y_t); |
| 111 |
FreeMatrix(epsilon_t); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
/* |
| 116 |
Computes the matrices A0(s)*Xi(s) and B(s) = Ahat(s) * inverse(A0(s)) |
| 117 |
where Ahat(s) are the first nvars*nlags rows of Aplus(s), for 0 <= s < h. |
| 118 |
The integer h is the number of regimes. Both A0_Xi and B must be matrix |
| 119 |
arrays of length h and each element of A0_Xi and B must be of the proper |
| 120 |
size. |
| 121 |
*/ |
| 122 |
static void GetIRMatrices(int h, TMatrix *A0_Xi, TMatrix *B, T_VAR_Parameters *p) |
| 123 |
{ |
| 124 |
TMatrix Aplus; |
| 125 |
int i, j, k; |
| 126 |
PRECISION tmp; |
| 127 |
for (k=h-1; k >= 0; k--) |
| 128 |
{ |
| 129 |
MakeA0(A0_Xi[k],k,p); |
| 130 |
Aplus=MakeAplus((TMatrix)NULL,k,p); |
| 131 |
SubMatrix(B[k],Aplus,0,0,p->nvars*p->nlags,p->nvars); |
| 132 |
FreeMatrix(Aplus); |
| 133 |
ProductInverseMM(B[k],B[k],A0_Xi[k]); |
| 134 |
for (j=p->nvars-1; j >= 0; j--) |
| 135 |
for (tmp=sqrt(p->Zeta[j][p->var_states[j][k]]), i=p->nvars-1; i >= 0; i--) |
| 136 |
ElementM(A0_Xi[k],i,j)*=tmp; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/* |
| 141 |
Returns the largest of the absolute values of the eigenvalues of the companion |
| 142 |
matrix of B |
| 143 |
*/ |
| 144 |
PRECISION LargestRoot(TMatrix B) |
| 145 |
{ |
| 146 |
TMatrix X; |
| 147 |
TVector re_eig, im_eig; |
| 148 |
int i, k=RowM(B), n=ColM(B); |
| 149 |
PRECISION max, tmp; |
| 150 |
InitializeMatrix(X=CreateMatrix(k,k),0.0); |
| 151 |
for (i=n; i < k; i++) ElementM(X,i-n,i)=1.0; |
| 152 |
InsertSubMatrix(X,B,0,0,0,0,k,n); |
| 153 |
re_eig=CreateVector(k); |
| 154 |
im_eig=CreateVector(k); |
| 155 |
Eigenvalues(re_eig,im_eig,X); |
| 156 |
max=ElementV(re_eig,0)*ElementV(re_eig,0)+ElementV(im_eig,0)*ElementV(im_eig,0); |
| 157 |
for (i=k-1; i > 0; i--) |
| 158 |
if ((tmp=ElementV(re_eig,i)*ElementV(re_eig,i)+ElementV(im_eig,i)*ElementV(im_eig,i)) > max) max=tmp;; |
| 159 |
FreeVector(im_eig); |
| 160 |
FreeVector(re_eig); |
| 161 |
FreeMatrix(X); |
| 162 |
return sqrt(max); |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
Returns 1 if any of the squares of the eigenvalues of the companion matrix of |
| 167 |
B are greater than max. |
| 168 |
*/ |
| 169 |
int IsExplosive(TMatrix B, PRECISION max) |
| 170 |
{ |
| 171 |
TMatrix X; |
| 172 |
TVector re_eig, im_eig; |
| 173 |
int i, k=RowM(B), n=ColM(B); |
| 174 |
InitializeMatrix(X=CreateMatrix(k,k),0.0); |
| 175 |
for (i=n; i < k; i++) ElementM(X,i-n,i)=1.0; |
| 176 |
InsertSubMatrix(X,B,0,0,0,0,k,n); |
| 177 |
re_eig=CreateVector(k); |
| 178 |
im_eig=CreateVector(k); |
| 179 |
Eigenvalues(re_eig,im_eig,X); |
| 180 |
for (i=k-1; i >= 0; i--) |
| 181 |
if (ElementV(re_eig,i)*ElementV(re_eig,i)+ElementV(im_eig,i)*ElementV(im_eig,i) > max) break; |
| 182 |
FreeVector(im_eig); |
| 183 |
FreeVector(re_eig); |
| 184 |
FreeMatrix(X); |
| 185 |
return (i < 0) ? 0 : 1; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
Creates the matrix F if necessary and fills it with the given percentile |
| 190 |
computed from the matrix histogram. IR must be sorted and percentile must |
| 191 |
be between 0 and 1, inclusive. |
| 192 |
*/ |
| 193 |
static TMatrix IRPercentile(TMatrix F, PRECISION percentile, TVector **IR, TStateModel *model) |
| 194 |
{ |
| 195 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 196 |
int i, j, k, n=DimV(IR[0][0])-1, horizon=dw_DimA(IR); |
| 197 |
PRECISION a, b; |
| 198 |
if (!F) |
| 199 |
{ |
| 200 |
if (!(F=CreateMatrix(horizon,p->nvars*p->nvars))) return (TMatrix)NULL; |
| 201 |
} |
| 202 |
else |
| 203 |
if ((RowM(F) != horizon) || (ColM(F) != p->nvars*p->nvars)) |
| 204 |
{ |
| 205 |
dw_Error(SIZE_ERR); |
| 206 |
return (TMatrix)NULL; |
| 207 |
} |
| 208 |
percentile*=n; |
| 209 |
k=(int)floor(percentile); |
| 210 |
b=percentile - k; |
| 211 |
a=1.0-b; |
| 212 |
if (k < n) |
| 213 |
for (i=RowM(F)-1; i >= 0; i--) |
| 214 |
for (j=ColM(F)-1; j >= 0; j--) |
| 215 |
ElementM(F,i,j)=a*ElementV(IR[i][j],k)+b*ElementV(IR[i][j],k+1); |
| 216 |
else |
| 217 |
for (i=RowM(F)-1; i >= 0; i--) |
| 218 |
for (j=ColM(F)-1; j >= 0; j--) |
| 219 |
ElementM(F,i,j)=ElementV(IR[i][j],k); |
| 220 |
return F; |
| 221 |
} |
| 222 |
|
| 223 |
static void SortIR(TVector **IR) |
| 224 |
{ |
| 225 |
int n=dw_DimA(IR), m=dw_DimA(IR[0]), i, j; |
| 226 |
for(i=n-1; i >= 0; i--) |
| 227 |
for (j=m-1; j >= 0; j--) |
| 228 |
{ |
| 229 |
//printf("Sorting irf(%d,%d)\n",i,j); |
| 230 |
SortVectorAscending(IR[i][j],IR[i][j]); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
Assumes |
| 236 |
draws : number of draws of the regime path to make for each posterior draw |
| 237 |
posterior_file : FILE pointer to file containing posterior draws. If null, |
| 238 |
current parameters are used. |
| 239 |
thin : thinning factor to use if posterior_file is not null. |
| 240 |
horizon : non-negative integer |
| 241 |
ergodic : if set, uses the ergodic distribution to draw the initial regime, |
| 242 |
otherwise uses the filtered probabilities at time nobs. |
| 243 |
model : point to valid TStateModel structure |
| 244 |
flag : determines the type of posterior draws file passed. |
| 245 |
|
| 246 |
Results: |
| 247 |
Creates matrix histogram containing the impulse responses. |
| 248 |
|
| 249 |
Returns: |
| 250 |
Pointer to matrix histogram upon success and null pointer on failure. |
| 251 |
|
| 252 |
Notes: |
| 253 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 254 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 255 |
response. |
| 256 |
*/ |
| 257 |
TVector** impulse_response_percentile_ergodic_full(int draws, FILE *posterior_file, int nposterior_draws, int thin, int ergodic, int horizon, TStateModel *model, int flag) |
| 258 |
{ |
| 259 |
T_VAR_Parameters *p; |
| 260 |
int done=0, *S, i, j, k, m, n=1000, r, T=model->nobs, K; |
| 261 |
TMatrix ir, *A0_Xi, *B; |
| 262 |
TVector init_prob, prob, **IR; |
| 263 |
|
| 264 |
// quick check of passed parameters |
| 265 |
if ((nposterior_draws <= 0) || (horizon <= 0) || !model) return 0; |
| 266 |
|
| 267 |
p=(T_VAR_Parameters*)(model->theta); |
| 268 |
K=draws*((nposterior_draws - 1)/thin + 1); |
| 269 |
|
| 270 |
// allocate memory |
| 271 |
S=(int*)dw_malloc(horizon*sizeof(int)); |
| 272 |
ir=CreateMatrix(horizon,p->nvars*p->nvars); |
| 273 |
IR=dw_CreateRectangularArray_vector(horizon,p->nvars*p->nvars); |
| 274 |
for (i=horizon-1; i >= 0; i--) |
| 275 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 276 |
IR[i][j]=CreateVector(K); |
| 277 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 278 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 279 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 280 |
{ |
| 281 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 282 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 283 |
} |
| 284 |
init_prob=CreateVector(p->nstates); |
| 285 |
prob=CreateVector(p->nstates); |
| 286 |
|
| 287 |
k=0; |
| 288 |
while (!done) |
| 289 |
{ |
| 290 |
// Read parameters and push them into model |
| 291 |
if (!posterior_file) |
| 292 |
done=1; |
| 293 |
else |
| 294 |
{ |
| 295 |
#ifdef OCTAVE_MEX_FILE |
| 296 |
OCTAVE_QUIT; |
| 297 |
#elif defined(MATLAB_MEX_FILE) |
| 298 |
if (utIsInterruptPending()) |
| 299 |
{ |
| 300 |
mexPrintf("\n\n** Ctrl-C detected, quitting...\n"); |
| 301 |
dw_exit(0); |
| 302 |
} |
| 303 |
#endif |
| 304 |
dw_NextLine(posterior_file,thin-1); |
| 305 |
if ((k >= K) || !GetPosteriorDraw(posterior_file,model,flag)) |
| 306 |
{ |
| 307 |
done=2; |
| 308 |
printf("total posterior/regime draws processed - %d\n",k); |
| 309 |
} |
| 310 |
else |
| 311 |
if (k == n) |
| 312 |
{ |
| 313 |
printf("%d posterior/regime draws processed\n",k); |
| 314 |
n+=1000; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if (done != 2) |
| 319 |
{ |
| 320 |
// Compute A0_Xi and B |
| 321 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 322 |
|
| 323 |
// Use ergodic initial probability or filtered probability at time T |
| 324 |
if (ergodic) |
| 325 |
Ergodic(init_prob,model->sv->Q); |
| 326 |
else |
| 327 |
for (j=p->nstates-1; j >= 0; j--) |
| 328 |
ElementV(init_prob,j)=ProbabilityStateConditionalCurrent(j,T,model); |
| 329 |
|
| 330 |
for (r=draws; r > 0; r--) |
| 331 |
{ |
| 332 |
// Draw time T regime |
| 333 |
m=DrawDiscrete(init_prob); |
| 334 |
|
| 335 |
// Draw regimes from time T+1 through T+h inclusive |
| 336 |
for (j=0; j < horizon; j++) |
| 337 |
{ |
| 338 |
ColumnVector(prob,model->sv->Q,m); |
| 339 |
S[j]=m=DrawDiscrete(prob); |
| 340 |
} |
| 341 |
|
| 342 |
// Compute impulse response |
| 343 |
impulse_response(ir,S,A0_Xi[S[0]],B); |
| 344 |
|
| 345 |
// Accumulate impulse response |
| 346 |
for (i=horizon-1; i >= 0; i--) |
| 347 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 348 |
ElementV(IR[i][j],k)=ElementM(ir,i,j); |
| 349 |
k++; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
FreeMatrix(ir); |
| 355 |
dw_free(S); |
| 356 |
dw_FreeArray(A0_Xi); |
| 357 |
dw_FreeArray(B); |
| 358 |
FreeVector(prob); |
| 359 |
FreeVector(init_prob); |
| 360 |
|
| 361 |
return IR; |
| 362 |
} |
| 363 |
|
| 364 |
/* |
| 365 |
Assumes |
| 366 |
draws : number of draws of the regime path to make for each posterior draw |
| 367 |
posterior_file : FILE pointer to file containing posterior draws. If null, |
| 368 |
current parameters are used. |
| 369 |
thin : thinning factor to use if posterior_file is not null. |
| 370 |
horizon : non-negative integer |
| 371 |
ergodic : if set, uses the ergodic distribution to draw the initial regime, |
| 372 |
otherwise uses the filtered probabilities at time nobs. |
| 373 |
model : point to valid TStateModel structure |
| 374 |
flag : determines the type of posterior draws file passed. |
| 375 |
|
| 376 |
Results: |
| 377 |
Creates matrix histogram containing the impulse responses. |
| 378 |
|
| 379 |
Returns: |
| 380 |
Pointer to matrix histogram upon success and null pointer on failure. |
| 381 |
|
| 382 |
Notes: |
| 383 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 384 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 385 |
response. |
| 386 |
*/ |
| 387 |
TMatrixHistogram* impulse_response_percentile_ergodic(int draws, FILE *posterior_file, int thin, int ergodic, int horizon, TStateModel *model, int flag) |
| 388 |
{ |
| 389 |
T_VAR_Parameters *p; |
| 390 |
int done=0, *S, i, j, k, m, n=1000, T=model->nobs; |
| 391 |
TMatrix ir, *A0_Xi, *B; |
| 392 |
TMatrixHistogram *histogram; |
| 393 |
TVector init_prob, prob; |
| 394 |
|
| 395 |
// quick check of passed parameters |
| 396 |
if ((horizon <= 0) || !model) return 0; |
| 397 |
|
| 398 |
p=(T_VAR_Parameters*)(model->theta); |
| 399 |
|
| 400 |
// allocate memory |
| 401 |
S=(int*)dw_malloc(horizon*sizeof(int)); |
| 402 |
ir=CreateMatrix(horizon,p->nvars*p->nvars); |
| 403 |
histogram=CreateMatrixHistogram(horizon,p->nvars*p->nvars,100,HISTOGRAM_VARIABLE); |
| 404 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 405 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 406 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 407 |
{ |
| 408 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 409 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 410 |
} |
| 411 |
init_prob=CreateVector(p->nstates); |
| 412 |
prob=CreateVector(p->nstates); |
| 413 |
|
| 414 |
i=0; |
| 415 |
while (!done) |
| 416 |
{ |
| 417 |
// Read parameters and push them into model |
| 418 |
if (!posterior_file) |
| 419 |
done=1; |
| 420 |
else |
| 421 |
{ |
| 422 |
dw_NextLine(posterior_file,thin-1); |
| 423 |
if (!GetPosteriorDraw(posterior_file,model,flag)) |
| 424 |
{ |
| 425 |
done=2; |
| 426 |
printf("total posterior draws processed - %d\n",i); |
| 427 |
} |
| 428 |
else |
| 429 |
if (i++ == n) |
| 430 |
{ |
| 431 |
printf("%d posterior draws processed\n",i); |
| 432 |
n+=1000; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if (done != 2) |
| 437 |
{ |
| 438 |
// Compute A0_Xi and B |
| 439 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 440 |
|
| 441 |
// Use ergodic initial probability or filtered probability at time T |
| 442 |
if (ergodic) |
| 443 |
Ergodic(init_prob,model->sv->Q); |
| 444 |
else |
| 445 |
for (j=p->nstates-1; j >= 0; j--) |
| 446 |
ElementV(init_prob,j)=ProbabilityStateConditionalCurrent(j,T,model); |
| 447 |
|
| 448 |
for (k=draws; k > 0; k--) |
| 449 |
{ |
| 450 |
// Draw time T regime |
| 451 |
m=DrawDiscrete(init_prob); |
| 452 |
|
| 453 |
// Draw regimes from time T+1 through T+h inclusive |
| 454 |
for (j=0; j < horizon; j++) |
| 455 |
{ |
| 456 |
ColumnVector(prob,model->sv->Q,m); |
| 457 |
S[j]=m=DrawDiscrete(prob); |
| 458 |
} |
| 459 |
|
| 460 |
// Compute impulse response |
| 461 |
impulse_response(ir,S,A0_Xi[S[0]],B); |
| 462 |
|
| 463 |
// Accumulate impulse response |
| 464 |
AddMatrixObservation(ir,histogram); |
| 465 |
} |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
FreeMatrix(ir); |
| 470 |
dw_free(S); |
| 471 |
dw_FreeArray(A0_Xi); |
| 472 |
dw_FreeArray(B); |
| 473 |
FreeVector(prob); |
| 474 |
FreeVector(init_prob); |
| 475 |
|
| 476 |
return histogram; |
| 477 |
} |
| 478 |
|
| 479 |
/* |
| 480 |
Assumes |
| 481 |
posterior_file : FILE pointer to file containing posterior draws. If null, |
| 482 |
current parameters are used. |
| 483 |
thin : thinning factor to use if posterior_file is not null. |
| 484 |
s : fixed regime |
| 485 |
horizon : non-negative integer |
| 486 |
model : point to valid TStateModel structure |
| 487 |
flag : determines the type of posterior draws file passed. |
| 488 |
|
| 489 |
Results: |
| 490 |
Creates matrix histogram containing the impulse responses. |
| 491 |
|
| 492 |
Returns: |
| 493 |
Pointer to matrix histogram upon success and null pointer on failure. |
| 494 |
|
| 495 |
Notes: |
| 496 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 497 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 498 |
response. |
| 499 |
*/ |
| 500 |
TVector** impulse_response_percentile_regime_full(FILE *posterior_file, int nposterior_draws, int thin, int s, int horizon, TStateModel *model, int flag) |
| 501 |
{ |
| 502 |
T_VAR_Parameters *p; |
| 503 |
int done=0, *S, i, j, k, n=1000, K; |
| 504 |
TMatrix ir, *A0_Xi, *B; |
| 505 |
TVector **IR; |
| 506 |
|
| 507 |
// quick check of passed parameters |
| 508 |
if ((nposterior_draws <= 0) || (horizon <= 0) || !model) return 0; |
| 509 |
|
| 510 |
p=(T_VAR_Parameters*)(model->theta); |
| 511 |
K=(nposterior_draws - 1)/thin + 1; |
| 512 |
|
| 513 |
// allocate memory |
| 514 |
S=(int*)dw_malloc(horizon*sizeof(int)); |
| 515 |
for (i=0; i < horizon; i++) S[i]=s; |
| 516 |
ir=CreateMatrix(horizon,p->nvars*p->nvars); |
| 517 |
IR=dw_CreateRectangularArray_vector(horizon,p->nvars*p->nvars); |
| 518 |
for (i=horizon-1; i >= 0; i--) |
| 519 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 520 |
IR[i][j]=CreateVector(K); |
| 521 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 522 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 523 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 524 |
{ |
| 525 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 526 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 527 |
} |
| 528 |
|
| 529 |
k=0; |
| 530 |
while (!done) |
| 531 |
{ |
| 532 |
// Read parameters and push them into model |
| 533 |
if (!posterior_file) |
| 534 |
done=1; |
| 535 |
else |
| 536 |
{ |
| 537 |
if ((k >= K) || !GetPosteriorDraw(posterior_file,model,flag)) |
| 538 |
{ |
| 539 |
done=2; |
| 540 |
printf("total posterior draws processed - %d\n",k); |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
dw_NextLine(posterior_file,thin-1); |
| 545 |
if (k == n) |
| 546 |
{ |
| 547 |
printf("%d posterior draws processed\n",k); |
| 548 |
n+=1000; |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if (done != 2) |
| 554 |
{ |
| 555 |
// Compute A0_Xi and B |
| 556 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 557 |
|
| 558 |
// Compute impulse response |
| 559 |
impulse_response(ir,S,A0_Xi[s],B); |
| 560 |
|
| 561 |
// Accumulate impulse response |
| 562 |
for (i=horizon-1; i >= 0; i--) |
| 563 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 564 |
ElementV(IR[i][j],k)=ElementM(ir,i,j); |
| 565 |
k++; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
FreeMatrix(ir); |
| 570 |
dw_free(S); |
| 571 |
dw_FreeArray(A0_Xi); |
| 572 |
dw_FreeArray(B); |
| 573 |
|
| 574 |
return IR; |
| 575 |
} |
| 576 |
|
| 577 |
#ifdef ECB_VERSION |
| 578 |
/* |
| 579 |
Normalization for ECB. Works for three cases. |
| 580 |
(1) One Markov process controlling coefficients |
| 581 |
(2) One Markov process controlling variances |
| 582 |
(3) Two Markov processes, one controlling variances and the other |
| 583 |
controlling coefficents |
| 584 |
|
| 585 |
If the coefficients are time varing, then its controlling process must have |
| 586 |
two regimes. |
| 587 |
|
| 588 |
|
| 589 |
Assumes |
| 590 |
N - integer array of length model->sv->nstates |
| 591 |
model - valid SBVAR TStateModel |
| 592 |
|
| 593 |
Results |
| 594 |
N[i] for 0 <= i < model->sv->nstates is the index of the un-normalized |
| 595 |
regime given that the normalized regime index is i. |
| 596 |
|
| 597 |
*/ |
| 598 |
static int DATA_FIRST=-1; |
| 599 |
static int DATA_LAST=-1; |
| 600 |
static int COEF_PROCESS=-1; |
| 601 |
static int VAR_PROCESS=-1; |
| 602 |
void sbvar_normalize_regimes_ecb_set_globals(int nargs, char **args, TStateModel *model) |
| 603 |
{ |
| 604 |
COEF_PROCESS=dw_ParseInteger_String(nargs,args,"ecb_coef_process",0)-1; |
| 605 |
VAR_PROCESS=dw_ParseInteger_String(nargs,args,"ecb_var_process",0)-1; |
| 606 |
if (VAR_PROCESS < 0) |
| 607 |
{ |
| 608 |
if (COEF_PROCESS < 0) |
| 609 |
{ |
| 610 |
printf("Neither coef nor var process specified\n"); |
| 611 |
dw_exit(0); |
| 612 |
} |
| 613 |
if (model->sv->n_state_variables != 0) |
| 614 |
{ |
| 615 |
printf("Only coef process specified but more than one Markov processes\n"); |
| 616 |
dw_exit(0); |
| 617 |
} |
| 618 |
if (COEF_PROCESS != 0) |
| 619 |
{ |
| 620 |
printf("coef process specified to be %d, but only one Markov process\n",COEF_PROCESS+1); |
| 621 |
dw_exit(0); |
| 622 |
} |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
if (COEF_PROCESS < 0) |
| 627 |
{ |
| 628 |
if (model->sv->n_state_variables != 0) |
| 629 |
{ |
| 630 |
printf("Only var process specified but more than one Markov processes\n"); |
| 631 |
dw_exit(0); |
| 632 |
} |
| 633 |
if (VAR_PROCESS != 0) |
| 634 |
{ |
| 635 |
printf("var process specified to be %d, but only one Markov process\n",VAR_PROCESS+1); |
| 636 |
dw_exit(0); |
| 637 |
} |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
if (model->sv->n_state_variables != 2) |
| 642 |
{ |
| 643 |
printf("Both coef and var process specified but there are not two Markov processes\n"); |
| 644 |
dw_exit(0); |
| 645 |
} |
| 646 |
if (COEF_PROCESS == VAR_PROCESS) |
| 647 |
{ |
| 648 |
printf("Same Markov process cannot control both coef and var\n"); |
| 649 |
dw_exit(0); |
| 650 |
} |
| 651 |
if ((COEF_PROCESS > 1) || (VAR_PROCESS > 1)) |
| 652 |
{ |
| 653 |
printf("Either coef or var process greater than two\n"); |
| 654 |
dw_exit(0); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
if (COEF_PROCESS >= 0) |
| 660 |
{ |
| 661 |
DATA_FIRST=dw_ParseInteger_String(nargs,args,"ecb_coef_start",0); |
| 662 |
DATA_LAST=dw_ParseInteger_String(nargs,args,"ecb_coef_end",0); |
| 663 |
if ((DATA_FIRST <= 0) || (DATA_LAST > model->nobs) || (DATA_FIRST > DATA_LAST)) |
| 664 |
{ |
| 665 |
printf("Error in specifying ecb_coef_start and/or ecb_coef_end\n"); |
| 666 |
dw_exit(0); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
/* |
| 671 |
|
| 672 |
*/ |
| 673 |
static int normalized_coef=0; |
| 674 |
static int normalized_var=0; |
| 675 |
void sbvar_normalize_regimes_ecb(int *N, TStateModel *model) |
| 676 |
{ |
| 677 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 678 |
int i, j, k, t, *var_idx=(int*)NULL, *coef_idx=(int*)NULL, var_regimes, coef_regimes, var_m, coef_m, var_change=0, coef_change=0; |
| 679 |
PRECISION *x; |
| 680 |
|
| 681 |
var_regimes=(VAR_PROCESS < 0) ? 1 : ((model->sv->n_state_variables == 0) ? model->sv->nstates : model->sv->state_variable[VAR_PROCESS]->nstates); |
| 682 |
coef_regimes=(COEF_PROCESS < 0) ? 1 : ((model->sv->n_state_variables == 0) ? model->sv->nstates : model->sv->state_variable[COEF_PROCESS]->nstates); |
| 683 |
|
| 684 |
var_m=(VAR_PROCESS == 0) ? coef_regimes : 1; |
| 685 |
coef_m=(COEF_PROCESS == 0) ? var_regimes : 1; |
| 686 |
|
| 687 |
if (VAR_PROCESS >= 0) |
| 688 |
{ |
| 689 |
var_idx=dw_malloc(var_regimes*sizeof(PRECISION)); |
| 690 |
x=(PRECISION*)dw_malloc(var_regimes*sizeof(PRECISION)); |
| 691 |
|
| 692 |
// compute traces |
| 693 |
for (i=var_regimes-1; i >= 0; i--) |
| 694 |
for (x[i]=0.0, j=p->nvars-1; j >= 0; j--) |
| 695 |
x[i]+=sqrt(p->Zeta[j][p->var_states[j][i*var_m]]); |
| 696 |
|
| 697 |
// order traces |
| 698 |
for (i=0; i < var_regimes; i++) var_idx[i]=i; |
| 699 |
for (i=0; i < var_regimes; i++) |
| 700 |
{ |
| 701 |
for (k=i, j=i+1; j < var_regimes; j++) |
| 702 |
if (x[var_idx[k]] < x[var_idx[j]]) k=j; |
| 703 |
if (k != i) |
| 704 |
{ |
| 705 |
j=var_idx[k]; |
| 706 |
var_idx[k]=var_idx[i]; |
| 707 |
var_idx[i]=j; |
| 708 |
var_change=1; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
dw_free(x); |
| 713 |
} |
| 714 |
|
| 715 |
if (COEF_PROCESS >= 0) |
| 716 |
{ |
| 717 |
coef_idx=dw_malloc(var_regimes*sizeof(PRECISION)); |
| 718 |
x=(PRECISION*)dw_malloc(var_regimes*sizeof(PRECISION)); |
| 719 |
|
| 720 |
// compute average smoothed probabilities |
| 721 |
ComputeSmoothedProbabilities(model); |
| 722 |
for (i=coef_regimes-1; i >= 0; i--) |
| 723 |
for (x[i]=0.0, t=DATA_FIRST; t <= DATA_LAST; t++) |
| 724 |
for (j=0; j < var_regimes; j++) |
| 725 |
x[i]+=ElementV(model->SP[t],i*coef_m+j*var_m); |
| 726 |
|
| 727 |
// order probabilities |
| 728 |
for (i=0; i < coef_regimes; i++) coef_idx[i]=i; |
| 729 |
for (i=0; i < coef_regimes; i++) |
| 730 |
{ |
| 731 |
for (k=i, j=i+1; j < coef_regimes; j++) |
| 732 |
if (x[coef_idx[k]] < x[coef_idx[j]]) k=j; |
| 733 |
if (k != i) |
| 734 |
{ |
| 735 |
j=coef_idx[k]; |
| 736 |
coef_idx[k]=coef_idx[i]; |
| 737 |
coef_idx[i]=j; |
| 738 |
coef_change=1; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
dw_free(x); |
| 743 |
} |
| 744 |
|
| 745 |
if (var_change) normalized_var++; |
| 746 |
if (coef_change) normalized_coef++; |
| 747 |
|
| 748 |
if (VAR_PROCESS < 0) |
| 749 |
{ if (COEF_PROCESS >= 0) memcpy(N,coef_idx,model->sv->nstates*sizeof(int)); } |
| 750 |
else |
| 751 |
if (COEF_PROCESS < 0) |
| 752 |
memcpy(N,var_idx,model->sv->nstates*sizeof(int)); |
| 753 |
else |
| 754 |
for (i=0; i < var_regimes; i++) |
| 755 |
for (j=0; j < coef_regimes; j++) |
| 756 |
N[i*var_m+j*coef_m]=var_idx[i]*var_m+coef_idx[j]*coef_m; |
| 757 |
|
| 758 |
//================== Test Normalization ======================= |
| 759 |
/* static int iter=0; */ |
| 760 |
/* TMatrix Zeta=(TMatrix)NULL; */ |
| 761 |
/* PRECISION sum; */ |
| 762 |
|
| 763 |
/* iter++; */ |
| 764 |
/* for (i=0; i < model->sv->nstates; i++) */ |
| 765 |
/* if (N[i] != i) break; */ |
| 766 |
|
| 767 |
/* //if (i < model->sv->nstates) */ |
| 768 |
/* { */ |
| 769 |
/* printf("iter: %d\n",iter); */ |
| 770 |
|
| 771 |
/* for (i=0; i < model->sv->nstates; i++) */ |
| 772 |
/* printf("%d ",N[i]); */ |
| 773 |
/* printf("\n"); */ |
| 774 |
|
| 775 |
/* printf("var: "); */ |
| 776 |
/* for (i=0; i < var_regimes; i++) printf("%d ",var_idx[i]); */ |
| 777 |
/* printf("\n"); */ |
| 778 |
|
| 779 |
/* printf("coef: "); */ |
| 780 |
/* for (i=0; i < coef_regimes; i++) printf("%d ",coef_idx[i]); */ |
| 781 |
/* printf("\n"); */ |
| 782 |
|
| 783 |
/* printf("var_m: %d coef_m: %d\n",var_m,coef_m); */ |
| 784 |
|
| 785 |
/* for (i=0; i < model->sv->nstates; i++) */ |
| 786 |
/* { */ |
| 787 |
/* printf("i = %d = (%d,%d) N[i] = %d = (%d,%d)\n",i,model->sv->index[i][0],model->sv->index[i][1],N[i],model->sv->index[N[i]][0],model->sv->index[N[i]][1]); */ |
| 788 |
/* Zeta=MakeZeta(Zeta,N[i],p); */ |
| 789 |
/* for (sum=0.0, j=0; j < p->nvars; sum+=sqrt(ElementM(Zeta,j,j)), j++) */ |
| 790 |
/* printf("%lf ",sqrt(ElementM(Zeta,j,j))); */ |
| 791 |
/* printf("\ntrace = %lf\n",sum); */ |
| 792 |
/* } */ |
| 793 |
|
| 794 |
/* printf("smoothed probabilities\n"); */ |
| 795 |
/* //for (t=DATA_FIRST; t <= DATA_LAST; t++) */ |
| 796 |
/* for (t=1; t <= p->nobs; t++) */ |
| 797 |
/* { */ |
| 798 |
/* printf("t = %d : ",t); */ |
| 799 |
/* for (i=0; i < model->sv->nstates; i++) */ |
| 800 |
/* printf("%lf ",ElementV(model->SP[t],N[i])); */ |
| 801 |
/* printf("\n"); */ |
| 802 |
/* } */ |
| 803 |
|
| 804 |
/* getchar(); */ |
| 805 |
/* } */ |
| 806 |
//============================================================= |
| 807 |
|
| 808 |
if (coef_idx) dw_free(coef_idx); |
| 809 |
if (var_idx) dw_free(var_idx); |
| 810 |
} |
| 811 |
|
| 812 |
/* |
| 813 |
Assumes |
| 814 |
posterior_file : FILE pointer to file containing posterior draws. If null, |
| 815 |
current parameters are used. |
| 816 |
thin : thinning factor to use if posterior_file is not null. |
| 817 |
s : fixed regime |
| 818 |
horizon : non-negative integer |
| 819 |
model : point to valid TStateModel structure |
| 820 |
flag : determines the type of posterior draws file passed. |
| 821 |
|
| 822 |
Results: |
| 823 |
Creates matrix histogram containing the impulse responses. |
| 824 |
|
| 825 |
Returns: |
| 826 |
Pointer to matrix histogram upon success and null pointer on failure. |
| 827 |
|
| 828 |
Notes: |
| 829 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 830 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 831 |
response. |
| 832 |
*/ |
| 833 |
TVector** impulse_response_percentile_regime_full_normalized(FILE *posterior_file, int nposterior_draws, int thin, int s, int horizon, PRECISION explosive_cutoff, TStateModel *model, int flag) |
| 834 |
{ |
| 835 |
T_VAR_Parameters *p; |
| 836 |
int done=0, *S, i, j, k=0, n=1000, m=0, *N, normalized_wz=0, explosive=0, K; |
| 837 |
TMatrix ir, *A0_Xi, *B; |
| 838 |
TVector **IR, tmp; |
| 839 |
PRECISION max_root=0.0, min_root=PLUS_INFINITY, root; |
| 840 |
|
| 841 |
// quick check of passed parameters |
| 842 |
if ((nposterior_draws <= 0) || (horizon <= 0) || !model) return 0; |
| 843 |
|
| 844 |
p=(T_VAR_Parameters*)(model->theta); |
| 845 |
K=(nposterior_draws - 1)/thin + 1; |
| 846 |
|
| 847 |
// allocate memory |
| 848 |
N=(int*)dw_malloc(model->sv->nstates*sizeof(int)); |
| 849 |
S=(int*)dw_malloc(horizon*sizeof(int)); |
| 850 |
ir=CreateMatrix(horizon,p->nvars*p->nvars); |
| 851 |
IR=dw_CreateRectangularArray_vector(horizon,p->nvars*p->nvars); |
| 852 |
for (i=horizon-1; i >= 0; i--) |
| 853 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 854 |
IR[i][j]=CreateVector(K); |
| 855 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 856 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 857 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 858 |
{ |
| 859 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 860 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 861 |
} |
| 862 |
|
| 863 |
normalized_var=normalized_coef=0; |
| 864 |
while (!done) |
| 865 |
{ |
| 866 |
// Read parameters and push them into model |
| 867 |
if (!posterior_file) |
| 868 |
done=1; |
| 869 |
else |
| 870 |
{ |
| 871 |
if ((m >= K) || !GetPosteriorDraw(posterior_file,model,flag)) |
| 872 |
{ |
| 873 |
done=2; |
| 874 |
printf("total posterior draws processed - %d\n",m); |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
dw_NextLine(posterior_file,thin-1); |
| 879 |
if (m == n) |
| 880 |
{ |
| 881 |
printf("%d posterior draws processed (explosive %d)\n",m,explosive); |
| 882 |
n+=1000; |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
if (done != 2) |
| 888 |
{ |
| 889 |
m++; |
| 890 |
|
| 891 |
// Normalize sbvar |
| 892 |
normalized_wz+=Normalize_VAR(model,(PRECISION*)NULL); |
| 893 |
|
| 894 |
// Compute A0_Xi and B |
| 895 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 896 |
|
| 897 |
// Get Normalization |
| 898 |
#ifdef ECB_VERSION |
| 899 |
sbvar_normalize_regimes_ecb(N,model); |
| 900 |
#endif |
| 901 |
for (i=0; i < horizon; i++) S[i]=N[s]; |
| 902 |
|
| 903 |
// all the same? |
| 904 |
//if ((N[0] != 2) || (N[1] != 3) || (N[2] != 4) || (N[3] != 5) || (N[4] != 0) || (N[5] != 1)) { for (i=0; i < model->sv->nstates; i++) printf("%d ",N[i]); getchar();} |
| 905 |
|
| 906 |
// Explosive? |
| 907 |
root=(explosive_cutoff < PLUS_INFINITY) ? LargestRoot(B[N[s]]) : 0.0; |
| 908 |
if (root > max_root) max_root=root; |
| 909 |
if (root < min_root) min_root=root; |
| 910 |
if (root > explosive_cutoff) |
| 911 |
{ |
| 912 |
explosive++; |
| 913 |
/* dw_PrintMatrix(stdout,B[N[s]],"%lf "); */ |
| 914 |
/* printf("\nroot = %lf\n",sqrt(root)); */ |
| 915 |
/* getchar(); */ |
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
// Compute impulse response |
| 920 |
impulse_response(ir,S,A0_Xi[N[s]],B); |
| 921 |
|
| 922 |
// Accumulate impulse response |
| 923 |
for (i=horizon-1; i >= 0; i--) |
| 924 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 925 |
ElementV(IR[i][j],k)=ElementM(ir,i,j); |
| 926 |
k++; |
| 927 |
} |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
if (k == 0) |
| 932 |
{ |
| 933 |
dw_FreeArray(IR); |
| 934 |
IR=(TVector**)NULL; |
| 935 |
} |
| 936 |
else |
| 937 |
if (k < K) |
| 938 |
for (i=horizon-1; i >= 0; i--) |
| 939 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 940 |
{ |
| 941 |
tmp=CreateVector(k); |
| 942 |
memcpy(pElementV(tmp),pElementV(IR[i][j]),k*sizeof(PRECISION)); |
| 943 |
FreeVector(IR[i][j]); |
| 944 |
IR[i][j]=tmp; |
| 945 |
} |
| 946 |
|
| 947 |
printf("Number variance state normalizations: %d out of %d\n",normalized_var,m); |
| 948 |
printf("Number coefficient regime normalizations: %d out of %d\n",normalized_coef,m); |
| 949 |
printf("Number Waggoner/Zha normalizations: %d out of %d\n",normalized_wz,m); |
| 950 |
printf("Number explosive parameterizations: %d out of %d\n",explosive,m); |
| 951 |
printf("Maximum root: %lf\n",max_root); |
| 952 |
printf("Minimum root: %lf\n",min_root); |
| 953 |
|
| 954 |
FreeMatrix(ir); |
| 955 |
dw_free(S); |
| 956 |
dw_free(N); |
| 957 |
dw_FreeArray(A0_Xi); |
| 958 |
dw_FreeArray(B); |
| 959 |
|
| 960 |
return IR; |
| 961 |
} |
| 962 |
#endif // ECB_VERSION |
| 963 |
|
| 964 |
|
| 965 |
/* |
| 966 |
Assumes |
| 967 |
posterior_file : FILE pointer to file containing posterior draws. If null, |
| 968 |
current parameters are used. |
| 969 |
thin : thinning factor to use if posterior_file is not null. |
| 970 |
s : fixed regime |
| 971 |
horizon : non-negative integer |
| 972 |
model : point to valid TStateModel structure |
| 973 |
flag : determines the type of posterior draws file passed. |
| 974 |
|
| 975 |
Results: |
| 976 |
Creates matrix histogram containing the impulse responses. |
| 977 |
|
| 978 |
Returns: |
| 979 |
Pointer to matrix histogram upon success and null pointer on failure. |
| 980 |
|
| 981 |
Notes: |
| 982 |
The element in position (k,i+j*nvars) of ir is the response of the ith |
| 983 |
variable to the jth shock at horizon k. Horizon 0 is the contemporaneous |
| 984 |
response. |
| 985 |
*/ |
| 986 |
TMatrixHistogram* impulse_response_percentile_regime(FILE *posterior_file, TVector *posterior_sample, PRECISION cut, int thin, int s, int horizon, TStateModel *model, int flag) |
| 987 |
{ |
| 988 |
T_VAR_Parameters *p; |
| 989 |
int done=0, *S, i, j, k, n=1000, nt; |
| 990 |
TMatrix ir, *A0_Xi, *B, min, max; |
| 991 |
TVector **IR; |
| 992 |
TMatrixHistogram *histogram; |
| 993 |
|
| 994 |
// Quick check of passed parameters |
| 995 |
if ((horizon <= 0) || (thin <= 0) || (s < 0) || (s >= model->sv->nstates) || !posterior_sample || !model) return 0; |
| 996 |
|
| 997 |
p=(T_VAR_Parameters*)(model->theta); |
| 998 |
nt=NumberFreeParametersTheta(model); |
| 999 |
|
| 1000 |
// allocate local memory |
| 1001 |
histogram=CreateMatrixHistogram(horizon,p->nvars*p->nvars,500,HISTOGRAM_FIXED); |
| 1002 |
S=(int*)dw_malloc(horizon*sizeof(int)); |
| 1003 |
for (i=0; i < horizon; i++) S[i]=s; |
| 1004 |
ir=CreateMatrix(horizon,p->nvars*p->nvars); |
| 1005 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 1006 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 1007 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 1008 |
{ |
| 1009 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 1010 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 1011 |
} |
| 1012 |
|
| 1013 |
// Compute histogram bounds |
| 1014 |
IR=dw_CreateRectangularArray_vector(horizon,p->nvars*p->nvars); |
| 1015 |
for (i=horizon-1; i >= 0; i--) |
| 1016 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 1017 |
IR[i][j]=CreateVector(dw_DimA(posterior_sample)); |
| 1018 |
for (k=dw_DimA(posterior_sample)-1; k >= 0; k--) |
| 1019 |
{ |
| 1020 |
// Push parameters into model |
| 1021 |
ConvertFreeParametersToTheta(model,pElementV(posterior_sample[k])+2); |
| 1022 |
ConvertFreeParametersToQ(model,pElementV(posterior_sample[k])+2+nt); |
| 1023 |
|
| 1024 |
// Compute A0_Xi and B |
| 1025 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 1026 |
|
| 1027 |
// Compute impulse response |
| 1028 |
impulse_response(ir,S,A0_Xi[s],B); |
| 1029 |
|
| 1030 |
// Accumulate impulse response |
| 1031 |
for (i=horizon-1; i >= 0; i--) |
| 1032 |
for (j=p->nvars*p->nvars-1; j >= 0; j--) |
| 1033 |
ElementV(IR[i][j],k)=ElementM(ir,i,j); |
| 1034 |
} |
| 1035 |
SortIR(IR); |
| 1036 |
min=IRPercentile((TMatrix)NULL,cut,IR,model); |
| 1037 |
max=IRPercentile((TMatrix)NULL,1.0-cut,IR,model); |
| 1038 |
SetMaxMinMatrixHistogram(min,max,histogram); |
| 1039 |
FreeMatrix(max); |
| 1040 |
FreeMatrix(min); |
| 1041 |
dw_FreeArray(IR); |
| 1042 |
|
| 1043 |
i=0; |
| 1044 |
while (!done) |
| 1045 |
{ |
| 1046 |
// Read parameters and push them into model |
| 1047 |
if (!posterior_file) |
| 1048 |
done=1; |
| 1049 |
else |
| 1050 |
if (!GetPosteriorDraw(posterior_file,model,flag)) |
| 1051 |
{ |
| 1052 |
done=2; |
| 1053 |
printf("total posterior draws processed - %d\n",i); |
| 1054 |
} |
| 1055 |
else |
| 1056 |
{ |
| 1057 |
dw_NextLine(posterior_file,thin-1); |
| 1058 |
if (i == n) |
| 1059 |
{ |
| 1060 |
printf("%d posterior draws processed\n",i); |
| 1061 |
n+=1000; |
| 1062 |
} |
| 1063 |
i++; |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (done != 2) |
| 1067 |
{ |
| 1068 |
// Compute A0_Xi and B |
| 1069 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 1070 |
|
| 1071 |
// Compute impulse response |
| 1072 |
impulse_response(ir,S,A0_Xi[s],B); |
| 1073 |
|
| 1074 |
// Accumulate impulse response |
| 1075 |
AddMatrixObservation(ir,histogram); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
FreeMatrix(ir); |
| 1080 |
dw_free(S); |
| 1081 |
dw_FreeArray(A0_Xi); |
| 1082 |
dw_FreeArray(B); |
| 1083 |
|
| 1084 |
return histogram; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
/* |
| 1089 |
Additional command line parameters |
| 1090 |
|
| 1091 |
-horizon <integer> |
| 1092 |
If this argument exists, then the forecast horizon is given by the passed |
| 1093 |
integer. The default value is 12. |
| 1094 |
|
| 1095 |
-filtered |
| 1096 |
Uses filtered probabilities at the end of the sample as initial conditions |
| 1097 |
for regime probabilities. The default behavior is to us the erogdic |
| 1098 |
distribution for the initial conditions. This flag only applies if neither |
| 1099 |
-regimes nor -regime is specified. |
| 1100 |
|
| 1101 |
-error_bands |
| 1102 |
Output error bands. (default = off - only median is computed) |
| 1103 |
|
| 1104 |
-percentiles n p_1 p_2 ... p_n |
| 1105 |
Percentiles to compute. The first parameter after percentiles must be the |
| 1106 |
number of percentiles and the following values are the actual percentiles. |
| 1107 |
default = 3 0.16 0.50 0.84 if error_bands flag is set |
| 1108 |
= 1 0.50 otherwise |
| 1109 |
|
| 1110 |
-parameter_uncertainty |
| 1111 |
Apply parameter uncertainty when computing error bands or median. |
| 1112 |
|
| 1113 |
-shocks_per_parameter <integer> |
| 1114 |
Number of regime paths to draw for each parameter draw. The default value |
| 1115 |
is 1 if parameter_uncertainty is set and 10,000 otherwise. |
| 1116 |
|
| 1117 |
-thin |
| 1118 |
Thinning factor. Only 1/thin of the draws in posterior draws file are |
| 1119 |
used. The default value is 1. |
| 1120 |
|
| 1121 |
-regimes |
| 1122 |
Produces forecasts as if each regime were permanent. (default = off) |
| 1123 |
|
| 1124 |
-regime <integer> |
| 1125 |
Produces forecasts as if regime were permanent. Regime numbers are zero |
| 1126 |
based. (default = off) |
| 1127 |
|
| 1128 |
-flat |
| 1129 |
Assumes that posterior draws is a flat file format. The default is that |
| 1130 |
the posterior draws are in a free file format. |
| 1131 |
|
| 1132 |
*/ |
| 1133 |
void dw_sbvar_impulse_response_command_line(int nargs, char **args) |
| 1134 |
{ |
| 1135 |
char *post=(char*)NULL, *out_filename, *fmt="%%sir_percentiles_regime_%d_%%s.out", *buffer; |
| 1136 |
TStateModel *model; |
| 1137 |
T_VAR_Parameters *p; |
| 1138 |
TVector percentiles=(TVector)NULL; |
| 1139 |
int ergodic, type, s, horizon, thin, draws, i, j, n, T __attribute__ ((unused)), nposterior_draws; |
| 1140 |
FILE *f_out, *posterior_file; |
| 1141 |
//TMatrixHistogram *histogram; |
| 1142 |
TMatrix ir; |
| 1143 |
TVector **IR, *sample __attribute__ ((unused)) = (TVector*)NULL; |
| 1144 |
PRECISION explosive_cutoff __attribute__ ((unused)); |
| 1145 |
|
| 1146 |
TVARCommandLine *cmd=(TVARCommandLine*)NULL; |
| 1147 |
|
| 1148 |
// Setup model and initial parameters |
| 1149 |
if (!(cmd=CreateTStateModel_VARCommandLine(nargs,args,(TVARCommandLine*)NULL))) |
| 1150 |
dw_Error(MEM_ERR); |
| 1151 |
else |
| 1152 |
if (!cmd->model) |
| 1153 |
dw_UserError("Unable to setup SBVAR from command line parameters"); |
| 1154 |
else |
| 1155 |
if (!cmd->parameters_filename_actual) |
| 1156 |
dw_UserError("Unable to initialize parameters from command line parameters"); |
| 1157 |
else |
| 1158 |
{ |
| 1159 |
model=cmd->model; |
| 1160 |
p=(T_VAR_Parameters*)(model->theta); |
| 1161 |
|
| 1162 |
// Is posterior mode explosive |
| 1163 |
TMatrix *A0_Xi, *B; |
| 1164 |
A0_Xi=dw_CreateArray_matrix(model->sv->nstates); |
| 1165 |
B=dw_CreateArray_matrix(model->sv->nstates); |
| 1166 |
for (i=model->sv->nstates-1; i >= 0; i--) |
| 1167 |
{ |
| 1168 |
A0_Xi[i]=CreateMatrix(p->nvars,p->nvars); |
| 1169 |
B[i]=CreateMatrix(p->nvars*p->nlags,p->nvars); |
| 1170 |
} |
| 1171 |
GetIRMatrices(model->sv->nstates,A0_Xi,B,p); |
| 1172 |
for (i=0; i < model->sv->nstates; i++) |
| 1173 |
printf("Largest root at posterior mode regime[%d]: %lf\n",i,LargestRoot(B[i])); |
| 1174 |
|
| 1175 |
// setup normalization |
| 1176 |
Setup_WZ_Normalization_VAR(p->A0,p); |
| 1177 |
|
| 1178 |
// horizon |
| 1179 |
horizon=dw_ParseInteger_String(nargs,args,"horizon",12); |
| 1180 |
|
| 1181 |
// Output tag |
| 1182 |
OutTag_VARCommandLine(cmd); |
| 1183 |
|
| 1184 |
// Type |
| 1185 |
type=(dw_FindArgument_String(nargs,args,"flat") >= 0) ? F_FLAT : F_FREE; |
| 1186 |
|
| 1187 |
// Ergodic distribution for initial value of shocks |
| 1188 |
ergodic=(dw_FindArgument_String(nargs,args,"filtered") >= 0) ? 0 : 1; |
| 1189 |
|
| 1190 |
// Last data point |
| 1191 |
T=p->nobs; |
| 1192 |
|
| 1193 |
// discard explosive parameters |
| 1194 |
explosive_cutoff=dw_ParseFloating_String(nargs,args,"explosive",PLUS_INFINITY); |
| 1195 |
|
| 1196 |
//============================= Compute impulse responses ============================= |
| 1197 |
|
| 1198 |
// Parameter uncertainty |
| 1199 |
if (dw_FindArgument_String(nargs,args,"parameter_uncertainty") != -1) |
| 1200 |
{ |
| 1201 |
// Open posterior draws file |
| 1202 |
post=CreateFilenameFromTag((type == F_FREE) ? "%ssimulation_%s.out" : "%sdraws_%s.out",cmd->in_tag ? cmd->in_tag : "notag",cmd->in_directory); |
| 1203 |
if (!(posterior_file=fopen(post,"rt"))) |
| 1204 |
{ |
| 1205 |
dw_free(post); |
| 1206 |
printf("dw_sbvar_forecast command line(): Unable to open posterior draws file: %s\n",post); |
| 1207 |
return; |
| 1208 |
} |
| 1209 |
else |
| 1210 |
dw_free(post); |
| 1211 |
|
| 1212 |
// get number posterior draws and sample |
| 1213 |
sample=RandomSamplePosterior(posterior_file,nposterior_draws=GetNumberDraws(posterior_file),1000); |
| 1214 |
|
| 1215 |
// Get thinning factor from command line |
| 1216 |
thin=dw_ParseInteger_String(nargs,args,"thin",1); |
| 1217 |
if (thin <= 0) thin=1; |
| 1218 |
|
| 1219 |
// Get shocks_per_parameter from command line |
| 1220 |
draws=dw_ParseInteger_String(nargs,args,"shocks_per_parameter",1); |
| 1221 |
} |
| 1222 |
else |
| 1223 |
{ |
| 1224 |
// Using posterior estimate |
| 1225 |
posterior_file=(FILE*)NULL; |
| 1226 |
nposterior_draws=1; |
| 1227 |
|
| 1228 |
// thinning factor not used |
| 1229 |
thin=1; |
| 1230 |
|
| 1231 |
// Get shocks_per_parameter from command line |
| 1232 |
draws=dw_ParseInteger_String(nargs,args,"shocks_per_parameter",10000); |
| 1233 |
} |
| 1234 |
|
| 1235 |
// Setup percentiles |
| 1236 |
if ((i=dw_FindArgument_String(nargs,args,"percentiles")) == -1) |
| 1237 |
if (dw_FindArgument_String(nargs,args,"error_bands") == -1) |
| 1238 |
{ |
| 1239 |
percentiles=CreateVector(1); |
| 1240 |
ElementV(percentiles,0)=0.5; |
| 1241 |
} |
| 1242 |
else |
| 1243 |
{ |
| 1244 |
percentiles=CreateVector(3); |
| 1245 |
ElementV(percentiles,0)=0.16; ElementV(percentiles,1)=0.5; ElementV(percentiles,2)=0.84; |
| 1246 |
} |
| 1247 |
else |
| 1248 |
if ((i+1 < nargs) && dw_IsInteger(args[i+1]) && ((n=atoi(args[i+1])) > 0) && (i+1+n < nargs)) |
| 1249 |
{ |
| 1250 |
percentiles=CreateVector(n); |
| 1251 |
for (j=0; j < n; j++) |
| 1252 |
if (!dw_IsFloat(args[i+2+j])|| ((ElementV(percentiles,j)=atof(args[i+2+j])) <= 0.0) || (ElementV(percentiles,j) >= 1.0)) |
| 1253 |
break; |
| 1254 |
if (j < n) |
| 1255 |
{ |
| 1256 |
FreeVector(percentiles); |
| 1257 |
dw_UserError("sbvar_impulse_response_command_line(): Error parsing percentiles\n"); |
| 1258 |
return; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
else |
| 1262 |
{ |
| 1263 |
dw_UserError("sbvar_impulse_response_command_line(): Error parsing percentiles\n"); |
| 1264 |
return; |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (dw_FindArgument_String(nargs,args,"regimes") != -1) |
| 1268 |
for (s=0; s < p->nstates; s++) |
| 1269 |
{ |
| 1270 |
#ifdef ECB_VERSION |
| 1271 |
sbvar_normalize_regimes_ecb_set_globals(nargs,args,model); |
| 1272 |
#endif |
| 1273 |
if (posterior_file) rewind(posterior_file); |
| 1274 |
// s must not contain more than 10 digits |
| 1275 |
sprintf(buffer=(char*)dw_malloc(strlen(fmt) + 10),fmt,s); |
| 1276 |
out_filename=CreateFilenameFromTag(buffer,cmd->out_tag,cmd->out_directory); |
| 1277 |
dw_free(buffer); |
| 1278 |
if ((f_out=fopen(out_filename,"wt"))) |
| 1279 |
{ |
| 1280 |
printf("Constructing percentiles for impulse responses - regime %d\n",s); |
| 1281 |
//if (histogram=impulse_response_percentile_regime(posterior_file,sample,0.01,thin,s,horizon,model,type)) // |
| 1282 |
#ifdef ECB_VERSION |
| 1283 |
if ((IR=impulse_response_percentile_regime_full_normalized(posterior_file,nposterior_draws,thin,s,horizon,explosive_cutoff,model,type))) |
| 1284 |
#else |
| 1285 |
if ((IR=impulse_response_percentile_regime_full(posterior_file,nposterior_draws,thin,s,horizon,model,type))) |
| 1286 |
#endif |
| 1287 |
{ |
| 1288 |
SortIR(IR); // |
| 1289 |
for (i=0; i < DimV(percentiles); i++) |
| 1290 |
//if (ir=MatrixPercentile((TMatrix)NULL,ElementV(percentiles,i),histogram)) // |
| 1291 |
if ((ir=IRPercentile((TMatrix)NULL,ElementV(percentiles,i),IR,model))) // |
| 1292 |
{ |
| 1293 |
dw_PrintMatrix(f_out,ir,"%lg "); |
| 1294 |
fprintf(f_out,"\n"); |
| 1295 |
FreeMatrix(ir); |
| 1296 |
} |
| 1297 |
//FreeMatrixHistogram(histogram); // |
| 1298 |
dw_FreeArray(IR); // |
| 1299 |
} |
| 1300 |
fclose(f_out); |
| 1301 |
} |
| 1302 |
dw_free(out_filename); |
| 1303 |
} |
| 1304 |
else |
| 1305 |
if (dw_FindArgument_String(nargs,args,"regime") != -1) |
| 1306 |
if (((s=dw_ParseInteger_String(nargs,args,"regime",-1)) >= 0) && (s < p->nstates)) |
| 1307 |
{ |
| 1308 |
#ifdef ECB_VERSION |
| 1309 |
sbvar_normalize_regimes_ecb_set_globals(nargs,args,model); |
| 1310 |
#endif |
| 1311 |
if (posterior_file) rewind(posterior_file); |
| 1312 |
// s must not contain more than 10 digits |
| 1313 |
sprintf(buffer=(char*)dw_malloc(strlen(fmt) + 10),fmt,s); |
| 1314 |
out_filename=CreateFilenameFromTag(buffer,cmd->out_tag,cmd->out_directory); |
| 1315 |
dw_free(buffer); |
| 1316 |
if ((f_out=fopen(out_filename,"wt"))) |
| 1317 |
{ |
| 1318 |
printf("Constructing percentiles for impulse responses - regime %d\n",s); |
| 1319 |
//if (histogram=impulse_response_percentile_regime(posterior_file,thin,s,horizon,model,type)) // |
| 1320 |
#ifdef ECB_VERSION |
| 1321 |
if ((IR=impulse_response_percentile_regime_full_normalized(posterior_file,nposterior_draws,thin,s,horizon,explosive_cutoff,model,type))) |
| 1322 |
#else |
| 1323 |
if ((IR=impulse_response_percentile_regime_full(posterior_file,nposterior_draws,thin,s,horizon,model,type))) |
| 1324 |
#endif |
| 1325 |
{ |
| 1326 |
SortIR(IR); // |
| 1327 |
for (i=0; i < DimV(percentiles); i++) |
| 1328 |
//if (ir=IRHistogramToPercentile((TMatrix)NULL,horizon,ElementV(percentiles,i),histogram,model)) // |
| 1329 |
if ((ir=IRPercentile((TMatrix)NULL,ElementV(percentiles,i),IR,model))) // |
| 1330 |
{ |
| 1331 |
dw_PrintMatrix(f_out,ir,"%lg "); |
| 1332 |
fprintf(f_out,"\n"); |
| 1333 |
FreeMatrix(ir); |
| 1334 |
} |
| 1335 |
//FreeMatrixHistogram(histogram); // |
| 1336 |
dw_FreeArray(IR); // |
| 1337 |
} |
| 1338 |
fclose(f_out); |
| 1339 |
} |
| 1340 |
dw_free(out_filename); |
| 1341 |
} |
| 1342 |
else |
| 1343 |
{ |
| 1344 |
printf("dw_sbvar_forecast command line(): regime number must be between 0 and %d inclusive\n",p->nstates-1); |
| 1345 |
return; |
| 1346 |
} |
| 1347 |
else |
| 1348 |
{ |
| 1349 |
out_filename=CreateFilenameFromTag(ergodic ? "%sir_percentiles_ergodic_%s.out" : "%sir_percentiles_filtered_%s.out", |
| 1350 |
cmd->out_tag,cmd->out_directory); |
| 1351 |
if ((f_out=fopen(out_filename,"wt"))) |
| 1352 |
{ |
| 1353 |
printf(ergodic ? "Constructing percentiles for ergodic impulse responses - %d draws of regimes per posterior value\n" |
| 1354 |
: "Constructing percentiles for filtered impulse responses - %d draws of regimes per posterior value\n",draws); |
| 1355 |
//if (histogram=impulse_response_percentile_ergodic(draws,posterior_file,thin,ergodic,horizon,model,type)) // |
| 1356 |
if ((IR=impulse_response_percentile_ergodic_full(draws,posterior_file,nposterior_draws,thin,ergodic,horizon,model,type))) // |
| 1357 |
{ |
| 1358 |
SortIR(IR); // |
| 1359 |
for (i=0; i < DimV(percentiles); i++) |
| 1360 |
//if (ir=IRHistogramToPercentile((TMatrix)NULL,horizon,ElementV(percentiles,i),histogram,model)) // |
| 1361 |
if ((ir=IRPercentile((TMatrix)NULL,ElementV(percentiles,i),IR,model))) // |
| 1362 |
{ |
| 1363 |
dw_PrintMatrix(f_out,ir,"%lg "); |
| 1364 |
fprintf(f_out,"\n"); |
| 1365 |
FreeMatrix(ir); |
| 1366 |
} |
| 1367 |
//FreeMatrixHistogram(histogram); // |
| 1368 |
dw_FreeArray(IR); // |
| 1369 |
} |
| 1370 |
fclose(f_out); |
| 1371 |
} |
| 1372 |
dw_free(out_filename); |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (posterior_file) fclose(posterior_file); |
| 1376 |
FreeVector(percentiles); |
| 1377 |
//============================================================================= |
| 1378 |
} |
| 1379 |
|
| 1380 |
Free_VARCommandLine(cmd); |
| 1381 |
} |