Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooBernstein.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitModels *
4 * @(#)root/roofit:$Id$
5 * Authors: *
6 * Kyle Cranmer *
7 * *
8 *****************************************************************************/
9
10/** \class RooBernstein
11 \ingroup Roofit
12
13Bernstein basis polynomials are positive-definite in the range [0,1].
14In this implementation, we extend [0,1] to be the range of the parameter.
15There are n+1 Bernstein basis polynomials of degree n:
16\f[
17 B_{i,n}(x) = \begin{pmatrix}n \\\ i \end{pmatrix} x^i \cdot (1-x)^{n-i}
18\f]
19Thus, by providing n coefficients that are positive-definite, there
20is a natural way to have well-behaved polynomial PDFs. For any n, the n+1 polynomials
21'form a partition of unity', i.e., they sum to one for all values of x.
22They can be used as a basis to span the space of polynomials with degree n or less:
23\f[
24 PDF(x, c_0, ..., c_n) = \mathcal{N} \cdot \sum_{i=0}^{n} c_i \cdot B_{i,n}(x).
25\f]
26By giving n+1 coefficients in the constructor, this class constructs the n+1
27polynomials of degree n, and sums them to form an element of the space of polynomials
28of degree n. \f$ \mathcal{N} \f$ is a normalisation constant that takes care of the
29cases where the \f$ c_i \f$ are not all equal to one.
30
31See also
32http://www.idav.ucdavis.edu/education/CAGDNotes/Bernstein-Polynomials.pdf
33**/
34
35#include "RooBernstein.h"
36#include "RooRealVar.h"
37#include "RooArgList.h"
38#include "RooBatchCompute.h"
39
40#include "TMath.h"
41
42#include <cmath>
43
45
46/// Constructor
47////////////////////////////////////////////////////////////////////////////////
48
49RooBernstein::RooBernstein(const char* name, const char* title,
50 RooAbsRealLValue& x, const RooArgList& coefList):
51 RooAbsPdf(name, title),
52 _x("x", "Dependent", this, x),
53 _coefList("coefficients","List of coefficients",this)
54{
55 _coefList.addTyped<RooAbsReal>(coefList);
56}
57
58////////////////////////////////////////////////////////////////////////////////
59
61 : RooAbsPdf(other, name),
62 _x("x", this, other._x),
63 _coefList("coefList", this, other._coefList),
64 _refRangeName{other._refRangeName}
65{
66}
67
68////////////////////////////////////////////////////////////////////////////////
69
70/// Force use of a given normalisation range.
71/// Needed for functions or PDFs (e.g. RooAddPdf) whose shape depends on the choice of normalisation.
72void RooBernstein::selectNormalizationRange(const char* rangeName, bool force)
73{
74 if (rangeName && (force || !_refRangeName.empty())) {
75 _refRangeName = rangeName;
76 }
77}
78
79////////////////////////////////////////////////////////////////////////////////
80
82{
83 double xmax;
84 double xmin;
85 std::tie(xmin, xmax) = _x->getRange(_refRangeName.empty() ? nullptr : _refRangeName.c_str());
86 double x = (_x - xmin) / (xmax - xmin); // rescale to [0,1]
87 Int_t degree = _coefList.size() - 1; // n+1 polys of degree n
88
89 if(degree == 0) {
90
91 return static_cast<RooAbsReal &>(_coefList[0]).getVal();
92
93 } else if(degree == 1) {
94
95 double a0 = static_cast<RooAbsReal &>(_coefList[0]).getVal(); // c0
96 double a1 = static_cast<RooAbsReal &>(_coefList[1]).getVal() - a0; // c1 - c0
97 return a1 * x + a0;
98
99 } else if(degree == 2) {
100
101 double a0 = static_cast<RooAbsReal &>(_coefList[0]).getVal(); // c0
102 double a1 = 2 * (static_cast<RooAbsReal &>(_coefList[1]).getVal() - a0); // 2 * (c1 - c0)
103 double a2 = static_cast<RooAbsReal &>(_coefList[2]).getVal() - a1 - a0; // c0 - 2 * c1 + c2
104 return (a2 * x + a1) * x + a0;
105
106 } else if(degree > 2) {
107
108 double t = x;
109 double s = 1 - x;
110
111 double result = static_cast<RooAbsReal &>(_coefList[0]).getVal() * s;
112 for(Int_t i = 1; i < degree; i++) {
113 result = (result + t * TMath::Binomial(degree, i)
114 * static_cast<RooAbsReal &>(_coefList[i]).getVal()) * s;
115 t *= x;
116 }
117 result += t * static_cast<RooAbsReal &>(_coefList[degree]).getVal();
118
119 return result;
120 }
121
122 // in case list of arguments passed is empty
123 return TMath::SignalingNaN();
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Compute multiple values of Bernstein distribution.
129{
130 const int nCoef = _coefList.size();
131 std::vector<double> extraArgs(nCoef+2);
132 for (int i=0; i<nCoef; i++)
133 extraArgs[i] = static_cast<RooAbsReal&>(_coefList[i]).getVal();
134 extraArgs[nCoef] = _x.min();
135 extraArgs[nCoef+1] = _x.max();
136
137 RooBatchCompute::compute(ctx.config(this), RooBatchCompute::Bernstein, ctx.output(), {ctx.at(_x)}, extraArgs);
138}
139
140////////////////////////////////////////////////////////////////////////////////
141
142Int_t RooBernstein::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
143{
144
145 if (matchArgs(allVars, analVars, _x)) return 1;
146 return 0;
147}
148
149////////////////////////////////////////////////////////////////////////////////
150
151double RooBernstein::analyticalIntegral(Int_t code, const char* rangeName) const
152{
153 R__ASSERT(code==1) ;
154
155 double xmax;
156 double xmin;
157 std::tie(xmin, xmax) = _x->getRange(_refRangeName.empty() ? nullptr : _refRangeName.c_str());
158
159 const double xlo = (_x.min(rangeName) - xmin) / (xmax - xmin);
160 const double xhi = (_x.max(rangeName) - xmin) / (xmax - xmin);
161
162 Int_t degree= _coefList.size()-1; // n+1 polys of degree n
163 double norm(0) ;
164
165 double temp=0;
166 for (int i=0; i<=degree; ++i){
167 // for each of the i Bernstein basis polynomials
168 // represent it in the 'power basis' (the naive polynomial basis)
169 // where the integral is straight forward.
170 temp = 0;
171 for (int j=i; j<=degree; ++j){ // power basisŧ
172 temp += pow(-1.,j-i) * TMath::Binomial(degree, j) * TMath::Binomial(j,i) * (TMath::Power(xhi,j+1) - TMath::Power(xlo,j+1)) / (j+1);
173 }
174 temp *= static_cast<RooAbsReal &>(_coefList[i]).getVal(); // include coeff
175 norm += temp; // add this basis's contribution to total
176 }
177
178 return norm * (xmax - xmin);
179}
#define ClassImp(name)
Definition Rtypes.h:377
#define R__ASSERT(e)
Definition TError.h:118
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
char name[80]
Definition TGX11.cxx:110
float xmin
float xmax
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
std::pair< double, double > getRange(const char *name=nullptr) const
Get low and high bound of the variable.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:59
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
bool matchArgs(const RooArgSet &allDeps, RooArgSet &numDeps, const RooArgProxy &a) const
Utility function for use in getAnalyticalIntegral().
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Bernstein basis polynomials are positive-definite in the range [0,1].
void selectNormalizationRange(const char *rangeName=nullptr, bool force=false) override
Force use of a given normalisation range.
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
std::string _refRangeName
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
void doEval(RooFit::EvalContext &) const override
Compute multiple values of Bernstein distribution.
RooTemplateProxy< RooAbsRealLValue > _x
RooListProxy _coefList
std::span< double > output()
RooBatchCompute::Config config(RooAbsArg const *arg) const
double max(const char *rname=nullptr) const
Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
double min(const char *rname=nullptr) const
Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
Double_t x[n]
Definition legend1.C:17
void compute(Config cfg, Computer comp, std::span< double > output, VarSpan vars, ArgSpan extraArgs={})
Double_t Binomial(Int_t n, Int_t k)
Calculates the binomial coefficient n over k.
Definition TMath.cxx:2111
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition TMath.h:721
Double_t SignalingNaN()
Returns a signaling NaN as defined by IEEE 754](http://en.wikipedia.org/wiki/NaN#Signaling_NaN).
Definition TMath.h:910