Logo ROOT  
Reference Guide
No Matches
exampleTKDE.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Example of using the TKDE class (kernel density estimator)

#include "TH1.h"
#include "TF1.h"
#include "TKDE.h"
#include "TCanvas.h"
/*#include "TStopwatch.h"*/
#include "TRandom.h"
#include "Math/DistFunc.h"
#include "TLegend.h"
// test TKDE
void exampleTKDE(int n = 1000) {
// generate some gaussian points
int nbin = 100;
double xmin = 0;
double xmax = 10;
TH1D * h1 = new TH1D("h1","h1",nbin,xmin,xmax);
// generate some points with bi- gaussian distribution
std::vector<double> data(n);
for (int i = 0; i < n; ++i) {
if (i < 0.4*n) {
data[i] = gRandom->Gaus(2,1);
else {
data[i] = gRandom->Gaus(7,1.5);
// scale histogram
h1->Scale(1./h1->Integral(),"width" );
// drawn true normalized density
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);
// create TKDE class
double rho = 1.0; //default value
TKDE * kde = new TKDE(n, &data[0], xmin,xmax, "", rho);
//kde->Draw("ConfidenceInterval@0.95 Same");
TLegend * legend = new TLegend(0.6,0.7,0.9,0.95);
legend->AddEntry(f1,"True function");
legend->AddEntry(kde->GetDrawnLowerFunction(),"TKDE - #sigma");
legend->AddEntry(kde->GetDrawnUpperFunction(),"TKDE + #sigma");
@ kGreen
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
float xmin
float xmax
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
1-Dim function class
Definition TF1.h:233
void Draw(Option_t *option="") override
Draw this function with its current attributes.
Definition TF1.cxx:1335
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:670
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6709
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3344
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition TH1.cxx:7932
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition TH1.cxx:6595
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8981
Kernel Density Estimation class.
Definition TKDE.h:37
TF1 * GetDrawnUpperFunction()
Definition TKDE.h:153
TF1 * GetDrawnLowerFunction()
Definition TKDE.h:154
TF1 * GetDrawnFunction()
Definition TKDE.h:152
void Draw(const Option_t *option="") override
Draws either the KDE functions or its errors.
Definition TKDE.cxx:895
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:320
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:425
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11
Lorenzo Moneta, Bartolomeu Rabacal (Dec 2010)

Definition in file exampleTKDE.C.