Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf707_kernelestimation.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Special pdf's: using non-parametric (multi-dimensional) kernel estimation pdfs
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13
14# Create low stats 1D dataset
15# -------------------------------------------------------
16
17# Create a toy pdf for sampling
18x = ROOT.RooRealVar("x", "x", 0, 20)
19p = ROOT.RooPolynomial("p", "p", x, [0.01, -0.01, 0.0004])
20
21# Sample 500 events from p
22data1 = p.generate({x}, 200)
23
24# Create 1D kernel estimation pdf
25# ---------------------------------------------------------------
26
27# Create adaptive kernel estimation pdf. In self configuration the input data
28# is mirrored over the boundaries to minimize edge effects in distribution
29# that do not fall to zero towards the edges
30kest1 = ROOT.RooKeysPdf("kest1", "kest1", x, data1, ROOT.RooKeysPdf.MirrorBoth)
31
32# An adaptive kernel estimation pdf on the same data without mirroring option
33# for comparison
34kest2 = ROOT.RooKeysPdf("kest2", "kest2", x, data1, ROOT.RooKeysPdf.NoMirror)
35
36# Adaptive kernel estimation pdf with increased bandwidth scale factor
37# (promotes smoothness over detail preservation)
38kest3 = ROOT.RooKeysPdf("kest1", "kest1", x, data1, ROOT.RooKeysPdf.MirrorBoth, 2)
39
40# Plot kernel estimation pdfs with and without mirroring over data
41frame = x.frame(Title="Adaptive kernel estimation pdf with and w/o mirroring", Bins=20)
42data1.plotOn(frame)
43kest1.plotOn(frame)
44kest2.plotOn(frame, LineStyle="--", LineColor="r")
45
46# Plot kernel estimation pdfs with regular and increased bandwidth
47frame2 = x.frame(Title="Adaptive kernel estimation pdf with regular, bandwidth")
48kest1.plotOn(frame2)
49kest3.plotOn(frame2, LineColor="m")
50
51# Create low status 2D dataset
52# -------------------------------------------------------
53
54# Construct a 2D toy pdf for sampleing
55y = ROOT.RooRealVar("y", "y", 0, 20)
56py = ROOT.RooPolynomial(
57 "py",
58 "py",
59 y,
60 [0.01, 0.01, -0.0004],
61)
62pxy = ROOT.RooProdPdf("pxy", "pxy", [p, py])
63data2 = pxy.generate({x, y}, 1000)
64
65# Create 2D kernel estimation pdf
66# ---------------------------------------------------------------
67
68# Create 2D adaptive kernel estimation pdf with mirroring
69kest4 = ROOT.RooNDKeysPdf("kest4", "kest4", [x, y], data2, "am")
70
71# Create 2D adaptive kernel estimation pdf with mirroring and double
72# bandwidth
73kest5 = ROOT.RooNDKeysPdf("kest5", "kest5", [x, y], data2, "am", 2)
74
75# Create a histogram of the data
76hh_data = ROOT.RooAbsData.createHistogram(data2, "hh_data", x, Binning=10, YVar=dict(var=y, Binning=10))
77
78# Create histogram of the 2d kernel estimation pdfs
79hh_pdf = kest4.createHistogram("hh_pdf", x, Binning=25, YVar=dict(var=y, Binning=25))
80hh_pdf2 = kest5.createHistogram("hh_pdf2", x, Binning=25, YVar=dict(var=y, Binning=25))
81hh_pdf.SetLineColor(ROOT.kBlue)
82hh_pdf2.SetLineColor(ROOT.kMagenta)
83
84c = ROOT.TCanvas("rf707_kernelestimation", "rf707_kernelestimation", 800, 800)
85c.Divide(2, 2)
86c.cd(1)
87ROOT.gPad.SetLeftMargin(0.15)
88frame.GetYaxis().SetTitleOffset(1.4)
89frame.Draw()
90c.cd(2)
91ROOT.gPad.SetLeftMargin(0.15)
92frame2.GetYaxis().SetTitleOffset(1.8)
93frame2.Draw()
94c.cd(3)
95ROOT.gPad.SetLeftMargin(0.15)
96hh_data.GetZaxis().SetTitleOffset(1.4)
97hh_data.Draw("lego")
98c.cd(4)
99ROOT.gPad.SetLeftMargin(0.20)
100hh_pdf.GetZaxis().SetTitleOffset(2.4)
101hh_pdf.Draw("surf")
102hh_pdf2.Draw("surfsame")
103
104c.SaveAs("rf707_kernelestimation.png")