bootstraptest.cpp

Go to the documentation of this file.
00001 #include "CMTStation.h"
00002 #include <boost/random/lagged_fibonacci.hpp>
00003 #include <boost/random/normal_distribution.hpp>
00004 #include <boost/random/uniform_int.hpp>
00005 #include <boost/random/uniform_real.hpp>
00006 #include <boost/random/variate_generator.hpp>
00007 #include <boost/generator_iterator.hpp>
00008 #include <boost/numeric/ublas/vector.hpp>
00009 #include <boost/numeric/ublas/io.hpp>
00010 #include <iostream>
00011 #include <ctime>
00012 #include <complex>
00013 #include <algorithm>
00014 #include <cmath>
00015  #include <fstream>
00016  #include "VecMat.h"
00017  
00018  namespace ublas = boost::numeric::ublas;
00019  
00020 double CalcRhoa(const std::complex<double> Z, const double frequency)
00021 {
00022         return 1./(2 * PI * frequency) * mu * (pow(Z.real(),2)+pow(Z.imag(),2)) * pow(1000.0,2);
00023     //return Z.real();
00024 }
00025 
00026 int main()
00027 {
00028    int nsamples =1000;
00029    int nbootstrap = 1000;
00030    std::cout<< "Number of samples: " ;
00031    std::cin >> nsamples;
00032    std::cout<< "Number of bootstraps: " ;
00033    std::cin >> nbootstrap;
00034    
00035    gplib::rvec RhoValues(nsamples);
00036    boost::lagged_fibonacci607 generator(static_cast<unsigned int>(std::time(0)));
00037    const double Zerr = 0.1;
00038    const double frequency = 0.1;
00039    const std::complex<double> Zmean(1.,1.);
00040    CMTStation Station;
00041    boost::normal_distribution<> real_dist(Zmean.real(),Zerr), imag_dist(Zmean.imag(),Zerr);
00042    //boost::uniform_real<> real_dist(Zmean.real()-Zerr,Zmean.real()+Zerr), imag_dist(Zmean.imag()-Zerr,Zmean.imag()+Zerr);
00043    boost::uniform_int<> int_dist(0,nsamples-1);
00044    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > randomreal(generator, real_dist);
00045    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > randomimag(generator, imag_dist);
00046    boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > randomindex(generator, int_dist);
00047    std::ofstream zvalues("zvalues");  
00048    for(int i = 0; i < nsamples; i++)
00049    {
00050            std::complex<double> number(randomreal(),randomimag());
00051        RhoValues(i) = CalcRhoa(number,frequency);
00052        zvalues << number.real() << std::endl;
00053        //zvalues << " " << number.imag() << std::endl;
00054    }
00055    std::ofstream rhovalues("rhovalues");
00056    gplib::rvec Means(nbootstrap);
00057    for(int i = 0; i < nsamples; i++)
00058            rhovalues << RhoValues(i) << std::endl; 
00059         std::ofstream meanvalues("meanvalues");
00060         for (int i = 0; i < nbootstrap; ++i)
00061         {
00062                 gplib::rvec Sample(RhoValues);
00063                 for(int j = 0; j < nsamples; j++)
00064                 {
00065                         int index = randomindex();
00066                         //std::cout << "j: " << j << " Sample(j): " << Sample(j) << " Index: "<< index <<" Sample(index): " << RhoValues(index) << std::endl;
00067                     Sample(j) = RhoValues(index);
00068                 }
00069                 //std::cout << Sample << std::endl;
00070                 Means(i) = ublas::sum(Sample)/double(nsamples);
00071                 meanvalues << Means(i) << std::endl;
00072                 //std::cout << Means(i) << std::endl;
00073         }
00074         double bootmean = sum(Means)/double(nbootstrap);
00075         double sumdev = 0;
00076         for (int i = 0; i < nbootstrap; ++i)
00077            sumdev += pow(Means(i) - bootmean,2);
00078         double bootvar =sqrt( 1./double(nbootstrap -1) * sumdev);
00079         double rhomean = ublas::sum(RhoValues)/double(nsamples);
00080         double rhovar = 0;
00081         for (int i = 0; i < nsamples; ++i)
00082            rhovar += pow(RhoValues(i) - rhomean,2);
00083         rhovar =sqrt( 1./double(nsamples -1) * rhovar);
00084         std::cout << "Bootmean: " << bootmean << " BootVar: " << bootvar << std::endl;
00085         std::cout << "Rhomean: " << rhomean << " RhoVar: " << rhovar << std::endl;
00086         std::sort(Means.begin(),Means.end());
00087         int lowerbound = floor(nbootstrap * 0.16);
00088         int upperbound = ceil(nbootstrap * 0.84);
00089         std::cout << "Bootmedian: " << Means(nbootstrap/2) << " BootLower: " << Means(lowerbound) <<
00090            " BootUpper: " << Means(upperbound)  << " Booterr: " <<  Means(upperbound) - Means(lowerbound)<< std::endl;
00091         double drhoa = 1./(PI * 0.1) * mu * abs(Zmean) * Zerr * pow(1000.0,2);
00092         double rhoa = CalcRhoa(Zmean,frequency);
00093         std::cout << "Propmean: " << rhoa << " Properr: " << drhoa << std::endl;
00094 }

Generated on Mon Sep 15 12:54:33 2008 for GPLIB++ by  doxygen 1.5.5