forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RooNLLVar.cxx
362 lines (286 loc) · 13.8 KB
/
RooNLLVar.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*****************************************************************************
* Project: RooFit *
* Package: RooFitCore *
* @(#)root/roofitcore:$Id$
* Authors: *
* WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
* DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
* *
* Copyright (c) 2000-2005, Regents of the University of California *
* and Stanford University. All rights reserved. *
* *
* Redistribution and use in source and binary forms, *
* with or without modification, are permitted according to the terms *
* listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
*****************************************************************************/
/**
\file RooNLLVar.cxx
\class RooNLLVar
\ingroup Roofitcore
Class RooNLLVar implements a -log(likelihood) calculation from a dataset
and a PDF. The NLL is calculated as
\f[
\sum_\mathrm{data} -\log( \mathrm{pdf}(x_\mathrm{data}))
\f]
In extended mode, a
\f$ N_\mathrm{expect} - N_\mathrm{observed}*log(N_\mathrm{expect}) \f$ term is added.
**/
#include "RooNLLVar.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooCmdConfig.h"
#include "RooMsgService.h"
#include "RooAbsDataStore.h"
#include "RooRealMPFE.h"
#include "RooRealSumPdf.h"
#include "RooRealVar.h"
#include "RooProdPdf.h"
#include "RooNaNPacker.h"
#include "RooDataHist.h"
#ifdef ROOFIT_CHECK_CACHED_VALUES
#include <iomanip>
#endif
#include "TMath.h"
#include "Math/Util.h"
#include <algorithm>
namespace {
template<class ...Args>
RooAbsTestStatistic::Configuration makeRooAbsTestStatisticCfg(Args const& ... args) {
RooAbsTestStatistic::Configuration cfg;
cfg.rangeName = RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","RangeWithName",0,"",args...);
cfg.addCoefRangeName = RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","AddCoefRange",0,"",args...);
cfg.nCPU = RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","NumCPU",0,1,args...);
cfg.interleave = RooFit::BulkPartition;
cfg.verbose = static_cast<bool>(RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","Verbose",0,1,args...));
cfg.splitCutRange = static_cast<bool>(RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","SplitRange",0,0,args...));
cfg.cloneInputData = static_cast<bool>(RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","CloneData",0,1,args...));
cfg.integrateOverBinsPrecision = RooCmdConfig::decodeDoubleOnTheFly("RooNLLVar::RooNLLVar", "IntegrateBins", 0, -1., {args...});
return cfg;
}
}
ClassImp(RooNLLVar)
RooNLLVar::~RooNLLVar() {}
RooArgSet RooNLLVar::_emptySet ;
////////////////////////////////////////////////////////////////////////////////
/// Construct likelihood from given p.d.f and (binned or unbinned dataset)
///
/// Argument | Description
/// -------------------------|------------
/// Extended() | Include extended term in calculation
/// NumCPU() | Activate parallel processing feature
/// Range() | Fit only selected region
/// SumCoefRange() | Set the range in which to interpret the coefficients of RooAddPdf components
/// SplitRange() | Fit range is split by index category of simultaneous PDF
/// ConditionalObservables() | Define conditional observables
/// Verbose() | Verbose output of GOF framework classes
/// CloneData() | Clone input dataset for internal use (default is true)
/// BatchMode() | Evaluate batches of data events (faster if PDFs support it)
/// IntegrateBins() | Integrate PDF within each bin. This sets the desired precision. Only useful for binned fits.
RooNLLVar::RooNLLVar(const char *name, const char* title, RooAbsPdf& pdf, RooAbsData& indata,
const RooCmdArg& arg1, const RooCmdArg& arg2,const RooCmdArg& arg3,
const RooCmdArg& arg4, const RooCmdArg& arg5,const RooCmdArg& arg6,
const RooCmdArg& arg7, const RooCmdArg& arg8,const RooCmdArg& arg9) :
RooAbsOptTestStatistic(name,title,pdf,indata,
*RooCmdConfig::decodeSetOnTheFly(
"RooNLLVar::RooNLLVar","ProjectedObservables",0,&_emptySet,
arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
makeRooAbsTestStatisticCfg(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))
{
RooCmdConfig pc("RooNLLVar::RooNLLVar") ;
pc.allowUndefined() ;
pc.defineInt("extended","Extended",0,false) ;
pc.defineInt("BatchMode", "BatchMode", 0, false);
pc.process(arg1) ; pc.process(arg2) ; pc.process(arg3) ;
pc.process(arg4) ; pc.process(arg5) ; pc.process(arg6) ;
pc.process(arg7) ; pc.process(arg8) ; pc.process(arg9) ;
_extended = pc.getInt("extended") ;
_skipZeroWeights = true;
}
////////////////////////////////////////////////////////////////////////////////
/// Construct likelihood from given p.d.f and (binned or unbinned dataset)
/// For internal use.
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
bool extended, RooAbsTestStatistic::Configuration const& cfg) :
RooNLLVar{name, title, pdf, indata, RooArgSet(), extended, cfg} {}
////////////////////////////////////////////////////////////////////////////////
/// Construct likelihood from given p.d.f and (binned or unbinned dataset)
/// For internal use.
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
const RooArgSet& projDeps,
bool extended, RooAbsTestStatistic::Configuration const& cfg) :
RooAbsOptTestStatistic(name,title,pdf,indata,projDeps, cfg),
_extended(extended)
{
// If binned likelihood flag is set, pdf is a RooRealSumPdf representing a yield vector
// for a binned likelihood calculation
_binnedPdf = cfg.binnedL ? static_cast<RooRealSumPdf*>(_funcClone) : nullptr ;
// Retrieve and cache bin widths needed to convert un-normalized binnedPdf values back to yields
if (_binnedPdf) {
// The Active label will disable pdf integral calculations
_binnedPdf->setAttribute("BinnedLikelihoodActive") ;
RooArgSet obs;
_funcClone->getObservables(_dataClone->get(), obs);
if (obs.size()!=1) {
_binnedPdf = nullptr;
} else {
auto* var = static_cast<RooRealVar*>(obs.first());
std::unique_ptr<std::list<double>> boundaries{_binnedPdf->binBoundaries(*var,var->getMin(),var->getMax())};
auto biter = boundaries->begin() ;
_binw.reserve(boundaries->size()-1) ;
double lastBound = (*biter) ;
++biter ;
while (biter!=boundaries->end()) {
_binw.push_back((*biter) - lastBound);
lastBound = (*biter) ;
++biter ;
}
}
_skipZeroWeights = false;
} else {
_skipZeroWeights = true;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Copy constructor
RooNLLVar::RooNLLVar(const RooNLLVar& other, const char* name) :
RooAbsOptTestStatistic(other,name),
_extended(other._extended),
_weightSq(other._weightSq),
_offsetSaveW2(other._offsetSaveW2),
_binw(other._binw),
_binnedPdf{other._binnedPdf}
{
}
////////////////////////////////////////////////////////////////////////////////
/// Create a test statistic using several properties of the current instance. This is used to duplicate
/// the test statistic in multi-processing scenarios.
RooAbsTestStatistic* RooNLLVar::create(const char *name, const char *title, RooAbsReal& pdf, RooAbsData& adata,
const RooArgSet& projDeps, RooAbsTestStatistic::Configuration const& cfg) {
RooAbsPdf & thePdf = dynamic_cast<RooAbsPdf&>(pdf);
// check if pdf can be extended
bool extendedPdf = _extended && thePdf.canBeExtended();
auto testStat = new RooNLLVar(name, title, thePdf, adata, projDeps, extendedPdf, cfg);
return testStat;
}
////////////////////////////////////////////////////////////////////////////////
void RooNLLVar::applyWeightSquared(bool flag)
{
if (_gofOpMode==Slave) {
if (flag != _weightSq) {
_weightSq = flag;
std::swap(_offset, _offsetSaveW2);
}
setValueDirty();
} else if ( _gofOpMode==MPMaster) {
for (int i=0 ; i<_nCPU ; i++)
_mpfeArray[i]->applyNLLWeightSquared(flag);
} else if ( _gofOpMode==SimMaster) {
for(auto& gof : _gofArray)
static_cast<RooNLLVar&>(*gof).applyWeightSquared(flag);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Calculate and return likelihood on subset of data.
/// \param[in] firstEvent First event to be processed.
/// \param[in] lastEvent First event not to be processed, any more.
/// \param[in] stepSize Steps between events.
/// \note For batch computations, the step size **must** be one.
///
/// If this an extended likelihood, the extended term is added to the return likelihood
/// in the batch that encounters the event with index 0.
double RooNLLVar::evaluatePartition(std::size_t firstEvent, std::size_t lastEvent, std::size_t stepSize) const
{
// Throughout the calculation, we use Kahan's algorithm for summing to
// prevent loss of precision - this is a factor four more expensive than
// straight addition, but since evaluating the PDF is usually much more
// expensive than that, we tolerate the additional cost...
ROOT::Math::KahanSum<double> result{0.0};
double sumWeight{0.0};
auto * pdfClone = static_cast<RooAbsPdf*>(_funcClone);
// If pdf is marked as binned - do a binned likelihood calculation here (sum of log-Poisson for each bin)
if (_binnedPdf) {
ROOT::Math::KahanSum<double> sumWeightKahanSum{0.0};
for (auto i=firstEvent ; i<lastEvent ; i+=stepSize) {
_dataClone->get(i) ;
double eventWeight = _dataClone->weight();
// Calculate log(Poisson(N|mu) for this bin
double N = eventWeight ;
double mu = _binnedPdf->getVal()*_binw[i] ;
//cout << "RooNLLVar::binnedL(" << GetName() << ") N=" << N << " mu = " << mu << endl ;
if (mu<=0 && N>0) {
// Catch error condition: data present where zero events are predicted
logEvalError(Form("Observed %f events in bin %lu with zero event yield",N,(unsigned long)i)) ;
} else if (std::abs(mu)<1e-10 && std::abs(N)<1e-10) {
// Special handling of this case since log(Poisson(0,0)=0 but can't be calculated with usual log-formula
// since log(mu)=0. No update of result is required since term=0.
} else {
result += -1*(-mu + N * std::log(mu) - TMath::LnGamma(N+1));
sumWeightKahanSum += eventWeight;
}
}
sumWeight = sumWeightKahanSum.Sum();
} else { //unbinned PDF
std::tie(result, sumWeight) = computeScalar(stepSize, firstEvent, lastEvent);
// include the extended maximum likelihood term, if requested
if(_extended && _setNum==_extSet) {
result += pdfClone->extendedTerm(*_dataClone, _weightSq, _doBinOffset);
}
} //unbinned PDF
// If part of simultaneous PDF normalize probability over
// number of simultaneous PDFs: -sum(log(p/n)) = -sum(log(p)) + N*log(n)
if (_simCount>1) {
result += sumWeight * std::log(static_cast<double>(_simCount));
}
// At the end of the first full calculation, wire the caches
if (_first) {
_first = false ;
_funcClone->wireAllCaches() ;
}
// Check if value offset flag is set.
if (_doOffset) {
// If no offset is stored enable this feature now
if (_offset.Sum() == 0 && _offset.Carry() == 0 && (result.Sum() != 0 || result.Carry() != 0)) {
coutI(Minimization) << "RooNLLVar::evaluatePartition(" << GetName() << ") first = "<< firstEvent << " last = " << lastEvent << " Likelihood offset now set to " << result.Sum() << std::endl ;
_offset = result ;
}
// Subtract offset
result -= _offset;
}
_evalCarry = result.Carry();
return result.Sum() ;
}
RooNLLVar::ComputeResult RooNLLVar::computeScalar(std::size_t stepSize, std::size_t firstEvent, std::size_t lastEvent) const {
auto pdfClone = static_cast<const RooAbsPdf*>(_funcClone);
return computeScalarFunc(pdfClone, _dataClone, _normSet, _weightSq, stepSize, firstEvent, lastEvent, _doBinOffset);
}
// static function, also used from TestStatistics::RooUnbinnedL
RooNLLVar::ComputeResult RooNLLVar::computeScalarFunc(const RooAbsPdf *pdfClone, RooAbsData *dataClone,
RooArgSet *normSet, bool weightSq, std::size_t stepSize,
std::size_t firstEvent, std::size_t lastEvent, bool doBinOffset)
{
ROOT::Math::KahanSum<double> kahanWeight;
ROOT::Math::KahanSum<double> kahanProb;
RooNaNPacker packedNaN(0.f);
const double logSumW = std::log(dataClone->sumEntries());
auto* dataHist = doBinOffset ? static_cast<RooDataHist*>(dataClone) : nullptr;
for (auto i=firstEvent; i<lastEvent; i+=stepSize) {
dataClone->get(i) ;
double weight = dataClone->weight(); //FIXME
const double ni = weight;
if (0. == weight * weight) continue ;
if (weightSq) weight = dataClone->weightSquared() ;
double logProba = pdfClone->getLogVal(normSet);
if(doBinOffset) {
logProba -= std::log(ni) - std::log(dataHist->binVolume(i)) - logSumW;
}
const double term = -weight * logProba;
kahanWeight.Add(weight);
kahanProb.Add(term);
packedNaN.accumulate(term);
}
if (packedNaN.getPayload() != 0.) {
// Some events with evaluation errors. Return "badness" of errors.
return {ROOT::Math::KahanSum<double>{packedNaN.getNaNWithPayload()}, kahanWeight.Sum()};
}
return {kahanProb, kahanWeight.Sum()};
}