| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file tryrand6.cpp |
| 5 | /// Test of newran - stable rngs. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | |
| 10 | #include "include.h" |
| 11 | #include "newran.h" |
| 12 | #include "format.h" |
| 13 | #include "tryrand.h" |
| 14 | #include "utility.h" |
| 15 | #include "test_out.h" |
| 16 | |
| 17 | |
| 18 | |
| 19 | // ****** simple tests of Stable random number generators ****** |
| 20 | |
| 21 | |
| 22 | void CharFnTest(Real u, Real alpha, Real beta, int N, |
| 23 | Stable::Notation notation = Stable::Kalpha); |
| 24 | |
| 25 | |
| 26 | void test6(int N) |
| 27 | { |
| 28 | cout << endl << endl; |
| 29 | |
| 30 | cout << "Testing Stable distributions" << endl; |
| 31 | cout << endl; |
| 32 | cout << "Compares estimated values of characteristic function with" << endl; |
| 33 | cout << "theoretical values producing approximately normally" << endl; |
| 34 | cout << "distributed test statistics;"<< endl; |
| 35 | cout << "param.1 and param.2 show the mean and s.d. of the test statistic;" |
| 36 | << endl; |
| 37 | cout << "statistic shows the test statistic value;" << endl; |
| 38 | cout << "n. stat shows the normalised value;" << endl; |
| 39 | cout << "-lg sig shows - log10 of the significance probability." << endl; |
| 40 | cout << "Should be less than 1.3 (5% sig) in most cases and" << endl; |
| 41 | cout << "2 (1% sig) in almost all cases." << endl; |
| 42 | cout << endl; |
| 43 | cout << "* shows 5% significance; ** shows 1% significance;" << endl; |
| 44 | cout << "*** shows 0.1% significance." << endl; |
| 45 | cout << endl; |
| 46 | |
| 47 | cout << BaseTest::Header << endl; |
| 48 | |
| 49 | CharFnTest(0.3, 0.2, 0, N, Stable::Kalpha); |
| 50 | CharFnTest(0.3, 0.5, 0, N, Stable::Kalpha); |
| 51 | CharFnTest(0.3, 1.0, 0, N, Stable::Kalpha); |
| 52 | CharFnTest(0.3, 1.5, 0, N, Stable::Kalpha); |
| 53 | CharFnTest(0.3, 2.0, 0, N, Stable::Kalpha); |
| 54 | |
| 55 | CharFnTest(0.5, 0.2, 0, N, Stable::Default); |
| 56 | CharFnTest(0.5, 0.5, 0, N, Stable::Default); |
| 57 | CharFnTest(0.5, 1.0, 0, N, Stable::Default); |
| 58 | CharFnTest(0.5, 1.5, 0, N, Stable::Default); |
| 59 | CharFnTest(0.5, 2.0, 0, N, Stable::Default); |
| 60 | |
| 61 | CharFnTest(0.7, 0.2, 0, N, Stable::Chambers); |
| 62 | CharFnTest(0.7, 0.5, 0, N, Stable::Chambers); |
| 63 | CharFnTest(0.7, 1.0, 0, N, Stable::Chambers); |
| 64 | CharFnTest(0.7, 1.5, 0, N, Stable::Chambers); |
| 65 | CharFnTest(0.7, 2.0, 0, N, Stable::Chambers); |
| 66 | |
| 67 | CharFnTest(1.0, 0.2, 0, N, Stable::Standard); |
| 68 | CharFnTest(1.0, 0.5, 0, N, Stable::Standard); |
| 69 | CharFnTest(1.0, 1.0, 0, N, Stable::Standard); |
| 70 | CharFnTest(1.0, 1.5, 0, N, Stable::Standard); |
| 71 | CharFnTest(1.0, 2.0, 0, N, Stable::Standard); |
| 72 | |
| 73 | CharFnTest(2.5, 0.2, 0, N, Stable::Kalpha); |
| 74 | CharFnTest(2.5, 0.5, 0, N, Stable::Kalpha); |
| 75 | CharFnTest(2.5, 1.0, 0, N, Stable::Kalpha); |
| 76 | CharFnTest(2.5, 1.5, 0, N, Stable::Kalpha); |
| 77 | CharFnTest(2.5, 2.0, 0, N, Stable::Kalpha); |
| 78 | |
| 79 | |
| 80 | CharFnTest(0.3, 0.2, 0.4, N, Stable::Kalpha); |
| 81 | CharFnTest(0.3, 0.5, 0.7, N, Stable::Standard); |
| 82 | CharFnTest(0.3, 1.0, -0.4, N, Stable::Chambers); |
| 83 | CharFnTest(0.3, 1.5, 0.3, N, Stable::Kalpha); |
| 84 | |
| 85 | CharFnTest(0.5, 0.2, -0.7, N, Stable::Chambers); |
| 86 | CharFnTest(0.5, 0.5, -0.2, N, Stable::Kalpha); |
| 87 | CharFnTest(0.5, 1.0, 0.9, N, Stable::Default); |
| 88 | CharFnTest(0.5, 1.5, -0.6, N, Stable::Chambers); |
| 89 | |
| 90 | CharFnTest(0.7, 0.2, -0.3, N, Stable::Standard); |
| 91 | CharFnTest(0.7, 0.5, 0.3, N, Stable::Chambers); |
| 92 | CharFnTest(0.7, 1.0, -0.4, N, Stable::Kalpha); |
| 93 | CharFnTest(0.7, 1.5, 0.8, N, Stable::Standard); |
| 94 | |
| 95 | CharFnTest(1.0, 0.2, 0.8, N, Stable::Kalpha); |
| 96 | CharFnTest(1.0, 0.5, -0.8, N, Stable::Standard); |
| 97 | CharFnTest(1.0, 1.0, 0.9, N, Stable::Chambers); |
| 98 | CharFnTest(1.0, 1.5, -0.2, N, Stable::Kalpha); |
| 99 | |
| 100 | CharFnTest(2.5, 0.2, -0.3, N, Stable::Chambers); |
| 101 | CharFnTest(2.5, 0.5, 0.3, N, Stable::Kalpha); |
| 102 | CharFnTest(2.5, 1.0, -0.6, N, Stable::Standard); |
| 103 | CharFnTest(2.5, 1.5, 0.4, N, Stable::Chambers); |
| 104 | |
| 105 | CharFnTest(0.5, 0.2, 1.00, N, Stable::Standard); |
| 106 | CharFnTest(0.5, 0.5, 1.00, N, Stable::Chambers); |
| 107 | CharFnTest(0.5, 1.0, 1.00, N, Stable::Kalpha); |
| 108 | CharFnTest(0.5, 1.5, 1.00, N, Stable::Standard); |
| 109 | CharFnTest(0.5, 2.0, 1.00, N, Stable::Chambers); |
| 110 | |
| 111 | CharFnTest(1.0, 0.2, -1.00, N, Stable::Kalpha); |
| 112 | CharFnTest(1.0, 0.5, -1.00, N, Stable::Standard); |
| 113 | CharFnTest(1.0, 1.0, -1.00, N, Stable::Chambers); |
| 114 | CharFnTest(1.0, 1.5, -1.00, N, Stable::Kalpha); |
| 115 | CharFnTest(1.0, 2.0, -1.00, N, Stable::Standard); |
| 116 | |
| 117 | CharFnTest(2.5, 0.2, 1.00, N, Stable::Kalpha); |
| 118 | CharFnTest(2.5, 0.5, 1.00, N, Stable::Kalpha); |
| 119 | CharFnTest(2.5, 1.0, 1.00, N, Stable::Standard); |
| 120 | CharFnTest(2.5, 1.5, 1.00, N, Stable::Chambers); |
| 121 | CharFnTest(2.5, 2.0, 1.00, N, Stable::Kalpha); |
| 122 | |
| 123 | |
| 124 | cout << endl; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | void CharFnTest(Real u, Real alpha, Real beta, int N, Stable::Notation notation) |
| 130 | { |
| 131 | Real au = fabs(u); |
| 132 | int su = (u > 0) ? 1 : (u < 0) ? -1 : 0; |
| 133 | Real re_f = 0.0, im_f = 0.0; |
| 134 | if (alpha != 1.0) |
| 135 | { |
| 136 | if (notation == Stable::Kalpha) |
| 137 | { |
| 138 | Real ka = 1.0 - fabs(1.0 - alpha); |
| 139 | Real arg = -1.570796327 * beta * ka * su; |
| 140 | Real px = - pow(au, alpha); |
| 141 | Real re_e = px * cos(arg); Real im_e = px * sin(arg); |
| 142 | Real av = exp(re_e); |
| 143 | re_f = av * cos(im_e); im_f = av * sin(im_e); |
| 144 | } |
| 145 | else if (notation == Stable::Standard) |
| 146 | { |
| 147 | Real re = - pow(au, alpha); |
| 148 | Real im = - re * beta * tan(1.570796327 * alpha) * su; |
| 149 | Real av = exp(re); |
| 150 | re_f = av * cos(im); im_f = av * sin(im); |
| 151 | } |
| 152 | else if (notation == Stable::Chambers) |
| 153 | { |
| 154 | Real re = - pow(au, alpha); |
| 155 | Real im = - (u + re * su) * beta * tan(1.570796327 * alpha); |
| 156 | Real av = exp(re); |
| 157 | re_f = av * cos(im); im_f = av * sin(im); |
| 158 | } |
| 159 | else if (notation == Stable::Default) |
| 160 | { |
| 161 | Real re = - pow(au, alpha); |
| 162 | re_f = exp(re); im_f = 0; |
| 163 | } |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | Real av = exp(-au); |
| 168 | Real im = - beta * log(au) * u / 1.570796327; |
| 169 | re_f = av * cos(im); im_f = av * sin(im); |
| 170 | } |
| 171 | |
| 172 | Real re_sum = 0.0, im_sum = 0.0; |
| 173 | Real re_sum_sq = 0.0, im_sum_sq = 0.0; |
| 174 | |
| 175 | Real count0 = 0; Real count1 = 0; Real count2 = 0; |
| 176 | |
| 177 | Stable stable(alpha, beta, notation); |
| 178 | |
| 179 | for (int i = 1; i <= N; ++i) |
| 180 | { |
| 181 | Real x = stable.Next(); |
| 182 | Real re, im; |
| 183 | if (fabs(x) > 1.0E12) re = im = 0.0; |
| 184 | else { re = cos(u * x); im = sin(u * x); } |
| 185 | re_sum += re; re_sum_sq += re * re; |
| 186 | im_sum += im; im_sum_sq += im * im; |
| 187 | if (x > 0) ++count0; |
| 188 | if (x > 1) ++count1; |
| 189 | if (x > 2) ++count2; |
| 190 | } |
| 191 | Real re_av = re_sum / N; |
| 192 | Real re_var = (re_sum_sq - re_av * re_sum) / (N-1) / N; |
| 193 | Real im_av = im_sum / N; |
| 194 | Real im_var = (im_sum_sq - im_av * im_sum) / (N-1) / N; |
| 195 | |
| 196 | //cout << "*** " << u << " " << alpha << " " << beta << " " << stable.beta_prime() << " ***" << endl; |
| 197 | NormalTestTwoSided ntts_re("Stable re_cf", re_av, re_f, re_var); |
| 198 | ntts_re.DoTest(); |
| 199 | NormalTestTwoSided ntts_im("Stable im_cf", im_av, im_f, im_var); |
| 200 | ntts_im.DoTest(); |
| 201 | //cout << setw(15) << setprecision(10) << count0 / N << " "; |
| 202 | //cout << setw(15) << setprecision(10) << count1 / N << " "; |
| 203 | //cout << setw(15) << setprecision(10) << count2 / N << endl; |
| 204 | |
| 205 | } |
| 206 | |
| 207 | ///@} |
| 208 |