BayesOpt
mcmc_sampler.hpp
Go to the documentation of this file.
1 
2 /*
3 -------------------------------------------------------------------------
4  This file is part of BayesOpt, an efficient C++ library for
5  Bayesian optimization.
6 
7  Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
8 
9  BayesOpt is free software: you can redistribute it and/or modify it
10  under the terms of the GNU Affero General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  BayesOpt is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Affero General Public License for more details.
18 
19  You should have received a copy of the GNU Affero General Public License
20  along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
21 ------------------------------------------------------------------------
22 */
23 
24 
25 #ifndef _MCMC_SAMPLER_HPP_
26 #define _MCMC_SAMPLER_HPP_
27 
28 #include <boost/scoped_ptr.hpp>
29 #include "randgen.hpp"
30 #include "optimizable.hpp"
31 
32 namespace bayesopt {
33 
34  // We plan to add more in the future
35  typedef enum {
38 
39 
51  {
52  public:
62  MCMCSampler(RBOptimizable* rbo, size_t dim, randEngine& eng);
63  virtual ~MCMCSampler();
64 
66  void setAlgorithm(McmcAlgorithms newAlg);
67 
69  void setNParticles(size_t nParticles);
70 
75  void setNBurnOut(size_t nParticles);
76 
81  void run(vectord &Xnext);
82 
83  vectord getParticle(size_t i);
84 
85  void printParticles();
86 
87  private:
88  void randomJump(vectord &x);
89  void burnOut(vectord &x);
90  void sliceSample(vectord &x);
91 
92  boost::scoped_ptr<RBOptimizableWrapper> obj;
93 
94  McmcAlgorithms mAlg;
95  size_t mDims;
96  size_t nBurnOut;
97  size_t nSamples;
98  bool mStepOut;
99 
100  vectord mSigma;
101  vecOfvec mParticles;
102  randEngine& mtRandom;
103 
104  private: //Forbidden
105  MCMCSampler();
106  MCMCSampler(MCMCSampler& copy);
107  };
108 
109  inline void MCMCSampler::setAlgorithm(McmcAlgorithms newAlg)
110  { mAlg = newAlg; };
111 
112  inline void MCMCSampler::setNParticles(size_t nParticles)
113  { nSamples = nParticles; };
114 
115  inline void MCMCSampler::setNBurnOut(size_t nParticles)
116  { nBurnOut = nParticles; };
117 
118  inline vectord MCMCSampler::getParticle(size_t i)
119  { return mParticles[i]; };
120 
121  inline void MCMCSampler::printParticles()
122  {
123  for(size_t i=0; i<mParticles.size(); ++i)
124  {
125  FILE_LOG(logDEBUG) << i << "->" << mParticles[i]
126  << " | Log-lik " << -obj->evaluate(mParticles[i]);
127  }
128  }
129 
130 } //namespace bayesopt
131 
132 
133 #endif
Namespace of the library interface.
Definition: using.dox:1
Slice sampling.
Abstract class for optimizable objects.
Boost types for random number generation.
Markov Chain Monte Carlo sampler.
void run(vectord &Xnext)
Compute the set of particles according to the target PDF.
void setNParticles(size_t nParticles)
Sets the number of particles that are stored.
void setNBurnOut(size_t nParticles)
Usually, the initial samples of any MCMC method are biased and they are discarded.
void setAlgorithm(McmcAlgorithms newAlg)
Sets the sampling algorithm (slice, MH, etc.)