| 1 |
|
| #include "eff_ci.h" |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <math.h> |
| 4 |
|
| #include <eff_math_fun.h> |
| 5 |
|
| |
| 6 |
|
| /* |
| 7 |
|
| * The code in this file is based on the code in ROOT's |
| 8 |
|
| * TGraphAsymmErrors::BayesDivide and its auxilliary functions. |
| 9 |
|
| * The original method has this information on the authors: |
| 10 |
|
| * |
| 11 |
|
| * Andy Haas (haas@fnal.gov) |
| 12 |
|
| * University of Washington |
| 13 |
|
| * |
| 14 |
|
| * Method and code taken from: |
| 15 |
|
| * Marc Paterno (paterno@fnal.gov) |
| 16 |
|
| * FNAL/CD |
| 17 |
|
| * and modified for this use case. |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| double brent(pTHX_ double ax, double bx, double cx, double tol, double *xmin, int k, int N, double conflevel); |
| 21 |
|
| |
| 22 |
|
| double search_upper(pTHX_ double low, int k, int N, double c); |
| 23 |
|
| double search_lower(pTHX_ double high, int k, int N, double c); |
| 24 |
|
| |
| 25 |
|
| double interval(pTHX_ double low, int k, int N, double conflevel); |
| 26 |
|
| |
| 27 |
|
| |
| 28 |
|
| #define MYSIGN(a,b) ((b >= 0) ? fabs(a) : -fabs(a)) |
| 29 |
|
| |
| 30 |
|
| /* Based on Root's TGraphAsymmErrors::Efficiency function. That function |
| 31 |
|
| * lists the following information: |
| 32 |
|
| * |
| 33 |
|
| * Calculate the shortest central confidence interval containing the required |
| 34 |
|
| * probability content. |
| 35 |
|
| * interval(low) returns the length of the interval starting at low |
| 36 |
|
| * that contains conflevel probability. We use Brent's method, |
| 37 |
|
| * except in two special cases: when k=0, or when k=N |
| 38 |
|
| * Main driver routine |
| 39 |
|
| * Author: Marc Paterno |
| 40 |
|
| */ |
| 41 |
|
| void |
| 42 |
1 |
| efficiency_ci(pTHX_ int k, int N, double conflevel, double* mode, double* low, double* high) |
| 43 |
|
| { |
| 44 |
|
| /* If there are no entries, then we know nothing, thus return the prior... */ |
| 45 |
1 |
▶ | if (0==N) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (0==N) { |
0 |
1 |
1 |
|
| 46 |
0 |
| *mode = .5; *low = 0.0; *high = 1.0; |
| 47 |
0 |
| return; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| /* Calculate the most probable value for the posterior cross section. |
| 51 |
|
| * This is easy, 'cause it is just k/N |
| 52 |
|
| */ |
| 53 |
1 |
| *mode = (double)k/N; |
| 54 |
|
| |
| 55 |
1 |
▶ | if (k == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == 0) { |
0 |
1 |
1 |
|
| 56 |
0 |
| *low = 0.0; |
| 57 |
0 |
| *high = search_upper(aTHX_ *low, k, N, conflevel); |
| 58 |
1 |
▶ | } else if (k == N) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (k == N) { |
0 |
1 |
1 |
|
| 59 |
0 |
| *high = 1.0; |
| 60 |
0 |
| *low = search_lower(aTHX_ *high, k, N, conflevel); |
| 61 |
|
| } else { |
| 62 |
1 |
| brent(aTHX_ 0.0, 0.5, 1.0, 1.0e-9, low, k, N, conflevel); |
| 63 |
1 |
| *high = *low + interval(aTHX_ *low, k, N, conflevel); |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
1 |
| return; |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| |
| 70 |
|
| |
| 71 |
|
| double |
| 72 |
23 |
| search_upper(pTHX_ double low, int k, int N, double c) |
| 73 |
|
| { |
| 74 |
|
| int loop; |
| 75 |
|
| double integral, too_low, too_high, test; |
| 76 |
|
| |
| 77 |
|
| /* Integrates the binomial distribution with |
| 78 |
|
| * parameters k,N, and determines what is the upper edge of the |
| 79 |
|
| * integration region which starts at low which contains probability |
| 80 |
|
| * content c. If an upper limit is found, the value is returned. If no |
| 81 |
|
| * solution is found, -1 is returned. |
| 82 |
|
| * check to see if there is any solution by verifying that the integral up |
| 83 |
|
| * to the maximum upper limit (1) is greater than c |
| 84 |
|
| */ |
| 85 |
|
| |
| 86 |
23 |
| integral = beta_ab(aTHX_ low, 1.0, k, N); |
| 87 |
23 |
▶ | if (integral == c) return 1.0; /* lucky -- this is the solution */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral == c) return 1.0; /* lucky -- this is the solution */ |
0 |
23 |
23 |
|
| 88 |
23 |
▶ | if (integral < c) return -1.0; /* no solution exists */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral < c) return -1.0; /* no solution exists */ |
8 |
15 |
23 |
|
| 89 |
15 |
| too_high = 1.0; /* upper edge estimate */ |
| 90 |
15 |
| too_low = low; |
| 91 |
|
| |
| 92 |
|
| /* use a bracket-and-bisect search |
| 93 |
|
| * LM: looping 20 times might be not enough to get an accurate precision. |
| 94 |
|
| * see for example bug https://savannah.cern.ch/bugs/?30246 |
| 95 |
|
| * now break loop when difference is less than 1E-15 |
| 96 |
|
| * t.b.d: use directly the beta distribution quantile */ |
| 97 |
|
| |
| 98 |
756 |
▶ | for (loop=0; loop<50; loop++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (loop=0; loop<50; loop++) { |
748 |
8 |
756 |
|
| 99 |
748 |
| test = 0.5*(too_low + too_high); |
| 100 |
748 |
| integral = beta_ab(aTHX_ low, test, k, N); |
| 101 |
748 |
▶ | if (integral > c) too_high = test; |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral > c) too_high = test; |
370 |
378 |
748 |
|
| 102 |
378 |
| else too_low = test; |
| 103 |
748 |
▶ | if (fabs(integral - c) <= 1.E-15) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(integral - c) <= 1.E-15) break; |
7 |
741 |
748 |
|
| 104 |
|
| } |
| 105 |
15 |
| return test; |
| 106 |
|
| } |
| 107 |
|
| |
| 108 |
|
| double |
| 109 |
0 |
| search_lower(pTHX_ double high, int k, int N, double c) |
| 110 |
|
| { |
| 111 |
|
| int loop; |
| 112 |
|
| double integral, too_low, too_high, test; |
| 113 |
|
| |
| 114 |
|
| /* Integrates the binomial distribution with |
| 115 |
|
| * parameters k,N, and determines what is the lower edge of the |
| 116 |
|
| * integration region which ends at high, and which contains |
| 117 |
|
| * probability content c. If a lower limit is found, the value is |
| 118 |
|
| * returned. If no solution is found, the -1 is returned. |
| 119 |
|
| * check to see if there is any solution by verifying that the integral down |
| 120 |
|
| * to the minimum lower limit (0) is greater than c */ |
| 121 |
|
| |
| 122 |
0 |
| integral = beta_ab(aTHX_ 0.0, high, k, N); |
| 123 |
0 |
▶ | if (integral == c) return 0.0; /* lucky -- this is the solution */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral == c) return 0.0; /* lucky -- this is the solution */ |
0 |
0 |
0 |
|
| 124 |
0 |
▶ | if (integral < c) return -1.0; /* no solution exists */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral < c) return -1.0; /* no solution exists */ |
0 |
0 |
0 |
|
| 125 |
0 |
| too_low = 0.0; /* lower edge estimate */ |
| 126 |
0 |
| too_high = high; |
| 127 |
|
| |
| 128 |
|
| /* use a bracket-and-bisect search |
| 129 |
|
| * LM: looping 20 times might be not enough to get an accurate precision. |
| 130 |
|
| * see for example bug https://savannah.cern.ch/bugs/?30246 |
| 131 |
|
| * now break loop when difference is less than 1E-15 |
| 132 |
|
| * t.b.d: use directly the beta distribution quantile */ |
| 133 |
|
| |
| 134 |
0 |
▶ | for (loop=0; loop<50; loop++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (loop=0; loop<50; loop++) { |
0 |
0 |
0 |
|
| 135 |
0 |
| test = 0.5*(too_high + too_low); |
| 136 |
0 |
| integral = beta_ab(aTHX_ test, high, k, N); |
| 137 |
0 |
▶ | if (integral > c) too_low = test; |
Branch: 1 branch
| Branch | True | False | Total |
| if (integral > c) too_low = test; |
0 |
0 |
0 |
|
| 138 |
0 |
| else too_high = test; |
| 139 |
0 |
▶ | if (fabs(integral - c) <= 1.E-15) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(integral - c) <= 1.E-15) break; |
0 |
0 |
0 |
|
| 140 |
|
| } |
| 141 |
0 |
| return test; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| |
| 145 |
|
| double |
| 146 |
23 |
| interval(pTHX_ double low, int k, int N, double conflevel) |
| 147 |
|
| { |
| 148 |
|
| double high; |
| 149 |
|
| /* Return the length of the interval starting at low |
| 150 |
|
| * that contains conflevel of the x^k*(1-x)^(N-k) |
| 151 |
|
| * distribution. |
| 152 |
|
| * If there is no sufficient interval starting at low, we return 2.0 |
| 153 |
|
| */ |
| 154 |
|
| |
| 155 |
23 |
| high = search_upper(aTHX_ low, k, N, conflevel); |
| 156 |
23 |
▶ | if (high == -1.0) return 2.0; // so that this won't be the shortest interval |
Branch: 1 branch
| Branch | True | False | Total |
| if (high == -1.0) return 2.0; // so that this won't be the shortest interval |
8 |
15 |
23 |
|
| 157 |
15 |
| return (high - low); |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| |
| 161 |
|
| double |
| 162 |
1 |
| brent(pTHX_ double ax, double bx, double cx, double tol, double *xmin, int k, int N, double conflevel) |
| 163 |
|
| { |
| 164 |
|
| int iter; |
| 165 |
1 |
| double a,b,d=0.,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm; |
| 166 |
1 |
| double e=0.0; |
| 167 |
|
| |
| 168 |
1 |
| const int kITMAX = 100; |
| 169 |
1 |
| const double kCGOLD = 0.3819660; |
| 170 |
1 |
| const double kZEPS = 1.0e-10; |
| 171 |
|
| |
| 172 |
|
| /* Implementation file for the numerical equation solver library. |
| 173 |
|
| * This includes root finding and minimum finding algorithms. |
| 174 |
|
| * Adapted from Numerical Recipes in C, 2nd edition. |
| 175 |
|
| * Translated to C++ by Marc Paterno |
| 176 |
|
| * Translated back to C by Steffen Mueller (shame on him for |
| 177 |
|
| * not going back to the original NR sources...) |
| 178 |
|
| */ |
| 179 |
|
| |
| 180 |
1 |
▶ | a=(ax < cx ? ax : cx); |
Branch: 1 branch
| Branch | True | False | Total |
| a=(ax < cx ? ax : cx); |
1 |
0 |
1 |
|
| 181 |
1 |
▶ | b=(ax > cx ? ax : cx); |
Branch: 1 branch
| Branch | True | False | Total |
| b=(ax > cx ? ax : cx); |
0 |
1 |
1 |
|
| 182 |
1 |
| x=w=v=bx; |
| 183 |
1 |
| fw=fv=fx=interval(aTHX_ x, k, N, conflevel); |
| 184 |
22 |
▶ | for (iter=1;iter<=kITMAX;iter++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (iter=1;iter<=kITMAX;iter++) { |
22 |
0 |
22 |
|
| 185 |
22 |
| xm=0.5*(a+b); |
| 186 |
22 |
| tol2=2.0*(tol1=tol*fabs(x)+kZEPS); |
| 187 |
22 |
▶ | if (fabs(x-xm) <= (tol2-0.5*(b-a))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(x-xm) <= (tol2-0.5*(b-a))) { |
1 |
21 |
22 |
|
| 188 |
1 |
| *xmin=x; |
| 189 |
1 |
| return fx; |
| 190 |
|
| } |
| 191 |
21 |
▶ | if (fabs(e) > tol1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(e) > tol1) { |
19 |
2 |
21 |
|
| 192 |
19 |
| r=(x-w)*(fx-fv); |
| 193 |
19 |
| q=(x-v)*(fx-fw); |
| 194 |
19 |
| p=(x-v)*q-(x-w)*r; |
| 195 |
19 |
| q=2.0*(q-r); |
| 196 |
19 |
▶ | if (q > 0.0) p = -p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q > 0.0) p = -p; |
8 |
11 |
19 |
|
| 197 |
19 |
| q=fabs(q); |
| 198 |
19 |
| etemp=e; |
| 199 |
19 |
| e=d; |
| 200 |
19 |
▶ | if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
Branch: 4 branches
| Branch | True | False | Total |
| if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
11 |
8 |
19 |
| if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
11 |
0 |
11 |
| if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
1 |
10 |
11 |
| if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
8 |
1 |
9 |
|
| 201 |
|
| else { |
| 202 |
10 |
| d=p/q; |
| 203 |
10 |
| u=x+d; |
| 204 |
10 |
▶ | if (u-a < tol2 || b-u < tol2) d=MYSIGN(tol1,xm-x); |
Branch: 3 branches
| Branch | True | False | Total |
| if (u-a < tol2 || b-u < tol2) d=MYSIGN(tol1,xm-x); |
10 |
0 |
10 |
| if (u-a < tol2 || b-u < tol2) d=MYSIGN(tol1,xm-x); |
0 |
10 |
10 |
| if (u-a < tol2 || b-u < tol2) d=MYSIGN(tol1,xm-x); |
0 |
0 |
0 |
|
| 205 |
|
| } |
| 206 |
|
| } else { |
| 207 |
2 |
▶ | d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
Branch: 1 branch
| Branch | True | False | Total |
| d=kCGOLD*(e=(x >= xm ? a-x : b-x)); |
1 |
1 |
2 |
|
| 208 |
|
| } |
| 209 |
21 |
▶ | u=(fabs(d) >= tol1 ? x+d : x+MYSIGN(tol1,d)); |
Branch: 2 branches
| Branch | True | False | Total |
| u=(fabs(d) >= tol1 ? x+d : x+MYSIGN(tol1,d)); |
19 |
2 |
21 |
| u=(fabs(d) >= tol1 ? x+d : x+MYSIGN(tol1,d)); |
2 |
0 |
2 |
|
| 210 |
21 |
| fu=interval(aTHX_ u, k, N, conflevel); |
| 211 |
21 |
▶ | if (fu <= fx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fu <= fx) { |
18 |
3 |
21 |
|
| 212 |
18 |
▶ | if (u >= x) a=x; else b=x; |
Branch: 1 branch
| Branch | True | False | Total |
| if (u >= x) a=x; else b=x; |
6 |
12 |
18 |
|
| 213 |
18 |
| v = w; |
| 214 |
18 |
| w = x; |
| 215 |
18 |
| x = u; |
| 216 |
18 |
| fv = fw; |
| 217 |
18 |
| fw = fx; |
| 218 |
18 |
| fx = fu; |
| 219 |
|
| } else { |
| 220 |
3 |
▶ | if (u < x) a=u; else b=u; |
Branch: 1 branch
| Branch | True | False | Total |
| if (u < x) a=u; else b=u; |
0 |
3 |
3 |
|
| 221 |
3 |
▶ | if (fu <= fw || w == x) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fu <= fw || w == x) { |
3 |
0 |
3 |
| if (fu <= fw || w == x) { |
0 |
3 |
3 |
|
| 222 |
0 |
| v=w; |
| 223 |
0 |
| w=u; |
| 224 |
0 |
| fv=fw; |
| 225 |
0 |
| fw=fu; |
| 226 |
3 |
▶ | } else if (fu <= fv || v == x || v == w) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (fu <= fv || v == x || v == w) { |
1 |
2 |
3 |
| } else if (fu <= fv || v == x || v == w) { |
1 |
0 |
1 |
| } else if (fu <= fv || v == x || v == w) { |
0 |
1 |
1 |
|
| 227 |
2 |
| v=u; |
| 228 |
2 |
| fv=fu; |
| 229 |
|
| } |
| 230 |
|
| } |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| { |
| 234 |
0 |
| const char *err = "brent: Too many interations\n"; |
| 235 |
0 |
▶ | if (use_exceptions(aTHX)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (use_exceptions(aTHX)) |
0 |
0 |
0 |
|
| 236 |
0 |
| croak("%s", err); |
| 237 |
|
| else |
| 238 |
0 |
| warn("%s", err); |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
0 |
| *xmin=x; |
| 242 |
0 |
| return fx; |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| int |
| 246 |
0 |
| use_exceptions(pTHX) |
| 247 |
|
| { |
| 248 |
0 |
| SV *flag_sv = get_sv("Statistics::EfficiencyCI::Exceptions", 0); |
| 249 |
0 |
▶ | return( (flag_sv && SvTRUE(flag_sv)) ? 1 : 0 ); |
Branch: 2 branches
| Branch | True | False | Total |
| return( (flag_sv && SvTRUE(flag_sv)) ? 1 : 0 ); |
0 |
0 |
0 |
| return( (flag_sv && SvTRUE(flag_sv)) ? 1 : 0 ); |
0 |
0 |
0 |
|
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
|
| #undef MYSIGN |