Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooUnbinnedL.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13/**
14\file RooUnbinnedL.cxx
15\class RooUnbinnedL
16\ingroup Roofitcore
17
18A -log(likelihood) calculation from a dataset
19(assumed to be unbinned) and a PDF. The NLL is calculated as
20\f[
21 \sum_\mathrm{data} -\log( \mathrm{pdf}(x_\mathrm{data}))
22\f]
23In extended mode, a
24\f$ N_\mathrm{expect} - N_\mathrm{observed}*log(N_\mathrm{expect}) \f$ term is added.
25**/
26
28
29#include <RooAbsData.h>
30#include <RooAbsPdf.h>
31#include <RooAbsDataStore.h>
32#include <RooBatchCompute.h>
33#include <RooChangeTracker.h>
34#include <RooNaNPacker.h>
35#include <RooFit/Evaluator.h>
36
38
39namespace RooFit {
40namespace TestStatistics {
41
42namespace {
43
44RooAbsL::ClonePdfData clonePdfData(RooAbsPdf &pdf, RooAbsData &data)
45{
46 // For the evaluation with the BatchMode, the pdf needs to be "compiled" for
47 // a given normalization set.
48 return {RooFit::Detail::compileForNormSet(pdf, *data.get()), &data};
49}
50
51} // namespace
52
54 RooFit::EvalBackend evalBackend)
55 : RooAbsL(clonePdfData(*pdf, *data), data->numEntries(), 1, extended)
56{
57 std::unique_ptr<RooArgSet> params(pdf->getParameters(data));
58 paramTracker_ = std::make_unique<RooChangeTracker>("chtracker", "change tracker", *params, true);
59
60 evaluator_ = std::make_unique<RooFit::Evaluator>(*pdf_, evalBackend.value() == RooFit::EvalBackend::Value::Cuda);
61 std::stack<std::vector<double>>{}.swap(_vectorBuffers);
62 // Zero-weight events must not be skipped here: the probabilities from the
63 // evaluator are indexed by the original event indices, aligned with the
64 // weights obtained from RooAbsData::getWeightBatch(). Events with zero
65 // weight are skipped in the summation instead.
66 auto dataSpans = RooFit::BatchModeDataHelpers::getDataSpans(*data, "", nullptr, /*skipZeroWeights=*/false,
67 /*takeGlobalObservablesFromData=*/false, _vectorBuffers);
68 for (auto const &item : dataSpans) {
69 evaluator_->setInput(item.first->GetName(), item.second, false);
70 }
71}
72
74 : RooAbsL(other),
75 apply_weight_squared(other.apply_weight_squared),
76 lastSection_(other.lastSection_),
77 cachedResult_(other.cachedResult_),
78 evaluator_(other.evaluator_)
79{
80 paramTracker_ = std::make_unique<RooChangeTracker>(*other.paramTracker_);
81}
82
84
85//////////////////////////////////////////////////////////////////////////////////
86
87/// Returns true if value was changed, false otherwise.
89{
90 if (apply_weight_squared != flag) {
92 return true;
93 }
94 // setValueDirty();
95 return false;
96}
97
98namespace {
99
100using ComputeResult = std::pair<ROOT::Math::KahanSum<double>, double>;
101
102// The probabilities were already evaluated
103// as a batch, and the weights are also retrieved as batches instead of looping
104// over RooAbsData::get(i), which loads every column of the dataset only to
105// then read a single weight. The reduction is done with the same vectorized
106// RooBatchCompute::reduceNLL() that RooNLLVarNew uses in the standard
107// evaluation backend, including its RooNaNPacker-based error propagation.
108ComputeResult computeBatchFunc(std::span<const double> probas, RooAbsData *dataClone, bool weightSq,
109 std::size_t firstEvent, std::size_t lastEvent, std::vector<double> &unitWeights,
110 RooBatchCompute::Config const &cfg)
111{
112 const std::size_t nEvents = lastEvent - firstEvent;
113 // Empty spans mean the dataset is unweighted, i.e. all weights are one.
114 std::span<const double> dataWeights = dataClone->getWeightBatch(firstEvent, nEvents, /*sumW2=*/weightSq);
115
116 double sumWeight;
117 const double *weightData = nullptr;
118 std::size_t nWeights = 0;
119 if (dataWeights.empty()) {
120 if (unitWeights.size() < nEvents) {
121 unitWeights.assign(nEvents, 1.0);
122 }
123 weightData = unitWeights.data();
124 nWeights = nEvents;
125 sumWeight = nEvents;
126 } else {
127 weightData = dataWeights.data();
128 nWeights = dataWeights.size();
129 sumWeight = RooBatchCompute::reduceSum(cfg, weightData, nWeights);
130 }
131 std::span<const double> weights{weightData, nWeights};
132
133 std::span<const double> probasInRange{probas.data() + firstEvent, nEvents};
134
135 auto out = RooBatchCompute::reduceNLL(cfg, probasInRange, weights, {});
136 return {ROOT::Math::KahanSum<double>{out.nllSum, out.nllSumCarry}, sumWeight};
137}
138
139} // namespace
140
141//////////////////////////////////////////////////////////////////////////////////
142/// Calculate and return likelihood on subset of data from firstEvent to lastEvent
143/// processed with a step size of 'stepSize'. If this an extended likelihood and
144/// and the zero event is processed the extended term is added to the return
145/// likelihood.
146///
148RooUnbinnedL::evaluatePartition(Section events, std::size_t /*components_begin*/, std::size_t /*components_end*/)
149{
150 // Throughout the calculation, we use Kahan's algorithm for summing to
151 // prevent loss of precision - this is a factor four more expensive than
152 // straight addition, but since evaluating the PDF is usually much more
153 // expensive than that, we tolerate the additional cost...
155 double sumWeight;
156 auto numEvalErrorsBefore = RooAbsReal::numEvalErrors();
157
158 // Do not reevaluate likelihood if parameters nor event range have changed
159 if (!paramTracker_->hasChanged(true) && events == lastSection_ &&
160 (cachedResult_.Sum() != 0 || cachedResult_.Carry() != 0))
161 return cachedResult_;
162
163 // Here, we have a memory allocation that should be avoided when this
164 // code needs to be optimized.
165 std::span<const double> probas = evaluator_->run();
166 std::tie(result, sumWeight) = computeBatchFunc(probas, data_.get(), apply_weight_squared, events.begin(N_events_),
168
169 // include the extended maximum likelihood term, if requested
170 if (extended_ && events.begin_fraction == 0) {
171 result += pdf_->extendedTerm(*data_, apply_weight_squared);
172 }
173
174 // If part of simultaneous PDF normalize probability over
175 // number of simultaneous PDFs: -sum(log(p/n)) = -sum(log(p)) + N*log(n)
176 if (sim_count_ > 1) {
177 result += sumWeight * log(1.0 * sim_count_);
178 }
179
182 numEvalErrorsBefore == RooAbsReal::numEvalErrors()) {
184 lastSection_ = events;
185 }
186 return result;
187}
188
189} // namespace TestStatistics
190} // namespace RooFit
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
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 result
The Kahan summation is a compensated summation algorithm, which significantly reduces numerical error...
Definition Util.h:141
T Sum() const
Definition Util.h:259
T Carry() const
Definition Util.h:269
RooFit::OwningPtr< 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...
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
virtual std::span< const double > getWeightBatch(std::size_t first, std::size_t len, bool sumW2=false) const =0
Return event weights of all events in range [first, first+len).
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
static ErrorLoggingMode evalErrorLoggingMode()
Return current evaluation error logging mode.
static Int_t numEvalErrors()
Return the number of logged evaluation errors since the last clearing.
Minimal configuration struct to steer the evaluation of a single node with the RooBatchCompute librar...
Value value() const
std::shared_ptr< RooAbsData > data_
Definition RooAbsL.h:136
std::shared_ptr< RooAbsPdf > pdf_
Definition RooAbsL.h:135
ROOT::Math::KahanSum< double > cachedResult_
std::vector< double > _unitWeights
! all-ones weights for unweighted data in batched evaluation
bool setApplyWeightSquared(bool flag)
Returns true if value was changed, false otherwise.
ROOT::Math::KahanSum< double > evaluatePartition(Section events, std::size_t components_begin, std::size_t components_end) override
Calculate and return likelihood on subset of data from firstEvent to lastEvent processed with a step ...
RooUnbinnedL(RooAbsPdf *pdf, RooAbsData *data, RooAbsL::Extended extended=RooAbsL::Extended::Auto, RooFit::EvalBackend evalBackend=RooFit::EvalBackend::Cpu())
std::unique_ptr< RooChangeTracker > paramTracker_
bool apply_weight_squared
Apply weights squared?
std::stack< std::vector< double > > _vectorBuffers
std::shared_ptr< RooFit::Evaluator > evaluator_
! For batched evaluation
double reduceSum(Config cfg, InputArr input, size_t n)
ReduceNLLOutput reduceNLL(Config cfg, std::span< const double > probas, std::span< const double > weights, std::span< const double > offsetProbas)
std::unique_ptr< T > compileForNormSet(T const &arg, RooArgSet const &normSet)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
void probas(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
A part of some range delimited by two fractional points between 0 and 1 (inclusive).
Definition RooAbsL.h:64
std::size_t begin(std::size_t N_total) const
Definition RooAbsL.h:72
std::size_t end(std::size_t N_total) const
Definition RooAbsL.h:74