jacknifetest.cpp

Go to the documentation of this file.
00001 #include "MTStation.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 <vector>
00009 #include <iostream>
00010 #include <ctime>
00011 #include <complex>
00012 #include <algorithm>
00013 #include <numeric>
00014 #include <cmath>
00015  #include <fstream>
00016  #include "Jacknife.h"
00017  #include "Bootstrap.h"
00018  #include "MTTensor.h"
00019  #include <time.h>
00020 
00021 void wait ( int seconds )
00022 {
00023   clock_t endwait;
00024   endwait = time (0) + seconds ;
00025   while (time(0) < endwait) {}
00026 }
00027 
00028  double CalcRhoa(const std::complex<double> Z, const double frequency)
00029 {
00030         return 1./(2 * PI * frequency) * mu * (pow(Z.real(),2)+pow(Z.imag(),2)) * pow(1000.0,2);
00031     //return Z.real();
00032 }
00033  
00034  
00035 int main()
00036 {
00037    int nsamples =1000;
00038   
00039    std::cout<< "Number of samples: " ;
00040    std::cin >> nsamples;
00041    
00042    std::vector<double> RhoValues(nsamples);
00043    boost::lagged_fibonacci607 generator(static_cast<unsigned int>(std::time(0)));
00044   
00045    CMTStation Station;
00046    
00047    Station.GetData("bel.mtt");
00048    CJacknife Jacknife(nsamples);
00049    wait(2);
00050    CBootstrap Bootstrap(nsamples);
00051    double JackMean, JackVar;
00052    double BootMean, BootVar;
00053    Jacknife.CalcErrors(&MTTensor::GetRhoxy,Station.GetMTData().at(0),JackMean,JackVar);
00054    Bootstrap.CalcErrors(&MTTensor::GetRhoxy,Station.GetMTData().at(0),BootMean,BootVar);
00055    const double Zerr = Station.GetMTData().at(0).GetdZxy();
00056    const double frequency = Station.GetMTData().at(0).GetFrequency();
00057    const std::complex<double> Zmean(Station.GetMTData().at(0).GetZxy()); 
00058    
00059    
00060   /* 
00061    boost::normal_distribution<> real_dist(Station.GetMTData().at(0).GetZxx().real(),Station.GetMTData().at(0).GetdZxx())
00062         , imag_dist(Station.GetMTData().at(0).GetZxx().imag(),Station.GetMTData().at(0).GetdZxx());
00063    //boost::uniform_real<> real_dist(Zmean.real()-Zerr,Zmean.real()+Zerr), imag_dist(Zmean.imag()-Zerr,Zmean.imag()+Zerr);
00064    boost::uniform_int<> int_dist(0,nsamples-1);
00065    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > randomreal(generator, real_dist);
00066    boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > randomimag(generator, imag_dist);
00067    boost::variate_generator<boost::lagged_fibonacci607&, boost::uniform_int<> > randomindex(generator, int_dist);
00068    std::ofstream zvalues("zvalues");  
00069    std::cout << "Rhoxx: " << Station.GetMTData().at(0).GetRhoxx() << std::endl;
00070    for(int i = 0; i < nsamples; i++)
00071    {
00072            std::complex<double> number(randomreal(),randomimag());
00073        RhoValues.at(i) = CalcRhoa(number,frequency);
00074        zvalues << number.real() << std::endl;
00075        //zvalues << " " << number.imag() << std::endl;
00076    }
00077    std::ofstream rhovalues("rhovalues");
00078   
00079    for(int i = 0; i < nsamples; i++)
00080            rhovalues << RhoValues.at(i) << std::endl; 
00081         
00082         std::vector<double> Pseudo(RhoValues);
00083         std::ofstream pseudovalues("pseudovalues");
00084         double initmean = std::accumulate(RhoValues.begin(),RhoValues.end(),0.)/RhoValues.size();
00085         for (int i = 0; i < nsamples; ++i)
00086         {
00087                 
00088                 std::vector<double> Sample(RhoValues);
00089                 std::vector<double>::iterator it(Sample.begin());
00090                 std::advance(it,i);
00091                 Sample.erase(it);
00092                 Pseudo.at(i) = nsamples * initmean - std::accumulate(Sample.begin(),Sample.end(),0.);
00093                 pseudovalues << Pseudo.at(i) << std::endl;
00094         }
00095         double jackmean = std::accumulate(Pseudo.begin(),Pseudo.end(),0.)/nsamples;
00096         double sumdev = 0;
00097         for (int i = 0; i < nsamples; ++i)
00098            sumdev += pow(Pseudo.at(i) - jackmean,2);
00099         double jackvar =sqrt( 1./double(nsamples -1) * sumdev);
00100         std::cout << "Jackmean: " << jackmean << " JackVar: " << jackvar << std::endl;
00101         */
00102         double drhoa = 1./(PI * frequency) * mu * abs(Zmean) * Zerr * pow(1000.0,2);
00103         double rhoa = CalcRhoa(Zmean,frequency);
00104         std::cout << "Propmean: " << rhoa << " Properr: " << drhoa << std::endl;
00105         std::cout << "Jackmean: " << JackMean << " Jackerr: " << JackVar << std::endl;
00106         std::cout << "Bootmean: " << BootMean << " Booterr: " << BootVar << std::endl;
00107 }

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