_stats.c

filter: Statementstmt 78.9% 229 / 290 Branchbran 65.0% 138 / 212 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.1% 367 / 502 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Statistics/CaseResampling.pm ↑ Summary RdGen.xs.inc »
1 #include "stats.h"
2 #include <math.h>
3  
4 #ifndef M_PI
5 # define M_PI 3.14159265358979323846264338327950288
6 #endif
7  
8 #ifndef M_SQRT2
9 # define M_SQRT2 1.41421356237309504880168872420969808
10 #endif
11  
12 #ifndef M_LN2
13 # define M_LN2 0.693147180559945309417232121458176568
14 #endif
15  
16 #define CS_SQRT2PI 2.50662827463100050241576528481104525
17 #define CS_SQRTPI_2 0.886226925452758013649083741670572591
18  
19 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800) || defined(__GNUC__) || defined(__clang__)
20 # define CS_HAVE_C99_MATH 1
21 #else
22 # define CS_HAVE_C99_MATH 0
23 #endif
24  
25 #define SWAP(a,b) tmp=(a);(a)=(b);(b)=tmp;
26  
27 double
28 2217 cs_select(double* sample, I32 n, U32 k) {
29 U32 i, ir, j, l, mid;
30 double a, tmp;
31  
32 2217 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 2217 2217
33 0 return 0.;
34 2217 ▶ if (k >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (k >= (U32)n) 0 2217 2217
35 0 k = (U32)(n - 1);
36  
37 /* Propagate NaN deterministically regardless of element permutation */
38 49913 ▶ for (i = 0; i < (U32)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (U32)n; ++i) { 47744 2169 49913
39 47744 ▶ if (CS_ISNAN(sample[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(sample[i])) 48 47696 47744
40 48 return sample[i];
41 }
42  
43 2169 l = 0;
44 2169 ir = n-1;
45 while(1) {
46 9144 ▶ if (ir <= l+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ir <= l+1) { 2169 6975 9144
47 2169 ▶ if (ir == l+1 && sample[ir] < sample[l]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ir == l+1 && sample[ir] < sample[l]) { 654 1515 2169
if (ir == l+1 && sample[ir] < sample[l]) { 239 415 654
48 239 SWAP(sample[l], sample[ir]);
49 }
50 2169 return sample[k];
51 }
52 else {
53 6975 mid = (l+ir) >> 1;
54 6975 SWAP(sample[mid], sample[l+1]);
55 6975 ▶ if (sample[l] > sample[ir]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l] > sample[ir]) { 3180 3795 6975
56 3180 SWAP(sample[l], sample[ir]);
57 }
58 6975 ▶ if (sample[l+1] > sample[ir]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l+1] > sample[ir]) { 1895 5080 6975
59 1895 SWAP(sample[l+1], sample[ir]);
60 }
61 6975 ▶ if (sample[l] > sample[l+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l] > sample[l+1]) { 1943 5032 6975
62 1943 SWAP(sample[l], sample[l+1]);
63 }
64 6975 i = l+1;
65 6975 j = ir;
66 6975 a = sample[l+1];
67 15388 while(1) {
68 44267 ▶ do i++; while (sample[i] < a);
Branch: 1 branch
BranchTrueFalseTotal
do i++; while (sample[i] < a); 21904 22363 44267
69 46824 ▶ do j--; while (sample[j] > a);
Branch: 1 branch
BranchTrueFalseTotal
do j--; while (sample[j] > a); 24461 22363 46824
70 22363 ▶ if (j < i)
Branch: 1 branch
BranchTrueFalseTotal
if (j < i) 6975 15388 22363
71 6975 break;
72 15388 SWAP(sample[i], sample[j]);
73 }
74 6975 sample[l+1] = sample[j];
75 6975 sample[j] = a;
76 6975 ▶ if (j >= k)
Branch: 1 branch
BranchTrueFalseTotal
if (j >= k) 3986 2989 6975
77 3986 ir = j-1;
78 6975 ▶ if (j <= k)
Branch: 1 branch
BranchTrueFalseTotal
if (j <= k) 3861 3114 6975
79 3861 l = i;
80 }
81 }
82 }
83  
84 double
85 2056 cs_quantile(double* sample, I32 n, double p)
86 {
87 double h, frac, val_lo, val_hi;
88 U32 lo, hi, i;
89  
90 2056 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 2056 2056
91 0 return 0.;
92 2056 ▶ if (CS_ISNAN(p))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(p)) 0 2056 2056
93 0 return p;
94 2056 ▶ if (p <= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p <= 0.0) 1 2055 2056
95 1 p = 0.0;
96 2055 ▶ else if (p >= 1.0)
Branch: 1 branch
BranchTrueFalseTotal
else if (p >= 1.0) 1 2054 2055
97 1 p = 1.0;
98  
99 2056 h = (double)(n - 1) * p;
100 2056 lo = (U32)floor(h);
101 2056 hi = (U32)ceil(h);
102 2056 ▶ if (lo >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (lo >= (U32)n) 0 2056 2056
103 0 lo = (U32)(n - 1);
104 2056 ▶ if (hi >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= (U32)n) 0 2056 2056
105 0 hi = (U32)(n - 1);
106 2056 frac = h - (double)lo;
107  
108 2056 val_lo = cs_select(sample, n, lo);
109 2056 ▶ if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0)
Branch: 3 branches
BranchTrueFalseTotal
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 2044 12 2056
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 2029 15 2044
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 0 2029 2029
110 27 return val_lo;
111  
112 /* Partition invariant of cs_select guarantees sample[lo+1..n-1] >= val_lo */
113 2029 val_hi = sample[lo + 1];
114 2029 ▶ if (CS_ISNAN(val_hi))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(val_hi)) 0 2029 2029
115 0 return val_hi;
116 22644 ▶ for (i = lo + 2; i < (U32)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = lo + 2; i < (U32)n; ++i) { 20615 2029 22644
117 20615 ▶ if (CS_ISNAN(sample[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(sample[i])) 0 20615 20615
118 0 return sample[i];
119 20615 ▶ if (sample[i] < val_hi)
Branch: 1 branch
BranchTrueFalseTotal
if (sample[i] < val_hi) 499 20116 20615
120 499 val_hi = sample[i];
121 }
122  
123 2029 ▶ if (val_lo == val_hi)
Branch: 1 branch
BranchTrueFalseTotal
if (val_lo == val_hi) 748 1281 2029
124 748 return val_lo;
125 1281 return (1.0 - frac) * val_lo + frac * val_hi;
126 }
127  
128 double
129 103 cs_median(double* sample, I32 n)
130 {
131 U32 k;
132 103 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 103 103
133 0 return 0.;
134 103 k = n/2 - !(n & 1);
135 103 return cs_select(sample, n, k);
136 }
137  
138 double
139 2013 cs_median_exact(double* sample, I32 n)
140 {
141 2013 return cs_quantile(sample, n, 0.5);
142 }
143  
144  
145 double
146 10 cs_first_quartile(double* sample, I32 n)
147 {
148 U32 k;
149 10 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 10 10
150 0 return 0.;
151 10 k = (U32)(n / 4);
152 10 ▶ if (k >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (k >= (U32)n) 0 10 10
153 0 k = (U32)(n - 1);
154 10 return cs_select(sample, n, k);
155 }
156  
157  
158 double
159 10 cs_third_quartile(double* sample, I32 n)
160 {
161 U32 k;
162 10 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 10 10
163 0 return 0.;
164 10 k = (U32)(((cs_u64)(U32)n * 3) / 4);
165 10 ▶ if (k >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (k >= (U32)n) 0 10 10
166 0 k = (U32)(n - 1);
167 10 return cs_select(sample, n, k);
168 }
169  
170  
171 double
172 34 cs_mean(double* sample, I32 n)
173 {
174 I32 i;
175 34 double sum = 0.;
176 34 double c = 0.;
177 34 int has_nonfinite = 0;
178  
179 34 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 34 34
180 0 return 0.;
181  
182 378 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 344 34 378
183 344 double x = sample[i];
184 double t;
185 344 ▶ if (!CS_ISFINITE(x))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(x)) 344 0 344
if (!CS_ISFINITE(x)) 0 344 344
186 0 has_nonfinite = 1;
187 344 t = sum + x;
188 344 ▶ if (CS_ISFINITE(t)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(t)) { 344 0 344
if (CS_ISFINITE(t)) { 344 0 344
189 344 ▶ if (fabs(sum) >= fabs(x))
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sum) >= fabs(x)) 291 53 344
190 291 c += (sum - t) + x;
191 else
192 53 c += (x - t) + sum;
193 }
194 344 sum = t;
195 }
196  
197 34 ▶ if (CS_ISFINITE(sum)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(sum)) { 34 0 34
if (CS_ISFINITE(sum)) { 34 0 34
198 34 double total = sum + c;
199 34 ▶ if (CS_ISFINITE(total))
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(total)) 34 0 34
if (CS_ISFINITE(total)) 34 0 34
200 34 return total / (double)n;
201 }
202  
203 0 ▶ if (has_nonfinite)
Branch: 1 branch
BranchTrueFalseTotal
if (has_nonfinite) 0 0 0
204 0 return sum / (double)n;
205  
206 /* All inputs were finite, but sum overflowed: compute scaled incremental mean */
207 {
208 0 double m = 0.;
209 0 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 0 0 0
210 0 double d = (double)(i + 1);
211 0 m = m * ((double)i / d) + sample[i] / d;
212 }
213 0 return m;
214 }
215 }
216  
217 void
218 9089 do_resample(double* original, I32 n, struct mt* rdgen, double* dest)
219 {
220 I32 i;
221 9089 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 9089 9089
222 0 return;
223 100460 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 91371 9089 100460
224 91371 dest[i] = original[mt_genrand_bounded(rdgen, (uint32_t)n)];
225 }
226 }
227  
228 static double
229 31 cs_erfc_impl(double x)
230 {
231 #if CS_HAVE_C99_MATH
232 31 return erfc(x);
233 #else
234 /* Fallback for pre-C99 compilers */
235 if (x < 0.0)
236 return 2.0 - cs_erfc_impl(-x);
237 if (x > 27.0)
238 return 0.0;
239 {
240 /* Abramowitz & Stegun 7.1.26 */
241 const double t = 1.0 / (1.0 + 0.3275911 * x);
242 const double poly = t * (0.254829592
243 + t * (-0.284496736
244 + t * (1.421413741
245 + t * (-1.453152027
246 + t * 1.061405429))));
247 return poly * exp(-x * x);
248 }
249 #endif
250 }
251  
252 double
253 21 cs_approx_erf(double x)
254 {
255 #if CS_HAVE_C99_MATH
256 21 return erf(x);
257 #else
258 if (fabs(x) < 1e-4) {
259 const double x2 = x * x;
260 return (2.0 / sqrt(M_PI)) * x * (1.0 - x2 / 3.0 + x2 * x2 / 10.0);
261 }
262 return (x < 0.0) ? -(1.0 - cs_erfc_impl(-x)) : (1.0 - cs_erfc_impl(x));
263 #endif
264 }
265  
266 double
267 8 cs_norm_cdf(double z)
268 {
269 8 return 0.5 * cs_erfc_impl(-z / M_SQRT2);
270 }
271  
272 double
273 20 cs_norm_ppf(double p)
274 {
275 static const double a1 = -3.969683028665376e+01;
276 static const double a2 = 2.209460984245205e+02;
277 static const double a3 = -2.759285104469687e+02;
278 static const double a4 = 1.383577518672690e+02;
279 static const double a5 = -3.066479806614716e+01;
280 static const double a6 = 2.506628277459239e+00;
281  
282 static const double b1 = -5.447609879822406e+01;
283 static const double b2 = 1.615858368580409e+02;
284 static const double b3 = -1.556989798598866e+02;
285 static const double b4 = 6.680131188771972e+01;
286 static const double b5 = -1.328068155288572e+01;
287  
288 static const double c1 = -7.784894002430293e-03;
289 static const double c2 = -3.223964580411365e-01;
290 static const double c3 = -2.400758277161838e+00;
291 static const double c4 = -2.549732539343734e+00;
292 static const double c5 = 4.374664141464968e+00;
293 static const double c6 = 2.938163982698783e+00;
294  
295 static const double d1 = 7.784695709041462e-03;
296 static const double d2 = 3.224671290700398e-01;
297 static const double d3 = 2.445134137142996e+00;
298 static const double d4 = 3.754408661907416e+00;
299  
300 20 const double p_low = 0.02425;
301 20 const double p_high = 1.0 - p_low;
302 double q, r, z, e, u;
303  
304 20 ▶ if (CS_ISNAN(p))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(p)) 0 20 20
305 0 return p;
306 20 ▶ if (p < 0.0 || p > 1.0)
Branch: 2 branches
BranchTrueFalseTotal
if (p < 0.0 || p > 1.0) 20 0 20
if (p < 0.0 || p > 1.0) 0 20 20
307 0 return log(-1.0); /* NaN */
308 20 ▶ if (p == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p == 0.0) 0 20 20
309 0 return log(0.0); /* -Inf */
310 20 ▶ if (p == 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p == 1.0) 0 20 20
311 0 return -log(0.0); /* +Inf */
312  
313 20 ▶ if (p < p_low) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < p_low) { 5 15 20
314 5 q = sqrt(-2.0 * log(p));
315 5 z = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6)
316 5 / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1.0);
317 }
318 15 ▶ else if (p <= p_high) {
Branch: 1 branch
BranchTrueFalseTotal
else if (p <= p_high) { 15 0 15
319 15 q = p - 0.5;
320 15 r = q * q;
321 15 z = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q
322 15 / (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1.0);
323 }
324 else {
325 0 q = sqrt(-2.0 * log(1.0 - p));
326 0 z = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6)
327 0 / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1.0);
328 }
329  
330 /* One Halley refinement step for full IEEE-754 double precision */
331 20 ▶ if (p < 0.25)
Branch: 1 branch
BranchTrueFalseTotal
if (p < 0.25) 13 7 20
332 13 e = 0.5 * cs_erfc_impl(-z / M_SQRT2) - p;
333 7 ▶ else if (p > 0.75)
Branch: 1 branch
BranchTrueFalseTotal
else if (p > 0.75) 0 7 7
334 0 e = (1.0 - p) - 0.5 * cs_erfc_impl(z / M_SQRT2);
335 else
336 7 e = 0.5 * cs_approx_erf(z / M_SQRT2) - (p - 0.5);
337  
338 20 u = e * CS_SQRT2PI * exp(0.5 * z * z);
339 20 ▶ if (CS_ISFINITE(u)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(u)) { 20 0 20
if (CS_ISFINITE(u)) { 20 0 20
340 20 double denom = 1.0 + 0.5 * z * u;
341 20 ▶ if (denom != 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom != 0.0) 20 0 20
342 20 z -= u / denom;
343 }
344  
345 20 return z;
346 }
347  
348 double
349 5 cs_approx_erf_inv(double x)
350 {
351 double z;
352  
353 5 ▶ if (CS_ISNAN(x))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(x)) 0 5 5
354 0 return x;
355 5 ▶ if (x <= -1.0 || x >= 1.0) {
Branch: 2 branches
BranchTrueFalseTotal
if (x <= -1.0 || x >= 1.0) { 5 0 5
if (x <= -1.0 || x >= 1.0) { 0 5 5
356 0 ▶ if (x == 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == 1.0) 0 0 0
357 0 return -log(0.0);
358 0 ▶ if (x == -1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == -1.0) 0 0 0
359 0 return log(0.0);
360 0 return log(-1.0);
361 }
362 5 ▶ if (x == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == 0.0) 0 5 5
363 0 return 0.0;
364  
365 5 ▶ if (x > 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x > 0.0) 5 0 5
366 5 z = -cs_norm_ppf(0.5 * (1.0 - x)) / M_SQRT2;
367 else
368 0 z = cs_norm_ppf(0.5 * (1.0 + x)) / M_SQRT2;
369  
370 /* Halley step directly on erf(z) - x avoids 1 - x cancellation near 0 */
371 5 ▶ if (fabs(x) < 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(x) < 0.5) { 3 2 5
372 3 double e = cs_approx_erf(z) - x;
373 3 double u = e * CS_SQRTPI_2 * exp(z * z);
374 3 ▶ if (CS_ISFINITE(u)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(u)) { 3 0 3
if (CS_ISFINITE(u)) { 3 0 3
375 3 double denom = 1.0 + z * u;
376 3 ▶ if (denom != 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom != 0.0) 3 0 3
377 3 z -= u / denom;
378 }
379 }
380  
381 5 return z;
382 }
383  
384 double
385 7 cs_alpha_to_nsigma(double alpha)
386 {
387 7 ▶ if (CS_ISNAN(alpha))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(alpha)) 0 7 7
388 0 return alpha;
389 7 ▶ if (alpha > 0.0 && alpha < 2.0)
Branch: 2 branches
BranchTrueFalseTotal
if (alpha > 0.0 && alpha < 2.0) 7 0 7
if (alpha > 0.0 && alpha < 2.0) 7 0 7
390 7 return -cs_norm_ppf(0.5 * alpha);
391 0 ▶ if (alpha == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (alpha == 0.0) 0 0 0
392 0 return -log(0.0);
393 0 ▶ if (alpha == 2.0)
Branch: 1 branch
BranchTrueFalseTotal
if (alpha == 2.0) 0 0 0
394 0 return log(0.0);
395 0 return log(-1.0);
396 }
397  
398 double
399 10 cs_nsigma_to_alpha(double nsigma)
400 {
401 10 return cs_erfc_impl(nsigma / M_SQRT2);
402 }
403  
404 static double
405 15 cs_lgamma_impl(double x)
406 {
407 #if CS_HAVE_C99_MATH
408 15 return lgamma(x);
409 #else
410 /* 9-term Lanczos approximation (g = 7) for x > 0 */
411 static const double c[9] = {
412 0.99999999999980993,
413 676.5203681218851,
414 -1259.1392167224028,
415 771.32342877765313,
416 -176.61502916214059,
417 12.507343278686905,
418 -0.13857109526572012,
419 9.9843695780195716e-6,
420 1.5056327351493116e-7
421 };
422 double z, sum, t;
423 int i;
424 if (x < 0.5)
425 return log(M_PI / sin(M_PI * x)) - cs_lgamma_impl(1.0 - x);
426 z = x - 1.0;
427 sum = c[0];
428 for (i = 1; i < 9; ++i)
429 sum += c[i] / (z + (double)i);
430 t = z + 7.5;
431 return 0.5 * log(2.0 * M_PI) + (z + 0.5) * log(t) - t + log(sum);
432 #endif
433 }
434  
435 static double
436 8 cs_bca_adj_prob(double z0, double a, double z_val)
437 {
438 8 double num = z0 + z_val;
439 8 double denom = 1.0 - a * num;
440 double p;
441 8 ▶ if (denom <= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom <= 0.0) 0 8 8
442 0 ▶ return (num < 0.0) ? 0.0 : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
return (num < 0.0) ? 0.0 : 1.0; 0 0 0
443 8 p = cs_norm_cdf(z0 + num / denom);
444 8 ▶ if (p < 0.0) return 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if (p < 0.0) return 0.0; 0 8 8
445 8 ▶ if (p > 1.0) return 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (p > 1.0) return 1.0; 0 8 8
446 8 return p;
447 }
448  
449 void
450 4 cs_bca_confidence_limits(
451 double statistic,
452 double* boot, I32 n_boot,
453 double* jack, I32 n_jack,
454 double confidence,
455 double* lower_out, double* upper_out)
456 {
457 4 I32 i, n_less = 0, n_eq = 0;
458 4 double p0, z0, jack_mean, max_d = 0.0, a = 0.0;
459 double alpha, z_lo, z_hi, p_lo, p_hi;
460  
461 4 ▶ if (CS_ISNAN(statistic)) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(statistic)) { 0 4 4
462 0 *lower_out = statistic;
463 0 *upper_out = statistic;
464 0 return;
465 }
466 311 ▶ for (i = 0; i < n_boot; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_boot; ++i) { 307 4 311
467 307 ▶ if (CS_ISNAN(boot[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(boot[i])) { 0 307 307
468 0 *lower_out = boot[i];
469 0 *upper_out = boot[i];
470 0 return;
471 }
472 307 ▶ if (boot[i] < statistic)
Branch: 1 branch
BranchTrueFalseTotal
if (boot[i] < statistic) 160 147 307
473 160 n_less++;
474 147 ▶ else if (boot[i] == statistic)
Branch: 1 branch
BranchTrueFalseTotal
else if (boot[i] == statistic) 7 140 147
475 7 n_eq++;
476 }
477 18 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 14 4 18
478 14 ▶ if (CS_ISNAN(jack[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(jack[i])) { 0 14 14
479 0 *lower_out = jack[i];
480 0 *upper_out = jack[i];
481 0 return;
482 }
483 }
484  
485 /* Bias-correction factor z0 (with mid-p tie handling and boundary clamping) */
486 4 ▶ if (n_boot <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_boot <= 1) { 0 4 4
487 0 p0 = 0.5;
488 }
489 else {
490 4 double min_p = 0.5 / (double)n_boot;
491 4 p0 = ((double)n_less + 0.5 * (double)n_eq) / (double)n_boot;
492 4 ▶ if (p0 < min_p)
Branch: 1 branch
BranchTrueFalseTotal
if (p0 < min_p) 0 4 4
493 0 p0 = min_p;
494 4 ▶ else if (p0 > 1.0 - min_p)
Branch: 1 branch
BranchTrueFalseTotal
else if (p0 > 1.0 - min_p) 0 4 4
495 0 p0 = 1.0 - min_p;
496 }
497 4 z0 = cs_norm_ppf(p0);
498  
499 /* Acceleration factor a from jackknife leave-one-out replicates */
500 4 ▶ if (n_jack >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_jack >= 2) { 4 0 4
501 4 jack_mean = cs_mean(jack, n_jack);
502 18 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 14 4 18
503 14 double ad = fabs(jack_mean - jack[i]);
504 14 ▶ if (ad > max_d)
Branch: 1 branch
BranchTrueFalseTotal
if (ad > max_d) 2 12 14
505 2 max_d = ad;
506 }
507 4 ▶ if (max_d > 0.0 && CS_ISFINITE(max_d)) {
Branch: 3 branches
BranchTrueFalseTotal
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 2 2 4
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 2 0 2
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 2 0 2
508 2 double s2 = 0.0, s3 = 0.0;
509 10 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 8 2 10
510 8 double d = (jack_mean - jack[i]) / max_d;
511 8 double d2 = d * d;
512 8 s2 += d2;
513 8 s3 += d2 * d;
514 }
515 2 ▶ if (s2 > 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (s2 > 0.0) 2 0 2
516 2 a = s3 / (6.0 * s2 * sqrt(s2));
517 }
518 }
519  
520 4 alpha = 1.0 - confidence;
521 4 z_lo = cs_norm_ppf(0.5 * alpha);
522 4 z_hi = -z_lo;
523  
524 4 p_lo = cs_bca_adj_prob(z0, a, z_lo);
525 4 p_hi = cs_bca_adj_prob(z0, a, z_hi);
526 4 ▶ if (p_lo > p_hi) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_lo > p_hi) { 0 4 4
527 0 double tmp = p_lo;
528 0 p_lo = p_hi;
529 0 p_hi = tmp;
530 }
531  
532 4 *lower_out = cs_quantile(boot, n_boot, p_lo);
533 4 *upper_out = cs_quantile(boot, n_boot, p_hi);
534 }
535  
536 void
537 12 cs_median_exact_confidence_limits(
538 double* sample, I32 n,
539 double confidence,
540 double* lower_out, double* median_out, double* upper_out)
541 {
542 I32 m, k;
543 double p, cov;
544 U32 lower_idx, upper_idx;
545  
546 12 *median_out = cs_median_exact(sample, n);
547 12 ▶ if (n <= 0 || CS_ISNAN(*median_out)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || CS_ISNAN(*median_out)) { 12 0 12
if (n <= 0 || CS_ISNAN(*median_out)) { 6 6 12
548 6 *lower_out = *median_out;
549 6 *upper_out = *median_out;
550 6 return;
551 }
552 6 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 1 5 6
553 1 *lower_out = sample[0];
554 1 *upper_out = sample[0];
555 1 return;
556 }
557  
558 /*
559 * Exact distribution-free confidence interval via Binomial(n, 0.5):
560 * P(X_(k) <= med <= X_(n-k+1)) = sum_{j=k}^{n-k} C(n,j) / 2^n.
561 * Walk outward symmetrically from the mode m = floor(n/2).
562 */
563 5 m = n / 2;
564 5 p = exp(
565 5 cs_lgamma_impl((double)n + 1.0)
566 5 - cs_lgamma_impl((double)m + 1.0)
567 5 - cs_lgamma_impl((double)(n - m) + 1.0)
568 5 - (double)n * M_LN2
569 );
570  
571 5 k = m;
572 5 ▶ if (n & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n & 1) 2 3 5
573 2 cov = 2.0 * p;
574 else
575 3 cov = p;
576  
577 14 ▶ while (k > 1 && cov < confidence - 1e-14) {
Branch: 2 branches
BranchTrueFalseTotal
while (k > 1 && cov < confidence - 1e-14) { 12 2 14
while (k > 1 && cov < confidence - 1e-14) { 9 3 12
578 9 p *= (double)k / (double)(n - k + 1);
579 9 k--;
580 9 cov += 2.0 * p;
581 }
582  
583 5 lower_idx = (U32)(k - 1);
584 5 upper_idx = (U32)(n - k);
585  
586 5 *lower_out = cs_select(sample, n, lower_idx);
587 5 *upper_out = cs_select(sample, n, upper_idx);
588 }
589  
« blib/lib/Statistics/CaseResampling.pm ↑ Summary RdGen.xs.inc »