62 fKernelFunction(nullptr),
67 fApproximateBias(nullptr),
71 fNBins(0), fNEvents(0), fSumOfCounts(0), fUseBinsNEvents(0),
72 fMean(0.),fSigma(0.), fSigmaRob(0.), fXMin(0.), fXMax(0.),
73 fRho(0.), fAdaptiveBandwidthFactor(0.), fWeightSize(0)
102 fNBins = events < 10000 ? 1000 : std::min(10000,
int(events / 100)*10);
130 std::string options = opt.
Data();
133 for (std::vector<std::string>::iterator it =
voption.
begin(); it !=
voption.
end() && !options.empty(); ++it) {
134 size_t pos = options.find_last_of(
';');
135 if (pos == std::string::npos) {
139 *it = options.substr(pos + 1);
140 options = options.substr(0, pos);
143 size_t pos = (*it).find(
':');
144 if (pos != std::string::npos) {
145 GetOptions((*it).substr(0, pos) , (*it).substr(pos + 1));
157 for (std::vector<std::string>::iterator it =
voption.
begin(); it !=
voption.
end() && !options.empty(); ++it) {
158 size_t pos = options.find_last_of(
';');
159 if (pos == std::string::npos) {
163 *it = options.substr(pos + 1);
164 options = options.substr(0, pos);
168 for (std::vector<std::string>::iterator it =
voption.
begin(); it !=
voption.
end() && !options.empty(); ++it) {
169 size_t pos = (*it).find(
':');
170 if (pos == std::string::npos)
break;
180 this->
Warning(
"SetDrawOptions",
"Unknown plotting option %s: setting to KDE estimate plot.",optionInstance.
Data());
181 this->
Info(
"SetDrawOptions",
"Possible plotting options are: Estimate,Errors,ConfidenceInterval");
183 }
else if (optionType.
Contains(
"drawoptions")) {
189 this->
Warning(
"SetDrawOptions",
"No plotting option: setting to KDE estimate plot.");
190 plotOpt =
"estimate";
193 this->
Warning(
"SetDrawOptions",
"No drawing options: setting to default ones.");
202 if (
option ==
"gaussian") {
204 }
else if (
option ==
"epanechnikov") {
206 }
else if (
option ==
"biweight") {
208 }
else if (
option ==
"cosinearch") {
210 }
else if (
option ==
"userdefined") {
213 this->
Warning(
"GetOptions",
"Unknown kernel type option %s: setting to Gaussian",option.c_str());
214 this->
Info(
"GetOptions",
"Possible kernel type options are: Gaussian, Epanechnikov, Biweight, Cosinearch, Userdefined");
217 }
else if (optionType ==
"iteration") {
219 if (
option ==
"adaptive") {
221 }
else if (
option ==
"fixed") {
224 this->
Warning(
"GetOptions",
"Unknown iteration option %s: setting to Adaptive",option.c_str());
225 this->
Info(
"GetOptions",
"Possible iteration type options are: Adaptive, Fixed");
228 }
else if (optionType ==
"mirror") {
230 if (
option ==
"nomirror") {
232 }
else if (
option ==
"mirrorleft") {
234 }
else if (
option ==
"mirrorright") {
236 }
else if (
option ==
"mirrorboth") {
238 }
else if (
option ==
"mirrorasymleft") {
240 }
else if (
option ==
"mirrorrightasymleft") {
242 }
else if (
option ==
"mirrorasymright") {
244 }
else if (
option ==
"mirrorleftasymright") {
246 }
else if (
option ==
"mirrorasymboth") {
249 this->
Warning(
"GetOptions",
"Unknown mirror option %s: setting to NoMirror",option.c_str());
250 this->
Info(
"GetOptions",
"Possible mirror type options are: NoMirror, MirrorLeft, MirrorRight, MirrorAsymLeft,"
251 "MirrorAsymRight, MirrorRightAsymLeft, MirrorLeftAsymRight, MirrorAsymBoth");
254 }
else if (optionType ==
"binning") {
256 if (
option ==
"unbinned") {
258 }
else if (
option ==
"relaxedbinning") {
260 }
else if (
option ==
"forcedbinning") {
263 this->
Warning(
"GetOptions",
"Unknown binning option %s: setting to RelaxedBinning", option.c_str());
264 this->
Info(
"GetOptions",
"Possible binning type options are: Unbinned, ForcedBinning, RelaxedBinning");
289 Error(
"CheckOptions",
"Illegal user kernel type input! Use template constructor for user defined kernel.");
292 Warning(
"CheckOptions",
"Illegal user iteration type input - use default value !");
296 Warning(
"CheckOptions",
"Illegal user mirroring type input - use default value !");
300 Warning(
"CheckOptions",
"Illegal user binning type input - use default value !");
304 Warning(
"CheckOptions",
"Tuning factor rho cannot be non-positive - use default value !");
349 Error(
"SetNBins",
"Number of bins must be greater than zero.");
358 Warning(
"SetNBins",
"Bin type using SetBinning must be set for using a binned evaluation");
360 Warning(
"SetNBins",
"Bin type using SetBinning or with SetUseBinsNEvents must be set for using a binned evaluation");
383 Error(
"SetRange",
"Minimum range cannot be bigger or equal than the maximum range! Present range values remain the same.");
458 this->
Warning(
"SetData",
"Default number of bins is greater or equal to number of events. Use SetNBins(UInt_t) to set the appropriate number of bins");
488 Error(
"ReInit",
"TKDE does not contain any data !");
504 Error(
"InitFromNewData",
"Re-felling is not supported with binning");
563 std::bind(std::minus<Double_t>(), 2 *
fXMin, std::placeholders::_1));
568 std::bind(std::minus<Double_t>(), 2 *
fXMax, std::placeholders::_1));
606 fKernel = std::make_unique<TKernel>(weight,
this);
609 fKernel->ComputeAdaptiveWeights();
614 "Using a fix kernel - bandwidth = %f - using n = %d, rho = %f , sigmaRob = %f , mean = %f , sigma = %f "
615 ", canonicalBandwidth= %f",
619 "Using an adaptive kernel - weight = %f - using n = %d, rho = %f , sigmaRob = %f , mean = %f , sigma = %f "
620 ", canonicalBandwidth= %f",
629 Fatal(
"SetKernelFunction",
"Kernel function pointer is not null");
662 Error(
"SetKernelFunction",
"User kernel function is not defined !");
716 this->
Warning(
"Fill",
"Cannot fill data with data binned option. Data input ignored.");
719 fData.push_back(data);
727 this->
Warning(
"Fill",
"Cannot fill data with data binned option. Data input ignored.");
730 fData.push_back(data);
744 (
const_cast<TKDE*
>(
this))->ReInit();
753 if (
fNewData) (
const_cast<TKDE*
>(
this))->InitFromNewData();
759 if (
fNewData) (
const_cast<TKDE*
>(
this))->InitFromNewData();
778 unsigned int n = fKDE->fData.size();
781 std::vector<Double_t> weights(
n, fWeights[0]);
784 for (
unsigned int i = 0; i <
n; ++i) {
787 weights[i] = fWeights[0];
790 f = (*fKDE->fKernel)(fKDE->fData[i]);
793 fKDE->Warning(
"ComputeAdativeWeights",
"function value is zero or negative for x = %f w = %f - set their bandwidth to zero",
800 weights[i] = std::max(weights[i] /= std::sqrt(
f),
minWeight);
801 fKDE->fAdaptiveBandwidthFactor += std::log(
f);
806 fKDE->fAdaptiveBandwidthFactor = fKDE->fUseMirroring ?
kAPPROX_GEO_MEAN / fKDE->fSigmaRob : std::sqrt(std::exp(fKDE->fAdaptiveBandwidthFactor / fKDE->fData.size()));
809 transform(weights.begin(), weights.end(), fWeights.begin(),
810 std::bind(std::multiplies<Double_t>(), std::placeholders::_1, fKDE->fAdaptiveBandwidthFactor));
816 return fWeights[fKDE->Index(
x)];
903 if (
plotOpt.Contains(
"errors")) {
904 drawOpt.ReplaceAll(
"errors",
"");
907 else if (
plotOpt.Contains(
"confidenceinterval") ||
908 plotOpt.Contains(
"confinterval")) {
910 drawOpt.ReplaceAll(
"confidenceinterval",
"");
911 drawOpt.ReplaceAll(
"confinterval",
"");
915 if (s !=
nullptr)
sscanf(s,
"interval@%lf",&level);
916 if((level <= 0) || (level >= 1)) {
917 Warning(
"Draw",
"given confidence level %.3lf is invalid - use default 0.95",level);
947 for (
UInt_t i = 0; i <=
n; ++i) {
949 y[i] = (*this)(
x[i]);
954 ge->SetName(
"kde_graph_error");
955 ge->SetTitle(
"Errors");
983 this->
Warning(
"GetFixedWeight()",
"Fixed iteration option not enabled. Returning %f.", result);
993 this->
Warning(
"GetFixedWeight()",
"Adaptive iteration option not enabled. Returning a NULL pointer<");
996 if (
fNewData) (
const_cast<TKDE*
>(
this))->InitFromNewData();
997 return &(
fKernel->GetAdaptiveWeights()).front();
1013 UInt_t n = fKDE->fData.size();
1023 for (
UInt_t i = 0; i <
n; ++i) {
1030 if (fWeights[i] == 0)
continue;
1034 if (fKDE->fAsymLeft) {
1037 if (fKDE->fAsymRight) {
1043 fKDE->Warning(
"operator()",
"Result is NaN for x %f \n",
x);
1054 if (bin == (
Int_t)
fData.size())
return --bin;
1057 }
else if (bin <= 0) {
1097 return std::sqrt(
result);
1106 Error(
"CheckKernelValidity",
"Kernel's integral is %f",
unity);
1111 Error(
"CheckKernelValidity",
"Kernel's mu is %f" ,mu);
1116 Error(
"CheckKernelValidity",
"Kernel's sigma2 is %f",
sigma2);
1119 Error(
"CheckKernelValidity",
"Validation conditions: the kernel's integral must be 1, the kernel's mu must be zero and the kernel's sigma2 must be finite positive to be a suitable kernel.");
1218 if (fIntegralResult == kNorm) {
1219 return std::pow((*fKDE->fKernelFunction)(
x), 2);
1220 }
else if (fIntegralResult == kMu) {
1221 return x * (*fKDE->fKernelFunction)(
x);
1222 }
else if (fIntegralResult == kSigma2) {
1223 return std::pow(
x, 2) * (*fKDE->fKernelFunction)(
x);
1224 }
else if (fIntegralResult == kUnitIntegration) {
1225 return (*fKDE->fKernelFunction)(
x);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
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
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
User Class for performing numerical integration of a function in one dimension.
User class for calculating the derivatives of a function.
Template class to wrap any C++ callable object which takes one argument i.e.
Template class to wrap any member function of a class taking a double and returning a double in a 1D ...
const_iterator begin() const
const_iterator end() const
void SetTitle(const char *title="") override
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
void Draw(Option_t *option="") override
Draw this function with its current attributes.
static Bool_t DefaultAddToGlobalList(Bool_t on=kTRUE)
Static method to add/avoid to add automatically functions to the global list (gROOT->GetListOfFunctio...
A TGraphErrors is a TGraph with error bars.
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
1-D histogram with a double per channel (see TH1 documentation)
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Double_t GetRMS(Int_t axis=1) const
This function returns the Standard Deviation (Sigma) of the distribution not the Root Mean Square (RM...
virtual void FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride=1)
Fill this histogram with an array x and weights w.
virtual Int_t GetQuantiles(Int_t n, Double_t *xp, const Double_t *p=nullptr)
Compute Quantiles for this histogram Quantile x_p := Q(p) is defined as the value x_p such that the c...
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
void ComputeAdaptiveWeights()
Double_t GetFixedWeight() const
TKernel(Double_t weight, TKDE *kde)
Double_t GetWeight(Double_t x) const
Double_t operator()(Double_t x) const
const std::vector< Double_t > & GetAdaptiveWeights() const
Kernel Density Estimation class.
TF1 * GetPDFUpperConfidenceInterval(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
TF1 * GetKDEApproximateBias(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
void SetData(const Double_t *data, const Double_t *weights)
TF1 * fLowerPDF
Output Kernel Density Estimation upper confidence interval PDF function.
std::vector< Double_t > fKernelSigmas2
Double_t ComputeKernelL2Norm() const
TF1 * GetPDFLowerConfidenceInterval(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
void SetKernelType(EKernelType kern)
std::vector< Double_t > fCanonicalBandwidths
UInt_t fNEvents
Data's number of events.
void ComputeDataStats()
Internal function to compute statistics (mean,stddev) using always all the provided data (i....
Double_t fXMax
Data maximum value.
Double_t UpperConfidenceInterval(const Double_t *x, const Double_t *p) const
Valid if the bandwidth is small compared to nEvents**1/5.
Double_t ApproximateBias(const Double_t *x, const Double_t *) const
Double_t ComputeMidspread()
Bool_t fNewData
Flag to control when new data are given.
void DrawConfidenceInterval(TString &drawOpt, double cl=0.95)
// Draws the KDE and its confidence interval
void SetMirroredEvents()
Intgernal function to mirror the data.
void SetUserCanonicalBandwidth()
void CheckKernelValidity()
const Double_t * GetAdaptiveWeights() const
Double_t fAdaptiveBandwidthFactor
Geometric mean of the kernel density estimation from the data for adaptive iteration.
void SetKernelFunction(KernelFunction_Ptr kernfunc=nullptr)
Double_t LowerConfidenceInterval(const Double_t *x, const Double_t *p) const
Valid if the bandwidth is small compared to nEvents**1/5.
Double_t fSigmaRob
Data std deviation (robust estimation)
std::vector< Double_t > fBinCount
Number of events per bin for binned data option.
void Draw(const Option_t *option="") override
Draws either the KDE functions or its errors.
EIteration
Iteration types. They can be set using SetIteration()
Double_t GetRAMISE() const
void SetIteration(EIteration iter)
Double_t ComputeKernelIntegral() const
Double_t CosineArchKernel(Double_t x) const
Returns the kernel evaluation at x.
Double_t fXMin
Data minimum value.
Double_t operator()(Double_t x) const
void SetUserKernelSigma2()
Double_t GetBias(Double_t x) const
std::vector< Double_t > fData
Data events.
Double_t fSumOfCounts
Data sum of weights.
UInt_t fUseBinsNEvents
If the algorithm is allowed to use automatic (relaxed) binning this is the minimum number of events t...
Double_t fSigma
Data std deviation.
TGraphErrors * GetGraphWithErrors(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
return a TGraphErrors with the KDE values and errors The return object is managed by the user
Double_t fRho
Adjustment factor for sigma.
Double_t fWeightSize
Caches the weight size.
void SetUseBinsNEvents(UInt_t nEvents)
std::vector< Double_t > fEvents
Original data storage.
Double_t GetError(Double_t x) const
TF1 * GetKDEFunction(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
void SetBinning(EBinning)
void GetOptions(std::string optionType, std::string option)
std::vector< Bool_t > fSettedOptions
User input options flag.
Double_t GaussianKernel(Double_t x) const
Returns the kernel evaluation at x.
void SetRange(Double_t xMin, Double_t xMax)
By default computed from the data.
Double_t ComputeKernelSigma2() const
void SetOptions(const Option_t *option, Double_t rho)
Double_t GetFixedWeight() const
TF1 * GetFunction(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
TF1 * GetUpperFunction(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
void Instantiate(KernelFunction_Ptr kernfunc, UInt_t events, const Double_t *data, const Double_t *weight, Double_t xMin, Double_t xMax, const Option_t *option, Double_t rho)
void SetDrawOptions(const Option_t *option, TString &plotOpt, TString &drawOpt)
EMirror
Data "mirroring" option to address the probability "spill out" boundary effect They can be set using ...
TGraphErrors * fGraph
Output Kernel Density Estimation approximate bias.
void SetCanonicalBandwidths()
TKDE()
default constructor used only by I/O
void SetBinCentreData(Double_t xmin, Double_t xmax)
void SetTuneFactor(Double_t rho)
UInt_t Index(Double_t x) const
compute the bin index given a data point x
TF1 * fUpperPDF
Output Kernel Density Estimation PDF function.
UInt_t fNBins
Number of bins for binned data option.
Double_t ComputeKernelMu() const
EKernelType
Types of Kernel functions They can be set using the function SetKernelType() or as a string in the co...
@ kTotalKernels
Internal use only for member initialization.
@ kUserDefined
Internal use only for the class's template constructor.
void DrawErrors(TString &drawOpt)
Draws a TGraphErrors with KDE values and errors.
void SetNBins(UInt_t nbins)
void CheckOptions(Bool_t isUserDefinedKernel=kFALSE)
Double_t EpanechnikovKernel(Double_t x) const
Double_t BiweightKernel(Double_t x) const
Returns the kernel evaluation at x.
std::unique_ptr< TKernel > fKernel
! internal kernel class. Transient because it is recreated after reading from a file
Bool_t fUseMinMaxFromData
Flag top control if min and max must be used from data.
Double_t GetSigma() const
EKernelType fKernelType
Graph with the errors.
TF1 * fApproximateBias
Output Kernel Density Estimation lower confidence interval PDF function.
void SetSigma(Double_t R)
std::vector< Double_t > fEventWeights
Original data weights.
TF1 * GetApproximateBias(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
TF1 * GetLowerFunction(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
KernelFunction_Ptr fKernelFunction
! pointer to kernel function
friend struct KernelIntegrand
EBinning
Data binning option.
@ kRelaxedBinning
The algorithm is allowed to use binning if the data is large enough.
const char * GetName() const override
Returns name of object.
const char * GetTitle() const override
Returns title of object.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
void ToLower()
Change string to lower-case.
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
@ kGAUSS
simple Gauss integration method with fixed rule
double normal_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
Double_t QuietNaN()
Returns a quiet NaN as defined by IEEE 754.
void Quantiles(Int_t n, Int_t nprob, Double_t *x, Double_t *quantiles, Double_t *prob, Bool_t isSorted=kTRUE, Int_t *index=nullptr, Int_t type=7)
Computes sample quantiles, corresponding to the given probabilities.
KernelIntegrand(const TKDE *kde, EIntegralResult intRes)
Double_t operator()(Double_t x) const
EIntegralResult fIntegralResult