| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include <esl_config.h> |
| 15 |
|
| 16 |
#include <math.h> |
| 17 |
|
| 18 |
#include "easel.h" |
| 19 |
#include "esl_stats.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
int |
| 46 |
esl_stats_DMean(const double *x, int n, double *opt_mean, double *opt_var) |
| 47 |
{ |
| 48 |
double sum = 0.; |
| 49 |
double sqsum = 0.; |
| 50 |
int i; |
| 51 |
|
| 52 |
for (i = 0; i < n; i++) |
| 53 |
{ |
| 54 |
sum += x[i]; |
| 55 |
sqsum += x[i]*x[i]; |
| 56 |
} |
| 57 |
if (opt_mean != NULL) *opt_mean = sum / (double) n; |
| 58 |
if (opt_var != NULL) *opt_var = (n > 1 ? fabs((sqsum - sum*sum/(double)n) / ((double)n-1)) : 0.); |
| 59 |
return eslOK; |
| 60 |
} |
| 61 |
int |
| 62 |
esl_stats_FMean(const float *x, int n, double *opt_mean, double *opt_var) |
| 63 |
{ |
| 64 |
double sum = 0.; |
| 65 |
double sqsum = 0.; |
| 66 |
int i; |
| 67 |
|
| 68 |
for (i = 0; i < n; i++) |
| 69 |
{ |
| 70 |
sum += x[i]; |
| 71 |
sqsum += x[i]*x[i]; |
| 72 |
} |
| 73 |
if (opt_mean != NULL) *opt_mean = sum / (double) n; |
| 74 |
if (opt_var != NULL) *opt_var = (n > 1 ? fabs((sqsum - sum*sum/(double)n) / ((double)n-1)) : 0.); |
| 75 |
return eslOK; |
| 76 |
} |
| 77 |
int |
| 78 |
esl_stats_IMean(const int *x, int n, double *opt_mean, double *opt_var) |
| 79 |
{ |
| 80 |
double sum = 0.; |
| 81 |
double sqsum = 0.; |
| 82 |
int i; |
| 83 |
|
| 84 |
for (i = 0; i < n; i++) |
| 85 |
{ |
| 86 |
sum += x[i]; |
| 87 |
sqsum += x[i]*x[i]; |
| 88 |
} |
| 89 |
if (opt_mean != NULL) *opt_mean = sum / (double) n; |
| 90 |
if (opt_var != NULL) *opt_var = (n > 1 ? fabs((sqsum - sum*sum/(double)n) / ((double)n-1)) : 0.); |
| 91 |
return eslOK; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
int |
| 119 |
esl_stats_LogGamma(double x, double *ret_answer) |
| 120 |
{ |
| 121 |
int i; |
| 122 |
double xx, tx; |
| 123 |
double tmp, value; |
| 124 |
static double cof[11] = { |
| 125 |
4.694580336184385e+04, |
| 126 |
-1.560605207784446e+05, |
| 127 |
2.065049568014106e+05, |
| 128 |
-1.388934775095388e+05, |
| 129 |
5.031796415085709e+04, |
| 130 |
-9.601592329182778e+03, |
| 131 |
8.785855930895250e+02, |
| 132 |
-3.155153906098611e+01, |
| 133 |
2.908143421162229e-01, |
| 134 |
-2.319827630494973e-04, |
| 135 |
1.251639670050933e-10 |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
if (x <= 0.0) ESL_EXCEPTION(eslERANGE, "invalid x <= 0 in esl_stats_LogGamma()"); |
| 140 |
|
| 141 |
xx = x - 1.0; |
| 142 |
tx = tmp = xx + 11.0; |
| 143 |
value = 1.0; |
| 144 |
for (i = 10; i >= 0; i--) |
| 145 |
{ |
| 146 |
value += cof[i] / tmp; |
| 147 |
tmp -= 1.0; |
| 148 |
} |
| 149 |
value = log(value); |
| 150 |
tx += 0.5; |
| 151 |
value += 0.918938533 + (xx+0.5)*log(tx) - tx; |
| 152 |
*ret_answer = value; |
| 153 |
return eslOK; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
int |
| 170 |
esl_stats_Psi(double x, double *ret_answer) |
| 171 |
{ |
| 172 |
double psi = 0.; |
| 173 |
double x2; |
| 174 |
|
| 175 |
if (x <= 0.0) ESL_EXCEPTION(eslERANGE, "invalid x <= 0 in esl_stats_Psi()"); |
| 176 |
else if (x <= 1e-5) { *ret_answer = -eslCONST_EULER - 1./x; return eslOK; } |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
while (x < 8.5) |
| 182 |
{ |
| 183 |
psi -= 1./x; |
| 184 |
x += 1.; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
x2 = 1./x; psi += log(x) - 0.5 * x2; |
| 189 |
x2 = x2*x2; psi += (-1./12.)*x2 + (1./120.)*x2*x2 - (1./252.)*x2*x2*x2; |
| 190 |
|
| 191 |
*ret_answer = psi; |
| 192 |
return eslOK; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
int |
| 210 |
esl_stats_Trigamma(double x, double *ret_answer) |
| 211 |
{ |
| 212 |
double trigam = 0.; |
| 213 |
double y; |
| 214 |
|
| 215 |
if (x <= 0.) ESL_EXCEPTION(eslERANGE, "invalid x <= 0 in esl_stats_Trigamma()"); |
| 216 |
else if (x <= 1.0e-4) { *ret_answer = 1.0 / (x*x); return eslOK; } |
| 217 |
|
| 218 |
while (x < 5.0) |
| 219 |
{ |
| 220 |
trigam += 1. / (x*x); |
| 221 |
x += 1.; |
| 222 |
} |
| 223 |
|
| 224 |
y = 1.0 / (x*x); |
| 225 |
trigam += 0.5 * y + (1. + y*((1./6.) + y*((1./30.)+ y*((1./42.)+ y*(1./30.))))) / x; |
| 226 |
|
| 227 |
*ret_answer = trigam; |
| 228 |
return eslOK; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
int |
| 267 |
esl_stats_IncompleteGamma(double a, double x, double *ret_pax, double *ret_qax) |
| 268 |
{ |
| 269 |
int iter; |
| 270 |
double pax; |
| 271 |
double qax; |
| 272 |
int status; |
| 273 |
|
| 274 |
if (a <= 0.) ESL_EXCEPTION(eslERANGE, "esl_stats_IncompleteGamma(): a must be > 0"); |
| 275 |
if (x < 0.) ESL_EXCEPTION(eslERANGE, "esl_stats_IncompleteGamma(): x must be >= 0"); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
if (x > a+1) |
| 282 |
{ |
| 283 |
double oldp; |
| 284 |
double nu0, nu1; |
| 285 |
double de0, de1; |
| 286 |
|
| 287 |
nu0 = 0.; |
| 288 |
de0 = 1.; |
| 289 |
nu1 = 1.; |
| 290 |
de1 = x; |
| 291 |
|
| 292 |
oldp = nu1; |
| 293 |
for (iter = 1; iter < 100; iter++) |
| 294 |
{ |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
nu0 = nu1 + ((double)iter - a) * nu0; |
| 303 |
de0 = de1 + ((double)iter - a) * de0; |
| 304 |
|
| 305 |
|
| 306 |
nu1 = x * nu0 + (double) iter * nu1; |
| 307 |
de1 = x * de0 + (double) iter * de1; |
| 308 |
|
| 309 |
if (de1 != 0.) |
| 310 |
{ |
| 311 |
nu0 /= de1; |
| 312 |
de0 /= de1; |
| 313 |
nu1 /= de1; |
| 314 |
de1 = 1.; |
| 315 |
} |
| 316 |
|
| 317 |
if (fabs((nu1-oldp)/nu1) < 1.e-7) |
| 318 |
{ |
| 319 |
if ((status = esl_stats_LogGamma(a, &qax)) != eslOK) return status; |
| 320 |
qax = nu1 * exp(a * log(x) - x - qax); |
| 321 |
|
| 322 |
if (ret_pax != NULL) *ret_pax = 1 - qax; |
| 323 |
if (ret_qax != NULL) *ret_qax = qax; |
| 324 |
return eslOK; |
| 325 |
} |
| 326 |
|
| 327 |
oldp = nu1; |
| 328 |
} |
| 329 |
ESL_EXCEPTION(eslENOHALT, |
| 330 |
"esl_stats_IncompleteGamma(): fraction failed to converge"); |
| 331 |
} |
| 332 |
else |
| 333 |
{ |
| 334 |
double p; |
| 335 |
double val; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
p = val = 1. / a; |
| 347 |
for (iter = 1; iter < 10000; iter++) |
| 348 |
{ |
| 349 |
val *= x / (a+(double)iter); |
| 350 |
p += val; |
| 351 |
|
| 352 |
if (fabs(val/p) < 1.e-7) |
| 353 |
{ |
| 354 |
if ((status = esl_stats_LogGamma(a, &pax)) != eslOK) return status; |
| 355 |
pax = p * exp(a * log(x) - x - pax); |
| 356 |
|
| 357 |
if (ret_pax != NULL) *ret_pax = pax; |
| 358 |
if (ret_qax != NULL) *ret_qax = 1. - pax; |
| 359 |
return eslOK; |
| 360 |
} |
| 361 |
} |
| 362 |
ESL_EXCEPTION(eslENOHALT, |
| 363 |
"esl_stats_IncompleteGamma(): series failed to converge"); |
| 364 |
} |
| 365 |
|
| 366 |
return eslOK; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
double |
| 408 |
esl_stats_erfc(double x) |
| 409 |
{ |
| 410 |
static const double erx = 8.45062911510467529297e-01; |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
static const double pp0 = 1.28379167095512558561e-01; |
| 415 |
static const double pp1 = -3.25042107247001499370e-01; |
| 416 |
static const double pp2 = -2.84817495755985104766e-02; |
| 417 |
static const double pp3 = -5.77027029648944159157e-03; |
| 418 |
static const double pp4 = -2.37630166566501626084e-05; |
| 419 |
static const double qq1 = 3.97917223959155352819e-01; |
| 420 |
static const double qq2 = 6.50222499887672944485e-02; |
| 421 |
static const double qq3 = 5.08130628187576562776e-03; |
| 422 |
static const double qq4 = 1.32494738004321644526e-04; |
| 423 |
static const double qq5 = -3.96022827877536812320e-06; |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
static const double pa0 = -2.36211856075265944077e-03; |
| 428 |
static const double pa1 = 4.14856118683748331666e-01; |
| 429 |
static const double pa2 = -3.72207876035701323847e-01; |
| 430 |
static const double pa3 = 3.18346619901161753674e-01; |
| 431 |
static const double pa4 = -1.10894694282396677476e-01; |
| 432 |
static const double pa5 = 3.54783043256182359371e-02; |
| 433 |
static const double pa6 = -2.16637559486879084300e-03; |
| 434 |
static const double qa1 = 1.06420880400844228286e-01; |
| 435 |
static const double qa2 = 5.40397917702171048937e-01; |
| 436 |
static const double qa3 = 7.18286544141962662868e-02; |
| 437 |
static const double qa4 = 1.26171219808761642112e-01; |
| 438 |
static const double qa5 = 1.36370839120290507362e-02; |
| 439 |
static const double qa6 = 1.19844998467991074170e-02; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
static const double ra0 = -9.86494403484714822705e-03; |
| 444 |
static const double ra1 = -6.93858572707181764372e-01; |
| 445 |
static const double ra2 = -1.05586262253232909814e+01; |
| 446 |
static const double ra3 = -6.23753324503260060396e+01; |
| 447 |
static const double ra4 = -1.62396669462573470355e+02; |
| 448 |
static const double ra5 = -1.84605092906711035994e+02; |
| 449 |
static const double ra6 = -8.12874355063065934246e+01; |
| 450 |
static const double ra7 = -9.81432934416914548592e+00; |
| 451 |
static const double sa1 = 1.96512716674392571292e+01; |
| 452 |
static const double sa2 = 1.37657754143519042600e+02; |
| 453 |
static const double sa3 = 4.34565877475229228821e+02; |
| 454 |
static const double sa4 = 6.45387271733267880336e+02; |
| 455 |
static const double sa5 = 4.29008140027567833386e+02; |
| 456 |
static const double sa6 = 1.08635005541779435134e+02; |
| 457 |
static const double sa7 = 6.57024977031928170135e+00; |
| 458 |
static const double sa8 = -6.04244152148580987438e-02; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
static const double rb0 = -9.86494292470009928597e-03; |
| 463 |
static const double rb1 = -7.99283237680523006574e-01; |
| 464 |
static const double rb2 = -1.77579549177547519889e+01; |
| 465 |
static const double rb3 = -1.60636384855821916062e+02; |
| 466 |
static const double rb4 = -6.37566443368389627722e+02; |
| 467 |
static const double rb5 = -1.02509513161107724954e+03; |
| 468 |
static const double rb6 = -4.83519191608651397019e+02; |
| 469 |
static const double sb1 = 3.03380607434824582924e+01; |
| 470 |
static const double sb2 = 3.25792512996573918826e+02; |
| 471 |
static const double sb3 = 1.53672958608443695994e+03; |
| 472 |
static const double sb4 = 3.19985821950859553908e+03; |
| 473 |
static const double sb5 = 2.55305040643316442583e+03; |
| 474 |
static const double sb6 = 4.74528541206955367215e+02; |
| 475 |
static const double sb7 = -2.24409524465858183362e+01; |
| 476 |
|
| 477 |
int hx,ix; |
| 478 |
double R,S,P,Q,s,y,z,r; |
| 479 |
|
| 480 |
ESL_GET_HIGHWORD(hx, x); |
| 481 |
ix = hx & 0x7fffffff; |
| 482 |
if (ix>=0x7ff00000) |
| 483 |
return (double)(((unsigned)hx>>31)<<1)+1.0/x; |
| 484 |
|
| 485 |
if (ix < 0x3feb0000) |
| 486 |
{ |
| 487 |
if (ix < 0x3c700000) return 1.0-x; |
| 488 |
z = x*x; |
| 489 |
r = pp0+z*(pp1+z*(pp2+z*(pp3+z*pp4))); |
| 490 |
s = 1.0+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5)))); |
| 491 |
y = r/s; |
| 492 |
if (hx < 0x3fd00000) |
| 493 |
{ |
| 494 |
return 1.0-(x+x*y); |
| 495 |
} |
| 496 |
else |
| 497 |
{ |
| 498 |
r = x*y; |
| 499 |
r += (x-0.5); |
| 500 |
return 0.5 - r ; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
if (ix < 0x3ff40000) |
| 505 |
{ |
| 506 |
s = fabs(x)-1.0; |
| 507 |
P = pa0+s*(pa1+s*(pa2+s*(pa3+s*(pa4+s*(pa5+s*pa6))))); |
| 508 |
Q = 1.0+s*(qa1+s*(qa2+s*(qa3+s*(qa4+s*(qa5+s*qa6))))); |
| 509 |
if (hx>=0) |
| 510 |
{ |
| 511 |
z = 1.0-erx; |
| 512 |
return z - P/Q; |
| 513 |
} |
| 514 |
else |
| 515 |
{ |
| 516 |
z = erx+P/Q; |
| 517 |
return 1.0+z; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if (ix < 0x403c0000) |
| 522 |
{ |
| 523 |
x = fabs(x); |
| 524 |
s = 1.0/(x*x); |
| 525 |
if (ix< 0x4006DB6D) |
| 526 |
{ |
| 527 |
R = ra0+s*(ra1+s*(ra2+s*(ra3+s*(ra4+s*(ra5+s*(ra6+s*ra7)))))); |
| 528 |
S = 1.0+s*(sa1+s*(sa2+s*(sa3+s*(sa4+s*(sa5+s*(sa6+s*(sa7+s*sa8))))))); |
| 529 |
} |
| 530 |
else |
| 531 |
{ |
| 532 |
if (hx < 0 && ix >= 0x40180000) return 2.0; |
| 533 |
R = rb0+s*(rb1+s*(rb2+s*(rb3+s*(rb4+s*(rb5+s*rb6))))); |
| 534 |
S = 1.0+s*(sb1+s*(sb2+s*(sb3+s*(sb4+s*(sb5+s*(sb6+s*sb7)))))); |
| 535 |
} |
| 536 |
z = x; |
| 537 |
ESL_SET_LOWWORD(z, 0); |
| 538 |
r = exp(-z*z-0.5625) * exp((z-x)*(z+x)+R/S); |
| 539 |
|
| 540 |
if (hx>0) return r/x; |
| 541 |
else return 2.0-r/x; |
| 542 |
} |
| 543 |
else |
| 544 |
{ |
| 545 |
if (hx>0) return 0.; |
| 546 |
else return 2.0; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
int |
| 587 |
esl_stats_GTest(int ca, int na, int cb, int nb, double *ret_G, double *ret_P) |
| 588 |
{ |
| 589 |
double a,b,c,d,n; |
| 590 |
double G = 0.; |
| 591 |
|
| 592 |
a = (double) ca; |
| 593 |
b = (double) (na - ca); |
| 594 |
c = (double) cb; |
| 595 |
d = (double) (nb - cb); |
| 596 |
n = (double) na+nb; |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (a > 0.) G = a * log(a); |
| 604 |
if (b > 0.) G += b * log(b); |
| 605 |
if (c > 0.) G += c * log(c); |
| 606 |
if (d > 0.) G += d * log(d); |
| 607 |
if (n > 0.) G += n * log(n); |
| 608 |
if (a+b > 0.) G -= (a+b) * log(a+b); |
| 609 |
if (c+d > 0.) G -= (c+d) * log(c+d); |
| 610 |
if (a+c > 0.) G -= (a+c) * log(a+c); |
| 611 |
if (b+d > 0.) G -= (b+d) * log(b+d); |
| 612 |
|
| 613 |
*ret_G = G; |
| 614 |
return esl_stats_IncompleteGamma( 0.5, G, NULL, ret_P); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
int |
| 639 |
esl_stats_ChiSquaredTest(int v, double x, double *ret_answer) |
| 640 |
{ |
| 641 |
return esl_stats_IncompleteGamma((double)v/2., x/2., NULL, ret_answer); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
int |
| 710 |
esl_stats_LinearRegression(const double *x, const double *y, const double *sigma, int n, |
| 711 |
double *opt_a, double *opt_b, |
| 712 |
double *opt_sigma_a, double *opt_sigma_b, double *opt_cov_ab, |
| 713 |
double *opt_cc, double *opt_Q) |
| 714 |
{ |
| 715 |
int status; |
| 716 |
double *t = NULL; |
| 717 |
double S, Sx, Sy, Stt; |
| 718 |
double Sxy, Sxx, Syy; |
| 719 |
double a, b, sigma_a, sigma_b, cov_ab, cc, X2, Q; |
| 720 |
double xdev, ydev; |
| 721 |
double tmp; |
| 722 |
int i; |
| 723 |
|
| 724 |
|
| 725 |
if (n <= 2) ESL_XEXCEPTION(eslEINVAL, "n must be > 2 for linear regression fitting"); |
| 726 |
if (sigma != NULL) |
| 727 |
for (i = 0; i < n; i++) if (sigma[i] <= 0.) ESL_XEXCEPTION(eslEINVAL, "sigma[%d] <= 0", i); |
| 728 |
status = eslEINVAL; |
| 729 |
for (i = 0; i < n; i++) if (x[i] != 0.) { status = eslOK; break; } |
| 730 |
if (status != eslOK) ESL_XEXCEPTION(eslEINVAL, "all x[i] are 0."); |
| 731 |
|
| 732 |
|
| 733 |
ESL_ALLOC(t, sizeof(double) * n); |
| 734 |
|
| 735 |
|
| 736 |
if (sigma != NULL) { for (S = 0., i = 0; i < n; i++) S += 1./ (sigma[i] * sigma[i]); } |
| 737 |
else S = (double) n; |
| 738 |
|
| 739 |
|
| 740 |
for (Sx = 0., i = 0; i < n; i++) { |
| 741 |
if (sigma == NULL) Sx += x[i]; |
| 742 |
else Sx += x[i] / (sigma[i] * sigma[i]); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
for (Sy = 0., i = 0; i < n; i++) { |
| 747 |
if (sigma == NULL) Sy += y[i]; |
| 748 |
else Sy += y[i] / (sigma[i] * sigma[i]); |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
for (i = 0; i < n; i++) { |
| 753 |
t[i] = x[i] - Sx/S; |
| 754 |
if (sigma != NULL) t[i] /= sigma[i]; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
for (Stt = 0., i = 0; i < n; i++) { Stt += t[i] * t[i]; } |
| 759 |
|
| 760 |
|
| 761 |
for (b = 0., i = 0; i < n; i++) { |
| 762 |
if (sigma != NULL) { b += t[i]*y[i] / sigma[i]; } |
| 763 |
else { b += t[i]*y[i]; } |
| 764 |
} |
| 765 |
b /= Stt; |
| 766 |
|
| 767 |
|
| 768 |
a = (Sy - Sx * b) / S; |
| 769 |
|
| 770 |
|
| 771 |
sigma_a = sqrt ((1. + (Sx*Sx) / (S*Stt)) / S); |
| 772 |
|
| 773 |
|
| 774 |
sigma_b = sqrt (1. / Stt); |
| 775 |
|
| 776 |
|
| 777 |
cov_ab = -Sx / (S * Stt); |
| 778 |
|
| 779 |
|
| 780 |
Sxy = Sxx = Syy = 0.; |
| 781 |
for (i = 0; i < n; i++) { |
| 782 |
if (sigma != NULL) { |
| 783 |
xdev = (x[i] / (sigma[i] * sigma[i])) - (Sx / n); |
| 784 |
ydev = (y[i] / (sigma[i] * sigma[i])) - (Sy / n); |
| 785 |
} else { |
| 786 |
xdev = x[i] - (Sx / n); |
| 787 |
ydev = y[i] - (Sy / n); |
| 788 |
} |
| 789 |
Sxy += xdev * ydev; |
| 790 |
Sxx += xdev * xdev; |
| 791 |
Syy += ydev * ydev; |
| 792 |
} |
| 793 |
cc = Sxy / (sqrt(Sxx) * sqrt(Syy)); |
| 794 |
|
| 795 |
|
| 796 |
for (X2 = 0., i = 0; i < n; i++) { |
| 797 |
tmp = y[i] - a - b*x[i]; |
| 798 |
if (sigma != NULL) tmp /= sigma[i]; |
| 799 |
X2 += tmp*tmp; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
if (sigma != NULL) { |
| 804 |
if (esl_stats_ChiSquaredTest(n-2, X2, &Q) != eslOK) { status = eslENORESULT; goto ERROR; } |
| 805 |
} else Q = 1.0; |
| 806 |
|
| 807 |
|
| 808 |
if (sigma == NULL) { |
| 809 |
tmp = sqrt(X2 / (double)(n-2)); |
| 810 |
sigma_a *= tmp; |
| 811 |
sigma_b *= tmp; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
free(t); |
| 817 |
if (opt_a != NULL) *opt_a = a; |
| 818 |
if (opt_b != NULL) *opt_b = b; |
| 819 |
if (opt_sigma_a != NULL) *opt_sigma_a = sigma_a; |
| 820 |
if (opt_sigma_b != NULL) *opt_sigma_b = sigma_b; |
| 821 |
if (opt_cov_ab != NULL) *opt_cov_ab = cov_ab; |
| 822 |
if (opt_cc != NULL) *opt_cc = cc; |
| 823 |
if (opt_Q != NULL) *opt_Q = Q; |
| 824 |
return eslOK; |
| 825 |
|
| 826 |
ERROR: |
| 827 |
if (t != NULL) free(t); |
| 828 |
if (opt_a != NULL) *opt_a = 0.; |
| 829 |
if (opt_b != NULL) *opt_b = 0.; |
| 830 |
if (opt_sigma_a != NULL) *opt_sigma_a = 0.; |
| 831 |
if (opt_sigma_b != NULL) *opt_sigma_b = 0.; |
| 832 |
if (opt_cov_ab != NULL) *opt_cov_ab = 0.; |
| 833 |
if (opt_cc != NULL) *opt_cc = 0.; |
| 834 |
if (opt_Q != NULL) *opt_Q = 0.; |
| 835 |
return status; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
#ifdef eslSTATS_TESTDRIVE |
| 845 |
#include "esl_random.h" |
| 846 |
#include "esl_stopwatch.h" |
| 847 |
#ifdef HAVE_LIBGSL |
| 848 |
#include <gsl/gsl_sf_gamma.h> |
| 849 |
#endif |
| 850 |
|
| 851 |
|
| 852 |
static void |
| 853 |
utest_DMean(ESL_RANDOMNESS *rng) |
| 854 |
{ |
| 855 |
char msg[] = "esl_stats:: DMean unit test failed"; |
| 856 |
double x[10000]; |
| 857 |
int n = 10000; |
| 858 |
int i; |
| 859 |
double mean, var; |
| 860 |
|
| 861 |
for (i = 0; i < n; i++) x[i] = esl_random(rng); |
| 862 |
esl_stats_DMean(x, n, &mean, &var); |
| 863 |
if (esl_DCompare(0.5, mean, 0., 0.01) != eslOK) esl_fatal(msg); |
| 864 |
if (esl_DCompare(1./12., var, 0., 0.01) != eslOK) esl_fatal(msg); |
| 865 |
|
| 866 |
|
| 867 |
for (i = 1; i < n; i++) x[i] = x[0]; |
| 868 |
esl_stats_DMean(x, n, &mean, &var); |
| 869 |
if (esl_DCompare(x[0], mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 870 |
if (esl_DCompare(0.0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 871 |
|
| 872 |
|
| 873 |
esl_stats_DMean(x, 1, &mean, &var); |
| 874 |
if (esl_DCompare(x[0], mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 875 |
if (esl_DCompare(0.0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 876 |
} |
| 877 |
|
| 878 |
static void |
| 879 |
utest_FMean(ESL_RANDOMNESS *rng) |
| 880 |
{ |
| 881 |
char msg[] = "esl_stats:: FMean unit test failed"; |
| 882 |
float x[10000]; |
| 883 |
int n = 10000; |
| 884 |
int i; |
| 885 |
double mean, var; |
| 886 |
|
| 887 |
for (i = 0; i < n; i++) x[i] = esl_random(rng); |
| 888 |
esl_stats_FMean(x, n, &mean, &var); |
| 889 |
if (esl_DCompare(0.5, mean, 0., 0.01) != eslOK) esl_fatal(msg); |
| 890 |
if (esl_DCompare(1./12., var, 0., 0.01) != eslOK) esl_fatal(msg); |
| 891 |
|
| 892 |
|
| 893 |
for (i = 1; i < n; i++) x[i] = x[0]; |
| 894 |
esl_stats_FMean(x, n, &mean, &var); |
| 895 |
if (esl_DCompare(x[0], mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 896 |
if (esl_DCompare(0.0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 897 |
|
| 898 |
|
| 899 |
esl_stats_FMean(x, 1, &mean, &var); |
| 900 |
if (esl_DCompare(x[0], mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 901 |
if (esl_DCompare(0.0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 902 |
} |
| 903 |
|
| 904 |
static void |
| 905 |
utest_IMean(ESL_RANDOMNESS *rng) |
| 906 |
{ |
| 907 |
char msg[] = "esl_stats:: IMean unit test failed"; |
| 908 |
int x[10000]; |
| 909 |
int n = 10000; |
| 910 |
int i; |
| 911 |
double mean, var; |
| 912 |
double mean0,var0; |
| 913 |
|
| 914 |
for (i = 0; i < n; i++) x[i] = esl_rnd_Roll(rng, 101); |
| 915 |
mean0 = 100./2.; |
| 916 |
var0 = (101.0*101.0-1.)/12.; |
| 917 |
esl_stats_IMean(x, n, &mean, &var); |
| 918 |
if (esl_DCompare(mean0, mean, 0.02, 0.02) != eslOK) esl_fatal(msg); |
| 919 |
if (esl_DCompare(var0, var, 0.02, 0.02) != eslOK) esl_fatal(msg); |
| 920 |
|
| 921 |
|
| 922 |
for (i = 1; i < n; i++) x[i] = x[0]; |
| 923 |
esl_stats_IMean(x, n, &mean, &var); |
| 924 |
mean0 = (double) x[0]; |
| 925 |
var0 = 0.; |
| 926 |
if (esl_DCompare(mean0, mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 927 |
if (esl_DCompare(var0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 928 |
|
| 929 |
|
| 930 |
esl_stats_IMean(x, 1, &mean, &var); |
| 931 |
if (esl_DCompare(mean0, mean, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 932 |
if (esl_DCompare(var0, var, 0., 1e-5) != eslOK) esl_fatal(msg); |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
static void |
| 941 |
utest_doublesplitting(ESL_RANDOMNESS *rng) |
| 942 |
{ |
| 943 |
char msg[] = "esl_stats:: doublesplitting unit test failed"; |
| 944 |
uint32_t ix0, ix1; |
| 945 |
double x; |
| 946 |
double x2; |
| 947 |
int iteration; |
| 948 |
|
| 949 |
for (iteration = 0; iteration < 2; iteration++) |
| 950 |
{ |
| 951 |
x = (iteration == 0 ? 2.0 : esl_random(rng)); |
| 952 |
ESL_GET_WORDS(ix0, ix1, x); |
| 953 |
ESL_SET_WORDS(x2, ix0, ix1); |
| 954 |
if (x2 != x) esl_fatal(msg); |
| 955 |
|
| 956 |
ESL_GET_HIGHWORD(ix0, x); |
| 957 |
ESL_SET_HIGHWORD(x2, ix0); |
| 958 |
if (x2 != x) esl_fatal(msg); |
| 959 |
|
| 960 |
ESL_GET_LOWWORD(ix0, x); |
| 961 |
ESL_SET_LOWWORD(x2, ix0); |
| 962 |
if (iteration == 0 && ix0 != 0) esl_fatal(msg); |
| 963 |
if (x2 != x) esl_fatal(msg); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
static void |
| 977 |
utest_LogGamma(ESL_RANDOMNESS *r, int N, int be_verbose) |
| 978 |
{ |
| 979 |
char *msg = "esl_stats_LogGamma() unit test failed"; |
| 980 |
ESL_STOPWATCH *w = esl_stopwatch_Create(); |
| 981 |
double *x = malloc(sizeof(double) * N); |
| 982 |
double *lg = malloc(sizeof(double) * N); |
| 983 |
double *lg2 = malloc(sizeof(double) * N); |
| 984 |
int i; |
| 985 |
|
| 986 |
for (i = 0; i < N; i++) |
| 987 |
x[i] = esl_random(r) * 100.; |
| 988 |
|
| 989 |
esl_stopwatch_Start(w); |
| 990 |
for (i = 0; i < N; i++) |
| 991 |
if (esl_stats_LogGamma(x[i], &(lg[i])) != eslOK) esl_fatal(msg); |
| 992 |
esl_stopwatch_Stop(w); |
| 993 |
|
| 994 |
if (be_verbose) esl_stopwatch_Display(stdout, w, "esl_stats_LogGamma() timing: "); |
| 995 |
|
| 996 |
#ifdef HAVE_LIBGSL |
| 997 |
esl_stopwatch_Start(w); |
| 998 |
for (i = 0; i < N; i++) lg2[i] = gsl_sf_lngamma(x[i]); |
| 999 |
esl_stopwatch_Stop(w); |
| 1000 |
|
| 1001 |
if (be_verbose) esl_stopwatch_Display(stdout, w, "gsl_sf_lngamma() timing: "); |
| 1002 |
|
| 1003 |
for (i = 0; i < N; i++) |
| 1004 |
if (esl_DCompare_old(lg[i], lg2[i], 1e-2) != eslOK) esl_fatal(msg); |
| 1005 |
#endif |
| 1006 |
|
| 1007 |
free(lg2); |
| 1008 |
free(lg); |
| 1009 |
free(x); |
| 1010 |
esl_stopwatch_Destroy(w); |
| 1011 |
} |
| 1012 |
|
| 1013 |
static void |
| 1014 |
utest_psi(ESL_RANDOMNESS *rng) |
| 1015 |
{ |
| 1016 |
char msg[] = "esl_stats:: psi unit test failed"; |
| 1017 |
int ntrials = 5; |
| 1018 |
int regime; |
| 1019 |
double x, psi_slow, psi, n; |
| 1020 |
|
| 1021 |
while (ntrials--) |
| 1022 |
{ |
| 1023 |
regime = esl_rnd_Roll(rng, 3); |
| 1024 |
if (regime == 0) x = 1.0e-5 * esl_rnd_UniformPositive(rng); |
| 1025 |
else if (regime == 1) x = 8.5 * esl_rnd_UniformPositive(rng); |
| 1026 |
else x = 8.5 + 91.5*esl_random(rng); |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
psi_slow = -eslCONST_EULER - 1./x; |
| 1031 |
for (n = 1.; n < 500000.; n += 1.) psi_slow += x / (n*(n+x)); |
| 1032 |
|
| 1033 |
if (esl_stats_Psi(x, &psi) != eslOK) esl_fatal(msg); |
| 1034 |
if (esl_DCompare(psi, psi_slow, 1e-4, 1e-4)) esl_fatal(msg); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
static void |
| 1039 |
utest_trigamma(ESL_RANDOMNESS *rng) |
| 1040 |
{ |
| 1041 |
char msg[] = "esl_stats:: trigamma unit test failed"; |
| 1042 |
int ntrials = 5; |
| 1043 |
int regime; |
| 1044 |
double x, trigam_slow, trigam, i; |
| 1045 |
|
| 1046 |
while (ntrials--) |
| 1047 |
{ |
| 1048 |
regime = esl_rnd_Roll(rng, 3); |
| 1049 |
if (regime == 0) x = 1.0e-4 * esl_rnd_UniformPositive(rng); |
| 1050 |
else if (regime == 1) x = 5.0 * esl_rnd_UniformPositive(rng); |
| 1051 |
else x = 5.0 + 95.*esl_random(rng); |
| 1052 |
if (x <= 0. || x >= 100.) esl_fatal(msg); |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
trigam_slow = 0.; |
| 1057 |
for (i = 0.; i < 1000000.; i += 1.0) trigam_slow += 1./((x+i)*(x+i)); |
| 1058 |
|
| 1059 |
if (esl_stats_Trigamma(x, &trigam) != eslOK) esl_fatal(msg); |
| 1060 |
if (esl_DCompare(trigam, trigam_slow, 1e-4, 1e-4)) esl_fatal(msg); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
static void |
| 1073 |
utest_LinearRegression(ESL_RANDOMNESS *r, int use_sigma, int be_verbose) |
| 1074 |
{ |
| 1075 |
char msg[] = "linear regression unit test failed"; |
| 1076 |
double a = -3.; |
| 1077 |
double b = 1.; |
| 1078 |
int n = 100; |
| 1079 |
double xori = -20.; |
| 1080 |
double xstep = 1.0; |
| 1081 |
double setsigma = 1.0; |
| 1082 |
int i; |
| 1083 |
double *x = NULL; |
| 1084 |
double *y = NULL; |
| 1085 |
double *sigma = NULL; |
| 1086 |
double ae, be, siga, sigb, cov_ab, cc, Q; |
| 1087 |
|
| 1088 |
if ((x = malloc(sizeof(double) * n)) == NULL) esl_fatal(msg); |
| 1089 |
if ((y = malloc(sizeof(double) * n)) == NULL) esl_fatal(msg); |
| 1090 |
if ((sigma = malloc(sizeof(double) * n)) == NULL) esl_fatal(msg); |
| 1091 |
|
| 1092 |
|
| 1093 |
for (i = 0; i < n; i++) |
| 1094 |
{ |
| 1095 |
sigma[i] = setsigma; |
| 1096 |
x[i] = xori + i*xstep; |
| 1097 |
y[i] = esl_rnd_Gaussian(r, a + b*x[i], sigma[i]); |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (use_sigma) { |
| 1101 |
if (esl_stats_LinearRegression(x, y, sigma, n, &ae, &be, &siga, &sigb, &cov_ab, &cc, &Q) != eslOK) esl_fatal(msg); |
| 1102 |
} else { |
| 1103 |
if (esl_stats_LinearRegression(x, y, NULL, n, &ae, &be, &siga, &sigb, &cov_ab, &cc, &Q) != eslOK) esl_fatal(msg); |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (be_verbose) { |
| 1107 |
printf("Linear regression test:\n"); |
| 1108 |
printf("estimated intercept a = %8.4f [true = %8.4f]\n", ae, a); |
| 1109 |
printf("estimated slope b = %8.4f [true = %8.4f]\n", be, b); |
| 1110 |
printf("estimated sigma on a = %8.4f\n", siga); |
| 1111 |
printf("estimated sigma on b = %8.4f\n", sigb); |
| 1112 |
printf("estimated cov(a,b) = %8.4f\n", cov_ab); |
| 1113 |
printf("correlation coeff = %8.4f\n", cc); |
| 1114 |
printf("P-value = %8.4f\n", Q); |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
if ( fabs(ae-a) > 2*siga ) esl_fatal(msg); |
| 1120 |
if ( fabs(be-b) > 2*sigb ) esl_fatal(msg); |
| 1121 |
if ( cc < 0.95) esl_fatal(msg); |
| 1122 |
if (use_sigma) { |
| 1123 |
if (Q < 0.001) esl_fatal(msg); |
| 1124 |
} else { |
| 1125 |
if (Q != 1.0) esl_fatal(msg); |
| 1126 |
} |
| 1127 |
|
| 1128 |
free(x); |
| 1129 |
free(y); |
| 1130 |
free(sigma); |
| 1131 |
} |
| 1132 |
|
| 1133 |
static void |
| 1134 |
utest_erfc(ESL_RANDOMNESS *rng, int be_verbose) |
| 1135 |
{ |
| 1136 |
char msg[] = "esl_stats:: erfc unit test failed"; |
| 1137 |
double x; |
| 1138 |
double result; |
| 1139 |
int i; |
| 1140 |
|
| 1141 |
if (be_verbose) { |
| 1142 |
printf("#--------------------------\n"); |
| 1143 |
printf("# erfc unit testing...\n"); |
| 1144 |
} |
| 1145 |
|
| 1146 |
result = esl_stats_erfc( eslNaN); |
| 1147 |
if (! isnan(result)) esl_fatal(msg); |
| 1148 |
if (esl_stats_erfc(-eslINFINITY) != 2.0) esl_fatal(msg); |
| 1149 |
if (esl_stats_erfc( 0.0) != 1.0) esl_fatal(msg); |
| 1150 |
if (esl_stats_erfc( eslINFINITY) != 0.0) esl_fatal(msg); |
| 1151 |
|
| 1152 |
for (i = 0; i < 42; i++) |
| 1153 |
{ |
| 1154 |
x = esl_random(rng) * 10. - 5.; |
| 1155 |
result = esl_stats_erfc(x); |
| 1156 |
if (!isfinite(result)) esl_fatal(msg); |
| 1157 |
#ifdef HAVE_ERFC |
| 1158 |
if (esl_DCompare_old(result, erfc(x), 1e-6) != eslOK) esl_fatal(msg); |
| 1159 |
if (be_verbose) |
| 1160 |
printf("%15f %15f %15f\n", x, result, erfc(x)); |
| 1161 |
#endif |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (be_verbose) |
| 1165 |
printf("#--------------------------\n"); |
| 1166 |
return; |
| 1167 |
} |
| 1168 |
|
| 1169 |
#endif |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
#ifdef eslSTATS_TESTDRIVE |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
#include <stdio.h> |
| 1183 |
#include "easel.h" |
| 1184 |
#include "esl_getopts.h" |
| 1185 |
#include "esl_random.h" |
| 1186 |
#include "esl_stats.h" |
| 1187 |
|
| 1188 |
static ESL_OPTIONS options[] = { |
| 1189 |
|
| 1190 |
{"-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 1191 |
{"-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 1192 |
{"-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "verbose: show verbose output", 0}, |
| 1193 |
{"-N", eslARG_INT,"10000000", NULL, NULL, NULL, NULL, NULL, "number of trials in LogGamma test", 0}, |
| 1194 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 1195 |
}; |
| 1196 |
static char usage[] = "[-options]"; |
| 1197 |
static char banner[] = "test driver for stats special functions"; |
| 1198 |
|
| 1199 |
int |
| 1200 |
main(int argc, char **argv) |
| 1201 |
{ |
| 1202 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1203 |
ESL_RANDOMNESS *r = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1204 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 1205 |
int N = esl_opt_GetInteger(go, "-N"); |
| 1206 |
|
| 1207 |
if (be_verbose) printf("seed = %" PRIu32 "\n", esl_randomness_GetSeed(r)); |
| 1208 |
|
| 1209 |
utest_DMean(r); |
| 1210 |
utest_FMean(r); |
| 1211 |
utest_IMean(r); |
| 1212 |
utest_doublesplitting(r); |
| 1213 |
utest_erfc(r, be_verbose); |
| 1214 |
utest_LogGamma(r, N, be_verbose); |
| 1215 |
utest_psi(r); |
| 1216 |
utest_trigamma(r); |
| 1217 |
utest_LinearRegression(r, TRUE, be_verbose); |
| 1218 |
utest_LinearRegression(r, FALSE, be_verbose); |
| 1219 |
|
| 1220 |
esl_getopts_Destroy(go); |
| 1221 |
esl_randomness_Destroy(r); |
| 1222 |
exit(0); |
| 1223 |
} |
| 1224 |
#endif |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
#ifdef eslSTATS_EXAMPLE |
| 1238 |
|
| 1239 |
|
| 1240 |
#include <stdio.h> |
| 1241 |
#include "easel.h" |
| 1242 |
#include "esl_random.h" |
| 1243 |
#include "esl_stats.h" |
| 1244 |
|
| 1245 |
int main(void) |
| 1246 |
{ |
| 1247 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 1248 |
double a = -3.; |
| 1249 |
double b = 1.; |
| 1250 |
double xori = -20.; |
| 1251 |
double xstep = 1.0; |
| 1252 |
double setsigma = 1.0; |
| 1253 |
int n = 100; |
| 1254 |
double *x = malloc(sizeof(double) * n); |
| 1255 |
double *y = malloc(sizeof(double) * n); |
| 1256 |
double *sigma = malloc(sizeof(double) * n); |
| 1257 |
int i; |
| 1258 |
double ae, be, siga, sigb, cov_ab, cc, Q; |
| 1259 |
|
| 1260 |
|
| 1261 |
for (i = 0; i < n; i++) { |
| 1262 |
sigma[i] = setsigma; |
| 1263 |
x[i] = xori + i*xstep; |
| 1264 |
y[i] = esl_rnd_Gaussian(r, a + b*x[i], sigma[i]); |
| 1265 |
} |
| 1266 |
|
| 1267 |
if (esl_stats_LinearRegression(x, y, sigma, n, &ae, &be, &siga, &sigb, &cov_ab, &cc, &Q) != eslOK) |
| 1268 |
esl_fatal("linear regression failed"); |
| 1269 |
|
| 1270 |
printf("estimated intercept a = %8.4f [true = %8.4f]\n", ae, a); |
| 1271 |
printf("estimated slope b = %8.4f [true = %8.4f]\n", be, b); |
| 1272 |
printf("estimated sigma on a = %8.4f\n", siga); |
| 1273 |
printf("estimated sigma on b = %8.4f\n", sigb); |
| 1274 |
printf("estimated cov(a,b) = %8.4f\n", cov_ab); |
| 1275 |
printf("correlation coeff = %8.4f\n", cc); |
| 1276 |
printf("P-value = %8.4f\n", Q); |
| 1277 |
|
| 1278 |
free(x); free(y); free(sigma); |
| 1279 |
esl_randomness_Destroy(r); |
| 1280 |
exit(0); |
| 1281 |
} |
| 1282 |
|
| 1283 |
#endif |
| 1284 |
|
| 1285 |
|
| 1286 |
#ifdef eslSTATS_EXAMPLE2 |
| 1287 |
|
| 1288 |
#include <stdlib.h> |
| 1289 |
|
| 1290 |
#include "easel.h" |
| 1291 |
#include "esl_getopts.h" |
| 1292 |
#include "esl_stats.h" |
| 1293 |
|
| 1294 |
static ESL_OPTIONS options[] = { |
| 1295 |
|
| 1296 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 1297 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 1298 |
}; |
| 1299 |
static char usage[] = "[-options] <ca> <na> <cb> <nb>"; |
| 1300 |
static char banner[] = "example from the stats module: using a G-test"; |
| 1301 |
|
| 1302 |
int |
| 1303 |
main(int argc, char **argv) |
| 1304 |
{ |
| 1305 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 4, argc, argv, banner, usage); |
| 1306 |
int ca = strtol(esl_opt_GetArg(go, 1), NULL, 10); |
| 1307 |
int na = strtol(esl_opt_GetArg(go, 2), NULL, 10); |
| 1308 |
int cb = strtol(esl_opt_GetArg(go, 3), NULL, 10); |
| 1309 |
int nb = strtol(esl_opt_GetArg(go, 4), NULL, 10); |
| 1310 |
double G, P; |
| 1311 |
int status; |
| 1312 |
|
| 1313 |
if (ca > na || cb > nb) esl_fatal("argument order wrong? expect ca, na, cb, nb for ca/na, cb/nb"); |
| 1314 |
|
| 1315 |
if ( (status = esl_stats_GTest(ca, na, cb, nb, &G, &P)) != eslOK) esl_fatal("G-test failed?"); |
| 1316 |
printf("%-10.3g %12.2f\n", P, G); |
| 1317 |
exit(0); |
| 1318 |
} |
| 1319 |
#endif |
| 1320 |
|
| 1321 |
|