Logo ROOT   6.10/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 MIXMAX240 vs MIXMAX256" << std::endl;
156 
157  Random<MixMaxEngine240> rmx1(1111);
158  Random<MixMaxEngine<256,2>> rmx2(2222);
159 
160  ret &= testUniform(rmx1, rmx2);
161  ret &= testGauss(rmx1, rmx2);
162  return ret;
163 }
164 
165 bool test3() {
166 
167  bool ret = true;
168 
169  std::cout << "\nTesting MIXMAX240 vs MIXMAX17" << std::endl;
170 
171 
172  Random<MixMaxEngine240> rmx1(1111);
173  Random<MixMaxEngine<17,0>> rmx2(2222);
174 
175  ret &= testUniform(rmx1, rmx2);
176  ret &= testGauss(rmx1, rmx2);
177  return ret;
178 }
179 
180 bool test4() {
181 
182  bool ret = true;
183 
184  std::cout << "\nTesting MIXMAX240 vs MIXMAX240 using different seeds" << std::endl;
185 
186 
187  Random<MixMaxEngine240> rmx1(1111);
188  Random<MixMaxEngine240> rmx2(2222);
189 
190  ret &= testUniform(rmx1, rmx2);
191  ret &= testGauss(rmx1, rmx2);
192  return ret;
193 }
194 
195 
197 
198 
199  bool ret = true;
200  std::cout << "testing generating " << NR << " numbers " << std::endl;
201 
202  ret &= test1();
203  ret &= test2();
204  ret &= test3();
205  ret &= test4();
206 
207  if (!ret) Error("testMathRandom","Test Failed");
208  else
209  std::cout << "\nTestMathRandom: OK \n";
210  return ret;
211 }
212 
213 int main() {
214  bool ret = testMathRandom();
215  return (ret) ? 0 : -1;
216 }
217 
float xmin
Definition: THbookFile.cxx:93
int main()
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
bool test3()
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
Definition: Rtypes.h:56
bool testMathRandom()
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:168
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
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).
bool test4()
double minPValue
TH1F * h1
Definition: legend1.C:5
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
bool test1()
float xmax
Definition: THbookFile.cxx:93
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
const long long NR
bool testGauss(R1 &r1, R2 &r2)
Double_t y[n]
Definition: legend1.C:17
bool test2()
void KolmogorovSmirnov2SamplesTest(Double_t &pvalue, Double_t &testStat) const
Definition: GoFTest.cxx:892
bool testCompatibility(const std::vector< double > &x, const std::vector< double > &y, double xmin=0, double xmax=1)
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
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Definition: THist.hxx:310
#define gPad
Definition: TVirtualPad.h:284
bool showPlots
bool testUniform(R1 &r1, R2 &r2)
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
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:39
Stopwatch class.
Definition: TStopwatch.h:28