CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Sampler.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2014 by Federico Marulli and Alfonso Veropalumbo *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #ifndef __SAMP__
35 #define __SAMP__
36 
37 #include "Distribution.h"
38 
39 
40 // ============================================================================================
41 
42 
43 namespace cbl {
44 
45  namespace statistics {
46 
51  enum class SamplerType {
52 
55 
58 
59  };
60 
67  inline std::vector<std::string> SamplerTypeNames () { return {"MetropolisHastings", "StretchMove"}; }
68 
77  inline SamplerType SamplerTypeCast (const int samplerTypeIndex) { return castFromValue<SamplerType>(samplerTypeIndex); }
78 
86  inline SamplerType SamplerTypeCast (const std::string samplerTypeName) { return castFromName<SamplerType>(samplerTypeName, SamplerTypeNames()); }
87 
95  inline std::vector<SamplerType> SamplerTypeCast (const std::vector<int> samplerTypeIndeces) { return castFromValues<SamplerType>(samplerTypeIndeces); }
96 
104  inline std::vector<SamplerType> SamplerTypeCast (const std::vector<std::string> samplerTypeNames) { return castFromNames<SamplerType>(samplerTypeNames, SamplerTypeNames()); }
105 
114  class Sampler
115  {
116 
117  protected:
118 
121 
124 
126  int m_npar;
127 
130 
132  std::function<double(std::vector<double> &)> m_function;
133 
135  std::vector<double> m_acceptance;
136 
139 
141  std::vector<std::vector<double>> m_function_chain;
142 
144  std::vector<std::vector<std::vector<double>>> m_chains;
145 
155  std::shared_ptr<random::DistributionRandomNumbers>m_set_gz (const int seed, const double aa=2);
156 
165  void m_initialize_chains (const std::vector<std::vector<double>> start);
166 
175  void m_initialize_chains_parallel (const std::vector<std::vector<double>> start);
176 
192  void m_sample_stretch_move_parallel_cpp (const int chain_size, const int nwalkers, const std::vector<std::vector<double>> start, const int seed=4241, const double aa=2);
193 
210  void m_sample_stretch_move_parallel_py (const int chain_size, const int nwalkers, const std::vector<std::vector<double>> start, const int seed=4241, const double aa=2);
211 
212  public:
213 
218 
222  Sampler () = default;
223 
229  Sampler (const int npar, const std::function<double(std::vector<double> &)> function) : m_npar(npar), m_npar_free(npar) {set_function(function);}
230 
237  Sampler (const int npar, const int npar_free, const std::function<double(std::vector<double> &)> function) : m_npar(npar), m_npar_free(npar_free) {set_function(function);}
238 
243  ~Sampler () = default;
244 
246 
254  double operator () (std::vector<double> &pp)
255  {
256  return m_function(pp);
257  }
258 
270  double get_chain (const int par, const int chain, const int step) {return m_chains[step][chain][par];}
271 
277  std::vector<std::vector<std::vector<double>>> get_chain () {return m_chains;}
278 
288  double get_function (const int chain, const int step) {return m_function_chain[step][chain];}
289 
299  void get_chain_function_acceptance(std::vector<std::vector<double>> &chains, std::vector<double> &function, std::vector<double> &acceptance, const int start=0, const int thin=1);
300 
309  void set_chain (const int npar, const int npar_free, const int chain_size, const int nwalkers);
310 
316  void set_function (const std::function<double(std::vector<double> &)> function);
317 
331  void sample_stretch_move (const int chain_size, const int nwalkers, const std::vector<std::vector<double>> start, const int seed=4241, const double aa=2, const std::string outputFile=cbl::par::defaultString);
332 
349  void sample_stretch_move_parallel (const int chain_size, const int nwalkers, const std::vector<std::vector<double>> start, const int seed=4241, const double aa=2);
350 
359  void write_chain (const std::string dir_output, const std::string file, const int start, const int thin);
360 
361  };
362  }
363 }
364 
365 #endif
The class Distribution.
The class Sampler.
Definition: Sampler.h:115
int m_chain_size
size of the chains
Definition: Sampler.h:123
double get_function(const int chain, const int step)
get the function value
Definition: Sampler.h:288
bool m_use_python
use python-defined function
Definition: Sampler.h:138
void write_chain(const std::string dir_output, const std::string file, const int start, const int thin)
write the chains in an output file
Definition: Sampler.cpp:431
std::vector< std::vector< std::vector< double > > > get_chain()
return the chains
Definition: Sampler.h:277
void sample_stretch_move(const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2, const std::string outputFile=cbl::par::defaultString)
sample the input function using the stretch-move algorithm on n-dimensional parameter space
Definition: Sampler.cpp:133
std::vector< double > m_acceptance
chain acceptance ratio
Definition: Sampler.h:135
Sampler(const int npar, const int npar_free, const std::function< double(std::vector< double > &)> function)
constructor
Definition: Sampler.h:237
void m_sample_stretch_move_parallel_cpp(const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
sample the input function using the stretch-move algorithm on n-dimensional parameter space....
Definition: Sampler.cpp:225
void get_chain_function_acceptance(std::vector< std::vector< double >> &chains, std::vector< double > &function, std::vector< double > &acceptance, const int start=0, const int thin=1)
get the chain values and the function
Definition: Sampler.cpp:411
void set_chain(const int npar, const int npar_free, const int chain_size, const int nwalkers)
function to set the chains
Definition: Sampler.cpp:90
Sampler(const int npar, const std::function< double(std::vector< double > &)> function)
constructor
Definition: Sampler.h:229
int m_npar_free
number of free parameters
Definition: Sampler.h:129
int m_nwalkers
number of chains
Definition: Sampler.h:120
void set_function(const std::function< double(std::vector< double > &)> function)
set the function
Definition: Sampler.cpp:112
void sample_stretch_move_parallel(const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
sample the input function using the stretch-move algorithm on n-dimensional parameter space - paralle...
Definition: Sampler.cpp:396
int m_npar
number of parameters
Definition: Sampler.h:126
std::shared_ptr< random::DistributionRandomNumbers > m_set_gz(const int seed, const double aa=2)
return the random generator for the stretch-move
Definition: Sampler.cpp:43
void m_initialize_chains_parallel(const std::vector< std::vector< double >> start)
initialize chains
Definition: Sampler.cpp:76
double operator()(std::vector< double > &pp)
evaluate the function
Definition: Sampler.h:254
std::function< double(std::vector< double > &)> m_function
the function to be sampled
Definition: Sampler.h:132
double get_chain(const int par, const int chain, const int step)
get the chain value
Definition: Sampler.h:270
~Sampler()=default
default destructor
Sampler()=default
default constructor
void m_initialize_chains(const std::vector< std::vector< double >> start)
initialize chains
Definition: Sampler.cpp:62
std::vector< std::vector< double > > m_function_chain
value of the function at sampled points
Definition: Sampler.h:141
void m_sample_stretch_move_parallel_py(const int chain_size, const int nwalkers, const std::vector< std::vector< double >> start, const int seed=4241, const double aa=2)
sample the input function using the stretch-move algorithm on n-dimensional parameter space - paralle...
Definition: Sampler.cpp:312
std::vector< std::vector< std::vector< double > > > m_chains
the chains
Definition: Sampler.h:144
static const std::string defaultString
default std::string value
Definition: Constants.h:336
SamplerType
the parameter type
Definition: Sampler.h:51
@ _MetropolisHastings_
Metropolis-Hastings sampler.
@ _StretchMove_
stretch-move sampler (Goodman & Weare 2010, Foreman-Mackey et al. 2012)
std::vector< std::string > SamplerTypeNames()
return a vector containing the SamplerType names
Definition: Sampler.h:67
SamplerType SamplerTypeCast(const int samplerTypeIndex)
cast an enum of type SamplerType from its index
Definition: Sampler.h:77
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38