Multidimensional models: marginizalization of multi-dimensional pdfs through integration
Author: Clemens Lange, Wouter Verkerke (C++ version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:15 PM.
import ROOT
Increase default precision of numeric integration as self exercise has high sensitivity to numeric integration precision
ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsRel(1e-8)
ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsAbs(1e-8)
Create observables
x = ROOT.RooRealVar("x", "x", -5, 5)
y = ROOT.RooRealVar("y", "y", -2, 2)
Create function f(y) = a0 + a1*y
a0 = ROOT.RooRealVar("a0", "a0", 0)
a1 = ROOT.RooRealVar("a1", "a1", -1.5, -3, 1)
fy = ROOT.RooPolyVar("fy", "fy", y, [a0, a1])
Create gaussx(x,f(y),sx)
sigmax = ROOT.RooRealVar("sigmax", "width of gaussian", 0.5)
gaussx = ROOT.RooGaussian("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)
[#0] WARNING:InputArguments -- The parameter 'sigmax' with range [-inf, inf] of the RooGaussian 'gaussx' exceeds the safe range of (0, inf). Advise to limit its range.
Create gaussy(y,0,2)
gaussy = ROOT.RooGaussian("gaussy", "Gaussian in y", y, 0.0, 2.0)
Create gaussx(x,sx|y) * gaussy(y)
model = ROOT.RooProdPdf(
"model",
"gaussx(x|y)*gaussy(y)",
{gaussy},
Conditional=({gaussx}, {x}),
)
modelx(x) = Int model(x,y) dy
modelx = model.createProjection({y})
Sample 1000 events from modelx
data = modelx.generateBinned({x}, 1000)
[#1] INFO:NumericIntegration -- RooRealIntegral::init([gaussy_NORM[y]_X_gaussx_NORM[x]]_Int[y]) using numeric integrator RooIntegrator1D to calculate Int(y)
Fit modelx to toy data
modelx.fitTo(data, Verbose=True, PrintLevel=-1)
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Fitting -- RooAbsPdf::fitTo(model_Int[y]_Norm[x,y]) fixing normalization set for coefficient determination to observables in data [#1] INFO:Fitting -- using CPU computation library compiled with -mavx2 [#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_Int[y]_Norm[x,y]_genData) Summation contains a RooNLLVar, using its error level [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#0] WARNING:Minimization -- RooAbsMinimizerFcn::synchronize: WARNING: no initial error estimate available for a1: using 0.4 [#0] WARNING:Minimization -- RooAbsMinimizerFcn::synchronize: WARNING: no initial error estimate available for y: using 0.4 [#1] INFO:NumericIntegration -- RooRealIntegral::init([gaussy_NORM[y]_X_gaussx_NORM[x]]_Int[y]) using numeric integrator RooIntegrator1D to calculate Int(y) prevFCN = 12037.78496 a1=-1.469, prevFCN = 1900.132597 a1=-1.531, prevFCN = 1901.591671 a1=-1.497, prevFCN = 1900.088181 a1=-1.503, prevFCN = 1900.238998 a1=-1.5, y=0.03051, prevFCN = 1900.156536 y=-0.03051, prevFCN = 1900.156536 y=0.003051, prevFCN = 1900.156536 y=-0.003051, prevFCN = 1900.156536 a1=-1.497, y=0, prevFCN = 1900.088181 a1=-1.485, prevFCN = 1899.958806 a1=-1.491, prevFCN = 1899.994382 a1=-1.484, prevFCN = 1899.958577 a1=-1.485, prevFCN = 1899.959183 a1=-1.484, prevFCN = 1899.958511 a1=-1.485, prevFCN = 1899.960007 a1=-1.485, y=0.0003051, prevFCN = 1899.958806 y=-0.0003051, prevFCN = 1899.958806 a1=-1.484, y=0, prevFCN = 1899.958497 a1=-1.483, prevFCN = 1899.958952 a1=-1.485, prevFCN = 1899.95895 a1=-1.484, y=0.003051, prevFCN = 1899.958497 y=-0.003051, prevFCN = 1899.958497 y=0, prevFCN = 1899.958497 a1=-1.483, prevFCN = 1899.958952 a1=-1.485, prevFCN = 1899.95895 a1=-1.484, y=0.003051, prevFCN = 1899.958497 y=-0.003051, prevFCN = 1899.958497 y=0.03051, prevFCN = 1899.958497 y=-0.03051, prevFCN = 1899.958497 y=0.3039, prevFCN = 1899.958497 y=-0.3039, prevFCN = 1899.958497 y=0.9764, prevFCN = 1899.958497 y=-0.9764, prevFCN = 1899.958497 y=0, [#0] WARNING:Minimization -- RooAbsMinimizerFcn::synchronize: WARNING: no initial error estimate available for a1: using 0.4 [#1] INFO:Minimization -- RooAbsMinimizerFcn::synchronize: value of parameter a1 changed from -1.5 to -1.484 [#0] WARNING:Minimization -- RooAbsMinimizerFcn::synchronize: WARNING: no initial error estimate available for y: using 0.4 prevFCN = 1899.958497 a1=-1.483, prevFCN = 1899.958952 a1=-1.485, prevFCN = 1899.95895 a1=-1.484, y=0.003051, prevFCN = 1899.958497 y=-0.003051, prevFCN = 1899.958497 y=0.03051, prevFCN = 1899.958497 y=-0.03051, prevFCN = 1899.958497 y=0.3039, prevFCN = 1899.958497 y=-0.3039, prevFCN = 1899.958497 y=0.9764, prevFCN = 1899.958497 y=-0.9764, prevFCN = 1899.958497 y=0, [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
Warning in <ROOT::Math::Fitter::CalculateHessErrors>: Error when calculating Hessian
Plot modelx over data
frame = x.frame(40)
data.plotOn(frame)
modelx.plotOn(frame)
<cppyy.gbl.RooPlot object at 0xb0490d0>
[#1] INFO:NumericIntegration -- RooRealIntegral::init([gaussy_NORM[y]_X_gaussx_NORM[x]]_Int[y]) using numeric integrator RooIntegrator1D to calculate Int(y)
Make 2D histogram of model(x,y)
hh = model.createHistogram("x,y")
hh.SetLineColor(ROOT.kBlue)
c = ROOT.TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400)
c.Divide(2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.20)
hh.GetZaxis().SetTitleOffset(2.5)
hh.Draw("surf")
c.SaveAs("rf315_projectpdf.png")
Info in <TCanvas::Print>: png file rf315_projectpdf.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()