exampleTKDE.C: Example of using the TKDE class (kernel density estimator) | Math tutorials | kdTreeBinning.C: kdTreeBinning tutorial: bin the data in cells of equal content using a kd-tree |
// ------------------------------------------------------------------------ // // GoFTest tutorial macro // // Using Anderson-Darling and Kolmogorov-Smirnov goodness of fit tests // 1 sample test is performed comparing data with a log-normal distribution // and a 2 sample test is done comparing two gaussian data sets. // // // Author: Bartolomeu Rabacal 6/2010 // // ------------------------------------------------------------------------ #include <cassert> #include "TCanvas.h" #include "TPaveText.h" #include "TH1.h" #include "TF1.h" #include "Math/GoFTest.h" #include "Math/Functor.h" #include "TRandom3.h" #include "Math/DistFunc.h" void goftest() { // ------------------------------------------------------------------------ // C a s e 1 : C r e a t e l o g N o r m a l r a n d o m s a m p l e // ------------------------------------------------------------------------ UInt_t nEvents1 = 1000; //ROOT::Math::Random<ROOT::Math::GSLRngMT> r; TF1 * f1 = new TF1("logNormal","ROOT::Math::lognormal_pdf(x,[0],[1])",0,500); // set the lognormal parameters (m and s) f1->SetParameters(5.0,2.0); f1->SetNpx(1000); Double_t* sample1 = new Double_t[nEvents1]; TH1D* h1smp = new TH1D("h1smp", "LogNormal distribution histogram", 100, 0, 500); h1smp->SetStats(kFALSE); for (UInt_t i = 0; i < nEvents1; ++i) { Double_t data = f1->GetRandom(); sample1[i] = data; h1smp->Fill(data); } // normalize correctly the histogram h1smp->Scale( ROOT::Math::lognormal_cdf(500.,5.,2) / nEvents1, "width"); TCanvas* c = new TCanvas("c","1-Sample and 2-Samples GoF Tests"); c->Divide(1, 2); c->cd(1); h1smp->Draw(); h1smp->SetLineColor(kBlue); f1->SetNpx(100); // use same points as histo for drawing f1->SetLineColor(kRed); f1->Draw("SAME"); // ----------------------------------------- // C r e a t e G o F T e s t o b j e c t // ----------------------------------------- ROOT::Math::GoFTest* goftest_1 = new ROOT::Math::GoFTest(nEvents1, sample1, ROOT::Math::GoFTest::kLogNormal); /* Possible calls for the Anderson - DarlingTest test */ /*----------------------------------------------------*/ /* a) Returning the Anderson-Darling standardized test statistic */ Double_t A2_1 = goftest_1-> AndersonDarlingTest("t"); Double_t A2_2 = (*goftest_1)(ROOT::Math::GoFTest::kAD, "t"); assert(A2_1 == A2_2); /* b) Returning the p-value for the Anderson-Darling test statistic */ Double_t pvalueAD_1 = goftest_1-> AndersonDarlingTest(); // p-value is the default choice Double_t pvalueAD_2 = (*goftest_1)(); // p-value and Anderson - Darling Test are the default choices assert(pvalueAD_1 == pvalueAD_2); /* Rebuild the test using the default 1-sample construtor */ delete goftest_1; goftest_1 = new ROOT::Math::GoFTest(nEvents1, sample1 ); // User must then input a distribution type option goftest_1->SetDistribution(ROOT::Math::GoFTest::kLogNormal); /* Possible calls for the Kolmogorov - Smirnov test */ /*--------------------------------------------------*/ /* a) Returning the Kolmogorov-Smirnov standardized test statistic */ Double_t Dn_1 = goftest_1-> KolmogorovSmirnovTest("t"); Double_t Dn_2 = (*goftest_1)(ROOT::Math::GoFTest::kKS, "t"); assert(Dn_1 == Dn_2); /* b) Returning the p-value for the Kolmogorov-Smirnov test statistic */ Double_t pvalueKS_1 = goftest_1-> KolmogorovSmirnovTest(); Double_t pvalueKS_2 = (*goftest_1)(ROOT::Math::GoFTest::kKS); assert(pvalueKS_1 == pvalueKS_2); /* Valid but incorrect calls for the 2-samples methods of the 1-samples constucted goftest_1 */ #ifdef TEST_ERROR_MESSAGE Double_t A2 = (*goftest_1)(ROOT::Math::GoFTest::kAD2s, "t"); // Issues error message Double_t pvalueKS = (*goftest_1)(ROOT::Math::GoFTest::kKS2s); // Issues error message assert(A2 == pvalueKS); #endif TPaveText* pt1 = new TPaveText(0.58, 0.6, 0.88, 0.80, "brNDC"); Char_t str1[50]; sprintf(str1, "p-value for A-D 1-smp test: %f", pvalueAD_1); pt1->AddText(str1); pt1->SetFillColor(18); pt1->SetTextFont(20); pt1->SetTextColor(4); Char_t str2[50]; sprintf(str2, "p-value for K-S 1-smp test: %f", pvalueKS_1); pt1->AddText(str2); pt1->Draw(); // ------------------------------------------------------------------------ // C a s e 2 : C r e a t e G a u s s i a n r a n d o m s a m p l e s // ------------------------------------------------------------------------ UInt_t nEvents2 = 2000; Double_t* sample2 = new Double_t[nEvents2]; TH1D* h2smps_1 = new TH1D("h2smps_1", "Gaussian distribution histograms", 100, 0, 500); h2smps_1->SetStats(kFALSE); TH1D* h2smps_2 = new TH1D("h2smps_2", "Gaussian distribution histograms", 100, 0, 500); h2smps_2->SetStats(kFALSE); TRandom3 r; for (UInt_t i = 0; i < nEvents1; ++i) { Double_t data = r.Gaus(300, 50); sample1[i] = data; h2smps_1->Fill(data); } h2smps_1->Scale(1. / nEvents1, "width"); c->cd(2); h2smps_1->Draw(); h2smps_1->SetLineColor(kBlue); for (UInt_t i = 0; i < nEvents2; ++i) { Double_t data = r.Gaus(300, 50); sample2[i] = data; h2smps_2->Fill(data); } h2smps_2->Scale(1. / nEvents2, "width"); h2smps_2->Draw("SAME"); h2smps_2->SetLineColor(kRed); // ----------------------------------------- // C r e a t e G o F T e s t o b j e c t // ----------------------------------------- ROOT::Math::GoFTest* goftest_2 = new ROOT::Math::GoFTest(nEvents1, sample1, nEvents2, sample2); /* Possible calls for the Anderson - DarlingTest test */ /*----------------------------------------------------*/ /* a) Returning the Anderson-Darling standardized test statistic */ A2_1 = goftest_2->AndersonDarling2SamplesTest("t"); A2_2 = (*goftest_2)(ROOT::Math::GoFTest::kAD2s, "t"); assert(A2_1 == A2_2); /* b) Returning the p-value for the Anderson-Darling test statistic */ pvalueAD_1 = goftest_2-> AndersonDarling2SamplesTest(); // p-value is the default choice pvalueAD_2 = (*goftest_2)(ROOT::Math::GoFTest::kAD2s); // p-value is the default choices assert(pvalueAD_1 == pvalueAD_2); /* Possible calls for the Kolmogorov - Smirnov test */ /*--------------------------------------------------*/ /* a) Returning the Kolmogorov-Smirnov standardized test statistic */ Dn_1 = goftest_2-> KolmogorovSmirnov2SamplesTest("t"); Dn_2 = (*goftest_2)(ROOT::Math::GoFTest::kKS2s, "t"); assert(Dn_1 == Dn_2); /* b) Returning the p-value for the Kolmogorov-Smirnov test statistic */ pvalueKS_1 = goftest_2-> KolmogorovSmirnov2SamplesTest(); pvalueKS_2 = (*goftest_2)(ROOT::Math::GoFTest::kKS2s); assert(pvalueKS_1 == pvalueKS_2); #ifdef TEST_ERROR_MESSAGE /* Valid but incorrect calls for the 1-sample methods of the 2-samples constucted goftest_2 */ A2 = (*goftest_2)(ROOT::Math::GoFTest::kAD, "t"); // Issues error message pvalueKS = (*goftest_2)(ROOT::Math::GoFTest::kKS); // Issues error message assert(A2 == pvalueKS); #endif TPaveText* pt2 = new TPaveText(0.13, 0.6, 0.43, 0.8, "brNDC"); sprintf(str1, "p-value for A-D 2-smps test: %f", pvalueAD_1); pt2->AddText(str1); pt2->SetFillColor(18); pt2->SetTextFont(20); pt2->SetTextColor(4); sprintf(str2, "p-value for K-S 2-smps test: %f", pvalueKS_1); pt2-> AddText(str2); pt2-> Draw(); // ------------------------------------------------------------------------ // C a s e 3 : C r e a t e L a n d a u r a n d o m s a m p l e // ------------------------------------------------------------------------ UInt_t nEvents3 = 1000; Double_t* sample3 = new Double_t[nEvents3]; for (UInt_t i = 0; i < nEvents3; ++i) { Double_t data = r.Landau(); sample3[i] = data; } // ------------------------------------------ // C r e a t e G o F T e s t o b j e c t s // ------------------------------------------ /* Possible constructors for the user input distribution */ /*-------------------------------------------------------*/ /* a) User input PDF */ ROOT::Math::Functor1D f(&TMath::Landau); double min = 3*TMath::MinElement(nEvents3, sample3); double max = 3*TMath::MaxElement(nEvents3, sample3); ROOT::Math::GoFTest* goftest_3a = new ROOT::Math::GoFTest(nEvents3, sample3, f, ROOT::Math::GoFTest::kPDF, min,max); // need to specify am interval /* b) User input CDF */ ROOT::Math::Functor1D fI(&TMath::LandauI); ROOT::Math::GoFTest* goftest_3b = new ROOT::Math::GoFTest(nEvents3, sample3, fI, ROOT::Math::GoFTest::kCDF,min,max); /* Returning the p-value for the Anderson-Darling test statistic */ pvalueAD_1 = goftest_3a-> AndersonDarlingTest(); // p-value is the default choice pvalueAD_2 = (*goftest_3b)(); // p-value and Anderson - Darling Test are the default choices /* Checking consistency between both tests */ std::cout << " \n\nTEST with LANDAU distribution:\t"; if (TMath::Abs(pvalueAD_1 - pvalueAD_2) > 1.E-1 * pvalueAD_2) { std::cout << "FAILED " << std::endl; Error("goftest","Error in comparing testing using Landau and Landau CDF"); std::cerr << " pvalues are " << pvalueAD_1 << " " << pvalueAD_2 << std::endl; } else std::cout << "OK ( pvalues = " << pvalueAD_2 << " )" << std::endl; } goftest.C:1 goftest.C:2 goftest.C:3 goftest.C:4 goftest.C:5 goftest.C:6 goftest.C:7 goftest.C:8 goftest.C:9 goftest.C:10 goftest.C:11 goftest.C:12 goftest.C:13 goftest.C:14 goftest.C:15 goftest.C:16 goftest.C:17 goftest.C:18 goftest.C:19 goftest.C:20 goftest.C:21 goftest.C:22 goftest.C:23 goftest.C:24 goftest.C:25 goftest.C:26 goftest.C:27 goftest.C:28 goftest.C:29 goftest.C:30 goftest.C:31 goftest.C:32 goftest.C:33 goftest.C:34 goftest.C:35 goftest.C:36 goftest.C:37 goftest.C:38 goftest.C:39 goftest.C:40 goftest.C:41 goftest.C:42 goftest.C:43 goftest.C:44 goftest.C:45 goftest.C:46 goftest.C:47 goftest.C:48 goftest.C:49 goftest.C:50 goftest.C:51 goftest.C:52 goftest.C:53 goftest.C:54 goftest.C:55 goftest.C:56 goftest.C:57 goftest.C:58 goftest.C:59 goftest.C:60 goftest.C:61 goftest.C:62 goftest.C:63 goftest.C:64 goftest.C:65 goftest.C:66 goftest.C:67 goftest.C:68 goftest.C:69 goftest.C:70 goftest.C:71 goftest.C:72 goftest.C:73 goftest.C:74 goftest.C:75 goftest.C:76 goftest.C:77 goftest.C:78 goftest.C:79 goftest.C:80 goftest.C:81 goftest.C:82 goftest.C:83 goftest.C:84 goftest.C:85 goftest.C:86 goftest.C:87 goftest.C:88 goftest.C:89 goftest.C:90 goftest.C:91 goftest.C:92 goftest.C:93 goftest.C:94 goftest.C:95 goftest.C:96 goftest.C:97 goftest.C:98 goftest.C:99 goftest.C:100 goftest.C:101 goftest.C:102 goftest.C:103 goftest.C:104 goftest.C:105 goftest.C:106 goftest.C:107 goftest.C:108 goftest.C:109 goftest.C:110 goftest.C:111 goftest.C:112 goftest.C:113 goftest.C:114 goftest.C:115 goftest.C:116 goftest.C:117 goftest.C:118 goftest.C:119 goftest.C:120 goftest.C:121 goftest.C:122 goftest.C:123 goftest.C:124 goftest.C:125 goftest.C:126 goftest.C:127 goftest.C:128 goftest.C:129 goftest.C:130 goftest.C:131 goftest.C:132 goftest.C:133 goftest.C:134 goftest.C:135 goftest.C:136 goftest.C:137 goftest.C:138 goftest.C:139 goftest.C:140 goftest.C:141 goftest.C:142 goftest.C:143 goftest.C:144 goftest.C:145 goftest.C:146 goftest.C:147 goftest.C:148 goftest.C:149 goftest.C:150 goftest.C:151 goftest.C:152 goftest.C:153 goftest.C:154 goftest.C:155 goftest.C:156 goftest.C:157 goftest.C:158 goftest.C:159 goftest.C:160 goftest.C:161 goftest.C:162 goftest.C:163 goftest.C:164 goftest.C:165 goftest.C:166 goftest.C:167 goftest.C:168 goftest.C:169 goftest.C:170 goftest.C:171 goftest.C:172 goftest.C:173 goftest.C:174 goftest.C:175 goftest.C:176 goftest.C:177 goftest.C:178 goftest.C:179 goftest.C:180 goftest.C:181 goftest.C:182 goftest.C:183 goftest.C:184 goftest.C:185 goftest.C:186 goftest.C:187 goftest.C:188 goftest.C:189 goftest.C:190 goftest.C:191 goftest.C:192 goftest.C:193 goftest.C:194 goftest.C:195 goftest.C:196 goftest.C:197 goftest.C:198 goftest.C:199 goftest.C:200 goftest.C:201 goftest.C:202 goftest.C:203 goftest.C:204 goftest.C:205 goftest.C:206 goftest.C:207 goftest.C:208 goftest.C:209 goftest.C:210 goftest.C:211 goftest.C:212 goftest.C:213 goftest.C:214 goftest.C:215 goftest.C:216 goftest.C:217 goftest.C:218 goftest.C:219 goftest.C:220 goftest.C:221 goftest.C:222 goftest.C:223 goftest.C:224 goftest.C:225 goftest.C:226 goftest.C:227 goftest.C:228 goftest.C:229 goftest.C:230 goftest.C:231 goftest.C:232 goftest.C:233 goftest.C:234 goftest.C:235 goftest.C:236 goftest.C:237 goftest.C:238 goftest.C:239 goftest.C:240 goftest.C:241 goftest.C:242 goftest.C:243 goftest.C:244 goftest.C:245 goftest.C:246 |
|