CaseResampling.xs

filter: Statementstmt 95.8% 536 / 559 Branchbran 77.8% 274 / 352 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.9% 810 / 911 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 16 U32ArrayPtr (pTHX_ int n)
18 {
19 SV * sv;
20 16 ▶ if (n <= 0 || n > 10000000)
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || n > 10000000) 16 0 16
if (n <= 0 || n > 10000000) 0 16 16
21 0 croak("Invalid array size for U32ArrayPtr");
22 16 sv = sv_2mortal( NEWSV( 0, n*sizeof(U32) ) );
23 16 ▶ SvGROW(sv, n*sizeof(U32));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, n*sizeof(U32)); 16 0 16
SvGROW(sv, n*sizeof(U32)); 0 16 16
24 16 return SvPVX(sv);
25 }
26  
27 static int
28 197584 cs_sv_to_double(pTHX_ SV* sv, double* out)
29 {
30 197584 ▶ if (sv == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (sv == NULL) 0 197584 197584
31 0 return 0;
32 197584 SvGETMAGIC(sv);
33 197584 ▶ if (!SvOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) 36 197548 197584
34 36 return 0;
35 197548 ▶ if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv))
Branch: 6 branches
BranchTrueFalseTotal
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 130152 67396 197548
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 90 130062 130152
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 6 84 90
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 0 6 6
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 0 0 0
if (!SvIOK(sv) && !SvNOK(sv) && !SvAMAGIC(sv) && !looks_like_number(sv)) 84 6 90
36 84 return -1;
37 197464 *out = SvNV(sv);
38 197464 return 1;
39 }
40  
41 static double
42 4151 cs_require_double(pTHX_ SV* sv, const char* name)
43 {
44 4151 double val = 0.;
45 4151 int ok = cs_sv_to_double(aTHX_ sv, &val);
46 4151 ▶ if (ok == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok == 0) 29 4122 4151
47 29 croak("%s must be defined", name);
48 4122 ▶ if (ok < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok < 0) 58 4064 4122
49 58 croak("%s must be numeric", name);
50 4064 return val;
51 }
52  
53 static I32
54 1660 cs_require_runs(pTHX_ SV* sv)
55 {
56 1660 double d = cs_require_double(aTHX_ sv, "Number of resample runs");
57 1636 ▶ if (!CS_ISFINITE(d) || d < 1. || d > 100000000.)
Branch: 4 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(d) || d < 1. || d > 100000000.) 1636 0 1636
if (!CS_ISFINITE(d) || d < 1. || d > 100000000.) 1636 0 1636
if (!CS_ISFINITE(d) || d < 1. || d > 100000000.) 1625 11 1636
if (!CS_ISFINITE(d) || d < 1. || d > 100000000.) 0 1625 1625
58 11 croak("Number of resample runs must be between 1 and 100000000");
59 1625 return (I32)d;
60 }
61  
62 double
63 234 cs_mean_av(pTHX_ AV* sample)
64 {
65 I32 i, n;
66 SV** elem;
67 234 double sum = 0.;
68 234 double c = 0.;
69 234 int has_nonfinite = 0;
70  
71 234 n = av_len(sample)+1;
72 234 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 1 233 234
73 1 croak("Cannot compute mean of empty sample");
74  
75 7333 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 7115 218 7333
76 double x, t;
77 int ok;
78 7115 ▶ if (NULL == (elem = av_fetch(sample, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(sample, i, 0))) { 0 7115 7115
79 0 croak("Could not fetch element from array");
80 }
81 7115 ok = cs_sv_to_double(aTHX_ *elem, &x);
82 7115 ▶ if (ok == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok == 0) 3 7112 7115
83 3 croak("Undefined value in sample");
84 7112 ▶ if (ok < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok < 0) 12 7100 7112
85 12 croak("Non-numeric value in sample");
86 7100 ▶ if (!CS_ISFINITE(x))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(x)) 7098 2 7100
if (!CS_ISFINITE(x)) 2 7096 7098
87 4 has_nonfinite = 1;
88 7100 t = sum + x;
89 7100 ▶ if (CS_ISFINITE(t)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(t)) { 7096 4 7100
if (CS_ISFINITE(t)) { 7091 5 7096
90 7091 ▶ if (fabs(sum) >= fabs(x))
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(sum) >= fabs(x)) 5700 1391 7091
91 5700 c += (sum - t) + x;
92 else
93 1391 c += (x - t) + sum;
94 }
95 7100 sum = t;
96 }
97  
98 218 ▶ if (CS_ISFINITE(sum)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(sum)) { 216 2 218
if (CS_ISFINITE(sum)) { 213 3 216
99 213 double total = sum + c;
100 213 ▶ if (CS_ISFINITE(total))
Branch: 2 branches
BranchTrueFalseTotal
if (CS_ISFINITE(total)) 213 0 213
if (CS_ISFINITE(total)) 213 0 213
101 213 return total / (double)n;
102 }
103  
104 5 ▶ if (has_nonfinite)
Branch: 1 branch
BranchTrueFalseTotal
if (has_nonfinite) 4 1 5
105 4 return sum / (double)n;
106  
107 /* All inputs were finite, but sum overflowed: use scaled incremental mean */
108 {
109 1 double m = 0.;
110 3 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 2 1 3
111 2 double d = (double)(i + 1);
112 double x;
113 2 elem = av_fetch(sample, i, 0);
114 2 cs_sv_to_double(aTHX_ *elem, &x);
115 2 m = m * ((double)i / d) + x / d;
116 }
117 1 return m;
118 }
119 }
120  
121 double
122 13 cs_stddev_av(pTHX_ double mean, AV* sample, int is_sample)
123 {
124 I32 i, n;
125 SV** elem;
126 13 double scale = 0.;
127 13 double ssq = 1.;
128 13 int has_inf = 0;
129  
130 13 n = av_len(sample)+1;
131 13 ▶ if (n <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) 0 13 13
132 0 return 0.;
133 13 ▶ if (CS_ISNAN(mean))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(mean)) 1 12 13
134 1 return mean;
135 12 ▶ if (!CS_ISFINITE(mean))
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(mean)) 12 0 12
if (!CS_ISFINITE(mean)) 1 11 12
136 1 has_inf = 1;
137  
138 66 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 55 11 66
139 double val, d, ad;
140 int ok;
141 55 ▶ if (NULL == (elem = av_fetch(sample, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(sample, i, 0))) { 0 55 55
142 0 croak("Could not fetch element from array");
143 }
144 55 ok = cs_sv_to_double(aTHX_ *elem, &val);
145 55 ▶ if (ok == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok == 0) 0 55 55
146 0 croak("Undefined value in sample");
147 55 ▶ if (ok < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok < 0) 0 55 55
148 0 croak("Non-numeric value in sample");
149 55 ▶ if (CS_ISNAN(val))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(val)) 0 55 55
150 1 return val;
151 55 d = val - mean;
152 55 ▶ if (CS_ISNAN(d))
Branch: 1 branch
BranchTrueFalseTotal
if (CS_ISNAN(d)) 1 54 55
153 1 return d;
154 54 ▶ if (!CS_ISFINITE(d)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(d)) { 54 0 54
if (!CS_ISFINITE(d)) { 3 51 54
155 3 has_inf = 1;
156 3 continue;
157 }
158 51 ad = fabs(d);
159 51 ▶ if (ad > 0.) {
Branch: 1 branch
BranchTrueFalseTotal
if (ad > 0.) { 38 13 51
160 38 ▶ if (scale < ad) {
Branch: 1 branch
BranchTrueFalseTotal
if (scale < ad) { 12 26 38
161 12 double r = scale / ad;
162 12 ssq = 1. + ssq * r * r;
163 12 scale = ad;
164 }
165 else {
166 26 double r = ad / scale;
167 26 ssq += r * r;
168 }
169 }
170 }
171  
172 11 ▶ if (has_inf)
Branch: 1 branch
BranchTrueFalseTotal
if (has_inf) 1 10 11
173 1 return HUGE_VAL;
174 10 ▶ if (scale == 0.)
Branch: 1 branch
BranchTrueFalseTotal
if (scale == 0.) 3 7 10
175 3 return 0.;
176 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
177 }
178  
179 void
180 9427 avToCAry(pTHX_ AV* in, double** out, I32* n)
181 {
182 I32 thisN;
183 double* ary;
184 SV** elem;
185 I32 i;
186 9427 *out = NULL;
187 9427 thisN = av_len(in)+1;
188 9427 ▶ if (thisN <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (thisN <= 0) { 19 9408 9427
189 19 *n = 0;
190 19 return;
191 }
192 9408 *n = thisN;
193  
194 9408 Newx(ary, thisN, double);
195 9408 *out = ary;
196 195643 ▶ for (i = 0; i < thisN; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < thisN; ++i) { 186251 9392 195643
197 int ok;
198 186251 ▶ if (NULL == (elem = av_fetch(in, i, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == (elem = av_fetch(in, i, 0))) { 0 186251 186251
199 0 Safefree(ary);
200 0 *out = NULL;
201 0 croak("Could not fetch element from array");
202 }
203 186251 ok = cs_sv_to_double(aTHX_ *elem, &ary[i]);
204 186251 ▶ if (ok <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok <= 0) { 16 186235 186251
205 16 Safefree(ary);
206 16 *out = NULL;
207 16 ▶ croak(ok == 0 ? "Undefined value in sample" : "Non-numeric value in sample");
Branch: 1 branch
BranchTrueFalseTotal
croak(ok == 0 ? "Undefined value in sample" : "Non-numeric value in sample"); 3 13 16
208 }
209 }
210 }
211  
212 void
213 7009 cAryToAV(pTHX_ double* in, AV** out, I32 n)
214 {
215 SV* elem;
216 I32 i;
217 7009 *out = newAV();
218 7009 ▶ if (n == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) 0 7009 7009
219 0 return;
220  
221 7009 av_extend(*out, n-1);
222  
223 56720 ▶ for (i = 0; i < n; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; ++i) { 49711 7009 56720
224 49711 elem = newSVnv(in[i]);
225 49711 ▶ if (NULL == av_store(*out, i, elem))
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == av_store(*out, i, elem)) 0 49711 49711
226 0 SvREFCNT_dec(elem);
227 }
228 }
229  
230 struct mt*
231 8645 get_rnd(pTHX)
232 {
233 IV tmp;
234 struct mt* rnd;
235 8645 SV* therndsv = get_sv("Statistics::CaseResampling::Rnd", 0);
236 8645 ▶ if (therndsv == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (therndsv == NULL 8645 0 8645
237 8645 ▶ || !SvROK(therndsv)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(therndsv) 8645 0 8645
238 8645 ▶ || !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen")) 0 8645 8645
239 {
240 0 croak("Random number generator not set up!");
241 }
242 8645 tmp = SvIV((SV*)SvRV(therndsv));
243 8645 rnd = INT2PTR(struct mt*, tmp);
244 8645 ▶ 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()) { 8645 0 8645
if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) { 41 8604 8645
if (rnd != NULL && rnd->auto_seeded && rnd->pid != (long)PerlProc_getpid()) { 1 40 41
245 1 dSP;
246 1 ENTER;
247 1 SAVETMPS;
248 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
249 1 PUTBACK;
250 1 call_pv("Statistics::CaseResampling::_reseed_default_rng", G_VOID | G_DISCARD);
251 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 1 1
252 1 LEAVE;
253 1 therndsv = get_sv("Statistics::CaseResampling::Rnd", 0);
254 1 ▶ if (therndsv == NULL
Branch: 1 branch
BranchTrueFalseTotal
if (therndsv == NULL 1 0 1
255 1 ▶ || !SvROK(therndsv)
Branch: 1 branch
BranchTrueFalseTotal
|| !SvROK(therndsv) 1 0 1
256 1 ▶ || !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen")) 0 1 1
257 {
258 0 croak("Random number generator not set up!");
259 }
260 1 tmp = SvIV((SV*)SvRV(therndsv));
261 1 rnd = INT2PTR(struct mt*, tmp);
262 }
263 8645 return rnd;
264 }
265  
266  
267 MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
268 PROTOTYPES: DISABLE
269  
270 INCLUDE: RdGen.xs.inc
271  
272 MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
273 PROTOTYPES: DISABLE
274  
275 AV*
276 resample(sample)
277 AV* sample
278 PREINIT:
279 I32 nelem;
280 7015 double* csample = NULL;
281 7015 double* destsample = NULL;
282 struct mt* rnd;
283 CODE:
284 7015 rnd = get_rnd(aTHX);
285 7015 avToCAry(aTHX_ sample, &csample, &nelem);
286 7010 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 7009 1 7010
287 7009 Newx(destsample, nelem, double);
288 7009 do_resample(csample, nelem, rnd, destsample);
289 7009 cAryToAV(aTHX_ destsample, &RETVAL, nelem);
290 7009 Safefree(destsample);
291 }
292 else
293 1 RETVAL = newAV();
294 7010 Safefree(csample);
295 7010 sv_2mortal((SV*)RETVAL);
296 OUTPUT: RETVAL
297  
298  
299 AV*
300 resample_medians(sample, runs_sv)
301 AV* sample
302 SV* runs_sv
303 PREINIT:
304 I32 runs, nelem, i_run;
305 7 double* csample = NULL;
306 7 double* destsample = NULL;
307 struct mt* rnd;
308 CODE:
309 7 runs = cs_require_runs(aTHX_ runs_sv);
310 2 rnd = get_rnd(aTHX);
311 2 avToCAry(aTHX_ sample, &csample, &nelem);
312 2 RETVAL = newAV();
313 2 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 1 1 2
314 1 Newx(destsample, nelem, double);
315 1 av_extend(RETVAL, runs-1);
316 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
317 30 do_resample(csample, nelem, rnd, destsample);
318 30 av_store(RETVAL, i_run, newSVnv(cs_median(destsample, nelem)));
319 }
320 1 Safefree(destsample);
321 }
322 2 Safefree(csample);
323 2 sv_2mortal((SV*)RETVAL);
324 OUTPUT: RETVAL
325  
326  
327 AV*
328 resample_quantiles(sample, p_sv, runs_sv)
329 AV* sample
330 SV* p_sv
331 SV* runs_sv
332 PREINIT:
333 double p;
334 I32 runs, nelem, i_run;
335 12 double* csample = NULL;
336 12 double* destsample = NULL;
337 struct mt* rnd;
338 CODE:
339 12 p = cs_require_double(aTHX_ p_sv, "Quantile probability p");
340 9 ▶ if (!(p >= 0. && p <= 1.))
Branch: 2 branches
BranchTrueFalseTotal
if (!(p >= 0. && p <= 1.)) 7 2 9
if (!(p >= 0. && p <= 1.)) 1 6 7
341 3 croak("Quantile probability p must be in [0, 1]");
342 6 runs = cs_require_runs(aTHX_ runs_sv);
343 2 rnd = get_rnd(aTHX);
344 2 avToCAry(aTHX_ sample, &csample, &nelem);
345 2 RETVAL = newAV();
346 2 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 1 1 2
347 1 Newx(destsample, nelem, double);
348 1 av_extend(RETVAL, runs-1);
349 51 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 50 1 51
350 50 do_resample(csample, nelem, rnd, destsample);
351 50 av_store(RETVAL, i_run, newSVnv(cs_quantile(destsample, nelem, p)));
352 }
353 1 Safefree(destsample);
354 }
355 2 Safefree(csample);
356 2 sv_2mortal((SV*)RETVAL);
357 OUTPUT: RETVAL
358  
359  
360 AV*
361 resample_means(sample, runs_sv)
362 AV* sample
363 SV* runs_sv
364 PREINIT:
365 I32 runs, nelem, i_run;
366 208 double* csample = NULL;
367 208 double* destsample = NULL;
368 struct mt* rnd;
369 CODE:
370 208 runs = cs_require_runs(aTHX_ runs_sv);
371 203 rnd = get_rnd(aTHX);
372 203 avToCAry(aTHX_ sample, &csample, &nelem);
373 203 RETVAL = newAV();
374 203 ▶ if (nelem != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem != 0) { 202 1 203
375 202 Newx(destsample, nelem, double);
376 202 av_extend(RETVAL, runs-1);
377 60732 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 60530 202 60732
378 60530 do_resample(csample, nelem, rnd, destsample);
379 60530 av_store(RETVAL, i_run, newSVnv(cs_mean(destsample, nelem)));
380 }
381 202 Safefree(destsample);
382 }
383 203 Safefree(csample);
384 203 sv_2mortal((SV*)RETVAL);
385 OUTPUT: RETVAL
386  
387  
388 double
389 median(sample)
390 AV* sample
391 PREINIT:
392 I32 nelem;
393 44 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 44 0 44
394 CODE:
395 44 avToCAry(aTHX_ sample, &csample, &nelem);
396 39 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 38 39
397 1 Safefree(csample);
398 1 croak("Cannot compute median of empty sample");
399 }
400 38 RETVAL = cs_median(csample, nelem);
401 38 Safefree(csample);
402 OUTPUT: RETVAL
403  
404 double
405 median_absolute_deviation(sample)
406 AV* sample
407 PREINIT:
408 I32 nelem;
409 12 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 12 0 12
410 CODE:
411 12 avToCAry(aTHX_ sample, &csample, &nelem);
412 12 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 11 12
413 1 Safefree(csample);
414 1 croak("Cannot compute median absolute deviation of empty sample");
415 }
416 {
417 I32 i;
418 double* absdev;
419  
420 11 const double median = cs_median(csample, nelem);
421 /* in principle, I think one could write an algorithm
422 * that doesn't require mallocing the second array by inlining an
423 * O(n) median that visits each element only once? */
424 11 Newx(absdev, nelem, double);
425 64 ▶ for (i = 0; i < nelem; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nelem; ++i) 53 11 64
426 53 absdev[i] = fabs(csample[i] - median);
427 11 RETVAL = cs_median(absdev, nelem);
428 11 Safefree(absdev);
429 }
430 11 Safefree(csample);
431 OUTPUT: RETVAL
432  
433  
434 double
435 first_quartile(sample)
436 AV* sample
437 PREINIT:
438 I32 nelem;
439 14 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 14 0 14
440 CODE:
441 14 avToCAry(aTHX_ sample, &csample, &nelem);
442 14 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 13 14
443 1 Safefree(csample);
444 1 croak("Cannot compute first quartile of empty sample");
445 }
446 13 RETVAL = cs_first_quartile(csample, nelem);
447 13 Safefree(csample);
448 OUTPUT: RETVAL
449  
450  
451 double
452 third_quartile(sample)
453 AV* sample
454 PREINIT:
455 I32 nelem;
456 14 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 14 0 14
457 CODE:
458 14 avToCAry(aTHX_ sample, &csample, &nelem);
459 14 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 13 14
460 1 Safefree(csample);
461 1 croak("Cannot compute third quartile of empty sample");
462 }
463 13 RETVAL = cs_third_quartile(csample, nelem);
464 13 Safefree(csample);
465 OUTPUT: RETVAL
466  
467  
468 double
469 mean(sample)
470 AV* sample
471 CODE:
472 216 RETVAL = cs_mean_av(aTHX_ sample);
473 OUTPUT: RETVAL
474  
475  
476 double
477 sample_standard_deviation(...)
478 PREINIT:
479 20 AV* sample = NULL;
480 20 ▶ double mean_val = 0.;
Branch: 1 branch
BranchTrueFalseTotal
double mean_val = 0.; 20 0 20
481 I32 n;
482 CODE:
483 20 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 13 7 20
484 13 SV* arg = ST(0);
485 13 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 12 1 13
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 12 12
486 1 croak("sample_standard_deviation: argument must be an array reference");
487 12 sample = (AV*)SvRV(arg);
488 12 n = av_len(sample)+1;
489 12 ▶ if (n < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) 1 11 12
490 1 croak("Sample standard deviation requires at least 2 data points");
491 11 mean_val = cs_mean_av(aTHX_ sample);
492 }
493 7 ▶ else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 2) { 6 1 7
494 6 SV* arg = ST(1);
495 int ok;
496 6 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 6 0 6
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 6 6
497 0 croak("sample_standard_deviation: second argument must be an array reference");
498 6 ok = cs_sv_to_double(aTHX_ ST(0), &mean_val);
499 6 ▶ if (ok <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok <= 0) 1 5 6
500 1 ▶ croak(ok == 0 ? "Undefined mean value" : "Non-numeric mean value");
Branch: 1 branch
BranchTrueFalseTotal
croak(ok == 0 ? "Undefined mean value" : "Non-numeric mean value"); 0 1 1
501 5 sample = (AV*)SvRV(arg);
502 5 n = av_len(sample)+1;
503 5 ▶ if (n < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 2) 2 3 5
504 2 croak("Sample standard deviation requires at least 2 data points");
505 }
506 else {
507 1 croak("Usage: sample_standard_deviation([$mean,] \\@sample)");
508 }
509 9 RETVAL = cs_stddev_av(aTHX_ mean_val, sample, 1);
510 OUTPUT: RETVAL
511  
512  
513 double
514 population_standard_deviation(...)
515 PREINIT:
516 12 AV* sample = NULL;
517 12 ▶ double mean_val = 0.;
Branch: 1 branch
BranchTrueFalseTotal
double mean_val = 0.; 12 0 12
518 I32 n;
519 CODE:
520 12 ▶ if (items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items == 1) { 8 4 12
521 8 SV* arg = ST(0);
522 8 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 8 0 8
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 8 8
523 0 croak("population_standard_deviation: argument must be an array reference");
524 8 sample = (AV*)SvRV(arg);
525 8 n = av_len(sample)+1;
526 8 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 7 8
527 1 croak("Population standard deviation requires at least 1 data point");
528 7 mean_val = cs_mean_av(aTHX_ sample);
529 }
530 4 ▶ else if (items == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 2) { 4 0 4
531 4 SV* arg = ST(1);
532 int ok;
533 4 ▶ if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 4 0 4
if (!SvROK(arg) || SvTYPE(SvRV(arg)) != SVt_PVAV) 0 4 4
534 0 croak("population_standard_deviation: second argument must be an array reference");
535 4 ok = cs_sv_to_double(aTHX_ ST(0), &mean_val);
536 4 ▶ if (ok <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ok <= 0) 1 3 4
537 1 ▶ croak(ok == 0 ? "Undefined mean value" : "Non-numeric mean value");
Branch: 1 branch
BranchTrueFalseTotal
croak(ok == 0 ? "Undefined mean value" : "Non-numeric mean value"); 1 0 1
538 3 sample = (AV*)SvRV(arg);
539 3 n = av_len(sample)+1;
540 3 ▶ if (n < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 1) 1 2 3
541 1 croak("Population standard deviation requires at least 1 data point");
542 }
543 else {
544 0 croak("Usage: population_standard_deviation([$mean,] \\@sample)");
545 }
546 4 RETVAL = cs_stddev_av(aTHX_ mean_val, sample, 0);
547 OUTPUT: RETVAL
548  
549  
550 double
551 quantile(sample, p_sv)
552 AV* sample
553 SV* p_sv
554 PREINIT:
555 I32 nelem;
556 double p;
557 36 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 36 0 36
558 CODE:
559 36 p = cs_require_double(aTHX_ p_sv, "Quantile probability p");
560 33 ▶ if (!(p >= 0. && p <= 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(p >= 0. && p <= 1.)) { 31 2 33
if (!(p >= 0. && p <= 1.)) { 1 30 31
561 3 croak("Quantile probability p must be in [0, 1]");
562 }
563 30 avToCAry(aTHX_ sample, &csample, &nelem);
564 25 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 24 25
565 1 Safefree(csample);
566 1 croak("Cannot compute quantile of empty sample");
567 }
568 24 RETVAL = cs_quantile(csample, nelem, p);
569 24 Safefree(csample);
570 OUTPUT: RETVAL
571  
572  
573 double
574 select_kth(sample, kth_sv)
575 AV* sample
576 SV* kth_sv
577 PREINIT:
578 I32 nelem;
579 I32 kth;
580 double kth_d;
581 35 ▶ double* csample = NULL;
Branch: 1 branch
BranchTrueFalseTotal
double* csample = NULL; 35 0 35
582 CODE:
583 35 kth_d = cs_require_double(aTHX_ kth_sv, "kth");
584 32 avToCAry(aTHX_ sample, &csample, &nelem);
585 32 ▶ if (!CS_ISFINITE(kth_d) || kth_d < 1. || kth_d > (double)nelem) {
Branch: 4 branches
BranchTrueFalseTotal
if (!CS_ISFINITE(kth_d) || kth_d < 1. || kth_d > (double)nelem) { 32 0 32
if (!CS_ISFINITE(kth_d) || kth_d < 1. || kth_d > (double)nelem) { 32 0 32
if (!CS_ISFINITE(kth_d) || kth_d < 1. || kth_d > (double)nelem) { 29 3 32
if (!CS_ISFINITE(kth_d) || kth_d < 1. || kth_d > (double)nelem) { 3 26 29
586 6 Safefree(csample);
587 6 ▶ kth = CS_ISFINITE(kth_d) ? (I32)kth_d : 0;
Branch: 2 branches
BranchTrueFalseTotal
kth = CS_ISFINITE(kth_d) ? (I32)kth_d : 0; 6 0 6
kth = CS_ISFINITE(kth_d) ? (I32)kth_d : 0; 6 0 6
588 6 croak("Can't select %ith smallest element from a list of %i elements", kth, nelem);
589 }
590 26 kth = (I32)kth_d;
591 26 RETVAL = cs_select(csample, nelem, kth-1);
592 26 Safefree(csample);
593 OUTPUT: RETVAL
594  
595  
596 void
597 median_simple_confidence_limits(sample, confidence_sv, ...)
598 AV* sample
599 SV* confidence_sv
600 PREINIT:
601 I32 runs, nelem, i_run;
602 14 double *csample = NULL, *destsample = NULL, *medians = NULL;
603 struct mt* rnd;
604 double confidence;
605 14 double median = 0.;
606 14 double lower_ci = 0.;
607 14 double upper_ci = 0.;
608 double alpha, q_lo, q_hi;
609 PPCODE:
610 14 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 5 9 14
611 5 runs = 1000;
612 9 ▶ else if (items == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) 9 0 9
613 9 runs = cs_require_runs(aTHX_ ST(2));
614 else {
615 0 croak("Usage: ($lower, $median, $upper) = median_simple_confidence_limits(\\@sample, $confidence, [$nruns]);");
616 }
617 9 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
618 6 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 5 1 6
if (!(confidence > 0. && confidence < 1.)) { 0 5 5
619 1 croak("Confidence level has to be in (0, 1)");
620 }
621 5 alpha = 1. - confidence;
622 5 rnd = get_rnd(aTHX);
623 5 avToCAry(aTHX_ sample, &csample, &nelem);
624 5 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 4 5
625 1 Safefree(csample);
626 1 croak("Cannot compute confidence limits of empty sample");
627 }
628 4 median = cs_median(csample, nelem);
629 4 Newx(medians, runs, double);
630 4 Newx(destsample, nelem, double);
631 424 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 420 4 424
632 420 do_resample(csample, nelem, rnd, destsample);
633 420 medians[i_run] = cs_median(destsample, nelem);
634 }
635 4 Safefree(destsample);
636 4 q_hi = cs_quantile(medians, runs, 1. - 0.5 * alpha);
637 4 q_lo = cs_quantile(medians, runs, 0.5 * alpha);
638 4 lower_ci = 2.*median - q_hi;
639 4 upper_ci = 2.*median - q_lo;
640 4 Safefree(medians);
641 4 Safefree(csample);
642 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
643 4 mPUSHn(lower_ci);
644 4 mPUSHn(median);
645 4 mPUSHn(upper_ci);
646  
647  
648 void
649 simple_confidence_limits_from_samples(statistic_sv, statistics, confidence_sv)
650 SV* statistic_sv
651 AV* statistics
652 SV* confidence_sv
653 ALIAS:
654 basic_confidence_limits_from_samples = 1
655 PREINIT:
656 I32 nelem;
657 222 double *cstatistics = NULL;
658 double statistic, confidence;
659 222 double lower_ci = 0.;
660 222 double upper_ci = 0.;
661 double alpha, q_lo, q_hi;
662 PPCODE:
663 PERL_UNUSED_VAR(ix);
664 222 statistic = cs_require_double(aTHX_ statistic_sv, "Statistic");
665 216 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
666 210 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 208 2 210
if (!(confidence > 0. && confidence < 1.)) { 0 208 208
667 2 croak("Confidence level has to be in (0, 1)");
668 }
669 208 alpha = 1. - confidence;
670 208 avToCAry(aTHX_ statistics, &cstatistics, &nelem);
671 208 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 2 206 208
672 2 Safefree(cstatistics);
673 2 croak("Cannot compute confidence limits from empty sample");
674 }
675 206 q_hi = cs_quantile(cstatistics, nelem, 1. - 0.5 * alpha);
676 206 q_lo = cs_quantile(cstatistics, nelem, 0.5 * alpha);
677 206 lower_ci = 2.*statistic - q_hi;
678 206 upper_ci = 2.*statistic - q_lo;
679 206 Safefree(cstatistics);
680 206 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 206 206
681 206 mPUSHn(lower_ci);
682 206 mPUSHn(statistic);
683 206 mPUSHn(upper_ci);
684  
685  
686 void
687 percentile_confidence_limits_from_samples(statistic_sv, statistics, confidence_sv)
688 SV* statistic_sv
689 AV* statistics
690 SV* confidence_sv
691 PREINIT:
692 I32 nelem;
693 12 double *cstatistics = NULL;
694 double statistic, confidence;
695 12 double lower_ci = 0.;
696 12 double upper_ci = 0.;
697 double alpha;
698 PPCODE:
699 12 statistic = cs_require_double(aTHX_ statistic_sv, "Statistic");
700 9 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
701 6 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 5 1 6
if (!(confidence > 0. && confidence < 1.)) { 0 5 5
702 1 croak("Confidence level has to be in (0, 1)");
703 }
704 5 alpha = 1. - confidence;
705 5 avToCAry(aTHX_ statistics, &cstatistics, &nelem);
706 5 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 4 5
707 1 Safefree(cstatistics);
708 1 croak("Cannot compute confidence limits from empty sample");
709 }
710 4 lower_ci = cs_quantile(cstatistics, nelem, 0.5 * alpha);
711 4 upper_ci = cs_quantile(cstatistics, nelem, 1. - 0.5 * alpha);
712 4 Safefree(cstatistics);
713 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
714 4 mPUSHn(lower_ci);
715 4 mPUSHn(statistic);
716 4 mPUSHn(upper_ci);
717  
718  
719 void
720 bca_confidence_limits_from_samples(statistic_sv, statistics, jackknife, confidence_sv)
721 SV* statistic_sv
722 AV* statistics
723 AV* jackknife
724 SV* confidence_sv
725 PREINIT:
726 I32 n_boot, n_jack;
727 14 double *cboot = NULL, *cjack = NULL;
728 double statistic, confidence;
729 14 double lower_ci = 0.;
730 14 double upper_ci = 0.;
731 PPCODE:
732 14 statistic = cs_require_double(aTHX_ statistic_sv, "Statistic");
733 11 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
734 8 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 7 1 8
if (!(confidence > 0. && confidence < 1.)) { 0 7 7
735 1 croak("Confidence level has to be in (0, 1)");
736 }
737 7 ▶ if (av_len(statistics) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(statistics) < 0) { 1 6 7
738 1 croak("Cannot compute BCa confidence limits from empty bootstrap sample");
739 }
740 6 ▶ if (av_len(jackknife) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(jackknife) < 0) { 1 5 6
741 1 croak("Cannot compute BCa confidence limits from empty jackknife sample");
742 }
743 5 ENTER;
744 5 avToCAry(aTHX_ statistics, &cboot, &n_boot);
745 5 SAVEFREEPV(cboot);
746 5 avToCAry(aTHX_ jackknife, &cjack, &n_jack);
747 4 SAVEFREEPV(cjack);
748 4 cs_bca_confidence_limits(statistic, cboot, n_boot, cjack, n_jack, confidence, &lower_ci, &upper_ci);
749 4 LEAVE;
750 4 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 4 4
751 4 mPUSHn(lower_ci);
752 4 mPUSHn(statistic);
753 4 mPUSHn(upper_ci);
754  
755  
756 void
757 median_percentile_confidence_limits(sample, confidence_sv, ...)
758 AV* sample
759 SV* confidence_sv
760 PREINIT:
761 I32 runs, nelem, i_run;
762 210 double *csample = NULL, *destsample = NULL, *medians = NULL;
763 struct mt* rnd;
764 double confidence;
765 210 double median = 0.;
766 210 double lower_ci = 0.;
767 210 double upper_ci = 0.;
768 double alpha;
769 PPCODE:
770 210 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 5 205 210
771 5 runs = 1000;
772 205 ▶ else if (items == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) 205 0 205
773 205 runs = cs_require_runs(aTHX_ ST(2));
774 else {
775 0 croak("Usage: ($lower, $median, $upper) = median_percentile_confidence_limits(\\@sample, $confidence, [$nruns]);");
776 }
777 206 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
778 203 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 202 1 203
if (!(confidence > 0. && confidence < 1.)) { 0 202 202
779 1 croak("Confidence level has to be in (0, 1)");
780 }
781 202 alpha = 1. - confidence;
782 202 rnd = get_rnd(aTHX);
783 202 avToCAry(aTHX_ sample, &csample, &nelem);
784 202 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 201 202
785 1 Safefree(csample);
786 1 croak("Cannot compute confidence limits of empty sample");
787 }
788 201 median = cs_median_exact(csample, nelem);
789 201 Newx(medians, runs, double);
790 201 Newx(destsample, nelem, double);
791 62201 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 62000 201 62201
792 62000 do_resample(csample, nelem, rnd, destsample);
793 62000 medians[i_run] = cs_median_exact(destsample, nelem);
794 }
795 201 Safefree(destsample);
796 201 lower_ci = cs_quantile(medians, runs, 0.5 * alpha);
797 201 upper_ci = cs_quantile(medians, runs, 1. - 0.5 * alpha);
798 201 Safefree(medians);
799 201 Safefree(csample);
800 201 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 201 201
801 201 mPUSHn(lower_ci);
802 201 mPUSHn(median);
803 201 mPUSHn(upper_ci);
804  
805  
806 void
807 median_exact_confidence_limits(sample, confidence_sv)
808 AV* sample
809 SV* confidence_sv
810 PREINIT:
811 I32 nelem;
812 417 double *csample = NULL;
813 double confidence;
814 417 double median = 0.;
815 417 double lower_ci = 0.;
816 417 double upper_ci = 0.;
817 PPCODE:
818 417 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
819 414 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 413 1 414
if (!(confidence > 0. && confidence < 1.)) { 0 413 413
820 1 croak("Confidence level has to be in (0, 1)");
821 }
822 413 avToCAry(aTHX_ sample, &csample, &nelem);
823 413 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 412 413
824 1 Safefree(csample);
825 1 croak("Cannot compute confidence limits of empty sample");
826 }
827 412 cs_median_exact_confidence_limits(csample, nelem, confidence, &lower_ci, &median, &upper_ci);
828 412 Safefree(csample);
829 412 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 412 412
830 412 mPUSHn(lower_ci);
831 412 mPUSHn(median);
832 412 mPUSHn(upper_ci);
833  
834  
835 void
836 mean_percentile_confidence_limits(sample, confidence_sv, ...)
837 AV* sample
838 SV* confidence_sv
839 PREINIT:
840 I32 runs, nelem, i_run;
841 413 double *csample = NULL, *destsample = NULL, *means = NULL;
842 struct mt* rnd;
843 double confidence;
844 413 double mean_val = 0.;
845 413 double lower_ci = 0.;
846 413 double upper_ci = 0.;
847 double alpha;
848 PPCODE:
849 413 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 5 408 413
850 5 runs = 1000;
851 408 ▶ else if (items == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) 408 0 408
852 408 runs = cs_require_runs(aTHX_ ST(2));
853 else {
854 0 croak("Usage: ($lower, $mean, $upper) = mean_percentile_confidence_limits(\\@sample, $confidence, [$nruns]);");
855 }
856 409 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
857 406 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 405 1 406
if (!(confidence > 0. && confidence < 1.)) { 0 405 405
858 1 croak("Confidence level has to be in (0, 1)");
859 }
860 405 alpha = 1. - confidence;
861 405 rnd = get_rnd(aTHX);
862 405 avToCAry(aTHX_ sample, &csample, &nelem);
863 405 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 404 405
864 1 Safefree(csample);
865 1 croak("Cannot compute confidence limits of empty sample");
866 }
867 404 mean_val = cs_mean(csample, nelem);
868 404 Newx(means, runs, double);
869 404 Newx(destsample, nelem, double);
870 121374 ▶ for (i_run = 0; i_run < runs; ++i_run) {
Branch: 1 branch
BranchTrueFalseTotal
for (i_run = 0; i_run < runs; ++i_run) { 120970 404 121374
871 120970 do_resample(csample, nelem, rnd, destsample);
872 120970 means[i_run] = cs_mean(destsample, nelem);
873 }
874 404 Safefree(destsample);
875 404 lower_ci = cs_quantile(means, runs, 0.5 * alpha);
876 404 upper_ci = cs_quantile(means, runs, 1. - 0.5 * alpha);
877 404 Safefree(means);
878 404 Safefree(csample);
879 404 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 404 404
880 404 mPUSHn(lower_ci);
881 404 mPUSHn(mean_val);
882 404 mPUSHn(upper_ci);
883  
884  
885 void
886 mean_bca_confidence_limits(sample, confidence_sv, ...)
887 AV* sample
888 SV* confidence_sv
889 PREINIT:
890 I32 runs, nelem;
891 413 double *csample = NULL;
892 struct mt* rnd;
893 double confidence;
894 413 double mean_val = 0.;
895 413 double lower_ci = 0.;
896 413 double upper_ci = 0.;
897 PPCODE:
898 413 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 5 408 413
899 5 runs = 1000;
900 408 ▶ else if (items == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) 408 0 408
901 408 runs = cs_require_runs(aTHX_ ST(2));
902 else {
903 0 croak("Usage: ($lower, $mean, $upper) = mean_bca_confidence_limits(\\@sample, $confidence, [$nruns]);");
904 }
905 409 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
906 406 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 405 1 406
if (!(confidence > 0. && confidence < 1.)) { 0 405 405
907 1 croak("Confidence level has to be in (0, 1)");
908 }
909 405 rnd = get_rnd(aTHX);
910 405 avToCAry(aTHX_ sample, &csample, &nelem);
911 405 ▶ if (nelem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem == 0) { 1 404 405
912 1 Safefree(csample);
913 1 croak("Cannot compute confidence limits of empty sample");
914 }
915 404 cs_mean_bca_confidence_limits(csample, nelem, confidence, runs, rnd, &lower_ci, &mean_val, &upper_ci);
916 404 Safefree(csample);
917 404 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 404 404
918 404 mPUSHn(lower_ci);
919 404 mPUSHn(mean_val);
920 404 mPUSHn(upper_ci);
921  
922  
923 void
924 mean_studentized_confidence_limits(sample, confidence_sv, ...)
925 AV* sample
926 SV* confidence_sv
927 PREINIT:
928 I32 runs, nelem;
929 414 double *csample = NULL;
930 struct mt* rnd;
931 double confidence;
932 414 double mean_val = 0.;
933 414 double lower_ci = 0.;
934 414 double upper_ci = 0.;
935 PPCODE:
936 414 ▶ if (items == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (items == 2) 5 409 414
937 5 runs = 1000;
938 409 ▶ else if (items == 3)
Branch: 1 branch
BranchTrueFalseTotal
else if (items == 3) 409 0 409
939 409 runs = cs_require_runs(aTHX_ ST(2));
940 else {
941 0 croak("Usage: ($lower, $mean, $upper) = mean_studentized_confidence_limits(\\@sample, $confidence, [$nruns]);");
942 }
943 410 confidence = cs_require_double(aTHX_ confidence_sv, "Confidence level");
944 407 ▶ if (!(confidence > 0. && confidence < 1.)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(confidence > 0. && confidence < 1.)) { 406 1 407
if (!(confidence > 0. && confidence < 1.)) { 0 406 406
945 1 croak("Confidence level has to be in (0, 1)");
946 }
947 406 rnd = get_rnd(aTHX);
948 406 avToCAry(aTHX_ sample, &csample, &nelem);
949 406 ▶ if (nelem < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (nelem < 2) { 2 404 406
950 2 Safefree(csample);
951 2 croak("Studentized bootstrap confidence limits require at least 2 data points");
952 }
953 404 cs_mean_studentized_confidence_limits(csample, nelem, confidence, runs, rnd, &lower_ci, &mean_val, &upper_ci);
954 404 Safefree(csample);
955 404 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 404 404
956 404 mPUSHn(lower_ci);
957 404 mPUSHn(mean_val);
958 404 mPUSHn(upper_ci);
959  
960  
961 double
962 approx_erf(x_sv)
963 SV* x_sv
964 CODE:
965 14 RETVAL = cs_approx_erf(cs_require_double(aTHX_ x_sv, "x"));
966 OUTPUT: RETVAL
967  
968  
969 double
970 approx_erf_inv(x_sv)
971 SV* x_sv
972 PREINIT:
973 double x;
974 CODE:
975 19 x = cs_require_double(aTHX_ x_sv, "x");
976 16 ▶ if (!(x > -1. && x < 1.))
Branch: 2 branches
BranchTrueFalseTotal
if (!(x > -1. && x < 1.)) 13 3 16
if (!(x > -1. && x < 1.)) 2 11 13
977 5 croak("The inverse error function is defined in (-1, 1). %f is outside that range", x);
978 11 RETVAL = cs_approx_erf_inv(x);
979 OUTPUT: RETVAL
980  
981  
982 double
983 alpha_to_nsigma(x_sv)
984 SV* x_sv
985 PREINIT:
986 double x;
987 CODE:
988 15 x = cs_require_double(aTHX_ x_sv, "alpha");
989 12 ▶ if (!(x > 0. && x <= 1.))
Branch: 2 branches
BranchTrueFalseTotal
if (!(x > 0. && x <= 1.)) 9 3 12
if (!(x > 0. && x <= 1.)) 1 8 9
990 4 croak("alpha must be in (0, 1]; got %f", x);
991 8 RETVAL = cs_alpha_to_nsigma(x);
992 OUTPUT: RETVAL
993  
994  
995 double
996 nsigma_to_alpha(x_sv)
997 SV* x_sv
998 PREINIT:
999 double x;
1000 CODE:
1001 16 x = cs_require_double(aTHX_ x_sv, "nsigma");
1002 13 ▶ if (!(x >= 0.))
Branch: 1 branch
BranchTrueFalseTotal
if (!(x >= 0.)) 2 11 13
1003 2 croak("nsigma must be >= 0; got %f", x);
1004 11 RETVAL = cs_nsigma_to_alpha(x);
1005 OUTPUT: RETVAL
1006  
« _mt.c ↑ Summary