| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file tryrand4.cpp |
| 5 | /// Test of newran - combinations and permutations. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | |
| 10 | #include "include.h" |
| 11 | #include "newran.h" |
| 12 | #include "str.h" |
| 13 | #include "test_out.h" |
| 14 | #include "format.h" |
| 15 | #include "tryrand.h" |
| 16 | #include "utility.h" |
| 17 | #include "array.h" |
| 18 | |
| 19 | |
| 20 | void test_perm(int N, int NN, unsigned int M); |
| 21 | |
| 22 | |
| 23 | void test4(int n) |
| 24 | { |
| 25 | cout << endl << endl; |
| 26 | cout << "Combinations and permutations" << endl; |
| 27 | cout << endl; |
| 28 | |
| 29 | cout << "Just checking that we get a valid permutation or combination" |
| 30 | << endl; |
| 31 | |
| 32 | { |
| 33 | cout << "Doing permutations" << endl; |
| 34 | RandomPermutation RP; |
| 35 | int i, j, k; |
| 36 | int p[10]; |
| 37 | |
| 38 | cout << "... select 10 items from 100...119 without replacement" << endl; |
| 39 | for (i = 1; i <= 10; i++) |
| 40 | { |
| 41 | RP.Next(20,10,p,100); |
| 42 | for (j = 0; j < 10; j++) |
| 43 | { |
| 44 | int pj = p[j]; |
| 45 | if (pj < 100 || pj > 119) |
| 46 | Throw(BaseException("Invalid permutation generated")); |
| 47 | for (k = 0; k < j; ++k) if (p[k] == pj) |
| 48 | Throw(BaseException("Invalid permutation generated")); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | cout << "... select 10 items from 100...109 without replacement" << endl; |
| 53 | for (i = 1; i <= 10; i++) |
| 54 | { |
| 55 | RP.Next(10,10,p,100); |
| 56 | for (j = 0; j < 10; j++) |
| 57 | { |
| 58 | int pj = p[j]; //cout << pj << " "; |
| 59 | if (pj < 100 || pj > 109) |
| 60 | Throw(BaseException("Invalid permutation generated")); |
| 61 | for (k = 0; k < j; ++k) if (p[k] == pj) |
| 62 | Throw(BaseException("Invalid permutation generated")); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | { |
| 68 | cout << "Doing combinations" << endl; |
| 69 | RandomCombination RC; |
| 70 | int i, j; |
| 71 | int p[10]; |
| 72 | |
| 73 | cout << "... select 10 items from 100...119 without replacement" << endl; |
| 74 | for (i = 1; i <= 10; i++) |
| 75 | { |
| 76 | RC.Next(20,10,p,100); |
| 77 | for (j = 0; j < 10; j++) |
| 78 | { |
| 79 | int pj = p[j]; |
| 80 | if (pj < 100 || pj > 119) |
| 81 | Throw(BaseException("Invalid permutation generated")); |
| 82 | if (j > 0 && pj <= p[j-1]) |
| 83 | Throw(BaseException("Invalid permutation generated")); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | cout << "... select 10 items from 100...109 without replacement" << endl; |
| 88 | for (i = 1; i <= 10; i++) |
| 89 | { |
| 90 | RC.Next(10,10,p,100); |
| 91 | for (j = 0; j < 10; j++) |
| 92 | { |
| 93 | int pj = p[j]; //cout << pj << " "; |
| 94 | if (pj < 100 || pj > 109) |
| 95 | Throw(BaseException("Invalid permutation generated")); |
| 96 | if (j > 0 && pj <= p[j-1]) |
| 97 | Throw(BaseException("Invalid permutation generated")); |
| 98 | } |
| 99 | //cout << "\n"; |
| 100 | } |
| 101 | //cout << "\n"; |
| 102 | } |
| 103 | |
| 104 | cout << endl; |
| 105 | cout << "Doing formal tests of permutation generator" << endl; |
| 106 | cout << endl; |
| 107 | cout << "Carries out a number of tests which generate approximately" << endl; |
| 108 | cout << "normally distributed test statistics;" << endl; |
| 109 | cout << "param. 1 and param. 2 show the mean and s.d. of the test statistic;" |
| 110 | << endl; |
| 111 | cout << "statistic shows the test statistic value;" << endl; |
| 112 | cout << "n. stat shows the normalised value;" << endl; |
| 113 | cout << "-lg sig shows -log10 of the significance probability." << endl; |
| 114 | cout << "Should be less than 1.3 (5% sig) in most cases and" << endl; |
| 115 | cout << "2 (1% sig) in almost all cases." << endl; |
| 116 | cout << endl; |
| 117 | cout << "* shows 5% significance; ** shows 1% significance;" << endl; |
| 118 | cout << "*** shows 0.1% significance." << endl; |
| 119 | |
| 120 | test_perm(1, 10, n * 10); |
| 121 | test_perm(4, 10, n * 10); |
| 122 | test_perm(7, 10, n * 10); |
| 123 | test_perm(10, 10, n * 10); |
| 124 | test_perm(1, 100, n); |
| 125 | test_perm(30, 100, n); |
| 126 | test_perm(65, 100, n); |
| 127 | test_perm(100, 100, n); |
| 128 | test_perm(100, 1000, n / 10); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | void test_perm(int N, int NN, unsigned int M) |
| 133 | { |
| 134 | int j; unsigned long i; double sum2 = 0; |
| 135 | Array<long> count(0,NN-1); // count number of times each ball is drawn |
| 136 | Array<int> last(0,NN-1); // details of last draw |
| 137 | long consec = 0; // count number of consecutive balls |
| 138 | last = 0; count = 0; // set the arrays to zero |
| 139 | |
| 140 | |
| 141 | cout << endl; |
| 142 | cout << "Size of urn = " << NN << "; "; |
| 143 | cout << "no. of balls = " << N << "; "; |
| 144 | cout << "no. of trials = " << M << endl; |
| 145 | cout << BaseTest::Header << endl; |
| 146 | if (N > NN) { Throw(Runtime_error("N > NN")); } |
| 147 | long double S0 = 0.0; // sum of ball numbers |
| 148 | long double S1 = 0.0; // sum of ball * draw |
| 149 | double centre = (NN+1) / 2.0; // average ball number |
| 150 | double avj = (N+1)/2.0; // average draw number |
| 151 | Array2<int> table1(0,NN-1,0,N-1); // times ball i occurs at draw j |
| 152 | Array2<int> table2(0,NN-1,0,NN-1); // times ball i and ball j both occur |
| 153 | table1 = 0; table2 = 0; // clear the tables |
| 154 | long turnings = 0; // number of turning points |
| 155 | RandomPermutation RP; // reset the Urn |
| 156 | Array<int> Draw(0,N-1); // for the draw |
| 157 | |
| 158 | |
| 159 | for (i=1; i<=M; i++) // iterate over total number of trials |
| 160 | { |
| 161 | int last_ball = -1; |
| 162 | RP.Next(NN,N,Draw.base(),1); // do the draw |
| 163 | Array<int> check(0,NN-1); check = 0; // for checking no repeats |
| 164 | for (j = 1; j <= N; j++) // N balls in draw |
| 165 | { |
| 166 | int x = Draw(j-1); |
| 167 | if (x<1 || x>NN) Throw(Runtime_error("Invalid number")); |
| 168 | check(x-1)++; count(x-1)++; sum2 += last(x-1); |
| 169 | if (x == last_ball+1 || x == last_ball-1) consec++; |
| 170 | last_ball = x; |
| 171 | double xc = x - centre; |
| 172 | S0 += xc; S1 += xc * (j-avj); |
| 173 | table1(x-1,j-1)++; |
| 174 | } |
| 175 | for (j=0; j<NN; j++) |
| 176 | { |
| 177 | if (check(j) > 1) Throw(Runtime_error("Repeated number")); |
| 178 | last(j) = check(j); |
| 179 | if (check(j)) // increment table of pairs |
| 180 | { |
| 181 | for (int k = j+1; k<NN; k++) if (check(k)) table2(j,k)++; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // count number of turnings |
| 186 | for (j = 2; j < N; j++) |
| 187 | { |
| 188 | long last = Draw(j-2); |
| 189 | long current = Draw(j-1); |
| 190 | long next = Draw(j); |
| 191 | if ( (last < current && next < current) || |
| 192 | (last > current && next > current) ) turnings++; |
| 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | // do the tests |
| 198 | double sum1 = 0.0; double d = M * (N / (double)NN); |
| 199 | for (j=0; j<NN; j++) { sum1 += square(count(j) - d); } |
| 200 | |
| 201 | if (N < NN) |
| 202 | { |
| 203 | sum1 /= d; |
| 204 | double sd = (NN - N) * sqrt( 2 * (M - 1) / (M * ((double)(NN - 1))) ); |
| 205 | NormalTestTwoSided freq_test("Ball freq.", sum1, NN - N, square(sd)); |
| 206 | freq_test.DoTest(); |
| 207 | |
| 208 | NormalTestTwoSided repeats_test("Repeats", sum2, (M - 1) * (N * N / (double)NN), |
| 209 | square((double)(N * (NN - N)) / (double)NN) * (M - 1) / (NN - 1) ); |
| 210 | repeats_test.DoTest(); |
| 211 | |
| 212 | double V0 = ((double)N * (NN+1.0) * (NN-N))/12.0; |
| 213 | NormalTestTwoSided average_test("Average", S0, 0, M * V0); |
| 214 | average_test.DoTest(); |
| 215 | } |
| 216 | |
| 217 | if (N > 1) |
| 218 | { |
| 219 | double V1 = (double)N * (N*N - 1) * NN * (NN + 1) / 144.0; |
| 220 | NormalTestTwoSided ball_times_draw("Ball * draw", S1, 0, M * V1); |
| 221 | ball_times_draw.DoTest(); |
| 222 | |
| 223 | double e = 2 * M * (N - 1) / (double)NN; |
| 224 | // expected # of consecutive balls |
| 225 | NormalTestTwoSided consec_test("Consec balls", consec, e, e); |
| 226 | consec_test.DoTest(); |
| 227 | } |
| 228 | |
| 229 | if (N > 1) |
| 230 | { |
| 231 | // balls vs draws |
| 232 | double CS = 0; double ev = (double)M / NN; |
| 233 | for (int i = 0; i < NN; i++) for (int j = 0; j < N; j++) |
| 234 | CS += square(table1(i,j)-ev); |
| 235 | CS /= ev; ev = N * (NN - 1); |
| 236 | double v = 2.0 * N * (M - 1) * (N - (2 - NN) * NN) / M / (NN - 1); |
| 237 | NormalTestTwoSided ball_draw("Ball vs draw", CS, ev, v); |
| 238 | ball_draw.DoTest(); |
| 239 | } |
| 240 | |
| 241 | if (N>1 && N < NN) |
| 242 | { |
| 243 | // pairs of balls |
| 244 | double CS = 0; double ev = (double)M*N*(N-1)/ NN / (NN-1); |
| 245 | for (int i = 0; i < NN; i++) for (int j = i+1; j < NN; j++) |
| 246 | CS += square(table2(i,j)-ev); |
| 247 | CS /= ev; ev = (NN-N)*(NN+N-1)/2.0; |
| 248 | double v = (double)(M-1)*square(NN-N)*(-3+6*N-3.0*N*N+2*NN-6.0*N*NN+ |
| 249 | 2.0*N*N*NN+NN*NN)/(M*(NN-2)*(NN-3)); |
| 250 | NormalTestTwoSided pairs_test("Pairs table", CS, ev, v); |
| 251 | pairs_test.DoTest(); |
| 252 | } |
| 253 | |
| 254 | if (N > 2) |
| 255 | { |
| 256 | double V2 = (16.0 * N - 29.0) / 90.0; |
| 257 | NormalTestTwoSided runs_test("Runs test", turnings, |
| 258 | (double)M * (N-2) / 1.5, M * V2); |
| 259 | runs_test.DoTest(); |
| 260 | } |
| 261 | |
| 262 | } |
| 263 | |
| 264 | ///@} |
| 265 | |
| 266 |