ROOT  6.06/09
Reference Guide
testMathRandom.cxx
Go to the documentation of this file.
1 #include "Math/Random.h"
2 #include "Math/TRandomEngine.h"
4 #include "Math/MixMaxEngine.h"
5 //#include "Math/MyMixMaxEngine.h"
6 //#include "Math/GSLRndmEngines.h"
7 #include "Math/GoFTest.h"
8 #include "Math/ProbFunc.h"
9 #include "TH1.h"
10 #include "TCanvas.h"
11 
12 #include "TRandom1.h"
13 #include "TRandom2.h"
14 #include "TRandom3.h"
15 //#include "TRandomNew3.h"
16 
17 #include "TStopwatch.h"
18 #include <iostream>
19 
20 #include <random>
21 
22 using namespace ROOT::Math;
23 
24 const long long NR = 1E7;
25 double minPValue = 1.E-3;
26 
27 bool showPlots = false;
28 
29 
30 bool testCompatibility(const std::vector<double> & x, const std::vector<double> & y, double xmin = 0, double xmax = 1) {
31 
32  GoFTest gof(x.size(), x.data(), y.size(), y.data() );
33 
34  bool ok = true;
35  double pvalue = gof.KolmogorovSmirnov2SamplesTest();
36  if ( pvalue < minPValue ) {
37  std::cout << "KS Test failed with p-value " << pvalue << std::endl;
38  ok = false;
39  }
40  else {
41  std::cout << "KS Test : OK - pvalue = " << pvalue << std::endl;
42  }
43 
44 
45  if (NR < 10000) {
46  pvalue = gof.AndersonDarling2SamplesTest();
47  if ( pvalue < minPValue ) {
48  std::cout << "AD Test failed with p-value " << pvalue << std::endl;
49  ok = false;
50  }
51  else {
52  std::cout << "AD Test : OK - pvalue = " << pvalue << std::endl;
53  }
54  }
55 
56  // do a chi2 binned test
57  int nbin = TMath::Min(x.size(), y.size() )/1000;
58  TH1D * h1 = new TH1D("h1","h1", nbin, xmin, xmax);
59  TH1D * h2 = new TH1D("h2","h2", nbin, xmin, xmax);
60  h1->FillN(x.size(), x.data(), nullptr );
61  h2->FillN(y.size(), y.data(), nullptr );
62 
63  pvalue = h1->Chi2Test(h2);
64  if ( pvalue < minPValue ) {
65  std::cout << "Chi2 Test failed with p-value " << pvalue << std::endl;
66  //showPlots = true;
67  ok = false;
68  }
69  else {
70  std::cout << "Chi2 Test: OK - pvalue = " << pvalue << std::endl;
71  }
72  if (showPlots) {
73  h1->Draw();
74  h2->SetLineColor(kRed);
75  h2->Draw("SAME");
76  if (gPad) gPad->Update();
77  }
78  else {
79  delete h1;
80  delete h2;
81  }
82 
83 
84  return ok;
85 }
86 
87 template <class R1, class R2>
88 bool testUniform(R1 & r1, R2 & r2) {
89 
90 
91  std::vector<double> x(NR);
92  std::vector<double> y(NR);
93 
94  TStopwatch w; w.Start();
95  for (int i = 0; i < NR; ++i)
96  x[i] = r1();
97 
98  w.Stop();
99  std::cout << "time for uniform filled for " << typeid(r1).name();
100  w.Print();
101 
102  w.Start();
103  for (int i = 0; i < NR; ++i)
104  y[i] = r2();
105 
106  w.Stop();
107  std::cout << "time for uniform filled for " << typeid(r2).name();
108  w.Print();
109 
110  return testCompatibility(x,y);
111 }
112 
113 template <class R1, class R2>
114 bool testGauss(R1 & r1, R2 & r2) {
115 
116 
117  std::vector<double> x(NR);
118  std::vector<double> y(NR);
119 
120  TStopwatch w; w.Start();
121  for (int i = 0; i < NR; ++i)
122  x[i] = ROOT::Math::normal_cdf(r1.Gaus(0,1),1);
123 
124  w.Stop();
125  std::cout << "time for GAUS filled for " << typeid(r1).name();
126  w.Print();
127 
128  w.Start();
129  for (int i = 0; i < NR; ++i)
130  y[i] = ROOT::Math::normal_cdf(r2.Gaus(0,1),1);
131 
132  w.Stop();
133  std::cout << "time for GAUS filled for " << typeid(r2).name();
134  w.Print();
135 
136  return testCompatibility(x,y);
137 }
138 
139 bool test1() {
140 
141  bool ret = true;
142  std::cout << "\nTesting MT vs MIXMAX " << std::endl;
143 
146  ret &= testUniform(rmx, rmt);
147  ret &= testGauss(rmx, rmt);
148  return ret;
149 }
150 
151 bool test2() {
152 
153  bool ret = true;
154 
155  std::cout << "\nTesting MIXMAX vs MIXMAX" << std::endl;
156 
157  // this test fails if first elemet is 0 is present !
158 
159  Random<MixMaxEngine> rmx1(1111);
160  Random<MixMaxEngine> rmx2(2222);
161 
162  ret &= testUniform(rmx1, rmx2);
163  ret &= testGauss(rmx1, rmx2);
164  return ret;
165 }
166 
167 
169 
170  // mixmax decimation
171  // test fails without decimation
172 #ifdef USE_MIXMAX_DECIMATION
173  std::cout << "Info:: Remove MIXMAX decimation " << std::endl;
176 #endif
177 
178 
179  bool ret = true;
180  std::cout << "testing generating " << NR << " numbers " << std::endl;
181 
182  ret &= test1();
183  ret &= test2();
184 
185  if (!ret) Error("testMathRandom","Test Failed");
186  else
187  std::cout << "\nTestMathRandom: OK \n";
188  return ret;
189 }
190 
191 int main() {
192  bool ret = testMathRandom();
193  return (ret) ? 0 : -1;
194 }
195 
static void SetSkipNumber(int nskip)
set the number we want to use to skip generation higher value means higher luxury but slower ...
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
float xmin
Definition: THbookFile.cxx:93
int main()
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
Definition: Rtypes.h:61
bool testMathRandom()
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
static void SetFirstReturnElement(int index)
set initial number to be used in the vector.
THist< 1, double > TH1D
Definition: THist.h:314
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
Double_t x[n]
Definition: legend1.C:17
double normal_cdf(double x, double sigma=1, double x0=0)
Cumulative distribution function of the normal (Gaussian) distribution (lower tail).
double minPValue
TH1F * h1
Definition: legend1.C:5
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
bool test1()
float xmax
Definition: THbookFile.cxx:93
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
const long long NR
bool testGauss(R1 &r1, R2 &r2)
Double_t y[n]
Definition: legend1.C:17
bool test2()
bool testCompatibility(const std::vector< double > &x, const std::vector< double > &y, double xmin=0, double xmax=1)
#define name(a, b)
Definition: linkTestLib0.cpp:5
#define nullptr
Definition: Rtypes.h:87
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:3265
#define gPad
Definition: TVirtualPad.h:288
bool showPlots
bool testUniform(R1 &r1, R2 &r2)
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
void KolmogorovSmirnov2SamplesTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:891
void Error(ErrorHandler_t func, int code, const char *va_(fmt),...)
Write error message and call a handler, if required.
Documentation for the Random class.
Definition: Random.h:41
Stopwatch class.
Definition: TStopwatch.h:30