thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
repositoryTestRandomGenerator.h
1// -*- C++ -*-
2//
3// utilitiesTestSmearing.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 Leif Lonnblad, 2015 Marco A. Harrendorf
5//
6// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8//
9#ifndef ThePEG_Repository_Test_RandomGenerator_H
10#define ThePEG_Repository_Test_RandomGenerator_H
11
12#include <boost/test/unit_test.hpp>
13#include <boost/iterator/iterator_concepts.hpp>
14
15#include "ThePEG/Repository/RandomGenerator.h"
16#include <StandardRandom.h>
17
18
19
20/*
21 * Helper class to test generated random numbers
22 *
23 * The class checks how many random numbers are inside a pre-defined interval and how many outside
24 * This class is e.g. useful to check random numbers produced by a gaussian distribution
25 */
26template <typename Unit>
28 private:
29 // Do not accept missing interval limits
30 BinningCheck() {}
31 public:
32 // Define interval limits
33 BinningCheck(Unit lo, Unit hi)
34 : m_lo(lo), m_hi(hi), m_in(0), m_out(0) {}
35 ~BinningCheck() {}
36
37 int in() {return m_in;}
38 int out() {return m_out;}
39 int tot() {return m_out + m_in;}
40 // Check if random number is inside / outside interval and increase corresponding counter
41 void add(Unit randomNumber) {
42 if(randomNumber >= m_lo && randomNumber <= m_hi) {
43 m_in++;
44 } else {
45 m_out++;
46 }
47 }
48 // Reset counters
49 void reset() {m_in = 0; m_out = 0;}
50
51 private:
52 Unit m_lo, m_hi;
53 int m_in, m_out;
54};
57
58/*
59 * Start of BOOST unit tests for Helper class
60 *
61 */
62BOOST_AUTO_TEST_SUITE(HelperRandomNumberBinning)
63
64BOOST_AUTO_TEST_CASE(HelperDoubleBinning)
65{
66 DoubleBinCheck a(0, 1);
67 a.add(-1.1);
68 a.add(-0.1);
69 a.add(0.1);
70 a.add(0.5);
71 a.add(0.8);
72 a.add(1.1);
73 a.add(100);
74 BOOST_CHECK_EQUAL(a.tot(), 7);
75 BOOST_CHECK_EQUAL(a.in(), 3);
76 BOOST_CHECK_EQUAL(a.out(), 4);
77
78 a.reset();
79 BOOST_CHECK_EQUAL(a.tot(), 0);
80 BOOST_CHECK_EQUAL(a.in(), 0);
81 BOOST_CHECK_EQUAL(a.out(), 0);
82
83
84 DoubleBinCheck b(-1.5, 0.5);
85 b.add(-1.1);
86 b.add(-0.1);
87 b.add(0.1);
88 b.add(0.5);
89 b.add(0.8);
90 b.add(1.1);
91 b.add(100);
92 BOOST_CHECK_EQUAL(b.tot(), 7);
93 BOOST_CHECK_EQUAL(b.in(), 4);
94 BOOST_CHECK_EQUAL(b.out(), 3);
95}
96
97/*
98 * End of BOOST unit tests for Helper class
99 *
100 */
101BOOST_AUTO_TEST_SUITE_END()
102
103
104
105/*
106 * Local fix to provide randomGenerator object
107 *
108 */
109struct FixLocal1 {
110 FixLocal1() : rng() {
111 BOOST_TEST_MESSAGE( "setup local fixture for repositoryTestRandomGenerator" );
112 }
113
114 ~FixLocal1() {
115 BOOST_TEST_MESSAGE( "teardown local fixture for repositoryTestRandomGenerator" );
116 }
117
119};
120
121/*
122 * Start of boost unit tests for RandomGenerator.h
123 *
124 */
125BOOST_FIXTURE_TEST_SUITE(repositoryRandomGenerator, FixLocal1)
126
127/*
128 * Boost unit tests
129 *
130 */
131BOOST_AUTO_TEST_CASE(rndZeroToOne)
132{
133 int N = 1000;
134 // Check for whole interval
135 DoubleBinCheck posRange(0, 1);
136 // Check for flat distribution
137 DoubleBinCheck quarter1(0, 0.25);
138 DoubleBinCheck quarter3(0.5, 0.75);
139
140 // rnd function
141 for(int i = 0; i < N; ++i) {
142 posRange.add(rng.rnd());
143 quarter1.add(rng.rnd());
144 quarter3.add(rng.rnd());
145 }
146 // Prob laying inside of interval should be 1
147 BOOST_CHECK_EQUAL(posRange.tot(), N);
148 BOOST_CHECK_EQUAL(posRange.in(), N);
149 BOOST_CHECK_EQUAL(posRange.out(), 0);
150
151 // Prob laying inside of interval should be 0.25
152 BOOST_CHECK_EQUAL(quarter1.tot(), N);
153 BOOST_CHECK_CLOSE(quarter1.in(), 0.25 * N, 10);
154 BOOST_CHECK_CLOSE(quarter1.out(), 0.75 * N, 5);
155
156 // Prob laying inside of interval should be 0.25
157 BOOST_CHECK_EQUAL(quarter3.tot(), N);
158 BOOST_CHECK_CLOSE(quarter3.in(), 0.25 * N, 10);
159 BOOST_CHECK_CLOSE(quarter3.out(), 0.75 * N, 5);
160
161
162 // repeat for operator()
163 posRange.reset();
164 quarter1.reset();
165 quarter3.reset();
166 for(int i = 0; i < N; ++i) {
167 posRange.add(rng());
168 quarter1.add(rng());
169 quarter3.add(rng());
170 }
171 BOOST_CHECK_EQUAL(posRange.tot(), N);
172 BOOST_CHECK_EQUAL(posRange.in(), N);
173 BOOST_CHECK_EQUAL(posRange.out(), 0);
174
175 // Prob laying inside of interval should be 0.25
176 BOOST_CHECK_EQUAL(quarter1.tot(), N);
177 BOOST_CHECK_CLOSE(quarter1.in(), 0.25 * N, 10);
178 BOOST_CHECK_CLOSE(quarter1.out(), 0.75 * N, 5);
179
180 // Prob laying inside of interval should be 0.25
181 BOOST_CHECK_EQUAL(quarter3.tot(), N);
182 BOOST_CHECK_CLOSE(quarter3.in(), 0.25 * N, 10);
183 BOOST_CHECK_CLOSE(quarter3.out(), 0.75 * N, 5);
184}
185
186BOOST_AUTO_TEST_CASE(rndZeroTohi)
187{
188 int N = 1000;
189 double hi = 2.5;
190 // Check for whole interval
191 DoubleBinCheck zero2hi(0, 2.5);
192 // Check for flat distribution
193 DoubleBinCheck quarter1(0, 0.5);
194 DoubleBinCheck quarter3(1.5, 2.0);
195
196 // rnd function
197 for(int i = 0; i < N; ++i) {
198 zero2hi.add(rng.rnd(hi));
199 quarter1.add(rng.rnd(hi));
200 quarter3.add(rng.rnd(hi));
201 }
202 // Prob laying inside of interval should be 1
203 BOOST_CHECK_EQUAL(zero2hi.tot(), N);
204 BOOST_CHECK_EQUAL(zero2hi.in(), N);
205 BOOST_CHECK_EQUAL(zero2hi.out(), 0);
206
207 // Prob laying inside of interval should be 0.2
208 BOOST_CHECK_EQUAL(quarter1.tot(), N);
209 BOOST_CHECK_CLOSE(quarter1.in(), 0.2 * N, 10);
210 BOOST_CHECK_CLOSE(quarter1.out(), 0.8 * N, 5);
211
212 // Prob laying inside of interval should be 0.2
213 BOOST_CHECK_EQUAL(quarter3.tot(), N);
214 BOOST_CHECK_CLOSE(quarter3.in(), 0.2 * N, 10);
215 BOOST_CHECK_CLOSE(quarter3.out(), 0.8 * N, 5);
216
217
218 // repeat for operator(), note it is generally requiring a long!
219 zero2hi.reset();
220 quarter1.reset();
221 quarter3.reset();
222 for(int i = 0; i < N; ++i) {
223 zero2hi.add(rng(hi));
224 quarter1.add(rng(hi));
225 quarter3.add(rng(hi));
226 }
227 BOOST_CHECK_EQUAL(zero2hi.tot(), N);
228 BOOST_CHECK_EQUAL(zero2hi.in(), N);
229 BOOST_CHECK_EQUAL(zero2hi.out(), 0);
230
231 // Prob laying inside of interval should be 0.2
232 BOOST_CHECK_EQUAL(quarter1.tot(), N);
233 BOOST_CHECK_CLOSE(quarter1.in(), 0.2 * N, 10);
234 BOOST_CHECK_CLOSE(quarter1.out(), 0.8 * N, 5);
235
236 // Prob laying inside of interval should be 0.2
237 BOOST_CHECK_EQUAL(quarter3.tot(), N);
238 BOOST_CHECK_CLOSE(quarter3.in(), 0.2 * N, 10);
239 BOOST_CHECK_CLOSE(quarter3.out(), 0.8 * N, 5);
240
241 // repeat for operator(), note it is requiring a long!
242 long hiTwo = 3;
243 // Check for whole interval
244 LongBinCheck zero2hiTwo(0, hiTwo);
245 // Check for flat distribution
246 LongBinCheck quarter1Two(1, 1);
247 LongBinCheck quarter3Two(2, 2);
248
249 // operator()(long N)
250 for(int i = 0; i < N; ++i) {
251 zero2hiTwo.add(rng(hiTwo));
252 quarter1Two.add(rng(hiTwo));
253 quarter3Two.add(rng(hiTwo));
254 }
255 // Prob laying inside of interval should be 1
256 BOOST_CHECK_EQUAL(zero2hiTwo.tot(), N);
257 BOOST_CHECK_EQUAL(zero2hiTwo.in(), N);
258 BOOST_CHECK_EQUAL(zero2hiTwo.out(), 0);
259
260 // Prob laying inside of interval should be 0.333
261 BOOST_CHECK_EQUAL(quarter1Two.tot(), N);
262 BOOST_CHECK_CLOSE(quarter1Two.in(), 0.333 * N, 10);
263 BOOST_CHECK_CLOSE(quarter1Two.out(), 0.666 * N, 5);
264
265 // Prob laying inside of interval should be 0.333
266 BOOST_CHECK_EQUAL(quarter3Two.tot(), N);
267 BOOST_CHECK_CLOSE(quarter3Two.in(), 0.333 * N, 10);
268 BOOST_CHECK_CLOSE(quarter3Two.out(), 0.666 * N, 5);
269}
270
271BOOST_AUTO_TEST_CASE(rndIntervallLowerLimitTohi)
272{
273 int N = 1000;
274 double lo = -1.5;
275 double hi = 2.5;
276 // Check for whole interval
277 DoubleBinCheck zero2hi(lo, hi);
278 // Check for flat distribution
279 DoubleBinCheck quarter1(-0.5, 0.5);
280 DoubleBinCheck quarter3(1.5, 2.5);
281
282 // rnd function
283 for(int i = 0; i < N; ++i) {
284 zero2hi.add(rng.rnd(lo, hi));
285 quarter1.add(rng.rnd(lo, hi));
286 quarter3.add(rng.rnd(lo, hi));
287 }
288 // Prob laying inside of interval should be 1
289 BOOST_CHECK_EQUAL(zero2hi.tot(), N);
290 BOOST_CHECK_EQUAL(zero2hi.in(), N);
291 BOOST_CHECK_EQUAL(zero2hi.out(), 0);
292
293 // Prob laying inside of interval should be 0.25
294 BOOST_CHECK_EQUAL(quarter1.tot(), N);
295 BOOST_CHECK_CLOSE(quarter1.in(), 0.25 * N, 10);
296 BOOST_CHECK_CLOSE(quarter1.out(), 0.75 * N, 5);
297
298 // Prob laying inside of interval should be 0.25
299 BOOST_CHECK_EQUAL(quarter3.tot(), N);
300 BOOST_CHECK_CLOSE(quarter3.in(), 0.25 * N, 10);
301 BOOST_CHECK_CLOSE(quarter3.out(), 0.75 * N, 5);
302}
303
304BOOST_AUTO_TEST_CASE(rndZeroToOneVector)
305{
306 int N = 10;
307 int L = 10;
308 // Check for whole interval
309 DoubleBinCheck posRange(0, 1);
310 // Check for flat distribution
311 DoubleBinCheck quarter1(0, 0.25);
312 DoubleBinCheck quarter3(0.5, 0.75);
313
314 for(int i = 0; i < N; ++i) {
315 posRange.reset();
316 quarter1.reset();
317 quarter3.reset();
318
319 std::vector<double> rndvec = rng.rndvec(L);
320 BOOST_CHECK_EQUAL(static_cast<int>(rndvec.size()), L);
321 for(int j = 0; j < static_cast<int>(rndvec.size()); ++j) {
322 posRange.add(rndvec[j]);
323 quarter1.add(rndvec[j]);
324 quarter3.add(rndvec[j]);
325 }
326 // Prob laying inside of interval should be 1
327 BOOST_CHECK_EQUAL(posRange.tot(), N);
328 BOOST_CHECK_EQUAL(posRange.in(), N);
329 BOOST_CHECK_EQUAL(posRange.out(), 0);
330
331 // Prob laying inside of interval should be 0.25
332 BOOST_CHECK_EQUAL(quarter1.tot(), N);
333 BOOST_CHECK_CLOSE(quarter1.in(), 0.25 * L, 300);
334 BOOST_CHECK_CLOSE(quarter1.out(), 0.75 * L, 300);
335
336 // Prob laying inside of interval should be 0.25
337 BOOST_CHECK_EQUAL(quarter3.tot(), N);
338 BOOST_CHECK_CLOSE(quarter3.in(), 0.25 * L, 300);
339 BOOST_CHECK_CLOSE(quarter3.out(), 0.75 * L, 300);
340 }
341}
342
343BOOST_AUTO_TEST_CASE(rndBoolSingleProbability)
344{
345 int N = 1000;
346 // Check for whole interval
347 LongBinCheck posRange(0, 1);
348 // Check for flat distribution
349 LongBinCheck zero(0, 0);
350 LongBinCheck one(1, 1);
351
352 // rndbool function, prob should be 0.5
353 for(int i = 0; i < N; ++i) {
354 posRange.add(rng.rndbool());
355 zero.add(rng.rndbool());
356 one.add(rng.rndbool());
357 }
358 // Prob laying inside of interval should be 1
359 BOOST_CHECK_EQUAL(posRange.tot(), N);
360 BOOST_CHECK_EQUAL(posRange.in(), N);
361 BOOST_CHECK_EQUAL(posRange.out(), 0);
362
363 // Prob laying inside of interval should be 0.5
364 BOOST_CHECK_EQUAL(zero.tot(), N);
365 BOOST_CHECK_CLOSE(zero.in(), 0.5 * N, 10);
366 BOOST_CHECK_CLOSE(zero.out(), 0.5 * N, 10);
367
368 // Prob laying inside of interval should be 0.5
369 BOOST_CHECK_EQUAL(one.tot(), N);
370 BOOST_CHECK_CLOSE(one.in(), 0.5 * N, 10);
371 BOOST_CHECK_CLOSE(one.out(), 0.5 * N, 10);
372
373
374 // Check for whole interval
375 LongBinCheck posRangeTwo(0, 1);
376 // Check for flat distribution
377 LongBinCheck midPoint(0, 0);
378 LongBinCheck hiEdge(1, 1);
379
380 // rndbool function, prob should be now 0.4
381 for(int i = 0; i < N; ++i) {
382 posRangeTwo.add(rng.rndbool(0.4));
383 midPoint.add(rng.rndbool(0.4));
384 hiEdge.add(rng.rndbool(0.4));
385 }
386 // Prob laying inside of interval should be 1
387 BOOST_CHECK_EQUAL(posRangeTwo.tot(), N);
388 BOOST_CHECK_EQUAL(posRangeTwo.in(), N);
389 BOOST_CHECK_EQUAL(posRangeTwo.out(), 0);
390
391 // Prob laying inside of interval should be 0.6
392 BOOST_CHECK_EQUAL(midPoint.tot(), N);
393 BOOST_CHECK_CLOSE(midPoint.in(), 0.6 * N, 10);
394 BOOST_CHECK_CLOSE(midPoint.out(), 0.4 * N, 10);
395
396 // Prob laying inside of interval should be 0.4
397 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
398 BOOST_CHECK_CLOSE(hiEdge.in(), 0.4 * N, 10);
399 BOOST_CHECK_CLOSE(hiEdge.out(), 0.6 * N, 10);
400}
401
402BOOST_AUTO_TEST_CASE(prndBoolSingleProbability)
403{
404 int N = 1000;
405 // Check for whole interval
406 LongBinCheck posRange(0, 1);
407 // Check for flat distribution
408 LongBinCheck zero(0, 0);
409 LongBinCheck one(1, 1);
410
411 // rndbool function, prob should be 0.5
412 for(int i = 0; i < N; ++i) {
413 posRange.add(rng.prndbool());
414 zero.add(rng.prndbool());
415 one.add(rng.prndbool());
416 }
417 // Prob laying inside of interval should be 1
418 BOOST_CHECK_EQUAL(posRange.tot(), N);
419 BOOST_CHECK_EQUAL(posRange.in(), N);
420 BOOST_CHECK_EQUAL(posRange.out(), 0);
421
422 // Prob laying inside of interval should be 0.5
423 BOOST_CHECK_EQUAL(zero.tot(), N);
424 BOOST_CHECK_CLOSE(zero.in(), 0.5 * N, 15);
425 BOOST_CHECK_CLOSE(zero.out(), 0.5 * N, 15);
426
427 // Prob laying inside of interval should be 0.5
428 BOOST_CHECK_EQUAL(one.tot(), N);
429 BOOST_CHECK_CLOSE(one.in(), 0.5 * N, 15);
430 BOOST_CHECK_CLOSE(one.out(), 0.5 * N, 15);
431
432
433 // Check for whole interval
434 LongBinCheck posRangeTwo(0, 1);
435 // Check for flat distribution
436 LongBinCheck midPoint(0, 0);
437 LongBinCheck hiEdge(1, 1);
438
439 // rndbool function, prob should be now 0.4
440 for(int i = 0; i < N; ++i) {
441 posRangeTwo.add(rng.prndbool(0.4));
442 midPoint.add(rng.prndbool(0.4));
443 hiEdge.add(rng.prndbool(0.4));
444 }
445 // Prob laying inside of interval should be 1
446 BOOST_CHECK_EQUAL(posRangeTwo.tot(), N);
447 BOOST_CHECK_EQUAL(posRangeTwo.in(), N);
448 BOOST_CHECK_EQUAL(posRangeTwo.out(), 0);
449
450 // Prob laying inside of interval should be 0.6
451 BOOST_CHECK_EQUAL(midPoint.tot(), N);
452 BOOST_CHECK_CLOSE(midPoint.in(), 0.6 * N, 10);
453 BOOST_CHECK_CLOSE(midPoint.out(), 0.4 * N, 10);
454
455 // Prob laying inside of interval should be 0.4
456 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
457 BOOST_CHECK_CLOSE(hiEdge.in(), 0.4 * N, 10);
458 BOOST_CHECK_CLOSE(hiEdge.out(), 0.6 * N, 10);
459}
460
461BOOST_AUTO_TEST_CASE(rndBoolTwoProbabilities)
462{
463 int N = 1000;
464 double p1 = 0.2;
465 double p2 = 0.3;
466 // Check for whole interval
467 LongBinCheck posRange(0, 1);
468 // Check for flat distribution
469 LongBinCheck zero(0, 0);
470 LongBinCheck one(1, 1);
471
472 for(int i = 0; i < N; ++i) {
473 posRange.add(rng.rndbool(p1, p2));
474 zero.add(rng.rndbool(p1, p2));
475 one.add(rng.rndbool(p1, p2));
476 }
477 // Prob laying inside of interval should be 1
478 BOOST_CHECK_EQUAL(posRange.tot(), N);
479 BOOST_CHECK_EQUAL(posRange.in(), N);
480 BOOST_CHECK_EQUAL(posRange.out(), 0);
481
482 // Prob laying inside of interval should be 0.6
483 BOOST_CHECK_EQUAL(zero.tot(), N);
484 BOOST_CHECK_CLOSE(zero.in(), 0.6 * N, 10);
485 BOOST_CHECK_CLOSE(zero.out(), 0.4 * N, 10);
486
487 // Prob laying inside of interval should be 0.2/0.5 = 0.4
488 BOOST_CHECK_EQUAL(one.tot(), N);
489 BOOST_CHECK_CLOSE(one.in(), 0.4 * N, 10);
490 BOOST_CHECK_CLOSE(one.out(), 0.6 * N, 10);
491}
492
493BOOST_AUTO_TEST_CASE(prndBoolTwoProbabilities)
494{
495 int N = 1000;
496 double p1 = 0.2;
497 double p2 = 0.3;
498 // Check for whole interval
499 LongBinCheck posRange(0, 1);
500 // Check for flat distribution
501 LongBinCheck zero(0, 0);
502 LongBinCheck one(1, 1);
503
504 for(int i = 0; i < N; ++i) {
505 posRange.add(rng.prndbool(p1, p2));
506 zero.add(rng.prndbool(p1, p2));
507 one.add(rng.prndbool(p1, p2));
508 }
509 // Prob laying inside of interval should be 1
510 BOOST_CHECK_EQUAL(posRange.tot(), N);
511 BOOST_CHECK_EQUAL(posRange.in(), N);
512 BOOST_CHECK_EQUAL(posRange.out(), 0);
513
514 // Prob laying inside of interval should be 0.6
515 BOOST_CHECK_EQUAL(zero.tot(), N);
516 BOOST_CHECK_CLOSE(zero.in(), 0.6 * N, 10);
517 BOOST_CHECK_CLOSE(zero.out(), 0.4 * N, 10);
518
519 // Prob laying inside of interval should be 0.2/0.5 = 0.4
520 BOOST_CHECK_EQUAL(one.tot(), N);
521 BOOST_CHECK_CLOSE(one.in(), 0.4 * N, 10);
522 BOOST_CHECK_CLOSE(one.out(), 0.6 * N, 10);
523}
524
525BOOST_AUTO_TEST_CASE(rndSign)
526{
527 int N = 1000;
528 double p1 = 0.4;
529 double p2 = 0.2;
530 double p3 = 0.3;
531 // Check for whole interval
532 LongBinCheck range(-1, 1);
533 // Check for flat distribution
534 LongBinCheck lowEdge(-1, -1);
535 LongBinCheck midPoint(0, 0);
536 LongBinCheck hiEdge(1, 1);
537
538 for(int i = 0; i < N; ++i) {
539 range.add(rng.rndsign(p1, p2, p3));
540 lowEdge.add(rng.rndsign(p1, p2, p3));
541 midPoint.add(rng.rndsign(p1, p2, p3));
542 hiEdge.add(rng.rndsign(p1, p2, p3));
543 }
544 // Prob laying inside of interval should be 1
545 BOOST_CHECK_EQUAL(range.tot(), N);
546 BOOST_CHECK_EQUAL(range.in(), N);
547 BOOST_CHECK_EQUAL(range.out(), 0);
548
549 // Prob laying inside of interval should be 0.4
550 BOOST_CHECK_EQUAL(lowEdge.tot(), N);
551 BOOST_CHECK_CLOSE(lowEdge.in(), 0.4/0.9 * N, 10);
552 BOOST_CHECK_CLOSE(lowEdge.out(), 0.5/0.9 * N, 10);
553
554 // Prob laying inside of interval should be 0.2
555 BOOST_CHECK_EQUAL(midPoint.tot(), N);
556 BOOST_CHECK_CLOSE(midPoint.in(), 0.2/0.9 * N, 10);
557 BOOST_CHECK_CLOSE(midPoint.out(), 0.7/0.9 * N, 10);
558
559 // Prob laying inside of interval should be 0.2
560 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
561 BOOST_CHECK_CLOSE(hiEdge.in(), 0.3/0.9 * N, 10);
562 BOOST_CHECK_CLOSE(hiEdge.out(), 0.6/0.9 * N, 10);
563}
564
565BOOST_AUTO_TEST_CASE(prndSign)
566{
567 int N = 1000;
568 double p1 = 0.4;
569 double p2 = 0.2;
570 double p3 = 0.3;
571 // Check for whole interval
572 LongBinCheck range(-1, 1);
573 // Check for flat distribution
574 LongBinCheck lowEdge(-1, -1);
575 LongBinCheck midPoint(0, 0);
576 LongBinCheck hiEdge(1, 1);
577
578 for(int i = 0; i < N; ++i) {
579 range.add(rng.prndsign(p1, p2, p3));
580 lowEdge.add(rng.prndsign(p1, p2, p3));
581 midPoint.add(rng.prndsign(p1, p2, p3));
582 hiEdge.add(rng.prndsign(p1, p2, p3));
583 }
584 // Prob laying inside of interval should be 1
585 BOOST_CHECK_EQUAL(range.tot(), N);
586 BOOST_CHECK_EQUAL(range.in(), N);
587 BOOST_CHECK_EQUAL(range.out(), 0);
588
589 // Prob laying inside of interval should be 0.4
590 BOOST_CHECK_EQUAL(lowEdge.tot(), N);
591 BOOST_CHECK_CLOSE(lowEdge.in(), 0.4/0.9 * N, 10);
592 BOOST_CHECK_CLOSE(lowEdge.out(), 0.5/0.9 * N, 10);
593
594 // Prob laying inside of interval should be 0.2
595 BOOST_CHECK_EQUAL(midPoint.tot(), N);
596 BOOST_CHECK_CLOSE(midPoint.in(), 0.2/0.9 * N, 10);
597 BOOST_CHECK_CLOSE(midPoint.out(), 0.7/0.9 * N, 10);
598
599 // Prob laying inside of interval should be 0.2
600 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
601 BOOST_CHECK_CLOSE(hiEdge.in(), 0.3/0.9 * N, 10);
602 BOOST_CHECK_CLOSE(hiEdge.out(), 0.6/0.9 * N, 10);
603}
604
605BOOST_AUTO_TEST_CASE(rnd2)
606{
607 int N = 1000;
608 double p1 = 0.4;
609 double p2 = 0.2;
610 // Check for whole interval
611 LongBinCheck posRange(0, 1);
612 // Check for flat distribution
613 LongBinCheck midPoint(0, 0);
614 LongBinCheck hiEdge(1, 1);
615
616 for(int i = 0; i < N; ++i) {
617 posRange.add(rng.rnd2(p1, p2));
618 midPoint.add(rng.rnd2(p1, p2));
619 hiEdge.add(rng.rnd2(p1, p2));
620 }
621 // Prob laying inside of interval should be 1
622 BOOST_CHECK_EQUAL(posRange.tot(), N);
623 BOOST_CHECK_EQUAL(posRange.in(), N);
624 BOOST_CHECK_EQUAL(posRange.out(), 0);
625
626 // Prob laying inside of interval should be 0.666
627 BOOST_CHECK_EQUAL(midPoint.tot(), N);
628 BOOST_CHECK_CLOSE(midPoint.in(), 0.666 * N, 10);
629 BOOST_CHECK_CLOSE(midPoint.out(), 0.333 * N, 10);
630
631 // Prob laying inside of interval should be 0.333
632 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
633 BOOST_CHECK_CLOSE(hiEdge.in(), 0.333 * N, 10);
634 BOOST_CHECK_CLOSE(hiEdge.out(), 0.666 * N, 10);
635}
636
637BOOST_AUTO_TEST_CASE(rnd3)
638{
639 int N = 1000;
640 double p1 = 0.4;
641 double p2 = 0.2;
642 double p3 = 0.3;
643 // Check for whole interval
644 LongBinCheck zero2two(0, 2);
645 // Check for flat distribution
646 LongBinCheck midPoint(0, 0);
647 LongBinCheck hiEdge(1, 1);
648 LongBinCheck two(2, 2);
649
650 for(int i = 0; i < N; ++i) {
651 zero2two.add(rng.rnd3(p1, p2, p3));
652 midPoint.add(rng.rnd3(p1, p2, p3));
653 hiEdge.add(rng.rnd3(p1, p2, p3));
654 two.add(rng.rnd3(p1, p2, p3));
655 }
656 // Prob laying inside of interval should be 1
657 BOOST_CHECK_EQUAL(zero2two.tot(), N);
658 BOOST_CHECK_EQUAL(zero2two.in(), N);
659 BOOST_CHECK_EQUAL(zero2two.out(), 0);
660
661 // Prob laying inside of interval should be 0.4 / 0.9
662 BOOST_CHECK_EQUAL(midPoint.tot(), N);
663 BOOST_CHECK_CLOSE(midPoint.in(), 0.4/0.9 * N, 10);
664 BOOST_CHECK_CLOSE(midPoint.out(), 0.5/0.9 * N, 10);
665
666 // Prob laying inside of interval should be 0.2 / 0.9
667 BOOST_CHECK_EQUAL(hiEdge.tot(), N);
668 BOOST_CHECK_CLOSE(hiEdge.in(), 0.2/0.9 * N, 10);
669 BOOST_CHECK_CLOSE(hiEdge.out(), 0.7/0.9 * N, 10);
670
671 // Prob laying inside of interval should be 0.3 / 0.9
672 BOOST_CHECK_EQUAL(two.tot(), N);
673 BOOST_CHECK_CLOSE(two.in(), 0.3/0.9 * N, 10);
674 BOOST_CHECK_CLOSE(two.out(), 0.6/0.9 * N, 10);
675}
676
677BOOST_AUTO_TEST_CASE(rnd4)
678{
679 int N = 1000;
680 double p1 = 0.4;
681 double p2 = 0.2;
682 double p3 = 0.3;
683 double p4 = 0.5;
684 // Check for whole interval
685 LongBinCheck bin_03(0, 3);
686 // Check for flat distribution
687 LongBinCheck bin0(0, 0);
688 LongBinCheck bin1(1, 1);
689 LongBinCheck bin2(2, 2);
690 LongBinCheck bin3(3, 3);
691
692 for(int i = 0; i < N; ++i) {
693 bin_03.add(rng.rnd4(p1, p2, p3, p4));
694 bin0.add(rng.rnd4(p1, p2, p3, p4));
695 bin1.add(rng.rnd4(p1, p2, p3, p4));
696 bin2.add(rng.rnd4(p1, p2, p3, p4));
697 bin3.add(rng.rnd4(p1, p2, p3, p4));
698 }
699 // Prob laying inside of interval should be 1
700 BOOST_CHECK_EQUAL(bin_03.tot(), N);
701 BOOST_CHECK_EQUAL(bin_03.in(), N);
702 BOOST_CHECK_EQUAL(bin_03.out(), 0);
703
704 // Prob laying inside of interval should be 0.4 / 1.4
705 BOOST_CHECK_EQUAL(bin0.tot(), N);
706 BOOST_CHECK_CLOSE(bin0.in(), 0.4/1.4 * N, 10);
707 BOOST_CHECK_CLOSE(bin0.out(), 1.0/1.4 * N, 10);
708
709 // Prob laying inside of interval should be 0.2 / 1.4
710 BOOST_CHECK_EQUAL(bin1.tot(), N);
711 BOOST_CHECK_CLOSE(bin1.in(), 0.2/1.4 * N, 10);
712 BOOST_CHECK_CLOSE(bin1.out(), 1.2/1.4 * N, 10);
713
714 // Prob laying inside of interval should be 0.3 / 1.4
715 BOOST_CHECK_EQUAL(bin2.tot(), N);
716 BOOST_CHECK_CLOSE(bin2.in(), 0.3/1.4 * N, 10);
717 BOOST_CHECK_CLOSE(bin2.out(), 1.1/1.4 * N, 10);
718
719 // Prob laying inside of interval should be 0.5 / 1.4
720 BOOST_CHECK_EQUAL(bin3.tot(), N);
721 BOOST_CHECK_CLOSE(bin3.in(), 0.5/1.4 * N, 10);
722 BOOST_CHECK_CLOSE(bin3.out(), 0.9/1.4 * N, 10);
723}
724
725BOOST_AUTO_TEST_CASE(rndExp)
726{
727 int N = 1000;
728 // Check for whole interval
729 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
730 // Check for median
731 DoubleBinCheck firstHalf(-DBL_MAX, log(2));
732 DoubleBinCheck secondHalf(log(2), DBL_MAX);
733 // Check for increasing probability
734 DoubleBinCheck neg1000(-1000, 0);
735 DoubleBinCheck pos1000(0, 1000);
736
737 for(int i = 0; i < N; ++i) {
738 allnums.add(rng.rndExp());
739 firstHalf.add(rng.rndExp());
740 secondHalf.add(rng.rndExp());
741 neg1000.add(rng.rndExp());
742 pos1000.add(rng.rndExp());
743 }
744 // Prob laying inside of interval should be 1
745 BOOST_CHECK_EQUAL(allnums.tot(), N);
746 BOOST_CHECK_EQUAL(allnums.in(), N);
747 BOOST_CHECK_EQUAL(allnums.out(), 0);
748
749 // Prob laying inside of interval should be 0.5
750 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
751 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 15);
752 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 15);
753
754 // Prob laying inside of interval should be 0.5
755 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
756 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 15);
757 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 15);
758
759 // Increasing probability
760 BOOST_CHECK(neg1000.in() <= pos1000.in());
761}
762
763BOOST_AUTO_TEST_CASE(rndExpMean)
764{
765 int N = 1000;
766 double meanValue = 5;
767 // Check for whole interval
768 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
769 // Check for median
770 DoubleBinCheck firstHalf(-DBL_MAX, log(2) * meanValue);
771 DoubleBinCheck secondHalf(log(2) * meanValue, DBL_MAX);
772 // Check for increasing probability
773 DoubleBinCheck neg1000(-1000, 0);
774 DoubleBinCheck pos1000(0, 1000);
775
776 for(int i = 0; i < N; ++i) {
777 allnums.add(rng.rndExp(meanValue));
778 firstHalf.add(rng.rndExp(meanValue));
779 secondHalf.add(rng.rndExp(meanValue));
780 neg1000.add(rng.rndExp(meanValue));
781 pos1000.add(rng.rndExp(meanValue));
782 }
783 // Prob laying inside of interval should be 1
784 BOOST_CHECK_EQUAL(allnums.tot(), N);
785 BOOST_CHECK_EQUAL(allnums.in(), N);
786 BOOST_CHECK_EQUAL(allnums.out(), 0);
787
788 // Prob laying inside of interval should be 0.5
789 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
790 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 15);
791 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 15);
792
793 // Prob laying inside of interval should be 0.5
794 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
795 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 15);
796 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 15);
797
798 // Increasing probability
799 BOOST_CHECK(neg1000.in() <= pos1000.in());
800}
801
802BOOST_AUTO_TEST_CASE(rndGauss)
803{
804 int N = 1000;
805
806 // Check for whole interval
807 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
808 // Check for median
809 DoubleBinCheck firstHalf(-DBL_MAX, 0);
810 DoubleBinCheck secondHalf(0, DBL_MAX);
811 // Check for increasing probability
812 DoubleBinCheck oneSigma(-1, 1);
813 DoubleBinCheck threeSigma(-3, 3);
814 DoubleBinCheck eightSigma(-8, 8);
815
816 for(int i = 0; i < N; ++i) {
817 allnums.add(rng.rndGauss());
818 firstHalf.add(rng.rndGauss());
819 secondHalf.add(rng.rndGauss());
820 oneSigma.add(rng.rndGauss());
821 threeSigma.add(rng.rndGauss());
822 eightSigma.add(rng.rndGauss());
823 }
824
825 // Prob laying inside of interval should be 1
826 BOOST_CHECK_EQUAL(allnums.tot(), N);
827 BOOST_CHECK_EQUAL(allnums.in(), N);
828 BOOST_CHECK_EQUAL(allnums.out(), 0);
829
830 // Prob laying inside of interval should be 0.5
831 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
832 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 15);
833 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 15);
834
835 // Prob laying inside of interval should be 0.5
836 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
837 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 15);
838 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 15);
839
840 // Prob laying inside of interval should be 0.68
841 BOOST_CHECK_EQUAL(oneSigma.tot(), N);
842 BOOST_CHECK_CLOSE(oneSigma.in(), 0.68 * N, 15);
843 BOOST_CHECK_CLOSE(oneSigma.out(), 0.32 * N, 15);
844
845 // Prob laying inside of interval should be 99,730 0204%
846 BOOST_CHECK_EQUAL(threeSigma.tot(), N);
847 BOOST_CHECK_CLOSE(threeSigma.in(), 0.99730 * N, 15);
848
849 // Prob laying inside of interval should be 99,999 999 999 %
850 BOOST_CHECK_EQUAL(eightSigma.tot(), N);
851 BOOST_CHECK_CLOSE(eightSigma.in(), 0.9999 * N, 15);
852}
853
854BOOST_AUTO_TEST_CASE(rndGaussMeanSigma)
855{
856 int N = 1000;
857
858 for(double mean = 1; mean < 5; mean += 1) {
859 for(double sigma = 0.25; sigma < 0.65; sigma += 0.1) {
860 // Check for whole interval
861 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
862 // Check for median
863 DoubleBinCheck firstHalf(-DBL_MAX, mean);
864 DoubleBinCheck secondHalf(mean, DBL_MAX);
865 // Check for increasing probability
866 DoubleBinCheck oneSigma(mean - sigma, mean + sigma);
867 DoubleBinCheck threeSigma(mean - 3 * sigma, mean + 3 * sigma);
868 DoubleBinCheck eightSigma(mean - 8 * sigma, mean + 8 * sigma);
869
870 for(int i = 0; i < N; ++i) {
871 allnums.add(rng.rndGauss(sigma, mean));
872 firstHalf.add(rng.rndGauss(sigma, mean));
873 secondHalf.add(rng.rndGauss(sigma, mean));
874 oneSigma.add(rng.rndGauss(sigma, mean));
875 threeSigma.add(rng.rndGauss(sigma, mean));
876 eightSigma.add(rng.rndGauss(sigma, mean));
877 }
878
879 // Prob laying inside of interval should be 1
880 BOOST_CHECK_EQUAL(allnums.tot(), N);
881 BOOST_CHECK_EQUAL(allnums.in(), N);
882 BOOST_CHECK_EQUAL(allnums.out(), 0);
883
884 // Prob laying inside of interval should be 0.5
885 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
886 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 15);
887 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 15);
888
889 // Prob laying inside of interval should be 0.5
890 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
891 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 15);
892 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 15);
893
894 // Prob laying inside of interval should be 0.68
895 BOOST_CHECK_EQUAL(oneSigma.tot(), N);
896 BOOST_CHECK_CLOSE(oneSigma.in(), 0.68 * N, 15);
897 BOOST_CHECK_CLOSE(oneSigma.out(), 0.32 * N, 15);
898
899 // Prob laying inside of interval should be 99,730 0204%
900 BOOST_CHECK_EQUAL(threeSigma.tot(), N);
901 BOOST_CHECK_CLOSE(threeSigma.in(), 0.99730 * N, 15);
902
903 // Prob laying inside of interval should be 99,999 999 999 %
904 BOOST_CHECK_EQUAL(eightSigma.tot(), N);
905 BOOST_CHECK_CLOSE(eightSigma.in(), 0.9999 * N, 15);
906 }
907 }
908}
909
910
911BOOST_AUTO_TEST_CASE(rndGaussTwoNumbers)
912{
913 int N = 1000;
914
915 // Check for whole interval
916 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
917 // Check for median
918 DoubleBinCheck firstHalf(-DBL_MAX, 0);
919 DoubleBinCheck secondHalf(0, DBL_MAX);
920 // Check for increasing probability
921 DoubleBinCheck oneSigma(-1, 1);
922 DoubleBinCheck threeSigma(-3, 3);
923 DoubleBinCheck eightSigma(-8, 8);
924
925 double r1, r2;
926 for(int i = 0; i < N; ++i) {
927 rng.rndGaussTwoNumbers(r1, r2);
928
929 allnums.add(r1);
930 firstHalf.add(r1);
931 secondHalf.add(r1);
932 oneSigma.add(r1);
933 threeSigma.add(r1);
934 eightSigma.add(r1);
935
936 allnums.add(r2);
937 firstHalf.add(r2);
938 secondHalf.add(r2);
939 oneSigma.add(r2);
940 threeSigma.add(r2);
941 eightSigma.add(r2);
942 }
943
944 // Two random numbers were added in each round
945 N *= 2;
946 // Prob laying inside of interval should be 1
947 BOOST_CHECK_EQUAL(allnums.tot(), N);
948 BOOST_CHECK_EQUAL(allnums.in(), N);
949 BOOST_CHECK_EQUAL(allnums.out(), 0);
950
951 // Prob laying inside of interval should be 0.5
952 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
953 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 15);
954 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 15);
955
956 // Prob laying inside of interval should be 0.5
957 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
958 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 15);
959 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 15);
960
961 // Prob laying inside of interval should be 0.68
962 BOOST_CHECK_EQUAL(oneSigma.tot(), N);
963 BOOST_CHECK_CLOSE(oneSigma.in(), 0.68 * N, 15);
964 BOOST_CHECK_CLOSE(oneSigma.out(), 0.32 * N, 15);
965
966 // Prob laying inside of interval should be 99,730 0204%
967 BOOST_CHECK_EQUAL(threeSigma.tot(), N);
968 BOOST_CHECK_CLOSE(threeSigma.in(), 0.99730 * N, 15);
969
970 // Prob laying inside of interval should be 99,999 999 999 %
971 BOOST_CHECK_EQUAL(eightSigma.tot(), N);
972 BOOST_CHECK_CLOSE(eightSigma.in(), 0.9999 * N, 15);
973}
974
975BOOST_AUTO_TEST_CASE(rndGaussTwoNumbersMeanSigma)
976{
977 int N = 1000;
978 // Two random numbers will be added in each round
979 int NTwo = 2 * N;
980 double r1, r2;
981
982 for(double mean = 1; mean < 5; mean += 1) {
983 for(double sigma = 0.25; sigma < 0.65; sigma += 0.1) {
984 // Check for whole interval
985 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
986 // Check for median
987 DoubleBinCheck firstHalf(-DBL_MAX, mean);
988 DoubleBinCheck secondHalf(mean, DBL_MAX);
989 // Check for increasing probability
990 DoubleBinCheck oneSigma(mean - sigma, mean + sigma);
991 DoubleBinCheck threeSigma(mean - 3 * sigma, mean + 3 * sigma);
992 DoubleBinCheck eightSigma(mean - 8 * sigma, mean + 8 * sigma);
993
994 for(int i = 0; i < N; ++i) {
995 rng.rndGaussTwoNumbers(r1, r2, sigma, mean);
996
997 allnums.add(r1);
998 firstHalf.add(r1);
999 secondHalf.add(r1);
1000 oneSigma.add(r1);
1001 threeSigma.add(r1);
1002 eightSigma.add(r1);
1003
1004 allnums.add(r2);
1005 firstHalf.add(r2);
1006 secondHalf.add(r2);
1007 oneSigma.add(r2);
1008 threeSigma.add(r2);
1009 eightSigma.add(r2);
1010 }
1011
1012 // Prob laying inside of interval should be 1
1013 BOOST_CHECK_EQUAL(allnums.tot(), NTwo);
1014 BOOST_CHECK_EQUAL(allnums.in(), NTwo);
1015 BOOST_CHECK_EQUAL(allnums.out(), 0);
1016
1017 // Prob laying inside of interval should be 0.5
1018 BOOST_CHECK_EQUAL(firstHalf.tot(), NTwo);
1019 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * NTwo, 15);
1020 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * NTwo, 15);
1021
1022 // Prob laying inside of interval should be 0.5
1023 BOOST_CHECK_EQUAL(secondHalf.tot(), NTwo);
1024 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * NTwo, 15);
1025 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * NTwo, 15);
1026
1027 // Prob laying inside of interval should be 0.68
1028 BOOST_CHECK_EQUAL(oneSigma.tot(), NTwo);
1029 BOOST_CHECK_CLOSE(oneSigma.in(), 0.68 * NTwo, 15);
1030 BOOST_CHECK_CLOSE(oneSigma.out(), 0.32 * NTwo, 15);
1031
1032 // Prob laying inside of interval should be 99,730 0204%
1033 BOOST_CHECK_EQUAL(threeSigma.tot(), NTwo);
1034 BOOST_CHECK_CLOSE(threeSigma.in(), 0.99730 * NTwo, 15);
1035
1036 // Prob laying inside of interval should be 99,999 999 999 %
1037 BOOST_CHECK_EQUAL(eightSigma.tot(), NTwo);
1038 BOOST_CHECK_CLOSE(eightSigma.in(), 0.9999 * NTwo, 15);
1039 }
1040 }
1041}
1042
1043BOOST_AUTO_TEST_CASE(rndPoisson)
1044{
1045 int N = 1000;
1046
1047 for(double mean = 5; mean < 21; mean += 5) {
1048 // Check for whole interval
1049 LongBinCheck zero2max(0, LONG_MAX);
1050 // Check for median
1051 LongBinCheck zero(0, mean);
1052 LongBinCheck secondHalf(mean, LONG_MAX);
1053 // Check for decreasing probability away from mean
1054 LongBinCheck nearMean(mean - std::sqrt(mean), mean + std::sqrt(mean));
1055 LongBinCheck awayMean(mean + std::sqrt(mean), mean + 3 * std::sqrt(mean));
1056
1057 for(int i = 0; i < N; ++i) {
1058 zero2max.add(rng.rndPoisson(mean));
1059 zero.add(rng.rndPoisson(mean));
1060 secondHalf.add(rng.rndPoisson(mean));
1061 nearMean.add(rng.rndPoisson(mean));
1062 awayMean.add(rng.rndPoisson(mean));
1063 }
1064
1065 // Prob laying inside of interval should be 1
1066 BOOST_CHECK_EQUAL(zero2max.tot(), N);
1067 BOOST_CHECK_EQUAL(zero2max.in(), N);
1068 BOOST_CHECK_EQUAL(zero2max.out(), 0);
1069
1070 // Prob laying inside of interval should be 0.5
1071 BOOST_CHECK_EQUAL(zero.tot(), N);
1072 BOOST_CHECK_CLOSE(zero.in(), 0.5 * N, 30);
1073 BOOST_CHECK_CLOSE(zero.out(), 0.5 * N, 30);
1074
1075 // Prob laying inside of interval should be 0.5
1076 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
1077 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 30);
1078 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 30);
1079
1080 // Prob should decrease away from mean
1081 BOOST_CHECK(nearMean.in() > awayMean.in());
1082 }
1083}
1084
1085BOOST_AUTO_TEST_CASE(rndBW)
1086{
1087 int N = 1000;
1088
1089 for(double mean = 5; mean < 21; mean += 5) {
1090 for (double gamma = 1; gamma < 5; gamma += 1) {
1091 // Check for whole interval
1092 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
1093 // Check for median
1094 DoubleBinCheck firstHalf(-DBL_MAX, mean);
1095 DoubleBinCheck secondHalf(mean, DBL_MAX);
1096 // Check for decreasing probability away from mean
1097 DoubleBinCheck nearMean(mean - std::sqrt(gamma), mean + std::sqrt(gamma));
1098 DoubleBinCheck awayMeanOne(mean + std::sqrt(gamma), mean + 3 * std::sqrt(gamma));
1099 // Additional check for symmetry
1100 DoubleBinCheck awayMeanTwo(mean - 3 * std::sqrt(gamma), mean - std::sqrt(gamma));
1101
1102 for(int i = 0; i < N; ++i) {
1103 allnums.add(rng.rndBW(mean, gamma));
1104 firstHalf.add(rng.rndBW(mean, gamma));
1105 secondHalf.add(rng.rndBW(mean, gamma));
1106 nearMean.add(rng.rndBW(mean, gamma));
1107 awayMeanOne.add(rng.rndBW(mean, gamma));
1108 awayMeanTwo.add(rng.rndBW(mean, gamma));
1109 }
1110
1111 // Prob laying inside of interval should be 1
1112 BOOST_CHECK_EQUAL(allnums.tot(), N);
1113 BOOST_CHECK_EQUAL(allnums.in(), N);
1114 BOOST_CHECK_EQUAL(allnums.out(), 0);
1115
1116 // Prob laying inside of interval should be 0.5
1117 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
1118 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 30);
1119 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 30);
1120
1121 // Prob laying inside of interval should be 0.5
1122 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
1123 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 30);
1124 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 30);
1125
1126 // Prob should decrease away from mean
1127 BOOST_CHECK(nearMean.in() > awayMeanOne.in());
1128
1129 // Prob should be roughly symmetric with a shift to the left or right
1130 BOOST_CHECK_CLOSE(1.0 * awayMeanTwo.in(), 1.0 * awayMeanOne.in(), 60);
1131 }
1132 }
1133}
1134
1135BOOST_AUTO_TEST_CASE(rndBWCut)
1136{
1137 int N = 1000;
1138
1139 double cutValue = 2;
1140 for(double mean = 5; mean < 10; mean += 2) {
1141 for (double gamma = 1; gamma < 5; gamma += 1) {
1142 // Check for whole interval
1143 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
1144 // Check for cut interval
1145 DoubleBinCheck inCut(mean - cutValue, mean + cutValue);
1146 // Check for median
1147 DoubleBinCheck firstHalf(-DBL_MAX, mean);
1148 DoubleBinCheck secondHalf(mean, DBL_MAX);
1149 // Check for decreasing probability away from mean
1150 DoubleBinCheck nearMean(mean - std::sqrt(gamma), mean + std::sqrt(gamma));
1151 DoubleBinCheck awayMeanOne(mean + std::sqrt(gamma), mean + 3 * std::sqrt(gamma));
1152 // Additional check for symmetry
1153 DoubleBinCheck awayMeanTwo(mean - 3 * std::sqrt(gamma), mean - std::sqrt(gamma));
1154
1155 for(int i = 0; i < N; ++i) {
1156 allnums.add(rng.rndBW(mean, gamma, cutValue));
1157 inCut.add(rng.rndBW(mean, gamma, cutValue));
1158 firstHalf.add(rng.rndBW(mean, gamma, cutValue));
1159 secondHalf.add(rng.rndBW(mean, gamma, cutValue));
1160 nearMean.add(rng.rndBW(mean, gamma, cutValue));
1161 awayMeanOne.add(rng.rndBW(mean, gamma, cutValue));
1162 awayMeanTwo.add(rng.rndBW(mean, gamma, cutValue));
1163 }
1164
1165 // Prob laying inside of interval should be 1
1166 BOOST_CHECK_EQUAL(allnums.tot(), N);
1167 BOOST_CHECK_EQUAL(allnums.in(), N);
1168 BOOST_CHECK_EQUAL(allnums.out(), 0);
1169
1170 // Prob laying inside of interval should be 1
1171 BOOST_CHECK_EQUAL(inCut.tot(), N);
1172 BOOST_CHECK_EQUAL(inCut.in(), N);
1173 BOOST_CHECK_EQUAL(inCut.out(), 0);
1174
1175 // Prob laying inside of interval should be 0.5
1176 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
1177 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 30);
1178 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 30);
1179
1180 // Prob laying inside of interval should be 0.5
1181 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
1182 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 30);
1183 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 30);
1184
1185 // Prob should decrease away from mean
1186 BOOST_CHECK(nearMean.in() > awayMeanOne.in());
1187
1188 // Prob should be roughly symmetric with a shift to the left or right
1189 BOOST_CHECK_CLOSE(1.0 * awayMeanTwo.in(), 1.0 * awayMeanOne.in(), 60);
1190 }
1191 }
1192}
1193
1194BOOST_AUTO_TEST_CASE(rndRelBW)
1195{
1196 int N = 1000;
1197
1198 for(double mean = 5; mean < 21; mean += 5) {
1199 for (double gamma = 1; gamma < 5; gamma += 1) {
1200 // Check for whole interval
1201 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
1202 // Check for median
1203 DoubleBinCheck firstHalf(-DBL_MAX, mean);
1204 DoubleBinCheck secondHalf(mean, DBL_MAX);
1205 // Check for decreasing probability away from mean
1206 DoubleBinCheck nearMean(mean - std::sqrt(gamma), mean + std::sqrt(gamma));
1207 DoubleBinCheck awayMeanOne(mean + std::sqrt(gamma), mean + 3 * std::sqrt(gamma));
1208 // Additional check for symmetry
1209 DoubleBinCheck awayMeanTwo(mean - 3 * std::sqrt(gamma), mean - std::sqrt(gamma));
1210
1211 for(int i = 0; i < N; ++i) {
1212 allnums.add(rng.rndRelBW<double>(mean, gamma));
1213 firstHalf.add(rng.rndRelBW<double>(mean, gamma));
1214 secondHalf.add(rng.rndRelBW<double>(mean, gamma));
1215 nearMean.add(rng.rndRelBW<double>(mean, gamma));
1216 awayMeanOne.add(rng.rndRelBW<double>(mean, gamma));
1217 awayMeanTwo.add(rng.rndRelBW<double>(mean, gamma));
1218 }
1219
1220 // Prob laying inside of interval should be 1
1221 BOOST_CHECK_EQUAL(allnums.tot(), N);
1222 BOOST_CHECK_EQUAL(allnums.in(), N);
1223 BOOST_CHECK_EQUAL(allnums.out(), 0);
1224
1225 // Prob laying inside of interval should be 0.5
1226 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
1227 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 50);
1228 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 50);
1229
1230 // Prob laying inside of interval should be 0.5
1231 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
1232 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 50);
1233 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 50);
1234
1235 // Prob should decrease away from mean
1236 BOOST_CHECK(nearMean.in() > awayMeanOne.in());
1237
1238 // Prob should be roughly symmetric with a shift to the left or right
1239 BOOST_CHECK_CLOSE(1.0 * awayMeanTwo.in(), 1.0 * awayMeanOne.in(), 200);
1240 }
1241 }
1242}
1243
1244BOOST_AUTO_TEST_CASE(rndRelBWCut)
1245{
1246 int N = 1000;
1247
1248 double cutValue = 2;
1249 for(double mean = 5; mean < 10; mean += 2) {
1250 for (double gamma = 1; gamma < 5; gamma += 1) {
1251 // Check for whole interval
1252 DoubleBinCheck allnums(-DBL_MAX, DBL_MAX);
1253 // Check for cut interval
1254 DoubleBinCheck inCut(mean - cutValue, mean + cutValue);
1255 // Check for median
1256 DoubleBinCheck firstHalf(-DBL_MAX, mean);
1257 DoubleBinCheck secondHalf(mean, DBL_MAX);
1258 // Check for decreasing probability away from mean
1259 DoubleBinCheck nearMean(mean - std::sqrt(gamma), mean + std::sqrt(gamma));
1260 DoubleBinCheck awayMeanOne(mean + std::sqrt(gamma), mean + 3 * std::sqrt(gamma));
1261 // Additional check for symmetry
1262 DoubleBinCheck awayMeanTwo(mean - 3 * std::sqrt(gamma), mean - std::sqrt(gamma));
1263
1264 for(int i = 0; i < N; ++i) {
1265 allnums.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1266 inCut.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1267 firstHalf.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1268 secondHalf.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1269 nearMean.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1270 awayMeanOne.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1271 awayMeanTwo.add(rng.rndRelBW<double>(mean, gamma, cutValue));
1272 }
1273
1274 // Prob laying inside of interval should be 1
1275 BOOST_CHECK_EQUAL(allnums.tot(), N);
1276 BOOST_CHECK_EQUAL(allnums.in(), N);
1277 BOOST_CHECK_EQUAL(allnums.out(), 0);
1278
1279 // Prob laying inside of interval should be 1
1280 BOOST_CHECK_EQUAL(inCut.tot(), N);
1281 BOOST_CHECK_EQUAL(inCut.in(), N);
1282 BOOST_CHECK_EQUAL(inCut.out(), 0);
1283
1284 // Prob laying inside of interval should be 0.5
1285 BOOST_CHECK_EQUAL(firstHalf.tot(), N);
1286 BOOST_CHECK_CLOSE(firstHalf.in(), 0.5 * N, 30);
1287 BOOST_CHECK_CLOSE(firstHalf.out(), 0.5 * N, 30);
1288
1289 // Prob laying inside of interval should be 0.5
1290 BOOST_CHECK_EQUAL(secondHalf.tot(), N);
1291 BOOST_CHECK_CLOSE(secondHalf.in(), 0.5 * N, 30);
1292 BOOST_CHECK_CLOSE(secondHalf.out(), 0.5 * N, 30);
1293
1294 // Prob should decrease away from mean
1295 BOOST_CHECK(nearMean.in() > awayMeanOne.in());
1296
1297 // Prob should be roughly symmetric with a shift to the left or right
1298 BOOST_CHECK_CLOSE(1.0 * awayMeanTwo.in(), 1.0 * awayMeanOne.in(), 120);
1299 }
1300 }
1301}
1302
1303BOOST_AUTO_TEST_SUITE_END()
1304
1305#endif /* ThePEG_Repository_Test_RandomGenerator_H */
StandardRandom inherits from the RandomGenerator class and is an interface to the CLHEP::JamesRandom ...
IteratorRange< typename Container::iterator > range(Container &c)
Return an IteratorRange corresponding to the whole container.
Definition: algorithm.h:49