forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestMathRandom.cxx
More file actions
278 lines (210 loc) · 6.4 KB
/
Copy pathtestMathRandom.cxx
File metadata and controls
278 lines (210 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "Math/Random.h"
#include "Math/TRandomEngine.h"
#include "Math/MersenneTwisterEngine.h"
#include "Math/MixMaxEngine.h"
//#include "Math/MyMixMaxEngine.h"
//#include "Math/GSLRndmEngines.h"
#include "Math/GoFTest.h"
#include "Math/ProbFuncMathCore.h"
#include "TH1.h"
#include "TVirtualPad.h"
#include "TRandom1.h"
#include "TRandom2.h"
#include "TRandom3.h"
//#include "TRandomNew3.h"
#include "TStopwatch.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <random>
#include <type_traits>
using namespace ROOT::Math;
const long long NR = 1E6;
double minPValue = 1.E-3;
bool showPlots = false;
bool testCompatibility(const std::vector<double> & x, const std::vector<double> & y, double xmin = 0, double xmax = 1) {
GoFTest gof(x.size(), x.data(), y.size(), y.data() );
bool ok = true;
double pvalue = gof.KolmogorovSmirnov2SamplesTest();
if ( pvalue < minPValue ) {
std::cout << "KS Test failed with p-value " << pvalue << std::endl;
ok = false;
}
else {
std::cout << "KS Test : OK - pvalue = " << pvalue << std::endl;
}
if (NR < 10000) {
pvalue = gof.AndersonDarling2SamplesTest();
if ( pvalue < minPValue ) {
std::cout << "AD Test failed with p-value " << pvalue << std::endl;
ok = false;
}
else {
std::cout << "AD Test : OK - pvalue = " << pvalue << std::endl;
}
}
// do a chi2 binned test
int nbin = TMath::Min(x.size(), y.size() )/1000;
TH1D * h1 = new TH1D("h1","h1", nbin, xmin, xmax);
TH1D * h2 = new TH1D("h2","h2", nbin, xmin, xmax);
h1->FillN(x.size(), x.data(), nullptr );
h2->FillN(y.size(), y.data(), nullptr );
pvalue = h1->Chi2Test(h2);
if ( pvalue < minPValue ) {
std::cout << "Chi2 Test failed with p-value " << pvalue << std::endl;
//showPlots = true;
ok = false;
}
else {
std::cout << "Chi2 Test: OK - pvalue = " << pvalue << std::endl;
}
if (showPlots) {
h1->Draw();
h2->SetLineColor(kRed);
h2->Draw("SAME");
if (gPad) gPad->Update();
}
else {
delete h1;
delete h2;
}
return ok;
}
template <class R1, class R2>
bool testUniform(R1 & r1, R2 & r2) {
std::vector<double> x(NR);
std::vector<double> y(NR);
TStopwatch w; w.Start();
for (int i = 0; i < NR; ++i)
x[i] = r1();
w.Stop();
std::cout << "time for uniform filled for " << typeid(r1).name();
w.Print();
w.Start();
for (int i = 0; i < NR; ++i)
y[i] = r2();
w.Stop();
std::cout << "time for uniform filled for " << typeid(r2).name();
w.Print();
return testCompatibility(x,y);
}
template <class R1, class R2>
bool testGauss(R1 & r1, R2 & r2) {
std::vector<double> x(NR);
std::vector<double> y(NR);
TStopwatch w; w.Start();
for (int i = 0; i < NR; ++i)
x[i] = ROOT::Math::normal_cdf(r1.Gaus(0,1),1);
w.Stop();
std::cout << "time for GAUS filled for " << typeid(r1).name();
w.Print();
w.Start();
for (int i = 0; i < NR; ++i)
y[i] = ROOT::Math::normal_cdf(r2.Gaus(0,1),1);
w.Stop();
std::cout << "time for GAUS filled for " << typeid(r2).name();
w.Print();
return testCompatibility(x,y);
}
bool test1() {
bool ret = true;
std::cout << "\nTesting MT vs MIXMAX " << std::endl;
Random<MersenneTwisterEngine> rmt;
Random<MixMaxEngine240> rmx;
ret &= testUniform(rmx, rmt);
ret &= testGauss(rmx, rmt);
return ret;
}
bool test2() {
bool ret = true;
std::cout << "\nTesting MIXMAX240 vs MIXMAX256" << std::endl;
Random<MixMaxEngine240> rmx1(1111);
Random<MixMaxEngine<256,2>> rmx2(2222);
ret &= testUniform(rmx1, rmx2);
ret &= testGauss(rmx1, rmx2);
return ret;
}
bool test3() {
bool ret = true;
std::cout << "\nTesting MIXMAX240 vs MIXMAX17" << std::endl;
Random<MixMaxEngine240> rmx1(1111);
Random<MixMaxEngine<17,0>> rmx2(2222);
ret &= testUniform(rmx1, rmx2);
ret &= testGauss(rmx1, rmx2);
return ret;
}
bool test4() {
bool ret = true;
std::cout << "\nTesting MIXMAX240 vs MIXMAX240 using different seeds" << std::endl;
Random<MixMaxEngine240> rmx1(1111);
Random<MixMaxEngine240> rmx2(2222);
ret &= testUniform(rmx1, rmx2);
ret &= testGauss(rmx1, rmx2);
return ret;
}
bool test5() {
std::cout << "\nTesting TRandom std::UniformRandomBitGenerator interface" << std::endl;
TRandom3 rng(42);
static_assert(std::is_same<TRandom::result_type, UInt_t>::value, "result_type must be UInt_t");
if (TRandom::min() != 0) {
std::cout << "TRandom::min() is not 0" << std::endl;
return false;
}
if (TRandom::max() != std::numeric_limits<UInt_t>::max()) {
std::cout << "TRandom::max() is wrong" << std::endl;
return false;
}
for (int i = 0; i < 10000; i++) {
auto v = rng();
if (v < TRandom::min() || v > TRandom::max()) {
std::cout << "operator() returned out-of-range value: " << v << std::endl;
return false;
}
}
std::vector<int> vec(10);
std::iota(vec.begin(), vec.end(), 1);
std::shuffle(vec.begin(), vec.end(), rng);
std::sort(vec.begin(), vec.end());
for (int i = 0; i < 10; i++) {
if (vec[i] != i + 1) {
std::cout << "std::shuffle corrupted the elements" << std::endl;
return false;
}
}
std::uniform_int_distribution<int> dist(0, 99);
for (int i = 0; i < 10000; i++) {
int v = dist(rng);
if (v < 0 || v > 99) {
std::cout << "std::uniform_int_distribution produced out-of-range value: " << v << std::endl;
return false;
}
}
// verify TRandom2 override returns raw integers (no double round-trip)
TRandom2 rng2(42);
for (int i = 0; i < 10000; i++) {
auto v2 = rng2();
if (v2 < TRandom::min() || v2 > TRandom::max()) {
std::cout << "TRandom2::operator() returned out-of-range value: " << v2 << std::endl;
return false;
}
}
std::cout << "TRandom std interface: OK" << std::endl;
return true;
}
bool testMathRandom() {
bool ret = true;
std::cout << "testing generating " << NR << " numbers " << std::endl;
ret &= test1();
ret &= test2();
ret &= test3();
ret &= test4();
ret &= test5();
if (!ret) Error("testMathRandom","Test Failed");
else
std::cout << "\nTestMathRandom: OK \n";
return ret;
}
int main() {
bool ret = testMathRandom();
return (ret) ? 0 : -1;
}