_stats.c

filter: Statementstmt 83.2% 337 / 405 Branchbran 71.5% 209 / 292 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.3% 546 / 697 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« RdGen.xs.inc ↑ Summary _mt.c »
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 static void
28 1 cs_heapsort(double* arr, U32 n)
29 {
30 U32 start, end;
31 double tmp;
32 1 ▶ if (n <= 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 1) 0 1 1
33 0 return;
34 985 ▶ for (start = n / 2; start > 0; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (start = n / 2; start > 0; ) { 984 1 985
35 U32 root;
36 984 start--;
37 984 root = start;
38 2466 ▶ while (2 * root + 1 < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (2 * root + 1 < n) { 1949 517 2466
39 1949 U32 child = 2 * root + 1;
40 1949 ▶ if (child + 1 < n && arr[child] < arr[child + 1])
Branch: 2 branches
BranchTrueFalseTotal
if (child + 1 < n && arr[child] < arr[child + 1]) 1946 3 1949
if (child + 1 < n && arr[child] < arr[child + 1]) 644 1302 1946
41 644 child++;
42 1949 ▶ if (arr[root] >= arr[child])
Branch: 1 branch
BranchTrueFalseTotal
if (arr[root] >= arr[child]) 467 1482 1949
43 467 break;
44 1482 SWAP(arr[root], arr[child]);
45 1482 root = child;
46 }
47 }
48 1968 ▶ for (end = n - 1; end > 0; end--) {
Branch: 1 branch
BranchTrueFalseTotal
for (end = n - 1; end > 0; end--) { 1967 1 1968
49 1967 U32 root = 0;
50 1967 SWAP(arr[0], arr[end]);
51 18651 ▶ while (2 * root + 1 < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (2 * root + 1 < end) { 17027 1624 18651
52 17027 U32 child = 2 * root + 1;
53 17027 ▶ if (child + 1 < end && arr[child] < arr[child + 1])
Branch: 2 branches
BranchTrueFalseTotal
if (child + 1 < end && arr[child] < arr[child + 1]) 17018 9 17027
if (child + 1 < end && arr[child] < arr[child + 1]) 7801 9217 17018
54 7801 child++;
55 17027 ▶ if (arr[root] >= arr[child])
Branch: 1 branch
BranchTrueFalseTotal
if (arr[root] >= arr[child]) 343 16684 17027
56 343 break;
57 16684 SWAP(arr[root], arr[child]);
58 16684 root = child;
59 }
60 }
61 }
62  
63 double
64 67319 cs_select(double* sample, I32 n, U32 k) {
65 U32 i, ir, j, l, mid;
66 double a, tmp;
67 67319 cs_u64 work = 0;
68 cs_u64 limit;
69  
70 67319 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 67319 67319
71 0 return 0.;
72 67319 ▶ if (k >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (k >= (U32)n) 0 67319 67319
73 0 k = (U32)(n - 1);
74  
75 /* Propagate NaN deterministically regardless of element permutation */
76 2946293 ▶ for (i = 0; i < (U32)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (U32)n; ++i) { 2879024 67269 2946293
77 2879024 ▶ if (CS_ISNAN(sample[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(sample[i])) 50 2878974 2879024
78 50 return sample[i];
79 }
80  
81 67269 limit = (cs_u64)(U32)n * 6;
82 67269 l = 0;
83 67269 ir = n-1;
84 while(1) {
85 339596 ▶ if (ir <= l+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ir <= l+1) { 67268 272328 339596
86 67268 ▶ if (ir == l+1 && sample[ir] < sample[l]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ir == l+1 && sample[ir] < sample[l]) { 18967 48301 67268
if (ir == l+1 && sample[ir] < sample[l]) { 6603 12364 18967
87 6603 SWAP(sample[l], sample[ir]);
88 }
89 67268 return sample[k];
90 }
91 else {
92 272328 work += (cs_u64)(ir - l + 1);
93 272328 ▶ if (work > limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (work > limit) { 1 272327 272328
94 1 cs_heapsort(sample + l, ir - l + 1);
95 1 return sample[k];
96 }
97 272327 mid = (l+ir) >> 1;
98 272327 SWAP(sample[mid], sample[l+1]);
99 272327 ▶ if (sample[l] > sample[ir]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l] > sample[ir]) { 122906 149421 272327
100 122906 SWAP(sample[l], sample[ir]);
101 }
102 272327 ▶ if (sample[l+1] > sample[ir]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l+1] > sample[ir]) { 79067 193260 272327
103 79067 SWAP(sample[l+1], sample[ir]);
104 }
105 272327 ▶ if (sample[l] > sample[l+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (sample[l] > sample[l+1]) { 78774 193553 272327
106 78774 SWAP(sample[l], sample[l+1]);
107 }
108 272327 i = l+1;
109 272327 j = ir;
110 272327 a = sample[l+1];
111 1005570 while(1) {
112 2923865 ▶ do i++; while (sample[i] < a);
Branch: 1 branch
BranchTrueFalseTotal
do i++; while (sample[i] < a); 1645968 1277897 2923865
113 2943773 ▶ do j--; while (sample[j] > a);
Branch: 1 branch
BranchTrueFalseTotal
do j--; while (sample[j] > a); 1665876 1277897 2943773
114 1277897 ▶ if (j < i)
Branch: 1 branch
BranchTrueFalseTotal
if (j < i) 272327 1005570 1277897
115 272327 break;
116 1005570 SWAP(sample[i], sample[j]);
117 }
118 272327 sample[l+1] = sample[j];
119 272327 sample[j] = a;
120 272327 ▶ if (j >= k)
Branch: 1 branch
BranchTrueFalseTotal
if (j >= k) 150931 121396 272327
121 150931 ir = j-1;
122 272327 ▶ if (j <= k)
Branch: 1 branch
BranchTrueFalseTotal
if (j <= k) 149127 123200 272327
123 149127 l = i;
124 }
125 }
126 }
127  
128 double
129 66483 cs_quantile(double* sample, I32 n, double p)
130 {
131 double h, frac, val_lo, val_hi;
132 U32 lo, hi, i;
133  
134 66483 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 66483 66483
135 0 return 0.;
136 66483 ▶ if (CS_ISNAN(p))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(p)) 0 66483 66483
137 0 return p;
138 66483 ▶ if (p <= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p <= 0.0) 1 66482 66483
139 1 p = 0.0;
140 66482 ▶ else if (p >= 1.0)
Branch: 1 branch
BranchTrueFalseTotal
else if (p >= 1.0) 1 66481 66482
141 1 p = 1.0;
142  
143 66483 h = (double)(n - 1) * p;
144 66483 lo = (U32)floor(h);
145 66483 hi = (U32)ceil(h);
146 66483 ▶ if (lo >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (lo >= (U32)n) 0 66483 66483
147 0 lo = (U32)(n - 1);
148 66483 ▶ if (hi >= (U32)n)
Branch: 1 branch
BranchTrueFalseTotal
if (hi >= (U32)n) 0 66483 66483
149 0 hi = (U32)(n - 1);
150 66483 frac = h - (double)lo;
151  
152 66483 val_lo = cs_select(sample, n, lo);
153 66483 ▶ if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0)
Branch: 3 branches
BranchTrueFalseTotal
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 66439 44 66483
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 65968 471 66439
if (CS_ISNAN(val_lo) || lo == hi || frac == 0.0) 0 65968 65968
154 515 return val_lo;
155  
156 /* Partition invariant of cs_select guarantees sample[lo+1..n-1] >= val_lo */
157 65968 val_hi = sample[lo + 1];
158 65968 ▶ if (CS_ISNAN(val_hi))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(val_hi)) 0 65968 65968
159 0 return val_hi;
160 1418986 ▶ for (i = lo + 2; i < (U32)n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = lo + 2; i < (U32)n; ++i) { 1353018 65968 1418986
161 1353018 ▶ if (CS_ISNAN(sample[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(sample[i])) 0 1353018 1353018
162 0 return sample[i];
163 1353018 ▶ if (sample[i] < val_hi)
Branch: 1 branch
BranchTrueFalseTotal
if (sample[i] < val_hi) 18042 1334976 1353018
164 18042 val_hi = sample[i];
165 }
166  
167 65968 ▶ if (val_lo == val_hi)
Branch: 1 branch
BranchTrueFalseTotal
if (val_lo == val_hi) 23078 42890 65968
168 23078 return val_lo;
169 42890 return (1.0 - frac) * val_lo + frac * val_hi;
170 }
171  
172 double
173 514 cs_median(double* sample, I32 n)
174 {
175 514 return cs_quantile(sample, n, 0.5);
176 }
177  
178 double
179 62613 cs_median_exact(double* sample, I32 n)
180 {
181 62613 return cs_quantile(sample, n, 0.5);
182 }
183  
184  
185 double
186 13 cs_first_quartile(double* sample, I32 n)
187 {
188 13 return cs_quantile(sample, n, 0.25);
189 }
190  
191  
192 double
193 13 cs_third_quartile(double* sample, I32 n)
194 {
195 13 return cs_quantile(sample, n, 0.75);
196 }
197  
198  
199 double
200 424188 cs_mean(double* sample, I32 n)
201 {
202 I32 i;
203 424188 double sum = 0.;
204 424188 double c = 0.;
205 424188 int has_nonfinite = 0;
206  
207 424188 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 424188 424188
208 0 return 0.;
209  
210 13095490 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 12671302 424188 13095490
211 12671302 double x = sample[i];
212 double t;
213 12671302 ▶ if (!CS_ISFINITE(x))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(x)) 12671284 18 12671302
if (!CS_ISFINITE(x)) 0 12671284 12671284
214 18 has_nonfinite = 1;
215 12671302 t = sum + x;
216 12671302 ▶ if (CS_ISFINITE(t)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(t)) { 12671266 36 12671302
if (CS_ISFINITE(t)) { 12671266 0 12671266
217 12671266 ▶ if (fabs(sum) >= fabs(x))
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sum) >= fabs(x)) 10907222 1764044 12671266
218 10907222 c += (sum - t) + x;
219 else
220 1764044 c += (x - t) + sum;
221 }
222 12671302 sum = t;
223 }
224  
225 424188 ▶ if (CS_ISFINITE(sum)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(sum)) { 424173 15 424188
if (CS_ISFINITE(sum)) { 424173 0 424173
226 424173 double total = sum + c;
227 424173 ▶ if (CS_ISFINITE(total))
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(total)) 424173 0 424173
if (CS_ISFINITE(total)) 424173 0 424173
228 424173 return total / (double)n;
229 }
230  
231 15 ▶ if (has_nonfinite)
Branch: 1 branch
BranchTrueFalseTotal
if (has_nonfinite) 15 0 15
232 15 return sum / (double)n;
233  
234 /* All inputs were finite, but sum overflowed: compute scaled incremental mean */
235 {
236 0 double m = 0.;
237 0 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 0 0 0
238 0 double d = (double)(i + 1);
239 0 m = m * ((double)i / d) + sample[i] / d;
240 }
241 0 return m;
242 }
243 }
244  
245 void
246 492079 do_resample(double* original, I32 n, struct mt* rdgen, double* dest)
247 {
248 I32 i;
249 492079 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 492079 492079
250 0 return;
251 15015230 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 14523151 492079 15015230
252 14523151 dest[i] = original[mt_genrand_bounded(rdgen, (uint32_t)n)];
253 }
254 }
255  
256 static double
257 1241 cs_erfc_impl(double x)
258 {
259 #if CS_HAVE_C99_MATH
260 1241 return erfc(x);
261 #else
262 /* Fallback for pre-C99 compilers */
263 if (x < 0.0)
264 return 2.0 - cs_erfc_impl(-x);
265 if (x > 27.0)
266 return 0.0;
267 {
268 /* Abramowitz & Stegun 7.1.26 */
269 const double t = 1.0 / (1.0 + 0.3275911 * x);
270 const double poly = t * (0.254829592
271 + t * (-0.284496736
272 + t * (1.421413741
273 + t * (-1.453152027
274 + t * 1.061405429))));
275 return poly * exp(-x * x);
276 }
277 #endif
278 }
279  
280 double
281 429 cs_approx_erf(double x)
282 {
283 #if CS_HAVE_C99_MATH
284 429 return erf(x);
285 #else
286 if (fabs(x) < 1e-4) {
287 const double x2 = x * x;
288 return (2.0 / sqrt(M_PI)) * x * (1.0 - x2 / 3.0 + x2 * x2 / 10.0);
289 }
290 return (x < 0.0) ? -(1.0 - cs_erfc_impl(-x)) : (1.0 - cs_erfc_impl(x));
291 #endif
292 }
293  
294 double
295 814 cs_norm_cdf(double z)
296 {
297 814 return 0.5 * cs_erfc_impl(-z / M_SQRT2);
298 }
299  
300 double
301 831 cs_norm_ppf(double p)
302 {
303 static const double a1 = -3.969683028665376e+01;
304 static const double a2 = 2.209460984245205e+02;
305 static const double a3 = -2.759285104469687e+02;
306 static const double a4 = 1.383577518672690e+02;
307 static const double a5 = -3.066479806614716e+01;
308 static const double a6 = 2.506628277459239e+00;
309  
310 static const double b1 = -5.447609879822406e+01;
311 static const double b2 = 1.615858368580409e+02;
312 static const double b3 = -1.556989798598866e+02;
313 static const double b4 = 6.680131188771972e+01;
314 static const double b5 = -1.328068155288572e+01;
315  
316 static const double c1 = -7.784894002430293e-03;
317 static const double c2 = -3.223964580411365e-01;
318 static const double c3 = -2.400758277161838e+00;
319 static const double c4 = -2.549732539343734e+00;
320 static const double c5 = 4.374664141464968e+00;
321 static const double c6 = 2.938163982698783e+00;
322  
323 static const double d1 = 7.784695709041462e-03;
324 static const double d2 = 3.224671290700398e-01;
325 static const double d3 = 2.445134137142996e+00;
326 static const double d4 = 3.754408661907416e+00;
327  
328 831 const double p_low = 0.02425;
329 831 const double p_high = 1.0 - p_low;
330 double q, r, z, e, u;
331  
332 831 ▶ if (CS_ISNAN(p))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(p)) 0 831 831
333 0 return p;
334 831 ▶ if (p < 0.0 || p > 1.0)
Branch: 2 branches
BranchTrueFalseTotal
if (p < 0.0 || p > 1.0) 831 0 831
if (p < 0.0 || p > 1.0) 0 831 831
335 0 return log(-1.0); /* NaN */
336 831 ▶ if (p == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p == 0.0) 0 831 831
337 0 return log(0.0); /* -Inf */
338 831 ▶ if (p == 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (p == 1.0) 0 831 831
339 0 return -log(0.0); /* +Inf */
340  
341 831 ▶ if (p < p_low) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < p_low) { 5 826 831
342 5 q = sqrt(-2.0 * log(p));
343 5 z = (((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6)
344 5 / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1.0);
345 }
346 826 ▶ else if (p <= p_high) {
Branch: 1 branch
BranchTrueFalseTotal
else if (p <= p_high) { 826 0 826
347 826 q = p - 0.5;
348 826 r = q * q;
349 826 z = (((((a1 * r + a2) * r + a3) * r + a4) * r + a5) * r + a6) * q
350 826 / (((((b1 * r + b2) * r + b3) * r + b4) * r + b5) * r + 1.0);
351 }
352 else {
353 0 q = sqrt(-2.0 * log(1.0 - p));
354 0 z = -(((((c1 * q + c2) * q + c3) * q + c4) * q + c5) * q + c6)
355 0 / ((((d1 * q + d2) * q + d3) * q + d4) * q + 1.0);
356 }
357  
358 /* One Halley refinement step for full IEEE-754 double precision */
359 831 ▶ if (p < 0.25)
Branch: 1 branch
BranchTrueFalseTotal
if (p < 0.25) 416 415 831
360 416 e = 0.5 * cs_erfc_impl(-z / M_SQRT2) - p;
361 415 ▶ else if (p > 0.75)
Branch: 1 branch
BranchTrueFalseTotal
else if (p > 0.75) 0 415 415
362 0 e = (1.0 - p) - 0.5 * cs_erfc_impl(z / M_SQRT2);
363 else
364 415 e = 0.5 * cs_approx_erf(z / M_SQRT2) - (p - 0.5);
365  
366 831 u = e * CS_SQRT2PI * exp(0.5 * z * z);
367 831 ▶ if (CS_ISFINITE(u)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(u)) { 831 0 831
if (CS_ISFINITE(u)) { 831 0 831
368 831 double denom = 1.0 + 0.5 * z * u;
369 831 ▶ if (denom != 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom != 0.0) 831 0 831
370 831 z -= u / denom;
371 }
372  
373 831 return z;
374 }
375  
376 double
377 11 cs_approx_erf_inv(double x)
378 {
379 double z;
380  
381 11 ▶ if (CS_ISNAN(x))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(x)) 0 11 11
382 0 return x;
383 11 ▶ if (x <= -1.0 || x >= 1.0) {
Branch: 2 branches
BranchTrueFalseTotal
if (x <= -1.0 || x >= 1.0) { 11 0 11
if (x <= -1.0 || x >= 1.0) { 0 11 11
384 0 ▶ if (x == 1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == 1.0) 0 0 0
385 0 return -log(0.0);
386 0 ▶ if (x == -1.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == -1.0) 0 0 0
387 0 return log(0.0);
388 0 return log(-1.0);
389 }
390 11 ▶ if (x == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x == 0.0) 2 9 11
391 2 return 0.0;
392  
393 9 ▶ if (x > 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (x > 0.0) 7 2 9
394 7 z = -cs_norm_ppf(0.5 * (1.0 - x)) / M_SQRT2;
395 else
396 2 z = cs_norm_ppf(0.5 * (1.0 + x)) / M_SQRT2;
397  
398 /* Halley step directly on erf(z) - x avoids 1 - x cancellation near 0 */
399 9 ▶ if (fabs(x) < 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(x) < 0.5) { 3 6 9
400 3 double e = cs_approx_erf(z) - x;
401 3 double u = e * CS_SQRTPI_2 * exp(z * z);
402 3 ▶ if (CS_ISFINITE(u)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(u)) { 3 0 3
if (CS_ISFINITE(u)) { 3 0 3
403 3 double denom = 1.0 + z * u;
404 3 ▶ if (denom != 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom != 0.0) 3 0 3
405 3 z -= u / denom;
406 }
407 }
408  
409 9 return z;
410 }
411  
412 double
413 8 cs_alpha_to_nsigma(double alpha)
414 {
415 8 ▶ if (CS_ISNAN(alpha))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(alpha)) 0 8 8
416 0 return alpha;
417 8 ▶ if (alpha > 0.0 && alpha < 2.0)
Branch: 2 branches
BranchTrueFalseTotal
if (alpha > 0.0 && alpha < 2.0) 8 0 8
if (alpha > 0.0 && alpha < 2.0) 8 0 8
418 8 return -cs_norm_ppf(0.5 * alpha);
419 0 ▶ if (alpha == 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (alpha == 0.0) 0 0 0
420 0 return -log(0.0);
421 0 ▶ if (alpha == 2.0)
Branch: 1 branch
BranchTrueFalseTotal
if (alpha == 2.0) 0 0 0
422 0 return log(0.0);
423 0 return log(-1.0);
424 }
425  
426 double
427 11 cs_nsigma_to_alpha(double nsigma)
428 {
429 11 return cs_erfc_impl(nsigma / M_SQRT2);
430 }
431  
432 static double
433 1215 cs_lgamma_impl(double x)
434 {
435 #if CS_HAVE_C99_MATH
436 1215 return lgamma(x);
437 #else
438 /* 9-term Lanczos approximation (g = 7) for x > 0 */
439 static const double c[9] = {
440 0.99999999999980993,
441 676.5203681218851,
442 -1259.1392167224028,
443 771.32342877765313,
444 -176.61502916214059,
445 12.507343278686905,
446 -0.13857109526572012,
447 9.9843695780195716e-6,
448 1.5056327351493116e-7
449 };
450 double z, sum, t;
451 int i;
452 if (x < 0.5)
453 return log(M_PI / sin(M_PI * x)) - cs_lgamma_impl(1.0 - x);
454 z = x - 1.0;
455 sum = c[0];
456 for (i = 1; i < 9; ++i)
457 sum += c[i] / (z + (double)i);
458 t = z + 7.5;
459 return 0.5 * log(2.0 * M_PI) + (z + 0.5) * log(t) - t + log(sum);
460 #endif
461 }
462  
463 static double
464 814 cs_bca_adj_prob(double z0, double a, double z_val)
465 {
466 814 double num = z0 + z_val;
467 814 double denom = 1.0 - a * num;
468 double p;
469 814 ▶ if (denom <= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (denom <= 0.0) 0 814 814
470 0 ▶ return (num < 0.0) ? 0.0 : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
return (num < 0.0) ? 0.0 : 1.0; 0 0 0
471 814 p = cs_norm_cdf(z0 + num / denom);
472 814 ▶ if (p < 0.0) return 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if (p < 0.0) return 0.0; 0 814 814
473 814 ▶ if (p > 1.0) return 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (p > 1.0) return 1.0; 0 814 814
474 814 return p;
475 }
476  
477 void
478 407 cs_bca_confidence_limits(
479 double statistic,
480 double* boot, I32 n_boot,
481 double* jack, I32 n_jack,
482 double confidence,
483 double* lower_out, double* upper_out)
484 {
485 407 I32 i, n_less = 0, n_eq = 0;
486 407 double p0, z0, jack_mean, max_d = 0.0, a = 0.0;
487 double alpha, z_lo, z_hi, p_lo, p_hi;
488  
489 407 ▶ if (CS_ISNAN(statistic)) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(statistic)) { 0 407 407
490 0 *lower_out = statistic;
491 0 *upper_out = statistic;
492 0 return;
493 }
494 121184 ▶ for (i = 0; i < n_boot; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_boot; ++i) { 120777 407 121184
495 120777 ▶ if (CS_ISNAN(boot[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(boot[i])) { 0 120777 120777
496 0 *lower_out = boot[i];
497 0 *upper_out = boot[i];
498 0 return;
499 }
500 120777 ▶ if (boot[i] < statistic)
Branch: 1 branch
BranchTrueFalseTotal
if (boot[i] < statistic) 61514 59263 120777
501 61514 n_less++;
502 59263 ▶ else if (boot[i] == statistic)
Branch: 1 branch
BranchTrueFalseTotal
else if (boot[i] == statistic) 81 59182 59263
503 81 n_eq++;
504 }
505 12436 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 12029 407 12436
506 12029 ▶ if (CS_ISNAN(jack[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(jack[i])) { 0 12029 12029
507 0 *lower_out = jack[i];
508 0 *upper_out = jack[i];
509 0 return;
510 }
511 }
512  
513 /* Bias-correction factor z0 (with mid-p tie handling and boundary clamping) */
514 407 ▶ if (n_boot <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_boot <= 1) { 0 407 407
515 0 p0 = 0.5;
516 }
517 else {
518 407 double min_p = 0.5 / (double)n_boot;
519 407 p0 = ((double)n_less + 0.5 * (double)n_eq) / (double)n_boot;
520 407 ▶ if (p0 < min_p)
Branch: 1 branch
BranchTrueFalseTotal
if (p0 < min_p) 0 407 407
521 0 p0 = min_p;
522 407 ▶ else if (p0 > 1.0 - min_p)
Branch: 1 branch
BranchTrueFalseTotal
else if (p0 > 1.0 - min_p) 0 407 407
523 0 p0 = 1.0 - min_p;
524 }
525 407 z0 = cs_norm_ppf(p0);
526  
527 /* Acceleration factor a from jackknife leave-one-out replicates */
528 407 ▶ if (n_jack >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_jack >= 2) { 406 1 407
529 406 jack_mean = cs_mean(jack, n_jack);
530 12434 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 12028 406 12434
531 12028 double ad = fabs(jack_mean - jack[i]);
532 12028 ▶ if (ad > max_d)
Branch: 1 branch
BranchTrueFalseTotal
if (ad > max_d) 1625 10403 12028
533 1625 max_d = ad;
534 }
535 406 ▶ if (max_d > 0.0 && CS_ISFINITE(max_d)) {
Branch: 3 branches
BranchTrueFalseTotal
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 403 3 406
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 403 0 403
if (max_d > 0.0 && CS_ISFINITE(max_d)) { 403 0 403
536 403 double s2 = 0.0, s3 = 0.0;
537 12421 ▶ for (i = 0; i < n_jack; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_jack; ++i) { 12018 403 12421
538 12018 double d = (jack_mean - jack[i]) / max_d;
539 12018 double d2 = d * d;
540 12018 s2 += d2;
541 12018 s3 += d2 * d;
542 }
543 403 ▶ if (s2 > 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (s2 > 0.0) 403 0 403
544 403 a = s3 / (6.0 * s2 * sqrt(s2));
545 }
546 }
547  
548 407 alpha = 1.0 - confidence;
549 407 z_lo = cs_norm_ppf(0.5 * alpha);
550 407 z_hi = -z_lo;
551  
552 407 p_lo = cs_bca_adj_prob(z0, a, z_lo);
553 407 p_hi = cs_bca_adj_prob(z0, a, z_hi);
554 407 ▶ if (p_lo > p_hi) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_lo > p_hi) { 0 407 407
555 0 double tmp = p_lo;
556 0 p_lo = p_hi;
557 0 p_hi = tmp;
558 }
559  
560 407 *lower_out = cs_quantile(boot, n_boot, p_lo);
561 407 *upper_out = cs_quantile(boot, n_boot, p_hi);
562 }
563  
564 void
565 412 cs_median_exact_confidence_limits(
566 double* sample, I32 n,
567 double confidence,
568 double* lower_out, double* median_out, double* upper_out)
569 {
570 I32 m, k;
571 double p, cov;
572 U32 lower_idx, upper_idx;
573  
574 412 *median_out = cs_median_exact(sample, n);
575 412 ▶ if (n <= 0 || CS_ISNAN(*median_out)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || CS_ISNAN(*median_out)) { 412 0 412
if (n <= 0 || CS_ISNAN(*median_out)) { 6 406 412
576 6 *lower_out = *median_out;
577 6 *upper_out = *median_out;
578 6 return;
579 }
580 406 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 1 405 406
581 1 *lower_out = sample[0];
582 1 *upper_out = sample[0];
583 1 return;
584 }
585  
586 /*
587 * Exact distribution-free confidence interval via Binomial(n, 0.5):
588 * P(X_(k) <= med <= X_(n-k+1)) = sum_{j=k}^{n-k} C(n,j) / 2^n.
589 * Walk outward symmetrically from the mode m = floor(n/2).
590 */
591 405 m = n / 2;
592 405 p = exp(
593 405 cs_lgamma_impl((double)n + 1.0)
594 405 - cs_lgamma_impl((double)m + 1.0)
595 405 - cs_lgamma_impl((double)(n - m) + 1.0)
596 405 - (double)n * M_LN2
597 );
598  
599 405 k = m;
600 405 ▶ if (n & 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n & 1) 2 403 405
601 2 cov = 2.0 * p;
602 else
603 403 cov = p;
604  
605 2414 ▶ while (k > 1 && cov < confidence - 1e-14) {
Branch: 2 branches
BranchTrueFalseTotal
while (k > 1 && cov < confidence - 1e-14) { 2412 2 2414
while (k > 1 && cov < confidence - 1e-14) { 2009 403 2412
606 2009 p *= (double)k / (double)(n - k + 1);
607 2009 k--;
608 2009 cov += 2.0 * p;
609 }
610  
611 405 lower_idx = (U32)(k - 1);
612 405 upper_idx = (U32)(n - k);
613  
614 405 *lower_out = cs_select(sample, n, lower_idx);
615 405 *upper_out = cs_select(sample, n, upper_idx);
616 }
617  
618 double
619 121003 cs_stddev(const double* sample, I32 n, double mean, int is_sample)
620 {
621 I32 i;
622 121003 double scale = 0.;
623 121003 double ssq = 1.;
624 121003 int has_inf = 0;
625  
626 121003 ▶ if (n <= 0 || (is_sample && n < 2))
Branch: 3 branches
BranchTrueFalseTotal
if (n <= 0 || (is_sample && n < 2)) 121003 0 121003
if (n <= 0 || (is_sample && n < 2)) 121003 0 121003
if (n <= 0 || (is_sample && n < 2)) 0 121003 121003
627 0 return 0.;
628 121003 ▶ if (CS_ISNAN(mean))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(mean)) 0 121003 121003
629 0 return mean;
630 121003 ▶ if (!CS_ISFINITE(mean))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(mean)) 121003 0 121003
if (!CS_ISFINITE(mean)) 0 121003 121003
631 0 has_inf = 1;
632  
633 3737419 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 3616416 121003 3737419
634 3616416 double val = sample[i];
635 double d, ad;
636 3616416 ▶ if (CS_ISNAN(val))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(val)) 0 3616416 3616416
637 0 return val;
638 3616416 d = val - mean;
639 3616416 ▶ if (CS_ISNAN(d))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(d)) 0 3616416 3616416
640 0 return d;
641 3616416 ▶ if (!CS_ISFINITE(d)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(d)) { 3616416 0 3616416
if (!CS_ISFINITE(d)) { 0 3616416 3616416
642 0 has_inf = 1;
643 0 continue;
644 }
645 3616416 ad = fabs(d);
646 3616416 ▶ if (ad > 0.) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad > 0.) { 3616194 222 3616416
647 3616194 ▶ if (scale < ad) {
Branch: 1 branch
BranchTrueFalseTotal
if (scale < ad) { 432152 3184042 3616194
648 432152 double r = scale / ad;
649 432152 ssq = 1. + ssq * r * r;
650 432152 scale = ad;
651 }
652 else {
653 3184042 double r = ad / scale;
654 3184042 ssq += r * r;
655 }
656 }
657 }
658  
659 121003 ▶ if (has_inf)
Branch: 1 branch
BranchTrueFalseTotal
if (has_inf) 0 121003 121003
660 0 return HUGE_VAL;
661 121003 ▶ if (scale == 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (scale == 0.) 106 120897 121003
662 106 return 0.;
663 120897 ▶ return scale * sqrt(ssq / (double)(is_sample ? n - 1 : n));
Branch: 1 branch
BranchTrueFalseTotal
return scale * sqrt(ssq / (double)(is_sample ? n - 1 : n)); 120897 0 120897
664 }
665  
666 void
667 404 cs_mean_bca_confidence_limits(
668 double* sample, I32 n,
669 double confidence, I32 runs, struct mt* rnd,
670 double* lower_out, double* mean_out, double* upper_out)
671 {
672 double m;
673 404 double *boot = NULL, *jack = NULL, *dest = NULL;
674 I32 i;
675  
676 404 m = cs_mean(sample, n);
677 404 *mean_out = m;
678 404 ▶ if (n <= 0 || CS_ISNAN(m)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || CS_ISNAN(m)) { 404 0 404
if (n <= 0 || CS_ISNAN(m)) { 1 403 404
679 1 *lower_out = m;
680 1 *upper_out = m;
681 1 return;
682 }
683  
684 403 Newx(boot, runs, double);
685 403 Newx(jack, n, double);
686 403 Newx(dest, n, double);
687  
688 120873 ▶ for (i = 0; i < runs; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < runs; ++i) { 120470 403 120873
689 120470 do_resample(sample, n, rnd, dest);
690 120470 boot[i] = cs_mean(dest, n);
691 }
692  
693 403 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 1 402 403
694 1 jack[0] = sample[0];
695 }
696 else {
697 402 double nm1 = (double)(n - 1);
698 12416 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 12014 402 12416
699 12014 jack[i] = m + (m / nm1 - sample[i] / nm1);
700 }
701 }
702  
703 403 cs_bca_confidence_limits(m, boot, runs, jack, n, confidence, lower_out, upper_out);
704  
705 403 Safefree(dest);
706 403 Safefree(jack);
707 403 Safefree(boot);
708 }
709  
710 void
711 404 cs_mean_studentized_confidence_limits(
712 double* sample, I32 n,
713 double confidence, I32 runs, struct mt* rnd,
714 double* lower_out, double* mean_out, double* upper_out)
715 {
716 double m, se, alpha, t_lo, t_hi;
717 404 double *tvals = NULL, *dest = NULL;
718 I32 i;
719  
720 404 m = cs_mean(sample, n);
721 404 *mean_out = m;
722 404 ▶ if (n < 2 || CS_ISNAN(m)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n < 2 || CS_ISNAN(m)) { 404 0 404
if (n < 2 || CS_ISNAN(m)) { 1 403 404
723 1 *lower_out = m;
724 1 *upper_out = m;
725 1 return;
726 }
727  
728 403 se = cs_stddev(sample, n, m, 1) / sqrt((double)n);
729 403 ▶ if (CS_ISNAN(se)) {
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(se)) { 0 403 403
730 0 *lower_out = se;
731 0 *upper_out = se;
732 0 return;
733 }
734 403 ▶ if (se == 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (se == 0.0) { 1 402 403
735 1 *lower_out = m;
736 1 *upper_out = m;
737 1 return;
738 }
739 402 ▶ if (!CS_ISFINITE(se)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(se)) { 402 0 402
if (!CS_ISFINITE(se)) { 0 402 402
740 0 *lower_out = -HUGE_VAL;
741 0 *upper_out = HUGE_VAL;
742 0 return;
743 }
744  
745 402 Newx(tvals, runs, double);
746 402 Newx(dest, n, double);
747  
748 121002 ▶ for (i = 0; i < runs; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < runs; ++i) { 120600 402 121002
749 double bm, bse;
750 120600 do_resample(sample, n, rnd, dest);
751 120600 bm = cs_mean(dest, n);
752 120600 bse = cs_stddev(dest, n, bm, 1) / sqrt((double)n);
753 120600 ▶ if (CS_ISNAN(bm) || CS_ISNAN(bse))
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISNAN(bm) || CS_ISNAN(bse)) 120600 0 120600
if (CS_ISNAN(bm) || CS_ISNAN(bse)) 0 120600 120600
754 0 tvals[i] = log(-1.0);
755 120600 ▶ else if (bse > 0.0)
Branch: 1 branch
BranchTrueFalseTotal
else if (bse > 0.0) 120495 105 120600
756 120495 tvals[i] = (bm - m) / bse;
757 105 ▶ else if (bm == m)
Branch: 1 branch
BranchTrueFalseTotal
else if (bm == m) 0 105 105
758 0 tvals[i] = 0.0;
759 else
760 105 ▶ tvals[i] = (bm > m) ? HUGE_VAL : -HUGE_VAL;
Branch: 1 branch
BranchTrueFalseTotal
tvals[i] = (bm > m) ? HUGE_VAL : -HUGE_VAL; 56 49 105
761 }
762  
763 402 alpha = 1.0 - confidence;
764 402 t_hi = cs_quantile(tvals, runs, 1.0 - 0.5 * alpha);
765 402 t_lo = cs_quantile(tvals, runs, 0.5 * alpha);
766  
767 402 *lower_out = m - t_hi * se;
768 402 *upper_out = m - t_lo * se;
769  
770 402 Safefree(dest);
771 402 Safefree(tvals);
772 }
773  
« RdGen.xs.inc ↑ Summary _mt.c »