28 MCMCSampler::MCMCSampler(
RBOptimizable* rbo,
size_t dim, randEngine& eng):
36 mSigma = svectord(dim,6);
39 MCMCSampler::~MCMCSampler()
42 void MCMCSampler::randomJump(vectord &x)
44 randNFloat sample( mtRandom, normalDist(0,1) );
45 FILE_LOG(logERROR) <<
"Doing random jump.";
46 for(vectord::iterator it = x.begin(); it != x.end(); ++it)
50 FILE_LOG(logERROR) <<
"Likelihood." << x <<
" | " << obj->evaluate(x);
54 void MCMCSampler::burnOut(vectord &x)
56 for(
size_t i=0; i<nBurnOut; ++i)
62 catch(std::runtime_error& e)
64 FILE_LOG(logERROR) << e.what();
71 void MCMCSampler::sliceSample(vectord &x)
73 randFloat sample( mtRandom, realUniformDist(0,1) );
80 for (
size_t i = 0; i<n; ++i)
82 const size_t ind = perms[i];
83 const double sigma = mSigma(ind);
85 const double y_max = -obj->evaluate(x);
86 const double y = y_max+std::log(sample());
90 throw std::runtime_error(
"Error in MCMC: Initial point" 91 " out of support region.");
95 const double x_cur = x(ind);
96 const double r = sample();
97 double xl = x_cur - r * sigma;
98 double xr = x_cur + (1-r)*sigma;
103 while (-obj->evaluate(x) > y) { x(ind) -= sigma; }
107 while (-obj->evaluate(x) > y) { x(ind) += sigma; }
112 bool on_slice =
false;
115 x(ind) = (xr-xl) * sample() + xl;
116 if (-obj->evaluate(x) < y)
118 if (x(ind) > x_cur) xr = x(ind);
119 else if (x(ind) < x_cur) xl = x(ind);
120 else throw std::runtime_error(
"Error in MCMC. Slice colapsed.");
133 randFloat sample( mtRandom, realUniformDist(0,1) );
134 if (nBurnOut>0) burnOut(Xnext);
137 for(
size_t i=0; i<nSamples; ++i)
143 catch(std::runtime_error& e)
145 FILE_LOG(logERROR) << e.what();
148 mParticles.push_back(Xnext);
Namespace of the library interface.
Latin Hypercube Sampling.
void randomPerms(D &arr, randEngine &mtRandom)
Modify an array using ramdom permutations.
void run(vectord &Xnext)
Compute the set of particles according to the target PDF.
Markov Chain Monte Carlo algorithms.
std::vector< int > return_index_vector(size_t n)
Generates a vector of indexes (1..n)
Modules and helper macros for logging.