| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file utility.cpp |
| 5 | /// Some miscellanous functions I need - bodies. |
| 6 | |
| 7 | #define WANT_MATH |
| 8 | #define WANT_STREAM |
| 9 | #define WANT_TIME |
| 10 | |
| 11 | #include "include.h" |
| 12 | #include "myexcept.h" |
| 13 | #include "utility.h" |
| 14 | #include "format.h" |
| 15 | |
| 16 | #ifdef use_namespace |
| 17 | using namespace RBD_STRING; |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | |
| 22 | double invchi95(int N) |
| 23 | // upper 95% point of chi-squared distribution |
| 24 | { |
| 25 | if (N < 0) |
| 26 | Throw(Invalid_argument("Error in invchi95 arg")); |
| 27 | if (N < 30) |
| 28 | { |
| 29 | double Q[] = { 0, 3.841459, 5.991465, 7.814728, 9.487729, 11.0705, |
| 30 | 12.59159, 14.06714, 15.50731, 16.91898, 18.30704, 19.67506, |
| 31 | 21.02601, 22.36199, 23.68475, 24.99576, 26.2962, 27.58709, |
| 32 | 28.86928, 30.14351, 31.4104, 32.6705, 33.9244, 35.1725, |
| 33 | 36.4151, 37.6525, 38.8852, 40.1133, 41.3372, 42.5569 }; |
| 34 | return Q[N]; |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | double A = 1.0/(4.5 * N); double H = (-0.0002 * 60)/N; |
| 39 | double Q = N * cube(1 - A + (1.645 - H) * sqrt(A)); |
| 40 | return Q; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | double invchi99(int N) |
| 45 | // upper 99% point of chi-squared distribution |
| 46 | { |
| 47 | if (N < 0) |
| 48 | Throw(Invalid_argument("Error in invchi99 arg")); |
| 49 | if (N < 30) |
| 50 | { |
| 51 | double Q[] = { 0, 6.63490, 9.21034, 11.3449, 13.2767, 15.0863, |
| 52 | 16.8119, 18.4753, 20.0902, 21.6660, 23.2093, 24.7250, |
| 53 | 26.2170, 27.6883, 29.1413, 30.5779, 31.9999, 33.4087, |
| 54 | 34.8053, 36.1908, 37.5662, 38.9321, 40.2894, 41.6384, |
| 55 | 42.9798, 44.3141, 45.6417, 46.9630, 48.2782, 49.5879 }; |
| 56 | return Q[N]; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | double A = 1.0/(4.5 * N); double H = (0.0008 * 60)/N; |
| 61 | double Q = N * cube(1 - A + (2.326 - H) * sqrt(A)); |
| 62 | return Q; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | double invchi999(int N) |
| 67 | // upper 99.9% point of chi-squared distribution |
| 68 | // not perfect but good enough here |
| 69 | { |
| 70 | if (N < 0) |
| 71 | Throw(Invalid_argument("Error in invchi999 arg")); |
| 72 | if (N < 30) |
| 73 | { |
| 74 | double Q[] = { 0, 10.828, 13.816, 16.266, 18.467, 20.515, |
| 75 | 22.458, 24.322, 26.125, 27.877, 29.588, 31.264, |
| 76 | 32.909, 34.528, 36.123, 37.697, 39.252, 40.790, |
| 77 | 42.312, 43.820, 45.315, 46.797, 48.268, 49.728, |
| 78 | 51.179, 52.620, 54.052, 55.476, 56.892, 58.302 }; |
| 79 | |
| 80 | return Q[N]; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | double A = 1.0/(4.5 * N); double H = (0.0045 * 60)/N; |
| 85 | double Q = N * cube(1 - A + (3.090 - H) * sqrt(A)); |
| 86 | return Q; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // cdf of normal distribution function |
| 91 | // From A.R. Curtis: In Methods of Numerical Approximation (1966) |
| 92 | // edited by D.C. Handscomb |
| 93 | // maximum relative error is bounded, but not good near x = 0 |
| 94 | double cdfnml(double x) |
| 95 | { |
| 96 | double c[]={ .592364,1.44485,-.193396,2.66832,3.73438,-1.73883 }; |
| 97 | double ax=fabs(x); |
| 98 | double cdf=exp(-.5*square(ax))*(c[0]/(ax+c[1])+c[2]*(ax+c[5]) |
| 99 | /((ax+c[3])*ax+c[4])); |
| 100 | if(x>=0.0) return 1.0-cdf; else return cdf; |
| 101 | } |
| 102 | |
| 103 | // inverse of cdf of normal distribution function |
| 104 | // Abramowitz & Stegun, 26.2.23 |
| 105 | double invcdfnml(double p) |
| 106 | { |
| 107 | double c[] = { 2.515517, 0.802853, 0.010328 }; |
| 108 | double d[] = { 1, 1.432788, 0.189269, 0.001308 }; |
| 109 | bool small = true; |
| 110 | if (p > 0.5) { p = 1.0 - p; small = false; } |
| 111 | if (p < 0.0) Throw(Invalid_argument("Error in invcdfnml arg")); |
| 112 | double t = sqrt(-2.0*log(p)); |
| 113 | double x = t - (c[0] + t * (c[1] + t * c[2])) |
| 114 | / (d[0] + t * (d[1] + t * (d[2] + t * d[3]))); |
| 115 | if (small) return - x; else return x; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | //*********************** chi-squared cdf ************************************ |
| 121 | |
| 122 | static double ln_gamma(double xx) |
| 123 | { |
| 124 | // log gamma function adapted from numerical recipes in C |
| 125 | |
| 126 | if (xx<1.0) // Use reflection formula |
| 127 | { |
| 128 | double piz = 3.14159265359 * (1.0-xx); |
| 129 | return log(piz/sin(piz))-ln_gamma(2.0-xx); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | static double cof[6]={76.18009173,-86.50532033,24.01409822, |
| 134 | -1.231739516,0.120858003e-2,-0.536382e-5}; |
| 135 | |
| 136 | double x=xx-1.0; double tmp=x+5.5; |
| 137 | tmp -= (x+0.5)*log(tmp); double ser=1.0; |
| 138 | for (int j=0;j<=5;j++) { x += 1.0; ser += cof[j]/x; } |
| 139 | return -tmp+log(2.50662827465*ser); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |
| 145 | // method good for x >> df |
| 146 | // also use for small df, but should sum in other direction |
| 147 | // need to do more work on accuracy |
| 148 | static Real cdf_chisq_large(Real x, int df, bool upper) |
| 149 | { |
| 150 | Real df2 = df / 2.0; Real x2 = x / 2.0; |
| 151 | Real term = exp((df2-1) * log(x2) - x2 - ln_gamma(df2)); |
| 152 | Real sum = term; |
| 153 | if (sum == 0) return upper ? 0.0 : 1.0; |
| 154 | for (;;) |
| 155 | { |
| 156 | df -= 2; |
| 157 | if (df == 1) |
| 158 | { sum += 2.0 * cdfnml(-sqrt(x)); return upper ? sum : 1.0 - sum; } |
| 159 | Real ratio = df / x; term *= ratio; sum += term; |
| 160 | if (ratio < 1.0) |
| 161 | { |
| 162 | if (upper) { if (term < 0.00001 * sum * (1 - ratio)) return sum; } |
| 163 | else if (term < 0.000001 * (1 - ratio)) return 1.0 - sum; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // method good for x << df - need to do more work on accuracy |
| 169 | static Real cdf_chisq_small(Real x, int df, bool upper) |
| 170 | { |
| 171 | df += 2; |
| 172 | Real df2 = df / 2.0; Real x2 = x / 2.0; |
| 173 | Real term = exp((df2-1) * log(x2) - x2 - ln_gamma(df2)); |
| 174 | Real sum = term; |
| 175 | if (sum == 0) return upper ? 1.0 : 0.0; |
| 176 | for (;;) |
| 177 | { |
| 178 | Real ratio = x / df; df += 2; term *= ratio; sum += term; |
| 179 | if (ratio < 1.0) |
| 180 | { |
| 181 | if (upper) { if (term < 0.000001 * (1 - ratio)) return 1.0 - sum; } |
| 182 | else if (term < 0.00001 * sum * (1 - ratio)) return sum; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // method good for x << 1, reduces round-off error for very low x |
| 188 | static Real cdf_chisq_very_small(Real x, int df) |
| 189 | { |
| 190 | Real df2 = df / 2.0; Real x2 = x / 2.0; |
| 191 | Real term = 2.0 * exp(df2 * log(x2) - ln_gamma(df2)); Real sum = term / df; |
| 192 | if (sum == 0) return 0.0; |
| 193 | for (int i = 1;; i++) |
| 194 | { |
| 195 | df += 2; term *= - x2 / i; Real addon = term / df; sum += addon; |
| 196 | if (fabs(addon) < 0.00001 * sum) return sum; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | // evaluate cdf of central chi-squared rv; |
| 202 | // do upper tail if upper is true |
| 203 | // need to set accuracy better but this is good enough here |
| 204 | // need to upgrade cdfnml |
| 205 | // inefficient for large df, x near df. |
| 206 | |
| 207 | Real cdf_chisq(Real x, int df, bool upper) |
| 208 | { |
| 209 | if (df <= 0) Throw(Out_of_range("Non-positive df in cdf_chisq")); |
| 210 | if (x <= 0) return upper ? 1.0 : 0.0; |
| 211 | if (x <= 0.5) |
| 212 | { |
| 213 | // better for !upper but cdfnml is giving suffient error for |
| 214 | // upper to also be a problem with odd df |
| 215 | Real sum = cdf_chisq_very_small(x, df); |
| 216 | return upper ? 1.0 - sum : sum; |
| 217 | } |
| 218 | if (df == 1) |
| 219 | { Real sum = 2.0 * cdfnml(-sqrt(x)); return upper ? sum : 1.0 - sum; } |
| 220 | if (df == 2) |
| 221 | { Real sum = exp(- x / 2); return upper ? sum : 1.0 - sum; } |
| 222 | // would like to do this with larger df but cdfnml is a problem |
| 223 | if (df < 3 || x > df) return cdf_chisq_large(x, df, upper); |
| 224 | return cdf_chisq_small(x, df, upper); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | // upper tail probabilities for 2 sided Kolmogorov-Smirnov test |
| 229 | // Stuart & Ord: Advanced Theory of Statistics, vol2 p1188 |
| 230 | |
| 231 | double KS_probabilities(double z) |
| 232 | { |
| 233 | if (z <= 0) |
| 234 | Throw(Out_of_range("Non-positive argument in KS_probabilities")); |
| 235 | double sum = 0.0; int sign = 1; |
| 236 | for (int r = 1; r <= 10000; r++) |
| 237 | { |
| 238 | double term = exp(-2.0 * square(r * z)); |
| 239 | sum += sign * term; sign = - sign; |
| 240 | if (term <= 0.000001) return 2.0 * sum; |
| 241 | } |
| 242 | Throw(Runtime_error("Convergence error in KS_probabilities")); |
| 243 | #ifndef UseExceptions |
| 244 | return -1; |
| 245 | #endif |
| 246 | } |
| 247 | |
| 248 | |
| 249 | //************** elapsed time class **************** |
| 250 | |
| 251 | time_lapse::time_lapse() |
| 252 | { |
| 253 | start_time = ((double)clock())/(double)CLOCKS_PER_SEC; |
| 254 | } |
| 255 | |
| 256 | time_lapse::~time_lapse() |
| 257 | { |
| 258 | Format F; F.FormatType(Format::DEC_FIGS); F.Precision(1); |
| 259 | double time = ((double)clock())/(double)CLOCKS_PER_SEC - start_time; |
| 260 | cout << "Elapsed (processor) time = " << F << time << " seconds" << endl; |
| 261 | cout << endl; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | |
| 266 | ///@} |
| 267 |