-
Notifications
You must be signed in to change notification settings - Fork 26
/
esl_gumbel.c
1405 lines (1238 loc) · 44.1 KB
/
esl_gumbel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
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
311
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
339
340
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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
488
489
490
491
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
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
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Statistical routines for Gumbel (type I extreme value) distributions.
*
* Contents:
* 1. Routines for evaluating densities and distributions
* 2. Generic API routines: for general interface w/ histogram module
* 3. Dumping plots to files
* 4. Sampling
* 5. ML fitting to complete data
* 6. ML fitting to censored data (x_i >= phi; z known)
* 7. ML fitting to truncated data (x_i >= phi; z unknown)
* 8. Stats driver
* 9. Unit tests
* 10. Test driver
* 11. Example
*
* To-do:
* - ML fitting routines will be prone to over/underfitting
* problems for scores outside a "normal" range, because
* of exp(-lambda * x) calls. The Lawless ML estimation
* may eventually need to be recast in log space.
* SRE, Mon Aug 6 13:42:09 2007
*
*/
#include <esl_config.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include "easel.h"
#include "esl_minimizer.h"
#include "esl_random.h"
#include "esl_stats.h"
#include "esl_vectorops.h"
#include "esl_gumbel.h"
/*****************************************************************
* 1. Routines for evaluating densities and distributions
*****************************************************************/
/* Function: esl_gumbel_pdf()
* Synopsis: Returns the probability density at $x$, $P(S=x)$.
*
* Purpose: Calculates the probability density function for the Gumbel,
* $P(X=x)$, given quantile <x> and Gumbel location and
* scale parameters <mu> and <lambda>.
*
* Let $y = \lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range is about $-6.5 <= y <= 710$.
* Returns 0.0 for smaller $y$, 0.0 for larger $y$.
*/
double
esl_gumbel_pdf(double x, double mu, double lambda)
{
double y;
y = lambda * (x - mu);
return (lambda * exp(-y - exp(-y)));
}
/* Function: esl_gumbel_logpdf()
* Synopsis: Returns the log of the pdf at $x$, $\log P(S=x)$.
*
* Purpose: Calculates the log probability density function for the Gumbel,
* $\log P(X=x)$.
*
* Let $y = \lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range is about $-708 <= y <= \infty$.
* Returns $-\infty$ for smaller or larger $y$.
*/
double
esl_gumbel_logpdf(double x, double mu, double lambda)
{
double y;
y = lambda * (x - mu);
return (log(lambda) -y - exp(-y));
}
/* Function: esl_gumbel_cdf()
* Synopsis: Returns the cumulative distribution at $x$, $P(S \leq x)$.
*
* Purpose: Calculates the cumulative distribution function
* for the Gumbel, $P(X \leq x)$.
*
* Let $y = \lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range for $y$ is about $-6.5 <= y <=36$.
* Returns 0.0 for smaller $y$, 1.0 for larger $y$.
*/
double
esl_gumbel_cdf(double x, double mu, double lambda)
{
double y;
y = lambda*(x-mu);
return exp(-exp(-y));
}
/* Function: esl_gumbel_logcdf()
* Synopsis: Returns the log of the cdf at $x$, $\log P(S \leq x)$.
*
* Purpose: Calculates the log of the cumulative distribution function
* for the Gumbel, $\log P(X \leq x)$.
*
* Let $y = \lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range for $y$ is about $-708 <= y <= 708$.
* Returns $-\infty$ for smaller $y$, 0.0 for larger $y$.
*/
double
esl_gumbel_logcdf(double x, double mu, double lambda)
{
double y;
y = lambda*(x-mu);
return (-exp(-y));
}
/* Function: esl_gumbel_surv()
* Synopsis: Returns right tail mass above $x$, $P(S > x)$.
*
* Purpose: Calculates the survivor function, $P(X>x)$ for a Gumbel
* (that is, 1-cdf), the right tail's probability mass.
*
* Let $y=\lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range for $y$ is $-3.6 <= y <= 708$.
* Returns 1.0 for $y$ below lower limit, and 0.0
* for $y$ above upper limit.
*/
double
esl_gumbel_surv(double x, double mu, double lambda)
{
double y = lambda*(x-mu);
double ey = -exp(-y);
/* Use 1-e^x ~ -x approximation here when e^-y is small. */
if (fabs(ey) < eslSMALLX1) return -ey;
else return 1 - exp(ey);
}
/* Function: esl_gumbel_logsurv()
* Synopsis: Returns log survival at $x$, $\log P(S > x)$.
*
* Purpose: Calculates $\log P(X>x)$ for a Gumbel (that is, $\log$(1-cdf)):
* the log of the right tail's probability mass.
*
* Let $y=\lambda(x-\mu)$; for 64-bit doubles,
* useful dynamic range for $y$ is $-6.5 <= y <= \infty$.
* Returns 0.0 for smaller $y$.
*/
double
esl_gumbel_logsurv(double x, double mu, double lambda)
{
double y = lambda*(x-mu);
double ey = -exp(-y);
/* The real calculation is log(1-exp(-exp(-y))).
* For "large" y, -exp(-y) is small, so 1-exp(-exp(-y) ~ exp(-y),
* and log of that gives us -y.
* For "small y", exp(-exp(-y) is small, and we can use log(1-x) ~ -x.
*/
if (fabs(ey) < eslSMALLX1) return -y;
else if (fabs(exp(ey)) < eslSMALLX1) return -exp(ey);
else return log(1-exp(ey));
}
/* Function: esl_gumbel_invcdf()
*
* Purpose: Calculates the inverse CDF for a Gumbel distribution
* with parameters <mu> and <lambda>. That is, returns
* the quantile <x> at which the CDF is <p>.
*/
double
esl_gumbel_invcdf(double p, double mu, double lambda)
{
return mu - ( log(-1. * log(p)) / lambda);
}
/* Function: esl_gumbel_invsurv()
*
* Purpose: Calculates the score at which the right tail's mass
* is p, for a Gumbel distribution
* with parameters <mu> and <lambda>. That is, returns
* the quantile <x> at which 1-CDF is <p>.
*/
double
esl_gumbel_invsurv(double p, double mu, double lambda)
{
/* The real calculation is mu - ( log(-1. * log(1-p)) / lambda).
* But there's a problem with small p:
* for p<1e-15, 1-p will be viewed as 1, so
* log ( -log(1-p) ) == log (0) -> inf
* Instead, use two approximations;
* (1) log( 1-p) ~= -p for small p (e.g. p<0.001)
* so log(-1. * log(1-p)) ~= log(p)
* (2) log (p) ~= (p^p - 1) / p
*
* See notes Mar 1, 2010.
*/
double log_part;
if (p < eslSMALLX1) {
log_part = (pow(p,p) - 1 ) / p;
} else {
log_part = log(-1. * log(1-p));
}
//test 2
return mu - ( log_part / lambda);
}
/*------------------ end of densities and distributions --------------------*/
/*****************************************************************
* 2. Generic API routines: for general interface w/ histogram module
*****************************************************************/
/* Function: esl_gumbel_generic_pdf()
*
* Purpose: Generic-API version of PDF function.
*/
double
esl_gumbel_generic_pdf(double p, void *params)
{
double *v = (double *) params;
return esl_gumbel_pdf(p, v[0], v[1]);
}
/* Function: esl_gumbel_generic_cdf()
*
* Purpose: Generic-API version of CDF function.
*/
double
esl_gumbel_generic_cdf(double x, void *params)
{
double *p = (double *) params;
return esl_gumbel_cdf(x, p[0], p[1]);
}
/* Function: esl_gumbel_generic_surv()
*
* Purpose: Generic-API version of survival function.
*/
double
esl_gumbel_generic_surv(double p, void *params)
{
double *v = (double *) params;
return esl_gumbel_surv(p, v[0], v[1]);
}
/* Function: esl_gumbel_generic_invcdf()
*
* Purpose: Generic-API version of inverse CDF.
*/
double
esl_gumbel_generic_invcdf(double p, void *params)
{
double *v = (double *) params;
return esl_gumbel_invcdf(p, v[0], v[1]);
}
/*------------------------- end of generic API --------------------------*/
/****************************************************************************
* 3. Routines for dumping plots for files
****************************************************************************/
/* Function: esl_gumbel_Plot()
* Synopsis: Plot a Gumbel function in XMGRACE XY format.
*
* Purpose: Plot a Gumbel function <func> (for instance,
* <esl_gumbel_pdf()>) for parameters <mu> and <lambda>, for
* a range of quantiles x from <xmin> to <xmax> in steps of <xstep>;
* output to an open stream <fp> in xmgrace XY input format.
*
* Returns: <eslOK> on success.
*
* Throws: <eslEWRITE> on any system write error, such as filled disk.
*/
int
esl_gumbel_Plot(FILE *fp, double mu, double lambda,
double (*func)(double x, double mu, double lambda),
double xmin, double xmax, double xstep)
{
double x;
for (x = xmin; x <= xmax; x += xstep)
if (fprintf(fp, "%f\t%g\n", x, (*func)(x, mu, lambda)) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "gumbel plot write failed");
if (fprintf(fp, "&\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "gumbel plot write failed");
return eslOK;
}
/*-------------------- end plot dumping routines ---------------------------*/
/*****************************************************************
* 4. Routines for sampling
*****************************************************************/
/* Function: esl_gumbel_Sample()
* Synopsis: Return a Gumbel-distributed random sample $x$.
*
* Purpose: Sample a Gumbel-distributed random variate
* by the transformation method.
*/
double
esl_gumbel_Sample(ESL_RANDOMNESS *r, double mu, double lambda)
{
double p;
p = esl_rnd_UniformPositive(r);
return esl_gumbel_invcdf(p, mu, lambda);
}
/*------------------------ end of sampling --------------------------------*/
/*****************************************************************
* 5. Maximum likelihood fitting to complete data
*****************************************************************/
/* lawless416()
* SRE, Thu Nov 13 11:48:50 1997 [St. Louis]
*
* Purpose: Equation 4.1.6 from [Lawless82], pg. 143, and
* its first derivative with respect to lambda,
* for finding the ML fit to Gumbel lambda parameter.
* This equation gives a result of zero for the maximum
* likelihood lambda.
*
* Args: x - array of sample values
* n - number of samples
* lambda - a lambda to test
* ret_f - RETURN: 4.1.6 evaluated at lambda
* ret_df - RETURN: first derivative of 4.1.6 evaluated at lambda
*
* Return: (void)
*/
static void
lawless416(double *x, int n, double lambda, double *ret_f, double *ret_df)
{
double esum; /* \sum e^(-lambda xi) */
double xesum; /* \sum xi e^(-lambda xi) */
double xxesum; /* \sum xi^2 e^(-lambda xi) */
double xsum; /* \sum xi */
int i;
esum = xesum = xsum = xxesum = 0.;
for (i = 0; i < n; i++)
{
xsum += x[i];
xesum += x[i] * exp(-1. * lambda * x[i]);
xxesum += x[i] * x[i] * exp(-1. * lambda * x[i]);
esum += exp(-1. * lambda * x[i]);
}
*ret_f = (1./lambda) - (xsum / n) + (xesum / esum);
*ret_df = ((xesum / esum) * (xesum / esum))
- (xxesum / esum)
- (1. / (lambda * lambda));
}
/* Function: esl_gumbel_FitComplete()
* Synopsis: Estimates $\mu$, $\lambda$ from complete data.
*
* Purpose: Given an array of Gumbel-distributed samples
* <x[0]..x[n-1]>, find maximum likelihood parameters <mu>
* and <lambda>.
*
* The number of samples <n> must be reasonably large to get
* an accurate fit. <n=100> suffices to get an accurate
* location parameter $\mu$ (to about 1% error), but
* <n~10000> is required to get a similarly accurate
* estimate of $\lambda$. It's probably a bad idea to try to
* fit a Gumbel to less than about 1000 data points.
*
* On a very small number of samples, the fit can fail
* altogether, in which case the routine will return a
* <eslENORESULT> code. Caller must check for this.
*
* Uses approach described in [Lawless82]. Solves for lambda
* using Newton/Raphson iterations, then substitutes lambda
* into Lawless' equation 4.1.5 to get mu.
*
* Args: x - list of Gumbel distributed samples
* n - number of samples (n>1)
* ret_mu - RETURN: ML estimate of mu
* ret_lambda - RETURN: ML estimate of lambda
*
* Returns: <eslOK> on success.
*
* <eslEINVAL> if n<=1.
* <eslENORESULT> if the fit fails, likely because the
* number of samples is too small. On either error,
* <*ret_mu> and <*ret_lambda> are 0.0. These are classed
* as failures (normal errors) because the data vector may
* have been provided by a user.
*/
int
esl_gumbel_FitComplete(double *x, int n, double *ret_mu, double *ret_lambda)
{
double variance;
double lambda, mu;
double fx; /* f(x) */
double dfx; /* f'(x) */
double esum; /* \sum e^(-lambda xi) */
double tol = 1e-5;
int i;
int status;
if (n <= 1) { status = eslEINVAL; goto FAILURE; }
/* 1. Find an initial guess at lambda
* (Evans/Hastings/Peacock, Statistical Distributions, 2000, p.86)
*/
esl_stats_DMean(x, n, NULL, &variance);
lambda = eslCONST_PI / sqrt(6.*variance);
/* 2. Use Newton/Raphson to solve Lawless 4.1.6 and find ML lambda
*/
for (i = 0; i < 100; i++)
{
lawless416(x, n, lambda, &fx, &dfx);
if (fabs(fx) < tol) break; /* success */
lambda = lambda - fx / dfx; /* Newton/Raphson is simple */
if (lambda <= 0.) lambda = 0.001; /* but be a little careful */
}
/* 2.5: If we did 100 iterations but didn't converge, Newton/Raphson failed.
* Resort to a bisection search. Worse convergence speed
* but guaranteed to converge (unlike Newton/Raphson).
* We assume that fx is a monotonically decreasing function of x;
* i.e. fx > 0 if we are left of the root, fx < 0 if we
* are right of the root.
*/
if (i == 100)
{
double left, right, mid;
ESL_DPRINTF2(("esl_gumbel_FitComplete(): Newton/Raphson failed; switchover to bisection\n"));
/* First bracket the root */
left = 0.; /* for sure */
right = eslCONST_PI / sqrt(6.*variance); /* an initial guess */
lawless416(x, n, lambda, &fx, &dfx);
while (fx > 0.)
{
right *= 2.; /* arbitrary leap to the right */
if (right > 1000.) /* no reasonable lambda should be > 1000, we assert */
{
ESL_DPRINTF2(("Failed to bracket root in esl_gumbel_FitComplete()."));
status = eslENORESULT;
goto FAILURE;
}
lawless416(x, n, right, &fx, &dfx);
}
/* Now, bisection search in left/right interval */
for (i = 0; i < 100; i++)
{
mid = (left + right) / 2.;
lawless416(x, n, mid, &fx, &dfx);
if (fabs(fx) < tol) break; /* success */
if (fx > 0.) left = mid;
else right = mid;
}
/* Too many iterations? Give up. */
if (i == 100)
{
ESL_DPRINTF2(("Even bisection search failed in esl_gumbel_FitComplete().\n"));
status = eslENORESULT;
goto FAILURE;
}
lambda = mid;
}
/* 3. Substitute into Lawless 4.1.5 to find mu
*/
esum = 0.;
for (i = 0; i < n; i++)
esum += exp(-lambda * x[i]);
mu = -log(esum / n) / lambda;
*ret_lambda = lambda;
*ret_mu = mu;
return eslOK;
FAILURE:
*ret_mu = 0.0;
*ret_lambda = 0.0;
return status;
}
/* Function: esl_gumbel_FitCompleteLoc()
* Synopsis: Estimates $\mu$ from complete data, given $\lambda$.
*
* Purpose: Given an array of Gumbel-distributed samples
* <x[0]..x[n-1]> (complete data), and a known
* (or otherwise fixed) <lambda>, find a maximum
* likelihood estimate for location parameter <mu>.
*
* Algorithm is a straightforward simplification of
* <esl_gumbel_FitComplete()>.
*
* Args: x - list of Gumbel distributed samples
* n - number of samples
* lambda - known lambda (scale) parameter
* ret_mu : RETURN: ML estimate of mu
*
* Returns: <eslOK> on success.
*
* <eslEINVAL> if n<=1; on this error, <*ret_mu> = 0.
*
* Throws: (no abnormal error conditions)
*/
int
esl_gumbel_FitCompleteLoc(double *x, int n, double lambda, double *ret_mu)
{
double esum;
int i;
int status;
if (n <= 1) { status = eslEINVAL; goto FAILURE; }
/* Substitute into Lawless 4.1.5 to find mu */
esum = 0.;
for (i = 0; i < n; i++)
esum += exp(-lambda * x[i]);
*ret_mu = -log(esum / n) / lambda;
return eslOK;
#if 0
/* Replace the code above w/ code below to test the direct method. */
double mean, variance;
esl_stats_DMean(x, n, &mean, &variance);
*ret_mu = mean - 0.57722/lambda;
return eslOK;
#endif
FAILURE:
*ret_mu = 0.;
return status;
}
#if 0
/* direct_mv_fit()
* SRE, Wed Jun 29 08:23:47 2005
*
* Purely for curiousity: a complete data fit using the
* simple direct method, calculating mu and lambda from mean
* and variance.
*/
static int
direct_mv_fit(double *x, int n, double *ret_mu, double *ret_lambda)
{
double mean, variance;
esl_stats_DMean(x, n, &mean, &variance);
*ret_lambda = eslCONST_PI / sqrt(6.*variance);
*ret_mu = mean - 0.57722/(*ret_lambda);
return eslOK;
}
#endif
/*------------------- end of complete data fit ---------------------------------*/
/*****************************************************************
* 6. Maximum likelihood fitting to censored data (x_i >= phi; z known)
*****************************************************************/
/* lawless422()
* SRE, Mon Nov 17 09:42:48 1997 [St. Louis]
*
* Purpose: Equation 4.2.2 from [Lawless82], pg. 169, and
* its first derivative with respect to lambda,
* for finding the ML fit to Gumbel lambda parameter
* for Type I censored data.
* This equation gives a result of zero for the maximum
* likelihood lambda.
*
* Args: x - array of observed sample values
* n - number of observed samples
* z - number of censored samples = N-n
* phi - censoring value; all observed x_i >= phi
* lambda - a lambda to test
* ret_f - RETURN: 4.2.2 evaluated at lambda
* ret_df - RETURN: first derivative of 4.2.2 evaluated at lambda
*
* Return: (void)
*/
static void
lawless422(double *x, int n, int z, double phi,
double lambda, double *ret_f, double *ret_df)
{
double esum; /* \sum e^(-lambda xi) + z term */
double xesum; /* \sum xi e^(-lambda xi) + z term */
double xxesum; /* \sum xi^2 e^(-lambda xi) + z term */
double xsum; /* \sum xi (no z term) */
int i;
esum = xesum = xsum = xxesum = 0.;
for (i = 0; i < n; i++)
{
xsum += x[i];
esum += exp(-1. * lambda * x[i]);
xesum += x[i] * exp(-1. * lambda * x[i]);
xxesum += x[i] * x[i] * exp(-1. * lambda * x[i]);
}
/* Add z terms for censored data
*/
esum += (double) z * exp(-1. * lambda * phi);
xesum += (double) z * phi * exp(-1. * lambda * phi);
xxesum += (double) z * phi * phi * exp(-1. * lambda * phi);
*ret_f = 1./lambda - xsum / n + xesum / esum;
*ret_df = ((xesum / esum) * (xesum / esum))
- (xxesum / esum)
- (1. / (lambda * lambda));
return;
}
/* Function: esl_gumbel_FitCensored()
* Synopsis: Estimates $\mu$, $\lambda$ from censored data.
*
* Purpose: Given a left-censored array of Gumbel-distributed samples
* <x[0]..x[n-1]>, the number of censored samples <z>, and
* the censoring value <phi> (all <x[i]> $\geq$ <phi>). Find
* maximum likelihood parameters <mu> and <lambda>.
*
* Algorithm: Uses approach described in [Lawless82]. Solves
* for lambda using Newton/Raphson iterations;
* then substitutes lambda into Lawless' equation 4.2.3
* to get mu.
*
* Args: x - array of Gumbel-distributed samples, 0..n-1
* n - number of observed samples
* z - number of censored samples
* phi - censoring value (all x_i >= phi)
* ret_mu - RETURN: ML estimate of mu
* ret_lambda - RETURN: ML estimate of lambda
*
* Returns: <eslOK> on success.
*
* <eslEINVAL> if n<=1.
* <eslENORESULT> if the fit fails, likey because the number
* of samples is too small.
* On either error, <*ret_mu> and <*ret_lambda> are 0.0.
* These are classed as failures (normal errors) because the
* data vector may have been provided by a user.
*/
int
esl_gumbel_FitCensored(double *x, int n, int z, double phi, double *ret_mu, double *ret_lambda)
{
double variance;
double lambda, mu;
double fx; /* f(x) */
double dfx; /* f'(x) */
double esum; /* \sum e^(-lambda xi) */
double tol = 1e-5;
int i;
int status;
if (n <= 1) { status = eslEINVAL; goto FAILURE; }
/* 1. Find an initial guess at lambda
* (Evans/Hastings/Peacock, Statistical Distributions, 2000, p.86)
*/
esl_stats_DMean(x, n, NULL, &variance);
lambda = eslCONST_PI / sqrt(6.*variance);
/* 2. Use Newton/Raphson to solve Lawless 4.2.2 and find ML lambda
*/
for (i = 0; i < 100; i++)
{
lawless422(x, n, z, phi, lambda, &fx, &dfx);
if (fabs(fx) < tol) break; /* success */
lambda = lambda - fx / dfx; /* Newton/Raphson is simple */
if (lambda <= 0.) lambda = 0.001; /* but be a little careful */
}
/* 2.5: If we did 100 iterations but didn't converge, Newton/Raphson failed.
* Resort to a bisection search. Worse convergence speed
* but guaranteed to converge (unlike Newton/Raphson).
* We assume (!?) that fx is a monotonically decreasing function of x;
* i.e. fx > 0 if we are left of the root, fx < 0 if we
* are right of the root.
*/
if (i == 100)
{
double left, right, mid;
ESL_DPRINTF2(("esl_gumbel_FitCensored(): Newton/Raphson failed; switched to bisection\n"));
/* First bracket the root */
left = 0.; /* we know that's the left bound */
right = eslCONST_PI / sqrt(6.*variance); /* start from here, move "right"... */
lawless422(x, n, z, phi, right, &fx, &dfx);
while (fx > 0.)
{
right *= 2.;
if (right > 1000.) /* no reasonable lambda should be > 1000, we assert */
{
ESL_DPRINTF2(("Failed to bracket root in esl_gumbel_FitCensored()."));
status = eslENORESULT;
goto FAILURE;
}
lawless422(x, n, z, phi, right, &fx, &dfx);
}
/* Now we bisection search in left/right interval */
for (i = 0; i < 100; i++)
{
mid = (left + right) / 2.;
lawless422(x, n, z, phi, mid, &fx, &dfx);
if (fabs(fx) < tol) break; /* success */
if (fx > 0.) left = mid;
else right = mid;
}
if (i == 100)
{
ESL_DPRINTF2(("Even bisection search failed in esl_gumbel_FitCensored().\n"));
status = eslENORESULT;
goto FAILURE;
}
lambda = mid;
}
/* 3. Substitute into Lawless 4.2.3 to find mu
*/
esum = 0.;
for (i = 0; i < n; i++)
esum += exp(-lambda * x[i]);
esum += z * exp(-1. * lambda * phi); /* term from censored data */
mu = -log(esum / n) / lambda;
*ret_lambda = lambda;
*ret_mu = mu;
return eslOK;
FAILURE:
*ret_lambda = 0.0;
*ret_mu = 0.0;
return status;
}
/* Function: esl_gumbel_FitCensoredLoc()
* Synopsis: Estimates $\mu$ from censored data, given $\lambda$.
*
* Purpose: Given a left-censored array of Gumbel distributed samples
* <x[0>..x[n-1]>, the number of censored samples <z>, and the censoring
* value <phi> (where all <x[i]> $\geq$ <phi>), and a known
* (or at least fixed) <lambda>;
* find the maximum likelihood estimate of the location
* parameter $\mu$ and return it in <ret_mu>.
*
* Note: A straightforward simplification of FitCensored().
*
* Args: x - array of Gumbel-distributed samples, 0..n-1
* n - number of observed samples
* z - number of censored samples
* phi - censoring value (all x_i >= phi)
* lambda - known scale parameter $\lambda$
* ret_mu - RETURN: ML estimate of $\mu$
*
* Returns: <eslOK> on success.
*
* <eslEINVAL> if n<=1; on this error, <*ret_mu> = 0.
*
* Throws: (no abnormal error conditions)
*/
int
esl_gumbel_FitCensoredLoc(double *x, int n, int z, double phi, double lambda,
double *ret_mu)
{
double esum;
int i;
int status;
if (n <= 1) { status = eslEINVAL; goto FAILURE; }
/* Immediately substitute into Lawless 4.2.3 to find mu, because
* lambda is known.
*/
esum = 0.;
for (i = 0; i < n; i++) /* contribution from observed data */
esum += exp(-lambda * x[i]);
esum += z * exp(-1. * lambda * phi); /* term from censored data */
*ret_mu = -log(esum / (double) n) / lambda;
return eslOK;
FAILURE:
*ret_mu = 0.;
return status;
}
/*****************************************************************
* 7. Maximum likelihood fitting to truncated data (x_i >= phi and z unknown)
*****************************************************************/
/* Easel's conjugate gradient descent code allows a single void ptr to
* point to any necessary fixed data, so we'll put everything into one
* structure:
*/
struct tevd_data {
double *x; /* data: n observed samples from a Gumbel */
int n; /* number of observed samples */
double phi; /* truncation threshold: all observed x_i >= phi */
};
/* tevd_func()
*
* Called by the optimizer: evaluate the objective function
* for the negative posterior log probability of a particular choice
* of parameters mu and lambda, given truncated Gumbel samples.
*/
static double
tevd_func(double *p, int nparam, void *dptr)
{
double mu, w, lambda;
struct tevd_data *data;
double *x;
int n;
double phi;
double logL;
int i;
/* unpack what the optimizer gave us; nparam==2 always
*/
mu = p[0];
w = p[1];
lambda = exp(w);
data = (struct tevd_data *) dptr;
x = data->x;
n = data->n;
phi = data->phi;
/* The log likelihood equation
*/
logL = n * log(lambda);
for (i = 0; i < n; i++)
logL -= lambda * (x[i] - mu);
for (i = 0; i < n; i++)
logL -= exp(-1. * lambda * (x[i] - mu));
logL -= n * esl_gumbel_logsurv(phi, mu, lambda);
return -1.0 * logL; /* objective: minimize the NLP */
}
/* tevd_grad()
*
* Called by the optimizer: evaluate the gradient of the objective
* function (the negative posterior log probability of the parameters
* mu and w, where w = log(lambda), at a particular choice of mu and
* lambda.
*/
static void
tevd_grad(double *p, int nparam, void *dptr, double *dp)
{
double mu, lambda, w;
struct tevd_data *data;
double *x;
int n;
double phi;
double dmu, dw;
double coeff;
int i;
/* unpack what the optimizer gave us; nparam==2 always
*/
mu = p[0];
w = p[1];
lambda = exp(w);
data = (struct tevd_data *) dptr;
x = data->x;
n = data->n;
phi = data->phi;
/* Both partials include a coefficient that
* basically looks like P(S=phi) / P(S>=phi); pre-calculate it.
* Watch out when phi >> mu, which'll give us 0/0; instead,
* recognize that for phi >> mu, coeff converges to \lambda.
*/
if (lambda*(phi-mu) > 50.) /* arbitrary crossover. */
coeff = lambda;
else
coeff = esl_gumbel_pdf(phi, mu, lambda) / esl_gumbel_surv(phi, mu, lambda);
/* Partial derivative w.r.t. mu.
*/
dmu = n * lambda;
for (i = 0; i < n; i++)
dmu -= lambda * exp(-1. * lambda * (x[i] - mu));
dmu -= n * coeff;
/* Partial derivative w.r.t. w=log(lambda).
*/
dw = n;
for (i = 0; i < n; i++) dw -= (x[i] - mu) * lambda;
for (i = 0; i < n; i++) dw += (x[i] - mu) * lambda * exp(-1. * lambda * (x[i] - mu));
dw += n * (phi - mu) * coeff;
/* Return the negative, because we're minimizing NLP, not maximizing.
*/
dp[0] = -1. * dmu; /* negative because we're minimizing NLP, not maximizing */
dp[1] = -1. * dw;
return;
}
/* Function: esl_gumbel_FitTruncated()
* Synopsis: Estimates $\mu$, $\lambda$ from truncated data.
*
* Purpose: Given a left-truncated array of Gumbel-distributed
* samples <x[0]..x[n-1]> and the truncation threshold
* <phi> (such that all <x[i]> $\geq$ <phi>).
* Find maximum likelihood parameters <mu> and <lambda>.
*
* <phi> should not be much greater than <mu>, the
* mode of the Gumbel, or the fit will become unstable
* or may even fail to converge. The problem is
* that for <phi> $>$ <mu>, the tail of the Gumbel
* becomes a scale-free exponential, and <mu> becomes
* undetermined.
*
* Algorithm: Uses conjugate gradient descent to optimize the
* log likelihood of the data. Follows a general
* approach to fitting missing data problems outlined
* in [Gelman95].
*
* Args: x - observed data samples [0..n-1]
* n - number of samples
* phi - truncation threshold; all x[i] >= phi
* ret_mu - RETURN: ML estimate of mu
* ret_lambda - RETURN: ML estimate of lambda
*
* Returns: <eslOK> on success.
*
* <eslEINVAL> if n<=1.
* <eslENORESULT> if the fit fails, likely because the
* number of samples <n> is too small, or because the
* truncation threshold is high enough that the tail
* looks like a scale-free exponential and we can't
* obtain <mu>.
* On either error, <*ret_mu> and <*ret_lambda> are
* returned as 0.0.
* These are "normal" (returned) errors because
* the data might be provided directly by a user.
*
* Throws: <eslEMEM> on allocation error.
*/
int
esl_gumbel_FitTruncated(double *x, int n, double phi, double *ret_mu, double *ret_lambda)
{
ESL_MIN_CFG *cfg = NULL; /* customization of the CG optimizer */
struct tevd_data data;
double p[2]; /* mu, w; lambda = e^w */
double mean, variance;
double mu, lambda;
double fx;
int i;
int status;
/* customization of the optimizer */
if ((cfg = esl_min_cfg_Create(2)) == NULL) { status = eslEMEM; goto ERROR; }
cfg->u[0] = 2.0;
cfg->u[1] = 0.1;
cfg->cg_rtol = 1e-4;
/* Can't fit to n<=1 */
if (n <= 1) { status = eslEINVAL; goto ERROR; }
/* Can fail on small <n>. One way is if x_i are all identical, so
* ML lambda is undefined.
*/
for (i = 1; i < n; i++) if (x[i] != x[0]) break;
if (i == n) { status = eslENORESULT; goto ERROR; }
data.x = x;
data.n = n;
data.phi = phi;
/* The source of the following magic is Evans/Hastings/Peacock,
* Statistical Distributions, 3rd edition (2000), p.86, which gives
* eq's for the mean and variance of a Gumbel in terms of mu and lambda;
* we turn them around to get mu and lambda in terms of the mean and variance.
* These would be reasonable estimators if we had a full set of Gumbel
* distributed variates. They'll be off for a truncated sample, but
* close enough to be a useful starting point.
*/
esl_stats_DMean(x, n, &mean, &variance);
lambda = eslCONST_PI / sqrt(6.*variance);
mu = mean - 0.57722/lambda;
p[0] = mu;
p[1] = log(lambda); /* c.o.v. because lambda is constrained to >0 */