| 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 |
|
| 19 |
#include "sbvar_historical_decomposition.h" |
| 20 |
#include "VARbase.h" |
| 21 |
#include "VARio.h" |
| 22 |
#include "dw_parse_cmd.h" |
| 23 |
#include "dw_error.h" |
| 24 |
#include "dw_std.h" |
| 25 |
|
| 26 |
#include <math.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
static void CheckHistoricalDecomposition(int *S, TMatrix HD, TStateModel *model); |
| 30 |
static void CheckCumulativeHistoricalDecomposition(TMatrix CHD, TStateModel *model); |
| 31 |
|
| 32 |
/* |
| 33 |
Assumes: |
| 34 |
S : integer vector of length nobs + 1 + h) with 0 <= S[t] < nstates |
| 35 |
t0 : integer between 1 and nobs |
| 36 |
h : non-negative integer |
| 37 |
model : pointer to valid TStateModel structure |
| 38 |
|
| 39 |
Returns: |
| 40 |
Fills var. The element in column (nepsilon+nu+2)*i is the ith variable, the |
| 41 |
element in column (nepsilon+nu+2)*i + 1 is the exogeneous component, the |
| 42 |
element in column (nepsilon+nu+2)*i + 2 + j is the cummulative response of |
| 43 |
the ith variable to the jth shock. The fundamental shocks epsilon(t) are |
| 44 |
ordered first. The kth row corresponds to time t0 + k. For k > nobs-t0+1, |
| 45 |
the element in column (nepsilon+nu+2)*i is the forecast of the ith variable. |
| 46 |
The initial condition is at time t0 -- E[z(t0) | Y(T)] |
| 47 |
|
| 48 |
Notes: |
| 49 |
This works when the only time varying parameters are Phiy and Phyz, which |
| 50 |
includes the constant parameter case. Also, nlags_encoded must be bigger |
| 51 |
zero. |
| 52 |
*/ |
| 53 |
TMatrix sbvar_cumulative_historical_decomposition(int *S, int t0, int h, TStateModel *model) |
| 54 |
{ |
| 55 |
TVector *cx, *cy; |
| 56 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 57 |
TMatrix CHD, HD, *A0, *Aplus, *Zeta; |
| 58 |
int i, j, t; |
| 59 |
|
| 60 |
CHD=CreateMatrix(p->nobs-t0+h+1,1+p->nvars*(p->nvars+2)); |
| 61 |
|
| 62 |
HD=sbvar_historical_decomposition(S,model); |
| 63 |
|
| 64 |
cx=dw_CreateArray_vector(p->nvars+1); |
| 65 |
for (i=0; i < p->nvars; i++) |
| 66 |
InitializeVector(cx[i]=CreateVector(p->npre),0.0); |
| 67 |
cx[p->nvars]=EquateVector((TVector)NULL,p->X[t0]); |
| 68 |
cy=dw_CreateArray_vector(p->nvars+1); |
| 69 |
for (i=0; i < p->nvars; i++) |
| 70 |
InitializeVector(cy[i]=CreateVector(p->nvars),0.0); |
| 71 |
cy[p->nvars]=EquateVector((TVector)NULL,p->Y[t0]); |
| 72 |
A0=MakeA0_All((TMatrix*)NULL,p); |
| 73 |
Aplus=MakeAplus_All((TMatrix*)NULL,p); |
| 74 |
Zeta=MakeZeta_All((TMatrix*)NULL,p); |
| 75 |
|
| 76 |
ElementM(CHD,0,0)=t0; |
| 77 |
for (j=p->nvars-1; j >= 0; j--) |
| 78 |
{ |
| 79 |
ElementM(CHD,0,j*(p->nvars+2)+1)=ElementV(p->Y[t0],j); |
| 80 |
ElementM(CHD,0,j*(p->nvars+2)+2)=ElementV(cy[p->nvars],j); |
| 81 |
for (i=p->nvars-1; i >= 0; i--) |
| 82 |
ElementM(CHD,0,j*(p->nvars+2)+3+i)=ElementV(cy[i],j); |
| 83 |
} |
| 84 |
|
| 85 |
for (t=t0+1; t <= p->nobs; t++) |
| 86 |
{ |
| 87 |
for (i=p->nvars; i >= 0; i--) |
| 88 |
{ |
| 89 |
memmove(pElementV(cx[i])+p->nvars,pElementV(cx[i]),p->nvars*(p->nlags-1)*sizeof(PRECISION)); |
| 90 |
memcpy(pElementV(cx[i]),pElementV(cy[i]),p->nvars*sizeof(PRECISION)); |
| 91 |
} |
| 92 |
|
| 93 |
i=p->nvars; |
| 94 |
ProductVM(cy[i],cx[i],Aplus[S[t]]); |
| 95 |
ProductInverseVM(cy[i],cy[i],A0[S[t]]); |
| 96 |
|
| 97 |
for (i--; i >= 0; i--) |
| 98 |
{ |
| 99 |
ProductVM(cy[i],cx[i],Aplus[S[t]]); |
| 100 |
ElementV(cy[i],i)+=ElementM(HD,t,i+1)/sqrt(ElementM(Zeta[S[t]],i,i)); |
| 101 |
ProductInverseVM(cy[i],cy[i],A0[S[t]]); |
| 102 |
} |
| 103 |
|
| 104 |
ElementM(CHD,t-t0,0)=t; |
| 105 |
for (j=p->nvars-1; j >= 0; j--) |
| 106 |
{ |
| 107 |
ElementM(CHD,t-t0,j*(p->nvars+2)+1)=ElementV(p->Y[t],j); |
| 108 |
ElementM(CHD,t-t0,j*(p->nvars+2)+2)=ElementV(cy[p->nvars],j); |
| 109 |
for (i=p->nvars-1; i >= 0; i--) |
| 110 |
ElementM(CHD,t-t0,j*(p->nvars+2)+3+i)=ElementV(cy[i],j); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
for ( ; t <= p->nobs+h; t++) |
| 115 |
{ |
| 116 |
for (i=p->nvars; i >= 0; i--) |
| 117 |
{ |
| 118 |
memmove(pElementV(cx[i]),pElementV(cx[i])+p->nvars,p->nvars*(p->nlags-1)*sizeof(PRECISION)); |
| 119 |
memcpy(pElementV(cx[i]),pElementV(cy[i]),p->nvars*sizeof(PRECISION)); |
| 120 |
} |
| 121 |
|
| 122 |
for (i=p->nvars; i >= 0; i--) |
| 123 |
{ |
| 124 |
ProductVM(cy[i],cx[i],Aplus[S[t]]); |
| 125 |
ProductInverseVM(cy[i],cy[i],A0[S[t]]); |
| 126 |
} |
| 127 |
|
| 128 |
for (j=p->nvars-1; j >= 0; j--) |
| 129 |
{ |
| 130 |
ElementM(CHD,t-t0-1,j*(p->nvars+2))=ElementV(p->Y[t],j); |
| 131 |
ElementM(CHD,t-t0-1,j*(p->nvars+2)+1)=ElementV(cy[p->nvars],j); |
| 132 |
for (i=p->nvars-1; i >= 0; i--) |
| 133 |
ElementM(CHD,t-t0-1,j*(p->nvars+2)+2+i)=ElementV(cy[i],j); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
dw_FreeArray(Zeta); |
| 138 |
dw_FreeArray(Aplus); |
| 139 |
dw_FreeArray(A0); |
| 140 |
dw_FreeArray(cy); |
| 141 |
dw_FreeArray(cx); |
| 142 |
FreeMatrix(HD); |
| 143 |
|
| 144 |
CheckCumulativeHistoricalDecomposition(CHD,model); |
| 145 |
|
| 146 |
return CHD; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/* |
| 151 |
S is a vector of integers of length at least nobs with 0 <= S[t] < nstates and model |
| 152 |
is a valid pointer to a properly initialized TStateMode structure. |
| 153 |
|
| 154 |
Returns a matrix of size nvars+2 by nobs-1. The first column is the date number, the |
| 155 |
next nvars columns are the historical shocks given the path of regimes S, and the |
| 156 |
last column is S. |
| 157 |
*/ |
| 158 |
TMatrix sbvar_historical_decomposition(int *S, TStateModel *model) |
| 159 |
{ |
| 160 |
TVector epsilon; |
| 161 |
TMatrix HD, *A0, *Aplus, *Zeta; |
| 162 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 163 |
int i, t; |
| 164 |
|
| 165 |
InitializeMatrix(HD=CreateMatrix(p->nobs+1,p->nvars+2),0.0); |
| 166 |
|
| 167 |
epsilon=CreateVector(p->nvars); |
| 168 |
A0=MakeA0_All((TMatrix*)NULL,p); |
| 169 |
Aplus=MakeAplus_All((TMatrix*)NULL,p); |
| 170 |
Zeta=MakeZeta_All((TMatrix*)NULL,p); |
| 171 |
|
| 172 |
for (t=1; t <= p->nobs; t++) |
| 173 |
{ |
| 174 |
ProductVM(epsilon,p->Y[t],A0[S[t]]); |
| 175 |
UpdateProductVM(1.0,epsilon,-1.0,p->X[t],Aplus[S[t]]); |
| 176 |
ElementM(HD,t,0)=t; |
| 177 |
for (i=p->nvars-1; i >= 0; i--) ElementM(HD,t,i+1)=ElementV(epsilon,i)*sqrt(ElementM(Zeta[S[t]],i,i)); |
| 178 |
ElementM(HD,t,p->nvars+1)=S[t]; |
| 179 |
} |
| 180 |
|
| 181 |
dw_FreeArray(Zeta); |
| 182 |
dw_FreeArray(Aplus); |
| 183 |
dw_FreeArray(A0); |
| 184 |
FreeVector(epsilon); |
| 185 |
|
| 186 |
CheckHistoricalDecomposition(S,HD,model); |
| 187 |
|
| 188 |
return HD; |
| 189 |
} |
| 190 |
|
| 191 |
static void CheckHistoricalDecomposition(int *S, TMatrix HD, TStateModel *model) |
| 192 |
{ |
| 193 |
TVector epsilon, y; |
| 194 |
TMatrix *A0, *Aplus, *Zeta; |
| 195 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 196 |
int i, k, t; |
| 197 |
|
| 198 |
epsilon=CreateVector(p->nvars); |
| 199 |
y=CreateVector(p->nvars); |
| 200 |
A0=MakeA0_All((TMatrix*)NULL,p); |
| 201 |
Aplus=MakeAplus_All((TMatrix*)NULL,p); |
| 202 |
Zeta=MakeZeta_All((TMatrix*)NULL,p); |
| 203 |
for (k=p->nstates-1; k >= 0; k--) |
| 204 |
for (i=p->nvars-1; i >= 0; i--) |
| 205 |
ElementM(Zeta[k],i,i)=1.0/sqrt(ElementM(Zeta[k],i,i)); |
| 206 |
|
| 207 |
for (t=1; t <= p->nobs; t++) |
| 208 |
{ |
| 209 |
for (i=p->nvars-1; i >= 0; i--) ElementV(epsilon,i)=ElementM(HD,t,i+1); |
| 210 |
ProductVM(y,epsilon,Zeta[S[t]]); |
| 211 |
UpdateProductVM(1.0,y,1.0,p->X[t],Aplus[S[t]]); |
| 212 |
UpdateProductVM(1.0,y,-1.0,p->Y[t],A0[S[t]]); |
| 213 |
if (Norm(y) > 1.0e-10) |
| 214 |
{ |
| 215 |
printf("historical shocks do not return data: t = %d S[%d] = %d\n y = ",t,t,S[t]); |
| 216 |
dw_PrintVector(stdout,y,"%lf "); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
printf("Finished check historical decomposition\n"); |
| 221 |
|
| 222 |
dw_FreeArray(Zeta); |
| 223 |
dw_FreeArray(Aplus); |
| 224 |
dw_FreeArray(A0); |
| 225 |
FreeVector(epsilon); |
| 226 |
} |
| 227 |
|
| 228 |
static void CheckCumulativeHistoricalDecomposition(TMatrix CHD, TStateModel *model) |
| 229 |
{ |
| 230 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 231 |
PRECISION y; |
| 232 |
int i, j, t; |
| 233 |
|
| 234 |
for (t=0; t < RowM(CHD); t++) |
| 235 |
for (j=p->nvars-1; j >= 0; j--) |
| 236 |
{ |
| 237 |
y=ElementM(CHD,t,j*(p->nvars+2)+1); |
| 238 |
for (i=p->nvars; i >= 0; i--) |
| 239 |
y-=ElementM(CHD,t,j*(p->nvars+2)+2+i); |
| 240 |
if (fabs(y) > 1.0e-10) |
| 241 |
printf("cumulative historical decomposition does not sum to data: t = %d, variable = %d - %lf\n",t,j,y); |
| 242 |
} |
| 243 |
|
| 244 |
printf("Finished check cumulative historical decomposition\n"); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
int* sbvar_most_likely_regime(int t0, int horizon, TStateModel *model) |
| 249 |
{ |
| 250 |
int *S; |
| 251 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 252 |
int i, t; |
| 253 |
TVector pr; |
| 254 |
|
| 255 |
S=dw_CreateArray_int(p->nobs-t0+1+horizon); |
| 256 |
ComputeSmoothedProbabilities(model); |
| 257 |
for (t=t0; t <= p->nobs; t++) |
| 258 |
for (S[t-t0]=0, i=p->nstates-1; i > 0; i--) |
| 259 |
if (ElementV(model->SP[t],i) > ElementV(model->SP[t],S[t-t0])) S[t-t0]=i; |
| 260 |
if (horizon > 0) |
| 261 |
{ |
| 262 |
pr=CreateVector(p->nstates); |
| 263 |
ComputeTransitionMatrix(p->nobs,model); |
| 264 |
EquateVector(pr,model->SP[p->nobs]); |
| 265 |
for ( ; t <= p->nobs+horizon; t++) |
| 266 |
{ |
| 267 |
ProductMV(pr,model->sv->Q,pr); |
| 268 |
for (S[t-t0]=0, i=p->nstates-1; i > 0; i--) |
| 269 |
if (ElementV(pr,i) > ElementV(pr,S[t-t0])) S[t-t0]=i; |
| 270 |
} |
| 271 |
FreeVector(pr); |
| 272 |
} |
| 273 |
|
| 274 |
return S; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/* |
| 279 |
Additional command line parameters |
| 280 |
|
| 281 |
-cumulative |
| 282 |
Produces the cumulative historical decomposition. The default is |
| 283 |
to produce the historical shock decomposition. |
| 284 |
|
| 285 |
-t0 |
| 286 |
The starting point for the cumulative historical decomposition. The default |
| 287 |
value is 1, which is the start of the data. |
| 288 |
|
| 289 |
-horizon |
| 290 |
Produces forecasts of the cumulative historical decomposition. The default |
| 291 |
value is 0. |
| 292 |
|
| 293 |
|
| 294 |
*/ |
| 295 |
void dw_sbvar_historical_decomposition_command_line(int nargs, char **args) |
| 296 |
{ |
| 297 |
char *out_filename; |
| 298 |
TStateModel *model; |
| 299 |
int *S; |
| 300 |
TMatrix HD; |
| 301 |
FILE *f_out; |
| 302 |
TVARCommandLine *cmd; |
| 303 |
int horizon, t0; |
| 304 |
|
| 305 |
// Setup model and initial parameters |
| 306 |
if (!(cmd=CreateTStateModel_VARCommandLine(nargs,args,(TVARCommandLine*)NULL))) |
| 307 |
dw_Error(MEM_ERR); |
| 308 |
else |
| 309 |
if (!cmd->model) |
| 310 |
dw_UserError("Unable to setup SBVAR from command line parameters"); |
| 311 |
else |
| 312 |
if (!cmd->parameters_filename_actual) |
| 313 |
dw_UserError("Unable to initialize parameters from command line parameters"); |
| 314 |
else |
| 315 |
{ |
| 316 |
model=cmd->model; |
| 317 |
|
| 318 |
// Output tag |
| 319 |
OutTag_VARCommandLine(cmd); |
| 320 |
|
| 321 |
out_filename=CreateFilenameFromTag("%ssbvar_historical_%s.out",cmd->out_tag,cmd->out_directory); |
| 322 |
|
| 323 |
if (dw_FindArgument_String(nargs,args,"cumulative") == -1) |
| 324 |
{ |
| 325 |
if (f_out=fopen(out_filename,"wt")) |
| 326 |
{ |
| 327 |
printf("Constructing historical decomposition\n"); |
| 328 |
|
| 329 |
S=sbvar_most_likely_regime(0,0,model); |
| 330 |
HD=sbvar_historical_decomposition(S,model); |
| 331 |
|
| 332 |
dw_PrintMatrix(f_out,HD,"%lf "); |
| 333 |
fprintf(f_out,"\n"); |
| 334 |
Write_VAR_ParametersFlat(f_out,model,"%le "); |
| 335 |
|
| 336 |
fclose(f_out); |
| 337 |
dw_FreeArray(S); |
| 338 |
FreeMatrix(HD); |
| 339 |
|
| 340 |
dw_free(out_filename); |
| 341 |
out_filename=CreateFilenameFromTag("%ssbvar_historical_%s_headers.out",cmd->out_tag,cmd->out_directory); |
| 342 |
if (f_out=fopen(out_filename,"wt")) |
| 343 |
{ |
| 344 |
Write_VAR_ParametersFlat_Headers(f_out,model); |
| 345 |
fclose(f_out); |
| 346 |
} |
| 347 |
} |
| 348 |
dw_free(out_filename); |
| 349 |
} |
| 350 |
else |
| 351 |
{ |
| 352 |
out_filename=CreateFilenameFromTag("%ssbvar_cumulative_historical_%s.out",cmd->out_tag,cmd->out_directory); |
| 353 |
|
| 354 |
if (f_out=fopen(out_filename,"wt")) |
| 355 |
{ |
| 356 |
printf("Constructing cumulative historical decomposition\n"); |
| 357 |
|
| 358 |
horizon=dw_ParseInteger_String(nargs,args,"horizon",0); |
| 359 |
t0=dw_ParseInteger_String(nargs,args,"t0",1); |
| 360 |
|
| 361 |
S=sbvar_most_likely_regime(0,horizon,model); |
| 362 |
HD=sbvar_cumulative_historical_decomposition(S,t0,horizon,model); |
| 363 |
|
| 364 |
dw_PrintMatrix(f_out,HD,"%lf "); |
| 365 |
|
| 366 |
fclose(f_out); |
| 367 |
dw_FreeArray(S); |
| 368 |
FreeMatrix(HD); |
| 369 |
|
| 370 |
/* dw_free(out_filename); */ |
| 371 |
/* out_filename=CreateFilenameFromTag("%ssbvar_historical_%s_headers.out",cmd->out_tag,cmd->out_directory); */ |
| 372 |
/* if (f_out=fopen(out_filename,"wt")) */ |
| 373 |
/* { */ |
| 374 |
/* Write_VAR_ParametersFlat_Headers(f_out,model); */ |
| 375 |
/* fclose(f_out); */ |
| 376 |
/* } */ |
| 377 |
} |
| 378 |
dw_free(out_filename); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
Free_VARCommandLine(cmd); |
| 383 |
} |
| 384 |
|