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

Generated on Tue May 4 16:52:14 2010 for GPLIB++ by  doxygen 1.5.8