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