tristanz / ScytheMCMC (http://bytebucket.org/tristanz/scythemcmc/wiki/html/index.html)
A Scythe Markov Chain Monte Carlo C++ Framework
Clone this repository (size: 1.0 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/tristanz/scythemcmc
| commit 13: | 9844e0828091 |
| parent 12: | fd1ece924824 |
| branch: | default |
| tags: | tip |
Updates. Switched to Lecuyer and enable seed saving.
ScytheMCMC /
src
/
mcmc.h
| r13:9844e0828091 | 1216 loc | 37.3 KB | embed / history / annotate / raw / |
|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 | /*! \mainpage
<table>
<tr><th>Library <td>Scythe MCMC - A Scythe Markov Chain Monte Carlo C++ Framework
<tr><th>Author <td>Tristan Zajonc (tristanz@gmail.com)
<tr><th>Source <td>http://ksghome.harvard.edu/~zajonct/
<tr><th>Version <td>0.1
</table>
\section intro INTRODUCTION
Scythe MCMC is a C++ header library that eases the development of Markov Chain Monte Carlo (MCMC). It is based on the
<a href="http://scythe.wustl.edu/">Scythe Statistical Library</a>.
Scythe MCMC provides an execution framework, including command line and option file parsing, that reduces the amount of boilerplate
code needed to write custom MCMC routines. It also provides common MCMC step types, including Gibbs, Metropolis-Hastings,
and Slice sampling. Users can experiment with which sampling steps provide the best results and implement their own sampling
steps as desired.
Scythe MCMC was motivated with the need to combine simpler samplers, such as univariate slice sampling, with more specialized
samplers for particular problems. Future versions of Scythe MCMC will include common samplers for nonparametric Bayesian models,
including a Collapsed Gibbs sampler for the Dirichlet Process, and Slice samplers for the Dirichlet Process and Indian Buffet Process.
There are many choices for writing MCMC samplers. Scythe MCMC is similar to, and heavily inspired by,
<a href="http://darwin.eeb.uconn.edu/mcmc++/mcmc++.html">MCMC++</a>, but is based
on the Scythe, which provides convenient matrix types, random number generators,
and probability distributions. Scythe MCMC is different in focus from
<a href="http://mcmcpack.wustl.edu/wiki/index.php/Main_Page">MCMCPack</a>, which
is also based on Scythe and interfaces with R. Users wishing to distribute a specific model to R users
should consider contributing to MCMCPack directly. Unlike MCMCPack, Scythe MCMC makes no attempt to interface with R
and assumes samplers are executed from the command line (although this is not required).
It also provides common algorithms (Gibbs, Metropolis-Hastings, Slice Sampling) that ease
development of samplers for many models.
Even though implementing models in ScytheMCMC requires more work than
implementing comparable models in <a href="http://mathstat.helsinki.fi/openbugs/">Bugs</a>
or <a href="http://www-ice.iarc.fr/~martyn/software/jags/">JAGS</a>, Scythe MCMC generally
leads to faster samplers that can exploit the particular structure of a given problem. It also
allows development of models that cannot be sampled effectively using either Bugs or Jags, such
nonparametric Bayesian models without truncation approximations.
\section features FEATURES
- Based on the <a href="http://scythe.wustl.edu/">Scythe Statistical Library</a>.
- Eliminates commandline and option parsing boilerplate code using
<a href ="http://code.jellycan.com/simpleopt/">SimpleOpt</a> and <a href="http://code.jellycan.com/simpleini/">SimpleIni</a>.
- Built in sampling steps:
- Gibbs,
- Metropolis-Hastings,
- Slice Sampling.
- Walker (2008) sampler for DP.
- Nonparameteric samplers including:
- Collapsed gibbs samplers for Dirichlet Process,
- Slice sampler for Dirichlet Process and Indian Buffet Process.
- MIT License.
\section gettingstarted GETTING STARTED
\section license MIT LICENSE
The license text below is the boilerplate "MIT License" used from:
http://www.opensource.org/licenses/mit-license.php
Copyright (c) 2009, Tristan Zajonc
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/progress.hpp>
#include <boost/timer.hpp>
#include <boost/algorithm/string.hpp>
#include <ctime>
#include <iostream>
#include <fstream>
#include <scythestat/rng/mersenne.h>
#include <scythestat/rng/lecuyer.h>
#include <scythestat/distributions.h>
#include <scythestat/matrix.h>
#include <scythestat/rng.h>
#include <scythestat/smath.h>
#include <scythestat/stat.h>
#include <scythestat/ide.h>
#include <scythestat/defs.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "Simple/SimpleOpt.h"
#include "Simple/SimpleIni.h"
// #include <mkl_vml_functions.h>
// #include <mkl_vml_defines.h>
#include "scythe_extensions.h"
/// Scythe MCMC version.
#define SCYTHE_MCMC_VERSION "0.1"
/// Value of missing data. If data is stored as double, use round(x)==MISSING.
#define MISSING -9999
using namespace scythe;
using namespace std;
/// Scythe column-oriented double matrix typedef.
typedef Matrix<double, Col> matrix;
/// Scythe column-oriented integer matrix typedef.
typedef Matrix<int, Col> imatrix;
/// Pair typedef for integers. Used to store (x, y) coordinates of a matrix location.
typedef pair<int, int> ipair;
/// Global positive infinity representation.
double dInf = std::numeric_limits<double>::infinity();
double iInf = std::numeric_limits<int>::infinity();
/// Global L'Ecuyer random number generator.
lecuyer myrng;
//mersenne myrng;
/*! \brief Basic MCMC options.
*
* These options are generally specified at the command line and are generic to all
* MCMC samplers. Other options are passed through an .ini config file.
*/
struct MCMCOptions {
/// Sample size. (iterations - burnin)/(thin + 1).
int sample_size;
/// Thinning interval
int thin;
/// Burn in period.
int burnin;
/// Chains.
int chains;
/// Seed for random number generator.
unsigned long random_seed [6];
/// Config file (.ini style).
string config_file;
/// Out file to save parameter chains.
string out_file;
};
/// Global MCMCOptions object. It is convenient to store command line options globally to avoid passing them
/// around everwhere.
MCMCOptions mcmc_options;
/*! \brief Check if file exists.
*
\param strFilename Filename to check.
\returns True is file exists. False otherwise.
\note Source: http://www.techbytes.ca/techbyte103.html.
*/
bool FileExists(string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(), &stFileInfo);
if (intStat == 0) {
// We were able to get the file attributes
// so the file obviously exists.
blnReturn = true;
} else {
// We were not able to get the file attributes.
// This may mean that we don't have permission to
// access the folder which contains this file. If you
// need to do that level of checking, lookup the
// return values of stat which will give you
// more details on why stat failed.
blnReturn = false;
}
return(blnReturn);
}
/*! \brief Convert any streaming type to CSV.
*
*/
template <class T>
inline string to_csv (const T& t) {
std::stringstream ss;
ss << t;
string s = ss.str();
replace(s.begin(), s.end(), ' ', ',');
return s;
}
/*! \brief Convert any streaming type to string
*
*/
template <class T>
inline string to_string (const T& t) {
std::stringstream ss;
ss << t;
string s = ss.str();
return s;
}
/*! \brief Shows command line usage.
Diplays:
<pre>
ScytheMCMC v. 0.1
Usage:
--help displays help message
--config-file=filename config file name
--out-file=filename out file name
--chains=arg number of chains
--sample-size=arg retained sample size
--burnin=arg burn in period
--thin=arg thinning iterval (1 = no thinning)
--random-seed=arg random number seed (0 uses current timestamp)
Example: ./command --config-file=command.ini --out-file=out.txt --chains=1 --sample-size=1000
--burnin=0 --thin=1 --random-seed=0
</pre>
*/
void ShowUsage() {
cout << " Usage:" << endl
<< " --help \t\t\t displays help message" << endl
<< " --config-file=filename \t config file name" << endl
<< " --out-file=filename \t out file name" << endl
<< " --chains=arg \t\t number of chains" << endl
<< " --sample-size=arg \t\t retained sample size" << endl
<< " --burnin=arg \t\t burn in period" << endl
<< " --thin=arg \t\t\t thinning iterval (1 = no thinning)" << endl
<< " --random-seed=arg \t\t random number seed (0 uses current timestamp, 1 attempts to load from lecuyer.seed file)" << endl << endl;
cout << " Example: ./command --config-file=command.ini --out-file=out.txt --chains=1 --sample-size=1000" << endl
<< "\t --burnin=0 --thin=1 --random-seed=0" << endl << endl;
}
/*! \brief Parses command line options.
Parses and stores command line options and stores them in MCMCOptions instance
mcmc_options. Initializes random mersenne instance myrng.
\see ShowUsage.
\see mcmc_options.
\see myrng.
*/
void DefaultStartUp(int argc, char* argv[]) {
cout << "ScytheMCMC v. " << SCYTHE_MCMC_VERSION << endl << endl;
// Config parser is based on SimpleOpt Library.
// See: http://code.jellycan.com/simpleopt/
enum { OPT_SAMPLE_SIZE, OPT_THIN, OPT_BURNIN, OPT_CHAINS, OPT_RANDOM_SEED, OPT_CONFIG_FILE, OPT_OUT_FILE, OPT_HELP };
CSimpleOpt::SOption g_rgOptions[] = {
{ OPT_HELP, "--help", SO_NONE },
{ OPT_CONFIG_FILE, "--config-file", SO_REQ_CMB },
{ OPT_OUT_FILE, "--out-file", SO_REQ_CMB },
{ OPT_CHAINS, "--chains", SO_REQ_CMB },
{ OPT_SAMPLE_SIZE, "--sample-size", SO_REQ_CMB },
{ OPT_BURNIN, "--burnin", SO_REQ_CMB },
{ OPT_THIN, "--thin", SO_REQ_CMB },
{ OPT_RANDOM_SEED, "--random-seed", SO_REQ_CMB },
SO_END_OF_OPTIONS
};
CSimpleOpt args(argc, argv, g_rgOptions);
int setflags = 0;
while (args.Next()) {
if (args.LastError() == SO_SUCCESS) {
switch (args.OptionId()) {
case OPT_HELP:
ShowUsage();
exit(0);
break;
case OPT_CONFIG_FILE:
setflags++;
mcmc_options.config_file = args.OptionArg();
break;
case OPT_OUT_FILE:
setflags++;
mcmc_options.out_file = args.OptionArg();
break;
case OPT_CHAINS:
setflags++;
mcmc_options.chains = atoi(args.OptionArg());
break;
case OPT_SAMPLE_SIZE:
setflags++;
mcmc_options.sample_size = atoi(args.OptionArg());
break;
case OPT_BURNIN:
setflags++;
mcmc_options.burnin = atoi(args.OptionArg());
break;
case OPT_THIN:
setflags++;
mcmc_options.thin = atoi(args.OptionArg());
break;
case OPT_RANDOM_SEED:
setflags++;
unsigned long seed = atoi(args.OptionArg());
cout << "TEST" << seed<<endl;
if(seed == 0) {
seed = time(NULL);
for(int i = 0; i < 6; ++i) {
mcmc_options.random_seed[i] = seed;
}
}
else if (seed == 1) {
// try to load seed from file
cout << "Try loading file..." << endl;
if (FileExists("lecuyer.seed")) {
Matrix<unsigned long, Col> seedmat("lecuyer.seed");
for(int i = 0; i < 6; ++i) {
mcmc_options.random_seed[i] = seedmat(i);
}
}
else {
cout << "No seed exists..." << endl;
seed = time(NULL);
for(int i = 0; i < 6; ++i) {
mcmc_options.random_seed[i] = seed;
}
}
}
else {
for(int i = 0; i < 6; ++i) {
mcmc_options.random_seed[i] = seed;
}
}
break;
}
} else {
cout << "Invalid argument: " << args.OptionText() << endl;
exit(1);
}
}
// Check that all required flags are set.
if (setflags != 7) {
ShowUsage();
exit(1);
}
// Check argument values.
if (mcmc_options.sample_size <= 0) {
cout << "Sample size must be positive.\n";
exit(1);
}
if (mcmc_options.burnin < 0) {
cout << "Burn in cannot be negative.\n";
exit(1);
}
if (mcmc_options.thin < 1) {
cout << "Thinning interval must be positive.\n";
exit(1);
}
if (!FileExists(mcmc_options.config_file)) {
cout << "Config file does not exist.\n";
exit(1);
}
// Seed random number generator.
//myrng.initialize(mcmc_options.random_seed);
myrng.SetSeed(mcmc_options.random_seed);
// Print mcmc options.
cout << "Arguments:" << endl;
cout << " Config file: " << mcmc_options.config_file << endl
<< " Chains: " << mcmc_options.chains << endl
<< " Desired sample size: " << mcmc_options.sample_size << endl
<< " Burn in period: " << mcmc_options.burnin << endl
<< " Thinning interval: " << mcmc_options.thin << endl
<< " Random number seed: "
<< mcmc_options.random_seed[0] << " "
<< mcmc_options.random_seed[1] << " "
<< mcmc_options.random_seed[2] << " "
<< mcmc_options.random_seed[3] << " "
<< mcmc_options.random_seed[4] << " "
<< mcmc_options.random_seed[5] << endl << endl;
}
/*! \brief Main parameter class.
*
* The model is defined as many instances of parameter subclasses. Different Step types require different methods
* to be implemented in each Parameter subclass.
*
* \section overwrite Methods that should be implemented in subclass:
* Required for all step types:
* - Constructor that sets, at least, Parameter::track_ and Parameter::name_
* - Parameter::Value
* - Parameter::Save
*
* GibbsStep:
* - Parameter::StartingValue
* - Parameter::RandomPosterior
*
* Metropolis-Hastings:
* - Parameter::StartingValue
* - Parameter::LogDensity
*
* Slice:
* - Parameter::StartingValue
* - Parameter::LogDensity
*
* FunctionStep (deterministic):
* - Parameter::Function
*
* \section examples Examples:
*
* \subsection example1 Normal mean parameter with normal prior, known standard deviation:
*
\code
// Priors
double mean_mu = 0;
double mean_sigma = 2;
// Parameters
double mean = 0; // free parameter
double sd = 1; // known
// Data
matrix X('X.txt');
// Define the mean parameter, implementing methods required for GibbsStep.
class MeanParameter : public Parameter<double> {
public:
/// Default constructors that call base constructors. (REQUIRED)
MeanParameter() : Parameter<double>() {}
MeanParameter(bool track, string name) : Parameter<double>(track, name) {}
/// Draw a random value from analytical posterior for GibbsStep.
double RandomPosterior() {
// Posterior of mean with known variance, (Gelman et al (2004) p 49.)
double x_mean = mean(X);
int n = X.rows();
double denom = 1/(1/pow(mean_sigma,2) + n / pow(sd,2))
double posterior_mean = ((1/pow(mean_sigma,2)) * mean_mu + (n / pow(sd,2)) * x_mean) / denom
double posterior_sd = sqrt(denom);
return myrng.rnorm(posterior_mean, posterior_sd);
}
/// Draw starting value from prior.
double StartingValue() {
return myrng.rnorm(mean_mu, mean_sigma);
}
/// Save back to global location
void Save(double new_value) {
mean = new_value;
}
/// Return value from global location.
double Value() {
return mean;
}
};
\endcode
*/
template<typename ParameterStorageType>
class Parameter {
public:
/// Default class constructor.
Parameter() {}
/*! \brief Base class constructor
*
* \param track True if parameter should be tracked and saved.
* \param label Label of parameter for tracking purposes.
*/
Parameter(bool track, string label) : track_(track), label_(label) {}
/// Function for deterministic nodes.
/// \return Function value.
virtual ParameterStorageType Function() {
return 0.0;
};
/// Starting value.
/// \return Starting value.
virtual ParameterStorageType StartingValue() {
return 0.0;
};
/// Log of the probability density (plus constant)
/// \param value Value of parameter to evaluate density at.
/// \return Log of probability density (plus constant)
virtual double LogDensity(double value) {
return 0.0;
}
/// Return a random draw from the posterior.
/// Random draw from posterior is called by GibbsStep.
/// \return Random draw from posterior.
virtual ParameterStorageType RandomPosterior() {
return 0.0;
}
/// Value of parameter.
/// \return Parameter value
virtual ParameterStorageType Value() {
return 0.0;
};
/// Save a new value of the parameter.
/// \param new_value New value to save.
virtual void Save(ParameterStorageType new_value) {};
/// Parameter is tracked / saved.
/// \return True if parameter is tracked.
bool Track() {
return track_;
}
/// String label of parameter.
/// \return Label of parameter, for purposes of saving output.
string Label() {
return label_;
}
private:
/// Should this variable be tracked?
bool track_;
/// Name of variable for tracking purposes.
string label_;
};
/*! \defgroup steps Implemented MCMC Step Types
*
* MCMC algorithms typically consist of many steps. While users will often want
* to implement their own step types, Scythe MCMC has implemented several that arise
* over and over. MetropStep and SliceStep in particular only require specification of
* a log density function (minus an unknown constant).
*
*/
/*! \brief Base step class.
*
* \ingroup steps
*
* The step (Gibbs, Metropolis-Hastings, Slice, etc) describes how the sampler
* calculates the next value of each parameter. The key function is Step::DoStep, which is
* equivalent to the Execute function in a <a href="http://en.wikipedia.org/wiki/Command_pattern">Command Pattern</a>.
* Samplers consists of a many of steps.
*
* \see GibbsStep
* \see FunctionStep
* \see MetropStep
* \see SliceStep
* \see Sampler
*/
class Step {
public:
/*! \brief Take step.
*
* Executes the main step function, such as taking a MH step or deterministic step.
*/
virtual void DoStep() {}
/*! \brief Set starting value.
*
* Calls the parameter's Parameter::Starting function and then saves the result using Parameter::Save.
*/
virtual void Start() {}
/*! \brief Return parameter value
*
* \return String representation of parameter value for csv output.
* \see Parameter::Value
*/
virtual string ParameterValue() {
return "";
}
/*! \brief Return parameter label
*
* \return The label of the parameter associated with the step instance.
* \see Parameter::Label
*/
virtual string ParameterLabel() {
return "";
}
/*! Return the tracking status
*
* \return The tracking status of the parameter associated with the step instance.
* \see Parameter::Track
*/
virtual bool ParameterTrack() {
return true;
}
};
/*! \brief Gibbs step.
*
* \ingroup steps
*
* A univariate Gibbs step draws from the Parameter::RandomPosterior function of a Parameter and then saves the result
* using Parameter:Save.
*/
template <typename ParameterType, typename ParameterStorageType>
class GibbsStep: public Step {
public:
/// Default constructor, for copy assignments, etc.
GibbsStep() {}
/*! \brief Main constructor taking a Parameter object.
* \param parameter A Parameter that implements Parameter::RandomPosterior.
*/
GibbsStep(ParameterType parameter) : parameter_(parameter) {
}
/*! \brief Take a Gibbs step for the parameter.
*
* Draws from the parameter's Parameter::RandomPosterior function and then saves the result using Parameter::Save.
*/
void DoStep() {
parameter_.Save(parameter_.RandomPosterior());
}
void Start() {
parameter_.Save(parameter_.StartingValue());
}
string ParameterLabel() {
return parameter_.Label();
}
string ParameterValue() {
return to_csv<ParameterStorageType>(parameter_.Value());
}
bool ParameterTrack() {
return parameter_.Track();
}
private:
/// Parameter associated with step instance.
ParameterType parameter_;
};
/*! \brief Deterministic function step.
*
* \ingroup steps
*
* Deterministic nodes can be helpful to track summary statistics or to reduce computations through intermediate use
* of sufficient statistics across multiple parameters. FunctionStep calls the parameters Parameter::Function method
* and saves the result using Parameter::Save.
*/
template <typename ParameterType, typename ParameterStorageType>
class FunctionStep: public Step {
public:
/// Default constructor, for copy assignments, etc.
FunctionStep() {}
/*! \brief Main constructor taking a Parameter object.
*
* \param parameter A Parameter that implements Parameter::Function.
*/
FunctionStep(ParameterType parameter) : parameter_(parameter) {
}
/*! \brief Take a function step for the parameter.
*
* Calculates the value of Parameter::Function and then saves the result using Parameter::Save.
*/
void DoStep() {
parameter_.Save(parameter_.Function());
}
/*! \brief Set function step starting value.
*
*/
void Start() {
parameter_.StartingValue();
}
string ParameterLabel() {
return parameter_.Label();
}
string ParameterValue() {
return to_csv<ParameterStorageType>(parameter_.Value());
}
bool ParameterTrack() {
return parameter_.Track();
}
private:
/// Parameter associated with step instance.
ParameterType parameter_;
};
/*! \defgroup proposals Metropolis-Hastings Proposals
*
* The MetropStep Metropolis-Hastings class requires both a Parameter instance
* and a Proposal instance. Proposal instances define a Draw and LogDensity method.
* The constructor should set a tuning parameter.
*
* MetroStep::DoStep uses the Draw method to draw new proposed values for the parameter
* and the LogDensity to calculate the proposal terms in the acceptance \f$\alpha\f$. For
* symmetric proposal distributions returning 0.0 is enough.
*/
/*! \brief Normal proposal for Metropolis-Hastings.
*
* \ingroup proposals
*
* Normal proposal draws from N(starting_value, standard_deviation).
*/
class NormalProposal {
public:
NormalProposal() {}
/*! \brief Constructor
*
* \param standard_deviation Tuning parameter for normal proposal.
*/
NormalProposal(double standard_deviation) : standard_deviation_(standard_deviation) {}
/*! \brief Draw from normal proposal
*
* \param starting_value Starting value.
*/
double Draw(double starting_value) {
return myrng.rnorm(starting_value, standard_deviation_);
}
/*! \brief Log probability of proposal new value, given starting value.
*
* \note Needs to be implemented but can be 0.0 if proposal is symmetric, which it is.
* \param starting_value Starting value.
* \param new_value Proposed new value.
*/
double LogDensity(double new_value, double starting_value) {
return 0.0; // Symmetric, doesn't matter.
}
private:
double standard_deviation_;
};
/*! \brief Beta proposal for Metropolis-Hastings.
*
* \ingroup proposals
*
* Useful proposal type for parameters with support between 0 and 1.
*
* The beta proposal is parameterized using the mean and the inverse denominator. The denominator
* is a measure a scale and can be thought of as the number of observed trials bernoulli. We use the inverse
* of the denominator so that larger values imply bigger steps, consistent with NormalProposal. For the Beta distribution
* the mean is
* \f[ m = \frac{\alpha}{\alpha + \beta} \f]
* and the denominator is
* \f[ d = \alpha + \beta. \f]
* Therefore
* \f[ \alpha = m \cdot d \f]
* and
* \f[ \beta = d \cdot (1-m). \f]
*
*/
class BetaProposal {
public:
/*! \brief Empy constructor */
BetaProposal() {}
/*! \brief Main constructor
*
* Scale of steps are parameterized using the inverse denominator of a Beta distribution:
* \f[ \frac{1}{d} = \frac{1}{\alpha + \beta} \f]
*
* \param idenominator Tuning parameter equal to the inverse denominator \f$1/d\f$ of a Beta distribution.
*/
BetaProposal(double idenominator) {
denom_ = 1 / idenominator;
}
/*! \brief Draw from Beta proposal
*
* \param starting_value Starting value (mean of Beta distribution).
*/
double Draw(double starting_value) {
double alpha = starting_value * denom_;
double beta = alpha - denom_;
return myrng.rbeta(alpha, beta);
}
/*! \brief Log probability of proposal new value, given starting value.
*
* \param starting_value Starting value.
* \param new_value Proposed new value.
*/
double LogDensity(double new_value, double starting_value) {
double alpha = starting_value * denom_;
double beta = alpha - denom_;
return dbeta(new_value, alpha, beta);
}
private:
double denom_;
};
/*! \brief Log normal proposal for Metropolis-Hastings.
*
* \ingroup proposals
*
* Useful proposal type for parameters with support between 0 and positive infinity.
*
* For convenience, the log normal proposal is parameterized using the unlogged mean (starting value)
* and the (positive) log standard deviation. This differs from how scythe::dlnorm is parameterized.
*
*/
class LogNormalProposal {
public:
/*! \brief Empy constructor */
LogNormalProposal() {}
/*! \brief Main constructor
*
* Scale of steps are standard deviation of the logged parameter value:
*
* \param logsd Tuning parameter equal to the log standard deviation.
*/
LogNormalProposal(double logsd) : logsd_(logsd) {}
/*! \brief Draw from log normal proposal
*
* \param starting_value Starting value (unlogged mean of log normal).
*/
double Draw(double starting_value) {
double logmean = log(starting_value);
return myrng.rlnorm(logmean, logsd_);
}
/*! \brief Log probability of proposal new value, given starting value.
*
* \param starting_value Starting value.
* \param new_value Proposed new value.
*/
double LogDensity(double new_value, double starting_value) {
double logmean = log(starting_value);
return dlnorm(new_value, logmean, logsd_);
}
private:
double logsd_;
};
/*! \brief Metropolis Hastings step
*
* \ingroup steps
*
* Basic univariate Metropolis-Hastings step. Requires both a Parameter and Proposal instance.
*/
template <typename ParameterType, typename ProposalType>
class MetropStep: public Step {
public:
/*! \brief Default constructor, for copy assignments, etc.
*/
MetropStep() {}
/*! \brief Main constructor taking a Parameter and Proposal object.
*
* \param parameter A Parameter that implements Parameter::LogDensity
* \param proposal A Proposal instance that implements Proposal::LogDensity and Proposal::Draw.
*/
MetropStep(ParameterType parameter, ProposalType proposal) : parameter_(parameter), proposal_(proposal) {}
/*! \brief Take a function step for the parameter.
*
* Calculates the value of Parameter::Function and then saves the result using Parameter::Save.
*/
void DoStep() {
// Draw a new parameter
double new_value = proposal_.Draw(parameter_.Value());
double old_value = parameter_.Value();
// MH accept/reject criteria
double alpha = parameter_.LogDensity(new_value) - parameter_.LogDensity(old_value)
+ proposal_.LogDensity(old_value, new_value) - proposal_.LogDensity(new_value, old_value);
if (myrng.runif() < min(exp(alpha), 1.0)) {
parameter_.Save(new_value);
}
}
void Start() {
parameter_.Save(parameter_.StartingValue());
}
string ParameterLabel() {
return parameter_.Label();
}
string ParameterValue() {
return to_csv<double>(parameter_.Value());
}
bool ParameterTrack() {
return parameter_.Track();
}
private:
/// Parameter associated with step instance.
ParameterType parameter_;
ProposalType proposal_;
};
/*! \brief Univariate slice sampling step.
*
* \ingroup steps
*
* Basic univariate slice sampling step with stepping out and shrinkage. Performs a slice sampling update from an initial
* point to a new point that leaves invariant the distribution with the specifified log density functions.
*
* The log desnity function may return -Inf for points outside the support of the distribution.
* If a lower and/or upper bound is specified for the support, the log density function will not be called outside
* such limits.
*
* \note See Neal, R. M (2003) "Slice Sampling" (with discussion), <i>Annals of Statistics</i>,
* vol. 31, no. 3, pp. 705-767.
* \note Code and description based on Neal's R code (March 17, 2008): http://www.cs.toronto.edu/~radford/ftp/slice-R-prog
* \note With poor initial values or choice of w, slice sampling can get take a very long time to find
* a suitable slice. This may appear like a crash. Experimenting with w may speed sampling, but things tends
* to work pretty well with default values in many cases.
*/
template <typename ParameterType>
class SliceStep: public Step {
public:
/// Default constructor, for copy assignments, etc.
SliceStep() {}
/*! \brief Main constructor for slice sampling step.
*
* \param parameter A Parameter that implements Parameter::LogDensity
* \param w Size of the steps for creating interval (default = 1)
* \param lower Lower bound on the support of the distribution (default = -Infinity)
* \param upper Upper bound on the support of the distribution (default = Infinity)
*/
SliceStep(ParameterType parameter, double w, double lower, double upper) :
parameter_(parameter), w_(w), lower_(lower), upper_(upper) {
step_count_ = 0;
}
/*! \brief Take a slice sampling step for the parameter.
*
*/
void DoStep() {
// Find the log density at the initial point.
double x0 = parameter_.Value();
double gx0 = parameter_.LogDensity(x0);
// Determine slice level, in log terms
double logy = gx0 - myrng.rexp(1);
//Find the initial interval to sample from.
double u = myrng.runif() * w_;
double L = x0 - u;
double R = x0 + (w_ - u);
// Expand the interval until its ends are outside the slice, or
// until the limit on steps is reached.
// allow infinite steps... could hang!
// FIXME: Add check for too many iterations and exit with error message.
while (true) {
if (L <= lower_) {
break;
}
if (parameter_.LogDensity(L) <= logy) {
break;
}
L = L - w_;
}
while (true) {
if (R >= upper_) {
break;
}
if (parameter_.LogDensity(R) <= logy) {
break;
}
R = R + w_;
}
// Shrink interval to lower and upper bounds.
if (L < lower_) {
L = lower_;
}
if (R > upper_) {
R = upper_;
}
// Sample from the interval, shrinking it on each rejection
double x1, gx1;
while (true) {
x1 = myrng.runif() * (R - L) + L; // Sample between L and R, uniformly.
gx1 = parameter_.LogDensity(x1);
if (gx1 >= logy) {
break;
}
if (x1 > x0) {
R = x1;
} else {
L = x1;
}
}
// Adapt
//step_count_++;
//w_ = (step_count_/(step_count_+1)) * w_ + (1/(step_count_+1))*(R - L);
// Save the point sampled
parameter_.Save(x1);
}
void Start() {
parameter_.Save(parameter_.StartingValue());
}
string ParameterLabel() {
return parameter_.Label();
}
string ParameterValue() {
return to_csv<double>(parameter_.Value());
}
bool ParameterTrack() {
return parameter_.Track();
}
private:
/// Parameter associated with step instance.
ParameterType parameter_;
/// Slice sampling control values
double w_;
double lower_;
double upper_;
int step_count_;
};
/*! \brief MCMC sampler.
*
* The sampler is the main MCMC object that holds all the MCMC steps for each parameter. Running the sampler
* performs MCMC sampling for the model, saving results, and displaying progress. In the language of the Command Pattern, the
* sampler is the Invoker or Command Manager.
*
* After instantiating the sampler, users should add all the required steps using the Sampler::AddStep method, which places
* each step onto a stack. The entire sampling process is run using Sampler::Run.
*/
class Sampler {
public:
/*! \brief Constructor to initialize sampler.
*
* \param options MCMC options object that defines sample_size, burnin, thinning interval, etc.
*/
Sampler(MCMCOptions options) {
cout << "Creating Sampler... " << endl;
sample_size_ = options.sample_size;
burnin_ = options.burnin;
thin_ = options.thin;
out_file_ = options.out_file;
}
/*! \brief Add Step to Sampler execution stack.
*
* All parameters should have an associated Step that is added to the sampler. The sampler stack
* defines one sampler iteration.
* \param step Step object for a given parameter.
*/
void AddStep(Step* step) {
// Add step to step stack.
steps_.push_back(step);
// Add index to tracking stack, if necessary tracked.
if (steps_.back().ParameterTrack()) {
tracks_.push_back(steps_.size() - 1);
}
}
/*! \brief Run sampler for a specific number of iterations.
*
* \param number_of_iterations Number of iterations to run sampler.
* \param progress Display a progress bar.
*/
void Iterate(int number_of_iterations, bool progress = false) {
if(progress) {
boost::progress_display show_progress(number_of_iterations);
for(int iter = 0; iter < number_of_iterations; ++iter) {
for(int i = 0; i < steps_.size(); ++i) {
steps_[i].DoStep();
}
++show_progress;
}
}
else {
for(int iter = 0; iter < number_of_iterations; ++iter) {
for(int i = 0; i < steps_.size(); ++i) {
steps_[i].DoStep();
}
}
}
}
/*! \brief Run sampler.
*
* Run the MCMC sampling procedure, including burning in, sampling, thinning, displaying progress,
* and saving results.
*/
void Run() {
// Timer
boost::timer timer;
// Status of sampler...
cout << "Running sampler..." << endl;
cout << "Number of steps added: " << NumberOfSteps() << endl;
cout << "Number of tracked steps added: " << NumberOfTrackedSteps() << endl;
// Opening output file
cout << "Opening output file: " << out_file_ << endl;
ofstream outfile(out_file_.c_str());
if (!outfile.is_open()) {
cerr << "ERROR: Cannot open file!";
exit(1);
}
for (int k = 0; k < tracks_.size(); ++k) {
if (k == tracks_.size() - 1) {
outfile << steps_[tracks_[k]].ParameterLabel() << endl;
} else {
outfile << steps_[tracks_[k]].ParameterLabel() << ",";
}
}
// Setting starting value:
cout << "Setting starting values..." << endl;
for (int i = 0; i < steps_.size(); ++i) {
steps_[i].Start();
}
// Burn in
cout << "Burning in... (" << burnin_ << " iterations)" << endl;
Iterate(burnin_, true);
cout << endl << "Sampling..." << endl;
boost::progress_display show_progress(sample_size_);
for (int i = 0; i < sample_size_; ++i) {
Iterate(thin_);
// Save data
for (int k = 0; k < tracks_.size(); ++k) {
if (k == tracks_.size() - 1) {
outfile << steps_[tracks_[k]].ParameterValue() << endl;
} else {
outfile << steps_[tracks_[k]].ParameterValue() << ",";
}
}
//Show progress
++show_progress;
}
outfile.close();
// Save lecuyer seed for future use
Matrix<unsigned long, Col> seedmat(6, 1, false);
unsigned long seed[6];
myrng.GetState(seed);
cout << "Saving seed: " << endl << seed[0] << " " << seed[1] << " " << seed[2]
<< " " << seed[3] << " " << seed[4] << " " << seed[5] << endl;
for(int i = 0; i < 6; ++i) {
seedmat(i) = seed[i];
}
seedmat.save("lecuyer.seed", 'o');
cout << "Total elapsed time: " << timer.elapsed() << " seconds" << endl;
}
/*! \brief Number of steps in one sampler iteration.
*
* \return Number of steps.
*/
int NumberOfSteps() {
return steps_.size();
}
/*! \brief Number of tracked steps in one sampler iteration.
*
* \return Number of tracked steps.
*/
int NumberOfTrackedSteps() {
return tracks_.size();
}
private:
int sample_size_;
int burnin_;
int thin_;
string out_file_;
boost::ptr_vector<Step> steps_;
vector<int> tracks_;
};
/*! \brief Approximately equal.
*
* Checks if two doubles are within the numeric_limits<double>::epsilon() precision of each other.
* Useful if === is too strict.
*/
bool approx_equal(double a, double b) {
return abs(a - b) <= numeric_limits<double>::epsilon() ? true : false;
}
/// Fast sum of log of dnorms
template <typename T, matrix_order PO, matrix_style PS>
double sum_fast_log_dnorm(const Matrix<T, PO, PS> &A, const double mean, const double sd) {
double result = 0;
for (int i = 0; i < A.size(); ++i) {
// Slower part here is A[i], can this be speed up using an iterator?
result += fast_log_dnorm(A[i], mean, sd);
}
return result;
}
/// Fast log of dnorm for mean parameter
double fast_log_dnorm(const double x, const double mean, const double sd) {
return -pow((x - mean), 2) / (2*pow(sd, 2));
}
|
