testRandomSamplerFromPDF.cc
Go to the documentation of this file.
1 
9 #include <tst/Verify.h>
10 #include <cppunit/extensions/HelperMacros.h>
11 #include <utl/RandomSamplerFromPDF.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 TestRandomSamplerFromPDF : public CppUnit::TestFixture {
29 
30  CPPUNIT_TEST_SUITE(TestRandomSamplerFromPDF);
31  CPPUNIT_TEST_EXCEPTION(TestLinearTooShort, InvalidConfigurationException);
32  CPPUNIT_TEST_EXCEPTION(TestDiscreteTooShort, InvalidConfigurationException);
33  CPPUNIT_TEST_EXCEPTION(TestStepTooShort, InvalidConfigurationException);
34  CPPUNIT_TEST_EXCEPTION(TestNegative, InvalidConfigurationException);
35  CPPUNIT_TEST(TestTabulatedFunction);
36  /*CPPUNIT_TEST(TestVectors);
37  CPPUNIT_TEST(TestVectorAndNormalization);
38  CPPUNIT_TEST(TestMap);
39  CPPUNIT_TEST(TestFunction);
40  CPPUNIT_TEST(TestFunction2);*/
41  CPPUNIT_TEST_SUITE_END();
42 
43 public:
44  void setUp() { }
45 
46  void tearDown() { }
47 
48  void
50  {
51  vector<double> pdf(1, 0);
52  RandomSamplerFromPDF sampler(pdf, RandomSamplerFromPDF::eLinear);
53  }
54 
55  void
57  {
58  vector<double> pdf;
59  RandomSamplerFromPDF sampler(pdf, RandomSamplerFromPDF::eDiscrete);
60  }
61 
62  void
64  {
65  vector<double> pdf;
66  RandomSamplerFromPDF sampler(pdf, RandomSamplerFromPDF::eStep);
67  }
68 
69  void
71  {
72  vector<double> pdf(3);
73  pdf[1] = 1;
74  pdf[2] = -0.5;
75  RandomSamplerFromPDF sampler(pdf, RandomSamplerFromPDF::eDiscrete);
76  }
77 
78  void
80  {
82  pdf.PushBack(-1, 0);
83  pdf.PushBack(1, 1);
84  pdf.PushBack(2, 0);
85  pdf.PushBack(4, 0);
86  pdf.PushBack(5, 0.5);
87  pdf.PushBack(6, 0);
88  pdf.PushBack(9, 0);
89  pdf.PushBack(10, 3);
90  CLHEP::MTwistEngine rand;
91  {
92  const RandomSamplerFromPDF sLin(pdf, RandomSamplerFromPDF::eLinear);
93  for (int i = 0; i < 1000; ++i) {
94  const double x = sLin.shoot(rand);
95  CPPUNIT_ASSERT((-1 <= x && x <= 2) || (4 <= x && x <= 6) || (9 <= x && x <= 10));
96  }
97  const double x[] = {
98  -1, -0.5, 0, 0.5, 1, 1.5, 2, 4.5, 5, 5.5,
99  6, 9.5, 10, 4.75, 9.2, 4.8, 9.9
100  };
101  const double y[] = {
102  0, 0.0625, 0.25, 0.5625, 1, 1.375, 1.5, 1.5625, 1.75, 1.9375,
103  2, 2.375, 3.5, 1.64063, 2.06, 1.66, 3.215
104  };
105  const unsigned int n = sizeof(x) / sizeof(x[0]);
106  for (unsigned int i = 0; i < n; ++i)
107  CLOSE_AT(sLin.GetInverseCDF(y[i]/3.5), x[i], 1e-5);
108  }
109  {
110  const RandomSamplerFromPDF sDis(pdf, RandomSamplerFromPDF::eDiscrete);
111  for (int i = 0; i < 1000; ++i) {
112  const double x = sDis.shoot(rand);
113  CPPUNIT_ASSERT(x == 1 || x == 5 || x == 10);
114  }
115  const double x[] = { -1, 1, 1, 1, 1, 5, 10 };
116  const double y[] = { 0, 0.1, 0.5, 0.9, 1, 1.1, 4.5 };
117  const unsigned int n = sizeof(x) / sizeof(x[0]);
118  for (unsigned int i = 0; i < n; ++i)
119  EQUAL(sDis.GetInverseCDF(y[i]/4.5), x[i]);
120  }
121  {
122  const RandomSamplerFromPDF sSt(pdf, RandomSamplerFromPDF::eStep);
123  for (int i = 0; i < 1000; ++i) {
124  const double x = sSt.shoot(rand);
125  CPPUNIT_ASSERT((1 <= x && x <= 2) || (5 <= x && x <= 6));
126  }
127  const double x[] = { 1.12, 1.93, 5.58 };
128  const double y[] = { 0.08, 0.62, 0.86 };
129  const unsigned int n = sizeof(x) / sizeof(x[0]);
130  for (unsigned int i = 0; i < n; ++i)
131  CLOSE(sSt.GetInverseCDF(y[i]), x[i]);
132  }
133  }
134 
135  /*void
136  TestVectors()
137  {
138  const double x[] = { 10, 10, 11, 11 };
139  const double y[] = { 0, 0.1, 0.1, 1 };
140  const unsigned int n = sizeof(x) / sizeof(x[0]);
141  vector<double> xx(x, x + n);
142  vector<double> yy(y, y + n);
143  const RandomSamplerFromCDF sampler(xx, yy);
144  CLHEP::MTwistEngine rand;
145  for (int i = 0; i < 1000; ++i) {
146  const double x = sampler.shoot(rand);
147  CPPUNIT_ASSERT(x == 10 || x == 11);
148  }
149  }
150 
151  void
152  TestVectorAndNormalization()
153  {
154  const double y[] = { 0, 1, 3, 7, 10 };
155  const unsigned int n = sizeof(y) / sizeof(y[0]);
156  const vector<double> yy(y, y + n);
157  const RandomSamplerFromCDF sampler(yy);
158  CLHEP::MTwistEngine rand;
159  for (int i = 0; i < 1000; ++i) {
160  const double x = sampler.shoot(rand);
161  CPPUNIT_ASSERT(0 <= x && x <= 1);
162  }
163  }
164 
165  void
166  TestMap()
167  {
168  const double x[] = { -5, 0, 8, 9, 9.5, 10 };
169  const double y[] = { 0, 25, 25, 50, 50, 100 };
170  map<double, double> xy;
171  const unsigned int n = sizeof(x) / sizeof(x[0]);
172  for (unsigned int i = 0; i < n; ++i)
173  xy[x[i]] = y[i];
174  const RandomSamplerFromCDF sampler(xy);
175  CLHEP::MTwistEngine rand;
176  for (int i = 0; i < 1000; ++i) {
177  const double x = sampler.shoot(rand);
178  CPPUNIT_ASSERT((-5 <= x && x <= 0) || (8 <= x && x <= 9) || (9.5 <= x && x <= 10));
179  }
180  }
181 
182  void
183  TestFunction()
184  {
185  const RandomSamplerFromCDF sampler("x*x - 1", 1, 2, 10);
186  CLOSE(sampler.GetInverseCDF(0), 1.);
187  CLOSE_AT(sampler.GetInverseCDF(0.5), sqrt(3*0.5+1), 1e-2);
188  CLOSE(sampler.GetInverseCDF(1), 2.);
189  CLHEP::MTwistEngine rand;
190  for (int i = 0; i < 1000; ++i) {
191  const double x = sampler.shoot(rand);
192  CPPUNIT_ASSERT((1 <= x && x <= 2));
193  }
194  }
195 
196  void
197  TestFunction2()
198  {
199  const double min = -1.222;
200  const double max = 0.63;
201  const string func = "sin(x) - sin(" + lexical_cast<string>(min) + ")";
202  const RandomSamplerFromCDF sampler(func, min, max, 1000);
203  CLOSE(sampler.GetInverseCDF(0), min);
204  CLOSE(sampler.GetInverseCDF(1), max);
205  CLHEP::MTwistEngine rand;
206  for (int i = 0; i < 1000; ++i) {
207  const double x = sampler.shoot(rand);
208  CPPUNIT_ASSERT((min <= x && x <= max));
209  }
210  }*/
211 
212 };
213 
214 
#define CLOSE(x, y)
#define EQUAL(x, y)
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)
double GetInverseCDF(const double y) const
#define CLOSE_AT(x, y, eps)

, generated on Tue Sep 26 2023.