Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
IntervalExamples.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roostats
3## \notebook
4## Example showing confidence intervals with four techniques.
5##
6## An example that shows confidence intervals with four techniques.
7## The model is a Normal Gaussian G(x|mu,sigma) with 100 samples of x.
8## The answer is known analytically, so this is a good example to validate
9## the RooStats tools.
10##
11## - expected interval is [-0.162917, 0.229075]
12## - plc interval is [-0.162917, 0.229075]
13## - fc interval is [-0.17 , 0.23] // stepsize is 0.01
14## - bc interval is [-0.162918, 0.229076]
15## - mcmc interval is [-0.166999, 0.230224]
16##
17## \macro_image
18## \macro_output
19## \macro_code
20##
21## \date July 2022
22## \authors Artem Busorgin, Kyle Cranmer (C++ version)
23
24import ROOT
25
26# Time this macro
27t = ROOT.TStopwatch()
28t.Start()
29
30# set RooFit random seed for reproducible results
31ROOT.RooRandom.randomGenerator().SetSeed(3001)
32
33# make a simple model via the workspace factory
34wspace = ROOT.RooWorkspace()
35wspace.factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])")
36wspace.defineSet("poi", "mu")
37wspace.defineSet("obs", "x")
38
39# specify components of model for statistical tools
40modelConfig = ROOT.RooStats.ModelConfig("Example G(x|mu,1)")
41modelConfig.SetWorkspace(wspace)
42modelConfig.SetPdf(wspace["normal"])
43modelConfig.SetParametersOfInterest(wspace.set("poi"))
44modelConfig.SetObservables(wspace.set("obs"))
45
46# create a toy dataset
47data = wspace["normal"].generate(wspace.set("obs"), 100)
48data.Print()
49
50# for convenience later on
51x = wspace["x"]
52mu = wspace["mu"]
53
54# set confidence level
55confidenceLevel = 0.95
56
57# example use profile likelihood calculator
58plc = ROOT.RooStats.ProfileLikelihoodCalculator(data, modelConfig)
59plc.SetConfidenceLevel(confidenceLevel)
60plInt = plc.GetInterval()
61
62# example use of Feldman-Cousins
63fc = ROOT.RooStats.FeldmanCousins(data, modelConfig)
64fc.SetConfidenceLevel(confidenceLevel)
65fc.SetNBins(100) # number of points to test per parameter
66fc.UseAdaptiveSampling(True) # make it go faster
67
68# Here, we consider only ensembles with 100 events
69# The PDF could be extended and this could be removed
70fc.FluctuateNumDataEntries(False)
71
72interval = fc.GetInterval()
73
74# example use of BayesianCalculator
75# now we also need to specify a prior in the ModelConfig
76wspace.factory("Uniform::prior(mu)")
77modelConfig.SetPriorPdf(wspace["prior"])
78
79# example usage of BayesianCalculator
80bc = ROOT.RooStats.BayesianCalculator(data, modelConfig)
81bc.SetConfidenceLevel(confidenceLevel)
82bcInt = bc.GetInterval()
83
84# example use of MCMCInterval
85mc = ROOT.RooStats.MCMCCalculator(data, modelConfig)
86mc.SetConfidenceLevel(confidenceLevel)
87# special options
88mc.SetNumBins(200) # bins used internally for representing posterior
89mc.SetNumBurnInSteps(500) # first N steps to be ignored as burn-in
90mc.SetNumIters(100000) # how long to run chain
91mc.SetLeftSideTailFraction(0.5) # for central interval
92mcInt = mc.GetInterval()
93
94# for this example we know the expected intervals
95expectedLL = data.mean(x) + ROOT.Math.normal_quantile((1 - confidenceLevel) / 2, 1) / ROOT.sqrt(data.numEntries())
96expectedUL = data.mean(x) + ROOT.Math.normal_quantile_c((1 - confidenceLevel) / 2, 1) / ROOT.sqrt(data.numEntries())
97
98# Use the intervals
99print("expected interval is [{}, {}]".format(expectedLL, expectedUL))
100print("plc interval is [{}, {}]".format(plInt.LowerLimit(mu), plInt.UpperLimit(mu)))
101print("fc interval is [{}, {}]".format(interval.LowerLimit(mu), interval.UpperLimit(mu)))
102print("bc interval is [{}, {}]".format(bcInt.LowerLimit(), bcInt.UpperLimit()))
103print("mc interval is [{}, {}]".format(mcInt.LowerLimit(mu), mcInt.UpperLimit(mu)))
104mu.setVal(0)
105print("is mu=0 in the interval? ", plInt.IsInInterval({mu}))
106
107# make a reasonable style
108ROOT.gStyle.SetCanvasColor(0)
109ROOT.gStyle.SetCanvasBorderMode(0)
110ROOT.gStyle.SetPadBorderMode(0)
111ROOT.gStyle.SetPadColor(0)
112ROOT.gStyle.SetCanvasColor(0)
113ROOT.gStyle.SetTitleFillColor(0)
114ROOT.gStyle.SetFillColor(0)
115ROOT.gStyle.SetFrameFillColor(0)
116ROOT.gStyle.SetStatColor(0)
117
118# some plots
119canvas = ROOT.TCanvas("canvas")
120canvas.Divide(2, 2)
121
122# plot the data
123canvas.cd(1)
124frame = x.frame()
125data.plotOn(frame)
126data.statOn(frame)
127frame.Draw()
128
129# plot the profile likelihood
130canvas.cd(2)
131plot = ROOT.RooStats.LikelihoodIntervalPlot(plInt)
132plot.Draw()
133
134# plot the MCMC interval
135canvas.cd(3)
136mcPlot = ROOT.RooStats.MCMCIntervalPlot(mcInt)
137mcPlot.SetLineColor(ROOT.kGreen)
138mcPlot.SetLineWidth(2)
139mcPlot.Draw()
140
141canvas.cd(4)
142bcPlot = bc.GetPosteriorPlot()
143bcPlot.Draw()
144
145canvas.Update()
146
147t.Stop()
148t.Print()
149
150canvas.SaveAs("IntervalExamples.png")
151
152# TODO: The BayesianCalculator and MCMCCalculator have to be destructed first.
153# Otherwise, we can get segmentation faults depending on the destruction order,
154# which is random in Python. Probably the issue is that some object has a
155# non-owning pointer to another object, which it uses in its destructor. This
156# should be fixed either in the design of RooStats in C++, or with
157# phythonizations.
158del bc
159del mc
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
double normal_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
double normal_quantile_c(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the upper tail of the normal (Gaussian) distri...