| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <esl_config.h> |
| 12 |
|
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
#include <math.h> |
| 16 |
#include <float.h> |
| 17 |
#include <limits.h> |
| 18 |
|
| 19 |
#include "easel.h" |
| 20 |
#include "esl_stats.h" |
| 21 |
#include "esl_vectorops.h" |
| 22 |
|
| 23 |
#include "esl_histogram.h" |
| 24 |
|
| 25 |
static int esl_histogram_sort(ESL_HISTOGRAM *h); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
ESL_HISTOGRAM * |
| 77 |
esl_histogram_Create(double bmin, double bmax, double w) |
| 78 |
{ |
| 79 |
ESL_HISTOGRAM *h = NULL; |
| 80 |
int status; |
| 81 |
int i; |
| 82 |
|
| 83 |
ESL_ALLOC(h, sizeof(ESL_HISTOGRAM)); |
| 84 |
|
| 85 |
h->xmin = DBL_MAX; |
| 86 |
h->xmax = -DBL_MAX; |
| 87 |
h->n = 0; |
| 88 |
h->obs = NULL; |
| 89 |
h->bmin = bmin; |
| 90 |
h->bmax = bmax; |
| 91 |
h->nb = (int)((bmax-bmin)/w); |
| 92 |
h->imin = h->nb; |
| 93 |
h->imax = -1; |
| 94 |
h->w = w; |
| 95 |
|
| 96 |
h->x = NULL; |
| 97 |
h->nalloc = 0; |
| 98 |
|
| 99 |
h->phi = 0.; |
| 100 |
h->cmin = h->imin; |
| 101 |
h->z = 0; |
| 102 |
h->Nc = 0; |
| 103 |
h->No = 0; |
| 104 |
|
| 105 |
h->expect = NULL; |
| 106 |
h->emin = -1; |
| 107 |
h->tailbase = 0.; |
| 108 |
h->tailmass = 1.0; |
| 109 |
|
| 110 |
h->is_full = FALSE; |
| 111 |
h->is_done = FALSE; |
| 112 |
h->is_sorted = FALSE; |
| 113 |
h->is_tailfit = FALSE; |
| 114 |
h->is_rounded = FALSE; |
| 115 |
h->dataset_is = COMPLETE; |
| 116 |
|
| 117 |
ESL_ALLOC(h->obs, sizeof(uint64_t) * h->nb); |
| 118 |
for (i = 0; i < h->nb; i++) h->obs[i] = 0; |
| 119 |
return h; |
| 120 |
|
| 121 |
ERROR: |
| 122 |
esl_histogram_Destroy(h); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
ESL_HISTOGRAM * |
| 137 |
esl_histogram_CreateFull(double bmin, double bmax, double w) |
| 138 |
{ |
| 139 |
int status; |
| 140 |
ESL_HISTOGRAM *h = esl_histogram_Create(bmin, bmax, w); |
| 141 |
if (h == NULL) return NULL; |
| 142 |
|
| 143 |
h->n = 0; |
| 144 |
h->nalloc = 128; |
| 145 |
ESL_ALLOC(h->x, sizeof(double) * h->nalloc); |
| 146 |
h->is_full = TRUE; |
| 147 |
return h; |
| 148 |
|
| 149 |
ERROR: |
| 150 |
esl_histogram_Destroy(h); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void |
| 161 |
esl_histogram_Destroy(ESL_HISTOGRAM *h) |
| 162 |
{ |
| 163 |
if (h == NULL) return; |
| 164 |
if (h->x != NULL) free(h->x); |
| 165 |
if (h->obs != NULL) free(h->obs); |
| 166 |
if (h->expect != NULL) free(h->expect); |
| 167 |
free(h); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
int |
| 187 |
esl_histogram_Score2Bin(ESL_HISTOGRAM *h, double x, int *ret_b) |
| 188 |
{ |
| 189 |
int status; |
| 190 |
|
| 191 |
if (! isfinite(x)) ESL_XEXCEPTION(eslERANGE, "value added to histogram is not finite"); |
| 192 |
|
| 193 |
x = ceil( ((x - h->bmin) / h->w) - 1.); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if (x < (double) INT_MIN || x > (double) INT_MAX) |
| 200 |
ESL_XEXCEPTION(eslERANGE, "value %f isn't going to fit in histogram", x); |
| 201 |
|
| 202 |
*ret_b = (int) x; |
| 203 |
return eslOK; |
| 204 |
|
| 205 |
ERROR: |
| 206 |
*ret_b = 0; |
| 207 |
return status; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
int |
| 236 |
esl_histogram_Add(ESL_HISTOGRAM *h, double x) |
| 237 |
{ |
| 238 |
int status; |
| 239 |
void *tmp; |
| 240 |
int b; |
| 241 |
int nnew; |
| 242 |
int bi; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (h->is_done) |
| 248 |
ESL_EXCEPTION(eslEINVAL, "can't add more data to this histogram"); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
if (h->is_full && h->nalloc == h->n) |
| 254 |
{ |
| 255 |
ESL_RALLOC(h->x, tmp, sizeof(double) * h->nalloc * 2); |
| 256 |
h->nalloc *= 2; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
if ((status = esl_histogram_Score2Bin(h,x, &b)) != eslOK) return status; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if (b < 0) |
| 268 |
{ |
| 269 |
nnew = -b*2; |
| 270 |
if (nnew > INT_MAX - h->nb) |
| 271 |
ESL_EXCEPTION(eslERANGE, "value %f requires unreasonable histogram bin number", x); |
| 272 |
ESL_RALLOC(h->obs, tmp, sizeof(uint64_t) * (nnew+ h->nb)); |
| 273 |
|
| 274 |
memmove(h->obs+nnew, h->obs, sizeof(uint64_t) * h->nb); |
| 275 |
h->nb += nnew; |
| 276 |
b += nnew; |
| 277 |
h->bmin -= nnew*h->w; |
| 278 |
h->imin += nnew; |
| 279 |
h->cmin += nnew; |
| 280 |
if (h->imax > -1) h->imax += nnew; |
| 281 |
for (bi = 0; bi < nnew; bi++) h->obs[bi] = 0; |
| 282 |
} |
| 283 |
else if (b >= h->nb) |
| 284 |
{ |
| 285 |
nnew = (b-h->nb+1) * 2; |
| 286 |
if (nnew > INT_MAX - h->nb) |
| 287 |
ESL_EXCEPTION(eslERANGE, "value %f requires unreasonable histogram bin number", x); |
| 288 |
ESL_RALLOC(h->obs, tmp, sizeof(uint64_t) * (nnew+ h->nb)); |
| 289 |
for (bi = h->nb; bi < h->nb+nnew; bi++) h->obs[bi] = 0; |
| 290 |
if (h->imin == h->nb) { |
| 291 |
h->imin+=nnew; |
| 292 |
h->cmin+=nnew; |
| 293 |
} |
| 294 |
h->bmax += nnew*h->w; |
| 295 |
h->nb += nnew; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
if (h->is_full) h->x[h->n] = x; |
| 302 |
h->is_sorted = FALSE; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
h->obs[b]++; |
| 307 |
h->n++; |
| 308 |
h->Nc++; |
| 309 |
h->No++; |
| 310 |
|
| 311 |
if (b > h->imax) h->imax = b; |
| 312 |
if (b < h->imin) { h->imin = b; h->cmin = b; } |
| 313 |
if (x > h->xmax) h->xmax = x; |
| 314 |
if (x < h->xmin) h->xmin = x; |
| 315 |
return eslOK; |
| 316 |
|
| 317 |
ERROR: |
| 318 |
return status; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
int |
| 333 |
esl_histogram_sort(ESL_HISTOGRAM *h) |
| 334 |
{ |
| 335 |
if (h->is_sorted) return eslOK; |
| 336 |
if (! h->is_full) return eslOK; |
| 337 |
|
| 338 |
esl_vec_DSortIncreasing(h->x, h->n); |
| 339 |
h->is_sorted = TRUE; |
| 340 |
return eslOK; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
int |
| 368 |
esl_histogram_DeclareCensoring(ESL_HISTOGRAM *h, int z, double phi) |
| 369 |
{ |
| 370 |
if (phi > h->xmin) ESL_EXCEPTION(eslEINVAL, "no uncensored x can be <= phi"); |
| 371 |
|
| 372 |
h->phi = phi; |
| 373 |
h->cmin = h->imin; |
| 374 |
h->z = z; |
| 375 |
h->Nc = h->n + z; |
| 376 |
h->No = h->n; |
| 377 |
h->dataset_is = TRUE_CENSORED; |
| 378 |
h->is_done = TRUE; |
| 379 |
return eslOK; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
int |
| 393 |
esl_histogram_DeclareRounding(ESL_HISTOGRAM *h) |
| 394 |
{ |
| 395 |
h->is_rounded = TRUE; |
| 396 |
return eslOK; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
int |
| 436 |
esl_histogram_SetTail(ESL_HISTOGRAM *h, double phi, double *ret_newmass) |
| 437 |
{ |
| 438 |
int b; |
| 439 |
int status; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if ((status = esl_histogram_Score2Bin(h,phi, &(h->cmin))) != eslOK) return status; |
| 446 |
if (phi == esl_histogram_Bin2UBound(h,h->cmin)) h->phi = phi; |
| 447 |
else h->phi = esl_histogram_Bin2LBound(h, h->cmin); |
| 448 |
|
| 449 |
h->z = 0; |
| 450 |
for (b = h->imin; b < h->cmin; b++) |
| 451 |
h->z += h->obs[b]; |
| 452 |
h->Nc = h->n; |
| 453 |
h->No = h->n - h->z; |
| 454 |
h->dataset_is = VIRTUAL_CENSORED; |
| 455 |
h->is_done = TRUE; |
| 456 |
if (ret_newmass != NULL) *ret_newmass = (double) h->No / (double) h->Nc; |
| 457 |
return eslOK; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
int |
| 488 |
esl_histogram_SetTailByMass(ESL_HISTOGRAM *h, double pmass, double *ret_newmass) |
| 489 |
{ |
| 490 |
int b; |
| 491 |
uint64_t sum = 0; |
| 492 |
|
| 493 |
for (b = h->imax; b >= h->imin; b--) |
| 494 |
{ |
| 495 |
sum += h->obs[b]; |
| 496 |
if (sum >= (pmass * (double)h->n)) break; |
| 497 |
} |
| 498 |
|
| 499 |
h->phi = esl_histogram_Bin2LBound(h,b); |
| 500 |
h->z = h->n - sum; |
| 501 |
h->cmin = b; |
| 502 |
h->Nc = h->n; |
| 503 |
h->No = h->n - h->z; |
| 504 |
h->dataset_is = VIRTUAL_CENSORED; |
| 505 |
h->is_done = TRUE; |
| 506 |
if (ret_newmass != NULL) *ret_newmass = (double) h->No / (double) h->Nc; |
| 507 |
return eslOK; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
int |
| 536 |
esl_histogram_GetRank(ESL_HISTOGRAM *h, int rank, double *ret_x) |
| 537 |
{ |
| 538 |
if (! h->is_full) |
| 539 |
ESL_EXCEPTION(eslEINVAL, |
| 540 |
"esl_histogram_GetRank() needs a full histogram"); |
| 541 |
if (rank > h->n) |
| 542 |
ESL_EXCEPTION(eslEINVAL, |
| 543 |
"no such rank: not that many scores in the histogram"); |
| 544 |
if (rank < 1) |
| 545 |
ESL_EXCEPTION(eslEINVAL, "histogram rank must be a value from 1..n"); |
| 546 |
|
| 547 |
esl_histogram_sort(h); |
| 548 |
*ret_x = h->x[h->n - rank]; |
| 549 |
return eslOK; |
| 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_histogram_GetData(ESL_HISTOGRAM *h, double **ret_x, int *ret_n) |
| 588 |
{ |
| 589 |
if (! h->is_full) ESL_EXCEPTION(eslEINVAL, "not a full histogram"); |
| 590 |
esl_histogram_sort(h); |
| 591 |
|
| 592 |
*ret_x = h->x; |
| 593 |
*ret_n = h->n; |
| 594 |
|
| 595 |
h->is_done = TRUE; |
| 596 |
return eslOK; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
int |
| 635 |
esl_histogram_GetTail(ESL_HISTOGRAM *h, double phi, |
| 636 |
double **ret_x, int *ret_n, int *ret_z) |
| 637 |
{ |
| 638 |
int hi, lo, mid; |
| 639 |
|
| 640 |
if (! h->is_full) ESL_EXCEPTION(eslEINVAL, "not a full histogram"); |
| 641 |
esl_histogram_sort(h); |
| 642 |
|
| 643 |
if (h->n == 0) mid = h->n; |
| 644 |
else if (h->x[0] > phi) mid = 0; |
| 645 |
else if (h->x[h->n-1] <= phi) mid = h->n; |
| 646 |
else |
| 647 |
{ |
| 648 |
lo = 0; |
| 649 |
hi = h->n-1; |
| 650 |
while (1) { |
| 651 |
mid = (lo + hi + 1) / 2; |
| 652 |
if (h->x[mid] <= phi) lo = mid; |
| 653 |
else if (h->x[mid-1] > phi) hi = mid; |
| 654 |
else break; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
if (ret_x != NULL) *ret_x = h->x + mid; |
| 659 |
if (ret_n != NULL) *ret_n = h->n - mid; |
| 660 |
if (ret_z != NULL) *ret_z = mid; |
| 661 |
h->is_done = TRUE; |
| 662 |
return eslOK; |
| 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 |
int |
| 702 |
esl_histogram_GetTailByMass(ESL_HISTOGRAM *h, double pmass, |
| 703 |
double **ret_x, int *ret_n, int *ret_z) |
| 704 |
{ |
| 705 |
uint64_t n; |
| 706 |
|
| 707 |
if (! h->is_full) |
| 708 |
ESL_EXCEPTION(eslEINVAL, "not a full histogram"); |
| 709 |
if (pmass < 0. || pmass > 1.) |
| 710 |
ESL_EXCEPTION(eslEINVAL, "pmass not a probability"); |
| 711 |
|
| 712 |
esl_histogram_sort(h); |
| 713 |
|
| 714 |
n = (uint64_t) ((double) h->n * pmass); |
| 715 |
|
| 716 |
if (ret_x != NULL) *ret_x = h->x + (h->n - n); |
| 717 |
if (ret_n != NULL) *ret_n = n; |
| 718 |
if (ret_z != NULL) *ret_z = h->n - n; |
| 719 |
h->is_done = TRUE; |
| 720 |
return eslOK; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
int |
| 756 |
esl_histogram_SetExpect(ESL_HISTOGRAM *h, |
| 757 |
double (*cdf)(double x, void *params), void *params) |
| 758 |
{ |
| 759 |
int i; |
| 760 |
double ai,bi; |
| 761 |
int status; |
| 762 |
|
| 763 |
if (h->expect == NULL) |
| 764 |
ESL_ALLOC(h->expect, sizeof(double) * h->nb); |
| 765 |
|
| 766 |
for (i = 0; i < h->nb; i++) |
| 767 |
{ |
| 768 |
ai = esl_histogram_Bin2LBound(h, i); |
| 769 |
bi = esl_histogram_Bin2UBound(h, i); |
| 770 |
|
| 771 |
h->expect[i] = h->Nc * ( (*cdf)(bi, params) - (*cdf)(ai, params) ); |
| 772 |
|
| 773 |
if (h->emin == -1 && h->expect[i] > 0.) h->emin = i; |
| 774 |
} |
| 775 |
|
| 776 |
h->is_done = TRUE; |
| 777 |
return eslOK; |
| 778 |
|
| 779 |
ERROR: |
| 780 |
return status; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
int |
| 813 |
esl_histogram_SetExpectedTail(ESL_HISTOGRAM *h, double base_val, double pmass, |
| 814 |
double (*cdf)(double x, void *params), |
| 815 |
void *params) |
| 816 |
{ |
| 817 |
int status; |
| 818 |
int b; |
| 819 |
double ai, bi; |
| 820 |
|
| 821 |
if (h->expect == NULL) ESL_ALLOC(h->expect, sizeof(double) * h->nb); |
| 822 |
|
| 823 |
if ((status = esl_histogram_Score2Bin(h, base_val, &(h->emin))) != eslOK) return status; |
| 824 |
h->emin += 1; |
| 825 |
esl_vec_DSet(h->expect, h->emin, 0.); |
| 826 |
|
| 827 |
for (b = h->emin; b < h->nb; b++) |
| 828 |
{ |
| 829 |
ai = esl_histogram_Bin2LBound(h, b); |
| 830 |
bi = esl_histogram_Bin2UBound(h, b); |
| 831 |
h->expect[b] = pmass * (double) h->Nc * |
| 832 |
( (*cdf)(bi, params) - (*cdf)(ai, params) ); |
| 833 |
} |
| 834 |
|
| 835 |
h->tailbase = base_val; |
| 836 |
h->tailmass = pmass; |
| 837 |
h->is_tailfit = TRUE; |
| 838 |
h->is_done = TRUE; |
| 839 |
return eslOK; |
| 840 |
|
| 841 |
ERROR: |
| 842 |
return status; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
int |
| 875 |
esl_histogram_Write(FILE *fp, ESL_HISTOGRAM *h) |
| 876 |
{ |
| 877 |
int i; |
| 878 |
double x; |
| 879 |
uint64_t maxbar; |
| 880 |
int imode; |
| 881 |
uint64_t units; |
| 882 |
int num; |
| 883 |
char buffer[81]; |
| 884 |
int pos; |
| 885 |
uint64_t lowcount, highcount; |
| 886 |
int ilowbound, ihighbound; |
| 887 |
int nlines; |
| 888 |
int emptybins = 3; |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
maxbar = 0; |
| 897 |
imode = 0; |
| 898 |
for (i = 0; i < h->nb; i++) |
| 899 |
if (h->obs[i] > maxbar) |
| 900 |
{ |
| 901 |
maxbar = h->obs[i]; |
| 902 |
imode = i; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
for (num = 0, ihighbound = imode; ihighbound < h->imax; ihighbound++) |
| 910 |
{ |
| 911 |
if (h->obs[ihighbound] > 0) { num = 0; continue; } |
| 912 |
if (++num == emptybins) { break; } |
| 913 |
} |
| 914 |
for (num = 0, ilowbound = imode; ilowbound > h->imin; ilowbound--) |
| 915 |
{ |
| 916 |
if (h->obs[ilowbound] > 0) { num = 0; continue; } |
| 917 |
if (++num == emptybins) { break; } |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
for (lowcount = 0, i = h->imin; i < ilowbound; i++) |
| 922 |
lowcount += h->obs[i]; |
| 923 |
for (highcount = 0, i = h->imax; i > ihighbound; i--) |
| 924 |
highcount += h->obs[i]; |
| 925 |
|
| 926 |
|
| 927 |
if (lowcount > maxbar) maxbar = lowcount; |
| 928 |
if (highcount > maxbar) maxbar = highcount; |
| 929 |
|
| 930 |
if (maxbar > 0) units = ((maxbar-1)/ 58) + 1; |
| 931 |
else units = 1; |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if (fprintf(fp, "%6s %6s %6s (one = represents %llu sequences)\n", |
| 936 |
"score", "obs", "exp", (unsigned long long) units) < 0) |
| 937 |
ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 938 |
if (fprintf(fp, "%6s %6s %6s\n", "-----", "---", "---") < 0) |
| 939 |
ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 940 |
|
| 941 |
|
| 942 |
buffer[80] = '\0'; |
| 943 |
buffer[79] = '\n'; |
| 944 |
nlines = 0; |
| 945 |
for (i = h->imin; i <= h->imax; i++) |
| 946 |
{ |
| 947 |
memset(buffer, ' ', 79 * sizeof(char)); |
| 948 |
x = i*h->w + h->bmin; |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
if (i < ilowbound) continue; |
| 953 |
else if (i > ihighbound) continue; |
| 954 |
else if (i == ilowbound && i != h->imin) |
| 955 |
{ |
| 956 |
snprintf(buffer, 81, "<%5.1f %6llu %6s|", x+h->w, (unsigned long long) lowcount, "-"); |
| 957 |
if (lowcount > 0) { |
| 958 |
num = 1+(lowcount-1) / units; |
| 959 |
for (pos = 21; num > 0; num--) buffer[pos++] = '='; |
| 960 |
} |
| 961 |
if (fputs(buffer, fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 962 |
nlines++; |
| 963 |
continue; |
| 964 |
} |
| 965 |
else if (i == ihighbound && i != h->imax) |
| 966 |
{ |
| 967 |
snprintf(buffer, 81, ">%5.1f %6llu %6s|", x, (unsigned long long) highcount, "-"); |
| 968 |
if (highcount > 0) { |
| 969 |
num = 1+(highcount-1) / units; |
| 970 |
for (pos = 21; num > 0; num--) buffer[pos++] = '='; |
| 971 |
} |
| 972 |
if (fputs(buffer, fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 973 |
nlines++; |
| 974 |
continue; |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
if (h->obs[i] < 1000000) |
| 980 |
{ |
| 981 |
if (h->expect != NULL) |
| 982 |
snprintf(buffer, 81, "%6.1f %6llu %6d|", x, (unsigned long long) h->obs[i], (int) h->expect[i]); |
| 983 |
else |
| 984 |
snprintf(buffer, 81, "%6.1f %6llu %6s|", x, (unsigned long long) h->obs[i], "-"); |
| 985 |
} |
| 986 |
else |
| 987 |
{ |
| 988 |
if (h->expect != NULL) |
| 989 |
snprintf(buffer, 81, "%6.1f %6.2e %6.2e|", x, (double) h->obs[i], h->expect[i]); |
| 990 |
else |
| 991 |
snprintf(buffer, 81, "%6.1f %6.2e %6s|", x, (double) h->obs[i], "-"); |
| 992 |
} |
| 993 |
buffer[21] = ' '; |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
if (h->obs[i] > 0) { |
| 998 |
num = 1 + (h->obs[i]-1) / units; |
| 999 |
for (pos = 21; num > 0; num--) buffer[pos++] = '='; |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
if (h->expect != NULL && h->expect[i] > 0.) |
| 1006 |
{ |
| 1007 |
pos = 21 + (int)(h->expect[i]-1) / units; |
| 1008 |
if (pos >= 78) pos = 78; |
| 1009 |
buffer[pos] = '*'; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
if (fputs(buffer, fp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 1015 |
nlines++; |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (nlines == 0 && fprintf(fp, "[histogram contained no data points]\n") < 0) |
| 1019 |
ESL_EXCEPTION_SYS(eslEWRITE, "histogram write failed"); |
| 1020 |
|
| 1021 |
return eslOK; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
int |
| 1042 |
esl_histogram_Plot(FILE *fp, ESL_HISTOGRAM *h) |
| 1043 |
{ |
| 1044 |
int imin, imax; |
| 1045 |
int i; |
| 1046 |
double x; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
for (i = h->imin; i <= h->imax; i++) |
| 1051 |
{ |
| 1052 |
x = esl_histogram_Bin2LBound(h,i); |
| 1053 |
if (fprintf(fp, "%f %llu\n", x, (unsigned long long) h->obs[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram plot write failed"); |
| 1054 |
} |
| 1055 |
x = esl_histogram_Bin2LBound(h,i); |
| 1056 |
if (fprintf(fp, "%f %d\n", x, 0) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram plot write failed"); |
| 1057 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram plot write failed"); |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
if (h->expect != NULL) |
| 1063 |
{ |
| 1064 |
for (imin = 0; imin < h->nb; imin++) |
| 1065 |
if (h->expect[imin] > 0.) break; |
| 1066 |
for (imax = h->nb-1; imax >= 0; imax--) |
| 1067 |
if (h->expect[imax] > 0.) break; |
| 1068 |
|
| 1069 |
for (i = imin; i <= imax; i++) |
| 1070 |
{ |
| 1071 |
x = esl_histogram_Bin2LBound(h,i); |
| 1072 |
if (fprintf(fp, "%f %g\n", x, h->expect[i]) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram plot write failed"); |
| 1073 |
} |
| 1074 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram plot write failed"); |
| 1075 |
} |
| 1076 |
return eslOK; |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
int |
| 1100 |
esl_histogram_PlotSurvival(FILE *fp, ESL_HISTOGRAM *h) |
| 1101 |
{ |
| 1102 |
int i; |
| 1103 |
uint64_t c = 0; |
| 1104 |
double esum; |
| 1105 |
double ai; |
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
if (h->obs[h->imax] > 1) |
| 1110 |
if (fprintf(fp, "%f\t%g\n", h->xmax, 1.0 / (double) h->Nc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram survival plot write failed"); |
| 1111 |
for (i = h->imax; i >= h->imin; i--) |
| 1112 |
{ |
| 1113 |
if (h->obs[i] > 0) { |
| 1114 |
c += h->obs[i]; |
| 1115 |
ai = esl_histogram_Bin2LBound(h, i); |
| 1116 |
if (fprintf(fp, "%f\t%g\n", ai, (double) c / (double) h->Nc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram survival plot write failed"); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram survival plot write failed"); |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
if (h->expect != NULL) |
| 1124 |
{ |
| 1125 |
esum = 0.; |
| 1126 |
for (i = h->nb-1; i >= 0; i--) |
| 1127 |
{ |
| 1128 |
if (h->expect[i] > 0.) { |
| 1129 |
esum += h->expect[i]; |
| 1130 |
ai = esl_histogram_Bin2LBound(h, i); |
| 1131 |
if (fprintf(fp, "%f\t%g\n", ai, esum / (double) h->Nc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram survival plot write failed"); |
| 1132 |
} |
| 1133 |
} |
| 1134 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram survival plot write failed"); |
| 1135 |
} |
| 1136 |
return eslOK; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
int |
| 1158 |
esl_histogram_PlotQQ(FILE *fp, ESL_HISTOGRAM *h, |
| 1159 |
double (*invcdf)(double x, void *params), void *params) |
| 1160 |
{ |
| 1161 |
int i; |
| 1162 |
double cdf; |
| 1163 |
double bi; |
| 1164 |
int bbase; |
| 1165 |
uint64_t sum; |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
if (h->dataset_is == TRUE_CENSORED || h->dataset_is == VIRTUAL_CENSORED) |
| 1170 |
sum = h->z; |
| 1171 |
else |
| 1172 |
sum = 0; |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
bbase = h->cmin; |
| 1177 |
if (h->is_tailfit && h->emin > bbase) bbase = h->emin; |
| 1178 |
for (i = h->cmin; i < bbase; i++) sum += h->obs[i]; |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
for (i = bbase; i < h->imax; i++) |
| 1183 |
{ |
| 1184 |
sum += h->obs[i]; |
| 1185 |
cdf = (double) sum / (double) h->Nc; |
| 1186 |
|
| 1187 |
if (h->is_tailfit) cdf = (cdf + h->tailmass - 1.) / (h->tailmass); |
| 1188 |
|
| 1189 |
bi = esl_histogram_Bin2UBound(h, i); |
| 1190 |
if (fprintf(fp, "%f\t%f\n", bi, (*invcdf)(cdf, params)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram QQ plot write failed"); |
| 1191 |
} |
| 1192 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram QQ plot write failed"); |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
bi = esl_histogram_Bin2LBound(h, bbase); |
| 1197 |
if (fprintf(fp, "%f\t%f\n", bi, bi) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram QQ plot write failed"); |
| 1198 |
if (fprintf(fp, "%f\t%f\n", h->xmax, h->xmax) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram QQ plot write failed"); |
| 1199 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "histogram QQ plot write failed"); |
| 1200 |
|
| 1201 |
return eslOK; |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
int |
| 1250 |
esl_histogram_Goodness(ESL_HISTOGRAM *h, |
| 1251 |
int nfitted, int *ret_nbins, |
| 1252 |
double *ret_G, double *ret_Gp, |
| 1253 |
double *ret_X2, double *ret_X2p) |
| 1254 |
{ |
| 1255 |
uint64_t *obs = NULL; |
| 1256 |
double *exp = NULL; |
| 1257 |
double *topx = NULL; |
| 1258 |
int nb; |
| 1259 |
uint64_t minc; |
| 1260 |
int i,b; |
| 1261 |
double G, Gp; |
| 1262 |
double X2, X2p; |
| 1263 |
double tmp; |
| 1264 |
int status; |
| 1265 |
int bbase; |
| 1266 |
uint64_t hmax; |
| 1267 |
uint64_t nobs; |
| 1268 |
double nexp; |
| 1269 |
|
| 1270 |
if (h->expect == NULL) ESL_EXCEPTION(eslEINVAL, "no expected counts in that histogram"); |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
bbase = h->cmin; |
| 1276 |
if (h->is_tailfit && h->emin > bbase) bbase = h->emin; |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
nobs = 0; |
| 1282 |
hmax = 0; |
| 1283 |
for (i = bbase; i <= h->imax; i++) |
| 1284 |
{ |
| 1285 |
nobs += h->obs[i]; |
| 1286 |
if (h->obs[i] > hmax) hmax = h->obs[i]; |
| 1287 |
} |
| 1288 |
if (nobs == 0) { status = eslENORESULT; goto ERROR; } |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
nb = 2* (int) pow((double) nobs, 0.4); |
| 1306 |
minc = 1 + nobs / (2*nb); |
| 1307 |
ESL_ALLOC(obs, sizeof(uint64_t) * (nb*2+1)); |
| 1308 |
ESL_ALLOC(exp, sizeof(double) * (nb*2+1)); |
| 1309 |
ESL_ALLOC(topx, sizeof(double) * (nb*2+1)); |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
nobs = 0; |
| 1318 |
nexp = 0.; |
| 1319 |
for (i = 0, b = bbase; b <= h->imax; b++) |
| 1320 |
{ |
| 1321 |
nobs += h->obs[b]; |
| 1322 |
nexp += h->expect[b]; |
| 1323 |
|
| 1324 |
|
| 1325 |
if (nobs >= minc && nexp >= minc) { |
| 1326 |
ESL_DASSERT1( (i < (nb*2+1)) ); |
| 1327 |
obs[i] = nobs; |
| 1328 |
exp[i] = nexp; |
| 1329 |
topx[i] = esl_histogram_Bin2UBound(h,b); |
| 1330 |
nobs = 0; |
| 1331 |
nexp = 0.; |
| 1332 |
i++; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
if (i == 0) { status = eslENORESULT; goto ERROR; } |
| 1336 |
obs[i-1] += nobs; |
| 1337 |
exp[i-1] += nexp; |
| 1338 |
topx[i-1] = esl_histogram_Bin2UBound(h, h->imax); |
| 1339 |
nb = i; |
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
if (nb-nfitted-1 <= 0) { status = eslENORESULT; goto ERROR; } |
| 1346 |
|
| 1347 |
|
| 1348 |
X2 = 0.; |
| 1349 |
for (i = 0; i < nb; i++) |
| 1350 |
{ |
| 1351 |
tmp = (double) obs[i] - exp[i]; |
| 1352 |
X2 += tmp*tmp / exp[i]; |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (X2 == 0.) |
| 1356 |
X2p = 1.0; |
| 1357 |
else if (X2 != eslINFINITY) { |
| 1358 |
if ((status = esl_stats_ChiSquaredTest(nb-nfitted, X2, &X2p)) != eslOK) goto ERROR; |
| 1359 |
} |
| 1360 |
else |
| 1361 |
X2p = 0.; |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
nobs = 0; |
| 1368 |
nexp = 0.; |
| 1369 |
for (i = 0; i < nb; i++) |
| 1370 |
{ |
| 1371 |
nobs += obs[i]; |
| 1372 |
nexp += exp[i]; |
| 1373 |
} |
| 1374 |
for (i = 0; i < nb; i++) |
| 1375 |
exp[i] = exp[i] * (double) nobs / nexp; |
| 1376 |
|
| 1377 |
|
| 1378 |
G = 0.; |
| 1379 |
for (i = 0; i < nb; i++) |
| 1380 |
G += (double) obs[i] * log ((double) obs[i] / exp[i]); |
| 1381 |
G *= 2; |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
if (G == 0.) |
| 1387 |
Gp = 1.0; |
| 1388 |
else if (G != eslINFINITY) |
| 1389 |
{ |
| 1390 |
if ((status = esl_stats_ChiSquaredTest(nb-nfitted-1, G, &Gp)) != eslOK) goto ERROR; |
| 1391 |
} |
| 1392 |
else Gp = 0.; |
| 1393 |
|
| 1394 |
if (ret_nbins) *ret_nbins = nb; |
| 1395 |
if (ret_G) *ret_G = G; |
| 1396 |
if (ret_Gp) *ret_Gp = Gp; |
| 1397 |
if (ret_X2) *ret_X2 = X2; |
| 1398 |
if (ret_X2p) *ret_X2p = X2p; |
| 1399 |
free(obs); |
| 1400 |
free(exp); |
| 1401 |
free(topx); |
| 1402 |
return eslOK; |
| 1403 |
|
| 1404 |
ERROR: |
| 1405 |
if (ret_nbins) *ret_nbins = 0; |
| 1406 |
if (ret_G) *ret_G = 0.; |
| 1407 |
if (ret_Gp) *ret_Gp = 1.; |
| 1408 |
if (ret_X2) *ret_X2 = 0.; |
| 1409 |
if (ret_X2p) *ret_X2p = 1.; |
| 1410 |
if (obs) free(obs); |
| 1411 |
if (exp) free(exp); |
| 1412 |
if (topx) free(topx); |
| 1413 |
return status; |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
#ifdef eslHISTOGRAM_TESTDRIVE |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
#include <stdio.h> |
| 1447 |
#include <stdlib.h> |
| 1448 |
|
| 1449 |
#include "easel.h" |
| 1450 |
#include "esl_stats.h" |
| 1451 |
#include "esl_gumbel.h" |
| 1452 |
#include "esl_exponential.h" |
| 1453 |
#include "esl_random.h" |
| 1454 |
#include "esl_getopts.h" |
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
static ESL_OPTIONS options[] = { |
| 1459 |
|
| 1460 |
{ "-j", eslARG_INT, "100", NULL, "n>0", NULL, NULL, NULL, "number of trials", 0 }, |
| 1461 |
{ "-m", eslARG_INT, "0", NULL, "n>=0", NULL, NULL, NULL, "number of test samples", 0 }, |
| 1462 |
{ "-n", eslARG_INT, "10000", NULL, "n>0", NULL, NULL, NULL, "number of training samples", 0 }, |
| 1463 |
{ "-t", eslARG_INT, "1", NULL, "1<=n<=5", NULL, NULL, NULL, "test type choice, 1-5", 0 }, |
| 1464 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be verbose?", 0 }, |
| 1465 |
{ "--ascii", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "output ASCII histogram to <f>", 0 }, |
| 1466 |
{ "--cmass", eslARG_REAL, "0.7", NULL, "0<=x<=1", NULL, NULL, NULL, "set virtual censoring mass to <x>", 0 }, |
| 1467 |
{ "--lambda", eslARG_REAL, "0.8", NULL, "x>0", NULL, NULL, NULL, "set Gumbel lambda param to <x>", 0 }, |
| 1468 |
{ "--mu", eslARG_REAL, "10.0", NULL, NULL, NULL, NULL, NULL, "set Gumbel mu param to <x>", 0 }, |
| 1469 |
{ "--phi", eslARG_REAL, "10.0", NULL, NULL, NULL, NULL, NULL, "set censoring threshold to <x>", 0 }, |
| 1470 |
{ "--plot", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "output histogram to xmgrace file <f>", 0 }, |
| 1471 |
{ "--qq", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "output Q-Q goodness of fit to xmgrace file <f>", 0 }, |
| 1472 |
{ "--surv", eslARG_STRING, NULL, NULL, NULL, NULL, NULL, NULL, "output survival plot to xmgrace file <f>", 0 }, |
| 1473 |
{ "--tail", eslARG_REAL, "0.1", NULL, "0<=x<=1", NULL, NULL, NULL, "set tail mass for fitting to <x>", 0 }, |
| 1474 |
{ 0,0,0,0,0,0,0,0,0,0 }, |
| 1475 |
}; |
| 1476 |
|
| 1477 |
static void |
| 1478 |
binmacro_utest(void) |
| 1479 |
{ |
| 1480 |
char *msg = "esl_histogram: binmacro unit test failure"; |
| 1481 |
ESL_HISTOGRAM *h = esl_histogram_Create(-100, 100, 1.0); |
| 1482 |
double trialx[3] = { -42.42, 0, 42.42 }; |
| 1483 |
double x, ai, bi; |
| 1484 |
int i,b; |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
for (i = 0; i < 3; i++) |
| 1489 |
{ |
| 1490 |
x = trialx[i]; |
| 1491 |
esl_histogram_Score2Bin(h, x, &b); |
| 1492 |
ai = esl_histogram_Bin2LBound(h, b); |
| 1493 |
bi = esl_histogram_Bin2UBound(h, b); |
| 1494 |
if (x <= ai || x > bi) esl_fatal(msg); |
| 1495 |
} |
| 1496 |
esl_histogram_Destroy(h); |
| 1497 |
return; |
| 1498 |
} |
| 1499 |
|
| 1500 |
static void |
| 1501 |
valuerange_utest(void) |
| 1502 |
{ |
| 1503 |
char *msg = "esl_histogram: value range unit test failure"; |
| 1504 |
ESL_HISTOGRAM *h = esl_histogram_Create(-100, 100, 1.0); |
| 1505 |
int b; |
| 1506 |
|
| 1507 |
esl_exception_SetHandler(&esl_nonfatal_handler); |
| 1508 |
|
| 1509 |
if (esl_histogram_Score2Bin(h, eslINFINITY, &b) != eslERANGE) esl_fatal(msg); |
| 1510 |
if (esl_histogram_Score2Bin(h, -eslINFINITY, &b) != eslERANGE) esl_fatal(msg); |
| 1511 |
if (esl_histogram_Score2Bin(h, eslNaN, &b) != eslERANGE) esl_fatal(msg); |
| 1512 |
if (esl_histogram_Score2Bin(h, 1e20, &b) != eslERANGE) esl_fatal(msg); |
| 1513 |
if (esl_histogram_Score2Bin(h, -1e20, &b) != eslERANGE) esl_fatal(msg); |
| 1514 |
|
| 1515 |
esl_exception_ResetDefaultHandler(); |
| 1516 |
esl_histogram_Destroy(h); |
| 1517 |
return; |
| 1518 |
} |
| 1519 |
|
| 1520 |
int |
| 1521 |
main(int argc, char **argv) |
| 1522 |
{ |
| 1523 |
ESL_GETOPTS *go; |
| 1524 |
ESL_RANDOMNESS *r; |
| 1525 |
ESL_HISTOGRAM *h; |
| 1526 |
ESL_HISTOGRAM *h1; |
| 1527 |
double p[2]; |
| 1528 |
double ep[2]; |
| 1529 |
double avg_ep[2]; |
| 1530 |
int ntrials, trial; |
| 1531 |
int ntrain, ntest; |
| 1532 |
int test_type; |
| 1533 |
enum { COLLECT_COMPLETE, COLLECT_CENSORED } cstrategy; |
| 1534 |
enum { FIT_BINNED, FIT_SAMPLES } bstrategy; |
| 1535 |
enum { FIT_COMPLETE, FIT_CENSORED, FIT_TAIL} fstrategy; |
| 1536 |
double phi; |
| 1537 |
int z; |
| 1538 |
double cmass; |
| 1539 |
double tailmass, save_tailmass; |
| 1540 |
int nfitted; |
| 1541 |
int nbins; |
| 1542 |
double G, Gp, X2, X2p, minGp, minX2p; |
| 1543 |
int verbose; |
| 1544 |
FILE *outfp; |
| 1545 |
char *ascfile, *plotfile, *survfile, *qqfile; |
| 1546 |
int i; |
| 1547 |
double x; |
| 1548 |
double *xv; |
| 1549 |
int n; |
| 1550 |
|
| 1551 |
go = esl_getopts_Create(options); |
| 1552 |
esl_opt_ProcessCmdline(go, argc, argv); |
| 1553 |
test_type = esl_opt_GetInteger(go, "-t"); |
| 1554 |
ntrials = esl_opt_GetInteger(go, "-j"); |
| 1555 |
ntrain = esl_opt_GetInteger(go, "-n"); |
| 1556 |
ntest = esl_opt_GetInteger(go, "-m"); |
| 1557 |
verbose = esl_opt_GetBoolean(go, "-v"); |
| 1558 |
cmass = esl_opt_GetReal (go, "--cmass"); |
| 1559 |
p[1] = esl_opt_GetReal (go, "--lambda"); |
| 1560 |
p[0] = esl_opt_GetReal (go, "--mu"); |
| 1561 |
phi = esl_opt_GetReal (go, "--phi"); |
| 1562 |
save_tailmass = esl_opt_GetReal (go, "--tail"); |
| 1563 |
ascfile = esl_opt_GetString (go, "--ascii"); |
| 1564 |
plotfile = esl_opt_GetString (go, "--plot"); |
| 1565 |
qqfile = esl_opt_GetString (go, "--qq"); |
| 1566 |
survfile = esl_opt_GetString (go, "--surv"); |
| 1567 |
esl_getopts_Destroy(go); |
| 1568 |
|
| 1569 |
r = esl_randomness_Create(42); |
| 1570 |
avg_ep[0] = 0.; |
| 1571 |
avg_ep[1] = 0.; |
| 1572 |
minGp = 1.; |
| 1573 |
minX2p = 1.; |
| 1574 |
tailmass = save_tailmass; |
| 1575 |
|
| 1576 |
if (test_type == 1) |
| 1577 |
{ |
| 1578 |
cstrategy = COLLECT_COMPLETE; |
| 1579 |
bstrategy = FIT_SAMPLES; |
| 1580 |
fstrategy = FIT_COMPLETE; |
| 1581 |
} |
| 1582 |
else if (test_type == 2) |
| 1583 |
{ |
| 1584 |
cstrategy = COLLECT_COMPLETE; |
| 1585 |
bstrategy = FIT_SAMPLES; |
| 1586 |
fstrategy = FIT_CENSORED; |
| 1587 |
} |
| 1588 |
else if (test_type == 3) |
| 1589 |
{ |
| 1590 |
cstrategy = COLLECT_COMPLETE; |
| 1591 |
bstrategy = FIT_SAMPLES; |
| 1592 |
fstrategy = FIT_TAIL; |
| 1593 |
} |
| 1594 |
else if (test_type == 4) |
| 1595 |
{ |
| 1596 |
cstrategy = COLLECT_CENSORED; |
| 1597 |
bstrategy = FIT_SAMPLES; |
| 1598 |
fstrategy = FIT_CENSORED; |
| 1599 |
} |
| 1600 |
else if (test_type == 5) |
| 1601 |
{ |
| 1602 |
cstrategy = COLLECT_COMPLETE; |
| 1603 |
bstrategy = FIT_BINNED; |
| 1604 |
fstrategy = FIT_TAIL; |
| 1605 |
} |
| 1606 |
else |
| 1607 |
esl_fatal("no such test type"); |
| 1608 |
|
| 1609 |
|
| 1610 |
for (trial = 0; trial < ntrials; trial++) |
| 1611 |
{ |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
h = esl_histogram_CreateFull(-100, 100, 0.1); |
| 1616 |
z = 0; |
| 1617 |
for (i = 0; i < ntrain; i++) { |
| 1618 |
x = esl_gumbel_Sample(r, p[0], p[1]); |
| 1619 |
if (cstrategy != COLLECT_CENSORED || x > phi) |
| 1620 |
esl_histogram_Add(h, x); |
| 1621 |
else |
| 1622 |
z++; |
| 1623 |
} |
| 1624 |
if (cstrategy == COLLECT_CENSORED) |
| 1625 |
esl_histogram_DeclareCensoring(h, z, phi); |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
if (cstrategy == COLLECT_COMPLETE && |
| 1638 |
bstrategy == FIT_SAMPLES && |
| 1639 |
fstrategy == FIT_COMPLETE) |
| 1640 |
{ |
| 1641 |
esl_histogram_GetData(h, &xv, &n); |
| 1642 |
if (esl_gumbel_FitComplete(xv, n, &(ep[0]), &ep[1]) != eslOK) esl_fatal("gumbel complete fit failed"); |
| 1643 |
} |
| 1644 |
else if (cstrategy == COLLECT_COMPLETE && |
| 1645 |
bstrategy == FIT_SAMPLES && |
| 1646 |
fstrategy == FIT_CENSORED) |
| 1647 |
{ |
| 1648 |
esl_histogram_GetTailByMass(h, cmass, &xv, &n, &z); |
| 1649 |
if (esl_gumbel_FitCensored(xv, n, z, xv[0], &(ep[0]), &ep[1]) != eslOK) esl_fatal("gumbel censored fit failed"); |
| 1650 |
} |
| 1651 |
else if (cstrategy == COLLECT_COMPLETE && |
| 1652 |
bstrategy == FIT_SAMPLES && |
| 1653 |
fstrategy == FIT_TAIL) |
| 1654 |
{ |
| 1655 |
esl_histogram_GetTailByMass(h, tailmass, &xv, &n, &z); |
| 1656 |
if (esl_exp_FitComplete(xv, n, &(ep[0]), &ep[1]) != eslOK) esl_fatal("exponential complete fit failed"); |
| 1657 |
} |
| 1658 |
else if (cstrategy == COLLECT_CENSORED && |
| 1659 |
bstrategy == FIT_SAMPLES && |
| 1660 |
fstrategy == FIT_CENSORED) |
| 1661 |
{ |
| 1662 |
esl_histogram_GetData(h, &xv, &n); |
| 1663 |
if (esl_gumbel_FitCensored(xv, n, h->z, h->phi, &(ep[0]), &ep[1]) != eslOK) esl_fatal("gumbel censored fit failed"); |
| 1664 |
} |
| 1665 |
else if (cstrategy == COLLECT_COMPLETE && |
| 1666 |
bstrategy == FIT_BINNED && |
| 1667 |
fstrategy == FIT_TAIL) |
| 1668 |
{ |
| 1669 |
tailmass = save_tailmass; |
| 1670 |
esl_histogram_SetTailByMass(h, tailmass, &tailmass); |
| 1671 |
if (esl_exp_FitCompleteBinned(h, &(ep[0]), &ep[1]) != eslOK) esl_fatal("exponential binned complete fit failed"); |
| 1672 |
} |
| 1673 |
else |
| 1674 |
ESL_EXCEPTION(eslEINVAL, "not a scenario we currently test"); |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
avg_ep[0] += ep[0] / (double) ntrials; |
| 1680 |
avg_ep[1] += ep[1] / (double) ntrials; |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
if (ntest > 0) |
| 1686 |
{ |
| 1687 |
h1 = esl_histogram_CreateFull(-100.05, 100.05, 0.2); |
| 1688 |
z = 0; |
| 1689 |
for (i = 0; i < ntest; i++) { |
| 1690 |
x = esl_gumbel_Sample(r, p[0], p[1]); |
| 1691 |
if (cstrategy != COLLECT_CENSORED || x > phi) |
| 1692 |
esl_histogram_Add(h1, x); |
| 1693 |
else |
| 1694 |
z++; |
| 1695 |
} |
| 1696 |
if (cstrategy == COLLECT_CENSORED) |
| 1697 |
esl_histogram_DeclareCensoring(h, z, phi); |
| 1698 |
} |
| 1699 |
else h1 = h; |
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
if (fstrategy == FIT_TAIL) |
| 1705 |
esl_histogram_SetExpectedTail(h1, ep[0], tailmass, |
| 1706 |
&esl_exp_generic_cdf, ep); |
| 1707 |
else |
| 1708 |
esl_histogram_SetExpect(h1, &esl_gumbel_generic_cdf, ep); |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
nfitted = (ntest == 0)? 2 : 0; |
| 1714 |
if (esl_histogram_Goodness(h1, nfitted, &nbins, &G, &Gp, &X2, &X2p) != eslOK) |
| 1715 |
esl_fatal("esl_histogram unit testing: goodness-of-fit failed"); |
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
if (Gp < minGp) minGp = Gp; |
| 1720 |
if (X2p < minX2p) minX2p = X2p; |
| 1721 |
|
| 1722 |
if (verbose) |
| 1723 |
printf("Estimated: %6.2f %6.4f nb %4d G %g\tGp %g\tX2 %g\tX2p %g\n", |
| 1724 |
ep[0], ep[1], nbins, G, Gp, X2, X2p); |
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
if (ascfile != NULL) |
| 1730 |
{ |
| 1731 |
outfp = fopen(ascfile, "w"); |
| 1732 |
esl_histogram_Write(outfp, h1); |
| 1733 |
fclose(outfp); |
| 1734 |
} |
| 1735 |
if (plotfile != NULL) |
| 1736 |
{ |
| 1737 |
outfp = fopen(plotfile, "w"); |
| 1738 |
esl_histogram_Plot(outfp, h1); |
| 1739 |
fclose(outfp); |
| 1740 |
} |
| 1741 |
if (survfile != NULL) |
| 1742 |
{ |
| 1743 |
outfp = fopen(survfile, "w"); |
| 1744 |
esl_histogram_PlotSurvival(outfp, h1); |
| 1745 |
fclose(outfp); |
| 1746 |
} |
| 1747 |
if (qqfile != NULL) |
| 1748 |
{ |
| 1749 |
outfp = fopen(qqfile, "w"); |
| 1750 |
if (fstrategy == FIT_TAIL) |
| 1751 |
esl_histogram_PlotQQ(outfp, h1, &esl_exp_generic_invcdf, ep); |
| 1752 |
else |
| 1753 |
esl_histogram_PlotQQ(outfp, h1, &esl_gumbel_generic_invcdf, ep); |
| 1754 |
fclose(outfp); |
| 1755 |
} |
| 1756 |
|
| 1757 |
esl_histogram_Destroy(h); |
| 1758 |
if (ntest > 0) esl_histogram_Destroy(h1); |
| 1759 |
} |
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
if (fstrategy != FIT_TAIL && fabs(avg_ep[0] - p[0]) > 0.1) |
| 1764 |
ESL_EXCEPTION(eslFAIL, "Something awry with Gumbel mu fit"); |
| 1765 |
if (fabs(avg_ep[1] - p[1]) > 0.1) |
| 1766 |
ESL_EXCEPTION(eslFAIL, "Something awry with lambda fit"); |
| 1767 |
if (minGp < 1. / (1000. * ntrials)) |
| 1768 |
ESL_EXCEPTION(eslFAIL, "Something awry with G-test"); |
| 1769 |
if (minX2p < 1. / (1000. * ntrials)) |
| 1770 |
ESL_EXCEPTION(eslFAIL, "Something awry with chi squared test"); |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
binmacro_utest(); |
| 1775 |
valuerange_utest(); |
| 1776 |
|
| 1777 |
esl_randomness_Destroy(r); |
| 1778 |
return 0; |
| 1779 |
} |
| 1780 |
#endif |
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
#ifdef eslHISTOGRAM_EXAMPLE |
| 1803 |
|
| 1804 |
#include "easel.h" |
| 1805 |
#include "esl_random.h" |
| 1806 |
#include "esl_histogram.h" |
| 1807 |
#include "esl_gumbel.h" |
| 1808 |
|
| 1809 |
int |
| 1810 |
main(int argc, char **argv) |
| 1811 |
{ |
| 1812 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 1813 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(-100, 100, 0.2); |
| 1814 |
int nsamples = 10000; |
| 1815 |
double mu = 10.0; |
| 1816 |
double lambda = 0.8; |
| 1817 |
double params[2]; |
| 1818 |
int i; |
| 1819 |
double x; |
| 1820 |
double *xv; |
| 1821 |
int n; |
| 1822 |
double G, Gp, X2, X2p; |
| 1823 |
|
| 1824 |
for (i = 0; i < nsamples; i++) { |
| 1825 |
x = esl_gumbel_Sample(r, mu, lambda); |
| 1826 |
esl_histogram_Add(h, x); |
| 1827 |
} |
| 1828 |
|
| 1829 |
esl_histogram_GetData(h, &xv, &n); |
| 1830 |
if (esl_gumbel_FitComplete(xv, n, &mu, &lambda) != eslOK) |
| 1831 |
esl_fatal("gumbel complete data fit failed"); |
| 1832 |
|
| 1833 |
params[0] = mu; |
| 1834 |
params[1] = lambda; |
| 1835 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 1836 |
|
| 1837 |
esl_histogram_Write(stdout, h); |
| 1838 |
if (esl_histogram_Goodness(h, 0, NULL, &G, &Gp, &X2, &X2p) != eslOK) |
| 1839 |
esl_fatal("goodness of fit testing failed"); |
| 1840 |
|
| 1841 |
printf("G = %f p = %f\n", G, Gp); |
| 1842 |
printf("X^2 = %f p = %f\n", X2, X2p); |
| 1843 |
|
| 1844 |
esl_histogram_Destroy(h); |
| 1845 |
esl_randomness_Destroy(r); |
| 1846 |
return 0; |
| 1847 |
} |
| 1848 |
|
| 1849 |
#endif |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
#ifdef eslHISTOGRAM_EXAMPLE2 |
| 1858 |
|
| 1859 |
#include "easel.h" |
| 1860 |
#include "esl_random.h" |
| 1861 |
#include "esl_histogram.h" |
| 1862 |
#include "esl_gumbel.h" |
| 1863 |
|
| 1864 |
int |
| 1865 |
main(int argc, char **argv) |
| 1866 |
{ |
| 1867 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 1868 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(-100, 100, 0.2); |
| 1869 |
int nsamples = 10000; |
| 1870 |
double mu = 10.0; |
| 1871 |
double lambda = 0.8; |
| 1872 |
double params[2]; |
| 1873 |
int i; |
| 1874 |
double x; |
| 1875 |
double *xv; |
| 1876 |
int n, z; |
| 1877 |
double G, Gp, X2, X2p; |
| 1878 |
|
| 1879 |
for (i = 0; i < nsamples; i++) { |
| 1880 |
x = esl_gumbel_Sample(r, mu, lambda); |
| 1881 |
esl_histogram_Add(h, x); |
| 1882 |
} |
| 1883 |
|
| 1884 |
esl_histogram_GetTailByMass(h, 0.5, &xv, &n, &z); |
| 1885 |
if (esl_gumbel_FitCensored(xv, n, z, xv[0], &mu, &lambda) != eslOK) |
| 1886 |
esl_fatal("gumbel censored fit failed"); |
| 1887 |
|
| 1888 |
params[0] = mu; |
| 1889 |
params[1] = lambda; |
| 1890 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 1891 |
|
| 1892 |
esl_histogram_Write(stdout, h); |
| 1893 |
if (esl_histogram_Goodness(h, 0, NULL, &G, &Gp, &X2, &X2p) != eslOK) |
| 1894 |
esl_fatal("goodness of fit testing failed"); |
| 1895 |
|
| 1896 |
printf("G = %f p = %f\n", G, Gp); |
| 1897 |
printf("X^2 = %f p = %f\n", X2, X2p); |
| 1898 |
|
| 1899 |
esl_histogram_Destroy(h); |
| 1900 |
esl_randomness_Destroy(r); |
| 1901 |
return 0; |
| 1902 |
} |
| 1903 |
|
| 1904 |
#endif |
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
#ifdef eslHISTOGRAM_EXAMPLE3 |
| 1912 |
|
| 1913 |
#include "easel.h" |
| 1914 |
#include "esl_random.h" |
| 1915 |
#include "esl_histogram.h" |
| 1916 |
#include "esl_gumbel.h" |
| 1917 |
#include "esl_exponential.h" |
| 1918 |
|
| 1919 |
int |
| 1920 |
main(int argc, char **argv) |
| 1921 |
{ |
| 1922 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 1923 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(-100, 100, 0.2); |
| 1924 |
int nsamples = 10000; |
| 1925 |
double mu = 10.0; |
| 1926 |
double lambda = 0.8; |
| 1927 |
double params[2]; |
| 1928 |
int i; |
| 1929 |
double x; |
| 1930 |
double *xv; |
| 1931 |
int n; |
| 1932 |
double G, Gp, X2, X2p; |
| 1933 |
|
| 1934 |
for (i = 0; i < nsamples; i++) { |
| 1935 |
x = esl_gumbel_Sample(r, mu, lambda); |
| 1936 |
esl_histogram_Add(h, x); |
| 1937 |
} |
| 1938 |
|
| 1939 |
esl_histogram_GetTailByMass(h, 0.1, &xv, &n, NULL); |
| 1940 |
esl_exp_FitComplete(xv, n, &mu, &lambda); |
| 1941 |
|
| 1942 |
params[0] = mu; |
| 1943 |
params[1] = lambda; |
| 1944 |
esl_histogram_SetExpectedTail(h, mu, 0.1, &esl_exp_generic_cdf, ¶ms); |
| 1945 |
|
| 1946 |
esl_histogram_Write(stdout, h); |
| 1947 |
if (esl_histogram_Goodness(h, 0, NULL, &G, &Gp, &X2, &X2p) != eslOK) |
| 1948 |
esl_fatal("goodness of fit testing failed"); |
| 1949 |
|
| 1950 |
printf("G = %f p = %f\n", G, Gp); |
| 1951 |
printf("X^2 = %f p = %f\n", X2, X2p); |
| 1952 |
|
| 1953 |
esl_histogram_Destroy(h); |
| 1954 |
esl_randomness_Destroy(r); |
| 1955 |
return 0; |
| 1956 |
} |
| 1957 |
|
| 1958 |
#endif |
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
#ifdef eslHISTOGRAM_EXAMPLE4 |
| 1966 |
|
| 1967 |
#include "easel.h" |
| 1968 |
#include "esl_random.h" |
| 1969 |
#include "esl_histogram.h" |
| 1970 |
#include "esl_gumbel.h" |
| 1971 |
|
| 1972 |
int |
| 1973 |
main(int argc, char **argv) |
| 1974 |
{ |
| 1975 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 1976 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(-100, 100, 0.2); |
| 1977 |
int nsamples = 10000; |
| 1978 |
double mu = 10.0; |
| 1979 |
double lambda = 0.8; |
| 1980 |
double phi = 9.0; |
| 1981 |
double params[2]; |
| 1982 |
int i; |
| 1983 |
double x; |
| 1984 |
double *xv; |
| 1985 |
int n, z; |
| 1986 |
double G, Gp, X2, X2p; |
| 1987 |
|
| 1988 |
z = 0; |
| 1989 |
for (i = 0; i < nsamples; i++) { |
| 1990 |
x = esl_gumbel_Sample(r, mu, lambda); |
| 1991 |
if (x > phi) esl_histogram_Add(h, x); |
| 1992 |
else z++; |
| 1993 |
} |
| 1994 |
|
| 1995 |
esl_histogram_GetData(h, &xv, &n); |
| 1996 |
if (esl_gumbel_FitCensored(xv, n, z, phi, &mu, &lambda) != eslOK) |
| 1997 |
esl_fatal("gumbel censored fit failed"); |
| 1998 |
|
| 1999 |
params[0] = mu; |
| 2000 |
params[1] = lambda; |
| 2001 |
esl_histogram_SetExpect(h, &esl_gumbel_generic_cdf, ¶ms); |
| 2002 |
|
| 2003 |
esl_histogram_Write(stdout, h); |
| 2004 |
if (esl_histogram_Goodness(h, 0, NULL, &G, &Gp, &X2, &X2p) != eslOK) |
| 2005 |
esl_fatal("goodness of fit testing failed"); |
| 2006 |
|
| 2007 |
printf("G = %f p = %f\n", G, Gp); |
| 2008 |
printf("X^2 = %f p = %f\n", X2, X2p); |
| 2009 |
|
| 2010 |
esl_histogram_Destroy(h); |
| 2011 |
esl_randomness_Destroy(r); |
| 2012 |
return 0; |
| 2013 |
} |
| 2014 |
|
| 2015 |
#endif |
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
#ifdef eslHISTOGRAM_EXAMPLE5 |
| 2023 |
|
| 2024 |
#include "easel.h" |
| 2025 |
#include "esl_random.h" |
| 2026 |
#include "esl_histogram.h" |
| 2027 |
#include "esl_gumbel.h" |
| 2028 |
#include "esl_exponential.h" |
| 2029 |
|
| 2030 |
int |
| 2031 |
main(int argc, char **argv) |
| 2032 |
{ |
| 2033 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 2034 |
ESL_HISTOGRAM *h = esl_histogram_Create(-100, 100, 1.0); |
| 2035 |
int nsamples = 10000; |
| 2036 |
double mu = 10.0; |
| 2037 |
double lambda = 0.8; |
| 2038 |
double params[2]; |
| 2039 |
int i; |
| 2040 |
double x; |
| 2041 |
double actual_mass; |
| 2042 |
double G, Gp, X2, X2p; |
| 2043 |
|
| 2044 |
for (i = 0; i < nsamples; i++) { |
| 2045 |
x = esl_gumbel_Sample(r, mu, lambda); |
| 2046 |
x = ceil(x); |
| 2047 |
esl_histogram_Add(h, x); |
| 2048 |
} |
| 2049 |
|
| 2050 |
esl_histogram_SetTailByMass(h, 0.1, &actual_mass); |
| 2051 |
esl_histogram_DeclareRounding(h); |
| 2052 |
if (esl_exp_FitCompleteBinned(h, &mu, &lambda) != eslOK) |
| 2053 |
esl_fatal("exponential ML fitting failed"); |
| 2054 |
|
| 2055 |
params[0] = mu; |
| 2056 |
params[1] = lambda; |
| 2057 |
esl_histogram_SetExpectedTail(h, mu, actual_mass, &esl_exp_generic_cdf, ¶ms); |
| 2058 |
|
| 2059 |
esl_histogram_Write(stdout, h); |
| 2060 |
if (esl_histogram_Goodness(h, 0, NULL, &G, &Gp, &X2, &X2p) != eslOK) |
| 2061 |
esl_fatal("goodness of fit testing failed"); |
| 2062 |
|
| 2063 |
printf("G = %f p = %f\n", G, Gp); |
| 2064 |
printf("X^2 = %f p = %f\n", X2, X2p); |
| 2065 |
|
| 2066 |
esl_histogram_Destroy(h); |
| 2067 |
esl_randomness_Destroy(r); |
| 2068 |
return 0; |
| 2069 |
} |
| 2070 |
|
| 2071 |
#endif |
| 2072 |
|
| 2073 |
|
| 2074 |
|