00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ROOT_TKDE
00012 #define ROOT_TKDE
00013
00014 #ifndef ROOT_Math_WrappedFunction
00015 #include "Math/WrappedFunction.h"
00016 #endif
00017
00018 #ifndef ROOT_TNamed
00019 #include "TNamed.h"
00020 #endif
00021
00022 #ifndef ROOT_Math_Math
00023 #include "Math/Math.h"
00024 #endif
00025
00026
00027 class TGraphErrors;
00028 class TF1;
00029
00030
00031
00032
00033
00034
00035
00036
00037 class TKDE : public TNamed {
00038 public:
00039
00040 enum EKernelType {
00041 kGaussian,
00042 kEpanechnikov,
00043 kBiweight,
00044 kCosineArch,
00045 kUserDefined,
00046 kTotalKernels
00047 };
00048
00049 enum EIteration {
00050 kAdaptive,
00051 kFixed
00052 };
00053
00054 enum EMirror {
00055 kNoMirror,
00056 kMirrorLeft,
00057 kMirrorRight,
00058 kMirrorBoth,
00059 kMirrorAsymLeft,
00060 kMirrorAsymLeftRight,
00061 kMirrorAsymRight,
00062 kMirrorLeftAsymRight,
00063 kMirrorAsymBoth
00064 };
00065
00066 enum EBinning{
00067 kUnbinned,
00068 kRelaxedBinning,
00069 kForcedBinning
00070 };
00071
00072 explicit TKDE(UInt_t events = 0, const Double_t* data = 0, Double_t xMin = 0.0, Double_t xMax = 0.0, const Option_t* option = "KernelType:Gaussian;Iteration:Adaptive;Mirror:noMirror;Binning:RelaxedBinning", Double_t rho = 1.0);
00073
00074 template<class KernelFunction>
00075 TKDE(const Char_t* , const KernelFunction& kernfunc, UInt_t events, const Double_t* data, Double_t xMin = 0.0, Double_t xMax = 0.0, const Option_t* option = "KernelType:UserDefined;Iteration:Adaptive;Mirror:noMirror;Binning:RelaxedBinning", Double_t rho = 1.0) {
00076 Instantiate(new ROOT::Math::WrappedFunction<const KernelFunction&>(kernfunc), events, data, xMin, xMax, option, rho);
00077 }
00078
00079 virtual ~TKDE();
00080
00081 void Fill(Double_t data);
00082 void SetKernelType(EKernelType kern);
00083 void SetIteration(EIteration iter);
00084 void SetMirror(EMirror mir);
00085 void SetBinning(EBinning);
00086 void SetNBins(UInt_t nbins);
00087 void SetUseBinsNEvents(UInt_t nEvents);
00088 void SetTuneFactor(Double_t rho);
00089 void SetRange(Double_t xMin, Double_t xMax);
00090
00091 virtual void Draw(const Option_t* option = "");
00092
00093 Double_t operator()(Double_t x) const;
00094 Double_t operator()(const Double_t* x, const Double_t* p=0) const;
00095
00096 Double_t GetValue(Double_t x) const { return (*this)(x); }
00097 Double_t GetError(Double_t x) const;
00098
00099 Double_t GetBias(Double_t x) const;
00100 Double_t GetMean() const;
00101 Double_t GetSigma() const;
00102 Double_t GetRAMISE() const;
00103
00104 Double_t GetFixedWeight() const;
00105
00106 TF1* GetFunction(UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00107 TF1* GetUpperFunction(Double_t confidenceLevel = 0.95, UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00108 TF1* GetLowerFunction(Double_t confidenceLevel = 0.95, UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00109 TF1* GetApproximateBias(UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00110 TGraphErrors * GetGraphWithErrors(UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00111
00112
00113
00114 TF1 * GetDrawnFunction() { return fPDF;}
00115 TF1 * GetDrawnUpperFunction() { return fUpperPDF;}
00116 TF1 * GetDrawnLowerFunction() { return fLowerPDF;}
00117 TGraphErrors * GetDrawnGraph() { return fGraph;}
00118
00119 const Double_t * GetAdaptiveWeights() const;
00120
00121
00122 private:
00123
00124 TKDE(TKDE& kde);
00125 TKDE operator=(TKDE& kde);
00126
00127 typedef ROOT::Math::IBaseFunctionOneDim* KernelFunction_Ptr;
00128 KernelFunction_Ptr fKernelFunction;
00129
00130 class TKernel;
00131 friend class TKernel;
00132
00133 TKernel* fKernel;
00134
00135 std::vector<Double_t> fData;
00136 std::vector<Double_t> fEvents;
00137
00138 TF1* fPDF;
00139 TF1* fUpperPDF;
00140 TF1* fLowerPDF;
00141 TF1* fApproximateBias;
00142 TGraphErrors* fGraph;
00143
00144 EKernelType fKernelType;
00145 EIteration fIteration;
00146 EMirror fMirror;
00147 EBinning fBinning;
00148
00149 Bool_t fUseMirroring, fMirrorLeft, fMirrorRight, fAsymLeft, fAsymRight;
00150 Bool_t fUseBins;
00151 Bool_t fNewData;
00152 Bool_t fUseMinMaxFromData;
00153
00154 UInt_t fNBins;
00155 UInt_t fNEvents;
00156 UInt_t fUseBinsNEvents;
00157
00158 Double_t fMean;
00159 Double_t fSigma;
00160 Double_t fSigmaRob;
00161 Double_t fXMin;
00162 Double_t fXMax;
00163 Double_t fRho;
00164 Double_t fAdaptiveBandwidthFactor;
00165
00166 Double_t fWeightSize;
00167
00168 std::vector<Double_t> fCanonicalBandwidths;
00169 std::vector<Double_t> fKernelSigmas2;
00170
00171 std::vector<UInt_t> fBinCount;
00172
00173 std::vector<Bool_t> fSettedOptions;
00174
00175 struct KernelIntegrand;
00176 friend struct KernelIntegrand;
00177
00178 void Instantiate(KernelFunction_Ptr kernfunc, UInt_t events, const Double_t* data,
00179 Double_t xMin, Double_t xMax, const Option_t* option, Double_t rho);
00180
00181 inline Double_t GaussianKernel(Double_t x) const {
00182
00183 Double_t k2_PI_ROOT_INV = 0.398942280401432703;
00184 return (x > -9. && x < 9.) ? k2_PI_ROOT_INV * std::exp(-.5 * x * x) : 0.0;
00185 }
00186 inline Double_t EpanechnikovKernel(Double_t x) const {
00187 return (x > -1. && x < 1.) ? 3. / 4. * (1. - x * x) : 0.0;
00188 }
00189 inline Double_t BiweightKernel(Double_t x) const {
00190
00191 return (x > -1. && x < 1.) ? 15. / 16. * (1. - x * x) * (1. - x * x) : 0.0;
00192 }
00193 inline Double_t CosineArchKernel(Double_t x) const {
00194
00195 return (x > -1. && x < 1.) ? M_PI_4 * std::cos(M_PI_2 * x) : 0.0;
00196 }
00197 Double_t UpperConfidenceInterval(const Double_t* x, const Double_t* p) const;
00198 Double_t LowerConfidenceInterval(const Double_t* x, const Double_t* p) const;
00199 Double_t ApproximateBias(const Double_t* x, const Double_t* ) const { return GetBias(*x); }
00200 Double_t ComputeKernelL2Norm() const;
00201 Double_t ComputeKernelSigma2() const;
00202 Double_t ComputeKernelMu() const;
00203 Double_t ComputeKernelIntegral() const;
00204 Double_t ComputeMidspread() ;
00205
00206 UInt_t Index(Double_t x) const;
00207
00208 void SetBinCentreData(Double_t xmin, Double_t xmax);
00209 void SetBinCountData();
00210 void CheckKernelValidity();
00211 void SetCanonicalBandwidth();
00212 void SetKernelSigma2();
00213 void SetCanonicalBandwidths();
00214 void SetKernelSigmas2();
00215 void SetHistogram();
00216 void SetUseBins();
00217 void SetMirror();
00218 void SetMean();
00219 void SetSigma(Double_t R);
00220 void SetKernel();
00221 void SetKernelFunction(KernelFunction_Ptr kernfunc = 0);
00222 void SetOptions(const Option_t* option, Double_t rho);
00223 void CheckOptions(Bool_t isUserDefinedKernel = kFALSE);
00224 void GetOptions(std::string optionType, std::string option);
00225 void AssureOptions();
00226 void SetData(const Double_t* data);
00227 void InitFromNewData();
00228 void SetMirroredEvents();
00229 void SetDrawOptions(const Option_t* option, TString& plotOpt, TString& drawOpt);
00230 void DrawErrors(TString& drawOpt);
00231 void DrawConfidenceInterval(TString& drawOpt, double cl=0.95);
00232
00233 TF1* GetKDEFunction(UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00234 TF1* GetKDEApproximateBias(UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00235
00236 TF1* GetPDFUpperConfidenceInterval(Double_t confidenceLevel = 0.95, UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00237 TF1* GetPDFLowerConfidenceInterval(Double_t confidenceLevel = 0.95, UInt_t npx = 100, Double_t xMin = 1.0, Double_t xMax = 0.0);
00238
00239 ClassDef(TKDE, 1)
00240
00241 };
00242
00243 #endif