-
Notifications
You must be signed in to change notification settings - Fork 7
/
statistic_functions.h
484 lines (408 loc) · 11.5 KB
/
statistic_functions.h
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* BaitFisher (version 1.2.8) a program for designing DNA target enrichment baits
* BaitFilter (version 1.0.6) a program for selecting optimal bait regions
* Copyright 2013-2017 by Christoph Mayer
*
* This source file is part of the BaitFisher-package.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BaitFisher. If not, see <http://www.gnu.org/licenses/>.
*
*
* For any enquiries send an Email to Christoph Mayer
*
* When publishing work that is based on the results please cite:
* Mayer et al. 2016: BaitFisher: A software package for multi-species target DNA enrichment probe design
*
*/
#ifndef STATISTIC_FUNCTIONS_H
#define STATISTIC_FUNCTIONS_H
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <cctype>
#include <iterator>
#include <cstdlib>
#include <algorithm>
#include <climits>
#define EPSS 0.00000000001;
#define macro_min(x,y) ((x)<(y) ? (x) : (y))
#define macro_max(x,y) ((x)>(y) ? (x) : (y))
#define macro_mean_double(x,y) (((x)+(y))/2.0)
template<typename T>
size_t vec_mean_sd(const std::vector<T> &vec,
double &mean, double &sd)
{
size_t i, n=vec.size();
double sum_x = 0;
double sum_xx = 0;
if (n==0)
return 0;
for (i=0; i<n; ++i)
{
sum_x += (double)vec[i];
sum_xx += (double)vec[i]*vec[i];
}
if (n!=0)
mean = sum_x/(double)n;
else
mean = 0;
if (n>1)
sd = sqrt( (sum_xx-sum_x*mean)/(double)(n-1.0) );
else
sd = 0;
if (sd < 0.000001)
sd = 0;
return n;
}
template<typename T>
size_t vec_mean_sd(const std::vector<T> &vec,
double &mean, double &sd,
T &sum, T &sum_of_squares)
{
size_t i, n=vec.size();
T sum_x = 0;
T sum_xx = 0;
T elem;
if (n==0)
return 0;
for (i=0; i<n; ++i)
{
elem = vec[i];
sum_x += elem;
sum_xx += elem*elem;
}
mean = (double)sum_x/(double)n;
if (n>1)
sd = sqrt( (sum_xx-sum_x*mean)/(double)(n-1.0) );
else
sd = 0;
if (sd < 0.000001)
sd = 0;
sum = sum_x;
sum_of_squares = sum_xx;
return n;
}
// Coordinates conventions:
// The first coordinate is the index of the first element in the range as 0 based index.
// The second coordinate is the index of the last element in the range as 0 based index.
template<typename T>
void median_range(std::vector<T> &vec, size_t f, size_t l, double &median, size_t &index, bool &is_datum)
{
size_t s = f+l; // This is not the length or size of the range. It is the sum of the indices.
size_t m = s/2; // This is the index of the mid of the range
// In order to see that the following code is correct one should look at the following examples:
// vec: 0,1,2, Range: 0 to 2, which is the full vector. => s=2, m=1 which is the correct mid of the range. s is even so index=m=1, median is vec[m]=vec[1].
// vec: 0,1,2,3 Range: 1 to 3, => s=4, m=2, median = vec[2], index = 2
// vec: 0,1,2,3 Range: 3 to 3, => s=6, m=3, median = vec[3], index = 3
// vec: 0,1,2,3 Range: 0 to 3, => s=3, m=1, s is odd so median = (vec[1]+vec[2])/2, index = 1
// vec: 0,1,2,4,4 Range 1 to 4, => s=5, m=2, s is odd so median = (vec[2]+vec[3])/2, index = 2
// vec: 0,1,2,4,4 Range 2 to 4, => s=6, m=3, median = vec[3], index = 3
// vec: 0,1,2,4,4 Range 1 to 3, => s=4, m=2, median = vec[2], index = 2
// The following code simply relies on the fact that:
// (i) m is obviously the middle of the range, or left of the middle if the number of elements is even.
// (ii) s is even if the range has an odd number of elements and vice versa.
if (s%2 == 0)
{
median = vec[m];
index = m;
is_datum = true;
}
else
{
median = (vec[m]+vec[m+1])/2;
index = m;
is_datum = false;
}
}
// Method 2 routines:
// Prerequisite: Type T must be a type for which it makes sense to multiply it with a double to obtain a double.
// Compute quartiles:
// second quartile=median
// Q1 and Q3 are medians of the first part and second part, where the first and second part are obtained by dividing the data at the median.
template<typename T>
size_t vec_median_quartile_sort_method2(std::vector<T> &vec, double &Q1, double &Q2, double &Q3)
{
size_t n=vec.size();
size_t index, dummy;
bool is_datum;
std::sort(vec.begin(), vec.end());
median_range(vec, 0, n-1, Q2, index, is_datum);
if (is_datum)
{
median_range(vec, 0, index, Q1, dummy, is_datum);
median_range(vec, index, n-1, Q3, dummy, is_datum);
}
else
{
median_range(vec, 0, index, Q1, dummy, is_datum);
median_range(vec, index+1, n-1, Q3, dummy, is_datum);
}
return n;
}
template<typename T>
size_t vec_median_quartile_method2(std::vector<T> vec, double &Q1, double &Q2, double &Q3)
{
return vec_median_quartile_sort_method2(vec, Q1, Q2, Q3);
}
//
// Computes outlier bounds for a vector.
// Side effect: vec gets sorted. More efficient than copying the vector.
template<typename T>
size_t vec_median_quartile_outlier_bounds_sort_method2(std::vector<T> &vec, double &Q1, double &Q2, double &Q3, double &O_lower, double &O_upper)
{
int res = vec_median_quartile_sort_method2(vec, Q1, Q2, Q3);
double IQR = Q3 - Q1;
O_lower = Q1 - 1.5 * IQR;
O_upper = Q3 + 1.5 * IQR;
return res;
}
// Computes outlier bounds for a vector.
// Same vec_median_quartile_outlier_bounds_sort but without sorting the vector. Slighly slower, since the vector has to be copied.
template<typename T>
size_t vec_median_quartile_outlier_bounds_method2(std::vector<T> vec, double &Q1, double &Q2, double &Q3, double &O_lower, double &O_upper)
{
int res = vec_median_quartile_sort_method2(vec, Q1, Q2, Q3);
double IQR = Q3 - Q1;
O_lower = Q1 - 1.5 * IQR;
O_upper = Q3 + 1.5 * IQR;
return res;
}
template<typename T>
void vec_mark_outlier_mehod2(std::vector<T> &vec, std::vector<bool> &outlier)
{
outlier.clear();
double Q1, Q2, Q3, O_lower, O_upper;
vec_median_quartile_outlier_bounds_method2(vec, Q1, Q2, Q3, O_lower, O_upper);
unsigned i, N = vec.size();
outlier.reserve(N);
for (i=0; i<N; ++i)
{
if (vec[i] < O_lower || vec[i] > O_upper)
outlier.push_back(true);
else
outlier.push_back(false);
}
std::cout << "Qi: " << Q1 << " " << Q2 << " " << Q3 << std::endl;
std::cout << "Bounds: " << O_lower << " " << O_upper << std::endl;
}
// Method 3 routines:
// Prerequisite: Type T must be a type for which it makes sense to multiply it with a double to obtain a double.
// Compute quartiles: Averages of values at certain indices.
//
template<typename T>
size_t vec_median_quartile_sort_method3(std::vector<T> &vec, double &Q1, double &Q2, double &Q3)
{
size_t n=vec.size();
size_t m = n/4;
size_t r = n%4;
std::sort(vec.begin(), vec.end());
if (n==0)
return 0;
if (n==1)
{
Q1 = vec[0];
Q2 = vec[0];
Q3 = vec[0];
return 1;
}
if (r == 0)
{
Q1 = 0.5*vec[ m-1] + 0.5*vec[ m];
Q2 = 0.5*vec[2*m-1] + 0.5*vec[2*m];
Q3 = 0.5*vec[3*m-1] + 0.5*vec[3*m];
}
else if (r == 1)
{
// std::cerr << "m: " << m << std::endl;
Q1 = 0.25*vec[ m-1] + 0.75*vec[ m];
Q2 = vec[2*m];
Q3 = 0.75*vec[ 3*m] + 0.25*vec[3*m+1];
}
else if (r == 2)
{
// std::cerr << "m: " << m << std::endl;
Q1 = vec[m];
Q2 = 0.50*vec[2*m] + 0.50*vec[2*m+1];
Q3 = vec[3*m+1];
}
else // if (r == 3)
{
// std::cerr << "m: " << m << std::endl;
Q1 = 0.75*vec[ m] + 0.25*vec[ m+1];
Q2 = vec[2*m+1];
Q3 = 0.25*vec[3*m+1] + 0.75*vec[3*m+2];
}
return n;
}
template<typename T>
size_t vec_median_quartile_method3(std::vector<T> vec, double &Q1, double &Q2, double &Q3)
{
return vec_median_quartile_sort_method3(vec, Q1, Q2, Q3);
}
// Computes outlier bounds for a vector.
// Side effect: vec gets sorted. More efficient than copying the vector.
template<typename T>
size_t vec_median_quartile_outlier_bounds_sort_method3(std::vector<T> &vec, double &Q1, double &Q2, double &Q3, double &O_lower, double &O_upper)
{
int res = vec_median_quartile_sort_method3(vec, Q1, Q2, Q3);
double IQR = Q3 - Q1;
O_lower = Q1 - 1.5 * IQR;
O_upper = Q3 + 1.5 * IQR;
return res;
}
// Computes outlier bounds for a vector.
// Same vec_median_quartile_outlier_bounds_sort but without sorting the vector. Slighly slower, since the vector has to be copied.
template<typename T>
size_t vec_median_quartile_outlier_bounds_method3(std::vector<T> vec, double &Q1, double &Q2, double &Q3, double &O_lower, double &O_upper)
{
int res = vec_median_quartile_sort_method3(vec, Q1, Q2, Q3);
double IQR = Q3 - Q1;
O_lower = Q1 - 1.5 * IQR;
O_upper = Q3 + 1.5 * IQR;
return res;
}
template<typename T>
void vec_mark_outlier_mehod3(std::vector<T> &vec, std::vector<bool> &outlier)
{
outlier.clear();
double Q1, Q2, Q3, O_lower, O_upper;
vec_median_quartile_outlier_bounds_method3(vec, Q1, Q2, Q3, O_lower, O_upper);
unsigned i, N = vec.size();
outlier.reserve(N);
for (i=0; i<N; ++i)
{
if (vec[i] < O_lower || vec[i] > O_upper)
outlier.push_back(true);
else
outlier.push_back(false);
}
std::cout << "Qi: " << Q1 << " " << Q2 << " " << Q3 << std::endl;
std::cout << "Bounds: " << O_lower << " " << O_upper << std::endl;
}
template<typename T>
size_t vec_min_max(const std::vector<T> &vec, T &min, T &max)
{
size_t i, n=vec.size();
if (n==0)
return 0;
max = min = vec[0];
for (i=0; i<n; ++i)
{
if (vec[i] < min)
min = vec[i];
if (vec[i] > max)
max = vec[i];
}
return n;
}
// Determines the mean and sd for a range.
template <typename T>
size_t range_mean_sd(T it_beg,
T it_end,
double &mean, double &sd)
{
double sum_x = 0;
double sum_xx = 0;
size_t n=0;
if (it_beg == it_end)
{
return 0;
}
while (it_end != it_beg)
{
sum_x += (double)*it_beg;
sum_xx += (double)*it_beg* *it_beg;
++it_beg;
++n;
}
mean = sum_x/(double)n;
if (n>1)
sd = sqrt( (sum_xx-sum_x*mean)/(double)(n-1.0) );
else // if (n==1)
sd = 0;
if (sd < 0.000001)
sd = 0;
return n;
}
template<typename T>
size_t range_min_max(T it_beg,
T it_end,
double &min, double &max)
{
size_t n=0;
if (it_beg == it_end)
return 0;
max = min = *it_beg;
while (it_end != it_beg)
{
if (*it_beg< min)
min = *it_beg;
if (*it_beg > max)
max = *it_beg;
++n;
++it_beg;
}
return n;
}
template <typename T>
void vec_sum_sum_squares(std::vector<T> v, T& sum, T&sum_squares)
{
sum=0;
sum_squares=0;
unsigned i, n=v.size();
for (i=0; i<n; ++i)
{
sum += v[i];
sum_squares += v[i]*v[i];
}
}
// More functions: Single result as return value, using pointers to specify ranges.
template <typename T>
T minimum(T*b, T*e)
{
T m;
if (b>=e)
return INT_MIN;
m = *b;
++b;
while (b!=e)
{
if (*b < m)
{
m = *b;
}
++b;
}
return m;
}
template <typename T>
T maximum(T*b, T*e)
{
T m;
if (b>=e)
return INT_MIN;
m = *b;
++b;
while (b!=e)
{
if (*b > m)
{
m = *b;
}
++b;
}
return m;
}
#endif