File Coverage

_stats.c
Criterion Covered Total %
statement 64 70 91.4
branch 29 30 96.6
condition n/a
subroutine n/a
pod n/a
total 93 100 93.0


line stmt bran cond sub pod time code
1             #include "stats.h"
2             #include
3              
4             #ifndef M_PI
5             # define M_PI 3.14159265
6             #endif
7              
8             #define SWAP(a,b) tmp=(a);(a)=(b);(b)=tmp;
9              
10             double
11 69           cs_select(double* sample, I32 n, U32 k) {
12             U32 i, ir, j, l, mid;
13             double a, tmp;
14              
15 69           l = 0;
16 69           ir = n-1;
17             while(1) {
18 170 100         if (ir <= l+1) {
19 69 100         if (ir == l+1 && sample[ir] < sample[l]) {
    100          
20 7           SWAP(sample[l], sample[ir]);
21             }
22 69           return sample[k];
23             }
24             else {
25 101           mid = (l+ir) >> 1;
26 101           SWAP(sample[mid], sample[l+1]);
27 101 100         if (sample[l] > sample[ir]) {
28 40           SWAP(sample[l], sample[ir]);
29             }
30 101 100         if (sample[l+1] > sample[ir]) {
31 19           SWAP(sample[l+1], sample[ir]);
32             }
33 101 100         if (sample[l] > sample[l+1]) {
34 26           SWAP(sample[l], sample[l+1]);
35             }
36 101           i = l+1;
37 101           j = ir;
38 101           a = sample[l+1];
39 86           while(1) {
40 333 100         do i++; while (sample[i] < a);
41 310 100         do j--; while (sample[j] > a);
42 187 100         if (j < i)
43 101           break;
44 86           SWAP(sample[i], sample[j]);
45             }
46 101           sample[l+1] = sample[j];
47 101           sample[j] = a;
48 101 100         if (j >= k)
49 68           ir = j-1;
50 101 100         if (j <= k)
51 60           l = i;
52             }
53             }
54             }
55              
56             double
57 55           cs_median(double* sample, I32 n)
58             {
59 55           U32 k = n/2 - !(n & 1);
60 55           return cs_select(sample, n, k);
61             }
62              
63              
64             double
65 0           cs_first_quartile(double* sample, I32 n)
66             {
67 0           U32 k = (U32)((n/4) + 1);
68 0           return cs_select(sample, n, k);
69             }
70              
71              
72             double
73 0           cs_third_quartile(double* sample, I32 n)
74             {
75 0           U32 k = (U32)((n*3/4) + 1);
76 0           return cs_select(sample, n, k);
77             }
78              
79              
80             double
81 30           cs_mean(double* sample, I32 n)
82             {
83             I32 i;
84 30           double sum = 0.;
85 360 100         for (i = 0; i < n; ++i)
86 330           sum += sample[i];
87 30           return sum/(double)n;
88             }
89              
90             void
91 61           do_resample(double* original, I32 n, struct mt* rdgen, double* dest)
92             {
93             I32 rndElem;
94             I32 i;
95 732 100         for (i = 0; i < n; ++i) {
96 671           rndElem = (I32) (mt_genrand(rdgen) * n);
97 671           dest[i] = original[rndElem];
98             }
99 61           }
100              
101             /*
102             void
103             cs_sort(double arr[], I32 beg, I32 end)
104             {
105             double t;
106             if (end > beg + 1)
107             {
108             double piv = arr[beg];
109             I32 l = beg + 1, r = end;
110             while (l < r)
111             {
112             if (arr[l] <= piv)
113             l++;
114             else {
115             t = arr[l];
116             arr[l] = arr[--r];
117             arr[r] = t;
118             }
119             }
120             t = arr[--l];
121             arr[l] = arr[beg];
122             arr[beg] = t;
123             cs_sort(arr, beg, l);
124             cs_sort(arr, r, end);
125             }
126             }
127             */
128              
129             /*
130             double
131             cs_median(double* sample, I32 n)
132             {
133             cs_sort(sample, 0, n);
134             if (n & 1) {
135             return sample[n/2];
136             }
137             else {
138             return 0.5*(sample[n/2-1]+sample[n/2]);
139             }
140             }
141             */
142              
143              
144             double
145 11           cs_approx_erf(double x)
146             {
147             /*
148             * const double a = ( 8./(3.*M_PI) )
149             * * (M_PI - 3.) / (4. - M_PI);
150             */
151 11           const double a = 0.147; /* better than the ~0.140 above */
152 11           const double xsq = x*x;
153             return
154 11 100         (x < 0 ? -1. : 1.)
155 22           * sqrt(
156 11           1. - exp(
157 11           -xsq * (4./M_PI + a*xsq) / (1. + a*xsq)
158             )
159             );
160             }
161              
162              
163             double
164 6           cs_approx_erf_inv(double x)
165             {
166 6           const double a = 0.147; /* better than the ~0.140 above */
167 6           const double b = log(1. - x*x);
168             return
169 6 50         (x < 0 ? -1. : 1.)
170 12           * sqrt(
171 6           (-2./(M_PI * a))
172 6           - b/2.
173 6           + sqrt(
174 6           pow( 2./(M_PI*a) + b/2., 2. )
175 6           - b/a
176             )
177             );
178             }
179              
180              
181             double
182 3           cs_alpha_to_nsigma(double alpha)
183             {
184 3           return sqrt(2.) * cs_approx_erf_inv(1.-alpha);
185             }
186              
187              
188              
189             double
190 3           cs_nsigma_to_alpha(double nsigma)
191             {
192 3           return 1.-cs_approx_erf(nsigma/sqrt(2.));
193             }
194