Logo ROOT  
Reference Guide
GoFTest.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: Bartolomeu Rabacal 05/2010
3 /**********************************************************************
4  * *
5  * Copyright (c) 2006 , LCG ROOT MathLib Team *
6  * *
7  * *
8  **********************************************************************/
9 // Header file for GoFTest
10 
11 #ifndef ROOT_Math_GoFTest
12 #define ROOT_Math_GoFTest
13 
14 #include "Math/WrappedFunction.h"
15 #include "TMath.h"
16 
17 #include <memory>
18 #include <vector>
19 
20 /*
21 */
22 
23 namespace ROOT {
24 
25  namespace Fit {
26  class BinData;
27  }
28 namespace Math {
29 
30 ///// @defgroup GoFClasses Goodness of Fit Statistical Tests Tools
31 
32 /*
33  Class for Goodness of Fit tests implementing the Anderson-Darling and Kolmogorov-Smirnov 1- and 2-Samples Goodness of Fit Tests.
34  @ingroup MathCore
35 
36  */
37 
38 
39 class GoFTest {
40 public:
41 
42  enum EDistribution { // H0 distributions for using only with 1-sample tests
43  kUndefined, // Default value for non templated 1-sample test. Set with SetDistribution
44  kUserDefined, // For internal use only within the class's template constructor
45  kGaussian,
46  kLogNormal,
48  };
49 
50  enum EUserDistribution { // User input distribution option
52  kPDF // Default value
53  };
54 
55  enum ETestType { // Goodness of Fit test types for using with the class's unary funtions as a shorthand for the in-built methods
56  kAD, // Anderson-Darling Test. Default value
57  kAD2s, // Anderson-Darling 2-Samples Test
58  kKS, // Kolmogorov-Smirnov Test
59  kKS2s // Kolmogorov-Smirnov 2-Samples Test
60  };
61 
62  /* Constructor for using only with 2-samples tests */
63  GoFTest(UInt_t sample1Size, const Double_t* sample1, UInt_t sample2Size, const Double_t* sample2);
64 
65  /* Constructor for using only with 1-sample tests with a specified distribution */
66  GoFTest(UInt_t sampleSize, const Double_t* sample, EDistribution dist = kUndefined);
67 
68  /* Templated constructor for using only with 1-sample tests with a user specified distribution */
69  template<class Dist>
70  GoFTest(UInt_t sampleSize, const Double_t* sample, Dist& dist, EUserDistribution userDist = kPDF,
71  Double_t xmin = 1, Double_t xmax = 0)
72  {
73  Instantiate(sample, sampleSize);
74  SetUserDistribution<Dist>(dist, userDist, xmin, xmax);
75  }
76 
77  /* Specialization using IGenFunction interface */
78  GoFTest(UInt_t sampleSize, const Double_t* sample, const IGenFunction& dist, EUserDistribution userDist = kPDF,
79  Double_t xmin = 1, Double_t xmax = 0)
80  {
81  Instantiate(sample, sampleSize);
82  SetUserDistribution(dist, userDist, xmin, xmax);
83  }
84 
85  /* Sets the user input distribution function for 1-sample tests. */
86  template<class Dist>
89  SetDistributionFunction(wdist, userDist, xmin, xmax);
90  }
91 
92  /* Template specialization to set the user input distribution for 1-sample tests */
95  }
96 
97  /* Sets the user input distribution as a probability density function for 1-sample tests */
98  template<class Dist>
99  void SetUserPDF(Dist& pdf, Double_t xmin = 1, Double_t xmax = 0) {
100  SetUserDistribution<Dist>(pdf, kPDF, xmin, xmax);
101  }
102 
103  /* Template specialization to set the user input distribution as a probability density function for 1-sample tests */
104  void SetUserPDF(const IGenFunction& pdf, Double_t xmin = 1, Double_t xmax = 0) {
106  }
107 
108  /* Sets the user input distribution as a cumulative distribution function for 1-sample tests
109  The CDF must return zero
110  */
111  template<class Dist>
112  void SetUserCDF(Dist& cdf, Double_t xmin = 1, Double_t xmax = 0) {
113  SetUserDistribution<Dist>(cdf, kCDF, xmin, xmax);
114  }
115 
116  /* Template specialization to set the user input distribution as a cumulative distribution function for 1-sample tests */
117  void SetUserCDF(const IGenFunction& cdf, Double_t xmin = 1, Double_t xmax = 0) {
119  }
120 
121 
122  /* Sets the distribution for the predefined distribution types */
124 
125 
126  virtual ~GoFTest();
127 
128 /*
129  The Anderson-Darling K-Sample Test algorithm is described and taken from
130  http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/andeksam.htm
131  and described and taken from
132  (1) Scholz F.W., Stephens M.A. (1987), K-sample Anderson-Darling Tests, Journal of the American Statistical Association, 82, 918–924. (2-samples variant implemented)
133 */ void AndersonDarling2SamplesTest(Double_t& pvalue, Double_t& testStat) const;
134  Double_t AndersonDarling2SamplesTest(const Char_t* option = "p") const; // Returns default p-value; option "t" returns the test statistic value "A2"
135 
136 /*
137  The Anderson-Darling 1-Sample Test algorithm for a specific distribution is described at
138  http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/andedarl.htm
139  and described and taken from (2)
140  Marsaglia J.C.W., Marsaglia G. (2004), Evaluating the Anderson-Darling Distribution, Journal of Statistical Software, Volume 09, Issue i02.
141  and described and taken from (3)
142  Lewis P.A.W. (1961), The Annals of Mathematical Statistics, Distribution of the Anderson-Darling Statistic, Volume 32, Number 4, 1118-1124.
143 */ void AndersonDarlingTest(Double_t& pvalue, Double_t& testStat) const;
144  Double_t AndersonDarlingTest(const Char_t* option = "p") const; // Returns default p-value; option "t" returns the test statistic value "A2"
145 
146 /*
147  The Kolmogorov-Smirnov 2-Samples Test algorithm is described at
148  http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/ks2samp.htm
149  and described and taken from
150  http://root.cern.ch/root/html/TMath.html#TMath:KolmogorovTest
151 */ void KolmogorovSmirnov2SamplesTest(Double_t& pvalue, Double_t& testStat) const;
152  Double_t KolmogorovSmirnov2SamplesTest(const Char_t* option = "p") const; // Returns default p-value; option "t" returns the test statistic value "Dn"
153 
154 /*
155  The Kolmogorov-Smirnov 1-Sample Test algorithm for a specific distribution is described at
156  http://www.itl.nist.gov/div898/software/dataplot/refman1/auxillar/kstest.htm
157  and described and taken from (4)
158  Press W. H., Teukolsky S.A., Vetterling W.T., Flannery B.P. (2007), Numerical Recipes - The Art of Scientific Computing (Third Edition), Cambridge Univerdity Press
159 */ void KolmogorovSmirnovTest(Double_t& pvalue, Double_t& testStat) const;
160  Double_t KolmogorovSmirnovTest(const Char_t* option = "p") const; // Returns default p-value; option "t" returns the test statistic value "Dn"
161 
162  // The class's unary functions
163  void operator()(ETestType test, Double_t& pvalue, Double_t& testStat) const;
164 
165  // Returns default Anderson Darling 1-Sample Test and default p-value; option "t" returns the test statistic value
166  // specific to the test type
167  Double_t operator()(ETestType test = kAD, const Char_t* option = "p") const;
168 
169  // Computation of the K-Sample Anderson-Darling Test's p-value as described in (1)
170  // given a normalized test statistic. The first variant described in the paper is used
171  static Double_t PValueADKSamples(UInt_t nsamples, Double_t A2 );
172 
173  // Compute The 2-Sample Anderson Darling test for binned data
174  static void AndersonDarling2SamplesTest(const ROOT::Fit::BinData & data1, const ROOT::Fit::BinData & data2, Double_t& pvalue, Double_t& testStat);
175 
176 private:
177 
178  GoFTest(); // Disallowed default constructor
179  GoFTest(GoFTest& gof); // Disallowed copy constructor
180  GoFTest operator=(GoFTest& gof); // Disallowed assign operator
181 
182  std::unique_ptr<IGenFunction> fCDF;
183 
184 
186 
189 
190  std::vector<Double_t> fCombinedSamples;
191 
192  std::vector<std::vector<Double_t> > fSamples;
193 
195 
196  void SetCDF();
198 
199  void Instantiate(const Double_t* sample, UInt_t sampleSize);
200 
201 
205 
206  static Double_t GetSigmaN(const std::vector<UInt_t> & ns, UInt_t N); // Computation of sigma_N as described in (1)
207 
208  static Double_t InterpolatePValues(int nsamples,Double_t A2); // Linear interpolation used in GoFTest::PValueAD2Samples
209 
210 
211  Double_t PValueAD1Sample(Double_t A2) const; // Computation of the 1-Sample Anderson-Darling Test's p-value
212 
213  void LogSample(); // Applies the logarithm to the sample when the specified distribution to test is LogNormal
214 
215  void SetSamples(std::vector<const Double_t*> samples, const std::vector<UInt_t> samplesSizes);
216 
217  void SetParameters(); // Sets the estimated mean and standard-deviation from the samples
218 }; // end GoFTest class
219 
220 
221 } // ROOT namespace
222 } // Math namespace
223 #endif
ROOT::Math::GoFTest::InterpolatePValues
static Double_t InterpolatePValues(int nsamples, Double_t A2)
ROOT::Math::GoFTest::operator()
void operator()(ETestType test, Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:218
ROOT::Math::GoFTest::SetSamples
void SetSamples(std::vector< const Double_t * > samples, const std::vector< UInt_t > samplesSizes)
Definition: GoFTest.cxx:190
ROOT::Math::GoFTest
Definition: GoFTest.h:44
ROOT::Math::WrappedFunction
Template class to wrap any C++ callable object which takes one argument i.e.
Definition: WrappedFunction.h:94
HFit::Fit
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:133
ROOT::Math::GoFTest::LogNormalCDF
Double_t LogNormalCDF(Double_t x) const
ROOT::Math::GoFTest::LogSample
void LogSample()
Definition: GoFTest.cxx:312
ROOT::Fit::BinData
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:62
ROOT::Math::GoFTest::GoFTest
GoFTest()
ROOT::Math::GoFTest::SetUserDistribution
void SetUserDistribution(Dist &dist, EUserDistribution userDist=kPDF, Double_t xmin=1, Double_t xmax=0)
Definition: GoFTest.h:92
ROOT::Math::GoFTest::SetUserCDF
void SetUserCDF(Dist &cdf, Double_t xmin=1, Double_t xmax=0)
Definition: GoFTest.h:117
ROOT::Math::GoFTest::AndersonDarling2SamplesTest
void AndersonDarling2SamplesTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:656
ROOT::Math::GoFTest::kGaussian
@ kGaussian
Definition: GoFTest.h:50
ROOT::Math::GoFTest::SetCDF
void SetCDF()
Definition: GoFTest.cxx:254
xmax
float xmax
Definition: THbookFile.cxx:95
ROOT::Math::GoFTest::fTestSampleFromH0
Bool_t fTestSampleFromH0
Definition: GoFTest.h:199
ROOT::Math::GoFTest::fMean
Double_t fMean
Definition: GoFTest.h:192
ROOT::Math::GoFTest::fDist
EDistribution fDist
Definition: GoFTest.h:190
ROOT::Math::GoFTest::PValueADKSamples
static Double_t PValueADKSamples(UInt_t nsamples, Double_t A2)
Definition: GoFTest.cxx:363
ROOT::Math::GoFTest::AndersonDarlingTest
void AndersonDarlingTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:872
N
#define N
ROOT::Math::GoFTest::fCDF
std::unique_ptr< IGenFunction > fCDF
Definition: GoFTest.h:187
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Math::GoFTest::fCombinedSamples
std::vector< Double_t > fCombinedSamples
Definition: GoFTest.h:195
ROOT::Math::GoFTest::KolmogorovSmirnovTest
void KolmogorovSmirnovTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:931
ROOT::Math::GoFTest::kUserDefined
@ kUserDefined
Definition: GoFTest.h:49
test
Definition: test.py:1
bool
ROOT::Math::gv_detail::dist
double dist(Rotation3D const &r1, Rotation3D const &r2)
Definition: 3DDistances.cxx:63
xmin
float xmin
Definition: THbookFile.cxx:95
ROOT::Math::GoFTest::GaussianCDF
Double_t GaussianCDF(Double_t x) const
Definition: GoFTest.cxx:304
ROOT::Math::GoFTest::fSamples
std::vector< std::vector< Double_t > > fSamples
Definition: GoFTest.h:197
ROOT::Math::GoFTest::GetSigmaN
static Double_t GetSigmaN(const std::vector< UInt_t > &ns, UInt_t N)
Definition: GoFTest.cxx:321
ROOT::Math::GoFTest::SetParameters
void SetParameters()
Definition: GoFTest.cxx:213
ROOT::Math::GoFTest::~GoFTest
virtual ~GoFTest()
Definition: GoFTest.cxx:188
ROOT::Math::GoFTest::kLogNormal
@ kLogNormal
Definition: GoFTest.h:51
ROOT::Math::GoFTest::kKS
@ kKS
Definition: GoFTest.h:63
TGeant4Unit::ns
static constexpr double ns
Definition: TGeant4SystemOfUnits.h:167
ROOT::Math::GoFTest::kExponential
@ kExponential
Definition: GoFTest.h:52
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
ROOT::Math::GoFTest::kPDF
@ kPDF
Definition: GoFTest.h:57
ROOT::Math::GoFTest::EUserDistribution
EUserDistribution
Definition: GoFTest.h:55
ROOT::Math::GoFTest::fSigma
Double_t fSigma
Definition: GoFTest.h:193
unsigned int
ROOT::Math::GoFTest::Instantiate
void Instantiate(const Double_t *sample, UInt_t sampleSize)
Definition: GoFTest.cxx:286
ROOT::Math::GoFTest::SetUserPDF
void SetUserPDF(Dist &pdf, Double_t xmin=1, Double_t xmax=0)
Definition: GoFTest.h:104
ROOT::Math::GSLSimAn::Dist
double Dist(void *xp, void *yp)
Definition: GSLSimAnnealing.cxx:139
ROOT::Math::IBaseFunctionOneDim
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:135
ROOT::Math::GoFTest::PValueAD1Sample
Double_t PValueAD1Sample(Double_t A2) const
Definition: GoFTest.cxx:493
Double_t
double Double_t
Definition: RtypesCore.h:59
ROOT::Math::GoFTest::kCDF
@ kCDF
Definition: GoFTest.h:56
ROOT::Math::GoFTest::SetDistribution
void SetDistribution(EDistribution dist)
Definition: GoFTest.cxx:133
WrappedFunction.h
ROOT::Math::GoFTest::kUndefined
@ kUndefined
Definition: GoFTest.h:48
ROOT::Math::GoFTest::kAD2s
@ kAD2s
Definition: GoFTest.h:62
ROOT::Math::GoFTest::kKS2s
@ kKS2s
Definition: GoFTest.h:64
Char_t
char Char_t
Definition: RtypesCore.h:33
ROOT::Math::GoFTest::KolmogorovSmirnov2SamplesTest
void KolmogorovSmirnov2SamplesTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:906
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Math::GoFTest::operator=
GoFTest operator=(GoFTest &gof)
Math
ROOT::Math::GoFTest::kAD
@ kAD
Definition: GoFTest.h:61
ROOT::Math::GoFTest::SetDistributionFunction
void SetDistributionFunction(const IGenFunction &cdf, Bool_t isPDF, Double_t xmin, Double_t xmax)
Definition: GoFTest.cxx:274
ROOT::Math::GoFTest::ETestType
ETestType
Definition: GoFTest.h:60
ROOT::Math::GoFTest::ExponentialCDF
Double_t ExponentialCDF(Double_t x) const
Definition: GoFTest.cxx:308
TMath.h
ROOT::Math::GoFTest::EDistribution
EDistribution
Definition: GoFTest.h:47