Browse files

convert sample function into tune, tune_global, burn, and sample

  • Loading branch information...
1 parent 657d3ad commit 1c6e5de014b7c9aeb15411e6fad94c9b967d7101 @armstrtw committed Nov 28, 2012
View
32 cppbugs/mcmc.model.hpp
@@ -157,37 +157,17 @@ namespace cppbugs {
}
}
- void run(int iterations, int burn, int thin) {
- if(logp()==-std::numeric_limits<double>::infinity()) {
- throw std::logic_error("ERROR: cannot start from a logp of -Inf.");
- }
-
- for(int i = 1; i <= (iterations + burn); i++) {
+ void burn(int iterations) {
+ for(int i = 0; i < iterations; i++) {
step();
- if(i > burn && (i % thin == 0)) { tally(); }
}
}
- void sample(int iterations, int burn, int adapt, int thin) {
-
-
- if(iterations % thin) {
- std::cout << "ERROR: interations not a multiple of thin." << std::endl;
- return;
- }
-
- update();
-
- if(logp()==-std::numeric_limits<double>::infinity()) {
- throw std::logic_error("ERROR: cannot start from a logp of -Inf.");
+ void sample(int iterations, int thin) {
+ for(int i = 1; i <= iterations; i++) {
+ step();
+ if(i % thin == 0) { tally(); }
}
-
- // tuning phase
- tune(adapt,static_cast<int>(adapt/100));
- if(true) { tune_global(adapt,static_cast<int>(adapt/100)); }
-
- // sampling
- run(iterations, burn, thin);
}
// push into specific lists here
View
6 test/eight.schools.cpp
@@ -42,8 +42,10 @@ int main() {
// things to track
std::vector<vec>& theta_hist = m.track<std::vector>(theta);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 5);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 5);
cout << "theta:" << endl << mean(theta_hist.begin(),theta_hist.end()) << endl;
cout << "samples: " << theta_hist.size() << endl;
View
6 test/herd.cpp
@@ -88,8 +88,10 @@ int main() {
std::vector<vec>& b_herd_hist = m.track<std::vector>(b_herd);
std::vector<vec>& overdisp_hist = m.track<std::vector>(overdisp);
- m.sample(1e6,5e5,1e4,50);
- //m.sample(1e5,1e4,10);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(5e5);
+ m.sample(1e6, 50);
cout << "acceptance_ratio: " << m.acceptance_ratio() << endl;
cout << "samples: " << b0_hist.size() << endl;
View
6 test/herd.fast.cpp
@@ -62,7 +62,11 @@ int main() {
std::vector<vec>& b_herd_hist = m.track<std::vector>(b_herd);
std::vector<vec>& overdisp_hist = m.track<std::vector>(overdisp);
- m.sample(1e6,1e5,1e4,50);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(5e5);
+ m.sample(1e6, 50);
+
cout << "acceptance_ratio: " << m.acceptance_ratio() << endl;
cout << "samples: " << b_hist.size() << endl;
cout << "b: " << endl << mean(b_hist.begin(),b_hist.end()) << endl;
View
7 test/linear.model.test.cpp
@@ -42,8 +42,11 @@ int main() {
std::vector<double>& tau_y_hist = m.track<std::vector>(tau_y);
std::vector<double>& rsq_hist = m.track<std::vector>(rsq);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 10);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 10);
+
cout << "lm coefs" << endl << coefs;
cout << "err sd: " << stddev(err,0) << endl;;
cout << "err tau: " << pow(stddev(err,0),-2) << endl;
View
6 test/logistic.model.test.cpp
@@ -41,8 +41,10 @@ int main() {
std::vector<vec>& b_hist = m.track<std::vector>(b);
std::vector<double>& rsq_hist = m.track<std::vector>(rsq);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 10);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 10);
cout << "b (actual):" << endl << real_b;
cout << "b: " << endl << mean(b_hist.begin(),b_hist.end());
View
7 test/price.cpp
@@ -49,8 +49,11 @@ int main() {
std::vector<double>& b_hist = m.track<std::vector>(b);
std::vector<double>& tau_hist = m.track<std::vector>(tau);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 5);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 5);
+
cout << "lm coefs" << endl << coefs;
cout << "err sd: " << stddev(err,0) << endl;;
cout << "err tau: " << pow(stddev(err,0),-2) << endl;
View
2 test/r.inline.example/run.cppbugs.r
@@ -11,7 +11,7 @@ cppbugs.plugin <- getPlugin("RcppArmadillo")
cppbugs.plugin$env$PKG_CXXFLAGS <- "-std=c++0x"
cppbugs.plugin$env$PKG_LIBS <- paste(cppbugs.plugin$env$PKG_LIBS,"-larmadillo")
-linear.model <- cxxfunction(signature(XR="numeric", yr="numeric",iterations="integer",burn="integer",adapt="adapt",thin="integer"), includes="#include <cppbugs/cppbugs.hpp>",body=src,settings=cppbugs.plugin)
+linear.model <- cxxfunction(signature(XR="numeric", yr="numeric",iterations="integer",burn="integer",adapt="integer",adapt_interval="integer",thin="integer"), includes="#include <cppbugs/cppbugs.hpp>",body=src,settings=cppbugs.plugin)
NR <- 1000
View
7 test/r.inline.example/run.model.cpp
@@ -7,6 +7,7 @@ const mat y = Rcpp::as<arma::mat>(yr);
int iterations_ = as<int>(iterations);
int burn_ = as<int>(burn);
int adapt_ = as<int>(adapt);
+int adapt_interval_ = as<int>(adapt_interval);
int thin_ = as<int>(thin);
vec b = randn<vec>(X.n_cols);
@@ -31,7 +32,11 @@ std::vector<vec>& b_hist = m.track<std::vector>(b);
std::vector<double>& rsq_hist = m.track<std::vector>(rsq);
-m.sample(iterations_, burn_, adapt_, thin_);
+ m.tune(adapt_,adapt_interval_);
+ m.tune_global(adapt_,adapt_interval_);
+ m.burn(burn_);
+ m.sample(iterations_, thin_);
+m.sample(, , , );
return Rcpp::List::create(Rcpp::Named("b", mean(b_hist.begin(),b_hist.end())),
Rcpp::Named("rsq", mean(rsq_hist.begin(),rsq_hist.end())),
View
6 test/radon1.cpp
@@ -129,7 +129,11 @@ int main() {
std::vector<vec>& a_hist = m.track<std::vector>(a);
std::vector<double>& b_hist = m.track<std::vector>(b);
- m.sample(50e3, 10e3, 1e4, 5);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(50e3, 5);
+
cout << "samples: " << a_hist.size() << endl;
cout << "a: " << endl << mean(a_hist.begin(),a_hist.end()) << endl;
cout << "b: " << mean(b_hist.begin(),b_hist.end()) << endl;
View
6 test/varying.coefs.global.prior.cpp
@@ -82,9 +82,11 @@ int main() {
std::vector<double>& tau_y_hist = m.track<std::vector>(tau_y);
std::vector<double>& rsq_hist = m.track<std::vector>(rsq);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 5);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 10);
cout << "lm coefs" << endl << coefs;
cout << "err sd: " << stddev(err,0) << endl;;
cout << "err tau: " << pow(stddev(err,0),-2) << endl;
View
7 test/varying.coefs.test.cpp
@@ -64,8 +64,11 @@ int main() {
std::vector<double>& tau_y_hist = m.track<std::vector>(tau_y);
std::vector<double>& rsq_hist = m.track<std::vector>(rsq);
- int iterations = 1e5;
- m.sample(iterations, 1e4, 1e4, 10);
+ m.tune(1e4,100);
+ m.tune_global(1e4,100);
+ m.burn(1e4);
+ m.sample(1e5, 5);
+
cout << "lm coefs" << endl << coefs;
cout << "err sd: " << stddev(err,0) << endl;;
cout << "err tau: " << pow(stddev(err,0),-2) << endl;

0 comments on commit 1c6e5de

Please sign in to comment.