Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf610_visualerror.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Likelihood and minimization: visualization of errors from a covariance matrix
5##
6## \macro_image
7## \macro_code
8## \macro_output
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C++ version)
12
13import ROOT
14
15
16# Setup example fit
17# ---------------------------------------
18
19# Create sum of two Gaussians pdf with factory
20x = ROOT.RooRealVar("x", "x", -10, 10)
21
22m = ROOT.RooRealVar("m", "m", 0, -10, 10)
23s = ROOT.RooRealVar("s", "s", 2, 1, 50)
24sig = ROOT.RooGaussian("sig", "sig", x, m, s)
25
26m2 = ROOT.RooRealVar("m2", "m2", -1, -10, 10)
27s2 = ROOT.RooRealVar("s2", "s2", 6, 1, 50)
28bkg = ROOT.RooGaussian("bkg", "bkg", x, m2, s2)
29
30fsig = ROOT.RooRealVar("fsig", "fsig", 0.33, 0, 1)
31model = ROOT.RooAddPdf("model", "model", [sig, bkg], [fsig])
32
33# Create binned dataset
34x.setBins(25)
35d = model.generateBinned({x}, 1000)
36
37# Perform fit and save fit result
38r = model.fitTo(d, Save=True, PrintLevel=-1)
39
40# Visualize fit error
41# -------------------------------------
42
43# Make plot frame
44frame = x.frame(Bins=40, Title="P.d.f with visualized 1-sigma error band")
45d.plotOn(frame)
46
47# Visualize 1-sigma error encoded in fit result 'r' as orange band using linear error propagation
48# ROOT.This results in an error band that is by construction symmetric
49#
50# The linear error is calculated as
51# error(x) = Z* F_a(x) * Corr(a,a') F_a'(x)
52#
53# where F_a(x) = [ f(x,a+da) - f(x,a-da) ] / 2,
54#
55# with f(x) = the plotted curve
56# 'da' = error taken from the fit result
57# Corr(a,a') = the correlation matrix from the fit result
58# Z = requested significance 'Z sigma band'
59#
60# The linear method is fast (required 2*N evaluations of the curve, N is the number of parameters),
61# but may not be accurate in the presence of strong correlations (~>0.9) and at Z>2 due to linear and
62# Gaussian approximations made
63#
64model.plotOn(frame, VisualizeError=(r, 1), FillColor="kOrange")
65
66# Calculate error using sampling method and visualize as dashed red line.
67#
68# In self method a number of curves is calculated with variations of the parameter values, sampled
69# from a multi-variate Gaussian pdf that is constructed from the fit results covariance matrix.
70# The error(x) is determined by calculating a central interval that capture N% of the variations
71# for each value of x, N% is controlled by Z (i.e. Z=1 gives N=68%). The number of sampling curves
72# is chosen to be such that at least 100 curves are expected to be outside the N% interval, is minimally
73# 100 (e.g. Z=1.Ncurve=356, Z=2.Ncurve=2156)) Intervals from the sampling method can be asymmetric,
74# and may perform better in the presence of strong correlations, may take
75# (much) longer to calculate
76model.plotOn(frame, VisualizeError=(r, 1, False), DrawOption="L", LineWidth=2, LineColor="r")
77
78# Perform the same type of error visualization on the background component only.
79# The VisualizeError() option can generally applied to _any_ kind of
80# plot (components, asymmetries, etc..)
81model.plotOn(frame, VisualizeError=(r, 1), FillColor="kOrange", Components="bkg")
82model.plotOn(
83 frame,
84 VisualizeError=(r, 1, False),
85 DrawOption="L",
86 LineWidth=2,
87 LineColor="r",
88 Components="bkg",
89 LineStyle="--",
90)
91
92# Overlay central value
93model.plotOn(frame)
94model.plotOn(frame, Components="bkg", LineStyle="--")
95d.plotOn(frame)
96frame.SetMinimum(0)
97
98# Visualize partial fit error
99# ------------------------------------------------------
100
101# Make plot frame
102frame2 = x.frame(Bins=40, Title="Visualization of 2-sigma partial error from (m,m2)")
103
104# Visualize partial error. For partial error visualization the covariance matrix is first reduced as follows
105# ___ -1
106# Vred = V22 = V11 - V12 * V22 * V21
107#
108# Where V11,V12,V21, represent a block decomposition of the covariance matrix into observables that
109# are propagated (labeled by index '1') and that are not propagated (labeled by index '2'), V22bar
110# is the Shur complement of V22, as shown above
111#
112# (Note that Vred is _not_ a simple sub-matrix of V)
113
114# Propagate partial error due to shape parameters (m,m2) using linear and
115# sampling method
116model.plotOn(frame2, VisualizeError=(r, {m, m2}, 2), FillColor="c")
117model.plotOn(frame2, Components="bkg", VisualizeError=(r, {m, m2}, 2), FillColor="c")
118
119model.plotOn(frame2)
120model.plotOn(frame2, Components="bkg", LineStyle="--")
121frame2.SetMinimum(0)
122
123# Make plot frame
124frame3 = x.frame(Bins=40, Title="Visualization of 2-sigma partial error from (s,s2)")
125
126# Propagate partial error due to yield parameter using linear and sampling
127# method
128model.plotOn(frame3, VisualizeError=(r, {s, s2}, 2), FillColor="g")
129model.plotOn(frame3, Components="bkg", VisualizeError=(r, {fsig}, 2), FillColor="g")
130
131model.plotOn(frame3)
132model.plotOn(frame3, Components="bkg", LineStyle="--")
133frame3.SetMinimum(0)
134
135# Make plot frame
136frame4 = x.frame(Bins=40, Title="Visualization of 2-sigma partial error from fsig")
137
138# Propagate partial error due to yield parameter using linear and sampling
139# method
140model.plotOn(frame4, VisualizeError=(r, {fsig}, 2), FillColor="m")
141model.plotOn(frame4, Components="bkg", VisualizeError=(r, {fsig}, 2), FillColor="m")
142
143model.plotOn(frame4)
144model.plotOn(frame4, Components="bkg", LineStyle="--")
145frame4.SetMinimum(0)
146
147c = ROOT.TCanvas("rf610_visualerror", "rf610_visualerror", 800, 800)
148c.Divide(2, 2)
149c.cd(1)
150ROOT.gPad.SetLeftMargin(0.15)
151frame.GetYaxis().SetTitleOffset(1.4)
152frame.Draw()
153c.cd(2)
154ROOT.gPad.SetLeftMargin(0.15)
155frame2.GetYaxis().SetTitleOffset(1.6)
156frame2.Draw()
157c.cd(3)
158ROOT.gPad.SetLeftMargin(0.15)
159frame3.GetYaxis().SetTitleOffset(1.6)
160frame3.Draw()
161c.cd(4)
162ROOT.gPad.SetLeftMargin(0.15)
163frame4.GetYaxis().SetTitleOffset(1.6)
164frame4.Draw()
165
166c.SaveAs("rf610_visualerror.png")