BayesOpt
mcmc_sampler.cpp
1 /*
2 -------------------------------------------------------------------------
3  This file is part of BayesOpt, an efficient C++ library for
4  Bayesian optimization.
5 
6  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
7 
8  BayesOpt is free software: you can redistribute it and/or modify it
9  under the terms of the GNU Affero General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  BayesOpt is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Affero General Public License for more details.
17 
18  You should have received a copy of the GNU Affero General Public License
19  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
20 ------------------------------------------------------------------------
21 */
22 #include "log.hpp"
23 #include "lhs.hpp"
24 #include "mcmc_sampler.hpp"
25 
26 namespace bayesopt
27 {
28  MCMCSampler::MCMCSampler(RBOptimizable* rbo, size_t dim, randEngine& eng):
29  mtRandom(eng), obj(new RBOptimizableWrapper(rbo))
30  {
31  mAlg = SLICE_MCMC;
32  mDims = dim;
33  nBurnOut = 100;
34  nSamples = 10;
35  mStepOut = true;
36  mSigma = svectord(dim,6);
37  };
38 
39  MCMCSampler::~MCMCSampler()
40  {};
41 
42  void MCMCSampler::randomJump(vectord &x)
43  {
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)
47  {
48  *it = sample()*6;
49  }
50  FILE_LOG(logERROR) << "Likelihood." << x << " | " << obj->evaluate(x);
51  }
52 
53  //TODO: Include new algorithms when we add them.
54  void MCMCSampler::burnOut(vectord &x)
55  {
56  for(size_t i=0; i<nBurnOut; ++i)
57  {
58  try
59  {
60  sliceSample(x);
61  }
62  catch(std::runtime_error& e)
63  {
64  FILE_LOG(logERROR) << e.what();
65  randomJump(x);
66  }
67  }
68  }
69 
70 
71  void MCMCSampler::sliceSample(vectord &x)
72  {
73  randFloat sample( mtRandom, realUniformDist(0,1) );
74  size_t n = x.size();
75 
76  std::vector<int> perms = utils::return_index_vector(0,n);
77 
78  utils::randomPerms(perms, mtRandom);
79 
80  for (size_t i = 0; i<n; ++i)
81  {
82  const size_t ind = perms[i];
83  const double sigma = mSigma(ind);
84 
85  const double y_max = -obj->evaluate(x);
86  const double y = y_max+std::log(sample());
87 
88  if (y == 0.0)
89  {
90  throw std::runtime_error("Error in MCMC: Initial point"
91  " out of support region.");
92  }
93 
94  // Step out
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;
99 
100  if (mStepOut)
101  {
102  x(ind) = xl;
103  while (-obj->evaluate(x) > y) { x(ind) -= sigma; }
104  xl = x(ind);
105 
106  x(ind) = xr;
107  while (-obj->evaluate(x) > y) { x(ind) += sigma; }
108  xr = x(ind);
109  }
110 
111  //Shrink
112  bool on_slice = false;
113  while (!on_slice)
114  {
115  x(ind) = (xr-xl) * sample() + xl;
116  if (-obj->evaluate(x) < y)
117  {
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.");
121  }
122  else
123  {
124  on_slice = true;
125  }
126  }
127  }
128  }
129 
130  //TODO: Include new algorithms when we add them.
131  void MCMCSampler::run(vectord &Xnext)
132  {
133  randFloat sample( mtRandom, realUniformDist(0,1) );
134  if (nBurnOut>0) burnOut(Xnext);
135 
136  mParticles.clear();
137  for(size_t i=0; i<nSamples; ++i)
138  {
139  try
140  {
141  sliceSample(Xnext);
142  }
143  catch(std::runtime_error& e)
144  {
145  FILE_LOG(logERROR) << e.what();
146  randomJump(Xnext);
147  }
148  mParticles.push_back(Xnext);
149  }
150  printParticles();
151  }
152 
153 }// namespace bayesopt
Namespace of the library interface.
Definition: using.dox:1
Slice sampling.
Latin Hypercube Sampling.
void randomPerms(D &arr, randEngine &mtRandom)
Modify an array using ramdom permutations.
Definition: lhs.hpp:80
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)
Definition: indexvector.hpp:50
Modules and helper macros for logging.