| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <esl_config.h> |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
#include "easel.h" |
| 27 |
#include "esl_histogram.h" |
| 28 |
#include "esl_random.h" |
| 29 |
#include "esl_stats.h" |
| 30 |
|
| 31 |
#include "esl_exponential.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
double |
| 55 |
esl_exp_pdf(double x, double mu, double lambda) |
| 56 |
{ |
| 57 |
if (x < mu) return 0.; |
| 58 |
return (lambda * exp(-lambda*(x-mu))); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
double |
| 68 |
esl_exp_logpdf(double x, double mu, double lambda) |
| 69 |
{ |
| 70 |
if (x < mu) return -eslINFINITY; |
| 71 |
|
| 72 |
if (lambda == eslINFINITY) |
| 73 |
{ |
| 74 |
if (x == mu) return eslINFINITY; |
| 75 |
else return -eslINFINITY; |
| 76 |
} |
| 77 |
return (log(lambda) - lambda*(x-mu)); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
double |
| 87 |
esl_exp_cdf(double x, double mu, double lambda) |
| 88 |
{ |
| 89 |
double y = lambda*(x-mu); |
| 90 |
|
| 91 |
if (x < mu) return 0.; |
| 92 |
|
| 93 |
|
| 94 |
if (y < eslSMALLX1) return y; |
| 95 |
else return 1 - exp(-y); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
double |
| 105 |
esl_exp_logcdf(double x, double mu, double lambda) |
| 106 |
{ |
| 107 |
double y = lambda * (x-mu); |
| 108 |
double ey = exp(-y); |
| 109 |
|
| 110 |
if (x < mu) return -eslINFINITY; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
if (y == 0) return -eslINFINITY; |
| 116 |
else if (y < eslSMALLX1) return log(y); |
| 117 |
else if (ey < eslSMALLX1) return -ey; |
| 118 |
else return log(1-ey); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
double |
| 128 |
esl_exp_surv(double x, double mu, double lambda) |
| 129 |
{ |
| 130 |
if (x < mu) return 1.0; |
| 131 |
return exp(-lambda * (x-mu)); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
double |
| 142 |
esl_exp_logsurv(double x, double mu, double lambda) |
| 143 |
{ |
| 144 |
if (x < mu) return 0.0; |
| 145 |
return -lambda * (x-mu); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
double |
| 156 |
esl_exp_invcdf(double p, double mu, double lambda) |
| 157 |
{ |
| 158 |
return mu - 1/lambda * log(1. - p); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
double |
| 170 |
esl_exp_invsurv(double p, double mu, double lambda) |
| 171 |
{ |
| 172 |
|
| 173 |
return mu - 1./lambda * log(p); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
double |
| 192 |
esl_exp_generic_pdf(double x, void *params) |
| 193 |
{ |
| 194 |
double *p = (double *) params; |
| 195 |
return esl_exp_pdf(x, p[0], p[1]); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
double |
| 203 |
esl_exp_generic_cdf(double x, void *params) |
| 204 |
{ |
| 205 |
double *p = (double *) params; |
| 206 |
return esl_exp_cdf(x, p[0], p[1]); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
double |
| 214 |
esl_exp_generic_surv(double x, void *params) |
| 215 |
{ |
| 216 |
double *p = (double *) params; |
| 217 |
return esl_exp_surv(x, p[0], p[1]); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
double |
| 225 |
esl_exp_generic_invcdf(double p, void *params) |
| 226 |
{ |
| 227 |
double *v = (double *) params; |
| 228 |
return esl_exp_invcdf(p, v[0], v[1]); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
int |
| 250 |
esl_exp_Plot(FILE *fp, double mu, double lambda, |
| 251 |
double (*func)(double x, double mu, double lambda), |
| 252 |
double xmin, double xmax, double xstep) |
| 253 |
{ |
| 254 |
double x; |
| 255 |
for (x = xmin; x <= xmax; x += xstep) |
| 256 |
if (fprintf(fp, "%f\t%g\n", x, (*func)(x, mu, lambda)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "exponential plot write failed"); |
| 257 |
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "exponential plot write failed"); |
| 258 |
return eslOK; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
double |
| 275 |
esl_exp_Sample(ESL_RANDOMNESS *r, double mu, double lambda) |
| 276 |
{ |
| 277 |
double p, x; |
| 278 |
p = esl_rnd_UniformPositive(r); |
| 279 |
|
| 280 |
x = mu - 1./lambda * log(p); |
| 281 |
|
| 282 |
return x; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
int |
| 311 |
esl_exp_FitComplete(double *x, int n, double *ret_mu, double *ret_lambda) |
| 312 |
{ |
| 313 |
double mu, mean; |
| 314 |
int i; |
| 315 |
int status; |
| 316 |
|
| 317 |
if (!n) ESL_XEXCEPTION(eslEINVAL, "empty data vector provided for exponential fit"); |
| 318 |
|
| 319 |
|
| 320 |
mu = x[0]; |
| 321 |
for (i = 1; i < n; i++) if (x[i] < mu) mu = x[i]; |
| 322 |
|
| 323 |
mean = 0.; |
| 324 |
for (i = 0; i < n; i++) mean += x[i] - mu; |
| 325 |
mean /= (double) n; |
| 326 |
|
| 327 |
*ret_mu = mu; |
| 328 |
*ret_lambda = 1./mean; |
| 329 |
return eslOK; |
| 330 |
|
| 331 |
ERROR: |
| 332 |
*ret_mu = 0.0; |
| 333 |
*ret_lambda = 0.0; |
| 334 |
return status; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
int |
| 356 |
esl_exp_FitCompleteScale(double *x, int n, double mu, double *ret_lambda) |
| 357 |
{ |
| 358 |
double mean; |
| 359 |
int i; |
| 360 |
|
| 361 |
mean = 0.; |
| 362 |
for (i = 0; i < n; i++) mean += x[i] - mu; |
| 363 |
mean /= (double) n; |
| 364 |
|
| 365 |
*ret_lambda = 1./mean; |
| 366 |
return eslOK; |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
int |
| 413 |
esl_exp_FitCompleteBinned(ESL_HISTOGRAM *g, double *ret_mu, double *ret_lambda) |
| 414 |
{ |
| 415 |
int i; |
| 416 |
double ai, bi, delta; |
| 417 |
double sa, sb; |
| 418 |
double mu = 0.; |
| 419 |
|
| 420 |
if (g->dataset_is == COMPLETE) |
| 421 |
{ |
| 422 |
if (g->is_rounded) mu = esl_histogram_Bin2LBound(g, g->imin); |
| 423 |
else mu = g->xmin; |
| 424 |
} |
| 425 |
else if (g->dataset_is == VIRTUAL_CENSORED) |
| 426 |
mu = g->phi; |
| 427 |
else if (g->dataset_is == TRUE_CENSORED) |
| 428 |
ESL_EXCEPTION(eslEINVAL, "can't fit true censored dataset"); |
| 429 |
|
| 430 |
delta = g->w; |
| 431 |
sa = sb = 0.; |
| 432 |
for (i = g->cmin; i <= g->imax; i++) |
| 433 |
{ |
| 434 |
if (g->obs[i] == 0) continue; |
| 435 |
ai = esl_histogram_Bin2LBound(g,i); |
| 436 |
bi = esl_histogram_Bin2UBound(g,i); |
| 437 |
sa += g->obs[i] * (ai-mu); |
| 438 |
sb += g->obs[i] * (bi-mu); |
| 439 |
} |
| 440 |
*ret_mu = mu; |
| 441 |
*ret_lambda = 1/delta * (log(sb) - log(sa)); |
| 442 |
return eslOK; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
#ifdef eslEXPONENTIAL_STATS |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
#include <stdio.h> |
| 461 |
|
| 462 |
#include "easel.h" |
| 463 |
#include "esl_getopts.h" |
| 464 |
#include "esl_random.h" |
| 465 |
#include "esl_exponential.h" |
| 466 |
|
| 467 |
int |
| 468 |
main(int argc, char **argv) |
| 469 |
{ |
| 470 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 471 |
int ntrials; |
| 472 |
int N; |
| 473 |
double mu, lambda; |
| 474 |
double est_mu, est_lambda; |
| 475 |
int trial; |
| 476 |
int i; |
| 477 |
double *x; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
ntrials = 1000; |
| 482 |
mu = 0.; |
| 483 |
lambda = 0.693; |
| 484 |
N = 95; |
| 485 |
|
| 486 |
x = malloc(sizeof(double) *N); |
| 487 |
for (trial = 0; trial < ntrials; trial++) |
| 488 |
{ |
| 489 |
for (i = 0; i < N; i++) |
| 490 |
x[i] = esl_exp_Sample(r, mu, lambda); |
| 491 |
esl_exp_FitComplete(x, N, &est_mu, &est_lambda); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
printf("%4d %8.4f %8.4f\n", i, est_mu, est_lambda); |
| 498 |
} |
| 499 |
free(x); |
| 500 |
return 0; |
| 501 |
} |
| 502 |
#endif |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
#ifdef eslEXPONENTIAL_TESTDRIVE |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
#include <stdio.h> |
| 520 |
#include <stdlib.h> |
| 521 |
#include <string.h> |
| 522 |
|
| 523 |
#include "easel.h" |
| 524 |
#include "esl_random.h" |
| 525 |
#include "esl_histogram.h" |
| 526 |
#include "esl_exponential.h" |
| 527 |
|
| 528 |
int |
| 529 |
main(int argc, char **argv) |
| 530 |
{ |
| 531 |
ESL_HISTOGRAM *h; |
| 532 |
ESL_RANDOMNESS *r; |
| 533 |
double mu = 10.0; |
| 534 |
double lambda = 1.0; |
| 535 |
int n = 10000; |
| 536 |
double binwidth = 0.1; |
| 537 |
double emu, elambda; |
| 538 |
int i; |
| 539 |
double x; |
| 540 |
double *data; |
| 541 |
int ndata; |
| 542 |
|
| 543 |
int opti; |
| 544 |
int be_verbose = FALSE; |
| 545 |
char *plotfile = NULL; |
| 546 |
FILE *pfp = stdout; |
| 547 |
int plot_pdf = FALSE; |
| 548 |
int plot_logpdf = FALSE; |
| 549 |
int plot_cdf = FALSE; |
| 550 |
int plot_logcdf = FALSE; |
| 551 |
int plot_surv = FALSE; |
| 552 |
int plot_logsurv = FALSE; |
| 553 |
int xmin_set = FALSE; |
| 554 |
double xmin; |
| 555 |
int xmax_set = FALSE; |
| 556 |
double xmax; |
| 557 |
int xstep_set = FALSE; |
| 558 |
double xstep; |
| 559 |
|
| 560 |
for (opti = 1; opti < argc && *(argv[opti]) == '-'; opti++) |
| 561 |
{ |
| 562 |
if (strcmp(argv[opti], "-m") == 0) mu = atof(argv[++opti]); |
| 563 |
else if (strcmp(argv[opti], "-l") == 0) lambda = atof(argv[++opti]); |
| 564 |
else if (strcmp(argv[opti], "-n") == 0) n = atoi(argv[++opti]); |
| 565 |
else if (strcmp(argv[opti], "-o") == 0) plotfile = argv[++opti]; |
| 566 |
else if (strcmp(argv[opti], "-v") == 0) be_verbose = TRUE; |
| 567 |
else if (strcmp(argv[opti], "-w") == 0) binwidth = atof(argv[++opti]); |
| 568 |
else if (strcmp(argv[opti], "-C") == 0) plot_cdf = TRUE; |
| 569 |
else if (strcmp(argv[opti], "-LC") == 0) plot_logcdf = TRUE; |
| 570 |
else if (strcmp(argv[opti], "-P") == 0) plot_pdf = TRUE; |
| 571 |
else if (strcmp(argv[opti], "-LP") == 0) plot_logpdf = TRUE; |
| 572 |
else if (strcmp(argv[opti], "-S") == 0) plot_surv = TRUE; |
| 573 |
else if (strcmp(argv[opti], "-LS") == 0) plot_logsurv = TRUE; |
| 574 |
else if (strcmp(argv[opti], "-XL") == 0) { xmin_set = TRUE; xmin = atof(argv[++opti]); } |
| 575 |
else if (strcmp(argv[opti], "-XH") == 0) { xmax_set = TRUE; xmax = atof(argv[++opti]); } |
| 576 |
else if (strcmp(argv[opti], "-XS") == 0) { xstep_set = TRUE; xstep = atof(argv[++opti]); } |
| 577 |
else esl_fatal("bad option"); |
| 578 |
} |
| 579 |
|
| 580 |
if (be_verbose) |
| 581 |
printf("Parametric: mu = %f lambda = %f\n", mu, lambda); |
| 582 |
|
| 583 |
r = esl_randomness_Create(0); |
| 584 |
h = esl_histogram_CreateFull(mu, 100., binwidth); |
| 585 |
if (plotfile != NULL) { |
| 586 |
if ((pfp = fopen(plotfile, "w")) == NULL) esl_fatal("Failed to open plotfile"); |
| 587 |
} |
| 588 |
if (! xmin_set) xmin = mu; |
| 589 |
if (! xmax_set) xmax = mu+20* (1./lambda); |
| 590 |
if (! xstep_set) xstep = 0.1; |
| 591 |
|
| 592 |
for (i = 0; i < n; i++) |
| 593 |
{ |
| 594 |
x = esl_exp_Sample(r, mu, lambda); |
| 595 |
esl_histogram_Add(h, x); |
| 596 |
} |
| 597 |
esl_histogram_GetData(h, &data, &ndata); |
| 598 |
|
| 599 |
esl_exp_FitComplete(data, ndata, &emu, &elambda); |
| 600 |
if (be_verbose) printf("Complete data fit: mu = %f lambda = %f\n", emu, elambda); |
| 601 |
if (fabs( (emu-mu)/mu ) > 0.01) esl_fatal("Error in (complete) fitted mu > 1%\n"); |
| 602 |
if (fabs( (elambda-lambda)/lambda ) > 0.10) esl_fatal("Error in (complete) fitted lambda > 10%\n"); |
| 603 |
|
| 604 |
esl_exp_FitCompleteBinned(h, &emu, &elambda); |
| 605 |
if (be_verbose) printf("Binned data fit: mu = %f lambda = %f\n", emu, elambda); |
| 606 |
if (fabs( (emu-mu)/mu ) > 0.01) esl_fatal("Error in (binned) fitted mu > 1%\n"); |
| 607 |
if (fabs( (elambda-lambda)/lambda ) > 0.10) esl_fatal("Error in (binned) fitted lambda > 10%\n"); |
| 608 |
|
| 609 |
if (plot_pdf) esl_exp_Plot(pfp, mu, lambda, &esl_exp_pdf, xmin, xmax, xstep); |
| 610 |
if (plot_logpdf) esl_exp_Plot(pfp, mu, lambda, &esl_exp_logpdf, xmin, xmax, xstep); |
| 611 |
if (plot_cdf) esl_exp_Plot(pfp, mu, lambda, &esl_exp_cdf, xmin, xmax, xstep); |
| 612 |
if (plot_logcdf) esl_exp_Plot(pfp, mu, lambda, &esl_exp_logcdf, xmin, xmax, xstep); |
| 613 |
if (plot_surv) esl_exp_Plot(pfp, mu, lambda, &esl_exp_surv, xmin, xmax, xstep); |
| 614 |
if (plot_logsurv) esl_exp_Plot(pfp, mu, lambda, &esl_exp_logsurv, xmin, xmax, xstep); |
| 615 |
|
| 616 |
if (plotfile != NULL) fclose(pfp); |
| 617 |
|
| 618 |
esl_randomness_Destroy(r); |
| 619 |
esl_histogram_Destroy(h); |
| 620 |
return 0; |
| 621 |
} |
| 622 |
#endif |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
#ifdef eslEXPONENTIAL_EXAMPLE |
| 629 |
|
| 630 |
#include <stdio.h> |
| 631 |
#include "easel.h" |
| 632 |
#include "esl_random.h" |
| 633 |
#include "esl_histogram.h" |
| 634 |
#include "esl_exponential.h" |
| 635 |
|
| 636 |
int |
| 637 |
main(int argc, char **argv) |
| 638 |
{ |
| 639 |
double mu = -50.0; |
| 640 |
double lambda = 0.5; |
| 641 |
ESL_RANDOMNESS *r = esl_randomness_Create(0); |
| 642 |
ESL_HISTOGRAM *h = esl_histogram_CreateFull(mu, 100., 0.1); |
| 643 |
int n = 10000; |
| 644 |
double emu, elambda; |
| 645 |
int i; |
| 646 |
double x; |
| 647 |
double *data; |
| 648 |
int ndata; |
| 649 |
|
| 650 |
for (i = 0; i < n; i++) |
| 651 |
{ |
| 652 |
x = esl_exp_Sample(r, mu, lambda); |
| 653 |
esl_histogram_Add(h, x); |
| 654 |
} |
| 655 |
esl_histogram_GetData(h, &data, &ndata); |
| 656 |
|
| 657 |
|
| 658 |
esl_histogram_PlotSurvival(stdout, h); |
| 659 |
esl_exp_Plot(stdout, mu, lambda, |
| 660 |
&esl_exp_surv, h->xmin, h->xmax, 0.1); |
| 661 |
|
| 662 |
|
| 663 |
esl_exp_FitComplete(data, ndata, &emu, &elambda); |
| 664 |
esl_exp_Plot(stdout, emu, elambda, |
| 665 |
&esl_exp_surv, h->xmin, h->xmax, 0.1); |
| 666 |
|
| 667 |
|
| 668 |
esl_exp_FitCompleteBinned(h, &emu, &elambda); |
| 669 |
esl_exp_Plot(stdout, emu, elambda, |
| 670 |
&esl_exp_surv, h->xmin, h->xmax, 0.1); |
| 671 |
|
| 672 |
esl_randomness_Destroy(r); |
| 673 |
esl_histogram_Destroy(h); |
| 674 |
return 0; |
| 675 |
} |
| 676 |
|
| 677 |
#endif |
| 678 |
|