exampleTKDE.C

Go to the documentation of this file.
00001 // Example of using the TKDE class (kernel density estimator)
00002 // Author: Lorenzo Moneta, Bartolomeu Rabacal (Dec 2010)
00003 
00004 #include "TH1.h"
00005 #include "TF1.h"
00006 #include "TKDE.h"
00007 #include "TCanvas.h"
00008 //#include "TStopwatch.h"
00009 #include "TRandom.h"
00010 #ifndef __CINT__
00011 #include "Math/DistFunc.h"
00012 #endif
00013 #include "TLegend.h"
00014 
00015 // test TKDE
00016 
00017 
00018 
00019 
00020 void exampleTKDE(int n = 1000) { 
00021 
00022  // generate some gaussian points
00023 
00024 
00025    int nbin = 100;
00026    double xmin = 0;
00027    double xmax = 10;
00028 
00029    TH1D * h1 = new TH1D("h1","h1",nbin,xmin,xmax);
00030 
00031    // generate some points with bi- gaussian distribution
00032 
00033    std::vector<double> data(n);
00034    for (int i = 0; i < n; ++i) {
00035       if (i < 0.4*n) { 
00036          data[i] = gRandom->Gaus(2,1);
00037          h1->Fill(data[i]);
00038       }
00039       else { 
00040          data[i] = gRandom->Gaus(7,1.5);
00041          h1->Fill(data[i]);
00042       }
00043    }
00044 
00045    // scale histogram
00046    h1->Scale(1./h1->Integral(),"width" );
00047    h1->SetStats(false);
00048    h1->SetTitle("Bi-Gaussian");
00049    h1->Draw();
00050 
00051    // drawn true normalized density 
00052    TF1 * f1 = new TF1("f1","0.4*ROOT::Math::normal_pdf(x,1,2)+0.6*ROOT::Math::normal_pdf(x,1.5,7)",xmin,xmax);
00053    f1->SetLineColor(kGreen+2);
00054    f1->Draw("SAME");
00055 
00056 
00057    ///////////////////////////////////////////////////////////////////////////
00058 
00059    // create TKDE class
00060    double rho = 1.0; //default value
00061    TKDE * kde = new TKDE(n, &data[0], xmin,xmax, "", rho);
00062    //kde->Draw("ConfidenceInterval@0.95 Same");
00063    kde->Draw("SAME");
00064 
00065    // KDE options
00066    // int nbinKDE = 0;
00067    // TKDE::EBinning bin;
00068    // TKDE::EIteration iter;
00069    // TKDE::EMirror mirror;
00070    // if (nbinKDE> 0) kde->SetNBins(nbinKDE);
00071    // if (iter != TKDE::kAdaptive) kde->SetIteration(iter);
00072    // if (mirror != TKDE::kNoMirror) kde->SetMirror(mirror);
00073 
00074 
00075    //alternative way instead of using kde->Draw()
00076    // TF1 * const hk = kde->GetFunction(1000);
00077    // hk->SetLineColor(kBlue);
00078    // hk->Draw("same");
00079    
00080    // TF1 * fl = kde->GetLowerFunction(0.684);
00081    // TF1 * fu = kde->GetUpperFunction(0.684);
00082    // fl->Draw("SAME");
00083    // fl->SetLineColor(kBlue-5);
00084    // fu->SetLineColor(kBlue-5);
00085    // fu->Draw("SAME");
00086 
00087 
00088    TLegend * legend = new TLegend(0.6,0.7,0.9,0.95);
00089    legend->AddEntry(f1,"True function");
00090    legend->AddEntry(kde->GetDrawnFunction(),"TKDE");
00091    legend->AddEntry(kde->GetDrawnLowerFunction(),"TKDE - #sigma");
00092    legend->AddEntry(kde->GetDrawnUpperFunction(),"TKDE + #sigma");
00093    legend->Draw();
00094    return;
00095 
00096    gPad->Update();
00097 
00098 
00099 }
00100 

Generated on Tue Jul 5 15:44:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1