Bootstrap.h
Go to the documentation of this file.00001 #ifndef CBOOTSTRAP_H_
00002 #define CBOOTSTRAP_H_
00003 #include <boost/random/lagged_fibonacci.hpp>
00004 #include <boost/random/variate_generator.hpp>
00005 #include <boost/random/uniform_int.hpp>
00006
00007 #include <iostream>
00008 #include <algorithm>
00009 #include <numeric>
00010 #include <vector>
00011 #include <ctime>
00012 #include "StatErrEst.h"
00013 #include "statutils.h"
00014
00015 namespace gplib
00016 {
00017
00018
00019
00020
00021
00022 template<class SampleGenerator>
00023 class Bootstrap: public StatErrEst<SampleGenerator>
00024 {
00025 private:
00026
00027 virtual void CalcErrors_Imp(double &TheMean, double &TheVar);
00028 public:
00029
00030 Bootstrap(const int nrea, SampleGenerator TheGenerator) :
00031 StatErrEst<SampleGenerator>::StatErrEst(nrea, TheGenerator)
00032 {
00033 }
00034 virtual ~Bootstrap()
00035 {
00036 }
00037 };
00038
00039
00040 template<class SampleGenerator>
00041 void Bootstrap<SampleGenerator>::CalcErrors_Imp(double &TheMean,
00042 double &TheVar)
00043 {
00044
00045 if (this->nrealizations < 2)
00046 {
00047 TheMean = Mean(this->Samples.begin(), this->Samples.end());
00048 TheVar = 0;
00049 return;
00050 }
00051 std::vector<double> Pseudo;
00052 Pseudo.reserve(this->nrealizations);
00053 std::vector<double> BootSample(this->nrealizations);
00054 boost::lagged_fibonacci607 generator(
00055 static_cast<unsigned int> (std::time(0)));
00056 boost::uniform_int<> int_dist(0, this->nrealizations - 1);
00057 boost::variate_generator<boost::lagged_fibonacci607&,
00058 boost::uniform_int<> > randomindex(generator, int_dist);
00059 for (int i = 0; i < this->nrealizations; ++i)
00060 {
00061 for (int j = 0; j < this->nrealizations; j++)
00062 {
00063 BootSample.at(j) = this->Samples.at(randomindex());
00064 }
00065 Pseudo.push_back(Variance(BootSample.begin(), BootSample.end()));
00066 }
00067 TheMean = Mean(Pseudo.begin(), Pseudo.end());
00068 TheVar = Variance(Pseudo.begin(), Pseudo.end(), TheMean);
00069 }
00070 }
00071 #endif