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