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