OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SampleGaussianSequence.h
Go to the documentation of this file.
1 #ifndef OPAL_SAMPLE_GAUSSIAN_SEQUENCE_H
2 #define OPAL_SAMPLE_GAUSSIAN_SEQUENCE_H
3 
5 #include "Utilities/Util.h"
6 
7 #ifdef WITH_UNIT_TESTS
8 #include <gtest/gtest_prod.h>
9 #endif
10 
12 {
13  // provides a sequence of sampling points that have a Gaussian distribution
14  // with
15  // mean = 0.5 * (upper + lower)
16  // sigma = (upper - lower) / 10
17  // This can be achieved if the integral of the Gaussian between the sampling
18  // points are all equal. The sampling points are therefore computed using
19  // the inverse error function at equally distributed arguments between
20  // -1 and 1.
21 
22 public:
23 
24  SampleGaussianSequence(double lower, double upper, size_t modulo, int nSample)
25  : numSamples_m(nSample)
26  , volumeLowerDimensions_m(modulo)
27  {
28  double mean = 0.5 * (lower + upper);
29  double sigma = (upper - lower) / 10; // +- 5 sigma
30  double factor = sigma / sqrt(2);
31  double dx = 2.0 / nSample;
32  for (long i = 0; i < nSample; ++ i) {
33  double x = -1.0 + (i + 0.5) * dx;
34  double y = Util::erfinv(x);
35  sampleChain_m.push_back(mean + factor * y);
36  }
37  }
38 
39  void create(boost::shared_ptr<SampleIndividual>& ind, size_t i) {
40  ind->genes[i] = getNext(ind->id);
41  }
42 
43  double getNext(unsigned int id) {
44  int bin = int(id / volumeLowerDimensions_m) % numSamples_m;
45 
46  double sample = sampleChain_m[bin];
47  return sample;
48  }
49 
50 private:
51 #ifdef WITH_UNIT_TESTS
52  FRIEND_TEST(GaussianSampleTest, ChainTest);
53 #endif
54  std::vector<double> sampleChain_m;
55  unsigned int numSamples_m; // size of this "dimension"
56  size_t volumeLowerDimensions_m; // the "volume" of the sampling space of the lower "dimensions"
57 };
58 
59 #endif
void create(boost::shared_ptr< SampleIndividual > &ind, size_t i)
std::vector< double > sampleChain_m
SampleGaussianSequence(double lower, double upper, size_t modulo, int nSample)
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
double getNext(unsigned int id)
double erfinv(double x)
Definition: Util.cpp:39