testRandomSamplerFromCDF.cc
Go to the documentation of this file.
1 
9 #include <tst/Verify.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <utl/RandomSamplerFromCDF.h>
12 #include <utl/AugerException.h>
13 #include <utl/TabulatedFunction.h>
14 #include <CLHEP/Random/Randomize.h>
15 #include <boost/lexical_cast.hpp>
16 
17 using namespace std;
18 using namespace utl;
19 using namespace tst;
20 using boost::lexical_cast;
21 
22 
23 #define EQUAL(x, y) CPPUNIT_ASSERT(Verify<Equal>(x, y))
24 #define CLOSE(x, y) CPPUNIT_ASSERT(Verify<CloseTo>(x, y))
25 #define CLOSE_AT(x, y, eps) CPPUNIT_ASSERT(Verify<CloseTo>(x, y, eps))
26 
27 
28 class TestRandomSamplerFromCDF : public CppUnit::TestFixture {
29 
30  CPPUNIT_TEST_SUITE(TestRandomSamplerFromCDF);
31  CPPUNIT_TEST_EXCEPTION(TestTooShort, InvalidConfigurationException);
32  CPPUNIT_TEST_EXCEPTION(TestStartZero, InvalidConfigurationException);
33  CPPUNIT_TEST_EXCEPTION(TestMonotonous, InvalidConfigurationException);
34  CPPUNIT_TEST(TestTabulatedFunction);
35  CPPUNIT_TEST(TestVectors);
36  CPPUNIT_TEST(TestVectorAndNormalization);
37  CPPUNIT_TEST(TestMap);
38  CPPUNIT_TEST(TestFunction);
39  CPPUNIT_TEST(TestFunction2);
40  CPPUNIT_TEST_SUITE_END();
41 
42 public:
43  void setUp() { }
44 
45  void tearDown() { }
46 
47  void
49  {
50  vector<double> cdf(1, 0);
51  RandomSamplerFromCDF sampler(cdf);
52  }
53 
54  void
56  {
57  vector<double> cdf(2, 1);
58  RandomSamplerFromCDF sampler(cdf);
59  }
60 
61  void
63  {
64  vector<double> cdf(3);
65  cdf[1] = 1;
66  cdf[2] = 0.5;
67  RandomSamplerFromCDF sampler(cdf);
68  }
69 
70  void
72  {
74  cdf.PushBack(-1, 0);
75  cdf.PushBack(1, 1);
76  cdf.PushBack(2, 1);
77  cdf.PushBack(3, 2);
78  const RandomSamplerFromCDF sampler(cdf);
79  CLHEP::MTwistEngine rand;
80  for (int i = 0; i < 1000; ++i) {
81  const double x = sampler.shoot(rand);
82  CPPUNIT_ASSERT(-1 <= x && x <= 3 && !(1 < x && x < 2));
83  }
84  const double eps = 1e-12;
85  EQUAL(sampler.GetInverseCDF(-1), -1.);
86  EQUAL(sampler.GetInverseCDF(0), -1.);
87  CLOSE(sampler.GetInverseCDF(0.25), 0.);
88  CLOSE(sampler.GetInverseCDF(0.5-eps), 1.);
89  CLOSE(sampler.GetInverseCDF(0.5), 1.);
90  CLOSE(sampler.GetInverseCDF(0.5+eps), 2.);
91  CLOSE(sampler.GetInverseCDF(0.75), 2.5);
92  EQUAL(sampler.GetInverseCDF(1), 3.);
93  EQUAL(sampler.GetInverseCDF(2), 3.);
94  }
95 
96  void
98  {
99  const double x[] = { 10, 10, 11, 11 };
100  const double y[] = { 0, 0.1, 0.1, 1 };
101  const unsigned int n = sizeof(x) / sizeof(x[0]);
102  vector<double> xx(x, x + n);
103  vector<double> yy(y, y + n);
104  const RandomSamplerFromCDF sampler(xx, yy);
105  CLHEP::MTwistEngine rand;
106  for (int i = 0; i < 1000; ++i) {
107  const double x = sampler.shoot(rand);
108  CPPUNIT_ASSERT(x == 10 || x == 11);
109  }
110  }
111 
112  void
114  {
115  const double y[] = { 0, 1, 3, 7, 10 };
116  const unsigned int n = sizeof(y) / sizeof(y[0]);
117  const vector<double> yy(y, y + n);
118  const RandomSamplerFromCDF sampler(yy);
119  CLHEP::MTwistEngine rand;
120  for (int i = 0; i < 1000; ++i) {
121  const double x = sampler.shoot(rand);
122  CPPUNIT_ASSERT(0 <= x && x <= 1);
123  }
124  }
125 
126  void
128  {
129  const double x[] = { -5, 0, 8, 9, 9.5, 10 };
130  const double y[] = { 0, 25, 25, 50, 50, 100 };
131  map<double, double> xy;
132  const unsigned int n = sizeof(x) / sizeof(x[0]);
133  for (unsigned int i = 0; i < n; ++i)
134  xy[x[i]] = y[i];
135  const RandomSamplerFromCDF sampler(xy);
136  CLHEP::MTwistEngine rand;
137  for (int i = 0; i < 1000; ++i) {
138  const double x = sampler.shoot(rand);
139  CPPUNIT_ASSERT((-5 <= x && x <= 0) || (8 <= x && x <= 9) || (9.5 <= x && x <= 10));
140  }
141  }
142 
143  void
145  {
146  const RandomSamplerFromCDF sampler("x*x - 1", 1, 2, 10);
147  CLOSE(sampler.GetInverseCDF(0), 1.);
148  CLOSE_AT(sampler.GetInverseCDF(0.5), sqrt(3*0.5+1), 1e-2);
149  CLOSE(sampler.GetInverseCDF(1), 2.);
150  CLHEP::MTwistEngine rand;
151  for (int i = 0; i < 1000; ++i) {
152  const double x = sampler.shoot(rand);
153  CPPUNIT_ASSERT((1 <= x && x <= 2));
154  }
155  }
156 
157  void
159  {
160  const double min = -1.222;
161  const double max = 0.63;
162  const string func = "sin(x) - sin(" + lexical_cast<string>(min) + ")";
163  const RandomSamplerFromCDF sampler(func, min, max, 1000);
164  CLOSE(sampler.GetInverseCDF(0), min);
165  CLOSE(sampler.GetInverseCDF(1), max);
166  CLHEP::MTwistEngine rand;
167  for (int i = 0; i < 1000; ++i) {
168  const double x = sampler.shoot(rand);
169  CPPUNIT_ASSERT((min <= x && x <= max));
170  }
171  }
172 
173 };
174 
175 
Base class for exceptions arising because configuration data are not valid.
Class to hold collection (x,y) points and provide interpolation between them.
void PushBack(const double x, const double y)
double shoot(HepEngine &engine) const
Method to shoot random values using a given engine by-passing the static generator.
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
#define max(a, b)
#define EQUAL(x, y)
double GetInverseCDF(const double y) const
#define CLOSE(x, y)
double eps
#define CLOSE_AT(x, y, eps)

, generated on Tue Sep 26 2023.