-
Notifications
You must be signed in to change notification settings - Fork 1
/
CaseResampling.xs
440 lines (389 loc) · 9.37 KB
/
CaseResampling.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#define PERL_NO_GET_CONTEXT /* we want efficiency */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "mt.h"
#include "stats.h"
#include <stdlib.h>
typedef struct mt * Statistics__CaseResampling__RdGen;
void*
U32ArrayPtr (pTHX_ int n)
{
SV * sv = sv_2mortal( NEWSV( 0, n*sizeof(U32) ) );
return SvPVX(sv);
}
double
cs_mean_av(pTHX_ AV* sample)
{
I32 i, n;
SV** elem;
double sum;
n = av_len(sample)+1;
sum = 0.;
for (i = 0; i < n; ++i) {
if (NULL == (elem = av_fetch(sample, i, 0))) {
croak("Could not fetch element from array");
}
else
sum += SvNV(*elem);
}
return sum/(double)n;
}
double
cs_sum_deviation_squared_av(pTHX_ double mean, AV* sample)
{
I32 i, n;
SV** elem;
double sum;
n = av_len(sample)+1;
sum = 0.;
for (i = 0; i < n; ++i) {
if (NULL == (elem = av_fetch(sample, i, 0))) {
croak("Could not fetch element from array");
}
else
sum += pow(SvNV(*elem)-mean, 2);
}
return sum;
}
void
avToCAry(pTHX_ AV* in, double** out, I32* n)
{
I32 thisN;
double* ary;
SV** elem;
I32 i;
thisN = av_len(in)+1;
*n = thisN;
if (thisN == 0)
return;
Newx(ary, thisN, double);
*out = ary;
for (i = 0; i < thisN; ++i) {
if (NULL == (elem = av_fetch(in, i, 0))) {
Safefree(ary);
croak("Could not fetch element from array");
}
else
ary[i] = SvNV(*elem);
}
}
void
cAryToAV(pTHX_ double* in, AV** out, I32 n)
{
SV* elem;
I32 i;
*out = newAV();
if (n == 0)
return;
av_extend(*out, n-1);
for (i = 0; i < n; ++i) {
elem = newSVnv(in[i]);
if (NULL == av_store(*out, i, elem))
SvREFCNT_dec(elem);
}
}
struct mt*
get_rnd(pTHX)
{
IV tmp;
SV* therndsv = get_sv("Statistics::CaseResampling::Rnd", 0);
if (therndsv == NULL
|| !SvROK(therndsv)
|| !sv_derived_from(therndsv, "Statistics::CaseResampling::RdGen"))
{
croak("Random number generator not set up!");
}
tmp = SvIV((SV*)SvRV(therndsv));
return INT2PTR(struct mt*, tmp);
}
MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
PROTOTYPES: DISABLE
INCLUDE: RdGen.xs.inc
MODULE = Statistics::CaseResampling PACKAGE = Statistics::CaseResampling
PROTOTYPES: DISABLE
AV*
resample(sample)
AV* sample
PREINIT:
I32 nelem;
double* csample;
double* destsample;
struct mt* rnd;
CODE:
rnd = get_rnd(aTHX);
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem != 0) {
Newx(destsample, nelem, double);
do_resample(csample, nelem, rnd, destsample);
cAryToAV(aTHX_ destsample, &RETVAL, nelem);
Safefree(destsample);
}
else
RETVAL = newAV();
Safefree(csample);
sv_2mortal((SV*)RETVAL);
OUTPUT: RETVAL
AV*
resample_medians(sample, runs)
AV* sample
I32 runs
PREINIT:
I32 nelem;
I32 i_run;
double* csample;
double* destsample;
struct mt* rnd;
CODE:
rnd = get_rnd(aTHX);
avToCAry(aTHX_ sample, &csample, &nelem);
RETVAL = newAV();
if (nelem != 0) {
Newx(destsample, nelem, double);
av_extend(RETVAL, runs-1);
for (i_run = 0; i_run < runs; ++i_run) {
do_resample(csample, nelem, rnd, destsample);
av_store(RETVAL, i_run, newSVnv(cs_median(destsample, nelem)));
}
Safefree(destsample);
}
Safefree(csample);
sv_2mortal((SV*)RETVAL);
OUTPUT: RETVAL
AV*
resample_means(sample, runs)
AV* sample
I32 runs
PREINIT:
I32 nelem;
I32 i_run;
double* csample;
double* destsample;
struct mt* rnd;
CODE:
rnd = get_rnd(aTHX);
avToCAry(aTHX_ sample, &csample, &nelem);
RETVAL = newAV();
if (nelem != 0) {
Newx(destsample, nelem, double);
av_extend(RETVAL, runs-1);
for (i_run = 0; i_run < runs; ++i_run) {
do_resample(csample, nelem, rnd, destsample);
av_store(RETVAL, i_run, newSVnv(cs_mean(destsample, nelem)));
}
Safefree(destsample);
}
Safefree(csample);
sv_2mortal((SV*)RETVAL);
OUTPUT: RETVAL
double
median(sample)
AV* sample
PREINIT:
I32 nelem;
double* csample;
CODE:
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem == 0)
RETVAL = 0.;
else
RETVAL = cs_median(csample, nelem);
Safefree(csample);
OUTPUT: RETVAL
double
median_absolute_deviation(sample)
AV* sample
PREINIT:
I32 nelem;
double* csample;
CODE:
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem == 0)
RETVAL = 0.;
else {
unsigned int i;
double* absdev;
const double median = cs_median(csample, nelem);
/* in principle, I think one could write an algorithm
* that doesn't require mallocing the second array by inlining an
* O(n) median that visits each element only once? */
absdev = (double*)malloc(nelem * sizeof(double));
for (i = 0; i < nelem; ++i)
absdev[i] = fabs(csample[i] - median);
RETVAL = cs_median(absdev, nelem);
free(absdev);
}
Safefree(csample);
OUTPUT: RETVAL
double
first_quartile(sample)
AV* sample
PREINIT:
I32 nelem;
double* csample;
CODE:
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem == 0)
RETVAL = 0.;
else
RETVAL = cs_first_quartile(csample, nelem);
Safefree(csample);
OUTPUT: RETVAL
double
third_quartile(sample)
AV* sample
PREINIT:
I32 nelem;
double* csample;
CODE:
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem == 0)
RETVAL = 0.;
else
RETVAL = cs_third_quartile(csample, nelem);
Safefree(csample);
OUTPUT: RETVAL
double
mean(sample)
AV* sample
CODE:
RETVAL = cs_mean_av(aTHX_ sample);
OUTPUT: RETVAL
double
sample_standard_deviation(mean, sample)
SV* mean
AV* sample
CODE:
RETVAL = cs_sum_deviation_squared_av(aTHX_ SvNV(mean), sample);
RETVAL = pow( RETVAL / av_len(sample), 0.5 ); /* av_len() is N-1! */
OUTPUT: RETVAL
double
population_standard_deviation(mean, sample)
SV* mean
AV* sample
CODE:
RETVAL = cs_sum_deviation_squared_av(aTHX_ SvNV(mean), sample);
RETVAL = pow( RETVAL / (av_len(sample)+1), 0.5 ); /* av_len() is N-1! */
OUTPUT: RETVAL
double
select_kth(sample, kth)
AV* sample
I32 kth
PREINIT:
I32 nelem;
double* csample;
CODE:
avToCAry(aTHX_ sample, &csample, &nelem);
if (kth < 1 || kth > nelem) {
croak("Can't select %ith smallest element from a list of %i elements", kth, nelem);
}
RETVAL = cs_select(csample, nelem, kth-1);
Safefree(csample);
OUTPUT: RETVAL
void
median_simple_confidence_limits(sample, confidence...)
AV* sample
double confidence
PREINIT:
/* "confidence" is 1-alpha */
I32 runs, nelem, i_run;
double *csample, *destsample, *medians;
struct mt* rnd;
double median = 0.;
double lower_ci = 0.;
double upper_ci = 0.;
double alpha;
INIT:
alpha = 1.-confidence;
PPCODE:
if (items == 2)
runs = 1000;
else if (items == 3)
runs = SvUV(ST(2));
else {
croak("Usage: ($lower, $median, $upper) = median_confidence_limits(\\@sample, $confidence, [$nruns]);");
}
if (confidence <= 0. || confidence >= 1.) {
croak("Confidence level has to be in (0, 1)");
}
rnd = get_rnd(aTHX);
avToCAry(aTHX_ sample, &csample, &nelem);
if (nelem != 0) {
median = cs_median(csample, nelem);
Newx(medians, runs, double);
Newx(destsample, nelem, double);
for (i_run = 0; i_run < runs; ++i_run) {
do_resample(csample, nelem, rnd, destsample);
medians[i_run] = cs_median(destsample, nelem);
}
Safefree(destsample);
/* lower = t - (t*_((R+1)*(1-alpha)) - t)
* upper = t - (t*_((R+1)*alpha) - t)
*/
lower_ci = 2.*median - cs_select( medians, runs, (I32)((runs+1.)*(1.-alpha)) );
upper_ci = 2.*median - cs_select( medians, runs, (I32)((runs+1.)*alpha) );
Safefree(medians);
}
Safefree(csample);
EXTEND(SP, 3);
mPUSHn(lower_ci);
mPUSHn(median);
mPUSHn(upper_ci);
void
simple_confidence_limits_from_samples(statistic, statistics, confidence)
double statistic
AV* statistics
double confidence
PREINIT:
/* "confidence" is 1-alpha */
I32 nelem;
double *cstatistics;
double lower_ci = 0.;
double upper_ci = 0.;
double alpha;
INIT:
alpha = 1.-confidence;
PPCODE:
if (confidence <= 0. || confidence >= 1.) {
croak("Confidence level has to be in (0, 1)");
}
avToCAry(aTHX_ statistics, &cstatistics, &nelem);
if (nelem != 0) {
/* lower = t - (t*_((R+1)*(1-alpha)) - t)
* upper = t - (t*_((R+1)*alpha) - t)
*/
lower_ci = 2.*statistic - cs_select( cstatistics, nelem, (I32)((nelem+1.)*(1.-alpha)) );
upper_ci = 2.*statistic - cs_select( cstatistics, nelem, (I32)((nelem+1.)*alpha) );
}
Safefree(cstatistics);
EXTEND(SP, 3);
mPUSHn(lower_ci);
mPUSHn(statistic);
mPUSHn(upper_ci);
double
approx_erf(x)
double x
CODE:
RETVAL = cs_approx_erf(x);
OUTPUT: RETVAL
double
approx_erf_inv(x)
double x
CODE:
if (x <= 0. || x >= 1.)
croak("The inverse error function is defined in (0,1). %f is outside that range", x);
RETVAL = cs_approx_erf_inv(x);
OUTPUT: RETVAL
double
alpha_to_nsigma(x)
double x
CODE:
RETVAL = cs_alpha_to_nsigma(x);
OUTPUT: RETVAL
double
nsigma_to_alpha(x)
double x
CODE:
RETVAL = cs_nsigma_to_alpha(x);
OUTPUT: RETVAL