| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner and Tao Zha |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "dw_switch.h" |
| 19 |
#include "dw_switchio.h" |
| 20 |
#include "dw_metropolis_theta.h" |
| 21 |
#include "dw_rand.h" |
| 22 |
#include "dw_error.h" |
| 23 |
#include "dw_ascii.h" |
| 24 |
#include "dw_parse_cmd.h" |
| 25 |
#include "dw_array.h" |
| 26 |
#include "dw_metropolis_simulation.h" |
| 27 |
#include "dw_std.h" |
| 28 |
|
| 29 |
#include <time.h> |
| 30 |
#include <string.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <limits.h> |
| 34 |
#include <math.h> |
| 35 |
|
| 36 |
#include <time.h> |
| 37 |
|
| 38 |
/* Undefine to use wall time instead of process time */ |
| 39 |
//#define WALL_TIME |
| 40 |
|
| 41 |
//======================= |
| 42 |
|
| 43 |
int Simulate(int burn_in, int draws, int thin, int period, TStateModel *model, FILE *f_out) |
| 44 |
{ |
| 45 |
#ifdef WALL_TIME |
| 46 |
time_t time; |
| 47 |
#else |
| 48 |
clock_t begin_time; |
| 49 |
#endif |
| 50 |
int i, j, check, ntheta, nq; |
| 51 |
TVector theta, q; |
| 52 |
|
| 53 |
// Initialization |
| 54 |
ntheta=NumberFreeParametersTheta(model); |
| 55 |
nq=NumberFreeParametersQ(model); |
| 56 |
if (ntheta) theta=CreateVector(ntheta); |
| 57 |
if (nq) q=CreateVector(nq); |
| 58 |
|
| 59 |
// Burn-in |
| 60 |
ResetCounts_metropolis_theta(model->metropolis_theta); |
| 61 |
ResetMetropolisHastingsCounts_q(model); |
| 62 |
if (burn_in > 0) |
| 63 |
{ |
| 64 |
printf("(0 / %d) Beginning burn in.\n",burn_in); |
| 65 |
#ifdef WALL_TIME |
| 66 |
begin_time=time((time_t*)NULL); |
| 67 |
#else |
| 68 |
begin_time=clock(); |
| 69 |
#endif |
| 70 |
for (check=period, i=1; i <= burn_in; i++) |
| 71 |
{ |
| 72 |
DrawAll(model); |
| 73 |
|
| 74 |
if (i == check) |
| 75 |
{ |
| 76 |
check+=period; |
| 77 |
#ifdef WALL_TIME |
| 78 |
printf("(%d / %d) Total Elapsed Time: %d seconds\n",i,burn_in,(int)(time((time_t*)NULL) - begin_time)); |
| 79 |
#else |
| 80 |
printf("(%d / %d) Total Elapsed Time: %lf seconds\n",i,burn_in,((double)(clock() - begin_time))/CLOCKS_PER_SEC); |
| 81 |
#endif |
| 82 |
} |
| 83 |
} |
| 84 |
#ifdef WALL_TIME |
| 85 |
printf("(%d / %d) Elapsed Time: %d seconds\n",burn_in,burn_in,(int)(time((time_t*)NULL) - begin_time)); |
| 86 |
#else |
| 87 |
printf("(%d / %d) Elapsed Time: %lf seconds\n",burn_in,burn_in,((double)(clock() - begin_time))/CLOCKS_PER_SEC); |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
// Simulate |
| 92 |
if (draws > 0) |
| 93 |
{ |
| 94 |
printf("(0 / %d) Beginning simulation.\n",draws); |
| 95 |
#ifdef WALL_TIME |
| 96 |
begin_time=time((time_t*)NULL); |
| 97 |
#else |
| 98 |
begin_time=clock(); |
| 99 |
#endif |
| 100 |
for (check=period, i=1; i <= draws; i++) |
| 101 |
{ |
| 102 |
for (j=thin; j > 0; j--) DrawAll(model); |
| 103 |
|
| 104 |
fprintf(f_out,"%le %le",LogPosterior_StatesIntegratedOut(model),LogLikelihood_StatesIntegratedOut(model)); |
| 105 |
if (ntheta) |
| 106 |
{ |
| 107 |
ConvertThetaToFreeParameters(model,pElementV(theta)); |
| 108 |
for (j=0; j < ntheta; j++) fprintf(f_out," %le",ElementV(theta,j)); |
| 109 |
} |
| 110 |
if (nq) |
| 111 |
{ |
| 112 |
ConvertQToFreeParameters(model,pElementV(q)); |
| 113 |
for (j=0; j < nq; j++) fprintf(f_out," %le",ElementV(q,j)); |
| 114 |
} |
| 115 |
fprintf(f_out,"\n"); |
| 116 |
|
| 117 |
if (i == check) |
| 118 |
{ |
| 119 |
check+=period; |
| 120 |
#ifdef WALL_TIME |
| 121 |
printf("(%d / %d) Total Elapsed Time: %d seconds\n",i,draws,(int)(time((time_t*)NULL) - begin_time)); |
| 122 |
#else |
| 123 |
printf("(%d / %d) Total Elapsed Time: %lf seconds\n",i,draws,((double)(clock() - begin_time))/CLOCKS_PER_SEC); |
| 124 |
#endif |
| 125 |
} |
| 126 |
} |
| 127 |
#ifdef WALL_TIME |
| 128 |
printf("(%d / %d) Elapsed Time: %d seconds\n",draws,draws,(int)(time((time_t*)NULL) - begin_time)); |
| 129 |
#else |
| 130 |
printf("(%d / %d) Elapsed Time: %lf seconds\n",draws,draws,((double)(clock() - begin_time))/CLOCKS_PER_SEC); |
| 131 |
#endif |
| 132 |
} |
| 133 |
|
| 134 |
// Clean up |
| 135 |
if (ntheta) FreeVector(theta); |
| 136 |
if (nq) FreeVector(q); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
void SetupDirections(TVector *direction, int n) |
| 141 |
{ |
| 142 |
/******************************************************************************/ |
| 143 |
// indexes 1 and 2 are extremely negatively correlated |
| 144 |
PRECISION b; |
| 145 |
InitializeVector(direction[1]=CreateVector(n),0.0); |
| 146 |
InitializeVector(direction[2]=CreateVector(n),0.0); |
| 147 |
b=1.0/sqrt(2); |
| 148 |
ElementV(direction[1],1)=b; |
| 149 |
ElementV(direction[1],2)=-b; |
| 150 |
ElementV(direction[2],1)=b; |
| 151 |
ElementV(direction[2],2)=b; |
| 152 |
/******************************************************************************/ |
| 153 |
|
| 154 |
/****************************************************************************** |
| 155 |
// indexes 1 and 2 are extremely negatively correlated |
| 156 |
// index 21 and sum of 1 and 2 are positively correlated |
| 157 |
PRECISION a, b, r; |
| 158 |
if (n > 21) |
| 159 |
{ |
| 160 |
InitializeVector(direction[1]=CreateVector(n),0.0); |
| 161 |
InitializeVector(direction[2]=CreateVector(n),0.0); |
| 162 |
InitializeVector(direction[21]=CreateVector(n),0.0); |
| 163 |
b=1.0/sqrt(2); |
| 164 |
a=0.02; |
| 165 |
r=1.0/sqrt(1+2*a*a); |
| 166 |
ElementV(direction[1],1)=b; |
| 167 |
ElementV(direction[1],2)=-b; |
| 168 |
|
| 169 |
ElementV(direction[2],1)=b*r; |
| 170 |
ElementV(direction[2],2)=b*r; |
| 171 |
ElementV(direction[2],21)=-2.0*a*b*r; |
| 172 |
|
| 173 |
ElementV(direction[21],1)=a*r; |
| 174 |
ElementV(direction[21],2)=a*r; |
| 175 |
ElementV(direction[21],21)=r; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
printf("Error SetupDirections(): dimension not large enough\n"); |
| 180 |
dw_exit(0); |
| 181 |
} |
| 182 |
/******************************************************************************/ |
| 183 |
} |
| 184 |
|
| 185 |
int WriteMetropolisThetaInfo(char *filename, char *tag, int type, PRECISION center_s, int period_s, int max_period_s, |
| 186 |
PRECISION center_a, int period_a, int max_period_a, int ndraws, int elapsed_time, |
| 187 |
TStateModel *model) |
| 188 |
{ |
| 189 |
FILE *f_out; |
| 190 |
time_t ltime=time((time_t*)NULL); |
| 191 |
|
| 192 |
if (!(f_out=fopen(filename,"wt"))) |
| 193 |
{ |
| 194 |
printf("Unable to create %s for output\n",filename); |
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
fprintf(f_out,"//== Calibration info ==//\n"); |
| 199 |
|
| 200 |
switch(type) |
| 201 |
{ |
| 202 |
case HESSIAN_CALIBRATION | TWO_PASS: fprintf(f_out,"type - hessian two pass calibration\n"); break; |
| 203 |
case DIAGONAL_CALIBRATION | TWO_PASS: fprintf(f_out,"type - diagonal two pass calibration\n"); break; |
| 204 |
case VARIANCE_CALIBRATION | TWO_PASS: fprintf(f_out,"type - variance two pass calibration\n"); break; |
| 205 |
case SPECIAL_CALIBRATION | TWO_PASS: fprintf(f_out,"type - special two pass calibration (call SetupDirections())\n"); break; |
| 206 |
case DIAGONAL_CALIBRATION | PRIOR_CALIBRATION: fprintf(f_out,"type - diagonal calibration of prior\n"); break; |
| 207 |
default: fprintf(f_out,"type - unknown\n"); break; |
| 208 |
} |
| 209 |
|
| 210 |
if (type & TWO_PASS) |
| 211 |
{ |
| 212 |
fprintf(f_out,"target acceptance rate single variable - %lf\n",center_s); |
| 213 |
fprintf(f_out,"initial period single variable - %d\n",period_s); |
| 214 |
fprintf(f_out,"maximum period single variable - %d\n",max_period_s); |
| 215 |
fprintf(f_out,"target acceptance rate overall - %lf\n",center_a); |
| 216 |
fprintf(f_out,"initial period overall - %d\n",period_a); |
| 217 |
fprintf(f_out,"maximum period overall - %d\n",max_period_a); |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
fprintf(f_out,"target acceptance rate - %lf\n",center_a); |
| 222 |
fprintf(f_out,"initial period - %d\n",period_a); |
| 223 |
fprintf(f_out,"maximum period - %d\n",max_period_a); |
| 224 |
} |
| 225 |
|
| 226 |
fprintf(f_out,"total draws - %d\n",ndraws); |
| 227 |
fprintf(f_out,"elapsed time - %d seconds\n",elapsed_time); |
| 228 |
fprintf(f_out,"end time - %s\n",ctime(<ime)); |
| 229 |
if (tag) |
| 230 |
fprintf(f_out,"file tag - %s\n",tag); |
| 231 |
else |
| 232 |
fprintf(f_out,"No file tag\n"); |
| 233 |
|
| 234 |
|
| 235 |
fprintf(f_out,"\n"); |
| 236 |
|
| 237 |
Write_metropolis_theta(f_out,(char*)NULL,(char*)NULL,model->metropolis_theta); |
| 238 |
|
| 239 |
fclose(f_out); |
| 240 |
|
| 241 |
return 1; |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
Returns 1 if metropolis_theta parameters properly set and 0 otherwise. See dw_Simulate() for |
| 246 |
command line options. |
| 247 |
*/ |
| 248 |
int SetUpMetropolisSimulation(int n_args, char **args, char *tag, TStateModel *model, int default_period) |
| 249 |
{ |
| 250 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 251 |
FILE *f_in; |
| 252 |
int i, ndraws, ntheta, nq, period_s, max_period_s, period_a, max_period_a, |
| 253 |
begin_time, end_time, free_filename_metropolis=0, free_filename_hessian=0; |
| 254 |
PRECISION center_s, center_a; |
| 255 |
char *filename_variance, *filename_hessian, *filename_metropolis, *id, *fmt; |
| 256 |
TMatrix hessian=(TMatrix)NULL, hessian_theta=(TMatrix)NULL, posterior_draws=(TMatrix)NULL, |
| 257 |
variance=(TMatrix)NULL, X=(TMatrix)NULL; |
| 258 |
TVector mean=(TVector)NULL, x=(TVector)NULL, theta=(TVector)NULL; |
| 259 |
|
| 260 |
// Get Sizes |
| 261 |
ntheta=NumberFreeParametersTheta(model); |
| 262 |
nq=NumberFreeParametersQ(model); |
| 263 |
|
| 264 |
// Input/output file name for metropolis_theta info |
| 265 |
if (!(filename_metropolis=dw_ParseString_String(n_args,args,"af",(char*)NULL))) |
| 266 |
{ |
| 267 |
if (!tag) |
| 268 |
{ |
| 269 |
printf("No adaptive filename (-af) or tag (-t) specified\n"); |
| 270 |
return 0; |
| 271 |
} |
| 272 |
fmt="metropolis_theta_%s.out"; |
| 273 |
sprintf(filename_metropolis=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 274 |
free_filename_metropolis=1; |
| 275 |
} |
| 276 |
|
| 277 |
// First try: read info from adpative file if -diagonal, -hessian, -variance, -special are not specified |
| 278 |
if ((dw_FindArgument_String(n_args,args,"diagonal") == -1) && (dw_FindArgument_String(n_args,args,"variance") == -1) |
| 279 |
&& (dw_FindArgument_String(n_args,args,"hessian") == -1) && (dw_FindArgument_String(n_args,args,"special") == -1)) |
| 280 |
if (Read_metropolis_theta((FILE*)NULL,filename_metropolis,(char*)NULL,metropolis)) |
| 281 |
{ |
| 282 |
// Cleanup and return |
| 283 |
if (free_filename_metropolis) dw_free(filename_metropolis); |
| 284 |
return READ_METROPOLIS_INFO; |
| 285 |
} |
| 286 |
else |
| 287 |
printf("Unable to open %s. Attempting calibration\n",filename_metropolis); |
| 288 |
else |
| 289 |
printf("Starting recalibration\n"); |
| 290 |
|
| 291 |
// Setup calibration parameters |
| 292 |
center_s=dw_ParseFloating_String(n_args,args,"cs",0.20); |
| 293 |
center_a=dw_ParseFloating_String(n_args,args,"ca",0.25); |
| 294 |
period_s=dw_ParseInteger_String(n_args,args,"ps",default_period); |
| 295 |
period_a=dw_ParseInteger_String(n_args,args,"pa",default_period); |
| 296 |
max_period_s=dw_ParseInteger_String(n_args,args,"mps",8*period_s); |
| 297 |
max_period_a=dw_ParseInteger_String(n_args,args,"mpa",16*period_a); |
| 298 |
|
| 299 |
// Second try: Read parameter draws and calibrate using variance directions if -variance is specified |
| 300 |
if (filename_variance=dw_ParseString_String(n_args,args,"variance",(char*)NULL)) |
| 301 |
{ |
| 302 |
if (!(posterior_draws=dw_ReadPosteriorDraws((FILE*)NULL,filename_variance,(char*)NULL,ntheta+nq))) |
| 303 |
{ |
| 304 |
printf("Unable to read posterior draws file - %s.\n",filename_variance); |
| 305 |
return 0; |
| 306 |
} |
| 307 |
InitializeMatrix(variance=CreateMatrix(ntheta,ntheta),0.0); |
| 308 |
X=CreateMatrix(ntheta,ntheta); |
| 309 |
InitializeVector(mean=CreateVector(ntheta),0.0); |
| 310 |
x=CreateVector(ntheta+nq+2); |
| 311 |
theta=CreateVector(ntheta); |
| 312 |
for (i=RowM(posterior_draws)-1; i >= 0; i--) |
| 313 |
{ |
| 314 |
RowVector(x,posterior_draws,i); |
| 315 |
SubVector(theta,x,2,ntheta); |
| 316 |
AddVV(mean,mean,theta); |
| 317 |
OuterProduct(X,theta,theta); |
| 318 |
AddMM(variance,variance,X); |
| 319 |
} |
| 320 |
ProductVS(mean,mean,1.0/(PRECISION)RowM(posterior_draws)); |
| 321 |
ProductMS(variance,variance,1.0/(PRECISION)RowM(posterior_draws)); |
| 322 |
SubtractMM(variance,variance,OuterProduct(X,mean,mean)); |
| 323 |
|
| 324 |
// Initializing scale and directions using hessian |
| 325 |
Setup_metropolis_theta_variance(metropolis,variance); |
| 326 |
Setup_metropolis_theta_blocks_single(metropolis); |
| 327 |
|
| 328 |
// Calibrating |
| 329 |
begin_time=(int)time((time_t*)NULL); |
| 330 |
ndraws=Calibrate_metropolis_theta_two_pass(model,center_s,period_s,max_period_s,center_a,period_a,max_period_a,1); |
| 331 |
end_time=(int)time((time_t*)NULL); |
| 332 |
|
| 333 |
// Print Metropolis info |
| 334 |
WriteMetropolisThetaInfo(filename_metropolis,tag,VARIANCE_CALIBRATION | TWO_PASS,center_s,period_s,max_period_s, |
| 335 |
center_a,period_a,max_period_a,ndraws,end_time-begin_time,model); |
| 336 |
|
| 337 |
// Clean up and return |
| 338 |
FreeMatrix(posterior_draws); |
| 339 |
FreeMatrix(variance); |
| 340 |
FreeMatrix(X); |
| 341 |
FreeVector(x); |
| 342 |
FreeVector(mean); |
| 343 |
FreeVector(theta); |
| 344 |
if (free_filename_metropolis) dw_free(filename_metropolis); |
| 345 |
return VARIANCE_CALIBRATION | TWO_PASS; |
| 346 |
} |
| 347 |
|
| 348 |
// Third try: Read hessian and calibrate using hessian directions if -hessian is specified |
| 349 |
if (dw_FindArgument_String(n_args,args,"hessian") != -1) |
| 350 |
{ |
| 351 |
if (!(filename_hessian=dw_ParseString_String(n_args,args,"hf",(char*)NULL))) |
| 352 |
{ |
| 353 |
if (!tag) |
| 354 |
{ |
| 355 |
printf("No hessian filename (-hf) or tag (-t) specified\n"); |
| 356 |
return 0; |
| 357 |
} |
| 358 |
fmt="outdataout_%s.prn"; |
| 359 |
sprintf(filename_hessian=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 360 |
free_filename_hessian=1; |
| 361 |
} |
| 362 |
if (f_in=fopen(filename_hessian,"rt")) |
| 363 |
{ |
| 364 |
// Use correct hessian identifier |
| 365 |
//id="%--- Hessian matrix at the log posterior peak ---"; |
| 366 |
id="Hessian = ["; |
| 367 |
if (dw_SetFilePosition(f_in,id)) |
| 368 |
{ |
| 369 |
if (dw_ReadMatrix(f_in,hessian=CreateMatrix(ntheta+nq,ntheta+nq))) |
| 370 |
{ |
| 371 |
hessian_theta=SubMatrix((TMatrix)NULL,hessian,0,0,ntheta,ntheta); |
| 372 |
|
| 373 |
// Initializing scale and directions using hessian |
| 374 |
Setup_metropolis_theta_hessian(metropolis,hessian_theta); |
| 375 |
Setup_metropolis_theta_blocks_single(metropolis); |
| 376 |
|
| 377 |
// Calibrating |
| 378 |
begin_time=(int)time((time_t*)NULL); |
| 379 |
ndraws=Calibrate_metropolis_theta_two_pass(model,center_s,period_s,max_period_s,center_a,period_a,max_period_a,1); |
| 380 |
end_time=(int)time((time_t*)NULL); |
| 381 |
|
| 382 |
// Print Metropolis info |
| 383 |
WriteMetropolisThetaInfo(filename_metropolis,tag,HESSIAN_CALIBRATION | TWO_PASS,center_s,period_s,max_period_s, |
| 384 |
center_a,period_a,max_period_a,ndraws,end_time-begin_time,model); |
| 385 |
|
| 386 |
// Clean up and return |
| 387 |
FreeMatrix(hessian_theta); |
| 388 |
FreeMatrix(hessian); |
| 389 |
if (free_filename_hessian) dw_free(filename_hessian); |
| 390 |
if (free_filename_metropolis) dw_free(filename_metropolis); |
| 391 |
fclose(f_in); |
| 392 |
return HESSIAN_CALIBRATION | TWO_PASS; |
| 393 |
} |
| 394 |
else |
| 395 |
printf("Unable to read hessian matrix after %s in %s.\n",id,filename_hessian); |
| 396 |
FreeMatrix(hessian); |
| 397 |
} |
| 398 |
else |
| 399 |
printf("Unable to find %s in %s.\n",id,filename_hessian); |
| 400 |
fclose(f_in); |
| 401 |
} |
| 402 |
else |
| 403 |
printf("Unable to open %s.\n",filename_hessian); |
| 404 |
if (free_filename_hessian) dw_free(filename_hessian); |
| 405 |
if (free_filename_metropolis) dw_free(filename_metropolis); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
// Last try: Use diagonal |
| 410 |
// Initializing scale and directions |
| 411 |
Setup_metropolis_theta_diagonal(metropolis); |
| 412 |
Setup_metropolis_theta_blocks_single(metropolis); |
| 413 |
|
| 414 |
// Special handling of directions? |
| 415 |
if (dw_FindArgument_String(n_args,args,"special") != -1) |
| 416 |
SetupDirections(metropolis->direction,metropolis->n_parameters); |
| 417 |
|
| 418 |
// Calibrating |
| 419 |
begin_time=(int)time((time_t*)NULL); |
| 420 |
ndraws=Calibrate_metropolis_theta_two_pass(model,center_s,period_s,max_period_s,center_a,period_a,max_period_a,1); |
| 421 |
end_time=(int)time((time_t*)NULL); |
| 422 |
|
| 423 |
// Print Metropolis info |
| 424 |
WriteMetropolisThetaInfo(filename_metropolis,tag,DIAGONAL_CALIBRATION | TWO_PASS,center_s,period_s,max_period_s, |
| 425 |
center_a,period_a,max_period_a,ndraws,end_time-begin_time,model); |
| 426 |
|
| 427 |
// Clean up and return |
| 428 |
if (free_filename_metropolis) dw_free(filename_metropolis); |
| 429 |
return ((dw_FindArgument_String(n_args,args,"special") != -1) ? SPECIAL_CALIBRATION : DIAGONAL_CALIBRATION) | TWO_PASS; |
| 430 |
} |
| 431 |
|
| 432 |
//****************************************************************************// |
| 433 |
//**************************** Simulation Main *****************************// |
| 434 |
//****************************************************************************// |
| 435 |
/* |
| 436 |
Command line parameters |
| 437 |
|
| 438 |
General parameters |
| 439 |
-t : filename tag |
| 440 |
-ft : filename tag - supersedes -t |
| 441 |
-outtag : filename tag - supersedes -t and -ft |
| 442 |
|
| 443 |
Adaptive jump ratio parameters |
| 444 |
-cs <real>: target acceptance rate for single variable calibrations (default = 0.20) |
| 445 |
-ca <real> : target acceptance rate for overall calibrations (default = 0.25) |
| 446 |
-ps <integer> : the initial period for the single variable calibrations (default = 20) |
| 447 |
-pa <integer> : the initial period for the overall calibrations (default = 20) |
| 448 |
-mps <integer> : the maximum period for the single variable calibrations (default = 8*ps) |
| 449 |
-mpa <integer> : the maximum period for the overall calibrations (default = 16*mps) |
| 450 |
-diagonal : Forces calibration using two-pass diagonal, even if calibration has alread been performed. |
| 451 |
This is the default option. |
| 452 |
-hessian : Forces calibration using two-pass hessian, even if calibration has already been performed. |
| 453 |
-variance <filename> : Forces calibration using two-pass variance, even if calibration has already |
| 454 |
been performed. Reads draws from filename. |
| 455 |
-special : Forces calibration using two-pass with custom directions (calls SetupDirections()), even if |
| 456 |
calibration has already been performed. |
| 457 |
-af <filename> : file for storing the calibrated metropolis parameters. (default = metropolis_theta_<tag>.out) |
| 458 |
-hf <filename> : file containing the hessian matrix. (default = outdataout_<tag>.prn) |
| 459 |
|
| 460 |
Simulation parameters |
| 461 |
-ndraws : number of draws to save (default = 1000) |
| 462 |
-burnin : number of burn-in draws (default = 0.1 * ndraws) |
| 463 |
-thin : thinning factor. Total number of draws made is thin*ndraws + burnin (default = 1) |
| 464 |
-of <filename> : output filename (default = simulation_<tag>.out) |
| 465 |
-random <filename> : Reads previous simulation output and randomly selects a starting value. |
| 466 |
-database : Saves draws to database - not currently implemented |
| 467 |
|
| 468 |
Attempts to setup metropolis parameters from command line for simulating theta. Note if none of -diagonal, |
| 469 |
-hessian, or -special are specified, the default behavior is to first look for calibration file and it is |
| 470 |
not present to use the two-pass diagonal method. |
| 471 |
*/ |
| 472 |
void dw_simulate_command_line(int n_args, char **args, TStateModel *model, TVector start_value, int generator_init) |
| 473 |
{ |
| 474 |
FILE *f_out, *f_start; |
| 475 |
char *tag, *default_filename, *filename, *fmt; |
| 476 |
int j, row, ndraws, burn_in, thin, default_period=20, n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); |
| 477 |
TMatrix draws; |
| 478 |
TVector draw; |
| 479 |
PRECISION logposterior, loglikelihood; |
| 480 |
|
| 481 |
// Initialize generator |
| 482 |
dw_initialize_generator(generator_init); |
| 483 |
|
| 484 |
// Get tag from command line |
| 485 |
tag=dw_ParseString(n_args,args,'t',(char*)NULL); |
| 486 |
tag=dw_ParseString_String(n_args,args,"ft",tag); |
| 487 |
tag=dw_ParseString_String(n_args,args,"outtag",tag); |
| 488 |
|
| 489 |
// Set maximum posterior and write initial value |
| 490 |
model->max_log_posterior=logposterior=LogPosterior_StatesIntegratedOut(model); |
| 491 |
loglikelihood=LogLikelihood_StatesIntegratedOut(model); |
| 492 |
if (model->max_posterior_theta) ConvertThetaToFreeParameters(model,pElementV(model->max_posterior_theta)); |
| 493 |
if (model->max_posterior_q) ConvertQToFreeParameters(model,pElementV(model->max_posterior_q)); |
| 494 |
fmt="simulation_startmax_%s.out"; |
| 495 |
sprintf(filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 496 |
if (!(f_start=fopen(filename,"wt"))) |
| 497 |
printf("Unable to open %s\n",filename); |
| 498 |
else |
| 499 |
{ |
| 500 |
fprintf(f_start,"%.14le %.14le",logposterior,loglikelihood); |
| 501 |
if (model->max_posterior_theta) |
| 502 |
for (j=0; j < n_theta; j++) fprintf(f_start," %.14le",ElementV(model->max_posterior_theta,j)); |
| 503 |
if (model->max_posterior_q) |
| 504 |
dw_PrintVector(f_start,model->max_posterior_q," %.14le"); |
| 505 |
else |
| 506 |
fprintf(f_start,"\n"); |
| 507 |
} |
| 508 |
dw_free(filename); |
| 509 |
|
| 510 |
// Push starting value into model |
| 511 |
if (start_value) |
| 512 |
{ |
| 513 |
if (n_theta) ConvertFreeParametersToTheta(model,pElementV(start_value)); |
| 514 |
if (n_q) ConvertFreeParametersToQ(model,pElementV(start_value)+n_theta); |
| 515 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 516 |
loglikelihood=LogLikelihood_StatesIntegratedOut(model); |
| 517 |
if (f_start) |
| 518 |
{ |
| 519 |
fprintf(f_start,"%.14le %.14le",logposterior,loglikelihood); |
| 520 |
dw_PrintVector(f_start,start_value," %.14le"); |
| 521 |
} |
| 522 |
if (logposterior > model->max_log_posterior) |
| 523 |
{ |
| 524 |
model->max_log_posterior=logposterior; |
| 525 |
if (model->max_posterior_theta) ConvertThetaToFreeParameters(model,pElementV(model->max_posterior_theta)); |
| 526 |
if (model->max_posterior_q) ConvertQToFreeParameters(model,pElementV(model->max_posterior_q)); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// Random starting values |
| 531 |
if (filename=dw_ParseString_String(n_args,args,"random",(char*)NULL)) |
| 532 |
if (draws=dw_ReadPosteriorDraws((FILE*)NULL,filename,(char*)NULL,n_theta+n_q)) |
| 533 |
{ |
| 534 |
row=(int)floor(dw_uniform_rnd()*RowM(draws)); |
| 535 |
if (row >= RowM(draws)) row=RowM(draws); |
| 536 |
draw=RowVector((TVector)NULL,draws,row); |
| 537 |
if (n_theta) ConvertFreeParametersToTheta(model,pElementV(draw)+2); |
| 538 |
if (n_q) ConvertFreeParametersToQ(model,pElementV(draw)+2+n_theta); |
| 539 |
printf("Using row %d as random starting value from %s\n",row,filename); |
| 540 |
printf("initial log posterior = %le - %le\n",logposterior=LogPosterior_StatesIntegratedOut(model),ElementV(draw,0)); |
| 541 |
printf("initial log likelihood = %le - %le\n",loglikelihood=LogLikelihood_StatesIntegratedOut(model),ElementV(draw,1)); |
| 542 |
ElementV(draw,0)=logposterior; |
| 543 |
ElementV(draw,1)=loglikelihood; |
| 544 |
if (f_start) dw_PrintVector(f_start,draw," %.14le"); |
| 545 |
if (logposterior > model->max_log_posterior) |
| 546 |
{ |
| 547 |
model->max_log_posterior=logposterior; |
| 548 |
if (model->max_posterior_theta) ConvertThetaToFreeParameters(model,pElementV(model->max_posterior_theta)); |
| 549 |
if (model->max_posterior_q) ConvertQToFreeParameters(model,pElementV(model->max_posterior_q)); |
| 550 |
} |
| 551 |
FreeVector(draw); |
| 552 |
FreeMatrix(draws); |
| 553 |
} |
| 554 |
else |
| 555 |
{ |
| 556 |
printf("Unable to open %s\n",filename); |
| 557 |
dw_exit(0); |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
printf("initial log posterior = %le\n",logposterior); |
| 562 |
printf("initial log likelihood = %le\n",loglikelihood); |
| 563 |
} |
| 564 |
|
| 565 |
if (SetUpMetropolisSimulation(n_args,args,tag,model,default_period)) |
| 566 |
{ |
| 567 |
// simulation parameters |
| 568 |
ndraws=dw_ParseInteger_String(n_args,args,"ndraws",1000); |
| 569 |
burn_in=dw_ParseInteger_String(n_args,args,"burnin",ndraws/10); |
| 570 |
thin=dw_ParseInteger_String(n_args,args,"thin",1); |
| 571 |
|
| 572 |
// Starting Simulation |
| 573 |
if (dw_FindArgument_String(n_args,args,"database") == -1) |
| 574 |
{ |
| 575 |
// output filename |
| 576 |
fmt="simulation_%s.out"; |
| 577 |
sprintf(default_filename=(char*)dw_malloc(strlen(fmt) + strlen(tag) - 1),fmt,tag); |
| 578 |
filename=dw_ParseString_String(n_args,args,"fo",default_filename); |
| 579 |
f_out=fopen(filename,"wt"); |
| 580 |
dw_free(default_filename); |
| 581 |
} |
| 582 |
else |
| 583 |
{ |
| 584 |
f_out=(FILE*)NULL; |
| 585 |
// initialize sdsm |
| 586 |
//sdsm_init(1001,100,100,1,0,1,1); |
| 587 |
printf("-database not yet supported\n"); |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
Simulate(burn_in,ndraws,thin,1000,model,f_out); |
| 592 |
|
| 593 |
if (f_start) |
| 594 |
{ |
| 595 |
fprintf(f_start,"%.14le %.14le",model->max_log_posterior,loglikelihood); |
| 596 |
if (model->max_posterior_theta) |
| 597 |
for (j=0; j < n_theta; j++) fprintf(f_start," %.14le",ElementV(model->max_posterior_theta,j)); |
| 598 |
if (model->max_posterior_q) |
| 599 |
dw_PrintVector(f_start,model->max_posterior_q," %.14le"); |
| 600 |
else |
| 601 |
fprintf(f_start,"\n"); |
| 602 |
fclose(f_start); |
| 603 |
} |
| 604 |
|
| 605 |
if (f_out) fclose(f_out); |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
printf("Error setting up Metropolis adaptive simulation\n"); |
| 610 |
if (f_start) fclose(f_start); |
| 611 |
} |
| 612 |
} |