| 1 |
/// \ingroup newran |
| 2 |
///@{ |
| 3 |
|
| 4 |
/// \file tryurng1.cpp |
| 5 |
/// Test uniform random number generators - the tests. |
| 6 |
|
| 7 |
#define WANT_STREAM |
| 8 |
#define WANT_MATH |
| 9 |
|
| 10 |
#include "include.h" |
| 11 |
#include "newran.h" |
| 12 |
#include "format.h" |
| 13 |
#include "tryurng.h" |
| 14 |
#include "utility.h" |
| 15 |
#include "test_out.h" |
| 16 |
#include "array.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
double chisq_test(const Array<double>& x, int n); |
| 23 |
|
| 24 |
short int CountBits(char c); |
| 25 |
|
| 26 |
short int CountBits(short int i); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
void test1(double N) |
| 31 |
{ |
| 32 |
Uniform U; |
| 33 |
double sum = 0.0, sumsq = 0.0, ar1 = 0.0, last = 0.0; |
| 34 |
double j; |
| 35 |
Array<double> chi0(0,15); |
| 36 |
Array<double> chi1(0,255); |
| 37 |
Array<double> chi1x(0,255); |
| 38 |
Array<double> chi2(0,65535); |
| 39 |
Array<double> chi2x(0,65535); |
| 40 |
chi0 = 0; chi1 = 0; chi1x = 0; chi2 = 0; chi2x = 0; |
| 41 |
|
| 42 |
Array<double> crawl7(0,127); |
| 43 |
Array<double> crawl8(0,255); |
| 44 |
Array<double> crawl15(0,32767); |
| 45 |
Array<double> crawl16(0,65535); |
| 46 |
crawl7 = 0; |
| 47 |
crawl8 = 0; |
| 48 |
crawl15 = 0; |
| 49 |
crawl16 = 0; |
| 50 |
unsigned long crawler = 0; |
| 51 |
|
| 52 |
|
| 53 |
int m_bits = (int)(log(N) / 0.693 - 0.471); // number of bits in sparse monkey test |
| 54 |
unsigned long M = 1; M <<= (m_bits - 3); // 2**m_bits / 8 |
| 55 |
String Seen(M, (char)0); // to accumulate results |
| 56 |
unsigned long mask1 = (M - 1); |
| 57 |
|
| 58 |
for (j = 0; j < N; ++j) |
| 59 |
{ |
| 60 |
double u = U.Next(); |
| 61 |
if (u == 1.0) { cout << "Reject value == 1" << endl; continue; } |
| 62 |
double v = u - 0.5; |
| 63 |
sum += v; |
| 64 |
sumsq += v * v; |
| 65 |
ar1 += v * (last - 0.5); |
| 66 |
int k = (int)floor(u * 256); ++chi1(k); |
| 67 |
int m = (int)floor(u * 65536); ++chi2(m); |
| 68 |
int a = (int)floor(u * 16); ++chi0(a); |
| 69 |
if (j > 0) |
| 70 |
{ |
| 71 |
int b = (int)floor(last * 16); |
| 72 |
++chi1x(a + 16 * b); |
| 73 |
int l = (int)floor(last * 256); ++chi2x(k + 256 * l); |
| 74 |
} |
| 75 |
last = u; |
| 76 |
|
| 77 |
crawler <<= 1; if (v >= 0) ++crawler; |
| 78 |
if (j >= 6) ++crawl7(crawler & 0x7F); |
| 79 |
if (j >= 7) ++crawl8(crawler & 0xFF); |
| 80 |
if (j >= 14) ++crawl15(crawler & 0x7FFF); |
| 81 |
if (j >= 15) ++crawl16(crawler & 0xFFFF); |
| 82 |
|
| 83 |
|
| 84 |
if ( j >= (unsigned int)(m_bits-1) ) |
| 85 |
{ |
| 86 |
unsigned char mask2 = 1; mask2 <<= crawler & 7; |
| 87 |
Seen[(crawler >> 3) & mask1] |= mask2; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
// check - have I got these right |
| 93 |
|
| 94 |
NormalTestTwoSided test_mean("Mean", sum, 0.0, N / 12.0); test_mean.DoTest(); |
| 95 |
NormalTestTwoSided test_var("Variance", sumsq, N / 12.0, N / 180.0); test_var.DoTest(); |
| 96 |
NormalTestTwoSided test_cov("AutoCov 1", ar1, 0.0, N / 144.0); test_cov.DoTest(); |
| 97 |
|
| 98 |
ChiSquaredTestTwoSided Chi_4("Chi 4", chisq_test(chi0, 16), 15); Chi_4.DoTest(); |
| 99 |
ChiSquaredTestTwoSided Chi_8("Chi 8", chisq_test(chi1, 256), 255); Chi_8.DoTest(); |
| 100 |
ChiSquaredTestTwoSided Chi_16("Chi 16", chisq_test(chi2, 65536), 65535); Chi_16.DoTest(); |
| 101 |
|
| 102 |
ChiSquaredTestTwoSided MM_0_4_2("MM 0-4-2", |
| 103 |
chisq_test(chi1x, 256) - chisq_test(chi0, 16), 240); |
| 104 |
MM_0_4_2.DoTest(); |
| 105 |
|
| 106 |
ChiSquaredTestTwoSided MM_0_8_2("MM 0-8-2", |
| 107 |
chisq_test(chi2x, 65536) - chisq_test(chi1, 256), 65280); |
| 108 |
MM_0_8_2.DoTest(); |
| 109 |
|
| 110 |
ChiSquaredTestTwoSided MM_0_1_8("MM 0-1-8", |
| 111 |
chisq_test(crawl8, 256) - chisq_test(crawl7, 128), 128); |
| 112 |
MM_0_1_8.DoTest(); |
| 113 |
|
| 114 |
ChiSquaredTestTwoSided MM_0_1_16("MM 0-1-16", |
| 115 |
chisq_test(crawl16, 65536) - chisq_test(crawl15, 32768), 32768); |
| 116 |
MM_0_1_16.DoTest(); |
| 117 |
|
| 118 |
|
| 119 |
double seen = 0.0; |
| 120 |
double M8 = M * 8.0; // number of possible m bit strings |
| 121 |
for (unsigned int i = 0; i < M; i++) seen += CountBits(Seen[i]); |
| 122 |
double unseen = M8 - seen; |
| 123 |
//cout << "Number of bits = " << m_bits << endl; |
| 124 |
//cout << "Number of target strings = " << M8 << endl; |
| 125 |
//cout << "Number seen = " << seen << endl; |
| 126 |
//cout << "Number unseen = " << unseen << endl; |
| 127 |
double expected = exp(m_bits * log(2.0) - N / M8); |
| 128 |
//cout << "Approx expected unseen = " << expected << endl; |
| 129 |
double p = exp(- (N / M8)); |
| 130 |
double var = expected * (1.0 - p * (1.0 + N / M8)); |
| 131 |
//cout << "Uncorrected s.d. = " << sqrt(var) << endl; |
| 132 |
var *= (1.0 + 2.0 * pow(p, 0.30)); // approximate correction |
| 133 |
//cout << "Approx s.d. = " << sqrt(var) << endl; |
| 134 |
|
| 135 |
NormalTestTwoSided mm_sparse("MM sparse", unseen, expected, var); mm_sparse.DoTest(); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
double chisq_test(const Array<double>& x, int n) |
| 144 |
{ |
| 145 |
double sum = 0; double total = 0; int i; |
| 146 |
for (i = 0; i < n; ++i) total += x(i); |
| 147 |
double e = (double)total / (double)n; |
| 148 |
for (i = 0; i < n; ++i) sum += square(x(i) - e); |
| 149 |
return sum / e; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
// count bits in char |
| 155 |
|
| 156 |
static short int SumOfBits[] = { // sum of bits in a byte |
| 157 |
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, |
| 158 |
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| 159 |
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| 160 |
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| 161 |
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, |
| 162 |
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| 163 |
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, |
| 164 |
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 }; |
| 165 |
|
| 166 |
short int CountBits(char c) { return SumOfBits[(unsigned char)c]; } |
| 167 |
|
| 168 |
short int CountBits(short int i) |
| 169 |
{ |
| 170 |
return (short int)(SumOfBits[ (unsigned char)(i & 255) ] |
| 171 |
+ SumOfBits[ (unsigned char)(i >> 8) ]); |
| 172 |
} |
| 173 |
|
| 174 |
///@} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|