| 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_metropolis_theta.h" |
| 19 |
#include "dw_rand.h" |
| 20 |
#include "dw_matrix_rand.h" |
| 21 |
#include "dw_matrix_array.h" |
| 22 |
#include "dw_std.h" |
| 23 |
|
| 24 |
#include <stdlib.h> |
| 25 |
#include <memory.h> |
| 26 |
#include <math.h> |
| 27 |
#include <time.h> |
| 28 |
|
| 29 |
//-----------------------------------------------------------------------------// |
| 30 |
//----------------------------- Metropolis Draws ------------------------------// |
| 31 |
//-----------------------------------------------------------------------------// |
| 32 |
TMetropolis_theta* Create_metropolis_theta(int n_parameters) |
| 33 |
{ |
| 34 |
TMetropolis_theta *metropolis=(TMetropolis_theta*)dw_malloc(sizeof(TMetropolis_theta)); |
| 35 |
|
| 36 |
metropolis->n_parameters=n_parameters; |
| 37 |
|
| 38 |
metropolis->n_blocks=0; |
| 39 |
metropolis->block_dims=(int*)NULL; |
| 40 |
metropolis->block_offsets=(int*)NULL; |
| 41 |
metropolis->draws=(int*)NULL; |
| 42 |
metropolis->jumps=(int*)NULL; |
| 43 |
|
| 44 |
InitializeVector(metropolis->scale=CreateVector(n_parameters),1.0); |
| 45 |
metropolis->direction=dw_CreateArray_vector(n_parameters); |
| 46 |
|
| 47 |
return metropolis; |
| 48 |
} |
| 49 |
|
| 50 |
void Free_metropolis_theta(TMetropolis_theta *metropolis) |
| 51 |
{ |
| 52 |
if (metropolis) |
| 53 |
{ |
| 54 |
if (metropolis->block_dims) dw_free(metropolis->block_dims); |
| 55 |
if (metropolis->block_offsets) dw_free(metropolis->block_offsets); |
| 56 |
if (metropolis->draws) dw_free(metropolis->draws); |
| 57 |
if (metropolis->jumps) dw_free(metropolis->jumps); |
| 58 |
FreeVector(metropolis->scale); |
| 59 |
dw_FreeArray(metropolis->direction); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
Sets the metropolis scale and directions. |
| 65 |
*/ |
| 66 |
void Setup_metropolis_theta(TMetropolis_theta *metropolis, TVector scale, TVector *direction) |
| 67 |
{ |
| 68 |
EquateVector(metropolis->scale,scale); |
| 69 |
dw_CopyArray(metropolis->direction,direction); |
| 70 |
} |
| 71 |
|
| 72 |
/* |
| 73 |
Sets the metropolis scale and directions. The scales are set to one and the |
| 74 |
directions are set to null, which implies that the ith direction vector is the |
| 75 |
ith column of the identity matrix. |
| 76 |
*/ |
| 77 |
void Setup_metropolis_theta_diagonal(TMetropolis_theta *metropolis) |
| 78 |
{ |
| 79 |
int i; |
| 80 |
for (i=metropolis->n_parameters-1; i >= 0; i--) |
| 81 |
{ |
| 82 |
ElementV(metropolis->scale,i)=1.0; |
| 83 |
if (metropolis->direction[i]) |
| 84 |
{ |
| 85 |
FreeVector(metropolis->direction[i]); |
| 86 |
metropolis->direction[i]=(TVector)NULL; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
Sets the metropolis scale and directions. The directions and scales are |
| 93 |
computed from the eigenvalue-eigenvector decomposition of the hessian matrix. |
| 94 |
The matrix hessian is assumed to be n_parameters x n_parameters. |
| 95 |
*/ |
| 96 |
void Setup_metropolis_theta_hessian(TMetropolis_theta *metropolis, TMatrix hessian) |
| 97 |
{ |
| 98 |
TMatrix U, V; |
| 99 |
TVector D; |
| 100 |
int i,j, n=metropolis->n_parameters; |
| 101 |
PRECISION ratio; |
| 102 |
|
| 103 |
if (!SVD(U=CreateMatrix(n,n),D=CreateVector(n),V=CreateMatrix(n,n),hessian)) |
| 104 |
{ |
| 105 |
printf("Error SetupMetropolisTheta_hessian(): unable to compute SVD\n"); |
| 106 |
dw_exit(0); |
| 107 |
} |
| 108 |
|
| 109 |
for (i=0; i < n; i++) |
| 110 |
for (j=0; j < n; j++) |
| 111 |
{ |
| 112 |
ratio=(fabs(ElementM(U,i,j)) > fabs(ElementM(V,i,j))) ? ElementM(V,i,j)/ElementM(U,i,j) : ElementM(U,i,j)/ElementM(V,i,j); |
| 113 |
if (ratio < 0.99) |
| 114 |
{ |
| 115 |
printf("Error SetupMetropolisTheta_hessian(): U not equal to V\n"); |
| 116 |
printf("U(%d,%d) = %lf V(%d,%d) = %lf\n %le",i,j,ElementM(U,i,j),i,j,ElementM(V,i,j),ratio); |
| 117 |
dw_exit(0); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
for (i=0; i < n; i++) |
| 122 |
{ |
| 123 |
ElementV(metropolis->scale,i) = (ElementV(D,i) < 1.0E-6) ? 1.0E3 |
| 124 |
: (ElementV(D,i) > 1.0E8) ? 1.0E-4 : 1.0/sqrt(ElementV(D,i)); |
| 125 |
metropolis->direction[i]=ColumnVector(metropolis->direction[i],U,i); |
| 126 |
} |
| 127 |
|
| 128 |
FreeMatrix(U); |
| 129 |
FreeMatrix(V); |
| 130 |
FreeVector(D); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
Sets the metropolis scale and directions. The directions and scales are |
| 135 |
computed from the eigenvalue-eigenvector decomposition of the variance matrix. |
| 136 |
The matrix variance is assumed to be n_parameters x n_parameters. |
| 137 |
*/ |
| 138 |
void Setup_metropolis_theta_variance(TMetropolis_theta *metropolis, TMatrix variance) |
| 139 |
{ |
| 140 |
TMatrix U, V; |
| 141 |
TVector D; |
| 142 |
int i,j, n=metropolis->n_parameters; |
| 143 |
PRECISION ratio; |
| 144 |
|
| 145 |
if (!SVD(U=CreateMatrix(n,n),D=CreateVector(n),V=CreateMatrix(n,n),variance)) |
| 146 |
{ |
| 147 |
printf("Error SetupMetropolisTheta_variance(): unable to compute SVD\n"); |
| 148 |
dw_exit(0); |
| 149 |
} |
| 150 |
|
| 151 |
for (i=0; i < n; i++) |
| 152 |
for (j=0; j < n; j++) |
| 153 |
{ |
| 154 |
ratio=(fabs(ElementM(U,i,j)) > fabs(ElementM(V,i,j))) ? ElementM(V,i,j)/ElementM(U,i,j) : ElementM(U,i,j)/ElementM(V,i,j); |
| 155 |
if (ratio < 0.99) |
| 156 |
{ |
| 157 |
printf("Error SetupMetropolisTheta_variance(): U not equal to V\n"); |
| 158 |
printf("U(%d,%d) = %lf V(%d,%d) = %lf\n %le",i,j,ElementM(U,i,j),i,j,ElementM(V,i,j),ratio); |
| 159 |
dw_exit(0); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
for (i=0; i < n; i++) |
| 164 |
{ |
| 165 |
ElementV(metropolis->scale,i)=sqrt(ElementV(D,i)); |
| 166 |
metropolis->direction[i]=ColumnVector(metropolis->direction[i],U,i); |
| 167 |
} |
| 168 |
|
| 169 |
FreeMatrix(U); |
| 170 |
FreeMatrix(V); |
| 171 |
FreeVector(D); |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
Creates a block structure for metropolis draws. The dimensions are given by |
| 176 |
the integer array dims. The array must be of length n_blocks and the sum of |
| 177 |
the dimensions must be equal to n_parameters. |
| 178 |
*/ |
| 179 |
void Setup_metropolis_theta_blocks(TMetropolis_theta *metropolis, int n_blocks, int *dims) |
| 180 |
{ |
| 181 |
int i; |
| 182 |
if (metropolis->block_dims) dw_free(metropolis->block_dims); |
| 183 |
if (metropolis->block_offsets) dw_free(metropolis->block_offsets); |
| 184 |
if (metropolis->draws) dw_free(metropolis->draws); |
| 185 |
if (metropolis->jumps) dw_free(metropolis->jumps); |
| 186 |
|
| 187 |
metropolis->n_blocks=n_blocks; |
| 188 |
metropolis->block_dims=(int*)dw_malloc(n_blocks*sizeof(int)); |
| 189 |
metropolis->block_offsets=(int*)dw_malloc((n_blocks+1)*sizeof(int)); |
| 190 |
metropolis->draws=(int*)dw_malloc(n_blocks*sizeof(int)); |
| 191 |
metropolis->jumps=(int*)dw_malloc(n_blocks*sizeof(int)); |
| 192 |
|
| 193 |
for (metropolis->block_offsets[0]=i=0; i < n_blocks; i++) |
| 194 |
{ |
| 195 |
metropolis->draws[i]=metropolis->jumps[i]=0; |
| 196 |
metropolis->block_dims[i]=dims[i]; |
| 197 |
metropolis->block_offsets[i+1]=metropolis->block_offsets[i]+dims[i]; |
| 198 |
} |
| 199 |
|
| 200 |
if (metropolis->block_offsets[n_blocks] != metropolis->n_parameters) |
| 201 |
{ |
| 202 |
printf("Error SetupMetropolisThetaBlocks(): invalid block dimensions\n"); |
| 203 |
dw_exit(0); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
Creates a block structure for metropolis draws. There are n_parameters |
| 209 |
blocks, each of dimension one. |
| 210 |
*/ |
| 211 |
void Setup_metropolis_theta_blocks_full(TMetropolis_theta *metropolis) |
| 212 |
{ |
| 213 |
int i; |
| 214 |
if (metropolis->block_dims) dw_free(metropolis->block_dims); |
| 215 |
if (metropolis->block_offsets) dw_free(metropolis->block_offsets); |
| 216 |
if (metropolis->draws) dw_free(metropolis->draws); |
| 217 |
if (metropolis->jumps) dw_free(metropolis->jumps); |
| 218 |
|
| 219 |
metropolis->n_blocks=metropolis->n_parameters; |
| 220 |
metropolis->block_dims=(int*)dw_malloc(metropolis->n_parameters*sizeof(int)); |
| 221 |
metropolis->block_offsets=(int*)dw_malloc((metropolis->n_parameters + 1)*sizeof(int)); |
| 222 |
metropolis->draws=(int*)dw_malloc(metropolis->n_parameters*sizeof(int)); |
| 223 |
metropolis->jumps=(int*)dw_malloc(metropolis->n_parameters*sizeof(int)); |
| 224 |
|
| 225 |
for (i=0; i < metropolis->n_parameters; i++) |
| 226 |
{ |
| 227 |
metropolis->draws[i]=metropolis->jumps[i]=0; |
| 228 |
metropolis->block_dims[i]=1; |
| 229 |
metropolis->block_offsets[i]=i; |
| 230 |
} |
| 231 |
metropolis->block_offsets[metropolis->n_parameters]=metropolis->n_parameters; |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
Creates a block structure for metropolis draws. There one block of dimension |
| 236 |
n_parameters. |
| 237 |
*/ |
| 238 |
void Setup_metropolis_theta_blocks_single(TMetropolis_theta *metropolis) |
| 239 |
{ |
| 240 |
if (metropolis->block_dims) dw_free(metropolis->block_dims); |
| 241 |
if (metropolis->block_offsets) dw_free(metropolis->block_offsets); |
| 242 |
if (metropolis->draws) dw_free(metropolis->draws); |
| 243 |
if (metropolis->jumps) dw_free(metropolis->jumps); |
| 244 |
|
| 245 |
metropolis->n_blocks=1; |
| 246 |
metropolis->block_dims=(int*)dw_malloc(sizeof(int)); |
| 247 |
metropolis->block_offsets=(int*)dw_malloc(2*sizeof(int)); |
| 248 |
metropolis->draws=(int*)dw_malloc(sizeof(int)); |
| 249 |
metropolis->jumps=(int*)dw_malloc(sizeof(int)); |
| 250 |
|
| 251 |
metropolis->draws[0]=metropolis->jumps[0]=0; |
| 252 |
metropolis->block_offsets[0]=0; |
| 253 |
metropolis->block_offsets[1]=metropolis->block_dims[0]=metropolis->n_parameters; |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
Resets Metropolis counts. |
| 258 |
*/ |
| 259 |
void ResetCounts_metropolis_theta(TMetropolis_theta *metropolis) |
| 260 |
{ |
| 261 |
int i; |
| 262 |
for (i=metropolis->n_blocks-1; i >= 0; i--) |
| 263 |
metropolis->draws[i]=metropolis->jumps[i]=0; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
/******************************************************************************/ |
| 268 |
/******************************************************************************/ |
| 269 |
/******************************************************************************/ |
| 270 |
/* |
| 271 |
Assumes: |
| 272 |
model: pointer to valid TStateModel structure with model->theta a TMixture |
| 273 |
structure. |
| 274 |
submodel : a submodel of model->theta |
| 275 |
logposterior_old : log posterior value of current parameters |
| 276 |
|
| 277 |
Results: |
| 278 |
A metropolis draw of theta is stored in submodel. |
| 279 |
|
| 280 |
Returns: |
| 281 |
The log posterior value of current draw. |
| 282 |
*/ |
| 283 |
PRECISION draw_metropolis_theta(TStateModel *model, PRECISION logposterior_old) |
| 284 |
{ |
| 285 |
// TStateModel specific calls |
| 286 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 287 |
|
| 288 |
int i, k, n=metropolis->n_parameters; |
| 289 |
TVector theta, theta_old; |
| 290 |
PRECISION logposterior; |
| 291 |
|
| 292 |
theta_old=CreateVector(n); |
| 293 |
theta=CreateVector(n); |
| 294 |
|
| 295 |
// TStateModel specific calls |
| 296 |
ConvertThetaToFreeParameters(model,pElementV(theta_old)); |
| 297 |
|
| 298 |
EquateVector(theta,theta_old); |
| 299 |
|
| 300 |
for (k=i=0; i < metropolis->n_blocks; i++) |
| 301 |
{ |
| 302 |
for ( ; k < metropolis->block_offsets[i+1]; k++) |
| 303 |
if (metropolis->direction[k]) |
| 304 |
UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]); |
| 305 |
else |
| 306 |
ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k); |
| 307 |
|
| 308 |
// TStateModel specific calls |
| 309 |
ConvertFreeParametersToTheta(model,pElementV(theta)); |
| 310 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 311 |
|
| 312 |
if (log(dw_uniform_rnd()) <= logposterior - logposterior_old) |
| 313 |
{ |
| 314 |
metropolis->jumps[i]++; |
| 315 |
if (i < metropolis->n_blocks-1) |
| 316 |
{ |
| 317 |
EquateVector(theta_old,theta); |
| 318 |
logposterior_old=logposterior; |
| 319 |
} |
| 320 |
} |
| 321 |
else |
| 322 |
if (i < metropolis->n_blocks-1) |
| 323 |
EquateVector(theta,theta_old); |
| 324 |
else |
| 325 |
{ |
| 326 |
// TStateModel specific calls |
| 327 |
ConvertFreeParametersToTheta(model,pElementV(theta_old)); |
| 328 |
} |
| 329 |
|
| 330 |
metropolis->draws[i]++; |
| 331 |
} |
| 332 |
|
| 333 |
FreeVector(theta); |
| 334 |
FreeVector(theta_old); |
| 335 |
|
| 336 |
return logposterior_old; |
| 337 |
} |
| 338 |
|
| 339 |
/* |
| 340 |
Assumes: |
| 341 |
model: pointer to valid TStateModel structure with model->theta a TMixture |
| 342 |
structure. |
| 343 |
submodel : a submodel of model->theta |
| 344 |
logposterior : log posterior value of current parameters |
| 345 |
|
| 346 |
Results: |
| 347 |
A metropolis draw of theta is stored in submodel. |
| 348 |
|
| 349 |
Returns: |
| 350 |
The log posterior value of current draw. |
| 351 |
*/ |
| 352 |
PRECISION draw_metropolis(TStateModel *model, PRECISION logposterior) |
| 353 |
{ |
| 354 |
// Draw transition matrix parameters and compute log posterior |
| 355 |
DrawStates(model); |
| 356 |
DrawTransitionMatrixParameters(model); |
| 357 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 358 |
|
| 359 |
// Draw theta |
| 360 |
logposterior=draw_metropolis_theta(model,logposterior); |
| 361 |
|
| 362 |
// Record max posterior value |
| 363 |
if (logposterior > model->max_log_posterior) |
| 364 |
{ |
| 365 |
model->max_log_posterior=logposterior; |
| 366 |
if (model->max_posterior_theta) ConvertThetaToFreeParameters(model,pElementV(model->max_posterior_theta)); |
| 367 |
if (model->max_posterior_q) ConvertQToFreeParameters(model,pElementV(model->max_posterior_q)); |
| 368 |
} |
| 369 |
|
| 370 |
return logposterior; |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
Assumes: |
| 375 |
model: pointer to valid TStateModel structure. |
| 376 |
|
| 377 |
Results: |
| 378 |
A metropolis draw of theta is stored in model |
| 379 |
*/ |
| 380 |
void Draw_metropolis_theta(TStateModel *model) |
| 381 |
{ |
| 382 |
// TStateModel specific calls |
| 383 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 384 |
|
| 385 |
int i, k, n=metropolis->n_parameters; |
| 386 |
TVector theta, theta_old; |
| 387 |
PRECISION logposterior, logposterior_old; |
| 388 |
|
| 389 |
theta_old=CreateVector(n); |
| 390 |
theta=CreateVector(n); |
| 391 |
|
| 392 |
// TStateModel specific calls |
| 393 |
logposterior_old=LogPosterior_StatesIntegratedOut(model); |
| 394 |
ConvertThetaToFreeParameters(model,pElementV(theta_old)); |
| 395 |
|
| 396 |
EquateVector(theta,theta_old); |
| 397 |
|
| 398 |
for (k=i=0; i < metropolis->n_blocks; i++) |
| 399 |
{ |
| 400 |
for ( ; k < metropolis->block_offsets[i+1]; k++) |
| 401 |
if (metropolis->direction[k]) |
| 402 |
UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]); |
| 403 |
else |
| 404 |
ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k); |
| 405 |
|
| 406 |
// TStateModel specific calls |
| 407 |
ConvertFreeParametersToTheta(model,pElementV(theta)); |
| 408 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 409 |
|
| 410 |
if (log(dw_uniform_rnd()) <= logposterior - logposterior_old) |
| 411 |
{ |
| 412 |
metropolis->jumps[i]++; |
| 413 |
if (i < metropolis->n_blocks-1) |
| 414 |
{ |
| 415 |
EquateVector(theta_old,theta); |
| 416 |
logposterior_old=logposterior; |
| 417 |
} |
| 418 |
} |
| 419 |
else |
| 420 |
if (i < metropolis->n_blocks-1) |
| 421 |
EquateVector(theta,theta_old); |
| 422 |
else |
| 423 |
// TStateModel specific calls |
| 424 |
ConvertFreeParametersToTheta(model,pElementV(theta_old)); |
| 425 |
|
| 426 |
metropolis->draws[i]++; |
| 427 |
} |
| 428 |
|
| 429 |
FreeVector(theta); |
| 430 |
FreeVector(theta_old); |
| 431 |
} |
| 432 |
|
| 433 |
/* |
| 434 |
Assumes: |
| 435 |
model: pointer to valid TStateModel structure. |
| 436 |
|
| 437 |
Results: |
| 438 |
A metropolis draw of theta |
| 439 |
*/ |
| 440 |
void Draw_metropolis_theta_single_block(int block, TStateModel *model) |
| 441 |
{ |
| 442 |
// TStateModel specific calls |
| 443 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 444 |
|
| 445 |
int k, n=metropolis->n_parameters; |
| 446 |
TVector theta, theta_old; |
| 447 |
PRECISION logposterior, logposterior_old; |
| 448 |
|
| 449 |
theta_old=CreateVector(n); |
| 450 |
theta=CreateVector(n); |
| 451 |
|
| 452 |
// TStateModel specific calls |
| 453 |
logposterior_old=LogPosterior_StatesIntegratedOut(model); |
| 454 |
ConvertThetaToFreeParameters(model,pElementV(theta_old)); |
| 455 |
|
| 456 |
EquateVector(theta,theta_old); |
| 457 |
|
| 458 |
for (k=metropolis->block_offsets[block]; k < metropolis->block_offsets[block+1]; k++) |
| 459 |
if (metropolis->direction[k]) |
| 460 |
UpdateV(1.0,theta,dw_gaussian_rnd()*ElementV(metropolis->scale,k),metropolis->direction[k]); |
| 461 |
else |
| 462 |
ElementV(theta,k)+=dw_gaussian_rnd()*ElementV(metropolis->scale,k); |
| 463 |
|
| 464 |
// TStateModel specific calls |
| 465 |
ConvertFreeParametersToTheta(model,pElementV(theta)); |
| 466 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 467 |
|
| 468 |
if (log(dw_uniform_rnd()) <= logposterior - logposterior_old) |
| 469 |
metropolis->jumps[block]++; |
| 470 |
else |
| 471 |
// TStateModel specific calls |
| 472 |
ConvertFreeParametersToTheta(model,pElementV(theta_old)); |
| 473 |
|
| 474 |
metropolis->draws[block]++; |
| 475 |
|
| 476 |
FreeVector(theta); |
| 477 |
FreeVector(theta_old); |
| 478 |
} |
| 479 |
/*******************************************************************************/ |
| 480 |
/*******************************************************************************/ |
| 481 |
/*******************************************************************************/ |
| 482 |
|
| 483 |
|
| 484 |
/*******************************************************************************/ |
| 485 |
/****************************** Adaptive Scaling *******************************/ |
| 486 |
/*******************************************************************************/ |
| 487 |
TAdaptive* CreateAdaptive(PRECISION mid) |
| 488 |
{ |
| 489 |
TAdaptive *adaptive=(TAdaptive*)dw_malloc(sizeof(TAdaptive)); |
| 490 |
|
| 491 |
if ((mid <= 0.0) || (mid >= 1.0)) |
| 492 |
{ |
| 493 |
printf("SetAdaptiveCenter(): center out of range\n"); |
| 494 |
dw_exit(0); |
| 495 |
} |
| 496 |
|
| 497 |
adaptive->mid=mid; |
| 498 |
adaptive->log_mid=log(mid); |
| 499 |
adaptive->lower_bound=exp(adaptive->log_mid/0.2); |
| 500 |
adaptive->upper_bound=exp(adaptive->log_mid/5.0); |
| 501 |
|
| 502 |
return adaptive; |
| 503 |
} |
| 504 |
|
| 505 |
void SetAdaptiveCenter(PRECISION mid, TAdaptive *adaptive) |
| 506 |
{ |
| 507 |
if ((mid <= 0.0) || (mid >= 1.0)) |
| 508 |
{ |
| 509 |
printf("SetAdaptiveCenter(): center out of range\n"); |
| 510 |
dw_exit(0); |
| 511 |
} |
| 512 |
|
| 513 |
adaptive->mid=mid; |
| 514 |
adaptive->log_mid=log(mid); |
| 515 |
adaptive->lower_bound=exp(adaptive->log_mid/0.2); |
| 516 |
adaptive->upper_bound=exp(adaptive->log_mid/5.0); |
| 517 |
} |
| 518 |
|
| 519 |
void InitializeAdaptive(int period, int block, TAdaptive *adaptive, TMetropolis_theta *metropolis) |
| 520 |
{ |
| 521 |
adaptive->period=period; |
| 522 |
adaptive->end_draws=metropolis->draws[block] + period; |
| 523 |
adaptive->begin_draws=metropolis->draws[block]; |
| 524 |
adaptive->begin_jumps=metropolis->jumps[block]=0; |
| 525 |
adaptive->best_scale=adaptive->scale=1.0; |
| 526 |
adaptive->previous_ratio=0.0; |
| 527 |
adaptive->low_scale=adaptive->low_jump_ratio=adaptive->high_scale=adaptive->high_jump_ratio=-1.0; |
| 528 |
} |
| 529 |
|
| 530 |
PRECISION RecomputeScale(int end_jumps, int end_draws, TAdaptive *adaptive) |
| 531 |
{ |
| 532 |
PRECISION new_scale, diff; |
| 533 |
|
| 534 |
// Compute jump ratio |
| 535 |
adaptive->previous_ratio=(PRECISION)(end_jumps - adaptive->begin_jumps)/(PRECISION)(end_draws - adaptive->begin_draws); |
| 536 |
|
| 537 |
// Recompute scale? |
| 538 |
if (adaptive->end_draws > end_draws) return adaptive->scale; |
| 539 |
|
| 540 |
// Set new low or high bounds |
| 541 |
if (adaptive->previous_ratio < adaptive->mid) |
| 542 |
{ |
| 543 |
adaptive->low_scale=adaptive->scale; |
| 544 |
adaptive->low_jump_ratio=adaptive->previous_ratio; |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
adaptive->high_scale=adaptive->scale; |
| 549 |
adaptive->high_jump_ratio=adaptive->previous_ratio; |
| 550 |
} |
| 551 |
|
| 552 |
// Compute new scale and best scale |
| 553 |
if (adaptive->low_jump_ratio < 0.0) |
| 554 |
{ |
| 555 |
adaptive->best_scale=adaptive->scale; |
| 556 |
new_scale=((adaptive->previous_ratio > adaptive->upper_bound) ? 5.0 |
| 557 |
: adaptive->log_mid/log(adaptive->previous_ratio))*adaptive->high_scale; |
| 558 |
} |
| 559 |
else |
| 560 |
if (adaptive->high_jump_ratio < 0.0) |
| 561 |
{ |
| 562 |
adaptive->best_scale=adaptive->scale; |
| 563 |
new_scale=((adaptive->previous_ratio < adaptive->lower_bound) ? 0.2 |
| 564 |
: adaptive->log_mid/log(adaptive->previous_ratio))*adaptive->low_scale; |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
new_scale=adaptive->best_scale=((diff=adaptive->high_jump_ratio - adaptive->low_jump_ratio) > 1.0e-6) |
| 569 |
? ((adaptive->mid - adaptive->low_jump_ratio)*adaptive->low_scale |
| 570 |
+ (adaptive->high_jump_ratio - adaptive->mid)*adaptive->high_scale)/diff |
| 571 |
: 0.5*(adaptive->low_scale + adaptive->high_scale); |
| 572 |
adaptive->period*=2; |
| 573 |
adaptive->low_jump_ratio=adaptive->high_jump_ratio=-1.0; |
| 574 |
} |
| 575 |
|
| 576 |
// Reset adaptive counts and scale |
| 577 |
adaptive->begin_jumps=end_jumps; |
| 578 |
adaptive->begin_draws=end_draws; |
| 579 |
adaptive->end_draws=end_draws + adaptive->period; |
| 580 |
adaptive->scale=new_scale; |
| 581 |
|
| 582 |
return new_scale; |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
Prints jumping info: |
| 587 |
block number (zero based) |
| 588 |
total jump ratio for all prevuous sub-runs |
| 589 |
jump ratio for previouus sub-run |
| 590 |
scale for current sub-run |
| 591 |
total number draws for current sub-run |
| 592 |
sub-run length |
| 593 |
|
| 594 |
*/ |
| 595 |
void PrintJumpingInfo(int b, TAdaptive *adaptive, TMetropolis_theta *metropolis) |
| 596 |
{ |
| 597 |
PRECISION total_jump_ratio=(PRECISION)(metropolis->jumps[b])/(PRECISION)(metropolis->draws[b]); |
| 598 |
printf("block: %d (%lf %lf %lf %d %d)\n",b,total_jump_ratio,adaptive->previous_ratio,adaptive->scale, |
| 599 |
metropolis->draws[b] - adaptive->begin_draws,adaptive->period); |
| 600 |
} |
| 601 |
|
| 602 |
void AdaptiveScale_metropolis_theta(TStateModel *model, PRECISION mid, int period, int max_period, int verbose) |
| 603 |
{ |
| 604 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 605 |
PRECISION new_scale, old_scale, rescale, logposterior; |
| 606 |
TAdaptive **adaptive; |
| 607 |
int done=0, i, k, begin_time=(int)time((time_t*)NULL), count=0, check=period; |
| 608 |
|
| 609 |
adaptive=(TAdaptive**)dw_CreateArray_pointer(metropolis->n_blocks,dw_free); |
| 610 |
for (k=metropolis->n_blocks-1; k >= 0; k--) |
| 611 |
InitializeAdaptive(period,k,adaptive[k]=CreateAdaptive(mid),metropolis); |
| 612 |
|
| 613 |
if (verbose) |
| 614 |
printf("Beginning adaptive burn in -- maximum period = %d.\n",max_period); |
| 615 |
|
| 616 |
logposterior=LogPosterior_StatesIntegratedOut(model); |
| 617 |
done=0; |
| 618 |
while (!done) |
| 619 |
{ |
| 620 |
count++; |
| 621 |
|
| 622 |
logposterior=draw_metropolis(model,logposterior); |
| 623 |
|
| 624 |
if (count == check) |
| 625 |
{ |
| 626 |
done=1; |
| 627 |
|
| 628 |
if (verbose) |
| 629 |
printf("\n%d iterations completed - elapsed time: %d seconds\n",count,(int)time((time_t*)NULL) - begin_time); |
| 630 |
|
| 631 |
for (k=metropolis->n_blocks-1; k >= 0; k--) |
| 632 |
{ |
| 633 |
old_scale=adaptive[k]->scale; |
| 634 |
new_scale=RecomputeScale(metropolis->jumps[k],metropolis->draws[k],adaptive[k]); |
| 635 |
if (new_scale != old_scale) |
| 636 |
for (rescale=new_scale/old_scale, i=metropolis->block_offsets[k]; i < metropolis->block_offsets[k+1]; i++) |
| 637 |
ElementV(metropolis->scale,i)*=rescale; |
| 638 |
if (adaptive[k]->period <= max_period) done=0; |
| 639 |
if (verbose) PrintJumpingInfo(k,adaptive[k],metropolis); |
| 640 |
} |
| 641 |
|
| 642 |
check+=period; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
for (i=k=0; k < metropolis->n_blocks; k++) |
| 647 |
if (adaptive[k]->scale != adaptive[k]->best_scale) |
| 648 |
for (rescale=adaptive[k]->best_scale/adaptive[k]->scale; i < metropolis->block_offsets[k+1]; i++) |
| 649 |
ElementV(metropolis->scale,i)*=rescale; |
| 650 |
|
| 651 |
dw_FreeArray(adaptive); |
| 652 |
} |
| 653 |
|
| 654 |
void AdaptiveScale_metropolis_theta_single_block(int block, TStateModel *model, PRECISION mid, int period, int max_period, int verbose) |
| 655 |
{ |
| 656 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 657 |
TAdaptive *adaptive; |
| 658 |
int done=0, i, begin_time=(int)time((time_t*)NULL), count=0, check=period; |
| 659 |
PRECISION new_scale, old_scale, rescale; |
| 660 |
|
| 661 |
if ((block < 0) || (block >= metropolis->n_blocks)) |
| 662 |
{ |
| 663 |
if (verbose) printf("AdaptiveScale_metropolis_theta_singleblock(): Invalid value for block\n"); |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
InitializeAdaptive(period,block,adaptive=CreateAdaptive(mid),metropolis); |
| 668 |
|
| 669 |
if (verbose) |
| 670 |
printf("Beginning adaptive burn in for block %d -- maximum period = %d.\n",block,max_period); |
| 671 |
|
| 672 |
while (!done) |
| 673 |
{ |
| 674 |
count++; |
| 675 |
|
| 676 |
DrawStates(model); |
| 677 |
DrawTransitionMatrixParameters(model); |
| 678 |
Draw_metropolis_theta_single_block(block,model); |
| 679 |
|
| 680 |
if (count == check) |
| 681 |
{ |
| 682 |
if (verbose) |
| 683 |
printf("\n%d iterations completed - elapsed time: %d seconds\n",count,(int)time((time_t*)NULL) - begin_time); |
| 684 |
|
| 685 |
old_scale=adaptive->scale; |
| 686 |
new_scale=RecomputeScale(metropolis->jumps[block],metropolis->draws[block],adaptive); |
| 687 |
if (new_scale != old_scale) |
| 688 |
for (rescale=new_scale/old_scale, i=metropolis->block_offsets[block]; i < metropolis->block_offsets[block+1]; i++) |
| 689 |
ElementV(metropolis->scale,i)*=rescale; |
| 690 |
if (adaptive->period > max_period) done=1; |
| 691 |
if (verbose) PrintJumpingInfo(block,adaptive,metropolis); |
| 692 |
|
| 693 |
check+=period; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
if (adaptive->scale != adaptive->best_scale) |
| 698 |
for (rescale=adaptive->best_scale/adaptive->scale, i=metropolis->block_offsets[block]; |
| 699 |
i < metropolis->block_offsets[block+1]; i++) |
| 700 |
ElementV(metropolis->scale,i)*=rescale; |
| 701 |
|
| 702 |
dw_free(adaptive); |
| 703 |
} |
| 704 |
|
| 705 |
/*******************************************************************************/ |
| 706 |
/*******************************************************************************/ |
| 707 |
/*******************************************************************************/ |
| 708 |
|
| 709 |
/* |
| 710 |
Assumes |
| 711 |
model : pointer to valid TStateModel structure |
| 712 |
center : target value for acceptance ration |
| 713 |
peroid : initial length of sub-runs to calibrate scales |
| 714 |
max_period : maximum length of sub-runs to calibrate scales |
| 715 |
verbose : non-zero for output to stdout |
| 716 |
|
| 717 |
Results |
| 718 |
The values of model->metropolis_scale are calibrated to obtain the |
| 719 |
desired acceptance ratio. |
| 720 |
*/ |
| 721 |
void Calibrate_metropolis_theta(TStateModel *model, PRECISION center, int period, int max_period, int verbose) |
| 722 |
{ |
| 723 |
int n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); |
| 724 |
TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL; |
| 725 |
|
| 726 |
// Save initial parameters |
| 727 |
if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta))); |
| 728 |
if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q))); |
| 729 |
|
| 730 |
// Calibrate |
| 731 |
AdaptiveScale_metropolis_theta(model,center,period,max_period,verbose); |
| 732 |
|
| 733 |
// Reset initial parameters |
| 734 |
if (n_theta) |
| 735 |
{ |
| 736 |
ConvertFreeParametersToTheta(model,pElementV(initial_theta)); |
| 737 |
FreeVector(initial_theta); |
| 738 |
} |
| 739 |
if (n_q) |
| 740 |
{ |
| 741 |
ConvertFreeParametersToQ(model,pElementV(initial_q)); |
| 742 |
FreeVector(initial_q); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
/* |
| 747 |
Assumes |
| 748 |
idx : 0 <= idx < NumberFreeParametersTheta(model) |
| 749 |
model : pointer to valid TStateModel structure |
| 750 |
center : target acceptance rate |
| 751 |
peroid : initial length of sub-runs to calibrate scale for direction idx |
| 752 |
max_period : maximum length of sub-runs to calibrate scale for direction idx |
| 753 |
verbose : non-zero for output to stdout |
| 754 |
|
| 755 |
Returns |
| 756 |
The total number of draws made. A single draw consists of a draw of the |
| 757 |
states, a draw of the transition matrix parameters, and a draw of theta. The |
| 758 |
draw of theta is only in the direction idx. |
| 759 |
|
| 760 |
Results |
| 761 |
The value of ElementV(model->metropolis_scale_theta,idx) is calibrated to |
| 762 |
obtain the desired acceptance ratio. |
| 763 |
*/ |
| 764 |
int Calibrate_metropolis_theta_single_direction(int idx, TStateModel *model, PRECISION center, int period, int max_period, int verbose) |
| 765 |
{ |
| 766 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 767 |
int *dims, n_draws, n_blocks, n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); |
| 768 |
TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL; |
| 769 |
|
| 770 |
// save initial parameters |
| 771 |
if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta))); |
| 772 |
if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q))); |
| 773 |
|
| 774 |
// Save old blocks |
| 775 |
n_blocks=metropolis->n_blocks; |
| 776 |
dims=(int*)dw_malloc(n_blocks*sizeof(int)); |
| 777 |
memcpy(dims,metropolis->block_dims,n_blocks*sizeof(int)); |
| 778 |
|
| 779 |
// Setup blocks |
| 780 |
Setup_metropolis_theta_blocks_full(metropolis); |
| 781 |
|
| 782 |
// Calibrate |
| 783 |
AdaptiveScale_metropolis_theta_single_block(idx,model,center,period,max_period,verbose); |
| 784 |
n_draws=metropolis->draws[idx]; |
| 785 |
|
| 786 |
// Reset starting value |
| 787 |
if (n_theta) |
| 788 |
{ |
| 789 |
ConvertFreeParametersToTheta(model,pElementV(initial_theta)); |
| 790 |
FreeVector(initial_theta); |
| 791 |
} |
| 792 |
if (n_q) |
| 793 |
{ |
| 794 |
ConvertFreeParametersToQ(model,pElementV(initial_q)); |
| 795 |
FreeVector(initial_q); |
| 796 |
} |
| 797 |
|
| 798 |
// Reset blocks |
| 799 |
Setup_metropolis_theta_blocks(metropolis,n_blocks,dims); |
| 800 |
dw_free(dims); |
| 801 |
|
| 802 |
return n_draws; |
| 803 |
} |
| 804 |
|
| 805 |
/* |
| 806 |
Assumes |
| 807 |
model : pointer to valid TStateModel structure |
| 808 |
center_s : target acceptance rate for each direction invidually |
| 809 |
peroid_s : initial length of sub-runs to calibrate scale for each direction individually |
| 810 |
max_period_s : maximum length of sub-runs to calibrate scale for each direction individually |
| 811 |
center_a : target acceptance rate for each block |
| 812 |
peroid_a : initial length of sub-runs to calibrate scales for each block |
| 813 |
max_period_a : maximum length of sub-runs to calibrate scales for each block |
| 814 |
verbose : non-zero for output to stdout |
| 815 |
|
| 816 |
Returns |
| 817 |
The total number of theta draws made. |
| 818 |
|
| 819 |
Results |
| 820 |
The values of model->metropolis_scale_theta are calibrated to obtain the |
| 821 |
desired acceptance ratio. |
| 822 |
*/ |
| 823 |
int Calibrate_metropolis_theta_two_pass(TStateModel *model, PRECISION center_s, int period_s, int max_period_s, |
| 824 |
PRECISION center_a, int period_a, int max_period_a, int verbose) |
| 825 |
{ |
| 826 |
TMetropolis_theta *metropolis=model->metropolis_theta; |
| 827 |
int *dims, n_draws=0, n_blocks, i, n_theta=NumberFreeParametersTheta(model), n_q=NumberFreeParametersQ(model); |
| 828 |
TVector initial_theta=(TVector)NULL, initial_q=(TVector)NULL; |
| 829 |
|
| 830 |
// save initial value |
| 831 |
if (n_theta) ConvertThetaToFreeParameters(model,pElementV(initial_theta=CreateVector(n_theta))); |
| 832 |
if (n_q) ConvertQToFreeParameters(model,pElementV(initial_q=CreateVector(n_q))); |
| 833 |
|
| 834 |
// Individual direction run |
| 835 |
if (period_s > 0) |
| 836 |
{ |
| 837 |
// Save old blocks |
| 838 |
n_blocks=metropolis->n_blocks; |
| 839 |
memcpy(dims=(int*)dw_malloc(n_blocks*sizeof(int)),metropolis->block_dims,n_blocks*sizeof(int)); |
| 840 |
|
| 841 |
// Setup blocks |
| 842 |
Setup_metropolis_theta_blocks_full(metropolis); |
| 843 |
|
| 844 |
// Calibrate |
| 845 |
for (i=0; i < n_theta; i++) |
| 846 |
{ |
| 847 |
AdaptiveScale_metropolis_theta_single_block(i,model,center_s,period_s,max_period_s,verbose); |
| 848 |
n_draws+=metropolis->draws[i]; |
| 849 |
if (n_theta) ConvertFreeParametersToTheta(model,pElementV(initial_theta)); |
| 850 |
if (n_q) ConvertFreeParametersToQ(model,pElementV(initial_q)); |
| 851 |
} |
| 852 |
|
| 853 |
// Reset blocks |
| 854 |
Setup_metropolis_theta_blocks(metropolis,n_blocks,dims); |
| 855 |
dw_free(dims); |
| 856 |
} |
| 857 |
else |
| 858 |
ResetCounts_metropolis_theta(metropolis); |
| 859 |
|
| 860 |
// Final run |
| 861 |
if (period_a > 0) |
| 862 |
{ |
| 863 |
AdaptiveScale_metropolis_theta(model,center_a,period_a,max_period_a,verbose); |
| 864 |
for (i=0; i < metropolis->n_blocks; i++) n_draws+=metropolis->draws[i]; |
| 865 |
if (n_theta) ConvertFreeParametersToTheta(model,pElementV(initial_theta)); |
| 866 |
if (n_q) ConvertFreeParametersToQ(model,pElementV(initial_q)); |
| 867 |
} |
| 868 |
|
| 869 |
if (n_theta) FreeVector(initial_theta); |
| 870 |
if (n_q) FreeVector(initial_q); |
| 871 |
|
| 872 |
return n_draws; |
| 873 |
} |