|
|
| 54 |
} |
54 |
} |
| 55 |
} |
55 |
} |
| 56 |
|
56 |
|
|
|
57 |
double |
| 58 |
ChiSquare (Ptr<RandomVariableStream> rng, const double hMin, const double hMax) |
| 59 |
{ |
| 60 |
gsl_histogram * h = gsl_histogram_alloc (N_BINS); |
| 61 |
|
| 62 |
gsl_histogram_set_ranges_uniform (h, hMin, hMax); |
| 63 |
|
| 64 |
std::vector<double> expected (N_BINS, (double)N_MEASUREMENTS / (double)N_BINS); |
| 65 |
|
| 66 |
return ChiSquare (rng, h, expected); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
double |
| 71 |
ChiSquare (Ptr<RandomVariableStream> rng, gsl_histogram * h, const std::vector<double> expected) |
| 72 |
{ |
| 73 |
for (uint32_t i = 0; i < N_MEASUREMENTS; ++i) |
| 74 |
{ |
| 75 |
gsl_histogram_increment (h, rng->GetValue ()); |
| 76 |
} |
| 77 |
|
| 78 |
// TODO: use std::valarray for these computations? |
| 79 |
double tmp[N_BINS]; |
| 80 |
|
| 81 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 82 |
{ |
| 83 |
tmp[i] = gsl_histogram_get (h, i); |
| 84 |
tmp[i] -= expected[i]; |
| 85 |
tmp[i] *= tmp[i]; |
| 86 |
tmp[i] /= expected[i]; |
| 87 |
} |
| 88 |
|
| 89 |
gsl_histogram_free (h); |
| 90 |
|
| 91 |
double chiSquared = 0; |
| 92 |
|
| 93 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 94 |
{ |
| 95 |
chiSquared += tmp[i]; |
| 96 |
} |
| 97 |
|
| 98 |
return chiSquared; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 57 |
} // anonymous namespace |
103 |
} // anonymous namespace |
| 58 |
|
104 |
|
| 59 |
// =========================================================================== |
105 |
// =========================================================================== |
|
|
| 69 |
RandomVariableStreamUniformTestCase (); |
115 |
RandomVariableStreamUniformTestCase (); |
| 70 |
virtual ~RandomVariableStreamUniformTestCase (); |
116 |
virtual ~RandomVariableStreamUniformTestCase (); |
| 71 |
|
117 |
|
| 72 |
double ChiSquaredTest (Ptr<UniformRandomVariable> u); |
|
|
| 73 |
|
| 74 |
private: |
118 |
private: |
| 75 |
virtual void DoRun (void); |
119 |
virtual void DoRun (void); |
| 76 |
}; |
120 |
}; |
|
|
| 84 |
{ |
128 |
{ |
| 85 |
} |
129 |
} |
| 86 |
|
130 |
|
| 87 |
double |
|
|
| 88 |
RandomVariableStreamUniformTestCase::ChiSquaredTest (Ptr<UniformRandomVariable> u) |
| 89 |
{ |
| 90 |
gsl_histogram * h = gsl_histogram_alloc (N_BINS); |
| 91 |
|
| 92 |
// Note that this assumes that the range for u is [0,1], which is |
| 93 |
// the default range for this distribution. |
| 94 |
gsl_histogram_set_ranges_uniform (h, 0., 1.); |
| 95 |
|
| 96 |
for (uint32_t i = 0; i < N_MEASUREMENTS; ++i) |
| 97 |
{ |
| 98 |
gsl_histogram_increment (h, u->GetValue ()); |
| 99 |
} |
| 100 |
|
| 101 |
double tmp[N_BINS]; |
| 102 |
|
| 103 |
double expected = ((double)N_MEASUREMENTS / (double)N_BINS); |
| 104 |
|
| 105 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 106 |
{ |
| 107 |
tmp[i] = gsl_histogram_get (h, i); |
| 108 |
tmp[i] -= expected; |
| 109 |
tmp[i] *= tmp[i]; |
| 110 |
tmp[i] /= expected; |
| 111 |
} |
| 112 |
|
| 113 |
gsl_histogram_free (h); |
| 114 |
|
| 115 |
double chiSquared = 0; |
| 116 |
|
| 117 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 118 |
{ |
| 119 |
chiSquared += tmp[i]; |
| 120 |
} |
| 121 |
|
| 122 |
return chiSquared; |
| 123 |
} |
| 124 |
|
| 125 |
void |
131 |
void |
| 126 |
RandomVariableStreamUniformTestCase::DoRun (void) |
132 |
RandomVariableStreamUniformTestCase::DoRun (void) |
| 127 |
{ |
133 |
{ |
|
|
| 133 |
for (uint32_t i = 0; i < N_RUNS; ++i) |
139 |
for (uint32_t i = 0; i < N_RUNS; ++i) |
| 134 |
{ |
140 |
{ |
| 135 |
Ptr<UniformRandomVariable> u = CreateObject<UniformRandomVariable> (); |
141 |
Ptr<UniformRandomVariable> u = CreateObject<UniformRandomVariable> (); |
| 136 |
double result = ChiSquaredTest (u); |
142 |
|
|
|
143 |
// Note that this assumes that the range for u is [0,1], which is |
| 144 |
// the default range for this distribution. |
| 145 |
double result = ChiSquare (u, 0., 1.); |
| 137 |
sum += result; |
146 |
sum += result; |
| 138 |
} |
147 |
} |
| 139 |
|
148 |
|
|
|
| 178 |
RandomVariableStreamUniformAntitheticTestCase (); |
187 |
RandomVariableStreamUniformAntitheticTestCase (); |
| 179 |
virtual ~RandomVariableStreamUniformAntitheticTestCase (); |
188 |
virtual ~RandomVariableStreamUniformAntitheticTestCase (); |
| 180 |
|
189 |
|
| 181 |
double ChiSquaredTest (Ptr<UniformRandomVariable> u); |
|
|
| 182 |
|
| 183 |
private: |
190 |
private: |
| 184 |
virtual void DoRun (void); |
191 |
virtual void DoRun (void); |
| 185 |
}; |
192 |
}; |
|
|
| 193 |
{ |
200 |
{ |
| 194 |
} |
201 |
} |
| 195 |
|
202 |
|
| 196 |
double |
|
|
| 197 |
RandomVariableStreamUniformAntitheticTestCase::ChiSquaredTest (Ptr<UniformRandomVariable> u) |
| 198 |
{ |
| 199 |
gsl_histogram * h = gsl_histogram_alloc (N_BINS); |
| 200 |
|
| 201 |
// Note that this assumes that the range for u is [0,1], which is |
| 202 |
// the default range for this distribution. |
| 203 |
gsl_histogram_set_ranges_uniform (h, 0., 1.); |
| 204 |
|
| 205 |
for (uint32_t i = 0; i < N_MEASUREMENTS; ++i) |
| 206 |
{ |
| 207 |
gsl_histogram_increment (h, u->GetValue ()); |
| 208 |
} |
| 209 |
|
| 210 |
double tmp[N_BINS]; |
| 211 |
|
| 212 |
double expected = ((double)N_MEASUREMENTS / (double)N_BINS); |
| 213 |
|
| 214 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 215 |
{ |
| 216 |
tmp[i] = gsl_histogram_get (h, i); |
| 217 |
tmp[i] -= expected; |
| 218 |
tmp[i] *= tmp[i]; |
| 219 |
tmp[i] /= expected; |
| 220 |
} |
| 221 |
|
| 222 |
gsl_histogram_free (h); |
| 223 |
|
| 224 |
double chiSquared = 0; |
| 225 |
|
| 226 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 227 |
{ |
| 228 |
chiSquared += tmp[i]; |
| 229 |
} |
| 230 |
|
| 231 |
return chiSquared; |
| 232 |
} |
| 233 |
|
| 234 |
void |
203 |
void |
| 235 |
RandomVariableStreamUniformAntitheticTestCase::DoRun (void) |
204 |
RandomVariableStreamUniformAntitheticTestCase::DoRun (void) |
| 236 |
{ |
205 |
{ |
|
|
| 246 |
// Make this generate antithetic values. |
215 |
// Make this generate antithetic values. |
| 247 |
u->SetAttribute ("Antithetic", BooleanValue (true)); |
216 |
u->SetAttribute ("Antithetic", BooleanValue (true)); |
| 248 |
|
217 |
|
| 249 |
double result = ChiSquaredTest (u); |
218 |
// Note that this assumes that the range for u is [0,1], which is |
|
|
219 |
// the default range for this distribution. |
| 220 |
double result = ChiSquare (u, 0., 1.); |
| 250 |
sum += result; |
221 |
sum += result; |
| 251 |
} |
222 |
} |
| 252 |
|
223 |
|
|
|
| 405 |
RandomVariableStreamNormalTestCase (); |
376 |
RandomVariableStreamNormalTestCase (); |
| 406 |
virtual ~RandomVariableStreamNormalTestCase (); |
377 |
virtual ~RandomVariableStreamNormalTestCase (); |
| 407 |
|
378 |
|
| 408 |
double ChiSquaredTest (Ptr<NormalRandomVariable> n); |
|
|
| 409 |
|
| 410 |
private: |
379 |
private: |
| 411 |
virtual void DoRun (void); |
380 |
virtual void DoRun (void); |
| 412 |
}; |
381 |
}; |
|
|
| 420 |
{ |
389 |
{ |
| 421 |
} |
390 |
} |
| 422 |
|
391 |
|
| 423 |
double |
392 |
void |
| 424 |
RandomVariableStreamNormalTestCase::ChiSquaredTest (Ptr<NormalRandomVariable> n) |
393 |
RandomVariableStreamNormalTestCase::DoRun (void) |
| 425 |
{ |
394 |
{ |
|
|
395 |
SeedManager::SetSeed (time (0)); |
| 396 |
|
| 397 |
double sum = 0.; |
| 398 |
double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); |
| 399 |
|
| 426 |
gsl_histogram * h = gsl_histogram_alloc (N_BINS); |
400 |
gsl_histogram * h = gsl_histogram_alloc (N_BINS); |
| 427 |
|
401 |
|
| 428 |
double range[N_BINS + 1]; |
402 |
double range[N_BINS + 1]; |
|
|
| 445 |
expected[i] *= N_MEASUREMENTS; |
419 |
expected[i] *= N_MEASUREMENTS; |
| 446 |
} |
420 |
} |
| 447 |
|
421 |
|
| 448 |
for (uint32_t i = 0; i < N_MEASUREMENTS; ++i) |
|
|
| 449 |
{ |
| 450 |
gsl_histogram_increment (h, n->GetValue ()); |
| 451 |
} |
| 452 |
|
| 453 |
double tmp[N_BINS]; |
| 454 |
|
| 455 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 456 |
{ |
| 457 |
tmp[i] = gsl_histogram_get (h, i); |
| 458 |
tmp[i] -= expected[i]; |
| 459 |
tmp[i] *= tmp[i]; |
| 460 |
tmp[i] /= expected[i]; |
| 461 |
} |
| 462 |
|
| 463 |
gsl_histogram_free (h); |
| 464 |
|
| 465 |
double chiSquared = 0; |
| 466 |
|
| 467 |
for (uint32_t i = 0; i < N_BINS; ++i) |
| 468 |
{ |
| 469 |
chiSquared += tmp[i]; |
| 470 |
} |
| 471 |
|
| 472 |
return chiSquared; |
| 473 |
} |
| 474 |
|
| 475 |
void |
| 476 |
RandomVariableStreamNormalTestCase::DoRun (void) |
| 477 |
{ |
| 478 |
SeedManager::SetSeed (time (0)); |
| 479 |
|
| 480 |
double sum = 0.; |
| 481 |
double maxStatistic = gsl_cdf_chisq_Qinv (0.05, N_BINS); |
| 482 |
|
| 483 |
for (uint32_t i = 0; i < N_RUNS; ++i) |
422 |
for (uint32_t i = 0; i < N_RUNS; ++i) |
| 484 |
{ |
423 |
{ |
| 485 |
Ptr<NormalRandomVariable> n = CreateObject<NormalRandomVariable> (); |
424 |
Ptr<NormalRandomVariable> n = CreateObject<NormalRandomVariable> (); |
| 486 |
double result = ChiSquaredTest (n); |
425 |
double result = ChiSquare (n, h, expected); |
| 487 |
sum += result; |
426 |
sum += result; |
| 488 |
} |
427 |
} |
| 489 |
|
428 |
|
|
|
| 2804 |
AddTestCase (new RandomVariableStreamWeibullTestCase, TestCase::QUICK); |
2743 |
AddTestCase (new RandomVariableStreamWeibullTestCase, TestCase::QUICK); |
| 2805 |
AddTestCase (new RandomVariableStreamWeibullAntitheticTestCase, TestCase::QUICK); |
2744 |
AddTestCase (new RandomVariableStreamWeibullAntitheticTestCase, TestCase::QUICK); |
| 2806 |
AddTestCase (new RandomVariableStreamLogNormalTestCase, TestCase::QUICK); |
2745 |
AddTestCase (new RandomVariableStreamLogNormalTestCase, TestCase::QUICK); |
| 2807 |
/// \todo This test is currently disabled because it fails sometimes. |
2746 |
/** |
| 2808 |
/// A possible reason for the failure is that the antithetic code is |
2747 |
* \todo This test is currently disabled because it fails sometimes. |
| 2809 |
/// not implemented properly for this log-normal case. |
2748 |
* A possible reason for the failure is that the antithetic code is |
| 2810 |
/* |
2749 |
* not implemented properly for this log-normal case. |
|
|
2750 |
* |
| 2811 |
AddTestCase (new RandomVariableStreamLogNormalAntitheticTestCase, TestCase::QUICK); |
2751 |
AddTestCase (new RandomVariableStreamLogNormalAntitheticTestCase, TestCase::QUICK); |
| 2812 |
*/ |
2752 |
*/ |
| 2813 |
AddTestCase (new RandomVariableStreamGammaTestCase, TestCase::QUICK); |
2753 |
AddTestCase (new RandomVariableStreamGammaTestCase, TestCase::QUICK); |
| 2814 |
/// \todo This test is currently disabled because it fails sometimes. |
2754 |
/** \todo This test is currently disabled because it fails sometimes. |
| 2815 |
/// A possible reason for the failure is that the antithetic code is |
2755 |
* A possible reason for the failure is that the antithetic code is |
| 2816 |
/// not implemented properly for this gamma case. |
2756 |
* not implemented properly for this gamma case. |
| 2817 |
/* |
2757 |
* |
| 2818 |
AddTestCase (new RandomVariableStreamGammaAntitheticTestCase, TestCase::QUICK); |
2758 |
AddTestCase (new RandomVariableStreamGammaAntitheticTestCase, TestCase::QUICK); |
| 2819 |
*/ |
2759 |
*/ |
| 2820 |
AddTestCase (new RandomVariableStreamErlangTestCase, TestCase::QUICK); |
2760 |
AddTestCase (new RandomVariableStreamErlangTestCase, TestCase::QUICK); |