GPLIB++
Bootstrap.h
Go to the documentation of this file.
1 #ifndef CBOOTSTRAP_H_
2 #define CBOOTSTRAP_H_
3 #include <boost/random/lagged_fibonacci.hpp>
4 #include <boost/random/variate_generator.hpp>
5 #include <boost/random/uniform_int.hpp>
6 
7 #include <iostream>
8 #include <algorithm>
9 #include <numeric>
10 #include <vector>
11 #include <ctime>
12 #include "StatErrEst.h"
13 #include "statutils.h"
14 
15 namespace gplib
16  {
17 
18  /** \addtogroup statistics Statistical methods */
19  /* @{ */
20 
21  //! Implementation of the Bootstrap error estimation method
22  template<class SampleGenerator>
23  class Bootstrap: public StatErrEst<SampleGenerator>
24  {
25  private:
26  //! The place of the actual implementation
27  virtual void CalcErrors_Imp(double &TheMean, double &TheVar);
28  public:
29  //! The constructor passes everything to the base
30  Bootstrap(const int nrea, SampleGenerator TheGenerator) :
31  StatErrEst<SampleGenerator>::StatErrEst(nrea, TheGenerator)
32  {
33  }
34  virtual ~Bootstrap()
35  {
36  }
37  };
38  /* @} */
39 
40  template<class SampleGenerator>
41  void Bootstrap<SampleGenerator>::CalcErrors_Imp(double &TheMean,
42  double &TheVar)
43  {
44  //first check whether the procedure is trivial
45  if (this->nrealizations < 2)
46  {
47  TheMean = Mean(this->Samples.begin(), this->Samples.end()); //we have at most one, but maybe no values
48  TheVar = 0;
49  return;
50  }
51  std::vector<double> Pseudo;
52  Pseudo.reserve(this->nrealizations);
53  std::vector<double> BootSample(this->nrealizations);
54  boost::lagged_fibonacci607 generator(
55  static_cast<unsigned int> (std::time(0))); //we have to create random subsamples
56  boost::uniform_int<> int_dist(0, this->nrealizations - 1); //so we need random numbers between 0 and size-1
57  boost::variate_generator<boost::lagged_fibonacci607&,
58  boost::uniform_int<> > randomindex(generator, int_dist); //this object generates them
59  for (int i = 0; i < this->nrealizations; ++i) //for the required number of realizations
60  {
61  for (int j = 0; j < this->nrealizations; j++) //create a new subsample with replacement of the same size
62  {
63  BootSample.at(j) = this->Samples.at(randomindex()); //by copying random elements into the sample
64  }
65  Pseudo.push_back(Variance(BootSample.begin(), BootSample.end())); //calculate the variance of the current subsample
66  }
67  TheMean = Mean(Pseudo.begin(), Pseudo.end()); //Calculate the statistics of the Pseudo Values
68  TheVar = Variance(Pseudo.begin(), Pseudo.end(), TheMean);
69  }
70  }
71 #endif /*CBOOTSTRAP_H_*/
virtual ~Bootstrap()
Definition: Bootstrap.h:34
This class is used as a base for stochastic error estimation.
Definition: StatErrEst.h:19
Implementation of the Bootstrap error estimation method.
Definition: Bootstrap.h:23
std::iterator_traits< InputIterator >::value_type Mean(InputIterator begin, InputIterator end)
Calculate the mean for a given range.
Definition: statutils.h:21
std::iterator_traits< InputIterator >::value_type Variance(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mv)
Calculate the Variance and give the mean as a third input parameter.
Definition: statutils.h:30
Bootstrap(const int nrea, SampleGenerator TheGenerator)
The constructor passes everything to the base.
Definition: Bootstrap.h:30