Likelihood and minimization: working with the profile likelihood estimator
Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:16 PM.
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooAddPdf.h"
#include "RooMinimizer.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
using namespace RooFit;
Observable
RooRealVar x("x", "x", -20, 20);
Model (intentional strong correlations)
RooRealVar mean("mean", "mean of g1 and g2", 0, -10, 10);
RooRealVar sigma_g1("sigma_g1", "width of g1", 3);
RooGaussian g1("g1", "g1", x, mean, sigma_g1);
RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 6.0);
RooGaussian g2("g2", "g2", x, mean, sigma_g2);
RooRealVar frac("frac", "frac", 0.5, 0.0, 1.0);
RooAddPdf model("model", "model", RooArgList(g1, g2), frac);
[#0] WARNING:InputArguments -- The parameter 'sigma_g1' with range [-inf, inf] of the RooGaussian 'g1' exceeds the safe range of (0, inf). Advise to limit its range.
Generate 1000 events
std::unique_ptr<RooDataSet> data{model.generate(x, 1000)};
input_line_50:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration std::unique_ptr<RooDataSet> data{model.generate(x, 1000)}; ^
Construct unbinned likelihood
std::unique_ptr<RooAbsReal> nll{model.createNLL(*data, NumCPU(2))};
input_line_51:2:51: error: reference to 'data' is ambiguous std::unique_ptr<RooAbsReal> nll{model.createNLL(*data, NumCPU(2))}; ^ input_line_50:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<RooDataSet> data{model.generate(x, 1000)}; ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
Minimize likelihood w.r.t all parameters before making plots
RooMinimizer(*nll).migrad();
input_line_53:2:18: error: use of undeclared identifier 'nll' (RooMinimizer(* nll).migrad()) ^ Error in <HandleInterpreterException>: Error evaluating expression (RooMinimizer(* nll).migrad()) Execution of your code was aborted.
Plot likelihood scan frac
RooPlot *frame1 = frac.frame(Bins(10), Range(0.01, 0.95), Title("LL and profileLL in frac"));
nll->plotOn(frame1, ShiftToZero());
input_line_54:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()' nll->plotOn(frame1, ShiftToZero()); ^~~~~~~~~~~
Plot likelihood scan in sigma_g2
RooPlot *frame2 = sigma_g2.frame(Bins(10), Range(3.3, 5.0), Title("LL and profileLL in sigma_g2"));
nll->plotOn(frame2, ShiftToZero());
input_line_55:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()' nll->plotOn(frame2, ShiftToZero()); ^~~~~~~~~~~
The profile likelihood estimator on nll for frac will minimize nll w.r.t all floating parameters except frac for each evaluation
std::unique_ptr<RooAbsReal> pll_frac{nll->createProfile(frac)};
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N52724__dynamic__cling_Un1Qu30E, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, __cxx_global_var_initcling_module_312_.2, __orc_init_func.cling-module-312, _ZN12__cling_N5278pll_fracE, _GLOBAL__sub_I_cling_module_312, __cxx_global_var_initcling_module_312_, _ZN12__cling_N52716__cling_Un1Qu327EPv, $.cling-module-312.__inits.0 }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*) Maybe you need to load the corresponding shared library? IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler() Maybe you need to load the corresponding shared library?
Plot the profile likelihood in frac
pll_frac->plotOn(frame1, LineColor(kRed));
input_line_57:2:37: error: cannot take the address of an rvalue of type 'EColor' pll_frac->plotOn(frame1, LineColor(kRed)); ^~~~ Error while creating dynamic expression for: pll_frac->plotOn(frame1, LineColor(kRed))
Adjust frame maximum for visual clarity
frame1->SetMinimum(0);
frame1->SetMaximum(3);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-312 }) } input_line_59:2:3: error: use of undeclared identifier 'frame1' (frame1->SetMinimum(0)) ^ Error in <HandleInterpreterException>: Error evaluating expression (frame1->SetMinimum(0)) Execution of your code was aborted.
The profile likelihood estimator on nll for sigma_g2 will minimize nll w.r.t all floating parameters except sigma_g2 for each evaluation
std::unique_ptr<RooAbsReal> pll_sigmag2{nll->createProfile(sigma_g2)};
cling JIT session error: Failed to materialize symbols: { (main, { _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __cxx_global_var_initcling_module_315_.2, _ZN12__cling_N53024__dynamic__cling_Un1Qu31E, _GLOBAL__sub_I_cling_module_315, _ZN12__cling_N53011pll_sigmag2E, __orc_init_func.cling-module-315, __cxx_global_var_initcling_module_315_, $.cling-module-315.__inits.0, _ZN12__cling_N53016__cling_Un1Qu331EPv }) }
Plot the profile likelihood in sigma_g2
pll_sigmag2->plotOn(frame2, LineColor(kRed));
input_line_61:2:40: error: cannot take the address of an rvalue of type 'EColor' pll_sigmag2->plotOn(frame2, LineColor(kRed)); ^~~~ Error while creating dynamic expression for: pll_sigmag2->plotOn(frame2, LineColor(kRed))
Adjust frame maximum for visual clarity
frame2->SetMinimum(0);
frame2->SetMaximum(3);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-315 }) } input_line_63:2:3: error: use of undeclared identifier 'frame2' (frame2->SetMinimum(0)) ^ Error in <HandleInterpreterException>: Error evaluating expression (frame2->SetMinimum(0)) Execution of your code was aborted.
Make canvas and draw RooPlots
TCanvas *c = new TCanvas("rf605_profilell", "rf605_profilell", 800, 400);
c->Divide(2);
c->cd(1);
gPad->SetLeftMargin(0.15);
frame1->GetYaxis()->SetTitleOffset(1.4);
frame1->Draw();
c->cd(2);
gPad->SetLeftMargin(0.15);
frame2->GetYaxis()->SetTitleOffset(1.4);
frame2->Draw();
input_line_65:2:3: error: use of undeclared identifier 'frame1' (frame1->GetYaxis()->SetTitleOffset(1.3999999999999999)) ^ Error in <HandleInterpreterException>: Error evaluating expression (frame1->GetYaxis()->SetTitleOffset(1.3999999999999999)) Execution of your code was aborted.
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()