CaseResampling.xs

filter: Statementstmt 95.7% 409 / 427 Branchbran 77.4% 220 / 284 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.4% 629 / 711 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« _mt.c ↑ Summary
1 #define PERL_NO_GET_CONTEXT /* we want efficiency */
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 #include "mt.h"
9 #include "stats.h"
10 #include <math.h>
11 #include <stdlib.h>
12  
13  
14 typedef struct mt * Statistics__CaseResampling__RdGen;
15  
16 void*
17 15 U32ArrayPtr (pTHX_ int n)
18 {
19 SV * sv;
20 15 ▶ if (n <= 0 || n > 10000000)
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || n > 10000000) 15 0 15
if (n <= 0 || n > 10000000) 0 15 15
21 0 croak("Invalid array size for U32ArrayPtr");
22 15 sv = sv_2mortal( NEWSV( 0, n*sizeof(U32) ) );
23 15 ▶ SvGROW(sv, n*sizeof(U32));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, n*sizeof(U32)); 15 0 15
SvGROW(sv, n*sizeof(U32)); 0 15 15
24 15 return SvPVX(sv);
25 }
26  
27 double
28 14 cs_mean_av(pTHX_ AV* sample)
29 {
30 I32 i, n;
31 SV** elem;
32 14 double sum = 0.;
33 14 double c = 0.;
34 14 int has_nonfinite = 0;
35  
36 14 n = av_len(sample)+1;
37 14 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 1 13 14
38 1 return 0.;
39  
40 1069 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 1056 13 1069
41 double x, t;
42 1056 ▶ if (NULL == (elem = av_fetch(sample, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(sample, i, 0))) { 0 1056 1056
43 0 croak("Could not fetch element from array");
44 }
45 1056 x = SvNV(*elem);
46 1056 ▶ if (!CS_ISFINITE(x))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(x)) 1055 1 1056
if (!CS_ISFINITE(x)) 1 1054 1055
47 2 has_nonfinite = 1;
48 1056 t = sum + x;
49 1056 ▶ if (CS_ISFINITE(t)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(t)) { 1055 1 1056
if (CS_ISFINITE(t)) { 1053 2 1055
50 1053 ▶ if (fabs(sum) >= fabs(x))
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sum) >= fabs(x)) 1033 20 1053
51 1033 c += (sum - t) + x;
52 else
53 20 c += (x - t) + sum;
54 }
55 1056 sum = t;
56 }
57  
58 13 ▶ if (CS_ISFINITE(sum)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(sum)) { 12 1 13
if (CS_ISFINITE(sum)) { 10 2 12
59 10 double total = sum + c;
60 10 ▶ if (CS_ISFINITE(total))
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(total)) 10 0 10
if (CS_ISFINITE(total)) 10 0 10
61 10 return total / (double)n;
62 }
63  
64 3 ▶ if (has_nonfinite)
Branch: 1 branch
BranchTrueFalseTotal
if (has_nonfinite) 2 1 3
65 2 return sum / (double)n;
66  
67 /* All inputs were finite, but sum overflowed: use scaled incremental mean */
68 {
69 1 double m = 0.;
70 3 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 2 1 3
71 2 double d = (double)(i + 1);
72 2 elem = av_fetch(sample, i, 0);
73 2 m = m * ((double)i / d) + SvNV(*elem) / d;
74 }
75 1 return m;
76 }
77 }
78  
79 double
80 13 cs_stddev_av(pTHX_ double mean, AV* sample, int is_sample)
81 {
82 I32 i, n;
83 SV** elem;
84 13 double scale = 0.;
85 13 double ssq = 1.;
86 13 int has_inf = 0;
87  
88 13 n = av_len(sample)+1;
89 13 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 13 13
90 0 return 0.;
91 13 ▶ if (CS_ISNAN(mean))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(mean)) 1 12 13
92 1 return mean;
93 12 ▶ if (!CS_ISFINITE(mean))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(mean)) 12 0 12
if (!CS_ISFINITE(mean)) 1 11 12
94 1 has_inf = 1;
95  
96 66 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 55 11 66
97 double val, d, ad;
98 55 ▶ if (NULL == (elem = av_fetch(sample, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(sample, i, 0))) { 0 55 55
99 0 croak("Could not fetch element from array");
100 }
101 55 val = SvNV(*elem);
102 55 ▶ if (CS_ISNAN(val))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(val)) 0 55 55
103 0 return val;
104 55 d = val - mean;
105 55 ▶ if (CS_ISNAN(d))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(d)) 1 54 55
106 1 return d;
107 54 ▶ if (!CS_ISFINITE(d)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(d)) { 54 0 54
if (!CS_ISFINITE(d)) { 3 51 54
108 3 has_inf = 1;
109 3 continue;
110 }
111 51 ad = fabs(d);
112 51 ▶ if (ad > 0.) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad > 0.) { 38 13 51
113 38 ▶ if (scale < ad) {
Branch: 1 branch
BranchTrueFalseTotal
if (scale < ad) { 12 26 38
114 12 double r = scale / ad;
115 12 ssq = 1. + ssq * r * r;
116 12 scale = ad;
117 }
118 else {
119 26 double r = ad / scale;
120 26 ssq += r * r;
121 }
122 }
123 }
124  
125 11 ▶ if (has_inf)
Branch: 1 branch
BranchTrueFalseTotal
if (has_inf) 1 10 11
126 1 return HUGE_VAL;
127 10 ▶ if (scale == 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (scale == 0.) 3 7 10
128 3 return 0.;
129 7 ▶ return scale * sqrt(ssq / (double)(is_sample ? n - 1 : n));
Branch: 1 branch
BranchTrueFalseTotal
return scale * sqrt(ssq / (double)(is_sample ? n - 1 : n)); 5 2 7
130 }
131  
132 void
133 7166 avToCAry(pTHX_ AV* in, double** out, I32* n)
134 {
135 I32 thisN;
136 double* ary;
137 SV** elem;
138 I32 i;
139 7166 *out = NULL;
140 7166 thisN = av_len(in)+1;
141 7166 ▶ if (thisN <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (thisN <= 0) { 15 7151 7166
142 15 *n = 0;
143 15 return;
144 }
145 7151 *n = thisN;
146  
147 7151 Newx(ary, thisN, double);
148 7151 *out = ary;
149 58445 ▶ for (i = 0; i < thisN; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < thisN; ++i) { 51294 7151 58445
150 51294 ▶ if (NULL == (elem = av_fetch(in, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(in, i, 0))) { 0 51294 51294
151 0 Safefree(ary);
152 0 *out = NULL;
153 0 croak("Could not fetch element from array");
154 }
155 else
156 51294 ary[i] = SvNV(*elem);
157 }
158 }
159  
160 void
161 7009 cAryToAV(pTHX_ double* in, AV** out, I32 n)
162 {
163 SV* elem;
164 I32 i;
165 7009 *out = newAV();
166 7009 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 7009 7009
167 0 return;
168  
169 7009 av_extend(*out, n-1);
170  
171 56720 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 49711 7009 56720
172 49711 elem = newSVnv(in[i]);
173 49711 ▶ if (NULL == av_store(*out, i, elem))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == av_store(*out, i, elem)) 0 49711 49711
174 0 SvREFCNT_dec(elem);
175 }
176 }
177  
178 struct mt*
179 7019 get_rnd(pTHX)
180 {
181 IV tmp;
182 struct mt* rnd;
183 7019 SV* therndsv = get_sv("Statistics::CaseResampling::Rnd", 0);
184 7019 ▶ if (therndsv == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (therndsv == NULL 7019 0 7019
185 7019 ▶ || !SvROK(therndsv)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(therndsv) 7019 0 7019
186 7019 ▶ || !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen")) 0 7019 7019
187 {
188 0 croak("Random number generator not set up!");
189 }
190 7019 tmp = SvIV((SV*)SvRV(therndsv));
191 7019 rnd = INT2PTR(struct mt*, tmp);
192 7019 ▶ if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) {
Branch: 3 branches
BranchTrueFalseTotal
if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) { 7019 0 7019
if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) { 17 7002 7019
if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) { 1 16 17
193 1 dSP;
194 1 ENTER;
195 1 SAVETMPS;
196 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
197 1 PUTBACK;
198 1 call_pv("Statistics::CaseResampling::_reseed_default_rng", G_VOID | G_DISCARD);
199 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 1 1
200 1 LEAVE;
201 1 therndsv = get_sv("Statistics::CaseResampling::Rnd", 0);
202 1 ▶ if (therndsv == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (therndsv == NULL 1 0 1
203 1 ▶ || !SvROK(therndsv)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(therndsv) 1 0 1
204 1 ▶ || !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen")) 0 1 1
205 {
206 0 croak("Random number generator not set up!");
207 }
208 1 tmp = SvIV((SV*)SvRV(therndsv));
209 1 rnd = INT2PTR(struct mt*, tmp);
210 }
211 7019 return rnd;
212 }
213  
214  
215 MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
216 PROTOTYPES: DISABLE
217  
218 INCLUDE: RdGen.xs.inc
219  
220 MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
221 PROTOTYPES: DISABLE
222  
223 AV*
224 resample(sample)
225 AV* sample
226 PREINIT:
227 I32 nelem;
228 7010 double* csample = NULL;
229 7010 double* destsample = NULL;
230 struct mt* rnd;
231 CODE:
232 7010 rnd = get_rnd(aTHX);
233 7010 avToCAry(aTHX_ sample, &csample, &nelem);
234 7010 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 7009 1 7010
235 7009 Newx(destsample, nelem, double);
236 7009 do_resample(csample, nelem, rnd, destsample);
237 7009 cAryToAV(aTHX_ destsample, &RETVAL, nelem);
238 7009 Safefree(destsample);
239 }
240 else
241 1 RETVAL = newAV();
242 7010 Safefree(csample);
243 7010 sv_2mortal((SV*)RETVAL);
244 OUTPUT: RETVAL
245  
246  
247 AV*
248 resample_medians(sample, runs)
249 AV* sample
250 I32 runs
251 PREINIT:
252 I32 nelem;
253 I32 i_run;
254 4 double* csample = NULL;
255 4 double* destsample = NULL;
256 struct mt* rnd;
257 CODE:
258 4 ▶ if (runs < 1 || runs > 100000000)
Branch: 2 branches
BranchTrueFalseTotal
if (runs < 1 || runs > 100000000) 2 2 4
if (runs < 1 || runs > 100000000) 0 2 2
259 2 croak("Number of resample runs must be between 1 and 100000000");
260 2 rnd = get_rnd(aTHX);
261 2 avToCAry(aTHX_ sample, &csample, &nelem);
262 2 RETVAL = newAV();
263 2 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 1 1 2
264 1 Newx(destsample, nelem, double);
265 1 av_extend(RETVAL, runs-1);
266 31 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 30 1 31
267 30 do_resample(csample, nelem, rnd, destsample);
268 30 av_store(RETVAL, i_run, newSVnv(cs_median(destsample, nelem)));
269 }
270 1 Safefree(destsample);
271 }
272 2 Safefree(csample);
273 2 sv_2mortal((SV*)RETVAL);
274 OUTPUT: RETVAL
275  
276  
277 AV*
278 resample_means(sample, runs)
279 AV* sample
280 I32 runs
281 PREINIT:
282 I32 nelem;
283 I32 i_run;
284 4 double* csample = NULL;
285 4 double* destsample = NULL;
286 struct mt* rnd;
287 CODE:
288 4 ▶ if (runs < 1 || runs > 100000000)
Branch: 2 branches
BranchTrueFalseTotal
if (runs < 1 || runs > 100000000) 2 2 4
if (runs < 1 || runs > 100000000) 0 2 2
289 2 croak("Number of resample runs must be between 1 and 100000000");
290 2 rnd = get_rnd(aTHX);
291 2 avToCAry(aTHX_ sample, &csample, &nelem);
292 2 RETVAL = newAV();
293 2 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 1 1 2
294 1 Newx(destsample, nelem, double);
295 1 av_extend(RETVAL, runs-1);
296 31 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 30 1 31
297 30 do_resample(csample, nelem, rnd, destsample);
298 30 av_store(RETVAL, i_run, newSVnv(cs_mean(destsample, nelem)));
299 }
300 1 Safefree(destsample);
301 }
302 2 Safefree(csample);
303 2 sv_2mortal((SV*)RETVAL);
304 OUTPUT: RETVAL
305  
306  
307 double
308 median(sample)
309 AV* sample
310 PREINIT:
311 I32 nelem;
312 34 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 34 0 34
313 CODE:
314 34 avToCAry(aTHX_ sample, &csample, &nelem);
315 34 ▶ if (nelem == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) 1 33 34
316 1 RETVAL = 0.;
317 else
318 33 RETVAL = cs_median(csample, nelem);
319 34 Safefree(csample);
320 OUTPUT: RETVAL
321  
322 double
323 median_absolute_deviation(sample)
324 AV* sample
325 PREINIT:
326 I32 nelem;
327 10 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 10 0 10
328 CODE:
329 10 avToCAry(aTHX_ sample, &csample, &nelem);
330 10 ▶ if (nelem == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) 1 9 10
331 1 RETVAL = 0.;
332 else {
333 I32 i;
334 double* absdev;
335  
336 9 const double median = cs_median(csample, nelem);
337 /* in principle, I think one could write an algorithm
338 * that doesn't require mallocing the second array by inlining an
339 * O(n) median that visits each element only once? */
340 9 Newx(absdev, nelem, double);
341 56 ▶ for (i = 0; i < nelem; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nelem; ++i) 47 9 56
342 47 absdev[i] = fabs(csample[i] - median);
343 9 RETVAL = cs_median(absdev, nelem);
344 9 Safefree(absdev);
345 }
346 10 Safefree(csample);
347 OUTPUT: RETVAL
348  
349  
350 double
351 first_quartile(sample)
352 AV* sample
353 PREINIT:
354 I32 nelem;
355 11 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 11 0 11
356 CODE:
357 11 avToCAry(aTHX_ sample, &csample, &nelem);
358 11 ▶ if (nelem == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) 1 10 11
359 1 RETVAL = 0.;
360 else
361 10 RETVAL = cs_first_quartile(csample, nelem);
362 11 Safefree(csample);
363 OUTPUT: RETVAL
364  
365  
366 double
367 third_quartile(sample)
368 AV* sample
369 PREINIT:
370 I32 nelem;
371 11 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 11 0 11
372 CODE:
373 11 avToCAry(aTHX_ sample, &csample, &nelem);
374 11 ▶ if (nelem == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) 1 10 11
375 1 RETVAL = 0.;
376 else
377 10 RETVAL = cs_third_quartile(csample, nelem);
378 11 Safefree(csample);
379 OUTPUT: RETVAL
380  
381  
382 double
383 mean(sample)
384 AV* sample
385 CODE:
386 6 RETVAL = cs_mean_av(aTHX_ sample);
387 OUTPUT: RETVAL
388  
389  
390 double
391 sample_standard_deviation(...)
392 PREINIT:
393 14 AV* sample = NULL;
394 14 ▶ double mean_val = 0.;
Branch: 1 branch
BranchTrueFalseTotal
double mean_val = 0.; 14 0 14
395 I32 n;
396 CODE:
397 14 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 8 6 14
398 8 SV* arg = ST(0);
399 8 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 7 1 8
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 7 7
400 1 croak("sample_standard_deviation: argument must be an array reference");
401 7 sample = (AV*)SvRV(arg);
402 7 n = av_len(sample)+1;
403 7 ▶ if (n < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) 1 6 7
404 1 croak("Sample standard deviation requires at least 2 data points");
405 6 mean_val = cs_mean_av(aTHX_ sample);
406 }
407 6 ▶ else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 2) { 5 1 6
408 5 SV* arg = ST(1);
409 5 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 5 0 5
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 5 5
410 0 croak("sample_standard_deviation: second argument must be an array reference");
411 5 mean_val = SvNV(ST(0));
412 5 sample = (AV*)SvRV(arg);
413 5 n = av_len(sample)+1;
414 5 ▶ if (n < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) 2 3 5
415 2 croak("Sample standard deviation requires at least 2 data points");
416 }
417 else {
418 1 croak("Usage: sample_standard_deviation([$mean,] \\@sample)");
419 }
420 9 RETVAL = cs_stddev_av(aTHX_ mean_val, sample, 1);
421 OUTPUT: RETVAL
422  
423  
424 double
425 population_standard_deviation(...)
426 PREINIT:
427 6 AV* sample = NULL;
428 6 ▶ double mean_val = 0.;
Branch: 1 branch
BranchTrueFalseTotal
double mean_val = 0.; 6 0 6
429 I32 n;
430 CODE:
431 6 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 3 3 6
432 3 SV* arg = ST(0);
433 3 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 3 0 3
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 3 3
434 0 croak("population_standard_deviation: argument must be an array reference");
435 3 sample = (AV*)SvRV(arg);
436 3 n = av_len(sample)+1;
437 3 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 2 3
438 1 croak("Population standard deviation requires at least 1 data point");
439 2 mean_val = cs_mean_av(aTHX_ sample);
440 }
441 3 ▶ else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 2) { 3 0 3
442 3 SV* arg = ST(1);
443 3 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 3 0 3
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 3 3
444 0 croak("population_standard_deviation: second argument must be an array reference");
445 3 mean_val = SvNV(ST(0));
446 3 sample = (AV*)SvRV(arg);
447 3 n = av_len(sample)+1;
448 3 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 2 3
449 1 croak("Population standard deviation requires at least 1 data point");
450 }
451 else {
452 0 croak("Usage: population_standard_deviation([$mean,] \\@sample)");
453 }
454 4 RETVAL = cs_stddev_av(aTHX_ mean_val, sample, 0);
455 OUTPUT: RETVAL
456  
457  
458 double
459 quantile(sample, p)
460 AV* sample
461 double p
462 PREINIT:
463 I32 nelem;
464 27 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 27 0 27
465 CODE:
466 27 ▶ if (!(p >= 0. && p <= 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(p >= 0. && p <= 1.)) { 25 2 27
if (!(p >= 0. && p <= 1.)) { 1 24 25
467 3 croak("Quantile probability p must be in [0, 1]");
468 }
469 24 avToCAry(aTHX_ sample, &csample, &nelem);
470 24 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 23 24
471 1 Safefree(csample);
472 1 croak("Cannot compute quantile of empty sample");
473 }
474 23 RETVAL = cs_quantile(csample, nelem, p);
475 23 Safefree(csample);
476 OUTPUT: RETVAL
477  
478  
479 double
480 select_kth(sample, kth)
481 AV* sample
482 I32 kth
483 PREINIT:
484 I32 nelem;
485 26 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 26 0 26
486 CODE:
487 26 avToCAry(aTHX_ sample, &csample, &nelem);
488 26 ▶ if (kth < 1 || kth > nelem) {
Branch: 2 branches
BranchTrueFalseTotal
if (kth < 1 || kth > nelem) { 23 3 26
if (kth < 1 || kth > nelem) { 3 20 23
489 6 Safefree(csample);
490 6 croak("Can't select %ith smallest element from a list of %i elements", kth, nelem);
491 }
492 20 RETVAL = cs_select(csample, nelem, kth-1);
493 20 Safefree(csample);
494 OUTPUT: RETVAL
495  
496  
497 void
498 median_simple_confidence_limits(sample, confidence, ...)
499 AV* sample
500 double confidence
501 PREINIT:
502 /* "confidence" is 1-alpha */
503 I32 runs, nelem, i_run;
504 I32 lower_idx, upper_idx;
505 6 double *csample = NULL, *destsample = NULL, *medians = NULL;
506 struct mt* rnd;
507 6 double median = 0.;
508 6 double lower_ci = 0.;
509 6 double upper_ci = 0.;
510 double alpha;
511 INIT:
512 6 alpha = 1.-confidence;
513 PPCODE:
514 6 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 2 4 6
515 2 runs = 1000;
516 4 ▶ else if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) { 4 0 4
517 4 IV r = SvIV(ST(2));
518 4 ▶ if (r < 1 || r > 100000000)
Branch: 2 branches
BranchTrueFalseTotal
if (r < 1 || r > 100000000) 2 2 4
if (r < 1 || r > 100000000) 0 2 2
519 2 croak("Number of resample runs must be between 1 and 100000000");
520 2 runs = (I32)r;
521 }
522 else {
523 0 croak("Usage: ($lower, $median, $upper) = median_confidence_limits(\\@sample, $confidence, [$nruns]);");
524 }
525 4 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 3 1 4
if (!(confidence > 0. && confidence < 1.)) { 0 3 3
526 1 croak("Confidence level has to be in (0, 1)");
527 }
528 3 rnd = get_rnd(aTHX);
529 3 avToCAry(aTHX_ sample, &csample, &nelem);
530 3 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 2 1 3
531 2 median = cs_median(csample, nelem);
532 2 Newx(medians, runs, double);
533 2 Newx(destsample, nelem, double);
534 22 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 20 2 22
535 20 do_resample(csample, nelem, rnd, destsample);
536 20 medians[i_run] = cs_median(destsample, nelem);
537 }
538 2 Safefree(destsample);
539 /* lower = t - (t*_((R+1)*(1-alpha)) - t)
540 * upper = t - (t*_((R+1)*alpha) - t)
541 */
542 2 lower_idx = (I32)((runs+1.)*(1.-alpha));
543 2 upper_idx = (I32)((runs+1.)*alpha);
544 2 ▶ if (lower_idx >= runs) lower_idx = runs - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (lower_idx >= runs) lower_idx = runs - 1; 1 1 2
545 2 ▶ if (lower_idx < 0) lower_idx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lower_idx < 0) lower_idx = 0; 0 2 2
546 2 ▶ if (upper_idx >= runs) upper_idx = runs - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (upper_idx >= runs) upper_idx = runs - 1; 1 1 2
547 2 ▶ if (upper_idx < 0) upper_idx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (upper_idx < 0) upper_idx = 0; 0 2 2
548 2 lower_ci = 2.*median - cs_select( medians, runs, (U32)lower_idx );
549 2 upper_ci = 2.*median - cs_select( medians, runs, (U32)upper_idx );
550 2 Safefree(medians);
551 }
552 3 Safefree(csample);
553 3 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 3 3
554 3 mPUSHn(lower_ci);
555 3 mPUSHn(median);
556 3 mPUSHn(upper_ci);
557  
558  
559 void
560 simple_confidence_limits_from_samples(statistic, statistics, confidence)
561 double statistic
562 AV* statistics
563 double confidence
564 PREINIT:
565 /* "confidence" is 1-alpha */
566 I32 nelem;
567 I32 lower_idx, upper_idx;
568 4 double *cstatistics = NULL;
569 4 double lower_ci = 0.;
570 4 double upper_ci = 0.;
571 double alpha;
572 INIT:
573 4 alpha = 1.-confidence;
574 PPCODE:
575 4 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 3 1 4
if (!(confidence > 0. && confidence < 1.)) { 0 3 3
576 1 croak("Confidence level has to be in (0, 1)");
577 }
578 3 avToCAry(aTHX_ statistics, &cstatistics, &nelem);
579 3 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 2 1 3
580 /* lower = t - (t*_((R+1)*(1-alpha)) - t)
581 * upper = t - (t*_((R+1)*alpha) - t)
582 */
583 2 lower_idx = (I32)((nelem+1.)*(1.-alpha));
584 2 upper_idx = (I32)((nelem+1.)*alpha);
585 2 ▶ if (lower_idx >= nelem) lower_idx = nelem - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (lower_idx >= nelem) lower_idx = nelem - 1; 1 1 2
586 2 ▶ if (lower_idx < 0) lower_idx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lower_idx < 0) lower_idx = 0; 0 2 2
587 2 ▶ if (upper_idx >= nelem) upper_idx = nelem - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (upper_idx >= nelem) upper_idx = nelem - 1; 1 1 2
588 2 ▶ if (upper_idx < 0) upper_idx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (upper_idx < 0) upper_idx = 0; 0 2 2
589 2 lower_ci = 2.*statistic - cs_select( cstatistics, nelem, (U32)lower_idx );
590 2 upper_ci = 2.*statistic - cs_select( cstatistics, nelem, (U32)upper_idx );
591 }
592 3 Safefree(cstatistics);
593 3 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 3 3
594 3 mPUSHn(lower_ci);
595 3 mPUSHn(statistic);
596 3 mPUSHn(upper_ci);
597  
598  
599 void
600 basic_confidence_limits_from_samples(statistic, statistics, confidence)
601 double statistic
602 AV* statistics
603 double confidence
604 PREINIT:
605 I32 nelem;
606 4 double *cstatistics = NULL;
607 4 double lower_ci = 0.;
608 4 double upper_ci = 0.;
609 double alpha, q_lo, q_hi;
610 INIT:
611 4 alpha = 1. - confidence;
612 PPCODE:
613 4 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 3 1 4
if (!(confidence > 0. && confidence < 1.)) { 0 3 3
614 1 croak("Confidence level has to be in (0, 1)");
615 }
616 3 avToCAry(aTHX_ statistics, &cstatistics, &nelem);
617 3 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 2 3
618 1 Safefree(cstatistics);
619 1 croak("Cannot compute confidence limits from empty sample");
620 }
621 2 q_hi = cs_quantile(cstatistics, nelem, 1. - 0.5 * alpha);
622 2 q_lo = cs_quantile(cstatistics, nelem, 0.5 * alpha);
623 2 lower_ci = 2. * statistic - q_hi;
624 2 upper_ci = 2. * statistic - q_lo;
625 2 Safefree(cstatistics);
626 2 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 2 2
627 2 mPUSHn(lower_ci);
628 2 mPUSHn(statistic);
629 2 mPUSHn(upper_ci);
630  
631  
632 void
633 percentile_confidence_limits_from_samples(statistic, statistics, confidence)
634 double statistic
635 AV* statistics
636 double confidence
637 PREINIT:
638 I32 nelem;
639 5 double *cstatistics = NULL;
640 5 double lower_ci = 0.;
641 5 double upper_ci = 0.;
642 double alpha;
643 INIT:
644 5 alpha = 1. - confidence;
645 PPCODE:
646 5 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 4 1 5
if (!(confidence > 0. && confidence < 1.)) { 0 4 4
647 1 croak("Confidence level has to be in (0, 1)");
648 }
649 4 avToCAry(aTHX_ statistics, &cstatistics, &nelem);
650 4 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 3 4
651 1 Safefree(cstatistics);
652 1 croak("Cannot compute confidence limits from empty sample");
653 }
654 3 lower_ci = cs_quantile(cstatistics, nelem, 0.5 * alpha);
655 3 upper_ci = cs_quantile(cstatistics, nelem, 1. - 0.5 * alpha);
656 3 Safefree(cstatistics);
657 3 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 3 3
658 3 mPUSHn(lower_ci);
659 3 mPUSHn(statistic);
660 3 mPUSHn(upper_ci);
661  
662  
663 void
664 bca_confidence_limits_from_samples(statistic, statistics, jackknife, confidence)
665 double statistic
666 AV* statistics
667 AV* jackknife
668 double confidence
669 PREINIT:
670 I32 n_boot, n_jack;
671 7 double *cboot = NULL, *cjack = NULL;
672 7 double lower_ci = 0.;
673 7 double upper_ci = 0.;
674 PPCODE:
675 7 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 6 1 7
if (!(confidence > 0. && confidence < 1.)) { 0 6 6
676 1 croak("Confidence level has to be in (0, 1)");
677 }
678 6 ▶ if (av_len(statistics) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(statistics) < 0) { 1 5 6
679 1 croak("Cannot compute BCa confidence limits from empty bootstrap sample");
680 }
681 5 ▶ if (av_len(jackknife) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(jackknife) < 0) { 1 4 5
682 1 croak("Cannot compute BCa confidence limits from empty jackknife sample");
683 }
684 4 avToCAry(aTHX_ statistics, &cboot, &n_boot);
685 4 avToCAry(aTHX_ jackknife, &cjack, &n_jack);
686 4 cs_bca_confidence_limits(statistic, cboot, n_boot, cjack, n_jack, confidence, &lower_ci, &upper_ci);
687 4 Safefree(cboot);
688 4 Safefree(cjack);
689 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
690 4 mPUSHn(lower_ci);
691 4 mPUSHn(statistic);
692 4 mPUSHn(upper_ci);
693  
694  
695 void
696 median_percentile_confidence_limits(sample, confidence, ...)
697 AV* sample
698 double confidence
699 PREINIT:
700 I32 runs, nelem, i_run;
701 4 double *csample = NULL, *destsample = NULL, *medians = NULL;
702 struct mt* rnd;
703 4 double median = 0.;
704 4 double lower_ci = 0.;
705 4 double upper_ci = 0.;
706 double alpha;
707 INIT:
708 4 alpha = 1. - confidence;
709 PPCODE:
710 4 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 2 2 4
711 2 runs = 1000;
712 2 ▶ else if (items == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) { 2 0 2
713 2 IV r = SvIV(ST(2));
714 2 ▶ if (r < 1 || r > 100000000)
Branch: 2 branches
BranchTrueFalseTotal
if (r < 1 || r > 100000000) 1 1 2
if (r < 1 || r > 100000000) 0 1 1
715 1 croak("Number of resample runs must be between 1 and 100000000");
716 1 runs = (I32)r;
717 }
718 else {
719 0 croak("Usage: ($lower, $median, $upper) = median_percentile_confidence_limits(\\@sample, $confidence, [$nruns]);");
720 }
721 3 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 2 1 3
if (!(confidence > 0. && confidence < 1.)) { 0 2 2
722 1 croak("Confidence level has to be in (0, 1)");
723 }
724 2 rnd = get_rnd(aTHX);
725 2 avToCAry(aTHX_ sample, &csample, &nelem);
726 2 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 1 2
727 1 Safefree(csample);
728 1 croak("Cannot compute confidence limits of empty sample");
729 }
730 1 median = cs_median_exact(csample, nelem);
731 1 Newx(medians, runs, double);
732 1 Newx(destsample, nelem, double);
733 2001 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 2000 1 2001
734 2000 do_resample(csample, nelem, rnd, destsample);
735 2000 medians[i_run] = cs_median_exact(destsample, nelem);
736 }
737 1 Safefree(destsample);
738 1 lower_ci = cs_quantile(medians, runs, 0.5 * alpha);
739 1 upper_ci = cs_quantile(medians, runs, 1. - 0.5 * alpha);
740 1 Safefree(medians);
741 1 Safefree(csample);
742 1 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 1 1
743 1 mPUSHn(lower_ci);
744 1 mPUSHn(median);
745 1 mPUSHn(upper_ci);
746  
747  
748 void
749 median_exact_confidence_limits(sample, confidence)
750 AV* sample
751 double confidence
752 PREINIT:
753 I32 nelem;
754 14 double *csample = NULL;
755 14 double median = 0.;
756 14 double lower_ci = 0.;
757 14 double upper_ci = 0.;
758 PPCODE:
759 14 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 13 1 14
if (!(confidence > 0. && confidence < 1.)) { 0 13 13
760 1 croak("Confidence level has to be in (0, 1)");
761 }
762 13 avToCAry(aTHX_ sample, &csample, &nelem);
763 13 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 12 13
764 1 Safefree(csample);
765 1 croak("Cannot compute confidence limits of empty sample");
766 }
767 12 cs_median_exact_confidence_limits(csample, nelem, confidence, &lower_ci, &median, &upper_ci);
768 12 Safefree(csample);
769 12 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 12 12
770 12 mPUSHn(lower_ci);
771 12 mPUSHn(median);
772 12 mPUSHn(upper_ci);
773  
774  
775 double
776 approx_erf(x)
777 double x
778 CODE:
779 11 RETVAL = cs_approx_erf(x);
780 OUTPUT: RETVAL
781  
782  
783 double
784 approx_erf_inv(x)
785 double x
786 CODE:
787 10 ▶ if (!(x > 0. && x < 1.))
Branch: 2 branches
BranchTrueFalseTotal
if (!(x > 0. && x < 1.)) 7 3 10
if (!(x > 0. && x < 1.)) 2 5 7
788 5 croak("The inverse error function is defined in (0,1). %f is outside that range", x);
789 5 RETVAL = cs_approx_erf_inv(x);
790 OUTPUT: RETVAL
791  
792  
793 double
794 alpha_to_nsigma(x)
795 double x
796 CODE:
797 7 RETVAL = cs_alpha_to_nsigma(x);
798 OUTPUT: RETVAL
799  
800  
801 double
802 nsigma_to_alpha(x)
803 double x
804 CODE:
805 10 RETVAL = cs_nsigma_to_alpha(x);
806 OUTPUT: RETVAL
807  
« _mt.c ↑ Summary