Logo ROOT  
Reference Guide
RooProfileLL.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2 * Project: RooFit *
3 * *
4 * Copyright (c) 2000-2005, Regents of the University of California *
5 * and Stanford University. All rights reserved. *
6 * *
7 * Redistribution and use in source and binary forms, *
8 * with or without modification, are permitted according to the terms *
9 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
10 *****************************************************************************/
11
12/**
13\file RooProfileLL.cxx
14\class RooProfileLL
15\ingroup Roofitcore
16
17Class RooProfileLL implements the profile likelihood estimator for
18a given likelihood and set of parameters of interest. The value return by
19RooProfileLL is the input likelihood nll minimized w.r.t all nuisance parameters
20(which are all parameters except for those listed in the constructor) minus
21the -log(L) of the best fit. Note that this function is slow to evaluate
22as a MIGRAD minimization step is executed for each function evaluation
23**/
24
25#include "Riostream.h"
26
27#include "RooProfileLL.h"
28#include "RooAbsReal.h"
29#include "RooMinimizer.h"
30#include "RooMsgService.h"
31#include "RooRealVar.h"
32
33using namespace std ;
34
36
37
38////////////////////////////////////////////////////////////////////////////////
39/// Default constructor
40/// Should only be used by proof.
41
43 RooAbsReal("RooProfileLL","RooProfileLL"),
44 _nll(),
45 _obs("paramOfInterest","Parameters of interest",this),
46 _par("nuisanceParam","Nuisance parameters",this,false,false),
47 _startFromMin(true),
48 _absMinValid(false),
49 _absMin(0),
50 _neval(0)
51{
52}
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Constructor of profile likelihood given input likelihood nll w.r.t
57/// the given set of variables. The input log likelihood is minimized w.r.t
58/// to all other variables of the likelihood at each evaluation and the
59/// value of the global log likelihood minimum is always subtracted.
60
61RooProfileLL::RooProfileLL(const char *name, const char *title,
62 RooAbsReal& nllIn, const RooArgSet& observables) :
63 RooAbsReal(name,title),
64 _nll("input","-log(L) function",this,nllIn),
65 _obs("paramOfInterest","Parameters of interest",this),
66 _par("nuisanceParam","Nuisance parameters",this,false,false),
67 _startFromMin(true),
68 _absMinValid(false),
69 _absMin(0),
70 _neval(0)
71{
72 // Determine actual parameters and observables
73 nllIn.getObservables(&observables, _obs) ;
74 nllIn.getParameters(&observables, _par) ;
75}
76
77
78
79////////////////////////////////////////////////////////////////////////////////
80/// Copy constructor
81
82RooProfileLL::RooProfileLL(const RooProfileLL& other, const char* name) :
83 RooAbsReal(other,name),
84 _nll("nll",this,other._nll),
85 _obs("obs",this,other._obs),
86 _par("par",this,other._par),
87 _startFromMin(other._startFromMin),
88 _absMinValid(false),
89 _absMin(0),
90 _paramFixed(other._paramFixed),
91 _neval(0)
92{
95
96}
97
98
99////////////////////////////////////////////////////////////////////////////////
100
102{
104 return _paramAbsMin ;
105}
106
107
108////////////////////////////////////////////////////////////////////////////////
109
111{
113 return _obsAbsMin ;
114}
115
116
117
118
119////////////////////////////////////////////////////////////////////////////////
120/// Optimized implementation of createProfile for profile likelihoods.
121/// Return profile of original function in terms of stated parameters
122/// of interest rather than profiling recursively.
123
125{
126 return nll().createProfile(paramsOfInterest) ;
127}
128
129
130
131
132////////////////////////////////////////////////////////////////////////////////
133
135{
136 coutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") Creating instance of MINUIT" << endl ;
137
138 bool smode = RooMsgService::instance().silentMode() ;
140 _minimizer = std::make_unique<RooMinimizer>(const_cast<RooAbsReal&>(_nll.arg())) ;
141 if (!smode) RooMsgService::instance().setSilentMode(false) ;
142
143}
144
145
146
147////////////////////////////////////////////////////////////////////////////////
148/// Evaluate profile likelihood by minimizing likelihood w.r.t. all
149/// parameters that are not considered observables of this profile
150/// likelihood object.
151
153{
154 // Instantiate minimizer if we haven't done that already
155 if (!_minimizer) {
157 }
158
159 // Save current value of observables
160 RooArgSet obsSetOrig;
161 _obs.snapshot(obsSetOrig) ;
162
164
165
166 // Set all observables constant in the minimization
167 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",true) ;
168 ccoutP(Eval) << "." ; ccoutP(Eval).flush() ;
169
170 // If requested set initial parameters to those corresponding to absolute minimum
171 if (_startFromMin) {
173 }
174
175 _minimizer->zeroEvalCount() ;
176
177 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
178 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
179 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
180 //_minimizer->minimize(minim.Data(),algorithm.Data());
181
182 _minimizer->migrad() ;
183 _neval = _minimizer->evalCounter() ;
184
185 // Restore original values and constant status of observables
186 for(auto const& arg : obsSetOrig) {
187 assert(dynamic_cast<RooRealVar*>(arg));
188 auto var = static_cast<RooRealVar*>(arg);
189 auto target = static_cast<RooRealVar*>(_obs.find(var->GetName())) ;
190 target->setVal(var->getVal()) ;
191 target->setConstant(var->isConstant()) ;
192 }
193
194 return _nll - _absMin ;
195}
196
197
198
199////////////////////////////////////////////////////////////////////////////////
200/// Check that parameters and likelihood value for 'best fit' are still valid. If not,
201/// because the best fit has never been calculated, or because constant parameters have
202/// changed value or parameters have changed const/float status, the minimum is recalculated
203
205{
206 // Check if constant status of any of the parameters have changed
207 if (_absMinValid) {
208 for(auto const& par : _par) {
209 if (_paramFixed[par->GetName()] != par->isConstant()) {
210 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") constant status of parameter " << par->GetName() << " has changed from "
211 << (_paramFixed[par->GetName()]?"fixed":"floating") << " to " << (par->isConstant()?"fixed":"floating")
212 << ", recalculating absolute minimum" << endl ;
213 _absMinValid = false ;
214 break ;
215 }
216 }
217 }
218
219
220 // If we don't have the absolute minimum w.r.t all observables, calculate that first
221 if (!_absMinValid) {
222
223 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") determining minimum likelihood for current configurations w.r.t all observable" << endl ;
224
225
226 if (!_minimizer) {
228 }
229
230 // Save current values of non-marginalized parameters
231 RooArgSet obsStart;
232 _obs.snapshot(obsStart, false) ;
233
234 // Start from previous global minimum
235 if (_paramAbsMin.getSize()>0) {
236 const_cast<RooSetProxy&>(_par).assignValueOnly(_paramAbsMin) ;
237 }
238 if (_obsAbsMin.getSize()>0) {
239 const_cast<RooSetProxy&>(_obs).assignValueOnly(_obsAbsMin) ;
240 }
241
242 // Find minimum with all observables floating
243 const_cast<RooSetProxy&>(_obs).setAttribAll("Constant",false) ;
244
245 //TString minim=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
246 //TString algorithm = ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str();
247 //if (algorithm == "Migrad") algorithm = "Minimize"; // prefer to use Minimize instead of Migrad
248 //_minimizer->minimize(minim.Data(),algorithm.Data());
249 _minimizer->migrad() ;
250
251 // Save value and remember
252 _absMin = _nll ;
253 _absMinValid = true ;
254
255 // Save parameter values at abs minimum as well
257
258 // Only store non-constant parameters here!
259 _paramAbsMin.addClone(*std::unique_ptr<RooArgSet>{static_cast<RooArgSet*>(_par.selectByAttrib("Constant",false))});
260
262
263 // Save constant status of all parameters
264 for(auto const& par : _par) {
265 _paramFixed[par->GetName()] = par->isConstant() ;
266 }
267
268 if (dologI(Minimization)) {
269 cxcoutI(Minimization) << "RooProfileLL::evaluate(" << GetName() << ") minimum found at (" ;
270
271 bool first=true ;
272 for(auto const& arg : _obs) {
273 ccxcoutI(Minimization) << (first?"":", ") << arg->GetName() << "="
274 << static_cast<RooAbsReal const*>(arg)->getVal() ;
275 first=false ;
276 }
277 ccxcoutI(Minimization) << ")" << endl ;
278 }
279
280 // Restore original parameter values
281 _obs.assign(obsStart) ;
282
283 }
284}
285
286
287
288////////////////////////////////////////////////////////////////////////////////
289
290bool RooProfileLL::redirectServersHook(const RooAbsCollection& /*newServerList*/, bool /*mustReplaceAll*/,
291 bool /*nameChange*/, bool /*isRecursive*/)
292{
293 _minimizer.reset(nullptr);
294 return false ;
295}
296
297
#define coutI(a)
Definition: RooMsgService.h:34
#define cxcoutI(a)
Definition: RooMsgService.h:89
#define ccoutP(a)
Definition: RooMsgService.h:43
#define dologI(a)
Definition: RooMsgService.h:70
#define ccxcoutI(a)
Definition: RooMsgService.h:90
#define ClassImp(name)
Definition: Rtypes.h:375
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
char name[80]
Definition: TGX11.cxx:110
RooArgSet * getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
Definition: RooAbsArg.h:317
RooArgSet * getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:569
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * selectByAttrib(const char *name, bool value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
Int_t getSize() const
Return the number of elements in the collection.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:64
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:94
virtual RooAbsReal * createProfile(const RooArgSet &paramsOfInterest)
Create a RooProfileLL object that eliminates all nuisance parameters in the present function.
Definition: RooAbsReal.cxx:511
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:180
static RooMsgService & instance()
Return reference to singleton instance.
void setSilentMode(bool flag)
bool silentMode() const
Class RooProfileLL implements the profile likelihood estimator for a given likelihood and set of para...
Definition: RooProfileLL.h:22
const RooArgSet & bestFitObs() const
RooArgSet _paramAbsMin
Parameter values at absolute minimum.
Definition: RooProfileLL.h:64
RooSetProxy _obs
Parameters of profile likelihood.
Definition: RooProfileLL.h:53
Int_t _neval
Number evaluations used in last minimization.
Definition: RooProfileLL.h:67
RooAbsReal * createProfile(const RooArgSet &paramsOfInterest) override
Optimized implementation of createProfile for profile likelihoods.
RooProfileLL()
Default constructor Should only be used by proof.
void initializeMinimizer() const
RooAbsReal & nll()
Definition: RooProfileLL.h:34
bool _absMinValid
flag if absmin is up-to-date
Definition: RooProfileLL.h:62
std::map< std::string, bool > _paramFixed
Parameter constant status at last time of use.
Definition: RooProfileLL.h:66
bool _startFromMin
Always start minimization for global minimum?
Definition: RooProfileLL.h:55
std::unique_ptr< RooMinimizer > _minimizer
! Internal minimizer instance
Definition: RooProfileLL.h:60
RooArgSet _obsAbsMin
Observable values at absolute minimum.
Definition: RooProfileLL.h:65
void validateAbsMin() const
Check that parameters and likelihood value for 'best fit' are still valid.
RooSetProxy _par
Marginalised parameters of likelihood.
Definition: RooProfileLL.h:54
double _absMin
absolute minimum of -log(L)
Definition: RooProfileLL.h:63
double evaluate() const override
Evaluate profile likelihood by minimizing likelihood w.r.t.
RooRealProxy _nll
Input -log(L) function.
Definition: RooProfileLL.h:52
bool redirectServersHook(const RooAbsCollection &, bool, bool, bool) override
Function that is called at the end of redirectServers().
const RooArgSet & bestFitParams() const
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
const T & arg() const
Return reference to object held in proxy.
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
@ Minimization
Definition: RooGlobalFunc.h:63
Definition: first.py:1