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 /** \addtogroup statistics Statistical methods */
00016 /* @{ */
00017 
00018 //! Implementation of the Bootstrap error estimation method
00019 template <class SampleGenerator>
00020 class Bootstrap : public StatErrEst<SampleGenerator>
00021 {
00022 private:
00023    //! The place of the actual implementation
00024         virtual void CalcErrors_Imp(double &TheMean, double &TheVar);
00025 public:
00026   //! The constructor passes everything to the base
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             //first check whether the procedure is trivial
00037                 if (this->nrealizations < 2)
00038                 {
00039                         TheMean = Mean(this->Samples.begin(),this->Samples.end()); //we have at most one, but maybe no values
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))); //we have to create random subsamples
00047                 boost::uniform_int<> int_dist(0,this->nrealizations-1); //so we need random numbers between 0 and size-1
00048                 boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > randomindex(generator, int_dist); //this object generates them
00049                 for (int i = 0; i <  this->nrealizations; ++i) //for the required number of realizations
00050                 {
00051                         for(int j = 0; j < this->nrealizations; j++) //create a new subsample with replacement of the same size
00052                         {
00053                             BootSample.at(j) = this->Samples.at(randomindex()); //by copying random elements into the sample
00054                         }
00055                         Pseudo.push_back(Variance(BootSample.begin(),BootSample.end())); //calculate the variance of the current subsample
00056                 }
00057                 TheMean = Mean(Pseudo.begin(),Pseudo.end()); //Calculate the statistics of the Pseudo Values
00058                 TheVar =Variance(Pseudo.begin(),Pseudo.end(),TheMean);
00059         }
00060 #endif /*CBOOTSTRAP_H_*/

Generated on Fri Jul 4 15:30:20 2008 for GPLIB++ by  doxygen 1.5.5