# Rf 6 0 4_Constraints¶

Likelihood and minimization: fitting with constraints

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:08 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooProdPdf.h"
#include "RooFitResult.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TH1.h"

In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;


## Create model and dataset¶

Construct a gaussian pdf

In [3]:
RooRealVar x("x", "x", -10, 10);

RooRealVar m("m", "m", 0, -10, 10);
RooRealVar s("s", "s", 2, 0.1, 10);
RooGaussian gauss("gauss", "gauss(x,m,s)", x, m, s);

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University



Construct a flat pdf (polynomial of 0th order)

In [4]:
RooPolynomial poly("poly", "poly(x)", x);


Construct model = fgauss + (1-f)poly

In [5]:
RooRealVar f("f", "f", 0.5, 0., 1.);
RooAddPdf model("model", "model", RooArgSet(gauss, poly), f);


Generate small dataset for use in fitting below

In [6]:
RooDataSet *d = model.generate(x, 50);


## Create constraint pdf¶

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

In [7]:
RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.8), RooConst(0.2));


## Method 1 - add internal constraint to model¶

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

Multiply constraint with pdf

In [8]:
RooProdPdf modelc("modelc", "model with constraint", RooArgSet(model, fconstraint));


Fit model (without use of constraint term)

In [9]:
RooFitResult *r1 = model.fitTo(*d, Save());

[#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 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 f            5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
2 m            0.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
3 s            2.00000e+00  9.50000e-01    1.00000e-01  1.00000e+01
**********
**    3 **SET ERR         0.5
**********
**********
**    4 **SET PRINT           1
**********
**********
**    5 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=136.523 FROM MIGRAD    STATUS=INITIATE       10 CALLS          11 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            5.00000e-01   1.00000e-01   2.01358e-01  -3.71768e+00
2  m            0.00000e+00   2.00000e+00   2.01358e-01  -2.53753e+01
3  s            2.00000e+00   9.50000e-01   2.51381e-01  -1.22085e+00
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=134.849 FROM MIGRAD    STATUS=CONVERGED      50 CALLS          51 TOTAL
EDM=4.23315e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            6.49867e-01   1.21447e-01   1.78337e-03  -1.77163e-02
2  m            7.18243e-01   4.96576e-01   3.89020e-04   8.83234e-02
3  s            2.18801e+00   4.61197e-01   7.70631e-04   3.46910e-03
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
1.508e-02 -6.689e-03  2.798e-02
-6.689e-03  2.468e-01 -3.900e-02
2.798e-02 -3.900e-02  2.136e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.49373   1.000 -0.110  0.493
2  0.17244  -0.110  1.000 -0.170
3  0.50660   0.493 -0.170  1.000
**********
**    7 **SET ERR         0.5
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        1500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=134.849 FROM HESSE     STATUS=OK             16 CALLS          67 TOTAL
EDM=4.23315e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  f            6.49867e-01   1.21452e-01   3.56674e-04   3.04414e-01
2  m            7.18243e-01   4.96642e-01   7.78041e-05   7.18862e-02
3  s            2.18801e+00   4.61289e-01   1.54126e-04  -6.16495e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
1.508e-02 -6.686e-03  2.800e-02
-6.686e-03  2.469e-01 -3.921e-02
2.800e-02 -3.921e-02  2.137e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.49379   1.000 -0.110  0.493
2  0.17319  -0.110  1.000 -0.171
3  0.50690   0.493 -0.171  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


Fit modelc with constraint term on parameter f

In [10]:
RooFitResult *r2 = modelc.fitTo(*d, Constrain(f), Save());

[#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)
**********
**   10 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 f            6.49867e-01  1.21452e-01    0.00000e+00  1.00000e+00
2 m            7.18243e-01  4.96642e-01   -1.00000e+01  1.00000e+01
3 s            2.18801e+00  4.61289e-01    1.00000e-01  1.00000e+01
**********
**   12 **SET ERR         0.5
**********
**********
**   13 **SET PRINT           1
**********
**********
**   14 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=134.268 FROM MIGRAD    STATUS=INITIATE       10 CALLS          11 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            6.49867e-01   1.21452e-01   2.58356e-01  -1.80809e+00
2  m            7.18243e-01   4.96642e-01   4.98137e-02   8.83296e-02
3  s            2.18801e+00   4.61289e-01   1.14852e-01   3.40505e-03
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=134.19 FROM MIGRAD    STATUS=CONVERGED      48 CALLS          49 TOTAL
EDM=7.15556e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            6.90394e-01   1.01488e-01   1.56024e-03  -2.66326e-03
2  m            6.95291e-01   4.98410e-01   3.92779e-04   4.60363e-03
3  s            2.26976e+00   4.75435e-01   8.11598e-04  -2.57087e-03
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
1.047e-02 -6.212e-03  2.307e-02
-6.212e-03  2.486e-01 -4.460e-02
2.307e-02 -4.460e-02  2.271e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.47437   1.000 -0.122  0.473
2  0.19139  -0.122  1.000 -0.188
3  0.49100   0.473 -0.188  1.000
**********
**   16 **SET ERR         0.5
**********
**********
**   17 **SET PRINT           1
**********
**********
**   18 **HESSE        1500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=134.19 FROM HESSE     STATUS=OK             16 CALLS          65 TOTAL
EDM=7.15613e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  f            6.90394e-01   1.01490e-01   3.12047e-04   3.90649e-01
2  m            6.95291e-01   4.98516e-01   7.85557e-05   6.95852e-02
3  s            2.26976e+00   4.75547e-01   1.62320e-04  -5.96397e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
1.047e-02 -6.221e-03  2.308e-02
-6.221e-03  2.487e-01 -4.490e-02
2.308e-02 -4.490e-02  2.272e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.47441   1.000 -0.122  0.473
2  0.19247  -0.122  1.000 -0.189
3  0.49137   0.473 -0.189  1.000
[#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.2 with resolution of 0.1

In [11]:
RooGaussian fconstext("fconstext", "fconstext", f, RooConst(0.2), RooConst(0.1));


Fit with external constraint

In [12]:
RooFitResult *r3 = model.fitTo(*d, ExternalConstraints(fconstext), Save());

[#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)
**********
**   19 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 f            6.90394e-01  1.01490e-01    0.00000e+00  1.00000e+00
2 m            6.95291e-01  4.98516e-01   -1.00000e+01  1.00000e+01
3 s            2.26976e+00  4.75547e-01    1.00000e-01  1.00000e+01
**********
**   21 **SET ERR         0.5
**********
**********
**   22 **SET PRINT           1
**********
**********
**   23 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=145.521 FROM MIGRAD    STATUS=INITIATE       10 CALLS          11 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            6.90394e-01   1.01490e-01   2.22303e-01   2.39366e+01
2  m            6.95291e-01   4.98516e-01   4.99937e-02   4.60680e-03
3  s            2.26976e+00   4.75547e-01   1.16751e-01  -2.63987e-03
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=137.195 FROM MIGRAD    STATUS=CONVERGED      52 CALLS          53 TOTAL
EDM=4.81135e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  f            3.61995e-01   8.14207e-02   1.26319e-03  -1.14476e-02
2  m            7.06198e-01   5.87858e-01   4.68561e-04   7.19385e-02
3  s            1.70830e+00   4.74684e-01   9.56739e-04   4.09583e-02
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
6.694e-03  5.868e-03  1.544e-02
5.868e-03  3.460e-01  4.994e-02
1.544e-02  4.994e-02  2.266e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.39975   1.000  0.122  0.396
2  0.18688   0.122  1.000  0.178
3  0.41743   0.396  0.178  1.000
**********
**   25 **SET ERR         0.5
**********
**********
**   26 **SET PRINT           1
**********
**********
**   27 **HESSE        1500
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=137.195 FROM HESSE     STATUS=OK             16 CALLS          69 TOTAL
EDM=4.81665e-05    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  f            3.61995e-01   8.14561e-02   2.52637e-04  -2.79640e-01
2  m            7.06198e-01   5.87996e-01   9.37122e-05   7.06786e-02
3  s            1.70830e+00   4.74927e-01   1.91348e-04  -7.41089e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  3    ERR DEF=0.5
6.700e-03  5.921e-03  1.549e-02
5.921e-03  3.461e-01  5.029e-02
1.549e-02  5.029e-02  2.268e-01
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2      3
1  0.40067   1.000  0.123  0.397
2  0.18809   0.123  1.000  0.179
3  0.41846   0.397  0.179  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization


Print the fit results

In [13]:
cout << "fit result without constraint (data generated at f=0.5)" << endl;
r1->Print("v");
cout << "fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)" << endl;
r2->Print("v");
cout << "fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)" << endl;
r3->Print("v");

fit result without constraint (data generated at f=0.5)

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>

fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)

RooFitResult: minimized FCN value: 134.19, estimated distance to minimum: 7.15613e-07
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0

Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
--------------------  ------------  --------------------------  --------
f    6.4987e-01    6.9039e-01 +/-  1.01e-01  <none>
m    7.1824e-01    6.9529e-01 +/-  4.99e-01  <none>
s    2.1880e+00    2.2698e+00 +/-  4.76e-01  <none>

fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)

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

Floating Parameter  InitialValue    FinalValue +/-  Error     GblCorr.
--------------------  ------------  --------------------------  --------
f    6.9039e-01    3.6200e-01 +/-  8.15e-02  <none>
m    6.9529e-01    7.0620e-01 +/-  5.88e-01  <none>
s    2.2698e+00    1.7083e+00 +/-  4.75e-01  <none>