| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <esl_config.h> |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <math.h> |
| 22 |
|
| 23 |
#include "easel.h" |
| 24 |
#include "esl_histogram.h" |
| 25 |
#include "esl_minimizer.h" |
| 26 |
#include "esl_random.h" |
| 27 |
#include "esl_stats.h" |
| 28 |
#include "esl_vectorops.h" |
| 29 |
|
| 30 |
#include "esl_weibull.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
double |
| 52 |
esl_wei_pdf(double x, double mu, double lambda, double tau) |
| 53 |
{ |
| 54 |
double y = lambda * (x-mu); |
| 55 |
double val; |
| 56 |
|
| 57 |
if (x < mu) return 0.; |
| 58 |
if (x == mu) { |
| 59 |
if (tau < 1.) return eslINFINITY; |
| 60 |
else if (tau > 1.) return 0.; |
| 61 |
else if (tau == 1.) return lambda; |
| 62 |
} |
| 63 |
|
| 64 |
val = lambda * tau * |
| 65 |
exp((tau-1)*log(y)) * |
| 66 |
exp(- exp(tau * log(y))); |
| 67 |
return val; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
double |
| 77 |
esl_wei_logpdf(double x, double mu, double lambda, double tau) |
| 78 |
{ |
| 79 |
double y = lambda * (x-mu); |
| 80 |
double val; |
| 81 |
|
| 82 |
if (x < mu) return -eslINFINITY; |
| 83 |
if (x == mu) { |
| 84 |
if (tau < 1.) return eslINFINITY; |
| 85 |
else if (tau > 1.) return -eslINFINITY; |
| 86 |
else if (tau == 1.) return log(lambda); |
| 87 |
} |
| 88 |
|
| 89 |
val = log(tau) + tau*log(lambda) + (tau-1)*log(x-mu) - exp(tau * log(y)); |
| 90 |
return val; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
double |
| 100 |
esl_wei_cdf(double x, double mu, double lambda, double tau) |
| 101 |
{ |
| 102 |
double y = lambda*(x-mu); |
| 103 |
double tly = tau * log(y); |
| 104 |
|
| 105 |
if (x <= mu) return 0.0; |
| 106 |
else if (fabs(tly) < eslSMALLX1) return exp(tly); |
| 107 |
else return 1 - exp(-exp(tly)); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
double |
| 117 |
esl_wei_logcdf(double x, double mu, double lambda, double tau) |
| 118 |
{ |
| 119 |
double y = lambda*(x-mu); |
| 120 |
double tly = tau * log(y); |
| 121 |
|
| 122 |
if (x <= mu) return -eslINFINITY; |
| 123 |
|
| 124 |
if (fabs(tly) < eslSMALLX1) return tly; |
| 125 |
else if (fabs(exp(-exp(tly))) < eslSMALLX1) return -exp(-exp(tly)); |
| 126 |
else return log(1 - exp(-exp(tly))); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
double |
| 138 |
esl_wei_surv(double x, double mu, double lambda, double tau) |
| 139 |
{ |
| 140 |
double y = lambda*(x-mu); |
| 141 |
double tly = tau * log(y); |
| 142 |
|
| 143 |
if (x <= mu) return 1.0; |
| 144 |
|
| 145 |
return exp(-exp(tly)); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
double |
| 156 |
esl_wei_logsurv(double x, double mu, double lambda, double tau) |
| 157 |
{ |
| 158 |
double y = lambda*(x-mu); |
| 159 |
double tly = tau * log(y); |
| 160 |
|
| 161 |
if (x <= mu) return 0.0; |
| 162 |
|
| 163 |
return -exp(tly); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
double |
| 173 |
esl_wei_invcdf(double p, double mu, double lambda, double tau) |
| 174 |
{ |
| 175 |
return mu + 1/lambda * exp(1/tau * log(-log((1.-p)))); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
double |
| 193 |
esl_wei_generic_pdf(double x, void *params) |
| 194 |
{ |
| 195 |
double *p = (double *) params; |
| 196 |
return esl_wei_pdf(x, p[0], p[1], p[2]); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
double |
| 206 |
esl_wei_generic_cdf(double x, void *params) |
| 207 |
{ |
| 208 |
double *p = (double *) params; |
| 209 |
return esl_wei_cdf(x, p[0], p[1], p[2]); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
double |
| 219 |
esl_wei_generic_surv(double x, void *params) |
| 220 |
{ |
| 221 |
double *p = (double *) params; |
| 222 |
return esl_wei_surv(x, p[0], p[1], p[2]); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
double |
| 232 |
esl_wei_generic_invcdf(double p, void *params) |
| 233 |
{ |
| 234 |
double *v = (double *) params; |
| 235 |
return esl_wei_invcdf(p, v[0], v[1], v[2]); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
int |
| 257 |
esl_wei_Plot(FILE *fp, double mu, double lambda, double tau, |
| 258 |
double (*func)(double x, double mu, double lambda, double tau), |
| 259 |
double xmin, double xmax, double xstep) |
| 260 |
{ |
| 261 |
double x; |
| 262 |
for (x = xmin; x <= xmax; x += xstep) |
| 263 |
if (x > mu || tau >= 1.) |
| 264 |
if (fprintf(fp, "%f\t%g\n", x, (*func)(x, mu, lambda, tau)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "weibull plot write failed"); |
| 265 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "weibull plot write failed"); |
| 266 |
return eslOK; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
double |
| 284 |
esl_wei_Sample(ESL_RANDOMNESS *r, double mu, double lambda, double tau) |
| 285 |
{ |
| 286 |
double p; |
| 287 |
p = esl_rnd_UniformPositive(r); |
| 288 |
return esl_wei_invcdf(p, mu, lambda, tau); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
struct wei_data { |
| 303 |
double *x; |
| 304 |
int n; |
| 305 |
double mu; |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static double |
| 313 |
wei_func(double *p, int nparam, void *dptr) |
| 314 |
{ |
| 315 |
double lambda, tau; |
| 316 |
struct wei_data *data; |
| 317 |
double logL; |
| 318 |
int i; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
lambda = exp(p[0]); |
| 323 |
tau = exp(p[1]); |
| 324 |
data = (struct wei_data *) dptr; |
| 325 |
|
| 326 |
logL = 0.; |
| 327 |
for (i = 0; i < data->n; i++) |
| 328 |
{ |
| 329 |
if (tau < 1. && data->x[i] == data->mu) continue; |
| 330 |
logL += esl_wei_logpdf(data->x[i], data->mu, lambda, tau); |
| 331 |
} |
| 332 |
return -logL; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
int |
| 356 |
esl_wei_FitComplete(double *x, int n, double *ret_mu, |
| 357 |
double *ret_lambda, double *ret_tau) |
| 358 |
{ |
| 359 |
struct wei_data data; |
| 360 |
double p[2]; |
| 361 |
double mean; |
| 362 |
double mu, lambda, tau; |
| 363 |
double fx; |
| 364 |
int status; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
mu = esl_vec_DMin(x, n); |
| 370 |
esl_stats_DMean(x, n, &mean, NULL); |
| 371 |
lambda = 1 / (mean - mu); |
| 372 |
tau = 0.9; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
data.x = x; |
| 377 |
data.n = n; |
| 378 |
data.mu = mu; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
p[0] = log(lambda); |
| 385 |
p[1] = log(tau); |
| 386 |
|
| 387 |
|
| 388 |
status = esl_min_ConjugateGradientDescent(NULL, p, 2, |
| 389 |
&wei_func, NULL, |
| 390 |
(void *)(&data), &fx, NULL); |
| 391 |
*ret_mu = mu; |
| 392 |
*ret_lambda = exp(p[0]); |
| 393 |
*ret_tau = exp(p[1]); |
| 394 |
return status; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
struct wei_binned_data { |
| 404 |
ESL_HISTOGRAM *h; |
| 405 |
double mu; |
| 406 |
}; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static double |
| 413 |
wei_binned_func(double *p, int nparam, void *dptr) |
| 414 |
{ |
| 415 |
struct wei_binned_data *data = (struct wei_binned_data *) dptr; |
| 416 |
ESL_HISTOGRAM *h = data->h; |
| 417 |
double lambda, tau; |
| 418 |
double logL; |
| 419 |
double ai,bi; |
| 420 |
int i; |
| 421 |
double tmp; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
lambda = exp(p[0]); |
| 426 |
tau = exp(p[1]); |
| 427 |
|
| 428 |
logL = 0.; |
| 429 |
for (i = h->cmin; i <= h->imax; i++) |
| 430 |
{ |
| 431 |
if (h->obs[i] == 0) continue; |
| 432 |
|
| 433 |
ai = esl_histogram_Bin2LBound(h,i); |
| 434 |
bi = esl_histogram_Bin2UBound(h,i); |
| 435 |
if (ai < data->mu) ai = data->mu; |
| 436 |
|
| 437 |
tmp = esl_wei_cdf(bi, data->mu, lambda, tau) - |
| 438 |
esl_wei_cdf(ai, data->mu, lambda, tau); |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
ESL_DASSERT1( (tmp + 1e-7 > 0.)); |
| 443 |
if (tmp <= 0.) return eslINFINITY; |
| 444 |
|
| 445 |
logL += h->obs[i] * log(tmp); |
| 446 |
} |
| 447 |
return -logL; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
int |
| 472 |
esl_wei_FitCompleteBinned(ESL_HISTOGRAM *h, double *ret_mu, |
| 473 |
double *ret_lambda, double *ret_tau) |
| 474 |
{ |
| 475 |
struct wei_binned_data data; |
| 476 |
double p[2]; |
| 477 |
double mean; |
| 478 |
double mu, lambda, tau; |
| 479 |
double fx; |
| 480 |
int i; |
| 481 |
double ai; |
| 482 |
int status; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
if (h->is_tailfit) mu = h->phi; |
| 489 |
else if (h->is_rounded) mu = esl_histogram_Bin2LBound(h, h->imin); |
| 490 |
else mu = h->xmin; |
| 491 |
|
| 492 |
mean = 0.; |
| 493 |
for (i = h->cmin; i <= h->imax; i++) |
| 494 |
{ |
| 495 |
ai = esl_histogram_Bin2LBound(h, i); |
| 496 |
ai += 0.5*h->w; |
| 497 |
mean += (double)h->obs[i] * ai; |
| 498 |
} |
| 499 |
mean /= h->No; |
| 500 |
lambda = 1 / (mean - mu); |
| 501 |
|
| 502 |
tau = 0.9; |
| 503 |
|
| 504 |
|
| 505 |
data.h = h; |
| 506 |
data.mu = mu; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
p[0] = log(lambda); |
| 513 |
p[1] = log(tau); |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
status = esl_min_ConjugateGradientDescent(NULL, p, 2, |
| 518 |
&wei_binned_func, NULL, |
| 519 |
(void *)(&data), &fx, NULL); |
| 520 |
*ret_mu = mu; |
| 521 |
*ret_lambda = exp(p[0]); |
| 522 |
*ret_tau = exp(p[1]); |
| 523 |
return status; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
#ifdef eslWEIBULL_TESTDRIVE |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
#include <stdio.h> |
| 540 |
#include <stdlib.h> |
| 541 |
#include <string.h> |
| 542 |
|
| 543 |
#include "easel.h" |
| 544 |
#include "esl_getopts.h" |
| 545 |
#include "esl_random.h" |
| 546 |
#include "esl_histogram.h" |
| 547 |
#include "esl_weibull.h" |
| 548 |
|
| 549 |
static ESL_OPTIONS options[] = { |
| 550 |
|
| 551 |
{ "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 }, |
| 552 |
{ "-l", eslARG_REAL, "1.0", NULL, NULL, NULL, NULL, NULL, "set slope of sampled variates (lambda parameter) to <x> ", 0 }, |
| 553 |
{ "-m", eslARG_REAL, "10.0", NULL, NULL, NULL, NULL, NULL, "set location of sampled variates (mu parameter) to <x>", 0 }, |
| 554 |
{ "-n", eslARG_INT, "10000", NULL, NULL, NULL, NULL, NULL, "set # of sampled variates to <n>", 0 }, |
| 555 |
{ "-o", eslARG_OUTFILE, NULL, NULL, NULL, NULL, NULL, NULL, "output histogram to file <f>", 0 }, |
| 556 |
{ "-s", eslARG_INT, "0", NULL, NULL, NULL, NULL, NULL, "set random number seed to <n>", 0 }, |
| 557 |
{ "-t", eslARG_REAL, "0.7", NULL, NULL, NULL, NULL, NULL, "set shape of sampled variates (tau parameter) to <x>", 0 }, |
| 558 |
{ "-v", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "be more verbose in output", 0 }, |
| 559 |
{ "-w", eslARG_REAL, "0.1", NULL, NULL, NULL, NULL, NULL, "set width of histogram bins to <x>", 0 }, |
| 560 |
{ "--cdf", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of cumulative distribution", 0 }, |
| 561 |
{ "--logcdf", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of log cumulative distribution", 0 }, |
| 562 |
{ "--pdf", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of probability density", 0 }, |
| 563 |
{ "--logpdf", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of log probability density", 0 }, |
| 564 |
{ "--surv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of survival P(s>x)", 0 }, |
| 565 |
{ "--logsurv", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "dump plot of log survival, log(P(s>x))", 0 }, |
| 566 |
{ "--xL", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "set minimum x-axis value on dumped plots to <x>", 0 }, |
| 567 |
{ "--xH", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "set maximum x-axis value on dumped plots to <x>", 0 }, |
| 568 |
{ "--xS", eslARG_REAL, NULL, NULL, NULL, NULL, NULL, NULL, "set x-axis increment value on dumped plots to <x>", 0 }, |
| 569 |
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 570 |
}; |
| 571 |
static char usage[] = "[-options]"; |
| 572 |
static char banner[] = "test driver for Easel's Weibull distribution module"; |
| 573 |
|
| 574 |
int |
| 575 |
main(int argc, char **argv) |
| 576 |
{ |
| 577 |
ESL_GETOPTS *go = esl_getopts_CreateDefaultApp(options, 0, argc, argv, banner, usage); |
| 578 |
ESL_RANDOMNESS *rng = esl_randomness_Create(esl_opt_GetInteger(go, "-s")); |
| 579 |
double mu = esl_opt_GetReal (go, "-m"); |
| 580 |
double lambda = esl_opt_GetReal (go, "-l"); |
| 581 |
double tau = esl_opt_GetReal (go, "-t"); |
| 582 |
int n = esl_opt_GetInteger(go, "-n"); |
| 583 |
double binwidth = esl_opt_GetReal (go, "-w"); |
| 584 |
int plot_cdf = esl_opt_GetBoolean(go, "--cdf"); |
| 585 |
int plot_logcdf = esl_opt_GetBoolean(go, "--logcdf"); |
| 586 |
int plot_pdf = esl_opt_GetBoolean(go, "--pdf"); |
| 587 |
int plot_logpdf = esl_opt_GetBoolean(go, "--logpdf"); |
| 588 |
int plot_surv = esl_opt_GetBoolean(go, "--surv"); |
| 589 |
int plot_logsurv = esl_opt_GetBoolean(go, "--logsurv"); |
| 590 |
int be_verbose = esl_opt_GetBoolean(go, "-v"); |
| 591 |
char *plotfile = esl_opt_GetString (go, "-o"); |
| 592 |
ESL_HISTOGRAM *h = NULL; |
| 593 |
int xmin_set = esl_opt_IsOn(go, "--xL"); |
| 594 |
double xmin = xmin_set ? esl_opt_GetReal(go, "--xL") : mu; |
| 595 |
int xmax_set = esl_opt_IsOn(go, "--xH"); |
| 596 |
double xmax = xmax_set ? esl_opt_GetReal(go, "--xH") : mu+40*(1./lambda); |
| 597 |
int xstep_set = esl_opt_IsOn(go, "--xH"); |
| 598 |
double xstep = xstep_set ? esl_opt_GetReal(go, "--xS") : 0.1; |
| 599 |
FILE *pfp = stdout; |
| 600 |
double emu, elambda, etau; |
| 601 |
int i; |
| 602 |
double x; |
| 603 |
double *data; |
| 604 |
int ndata; |
| 605 |
|
| 606 |
fprintf(stderr, "## %s\n", argv[0]); |
| 607 |
fprintf(stderr, "# rng seed = %" PRIu32 "\n", esl_randomness_GetSeed(rng)); |
| 608 |
|
| 609 |
if (be_verbose) printf("Parametric: mu = %f lambda = %f tau = %f\n", mu, lambda, tau); |
| 610 |
|
| 611 |
h = esl_histogram_CreateFull(mu, 100., binwidth); |
| 612 |
if (plotfile && (pfp = fopen(plotfile, "w")) == NULL) ESL_EXCEPTION(eslFAIL, "Failed to open plotfile"); |
| 613 |
|
| 614 |
for (i = 0; i < n; i++) |
| 615 |
{ |
| 616 |
x = esl_wei_Sample(rng, mu, lambda, tau); |
| 617 |
esl_histogram_Add(h, x); |
| 618 |
} |
| 619 |
esl_histogram_GetData(h, &data, &ndata); |
| 620 |
|
| 621 |
esl_wei_FitComplete(data, ndata, &emu, &elambda, &etau); |
| 622 |
if (be_verbose) printf("Complete data fit: mu = %f lambda = %f tau = %f\n", emu, elambda, etau); |
| 623 |
if (fabs( (emu-mu)/mu ) > 0.01) ESL_EXCEPTION(eslFAIL, "Error in (complete) fitted mu > 1%\n"); |
| 624 |
if (fabs( (elambda-lambda)/lambda ) > 0.10) ESL_EXCEPTION(eslFAIL, "Error in (complete) fitted lambda > 10%\n"); |
| 625 |
if (fabs( (etau-tau)/tau ) > 0.10) ESL_EXCEPTION(eslFAIL, "Error in (complete) fitted tau > 10%\n"); |
| 626 |
|
| 627 |
esl_wei_FitCompleteBinned(h, &emu, &elambda, &etau); |
| 628 |
if (be_verbose) printf("Binned data fit: mu = %f lambda = %f tau = %f\n", emu, elambda, etau); |
| 629 |
if (fabs( (emu-mu)/mu ) > 0.01) ESL_EXCEPTION(eslFAIL, "Error in (binned) fitted mu > 1%\n"); |
| 630 |
if (fabs( (elambda-lambda)/lambda ) > 0.10) ESL_EXCEPTION(eslFAIL, "Error in (binned) fitted lambda > 10%\n"); |
| 631 |
if (fabs( (etau-tau)/tau ) > 0.10) ESL_EXCEPTION(eslFAIL, "Error in (binned) fitted lambda > 10%\n"); |
| 632 |
|
| 633 |
if (plot_pdf) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_pdf, xmin, xmax, xstep); |
| 634 |
if (plot_logpdf) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_logpdf, xmin, xmax, xstep); |
| 635 |
if (plot_cdf) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_cdf, xmin, xmax, xstep); |
| 636 |
if (plot_logcdf) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_logcdf, xmin, xmax, xstep); |
| 637 |
if (plot_surv) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_surv, xmin, xmax, xstep); |
| 638 |
if (plot_logsurv) esl_wei_Plot(pfp, mu, lambda, tau, &esl_wei_logsurv, xmin, xmax, xstep); |
| 639 |
|
| 640 |
if (plotfile) fclose(pfp); |
| 641 |
esl_histogram_Destroy(h); |
| 642 |
esl_randomness_Destroy(rng); |
| 643 |
esl_getopts_Destroy(go); |
| 644 |
|
| 645 |
fprintf(stderr, "# status = ok\n"); |
| 646 |
return 0; |
| 647 |
} |
| 648 |
#endif |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
#ifdef eslWEIBULL_EXAMPLE |
| 654 |
|
| 655 |
#include <stdio.h> |
| 656 |
#include "easel.h" |
| 657 |
#include "esl_random.h" |
| 658 |
#include "esl_histogram.h" |
| 659 |
#include "esl_weibull.h" |
| 660 |
|
| 661 |
int |
| 662 |
main(int argc, char **argv) |
| 663 |
{ |
| 664 |
double mu = -2.1; |
| 665 |
double lambda = 1.0; |
| 666 |
double tau = 0.8; |
| 667 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(mu, 100., 0.1); |
| 668 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 669 |
int n = 10000; |
| 670 |
double emu, elambda, etau; |
| 671 |
double *data; |
| 672 |
int ndata; |
| 673 |
double x; |
| 674 |
int i; |
| 675 |
|
| 676 |
for (i = 0; i < n; i++) |
| 677 |
{ |
| 678 |
x = esl_wei_Sample(r, mu, lambda, tau); |
| 679 |
esl_histogram_Add(h, x); |
| 680 |
} |
| 681 |
esl_histogram_GetData(h, &data, &ndata); |
| 682 |
|
| 683 |
|
| 684 |
esl_histogram_PlotSurvival(stdout, h); |
| 685 |
esl_wei_Plot(stdout, mu, lambda, tau, |
| 686 |
&esl_wei_surv, h->xmin, h->xmax, 0.1); |
| 687 |
|
| 688 |
|
| 689 |
esl_wei_FitComplete(data, ndata, &emu, &elambda, &etau); |
| 690 |
esl_wei_Plot(stdout, emu, elambda, etau, |
| 691 |
&esl_wei_surv, h->xmin, h->xmax, 0.1); |
| 692 |
|
| 693 |
|
| 694 |
esl_wei_FitCompleteBinned(h, &emu, &elambda, &etau); |
| 695 |
esl_wei_Plot(stdout, emu, elambda, etau, |
| 696 |
&esl_wei_surv, h->xmin, h->xmax, 0.1); |
| 697 |
|
| 698 |
esl_randomness_Destroy(r); |
| 699 |
esl_histogram_Destroy(h); |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
#endif |
| 704 |
|
| 705 |
|
| 706 |
|