| 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_switchio.h" |
| 19 |
#include "dw_metropolis_theta.h" |
| 20 |
#include "dw_array.h" |
| 21 |
#include "dw_matrix_array.h" |
| 22 |
#include "dw_error.h" |
| 23 |
#include "dw_ascii.h" |
| 24 |
#include "dw_std.h" |
| 25 |
#include "dw_math.h" |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <math.h> |
| 30 |
#include <limits.h> |
| 31 |
|
| 32 |
/******************************************************************************/ |
| 33 |
/************************** TStateVariable Routines ***************************/ |
| 34 |
/******************************************************************************/ |
| 35 |
/* |
| 36 |
Assumes: |
| 37 |
f : pointer to valid FILE structure or null pointer |
| 38 |
filename : pointer to null terminated string or null |
| 39 |
|
| 40 |
Returns: |
| 41 |
Valid pointer to a TMarkovStateVariable structure upon success and null |
| 42 |
pointer upon failure. |
| 43 |
|
| 44 |
Results: |
| 45 |
Reads Markov specification from file and creates TMarkovStateVariable |
| 46 |
structure. |
| 47 |
|
| 48 |
Notes: |
| 49 |
One of f or filename should be non-null. |
| 50 |
*/ |
| 51 |
TMarkovStateVariable* ReadMarkovSpecification(FILE *f, char *filename) |
| 52 |
{ |
| 53 |
FILE *f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL; |
| 54 |
TMarkovStateVariable *sv=(f_in) ? sv_ReadMarkovSpecification(f_in,"") : (TMarkovStateVariable*)NULL; |
| 55 |
if (!f && f_in) fclose(f_in); |
| 56 |
return sv; |
| 57 |
} |
| 58 |
|
| 59 |
/* |
| 60 |
Assumes: |
| 61 |
f : pointer to valid FILE structure or null pointer |
| 62 |
filename : pointer to null terminated string or null |
| 63 |
model : pointer to valid TStateModel structure. |
| 64 |
|
| 65 |
Returns: |
| 66 |
one upon upon success and zero upon failure. |
| 67 |
|
| 68 |
Results: |
| 69 |
Appends Markov specification to file. |
| 70 |
|
| 71 |
Notes: |
| 72 |
One of f or filename should be non-null. |
| 73 |
*/ |
| 74 |
int WriteMarkovSpecification(FILE *f, char *filename, TStateModel *model) |
| 75 |
{ |
| 76 |
FILE *f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 77 |
int rtrn=(f_out) ? sv_WriteMarkovSpecification(f_out,model->sv,"") : 0; |
| 78 |
if (!f && f_out) fclose(f_out); |
| 79 |
return rtrn; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
Assumes: |
| 84 |
f : pointer to valid FILE structure or null pointer |
| 85 |
filename : pointer to null terminated string or null |
| 86 |
header : pointer to null terminated string or null |
| 87 |
model : pointer to valid TStateModel structure. |
| 88 |
|
| 89 |
Returns: |
| 90 |
one upon upon success and zero upon failure. |
| 91 |
|
| 92 |
Results: |
| 93 |
Reads states from file and checks to ensure that the states are valid. |
| 94 |
|
| 95 |
Notes: |
| 96 |
One of f or filename should be non-null. |
| 97 |
*/ |
| 98 |
int ReadStates(FILE *f, char *filename, char *header, TStateModel *model) |
| 99 |
{ |
| 100 |
FILE *f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL; |
| 101 |
char *format="//== %sStates ==//"; |
| 102 |
int err, i; |
| 103 |
|
| 104 |
if (!f_in) |
| 105 |
err=SWITCHIO_FILE_ERROR; |
| 106 |
else |
| 107 |
{ |
| 108 |
if (!(err=ReadIntArray(f_in,format,header,model->S))) |
| 109 |
{ |
| 110 |
// Check states |
| 111 |
for (i=model->nobs; i >= 0; i--) |
| 112 |
if ((model->S[i] < 0) || (model->S[i] >= model->sv->nstates)) |
| 113 |
{ |
| 114 |
for ( ; i >= 0; i--) model->S[i]=0; |
| 115 |
err=SWITCHIO_ERROR_READING_DATA; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
else |
| 120 |
for (i=model->nobs; i >= 0; i--) model->S[i]=0; |
| 121 |
StatesChanged(model); |
| 122 |
} |
| 123 |
|
| 124 |
if (err) ReadError(format,header,err); |
| 125 |
|
| 126 |
if (!f && f_in) fclose(f_in); |
| 127 |
|
| 128 |
return err ? 0 : 1; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
Assumes: |
| 133 |
f : pointer to valid FILE structure or null pointer |
| 134 |
filename : pointer to null terminated string or null |
| 135 |
header : pointer to null terminated string or null |
| 136 |
model : pointer to valid TStateModel structure. |
| 137 |
|
| 138 |
Returns: |
| 139 |
one upon upon success and zero upon failure. |
| 140 |
|
| 141 |
Results: |
| 142 |
Writes states to file. |
| 143 |
|
| 144 |
Notes: |
| 145 |
One of f or filename should be non-null. |
| 146 |
*/ |
| 147 |
int WriteStates(FILE *f, char *filename, char *header, TStateModel *model) |
| 148 |
{ |
| 149 |
FILE *f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 150 |
fprintf(f_out,"//== %sStates ==//\n",header); |
| 151 |
dw_PrintArray(f_out,model->S,"%d "); |
| 152 |
fprintf(f_out,"\n"); |
| 153 |
if (!f && f_out) fclose(f_out); |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/* |
| 159 |
Assumes: |
| 160 |
f : pointer to valid FILE structure or null pointer |
| 161 |
filename : pointer to null terminated string or null |
| 162 |
header : pointer to null terminated string. |
| 163 |
model : pointer to valid TStateModel structure. |
| 164 |
|
| 165 |
Returns: |
| 166 |
One upon success and zero upon failure. |
| 167 |
|
| 168 |
Results: |
| 169 |
Attempts to read transition matrix parameters into model. |
| 170 |
|
| 171 |
Notes: |
| 172 |
One of f or filename should be non-null. Calls TransitionMatricesParametersChanged(). |
| 173 |
*/ |
| 174 |
int ReadTransitionMatrixParameters(FILE *f, char *filename, char *header, TStateModel *model) |
| 175 |
{ |
| 176 |
FILE *f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL; |
| 177 |
int rtrn=(f_in) ? sv_ReadTransitionMatrixParameters(f_in,model->sv,header) : 0; |
| 178 |
TransitionMatrixParametersChanged(model); |
| 179 |
if (!f && f_in) fclose(f_in); |
| 180 |
return rtrn; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
Assumes: |
| 185 |
f : pointer to valid FILE pointer or null pointer |
| 186 |
filename : pointer to null terminated string or null |
| 187 |
header : pointer to null terminated string. |
| 188 |
model : pointer to valid TStateModel structure. |
| 189 |
|
| 190 |
Returns: |
| 191 |
One upon success and zero upon failure. |
| 192 |
|
| 193 |
Results: |
| 194 |
Writes free transition matrix parameters |
| 195 |
|
| 196 |
Notes: |
| 197 |
One of f or filename should be non-null. |
| 198 |
*/ |
| 199 |
int WriteTransitionMatrixParameters(FILE *f, char *filename, char *header, TStateModel *model) |
| 200 |
{ |
| 201 |
FILE *f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 202 |
int rtrn=(f_out) ? sv_WriteTransitionMatrixParameters(f_out,model->sv,header) : 0; |
| 203 |
if (!f && f_out) fclose(f_out); |
| 204 |
return rtrn; |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
Assumes: |
| 209 |
f : pointer to valid FILE pointer or null pointer. |
| 210 |
filename : pointer to null terminated string or null. |
| 211 |
header : pointer to null terminated string. |
| 212 |
model : pointer to valid TStateModel structure. |
| 213 |
|
| 214 |
Returns: |
| 215 |
One upon success and zero otherwise. |
| 216 |
|
| 217 |
Results: |
| 218 |
Writes free theta parameters |
| 219 |
|
| 220 |
Notes: |
| 221 |
One of f or filename should be non-null. |
| 222 |
*/ |
| 223 |
int ReadTheta(FILE *f, char *filename, char *header, TStateModel *model) |
| 224 |
{ |
| 225 |
int err, n=NumberFreeParametersTheta(model); |
| 226 |
char *format="//== %sFree Theta Parameters ==//"; |
| 227 |
TVector theta; |
| 228 |
FILE *f_in; |
| 229 |
if (n == 0) return 1; |
| 230 |
if ((f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL)) |
| 231 |
{ |
| 232 |
if (!header) header=""; |
| 233 |
if ((err=ReadVector(f_in,format,header,theta=CreateVector(n)))) |
| 234 |
ReadError(format,header,err); |
| 235 |
else |
| 236 |
ConvertFreeParametersToTheta(model,pElementV(theta)); |
| 237 |
FreeVector(theta); |
| 238 |
if (!f) fclose(f_in); |
| 239 |
return err ? 0 : 1; |
| 240 |
} |
| 241 |
else |
| 242 |
{ |
| 243 |
ReadError((char*)NULL,(char*)NULL,SWITCHIO_FILE_ERROR); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
Assumes: |
| 250 |
f : pointer to valid FILE pointer or null pointer. |
| 251 |
filename : pointer to null terminated string or null. |
| 252 |
header : pointer to null terminated string. |
| 253 |
model : pointer to valid TStateModel structure. |
| 254 |
|
| 255 |
Returns: |
| 256 |
One upon success and zero otherwise. |
| 257 |
|
| 258 |
Results: |
| 259 |
Writes free theta parameters |
| 260 |
|
| 261 |
Notes: |
| 262 |
One of f or filename should be non-null. |
| 263 |
*/ |
| 264 |
int WriteTheta(FILE *f, char *filename, char *header, TStateModel *model) |
| 265 |
{ |
| 266 |
PRECISION *theta; |
| 267 |
int i, rtrn=0, n=NumberFreeParametersTheta(model); |
| 268 |
FILE *f_out; |
| 269 |
if (n == 0) return 1; |
| 270 |
if ((f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL)) |
| 271 |
{ |
| 272 |
if ((theta=(PRECISION*)dw_malloc(n*sizeof(PRECISION)))) |
| 273 |
{ |
| 274 |
ConvertThetaToFreeParameters(model,theta); |
| 275 |
if (!header) header=""; |
| 276 |
fprintf(f_out,"//== %sFree Theta Parameters ==//\n",header); |
| 277 |
for (i=0; i < n; i++) fprintf(f_out,"%22.14le ",(double)(theta[i])); |
| 278 |
fprintf(f_out,"\n\n"); |
| 279 |
dw_free(theta); |
| 280 |
rtrn=1; |
| 281 |
} |
| 282 |
if (!f) fclose(f_out); |
| 283 |
} |
| 284 |
else |
| 285 |
ReadError((char*)NULL,(char*)NULL,SWITCHIO_FILE_ERROR); |
| 286 |
return rtrn; |
| 287 |
} |
| 288 |
|
| 289 |
/* |
| 290 |
Assumes: |
| 291 |
f : pointer to valid FILE structure or null pointer |
| 292 |
filename : pointer to null terminated string or null |
| 293 |
header : pointer to null terminated string or null |
| 294 |
model : pointer to valid TStateModel structure. |
| 295 |
|
| 296 |
Returns: |
| 297 |
One upon success and zero upon failure. |
| 298 |
|
| 299 |
Results: |
| 300 |
Attempts to read transition matrix into model. |
| 301 |
|
| 302 |
Notes: |
| 303 |
One of f or filename should be non-null. Calls TransitionMatricesParametersChanged(). |
| 304 |
*/ |
| 305 |
int ReadBaseTransitionMatrices_t(int t, FILE *f, char *filename, char *header, TStateModel *model) |
| 306 |
{ |
| 307 |
int rtrn=0; |
| 308 |
FILE *f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL; |
| 309 |
if (f_in) |
| 310 |
{ |
| 311 |
TransitionMatrixParametersChanged(model); |
| 312 |
rtrn=sv_ReadBaseTransitionMatrices_t(t,f_in,header,"",model->sv,model); |
| 313 |
if (!f) fclose(f_in); |
| 314 |
} |
| 315 |
return rtrn; |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
Assumes: |
| 320 |
f : pointer to valid FILE structure or null pointer |
| 321 |
filename : pointer to null terminated string or null |
| 322 |
header : pointer to null terminated string or null |
| 323 |
model : pointer to valid TStateModel structure. |
| 324 |
|
| 325 |
Returns: |
| 326 |
One upon success and zero upon failure. |
| 327 |
|
| 328 |
Results: |
| 329 |
Attempts to read transition matrix into model. |
| 330 |
|
| 331 |
Notes: |
| 332 |
One of f or filename should be non-null. Calls TransitionMatricesParametersChanged(). |
| 333 |
*/ |
| 334 |
int ReadBaseTransitionMatricesFlat_t(int t, FILE *f, char *filename, TStateModel *model) |
| 335 |
{ |
| 336 |
int rtrn=0; |
| 337 |
FILE *f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL; |
| 338 |
if (f_in) |
| 339 |
{ |
| 340 |
TransitionMatrixParametersChanged(model); |
| 341 |
rtrn=sv_ReadBaseTransitionMatricesFlat_t(t,f_in,model->sv,model); |
| 342 |
if (!f) fclose(f_in); |
| 343 |
} |
| 344 |
return rtrn; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
/* |
| 349 |
Assumes: |
| 350 |
t : time |
| 351 |
f : pointer to valid FILE structure or null pointer |
| 352 |
filename : pointer to null terminated string or null |
| 353 |
header : pointer to null terminated string |
| 354 |
model : pointer to valid TStateModel structure. |
| 355 |
|
| 356 |
Returns: |
| 357 |
One upon success and zero upon failure. |
| 358 |
|
| 359 |
Results: |
| 360 |
Writes transition matrix. |
| 361 |
|
| 362 |
Notes: |
| 363 |
One of f or filename should be non-null. |
| 364 |
*/ |
| 365 |
int WriteGrandTransitionMatrix_t(int t, FILE *f, char *filename, char *header, TStateModel *model) |
| 366 |
{ |
| 367 |
FILE *f_out; |
| 368 |
int rtrn=0; |
| 369 |
if (sv_ComputeTransitionMatrix(t,model->sv,model)) |
| 370 |
if ((f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL)) |
| 371 |
{ |
| 372 |
fprintf(f_out,"//== %sGrand transition matrix%s ==//\n",header ? header : "",""); |
| 373 |
rtrn=dw_PrintMatrix(f_out,model->sv->Q,"%.14le "); |
| 374 |
fprintf(f_out,"\n"); |
| 375 |
if (!f) fclose(f_out); |
| 376 |
} |
| 377 |
return rtrn; |
| 378 |
} |
| 379 |
|
| 380 |
/* |
| 381 |
Assumes: |
| 382 |
f : pointer to valid FILE structure or null pointer |
| 383 |
filename : pointer to null terminated string or null |
| 384 |
header : pointer to null terminated string |
| 385 |
model : pointer to valid TStateModel structure. |
| 386 |
|
| 387 |
Returns: |
| 388 |
One upon success and zero upon failure. |
| 389 |
|
| 390 |
Results: |
| 391 |
Writes transition matrix. |
| 392 |
|
| 393 |
Notes: |
| 394 |
One of f or filename should be non-null. |
| 395 |
*/ |
| 396 |
int WriteBaseTransitionMatrices_t(int t, FILE *f, char *filename, char *header, TStateModel *model) |
| 397 |
{ |
| 398 |
FILE *f_out; |
| 399 |
int rtrn=0; |
| 400 |
if (sv_ComputeTransitionMatrix(t,model->sv,model)) |
| 401 |
{ |
| 402 |
f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 403 |
rtrn=(f_out) ? sv_WriteBaseTransitionMatrices(f_out,model->sv,header,"") : 0; |
| 404 |
if (!f && f_out) fclose(f_out); |
| 405 |
} |
| 406 |
return rtrn; |
| 407 |
} |
| 408 |
|
| 409 |
/* |
| 410 |
Assumes: |
| 411 |
f : pointer to valid FILE structure or null pointer |
| 412 |
filename : pointer to null terminated string or null |
| 413 |
header : pointer to null terminated string |
| 414 |
model : pointer to valid TStateModel structure. |
| 415 |
|
| 416 |
Returns: |
| 417 |
One upon success and zero upon failure. |
| 418 |
|
| 419 |
Results: |
| 420 |
Writes transition matrix in a flat format. |
| 421 |
|
| 422 |
Notes: |
| 423 |
One of f or filename should be non-null. |
| 424 |
*/ |
| 425 |
int WriteBaseTransitionMatricesFlat_t(int t, FILE *f, char *filename, TStateModel *model, char* fmt) |
| 426 |
{ |
| 427 |
FILE *f_out; |
| 428 |
int rtrn=0; |
| 429 |
if (sv_ComputeTransitionMatrix(t,model->sv,model)) |
| 430 |
{ |
| 431 |
f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 432 |
rtrn=(f_out) ? sv_WriteBaseTransitionMatricesFlat(f_out,model->sv,fmt) : 0; |
| 433 |
if (!f && f_out) fclose(f_out); |
| 434 |
} |
| 435 |
return rtrn; |
| 436 |
} |
| 437 |
|
| 438 |
int WriteBaseTransitionMatricesFlat_Headers(FILE *f, TStateModel *model) |
| 439 |
{ |
| 440 |
return f ? sv_WriteBaseTransitionMatricesFlat_Headers(f,model->sv,"") : 0; |
| 441 |
} |
| 442 |
|
| 443 |
int ReadFreeParameters(FILE *f, char *filename, TStateModel *model) |
| 444 |
{ |
| 445 |
int n; |
| 446 |
TVector parameters; |
| 447 |
FILE *f_in=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 448 |
if (f_in) |
| 449 |
{ |
| 450 |
if (dw_ReadVector(f_in,parameters=CreateVector((n=NumberFreeParametersTheta(model))+NumberFreeParametersQ(model)))) |
| 451 |
{ |
| 452 |
ConvertFreeParametersToTheta(model,pElementV(parameters)); |
| 453 |
ConvertFreeParametersToQ(model,pElementV(parameters)+n); |
| 454 |
} |
| 455 |
FreeVector(parameters); |
| 456 |
if (!f) fclose(f_in); |
| 457 |
return 1; |
| 458 |
} |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
/* |
| 463 |
Writes the free parameters to the file pointer f in non-null or the file |
| 464 |
specified by filename. Writes the free Theta parameters first followed by the |
| 465 |
free Q parameters. |
| 466 |
*/ |
| 467 |
int WriteFreeParameters(FILE *f, char *filename, TStateModel *model) |
| 468 |
{ |
| 469 |
int n; |
| 470 |
TVector parameters; |
| 471 |
FILE *f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL; |
| 472 |
if (f_out) |
| 473 |
{ |
| 474 |
if ((parameters=CreateVector((n=NumberFreeParametersTheta(model))+NumberFreeParametersQ(model)))) |
| 475 |
{ |
| 476 |
ConvertThetaToFreeParameters(model,pElementV(parameters)); |
| 477 |
ConvertQToFreeParameters(model,pElementV(parameters)+n); |
| 478 |
dw_PrintVector(f_out,parameters,"%.12le "); |
| 479 |
FreeVector(parameters); |
| 480 |
if (!f) fclose(f_out); |
| 481 |
return 1; |
| 482 |
} |
| 483 |
if (!f) fclose(f_out); |
| 484 |
} |
| 485 |
return 0; |
| 486 |
} |
| 487 |
/*******************************************************************************/ |
| 488 |
/*******************************************************************************/ |
| 489 |
/*******************************************************************************/ |
| 490 |
|
| 491 |
/* |
| 492 |
The file contains 2 + nparameters columns which are to be interpreted as |
| 493 |
|
| 494 |
Column 0 : 0 = log posterior density |
| 495 |
Column 1 : 1 = log likelihood |
| 496 |
Columns 2 : 1 + nparameters = parameters |
| 497 |
|
| 498 |
The file is a delimited ascii file and the delimiter is a space (' '). |
| 499 |
|
| 500 |
Creates and returns a m x (2 + nparameters) matrix containing the data in the |
| 501 |
file. |
| 502 |
|
| 503 |
Fields that are empty or do not parse into valid floating point numbers are |
| 504 |
ignored. |
| 505 |
|
| 506 |
Rows that do not contain 2 + nparameter valid fields are ignored. |
| 507 |
|
| 508 |
One of f_in, filename or tag must not be null. If f_in is not null, this file |
| 509 |
is read, starting at the current pointer. Otherwise, if filename is not null |
| 510 |
then it is opened and read. If both f_in and filename are null, then the file |
| 511 |
simulation_<tag>.out is opened and read. |
| 512 |
*/ |
| 513 |
TMatrix dw_ReadPosteriorDraws(FILE *f_in, char *filename, char *tag, int nparameters) |
| 514 |
{ |
| 515 |
char *fmt="simulation_%s.out", delimiter=' '; |
| 516 |
TMatrix posterior_draws=(TMatrix)NULL; |
| 517 |
int free_filename=0, m=0, cols=2+nparameters, i, j, k; |
| 518 |
double** Y; |
| 519 |
|
| 520 |
// setup filename |
| 521 |
if (!f_in && !filename) |
| 522 |
{ |
| 523 |
if (!tag) |
| 524 |
{ |
| 525 |
dw_UserError("dw_ReadPosterior(): Must specify FILE pointer, filename or tag"); |
| 526 |
return (TMatrix)NULL; |
| 527 |
} |
| 528 |
else |
| 529 |
{ |
| 530 |
sprintf(filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 531 |
free_filename=1; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
// Get matrix |
| 536 |
if ((Y=dw_ReadDelimitedFile_floating(f_in,filename,delimiter,REMOVE_EMPTY_FIELDS,0.0))) |
| 537 |
{ |
| 538 |
for (k=dw_DimA(Y)-1; k >= 0; k--) |
| 539 |
if (dw_DimA(Y[k]) == cols) m++; |
| 540 |
if ((m > 0) && (posterior_draws=CreateMatrix(m,cols))) |
| 541 |
{ |
| 542 |
for (k=i=0; k < dw_DimA(Y); k++) |
| 543 |
if (dw_DimA(Y[k]) == cols) |
| 544 |
{ |
| 545 |
for (j=cols-1; j >= 0; j--) ElementM(posterior_draws,i,j)=Y[k][j]; |
| 546 |
i++; |
| 547 |
} |
| 548 |
} |
| 549 |
dw_FreeArray(Y); |
| 550 |
} |
| 551 |
|
| 552 |
// Clean up |
| 553 |
if (free_filename) dw_free(filename); |
| 554 |
|
| 555 |
// Return |
| 556 |
return posterior_draws; |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
The file contains (2 + ntheta + nq) columns which are to be |
| 561 |
interpreted as |
| 562 |
|
| 563 |
Column 0 : 0 = log posterior density |
| 564 |
Column 1 : 1 = log likelihood |
| 565 |
Columns 2 : 1 + ntheta + nq = parameters |
| 566 |
|
| 567 |
The file is a delimited ascii file and the delimiter is a space (' '). |
| 568 |
|
| 569 |
Attempts to read parameters from file, and upon success sets the parameters in |
| 570 |
the model and returns 1. Returns 0 upon failure. |
| 571 |
*/ |
| 572 |
int dw_ReadPosteriorDraw(FILE *f_in, TStateModel *model) |
| 573 |
{ |
| 574 |
int i, rtrn=0, terminal=dw_SetTerminalErrors(0), verbose=dw_SetVerboseErrors(0), |
| 575 |
nt=NumberFreeParametersTheta(model), nq=NumberFreeParametersQ(model); |
| 576 |
PRECISION *x; |
| 577 |
char **v; |
| 578 |
if (f_in) |
| 579 |
if ((v=dw_ReadDelimitedLine(f_in,' ',REMOVE_EMPTY_FIELDS))) |
| 580 |
{ |
| 581 |
if (dw_DimA(v) == 2+nt+nq) |
| 582 |
if ((x=(PRECISION*)dw_malloc((nt+nq)*sizeof(PRECISION)))) |
| 583 |
{ |
| 584 |
for (i=nt+nq+1; i >= 2; i--) |
| 585 |
if (dw_IsFloat(v[i])) |
| 586 |
x[i-2]=atof(v[i]); |
| 587 |
else |
| 588 |
break; |
| 589 |
if (i < 2) |
| 590 |
{ |
| 591 |
ConvertFreeParametersToTheta(model,x); |
| 592 |
ConvertFreeParametersToQ(model,x+nt); |
| 593 |
rtrn=1; |
| 594 |
} |
| 595 |
dw_free(x); |
| 596 |
} |
| 597 |
dw_FreeArray(v); |
| 598 |
} |
| 599 |
dw_SetTerminalErrors(terminal); |
| 600 |
dw_SetVerboseErrors(verbose); |
| 601 |
return rtrn; |
| 602 |
} |
| 603 |
|
| 604 |
/*******************************************************************************/ |
| 605 |
/******************************** Metropolis IO ********************************/ |
| 606 |
/*******************************************************************************/ |
| 607 |
int Read_metropolis_theta(FILE *f, char *filename, char *header, TMetropolis_theta *metropolis) |
| 608 |
{ |
| 609 |
int *dims, err, i, j, n, m; |
| 610 |
char *format; |
| 611 |
TMatrix X; |
| 612 |
FILE *f_in; |
| 613 |
if ((f_in=f ? f : filename ? fopen(filename,"rt") : (FILE*)NULL)) |
| 614 |
{ |
| 615 |
if (!header) header=""; |
| 616 |
format="//== %sNumber blocks/dimensions theta ==//"; |
| 617 |
if ((err=ReadInteger(f_in,format,header,&n))) |
| 618 |
ReadError(format,header,err); |
| 619 |
else |
| 620 |
if (n <= 0) |
| 621 |
ReadError(format,header,err=SWITCHIO_ERROR_READING_DATA); |
| 622 |
else |
| 623 |
{ |
| 624 |
if (!dw_ReadArray(f_in,dims=dw_CreateArray_int(n))) |
| 625 |
ReadError(format,header,err=SWITCHIO_ERROR_READING_DATA); |
| 626 |
else |
| 627 |
{ |
| 628 |
for (m=metropolis->n_parameters, i=n-1; i >= 0; i--) |
| 629 |
if (dims[i] <= 0) |
| 630 |
break; |
| 631 |
else |
| 632 |
m-=dims[i]; |
| 633 |
if ((i >= 0) || (m != 0)) |
| 634 |
ReadError(format,header,err=SWITCHIO_ERROR_READING_DATA); |
| 635 |
else |
| 636 |
{ |
| 637 |
format="//== %sMetropolis scales theta ==//"; |
| 638 |
if ((err=ReadVector(f_in,format,header,metropolis->scale))) |
| 639 |
ReadError(format,header,err); |
| 640 |
else |
| 641 |
{ |
| 642 |
for (i=0; i < metropolis->n_parameters; i++) |
| 643 |
if (metropolis->direction[i]) |
| 644 |
{ |
| 645 |
FreeVector(metropolis->direction[i]); |
| 646 |
metropolis->direction[i]=(TVector)NULL; |
| 647 |
} |
| 648 |
format="//== %sMetropolis directions theta ==//"; |
| 649 |
if ((err=ReadMatrix(f_in,format,header,X=CreateMatrix(metropolis->n_parameters,metropolis->n_parameters)))) |
| 650 |
ReadError(format,header,err=SWITCHIO_LINE_ID_NOT_FOUND); |
| 651 |
else |
| 652 |
{ |
| 653 |
for (i=0; i < metropolis->n_parameters; i++) |
| 654 |
{ |
| 655 |
for (j=0; j < metropolis->n_parameters; j++) |
| 656 |
{ |
| 657 |
if ((i == j) && (ElementM(X,i,j) != 1.0)) break; |
| 658 |
if ((i != j) && (ElementM(X,i,j) != 0.0)) break; |
| 659 |
} |
| 660 |
if (j < metropolis->n_parameters) |
| 661 |
metropolis->direction[i]=ColumnVector(metropolis->direction[i],X,i); |
| 662 |
} |
| 663 |
Setup_metropolis_theta_blocks(metropolis,n,dims); |
| 664 |
} |
| 665 |
FreeMatrix(X); |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
dw_FreeArray(dims); |
| 670 |
} |
| 671 |
if (!f) fclose(f_in); |
| 672 |
return err ? 0 : 1; |
| 673 |
} |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
int Write_metropolis_theta(FILE *f, char *filename, char *header, TMetropolis_theta *metropolis) |
| 678 |
{ |
| 679 |
int i; |
| 680 |
TMatrix X; |
| 681 |
FILE *f_out; |
| 682 |
if ((f_out=f ? f : filename ? fopen(filename,"at") : (FILE*)NULL)) |
| 683 |
{ |
| 684 |
if (!header) header=""; |
| 685 |
|
| 686 |
fprintf(f_out,"//== %sNumber blocks/dimensions theta ==//\n%d\n",header,metropolis->n_blocks); |
| 687 |
for (i=0; i < metropolis->n_blocks; i++) |
| 688 |
fprintf(f_out,"%d ",metropolis->block_dims[i]); |
| 689 |
fprintf(f_out,"\n\n"); |
| 690 |
|
| 691 |
fprintf(f_out,"//== %sMetropolis scales theta ==//\n",header); |
| 692 |
dw_PrintVector(f_out,metropolis->scale,"%.14le "); |
| 693 |
fprintf(f_out,"\n"); |
| 694 |
|
| 695 |
X=IdentityMatrix((TMatrix)NULL,metropolis->n_parameters); |
| 696 |
for (i=0; i < metropolis->n_parameters; i++) |
| 697 |
if (metropolis->direction[i]) InsertColumnVector(X,metropolis->direction[i],i); |
| 698 |
fprintf(f_out,"//== %sMetropolis directions theta ==//\n",header); |
| 699 |
dw_PrintMatrix(f_out,X,"%.14lf "); |
| 700 |
fprintf(f_out,"\n"); |
| 701 |
FreeMatrix(X); |
| 702 |
|
| 703 |
if (!f) fclose(f_out); |
| 704 |
return 1; |
| 705 |
} |
| 706 |
return 0; |
| 707 |
} |
| 708 |
/*******************************************************************************/ |
| 709 |
/*******************************************************************************/ |
| 710 |
/*******************************************************************************/ |
| 711 |
|
| 712 |
/*******************************************************************************/ |
| 713 |
/***************** TMarkovStateVariable Input/Output Routines ******************/ |
| 714 |
/*******************************************************************************/ |
| 715 |
/* |
| 716 |
Assumes: |
| 717 |
f : pointer to valid FILE structure |
| 718 |
idstring : pointer to null terminated string |
| 719 |
|
| 720 |
Returns: |
| 721 |
Valid pointer to a TMarkovStateVariable structure upon success and null |
| 722 |
pointer upon failure. |
| 723 |
|
| 724 |
Results: |
| 725 |
Reads Markov specification from file and creates TMarkovStateVariable |
| 726 |
structure. |
| 727 |
|
| 728 |
Notes: |
| 729 |
The null terminated string idstring is of the form "" or "[i][j]...[k]". |
| 730 |
Usually, this function will be called with idstring equal to "". The routine |
| 731 |
is called recursively with the more general form for idstring. For each path |
| 732 |
of Markov state variables of the form |
| 733 |
|
| 734 |
sv->state_variable[i]->state_variable[j]-> ... ->state_variable[k] |
| 735 |
|
| 736 |
which corresponds to a single Markov state variable, there must be an entries |
| 737 |
in the file of the form: |
| 738 |
*/ |
| 739 |
TMarkovStateVariable* sv_ReadMarkovSpecification(FILE *f_in, char *idstring) |
| 740 |
{ |
| 741 |
char *idformat, *trailer, *fmt, *idstring_new; |
| 742 |
int err, i, j, n_state_variables, nstates, nlags_encoded, nbasestates, nfree; |
| 743 |
TMarkovStateVariable *sv=(TMarkovStateVariable*)NULL, **sv_array; |
| 744 |
void *info; |
| 745 |
|
| 746 |
if (!f_in) return (TMarkovStateVariable*)NULL; |
| 747 |
|
| 748 |
// Construct trailer |
| 749 |
if (idstring[0]) |
| 750 |
{ |
| 751 |
fmt="for state_variable%s ==//"; |
| 752 |
sprintf(trailer=(char*)dw_malloc(strlen(fmt)+strlen(idstring)-1),fmt,idstring); |
| 753 |
} |
| 754 |
else |
| 755 |
{ |
| 756 |
fmt="==//"; |
| 757 |
strcpy(trailer=(char*)dw_malloc(strlen(fmt)+1),fmt); |
| 758 |
} |
| 759 |
|
| 760 |
// Read number of state variables |
| 761 |
if (!(err=ReadInteger(f_in,idformat="//== Number independent state variables %s",trailer,&n_state_variables))) |
| 762 |
{ |
| 763 |
if (n_state_variables < 0) |
| 764 |
err=SWITCHIO_ERROR_READING_DATA; |
| 765 |
else |
| 766 |
if (n_state_variables == 0) |
| 767 |
{ |
| 768 |
// Read number states |
| 769 |
switch (err=ReadInteger(f_in,idformat="//== Number states %s",trailer,&nstates)) |
| 770 |
{ |
| 771 |
case SWITCHIO_ERROR_READING_DATA: |
| 772 |
break; |
| 773 |
case SWITCHIO_LINE_ID_NOT_FOUND: // allows nstates to be computed from nlags_encoded and nbasestates. |
| 774 |
nstates=0; |
| 775 |
err=0; |
| 776 |
break; |
| 777 |
case 0: |
| 778 |
if (nstates <= 0) err=SWITCHIO_ERROR_READING_DATA; |
| 779 |
break; |
| 780 |
} |
| 781 |
|
| 782 |
// Read number of lags to encode |
| 783 |
if (!err) |
| 784 |
switch (err=ReadInteger(f_in,idformat="//== Number of lags encoded %s",trailer,&nlags_encoded)) |
| 785 |
{ |
| 786 |
case SWITCHIO_ERROR_READING_DATA: |
| 787 |
break; |
| 788 |
case SWITCHIO_LINE_ID_NOT_FOUND: |
| 789 |
nlags_encoded=0; |
| 790 |
err=0; |
| 791 |
break; |
| 792 |
case 0: |
| 793 |
if (nlags_encoded < 0) err=SWITCHIO_ERROR_READING_DATA; |
| 794 |
break; |
| 795 |
} |
| 796 |
|
| 797 |
// Read number of base states |
| 798 |
if (!err) |
| 799 |
switch(err=ReadInteger(f_in,idformat="//== Number of base states %s",trailer,&nbasestates)) |
| 800 |
{ |
| 801 |
case SWITCHIO_ERROR_READING_DATA: |
| 802 |
break; |
| 803 |
case SWITCHIO_LINE_ID_NOT_FOUND: |
| 804 |
if ((nlags_encoded == 0) && (nstates > 0)) |
| 805 |
{ |
| 806 |
nbasestates=nstates; |
| 807 |
err=0; |
| 808 |
} |
| 809 |
else |
| 810 |
err=SWITCHIO_ERROR_READING_DATA; |
| 811 |
break; |
| 812 |
case 0: |
| 813 |
if (nbasestates <= 0) |
| 814 |
err=SWITCHIO_ERROR_READING_DATA; |
| 815 |
else |
| 816 |
{ |
| 817 |
// Check consistency of nlags_encoded, nstates, and nbasestates |
| 818 |
for (j=nbasestates, i=nlags_encoded; i > 0; i--) j*=nbasestates; |
| 819 |
if (nstates == 0) |
| 820 |
nstates=j; |
| 821 |
else |
| 822 |
if (j != nstates) |
| 823 |
err=SWITCHIO_ERROR_READING_DATA; |
| 824 |
} |
| 825 |
break; |
| 826 |
} |
| 827 |
|
| 828 |
// Setup single restricted Markov state variable |
| 829 |
if (!err) |
| 830 |
{ |
| 831 |
if ((info=ReadSpecification_dirichlet_restrictions(f_in,trailer,nbasestates))) |
| 832 |
{ |
| 833 |
nfree=((TDirichletRestrictions*)(info))->total_Dirichlet_parameters |
| 834 |
- dw_DimA(((TDirichletRestrictions*)(info))->DirichletDim); |
| 835 |
sv=CreateMarkovStateVariable_Single(nbasestates,nfree,nlags_encoded,dirichlet_restrictions_CreateQRoutines(),info); |
| 836 |
} |
| 837 |
else |
| 838 |
err=SWITCHIO_ERROR_READING_DATA; |
| 839 |
} |
| 840 |
} |
| 841 |
else |
| 842 |
{ |
| 843 |
// Read number of lags to encode |
| 844 |
switch (err=ReadInteger(f_in,idformat="//== Number of lags encoded %s",trailer,&nlags_encoded)) |
| 845 |
{ |
| 846 |
case SWITCHIO_ERROR_READING_DATA: |
| 847 |
break; |
| 848 |
case SWITCHIO_LINE_ID_NOT_FOUND: |
| 849 |
nlags_encoded=0; |
| 850 |
err=0; |
| 851 |
break; |
| 852 |
case 0: |
| 853 |
if (nlags_encoded < 0) err=SWITCHIO_ERROR_READING_DATA; |
| 854 |
break; |
| 855 |
} |
| 856 |
|
| 857 |
// Setup multiple Markov state variables |
| 858 |
if (!err) |
| 859 |
{ |
| 860 |
sv_array=(TMarkovStateVariable**)dw_CreateArray_pointer(n_state_variables,(void (*)(void*))FreeMarkovStateVariable); |
| 861 |
for (i=0; i < n_state_variables; i++) |
| 862 |
{ |
| 863 |
idstring_new=CreateIntegerString("%s[%d]",idstring,i+1); |
| 864 |
sv_array[i]=sv_ReadMarkovSpecification(f_in,idstring_new); |
| 865 |
dw_free(idstring_new); |
| 866 |
if (!sv_array[i]) |
| 867 |
{ |
| 868 |
dw_FreeArray(sv_array); |
| 869 |
break; |
| 870 |
} |
| 871 |
} |
| 872 |
if ((i != n_state_variables) || !(sv=CreateMarkovStateVariable_Multiple(nlags_encoded,sv_array))) |
| 873 |
err=SWITCHIO_ERROR_READING_DATA; |
| 874 |
} |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
if (err) ReadError(idformat,trailer,err); |
| 879 |
if (trailer) dw_free(trailer); |
| 880 |
return sv; |
| 881 |
} |
| 882 |
|
| 883 |
/* |
| 884 |
Assumes: |
| 885 |
f : pointer to valid FILE structure |
| 886 |
idstring : pointer to null terminated string |
| 887 |
|
| 888 |
Returns: |
| 889 |
One upon success and zero upon upon failure. (This routine cannot fail). |
| 890 |
|
| 891 |
Results: |
| 892 |
Writes Markov specification to file. |
| 893 |
|
| 894 |
Notes: |
| 895 |
The null terminated string idstring is of the form "" or "[i][j]...[k]". |
| 896 |
Usually, this function will be called with idstring equal to "". The routine |
| 897 |
is called recursively with the more general form for idstring. For each path |
| 898 |
of Markov state variables of the form |
| 899 |
|
| 900 |
sv->state_variable[i]->state_variable[j]-> ... ->state_variable[k] |
| 901 |
|
| 902 |
a specification is written. See sv_ReadMarkovSpecification() for details. |
| 903 |
|
| 904 |
*/ |
| 905 |
int sv_WriteMarkovSpecification(FILE *f_out, TMarkovStateVariable *sv, char *idstring) |
| 906 |
{ |
| 907 |
int i; |
| 908 |
char *trailer, *idbuffer, *fmt; |
| 909 |
|
| 910 |
if (!f_out) return 0; |
| 911 |
|
| 912 |
if (idstring[0]) |
| 913 |
{ |
| 914 |
fmt="for state_variable%s ==//"; |
| 915 |
sprintf(trailer=(char*)dw_malloc(strlen(fmt)+strlen(idstring)-1),fmt,idstring); |
| 916 |
|
| 917 |
fprintf(f_out,"//********************** Specification for state_variable%s **********************//\n\n",idstring); |
| 918 |
} |
| 919 |
else |
| 920 |
{ |
| 921 |
fmt="==//"; |
| 922 |
strcpy(trailer=(char*)dw_malloc(strlen(fmt)+1),fmt); |
| 923 |
|
| 924 |
fprintf(f_out,"//*********************** State Variable Specification ************************//\n"); |
| 925 |
} |
| 926 |
|
| 927 |
fprintf(f_out,"//== Number independent state variables %s\n%d\n",trailer,sv->n_state_variables); |
| 928 |
|
| 929 |
if (sv->n_state_variables > 0) |
| 930 |
{ |
| 931 |
if (sv->nlags_encoded > 0) |
| 932 |
fprintf(f_out,"\n//== Number of lags encoded %s\n%d\n",trailer,sv->nlags_encoded); |
| 933 |
|
| 934 |
fprintf(f_out,"//*****************************************************************************//\n\n"); |
| 935 |
for (i=0; i < sv->n_state_variables; i++) |
| 936 |
{ |
| 937 |
idbuffer=CreateIntegerString("%s[%d]",idstring,i+1); |
| 938 |
if (!sv_WriteMarkovSpecification(f_out,sv->state_variable[i],idbuffer)) |
| 939 |
{ |
| 940 |
dw_free(idbuffer); |
| 941 |
return 0; |
| 942 |
} |
| 943 |
dw_free(idbuffer); |
| 944 |
} |
| 945 |
} |
| 946 |
else |
| 947 |
{ |
| 948 |
fprintf(f_out,"\n//== Number states %s\n%d\n\n",trailer,sv->nstates); |
| 949 |
|
| 950 |
fprintf(f_out,"//== Number of lags encoded %s\n%d\n\n",trailer,sv->nlags_encoded); |
| 951 |
|
| 952 |
fprintf(f_out,"//== Number of base states %s\n%d\n\n",trailer,sv->nbasestates); |
| 953 |
|
| 954 |
if (sv->routines->WriteSpecification) |
| 955 |
sv->routines->WriteSpecification(f_out,sv,trailer); |
| 956 |
|
| 957 |
fprintf(f_out,"//*****************************************************************************//\n\n"); |
| 958 |
} |
| 959 |
|
| 960 |
dw_free(trailer); |
| 961 |
return 1; |
| 962 |
} |
| 963 |
|
| 964 |
/* |
| 965 |
Assumes: |
| 966 |
f : pointer to valid FILE structure |
| 967 |
sv : pointer to valid TMarkovStateVariable structure |
| 968 |
header : pointer to null terminated string or null pointer |
| 969 |
|
| 970 |
Returns: |
| 971 |
One upon success and zero upon failure. |
| 972 |
|
| 973 |
Results: |
| 974 |
Reads transition matrix parameters from sv to a file. |
| 975 |
*/ |
| 976 |
int sv_ReadTransitionMatrixParameters(FILE *f_in, TMarkovStateVariable *sv, char *header) |
| 977 |
{ |
| 978 |
int i; |
| 979 |
|
| 980 |
if (sv->nfree == 0) return 1; |
| 981 |
|
| 982 |
if (!f_in) return 0; |
| 983 |
|
| 984 |
if (!header) header=""; |
| 985 |
if (SetFilePosition(f_in,"//== %sFree Transition Matrix Parameters ==//",header) == SWITCHIO_LINE_ID_NOT_FOUND) |
| 986 |
return 0; |
| 987 |
|
| 988 |
for (i=0; i < sv->nfree; i++) |
| 989 |
#if (PRECISION_SIZE == 8) |
| 990 |
if (fscanf(f_in," %lf ",sv->q+i) != 1) return 0; |
| 991 |
#else |
| 992 |
if (fscanf(f_in," %f ",sv->q+i) != 1) return 0; |
| 993 |
#endif |
| 994 |
|
| 995 |
return 1; |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
Assumes: |
| 1000 |
f : pointer to valid FILE structure |
| 1001 |
sv : pointer to valid TMarkovStateVariable structure |
| 1002 |
header : pointer to null terminated string or null pointer |
| 1003 |
|
| 1004 |
Returns: |
| 1005 |
One upon success and zero upon failure. |
| 1006 |
|
| 1007 |
Results: |
| 1008 |
Writes transition matrix parameters from sv to a file. |
| 1009 |
*/ |
| 1010 |
int sv_WriteTransitionMatrixParameters(FILE *f_out, TMarkovStateVariable *sv, char *header) |
| 1011 |
{ |
| 1012 |
int i; |
| 1013 |
|
| 1014 |
if (sv->nfree == 0) return 1; |
| 1015 |
|
| 1016 |
if (!f_out) return 0; |
| 1017 |
|
| 1018 |
if (!header) header=""; |
| 1019 |
fprintf(f_out,"//== %sFree Transition Matrix Parameters ==//\n",header); |
| 1020 |
for (i=0; i < sv->nfree; i++) |
| 1021 |
fprintf(f_out,"%22.14le ",sv->q[i]); |
| 1022 |
fprintf(f_out,"\n\n"); |
| 1023 |
|
| 1024 |
return 1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
/* |
| 1028 |
Assumes: |
| 1029 |
t : integer - time at which transition matrix is valid |
| 1030 |
f : pointer to valid FILE structure |
| 1031 |
header : pointer to null terminate string or null pointer |
| 1032 |
idstring : pointer to null terminated string |
| 1033 |
sv : pointer to valid TMarkovStateVariable structure |
| 1034 |
model : pointer to valit TStateModel structure |
| 1035 |
|
| 1036 |
Returns: |
| 1037 |
One upon success and zero upon failure. |
| 1038 |
|
| 1039 |
Results: |
| 1040 |
Reads base transition matrices from file into sv. All transition matrices and the |
| 1041 |
associated free parameters are set. |
| 1042 |
|
| 1043 |
Notes: |
| 1044 |
The null terminated string idstring is of the form "" or "[i][j]...[k]". |
| 1045 |
Usually, this function will be called with idstring equal to "". The routine |
| 1046 |
is called recursively with the more general form for idstring. For each path |
| 1047 |
of Markov state variables of the form |
| 1048 |
|
| 1049 |
sv->state_variable[i]->state_variable[j]-> ... ->state_variable[k] |
| 1050 |
|
| 1051 |
which corresponds to a single Markov state variable, there must be an entry |
| 1052 |
in the file of the form: |
| 1053 |
|
| 1054 |
//== <header>Transition matrix[i][j]...[k] ==// |
| 1055 |
x x ... x |
| 1056 |
x x ... x |
| 1057 |
. . . |
| 1058 |
x x ... x |
| 1059 |
|
| 1060 |
If sv itself is a single Markov state variable, then the format can be |
| 1061 |
|
| 1062 |
//== <header>Transition matrix[1] ==// |
| 1063 |
x x ... x |
| 1064 |
x x ... x |
| 1065 |
. . . |
| 1066 |
x x ... x |
| 1067 |
|
| 1068 |
or |
| 1069 |
|
| 1070 |
//== <header>Transition matrix ==// |
| 1071 |
x x ... x |
| 1072 |
x x ... x |
| 1073 |
. . . |
| 1074 |
x x ... x |
| 1075 |
|
| 1076 |
Here the term <header> is replaced with the null terminated string header. |
| 1077 |
Note that the spacing is important. |
| 1078 |
*/ |
| 1079 |
int sv_ReadBaseTransitionMatrices_t(int t, FILE *f_in, char *header, char *idstring, TMarkovStateVariable* sv, TStateModel *model) |
| 1080 |
{ |
| 1081 |
int i, j, err; |
| 1082 |
char *format, *buffer; |
| 1083 |
PRECISION sum; |
| 1084 |
|
| 1085 |
if (sv->n_state_variables > 0) |
| 1086 |
{ |
| 1087 |
format="%s[%d]"; |
| 1088 |
sv->t0=0; |
| 1089 |
sv->t1=INT_MAX; |
| 1090 |
for (i=0; i < sv->n_state_variables; i++) |
| 1091 |
{ |
| 1092 |
buffer=CreateIntegerString(format,idstring,i+1); |
| 1093 |
if (!sv_ReadBaseTransitionMatrices_t(t,f_in,header,buffer,sv->state_variable[i],model)) |
| 1094 |
{ |
| 1095 |
sv->t1=-1; |
| 1096 |
dw_free(buffer); |
| 1097 |
return 0; |
| 1098 |
} |
| 1099 |
else |
| 1100 |
{ |
| 1101 |
if (sv->t0 < sv->state_variable[i]->t0) sv->t0=sv->state_variable[i]->t0; |
| 1102 |
if (sv->t1 > sv->state_variable[i]->t1) sv->t1=sv->state_variable[i]->t1; |
| 1103 |
} |
| 1104 |
dw_free(buffer); |
| 1105 |
} |
| 1106 |
MatrixTensor(sv->baseQ,sv->QA); |
| 1107 |
if (sv->nlags_encoded > 0) ConvertBaseTransitionMatrix(sv->Q,sv->baseQ,sv->nlags_encoded); |
| 1108 |
return 1; |
| 1109 |
} |
| 1110 |
else |
| 1111 |
{ |
| 1112 |
// Read transition matrix |
| 1113 |
if (!header) header=""; |
| 1114 |
format="//== %sBase transition matrix%s ==//"; |
| 1115 |
sprintf(buffer=(char*)dw_malloc(strlen(header) + strlen(format) + strlen(idstring) - 3),format,header,idstring); |
| 1116 |
if ((err=ReadMatrix(f_in,(char*)NULL,buffer,sv->baseQ)) && !idstring[0]) |
| 1117 |
{ |
| 1118 |
dw_free(buffer); |
| 1119 |
sprintf(buffer=(char*)dw_malloc(strlen(header) + strlen(format) + strlen(idstring) - 3),format,header,"[1]"); |
| 1120 |
err=ReadMatrix(f_in,(char*)NULL,buffer,sv->baseQ); |
| 1121 |
} |
| 1122 |
dw_free(buffer); |
| 1123 |
if (!err) |
| 1124 |
{ |
| 1125 |
// Scale the columns of Q - loose requirement on sumation to one |
| 1126 |
for (j=sv->nbasestates-1; j >= 0; j--) |
| 1127 |
{ |
| 1128 |
for (sum=0.0, i=sv->nbasestates-1; i >= 0; i--) |
| 1129 |
if (ElementM(sv->baseQ,i,j) < 0.0) |
| 1130 |
{ |
| 1131 |
dw_UserError("Transition matrix can not have negative elements."); |
| 1132 |
sv->t1=-1; |
| 1133 |
return 0; |
| 1134 |
} |
| 1135 |
else |
| 1136 |
sum+=ElementM(sv->baseQ,i,j); |
| 1137 |
if (fabs(sum-1.0) > SQRT_MACHINE_EPSILON) |
| 1138 |
{ |
| 1139 |
dw_UserError("Transition matrix columns must sum to one."); |
| 1140 |
sv->t1=-1; |
| 1141 |
return 0; |
| 1142 |
} |
| 1143 |
for (sum=1.0/sum, i=sv->nstates-1; i >= 0; i--) |
| 1144 |
ElementM(sv->baseQ,i,j)*=sum; |
| 1145 |
} |
| 1146 |
|
| 1147 |
if (sv->nlags_encoded > 0) ConvertBaseTransitionMatrix(sv->Q,sv->baseQ,sv->nlags_encoded); |
| 1148 |
|
| 1149 |
// Update and set sv->t0 and sv->t1 |
| 1150 |
if (!sv->routines || !sv->routines->Update_q_from_BaseTransitionMatrix || !sv->routines->Update_q_from_BaseTransitionMatrix(t,sv,model)) |
| 1151 |
{ |
| 1152 |
dw_UserError("Unable to extract free parameters from transition matrix"); |
| 1153 |
sv->t1=-1; |
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
else |
| 1157 |
return 1; |
| 1158 |
} |
| 1159 |
else |
| 1160 |
{ |
| 1161 |
sv->t1=-1; |
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
/* |
| 1168 |
Assumes: |
| 1169 |
t : integer - time at which transition matrix is valid |
| 1170 |
f : pointer to valid FILE structure |
| 1171 |
sv : pointer to valid TMarkovStateVariable structure |
| 1172 |
model : pointer to valit TStateModel structure |
| 1173 |
|
| 1174 |
Returns: |
| 1175 |
One upon success and zero upon failure. |
| 1176 |
|
| 1177 |
Results: |
| 1178 |
Reads base transition matrices from file into sv. All transition matrices and the |
| 1179 |
associated free parameters are set. |
| 1180 |
*/ |
| 1181 |
int sv_ReadBaseTransitionMatricesFlat_t(int t, FILE *f_in, TMarkovStateVariable *sv, TStateModel *model) |
| 1182 |
{ |
| 1183 |
int i, j; |
| 1184 |
PRECISION sum; |
| 1185 |
|
| 1186 |
if (sv->n_state_variables > 0) |
| 1187 |
{ |
| 1188 |
sv->t0=0; |
| 1189 |
sv->t1=INT_MAX; |
| 1190 |
for (i=0; i < sv->n_state_variables; i++) |
| 1191 |
if (!sv_ReadBaseTransitionMatricesFlat_t(t,f_in,sv->state_variable[i],model)) |
| 1192 |
{ |
| 1193 |
sv->t1=-1; |
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
else |
| 1197 |
{ |
| 1198 |
if (sv->t0 < sv->state_variable[i]->t0) sv->t0=sv->state_variable[i]->t0; |
| 1199 |
if (sv->t1 > sv->state_variable[i]->t1) sv->t1=sv->state_variable[i]->t1; |
| 1200 |
} |
| 1201 |
MatrixTensor(sv->baseQ,sv->QA); |
| 1202 |
if (sv->nlags_encoded > 0) ConvertBaseTransitionMatrix(sv->Q,sv->baseQ,sv->nlags_encoded); |
| 1203 |
return 1; |
| 1204 |
} |
| 1205 |
else |
| 1206 |
{ |
| 1207 |
// Read transition matrix |
| 1208 |
for (j=0; j < sv->nbasestates; j++) |
| 1209 |
for (i=0; i < sv->nbasestates; i++) |
| 1210 |
if (fscanf(f_in," %lf ",&ElementM(sv->baseQ,i,j)) != 1) |
| 1211 |
{ |
| 1212 |
sv->t1=-1; |
| 1213 |
dw_UserError("Unable to read transition matrix."); |
| 1214 |
return 0; |
| 1215 |
} |
| 1216 |
|
| 1217 |
// Scale the columns of Q - loose requirement on sumation to one |
| 1218 |
for (j=sv->nbasestates-1; j >= 0; j--) |
| 1219 |
{ |
| 1220 |
for (sum=0.0, i=sv->nbasestates-1; i >= 0; i--) |
| 1221 |
if (ElementM(sv->baseQ,i,j) < 0.0) |
| 1222 |
{ |
| 1223 |
dw_UserError("Transition matrix can not have negative elements."); |
| 1224 |
sv->t1=-1; |
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
else |
| 1228 |
sum+=ElementM(sv->baseQ,i,j); |
| 1229 |
if (fabs(sum-1.0) > SQRT_MACHINE_EPSILON) |
| 1230 |
{ |
| 1231 |
dw_UserError("Transition matrix columns must sum to one."); |
| 1232 |
sv->t1=-1; |
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
for (sum=1.0/sum, i=sv->nstates-1; i >= 0; i--) |
| 1236 |
ElementM(sv->baseQ,i,j)*=sum; |
| 1237 |
} |
| 1238 |
|
| 1239 |
if (sv->nlags_encoded > 0) ConvertBaseTransitionMatrix(sv->Q,sv->baseQ,sv->nlags_encoded); |
| 1240 |
|
| 1241 |
// Update and set sv->t0 and sv->t1 |
| 1242 |
if (!sv->routines || !sv->routines->Update_q_from_BaseTransitionMatrix || !sv->routines->Update_q_from_BaseTransitionMatrix(t,sv,model)) |
| 1243 |
{ |
| 1244 |
dw_UserError("Unable to extract free parameters from transition matrix"); |
| 1245 |
sv->t1=-1; |
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
else |
| 1249 |
return 1; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
/* |
| 1254 |
Assumes: |
| 1255 |
f : pointer to valid FILE structure |
| 1256 |
sv : pointer to valid TMarkovStateVariable structure |
| 1257 |
header : pointer to null terminated string or null terminated string |
| 1258 |
idstring : pointer to null terminated string |
| 1259 |
|
| 1260 |
Returns: |
| 1261 |
One is always returned. |
| 1262 |
|
| 1263 |
Results: |
| 1264 |
Writes base transition matrices from sv to a file. |
| 1265 |
|
| 1266 |
Notes: |
| 1267 |
The null terminated string idstring is of the form "" or "[i][j]...[k]". |
| 1268 |
Usually, this routine will be called with idstring equal to "". The routine |
| 1269 |
is called recursively with the more general form for idstring. |
| 1270 |
*/ |
| 1271 |
int sv_WriteBaseTransitionMatrices(FILE *f_out, TMarkovStateVariable* sv, char *header, char *idstring) |
| 1272 |
{ |
| 1273 |
int i, rtrn=1; |
| 1274 |
char *idbuffer; |
| 1275 |
|
| 1276 |
if (sv->n_state_variables > 0) |
| 1277 |
for (i=0; i < sv->n_state_variables; i++) |
| 1278 |
{ |
| 1279 |
idbuffer=CreateIntegerString("%s[%d]",idstring,i+1); |
| 1280 |
rtrn=sv_WriteBaseTransitionMatrices(f_out,sv->state_variable[i],header,idbuffer); |
| 1281 |
dw_free(idbuffer); |
| 1282 |
if (!rtrn) break; |
| 1283 |
} |
| 1284 |
else |
| 1285 |
{ |
| 1286 |
if (!header) header=""; |
| 1287 |
fprintf(f_out,"//== %sBase transition matrix%s ==//\n",header,idstring); |
| 1288 |
rtrn=dw_PrintMatrix(f_out,sv->baseQ,"%.14le "); |
| 1289 |
fprintf(f_out,"\n"); |
| 1290 |
} |
| 1291 |
|
| 1292 |
return rtrn; |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* |
| 1296 |
Assumes: |
| 1297 |
f : pointer to valid FILE structure |
| 1298 |
sv : pointer to valid TMarkovStateVariable structure |
| 1299 |
|
| 1300 |
Returns: |
| 1301 |
One is always returned. |
| 1302 |
|
| 1303 |
Results: |
| 1304 |
Writes base transition matrices from sv to a file in a flat format. See |
| 1305 |
sv_WriteBaseTransitionMatricsFlat_headers() for the format. |
| 1306 |
|
| 1307 |
Notes: |
| 1308 |
A line feed is NOT written at the end of the transition matrix. |
| 1309 |
*/ |
| 1310 |
int sv_WriteBaseTransitionMatricesFlat(FILE *f_out, TMarkovStateVariable* sv, char *fmt) |
| 1311 |
{ |
| 1312 |
int i, n, rtrn=1; |
| 1313 |
PRECISION *p; |
| 1314 |
|
| 1315 |
if (sv->n_state_variables > 0) |
| 1316 |
for (i=0; i < sv->n_state_variables; i++) |
| 1317 |
{ |
| 1318 |
rtrn=sv_WriteBaseTransitionMatricesFlat(f_out,sv->state_variable[i],fmt); |
| 1319 |
if (!rtrn) break; |
| 1320 |
} |
| 1321 |
else |
| 1322 |
{ |
| 1323 |
if (!fmt) fmt="%.5lf "; |
| 1324 |
for (p=pElementM(sv->baseQ), n=RowM(sv->baseQ)*ColM(sv->baseQ), i=0; i < n; i++) |
| 1325 |
fprintf(f_out,fmt,p[i]); |
| 1326 |
} |
| 1327 |
|
| 1328 |
return rtrn; |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* |
| 1332 |
Assumes: |
| 1333 |
f: valid file pointer or null pointer |
| 1334 |
sv: pointer to valid TMarkovStateVariable structure |
| 1335 |
header: pointer to null terminated string |
| 1336 |
idstring: pointer to null terminated string |
| 1337 |
|
| 1338 |
Returns: |
| 1339 |
One is always returned. |
| 1340 |
|
| 1341 |
Results: |
| 1342 |
Writes base transition matrices from sv to a file. |
| 1343 |
|
| 1344 |
Notes: |
| 1345 |
The null terminated string idstring is of the form "" or "[i][j]...[k]". |
| 1346 |
Usually, this routine will be called with idstring equal to "". The routine |
| 1347 |
is called recursively with the more general form for idstring. |
| 1348 |
|
| 1349 |
The matrices are written column by column. |
| 1350 |
*/ |
| 1351 |
int sv_WriteBaseTransitionMatricesFlat_Headers(FILE *f_out, TMarkovStateVariable* sv, char *idstring) |
| 1352 |
{ |
| 1353 |
int i, j; |
| 1354 |
char *idbuffer; |
| 1355 |
|
| 1356 |
if (sv->n_state_variables > 0) |
| 1357 |
{ |
| 1358 |
for (j=10, i=1; sv->n_state_variables >= j; j*=10, i++); |
| 1359 |
strcpy(idbuffer=(char*)dw_malloc((j=(int)strlen(idstring))+i+3),idstring); |
| 1360 |
for (i=0; i < sv->n_state_variables; i++) |
| 1361 |
{ |
| 1362 |
sprintf(idbuffer+j,"[%d]",i+1); |
| 1363 |
sv_WriteBaseTransitionMatricesFlat_Headers(f_out,sv->state_variable[i],idbuffer); |
| 1364 |
} |
| 1365 |
dw_free(idbuffer); |
| 1366 |
} |
| 1367 |
else |
| 1368 |
{ |
| 1369 |
for (j=0; j < sv->nbasestates; j++) |
| 1370 |
for (i=0; i < sv->nbasestates; i++) |
| 1371 |
fprintf(f_out,"Q%s(%d,%d) ",idstring,i+1,j+1); |
| 1372 |
} |
| 1373 |
|
| 1374 |
return 1; |
| 1375 |
} |
| 1376 |
|
| 1377 |
/*******************************************************************************/ |
| 1378 |
/*******************************************************************************/ |
| 1379 |
/*******************************************************************************/ |
| 1380 |
|
| 1381 |
/*******************************************************************************/ |
| 1382 |
/*********** Creating TMarkovStateVariable with simple restrictions ************/ |
| 1383 |
/*******************************************************************************/ |
| 1384 |
/* |
| 1385 |
//== Markov State Variables with Simple Restrictions ==// |
| 1386 |
|
| 1387 |
//-----------------------------------------------------------------------------// |
| 1388 |
//-- If n_state_variables is one, then there must be entries for --// |
| 1389 |
//-- state_variable[1] and all others are ignored. --// |
| 1390 |
//-----------------------------------------------------------------------------// |
| 1391 |
//== Number independent state variables ==// |
| 1392 |
n_state_variables |
| 1393 |
|
| 1394 |
//-----------------------------------------------------------------------------// |
| 1395 |
//-- state_variable[i][j]...[j] --// |
| 1396 |
//-----------------------------------------------------------------------------// |
| 1397 |
|
| 1398 |
//-----------------------------------------------------------------------------// |
| 1399 |
//-- If this identifier is not present, the number of independent state --// |
| 1400 |
//-- variables is set to one. --// |
| 1401 |
//-----------------------------------------------------------------------------// |
| 1402 |
//== Number independent state variables for state_variable[i][j]...[k] ==// |
| 1403 |
n_state_variables |
| 1404 |
|
| 1405 |
//-----------------------------------------------------------------------------// |
| 1406 |
//-- The following fields must be present if n_state_variable is 1 and are --// |
| 1407 |
//-- ignored otherwise. --// |
| 1408 |
//-----------------------------------------------------------------------------// |
| 1409 |
|
| 1410 |
//== Number of states for state_variable[i][j]...[k] ==// |
| 1411 |
n_states |
| 1412 |
|
| 1413 |
//-----------------------------------------------------------------------------// |
| 1414 |
//-- Allows for lagged values of the state variable to be encoded. If this --// |
| 1415 |
//-- identifier is not present, then the value of nlags_encoded is set to --// |
| 1416 |
//-- zero. --// |
| 1417 |
//-----------------------------------------------------------------------------// |
| 1418 |
//== Number of lags encoded for state_variable[i][j]...[k] ==// |
| 1419 |
nlags_encoded |
| 1420 |
|
| 1421 |
//-----------------------------------------------------------------------------// |
| 1422 |
//-- Each column contains the parameters for a Dirichlet prior on the --// |
| 1423 |
//-- corresponding column of the transition matrix. Each element must be --// |
| 1424 |
//-- positive. For each column, the relative size of the prior elements --// |
| 1425 |
//-- determine the relative size of the elements of the transition matrix --// |
| 1426 |
//-- and overall larger sizes implies a tighter prior. The prior must be --// |
| 1427 |
//-- nstates by nstates. |
| 1428 |
//-----------------------------------------------------------------------------// |
| 1429 |
//== Transition matrix prior for state_variable[i][j]...[k] ==// |
| 1430 |
prior |
| 1431 |
//-----------------------------------------------------------------------------// |
| 1432 |
//-- Dimension of the Dirichlet variable controlling each of the columns of --// |
| 1433 |
//-- transition matrix. --// |
| 1434 |
//-----------------------------------------------------------------------------// |
| 1435 |
//== Free Dirichet dimensions for state_variable[i][j]...[k] ==// |
| 1436 |
free[0] ... free[n_states - 1] |
| 1437 |
|
| 1438 |
//-----------------------------------------------------------------------------// |
| 1439 |
//-- The jth restriction matrix is n_states x free[j]. Each row of the --// |
| 1440 |
//-- restriction matrix has exactly one non-zero entry and the sum of each --// |
| 1441 |
//-- column of the restriction matrix must be one. If entry (i,k) of the --// |
| 1442 |
//-- jth restriction matrix is non-zero, then entry (i,j) in the transition --// |
| 1443 |
//-- matrix is controlled by the kth element of jth free Dirichlet random --// |
| 1444 |
//-- variable --// |
| 1445 |
//-----------------------------------------------------------------------------// |
| 1446 |
//== Column restrictions for state_variable[i][j]...[k] ==// |
| 1447 |
restriction[0] |
| 1448 |
. |
| 1449 |
. |
| 1450 |
. |
| 1451 |
restriction[n_states - 1] |
| 1452 |
*/ |
| 1453 |
TMarkovStateVariable* CreateMarkovStateVariable_Simple(FILE *f_in, char *idstring) |
| 1454 |
{ |
| 1455 |
int nlags, n_state_variables, nstates, i, j, err=0; |
| 1456 |
char *format, *old_format, *idstring_new, *trailer=(char*)NULL; |
| 1457 |
TMatrix prior=(TMatrix)NULL; |
| 1458 |
int *dims=(int*)NULL; |
| 1459 |
TMatrix* restrictions=(TMatrix*)NULL; |
| 1460 |
TMarkovStateVariable **sv_array=(TMarkovStateVariable**)NULL, *sv=(TMarkovStateVariable*)NULL; |
| 1461 |
|
| 1462 |
// Check file pointer |
| 1463 |
if (!f_in) |
| 1464 |
{ |
| 1465 |
dw_UserError("CreateMarkovStateVariable_Simple(): null file pointer\n"); |
| 1466 |
return (TMarkovStateVariable*)NULL; |
| 1467 |
} |
| 1468 |
|
| 1469 |
// Construct trailer |
| 1470 |
if (idstring[0]) |
| 1471 |
sprintf(trailer=(char*)dw_malloc(24+strlen(idstring)),"for state_variable%s ==//",idstring); |
| 1472 |
else |
| 1473 |
strcpy(trailer=(char*)dw_malloc(5),"==//"); |
| 1474 |
|
| 1475 |
// Read number of Markov state variables |
| 1476 |
if ((err=ReadInteger(f_in,format="//== Number independent state variables %s",trailer,&n_state_variables) == SWITCHIO_LINE_ID_NOT_FOUND)) |
| 1477 |
if (!(err=ReadInteger(f_in,format="//== Number Independent State Variables %s",trailer,&n_state_variables))) |
| 1478 |
printf("Warning CreateMarkovStateVariable_Simple(): Using old file format for Number independent state variables\n"); |
| 1479 |
if (err == SWITCHIO_LINE_ID_NOT_FOUND) |
| 1480 |
n_state_variables=1; |
| 1481 |
else |
| 1482 |
if (err == SWITCHIO_ERROR_READING_DATA) |
| 1483 |
goto CLEAN_UP; |
| 1484 |
else |
| 1485 |
if (n_state_variables <= 0) |
| 1486 |
{ |
| 1487 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1488 |
goto CLEAN_UP; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (n_state_variables > 1) |
| 1492 |
{ |
| 1493 |
dw_free(trailer); |
| 1494 |
sv_array=(TMarkovStateVariable**)dw_CreateArray_pointer(n_state_variables,(void (*)(void*))FreeMarkovStateVariable); |
| 1495 |
for (i=0; i < n_state_variables; i++) |
| 1496 |
{ |
| 1497 |
sv_array[i]=CreateMarkovStateVariable_Simple(f_in,idstring_new=CreateIntegerString("%s[%d]",idstring,i+1)); |
| 1498 |
dw_free(idstring_new); |
| 1499 |
if (!sv_array[i]) break; |
| 1500 |
} |
| 1501 |
if (i != n_state_variables) |
| 1502 |
dw_FreeArray(sv_array); |
| 1503 |
else |
| 1504 |
if (!(sv=CreateMarkovStateVariable_Multiple(0,sv_array))) |
| 1505 |
{ |
| 1506 |
dw_FreeArray(sv_array); |
| 1507 |
dw_UserError("Unexpected error: unable to create multiple TMarkovStateVariable structure\n"); |
| 1508 |
} |
| 1509 |
return sv; |
| 1510 |
} |
| 1511 |
|
| 1512 |
//===== Single Markov state variable =====// |
| 1513 |
// Reset trailer if necessary |
| 1514 |
if (!idstring[0]) |
| 1515 |
{ |
| 1516 |
dw_free(trailer); |
| 1517 |
memcpy(trailer=(char*)dw_malloc(27),"for state_variable[1] ==//",27); |
| 1518 |
} |
| 1519 |
|
| 1520 |
// Read number of states |
| 1521 |
if ((err=ReadInteger(f_in,format="//== Number of states %s",trailer,&nstates))) goto CLEAN_UP; |
| 1522 |
if (nstates <= 0) |
| 1523 |
{ |
| 1524 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1525 |
goto CLEAN_UP; |
| 1526 |
} |
| 1527 |
|
| 1528 |
// Read number of lags |
| 1529 |
if ((err=ReadInteger(f_in,format="//== Number of lags encoded %s",trailer,&nlags)) == SWITCHIO_LINE_ID_NOT_FOUND) |
| 1530 |
{ |
| 1531 |
err=0; |
| 1532 |
nlags=0; |
| 1533 |
} |
| 1534 |
else |
| 1535 |
if (err == SWITCHIO_ERROR_READING_DATA) |
| 1536 |
goto CLEAN_UP; |
| 1537 |
else |
| 1538 |
if (nlags < 0) |
| 1539 |
{ |
| 1540 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1541 |
goto CLEAN_UP; |
| 1542 |
} |
| 1543 |
|
| 1544 |
//===== TDirichletRestrictions data =====// |
| 1545 |
// Read prior |
| 1546 |
if ((err=ReadMatrix(f_in,format="//== Transition matrix prior %s",trailer,prior=CreateMatrix(nstates,nstates)))) |
| 1547 |
{ |
| 1548 |
// Old format |
| 1549 |
if (prior) FreeMatrix(prior); |
| 1550 |
if (strlen(trailer) >= 5) |
| 1551 |
trailer[strlen(trailer)-5]='\0'; |
| 1552 |
else |
| 1553 |
trailer[0]='\0'; |
| 1554 |
err=ReadMatrix(f_in,old_format="//== Transition matrix prior %s. (n_states x n_states) ==//",trailer,prior=CreateMatrix(nstates,nstates)); |
| 1555 |
if (trailer[0]) |
| 1556 |
trailer[strlen(trailer)]=' '; |
| 1557 |
else |
| 1558 |
trailer[0]='='; |
| 1559 |
if (err) |
| 1560 |
goto CLEAN_UP; |
| 1561 |
else |
| 1562 |
printf("Warning CreateMarkovStateVariable_Simple(): Using old format for Transition matrix prior.\n"); |
| 1563 |
} |
| 1564 |
|
| 1565 |
// Read dimension of Dirichlet random variable controlling each column |
| 1566 |
if ((err=ReadIntArray(f_in,format="//== Dirichlet dimensions %s",trailer,dims=dw_CreateArray_int(nstates)))) |
| 1567 |
{ |
| 1568 |
dw_FreeArray(dims); |
| 1569 |
if (strlen(trailer) >= 5) |
| 1570 |
trailer[strlen(trailer)-5]='\0'; |
| 1571 |
else |
| 1572 |
trailer[0]='\0'; |
| 1573 |
err=ReadIntArray(f_in,format="//== Free Dirichet dimensions %s ==//",trailer,dims=dw_CreateArray_int(nstates)); |
| 1574 |
if (!err) |
| 1575 |
{ |
| 1576 |
if (trailer[0]) |
| 1577 |
trailer[strlen(trailer)]=' '; |
| 1578 |
else |
| 1579 |
trailer[0]='='; |
| 1580 |
} |
| 1581 |
if (err) |
| 1582 |
goto CLEAN_UP; |
| 1583 |
else |
| 1584 |
printf("Warning CreateMarkovStateVariable_Simple(): Using old format for Dirichlet dimensions.\n"); |
| 1585 |
} |
| 1586 |
for (j=0; j < nstates; j++) |
| 1587 |
if (dims[j] <= 0) |
| 1588 |
{ |
| 1589 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1590 |
goto CLEAN_UP; |
| 1591 |
} |
| 1592 |
|
| 1593 |
// Read restrictions |
| 1594 |
if ((err=SetFilePosition(f_in,format="//== Column restrictions %s",trailer))) goto CLEAN_UP; |
| 1595 |
|
| 1596 |
restrictions=dw_CreateArray_matrix(nstates); |
| 1597 |
|
| 1598 |
for (j=0; j < nstates; j++) |
| 1599 |
if (!dw_ReadMatrix(f_in,restrictions[j]=CreateMatrix(nstates,dims[j]))) |
| 1600 |
{ |
| 1601 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1602 |
goto CLEAN_UP; |
| 1603 |
} |
| 1604 |
|
| 1605 |
// Create Markov state variable |
| 1606 |
if (!(sv=CreateMarkovStateVariable_SimpleRestrictions(nstates,nlags,prior,restrictions))) |
| 1607 |
dw_UserError("CreateMarkovStateVariable_Simple(): unable to create Markov state variable with simple restrictions\n"); |
| 1608 |
|
| 1609 |
CLEAN_UP: |
| 1610 |
|
| 1611 |
// Print error message if necessary |
| 1612 |
if (err) ReadError(format,trailer,err); |
| 1613 |
|
| 1614 |
// Free memory as necessary |
| 1615 |
if (restrictions) dw_FreeArray(restrictions); |
| 1616 |
if (dims) dw_FreeArray(dims); |
| 1617 |
if (prior) FreeMatrix(prior); |
| 1618 |
if (trailer) dw_free(trailer); |
| 1619 |
|
| 1620 |
// Return |
| 1621 |
return sv; |
| 1622 |
} |
| 1623 |
|
| 1624 |
TMarkovStateVariable* CreateMarkovStateVariable_File(FILE *f, char *filename) |
| 1625 |
{ |
| 1626 |
FILE *f_in; |
| 1627 |
char *str, *str_old; |
| 1628 |
int err; |
| 1629 |
TMarkovStateVariable *sv=(TMarkovStateVariable*)NULL; |
| 1630 |
|
| 1631 |
// Open file if necessary |
| 1632 |
if (f) |
| 1633 |
f_in=f; |
| 1634 |
else |
| 1635 |
if (!(f_in=fopen(filename,"rt"))) |
| 1636 |
{ |
| 1637 |
err=SWITCHIO_FILE_ERROR; |
| 1638 |
return (TMarkovStateVariable*)NULL; |
| 1639 |
} |
| 1640 |
|
| 1641 |
// Check for file type |
| 1642 |
if ((err=SetFilePosition(f_in,(char*)NULL,str="//== Markov State Variables with Simple Restrictions ==//"))) |
| 1643 |
{ |
| 1644 |
if ((err=SetFilePosition(f_in,(char*)NULL,str_old="//== Flat Independent Markov States and Simple Restrictions ==//"))) |
| 1645 |
ReadError((char*)NULL,str,err); |
| 1646 |
else |
| 1647 |
printf("Warning CreateMarkovStateVariable_File(): Old file format for file type.\n"); |
| 1648 |
} |
| 1649 |
sv=CreateMarkovStateVariable_Simple(f_in,""); |
| 1650 |
|
| 1651 |
// Close file if necessary |
| 1652 |
if (!f && f_in) fclose(f_in); |
| 1653 |
|
| 1654 |
// Return |
| 1655 |
return sv; |
| 1656 |
} |
| 1657 |
/*******************************************************************************/ |
| 1658 |
/*******************************************************************************/ |
| 1659 |
/*******************************************************************************/ |
| 1660 |
|
| 1661 |
|
| 1662 |
/*******************************************************************************/ |
| 1663 |
/************************** TDirichletRestrictions IO **************************/ |
| 1664 |
/*******************************************************************************/ |
| 1665 |
TDirichletRestrictions* ReadSpecification_dirichlet_restrictions(FILE *f_in, char *trailer, int nstates) |
| 1666 |
{ |
| 1667 |
TMatrix Prior, MQ; |
| 1668 |
int *FreeDim, **NonZeroIndex, i, err; |
| 1669 |
char *idformat; |
| 1670 |
TDirichletRestrictions *restrictions=(TDirichletRestrictions*)NULL; |
| 1671 |
|
| 1672 |
// Read prior |
| 1673 |
Prior=CreateMatrix(nstates,nstates); |
| 1674 |
if (!(err=ReadMatrix(f_in,idformat="//== Prior %s",trailer,Prior))) |
| 1675 |
{ |
| 1676 |
// Read free Restricted dimensions |
| 1677 |
if (!(err=ReadInteger(f_in,idformat="//== Number Dirichlet variables %s",trailer,&i))) |
| 1678 |
{ |
| 1679 |
FreeDim=dw_CreateArray_int(i); |
| 1680 |
if (!(err=ReadIntArray(f_in,idformat="//== Dirichlet dimensions %s",trailer,FreeDim))) |
| 1681 |
{ |
| 1682 |
// Read free Restricted index |
| 1683 |
NonZeroIndex=dw_CreateRectangularArray_int(nstates,nstates); |
| 1684 |
if (!(err=ReadIntArray(f_in,idformat="//== Dirichlet index (total) %s",trailer,NonZeroIndex))) |
| 1685 |
{ |
| 1686 |
// Read free Restricted multipliers |
| 1687 |
MQ=CreateMatrix(nstates,nstates); |
| 1688 |
if (!(err=ReadMatrix(f_in,idformat="//== Dirichlet multipliers %s",trailer,MQ))) |
| 1689 |
restrictions=CreateDirichletRestrictions(nstates,Prior,FreeDim,NonZeroIndex,MQ); |
| 1690 |
FreeMatrix(MQ); |
| 1691 |
} |
| 1692 |
dw_FreeArray(NonZeroIndex); |
| 1693 |
} |
| 1694 |
dw_FreeArray(FreeDim); |
| 1695 |
} |
| 1696 |
} |
| 1697 |
FreeMatrix(Prior); |
| 1698 |
return restrictions; |
| 1699 |
} |
| 1700 |
|
| 1701 |
int WriteSpecification_dirichlet_restrictions(FILE *f_out, TMarkovStateVariable *sv, char *trailer) |
| 1702 |
{ |
| 1703 |
TDirichletRestrictions *restricted=(TDirichletRestrictions*)(sv->info); |
| 1704 |
|
| 1705 |
fprintf(f_out,"//== Prior %s\n",trailer); |
| 1706 |
dw_PrintMatrix(f_out,restricted->Prior,"%22.14le "); |
| 1707 |
fprintf(f_out,"\n"); |
| 1708 |
|
| 1709 |
fprintf(f_out,"//== Number Dirichlet variables %s\n%d\n\n",trailer,dw_DimA(restricted->DirichletDim)); |
| 1710 |
|
| 1711 |
fprintf(f_out,"//== Dirichlet dimensions %s\n",trailer); |
| 1712 |
dw_PrintArray(f_out,restricted->DirichletDim,"%d "); |
| 1713 |
fprintf(f_out,"\n"); |
| 1714 |
|
| 1715 |
fprintf(f_out,"//== Dirichlet index (total) %s\n",trailer); |
| 1716 |
dw_PrintArray(f_out,restricted->NonZeroIndex,"%d "); |
| 1717 |
|
| 1718 |
fprintf(f_out,"//== Dirichlet multipliers %s\n",trailer); |
| 1719 |
dw_PrintMatrix(f_out,restricted->MQ,"%22.14le "); |
| 1720 |
|
| 1721 |
return 1; |
| 1722 |
} |
| 1723 |
|
| 1724 |
/*******************************************************************************/ |
| 1725 |
/*******************************************************************************/ |
| 1726 |
/*******************************************************************************/ |
| 1727 |
|
| 1728 |
/*******************************************************************************/ |
| 1729 |
/************************** Reading Tao's dsge model ***************************/ |
| 1730 |
/*******************************************************************************/ |
| 1731 |
/* |
| 1732 |
Assumes |
| 1733 |
j : An integer with 0 <= j < number of columns in states |
| 1734 |
states : Rectangular array of non-negative integers |
| 1735 |
|
| 1736 |
Results |
| 1737 |
Returns the number of regimes associated with the jth column of states. |
| 1738 |
|
| 1739 |
Notes: |
| 1740 |
The rectangular array states is a translation matrix. Each column of states |
| 1741 |
provides a translation from the Markov state variable with dw_DimA(states) |
| 1742 |
regimes to the Markov state variable with |
| 1743 |
*/ |
| 1744 |
int GetNumberStatesFromTranslationMatrix(int j, int **states) |
| 1745 |
{ |
| 1746 |
int i, n=0; |
| 1747 |
for (i=dw_DimA(states[j])-1; i >= 0; i--) |
| 1748 |
if (states[j][i] > n) n=states[j][i]; |
| 1749 |
return n+1; |
| 1750 |
} |
| 1751 |
|
| 1752 |
/* |
| 1753 |
Assumes |
| 1754 |
*/ |
| 1755 |
static int GetTotalNumberStateVariablesIncludingLags(TMarkovStateVariable *sv) |
| 1756 |
{ |
| 1757 |
int i, n=0; |
| 1758 |
if (sv->n_state_variables > 0) |
| 1759 |
for (i=sv->n_state_variables-1; i >= 0; i--) |
| 1760 |
n+=GetTotalNumberStateVariablesIncludingLags(sv->state_variable[i]); |
| 1761 |
else |
| 1762 |
n=sv->nlags_encoded+1; |
| 1763 |
return n; |
| 1764 |
} |
| 1765 |
|
| 1766 |
static void GetDimStateVariablesIncludingLags(int *tv_dims, TMarkovStateVariable *sv) |
| 1767 |
{ |
| 1768 |
int i; |
| 1769 |
if (sv->n_state_variables > 0) |
| 1770 |
for (i=0; i < sv->n_state_variables; tv_dims+=GetTotalNumberStateVariablesIncludingLags(sv->state_variable[i]), i++) |
| 1771 |
GetDimStateVariablesIncludingLags(tv_dims,sv->state_variable[i]); |
| 1772 |
else |
| 1773 |
for (i=sv->nlags_encoded; i >= 0; i--) |
| 1774 |
tv_dims[i]=sv->nbasestates; |
| 1775 |
} |
| 1776 |
|
| 1777 |
/* |
| 1778 |
Assumes |
| 1779 |
states - An rectangular array of integers. The integer states[j][k] will |
| 1780 |
be non-zero if the kth state (appropriately counted) effects the |
| 1781 |
jth group. |
| 1782 |
sv - Valid pointer to TMarkovStateVariable structure. It must be the |
| 1783 |
case that the number of columns in states is equal to |
| 1784 |
GetTotalNumberStateVariablesIncludingLags(sv). |
| 1785 |
|
| 1786 |
Returns |
| 1787 |
A rectangular array of integers that is a translation from the values of |
| 1788 |
the state variable sv to the state variable that is a product of the state |
| 1789 |
variables with non-zero entries in states. |
| 1790 |
*/ |
| 1791 |
static int** CreateTranslationMatrix(int **table, TMarkovStateVariable *sv) |
| 1792 |
{ |
| 1793 |
int **translation, *state, *tv_dims; |
| 1794 |
int tv, cols=dw_DimA(table), i, j, k; |
| 1795 |
|
| 1796 |
translation=dw_CreateRectangularArray_int(sv->nstates,cols); |
| 1797 |
tv=GetTotalNumberStateVariablesIncludingLags(sv); |
| 1798 |
tv_dims=(int*)dw_malloc(tv*sizeof(int)); |
| 1799 |
state=(int*)dw_malloc(tv*sizeof(int)); |
| 1800 |
GetDimStateVariablesIncludingLags(tv_dims,sv); |
| 1801 |
|
| 1802 |
for (j=tv-1; j >= 0; j--) state[j]=0; |
| 1803 |
i=0; |
| 1804 |
do |
| 1805 |
{ |
| 1806 |
for (j=cols-1; j >= 0; j--) |
| 1807 |
{ |
| 1808 |
translation[i][j]=0; |
| 1809 |
for (k=0; k < tv; k++) |
| 1810 |
if (table[j][k]) |
| 1811 |
translation[i][j]=translation[i][j]*tv_dims[k] + state[k]; |
| 1812 |
} |
| 1813 |
|
| 1814 |
for (k=tv-1; k >= 0; k--) |
| 1815 |
if (state[k] < tv_dims[k]-1) |
| 1816 |
{ |
| 1817 |
state[k]++; |
| 1818 |
break; |
| 1819 |
} |
| 1820 |
else |
| 1821 |
state[k]=0; |
| 1822 |
i++; |
| 1823 |
} |
| 1824 |
while (k >= 0); |
| 1825 |
|
| 1826 |
dw_free(tv_dims); |
| 1827 |
|
| 1828 |
return translation; |
| 1829 |
} |
| 1830 |
|
| 1831 |
/* |
| 1832 |
Assumes: |
| 1833 |
not_time_varying : pointer to integer. |
| 1834 |
n_coef : pointer to integer array. *n_coef should be unallocated. |
| 1835 |
cum_total_coef : pointer to integer array. *cum_total_coef should be unallocated. |
| 1836 |
idx_coef : pointer to integer matrix. *idx_coef should be unallocated. |
| 1837 |
n_var : pointer to integer array. *n_var should be unallocated. |
| 1838 |
cum_total_var : pointer to integer array. *cum_total_var should be unallocated. |
| 1839 |
idx_var : pointer to integer matrix. *idx_var should be unallocated. |
| 1840 |
f_in : pointer to valid file sturcture. |
| 1841 |
sv : pointer to valid TMarkovStateVariable structure. |
| 1842 |
|
| 1843 |
Returns: |
| 1844 |
One upon success and zero otherwise. |
| 1845 |
|
| 1846 |
Results: |
| 1847 |
Upon success, *not_time_varying is set and memory is allocated for *n_coef, |
| 1848 |
*cum_total_coef, *idx_coef, *n_var, *cum_total_var, and *idx_var. Upon |
| 1849 |
failure, theses are set to null. |
| 1850 |
|
| 1851 |
Notes: |
| 1852 |
Upon success, the calling routines should free the allocated memory. The |
| 1853 |
routine dw_FreeArray() MUST be used to free *n_coef, *cum_total_coef, |
| 1854 |
*idx_coef, *n_var, *cum_total_var, and *idx_var. |
| 1855 |
|
| 1856 |
The total number of coefficient variables can be obtained by the call |
| 1857 |
dw_DimA(n_coef) and will be zero if n_coef is null. The total number |
| 1858 |
of variance variables can be obtained by the call dw_DimA(n_var) and |
| 1859 |
will be zero if n_var is zero. |
| 1860 |
|
| 1861 |
(*n_coef)[i] is the number of regimes associated with the ith coefficient |
| 1862 |
parameter. |
| 1863 |
|
| 1864 |
(*cum_total_coef)[i] is the offset of the ith coefficient parameter. Note |
| 1865 |
that (*cum_total_coef)[i] + (*n_coef)[i] = *cum_total_coef[i+1]. |
| 1866 |
|
| 1867 |
(*idx_coef)[k][i] is the regime of the ith coefficient parameter associated |
| 1868 |
with grand regime k. |
| 1869 |
|
| 1870 |
(*n_var)[i] is the number of regimes associated with the ith variance |
| 1871 |
parameter. |
| 1872 |
|
| 1873 |
(*cum_total_var)[i] is the offset of the ith variance parameter. Note that |
| 1874 |
(*cum_total_var)[i] + (*n_var)[i] = *cum_total_var[i+1]. |
| 1875 |
|
| 1876 |
(*idx_var)[k][i] is the regime of the ith variable parameter associated with |
| 1877 |
grand regime k. |
| 1878 |
*/ |
| 1879 |
int ReadDSGEControllingVariables(int *not_time_varying, int **n_coef, int **cum_total_coef, int*** idx_coef, |
| 1880 |
int **n_var, int **cum_total_var, int ***idx_var, FILE *f_in, TMarkovStateVariable *sv) |
| 1881 |
{ |
| 1882 |
int j, k, m, err=0, offset, ncoef, nvar, tv; |
| 1883 |
int *tv_dim, **table=(int**)NULL; |
| 1884 |
char *str; |
| 1885 |
|
| 1886 |
*idx_coef=(int**)NULL; |
| 1887 |
*n_coef=(int*)NULL;; |
| 1888 |
*cum_total_coef=(int*)NULL; |
| 1889 |
*idx_var=(int**)NULL; |
| 1890 |
*n_var=(int*)NULL;; |
| 1891 |
*cum_total_var=(int*)NULL; |
| 1892 |
|
| 1893 |
if (!f_in) return 0; |
| 1894 |
|
| 1895 |
tv=GetTotalNumberStateVariablesIncludingLags(sv); |
| 1896 |
tv_dim=(int*)dw_malloc(tv*sizeof(int)); |
| 1897 |
GetDimStateVariablesIncludingLags(tv_dim,sv); |
| 1898 |
|
| 1899 |
// Read number of free parameters not time varying |
| 1900 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Number of free parameters not time varying ==//",not_time_varying))) |
| 1901 |
goto CLEAN_UP; |
| 1902 |
else |
| 1903 |
if (*not_time_varying < 0) |
| 1904 |
{ |
| 1905 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1906 |
goto CLEAN_UP; |
| 1907 |
} |
| 1908 |
offset=*not_time_varying; |
| 1909 |
|
| 1910 |
// Read time varying coefficient parameters |
| 1911 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Time varying coefficient parameters ==//",&ncoef))) |
| 1912 |
goto CLEAN_UP; |
| 1913 |
else |
| 1914 |
if (ncoef < 0) |
| 1915 |
{ |
| 1916 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1917 |
goto CLEAN_UP; |
| 1918 |
} |
| 1919 |
if (ncoef > 0) |
| 1920 |
{ |
| 1921 |
if (!dw_ReadArray(f_in,table=dw_CreateRectangularArray_int(ncoef,tv))) |
| 1922 |
{ |
| 1923 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1924 |
goto CLEAN_UP; |
| 1925 |
} |
| 1926 |
|
| 1927 |
*idx_coef=CreateTranslationMatrix(table,sv); |
| 1928 |
|
| 1929 |
*n_coef=dw_CreateArray_int(ncoef); |
| 1930 |
*cum_total_coef=dw_CreateArray_int(ncoef); |
| 1931 |
for (j=0; j < ncoef; j++) |
| 1932 |
{ |
| 1933 |
(*cum_total_coef)[j]=offset; |
| 1934 |
for (m=1, k=0; k < tv; k++) |
| 1935 |
if (table[j][k]) m*=tv_dim[k]; |
| 1936 |
(*n_coef)[j]=m; |
| 1937 |
offset+=m; |
| 1938 |
} |
| 1939 |
|
| 1940 |
dw_FreeArray(table); |
| 1941 |
table=(int**)NULL; |
| 1942 |
} |
| 1943 |
|
| 1944 |
// Read time varying variance parameters |
| 1945 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Time varying variance parameters ==//",&nvar))) |
| 1946 |
goto CLEAN_UP; |
| 1947 |
else |
| 1948 |
if (nvar < 0) |
| 1949 |
{ |
| 1950 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1951 |
goto CLEAN_UP; |
| 1952 |
} |
| 1953 |
if (nvar > 0) |
| 1954 |
{ |
| 1955 |
if (!dw_ReadArray(f_in,table=dw_CreateRectangularArray_int(nvar,tv))) |
| 1956 |
{ |
| 1957 |
err=SWITCHIO_ERROR_READING_DATA; |
| 1958 |
goto CLEAN_UP; |
| 1959 |
} |
| 1960 |
|
| 1961 |
*idx_var=CreateTranslationMatrix(table,sv); |
| 1962 |
|
| 1963 |
*n_var=dw_CreateArray_int(nvar); |
| 1964 |
*cum_total_var=dw_CreateArray_int(nvar); |
| 1965 |
for (j=0; j < nvar; j++) |
| 1966 |
{ |
| 1967 |
(*cum_total_var)[j]=offset; |
| 1968 |
for (m=1, k=0; k < tv; k++) |
| 1969 |
if (table[j][k]) m*=tv_dim[k]; |
| 1970 |
(*n_var)[j]=m; |
| 1971 |
offset+=m; |
| 1972 |
} |
| 1973 |
|
| 1974 |
dw_FreeArray(table); |
| 1975 |
table=(int**)NULL; |
| 1976 |
} |
| 1977 |
|
| 1978 |
return 1; |
| 1979 |
|
| 1980 |
CLEAN_UP: |
| 1981 |
if (table) dw_FreeArray(table); |
| 1982 |
if (*idx_coef) |
| 1983 |
{ |
| 1984 |
dw_FreeArray(*idx_coef); |
| 1985 |
*idx_coef=(int**)NULL; |
| 1986 |
} |
| 1987 |
if (*n_coef) |
| 1988 |
{ |
| 1989 |
dw_FreeArray(*n_coef); |
| 1990 |
*n_coef=(int*)NULL; |
| 1991 |
} |
| 1992 |
if (*cum_total_coef) |
| 1993 |
{ |
| 1994 |
dw_FreeArray(*cum_total_coef); |
| 1995 |
*cum_total_coef=(int*)NULL; |
| 1996 |
} |
| 1997 |
if (*idx_var) |
| 1998 |
{ |
| 1999 |
dw_FreeArray(*idx_var); |
| 2000 |
*idx_var=(int**)NULL; |
| 2001 |
} |
| 2002 |
if (*n_var) |
| 2003 |
{ |
| 2004 |
dw_FreeArray(*n_var); |
| 2005 |
*n_var=(int*)NULL; |
| 2006 |
} |
| 2007 |
if (*cum_total_var) |
| 2008 |
{ |
| 2009 |
dw_FreeArray(*cum_total_var); |
| 2010 |
*cum_total_var=(int*)NULL; |
| 2011 |
} |
| 2012 |
return 0; |
| 2013 |
} |
| 2014 |
/*******************************************************************************/ |
| 2015 |
/*******************************************************************************/ |
| 2016 |
/*******************************************************************************/ |
| 2017 |
|
| 2018 |
|
| 2019 |
/* |
| 2020 |
Assumes |
| 2021 |
states - An m x sv->n_state_variables array of integers. The integer |
| 2022 |
states[j][k] will be non-zero if the kth state effects the jth |
| 2023 |
group. |
| 2024 |
sv - Valid pointer to TMarkovStateVariable structure. It must be the |
| 2025 |
case that the number of columns in states is equal to |
| 2026 |
sv->n_state_variables. |
| 2027 |
|
| 2028 |
Returns |
| 2029 |
An m x nstates array of integers that is a translation from the values of |
| 2030 |
the state variable sv to the state variable that is a product of the state |
| 2031 |
variables with non-zero entries in states. |
| 2032 |
*/ |
| 2033 |
/* static int** CreateTranslationMatrix_Flat_old(int **table, TMarkovStateVariable *sv) */ |
| 2034 |
/* { */ |
| 2035 |
/* int **translation; */ |
| 2036 |
/* int i, j, k; */ |
| 2037 |
/* translation=dw_CreateRectangularArray_int(dw_DimA(table),sv->nstates); */ |
| 2038 |
|
| 2039 |
/* for (j=dw_DimA(table)-1; j >= 0; j--) */ |
| 2040 |
/* for (i=sv->nstates-1; i >= 0; i--) */ |
| 2041 |
/* for (translation[j][i]=k=0; k < sv->n_state_variables; k++) */ |
| 2042 |
/* if (table[j][k]) */ |
| 2043 |
/* translation[j][i]=translation[j][i]*sv->state_variable[k]->nstates + sv->index[i][k]; */ |
| 2044 |
|
| 2045 |
/* return translation; */ |
| 2046 |
/* } */ |
| 2047 |
/* |
| 2048 |
Assumes: |
| 2049 |
not_time_varying : pointer to integer. |
| 2050 |
n_coef : pointer to integer array. *n_coef should be unallocated. |
| 2051 |
cum_total_coef : pointer to integer array. *cum_total_coef should be unallocated. |
| 2052 |
idx_coef : pointer to integer matrix. *idx_coef should be unallocated. |
| 2053 |
n_var : pointer to integer array. *n_var should be unallocated. |
| 2054 |
cum_total_var : pointer to integer array. *cum_total_var should be unallocated. |
| 2055 |
idx_var : pointer to integer matrix. *idx_var should be unallocated. |
| 2056 |
f_in : pointer to valid file sturcture. |
| 2057 |
sv : pointer to valid TMarkovStateVariable structure. |
| 2058 |
|
| 2059 |
Returns: |
| 2060 |
One upon success and zero otherwise. |
| 2061 |
|
| 2062 |
Results: |
| 2063 |
Upon success, *not_time_varying is set and memory is allocated for *n_coef, |
| 2064 |
*cum_total_coef, *idx_coef, *n_var, *cum_total_var, and *idx_var. Upon |
| 2065 |
failure, theses are set to null. |
| 2066 |
|
| 2067 |
Notes: |
| 2068 |
Upon success, the calling routines should free the allocated memory. The |
| 2069 |
routine dw_FreeArray() MUST be used to free *n_coef, *cum_total_coef, |
| 2070 |
*idx_coef, *n_var, *cum_total_var, and *idx_var. |
| 2071 |
|
| 2072 |
The total number of coefficient variables can be obtained by the call |
| 2073 |
dw_DimA(n_coef) and will be zero if n_coef is null. The total number |
| 2074 |
of variance variables can be obtained by the call dw_DimA(n_var) and |
| 2075 |
will be zero if n_var is zero. |
| 2076 |
|
| 2077 |
(*n_coef)[i] is the number of regimes associated with the ith coefficient |
| 2078 |
parameter. |
| 2079 |
|
| 2080 |
(*cum_total_coef)[i] is the offset of the ith coefficient parameter. Note |
| 2081 |
that (*cum_total_coef)[i] + (*n_coef)[i] = *cum_total_coef[i+1]. |
| 2082 |
|
| 2083 |
(*idx_coef)[k][i] is the regime of the ith coefficient parameter associated |
| 2084 |
with grand regime k. |
| 2085 |
|
| 2086 |
(*n_var)[i] is the number of regimes associated with the ith variance |
| 2087 |
parameter. |
| 2088 |
|
| 2089 |
(*cum_total_var)[i] is the offset of the ith variance parameter. Note that |
| 2090 |
(*cum_total_var)[i] + (*n_var)[i] = *cum_total_var[i+1]. |
| 2091 |
|
| 2092 |
(*idx_var)[k][i] is the regime of the ith variable parameter associated with |
| 2093 |
grand regime k. |
| 2094 |
*/ |
| 2095 |
int ReadDSGEControllingVariables_old(int *not_time_varying, int **n_coef, int **cum_total_coef, int*** idx_coef, |
| 2096 |
int **n_var, int **cum_total_var, int ***idx_var, FILE *f_in, TMarkovStateVariable *sv) |
| 2097 |
{ |
| 2098 |
int j, k, m, err=0, offset, ncoef, nvar; |
| 2099 |
int **table=(int**)NULL; |
| 2100 |
char *str; |
| 2101 |
|
| 2102 |
*idx_coef=(int**)NULL; |
| 2103 |
*n_coef=(int*)NULL;; |
| 2104 |
*cum_total_coef=(int*)NULL; |
| 2105 |
*idx_var=(int**)NULL; |
| 2106 |
*n_var=(int*)NULL;; |
| 2107 |
*cum_total_var=(int*)NULL; |
| 2108 |
|
| 2109 |
if (!f_in) return 0; |
| 2110 |
|
| 2111 |
// Read number of free parameters not time varying |
| 2112 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Number of free parameters not time varying ==//",not_time_varying))) |
| 2113 |
goto CLEAN_UP; |
| 2114 |
else |
| 2115 |
if (*not_time_varying < 0) |
| 2116 |
{ |
| 2117 |
err=SWITCHIO_ERROR_READING_DATA; |
| 2118 |
goto CLEAN_UP; |
| 2119 |
} |
| 2120 |
offset=*not_time_varying; |
| 2121 |
|
| 2122 |
// Read time varying coefficient parameters |
| 2123 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Time varying coefficient parameters ==//",&ncoef))) |
| 2124 |
goto CLEAN_UP; |
| 2125 |
else |
| 2126 |
if (ncoef < 0) |
| 2127 |
{ |
| 2128 |
err=SWITCHIO_ERROR_READING_DATA; |
| 2129 |
goto CLEAN_UP; |
| 2130 |
} |
| 2131 |
if (ncoef > 0) |
| 2132 |
{ |
| 2133 |
if (!dw_ReadArray(f_in,table=dw_CreateRectangularArray_int(ncoef,sv->n_state_variables))) |
| 2134 |
{ |
| 2135 |
err=SWITCHIO_ERROR_READING_DATA; |
| 2136 |
goto CLEAN_UP; |
| 2137 |
} |
| 2138 |
*idx_coef=CreateTranslationMatrix(table,sv); |
| 2139 |
*n_coef=dw_CreateArray_int(ncoef); |
| 2140 |
*cum_total_coef=dw_CreateArray_int(ncoef); |
| 2141 |
for (j=0; j < ncoef; j++) |
| 2142 |
{ |
| 2143 |
(*cum_total_coef)[j]=offset; |
| 2144 |
for (m=1, k=0; k < sv->n_state_variables; k++) |
| 2145 |
if (table[j][k]) m*=sv->state_variable[k]->nstates; |
| 2146 |
(*n_coef)[j]=m; |
| 2147 |
offset+=m; |
| 2148 |
} |
| 2149 |
dw_FreeArray(table); |
| 2150 |
table=(int**)NULL; |
| 2151 |
} |
| 2152 |
|
| 2153 |
// Read time varying variance parameters |
| 2154 |
if ((err=ReadInteger(f_in,(char*)NULL,str="//== Time varying variance parameters ==//",&nvar))) |
| 2155 |
goto CLEAN_UP; |
| 2156 |
else |
| 2157 |
if (nvar < 0) |
| 2158 |
{ |
| 2159 |
err=SWITCHIO_ERROR_READING_DATA; |
| 2160 |
goto CLEAN_UP; |
| 2161 |
} |
| 2162 |
if (nvar > 0) |
| 2163 |
{ |
| 2164 |
if (!dw_ReadArray(f_in,table=dw_CreateRectangularArray_int(nvar,sv->n_state_variables))) |
| 2165 |
{ |
| 2166 |
err=SWITCHIO_ERROR_READING_DATA; |
| 2167 |
goto CLEAN_UP; |
| 2168 |
} |
| 2169 |
*idx_var=CreateTranslationMatrix(table,sv); |
| 2170 |
*n_var=dw_CreateArray_int(nvar); |
| 2171 |
*cum_total_var=dw_CreateArray_int(nvar); |
| 2172 |
for (j=0; j < nvar; j++) |
| 2173 |
{ |
| 2174 |
(*cum_total_var)[j]=offset; |
| 2175 |
for (m=1, k=0; k < sv->n_state_variables; k++) |
| 2176 |
if (table[j][k]) m*=sv->state_variable[k]->nstates; |
| 2177 |
(*n_var)[j]=m; |
| 2178 |
offset+=m; |
| 2179 |
} |
| 2180 |
dw_FreeArray(table); |
| 2181 |
table=(int**)NULL; |
| 2182 |
} |
| 2183 |
|
| 2184 |
return 1; |
| 2185 |
|
| 2186 |
CLEAN_UP: |
| 2187 |
if (table) dw_FreeArray(table); |
| 2188 |
if (*idx_coef) |
| 2189 |
{ |
| 2190 |
dw_FreeArray(*idx_coef); |
| 2191 |
*idx_coef=(int**)NULL; |
| 2192 |
} |
| 2193 |
if (*n_coef) |
| 2194 |
{ |
| 2195 |
dw_FreeArray(*n_coef); |
| 2196 |
*n_coef=(int*)NULL; |
| 2197 |
} |
| 2198 |
if (*cum_total_coef) |
| 2199 |
{ |
| 2200 |
dw_FreeArray(*cum_total_coef); |
| 2201 |
*cum_total_coef=(int*)NULL; |
| 2202 |
} |
| 2203 |
if (*idx_var) |
| 2204 |
{ |
| 2205 |
dw_FreeArray(*idx_var); |
| 2206 |
*idx_var=(int**)NULL; |
| 2207 |
} |
| 2208 |
if (*n_var) |
| 2209 |
{ |
| 2210 |
dw_FreeArray(*n_var); |
| 2211 |
*n_var=(int*)NULL; |
| 2212 |
} |
| 2213 |
if (*cum_total_var) |
| 2214 |
{ |
| 2215 |
dw_FreeArray(*cum_total_var); |
| 2216 |
*cum_total_var=(int*)NULL; |
| 2217 |
} |
| 2218 |
return 0; |
| 2219 |
} |
| 2220 |
|
| 2221 |
/*******************************************************************************/ |
| 2222 |
/*******************************************************************************/ |
| 2223 |
/*******************************************************************************/ |
| 2224 |
|
| 2225 |
|
| 2226 |
/*******************************************************************************/ |
| 2227 |
/****************************** Utility Routines *******************************/ |
| 2228 |
/*******************************************************************************/ |
| 2229 |
/* |
| 2230 |
Assumes: |
| 2231 |
format : null or "*%s*" |
| 2232 |
str : "*" |
| 2233 |
err : error flag |
| 2234 |
|
| 2235 |
Results: |
| 2236 |
Assembles error message and calls error routines. If USER_ERR is a verbose |
| 2237 |
error, then an error message is displayed. If USER_ERR is a terminal error |
| 2238 |
the the program will terminate. |
| 2239 |
*/ |
| 2240 |
void ReadError(char *format, char *str, int err) |
| 2241 |
{ |
| 2242 |
char *errmsg, *errmsg_format, *buffer; |
| 2243 |
switch (err) |
| 2244 |
{ |
| 2245 |
case SWITCHIO_LINE_ID_NOT_FOUND: |
| 2246 |
errmsg_format="Line identifier ""%s"" not found."; |
| 2247 |
break; |
| 2248 |
case SWITCHIO_ERROR_READING_DATA: |
| 2249 |
errmsg_format="Error reading data after line identifier ""%s""."; |
| 2250 |
break; |
| 2251 |
case SWITCHIO_FILE_ERROR: |
| 2252 |
if (str) |
| 2253 |
{ |
| 2254 |
errmsg_format="Error reading/opening %s."; |
| 2255 |
sprintf(errmsg=(char*)dw_malloc(strlen(errmsg_format)+strlen(str)-1),errmsg_format,str); |
| 2256 |
dw_UserError(errmsg); |
| 2257 |
dw_free(errmsg); |
| 2258 |
} |
| 2259 |
else |
| 2260 |
dw_Error(FILE_ERR); |
| 2261 |
case 0: |
| 2262 |
return; |
| 2263 |
default: |
| 2264 |
dw_Error(UNKNOWN_ERR); |
| 2265 |
return; |
| 2266 |
} |
| 2267 |
if (format) |
| 2268 |
sprintf(buffer=(char*)dw_malloc(strlen(format)+strlen(str)-1),format,str); |
| 2269 |
else |
| 2270 |
buffer=str; |
| 2271 |
sprintf(errmsg=(char*)dw_malloc(strlen(errmsg_format)+strlen(buffer)-1),errmsg_format,buffer); |
| 2272 |
if (buffer != str) dw_free(buffer); |
| 2273 |
dw_UserError(errmsg); |
| 2274 |
dw_free(errmsg); |
| 2275 |
} |
| 2276 |
|
| 2277 |
/* |
| 2278 |
Assumes |
| 2279 |
f_in : pointer to valid file structure |
| 2280 |
format : "*%s*" or null |
| 2281 |
str : "*" |
| 2282 |
|
| 2283 |
Returns: |
| 2284 |
Zero upon success and SWITCHIO_LINE_ID_NOT_FOUND upon failure. |
| 2285 |
|
| 2286 |
Results |
| 2287 |
Starting at the current position, and wrapping around the end of the file if |
| 2288 |
necessary, finds the first occurance of the string at the beginning of a |
| 2289 |
line. Upon success, the file pointer is positioned at the beginning of the |
| 2290 |
next line. |
| 2291 |
*/ |
| 2292 |
int SetFilePosition(FILE *f_in, char *format, char *str) |
| 2293 |
{ |
| 2294 |
char *buffer; |
| 2295 |
int rtrn; |
| 2296 |
if (format) |
| 2297 |
sprintf(buffer=(char*)dw_malloc(strlen(format)+strlen(str)-1),format,str); |
| 2298 |
else |
| 2299 |
buffer=str; |
| 2300 |
rtrn=dw_SetFilePosition(f_in,buffer); |
| 2301 |
if (buffer != str) dw_free(buffer); |
| 2302 |
return rtrn ? 0 : SWITCHIO_LINE_ID_NOT_FOUND; |
| 2303 |
} |
| 2304 |
|
| 2305 |
/* |
| 2306 |
Assumes |
| 2307 |
f_in : pointer to valid file structure |
| 2308 |
format : "*%s*" or null |
| 2309 |
str : "*" |
| 2310 |
i : pointer to integer |
| 2311 |
|
| 2312 |
Returns: |
| 2313 |
Zero upon success and SWITCHIO_LINE_ID_NOT_FOUND or |
| 2314 |
SWITCHIO_ERROR_READING_DATA on failure. |
| 2315 |
|
| 2316 |
Results |
| 2317 |
Attempts to read integer from the first line following the first occurance |
| 2318 |
of the string at the beginning of a line. |
| 2319 |
*/ |
| 2320 |
int ReadInteger(FILE *f_in, char *format, char *str, int *i) |
| 2321 |
{ |
| 2322 |
if (SetFilePosition(f_in,format,str)) return SWITCHIO_LINE_ID_NOT_FOUND; |
| 2323 |
return (fscanf(f_in," %d ",i) == 1) ? 0 : SWITCHIO_ERROR_READING_DATA; |
| 2324 |
} |
| 2325 |
|
| 2326 |
/* |
| 2327 |
Assumes |
| 2328 |
f_in : pointer to valid file structure |
| 2329 |
format : "*%s*" or null |
| 2330 |
str : "*" |
| 2331 |
X : pointer to valid matrix structure |
| 2332 |
|
| 2333 |
Returns: |
| 2334 |
Zero upon success and SWITCHIO_LINE_ID_NOT_FOUND or |
| 2335 |
SWITCHIO_ERROR_READING_DATA on failure. |
| 2336 |
|
| 2337 |
Results |
| 2338 |
Attempts to read integer from the first line following the first occurance |
| 2339 |
of the string at the beginning of a line. |
| 2340 |
*/ |
| 2341 |
int ReadMatrix(FILE *f_in, char *format, char *str, TMatrix X) |
| 2342 |
{ |
| 2343 |
if (SetFilePosition(f_in,format,str)) return SWITCHIO_LINE_ID_NOT_FOUND; |
| 2344 |
return dw_ReadMatrix(f_in,X) ? 0 : SWITCHIO_ERROR_READING_DATA; |
| 2345 |
} |
| 2346 |
|
| 2347 |
/* |
| 2348 |
Assumes |
| 2349 |
f_in : pointer to valid file structure |
| 2350 |
format : "*%s*" or null |
| 2351 |
str : "*" |
| 2352 |
v : pointer to valid vector structure |
| 2353 |
|
| 2354 |
Returns: |
| 2355 |
Zero upon success and SWITCHIO_LINE_ID_NOT_FOUND or |
| 2356 |
SWITCHIO_ERROR_READING_DATA on failure. |
| 2357 |
|
| 2358 |
Results |
| 2359 |
Attempts to read integer from the first line following the first occurance |
| 2360 |
of the string at the beginning of a line. |
| 2361 |
*/ |
| 2362 |
int ReadVector(FILE *f_in, char *format, char *str, TVector v) |
| 2363 |
{ |
| 2364 |
if (SetFilePosition(f_in,format,str)) return SWITCHIO_LINE_ID_NOT_FOUND; |
| 2365 |
return dw_ReadVector(f_in,v) ? 0 : SWITCHIO_ERROR_READING_DATA; |
| 2366 |
} |
| 2367 |
|
| 2368 |
/* |
| 2369 |
Assumes |
| 2370 |
f_in : pointer to valid file structure |
| 2371 |
format : "*%s*" or null |
| 2372 |
str : "*" |
| 2373 |
A : pointer to valid integer array |
| 2374 |
|
| 2375 |
Returns: |
| 2376 |
Zero upon success and SWITCHIO_LINE_ID_NOT_FOUND or |
| 2377 |
SWITCHIO_ERROR_READING_DATA on failure. |
| 2378 |
|
| 2379 |
Results |
| 2380 |
Attempts to read integer from the first line following the first occurance |
| 2381 |
of the string at the beginning of a line. |
| 2382 |
*/ |
| 2383 |
int ReadIntArray(FILE *f_in, char *format, char *str, void *X) |
| 2384 |
{ |
| 2385 |
if (SetFilePosition(f_in,format,str)) return SWITCHIO_LINE_ID_NOT_FOUND; |
| 2386 |
return dw_ReadArray(f_in,X) ? 0 : SWITCHIO_ERROR_READING_DATA; |
| 2387 |
} |
| 2388 |
|
| 2389 |
/* |
| 2390 |
Assumes |
| 2391 |
format - either null or null terminated string of the form "*%s*%d" or "*%d*" |
| 2392 |
str - either null or null terminated string |
| 2393 |
k - integer |
| 2394 |
|
| 2395 |
Returns |
| 2396 |
Allocates memory and returns null terminated string using the supplied |
| 2397 |
format. |
| 2398 |
|
| 2399 |
Notes |
| 2400 |
Calling routine is responsible for freeing returned memory. If both |
| 2401 |
format and str are not null, then format must be of the form "*%s*%d*". If |
| 2402 |
format is not null but str is null, then format must be of the form "*%d*". |
| 2403 |
*/ |
| 2404 |
char* CreateIntegerString(char *format, char *str, int k) |
| 2405 |
{ |
| 2406 |
int i, j; |
| 2407 |
char *buffer; |
| 2408 |
for (j=10, i=1; k >= j; j*=10, i++); |
| 2409 |
if (format) |
| 2410 |
if (str) |
| 2411 |
sprintf(buffer=(char*)dw_malloc(strlen(format)+strlen(str)+i-3),format,str,k); |
| 2412 |
else |
| 2413 |
sprintf(buffer=(char*)dw_malloc(strlen(format)+i-1),format,k); |
| 2414 |
else |
| 2415 |
sprintf(buffer=(char*)dw_malloc(i+1),"%d",k); |
| 2416 |
return buffer; |
| 2417 |
} |
| 2418 |
/*******************************************************************************/ |
| 2419 |
/*******************************************************************************/ |
| 2420 |
/*******************************************************************************/ |