-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistic_functions.h
More file actions
492 lines (417 loc) · 12.2 KB
/
statistic_functions.h
File metadata and controls
492 lines (417 loc) · 12.2 KB
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
485
486
487
488
489
490
491
492
/***************************************************************************************************
* The PolyMoSim project is distributed under the following license:
*
* Copyright (c) 2006-2025, Christoph Mayer, Leibniz Institute for the Analysis of Biodiversity Change,
* Bonn, Germany
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code (complete or in parts) must retain
* the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or any use of this software
* e.g. in publications must display the following acknowledgement:
* This product includes software developed by Christoph Mayer, Forschungsmuseum
* Alexander Koenig, Bonn, Germany.
* 4. Neither the name of the organization nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY CHRISTOPH MAYER ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTHOLDER OR ITS ORGANISATION BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* IMPORTANT (needs to be included, if code is redistributed):
* Please not that this license is not compatible with the GNU Public License (GPL)
* due to paragraph 3 in the copyright. It is not allowed under any
* circumstances to use the code of this software in projects distributed under the GPL.
* Furthermore, it is not allowed to redistribute the code in projects which are
* distributed under a license which is incompatible with one of the 4 paragraphs above.
*
* This project makes use of code coming from other projects. What follows is a complete
* list of files which make use of external code. Please refer to the copyright within
* these files.
*
* Files in tclap foler: Copyright (c) 2003 Michael E. Smoot
* See copyright in tclap/COPYRIGHT file for details.
* discrete_gamma.c: Copyright 1993-2004 by Ziheng Yang.
* See copyright in this file for details.
* CRandom.h: Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura
* See copyright in this file for details.
***************************************************************************************************/
#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;
}
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;
size_t m = s/2;
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:
// First quartile, second quartile=median, and third quartile
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:
// First quartile, second quartile=median, and third quartile
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