rf604_constraints

Likelihood and minimization: fitting with constraints

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 Sunday, November 27, 2022 at 11:07 AM.

In [1]:
from __future__ import print_function
import ROOT
Welcome to JupyROOT 6.27/01

Create model and dataset

Construct a Gaussian pdf

In [2]:
x = ROOT.RooRealVar("x", "x", -10, 10)

m = ROOT.RooRealVar("m", "m", 0, -10, 10)
s = ROOT.RooRealVar("s", "s", 2, 0.1, 10)
gauss = ROOT.RooGaussian("gauss", "gauss(x,m,s)", x, m, s)

Construct a flat pdf (polynomial of 0th order)

In [3]:
poly = ROOT.RooPolynomial("poly", "poly(x)", x)

model = fgauss + (1-f)poly

In [4]:
f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0)
model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f])

Generate small dataset for use in fitting below

In [5]:
d = model.generate({x}, 50)

Create constraint pdf

Construct Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1

In [6]:
fconstraint = ROOT.RooGaussian("fconstraint", "fconstraint", f, ROOT.RooFit.RooConst(0.8), ROOT.RooFit.RooConst(0.1))

Method 1 - add internal constraint to model

Multiply constraint term with regular pdf using ROOT.RooProdPdf Specify in fitTo() that internal constraints on parameter f should be used

Multiply constraint with pdf

In [7]:
modelc = ROOT.RooProdPdf("modelc", "model with constraint", [model, fconstraint])

Fit model (without use of constraint term)

In [8]:
r1 = model.fitTo(d, Save=True, PrintLevel=-1)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Fit modelc with constraint term on parameter f

In [9]:
r2 = modelc.fitTo(d, Constrain={f}, Save=True, PrintLevel=-1)
[#1] INFO:Minimization --  Including the following constraint terms in minimization: (fconstraint)
[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f)
[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_modelc_modelData_with_constr) Summation contains a RooNLLVar, using its error level
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Method 2 - specify external constraint when fitting

Construct another Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1

In [10]:
fconstext = ROOT.RooGaussian("fconstext", "fconstext", f, ROOT.RooFit.RooConst(0.2), ROOT.RooFit.RooConst(0.1))

Fit with external constraint

In [11]:
r3 = model.fitTo(d, ExternalConstraints={fconstext}, Save=True, PrintLevel=-1)
[#1] INFO:Minimization --  Including the following constraint terms in minimization: (fconstext)
[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f,m,s)
[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData_with_constr) Summation contains a RooNLLVar, using its error level
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions have been identified as constant and will be precalculated and cached: (poly)
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (gauss)
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Print the fit results

In [12]:
print("fit result without constraint (data generated at f=0.5)")
r1.Print("v")
print("fit result with internal constraint (data generated at f=0.5, is f=0.8+/-0.2)")
r2.Print("v")
print("fit result with (another) external constraint (data generated at f=0.5, is f=0.2+/-0.1)")
r3.Print("v")
fit result without constraint (data generated at f=0.5)
fit result with internal constraint (data generated at f=0.5, is f=0.8+/-0.2)
fit result with (another) external constraint (data generated at f=0.5, is f=0.2+/-0.1)

  RooFitResult: minimized FCN value: 134.849, estimated distance to minimum: 4.23315e-05
                covariance matrix quality: Full, accurate covariance matrix
                Status : MINIMIZE=0 HESSE=0 

    Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
  --------------------  ------------  --------------------------  --------
                     f    5.0000e-01    6.4987e-01 +/-  1.21e-01  <none>
                     m    0.0000e+00    7.1824e-01 +/-  4.97e-01  <none>
                     s    2.0000e+00    2.1880e+00 +/-  4.61e-01  <none>


  RooFitResult: minimized FCN value: 133.901, estimated distance to minimum: 1.19334e-05
                covariance matrix quality: Full, accurate covariance matrix
                Status : MINIMIZE=0 HESSE=0 

    Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
  --------------------  ------------  --------------------------  --------
                     f    6.4987e-01    7.3784e-01 +/-  7.53e-02  <none>
                     m    7.1824e-01    6.6350e-01 +/-  5.04e-01  <none>
                     s    2.1880e+00    2.3885e+00 +/-  4.95e-01  <none>


  RooFitResult: minimized FCN value: 137.195, estimated distance to minimum: 1.4321e-05
                covariance matrix quality: Full, accurate covariance matrix
                Status : MINIMIZE=0 HESSE=0 

    Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
  --------------------  ------------  --------------------------  --------
                     f    7.3784e-01    3.6176e-01 +/-  8.16e-02  <none>
                     m    6.6350e-01    7.0131e-01 +/-  5.88e-01  <none>
                     s    2.3885e+00    1.7050e+00 +/-  4.77e-01  <none>