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
00016
00017
00018
00019 template <class SampleGenerator>
00020 class Bootstrap : public StatErrEst<SampleGenerator>
00021 {
00022 private:
00023
00024 virtual void CalcErrors_Imp(double &TheMean, double &TheVar);
00025 public:
00026
00027 Bootstrap(const int nrea, SampleGenerator TheGenerator):
00028 StatErrEst<SampleGenerator>::StatErrEst(nrea,TheGenerator){}
00029 virtual ~Bootstrap(){}
00030 };
00031
00032
00033 template <class SampleGenerator>
00034 void Bootstrap<SampleGenerator>::CalcErrors_Imp(double &TheMean, double &TheVar)
00035 {
00036
00037 if (this->nrealizations < 2)
00038 {
00039 TheMean = Mean(this->Samples.begin(),this->Samples.end());
00040 TheVar = 0;
00041 return;
00042 }
00043 std::vector<double> Pseudo;
00044 Pseudo.reserve(this->nrealizations);
00045 std::vector<double> BootSample(this->nrealizations);
00046 boost::lagged_fibonacci607 generator(static_cast<unsigned int>(std::time(0)));
00047 boost::uniform_int<> int_dist(0,this->nrealizations-1);
00048 boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > randomindex(generator, int_dist);
00049 for (int i = 0; i < this->nrealizations; ++i)
00050 {
00051 for(int j = 0; j < this->nrealizations; j++)
00052 {
00053 BootSample.at(j) = this->Samples.at(randomindex());
00054 }
00055 Pseudo.push_back(Variance(BootSample.begin(),BootSample.end()));
00056 }
00057 TheMean = Mean(Pseudo.begin(),Pseudo.end());
00058 TheVar =Variance(Pseudo.begin(),Pseudo.end(),TheMean);
00059 }
00060 #endif