| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <esl_config.h> |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <math.h> |
| 28 |
#include <float.h> |
| 29 |
|
| 30 |
#include "easel.h" |
| 31 |
#include "esl_minimizer.h" |
| 32 |
#include "esl_random.h" |
| 33 |
#include "esl_stats.h" |
| 34 |
#include "esl_vectorops.h" |
| 35 |
|
| 36 |
#include "esl_gumbel.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
double |
| 54 |
esl_gumbel_pdf(double x, double mu, double lambda) |
| 55 |
{ |
| 56 |
double y; |
| 57 |
y = lambda * (x - mu); |
| 58 |
return (lambda * exp(-y - exp(-y))); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
double |
| 73 |
esl_gumbel_logpdf(double x, double mu, double lambda) |
| 74 |
{ |
| 75 |
double y; |
| 76 |
y = lambda * (x - mu); |
| 77 |
return (log(lambda) -y - exp(-y)); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
double |
| 92 |
esl_gumbel_cdf(double x, double mu, double lambda) |
| 93 |
{ |
| 94 |
double y; |
| 95 |
y = lambda*(x-mu); |
| 96 |
return exp(-exp(-y)); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
double |
| 110 |
esl_gumbel_logcdf(double x, double mu, double lambda) |
| 111 |
{ |
| 112 |
double y; |
| 113 |
y = lambda*(x-mu); |
| 114 |
return (-exp(-y)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
double |
| 129 |
esl_gumbel_surv(double x, double mu, double lambda) |
| 130 |
{ |
| 131 |
double y = lambda*(x-mu); |
| 132 |
double ey = -exp(-y); |
| 133 |
|
| 134 |
|
| 135 |
if (fabs(ey) < eslSMALLX1) return -ey; |
| 136 |
else return 1 - exp(ey); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
double |
| 150 |
esl_gumbel_logsurv(double x, double mu, double lambda) |
| 151 |
{ |
| 152 |
double y = lambda*(x-mu); |
| 153 |
double ey = -exp(-y); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
if (fabs(ey) < eslSMALLX1) return -y; |
| 161 |
else if (fabs(exp(ey)) < eslSMALLX1) return -exp(ey); |
| 162 |
else return log(1-exp(ey)); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
double |
| 172 |
esl_gumbel_invcdf(double p, double mu, double lambda) |
| 173 |
{ |
| 174 |
return mu - ( log(-1. * log(p)) / lambda); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
double |
| 185 |
esl_gumbel_invsurv(double p, double mu, double lambda) |
| 186 |
{ |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
double log_part; |
| 199 |
if (p < eslSMALLX1) { |
| 200 |
log_part = (pow(p,p) - 1 ) / p; |
| 201 |
} else { |
| 202 |
log_part = log(-1. * log(1-p)); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
return mu - ( log_part / lambda); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
double |
| 221 |
esl_gumbel_generic_pdf(double p, void *params) |
| 222 |
{ |
| 223 |
double *v = (double *) params; |
| 224 |
return esl_gumbel_pdf(p, v[0], v[1]); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
double |
| 232 |
esl_gumbel_generic_cdf(double x, void *params) |
| 233 |
{ |
| 234 |
double *p = (double *) params; |
| 235 |
return esl_gumbel_cdf(x, p[0], p[1]); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
double |
| 243 |
esl_gumbel_generic_surv(double p, void *params) |
| 244 |
{ |
| 245 |
double *v = (double *) params; |
| 246 |
return esl_gumbel_surv(p, v[0], v[1]); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
double |
| 254 |
esl_gumbel_generic_invcdf(double p, void *params) |
| 255 |
{ |
| 256 |
double *v = (double *) params; |
| 257 |
return esl_gumbel_invcdf(p, v[0], v[1]); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
int |
| 282 |
esl_gumbel_Plot(FILE *fp, double mu, double lambda, |
| 283 |
double (*func)(double x, double mu, double lambda), |
| 284 |
double xmin, double xmax, double xstep) |
| 285 |
{ |
| 286 |
double x; |
| 287 |
for (x = xmin; x <= xmax; x += xstep) |
| 288 |
if (fprintf(fp, "%f\t%g\n", x, (*func)(x, mu, lambda)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "gumbel plot write failed"); |
| 289 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "gumbel plot write failed"); |
| 290 |
return eslOK; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
double |
| 307 |
esl_gumbel_Sample(ESL_RANDOMNESS *r, double mu, double lambda) |
| 308 |
{ |
| 309 |
double p; |
| 310 |
p = esl_rnd_UniformPositive(r); |
| 311 |
return esl_gumbel_invcdf(p, mu, lambda); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
static void |
| 339 |
lawless416(double *x, int n, double lambda, double *ret_f, double *ret_df) |
| 340 |
{ |
| 341 |
double esum; |
| 342 |
double xesum; |
| 343 |
double xxesum; |
| 344 |
double xsum; |
| 345 |
int i; |
| 346 |
|
| 347 |
esum = xesum = xsum = xxesum = 0.; |
| 348 |
for (i = 0; i < n; i++) |
| 349 |
{ |
| 350 |
xsum += x[i]; |
| 351 |
xesum += x[i] * exp(-1. * lambda * x[i]); |
| 352 |
xxesum += x[i] * x[i] * exp(-1. * lambda * x[i]); |
| 353 |
esum += exp(-1. * lambda * x[i]); |
| 354 |
} |
| 355 |
*ret_f = (1./lambda) - (xsum / n) + (xesum / esum); |
| 356 |
*ret_df = ((xesum / esum) * (xesum / esum)) |
| 357 |
- (xxesum / esum) |
| 358 |
- (1. / (lambda * lambda)); |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 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 |
int |
| 398 |
esl_gumbel_FitComplete(double *x, int n, double *ret_mu, double *ret_lambda) |
| 399 |
{ |
| 400 |
double variance; |
| 401 |
double lambda, mu; |
| 402 |
double fx; |
| 403 |
double dfx; |
| 404 |
double esum; |
| 405 |
double tol = 1e-5; |
| 406 |
int i; |
| 407 |
int status; |
| 408 |
|
| 409 |
if (n <= 1) { status = eslEINVAL; goto FAILURE; } |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
esl_stats_DMean(x, n, NULL, &variance); |
| 415 |
lambda = eslCONST_PI / sqrt(6.*variance); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
for (i = 0; i < 100; i++) |
| 420 |
{ |
| 421 |
lawless416(x, n, lambda, &fx, &dfx); |
| 422 |
if (fabs(fx) < tol) break; |
| 423 |
lambda = lambda - fx / dfx; |
| 424 |
if (lambda <= 0.) lambda = 0.001; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
if (i == 100) |
| 435 |
{ |
| 436 |
double left, right, mid; |
| 437 |
ESL_DPRINTF2(("esl_gumbel_FitComplete(): Newton/Raphson failed; switchover to bisection\n")); |
| 438 |
|
| 439 |
|
| 440 |
left = 0.; |
| 441 |
right = eslCONST_PI / sqrt(6.*variance); |
| 442 |
lawless416(x, n, lambda, &fx, &dfx); |
| 443 |
while (fx > 0.) |
| 444 |
{ |
| 445 |
right *= 2.; |
| 446 |
if (right > 1000.) |
| 447 |
{ |
| 448 |
ESL_DPRINTF2(("Failed to bracket root in esl_gumbel_FitComplete().")); |
| 449 |
status = eslENORESULT; |
| 450 |
goto FAILURE; |
| 451 |
} |
| 452 |
|
| 453 |
lawless416(x, n, right, &fx, &dfx); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
for (i = 0; i < 100; i++) |
| 458 |
{ |
| 459 |
mid = (left + right) / 2.; |
| 460 |
lawless416(x, n, mid, &fx, &dfx); |
| 461 |
if (fabs(fx) < tol) break; |
| 462 |
if (fx > 0.) left = mid; |
| 463 |
else right = mid; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
if (i == 100) |
| 468 |
{ |
| 469 |
ESL_DPRINTF2(("Even bisection search failed in esl_gumbel_FitComplete().\n")); |
| 470 |
status = eslENORESULT; |
| 471 |
goto FAILURE; |
| 472 |
} |
| 473 |
|
| 474 |
lambda = mid; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
esum = 0.; |
| 480 |
for (i = 0; i < n; i++) |
| 481 |
esum += exp(-lambda * x[i]); |
| 482 |
mu = -log(esum / n) / lambda; |
| 483 |
|
| 484 |
*ret_lambda = lambda; |
| 485 |
*ret_mu = mu; |
| 486 |
return eslOK; |
| 487 |
|
| 488 |
FAILURE: |
| 489 |
*ret_mu = 0.0; |
| 490 |
*ret_lambda = 0.0; |
| 491 |
return status; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
int |
| 517 |
esl_gumbel_FitCompleteLoc(double *x, int n, double lambda, double *ret_mu) |
| 518 |
{ |
| 519 |
double esum; |
| 520 |
int i; |
| 521 |
int status; |
| 522 |
|
| 523 |
if (n <= 1) { status = eslEINVAL; goto FAILURE; } |
| 524 |
|
| 525 |
|
| 526 |
esum = 0.; |
| 527 |
for (i = 0; i < n; i++) |
| 528 |
esum += exp(-lambda * x[i]); |
| 529 |
*ret_mu = -log(esum / n) / lambda; |
| 530 |
return eslOK; |
| 531 |
|
| 532 |
#if 0 |
| 533 |
|
| 534 |
double mean, variance; |
| 535 |
esl_stats_DMean(x, n, &mean, &variance); |
| 536 |
*ret_mu = mean - 0.57722/lambda; |
| 537 |
return eslOK; |
| 538 |
#endif |
| 539 |
|
| 540 |
FAILURE: |
| 541 |
*ret_mu = 0.; |
| 542 |
return status; |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
#if 0 |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static int |
| 555 |
direct_mv_fit(double *x, int n, double *ret_mu, double *ret_lambda) |
| 556 |
{ |
| 557 |
double mean, variance; |
| 558 |
|
| 559 |
esl_stats_DMean(x, n, &mean, &variance); |
| 560 |
*ret_lambda = eslCONST_PI / sqrt(6.*variance); |
| 561 |
*ret_mu = mean - 0.57722/(*ret_lambda); |
| 562 |
return eslOK; |
| 563 |
} |
| 564 |
#endif |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static void |
| 594 |
lawless422(double *x, int n, int z, double phi, |
| 595 |
double lambda, double *ret_f, double *ret_df) |
| 596 |
{ |
| 597 |
double esum; |
| 598 |
double xesum; |
| 599 |
double xxesum; |
| 600 |
double xsum; |
| 601 |
int i; |
| 602 |
|
| 603 |
esum = xesum = xsum = xxesum = 0.; |
| 604 |
for (i = 0; i < n; i++) |
| 605 |
{ |
| 606 |
xsum += x[i]; |
| 607 |
esum += exp(-1. * lambda * x[i]); |
| 608 |
xesum += x[i] * exp(-1. * lambda * x[i]); |
| 609 |
xxesum += x[i] * x[i] * exp(-1. * lambda * x[i]); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
esum += (double) z * exp(-1. * lambda * phi); |
| 615 |
xesum += (double) z * phi * exp(-1. * lambda * phi); |
| 616 |
xxesum += (double) z * phi * phi * exp(-1. * lambda * phi); |
| 617 |
|
| 618 |
*ret_f = 1./lambda - xsum / n + xesum / esum; |
| 619 |
*ret_df = ((xesum / esum) * (xesum / esum)) |
| 620 |
- (xxesum / esum) |
| 621 |
- (1. / (lambda * lambda)); |
| 622 |
|
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
int |
| 656 |
esl_gumbel_FitCensored(double *x, int n, int z, double phi, double *ret_mu, double *ret_lambda) |
| 657 |
{ |
| 658 |
double variance; |
| 659 |
double lambda, mu; |
| 660 |
double fx; |
| 661 |
double dfx; |
| 662 |
double esum; |
| 663 |
double tol = 1e-5; |
| 664 |
int i; |
| 665 |
int status; |
| 666 |
|
| 667 |
if (n <= 1) { status = eslEINVAL; goto FAILURE; } |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
esl_stats_DMean(x, n, NULL, &variance); |
| 673 |
lambda = eslCONST_PI / sqrt(6.*variance); |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
for (i = 0; i < 100; i++) |
| 678 |
{ |
| 679 |
lawless422(x, n, z, phi, lambda, &fx, &dfx); |
| 680 |
if (fabs(fx) < tol) break; |
| 681 |
lambda = lambda - fx / dfx; |
| 682 |
if (lambda <= 0.) lambda = 0.001; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
if (i == 100) |
| 693 |
{ |
| 694 |
double left, right, mid; |
| 695 |
ESL_DPRINTF2(("esl_gumbel_FitCensored(): Newton/Raphson failed; switched to bisection\n")); |
| 696 |
|
| 697 |
|
| 698 |
left = 0.; |
| 699 |
right = eslCONST_PI / sqrt(6.*variance); |
| 700 |
lawless422(x, n, z, phi, right, &fx, &dfx); |
| 701 |
while (fx > 0.) |
| 702 |
{ |
| 703 |
right *= 2.; |
| 704 |
if (right > 1000.) |
| 705 |
{ |
| 706 |
ESL_DPRINTF2(("Failed to bracket root in esl_gumbel_FitCensored().")); |
| 707 |
status = eslENORESULT; |
| 708 |
goto FAILURE; |
| 709 |
} |
| 710 |
lawless422(x, n, z, phi, right, &fx, &dfx); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
for (i = 0; i < 100; i++) |
| 715 |
{ |
| 716 |
mid = (left + right) / 2.; |
| 717 |
lawless422(x, n, z, phi, mid, &fx, &dfx); |
| 718 |
if (fabs(fx) < tol) break; |
| 719 |
if (fx > 0.) left = mid; |
| 720 |
else right = mid; |
| 721 |
} |
| 722 |
if (i == 100) |
| 723 |
{ |
| 724 |
ESL_DPRINTF2(("Even bisection search failed in esl_gumbel_FitCensored().\n")); |
| 725 |
status = eslENORESULT; |
| 726 |
goto FAILURE; |
| 727 |
} |
| 728 |
lambda = mid; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
esum = 0.; |
| 734 |
for (i = 0; i < n; i++) |
| 735 |
esum += exp(-lambda * x[i]); |
| 736 |
esum += z * exp(-1. * lambda * phi); |
| 737 |
mu = -log(esum / n) / lambda; |
| 738 |
|
| 739 |
*ret_lambda = lambda; |
| 740 |
*ret_mu = mu; |
| 741 |
return eslOK; |
| 742 |
|
| 743 |
FAILURE: |
| 744 |
*ret_lambda = 0.0; |
| 745 |
*ret_mu = 0.0; |
| 746 |
return status; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
int |
| 776 |
esl_gumbel_FitCensoredLoc(double *x, int n, int z, double phi, double lambda, |
| 777 |
double *ret_mu) |
| 778 |
{ |
| 779 |
double esum; |
| 780 |
int i; |
| 781 |
int status; |
| 782 |
|
| 783 |
if (n <= 1) { status = eslEINVAL; goto FAILURE; } |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
esum = 0.; |
| 789 |
for (i = 0; i < n; i++) |
| 790 |
esum += exp(-lambda * x[i]); |
| 791 |
esum += z * exp(-1. * lambda * phi); |
| 792 |
|
| 793 |
*ret_mu = -log(esum / (double) n) / lambda; |
| 794 |
return eslOK; |
| 795 |
|
| 796 |
FAILURE: |
| 797 |
*ret_mu = 0.; |
| 798 |
return status; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
struct tevd_data { |
| 811 |
double *x; |
| 812 |
int n; |
| 813 |
double phi; |
| 814 |
}; |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
static double |
| 823 |
tevd_func(double *p, int nparam, void *dptr) |
| 824 |
{ |
| 825 |
double mu, w, lambda; |
| 826 |
struct tevd_data *data; |
| 827 |
double *x; |
| 828 |
int n; |
| 829 |
double phi; |
| 830 |
double logL; |
| 831 |
int i; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
mu = p[0]; |
| 836 |
w = p[1]; |
| 837 |
lambda = exp(w); |
| 838 |
data = (struct tevd_data *) dptr; |
| 839 |
x = data->x; |
| 840 |
n = data->n; |
| 841 |
phi = data->phi; |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
logL = n * log(lambda); |
| 846 |
for (i = 0; i < n; i++) |
| 847 |
logL -= lambda * (x[i] - mu); |
| 848 |
for (i = 0; i < n; i++) |
| 849 |
logL -= exp(-1. * lambda * (x[i] - mu)); |
| 850 |
logL -= n * esl_gumbel_logsurv(phi, mu, lambda); |
| 851 |
|
| 852 |
return -1.0 * logL; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
static void |
| 863 |
tevd_grad(double *p, int nparam, void *dptr, double *dp) |
| 864 |
{ |
| 865 |
double mu, lambda, w; |
| 866 |
struct tevd_data *data; |
| 867 |
double *x; |
| 868 |
int n; |
| 869 |
double phi; |
| 870 |
double dmu, dw; |
| 871 |
double coeff; |
| 872 |
int i; |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
mu = p[0]; |
| 877 |
w = p[1]; |
| 878 |
lambda = exp(w); |
| 879 |
data = (struct tevd_data *) dptr; |
| 880 |
x = data->x; |
| 881 |
n = data->n; |
| 882 |
phi = data->phi; |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
if (lambda*(phi-mu) > 50.) |
| 890 |
coeff = lambda; |
| 891 |
else |
| 892 |
coeff = esl_gumbel_pdf(phi, mu, lambda) / esl_gumbel_surv(phi, mu, lambda); |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
dmu = n * lambda; |
| 897 |
for (i = 0; i < n; i++) |
| 898 |
dmu -= lambda * exp(-1. * lambda * (x[i] - mu)); |
| 899 |
dmu -= n * coeff; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
dw = n; |
| 904 |
for (i = 0; i < n; i++) dw -= (x[i] - mu) * lambda; |
| 905 |
for (i = 0; i < n; i++) dw += (x[i] - mu) * lambda * exp(-1. * lambda * (x[i] - mu)); |
| 906 |
dw += n * (phi - mu) * coeff; |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
dp[0] = -1. * dmu; |
| 911 |
dp[1] = -1. * dw; |
| 912 |
return; |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
int |
| 957 |
esl_gumbel_FitTruncated(double *x, int n, double phi, double *ret_mu, double *ret_lambda) |
| 958 |
{ |
| 959 |
ESL_MIN_CFG *cfg = NULL; |
| 960 |
struct tevd_data data; |
| 961 |
double p[2]; |
| 962 |
double mean, variance; |
| 963 |
double mu, lambda; |
| 964 |
double fx; |
| 965 |
int i; |
| 966 |
int status; |
| 967 |
|
| 968 |
|
| 969 |
if ((cfg = esl_min_cfg_Create(2)) == NULL) { status = eslEMEM; goto ERROR; } |
| 970 |
cfg->u[0] = 2.0; |
| 971 |
cfg->u[1] = 0.1; |
| 972 |
cfg->cg_rtol = 1e-4; |
| 973 |
|
| 974 |
|
| 975 |
if (n <= 1) { status = eslEINVAL; goto ERROR; } |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
for (i = 1; i < n; i++) if (x[i] != x[0]) break; |
| 981 |
if (i == n) { status = eslENORESULT; goto ERROR; } |
| 982 |
|
| 983 |
data.x = x; |
| 984 |
data.n = n; |
| 985 |
data.phi = phi; |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
esl_stats_DMean(x, n, &mean, &variance); |
| 996 |
lambda = eslCONST_PI / sqrt(6.*variance); |
| 997 |
mu = mean - 0.57722/lambda; |
| 998 |
|
| 999 |
p[0] = mu; |
| 1000 |
p[1] = log(lambda); |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
status = esl_min_ConjugateGradientDescent(cfg, p, 2, |
| 1006 |
&tevd_func, &tevd_grad,(void *)(&data), |
| 1007 |
&fx, NULL); |
| 1008 |
if (status == eslENOHALT) { status = eslENORESULT; goto ERROR; } |
| 1009 |
else if (status != eslOK) goto ERROR; |
| 1010 |
|
| 1011 |
esl_min_cfg_Destroy(cfg); |
| 1012 |
*ret_mu = p[0]; |
| 1013 |
*ret_lambda = exp(p[1]); |
| 1014 |
return status; |
| 1015 |
|
| 1016 |
ERROR: |
| 1017 |
esl_min_cfg_Destroy(cfg); |
| 1018 |
*ret_mu = 0.0; |
| 1019 |
*ret_lambda = 0.0; |
| 1020 |
return status; |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
#ifdef eslGUMBEL_STATS |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
#include <stdio.h> |
| 1040 |
|
| 1041 |
#include "easel.h" |
| 1042 |
#include "esl_random.h" |
| 1043 |
#include "esl_minimizer.h" |
| 1044 |
#include "esl_gumbel.h" |
| 1045 |
|
| 1046 |
int |
| 1047 |
main(int argc, char **argv) |
| 1048 |
{ |
| 1049 |
ESL_RANDOMNESS *r; |
| 1050 |
int totalN[4] = {100, 1000, 10000, 100000}; |
| 1051 |
int nexps = 4; |
| 1052 |
int exp; |
| 1053 |
int trial, ntrials; |
| 1054 |
double phi; |
| 1055 |
int i; |
| 1056 |
int n; |
| 1057 |
double *x; |
| 1058 |
double mu, lambda; |
| 1059 |
double est_mu, est_lambda; |
| 1060 |
double val; |
| 1061 |
int do_complete, do_censored, do_truncated, do_location; |
| 1062 |
|
| 1063 |
ntrials = 500; |
| 1064 |
mu = -20.0; |
| 1065 |
lambda = 0.693; |
| 1066 |
phi = -15.; |
| 1067 |
|
| 1068 |
do_complete = TRUE; |
| 1069 |
do_censored = FALSE; |
| 1070 |
do_truncated = FALSE; |
| 1071 |
do_location = FALSE; |
| 1072 |
|
| 1073 |
r = esl_randomness_Create(0); |
| 1074 |
x = malloc(sizeof(double) * totalN[nexps-1]); |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
if (do_complete) { |
| 1079 |
for (exp = 0; exp < nexps; exp++) |
| 1080 |
{ |
| 1081 |
for (trial = 0; trial < ntrials; trial++) |
| 1082 |
{ |
| 1083 |
for (i = 0; i < totalN[exp]; i++) |
| 1084 |
x[i] = esl_gumbel_Sample(r, mu, lambda); |
| 1085 |
|
| 1086 |
|
| 1087 |
if (esl_gumbel_FitComplete(x, totalN[exp], &est_mu, &est_lambda) != eslOK) |
| 1088 |
esl_fatal("gumbel complete fit fit failed"); |
| 1089 |
|
| 1090 |
printf("complete %6d %6d %9.5f %9.5f %8.6f %8.6f\n", |
| 1091 |
totalN[exp], totalN[exp], mu, est_mu, lambda, est_lambda); |
| 1092 |
} |
| 1093 |
printf("\n"); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
if (do_censored) { |
| 1100 |
for (exp = 0; exp < nexps; exp++) |
| 1101 |
{ |
| 1102 |
for (trial = 0; trial < ntrials; trial++) |
| 1103 |
{ |
| 1104 |
for (n = 0, i = 0; i < totalN[exp]; i++) |
| 1105 |
{ |
| 1106 |
val = esl_gumbel_Sample(r, mu, lambda); |
| 1107 |
if (val >= phi) x[n++] = val; |
| 1108 |
} |
| 1109 |
if (esl_gumbel_FitCensored(x, n, totalN[exp]-n, phi, &est_mu, &est_lambda) != eslOK) |
| 1110 |
esl_fatal("gumbel censored fit failed"); |
| 1111 |
|
| 1112 |
printf("censored %6d %6d %9.5f %9.5f %8.6f %8.6f\n", |
| 1113 |
totalN[exp], n, mu, est_mu, lambda, est_lambda); |
| 1114 |
} |
| 1115 |
printf("\n"); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
if (do_truncated) { |
| 1122 |
for (exp = 0; exp < nexps; exp++) |
| 1123 |
{ |
| 1124 |
for (trial = 0; trial < ntrials; trial++) |
| 1125 |
{ |
| 1126 |
for (n = 0, i = 0; i < totalN[exp]; i++) |
| 1127 |
{ |
| 1128 |
val = esl_gumbel_Sample(r, mu, lambda); |
| 1129 |
if (val >= phi) x[n++] = val; |
| 1130 |
} |
| 1131 |
if (esl_gumbel_FitTruncated(x, n, phi, &est_mu, &est_lambda) != eslOK) |
| 1132 |
esl_fatal("gumbel truncated fit failed"); |
| 1133 |
|
| 1134 |
printf("truncated %6d %6d %9.5f %9.5f %8.6f %8.6f\n", |
| 1135 |
totalN[exp], n, mu, est_mu, lambda, est_lambda); |
| 1136 |
} |
| 1137 |
printf("\n"); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
if (do_location) { |
| 1144 |
for (exp = 0; exp < nexps; exp++) |
| 1145 |
{ |
| 1146 |
for (trial = 0; trial < ntrials; trial++) |
| 1147 |
{ |
| 1148 |
for (i = 0; i < totalN[exp]; i++) |
| 1149 |
x[i] = esl_gumbel_Sample(r, mu, lambda); |
| 1150 |
|
| 1151 |
if (esl_gumbel_FitCompleteLoc(x, totalN[exp], lambda, &est_mu) != eslOK) |
| 1152 |
esl_fatal("gumbel location-only complete fit failed"); |
| 1153 |
|
| 1154 |
printf("location %6d %6d %9.5f %9.5f\n", |
| 1155 |
totalN[exp], totalN[exp], mu, est_mu); |
| 1156 |
} |
| 1157 |
printf("\n"); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
esl_randomness_Destroy(r); |
| 1162 |
free(x); |
| 1163 |
return 0; |
| 1164 |
} |
| 1165 |
#endif |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
#ifdef eslGUMBEL_TESTDRIVE |
| 1171 |
|
| 1172 |
#include "esl_getopts.h" |
| 1173 |
#include "esl_random.h" |
| 1174 |
|
| 1175 |
static void |
| 1176 |
utest_fitting(ESL_RANDOMNESS *rng) |
| 1177 |
{ |
| 1178 |
char msg[] = "esl_gumbel: fitting unit test failed"; |
| 1179 |
int totalN = 10000; |
| 1180 |
double pmu = -20.; |
| 1181 |
double plambda = 0.4; |
| 1182 |
double phi = -20.; |
| 1183 |
double *x = NULL; |
| 1184 |
int i; |
| 1185 |
int n; |
| 1186 |
double mu; |
| 1187 |
double lambda; |
| 1188 |
int status; |
| 1189 |
|
| 1190 |
|
| 1191 |
ESL_ALLOC(x, sizeof(double) * totalN); |
| 1192 |
for (i = 0; i < totalN; i++) |
| 1193 |
x[i] = esl_gumbel_Sample(rng, pmu, plambda); |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
if ((status = esl_gumbel_FitComplete(x, totalN, &mu, &lambda)) != eslOK) esl_fatal(msg); |
| 1199 |
if (fabs((mu -pmu) /pmu) > 0.01) esl_fatal(msg); |
| 1200 |
if (fabs((lambda-plambda)/plambda) > 0.03) esl_fatal(msg); |
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
if ((status = esl_gumbel_FitCompleteLoc(x, totalN, plambda, &mu)) != eslOK) esl_fatal(msg); |
| 1205 |
if (fabs((mu - pmu) / pmu) > 0.01) esl_fatal(msg); |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
for (n=0, i = 0; i < totalN; i++) |
| 1212 |
if (x[i] >= phi) x[n++] = x[i]; |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
if ((status = esl_gumbel_FitCensored(x, n, totalN-n, phi, &mu, &lambda)) != eslOK) esl_fatal(msg); |
| 1218 |
if (fabs((mu - pmu) /pmu) > 0.01) esl_fatal(msg); |
| 1219 |
if (fabs((lambda - plambda)/plambda) > 0.04) esl_fatal(msg); |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
if ((status = esl_gumbel_FitCensoredLoc(x, n, totalN-n, phi, plambda, &mu)) != eslOK) esl_fatal(msg); |
| 1224 |
if (fabs((mu - pmu) / pmu) > 0.01) esl_fatal(msg); |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
if ((status = esl_gumbel_FitTruncated(x, n, phi, &mu, &lambda)) != eslOK) esl_fatal(msg); |
| 1230 |
if (fabs((mu - pmu) /pmu) > 0.05) esl_fatal(msg); |
| 1231 |
if (fabs((lambda - plambda)/plambda) > 0.08) esl_fatal(msg); |
| 1232 |
|
| 1233 |
free(x); |
| 1234 |
return; |
| 1235 |
|
| 1236 |
ERROR: |
| 1237 |
if (x) free(x); |
| 1238 |
esl_fatal("allocation failure in esl_gumbel : fitting unit test"); |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
static void |
| 1243 |
utest_fit_failure(void) |
| 1244 |
{ |
| 1245 |
char msg[] = "esl_gumbel: fit_failure unit test failed"; |
| 1246 |
double x[10]; |
| 1247 |
double mu; |
| 1248 |
double lambda; |
| 1249 |
int status; |
| 1250 |
|
| 1251 |
x[0] = 1.0; |
| 1252 |
x[1] = 1.0; |
| 1253 |
|
| 1254 |
|
| 1255 |
status = esl_gumbel_FitComplete(x, 1, &mu, &lambda); |
| 1256 |
if (status != eslEINVAL) esl_fatal(msg); |
| 1257 |
if (mu != 0.0) esl_fatal(msg); |
| 1258 |
if (lambda != 0.0) esl_fatal(msg); |
| 1259 |
|
| 1260 |
|
| 1261 |
status = esl_gumbel_FitComplete(x, 2, &mu, &lambda); |
| 1262 |
if (status != eslENORESULT) esl_fatal(msg); |
| 1263 |
if (mu != 0.0) esl_fatal(msg); |
| 1264 |
if (lambda != 0.0) esl_fatal(msg); |
| 1265 |
|
| 1266 |
|
| 1267 |
status = esl_gumbel_FitCompleteLoc(x, 1, 1.0, &mu); |
| 1268 |
if (status != eslEINVAL) esl_fatal(msg); |
| 1269 |
if (mu != 0.0) esl_fatal(msg); |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
status = esl_gumbel_FitCensored(x, 1, 1, 0.0, &mu, &lambda); |
| 1274 |
if (status != eslEINVAL) esl_fatal(msg); |
| 1275 |
if (mu != 0.0) esl_fatal(msg); |
| 1276 |
if (lambda != 0.0) esl_fatal(msg); |
| 1277 |
|
| 1278 |
|
| 1279 |
status = esl_gumbel_FitCensored(x, 2, 1, 0.0, &mu, &lambda); |
| 1280 |
if (status != eslENORESULT) esl_fatal(msg); |
| 1281 |
if (mu != 0.0) esl_fatal(msg); |
| 1282 |
if (lambda != 0.0) esl_fatal(msg); |
| 1283 |
|
| 1284 |
|
| 1285 |
status = esl_gumbel_FitCensoredLoc(x, 1, 1, 0.0, 1.0, &mu); |
| 1286 |
if (status != eslEINVAL) esl_fatal(msg); |
| 1287 |
if (mu != 0.0) esl_fatal(msg); |
| 1288 |
|
| 1289 |
|
| 1290 |
status = esl_gumbel_FitTruncated(x, 1, 0.0, &mu, &lambda); |
| 1291 |
if (status != eslEINVAL) esl_fatal(msg); |
| 1292 |
if (mu != 0.0) esl_fatal(msg); |
| 1293 |
if (lambda != 0.0) esl_fatal(msg); |
| 1294 |
|
| 1295 |
|
| 1296 |
status = esl_gumbel_FitTruncated(x, 2, 0.0, &mu, &lambda); |
| 1297 |
if (status != eslENORESULT) esl_fatal(msg); |
| 1298 |
if (mu != 0.0) esl_fatal(msg); |
| 1299 |
if (lambda != 0.0) esl_fatal(msg); |
| 1300 |
|
| 1301 |
return; |
| 1302 |
} |
| 1303 |
#endif |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
#ifdef eslGUMBEL_TESTDRIVE |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
#include <stdio.h> |
| 1315 |
|
| 1316 |
#include "easel.h" |
| 1317 |
#include "esl_getopts.h" |
| 1318 |
#include "esl_random.h" |
| 1319 |
#include "esl_minimizer.h" |
| 1320 |
#include "esl_gumbel.h" |
| 1321 |
#include "esl_stats.h" |
| 1322 |
|
| 1323 |
static ESL_OPTIONS options[] = { |
| 1324 |
|
| 1325 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show help and usage", 0}, |
| 1326 |
{ "-s", eslARG_INT, "42", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0}, |
| 1327 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "verbose: show verbose output", 0}, |
| 1328 |
{ 0,0,0,0,0,0,0,0,0,0}, |
| 1329 |
}; |
| 1330 |
static char usage[] = "[-options]"; |
| 1331 |
static char banner[] = "test driver for Gumbel distribution routines"; |
| 1332 |
|
| 1333 |
int |
| 1334 |
main(int argc, char **argv) |
| 1335 |
{ |
| 1336 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 1337 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 1338 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 1339 |
|
| 1340 |
if (be_verbose) printf("seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 1341 |
|
| 1342 |
utest_fitting(rng); |
| 1343 |
utest_fit_failure(); |
| 1344 |
|
| 1345 |
esl_randomness_Destroy(rng); |
| 1346 |
esl_getopts_Destroy(go); |
| 1347 |
return 0; |
| 1348 |
} |
| 1349 |
#endif |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
#ifdef eslGUMBEL_EXAMPLE |
| 1356 |
|
| 1357 |
#include <stdio.h> |
| 1358 |
#include "easel.h" |
| 1359 |
#include "esl_random.h" |
| 1360 |
#include "esl_gumbel.h" |
| 1361 |
|
| 1362 |
int |
| 1363 |
main(int argc, char **argv) |
| 1364 |
{ |
| 1365 |
ESL_RANDOMNESS *r = esl_randomness_Create(0);; |
| 1366 |
int n = 10000; |
| 1367 |
double mu = -20.0; |
| 1368 |
double lambda = 0.4; |
| 1369 |
double min = 9999.; |
| 1370 |
double max = -9999.; |
| 1371 |
double *x = malloc(sizeof(double) * n); |
| 1372 |
double z, est_mu, est_lambda; |
| 1373 |
int i; |
| 1374 |
|
| 1375 |
for (i = 0; i < n; i++) |
| 1376 |
{ |
| 1377 |
x[i] = esl_gumbel_Sample(r, mu, lambda); |
| 1378 |
if (x[i] < min) min = x[i]; |
| 1379 |
if (x[i] > max) max = x[i]; |
| 1380 |
} |
| 1381 |
|
| 1382 |
z = esl_gumbel_surv(max, mu, lambda); |
| 1383 |
printf("max = %6.1f P(>max) = %g\n", max, z); |
| 1384 |
z = esl_gumbel_cdf(min, mu, lambda); |
| 1385 |
printf("min = %6.1f P(<=min) = %g\n", min, z); |
| 1386 |
|
| 1387 |
if (esl_gumbel_FitComplete(x, n, &est_mu, &est_lambda) != eslOK) |
| 1388 |
esl_fatal("gumbel ML complete data fit failed"); |
| 1389 |
|
| 1390 |
z = 100. * fabs((est_mu - mu) / mu); |
| 1391 |
printf("Parametric mu = %6.1f. Estimated mu = %6.2f. Difference = %.1f%%.\n", |
| 1392 |
mu, est_mu, z); |
| 1393 |
z = 100. * fabs((est_lambda - lambda) /lambda); |
| 1394 |
printf("Parametric lambda = %6.1f. Estimated lambda = %6.2f. Difference = %.1f%%.\n", |
| 1395 |
lambda, est_lambda, z); |
| 1396 |
|
| 1397 |
free(x); |
| 1398 |
esl_randomness_Destroy(r); |
| 1399 |
return 0; |
| 1400 |
} |
| 1401 |
|
| 1402 |
#endif |
| 1403 |
|
| 1404 |
|
| 1405 |
|