Logo ROOT   6.10/09
Reference Guide
TKDE.cxx
Go to the documentation of this file.
1 // // @(#)root/hist:$Id$
2 // Authors: Bartolomeu Rabacal 07/2010
3 /**********************************************************************
4  * *
5  * Copyright (c) 2006 , ROOT MathLib Team *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  * *
10  **********************************************************************/
11 //////////////////////////////////////////////////////////////////////////////
12 /** \class TKDE
13  \ingroup Hist
14  Kernel Density Estimation class.
15  The three main references are:
16  1. "Scott DW, Multivariate Density Estimation. Theory, Practice and Visualization. New York: Wiley",
17  2. "Jann Ben - ETH Zurich, Switzerland -, Univariate kernel density estimation document for KDENS:
18  Stata module for univariate kernel density estimation."
19  3. "Hardle W, Muller M, Sperlich S, Werwatz A, Nonparametric and Semiparametric Models. Springer."
20  4. "Cranmer KS, Kernel Estimation in High-Energy
21  Physics. Computer Physics Communications 136:198-207,2001" - e-Print Archive: hep ex/0011057.
22 
23  The algorithm is briefly described in (4). A binned version is also implemented to address the
24  performance issue due to its data size dependance.
25  */
26 
27 
28 #include <functional>
29 #include <algorithm>
30 #include <numeric>
31 #include <limits>
32 #include <cassert>
33 
34 #include "Math/Error.h"
35 #include "TMath.h"
36 #include "Math/Functor.h"
37 #include "Math/Integrator.h"
38 #include "Math/QuantFuncMathCore.h"
40 #include "TGraphErrors.h"
41 #include "TF1.h"
42 #include "TH1.h"
43 #include "TCanvas.h"
44 #include "TKDE.h"
45 
46 
48 
49 class TKDE::TKernel {
50  TKDE* fKDE;
51  UInt_t fNWeights; // Number of kernel weights (bandwidth as vectorized for binning)
52  std::vector<Double_t> fWeights; // Kernel weights (bandwidth)
53 public:
54  TKernel(Double_t weight, TKDE* kde);
55  void ComputeAdaptiveWeights();
57  Double_t GetWeight(Double_t x) const;
58  Double_t GetFixedWeight() const;
59  const std::vector<Double_t> & GetAdaptiveWeights() const;
60 };
61 
62 struct TKDE::KernelIntegrand {
63  enum EIntegralResult{kNorm, kMu, kSigma2, kUnitIntegration};
64  KernelIntegrand(const TKDE* kde, EIntegralResult intRes);
66 private:
67  const TKDE* fKDE;
68  EIntegralResult fIntegralResult;
69 };
70 
71 // TKDE::TKDE(UInt_t events, const Double_t* data, Double_t xMin, Double_t xMax, const Option_t* option, Double_t rho) :
72 // fData(events, 0.0),
73 // fEvents(events, 0.0),
74 // fPDF(0),
75 // fUpperPDF(0),
76 // fLowerPDF(0),
77 // fApproximateBias(0),
78 // fGraph(0),
79 // fNewData(false),
80 // fUseMinMaxFromData((xMin >= xMax)),
81 // fNBins(events < 10000 ? 100: events / 10),
82 // fNEvents(events),
83 // fUseBinsNEvents(10000),
84 // fMean(0.0),
85 // fSigma(0.0),
86 // fXMin(xMin),
87 // fXMax(xMax),
88 // fAdaptiveBandwidthFactor(1.0),
89 // fCanonicalBandwidths(std::vector<Double_t>(kTotalKernels, 0.0)),
90 // fKernelSigmas2(std::vector<Double_t>(kTotalKernels, -1.0)),
91 // fSettedOptions(std::vector<Bool_t>(4, kFALSE))
92 // {
93 // //Class constructor
94 // SetOptions(option, rho);
95 // CheckOptions();
96 // SetMirror();
97 // SetUseBins();
98 // SetKernelFunction();
99 // SetData(data);
100 // SetCanonicalBandwidths();
101 // SetKernelSigmas2();
102 // SetKernel();
103 // }
104 
106  //Class destructor
107  if (fPDF) delete fPDF;
108  if (fUpperPDF) delete fUpperPDF;
109  if (fLowerPDF) delete fLowerPDF;
110  if (fGraph) delete fGraph;
113  if (fKernel) delete fKernel;
114 }
115 
116 void TKDE::Instantiate(KernelFunction_Ptr kernfunc, UInt_t events, const Double_t* data, const Double_t* dataWeights, Double_t xMin, Double_t xMax, const Option_t* option, Double_t rho) {
117  // Template's constructor surrogate
118  fData = std::vector<Double_t>(events, 0.0);
119  fEvents = std::vector<Double_t>(events, 0.0);
120  fPDF = 0;
121  fKernel = 0;
122  fKernelFunction = 0;
123  fUpperPDF = 0;
124  fLowerPDF = 0;
125  fApproximateBias = 0;
126  fGraph = 0;
127  fNewData = false;
128  fUseMirroring = false; fMirrorLeft = false; fMirrorRight = false;
129  fAsymLeft = false; fAsymRight = false;
130  fNBins = events < 10000 ? 100 : events / 10;
131  fNEvents = events;
132  fUseBinsNEvents = 10000;
133  fMean = 0.0;
134  fSigma = 0.0;
135  fXMin = xMin;
136  fXMax = xMax;
138  fSumOfCounts = 0;
140  fRho = rho;
141  fWeightSize = 0;
142  fCanonicalBandwidths = std::vector<Double_t>(kTotalKernels, 0.0);
143  fKernelSigmas2 = std::vector<Double_t>(kTotalKernels, -1.0);
144  fSettedOptions = std::vector<Bool_t>(4, kFALSE);
145  SetOptions(option, rho);
147  SetMirror();
148  SetUseBins();
149  SetData(data, dataWeights);
150  SetKernelFunction(kernfunc);
151 }
152 
153 void TKDE::SetOptions(const Option_t* option, Double_t rho) {
154  //Sets User defined construction options
155  TString opt = option;
156  opt.ToLower();
157  std::string options = opt.Data();
158  size_t numOpt = 4;
159  std::vector<std::string> voption(numOpt, "");
160  for (std::vector<std::string>::iterator it = voption.begin(); it != voption.end() && !options.empty(); ++it) {
161  size_t pos = options.find_last_of(';');
162  if (pos == std::string::npos) {
163  *it = options;
164  break;
165  }
166  *it = options.substr(pos + 1);
167  options = options.substr(0, pos);
168  }
169  for (std::vector<std::string>::iterator it = voption.begin(); it != voption.end(); ++it) {
170  size_t pos = (*it).find(':');
171  if (pos != std::string::npos) {
172  GetOptions((*it).substr(0, pos) , (*it).substr(pos + 1));
173  }
174  }
175  AssureOptions();
176  fRho = rho;
177 }
178 
179 void TKDE::SetDrawOptions(const Option_t* option, TString& plotOpt, TString& drawOpt) {
180  // Sets User defined drawing options
181  size_t numOpt = 2;
182  std::string options = TString(option).Data();
183  std::vector<std::string> voption(numOpt, "");
184  for (std::vector<std::string>::iterator it = voption.begin(); it != voption.end() && !options.empty(); ++it) {
185  size_t pos = options.find_last_of(';');
186  if (pos == std::string::npos) {
187  *it = options;
188  break;
189  }
190  *it = options.substr(pos + 1);
191  options = options.substr(0, pos);
192  }
193  Bool_t foundPlotOPt = kFALSE;
194  Bool_t foundDrawOPt = kFALSE;
195  for (std::vector<std::string>::iterator it = voption.begin(); it != voption.end() && !options.empty(); ++it) {
196  size_t pos = (*it).find(':');
197  if (pos == std::string::npos) break;
198  TString optionType = (*it).substr(0, pos);
199  TString optionInstance = (*it).substr(pos + 1);
200  optionType.ToLower();
201  optionInstance.ToLower();
202  if (optionType.Contains("plot")) {
203  foundPlotOPt = kTRUE;
204  if (optionInstance.Contains("estimate") || optionInstance.Contains("errors") || optionInstance.Contains("confidenceinterval"))
205  plotOpt = optionInstance;
206  else
207  this->Warning("SetDrawOptions", "Unknown plotting option: setting to KDE estimate plot.");
208  } else if (optionType.Contains("drawoptions")) {
209  foundDrawOPt = kTRUE;
210  drawOpt = optionInstance;
211  }
212  }
213  if (!foundPlotOPt) {
214  this->Warning("SetDrawOptions", "No plotting option: setting to KDE estimate plot.");
215  plotOpt = "estimate";
216  }
217  if (!foundDrawOPt) {
218  this->Warning("SetDrawOptions", "No drawing options: setting to default ones.");
219  drawOpt = "apl4";
220  }
221 }
222 
223 void TKDE::GetOptions(std::string optionType, std::string option) {
224  // Gets User defined KDE construction options
225  if (optionType.compare("kerneltype") == 0) {
226  fSettedOptions[0] = kTRUE;
227  if (option.compare("gaussian") == 0) {
229  } else if (option.compare("epanechnikov") == 0) {
231  } else if (option.compare("biweight") == 0) {
233  } else if (option.compare("cosinearch") == 0) {
235  } else if (option.compare("userdefined") == 0) {
237  } else {
238  this->Warning("GetOptions", "Unknown kernel type option: setting to Gaussian");
240  }
241  } else if (optionType.compare("iteration") == 0) {
242  fSettedOptions[1] = kTRUE;
243  if (option.compare("adaptive") == 0) {
245  } else if (option.compare("fixed") == 0) {
246  fIteration = kFixed;
247  } else {
248  this->Warning("GetOptions", "Unknown iteration option: setting to Adaptive");
250  }
251  } else if (optionType.compare("mirror") == 0) {
252  fSettedOptions[2] = kTRUE;
253  if (option.compare("nomirror") == 0) {
254  fMirror = kNoMirror;
255  } else if (option.compare("mirrorleft") == 0) {
257  } else if (option.compare("mirrorright") == 0) {
259  } else if (option.compare("mirrorboth") == 0) {
261  } else if (option.compare("mirrorasymleft") == 0) {
263  } else if (option.compare("mirrorasymleftright") == 0) {
265  } else if (option.compare("mirrorasymright") == 0) {
267  } else if (option.compare("mirrorleftasymright") == 0) {
269  } else if (option.compare("mirrorasymboth") == 0) {
271  } else {
272  this->Warning("GetOptions", "Unknown mirror option: setting to NoMirror");
273  fMirror = kNoMirror;
274  }
275  } else if (optionType.compare("binning") == 0) {
276  fSettedOptions[3] = kTRUE;
277  if (option.compare("unbinned") == 0) {
279  } else if (option.compare("relaxedbinning") == 0) {
281  } else if (option.compare("forcedbinning") == 0) {
283  } else {
284  this->Warning("GetOptions", "Unknown binning option: setting to RelaxedBinning");
286  }
287  }
288 }
289 
291  // Sets missing construction options to default ones
292  if (!fSettedOptions[0]) {
294  }
295  if (!fSettedOptions[1]) {
297  }
298  if (!fSettedOptions[2]) {
299  fMirror = kNoMirror;
300  }
301  if (!fSettedOptions[3]) {
303  }
304 }
305 
306 void TKDE::CheckOptions(Bool_t isUserDefinedKernel) {
307  // Sets User global options
308  if (!(isUserDefinedKernel) && !(fKernelType >= kGaussian && fKernelType < kUserDefined)) {
309  Error("CheckOptions", "Illegal user kernel type input! Use template constructor for user defined kernel.");
310  }
311  if (fIteration != kAdaptive && fIteration != kFixed) {
312  Warning("CheckOptions", "Illegal user iteration type input - use default value !");
314  }
315  if (!(fMirror >= kNoMirror && fMirror <= kMirrorAsymBoth)) {
316  Warning("CheckOptions", "Illegal user mirroring type input - use default value !");
317  fMirror = kNoMirror;
318  }
319  if (!(fBinning >= kUnbinned && fBinning <= kForcedBinning)) {
320  Warning("CheckOptions", "Illegal user binning type input - use default value !");
322  }
323  if (fRho <= 0.0) {
324  Warning("CheckOptions", "Tuning factor rho cannot be non-positive - use default value !");
325  fRho = 1.0;
326  }
327 }
328 
330  // Sets User option for the choice of kernel estimator
332  delete fKernelFunction;
333  fKernelFunction = 0;
334  }
335  fKernelType = kern;
336  CheckOptions();
338 }
339 
341  // Sets User option for fixed or adaptive iteration
342  fIteration = iter;
343  CheckOptions();
344  SetKernel();
345 }
346 
347 
349  // Sets User option for mirroring the data
350  fMirror = mir;
351  CheckOptions();
352  SetMirror();
353  if (fUseMirroring) {
355  }
356  SetKernel();
357 }
358 
359 
361  // Sets User option for binning the weights
362  fBinning = bin;
363  CheckOptions();
364  SetUseBins();
365  SetKernel();
366 }
367 
369  // Sets User option for number of bins
370  if (!nbins) {
371  Error("SetNBins", "Number of bins must be greater than zero.");
372  return;
373  }
374  fNBins = nbins;
375  fWeightSize = fNBins / (fXMax - fXMin);
377  SetBinCountData();
378 
379  if (fBinning == kUnbinned) {
380  Warning("SetNBins", "Bin type using SetBinning must set for using a binned evaluation");
381  return;
382  }
383 
384  SetKernel();
385 }
386 
388  // Sets User option for the minimum number of events for allowing automatic binning
390  SetUseBins();
391  SetKernel();
392 }
393 
395  // Factor which can be used to tune the smoothing.
396  // It is used as multiplicative factor for the fixed and adaptive bandwidth.
397  // A value < 1 will reproduce better the tails but oversmooth the peak
398  // while a factor > 1 will overestimate the tail
399  fRho = rho;
400  CheckOptions();
401  SetKernel();
402 }
403 
404 void TKDE::SetRange(Double_t xMin, Double_t xMax) {
405  // Sets minimum range value and maximum range value
406  if (xMin >= xMax) {
407  Error("SetRange", "Minimum range cannot be bigger or equal than the maximum range! Present range values remain the same.");
408  return;
409  }
410  fXMin = xMin;
411  fXMax = xMax;
412  fUseMinMaxFromData = false;
413  SetKernel();
414 }
415 
416 // private methods
417 
419  // Sets User option for using binned weights
420  switch (fBinning) {
421  default:
422  case kRelaxedBinning:
423  if (fNEvents >= fUseBinsNEvents) {
424  fUseBins = kTRUE;
425  } else {
426  fUseBins = kFALSE;
427  }
428  break;
429  case kForcedBinning:
430  fUseBins = kTRUE;
431  break;
432  case kUnbinned:
433  fUseBins = kFALSE;
434  }
435 }
436 
438  // Sets the mirroring
444 }
445 
446 void TKDE::SetData(const Double_t* data, const Double_t* wgts) {
447  // Sets the data events input sample or bin centres for binned option and computes basic estimators
448  if (!data) {
449  if (fNEvents) fData.reserve(fNEvents);
450  return;
451  }
452  fEvents.assign(data, data + fNEvents);
453  if (wgts) fEventWeights.assign(wgts, wgts + fNEvents);
454 
455  if (fUseMinMaxFromData) {
456  fXMin = *std::min_element(fEvents.begin(), fEvents.end());
457  fXMax = *std::max_element(fEvents.begin(), fEvents.end());
458  }
459 
460  if (fUseBins) {
461  if (fNBins >= fNEvents) {
462  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");
463  }
464  fWeightSize = fNBins / (fXMax - fXMin);
466  } else {
468  fData = fEvents;
469  }
470  // to set fBinCOunt and fSumOfCounts
471  SetBinCountData();
472 
473 
474  ComputeDataStats();
475  if (fUseMirroring) {
477  }
478 }
479 
481  // re-initialize when new data have been filled in TKDE
482  // re-compute kernel quantities and mean and sigma
483  fNewData = false;
484  fEvents = fData;
485  if (fUseMinMaxFromData) {
486  fXMin = *std::min_element(fEvents.begin(), fEvents.end());
487  fXMax = *std::max_element(fEvents.begin(), fEvents.end());
488  }
489  ComputeDataStats();
490  // if (fUseBins) {
491  // } // bin usage is not supported in this case
492  //
494  if (fUseMirroring) {
496  }
497  SetKernel();
498 }
499 
501  // Mirrors the data
502  std::vector<Double_t> originalEvents = fEvents;
503  std::vector<Double_t> originalWeights = fEventWeights;
504  if (fMirrorLeft) {
505  fEvents.resize(2 * fNEvents, 0.0);
506  transform(fEvents.begin(), fEvents.begin() + fNEvents, fEvents.begin() + fNEvents, std::bind1st(std::minus<Double_t>(), 2 * fXMin));
507  }
508  if (fMirrorRight) {
509  fEvents.resize((fMirrorLeft + 2) * fNEvents, 0.0);
510  transform(fEvents.begin(), fEvents.begin() + fNEvents, fEvents.begin() + (fMirrorLeft + 1) * fNEvents, std::bind1st(std::minus<Double_t>(), 2 * fXMax));
511  }
512  if (!fEventWeights.empty() && (fMirrorLeft || fMirrorRight)) {
513  // copy weights too
514  fEventWeights.insert(fEventWeights.end(), fEventWeights.begin(), fEventWeights.end() );
515  }
516 
517  if(fUseBins) {
518  fNBins *= (fMirrorLeft + fMirrorRight + 1);
521  SetBinCentreData(xmin, xmax);
522  } else {
523  fData = fEvents;
524  }
525  SetBinCountData();
526 
527  fEvents = originalEvents;
528  fEventWeights = originalWeights;
529 }
530 
532  // Computes input data's mean
533  fMean = std::accumulate(fEvents.begin(), fEvents.end(), 0.0) / fEvents.size();
534 }
535 
537  // Computes input data's sigma
538  fSigma = std::sqrt(1. / (fEvents.size() - 1.) * (std::inner_product(fEvents.begin(), fEvents.end(), fEvents.begin(), 0.0) - fEvents.size() * std::pow(fMean, 2.)));
539  fSigmaRob = std::min(fSigma, R / 1.349); // Sigma's robust estimator
540 }
541 
543  // Sets the kernel density estimator
544  UInt_t n = fData.size();
545  if (n == 0) return;
546  // Optimal bandwidth (Silverman's rule of thumb with assumed Gaussian density)
547  Double_t weight = fCanonicalBandwidths[kGaussian] * fSigmaRob * std::pow(3. / (8. * std::sqrt(M_PI)) * n, -0.2);
549  if (fKernel) delete fKernel;
550  fKernel = new TKernel(weight, this);
551  if (fIteration == kAdaptive) {
552  fKernel->ComputeAdaptiveWeights();
553  }
554 }
555 
557 
558  assert(fKernelFunction == 0); // to avoid memory leaks
559  switch (fKernelType) {
560  case kGaussian :
562  break;
563  case kEpanechnikov :
565  break;
566  case kBiweight :
568  break;
569  case kCosineArch :
571  break;
572  case kUserDefined :
573  fKernelFunction = kernfunc;
575  break;
576  case kTotalKernels :
577  default:
578  /// for user defined kernels
579  fKernelFunction = kernfunc;
581  }
582 
583  if (fKernelType == kUserDefined) {
584  if (fKernelFunction) {
588  }
589  else {
590  Error("SetKernelFunction", "User kernel function is not defined !");
591  return;
592  }
593  }
594  assert(fKernelFunction);
597  SetKernel();
598 }
599 
601  // Sets the canonical bandwidths according to the kernel type
602  fCanonicalBandwidths[kGaussian] = 0.7764; // Checked in Mathematica
603  fCanonicalBandwidths[kEpanechnikov] = 1.7188; // Checked in Mathematica
604  fCanonicalBandwidths[kBiweight] = 2.03617; // Checked in Mathematica
605  fCanonicalBandwidths[kCosineArch] = 1.7663; // Checked in Mathematica
606  fCanonicalBandwidths[kUserDefined] = 1.0; // To be Checked
607 }
608 
610  // Sets the kernel sigmas2 according to the kernel type
611  fKernelSigmas2[kGaussian] = 1.0;
612  fKernelSigmas2[kEpanechnikov] = 1.0 / 5.0;
613  fKernelSigmas2[kBiweight] = 1.0 / 7.0;
614  fKernelSigmas2[kCosineArch] = 1.0 - 8.0 / std::pow(M_PI, 2);
615 }
616 
618  // Returns the PDF estimate as a function sampled in npx between xMin and xMax
619  // the KDE is not re-normalized to the xMin/xMax range.
620  // The user manages the returned function
621  // For getting a non-sampled TF1, one can create a TF1 directly from the TKDE by doing
622  // TF1 * f1 = new TF1("f1",kde,xMin,xMax,0);
623  return GetKDEFunction(npx,xMin,xMax);
624 }
625 
626 TF1* TKDE::GetUpperFunction(Double_t confidenceLevel, UInt_t npx, Double_t xMin, Double_t xMax) {
627  // Returns the PDF upper estimate (upper confidence interval limit)
628  return GetPDFUpperConfidenceInterval(confidenceLevel,npx,xMin,xMax);
629 }
630 
631 TF1* TKDE::GetLowerFunction(Double_t confidenceLevel, UInt_t npx, Double_t xMin, Double_t xMax) {
632  // Returns the PDF lower estimate (lower confidence interval limit)
633  return GetPDFLowerConfidenceInterval(confidenceLevel,npx,xMin,xMax);
634 }
635 
637  // Returns the PDF estimate bias
638  return GetKDEApproximateBias(npx,xMin,xMax);
639 }
640 
642  // Fills data member with User input data event for the unbinned option
643  if (fUseBins) {
644  this->Warning("Fill", "Cannot fill data with data binned option. Data input ignored.");
645  return;
646  }
647  fData.push_back(data);
648  fNEvents++;
649  fNewData = kTRUE;
650 }
651 
653  // Fills data member with User input data event for the unbinned option
654  if (fUseBins) {
655  this->Warning("Fill", "Cannot fill data with data binned option. Data input ignored.");
656  return;
657  }
658  fData.push_back(data); // should not be here fEvent ??
659  fEventWeights.push_back(weight);
660  fNEvents++;
661  fNewData = kTRUE;
662 }
663 
664 Double_t TKDE::operator()(const Double_t* x, const Double_t*) const {
665  // The class's unary function: returns the kernel density estimate
666  return (*this)(*x);
667 }
668 
670  // The class's unary function: returns the kernel density estimate
671  if (fNewData) (const_cast<TKDE*>(this))->InitFromNewData();
672  return (*fKernel)(x);
673 }
674 
676  // return the mean of the data
677  if (fNewData) (const_cast<TKDE*>(this))->InitFromNewData();
678  return fMean;
679 }
680 
682  // return the standard deviation of the data
683  if (fNewData) (const_cast<TKDE*>(this))->InitFromNewData();
684  return fSigma;
685 }
686 
688  // Returns the Root Asymptotic Mean Integrated Squared Error according to Silverman's rule of thumb with assumed Gaussian density
690  return std::sqrt(result);
691 }
692 
693 TKDE::TKernel::TKernel(Double_t weight, TKDE* kde) :
694 // Internal class constructor
695 fKDE(kde),
696 fNWeights(kde->fData.size()),
697 fWeights(fNWeights, weight)
698 {}
699 
700 void TKDE::TKernel::ComputeAdaptiveWeights() {
701  // Gets the adaptive weights (bandwidths) for TKernel internal computation
702  std::vector<Double_t> weights = fWeights;
703  Double_t minWeight = weights[0] * 0.05;
704  unsigned int n = fKDE->fData.size();
705  assert( n == weights.size() );
706  bool useDataWeights = (fKDE->fBinCount.size() == n);
707  Double_t f = 0.0;
708  for (unsigned int i = 0; i < n; ++i) {
709 // for (; weight != weights.end(); ++weight, ++data, ++dataW) {
710  if (useDataWeights && fKDE->fBinCount[i] <= 0) continue; // skip negative or null weights
711  f = (*fKDE->fKernel)(fKDE->fData[i]);
712  if (f <= 0)
713  fKDE->Warning("ComputeAdativeWeights","function value is zero or negative for x = %f w = %f",
714  fKDE->fData[i],(useDataWeights) ? fKDE->fBinCount[i] : 1.);
715  weights[i] = std::max(weights[i] /= std::sqrt(f), minWeight);
716  fKDE->fAdaptiveBandwidthFactor += std::log(f);
717  //printf("(f = %f w = %f af = %f ),",f,*weight,fKDE->fAdaptiveBandwidthFactor);
718  }
719  Double_t kAPPROX_GEO_MEAN = 0.241970724519143365; // 1 / TMath::Power(2 * TMath::Pi(), .5) * TMath::Exp(-.5). Approximated geometric mean over pointwise data (the KDE function is substituted by the "real Gaussian" pdf) and proportional to sigma. Used directly when the mirroring is enabled, otherwise computed from the data
720  fKDE->fAdaptiveBandwidthFactor = fKDE->fUseMirroring ? kAPPROX_GEO_MEAN / fKDE->fSigmaRob : std::sqrt(std::exp(fKDE->fAdaptiveBandwidthFactor / fKDE->fData.size()));
721  transform(weights.begin(), weights.end(), fWeights.begin(), std::bind2nd(std::multiplies<Double_t>(), fKDE->fAdaptiveBandwidthFactor));
722  //printf("adaptive bandwidth factor % f weight 0 %f , %f \n",fKDE->fAdaptiveBandwidthFactor, weights[0],fWeights[0] );
723 }
724 
725 Double_t TKDE::TKernel::GetWeight(Double_t x) const {
726  // Returns the bandwidth
727  return fWeights[fKDE->Index(x)];
728 }
729 
731  // Returns the bins' centres from the data for using with the binned option
732  fData.assign(fNBins, 0.0);
733  Double_t binWidth((xmax - xmin) / fNBins);
734  for (UInt_t i = 0; i < fNBins; ++i) {
735  fData[i] = xmin + (i + 0.5) * binWidth;
736  }
737 }
738 
740  // Returns the bins' count from the data for using with the binned option
741  // or set the bin count to the weights in case of weighted data
742  if (fUseBins) {
743  fBinCount.resize(fNBins);
744  fSumOfCounts = 0;
745  // case of weighted events
746  if (!fEventWeights.empty() ) {
747  for (UInt_t i = 0; i < fNEvents; ++i) {
748  if (fEvents[i] >= fXMin && fEvents[i] < fXMax) {
751  //printf("sum of counts %f - bin count %d - %f \n",fSumOfCounts, Index(fEvents[i]), fBinCount[Index(fEvents[i])] );
752  }
753  }
754  }
755  // case of unweighted data
756  else {
757  for (UInt_t i = 0; i < fNEvents; ++i) {
758  if (fEvents[i] >= fXMin && fEvents[i] < fXMax) {
759  fBinCount[Index(fEvents[i])] += 1;
760  fSumOfCounts += 1;
761  }
762  }
763  }
764  }
765  else if (!fEventWeights.empty() ) {
767  fSumOfCounts = 0;
768  for (UInt_t i = 0; i < fNEvents; ++i)
770  }
771  else {
773  fBinCount.clear();
774  }
775 }
776 
777 void TKDE::Draw(const Option_t* opt) {
778  // Draws either the KDE functions or its errors
779  // Possible options:
780  // "" (default) - draw just the kde
781  // "same" draw on top of existing pad
782  // "Errors" draw a TGraphErrors with the point and errors
783  // "confidenceinterval" draw KDE + conf interval functions (default is 95%)
784  // "confidenceinterval@0.90" draw KDE + conf interval functions at 90%
785  // Extra options can be passed in opt for drawing the TF1 or the TGraph
786  //
787  //NOTE: The functions GetDrawnFunction(), GetDrawnUpperFunction(), GetDrawnLowerFunction()
788  // and GetGraphWithErrors() return the corresponding drawn objects (which are maneged by the TKDE)
789  // They can be used to changes style, color, etc...
790 
791  // TString plotOpt = "";
792  // TString drawOpt = "";
793  // LM : this is too complicates - skip it - not needed for just
794  // three options
795  // SetDrawOptions(opt, plotOpt, drawOpt);
796  TString plotOpt = opt;
797  plotOpt.ToLower();
798  TString drawOpt = plotOpt;
799  if(gPad && !plotOpt.Contains("same")) {
800  gPad->Clear();
801  }
802  if (plotOpt.Contains("errors")) {
803  drawOpt.ReplaceAll("errors","");
804  DrawErrors(drawOpt);
805  }
806  else if (plotOpt.Contains("confidenceinterval") ||
807  plotOpt.Contains("confinterval")) {
808  // parse level option
809  drawOpt.ReplaceAll("confidenceinterval","");
810  drawOpt.ReplaceAll("confinterval","");
811  Double_t level = 0.95;
812  const char * s = strstr(plotOpt.Data(),"interval@");
813  // coverity [secure_coding : FALSE]
814  if (s != 0) sscanf(s,"interval@%lf",&level);
815  if((level <= 0) || (level >= 1)) {
816  Warning("Draw","given confidence level %.3lf is invalid - use default 0.95",level);
817  level = 0.95;
818  }
819  DrawConfidenceInterval(drawOpt,level);
820  }
821  else {
822  if (fPDF) delete fPDF;
823  fPDF = GetKDEFunction();
824  fPDF->Draw(drawOpt);
825  }
826 }
827 
828 void TKDE::DrawErrors(TString& drawOpt) {
829  // Draws a TGraphErrors for the KDE errors
830  if (fGraph) delete fGraph;
832  fGraph->Draw(drawOpt.Data());
833 }
834 
836  if (xmin>= xmax) { xmin = fXMin; xmax = fXMax; }
837  // return a TGraphErrors for the KDE errors
838  UInt_t n = npx;
839  Double_t* x = new Double_t[n + 1];
840  Double_t* ex = new Double_t[n + 1];
841  Double_t* y = new Double_t[n + 1];
842  Double_t* ey = new Double_t[n + 1];
843  for (UInt_t i = 0; i <= n; ++i) {
844  x[i] = xmin + i * (xmax - xmin) / n;
845  y[i] = (*this)(x[i]);
846  ex[i] = 0;
847  ey[i] = this->GetError(x[i]);
848  }
849  TGraphErrors* ge = new TGraphErrors(n, &x[0], &y[0], &ex[0], &ey[0]);
850  ge->SetName("kde_graph_error");
851  ge->SetTitle("Errors");
852  delete [] x;
853  delete [] ex;
854  delete [] y;
855  delete [] ey;
856  return ge;
857 }
858 
859 void TKDE::DrawConfidenceInterval(TString& drawOpt,double cl) {
860  // Draws the KDE and its confidence interval
861  GetKDEFunction()->Draw(drawOpt.Data());
862  TF1* upper = GetPDFUpperConfidenceInterval(cl);
863  upper->SetLineColor(kBlue);
864  upper->Draw(("same" + drawOpt).Data());
865  TF1* lower = GetPDFLowerConfidenceInterval(cl);
866  lower->SetLineColor(kRed);
867  lower->Draw(("same" + drawOpt).Data());
868  if (fUpperPDF) delete fUpperPDF;
869  if (fLowerPDF) delete fLowerPDF;
870  fUpperPDF = upper;
871  fLowerPDF = lower;
872 }
873 
875  // Returns the bandwidth for the non adaptive KDE
876  Double_t result = -1.;
877  if (fIteration == TKDE::kAdaptive) {
878  this->Warning("GetFixedWeight()", "Fixed iteration option not enabled. Returning %f.", result);
879  } else {
880  result = fKernel->GetFixedWeight();
881  }
882  return result;
883 }
884 
886  // Returns the bandwidths for the adaptive KDE
887  if (fIteration != TKDE::kAdaptive) {
888  this->Warning("GetFixedWeight()", "Adaptive iteration option not enabled. Returning a NULL pointer<");
889  return 0;
890  }
891  if (fNewData) (const_cast<TKDE*>(this))->InitFromNewData();
892  return &(fKernel->GetAdaptiveWeights()).front();
893 }
894 
895 Double_t TKDE::TKernel::GetFixedWeight() const {
896  // Returns the bandwidth for the non adaptive KDE
897  return fWeights[0];
898 }
899 
900 const std::vector<Double_t> & TKDE::TKernel::GetAdaptiveWeights() const {
901  // Returns the bandwidth for the non adaptive KDE
902  return fWeights;
903 }
904 
906  // The internal class's unary function: returns the kernel density estimate
907  Double_t result(0.0);
908  UInt_t n = fKDE->fData.size();
909  // case of bins or weighted data
910  Bool_t useBins = (fKDE->fBinCount.size() == n);
911  Double_t nSum = (useBins) ? fKDE->fSumOfCounts : fKDE->fNEvents;
912  // double dmin = 1.E10;
913  // double xmin,bmin,wmin;
914  for (UInt_t i = 0; i < n; ++i) {
915  Double_t binCount = (useBins) ? fKDE->fBinCount[i] : 1.0;
916  //printf("data point %i %f count %f weight % f result % f\n",i,fKDE->fData[i],binCount,fWeights[i], result);
917  result += binCount / fWeights[i] * (*fKDE->fKernelFunction)((x - fKDE->fData[i]) / fWeights[i]);
918  if (fKDE->fAsymLeft) {
919  result -= binCount / fWeights[i] * (*fKDE->fKernelFunction)((x - (2. * fKDE->fXMin - fKDE->fData[i])) / fWeights[i]);
920  }
921  if (fKDE->fAsymRight) {
922  result -= binCount / fWeights[i] * (*fKDE->fKernelFunction)((x - (2. * fKDE->fXMax - fKDE->fData[i])) / fWeights[i]);
923  }
924  // if ( TMath::IsNaN(result) ) {
925  // printf("event %i count %f weight %f data % f x %f \n",i,binCount,fWeights[i],fKDE->fData[i],x );
926  // }
927  // if ( result <= 0 ) {
928  // printf("event %i count %f weight %f data % f x %f \n",i,binCount,fWeights[i],fKDE->fData[i],x );
929  // }
930  // if (std::abs(x - fKDE->fData[i]) < dmin ) {
931  // xmin = x;
932  // bmin = binCount;
933  // wmin = fWeights[i];
934  // dmin = std::abs(x - fKDE->fData[i]);
935  // }
936  }
937  if ( TMath::IsNaN(result) ) {
938  fKDE->Warning("operator()","Result is NaN for x %f \n",x);
939  //xmin % f , %f, %f \n",result,x,xmin,bmin,wmin );
940  }
941  return result / nSum;
942 }
943 
945  // Returns the indices (bins) for the binned weights
946  Int_t bin = Int_t((x - fXMin) * fWeightSize);
947  if (bin == (Int_t)fData.size()) return --bin;
948  if (fUseMirroring && (fMirrorLeft || !fMirrorRight)) {
949  bin += fData.size() / (fMirrorLeft + fMirrorRight + 1);
950  }
951  if (bin > (Int_t)fData.size()) {
952  return (Int_t)(fData.size()) - 1;
953  } else if (bin <= 0) {
954  return 0;
955  }
956  return bin;
957 }
958 
960  // Returns the pointwise upper estimated density
961  Double_t f = (*this)(x);
962  Double_t sigma = GetError(*x);
963  Double_t prob = 1. - (1.-*p)/2; // this is 1.-alpha/2
965  return f + z * sigma;
966 }
967 
969  // Returns the pointwise lower estimated density
970  Double_t f = (*this)(x);
971  Double_t sigma = GetError(*x);
972  Double_t prob = (1.-*p)/2; // this is alpha/2
974  return f + z * sigma;
975 }
976 
977 
979  // Returns the pointwise approximate estimated density bias
982  rd.SetFunction(kern);
983  Double_t df2 = rd.Derivative2(x);
984  Double_t weight = fKernel->GetWeight(x); // Bandwidth
985  return 0.5 * fKernelSigmas2[fKernelType] * std::pow(weight, 2) * df2;
986 }
988  // Returns the pointwise sigma of estimated density
989  Double_t kernelL2Norm = ComputeKernelL2Norm();
990  Double_t f = (*this)(x);
991  Double_t weight = fKernel->GetWeight(x); // Bandwidth
992  Double_t result = f * kernelL2Norm / (fNEvents * weight);
993  return std::sqrt(result);
994 }
995 
997  // Checks if kernel has unit integral, mu = 0 and positive finite sigma conditions
998  Double_t valid = kTRUE;
1000  valid = valid && unity == 1.;
1001  if (!valid) {
1002  Error("CheckKernelValidity", "Kernel's integral is %f",unity);
1003  }
1004  Double_t mu = ComputeKernelMu();
1005  valid = valid && mu == 0.;
1006  if (!valid) {
1007  Error("CheckKernelValidity", "Kernel's mu is %f" ,mu);
1008  }
1009  Double_t sigma2 = ComputeKernelSigma2();
1010  valid = valid && sigma2 > 0 && sigma2 != std::numeric_limits<Double_t>::infinity();
1011  if (!valid) {
1012  Error("CheckKernelValidity", "Kernel's sigma2 is %f",sigma2);
1013  }
1014  if (!valid) {
1015  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.");
1016  //exit(EXIT_FAILURE);
1017  }
1018 }
1019 
1021  // Computes the kernel's L2 norm
1024  ig.SetFunction(kernel);
1025  Double_t result = ig.Integral();
1026  return result;
1027 }
1028 
1030  // Computes the kernel's sigma squared
1032  KernelIntegrand kernel(this, TKDE::KernelIntegrand::kSigma2);
1033  ig.SetFunction(kernel);
1034  Double_t result = ig.Integral();
1035  return result;
1036 }
1037 
1039  // Computes the kernel's mu
1041  KernelIntegrand kernel(this, TKDE::KernelIntegrand::kMu);
1042  ig.SetFunction(kernel);
1043  Double_t result = ig.Integral();
1044  return result;
1045 }
1046 
1048  // Computes the kernel's integral which ought to be unity
1050  KernelIntegrand kernel(this, TKDE::KernelIntegrand::kUnitIntegration);
1051  ig.SetFunction(kernel);
1052  Double_t result = ig.Integral();
1053  return result;
1054 }
1055 
1057  /// in case of weights use
1058  if (!fEventWeights.empty() ) {
1059  // weighted data
1060  double x1 = fXMin - 0.001*(fXMax-fXMin);
1061  double x2 = fXMax + 0.001*(fXMax-fXMin);
1062  TH1D h1("temphist","", 500, x1, x2);
1063  h1.FillN(fEvents.size(), fEvents.data(), fEventWeights.data() );
1064  assert (h1.GetSumOfWeights() > 0) ;
1065  fMean = h1.GetMean();
1066  fSigma = h1.GetRMS();
1067  // compute robust sigma using midspread
1068  Double_t quantiles[2] = {0.0, 0.0};
1069  Double_t prob[2] = {0.25, 0.75};
1070  h1.GetQuantiles(2, quantiles, prob);
1071  Double_t midspread = quantiles[1] - quantiles[0];
1072  fSigmaRob = std::min(fSigma, midspread / 1.349); // Sigma's robust estimator
1073  //printf("weight case - stat: m = %f, s= %f, sr = %f \n",fMean, fSigma, midspread);
1074  return;
1075  }
1076  else {
1077  // compute statistics using the data
1078  SetMean();
1079  Double_t midspread = ComputeMidspread();
1080  SetSigma(midspread);
1081  //printf("un-weight case - stat: m = %f, s= %f, sr = %f \n",fMean, fSigma, fSigmaRob);
1082  }
1083 }
1084 
1086  // Computes the inter-quartile range from the data
1087  std::sort(fEvents.begin(), fEvents.end());
1088  Double_t quantiles[2] = {0.0, 0.0};
1089  Double_t prob[2] = {0.25, 0.75};
1090  TMath::Quantiles(fEvents.size(), 2, &fEvents[0], quantiles, prob);
1091  Double_t lowerquartile = quantiles[0];
1092  Double_t upperquartile = quantiles[1];
1093  return upperquartile - lowerquartile;
1094 }
1095 
1097  // Computes the user's input kernel function canonical bandwidth
1099 }
1100 
1102  // Computes the user's input kernel function sigma2
1104 }
1105 
1106 TKDE::KernelIntegrand::KernelIntegrand(const TKDE* kde, EIntegralResult intRes) : fKDE(kde), fIntegralResult(intRes) {
1107  // Internal class constructor
1108 }
1109 
1111  // Internal class unary function
1112  if (fIntegralResult == kNorm) {
1113  return std::pow((*fKDE->fKernelFunction)(x), 2);
1114  } else if (fIntegralResult == kMu) {
1115  return x * (*fKDE->fKernelFunction)(x);
1116  } else if (fIntegralResult == kSigma2) {
1117  return std::pow(x, 2) * (*fKDE->fKernelFunction)(x);
1118  } else if (fIntegralResult == kUnitIntegration) {
1119  return (*fKDE->fKernelFunction)(x);
1120  } else {
1121  return -1;
1122  }
1123 }
1124 
1126  //Returns the estimated density
1127  TString name = "KDEFunc_"; name+= GetName();
1128  TString title = "KDE "; title+= GetTitle();
1129  if (xMin >= xMax) { xMin = fXMin; xMax = fXMax; }
1130  //Do not register the TF1 to global list
1131  bool previous = TF1::DefaultAddToGlobalList(kFALSE);
1132  TF1 * pdf = new TF1(name.Data(), this, xMin, xMax, 0);
1133  TF1::DefaultAddToGlobalList(previous);
1134  if (npx > 0) pdf->SetNpx(npx);
1135  pdf->SetTitle(title);
1136  return pdf;
1137 }
1138 
1140  // Returns the upper estimated density
1141  TString name;
1142  name.Form("KDE_UpperCL%f5.3_%s",confidenceLevel,GetName());
1143  if (xMin >= xMax) { xMin = fXMin; xMax = fXMax; }
1144  TF1 * upperPDF = new TF1(name, this, &TKDE::UpperConfidenceInterval, xMin, xMax, 1);
1145  upperPDF->SetParameter(0, confidenceLevel);
1146  if (npx > 0) upperPDF->SetNpx(npx);
1147  TF1 * f = (TF1*)upperPDF->Clone();
1148  delete upperPDF;
1149  return f;
1150 }
1151 
1153  // Returns the upper estimated density
1154  TString name;
1155  name.Form("KDE_LowerCL%f5.3_%s",confidenceLevel,GetName());
1156  if (xMin >= xMax) { xMin = fXMin; xMax = fXMax; }
1157  TF1 * lowerPDF = new TF1(name, this, &TKDE::LowerConfidenceInterval, xMin, xMax, 1);
1158  lowerPDF->SetParameter(0, confidenceLevel);
1159  if (npx > 0) lowerPDF->SetNpx(npx);
1160  TF1 * f = (TF1*)lowerPDF->Clone();
1161  delete lowerPDF;
1162  return f;
1163 }
1164 
1166  // Returns the approximate bias
1167  TString name = "KDE_Bias_"; name += GetName();
1168  if (xMin >= xMax) { xMin = fXMin; xMax = fXMax; }
1169  TF1 * approximateBias = new TF1(name, this, &TKDE::ApproximateBias, xMin, xMax, 0);
1170  if (npx > 0) approximateBias->SetNpx(npx);
1171  TF1 * f = (TF1*)approximateBias->Clone();
1172  delete approximateBias;
1173  return f;
1174 }
Double_t EpanechnikovKernel(Double_t x) const
Definition: TKDE.h:195
Double_t ComputeKernelSigma2() const
Definition: TKDE.cxx:1029
double normal_quantile(double z, double sigma)
Inverse ( ) of the cumulative distribution function of the lower tail of the normal (Gaussian) distri...
for(Int_t i=0;i< n;i++)
Definition: legend1.C:18
Double_t LowerConfidenceInterval(const Double_t *x, const Double_t *p) const
Definition: TKDE.cxx:968
void SetMean()
Definition: TKDE.cxx:531
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Bool_t fUseMinMaxFromData
Definition: TKDE.h:160
Bool_t fUseMirroring
Definition: TKDE.h:157
float xmin
Definition: THbookFile.cxx:93
void SetUserKernelSigma2()
Definition: TKDE.cxx:1101
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:134
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3202
TF1 * GetFunction(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:617
friend struct KernelIntegrand
Definition: TKDE.h:184
Kernel Density Estimation class.
Definition: TKDE.h:31
void SetRange(Double_t xMin, Double_t xMax)
Definition: TKDE.cxx:404
void SetKernel()
Definition: TKDE.cxx:542
const Double_t * GetAdaptiveWeights() const
Definition: TKDE.cxx:885
Bool_t fAsymRight
Definition: TKDE.h:157
EKernelType
Definition: TKDE.h:34
const char Option_t
Definition: RtypesCore.h:62
TF1 * GetLowerFunction(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:631
Definition: Rtypes.h:56
void SetKernelSigmas2()
Definition: TKDE.cxx:609
void InitFromNewData()
Definition: TKDE.cxx:480
void GetOptions(std::string optionType, std::string option)
Definition: TKDE.cxx:223
Double_t ApproximateBias(const Double_t *x, const Double_t *) const
Definition: TKDE.h:208
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition: TH1.cxx:7103
UInt_t fNEvents
Definition: TKDE.h:163
virtual Int_t GetQuantiles(Int_t nprobSum, Double_t *q, const Double_t *probSum=0)
Compute Quantiles for this histogram Quantile x_q of a probability distribution Function F is defined...
Definition: TH1.cxx:4191
void SetIteration(EIteration iter)
Definition: TKDE.cxx:340
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.
Definition: TH1.cxx:6763
double inner_product(const LAVector &, const LAVector &)
Basic string class.
Definition: TString.h:129
static Bool_t DefaultAddToGlobalList(Bool_t on=kTRUE)
Static method to add/avoid to add automatically functions to the global list (gROOT->GetListOfFunctio...
Definition: TF1.cxx:657
EMirror fMirror
Definition: TKDE.h:154
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1099
void ComputeDataStats()
Definition: TKDE.cxx:1056
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:296
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF1.cxx:1087
std::vector< Double_t > fKernelSigmas2
Definition: TKDE.h:178
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2180
int nbins[3]
Bool_t fMirrorRight
Definition: TKDE.h:157
double Derivative2(double x)
Returns the second derivative of the function at point x, computed by Richardson&#39;s extrapolation meth...
virtual void Draw(const Option_t *option="")
Definition: TKDE.cxx:777
TF1 * fPDF
Definition: TKDE.h:146
Double_t BiweightKernel(Double_t x) const
Definition: TKDE.h:198
Double_t fRho
Definition: TKDE.h:172
TRObject operator()(const T1 &t1) const
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
Double_t GetFixedWeight() const
Definition: TKDE.cxx:874
TF1 * GetPDFLowerConfidenceInterval(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:1152
void SetKernelType(EKernelType kern)
Definition: TKDE.cxx:329
std::vector< Double_t > fCanonicalBandwidths
Definition: TKDE.h:177
Double_t operator()(Double_t x) const
Definition: TKDE.cxx:669
Template class to wrap any C++ callable object which takes one argument i.e.
Double_t GetError(Double_t x) const
Definition: TKDE.cxx:987
TF1 * GetApproximateBias(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:636
void SetCanonicalBandwidths()
Definition: TKDE.cxx:600
double sqrt(double)
TF1 * fUpperPDF
Definition: TKDE.h:147
UInt_t Index(Double_t x) const
Definition: TKDE.cxx:944
static const double x2[5]
Double_t x[n]
Definition: legend1.C:17
TGraphErrors * GetGraphWithErrors(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:835
std::vector< Double_t > fBinCount
Definition: TKDE.h:180
void SetFunction(const IGenFunction &f)
Set function for derivative calculation (copy the function if option has been enabled in the construc...
TF1 * fApproximateBias
Definition: TKDE.h:149
TF1 * GetUpperFunction(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:626
void Quantiles(Int_t n, Int_t nprob, Double_t *x, Double_t *quantiles, Double_t *prob, Bool_t isSorted=kTRUE, Int_t *index=0, Int_t type=7)
Computes sample quantiles, corresponding to the given probabilities Parameters: x -the data sample n ...
Definition: TMath.cxx:1177
double pow(double, double)
EBinning
Definition: TKDE.h:60
void SetKernelFunction(KernelFunction_Ptr kernfunc=0)
Definition: TKDE.cxx:556
EKernelType fKernelType
Definition: TKDE.h:152
std::vector< std::vector< double > > Data
const Double_t sigma
friend class TKernel
Definition: TKDE.h:137
EMirror
Definition: TKDE.h:48
TH1F * h1
Definition: legend1.C:5
std::vector< Double_t > fData
Definition: TKDE.h:142
void SetUseBinsNEvents(UInt_t nEvents)
Definition: TKDE.cxx:387
TGraphErrors * fGraph
Definition: TKDE.h:150
void SetMirror()
Definition: TKDE.cxx:437
TF1 * GetKDEFunction(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:1125
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
double Integral(Function &f, double a, double b)
evaluate the Integral of a function f over the defined interval (a,b)
Definition: Integrator.h:496
const int nEvents
Definition: testRooFit.cxx:42
void Fill(Double_t data)
Definition: TKDE.cxx:641
TKernel * fKernel
Definition: TKDE.h:140
void CheckKernelValidity()
Definition: TKDE.cxx:996
Double_t ComputeKernelMu() const
Definition: TKDE.cxx:1038
#define M_PI
Definition: Rotated.cxx:105
Double_t fXMin
Definition: TKDE.h:170
Double_t GetRAMISE() const
Definition: TKDE.cxx:687
virtual ~TKDE()
Definition: TKDE.cxx:105
void SetOptions(const Option_t *option, Double_t rho)
Definition: TKDE.cxx:153
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2332
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
Double_t fSumOfCounts
Definition: TKDE.h:164
User Class for performing numerical integration of a function in one dimension.
Definition: Integrator.h:94
std::vector< Double_t > fEventWeights
Definition: TKDE.h:144
float xmax
Definition: THbookFile.cxx:93
Double_t ComputeKernelIntegral() const
Definition: TKDE.cxx:1047
void SetSigma(Double_t R)
Definition: TKDE.cxx:536
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
Bool_t fMirrorLeft
Definition: TKDE.h:157
Double_t fWeightSize
Definition: TKDE.h:175
Bool_t fAsymLeft
Definition: TKDE.h:157
Bool_t fNewData
Definition: TKDE.h:159
const Bool_t kFALSE
Definition: RtypesCore.h:92
TF1 * fLowerPDF
Definition: TKDE.h:148
void SetNBins(UInt_t nbins)
Definition: TKDE.cxx:368
virtual void SetTitle(const char *title="")
Set function title if title has the form "fffffff;xxxx;yyyy", it is assumed that the function title i...
Definition: TF1.cxx:3315
Double_t fMean
Definition: TKDE.h:167
static const double x1[5]
#define ClassImp(name)
Definition: Rtypes.h:336
double f(double x)
std::vector< Bool_t > fSettedOptions
Definition: TKDE.h:182
double Double_t
Definition: RtypesCore.h:55
void SetBinning(EBinning)
Definition: TKDE.cxx:360
EIteration fIteration
Definition: TKDE.h:153
Bool_t fUseBins
Definition: TKDE.h:158
std::vector< Double_t > fEvents
Definition: TKDE.h:143
Double_t fSigma
Definition: TKDE.h:168
TF1 * GetPDFUpperConfidenceInterval(Double_t confidenceLevel=0.95, UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:1139
KernelFunction_Ptr fKernelFunction
Definition: TKDE.h:135
Double_t y[n]
Definition: legend1.C:17
Double_t ey[n]
Definition: legend1.C:17
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
void DrawErrors(TString &drawOpt)
Definition: TKDE.cxx:828
Double_t ComputeKernelL2Norm() const
Definition: TKDE.cxx:1020
Double_t GetSigma() const
Definition: TKDE.cxx:681
EIteration
Definition: TKDE.h:43
UInt_t fUseBinsNEvents
Definition: TKDE.h:165
virtual TObject * Clone(const char *newname="") const
Make a clone of an object using the Streamer facility.
Definition: TNamed.cxx:65
UInt_t fNBins
Definition: TKDE.h:162
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
void SetUseBins()
Definition: TKDE.cxx:418
Double_t fSigmaRob
Definition: TKDE.h:169
void SetBinCountData()
Definition: TKDE.cxx:739
void CheckOptions(Bool_t isUserDefinedKernel=kFALSE)
Definition: TKDE.cxx:306
EBinning fBinning
Definition: TKDE.h:155
void AssureOptions()
Definition: TKDE.cxx:290
void SetFunction(Function &f)
method to set the a generic integration function
Definition: Integrator.h:489
Double_t GetMean() const
Definition: TKDE.cxx:675
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)
Definition: TKDE.cxx:116
Double_t fAdaptiveBandwidthFactor
Definition: TKDE.h:173
void SetMirroredEvents()
Definition: TKDE.cxx:500
void SetUserCanonicalBandwidth()
Definition: TKDE.cxx:1096
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.
Definition: TH1.cxx:3229
1-Dim function class
Definition: TF1.h:150
TF1 * GetKDEApproximateBias(UInt_t npx=100, Double_t xMin=1.0, Double_t xMax=0.0)
Definition: TKDE.cxx:1165
Int_t IsNaN(Double_t x)
Definition: TMath.h:778
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
void SetTuneFactor(Double_t rho)
Definition: TKDE.cxx:394
#define gPad
Definition: TVirtualPad.h:284
Double_t CosineArchKernel(Double_t x) const
Definition: TKDE.h:202
void SetData(const Double_t *data, const Double_t *weights)
Definition: TKDE.cxx:446
double result[121]
Definition: Rtypes.h:56
virtual void SetParameter(Int_t param, Double_t value)
Definition: TF1.h:578
Double_t UpperConfidenceInterval(const Double_t *x, const Double_t *p) const
Definition: TKDE.cxx:959
void DrawConfidenceInterval(TString &drawOpt, double cl=0.95)
Definition: TKDE.cxx:859
Double_t GaussianKernel(Double_t x) const
Definition: TKDE.h:190
double exp(double)
const Bool_t kTRUE
Definition: RtypesCore.h:91
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
void SetBinCentreData(Double_t xmin, Double_t xmax)
Definition: TKDE.cxx:730
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
Template class to wrap any member function of a class taking a double and returning a double in a 1D ...
User class for calculating the derivatives of a function.
double log(double)
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:859
Double_t fXMax
Definition: TKDE.h:171
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
Double_t ComputeMidspread()
Definition: TKDE.cxx:1085
const char * Data() const
Definition: TString.h:347
void SetDrawOptions(const Option_t *option, TString &plotOpt, TString &drawOpt)
Definition: TKDE.cxx:179
Double_t GetBias(Double_t x) const
Definition: TKDE.cxx:978