| 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 "VARio_matlab.h" |
| 19 |
#include "switchio.h" |
| 20 |
|
| 21 |
#include "dw_error.h" |
| 22 |
#include "dw_ascii.h" |
| 23 |
#include "dw_std.h" |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
#include <math.h> |
| 28 |
|
| 29 |
static int ReadError_VARio_matlab(char *id) |
| 30 |
{ |
| 31 |
char *errmsg, *fmt="Error after line identifier ""%s"""; |
| 32 |
sprintf(errmsg=(char*)dw_malloc(strlen(fmt) + strlen(id) - 1),fmt,id); |
| 33 |
dw_UserError(errmsg); |
| 34 |
dw_free(errmsg); |
| 35 |
return 1; |
| 36 |
} |
| 37 |
|
| 38 |
TStateModel* Combine_matlab_standard(char *matlabfile, char *standardfile) |
| 39 |
{ |
| 40 |
FILE *f_in; |
| 41 |
TStateModel *model; |
| 42 |
TMarkovStateVariable *sv, ***coef_sv, ***var_sv; |
| 43 |
T_VAR_Parameters *p; |
| 44 |
char *id; |
| 45 |
int *IV, **States; |
| 46 |
int nlags, nvars, nexg, npre, nstates, nobs, i, j, n, spec, no_spec=0, n_state_variables; |
| 47 |
PRECISION scalar_zeta_a_prior, scalar_zeta_b_prior, lambda_prior; |
| 48 |
int **coef_states, **var_states; |
| 49 |
TMatrix *U, *V, *W, *A0_prior, *Aplus_prior, X=(TMatrix)NULL, Y=(TMatrix)NULL, S; |
| 50 |
TVector zeta_a_prior, zeta_b_prior; |
| 51 |
|
| 52 |
//=== Open matlab input file |
| 53 |
f_in=dw_OpenTextFile(matlabfile); |
| 54 |
|
| 55 |
//=== Read sizes ===// |
| 56 |
id="//== lags, nvar, nStates, T ==//"; |
| 57 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %d %d %d %d ",&nlags,&nvars,&nstates,&nobs) != 4)) ReadError_VARio_matlab(id); |
| 58 |
|
| 59 |
//=== A single constant term ===// |
| 60 |
nexg=1; |
| 61 |
npre=nvars * nlags + nexg; |
| 62 |
|
| 63 |
//=== Restrictions - U[j] ===// |
| 64 |
IV=dw_CreateArray_int(nvars); |
| 65 |
id="//== n0const: nvar-by-1 ==//"; |
| 66 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,IV)) ReadError_VARio_matlab(id); |
| 67 |
id="//== Uiconst: cell(nvar,1) and nvar-by-n0const(i) for the ith cell (equation) ==//"; |
| 68 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 69 |
U=dw_CreateArray_matrix(nvars); |
| 70 |
for (j=0; j < nvars; j++) |
| 71 |
if (!dw_ReadMatrix(f_in,U[j]=CreateMatrix(nvars,IV[j]))) ReadError_VARio_matlab(id); |
| 72 |
dw_FreeArray(IV); |
| 73 |
|
| 74 |
//=== Specification ===// |
| 75 |
id= "//== Specification (0=default 1=Sims-Zha 2=Random Walk) ==//"; |
| 76 |
if (!dw_SetFilePosition(f_in,id)) |
| 77 |
{ |
| 78 |
no_spec=1; |
| 79 |
spec=SPEC_SIMS_ZHA | SPEC_RANDOM_WALK; |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
if (fscanf(f_in," %d ",&spec) != 1) ReadError_VARio_matlab(id); |
| 84 |
switch (spec) |
| 85 |
{ |
| 86 |
case 0: spec=0; break; |
| 87 |
case 1: spec=SPEC_SIMS_ZHA | SPEC_RANDOM_WALK; break; |
| 88 |
case 2: spec=SPEC_RANDOM_WALK; break; |
| 89 |
default: ReadError_VARio_matlab(id); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
//=== Restrictions - V[j] (V[j] should be an npre x npre identity matrix) ===// |
| 94 |
IV=dw_CreateArray_int(nvars); |
| 95 |
id="//== npconst: nvar-by-1 ==//"; |
| 96 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,IV)) ReadError_VARio_matlab(id); |
| 97 |
if (spec & SPEC_SIMS_ZHA) |
| 98 |
for (j=nvars-1; j >= 0; j--) |
| 99 |
if (IV[j] != npre) |
| 100 |
{ |
| 101 |
if (no_spec) |
| 102 |
{ |
| 103 |
spec=SPEC_RANDOM_WALK; |
| 104 |
break; |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
dw_UserError("Improper use of Sims-Zha specification: V[%d] must be identity\n"); |
| 109 |
dw_exit(0); |
| 110 |
} |
| 111 |
} |
| 112 |
V=dw_CreateArray_matrix(nvars); |
| 113 |
if (spec & SPEC_SIMS_ZHA) |
| 114 |
{ |
| 115 |
for (j=nvars-1; j >= 0; j--) |
| 116 |
V[j]=IdentityMatrix((TMatrix)NULL,npre); |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
id="//== Viconst: cell(nvar,1) and ncoef-by-n0const(i) for the ith cell (equation) ==//"; |
| 121 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 122 |
for (j=0; j < nvars; j++) |
| 123 |
if (!dw_ReadMatrix(f_in,V[j]=CreateMatrix(npre,IV[j]))) ReadError_VARio_matlab(id); |
| 124 |
} |
| 125 |
dw_FreeArray(IV); |
| 126 |
|
| 127 |
//=== Restrictions - W[j] (Random walk specification) ===// |
| 128 |
InitializeMatrix(S=CreateMatrix(npre,nvars),0.0); |
| 129 |
for (j=nvars-1; j >= 0; j--) ElementM(S,j,j)=-1.0; |
| 130 |
W=dw_CreateArray_matrix(nvars); |
| 131 |
for (j=nvars-1; j >= 0; j--) |
| 132 |
W[j]=EquateMatrix((TMatrix)NULL,S); |
| 133 |
FreeMatrix(S); |
| 134 |
|
| 135 |
//====== Priors ====== |
| 136 |
id="//== gxia: alpha parameter for gamma prior of xi ==//"; |
| 137 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&scalar_zeta_a_prior) != 1)) ReadError_VARio_matlab(id); |
| 138 |
id="//== gxib: beta parameter for gamma prior of xi ==//"; |
| 139 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&scalar_zeta_b_prior) != 1)) ReadError_VARio_matlab(id); |
| 140 |
zeta_a_prior=CreateVector(nvars); |
| 141 |
zeta_b_prior=CreateVector(nvars); |
| 142 |
for (j=nvars-1; j >= 0; j--) |
| 143 |
{ |
| 144 |
ElementV(zeta_a_prior,j)=scalar_zeta_a_prior; |
| 145 |
ElementV(zeta_b_prior,j)=scalar_zeta_b_prior; |
| 146 |
} |
| 147 |
|
| 148 |
id="//== H0barconstcell: cell(nvar,1) and n-by-n for the ith cell (equation) ==//"; |
| 149 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 150 |
A0_prior=dw_CreateArray_matrix(nvars); |
| 151 |
for (j=0; j < nvars; j++) |
| 152 |
if (!dw_ReadMatrix(f_in,A0_prior[j]=CreateMatrix(nvars,nvars))) ReadError_VARio_matlab(id); |
| 153 |
|
| 154 |
id="//== Hpbarconstcell: cell(nvar,1) and ncoef-by-ncoef for the ith cell (equation) ==//"; |
| 155 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 156 |
Aplus_prior=dw_CreateArray_matrix(nvars); |
| 157 |
for (j=0; j < nvars; j++) |
| 158 |
if (!dw_ReadMatrix(f_in,Aplus_prior[j]=CreateMatrix(npre,npre))) ReadError_VARio_matlab(id); |
| 159 |
|
| 160 |
// If not random walk specification, modify W |
| 161 |
if (!(spec & SPEC_RANDOM_WALK)) |
| 162 |
for (j=nvars-1; j >= 0; j--) |
| 163 |
{ |
| 164 |
X=ProductMM((TMatrix)NULL,Aplus_prior[j],W[j]); |
| 165 |
Y=TransposeProductMM((TMatrix)NULL,V[j],X); |
| 166 |
FreeMatrix(X); |
| 167 |
S=Inverse_LU((TMatrix)NULL,Aplus_prior[j]); |
| 168 |
X=MatrixInnerProductSymmetric((TMatrix)NULL,V[j],S); |
| 169 |
FreeMatrix(S); |
| 170 |
S=InverseProductMM((TMatrix)NULL,X,Y); |
| 171 |
FreeMatrix(Y); |
| 172 |
FreeMatrix(X); |
| 173 |
W[j]=ProductMM((TMatrix)NULL,V[j],S); |
| 174 |
FreeMatrix(S); |
| 175 |
} |
| 176 |
|
| 177 |
// Initialize Y |
| 178 |
id="//== Yleft -- Y: T-by-nvar ==//"; |
| 179 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,Y=CreateMatrix(nobs,nvars))) ReadError_VARio_matlab(id); |
| 180 |
|
| 181 |
// Initialize X |
| 182 |
id="//== Xright -- X: T-by-ncoef ==//"; |
| 183 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,X=CreateMatrix(nobs,npre))) ReadError_VARio_matlab(id); |
| 184 |
|
| 185 |
//=== Sims-Zha specification === |
| 186 |
id="//== glamdasig: sigma parameter for normal prior of lamda ==//"; |
| 187 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&lambda_prior) != 1)) ReadError_VARio_matlab(id); |
| 188 |
lambda_prior*=lambda_prior; |
| 189 |
|
| 190 |
//=== Close matlab input file === |
| 191 |
fclose(f_in); |
| 192 |
|
| 193 |
//=== Open standard input file |
| 194 |
f_in=dw_OpenTextFile(standardfile); |
| 195 |
|
| 196 |
//=== Create Markov state variable ===// |
| 197 |
sv=CreateMarkovStateVariable_File(f_in,(char*)NULL); |
| 198 |
|
| 199 |
//====== coefficient/variance state variables ====== |
| 200 |
n_state_variables=sv->n_state_variables ? sv->n_state_variables : 1; |
| 201 |
id="//== Controlling states variables for coefficients ==//"; |
| 202 |
States=(int**)NULL; |
| 203 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,States=dw_CreateRectangularArray_int(nvars,n_state_variables))) |
| 204 |
ReadError_VARio_matlab(id); |
| 205 |
coef_sv=(TMarkovStateVariable ***)dw_CreateArray_array(nvars); |
| 206 |
for (j=nvars-1; j >= 0; j--) |
| 207 |
{ |
| 208 |
for (n=i=0; i < n_state_variables; i++) |
| 209 |
if (States[j][i]) n++; |
| 210 |
if (n > 0) |
| 211 |
{ |
| 212 |
coef_sv[j]=(TMarkovStateVariable **)dw_CreateArray_pointer(n,NULL); |
| 213 |
for (n=i=0; i < n_state_variables; i++) |
| 214 |
if (States[j][i]) coef_sv[j][n++]=sv->n_state_variables ? sv->state_variable[i] : sv; |
| 215 |
} |
| 216 |
} |
| 217 |
coef_states=CreateTranslationMatrix(coef_sv,sv); |
| 218 |
dw_FreeArray(States); |
| 219 |
dw_FreeArray(coef_sv); |
| 220 |
|
| 221 |
id="//== Controlling states variables for variance ==//"; |
| 222 |
States=(int**)NULL; |
| 223 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,States=dw_CreateRectangularArray_int(nvars,n_state_variables))) |
| 224 |
ReadError_VARio_matlab(id); |
| 225 |
var_sv=(TMarkovStateVariable ***)dw_CreateArray_array(nvars); |
| 226 |
for (j=nvars-1; j >= 0; j--) |
| 227 |
{ |
| 228 |
for (n=i=0; i < n_state_variables; i++) |
| 229 |
if (States[j][i]) n++; |
| 230 |
if (n > 0) |
| 231 |
{ |
| 232 |
var_sv[j]=(TMarkovStateVariable **)dw_CreateArray_pointer(n,NULL); |
| 233 |
for (n=i=0; i < n_state_variables; i++) |
| 234 |
if (States[j][i]) var_sv[j][n++]=sv->n_state_variables ? sv->state_variable[i] : sv; |
| 235 |
} |
| 236 |
} |
| 237 |
var_states=CreateTranslationMatrix(var_sv,sv); |
| 238 |
dw_FreeArray(States); |
| 239 |
dw_FreeArray(var_sv); |
| 240 |
|
| 241 |
//=== Close standard input file === |
| 242 |
fclose(f_in); |
| 243 |
|
| 244 |
//=== Create T_VAR_Parameters structure === |
| 245 |
p=CreateTheta_VAR(spec,nvars,nlags,nexg,sv->nstates,nobs,coef_states,var_states,U,V,W,Y,X); |
| 246 |
if (spec & SPEC_SIMS_ZHA) |
| 247 |
SetPriors_VAR_SimsZha(p,A0_prior,Aplus_prior,zeta_a_prior,zeta_b_prior,lambda_prior); |
| 248 |
else |
| 249 |
SetPriors_VAR(p,A0_prior,Aplus_prior,zeta_a_prior,zeta_b_prior); |
| 250 |
|
| 251 |
//p=Create_VAR_Parameters(nvars,nlags,nexg,sv->nstates,sv->nobs,U,V,W,Zeta_a_prior,Zeta_b_prior,A0_prior,Aplus_prior,Y,X,coef_states,var_states); |
| 252 |
//SetupSimsZhaSpecification(p,delta_prior*delta_prior); |
| 253 |
|
| 254 |
//=== Create TStateModel === |
| 255 |
model=CreateStateModel(nobs,sv,CreateRoutines_VAR(),p); |
| 256 |
|
| 257 |
//=== Print Model specifications to file === |
| 258 |
|
| 259 |
//=== Free memory === |
| 260 |
FreeMatrix(X); |
| 261 |
FreeMatrix(Y); |
| 262 |
dw_FreeArray(Aplus_prior); |
| 263 |
dw_FreeArray(A0_prior); |
| 264 |
FreeVector(zeta_b_prior); |
| 265 |
FreeVector(zeta_a_prior); |
| 266 |
dw_FreeArray(W); |
| 267 |
dw_FreeArray(V); |
| 268 |
dw_FreeArray(U); |
| 269 |
|
| 270 |
return model; |
| 271 |
} |
| 272 |
|
| 273 |
/* |
| 274 |
This reads the constant parameters from filename, which was created |
| 275 |
from Matlab and then sets all the parameters to the constant parameters. |
| 276 |
*/ |
| 277 |
void ReadConstantParameters(char *filename, TStateModel *model) |
| 278 |
{ |
| 279 |
char *id; |
| 280 |
int i, j, s; |
| 281 |
FILE *f_in; |
| 282 |
TMatrix A0=(TMatrix)NULL, Aplus=(TMatrix)NULL; |
| 283 |
T_VAR_Parameters *p=(T_VAR_Parameters*)(model->theta); |
| 284 |
|
| 285 |
if (!(f_in=fopen(filename,"rt"))) |
| 286 |
{ |
| 287 |
printf("Unable to read the input data file: %s\n", filename); |
| 288 |
dw_exit(0); |
| 289 |
} |
| 290 |
|
| 291 |
// A0 |
| 292 |
id="//== A0hat: nvar-by-nvar ==//"; |
| 293 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,A0=CreateMatrix(p->nvars,p->nvars))) ReadError_VARio_matlab(id); |
| 294 |
for (j=p->nvars-1; j >= 0; j--) |
| 295 |
for (s=p->n_coef_states[j]-1; s >= 0; s--) |
| 296 |
for (i=p->nvars-1; i >= 0; i--) |
| 297 |
ElementV(p->A0[j][s],i)=ElementM(A0,i,j); |
| 298 |
FreeMatrix(A0); |
| 299 |
|
| 300 |
// Aplus |
| 301 |
id="//== Aphat: ncoef(lags*nvar+1)-by-nvar ==//"; |
| 302 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,Aplus=CreateMatrix(p->npre,p->nvars))) ReadError_VARio_matlab(id); |
| 303 |
for (j=p->nvars-1; j >= 0; j--) |
| 304 |
for (s=p->n_coef_states[j]-1; s >= 0; s--) |
| 305 |
for (i=p->npre-1; i >= 0; i--) |
| 306 |
ElementV(p->Aplus[j][s],i)=ElementM(Aplus,i,j); |
| 307 |
FreeMatrix(Aplus); |
| 308 |
|
| 309 |
// Zeta |
| 310 |
for (j=p->nvars-1; j >= 0; j--) |
| 311 |
for (s=p->n_var_states[j]-1; s >= 0; s--) |
| 312 |
p->Zeta[j][s]=1.0; |
| 313 |
|
| 314 |
// b0, bplus, lambda, and psi |
| 315 |
Update_b0_bplus_from_A0_Aplus(p); |
| 316 |
if (p->Specification & SPEC_SIMS_ZHA) Update_lambda_psi_from_bplus(p); |
| 317 |
|
| 318 |
// Flags |
| 319 |
p->valid_parameters=1; |
| 320 |
|
| 321 |
// Transition matrix |
| 322 |
DefaultTransitionMatrixParameters(model); |
| 323 |
|
| 324 |
ThetaChanged(model); |
| 325 |
|
| 326 |
// Close input file |
| 327 |
fclose(f_in); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
Create Model from Matlab data file |
| 332 |
*/ |
| 333 |
TStateModel* CreateStateModel_VAR_matlab(char *filename) |
| 334 |
{ |
| 335 |
T_VAR_Parameters *p; |
| 336 |
FILE *f_in; |
| 337 |
char *id; |
| 338 |
TMatrix PriorTransitionMatrix, S; |
| 339 |
int *IV, **IM; |
| 340 |
PRECISION scalar_Zeta_a_prior, scalar_Zeta_b_prior, lambda_prior; |
| 341 |
int i, j, nvars, nlags, nexg, npre, nobs, nstates; |
| 342 |
TMatrix *U, *V, *W; |
| 343 |
TVector Zeta_a_prior, Zeta_b_prior; |
| 344 |
TMatrix *A0_prior, *Aplus_prior; |
| 345 |
TMatrix Y=(TMatrix)NULL, X=(TMatrix)NULL; |
| 346 |
int **coef_states, **var_states; |
| 347 |
TMarkovStateVariable *sv; |
| 348 |
|
| 349 |
//=== Open file === |
| 350 |
if (!(f_in=fopen(filename,"rt"))) |
| 351 |
{ |
| 352 |
printf("Unable to read the input data file: %s\n", filename); |
| 353 |
dw_exit(0); |
| 354 |
} |
| 355 |
|
| 356 |
//=== Read sizes ===// |
| 357 |
id="//== lags, nvar, nStates, T ==//"; |
| 358 |
if (!dw_SetFilePosition(f_in,id) |
| 359 |
|| (fscanf(f_in," %d %d %d %d ",&nlags,&nvars,&nstates,&nobs) != 4)) ReadError_VARio_matlab(id); |
| 360 |
|
| 361 |
//=== A single constant term ===// |
| 362 |
nexg=1; |
| 363 |
npre=nvars * nlags + nexg; |
| 364 |
|
| 365 |
//=== Restrictions - U[j] ===// |
| 366 |
IV=dw_CreateArray_int(nvars); |
| 367 |
id="//== n0const: nvar-by-1 ==//"; |
| 368 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,IV)) ReadError_VARio_matlab(id); |
| 369 |
id="//== Uiconst: cell(nvar,1) and nvar-by-n0const(i) for the ith cell (equation) ==//"; |
| 370 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 371 |
U=dw_CreateArray_matrix(nvars); |
| 372 |
for (j=0; j < nvars; j++) |
| 373 |
if (!dw_ReadMatrix(f_in,U[j]=CreateMatrix(nvars,IV[j]))) ReadError_VARio_matlab(id); |
| 374 |
dw_FreeArray(IV); |
| 375 |
|
| 376 |
//=== Restrictions - V[j] (V[j] should be an npre x npre identity matrix) ===// |
| 377 |
IV=dw_CreateArray_int(nvars); |
| 378 |
id="//== npconst: nvar-by-1 ==//"; |
| 379 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,IV)) ReadError_VARio_matlab(id); |
| 380 |
for (j=nvars-1; j >= 0; j--) |
| 381 |
if (IV[j] != npre) |
| 382 |
{ |
| 383 |
printf("V[%d] not %d x %d\n",j,npre,npre); |
| 384 |
dw_exit(0); |
| 385 |
} |
| 386 |
V=dw_CreateArray_matrix(nvars); |
| 387 |
for (j=nvars-1; j >= 0; j--) |
| 388 |
V[j]=IdentityMatrix((TMatrix)NULL,npre); |
| 389 |
dw_FreeArray(IV); |
| 390 |
|
| 391 |
//=== Restrictions - W[j] (Random walk specification) ===// |
| 392 |
InitializeMatrix(S=CreateMatrix(npre,nvars),0.0); |
| 393 |
for (j=nvars-1; j >= 0; j--) ElementM(S,j,j)=-1.0; |
| 394 |
W=dw_CreateArray_matrix(nvars); |
| 395 |
for (j=nvars-1; j >= 0; j--) |
| 396 |
W[j]=EquateMatrix((TMatrix)NULL,S); |
| 397 |
FreeMatrix(S); |
| 398 |
|
| 399 |
//=== Create TMarkovStateVariable ===// |
| 400 |
PriorTransitionMatrix=CreateMatrix(nstates,nstates); |
| 401 |
id="//== Galpha: nStates-by-nStates ==//"; |
| 402 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,PriorTransitionMatrix)) ReadError_VARio_matlab(id); |
| 403 |
sv=CreateMarkovStateVariable_NoRestrictions(nstates,nobs,PriorTransitionMatrix); |
| 404 |
FreeMatrix(PriorTransitionMatrix); |
| 405 |
|
| 406 |
//====== regime/shock state variables ====== |
| 407 |
coef_states=dw_CreateRectangularArray_int(nvars,nstates); |
| 408 |
var_states=dw_CreateRectangularArray_int(nvars,nstates); |
| 409 |
IM=dw_CreateRectangularArray_int(nvars,2); |
| 410 |
id="//== indxEqnTv_m: nvar-by-2 ==//"; |
| 411 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,IM)) ReadError_VARio_matlab(id); |
| 412 |
for (j=nvars-1; j >= 0; j--) |
| 413 |
switch (IM[j][1]) |
| 414 |
{ |
| 415 |
case 1: |
| 416 |
for (i=nstates-1; i >= 0; i--) |
| 417 |
coef_states[j][i]=var_states[j][i]=0; |
| 418 |
break; |
| 419 |
case 2: |
| 420 |
for (i=nstates-1; i >= 0; i--) |
| 421 |
{ |
| 422 |
coef_states[j][i]=0; |
| 423 |
var_states[j][i]=i; |
| 424 |
} |
| 425 |
break; |
| 426 |
case 3: |
| 427 |
for (i=nstates-1; i >= 0; i--) |
| 428 |
{ |
| 429 |
coef_states[j][i]=i; |
| 430 |
var_states[j][i]=0; |
| 431 |
} |
| 432 |
break; |
| 433 |
case 4: |
| 434 |
printf("Case %d not implemented.\n",4); |
| 435 |
dw_exit(0); |
| 436 |
default: |
| 437 |
printf("Unknown type.\n"); |
| 438 |
dw_exit(0); |
| 439 |
} |
| 440 |
dw_FreeArray(IM); |
| 441 |
|
| 442 |
//====== Priors ====== |
| 443 |
id="//== gxia: alpha parameter for gamma prior of xi ==//"; |
| 444 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&scalar_Zeta_a_prior) != 1)) ReadError_VARio_matlab(id); |
| 445 |
id="//== gxib: beta parameter for gamma prior of xi ==//"; |
| 446 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&scalar_Zeta_b_prior) != 1)) ReadError_VARio_matlab(id); |
| 447 |
Zeta_a_prior=CreateVector(nvars); |
| 448 |
Zeta_b_prior=CreateVector(nvars); |
| 449 |
for (j=nvars-1; j >= 0; j--) |
| 450 |
{ |
| 451 |
ElementV(Zeta_a_prior,j)=scalar_Zeta_a_prior; |
| 452 |
ElementV(Zeta_b_prior,j)=scalar_Zeta_b_prior; |
| 453 |
} |
| 454 |
|
| 455 |
id="//== H0barconstcell: cell(nvar,1) and n-by-n for the ith cell (equation) ==//"; |
| 456 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 457 |
A0_prior=dw_CreateArray_matrix(nvars); |
| 458 |
for (j=0; j < nvars; j++) |
| 459 |
if (!dw_ReadMatrix(f_in,A0_prior[j]=CreateMatrix(nvars,nvars))) ReadError_VARio_matlab(id); |
| 460 |
|
| 461 |
id="//== Hpbarconstcell: cell(nvar,1) and ncoef-by-ncoef for the ith cell (equation) ==//"; |
| 462 |
if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); |
| 463 |
Aplus_prior=dw_CreateArray_matrix(nvars); |
| 464 |
for (j=0; j < nvars; j++) |
| 465 |
if (!dw_ReadMatrix(f_in,Aplus_prior[j]=CreateMatrix(npre,npre))) ReadError_VARio_matlab(id); |
| 466 |
|
| 467 |
/* //=========================== Checks */ |
| 468 |
/* TMatrix *H0, *Ui; */ |
| 469 |
/* int *n0s; */ |
| 470 |
/* TMatrix Sigma, XX, YY, ZZ; */ |
| 471 |
/* int i, ii, jj; */ |
| 472 |
/* PRECISION max; */ |
| 473 |
|
| 474 |
/* id="//== n0s: nvar-by-1 ==//"; */ |
| 475 |
/* if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,n0s=dw_CreateArray_int(nvars))) ReadError_VARio_matlab(id); */ |
| 476 |
|
| 477 |
/* id="//== H0tldcell_inv: cell(nvar,1) and n0s(i)-by-n0s(i) for the ith cell ==//"; */ |
| 478 |
/* if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); */ |
| 479 |
/* H0=dw_CreateArray_matrix(nvars); */ |
| 480 |
/* for (j=0; j < nvars; j++) */ |
| 481 |
/* if (!dw_ReadMatrix(f_in,H0[j]=CreateMatrix(n0s[j],n0s[j]))) ReadError_VARio_matlab(id); */ |
| 482 |
|
| 483 |
/* id="//== Ui: cell(nvar,1) and nvar*nStates-by-n0s(i) for the ith cell ==//"; */ |
| 484 |
/* if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); */ |
| 485 |
/* Ui=dw_CreateArray_matrix(nvars); */ |
| 486 |
/* for (j=0; j < nvars; j++) */ |
| 487 |
/* if (!dw_ReadMatrix(f_in,Ui[j]=CreateMatrix(nvars*nstates,n0s[j]))) ReadError_VARio_matlab(id); */ |
| 488 |
|
| 489 |
/* Sigma=CreateMatrix(nvars*nstates,nvars*nstates); */ |
| 490 |
/* XX=CreateMatrix(nvars,nvars); */ |
| 491 |
/* for (j=0; j < nvars; j++) */ |
| 492 |
/* { */ |
| 493 |
/* InitializeMatrix(Sigma,0.0); */ |
| 494 |
/* Inverse_LU(XX,A0_prior[j]); */ |
| 495 |
/* for (i=0; i < nstates; i++) */ |
| 496 |
/* for (ii=0; ii < nvars; ii++) */ |
| 497 |
/* for (jj=0; jj < nvars; jj++) */ |
| 498 |
/* ElementM(Sigma,i*nvars+ii,i*nvars+jj)=ElementM(XX,ii,jj); */ |
| 499 |
/* YY=TransposeProductMM((TMatrix)NULL,Ui[j],Sigma); */ |
| 500 |
/* ZZ=ProductMM((TMatrix)NULL,YY,Ui[j]); */ |
| 501 |
|
| 502 |
/* printf("Computed[%d]\n",j); dw_PrintMatrix(stdout,ZZ,"%le "); printf("\n"); */ |
| 503 |
/* printf("File[%d]\n",j); dw_PrintMatrix(stdout,H0[j],"%le "); printf("\n"); */ |
| 504 |
/* max=0.0; */ |
| 505 |
/* for (ii=0; ii < RowM(ZZ); ii++) */ |
| 506 |
/* for (jj=0; jj < ColM(ZZ); jj++) */ |
| 507 |
/* if (max < fabs(ElementM(H0[j],ii,jj) - ElementM(ZZ,ii,jj))) max=fabs(ElementM(H0[j],ii,jj) - ElementM(ZZ,ii,jj)); */ |
| 508 |
/* printf("H0: max[%d] = %le\n",j,max); */ |
| 509 |
|
| 510 |
/* FreeMatrix(ZZ); */ |
| 511 |
/* FreeMatrix(YY); */ |
| 512 |
/* getc(stdin); */ |
| 513 |
/* } */ |
| 514 |
/* //dw_exit(0); */ |
| 515 |
|
| 516 |
/* id="//== nps: nvar-by-1 ==//"; */ |
| 517 |
/* if (!dw_SetFilePosition(f_in,id) || !dw_ReadArray(f_in,n0s=dw_CreateArray_int(nvars))) ReadError_VARio_matlab(id); */ |
| 518 |
|
| 519 |
/* id="//== Hptldcell_inv: cell(nvar,1) and nps(i)-by-nps(i) for the ith cell ==//"; */ |
| 520 |
/* if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); */ |
| 521 |
/* H0=dw_CreateArray_matrix(nvars); */ |
| 522 |
/* for (j=0; j < nvars; j++) */ |
| 523 |
/* if (!dw_ReadMatrix(f_in,H0[j]=CreateMatrix(n0s[j],n0s[j]))) ReadError_VARio_matlab(id); */ |
| 524 |
|
| 525 |
/* id="//== Vi: cell(nvar,1) and k*nStates-by-nps(i) for the ith cell ==//"; */ |
| 526 |
/* if (!dw_SetFilePosition(f_in,id)) ReadError_VARio_matlab(id); */ |
| 527 |
/* Ui=dw_CreateArray_matrix(nvars); */ |
| 528 |
/* for (j=0; j < nvars; j++) */ |
| 529 |
/* if (!dw_ReadMatrix(f_in,Ui[j]=CreateMatrix(npre*nstates,n0s[j]))) ReadError_VARio_matlab(id); */ |
| 530 |
|
| 531 |
/* Sigma=CreateMatrix(npre*nstates,npre*nstates); */ |
| 532 |
/* XX=CreateMatrix(npre,npre); */ |
| 533 |
/* for (j=0; j < nvars; j++) */ |
| 534 |
/* { */ |
| 535 |
/* InitializeMatrix(Sigma,0.0); */ |
| 536 |
/* Inverse_LU(XX,Aplus_prior[j]); */ |
| 537 |
/* for (i=0; i < nstates; i++) */ |
| 538 |
/* for (ii=0; ii < npre; ii++) */ |
| 539 |
/* for (jj=0; jj < npre; jj++) */ |
| 540 |
/* ElementM(Sigma,i*npre+ii,i*npre+jj)=ElementM(XX,ii,jj); */ |
| 541 |
/* YY=TransposeProductMM((TMatrix)NULL,Ui[j],Sigma); */ |
| 542 |
/* ZZ=ProductMM((TMatrix)NULL,YY,Ui[j]); */ |
| 543 |
|
| 544 |
/* max=0.0; */ |
| 545 |
/* for (ii=0; ii < RowM(ZZ); ii++) */ |
| 546 |
/* for (jj=0; jj < ColM(ZZ); jj++) */ |
| 547 |
/* if (max < fabs(ElementM(H0[j],ii,jj) - ElementM(ZZ,ii,jj))) max=fabs(ElementM(H0[j],ii,jj) - ElementM(ZZ,ii,jj)); */ |
| 548 |
/* printf("max[%d] = %le\n",j,max); */ |
| 549 |
|
| 550 |
/* FreeMatrix(ZZ); */ |
| 551 |
/* FreeMatrix(YY); */ |
| 552 |
/* getc(stdin); */ |
| 553 |
/* } */ |
| 554 |
/* dw_exit(0); */ |
| 555 |
/* //=========================== Checks */ |
| 556 |
|
| 557 |
// Initialize Y |
| 558 |
id="//== Yleft -- Y: T-by-nvar ==//"; |
| 559 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,Y=CreateMatrix(nobs,nvars))) ReadError_VARio_matlab(id); |
| 560 |
|
| 561 |
// Initialize X |
| 562 |
id="//== Xright -- X: T-by-ncoef ==//"; |
| 563 |
if (!dw_SetFilePosition(f_in,id) || !dw_ReadMatrix(f_in,X=CreateMatrix(nobs,npre))) ReadError_VARio_matlab(id); |
| 564 |
|
| 565 |
//=== Create T_VAR_Parameters structure === |
| 566 |
p=CreateTheta_VAR(SPEC_SIMS_ZHA | SPEC_RANDOM_WALK,nvars,nlags,nexg,nstates,nobs,coef_states,var_states,U,V,W,Y,X); |
| 567 |
|
| 568 |
//=== Sims-Zha specification === |
| 569 |
id="//== glamdasig: sigma parameter for normal prior of lamda ==//"; |
| 570 |
if (!dw_SetFilePosition(f_in,id) || (fscanf(f_in," %lf ",&lambda_prior) != 1)) ReadError_VARio_matlab(id); |
| 571 |
SetPriors_VAR_SimsZha(p,A0_prior,Aplus_prior,Zeta_a_prior,Zeta_b_prior,lambda_prior*lambda_prior); |
| 572 |
|
| 573 |
//=== Close input file === |
| 574 |
fclose(f_in); |
| 575 |
|
| 576 |
//=== Create TStateModel === |
| 577 |
return CreateStateModel(nobs,sv,CreateRoutines_VAR(),p); |
| 578 |
} |
| 579 |
/*******************************************************************************/ |
| 580 |
/*******************************************************************************/ |
| 581 |
/*******************************************************************************/ |